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_curve_types.h"
44 #include "DNA_key_types.h"
45 #include "DNA_lattice_types.h"
46 #include "DNA_mesh_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_particle_types.h"
49 #include "DNA_screen_types.h"
50 #include "DNA_windowmanager_types.h"
51
52 #include "BKE_idcode.h"
53 #include "BKE_library.h"
54 #include "BKE_main.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
66 #include "intern/builder/deg_builder.h"
67 #include "intern/eval/deg_eval_flush.h"
68 #include "intern/nodes/deg_node.h"
69 #include "intern/nodes/deg_node_component.h"
70 #include "intern/nodes/deg_node_id.h"
71 #include "intern/nodes/deg_node_operation.h"
72
73 #include "intern/depsgraph_intern.h"
74 #include "util/deg_util_foreach.h"
75
76 /* *********************** */
77 /* Update Tagging/Flushing */
78
79 namespace DEG {
80
81 namespace {
82
83 void deg_graph_id_tag_update(Main *bmain, Depsgraph *graph, ID *id, int flag);
84
85 void depsgraph_geometry_tag_to_component(const ID *id,
86                                          eDepsNode_Type *component_type)
87 {
88         const ID_Type id_type = GS(id->name);
89         switch (id_type) {
90                 case ID_OB:
91                 {
92                         const Object *object = (Object *)id;
93                         switch (object->type) {
94                                 case OB_MESH:
95                                 case OB_CURVE:
96                                 case OB_SURF:
97                                 case OB_FONT:
98                                 case OB_LATTICE:
99                                 case OB_MBALL:
100                                         *component_type = DEG_NODE_TYPE_GEOMETRY;
101                                         break;
102                                 case OB_ARMATURE:
103                                         *component_type = DEG_NODE_TYPE_EVAL_POSE;
104                                         break;
105                                         /* TODO(sergey): More cases here? */
106                         }
107                         break;
108                 }
109                 case ID_ME:
110                         *component_type = DEG_NODE_TYPE_GEOMETRY;
111                         break;
112                 case ID_PA:
113                         return;
114                 case ID_LP:
115                         *component_type = DEG_NODE_TYPE_PARAMETERS;
116                         break;
117                 default:
118                         break;
119         }
120 }
121
122 void depsgraph_select_tag_to_component_opcode(
123         const ID *id,
124         eDepsNode_Type *component_type,
125         eDepsOperation_Code *operation_code)
126 {
127         const ID_Type id_type = GS(id->name);
128         if (id_type == ID_SCE) {
129                 /* We need to flush base flags to all objects in a scene since we
130                  * don't know which ones changed. However, we don't want to update
131                  * the whole scene, so pick up some operation which will do as less
132                  * as possible.
133                  *
134                  * TODO(sergey): We can introduce explicit exit operation which
135                  * does nothing and which is only used to cascade flush down the
136                  * road.
137                  */
138                 *component_type = DEG_NODE_TYPE_LAYER_COLLECTIONS;
139                 *operation_code = DEG_OPCODE_VIEW_LAYER_DONE;
140         }
141         else if (id_type == ID_OB) {
142                 *component_type = DEG_NODE_TYPE_LAYER_COLLECTIONS;
143                 *operation_code = DEG_OPCODE_OBJECT_BASE_FLAGS;
144         }
145         else {
146                 *component_type = DEG_NODE_TYPE_BATCH_CACHE;
147                 *operation_code = DEG_OPCODE_GEOMETRY_SELECT_UPDATE;
148         }
149 }
150
151 void depsgraph_base_flags_tag_to_component_opcode(
152         const ID *id,
153         eDepsNode_Type *component_type,
154         eDepsOperation_Code *operation_code)
155 {
156         const ID_Type id_type = GS(id->name);
157         if (id_type == ID_SCE) {
158                 *component_type = DEG_NODE_TYPE_LAYER_COLLECTIONS;
159                 *operation_code = DEG_OPCODE_VIEW_LAYER_INIT;
160         }
161         else if (id_type == ID_OB) {
162                 *component_type = DEG_NODE_TYPE_LAYER_COLLECTIONS;
163                 *operation_code = DEG_OPCODE_OBJECT_BASE_FLAGS;
164         }
165 }
166
167 void depsgraph_tag_to_component_opcode(const ID *id,
168                                        eDepsgraph_Tag tag,
169                                        eDepsNode_Type *component_type,
170                                        eDepsOperation_Code *operation_code)
171 {
172         const ID_Type id_type = GS(id->name);
173         *component_type = DEG_NODE_TYPE_UNDEFINED;
174         *operation_code = DEG_OPCODE_OPERATION;
175         /* Special case for now, in the future we should get rid of this. */
176         if (tag == 0) {
177                 *component_type = DEG_NODE_TYPE_ID_REF;
178                 *operation_code = DEG_OPCODE_OPERATION;
179                 return;
180         }
181         switch (tag) {
182                 case DEG_TAG_TRANSFORM:
183                         *component_type = DEG_NODE_TYPE_TRANSFORM;
184                         break;
185                 case DEG_TAG_GEOMETRY:
186                         depsgraph_geometry_tag_to_component(id, component_type);
187                         break;
188                 case DEG_TAG_TIME:
189                         *component_type = DEG_NODE_TYPE_ANIMATION;
190                         break;
191                 case DEG_TAG_PSYS_REDO:
192                 case DEG_TAG_PSYS_RESET:
193                 case DEG_TAG_PSYS_TYPE:
194                 case DEG_TAG_PSYS_CHILD:
195                 case DEG_TAG_PSYS_PHYS:
196                         if (id_type == ID_PA) {
197                                 /* NOTES:
198                                  * - For particle settings node we need to use different
199                                  *   component. Will be nice to get this unified with object,
200                                  *   but we can survive for now with single exception here.
201                                  *   Particles needs reconsideration anyway,
202                                  * - We do direct injection of particle settings recalc flag
203                                  *   here. This is what we need to do for until particles
204                                  *   are switched away from own recalc flag and are using
205                                  *   ID->recalc flags instead.
206                                  */
207                                 ParticleSettings *particle_settings = (ParticleSettings *)id;
208                                 particle_settings->recalc |= (tag & DEG_TAG_PSYS_ALL);
209                                 *component_type = DEG_NODE_TYPE_PARAMETERS;
210                         }
211                         else {
212                                 *component_type = DEG_NODE_TYPE_EVAL_PARTICLES;
213                         }
214                         break;
215                 case DEG_TAG_COPY_ON_WRITE:
216                         *component_type = DEG_NODE_TYPE_COPY_ON_WRITE;
217                         break;
218                 case DEG_TAG_SHADING_UPDATE:
219                         if (id_type == ID_NT) {
220                                 *component_type = DEG_NODE_TYPE_SHADING_PARAMETERS;
221                         }
222                         else {
223                                 *component_type = DEG_NODE_TYPE_SHADING;
224                         }
225                         break;
226                 case DEG_TAG_SELECT_UPDATE:
227                         depsgraph_select_tag_to_component_opcode(id,
228                                                                  component_type,
229                                                                  operation_code);
230                         break;
231                 case DEG_TAG_BASE_FLAGS_UPDATE:
232                         depsgraph_base_flags_tag_to_component_opcode(id,
233                                                                      component_type,
234                                                                      operation_code);
235                 case DEG_TAG_EDITORS_UPDATE:
236                         /* There is no such node in depsgraph, this tag is to be handled
237                          * separately.
238                          */
239                         break;
240                 case DEG_TAG_PSYS_ALL:
241                         BLI_assert(!"Should not happen");
242                         break;
243         }
244 }
245
246 void id_tag_update_ntree_special(Main *bmain, Depsgraph *graph, ID *id, int flag)
247 {
248         bNodeTree *ntree = ntreeFromID(id);
249         if (ntree == NULL) {
250                 return;
251         }
252         deg_graph_id_tag_update(bmain, graph, &ntree->id, flag);
253 }
254
255 void depsgraph_update_editors_tag(Main *bmain, Depsgraph *graph, ID *id)
256 {
257         /* NOTE: We handle this immediately, without delaying anything, to be
258          * sure we don't cause threading issues with OpenGL.
259          */
260         /* TODO(sergey): Make sure this works for CoW-ed datablocks as well. */
261         DEGEditorUpdateContext update_ctx = {NULL};
262         update_ctx.bmain = bmain;
263         update_ctx.depsgraph = (::Depsgraph *)graph;
264         update_ctx.scene = graph->scene;
265         update_ctx.view_layer = graph->view_layer;
266         deg_editors_id_update(&update_ctx, id);
267 }
268
269 void depsgraph_tag_component(Depsgraph *graph,
270                              IDDepsNode *id_node,
271                              eDepsNode_Type component_type,
272                              eDepsOperation_Code operation_code)
273 {
274         ComponentDepsNode *component_node =
275                 id_node->find_component(component_type);
276         if (component_node == NULL) {
277                 return;
278         }
279         if (operation_code == DEG_OPCODE_OPERATION) {
280                 component_node->tag_update(graph);
281         }
282         else {
283                 OperationDepsNode *operation_node =
284                         component_node->find_operation(operation_code);
285                 if (operation_node != NULL) {
286                         operation_node->tag_update(graph);
287                 }
288         }
289 }
290
291 /* This is a tag compatibility with legacy code.
292  *
293  * Mainly, old code was tagging object with OB_RECALC_DATA tag to inform
294  * that object's data datablock changed. Now API expects that ID is given
295  * explicitly, but not all areas are aware of this yet.
296  */
297 void deg_graph_id_tag_legacy_compat(Main *bmain,
298                                     ID *id,
299                                     eDepsgraph_Tag tag)
300 {
301         if (tag == DEG_TAG_GEOMETRY || tag == 0) {
302                 switch (GS(id->name)) {
303                         case ID_OB:
304                         {
305                                 Object *object = (Object *)id;
306                                 ID *data_id = (ID *)object->data;
307                                 if (data_id != NULL) {
308                                         DEG_id_tag_update_ex(bmain, data_id, 0);
309                                 }
310                                 break;
311                         }
312                         /* TODO(sergey): Shape keys are annoying, maybe we should find a
313                          * way to chain geometry evaluation to them, so we don't need extra
314                          * tagging here.
315                          */
316                         case ID_ME:
317                         {
318                                 Mesh *mesh = (Mesh *)id;
319                                 ID *key_id = &mesh->key->id;
320                                 if (key_id != NULL) {
321                                         DEG_id_tag_update_ex(bmain, key_id, 0);
322                                 }
323                                 break;
324                         }
325                         case ID_LT:
326                         {
327                                 Lattice *lattice = (Lattice *)id;
328                                 ID *key_id = &lattice->key->id;
329                                 if (key_id != NULL) {
330                                         DEG_id_tag_update_ex(bmain, key_id, 0);
331                                 }
332                                 break;
333                         }
334                         case ID_CU:
335                         {
336                                 Curve *curve = (Curve *)id;
337                                 ID *key_id = &curve->key->id;
338                                 if (key_id != NULL) {
339                                         DEG_id_tag_update_ex(bmain, key_id, 0);
340                                 }
341                                 break;
342                         }
343                         default:
344                                 break;
345                 }
346         }
347 }
348
349 void deg_graph_id_tag_update_single_flag(Main *bmain,
350                                          Depsgraph *graph,
351                                          ID *id,
352                                          IDDepsNode *id_node,
353                                          eDepsgraph_Tag tag)
354 {
355         if (tag == DEG_TAG_EDITORS_UPDATE) {
356                 if (graph != NULL) {
357                         depsgraph_update_editors_tag(bmain, graph, id);
358                 }
359                 return;
360         }
361         /* Get description of what is to be tagged. */
362         eDepsNode_Type component_type;
363         eDepsOperation_Code operation_code;
364         depsgraph_tag_to_component_opcode(id,
365                                           tag,
366                                           &component_type,
367                                           &operation_code);
368         /* Check whether we've got something to tag. */
369         if (component_type == DEG_NODE_TYPE_UNDEFINED) {
370                 /* Given ID does not support tag. */
371                 /* TODO(sergey): Shall we raise some panic here? */
372                 return;
373         }
374         /* Tag ID recalc flag. */
375         DepsNodeFactory *factory = deg_type_get_factory(component_type);
376         BLI_assert(factory != NULL);
377         id->recalc |= factory->id_recalc_tag();
378         /* Some sanity checks before moving forward. */
379         if (id_node == NULL) {
380                 /* Happens when object is tagged for update and not yet in the
381                  * dependency graph (but will be after relations update).
382                  */
383                 return;
384         }
385         /* Tag corresponding dependency graph operation for update. */
386         if (component_type == DEG_NODE_TYPE_ID_REF) {
387                 id_node->tag_update(graph);
388         }
389         else {
390                 depsgraph_tag_component(graph, id_node, component_type, operation_code);
391         }
392         /* TODO(sergey): Get rid of this once all areas are using proper data ID
393          * for tagging.
394          */
395         deg_graph_id_tag_legacy_compat(bmain, id, tag);
396
397 }
398
399 void deg_graph_id_tag_update(Main *bmain, Depsgraph *graph, ID *id, int flag)
400 {
401         IDDepsNode *id_node = (graph != NULL) ? graph->find_id_node(id)
402                                               : NULL;
403         DEG_id_type_tag(bmain, GS(id->name));
404         if (flag == 0) {
405                 /* TODO(sergey): Which recalc flags to set here? */
406                 id->recalc |= ID_RECALC_ALL;
407                 if (id_node != NULL) {
408                         id_node->tag_update(graph);
409                 }
410                 deg_graph_id_tag_legacy_compat(bmain, id, (eDepsgraph_Tag)0);
411         }
412         int current_flag = flag;
413         while (current_flag != 0) {
414                 eDepsgraph_Tag tag =
415                         (eDepsgraph_Tag)(1 << bitscan_forward_clear_i(&current_flag));
416                 deg_graph_id_tag_update_single_flag(bmain,
417                                                     graph,
418                                                     id,
419                                                     id_node,
420                                                     tag);
421         }
422         /* Special case for nested node tree datablocks. */
423         id_tag_update_ntree_special(bmain, graph, id, flag);
424 }
425
426 /* TODO(sergey): Consider storing scene and view layer at depsgraph allocation
427  * time.
428  */
429 void deg_ensure_scene_view_layer(Depsgraph *graph,
430                                  Scene *scene,
431                                  ViewLayer *view_layer)
432 {
433         if (!graph->need_update) {
434                 return;
435         }
436         graph->scene = scene;
437         graph->view_layer = view_layer;
438 }
439
440 void deg_id_tag_update(Main *bmain, ID *id, int flag)
441 {
442         deg_graph_id_tag_update(bmain, NULL, id, flag);
443         BLI_LISTBASE_FOREACH (Scene *, scene, &bmain->scene) {
444                 BLI_LISTBASE_FOREACH (ViewLayer *, view_layer, &scene->view_layers) {
445                         Depsgraph *depsgraph =
446                                 (Depsgraph *)BKE_scene_get_depsgraph(scene,
447                                                                      view_layer,
448                                                                      false);
449                         if (depsgraph != NULL) {
450                                 /* Make sure depsgraph is pointing to a correct scene and
451                                  * view layer. This is mainly required in cases when depsgraph
452                                  * was not built yet.
453                                  */
454                                 deg_ensure_scene_view_layer(depsgraph, scene, view_layer);
455                                 deg_graph_id_tag_update(bmain, depsgraph, id, flag);
456                         }
457                 }
458         }
459 }
460
461 void deg_graph_on_visible_update(Main *bmain, Depsgraph *graph)
462 {
463         /* Make sure objects are up to date. */
464         foreach (DEG::IDDepsNode *id_node, graph->id_nodes) {
465                 const ID_Type id_type = GS(id_node->id_orig->name);
466                 int flag = 0;
467                 /* We only tag components which needs an update. Tagging everything is
468                  * not a good idea because that might reset particles cache (or any
469                  * other type of cache).
470                  *
471                  * TODO(sergey): Need to generalize this somehow.
472                  */
473                 if (id_type == ID_OB) {
474                         flag |= OB_RECALC_OB | OB_RECALC_DATA | DEG_TAG_COPY_ON_WRITE;
475                 }
476                 deg_graph_id_tag_update(bmain, graph, id_node->id_orig, flag);
477         }
478         /* Make sure collection properties are up to date. */
479         for (Scene *scene_iter = graph->scene;
480              scene_iter != NULL;
481              scene_iter = scene_iter->set)
482         {
483                 IDDepsNode *scene_id_node = graph->find_id_node(&scene_iter->id);
484                 if (scene_id_node != NULL) {
485                         scene_id_node->tag_update(graph);
486                 }
487                 else {
488                         BLI_assert(graph->need_update);
489                 }
490         }
491 }
492
493 }  /* namespace */
494
495 }  // namespace DEG
496
497 /* Data-Based Tagging  */
498
499 /* Tag given ID for an update in all the dependency graphs. */
500 void DEG_id_tag_update(ID *id, int flag)
501 {
502         DEG_id_tag_update_ex(G.main, id, flag);
503 }
504
505 void DEG_id_tag_update_ex(Main *bmain, ID *id, int flag)
506 {
507         if (id == NULL) {
508                 /* Ideally should not happen, but old depsgraph allowed this. */
509                 return;
510         }
511         DEG_DEBUG_PRINTF("%s: id=%s flag=%d\n", __func__, id->name, flag);
512         DEG::deg_id_tag_update(bmain, id, flag);
513 }
514
515 void DEG_graph_id_tag_update(struct Main *bmain,
516                              struct Depsgraph *depsgraph,
517                              struct ID *id,
518                              int flag)
519 {
520         DEG::Depsgraph *graph = (DEG::Depsgraph *)depsgraph;
521         DEG::deg_graph_id_tag_update(bmain, graph, id, flag);
522 }
523
524 /* Mark a particular datablock type as having changing. */
525 void DEG_id_type_tag(Main *bmain, short id_type)
526 {
527         if (id_type == ID_NT) {
528                 /* Stupid workaround so parent datablocks of nested nodetree get looped
529                  * over when we loop over tagged datablock types.
530                  */
531                 DEG_id_type_tag(bmain, ID_MA);
532                 DEG_id_type_tag(bmain, ID_TE);
533                 DEG_id_type_tag(bmain, ID_LA);
534                 DEG_id_type_tag(bmain, ID_WO);
535                 DEG_id_type_tag(bmain, ID_SCE);
536         }
537
538         bmain->id_tag_update[BKE_idcode_to_index(id_type)] = 1;
539 }
540
541 void DEG_graph_flush_update(Main *bmain, Depsgraph *depsgraph)
542 {
543         if (depsgraph == NULL) {
544                 return;
545         }
546         DEG::deg_graph_flush_updates(bmain, (DEG::Depsgraph *)depsgraph);
547 }
548
549 /* Update dependency graph when visible scenes/layers changes. */
550 void DEG_graph_on_visible_update(Main *bmain, Depsgraph *depsgraph)
551 {
552         DEG::Depsgraph *graph = (DEG::Depsgraph *)depsgraph;
553         DEG::deg_graph_on_visible_update(bmain, graph);
554 }
555
556 void DEG_on_visible_update(Main *bmain, const bool UNUSED(do_time))
557 {
558         BLI_LISTBASE_FOREACH (Scene *, scene, &bmain->scene) {
559                 BLI_LISTBASE_FOREACH (ViewLayer *, view_layer, &scene->view_layers) {
560                         Depsgraph *depsgraph =
561                                 (Depsgraph *)BKE_scene_get_depsgraph(scene,
562                                                                      view_layer,
563                                                                      false);
564                         if (depsgraph != NULL) {
565                                 DEG_graph_on_visible_update(bmain, depsgraph);
566                         }
567                 }
568         }
569 }
570
571 /* Check if something was changed in the database and inform
572  * editors about this.
573  */
574 void DEG_ids_check_recalc(Main *bmain,
575                           Depsgraph *depsgraph,
576                           Scene *scene,
577                           ViewLayer *view_layer,
578                           bool time)
579 {
580         ListBase *lbarray[MAX_LIBARRAY];
581         int a;
582         bool updated = false;
583
584         /* Loop over all ID types. */
585         a  = set_listbasepointers(bmain, lbarray);
586         while (a--) {
587                 ListBase *lb = lbarray[a];
588                 ID *id = (ID *)lb->first;
589
590                 if (id && bmain->id_tag_update[BKE_idcode_to_index(GS(id->name))]) {
591                         updated = true;
592                         break;
593                 }
594         }
595
596         DEGEditorUpdateContext update_ctx = {NULL};
597         update_ctx.bmain = bmain;
598         update_ctx.depsgraph = depsgraph;
599         update_ctx.scene = scene;
600         update_ctx.view_layer = view_layer;
601         DEG::deg_editors_scene_update(&update_ctx, (updated || time));
602 }
603
604 void DEG_ids_clear_recalc(Main *bmain)
605 {
606         ListBase *lbarray[MAX_LIBARRAY];
607         bNodeTree *ntree;
608         int a;
609
610         /* TODO(sergey): Re-implement POST_UPDATE_HANDLER_WORKAROUND using entry_tags
611          * and id_tags storage from the new dependency graph.
612          */
613
614         /* Loop over all ID types. */
615         a  = set_listbasepointers(bmain, lbarray);
616         while (a--) {
617                 ListBase *lb = lbarray[a];
618                 ID *id = (ID *)lb->first;
619
620                 if (id && bmain->id_tag_update[BKE_idcode_to_index(GS(id->name))]) {
621                         for (; id; id = (ID *)id->next) {
622                                 id->recalc &= ~ID_RECALC_ALL;
623
624                                 /* Some ID's contain semi-datablock nodetree */
625                                 ntree = ntreeFromID(id);
626                                 if (ntree != NULL) {
627                                         ntree->id.recalc &= ~ID_RECALC_ALL;
628                                 }
629                         }
630                 }
631         }
632
633         memset(bmain->id_tag_update, 0, sizeof(bmain->id_tag_update));
634 }