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