Blender  V2.93
bvh_params.h
Go to the documentation of this file.
1 /*
2  * Adapted from code copyright 2009-2010 NVIDIA Corporation
3  * Modifications Copyright 2011, Blender Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef __BVH_PARAMS_H__
19 #define __BVH_PARAMS_H__
20 
21 #include "util/util_boundbox.h"
22 
23 #include "kernel/kernel_types.h"
24 
26 
27 /* Layout of BVH tree.
28  *
29  * For example, how wide BVH tree is, in terms of number of children
30  * per node.
31  */
33 
34 /* Names bitflag type to denote which BVH layouts are supported by
35  * particular area.
36  *
37  * Bitflags are the BVH_LAYOUT_* values.
38  */
39 typedef int BVHLayoutMask;
40 
41 /* Get human readable name of BVH layout. */
42 const char *bvh_layout_name(BVHLayout layout);
43 
44 /* BVH Parameters */
45 
46 class BVHParams {
47  public:
48  /* spatial split area threshold */
51 
52  /* Unaligned nodes creation threshold */
54 
55  /* SAH costs */
58 
59  /* number of primitives in leaf */
65 
66  /* object or mesh level bvh */
67  bool top_level;
68 
69  /* BVH layout to be built. */
71 
72  /* Use unaligned bounding boxes.
73  * Only used for curves BVH.
74  */
76 
77  /* Split time range to this number of steps and create leaf node for each
78  * of this time steps.
79  *
80  * Speeds up rendering of motion curve primitives in the cost of higher
81  * memory usage.
82  */
84 
85  /* Same as above, but for triangle primitives. */
87 
88  /* Same as in SceneParams. */
89  int bvh_type;
90 
91  /* These are needed for Embree. */
93 
94  /* fixed parameters */
95  enum { MAX_DEPTH = 64, MAX_SPATIAL_DEPTH = 48, NUM_SPATIAL_BINS = 32 };
96 
98  {
99  use_spatial_split = true;
100  spatial_split_alpha = 1e-5f;
101 
103 
104  /* todo: see if splitting up primitive cost to be separate for triangles
105  * and curves can help. so far in tests it doesn't help, but why? */
106  sah_node_cost = 1.0f;
107  sah_primitive_cost = 1.0f;
108 
109  min_leaf_size = 1;
114 
115  top_level = false;
117  use_unaligned_nodes = false;
118 
121 
122  bvh_type = 0;
123 
124  curve_subdivisions = 4;
125  }
126 
127  /* SAH costs */
128  __forceinline float cost(int num_nodes, int num_primitives) const
129  {
130  return node_cost(num_nodes) + primitive_cost(num_primitives);
131  }
132 
133  __forceinline float primitive_cost(int n) const
134  {
135  return n * sah_primitive_cost;
136  }
137 
138  __forceinline float node_cost(int n) const
139  {
140  return n * sah_node_cost;
141  }
142 
144  {
145  return (size <= min_leaf_size || level >= MAX_DEPTH);
146  }
147 
148  /* Gets best matching BVH.
149  *
150  * If the requested layout is supported by the device, it will be used.
151  * Otherwise, widest supported layout below that will be used.
152  */
153  static BVHLayout best_bvh_layout(BVHLayout requested_layout, BVHLayoutMask supported_layouts);
154 };
155 
156 /* BVH Reference
157  *
158  * Reference to a primitive. Primitive index and object are sneakily packed
159  * into BoundBox to reduce memory usage and align nicely */
160 
162  public:
164  {
165  }
166 
168  int prim_index_,
169  int prim_object_,
170  int prim_type,
171  float time_from = 0.0f,
172  float time_to = 1.0f)
174  {
175  rbounds.min.w = __int_as_float(prim_index_);
176  rbounds.max.w = __int_as_float(prim_object_);
177  type = prim_type;
178  }
179 
181  {
182  return rbounds;
183  }
185  {
186  return __float_as_int(rbounds.min.w);
187  }
189  {
190  return __float_as_int(rbounds.max.w);
191  }
193  {
194  return type;
195  }
196  __forceinline float time_from() const
197  {
198  return time_from_;
199  }
200  __forceinline float time_to() const
201  {
202  return time_to_;
203  }
204 
206  {
207  if (&arg != this) {
208  /* TODO(sergey): Check if it is still faster to memcpy() with
209  * modern compilers.
210  */
211  memcpy((void *)this, &arg, sizeof(BVHReference));
212  }
213  return *this;
214  }
215 
216  protected:
220 };
221 
222 /* BVH Range
223  *
224  * Build range used during construction, to indicate the bounds and place in
225  * the reference array of a subset of primitives Again uses trickery to pack
226  * integers into BoundBox for alignment purposes. */
227 
228 class BVHRange {
229  public:
231  {
232  rbounds.min.w = __int_as_float(0);
233  rbounds.max.w = __int_as_float(0);
234  }
235 
236  __forceinline BVHRange(const BoundBox &bounds_, int start_, int size_) : rbounds(bounds_)
237  {
238  rbounds.min.w = __int_as_float(start_);
239  rbounds.max.w = __int_as_float(size_);
240  }
241 
242  __forceinline BVHRange(const BoundBox &bounds_, const BoundBox &cbounds_, int start_, int size_)
243  : rbounds(bounds_), cbounds(cbounds_)
244  {
245  rbounds.min.w = __int_as_float(start_);
246  rbounds.max.w = __int_as_float(size_);
247  }
248 
249  __forceinline void set_start(int start_)
250  {
251  rbounds.min.w = __int_as_float(start_);
252  }
253 
255  {
256  return rbounds;
257  }
259  {
260  return cbounds;
261  }
262  __forceinline int start() const
263  {
264  return __float_as_int(rbounds.min.w);
265  }
266  __forceinline int size() const
267  {
268  return __float_as_int(rbounds.max.w);
269  }
270  __forceinline int end() const
271  {
272  return start() + size();
273  }
274 
275  protected:
278 };
279 
280 /* BVH Spatial Bin */
281 
284  int enter;
285  int exit;
286 
288  {
289  }
290 };
291 
292 /* BVH Spatial Storage
293  *
294  * The idea of this storage is have thread-specific storage for the spatial
295  * splitters. We can pre-allocate this storage in advance and avoid heavy memory
296  * operations during split process.
297  */
298 
300  /* Accumulated bounds when sweeping from right to left. */
302 
303  /* Bins used for histogram when selecting best split plane. */
305 
306  /* Temporary storage for the new references. Used by spatial split to store
307  * new references in before they're getting inserted into actual array,
308  */
310 };
311 
313 
314 #endif /* __BVH_PARAMS_H__ */
unsigned int uint
Definition: BLI_sys_types.h:83
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
int BVHLayoutMask
Definition: bvh_params.h:39
const char * bvh_layout_name(BVHLayout layout)
Definition: bvh.cpp:32
CCL_NAMESPACE_BEGIN typedef KernelBVHLayout BVHLayout
Definition: bvh_params.h:32
__forceinline float node_cost(int n) const
Definition: bvh_params.h:138
bool use_spatial_split
Definition: bvh_params.h:49
int max_triangle_leaf_size
Definition: bvh_params.h:61
int num_motion_triangle_steps
Definition: bvh_params.h:86
@ MAX_SPATIAL_DEPTH
Definition: bvh_params.h:95
@ NUM_SPATIAL_BINS
Definition: bvh_params.h:95
static BVHLayout best_bvh_layout(BVHLayout requested_layout, BVHLayoutMask supported_layouts)
Definition: bvh.cpp:53
float spatial_split_alpha
Definition: bvh_params.h:50
BVHLayout bvh_layout
Definition: bvh_params.h:70
__forceinline float cost(int num_nodes, int num_primitives) const
Definition: bvh_params.h:128
__forceinline bool small_enough_for_leaf(int size, int level)
Definition: bvh_params.h:143
int max_curve_leaf_size
Definition: bvh_params.h:63
bool use_unaligned_nodes
Definition: bvh_params.h:75
int max_motion_curve_leaf_size
Definition: bvh_params.h:64
int min_leaf_size
Definition: bvh_params.h:60
int max_motion_triangle_leaf_size
Definition: bvh_params.h:62
float sah_node_cost
Definition: bvh_params.h:56
int curve_subdivisions
Definition: bvh_params.h:92
float sah_primitive_cost
Definition: bvh_params.h:57
bool top_level
Definition: bvh_params.h:67
int bvh_type
Definition: bvh_params.h:89
__forceinline float primitive_cost(int n) const
Definition: bvh_params.h:133
float unaligned_split_threshold
Definition: bvh_params.h:53
int num_motion_curve_steps
Definition: bvh_params.h:83
BoundBox cbounds
Definition: bvh_params.h:277
__forceinline BVHRange(const BoundBox &bounds_, int start_, int size_)
Definition: bvh_params.h:236
__forceinline int size() const
Definition: bvh_params.h:266
__forceinline int start() const
Definition: bvh_params.h:262
__forceinline const BoundBox & cent_bounds() const
Definition: bvh_params.h:258
BoundBox rbounds
Definition: bvh_params.h:276
__forceinline BVHRange()
Definition: bvh_params.h:230
__forceinline void set_start(int start_)
Definition: bvh_params.h:249
__forceinline BVHRange(const BoundBox &bounds_, const BoundBox &cbounds_, int start_, int size_)
Definition: bvh_params.h:242
__forceinline const BoundBox & bounds() const
Definition: bvh_params.h:254
__forceinline int end() const
Definition: bvh_params.h:270
BVHReference & operator=(const BVHReference &arg)
Definition: bvh_params.h:205
BoundBox rbounds
Definition: bvh_params.h:217
__forceinline int prim_type() const
Definition: bvh_params.h:192
float time_from_
Definition: bvh_params.h:219
__forceinline BVHReference()
Definition: bvh_params.h:163
__forceinline int prim_object() const
Definition: bvh_params.h:188
__forceinline const BoundBox & bounds() const
Definition: bvh_params.h:180
__forceinline BVHReference(const BoundBox &bounds_, int prim_index_, int prim_object_, int prim_type, float time_from=0.0f, float time_to=1.0f)
Definition: bvh_params.h:167
__forceinline float time_from() const
Definition: bvh_params.h:196
float time_to_
Definition: bvh_params.h:219
__forceinline float time_to() const
Definition: bvh_params.h:200
__forceinline int prim_index() const
Definition: bvh_params.h:184
#define CCL_NAMESPACE_END
KernelBVHLayout
@ BVH_LAYOUT_BVH2
__forceinline BVHSpatialBin()
Definition: bvh_params.h:287
BoundBox bounds
Definition: bvh_params.h:283
vector< BVHReference > new_references
Definition: bvh_params.h:309
vector< BoundBox > right_bounds
Definition: bvh_params.h:301
BVHSpatialBin bins[3][BVHParams::NUM_SPATIAL_BINS]
Definition: bvh_params.h:304
float3 max
Definition: util_boundbox.h:34
float3 min
Definition: util_boundbox.h:34
#define __forceinline
Definition: util_defines.h:71
ccl_device_inline int __float_as_int(float f)
Definition: util_math.h:202
ccl_device_inline float __int_as_float(int i)
Definition: util_math.h:212