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