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