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