Fix Spring characters being in the rest pose on file open
[blender.git] / source / blender / depsgraph / intern / depsgraph_tag.cc
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2013 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file \ingroup depsgraph
21  *
22  * Core routines for how the Depsgraph works.
23  */
24
25 #include "intern/depsgraph_tag.h"
26
27 #include <stdio.h>
28 #include <cstring>  /* required for memset */
29 #include <queue>
30
31 #include "BLI_utildefines.h"
32 #include "BLI_listbase.h"
33 #include "BLI_math_bits.h"
34 #include "BLI_task.h"
35
36 extern "C" {
37 #include "DNA_anim_types.h"
38 #include "DNA_curve_types.h"
39 #include "DNA_key_types.h"
40 #include "DNA_lattice_types.h"
41 #include "DNA_mesh_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_particle_types.h"
44 #include "DNA_screen_types.h"
45 #include "DNA_windowmanager_types.h"
46
47 #include "BKE_animsys.h"
48 #include "BKE_global.h"
49 #include "BKE_idcode.h"
50 #include "BKE_node.h"
51 #include "BKE_scene.h"
52 #include "BKE_workspace.h"
53
54 #define new new_
55 #include "BKE_screen.h"
56 #undef new
57 } /* extern "C" */
58
59 #include "DEG_depsgraph.h"
60 #include "DEG_depsgraph_debug.h"
61 #include "DEG_depsgraph_query.h"
62
63 #include "intern/builder/deg_builder.h"
64 #include "intern/depsgraph.h"
65 #include "intern/depsgraph_update.h"
66 #include "intern/eval/deg_eval_copy_on_write.h"
67 #include "intern/eval/deg_eval_flush.h"
68 #include "intern/node/deg_node.h"
69 #include "intern/node/deg_node_component.h"
70 #include "intern/node/deg_node_factory.h"
71 #include "intern/node/deg_node_id.h"
72 #include "intern/node/deg_node_operation.h"
73
74 /* *********************** */
75 /* Update Tagging/Flushing */
76
77 namespace DEG {
78
79 namespace {
80
81 void depsgraph_geometry_tag_to_component(const ID *id,
82                                          NodeType *component_type)
83 {
84         const NodeType result = geometry_tag_to_component(id);
85         if (result != NodeType::UNDEFINED) {
86                 *component_type = result;
87         }
88 }
89
90 bool is_selectable_data_id_type(const ID_Type id_type)
91 {
92         return ELEM(id_type, ID_ME, ID_CU, ID_MB, ID_LT, ID_GD);
93 }
94
95 void depsgraph_select_tag_to_component_opcode(
96         const ID *id,
97         NodeType *component_type,
98         OperationCode *operation_code)
99 {
100         const ID_Type id_type = GS(id->name);
101         if (id_type == ID_SCE) {
102                 /* We need to flush base flags to all objects in a scene since we
103                  * don't know which ones changed. However, we don't want to update
104                  * the whole scene, so pick up some operation which will do as less
105                  * as possible.
106                  *
107                  * TODO(sergey): We can introduce explicit exit operation which
108                  * does nothing and which is only used to cascade flush down the
109                  * road. */
110                 *component_type = NodeType::LAYER_COLLECTIONS;
111                 *operation_code = OperationCode::VIEW_LAYER_EVAL;
112         }
113         else if (id_type == ID_OB) {
114                 *component_type = NodeType::OBJECT_FROM_LAYER;
115                 *operation_code = OperationCode::OBJECT_BASE_FLAGS;
116         }
117         else if (id_type == ID_MC) {
118                 *component_type = NodeType::BATCH_CACHE;
119                 *operation_code = OperationCode::MOVIECLIP_SELECT_UPDATE;
120         }
121         else if (is_selectable_data_id_type(id_type)) {
122                 *component_type = NodeType::BATCH_CACHE;
123                 *operation_code = OperationCode::GEOMETRY_SELECT_UPDATE;
124         }
125         else {
126                 *component_type = NodeType::COPY_ON_WRITE;
127                 *operation_code = OperationCode::COPY_ON_WRITE;
128         }
129 }
130
131 void depsgraph_base_flags_tag_to_component_opcode(
132         const ID *id,
133         NodeType *component_type,
134         OperationCode *operation_code)
135 {
136         const ID_Type id_type = GS(id->name);
137         if (id_type == ID_SCE) {
138                 *component_type = NodeType::LAYER_COLLECTIONS;
139                 *operation_code = OperationCode::VIEW_LAYER_EVAL;
140         }
141         else if (id_type == ID_OB) {
142                 *component_type = NodeType::OBJECT_FROM_LAYER;
143                 *operation_code = OperationCode::OBJECT_BASE_FLAGS;
144         }
145 }
146
147 OperationCode psysTagToOperationCode(IDRecalcFlag tag)
148 {
149         if (tag == ID_RECALC_PSYS_RESET) {
150                 return OperationCode::PARTICLE_SETTINGS_RESET;
151         }
152         return OperationCode::OPERATION;
153 }
154
155 void depsgraph_tag_to_component_opcode(const ID *id,
156                                        IDRecalcFlag tag,
157                                        NodeType *component_type,
158                                        OperationCode *operation_code)
159 {
160         const ID_Type id_type = GS(id->name);
161         *component_type = NodeType::UNDEFINED;
162         *operation_code = OperationCode::OPERATION;
163         /* Special case for now, in the future we should get rid of this. */
164         if (tag == 0) {
165                 *component_type = NodeType::ID_REF;
166                 *operation_code = OperationCode::OPERATION;
167                 return;
168         }
169         switch (tag) {
170                 case ID_RECALC_TRANSFORM:
171                         *component_type = NodeType::TRANSFORM;
172                         break;
173                 case ID_RECALC_GEOMETRY:
174                         depsgraph_geometry_tag_to_component(id, component_type);
175                         break;
176                 case ID_RECALC_ANIMATION:
177                         *component_type = NodeType::ANIMATION;
178                         break;
179                 case ID_RECALC_PSYS_REDO:
180                 case ID_RECALC_PSYS_RESET:
181                 case ID_RECALC_PSYS_CHILD:
182                 case ID_RECALC_PSYS_PHYS:
183                         if (id_type == ID_PA) {
184                                 /* NOTES:
185                                  * - For particle settings node we need to use different
186                                  *   component. Will be nice to get this unified with object,
187                                  *   but we can survive for now with single exception here.
188                                  *   Particles needs reconsideration anyway, */
189                                 *component_type = NodeType::PARTICLE_SETTINGS;
190                                 *operation_code = psysTagToOperationCode(tag);
191                         }
192                         else {
193                                 *component_type = NodeType::PARTICLE_SYSTEM;
194                         }
195                         break;
196                 case ID_RECALC_COPY_ON_WRITE:
197                         *component_type = NodeType::COPY_ON_WRITE;
198                         break;
199                 case ID_RECALC_SHADING:
200                         if (id_type == ID_NT) {
201                                 *component_type = NodeType::SHADING_PARAMETERS;
202                         }
203                         else {
204                                 *component_type = NodeType::SHADING;
205                         }
206                         break;
207                 case ID_RECALC_SELECT:
208                         depsgraph_select_tag_to_component_opcode(id,
209                                                                  component_type,
210                                                                  operation_code);
211                         break;
212                 case ID_RECALC_BASE_FLAGS:
213                         depsgraph_base_flags_tag_to_component_opcode(id,
214                                                                      component_type,
215                                                                      operation_code);
216                         break;
217                 case ID_RECALC_POINT_CACHE:
218                         *component_type = NodeType::POINT_CACHE;
219                         break;
220                 case ID_RECALC_EDITORS:
221                         /* There is no such node in depsgraph, this tag is to be handled
222                          * separately. */
223                         break;
224                 case ID_RECALC_ALL:
225                 case ID_RECALC_PSYS_ALL:
226                         BLI_assert(!"Should not happen");
227                         break;
228         }
229 }
230
231 void id_tag_update_ntree_special(Main *bmain,
232                                  Depsgraph *graph,
233                                  ID *id,
234                                  int flag,
235                                  eUpdateSource update_source)
236 {
237         bNodeTree *ntree = ntreeFromID(id);
238         if (ntree == NULL) {
239                 return;
240         }
241         graph_id_tag_update(bmain, graph, &ntree->id, flag, update_source);
242 }
243
244 void depsgraph_update_editors_tag(Main *bmain, Depsgraph *graph, ID *id)
245 {
246         /* NOTE: We handle this immediately, without delaying anything, to be
247          * sure we don't cause threading issues with OpenGL. */
248         /* TODO(sergey): Make sure this works for CoW-ed datablocks as well. */
249         DEGEditorUpdateContext update_ctx = {NULL};
250         update_ctx.bmain = bmain;
251         update_ctx.depsgraph = (::Depsgraph *)graph;
252         update_ctx.scene = graph->scene;
253         update_ctx.view_layer = graph->view_layer;
254         deg_editors_id_update(&update_ctx, id);
255 }
256
257 void depsgraph_tag_component(Depsgraph *graph,
258                              IDNode *id_node,
259                              NodeType component_type,
260                              OperationCode operation_code,
261                              eUpdateSource update_source)
262 {
263         ComponentNode *component_node =
264                 id_node->find_component(component_type);
265         if (component_node == NULL) {
266                 return;
267         }
268         if (operation_code == OperationCode::OPERATION) {
269                 component_node->tag_update(graph, update_source);
270         }
271         else {
272                 OperationNode *operation_node =
273                         component_node->find_operation(operation_code);
274                 if (operation_node != NULL) {
275                         operation_node->tag_update(graph, update_source);
276                 }
277         }
278         /* If component depends on copy-on-write, tag it as well. */
279         if (component_node->need_tag_cow_before_update()) {
280                 ComponentNode *cow_comp =
281                         id_node->find_component(NodeType::COPY_ON_WRITE);
282                 cow_comp->tag_update(graph, update_source);
283         }
284 }
285
286 /* This is a tag compatibility with legacy code.
287  *
288  * Mainly, old code was tagging object with ID_RECALC_GEOMETRY tag to inform
289  * that object's data datablock changed. Now API expects that ID is given
290  * explicitly, but not all areas are aware of this yet. */
291 void deg_graph_id_tag_legacy_compat(Main *bmain,
292                                     Depsgraph *depsgraph,
293                                     ID *id,
294                                     IDRecalcFlag tag,
295                                     eUpdateSource update_source)
296 {
297         if (tag == ID_RECALC_GEOMETRY || tag == 0) {
298                 switch (GS(id->name)) {
299                         case ID_OB:
300                         {
301                                 Object *object = (Object *)id;
302                                 ID *data_id = (ID *)object->data;
303                                 if (data_id != NULL) {
304                                         graph_id_tag_update(
305                                                 bmain, depsgraph, data_id, 0, update_source);
306                                 }
307                                 break;
308                         }
309                         /* TODO(sergey): Shape keys are annoying, maybe we should find a
310                          * way to chain geometry evaluation to them, so we don't need extra
311                          * tagging here. */
312                         case ID_ME:
313                         {
314                                 Mesh *mesh = (Mesh *)id;
315                                 ID *key_id = &mesh->key->id;
316                                 if (key_id != NULL) {
317                                         graph_id_tag_update(
318                                                 bmain, depsgraph, key_id, 0, update_source);
319                                 }
320                                 break;
321                         }
322                         case ID_LT:
323                         {
324                                 Lattice *lattice = (Lattice *)id;
325                                 ID *key_id = &lattice->key->id;
326                                 if (key_id != NULL) {
327                                         graph_id_tag_update(
328                                                 bmain, depsgraph, key_id, 0, update_source);
329                                 }
330                                 break;
331                         }
332                         case ID_CU:
333                         {
334                                 Curve *curve = (Curve *)id;
335                                 ID *key_id = &curve->key->id;
336                                 if (key_id != NULL) {
337                                         graph_id_tag_update(
338                                                 bmain, depsgraph, key_id, 0, update_source);
339                                 }
340                                 break;
341                         }
342                         default:
343                                 break;
344                 }
345         }
346 }
347
348 static void graph_id_tag_update_single_flag(Main *bmain,
349                                             Depsgraph *graph,
350                                             ID *id,
351                                             IDNode *id_node,
352                                             IDRecalcFlag tag,
353                                             eUpdateSource update_source)
354 {
355         if (tag == ID_RECALC_EDITORS) {
356                 if (graph != NULL) {
357                         depsgraph_update_editors_tag(bmain, graph, id);
358                 }
359                 return;
360         }
361         /* Get description of what is to be tagged. */
362         NodeType component_type;
363         OperationCode operation_code;
364         depsgraph_tag_to_component_opcode(id,
365                                           tag,
366                                           &component_type,
367                                           &operation_code);
368         /* Check whether we've got something to tag. */
369         if (component_type == NodeType::UNDEFINED) {
370                 /* Given ID does not support tag. */
371                 /* TODO(sergey): Shall we raise some panic here? */
372                 return;
373         }
374         /* Tag ID recalc flag. */
375         DepsNodeFactory *factory = type_get_factory(component_type);
376         BLI_assert(factory != NULL);
377         id->recalc |= factory->id_recalc_tag();
378         /* Some sanity checks before moving forward. */
379         if (id_node == NULL) {
380                 /* Happens when object is tagged for update and not yet in the
381                  * dependency graph (but will be after relations update). */
382                 return;
383         }
384         /* Tag corresponding dependency graph operation for update. */
385         if (component_type == NodeType::ID_REF) {
386                 id_node->tag_update(graph, update_source);
387         }
388         else {
389                 depsgraph_tag_component(
390                         graph, id_node, component_type, operation_code, update_source);
391         }
392         /* TODO(sergey): Get rid of this once all areas are using proper data ID
393          * for tagging. */
394         deg_graph_id_tag_legacy_compat(bmain, graph, id, tag, update_source);
395
396 }
397
398 string stringify_append_bit(const string& str, IDRecalcFlag tag)
399 {
400         string result = str;
401         if (!result.empty()) {
402                 result += ", ";
403         }
404         result += DEG_update_tag_as_string(tag);
405         return result;
406 }
407
408 string stringify_update_bitfield(int flag)
409 {
410         if (flag == 0) {
411                 return "LEGACY_0";
412         }
413         string result = "";
414         int current_flag = flag;
415         /* Special cases to avoid ALL flags form being split into
416          * individual bits. */
417         if ((current_flag & ID_RECALC_PSYS_ALL) == ID_RECALC_PSYS_ALL) {
418                 result = stringify_append_bit(result, ID_RECALC_PSYS_ALL);
419         }
420         /* Handle all the rest of the flags. */
421         while (current_flag != 0) {
422                 IDRecalcFlag tag =
423                         (IDRecalcFlag)(1 << bitscan_forward_clear_i(&current_flag));
424                 result = stringify_append_bit(result, tag);
425         }
426         return result;
427 }
428
429 const char *update_source_as_string(eUpdateSource source)
430 {
431         switch (source) {
432                 case DEG_UPDATE_SOURCE_TIME: return "TIME";
433                 case DEG_UPDATE_SOURCE_USER_EDIT: return "USER_EDIT";
434                 case DEG_UPDATE_SOURCE_RELATIONS: return "RELATIONS";
435                 case DEG_UPDATE_SOURCE_VISIBILITY: return "VISIBILITY";
436         }
437         BLI_assert(!"Should never happen.");
438         return "UNKNOWN";
439 }
440
441 /* Special tag function which tags all components which needs to be tagged
442  * for update flag=0.
443  *
444  * TODO(sergey): This is something to be avoid in the future, make it more
445  * explicit and granular for users to tag what they really need. */
446 void deg_graph_node_tag_zero(Main *bmain,
447                              Depsgraph *graph,
448                              IDNode *id_node,
449                              eUpdateSource update_source)
450 {
451         if (id_node == NULL) {
452                 return;
453         }
454         ID *id = id_node->id_orig;
455         /* TODO(sergey): Which recalc flags to set here? */
456         id->recalc |= ID_RECALC_ALL & ~(ID_RECALC_PSYS_ALL | ID_RECALC_ANIMATION);
457         GHASH_FOREACH_BEGIN(ComponentNode *, comp_node, id_node->components)
458         {
459                 if (comp_node->type == NodeType::ANIMATION) {
460                         continue;
461                 }
462                 comp_node->tag_update(graph, update_source);
463         }
464         GHASH_FOREACH_END();
465         deg_graph_id_tag_legacy_compat(
466                 bmain, graph, id, (IDRecalcFlag)0, update_source);
467 }
468
469 void deg_graph_on_visible_update(Main *bmain, Depsgraph *graph)
470 {
471         for (DEG::IDNode *id_node : graph->id_nodes) {
472                 if (!id_node->visible_components_mask) {
473                         /* ID has no components which affects anything visible. no meed
474                          * bother with it to tag or anything. */
475                         continue;
476                 }
477                 if (id_node->visible_components_mask ==
478                     id_node->previously_visible_components_mask)
479                 {
480                         /* The ID was already visible and evaluated, all the subsequent
481                          * updates and tags are to be done explicitly. */
482                         continue;
483                 }
484                 int flag = 0;
485                 if (!DEG::deg_copy_on_write_is_expanded(id_node->id_cow)) {
486                         flag |= ID_RECALC_COPY_ON_WRITE;
487                         /* TODO(sergey): Shouldn't be needed, but currently we are lackign
488                          * some flushing of evaluated data to the original one, which makes,
489                          * for example, files saved with the rest pose.
490                          * Need to solve those issues carefully, for until then we evaluate
491                          * animation for datablocks which appears in the graph for the first
492                          * time. */
493                         flag |= ID_RECALC_ANIMATION;
494                 }
495                 /* We only tag components which needs an update. Tagging everything is
496                  * not a good idea because that might reset particles cache (or any
497                  * other type of cache).
498                  *
499                  * TODO(sergey): Need to generalize this somehow. */
500                 const ID_Type id_type = GS(id_node->id_orig->name);
501                 if (id_type == ID_OB) {
502                         flag |= ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY;
503                 }
504                 graph_id_tag_update(bmain,
505                                     graph,
506                                     id_node->id_orig,
507                                     flag,
508                                     DEG_UPDATE_SOURCE_VISIBILITY);
509                 if (id_type == ID_SCE) {
510                         /* Make sure collection properties are up to date. */
511                         id_node->tag_update(graph, DEG_UPDATE_SOURCE_VISIBILITY);
512                 }
513                 /* Now when ID is updated to the new visibility state, prevent it from
514                  * being re-tagged again. Simplest way to do so is to pretend that it
515                  * was already updated by the "previous" dependency graph.
516                  *
517                  * NOTE: Even if the on_visible_update() is called from the state when
518                  * dependency graph is tagged for relations update, it will be fine:
519                  * since dependency graph builder re-schedules entry tags, all the
520                  * tags we request from here will be applied in the updated state of
521                  * dependency graph. */
522                 id_node->previously_visible_components_mask =
523                         id_node->visible_components_mask;
524         }
525 }
526
527 }  /* namespace */
528
529 NodeType geometry_tag_to_component(const ID *id)
530 {
531         const ID_Type id_type = GS(id->name);
532         switch (id_type) {
533                 case ID_OB:
534                 {
535                         const Object *object = (Object *)id;
536                         switch (object->type) {
537                                 case OB_MESH:
538                                 case OB_CURVE:
539                                 case OB_SURF:
540                                 case OB_FONT:
541                                 case OB_LATTICE:
542                                 case OB_MBALL:
543                                 case OB_GPENCIL:
544                                         return NodeType::GEOMETRY;
545                                 case OB_ARMATURE:
546                                         return NodeType::EVAL_POSE;
547                                         /* TODO(sergey): More cases here? */
548                         }
549                         break;
550                 }
551                 case ID_ME:
552                 case ID_CU:
553                 case ID_LT:
554                 case ID_MB:
555                         return NodeType::GEOMETRY;
556                 case ID_PA: /* Particles */
557                         return NodeType::UNDEFINED;
558                 case ID_LP:
559                         return NodeType::PARAMETERS;
560                 case ID_GD:
561                         return NodeType::GEOMETRY;
562                 case ID_PAL: /* Palettes */
563                         return NodeType::PARAMETERS;
564                 default:
565                         break;
566         }
567         return NodeType::UNDEFINED;
568 }
569
570 void id_tag_update(Main *bmain, ID *id, int flag, eUpdateSource update_source)
571 {
572         graph_id_tag_update(bmain, NULL, id, flag, update_source);
573         LISTBASE_FOREACH (Scene *, scene, &bmain->scene) {
574                 LISTBASE_FOREACH (ViewLayer *, view_layer, &scene->view_layers) {
575                         Depsgraph *depsgraph =
576                                 (Depsgraph *)BKE_scene_get_depsgraph(scene,
577                                                                      view_layer,
578                                                                      false);
579                         if (depsgraph != NULL) {
580                                 graph_id_tag_update(
581                                         bmain, depsgraph, id, flag, update_source);
582                         }
583                 }
584         }
585 }
586
587 void graph_id_tag_update(Main *bmain,
588                          Depsgraph *graph,
589                          ID *id,
590                          int flag,
591                          eUpdateSource update_source)
592 {
593         const int debug_flags = (graph != NULL)
594                 ? DEG_debug_flags_get((::Depsgraph *)graph)
595                 : G.debug;
596         if (debug_flags & G_DEBUG_DEPSGRAPH_TAG) {
597                 printf("%s: id=%s flags=%s source=%s\n",
598                        __func__,
599                        id->name,
600                        stringify_update_bitfield(flag).c_str(),
601                        update_source_as_string(update_source));
602         }
603         IDNode *id_node = (graph != NULL) ? graph->find_id_node(id)
604                                               : NULL;
605         DEG_id_type_tag(bmain, GS(id->name));
606         if (flag == 0) {
607                 deg_graph_node_tag_zero(bmain, graph, id_node, update_source);
608         }
609         id->recalc |= flag;
610         int current_flag = flag;
611         while (current_flag != 0) {
612                 IDRecalcFlag tag =
613                         (IDRecalcFlag)(1 << bitscan_forward_clear_i(&current_flag));
614                 graph_id_tag_update_single_flag(
615                         bmain, graph, id, id_node, tag, update_source);
616         }
617         /* Special case for nested node tree datablocks. */
618         id_tag_update_ntree_special(bmain, graph, id, flag, update_source);
619         /* Direct update tags means that something outside of simulated/cached
620          * physics did change and that cache is to be invalidated. */
621         if (update_source == DEG_UPDATE_SOURCE_USER_EDIT) {
622                 graph_id_tag_update_single_flag(
623                         bmain, graph, id, id_node, ID_RECALC_POINT_CACHE, update_source);
624         }
625 }
626
627 }  // namespace DEG
628
629 const char *DEG_update_tag_as_string(IDRecalcFlag flag)
630 {
631         switch (flag) {
632                 case ID_RECALC_TRANSFORM: return "TRANSFORM";
633                 case ID_RECALC_GEOMETRY: return "GEOMETRY";
634                 case ID_RECALC_ANIMATION: return "ANIMATION";
635                 case ID_RECALC_PSYS_REDO: return "PSYS_REDO";
636                 case ID_RECALC_PSYS_RESET: return "PSYS_RESET";
637                 case ID_RECALC_PSYS_CHILD: return "PSYS_CHILD";
638                 case ID_RECALC_PSYS_PHYS: return "PSYS_PHYS";
639                 case ID_RECALC_PSYS_ALL: return "PSYS_ALL";
640                 case ID_RECALC_COPY_ON_WRITE: return "COPY_ON_WRITE";
641                 case ID_RECALC_SHADING: return "SHADING";
642                 case ID_RECALC_SELECT: return "SELECT";
643                 case ID_RECALC_BASE_FLAGS: return "BASE_FLAGS";
644                 case ID_RECALC_POINT_CACHE: return "POINT_CACHE";
645                 case ID_RECALC_EDITORS: return "EDITORS";
646                 case ID_RECALC_ALL: return "ALL";
647         }
648         BLI_assert(!"Unhandled update flag, should never happen!");
649         return "UNKNOWN";
650 }
651
652 /* Data-Based Tagging  */
653
654 /* Tag given ID for an update in all the dependency graphs. */
655 void DEG_id_tag_update(ID *id, int flag)
656 {
657         DEG_id_tag_update_ex(G.main, id, flag);
658 }
659
660 void DEG_id_tag_update_ex(Main *bmain, ID *id, int flag)
661 {
662         if (id == NULL) {
663                 /* Ideally should not happen, but old depsgraph allowed this. */
664                 return;
665         }
666         DEG::id_tag_update(
667                 bmain, id, flag, DEG::DEG_UPDATE_SOURCE_USER_EDIT);
668 }
669
670 void DEG_graph_id_tag_update(struct Main *bmain,
671                              struct Depsgraph *depsgraph,
672                              struct ID *id,
673                              int flag)
674 {
675         DEG::Depsgraph *graph = (DEG::Depsgraph *)depsgraph;
676         DEG::graph_id_tag_update(
677                 bmain, graph, id, flag, DEG::DEG_UPDATE_SOURCE_USER_EDIT);
678 }
679
680 /* Mark a particular datablock type as having changing. */
681 void DEG_id_type_tag(Main *bmain, short id_type)
682 {
683         if (id_type == ID_NT) {
684                 /* Stupid workaround so parent datablocks of nested nodetree get looped
685                  * over when we loop over tagged datablock types. */
686                 DEG_id_type_tag(bmain, ID_MA);
687                 DEG_id_type_tag(bmain, ID_TE);
688                 DEG_id_type_tag(bmain, ID_LA);
689                 DEG_id_type_tag(bmain, ID_WO);
690                 DEG_id_type_tag(bmain, ID_SCE);
691         }
692
693         int id_type_index = BKE_idcode_to_index(id_type);
694
695         LISTBASE_FOREACH (Scene *, scene, &bmain->scene) {
696                 LISTBASE_FOREACH (ViewLayer *, view_layer, &scene->view_layers) {
697                         Depsgraph *depsgraph =
698                                 (Depsgraph *)BKE_scene_get_depsgraph(scene,
699                                                                      view_layer,
700                                                                      false);
701                         if (depsgraph != NULL) {
702                                 DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
703                                 deg_graph->id_type_updated[id_type_index] = 1;
704                         }
705                 }
706         }
707 }
708
709 void DEG_graph_flush_update(Main *bmain, Depsgraph *depsgraph)
710 {
711         if (depsgraph == NULL) {
712                 return;
713         }
714         DEG::deg_graph_flush_updates(bmain, (DEG::Depsgraph *)depsgraph);
715 }
716
717 /* Update dependency graph when visible scenes/layers changes. */
718 void DEG_graph_on_visible_update(Main *bmain, Depsgraph *depsgraph)
719 {
720         DEG::Depsgraph *graph = (DEG::Depsgraph *)depsgraph;
721         DEG::deg_graph_on_visible_update(bmain, graph);
722 }
723
724 void DEG_on_visible_update(Main *bmain, const bool UNUSED(do_time))
725 {
726         LISTBASE_FOREACH (Scene *, scene, &bmain->scene) {
727                 LISTBASE_FOREACH (ViewLayer *, view_layer, &scene->view_layers) {
728                         Depsgraph *depsgraph =
729                                 (Depsgraph *)BKE_scene_get_depsgraph(scene,
730                                                                      view_layer,
731                                                                      false);
732                         if (depsgraph != NULL) {
733                                 DEG_graph_on_visible_update(bmain, depsgraph);
734                         }
735                 }
736         }
737 }
738
739 /* Check if something was changed in the database and inform
740  * editors about this. */
741 void DEG_ids_check_recalc(Main *bmain,
742                           Depsgraph *depsgraph,
743                           Scene *scene,
744                           ViewLayer *view_layer,
745                           bool time)
746 {
747         bool updated = time || DEG_id_type_any_updated(depsgraph);
748
749         DEGEditorUpdateContext update_ctx = {NULL};
750         update_ctx.bmain = bmain;
751         update_ctx.depsgraph = depsgraph;
752         update_ctx.scene = scene;
753         update_ctx.view_layer = view_layer;
754         DEG::deg_editors_scene_update(&update_ctx, updated);
755 }
756
757 static void deg_graph_clear_id_node_func(
758         void *__restrict data_v,
759         const int i,
760         const ParallelRangeTLS *__restrict /*tls*/)
761 {
762         /* TODO: we clear original ID recalc flags here, but this may not work
763          * correctly when there are multiple depsgraph with others still using
764          * the recalc flag. */
765         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(data_v);
766         DEG::IDNode *id_node = deg_graph->id_nodes[i];
767         id_node->id_cow->recalc &= ~ID_RECALC_ALL;
768
769         /* Clear embedded node trees too. */
770         bNodeTree *ntree_cow = ntreeFromID(id_node->id_cow);
771         if (ntree_cow) {
772                 ntree_cow->id.recalc &= ~ID_RECALC_ALL;
773         }
774         bNodeTree *ntree_orig = ntreeFromID(id_node->id_orig);
775         if (ntree_orig) {
776                 ntree_orig->id.recalc &= ~ID_RECALC_ALL;
777         }
778 }
779
780 void DEG_ids_clear_recalc(Main *UNUSED(bmain),
781                           Depsgraph *depsgraph)
782 {
783         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
784         /* TODO(sergey): Re-implement POST_UPDATE_HANDLER_WORKAROUND using entry_tags
785          * and id_tags storage from the new dependency graph. */
786         if (!DEG_id_type_any_updated(depsgraph)) {
787                 return;
788         }
789         /* Go over all ID nodes nodes, clearing tags. */
790         const int num_id_nodes = deg_graph->id_nodes.size();
791         ParallelRangeSettings settings;
792         BLI_parallel_range_settings_defaults(&settings);
793         settings.min_iter_per_thread = 1024;
794         BLI_task_parallel_range(0, num_id_nodes,
795                                 deg_graph,
796                                 deg_graph_clear_id_node_func,
797                                 &settings);
798         memset(deg_graph->id_type_updated, 0, sizeof(deg_graph->id_type_updated));
799 }