Blender V5.3
light_tree.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "scene/light.h"
8#include "scene/scene.h"
9
10#include "util/boundbox.h"
11#include "util/task.h"
12#include "util/types.h"
13#include "util/vector.h"
14
15#include <atomic>
16#include <variant>
17
19
20/* Orientation Bounds
21 *
22 * Bounds the normal axis of the lights,
23 * along with their emission profiles */
25 float3 axis; /* normal axis of the light */
26 float theta_o; /* angle bounding the normals */
27 float theta_e; /* angle bounding the light emissions */
28
30
31 __forceinline OrientationBounds(const float3 &axis_, float theta_o_, float theta_e_)
32 : axis(axis_), theta_o(theta_o_), theta_e(theta_e_)
33 {
34 }
35
36 enum empty_t { empty = 0 };
37
38 /* If the orientation bound is set to empty, the values are set to minimums
39 * so that merging it with another non-empty orientation bound guarantees that
40 * the return value is equal to non-empty orientation bound. */
45
47 {
48 return is_zero(axis);
49 }
50
51 float calculate_measure() const;
52};
53
55
56/* --------------------------------------------------------------------
57 * Light Tree Construction
58 *
59 * The light tree construction is based on PBRT's BVH construction.
60 */
61
62/* Light Tree uses the bounding box, the orientation bounding cone, and the energy of a cluster to
63 * compute the Surface Area Orientation Heuristic (SAOH). */
67 float energy = 0.0f;
68
69 enum empty_t { empty = 0 };
70
72
74
77 const float &energy)
79 {
80 }
81
83
84 = default;
85
87 {
88 return energy == 0;
89 }
90
91 __forceinline void add(const LightTreeMeasure &measure)
92 {
93 if (!measure.is_zero()) {
94 bbox.grow(measure.bbox);
95 bcone = merge(bcone, measure.bcone);
96 energy += measure.energy;
97 }
98 }
99
100 /* Taken from Eq. 2 in the paper. */
102 {
103 if (is_zero()) {
104 return 0.0f;
105 }
106
107 const float area = bbox.area();
108 const float area_measure = area == 0 ? len(bbox.size()) : area;
109 return energy * area_measure * bcone.calculate_measure();
110 }
111
113 {
114 *this = {};
115 }
116
117 bool transform(const Transform &tfm)
118 {
119 float scale_squared;
120 if (transform_uniform_scale(tfm, scale_squared)) {
121 bbox = bbox.transformed(&tfm);
122 bcone.axis = transform_direction(&tfm, bcone.axis) * inversesqrtf(scale_squared);
123 energy *= scale_squared;
124 return true;
125 }
126 return false;
127 }
128};
129
131
132struct LightTreeNode;
133
134/* Light Linking. */
136 /* Bitmask for membership of primitives in this node. */
138
139 /* When all primitives below this node have identical light set membership, this
140 * part of the light tree can be shared between specialized trees. */
141 bool shareable = true;
143
146
147 void add(const uint64_t prim_set_membership)
148 {
149 if (set_membership == 0) {
150 set_membership = prim_set_membership;
151 }
152 else if (prim_set_membership != set_membership) {
153 set_membership |= prim_set_membership;
154 shareable = false;
155 }
156 }
157
158 void add(const LightTreeLightLink &other)
159 {
160 /* other.set_membership is zero when expanding with an empty bucket: in this case there is no
161 * need to mark node as not shareable. */
162 if (other.set_membership == 0) {
163 return;
164 }
165
166 if (set_membership == 0) {
168 shareable = other.shareable;
169 }
170 else if (other.set_membership != set_membership) {
172 shareable = false;
173 }
174 else if (!other.shareable) {
175 shareable = false;
176 }
177 }
178};
179
181
182/* Light Tree Emitter
183 * An emitter is a built-in light, an emissive mesh, or an emissive triangle. */
185 /* If the emitter is a mesh, point to the root node of its subtree. */
187
188 /* Primitive index. */
190 /* Object index. */
194
196
197 LightTreeEmitter(Object *object, const int object_id); /* Mesh emitter. */
198 LightTreeEmitter(Scene *scene,
199 const int prim_id,
200 const int object_id,
201 bool need_transformation = false);
202
204 {
205 return root != nullptr;
206 };
207
209 {
210 return !is_mesh() && prim_id >= 0;
211 };
212
214 {
215 return !is_mesh() && prim_id < 0;
216 };
217};
218
219/* Light Tree Bucket
220 * Struct used to determine splitting costs in the light BVH. */
224 int count = 0;
225 static const int num_buckets = 12;
226
227 LightTreeBucket() = default;
228
235
236 void add(const LightTreeEmitter &emitter)
237 {
238 measure.add(emitter.measure);
240 count++;
241 }
242};
243
245
246/* Light Tree Node */
252
253 /* A bitmask of `LightTreeNodeType`, as in the building process an instance node can also be a
254 * leaf or an inner node. */
255 int type;
256
257 struct Leaf {
258 /* The number of emitters a leaf node stores. */
259 int num_emitters = -1;
260 /* Index to first emitter. */
262 };
263
264 struct Inner {
265 /* Inner node has two children. */
267 };
268
269 struct Instance {
271 };
272
273 std::variant<Leaf, Inner, Instance> variant_type;
274
275 LightTreeNode(const LightTreeMeasure &measure, const uint &bit_trial)
276 : measure(measure), bit_trail(bit_trial), variant_type(Inner())
277 {
279 }
280
281 ~LightTreeNode() = default;
282
283 __forceinline void add(const LightTreeEmitter &emitter)
284 {
285 measure.add(emitter.measure);
287 }
288
290 {
291 return std::get<Leaf>(variant_type);
292 }
293
295 {
296 return std::get<Leaf>(variant_type);
297 }
298
300 {
301 return std::get<Inner>(variant_type);
302 }
303
305 {
306 return std::get<Inner>(variant_type);
307 }
308
310 {
311 return std::get<Instance>(variant_type);
312 }
313
315 {
316 return std::get<Instance>(variant_type);
317 }
318
319 void make_leaf(const int first_emitter_index, const int num_emitters)
320 {
321 variant_type = Leaf();
322 Leaf &leaf = get_leaf();
323
324 leaf.first_emitter_index = first_emitter_index;
325 leaf.num_emitters = num_emitters;
327 }
328
329 void make_distant(const int first_emitter_index, const int num_emitters)
330 {
331 variant_type = Leaf();
332 Leaf &leaf = get_leaf();
333
334 leaf.first_emitter_index = first_emitter_index;
335 leaf.num_emitters = num_emitters;
337 }
338
339 void make_instance(LightTreeNode *reference, const int object_id)
340 {
342 Instance &instance = get_instance();
343
344 instance.reference = reference;
345 this->object_id = object_id;
347 }
348
350 {
352 if (type == LIGHT_TREE_INSTANCE) {
353 return get_instance().reference;
354 }
355 return this;
356 }
357
359 {
360 return type & LIGHT_TREE_INSTANCE;
361 }
362
364 {
365 return type & LIGHT_TREE_LEAF;
366 }
367
369 {
370 return type & LIGHT_TREE_INNER;
371 }
372
374 {
375 return type == LIGHT_TREE_DISTANT;
376 }
377};
378
379/* Light BVH
380 *
381 * BVH-like data structure that keeps track of lights
382 * and considers additional orientation and energy information */
385
386 /* Local lights, distant lights and mesh lights are added to separate vectors for light tree
387 * construction. They are all considered as `emitters_`. */
388 vector<LightTreeEmitter> emitters_;
389 vector<LightTreeEmitter> local_lights_;
390 vector<LightTreeEmitter> distant_lights_;
391 vector<LightTreeEmitter> mesh_lights_;
392
393 std::unordered_map<Mesh *, int> offset_map_;
394
395 Progress &progress_;
396
397 uint max_lights_in_leaf_;
398
399 public:
400 std::atomic<int> num_nodes = 0;
401 size_t num_triangles = 0;
402
403 /* Bitmask of receiver light sets used. Default set is always used. */
405
406 /* An inner node itself or its left and right child. */
407 enum Child {
408 self = -1,
409 left = 0,
410 right = 1,
411 };
412
413 LightTree(Scene *scene, DeviceScene *dscene, Progress &progress, const uint max_lights_in_leaf);
414
415 /* Returns a pointer to the root node. */
416 LightTreeNode *build(Scene *scene, DeviceScene *dscene);
417
418 /* NOTE: Always use this function to create a new node so the number of nodes is in sync. */
419 unique_ptr<LightTreeNode> create_node(const LightTreeMeasure &measure, const uint &bit_trial)
420 {
421 num_nodes++;
422 return make_unique<LightTreeNode>(measure, bit_trial);
423 }
424
425 size_t num_emitters() const
426 {
427 return emitters_.size();
428 }
429
431 {
432 return emitters_.data();
433 }
434
435 private:
436 /* Thread. */
437 TaskPool task_pool;
438 /* Do not spawn a thread if less than this amount of emitters are to be processed. */
439 enum { MIN_EMITTERS_PER_THREAD = 4096 };
440
441 void recursive_build(Child child,
442 LightTreeNode *inner,
443 const int start,
444 const int end,
445 LightTreeEmitter *emitters,
446 const uint bit_trail,
447 int depth);
448
449 bool should_split(LightTreeEmitter *emitters,
450 const int start,
451 int &middle,
452 const int end,
453 LightTreeMeasure &measure,
454 LightTreeLightLink &light_link,
455 int &split_dim);
456
457 /* Check whether the light tree can use this triangle as light-emissive. */
458 bool triangle_usable_as_light(Mesh *mesh, const int prim_id);
459
460 /* Add all the emissive triangles of a mesh to the light tree. */
461 void add_mesh(Scene *scene, Mesh *mesh, const int object_id);
462};
463
unsigned long long int uint64_t
void build(btStridingMeshInterface *triangles, bool useQuantizedAabbCompression, const btVector3 &bvhAabbMin, const btVector3 &bvhAabbMax)
BoundBox transformed(const Transform *tfm) const
Definition boundbox.h:134
__forceinline float3 size() const
Definition boundbox.h:122
__forceinline void grow(const float3 &pt)
Definition boundbox.h:35
__forceinline float area() const
Definition boundbox.h:101
std::atomic< int > num_nodes
Definition light_tree.h:400
const LightTreeEmitter * get_emitters() const
Definition light_tree.h:430
LightTree(Scene *scene, DeviceScene *dscene, Progress &progress, const uint max_lights_in_leaf)
unique_ptr< LightTreeNode > create_node(const LightTreeMeasure &measure, const uint &bit_trial)
Definition light_tree.h:419
uint64_t light_link_receiver_used
Definition light_tree.h:404
size_t num_triangles
Definition light_tree.h:401
size_t num_emitters() const
Definition light_tree.h:425
Definition scene.h:133
#define __forceinline
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define assert(assertion)
#define FLT_MIN
bool is_zero(float3x3 a)
float3 transform_direction(float3x3 mat, float3 direction)
@ LIGHT_TREE_INSTANCE
@ LIGHT_TREE_DISTANT
@ LIGHT_TREE_INNER
@ LIGHT_TREE_LEAF
__forceinline LightTreeMeasure operator+(const LightTreeMeasure &a, const LightTreeMeasure &b)
OrientationBounds merge(const OrientationBounds &cone_a, const OrientationBounds &cone_b)
OrientationBounds merge(const OrientationBounds &cone_a, const OrientationBounds &cone_b)
ccl_device_inline float inversesqrtf(const float f)
Definition math_base.h:576
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
void add(const LightTreeEmitter &emitter)
Definition light_tree.h:236
LightTreeBucket()=default
LightTreeLightLink light_link
Definition light_tree.h:223
static const int num_buckets
Definition light_tree.h:225
LightTreeMeasure measure
Definition light_tree.h:222
LightTreeBucket(const LightTreeMeasure &measure, const LightTreeLightLink &light_link, const int &count)
Definition light_tree.h:229
__forceinline bool is_mesh() const
Definition light_tree.h:203
__forceinline bool is_triangle() const
Definition light_tree.h:208
unique_ptr< LightTreeNode > root
Definition light_tree.h:186
uint64_t light_set_membership
Definition light_tree.h:193
LightTreeMeasure measure
Definition light_tree.h:195
LightTreeEmitter(Object *object, const int object_id)
__forceinline bool is_light() const
Definition light_tree.h:213
__forceinline LightTreeMeasure(empty_t)
Definition light_tree.h:73
OrientationBounds bcone
Definition light_tree.h:66
bool transform(const Transform &tfm)
Definition light_tree.h:117
__forceinline LightTreeMeasure()=default
__forceinline void reset()
Definition light_tree.h:112
__forceinline float calculate()
Definition light_tree.h:101
__forceinline LightTreeMeasure(const LightTreeMeasure &other)=default
__forceinline LightTreeMeasure(const BoundBox &bbox, const OrientationBounds &bcone, const float &energy)
Definition light_tree.h:75
__forceinline void add(const LightTreeMeasure &measure)
Definition light_tree.h:91
__forceinline bool is_zero() const
Definition light_tree.h:86
unique_ptr< LightTreeNode > children[2]
Definition light_tree.h:266
LightTreeNode * reference
Definition light_tree.h:270
LightTreeNode(const LightTreeMeasure &measure, const uint &bit_trial)
Definition light_tree.h:275
void make_instance(LightTreeNode *reference, const int object_id)
Definition light_tree.h:339
__forceinline void add(const LightTreeEmitter &emitter)
Definition light_tree.h:283
__forceinline Inner & get_inner()
Definition light_tree.h:299
__forceinline Instance & get_instance()
Definition light_tree.h:309
__forceinline bool is_leaf() const
Definition light_tree.h:363
void make_leaf(const int first_emitter_index, const int num_emitters)
Definition light_tree.h:319
LightTreeNode * get_reference()
Definition light_tree.h:349
LightTreeMeasure measure
Definition light_tree.h:248
std::variant< Leaf, Inner, Instance > variant_type
Definition light_tree.h:273
void make_distant(const int first_emitter_index, const int num_emitters)
Definition light_tree.h:329
__forceinline bool is_inner() const
Definition light_tree.h:368
LightTreeLightLink light_link
Definition light_tree.h:249
__forceinline bool is_instance() const
Definition light_tree.h:358
__forceinline Leaf & get_leaf()
Definition light_tree.h:289
__forceinline bool is_distant() const
Definition light_tree.h:373
__forceinline const Inner & get_inner() const
Definition light_tree.h:304
~LightTreeNode()=default
__forceinline const Leaf & get_leaf() const
Definition light_tree.h:294
__forceinline const Instance & get_instance() const
Definition light_tree.h:314
__forceinline OrientationBounds(const float3 &axis_, float theta_o_, float theta_e_)
Definition light_tree.h:31
__forceinline bool is_empty() const
Definition light_tree.h:46
__forceinline OrientationBounds()=default
__forceinline OrientationBounds(empty_t)
Definition light_tree.h:41
float calculate_measure() const
ccl_device_inline bool transform_uniform_scale(const Transform &tfm, float &scale)
Definition transform.h:406