Blender  V3.3
params.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Adapted from code copyright 2009-2010 NVIDIA Corporation
3  * Modifications Copyright 2011-2022 Blender Foundation. */
4 
5 #ifndef __BVH_PARAMS_H__
6 #define __BVH_PARAMS_H__
7 
8 #include "util/boundbox.h"
9 
10 #include "kernel/types.h"
11 
13 
14 /* Layout of BVH tree.
15  *
16  * For example, how wide BVH tree is, in terms of number of children
17  * per node.
18  */
20 
21 /* Type of BVH, in terms whether it is supported dynamic updates of meshes
22  * or whether modifying geometry requires full BVH rebuild.
23  */
24 enum BVHType {
25  /* BVH supports dynamic updates of geometry.
26  *
27  * Faster for updating BVH tree when doing modifications in viewport,
28  * but slower for rendering.
29  */
31  /* BVH tree is calculated for specific scene, updates in geometry
32  * requires full tree rebuild.
33  *
34  * Slower to update BVH tree when modifying objects in viewport, also
35  * slower to build final BVH tree but gives best possible render speed.
36  */
38 
40 };
41 
42 /* Names bit-flag type to denote which BVH layouts are supported by
43  * particular area.
44  *
45  * Bit-flags are the BVH_LAYOUT_* values.
46  */
47 typedef int BVHLayoutMask;
48 
49 /* Get human readable name of BVH layout. */
50 const char *bvh_layout_name(BVHLayout layout);
51 
52 /* BVH Parameters */
53 
54 class BVHParams {
55  public:
56  /* spatial split area threshold */
59 
60  /* Unaligned nodes creation threshold */
62 
63  /* SAH costs */
66 
67  /* number of primitives in leaf */
75 
76  /* object or mesh level bvh */
77  bool top_level;
78 
79  /* BVH layout to be built. */
81 
82  /* Use unaligned bounding boxes.
83  * Only used for curves BVH.
84  */
86 
87  /* Use compact acceleration structure (Embree)*/
89 
90  /* Split time range to this number of steps and create leaf node for each
91  * of this time steps.
92  *
93  * Speeds up rendering of motion primitives in the cost of higher memory usage.
94  */
95 
96  /* Same as above, but for triangle primitives. */
100 
101  /* Same as in SceneParams. */
102  int bvh_type;
103 
104  /* These are needed for Embree. */
106 
107  /* fixed parameters */
108  enum { MAX_DEPTH = 64, MAX_SPATIAL_DEPTH = 48, NUM_SPATIAL_BINS = 32 };
109 
111  {
112  use_spatial_split = true;
113  spatial_split_alpha = 1e-5f;
114 
116 
117  /* todo: see if splitting up primitive cost to be separate for triangles
118  * and curves can help. so far in tests it doesn't help, but why? */
119  sah_node_cost = 1.0f;
120  sah_primitive_cost = 1.0f;
121 
122  min_leaf_size = 1;
129 
130  top_level = false;
132  use_compact_structure = false;
133  use_unaligned_nodes = false;
134 
138 
139  bvh_type = 0;
140 
141  curve_subdivisions = 4;
142  }
143 
144  /* SAH costs */
145  __forceinline float cost(int num_nodes, int num_primitives) const
146  {
147  return node_cost(num_nodes) + primitive_cost(num_primitives);
148  }
149 
150  __forceinline float primitive_cost(int n) const
151  {
152  return n * sah_primitive_cost;
153  }
154 
155  __forceinline float node_cost(int n) const
156  {
157  return n * sah_node_cost;
158  }
159 
161  {
162  return (size <= min_leaf_size || level >= MAX_DEPTH);
163  }
164 
166  {
169  }
170 
171  /* Gets best matching BVH.
172  *
173  * If the requested layout is supported by the device, it will be used.
174  * Otherwise, widest supported layout below that will be used.
175  */
176  static BVHLayout best_bvh_layout(BVHLayout requested_layout, BVHLayoutMask supported_layouts);
177 };
178 
179 /* BVH Reference
180  *
181  * Reference to a primitive. Primitive index and object are sneakily packed
182  * into BoundBox to reduce memory usage and align nicely */
183 
185  public:
187  {
188  }
189 
191  int prim_index_,
192  int prim_object_,
193  int prim_type,
194  float time_from = 0.0f,
195  float time_to = 1.0f)
197  {
198  rbounds.min.w = __int_as_float(prim_index_);
199  rbounds.max.w = __int_as_float(prim_object_);
200  type = prim_type;
201  }
202 
204  {
205  return rbounds;
206  }
208  {
209  return __float_as_int(rbounds.min.w);
210  }
212  {
213  return __float_as_int(rbounds.max.w);
214  }
216  {
217  return type;
218  }
219  __forceinline float time_from() const
220  {
221  return time_from_;
222  }
223  __forceinline float time_to() const
224  {
225  return time_to_;
226  }
227 
229  {
230  if (&arg != this) {
231  /* TODO(sergey): Check if it is still faster to memcpy() with
232  * modern compilers.
233  */
234  memcpy((void *)this, &arg, sizeof(BVHReference));
235  }
236  return *this;
237  }
238 
239  protected:
243 };
244 
245 /* BVH Range
246  *
247  * Build range used during construction, to indicate the bounds and place in
248  * the reference array of a subset of primitives Again uses trickery to pack
249  * integers into BoundBox for alignment purposes. */
250 
251 class BVHRange {
252  public:
254  {
255  rbounds.min.w = __int_as_float(0);
256  rbounds.max.w = __int_as_float(0);
257  }
258 
259  __forceinline BVHRange(const BoundBox &bounds_, int start_, int size_) : rbounds(bounds_)
260  {
261  rbounds.min.w = __int_as_float(start_);
262  rbounds.max.w = __int_as_float(size_);
263  }
264 
265  __forceinline BVHRange(const BoundBox &bounds_, const BoundBox &cbounds_, int start_, int size_)
266  : rbounds(bounds_), cbounds(cbounds_)
267  {
268  rbounds.min.w = __int_as_float(start_);
269  rbounds.max.w = __int_as_float(size_);
270  }
271 
272  __forceinline void set_start(int start_)
273  {
274  rbounds.min.w = __int_as_float(start_);
275  }
276 
278  {
279  return rbounds;
280  }
282  {
283  return cbounds;
284  }
285  __forceinline int start() const
286  {
287  return __float_as_int(rbounds.min.w);
288  }
289  __forceinline int size() const
290  {
291  return __float_as_int(rbounds.max.w);
292  }
293  __forceinline int end() const
294  {
295  return start() + size();
296  }
297 
298  protected:
301 };
302 
303 /* BVH Spatial Bin */
304 
307  int enter;
308  int exit;
309 
311  {
312  }
313 };
314 
315 /* BVH Spatial Storage
316  *
317  * The idea of this storage is have thread-specific storage for the spatial
318  * splitters. We can pre-allocate this storage in advance and avoid heavy memory
319  * operations during split process.
320  */
321 
323  /* Accumulated bounds when sweeping from right to left. */
325 
326  /* Bins used for histogram when selecting best split plane. */
328 
329  /* Temporary storage for the new references. Used by spatial split to store
330  * new references in before they're getting inserted into actual array,
331  */
333 };
334 
336 
337 #endif /* __BVH_PARAMS_H__ */
unsigned int uint
Definition: BLI_sys_types.h:67
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
__forceinline float node_cost(int n) const
Definition: params.h:155
int max_point_leaf_size
Definition: params.h:73
bool use_spatial_split
Definition: params.h:57
int max_triangle_leaf_size
Definition: params.h:69
int num_motion_triangle_steps
Definition: params.h:97
static BVHLayout best_bvh_layout(BVHLayout requested_layout, BVHLayoutMask supported_layouts)
Definition: bvh.cpp:45
float spatial_split_alpha
Definition: params.h:58
BVHLayout bvh_layout
Definition: params.h:80
bool use_compact_structure
Definition: params.h:88
__forceinline float cost(int num_nodes, int num_primitives) const
Definition: params.h:145
__forceinline bool small_enough_for_leaf(int size, int level)
Definition: params.h:160
int max_curve_leaf_size
Definition: params.h:71
bool use_unaligned_nodes
Definition: params.h:85
int max_motion_curve_leaf_size
Definition: params.h:72
BVHParams()
Definition: params.h:110
int min_leaf_size
Definition: params.h:68
int max_motion_point_leaf_size
Definition: params.h:74
int max_motion_triangle_leaf_size
Definition: params.h:70
float sah_node_cost
Definition: params.h:64
int curve_subdivisions
Definition: params.h:105
bool use_motion_steps()
Definition: params.h:165
int num_motion_point_steps
Definition: params.h:99
float sah_primitive_cost
Definition: params.h:65
@ MAX_DEPTH
Definition: params.h:108
@ MAX_SPATIAL_DEPTH
Definition: params.h:108
@ NUM_SPATIAL_BINS
Definition: params.h:108
bool top_level
Definition: params.h:77
int bvh_type
Definition: params.h:102
__forceinline float primitive_cost(int n) const
Definition: params.h:150
float unaligned_split_threshold
Definition: params.h:61
int num_motion_curve_steps
Definition: params.h:98
BoundBox cbounds
Definition: params.h:300
__forceinline BVHRange(const BoundBox &bounds_, int start_, int size_)
Definition: params.h:259
__forceinline int size() const
Definition: params.h:289
__forceinline int start() const
Definition: params.h:285
__forceinline const BoundBox & cent_bounds() const
Definition: params.h:281
BoundBox rbounds
Definition: params.h:299
__forceinline BVHRange()
Definition: params.h:253
__forceinline void set_start(int start_)
Definition: params.h:272
__forceinline BVHRange(const BoundBox &bounds_, const BoundBox &cbounds_, int start_, int size_)
Definition: params.h:265
__forceinline const BoundBox & bounds() const
Definition: params.h:277
__forceinline int end() const
Definition: params.h:293
BVHReference & operator=(const BVHReference &arg)
Definition: params.h:228
BoundBox rbounds
Definition: params.h:240
__forceinline int prim_type() const
Definition: params.h:215
float time_from_
Definition: params.h:242
__forceinline BVHReference()
Definition: params.h:186
__forceinline int prim_object() const
Definition: params.h:211
__forceinline const BoundBox & bounds() const
Definition: params.h:203
__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: params.h:190
__forceinline float time_from() const
Definition: params.h:219
float time_to_
Definition: params.h:242
__forceinline float time_to() const
Definition: params.h:223
uint type
Definition: params.h:241
__forceinline int prim_index() const
Definition: params.h:207
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
KernelBVHLayout
@ BVH_LAYOUT_BVH2
#define __forceinline
int BVHLayoutMask
Definition: params.h:47
const char * bvh_layout_name(BVHLayout layout)
Definition: bvh.cpp:20
CCL_NAMESPACE_BEGIN typedef KernelBVHLayout BVHLayout
Definition: params.h:19
BVHType
Definition: params.h:24
@ BVH_TYPE_DYNAMIC
Definition: params.h:30
@ BVH_NUM_TYPES
Definition: params.h:39
@ BVH_TYPE_STATIC
Definition: params.h:37
__forceinline BVHSpatialBin()
Definition: params.h:310
BoundBox bounds
Definition: params.h:306
vector< BVHReference > new_references
Definition: params.h:332
vector< BoundBox > right_bounds
Definition: params.h:324
BVHSpatialBin bins[3][BVHParams::NUM_SPATIAL_BINS]
Definition: params.h:327
float3 max
Definition: boundbox.h:21
float3 min
Definition: boundbox.h:21
ccl_device_inline int __float_as_int(float f)
Definition: util/math.h:243
ccl_device_inline float __int_as_float(int i)
Definition: util/math.h:253