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