Blender V5.3
depsgraph_light_linking.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
12
13#include "MEM_guardedalloc.h"
14
15#include "BLI_hash.hh"
16#include "BLI_listbase.hh"
17#include "BLI_map.hh"
18
20#include "DNA_object_types.h"
21#include "DNA_scene_types.h"
22
25
26#include "intern/depsgraph.hh"
27
28namespace blender {
29
30/* -------------------------------------------------------------------- */
33
34namespace deg::light_linking {
35
36void eval_runtime_data(const ::blender::Depsgraph *depsgraph, Object &object_eval)
37{
38 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(depsgraph);
39 deg_graph->light_linking_cache.eval_runtime_data(object_eval);
40}
41
42} // namespace deg::light_linking
43
45
46/* -------------------------------------------------------------------- */
49
50namespace {
51
52/* Check whether the ID is suitable to be an input of the dependency graph. */
53/* TODO(sergey): Move the function and check to a more generic place. */
54#ifndef NDEBUG
55bool is_valid_input_id(const ID &id)
56{
57 return (id.tag & ID_TAG_LOCALIZED) || DEG_is_original(&id);
58}
59#endif
60
61} // namespace
62
63namespace deg::light_linking {
64
70
71namespace internal {
72
73namespace {
74
75/* Helper class which takes care of allocating an unique light set IDs, performing checks for
76 * overflows. */
77class LightSetIDManager {
79
80 public:
81 explicit LightSetIDManager(const Scene &scene) : scene_(scene) {}
82
83 bool get(const LightSet &light_set, uint64_t &id)
84 {
85 /* Performance note.
86 *
87 * Always ensure the light set data exists in the map, even when an overflow happens. This has
88 * a downside of potentially higher memory usage and when there are many emitters with light
89 * linking, but it avoids distinct lookup + add fore the normal cases. */
90
91 const uint64_t light_set_id = light_set_id_map_.lookup_or_add_cb(light_set, [&]() {
92 const uint64_t new_light_set_id = next_light_set_id_++;
93
94 if (new_light_set_id == LightSet::MAX_ID + 1) {
95 printf("Maximum number of light linking sets (%d) exceeded scene \"%s\".\n",
97 scene_.id.name + 2);
98 }
99
100 return new_light_set_id;
101 });
102
103 id = light_set_id;
104
105 return id <= LightSet::MAX_ID;
106 }
107
108 private:
109 const Scene &scene_;
110
111 /* Next unique ID of a light set. */
112 uint64_t next_light_set_id_ = LightSet::DEFAULT_ID + 1;
113
114 /* Map from a link set to its original id. */
115 Map<internal::LightSet, uint64_t> light_set_id_map_;
116};
117
118} // namespace
119
120/* LightSet */
121
127
133
134/* EmitterSetMembership */
135
137{
138 const uint64_t effective_included_mask = included_sets_mask ? included_sets_mask :
140 return effective_included_mask & ~excluded_sets_mask;
141}
142
143/* EmitterDataMap. */
144
146{
147 emitter_data_map_.clear();
148 next_collection_id_ = 0;
149}
150
152{
153 BLI_assert(is_valid_input_id(emitter.id));
154
155 const Collection *collection = get_collection(emitter);
156 BLI_assert(collection);
157
158 /* Performance note.
159 *
160 * Always ensure the emitter data exists in the map, even when an overflow happens. This has a
161 * downside of potentially higher memory usage when there are many emitters with light linking,
162 * but it avoids distinct lookup + add fore the normal cases.
163 *
164 * On the API level the function always returns nullptr on overflow, so it is more of an internal
165 * behavior. */
166
167 EmitterData &emitter_data = emitter_data_map_.lookup_or_add_cb(collection, [&]() {
168 const uint64_t collection_id = next_collection_id_++;
169
170 EmitterData new_emitter_data;
171
172 if (collection_id > EmitterData::MAX_COLLECTION_ID) {
173 if (collection_id == EmitterData::MAX_COLLECTION_ID + 1) {
174 printf("Maximum number of light linking collections (%d) exceeded in scene \"%s\".\n",
176 scene.id.name + 2);
177 }
178 new_emitter_data.collection_mask = 0;
179 }
180 else {
181 new_emitter_data.collection_mask = uint64_t(1) << collection_id;
182 }
183
184 return new_emitter_data;
185 });
186
187 if (!emitter_data.collection_mask) {
188 return nullptr;
189 }
190
191 return &emitter_data;
192}
193
194const EmitterData *EmitterDataMap::get_data(const Object &emitter) const
195{
196 const Collection *collection_eval = get_collection(emitter);
197
198 if (!collection_eval) {
199 return nullptr;
200 }
201
202 const Collection *collection_orig = DEG_get_original(collection_eval);
203
204 return emitter_data_map_.lookup_ptr(collection_orig);
205}
206
207bool EmitterDataMap::can_skip_emitter(const Object &emitter) const
208{
209 BLI_assert(is_valid_input_id(emitter.id));
210
211 const Collection *collection = get_collection(emitter);
212
213 if (!collection) {
214 return true;
215 }
216
217 return emitter_data_map_.contains(collection);
218}
219
220/* LinkingData */
221
223{
224 light_linked_sets_.clear();
225 object_light_sets_.clear();
226}
227
228void LinkingData::link_object(const EmitterData &emitter_data,
229 const eCollectionLightLinkingState link_state,
230 const Object &object)
231{
232 LightSet &light_set = ensure_light_set_for(object);
233
234 switch (link_state) {
236 light_set.include_collection_mask |= emitter_data.collection_mask;
237 light_set.exclude_collection_mask &= ~emitter_data.collection_mask;
238 break;
239
241 light_set.exclude_collection_mask |= emitter_data.collection_mask;
242 light_set.include_collection_mask &= ~emitter_data.collection_mask;
243 break;
244 }
245}
246
247LightSet &LinkingData::ensure_light_set_for(const Object &object)
248{
249 BLI_assert(is_valid_input_id(object.id));
250
251 return light_linked_sets_.lookup_or_add_as(&object);
252}
253
254void LinkingData::clear_after_build()
255{
256 light_linked_sets_.clear();
257}
258
259void LinkingData::end_build(const Scene &scene, EmitterDataMap &emitter_data_map)
260{
261 LightSetIDManager light_set_id_manager(scene);
262
263 for (const auto it : light_linked_sets_.items()) {
264 const Object *receiver = it.key;
265 LightSet &light_set = it.value;
266
267 uint64_t light_set_id;
268 if (!light_set_id_manager.get(light_set, light_set_id)) {
269 continue;
270 }
271
272 const uint64_t light_set_mask = uint64_t(1) << light_set_id;
273
274 object_light_sets_.add(receiver, light_set_id);
275
276 update_emitters_membership(emitter_data_map, light_set, light_set_mask);
277 }
278
279 clear_after_build();
280}
281
282void LinkingData::update_emitters_membership(EmitterDataMap &emitter_data_map,
283 const LightSet &light_set,
284 const uint64_t light_set_mask)
285{
286 for (EmitterData &emitter_data : emitter_data_map.values()) {
287 EmitterSetMembership &set_membership = get_emitter_set_membership(emitter_data);
288
289 if (emitter_data.collection_mask & light_set.include_collection_mask) {
290 set_membership.included_sets_mask |= light_set_mask;
291 }
292 if (emitter_data.collection_mask & light_set.exclude_collection_mask) {
293 set_membership.excluded_sets_mask |= light_set_mask;
294 }
295 }
296}
297
299{
300 const Object *object_orig = DEG_get_original(&object);
301 return object_light_sets_.lookup_default(object_orig, LightSet::DEFAULT_ID);
302}
303
304} // namespace internal
305
306namespace {
307
308/* Iterate over all objects of the collection and invoke the given callback with two arguments:
309 * the given collection light linking settings, and the object (passed as reference).
310 *
311 * Note that if an object is reachable from multiple children collection the callback is invoked
312 * for all of them. */
313template<class Proc>
314void foreach_light_collection_object_inner(const CollectionLightLinking &collection_light_linking,
315 const Collection &collection,
316 Proc &&callback)
317{
318 for (const CollectionChild &collection_child : collection.children) {
319 foreach_light_collection_object_inner(
320 collection_light_linking, *collection_child.collection, callback);
321 }
322
323 for (const CollectionObject &collection_object : collection.gobject) {
324 callback(collection_light_linking, *collection_object.ob);
325 }
326}
327
328/* Iterate over all objects of the collection and invoke the given callback with two arguments:
329 * CollectionLightLinking and the actual Object (passed as reference).
330 *
331 * The CollectionLightLinking denotes the effective light linking settings of the object. It comes
332 * from the first level of hierarchy from the given collection.
333 *
334 * Note that if an object is reachable from multiple children collection the callback is invoked
335 * for all of them. */
336template<class Proc>
337void foreach_light_collection_object(const Collection &collection, Proc &&callback)
338{
339 for (const CollectionChild &collection_child : collection.children) {
340 foreach_light_collection_object_inner(
341 collection_child.light_linking, *collection_child.collection, callback);
342 }
343
344 for (const CollectionObject &collection_object : collection.gobject) {
345 callback(collection_object.light_linking, *collection_object.ob);
346 }
347}
348
349} // namespace
350
352{
353 light_emitter_data_map_.clear();
354 shadow_emitter_data_map_.clear();
355
356 light_linking_.clear();
357 shadow_linking_.clear();
358}
359
360void Cache::add_emitter(const Scene &scene, const Object &emitter)
361{
362 BLI_assert(is_valid_input_id(emitter.id));
363
364 add_light_linking_emitter(scene, emitter);
365 add_shadow_linking_emitter(scene, emitter);
366}
367
368void Cache::add_light_linking_emitter(const Scene &scene, const Object &emitter)
369{
370 BLI_assert(is_valid_input_id(emitter.id));
371
372 if (light_emitter_data_map_.can_skip_emitter(emitter)) {
373 return;
374 }
375
376 const EmitterData *light_emitter_data = light_emitter_data_map_.ensure_data_if_possible(scene,
377 emitter);
378 if (light_emitter_data) {
379 foreach_light_collection_object(
381 [&](const CollectionLightLinking &collection_light_linking, const Object &receiver) {
382 add_receiver_object(*light_emitter_data, collection_light_linking, receiver);
383 });
384 }
385}
386
387void Cache::add_shadow_linking_emitter(const Scene &scene, const Object &emitter)
388{
389 BLI_assert(is_valid_input_id(emitter.id));
390
391 if (shadow_emitter_data_map_.can_skip_emitter(emitter)) {
392 return;
393 }
394
395 const EmitterData *shadow_emitter_data = shadow_emitter_data_map_.ensure_data_if_possible(
396 scene, emitter);
397 if (shadow_emitter_data) {
398 foreach_light_collection_object(
399 *emitter.light_linking->blocker_collection,
400 [&](const CollectionLightLinking &collection_light_linking, const Object &receiver) {
401 add_blocker_object(*shadow_emitter_data, collection_light_linking, receiver);
402 });
403 }
404}
405
406void Cache::add_receiver_object(const EmitterData &emitter_data,
407 const CollectionLightLinking &collection_light_linking,
408 const Object &receiver)
409{
410 BLI_assert(is_valid_input_id(receiver.id));
411
412 light_linking_.link_object(
413 emitter_data, eCollectionLightLinkingState(collection_light_linking.link_state), receiver);
414}
415
416void Cache::add_blocker_object(const EmitterData &emitter_data,
417 const CollectionLightLinking &collection_light_linking,
418 const Object &blocker)
419{
420 BLI_assert(is_valid_input_id(blocker.id));
421
422 shadow_linking_.link_object(
423 emitter_data, eCollectionLightLinkingState(collection_light_linking.link_state), blocker);
424}
425
426void Cache::end_build(const Scene &scene)
427{
428 if (!has_light_linking()) {
429 return;
430 }
431
432 light_linking_.end_build(scene, light_emitter_data_map_);
433 shadow_linking_.end_build(scene, shadow_emitter_data_map_);
434}
435
436void Cache::eval_runtime_data(Object &object_eval) const
437{
438 static const LightLinkingRuntime runtime_no_links = {
440
441 if (!has_light_linking()) {
442 /* No light linking used in the scene, still reset to default on objects that have
443 * allocated light linking data structures since we can't free them here. */
444 if (object_eval.light_linking) {
445 object_eval.light_linking->runtime = runtime_no_links;
446 }
447
448 return;
449 }
450
451 /* Receiver and blocker configuration. */
452 LightLinkingRuntime runtime = {};
453 runtime.receiver_light_set = light_linking_.get_light_set_for(object_eval);
454 runtime.blocker_shadow_set = shadow_linking_.get_light_set_for(object_eval);
455
456 /* Emitter configuration. */
457 const EmitterData *light_emitter_data = light_emitter_data_map_.get_data(object_eval);
458 if (light_emitter_data) {
459 runtime.light_set_membership = light_emitter_data->light_membership.get_mask();
460 }
461 else {
463 }
464
465 const EmitterData *shadow_emitter_data = shadow_emitter_data_map_.get_data(object_eval);
466 if (shadow_emitter_data) {
467 runtime.shadow_set_membership = shadow_emitter_data->shadow_membership.get_mask();
468 }
469 else {
471 }
472
473 const bool need_runtime = (memcmp(&runtime, &runtime_no_links, sizeof(runtime)) != 0);
474
475 /* Assign, allocating light linking on demand if needed. */
476 if (object_eval.light_linking) {
477 object_eval.light_linking->runtime = runtime;
478 if (!need_runtime) {
479 /* Note that this will only remove lazily allocated light_linking on the evaluated object,
480 * as an empty light_linking is not allowed on the original object. */
482 }
483 }
484 else if (need_runtime) {
485 BKE_light_linking_ensure(&object_eval);
486 object_eval.light_linking->runtime = runtime;
487 }
488}
489
490} // namespace deg::light_linking
491
493
494} // namespace blender
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define BLI_assert(a)
Definition scene.h:133
void eval_runtime_data(Object &object_eval) const
void add_emitter(const Scene &scene, const Object &emitter)
EmitterData * ensure_data_if_possible(const Scene &scene, const Object &emitter)
const EmitterData * get_data(const Object &emitter) const
void end_build(const Scene &scene, EmitterDataMap &emitter_data_map)
void link_object(const EmitterData &emitter_data, eCollectionLightLinkingState link_state, const Object &object)
internal::EmitterSetMembership EmitterSetMembership
void eval_runtime_data(const Depsgraph *depsgraph, Object &object_eval)
internal::EmitterDataMap EmitterDataMap
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
void BKE_light_linking_free_if_empty(struct Object *object)
bool DEG_is_original(const T *id)
@ COLLECTION_LIGHT_LINKING_STATE_INCLUDE
@ COLLECTION_LIGHT_LINKING_STATE_EXCLUDE
void BKE_light_linking_ensure(struct Object *object)
T * DEG_get_original(T *id)
@ ID_TAG_LOCALIZED
Definition DNA_ID.h:990
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
ListBaseT< CollectionChild > children
ListBaseT< CollectionObject > gobject
char name[258]
Definition DNA_ID.h:433
LightLinkingRuntime runtime
struct Collection * receiver_collection
LightLinking * light_linking
light_linking::Cache light_linking_cache
Definition depsgraph.hh:187