Blender V5.3
deg_eval_copy_on_write.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2017 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9/* Enable special trickery to treat nested owned IDs (such as nodetree of
10 * material) to be handled in same way as "real" data-blocks, even tho some
11 * internal BKE routines doesn't treat them like that.
12 *
13 * TODO(sergey): Re-evaluate that after new ID handling is in place. */
14#define NESTED_ID_NASTY_WORKAROUND
15
16/* Silence warnings from copying deprecated fields. */
17#define DNA_DEPRECATED_ALLOW
18
20
21#include <cstring>
22
23#include "BLI_listbase.hh"
24#include "BLI_utildefines.hh"
25
26#include "BKE_curve.hh"
27#include "BKE_global.hh"
28#include "BKE_layer.hh"
29#include "BKE_lib_id.hh"
30#include "BKE_scene.hh"
31
32#include "DEG_depsgraph.hh"
34
35#include "MEM_guardedalloc.h"
36
37#include "DNA_ID.h"
38#include "DNA_anim_types.h"
39#include "DNA_armature_types.h"
40#include "DNA_mesh_types.h"
41#include "DNA_modifier_types.h"
42#include "DNA_object_types.h"
43#include "DNA_particle_types.h"
44#include "DNA_scene_types.h"
45
46#include "DRW_engine.hh"
47
48#ifdef NESTED_ID_NASTY_WORKAROUND
49# include "DNA_curve_types.h"
50# include "DNA_key_types.h"
51# include "DNA_lattice_types.h"
52# include "DNA_light_types.h"
53# include "DNA_linestyle_types.h"
54# include "DNA_material_types.h"
55# include "DNA_meta_types.h"
56# include "DNA_node_types.h"
57# include "DNA_texture_types.h"
58# include "DNA_world_types.h"
59#endif
60
61#include "BKE_anim_data.hh"
62#include "BKE_animsys.hh"
63#include "BKE_armature.hh"
64#include "BKE_editmesh.hh"
65#include "BKE_lib_query.hh"
66#include "BKE_mesh_types.hh"
67#include "BKE_modifier.hh"
68#include "BKE_object.hh"
69#include "BKE_pointcache.h"
70
71#include "SEQ_relations.hh"
72
75#include "intern/depsgraph.hh"
79
80namespace blender::deg {
81
82#define DEBUG_PRINT \
83 if (G.debug & G_DEBUG_DEPSGRAPH_EVAL) \
84 printf
85
86namespace {
87
88#ifdef NESTED_ID_NASTY_WORKAROUND
89constexpr size_t NestedIDHackTempStorage = std::max({sizeof(Curve),
90 sizeof(FreestyleLineStyle),
91 sizeof(Light),
92 sizeof(Lattice),
93 sizeof(Material),
94 sizeof(Mesh),
95 sizeof(Scene),
96 sizeof(Tex),
97 sizeof(World)});
98
99/* Set nested owned ID pointers to nullptr. */
100void nested_id_hack_discard_pointers(ID *id_cow)
101{
102 switch (GS(id_cow->name)) {
103# define SPECIAL_CASE(id_type, dna_type, field) \
104 case id_type: { \
105 ((dna_type *)id_cow)->field = nullptr; \
106 break; \
107 }
108
109 SPECIAL_CASE(ID_LS, FreestyleLineStyle, nodetree)
110 SPECIAL_CASE(ID_LA, Light, nodetree)
111 SPECIAL_CASE(ID_MA, Material, nodetree)
112 SPECIAL_CASE(ID_TE, Tex, nodetree)
113 SPECIAL_CASE(ID_WO, World, nodetree)
114
115 SPECIAL_CASE(ID_CU_LEGACY, Curve, key)
116 SPECIAL_CASE(ID_LT, Lattice, key)
117 SPECIAL_CASE(ID_ME, Mesh, key)
118
119 case ID_SCE: {
120 Scene *scene_cow = id_cast<Scene *>(id_cow);
121 /* Tool settings pointer is shared with the original scene. */
122 scene_cow->toolsettings = nullptr;
123 break;
124 }
125
126 case ID_OB: {
127 /* Clear the ParticleSettings pointer to prevent doubly-freeing it. */
128 Object *ob = id_cast<Object *>(id_cow);
129 for (ParticleSystem &psys : ob->particlesystem) {
130 psys.part = nullptr;
131 }
132 break;
133 }
134# undef SPECIAL_CASE
135
136 default:
137 break;
138 }
139}
140
141/* Set ID pointer of nested owned IDs (nodetree, key) to nullptr.
142 *
143 * Return pointer to a new ID to be used. */
144const ID *nested_id_hack_get_discarded_pointers(void *storage, const ID *id)
145{
146 switch (GS(id->name)) {
147# define SPECIAL_CASE(id_type, dna_type, field, variable) \
148 case id_type: { \
149 dna_type *data = static_cast<dna_type *>(storage); \
150 *data = dna::shallow_copy(*(dna_type *)id); \
151 data->field = nullptr; \
152 return &data->id; \
153 }
154
155 SPECIAL_CASE(ID_LS, FreestyleLineStyle, nodetree, linestyle)
156 SPECIAL_CASE(ID_LA, Light, nodetree, lamp)
157 SPECIAL_CASE(ID_MA, Material, nodetree, material)
158 SPECIAL_CASE(ID_TE, Tex, nodetree, tex)
159 SPECIAL_CASE(ID_WO, World, nodetree, world)
160
161 SPECIAL_CASE(ID_CU_LEGACY, Curve, key, curve)
162 SPECIAL_CASE(ID_LT, Lattice, key, lattice)
163 SPECIAL_CASE(ID_ME, Mesh, key, mesh)
164
165 case ID_SCE: {
166 Scene *scene = static_cast<Scene *>(storage);
167 *scene = dna::shallow_copy(*id_cast<Scene *>(const_cast<ID *>(id)));
168 scene->toolsettings = nullptr;
169 return &scene->id;
170 }
171
172# undef SPECIAL_CASE
173
174 default:
175 break;
176 }
177 return id;
178}
179
180/* Set ID pointer of nested owned IDs (nodetree, key) to the original value. */
181void nested_id_hack_restore_pointers(const ID *old_id, ID *new_id)
182{
183 if (new_id == nullptr) {
184 return;
185 }
186 switch (GS(old_id->name)) {
187# define SPECIAL_CASE(id_type, dna_type, field) \
188 case id_type: { \
189 ((dna_type *)(new_id))->field = ((dna_type *)(old_id))->field; \
190 break; \
191 }
192
193 SPECIAL_CASE(ID_LS, FreestyleLineStyle, nodetree)
194 SPECIAL_CASE(ID_LA, Light, nodetree)
195 SPECIAL_CASE(ID_MA, Material, nodetree)
196 SPECIAL_CASE(ID_TE, Tex, nodetree)
197 SPECIAL_CASE(ID_WO, World, nodetree)
198
199 SPECIAL_CASE(ID_CU_LEGACY, Curve, key)
200 SPECIAL_CASE(ID_LT, Lattice, key)
201 SPECIAL_CASE(ID_ME, Mesh, key)
202
203# undef SPECIAL_CASE
204 default:
205 break;
206 }
207}
208
209/* Remap pointer of nested owned IDs (nodetree. key) to the new ID values. */
210void ntree_hack_remap_pointers(const Depsgraph *depsgraph, ID *id_cow)
211{
212 switch (GS(id_cow->name)) {
213# define SPECIAL_CASE(id_type, dna_type, field, field_type) \
214 case id_type: { \
215 dna_type *data = (dna_type *)id_cow; \
216 if (data->field != nullptr) { \
217 ID *ntree_id_cow = depsgraph->get_cow_id(&data->field->id); \
218 if (ntree_id_cow != nullptr) { \
219 DEG_COW_PRINT(" Remapping datablock for %s: id_orig=%p id_cow=%p\n", \
220 data->field->id.name, \
221 data->field, \
222 ntree_id_cow); \
223 data->field = (field_type *)ntree_id_cow; \
224 } \
225 } \
226 break; \
227 }
228
229 SPECIAL_CASE(ID_LS, FreestyleLineStyle, nodetree, bNodeTree)
230 SPECIAL_CASE(ID_LA, Light, nodetree, bNodeTree)
231 SPECIAL_CASE(ID_MA, Material, nodetree, bNodeTree)
232 SPECIAL_CASE(ID_TE, Tex, nodetree, bNodeTree)
233 SPECIAL_CASE(ID_WO, World, nodetree, bNodeTree)
234
235 SPECIAL_CASE(ID_CU_LEGACY, Curve, key, Key)
236 SPECIAL_CASE(ID_LT, Lattice, key, Key)
237 SPECIAL_CASE(ID_ME, Mesh, key, Key)
238
239# undef SPECIAL_CASE
240 default:
241 break;
242 }
243}
244#endif /* NODETREE_NASTY_WORKAROUND */
245
246struct ValidateData {
247 bool is_valid;
248};
249
250/* Similar to generic BKE_id_copy() but does not require main and assumes pointer
251 * is already allocated. */
252bool id_copy_inplace_no_main(const ID *id, ID *newid)
253{
254 const ID *id_for_copy = id;
255
256 if (G.debug & G_DEBUG_DEPSGRAPH_UID) {
257 const ID_Type id_type = GS(id_for_copy->name);
258 if (id_type == ID_OB) {
259 const Object *object = reinterpret_cast<const Object *>(id_for_copy);
261 }
262 }
263
264#ifdef NESTED_ID_NASTY_WORKAROUND
265 uint8_t id_hack_storage[NestedIDHackTempStorage];
266 id_for_copy = nested_id_hack_get_discarded_pointers(&id_hack_storage, id);
267#endif
268
269 bool result = (BKE_id_copy_ex(nullptr,
270 const_cast<ID *>(id_for_copy),
271 &newid,
274
275#ifdef NESTED_ID_NASTY_WORKAROUND
276 if (result) {
277 nested_id_hack_restore_pointers(id, newid);
278 }
279#endif
280
281 return result;
282}
283
284/* Similar to BKE_scene_copy() but does not require main and assumes pointer
285 * is already allocated. */
286bool scene_copy_inplace_no_main(const Scene *scene, Scene *new_scene)
287{
288
289 if (G.debug & G_DEBUG_DEPSGRAPH_UID) {
291 }
292
293#ifdef NESTED_ID_NASTY_WORKAROUND
294 uint8_t id_hack_storage[NestedIDHackTempStorage];
295 const ID *id_for_copy = nested_id_hack_get_discarded_pointers(&id_hack_storage, &scene->id);
296#else
297 const ID *id_for_copy = &scene->id;
298#endif
299 bool result = (BKE_id_copy_ex(nullptr,
300 id_for_copy,
301 reinterpret_cast<ID **>(&new_scene),
304
305#ifdef NESTED_ID_NASTY_WORKAROUND
306 if (result) {
307 nested_id_hack_restore_pointers(&scene->id, &new_scene->id);
308 }
309#endif
310
311 return result;
312}
313
314/* For the given scene get view layer which corresponds to an original for the
315 * scene's evaluated one. This depends on how the scene is pulled into the
316 * dependency graph. */
317ViewLayer *get_original_view_layer(const Depsgraph *depsgraph, const IDNode *id_node)
318{
319 if (id_node->linked_state == DEG_ID_LINKED_DIRECTLY) {
320 return depsgraph->view_layer;
321 }
322 if (id_node->linked_state == DEG_ID_LINKED_VIA_SET) {
323 Scene *scene_orig = reinterpret_cast<Scene *>(id_node->id_orig);
324 return BKE_view_layer_default_render(scene_orig);
325 }
326 /* Is possible to have scene linked indirectly (i.e. via the driver) which
327 * we need to support. Currently there are issues somewhere else, which
328 * makes testing hard. This is a reported problem, so will eventually be
329 * properly fixed.
330 *
331 * TODO(sergey): Support indirectly linked scene. */
332 return nullptr;
333}
334
335/* Remove all bases from all view layers except the input one. */
336void scene_minimize_unused_view_layers(const Depsgraph *depsgraph,
337 const IDNode *id_node,
338 Scene *scene_cow)
339{
340 if (depsgraph->is_render_pipeline_depsgraph) {
341 /* If the dependency graph is used for post-processing (such as compositor) we do need to
342 * have access to its view layer names so can not remove any view layers.
343 * On a more positive side we can remove all the bases from all the view layers.
344 *
345 * NOTE: Need to clear pointers which might be pointing to original on freed (due to being
346 * unused) data.
347 *
348 * NOTE: Need to keep view layers for all scenes, even indirect ones. This is because of
349 * render layer node possibly pointing to another scene. */
350 for (ViewLayer &view_layer : scene_cow->view_layers) {
352 }
353 return;
354 }
355
356 const ViewLayer *view_layer_input = get_original_view_layer(depsgraph, id_node);
357 ViewLayer *view_layer_eval = nullptr;
358 /* Find evaluated view layer. At the same time we free memory used by
359 * all other of the view layers. */
360 for (ViewLayer *view_layer_cow = reinterpret_cast<ViewLayer *>(scene_cow->view_layers.first_),
361 *view_layer_next;
362 view_layer_cow != nullptr;
363 view_layer_cow = view_layer_next)
364 {
365 view_layer_next = view_layer_cow->next;
366 if (view_layer_input != nullptr && STREQ(view_layer_input->name, view_layer_cow->name)) {
367 view_layer_eval = view_layer_cow;
368 }
369 else {
371 }
372 }
373
374 /* Make evaluated view layer the first one in the evaluated scene (if it exists). This is for
375 * legacy sake, as this used to remove all other view layers, automatically making the evaluated
376 * one the first. Some other code may still assume it is. */
377 if (view_layer_eval != nullptr) {
379 &scene_cow->view_layers, scene_cow->view_layers.first_, view_layer_eval);
380 }
381}
382
383void scene_remove_all_bases(Scene *scene_cow)
384{
385 for (ViewLayer &view_layer : scene_cow->view_layers) {
386 view_layer.object_bases.free_no_destruct();
387 }
388}
389
390/* Makes it so given view layer only has bases corresponding to enabled
391 * objects. */
392void view_layer_remove_disabled_bases(const Depsgraph *depsgraph,
393 const Scene *scene,
394 ViewLayer *view_layer)
395{
396 if (view_layer == nullptr) {
397 return;
398 }
399 ListBaseT<Base> enabled_bases = {nullptr, nullptr};
400 BKE_view_layer_synced_ensure(*depsgraph->bmain, scene, view_layer);
401 for (Base &base : BKE_view_layer_object_bases_get(view_layer)->items_mutable()) {
402 /* TODO(sergey): Would be cool to optimize this somehow, or make it so
403 * builder tags bases.
404 *
405 * NOTE: The idea of using id's tag and check whether its copied ot not
406 * is not reliable, since object might be indirectly linked into the
407 * graph.
408 *
409 * NOTE: We are using original base since the object which evaluated base
410 * points to is not yet copied. This is dangerous access from evaluated
411 * domain to original one, but this is how the entire copy-on-evaluation works:
412 * it does need to access original for an initial copy. */
413 const bool is_object_enabled = deg_check_base_in_depsgraph(depsgraph, &base);
414 if (is_object_enabled) {
415 BLI_addtail(&enabled_bases, &base);
416 }
417 else {
418 if (&base == view_layer->basact) {
419 view_layer->basact = nullptr;
420 }
421 MEM_delete(&base);
422 }
423 }
424 view_layer->object_bases = enabled_bases;
425}
426
427void view_layer_update_orig_base_pointers(const ViewLayer *view_layer_orig,
428 ViewLayer *view_layer_eval)
429{
430 if (view_layer_orig == nullptr || view_layer_eval == nullptr) {
431 /* Happens when scene is only used for parameters or compositor/sequencer. */
432 return;
433 }
434 Base *base_orig = reinterpret_cast<Base *>(view_layer_orig->object_bases.first_);
435 for (Base &base_eval : view_layer_eval->object_bases) {
436 base_eval.base_orig = base_orig;
437 base_orig = base_orig->next;
438 }
439}
440
441void scene_setup_view_layers_before_remap(const Depsgraph *depsgraph,
442 const IDNode *id_node,
443 Scene *scene_cow)
444{
445 scene_minimize_unused_view_layers(depsgraph, id_node, scene_cow);
446 /* If dependency graph is used for post-processing we don't need any bases and can free of them.
447 * Do it before re-mapping to make that process faster. */
448 if (depsgraph->is_render_pipeline_depsgraph) {
449 scene_remove_all_bases(scene_cow);
450 }
451}
452
453void scene_setup_view_layers_after_remap(const Depsgraph *depsgraph,
454 const IDNode *id_node,
455 Scene *scene_cow)
456{
457 const ViewLayer *view_layer_orig = get_original_view_layer(depsgraph, id_node);
458 ViewLayer *view_layer_eval = reinterpret_cast<ViewLayer *>(scene_cow->view_layers.first_);
459 view_layer_update_orig_base_pointers(view_layer_orig, view_layer_eval);
460 view_layer_remove_disabled_bases(depsgraph, scene_cow, view_layer_eval);
461 /* TODO(sergey): Remove objects from collections as well.
462 * Not a HUGE deal for now, nobody is looking into those CURRENTLY.
463 * Still not an excuse to have those. */
464}
465
466/* Check whether given ID is expanded or still a shallow copy. */
467inline bool check_datablock_expanded(const ID *id_cow)
468{
469 return (id_cow->name[0] != '\0');
470}
471
472/* Callback for BKE_library_foreach_ID_link which remaps original ID pointer
473 * with the one created by copy-on-evaluation system. */
474
475struct RemapCallbackUserData {
476 /* Dependency graph for which remapping is happening. */
477 const Depsgraph *depsgraph;
478};
479
480int foreach_libblock_remap_callback(LibraryIDLinkCallbackData *cb_data)
481{
482 ID **id_p = cb_data->id_pointer;
483 if (*id_p == nullptr) {
484 return IDWALK_RET_NOP;
485 }
486
487 BLI_assert(cb_data->owner_id);
488 BLI_assert(cb_data->owner_id->tag & ID_TAG_COPIED_ON_EVAL);
489
490 RemapCallbackUserData *user_data = static_cast<RemapCallbackUserData *>(cb_data->user_data);
491 const Depsgraph *depsgraph = user_data->depsgraph;
492 ID *id_orig = *id_p;
493 if (deg_eval_copy_is_needed(id_orig)) {
494 ID *id_cow = depsgraph->get_cow_id(id_orig);
495 BLI_assert(id_cow != nullptr);
497 " Remapping data-block for %s: id_orig=%p id_cow=%p\n", id_orig->name, id_orig, id_cow);
498 *id_p = id_cow;
499 }
500 return IDWALK_RET_NOP;
501}
502
503void update_armature_edit_mode_pointers(const Depsgraph * /*depsgraph*/,
504 const ID *id_orig,
505 ID *id_cow)
506{
507 const bArmature *armature_orig = id_cast<const bArmature *>(id_orig);
508 bArmature *armature_cow = id_cast<bArmature *>(id_cow);
509 armature_cow->edbo = armature_orig->edbo;
510 armature_cow->act_edbone = armature_orig->act_edbone;
511}
512
513void update_curve_edit_mode_pointers(const Depsgraph * /*depsgraph*/,
514 const ID *id_orig,
515 ID *id_cow)
516{
517 const Curve *curve_orig = id_cast<const Curve *>(id_orig);
518 Curve *curve_cow = id_cast<Curve *>(id_cow);
519 curve_cow->editnurb = curve_orig->editnurb;
520 curve_cow->editfont = curve_orig->editfont;
521}
522
523void update_mball_edit_mode_pointers(const Depsgraph * /*depsgraph*/,
524 const ID *id_orig,
525 ID *id_cow)
526{
527 const MetaBall *mball_orig = id_cast<const MetaBall *>(id_orig);
528 MetaBall *mball_cow = id_cast<MetaBall *>(id_cow);
529 mball_cow->editelems = mball_orig->editelems;
530}
531
532void update_lattice_edit_mode_pointers(const Depsgraph * /*depsgraph*/,
533 const ID *id_orig,
534 ID *id_cow)
535{
536 const Lattice *lt_orig = id_cast<const Lattice *>(id_orig);
537 Lattice *lt_cow = id_cast<Lattice *>(id_cow);
538 lt_cow->editlatt = lt_orig->editlatt;
539}
540
541void update_mesh_edit_mode_pointers(const ID *id_orig, ID *id_cow)
542{
543 const Mesh *mesh_orig = id_cast<const Mesh *>(id_orig);
544 Mesh *mesh_cow = id_cast<Mesh *>(id_cow);
545 if (mesh_orig->runtime->edit_mesh == nullptr) {
546 return;
547 }
548 mesh_cow->runtime->edit_mesh = mesh_orig->runtime->edit_mesh;
549}
550
551/* Edit data is stored and owned by original datablocks, copied ones
552 * are simply referencing to them. */
553void update_edit_mode_pointers(const Depsgraph *depsgraph, const ID *id_orig, ID *id_cow)
554{
555 const ID_Type type = GS(id_orig->name);
556 switch (type) {
557 case ID_AR:
558 update_armature_edit_mode_pointers(depsgraph, id_orig, id_cow);
559 break;
560 case ID_ME:
561 update_mesh_edit_mode_pointers(id_orig, id_cow);
562 break;
563 case ID_CU_LEGACY:
564 update_curve_edit_mode_pointers(depsgraph, id_orig, id_cow);
565 break;
566 case ID_MB:
567 update_mball_edit_mode_pointers(depsgraph, id_orig, id_cow);
568 break;
569 case ID_LT:
570 update_lattice_edit_mode_pointers(depsgraph, id_orig, id_cow);
571 break;
572 default:
573 break;
574 }
575}
576
577template<typename T>
578void update_list_orig_pointers(const ListBaseT<T> *listbase_orig,
579 ListBaseT<T> *listbase,
580 T *T::*orig_field)
581{
582 T *element_orig = reinterpret_cast<T *>(listbase_orig->first_);
583 T *element_cow = reinterpret_cast<T *>(listbase->first_);
584
585 /* Both lists should have the same number of elements, so the check on
586 * `element_cow` is just to prevent a crash if this is not the case. */
587 while (element_orig != nullptr && element_cow != nullptr) {
588 element_cow->*orig_field = element_orig;
589 element_cow = element_cow->next;
590 element_orig = element_orig->next;
591 }
592
593 BLI_assert_msg(element_orig == nullptr && element_cow == nullptr,
594 "list of pointers of different sizes, unable to reliably set orig pointer");
595}
596
597void update_particle_system_orig_pointers(const Object *object_orig, Object *object_cow)
598{
599 update_list_orig_pointers(
600 &object_orig->particlesystem, &object_cow->particlesystem, &ParticleSystem::orig_psys);
601}
602
603void set_particle_system_modifiers_loaded(Object *object_cow)
604{
605 for (ModifierData &md : object_cow->modifiers) {
606 if (md.type != eModifierType_ParticleSystem) {
607 continue;
608 }
609 ParticleSystemModifierData *psmd = reinterpret_cast<ParticleSystemModifierData *>(&md);
611 }
612}
613
614void reset_particle_system_edit_eval(const Depsgraph *depsgraph, Object *object_cow)
615{
616 /* Inactive (and render) dependency graphs are living in their own little bubble, should not care
617 * about edit mode at all. */
618 if (!DEG_is_active(reinterpret_cast<const ::blender::Depsgraph *>(depsgraph))) {
619 return;
620 }
621 for (ParticleSystem &psys : object_cow->particlesystem) {
622 ParticleSystem *orig_psys = psys.orig_psys;
623 if (orig_psys->edit != nullptr) {
624 orig_psys->edit->psys_eval = nullptr;
625 orig_psys->edit->psmd_eval = nullptr;
626 }
627 }
628}
629
630void update_particles_after_copy(const Depsgraph *depsgraph,
631 const Object *object_orig,
632 Object *object_cow)
633{
634 update_particle_system_orig_pointers(object_orig, object_cow);
635 set_particle_system_modifiers_loaded(object_cow);
636 reset_particle_system_edit_eval(depsgraph, object_cow);
637}
638
639void update_pose_orig_pointers(const bPose *pose_orig, bPose *pose_cow)
640{
641 update_list_orig_pointers(&pose_orig->chanbase, &pose_cow->chanbase, &bPoseChannel::orig_pchan);
642}
643
644void update_nla_strips_orig_pointers(const ListBaseT<NlaStrip> *strips_orig,
645 ListBaseT<NlaStrip> *strips_cow)
646{
647 NlaStrip *strip_orig = reinterpret_cast<NlaStrip *>(strips_orig->first_);
648 NlaStrip *strip_cow = reinterpret_cast<NlaStrip *>(strips_cow->first_);
649 while (strip_orig != nullptr) {
650 strip_cow->orig_strip = strip_orig;
651 update_nla_strips_orig_pointers(&strip_orig->strips, &strip_cow->strips);
652 strip_cow = strip_cow->next;
653 strip_orig = strip_orig->next;
654 }
655}
656
657void update_nla_tracks_orig_pointers(const ListBaseT<NlaTrack> *tracks_orig,
658 ListBaseT<NlaTrack> *tracks_cow)
659{
660 NlaTrack *track_orig = reinterpret_cast<NlaTrack *>(tracks_orig->first_);
661 NlaTrack *track_cow = reinterpret_cast<NlaTrack *>(tracks_cow->first_);
662 while (track_orig != nullptr) {
663 update_nla_strips_orig_pointers(&track_orig->strips, &track_cow->strips);
664 track_cow = track_cow->next;
665 track_orig = track_orig->next;
666 }
667}
668
669void update_animation_data_after_copy(const ID *id_orig, ID *id_cow)
670{
671 const AnimData *anim_data_orig = BKE_animdata_from_id(const_cast<ID *>(id_orig));
672 if (anim_data_orig == nullptr) {
673 return;
674 }
675 AnimData *anim_data_cow = BKE_animdata_from_id(id_cow);
676 BLI_assert(anim_data_cow != nullptr);
677 update_nla_tracks_orig_pointers(&anim_data_orig->nla_tracks, &anim_data_cow->nla_tracks);
678 /* If the driver count on the evaluated ID is different, we will get a crash when trying to
679 * evaluate the drivers because it will read an FCurve out of Array bounds. See #158665. */
680 BLI_assert(BLI_listbase_count(&anim_data_orig->drivers) ==
681 BLI_listbase_count(&anim_data_cow->drivers));
682}
683
684/* Do some special treatment of data transfer from original ID to its
685 * evaluated complementary part.
686 *
687 * Only use for the newly created evaluated data-blocks. */
688void update_id_after_copy(const Depsgraph *depsgraph,
689 const IDNode *id_node,
690 const ID *id_orig,
691 ID *id_cow)
692{
693 const ID_Type type = GS(id_orig->name);
694 update_animation_data_after_copy(id_orig, id_cow);
695 switch (type) {
696 case ID_OB: {
697 /* Ensure we don't drag someone's else derived mesh to the
698 * new copy of the object. */
699 Object *object_cow = id_cast<Object *>(id_cow);
700 const Object *object_orig = id_cast<const Object *>(id_orig);
701 object_cow->mode = object_orig->mode;
702 object_cow->runtime->sculpt_session = object_orig->runtime->sculpt_session;
703 object_cow->runtime->data_orig = object_cow->data;
704 if (object_cow->type == OB_ARMATURE) {
705 const bArmature *armature_orig = id_cast<bArmature *>(object_orig->data);
706 if (armature_orig->edbo == nullptr) {
707 update_pose_orig_pointers(object_orig->pose, object_cow->pose);
708 }
709 BKE_pose_pchan_index_rebuild(object_cow->pose);
710 }
711 update_particles_after_copy(depsgraph, object_orig, object_cow);
712 break;
713 }
714 case ID_SCE: {
715 Scene *scene_cow = id_cast<Scene *>(id_cow);
716 const Scene *scene_orig = id_cast<const Scene *>(id_orig);
717 scene_cow->toolsettings = scene_orig->toolsettings;
718 scene_setup_view_layers_after_remap(depsgraph, id_node, reinterpret_cast<Scene *>(id_cow));
719 break;
720 }
721 default:
722 break;
723 }
724 update_edit_mode_pointers(depsgraph, id_orig, id_cow);
726}
727
728/* This callback is used to validate that all nested ID data-blocks are
729 * properly expanded. */
730int foreach_libblock_validate_callback(LibraryIDLinkCallbackData *cb_data)
731{
732 ValidateData *data = static_cast<ValidateData *>(cb_data->user_data);
733 ID **id_p = cb_data->id_pointer;
734
735 if (*id_p != nullptr) {
736 if (!check_datablock_expanded(*id_p)) {
737 data->is_valid = false;
738 /* TODO(sergey): Store which is not valid? */
739 }
740 }
741 return IDWALK_RET_NOP;
742}
743
744/* Actual implementation of logic which "expands" all the data which was not
745 * yet copied-on-eval.
746 *
747 * NOTE: Expects that evaluated datablock is empty. */
748ID *deg_expand_eval_copy_datablock(const Depsgraph *depsgraph, const IDNode *id_node)
749{
750 const ID *id_orig = id_node->id_orig;
751 ID *id_cow = id_node->id_cow;
752 const int id_cow_recalc = id_cow->recalc;
753
754 /* No need to expand such datablocks, their copied ID is same as original
755 * one already. */
756 if (!deg_eval_copy_is_needed(id_orig)) {
757 return id_cow;
758 }
759
761 "Expanding datablock for %s: id_orig=%p id_cow=%p\n", id_orig->name, id_orig, id_cow);
762
763 /* Sanity checks.
764 *
765 * At this point, `id_cow` is essentially considered as a (partially dirty) allocated buffer (it
766 * has been freed, but not fully cleared, as a result of calling #deg_free_eval_copy_datablock on
767 * it). It is not expected to have any valid sub-data, not even a valid `ID::runtime` pointer.
768 */
769 BLI_assert(check_datablock_expanded(id_cow) == false);
770 BLI_assert(id_cow->py_instance == nullptr);
771 BLI_assert(id_cow->runtime == nullptr);
772
773 /* Copy data from original ID to a copied version. */
774 /* TODO(sergey): We do some trickery with temp bmain and extra ID pointer
775 * just to be able to use existing API. Ideally we need to replace this with
776 * in-place copy from existing datablock to a prepared memory.
777 *
778 * NOTE: We don't use BKE_main_{new,free} because:
779 * - We don't want heap-allocations here.
780 * - We don't want bmain's content to be freed when main is freed. */
781 bool done = false;
782 /* First we handle special cases which are not covered by BKE_id_copy() yet.
783 * or cases where we want to do something smarter than simple datablock
784 * copy. */
785 const ID_Type id_type = GS(id_orig->name);
786 switch (id_type) {
787 case ID_SCE: {
788 done = scene_copy_inplace_no_main(id_cast<Scene *>(const_cast<ID *>(id_orig)),
789 reinterpret_cast<Scene *>(id_cow));
790 if (done) {
791 /* NOTE: This is important to do before remap, because this
792 * function will make it so less IDs are to be remapped. */
793 scene_setup_view_layers_before_remap(depsgraph, id_node, id_cast<Scene *>(id_cow));
794 }
795 break;
796 }
797 case ID_ME: {
798 /* TODO(sergey): Ideally we want to handle meshes in a special
799 * manner here to avoid initial copy of all the geometry arrays. */
800 break;
801 }
802 default:
803 break;
804 }
805 if (!done) {
806 done = id_copy_inplace_no_main(id_orig, id_cow);
807 }
808 if (!done) {
809 BLI_assert_msg(0, "No idea how to perform evaluated copy on datablock");
810 }
811 /* Update pointers to nested ID datablocks. */
813 " Remapping ID links for %s: id_orig=%p id_cow=%p\n", id_orig->name, id_orig, id_cow);
814
815#ifdef NESTED_ID_NASTY_WORKAROUND
816 ntree_hack_remap_pointers(depsgraph, id_cow);
817#endif
818 /* Do it now, so remapping will understand that possibly remapped self ID
819 * is not to be remapped again. */
820 deg_tag_eval_copy_id(const_cast<Depsgraph &>(*depsgraph), id_cow, id_orig);
821 /* Perform remapping of the nodes. */
822 RemapCallbackUserData user_data = {nullptr};
823 user_data.depsgraph = depsgraph;
824 /* About IDWALK flags:
825 * - #IDWALK_IGNORE_EMBEDDED_ID: In depsgraph embedded IDs are handled (mostly) as regular IDs,
826 * and processed on their own, not as part of their owner ID (the owner ID's pointer to its
827 * embedded data is set to null before actual copying, in #id_copy_inplace_no_main).
828 * - #IDWALK_IGNORE_MISSING_OWNER_ID is necessary for the same reason: when directly processing
829 * an embedded ID here, its owner is unknown, and its internal owner ID pointer is not yet
830 * remapped, so it is currently 'invalid'. */
832 id_cow,
833 foreach_libblock_remap_callback,
834 static_cast<void *>(&user_data),
836 /* Correct or tweak some pointers which are not taken care by foreach
837 * from above. */
838 update_id_after_copy(depsgraph, id_node, id_orig, id_cow);
839 id_cow->recalc = id_cow_recalc;
840 return id_cow;
841}
842
843} // namespace
844
845ID *deg_update_eval_copy_datablock(const Depsgraph *depsgraph, const IDNode *id_node)
846{
847 const ID *id_orig = id_node->id_orig;
848 ID *id_cow = id_node->id_cow;
849 /* Similar to expansion, no need to do anything here. */
850 if (!deg_eval_copy_is_needed(id_orig)) {
851 return id_cow;
852 }
853
854 /* When updating object data in edit-mode, don't request copy-on-eval update since this will
855 * duplicate all object data which is unnecessary when the edit-mode data is used for calculating
856 * modifiers.
857 *
858 * TODO: Investigate modes besides edit-mode. */
859 if (check_datablock_expanded(id_cow) && !id_node->is_cow_explicitly_tagged) {
860 const ID_Type id_type = GS(id_orig->name);
861 /* Pass nullptr as the object is only needed for Curves which do not have edit mode pointers.
862 */
863 if (OB_DATA_SUPPORT_EDITMODE(id_type) && BKE_object_data_is_in_editmode(nullptr, id_orig)) {
864 /* Make sure pointers in the edit mode data are updated in the copy.
865 * This allows depsgraph to pick up changes made in another context after it has been
866 * evaluated. Consider the following scenario:
867 *
868 * - ObjectA in SceneA is using Mesh.
869 * - ObjectB in SceneB is using Mesh (same exact datablock).
870 * - Depsgraph of SceneA is evaluated.
871 * - Depsgraph of SceneB is evaluated.
872 * - User enters edit mode of ObjectA in SceneA. */
873 update_edit_mode_pointers(depsgraph, id_orig, id_cow);
874 return id_cow;
875 }
876 }
877
878 RuntimeBackup backup(depsgraph);
879 backup.init_from_id(id_cow);
881 deg_expand_eval_copy_datablock(depsgraph, id_node);
882 backup.restore_to_id(id_cow);
883 return id_cow;
884}
885
886ID *deg_update_eval_copy_datablock(const Depsgraph *depsgraph, ID *id_orig)
887{
888 /* NOTE: Depsgraph is supposed to have ID node already. */
889
890 IDNode *id_node = depsgraph->find_id_node(id_orig);
891 BLI_assert(id_node != nullptr);
892 return deg_update_eval_copy_datablock(depsgraph, id_node);
893}
894
895namespace {
896
897void discard_armature_edit_mode_pointers(ID *id_cow)
898{
899 bArmature *armature_cow = id_cast<bArmature *>(id_cow);
900 armature_cow->edbo = nullptr;
901}
902
903void discard_curve_edit_mode_pointers(ID *id_cow)
904{
905 Curve *curve_cow = id_cast<Curve *>(id_cow);
906 curve_cow->editnurb = nullptr;
907 curve_cow->editfont = nullptr;
908}
909
910void discard_mball_edit_mode_pointers(ID *id_cow)
911{
912 MetaBall *mball_cow = id_cast<MetaBall *>(id_cow);
913 mball_cow->editelems = nullptr;
914}
915
916void discard_lattice_edit_mode_pointers(ID *id_cow)
917{
918 Lattice *lt_cow = id_cast<Lattice *>(id_cow);
919 lt_cow->editlatt = nullptr;
920}
921
922void discard_mesh_edit_mode_pointers(ID *id_cow)
923{
924 Mesh *mesh_cow = id_cast<Mesh *>(id_cow);
925 mesh_cow->runtime->edit_mesh = nullptr;
926}
927
928void discard_scene_pointers(ID *id_cow)
929{
930 Scene *scene_cow = id_cast<Scene *>(id_cow);
931 scene_cow->toolsettings = nullptr;
932}
933
934/* nullptr-ify all edit mode pointers which points to data from
935 * original object. */
936void discard_edit_mode_pointers(ID *id_cow)
937{
938 const ID_Type type = GS(id_cow->name);
939 switch (type) {
940 case ID_AR:
941 discard_armature_edit_mode_pointers(id_cow);
942 break;
943 case ID_ME:
944 discard_mesh_edit_mode_pointers(id_cow);
945 break;
946 case ID_CU_LEGACY:
947 discard_curve_edit_mode_pointers(id_cow);
948 break;
949 case ID_MB:
950 discard_mball_edit_mode_pointers(id_cow);
951 break;
952 case ID_LT:
953 discard_lattice_edit_mode_pointers(id_cow);
954 break;
955 case ID_SCE:
956 /* Not really edit mode but still needs to run before
957 * BKE_libblock_free_datablock() */
958 discard_scene_pointers(id_cow);
959 break;
960 default:
961 break;
962 }
963}
964
965} // namespace
966
968{
969 /* Free content of the evaluated data-block.
970 * Notes:
971 * - Does not recurse into nested ID data-blocks.
972 * - Does not free data-block itself.
973 */
974
975 if (!check_datablock_expanded(id_cow)) {
976 /* Actual content was never copied on top of evaluated data-block, we have
977 * nothing to free. */
978 return;
979 }
980 const ID_Type type = GS(id_cow->name);
981#ifdef NESTED_ID_NASTY_WORKAROUND
982 nested_id_hack_discard_pointers(id_cow);
983#endif
984 switch (type) {
985 case ID_OB: {
986 /* TODO(sergey): This workaround is only to prevent free derived
987 * caches from modifying object->data. This is currently happening
988 * due to mesh/curve data-block bound-box tagging dirty. */
989 Object *ob_cow = id_cast<Object *>(id_cow);
990 ob_cow->data = nullptr;
991 ob_cow->runtime->sculpt_session = nullptr;
992 break;
993 }
994 default:
995 break;
996 }
997 discard_edit_mode_pointers(id_cow);
1000 BKE_libblock_free_data(id_cow, false);
1001 /* Signal datablock as not being expanded. */
1002 id_cow->name[0] = '\0';
1003}
1004
1005void deg_create_eval_copy(blender::Depsgraph *graph, const IDNode *id_node)
1006{
1007 const Depsgraph *depsgraph = reinterpret_cast<const Depsgraph *>(graph);
1008 DEG_debug_print_eval(graph, __func__, id_node->id_orig->name, id_node->id_cow);
1009 if (id_node->id_orig == &depsgraph->scene->id) {
1010 /* NOTE: This is handled by eval_ctx setup routines, which
1011 * ensures scene and view layer pointers are valid. */
1012 return;
1013 }
1014 deg_update_eval_copy_datablock(depsgraph, id_node);
1015}
1016
1018{
1019 if (id_cow == nullptr) {
1020 return false;
1021 }
1022 ValidateData data;
1023 data.is_valid = true;
1025 nullptr, id_cow, foreach_libblock_validate_callback, &data, IDWALK_NOP);
1026 return data.is_valid;
1027}
1028
1029void deg_tag_eval_copy_id(deg::Depsgraph &depsgraph, ID *id_cow, const ID *id_orig)
1030{
1031 BLI_assert(id_cow != id_orig);
1032 BLI_assert((id_orig->tag & ID_TAG_COPIED_ON_EVAL) == 0);
1033 id_cow->tag |= ID_TAG_COPIED_ON_EVAL;
1034 /* This ID is no longer localized, is a self-sustaining copy now. */
1035 id_cow->tag &= ~ID_TAG_LOCALIZED;
1036 id_cow->orig_id = const_cast<ID *>(id_orig);
1037 id_cow->runtime->depsgraph = &reinterpret_cast<::blender::Depsgraph &>(depsgraph);
1038}
1039
1041{
1042 return check_datablock_expanded(id_cow);
1043}
1044
1045bool deg_eval_copy_is_needed(const ID *id_orig)
1046{
1047 const ID_Type id_type = GS(id_orig->name);
1048 return deg_eval_copy_is_needed(id_type);
1049}
1050
1052{
1053 return ID_TYPE_USE_COPY_ON_EVAL(id_type);
1054}
1055
1056} // namespace blender::deg
General operations, lookup, etc. for blender objects.
ID and Library types, which are fundamental for SDNA.
#define ID_TYPE_USE_COPY_ON_EVAL(_id_type)
Definition DNA_ID.h:746
Object is a sort of wrapper for general info.
#define OB_DATA_SUPPORT_EDITMODE(_type)
Read Blender memory allocation and freeing.
#define BLI_assert(a)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
Definition scene.h:133
blender::ID_Type ID_Type
#define SPECIAL_CASE(id_type, dna_type, field)
#define DEG_COW_PRINT(format,...)
#define storage(slot, qualifiers)
#define T
#define G(x, y, z)
void deg_tag_eval_copy_id(deg::Depsgraph &depsgraph, ID *id_cow, const ID *id_orig)
bool deg_check_base_in_depsgraph(const Depsgraph *graph, Base *base)
bool deg_eval_copy_is_expanded(const ID *id_cow)
void deg_free_eval_copy_datablock(ID *id_cow)
bool deg_eval_copy_is_needed(const ID *id_orig)
ID * deg_update_eval_copy_datablock(const Depsgraph *depsgraph, const IDNode *id_node)
bool deg_validate_eval_copy_datablock(ID *id_cow)
void deg_create_eval_copy(blender::Depsgraph *graph, const IDNode *id_node)
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
void relations_check_uids_unique_and_report(const Scene *scene)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ eParticleSystemFlag_file_loaded
bool DEG_is_active(const Depsgraph *depsgraph)
Definition depsgraph.cc:324
@ IDWALK_IGNORE_EMBEDDED_ID
@ IDWALK_IGNORE_MISSING_OWNER_ID
@ eModifierType_ParticleSystem
void DEG_debug_print_eval(Depsgraph *depsgraph, const char *function_name, const char *object_name, const void *object_address)
@ LIB_ID_COPY_LOCALIZE
@ LIB_ID_COPY_SET_COPIED_ON_WRITE
@ LIB_ID_CREATE_NO_ALLOCATE
@ G_DEBUG_DEPSGRAPH_UID
void BKE_libblock_free_data(ID *id, bool do_id_user) ATTR_NONNULL()
AnimData * BKE_animdata_from_id(const ID *id)
Definition anim_data.cc:93
void BKE_pose_pchan_index_rebuild(bPose *pose)
BMesh const char void * data
void BKE_view_layer_free_object_content(ViewLayer *view_layer)
unsigned char uint8_t
Definition BKE_text.h:79
bool BKE_object_data_is_in_editmode(const Object *ob, const ID *id)
Definition object.cc:1932
void BLI_listbase_swaplinks(ListBase *listbase, void *vlinka, void *vlinkb) ATTR_NONNULL(1
Definition listbase.cc:171
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
void BKE_object_check_uids_unique_and_report(const Object *object)
Definition object.cc:5728
int BLI_listbase_count(const ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:531
void BKE_animsys_update_driver_array(ID *id)
Definition anim_sys.cc:3791
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
ID_Type GS(const char *name)
Definition DNA_ID.h:410
@ ID_TAG_COPIED_ON_EVAL
Definition DNA_ID.h:1000
@ ID_TAG_LOCALIZED
Definition DNA_ID.h:990
void BKE_libblock_free_datablock(ID *id, int flag) ATTR_NONNULL()
ID * BKE_id_copy_ex(Main *bmain, const ID *id, ID **new_id_p, int flag)
Definition lib_id.cc:851
ViewLayer * BKE_view_layer_default_render(const Scene *scene)
void BKE_library_foreach_ID_link(Main *bmain, ID *id, FunctionRef< LibraryIDLinkCallback > callback, void *user_data, LibraryForeachIDFlag flag)
Definition lib_query.cc:449
ListBaseT< Base > * BKE_view_layer_object_bases_get(ViewLayer *view_layer)
void BKE_libblock_free_data_py(ID *id)
const NodeType * type
Definition graph/node.h:179
char name[258]
Definition DNA_ID.h:433
bke::id::ID_Runtime * runtime
Definition DNA_ID.h:541
struct ID * orig_id
Definition DNA_ID.h:502
bke::ObjectRuntime * runtime
struct ParticleSystem * orig_psys
ListBaseT< EditBone > * edbo
struct bPoseChannel * orig_pchan
IDNode * find_id_node(const ID *id) const
Definition depsgraph.cc:102