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