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