Fermat
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
sah_builder.h
1 /*
2  * Copyright (c) 2010-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
32 #pragma once
33 
34 #include <nih/bvh/bvh.h>
35 #include <nih/linalg/vector.h>
36 #include <nih/linalg/bbox.h>
37 #include <nih/bintree/bintree_node.h>
38 #include <nih/bintree/cuda/bintree_gen_context.h>
39 #include <thrust/device_vector.h>
40 
41 namespace nih {
42 namespace cuda {
43 
44 namespace sah {
45 
46  struct Bin
47  {
48  float4 bmin;
49  float4 bmax;
50 
51  FORCE_INLINE NIH_HOST_DEVICE int32 get_size() const { return binary_cast<int32>( bmin.w ); }
52  FORCE_INLINE NIH_HOST_DEVICE void set_size(const int32 x) { bmin.w = binary_cast<float>(x); }
53  };
54  struct Bbox
55  {
56  float3 bmin;
57  float3 bmax;
58  };
59  struct Bins
60  {
61  float3* bmin;
62  float3* bmax;
63  int32* size;
64  };
65 
66 } // namespace sah
67 
72 struct Sah_builder
105 {
112  thrust::device_vector<Bvh_node>& nodes,
113  thrust::device_vector<uint2>& leaves,
114  thrust::device_vector<uint32>& index) :
115  m_nodes( &nodes ), m_leaves( &leaves ), m_index( &index )
116  {
117  m_sorting_time = 0.0f;
118  m_compression_time = 0.0f;
119  m_sah_split_time = 0.0f;
120  m_distribute_objects_time = 0.0f;
121  m_temp_storage = 0;
122  }
123 
132  template <typename Iterator>
133  void build(
134  const Bbox3f bbox,
135  const Iterator bbox_begin,
136  const Iterator bbox_end,
137  const uint32 max_leaf_size,
138  const float max_cost = 1.8f);
139 
140  thrust::device_vector<Bvh_node>* m_nodes;
141  thrust::device_vector<uint2>* m_leaves;
142  thrust::device_vector<uint32>* m_index;
143  uint32 m_levels[128];
144  uint32 m_level_count;
145  uint32 m_node_count;
146  uint32 m_leaf_count;
147 
148  float m_sorting_time;
149  float m_compression_time;
150  float m_sah_split_time;
151  float m_distribute_objects_time;
152  uint32 m_temp_storage;
153 
154 private:
155  void sort(
156  const uint32 n_objects,
157  thrust::device_vector<uint32>::iterator segment_ids,
158  thrust::device_vector<uint32>::iterator segment_keys,
159  thrust::device_vector<uint2>::iterator in_bounds,
160  thrust::device_vector<uint2>::iterator bounds,
161  thrust::device_vector<uint2>::iterator bounds_tmp,
162  thrust::device_vector<uint32>::iterator order,
163  uint32& n_active_objects,
164  uint32& n_segments);
165 
166  void eval_split_costs(
167  const uint32 n_objects,
168  const uint32 n_segments,
169  thrust::device_vector<uint32>::iterator segment_keys,
170  thrust::device_vector<uint32>::iterator segment_heads,
171  thrust::device_vector<uint2>::iterator bounds,
172  thrust::device_vector<uint2>::iterator bounds_l,
173  thrust::device_vector<uint2>::iterator bounds_r,
174  thrust::device_vector<float>::iterator split_costs,
175  thrust::device_vector<uint32>::iterator split_index);
176 
177  typedef sah::Bin Bin;
178  typedef sah::Bbox Bbox;
179  typedef sah::Bins Bins;
180 
181  thrust::device_vector<uint32> m_segment_heads;
182  thrust::device_vector<uint32> m_segment_keys;
183  thrust::device_vector<Bin> m_queue_bins[2];
184  thrust::device_vector<uint2> m_scan_bounds;
185  thrust::device_vector<float> m_split_costs;
186  thrust::device_vector<uint32> m_split_index;
187 
188  thrust::device_vector<uint32> m_segment_ids;
189  thrust::device_vector<uint32> m_node_ids;
190  thrust::device_vector<uint32> m_counters;
191 };
192 
196 } // namespace cuda
197 } // namespace nih
198 
199 #include <nih/bvh/cuda/sah_builder_inline.h>
Sah_builder(thrust::device_vector< Bvh_node > &nodes, thrust::device_vector< uint2 > &leaves, thrust::device_vector< uint32 > &index)
Definition: sah_builder.h:111
Definition: sah_builder.h:37
Definition: sah_builder.h:115
Definition: sah_builder.h:110
Definition: sah_builder.h:46
GPU-based SAH BVH builder.
Definition: sah_builder.h:41
Definition: sah_builder.h:80
Definition: sah_builder.h:59
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE Out binary_cast(const In in)
Definition: types.h:288
Definition: sah_builder.h:54