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