2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2013 Blender Foundation.
19 * All rights reserved.
21 * Original Author: Joshua Leung
22 * Contributor(s): None Yet
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/depsgraph/intern/depsgraph_tag.cc
30 * Core routines for how the Depsgraph works.
34 #include <cstring> /* required for memset */
37 #include "BLI_utildefines.h"
38 #include "BLI_listbase.h"
39 #include "BLI_math_bits.h"
43 #include "DNA_object_types.h"
44 #include "DNA_particle_types.h"
45 #include "DNA_screen_types.h"
46 #include "DNA_windowmanager_types.h"
49 #include "BKE_idcode.h"
50 #include "BKE_library.h"
53 #include "BKE_scene.h"
54 #include "BKE_workspace.h"
57 #include "BKE_screen.h"
61 #include "DEG_depsgraph.h"
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"
70 #include "intern/depsgraph_intern.h"
71 #include "util/deg_util_foreach.h"
73 /* *********************** */
74 /* Update Tagging/Flushing */
80 void deg_graph_id_tag_update(Main *bmain, Depsgraph *graph, ID *id, int flag);
82 void depsgraph_geometry_tag_to_component(const ID *id,
83 eDepsNode_Type *component_type)
85 const ID_Type id_type = GS(id->name);
89 const Object *object = (Object *)id;
90 switch (object->type) {
96 *component_type = DEG_NODE_TYPE_GEOMETRY;
99 *component_type = DEG_NODE_TYPE_EVAL_POSE;
101 /* TODO(sergey): More cases here? */
106 *component_type = DEG_NODE_TYPE_GEOMETRY;
111 *component_type = DEG_NODE_TYPE_PARAMETERS;
118 void depsgraph_select_tag_to_component_opcode(
120 eDepsNode_Type *component_type,
121 eDepsOperation_Code *operation_code)
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
130 * TODO(sergey): We can introduce explicit exit operation which
131 * does nothing and which is only used to cascade flush down the
134 *component_type = DEG_NODE_TYPE_LAYER_COLLECTIONS;
135 *operation_code = DEG_OPCODE_VIEW_LAYER_DONE;
137 else if (id_type == ID_OB) {
138 *component_type = DEG_NODE_TYPE_LAYER_COLLECTIONS;
139 *operation_code = DEG_OPCODE_OBJECT_BASE_FLAGS;
142 *component_type = DEG_NODE_TYPE_BATCH_CACHE;
143 *operation_code = DEG_OPCODE_GEOMETRY_SELECT_UPDATE;
147 void depsgraph_base_flags_tag_to_component_opcode(
149 eDepsNode_Type *component_type,
150 eDepsOperation_Code *operation_code)
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;
157 else if (id_type == ID_OB) {
158 *component_type = DEG_NODE_TYPE_LAYER_COLLECTIONS;
159 *operation_code = DEG_OPCODE_OBJECT_BASE_FLAGS;
163 void depsgraph_tag_to_component_opcode(const ID *id,
165 eDepsNode_Type *component_type,
166 eDepsOperation_Code *operation_code)
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. */
173 *component_type = DEG_NODE_TYPE_ID_REF;
174 *operation_code = DEG_OPCODE_OPERATION;
178 case DEG_TAG_TRANSFORM:
179 *component_type = DEG_NODE_TYPE_TRANSFORM;
181 case DEG_TAG_GEOMETRY:
182 depsgraph_geometry_tag_to_component(id, component_type);
185 *component_type = DEG_NODE_TYPE_ANIMATION;
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) {
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.
203 ParticleSettings *particle_settings = (ParticleSettings *)id;
204 particle_settings->recalc |= (tag & DEG_TAG_PSYS_ALL);
205 *component_type = DEG_NODE_TYPE_PARAMETERS;
208 *component_type = DEG_NODE_TYPE_EVAL_PARTICLES;
211 case DEG_TAG_COPY_ON_WRITE:
212 *component_type = DEG_NODE_TYPE_COPY_ON_WRITE;
214 case DEG_TAG_SHADING_UPDATE:
215 if (id_type == ID_NT) {
216 *component_type = DEG_NODE_TYPE_SHADING_PARAMETERS;
219 *component_type = DEG_NODE_TYPE_SHADING;
222 case DEG_TAG_SELECT_UPDATE:
223 depsgraph_select_tag_to_component_opcode(id,
227 case DEG_TAG_BASE_FLAGS_UPDATE:
228 depsgraph_base_flags_tag_to_component_opcode(id,
231 case DEG_TAG_EDITORS_UPDATE:
232 /* There is no such node in depsgraph, this tag is to be handled
236 case DEG_TAG_PSYS_ALL:
237 BLI_assert(!"Should not happen");
242 void id_tag_update_ntree_special(Main *bmain, Depsgraph *graph, ID *id, int flag)
244 bNodeTree *ntree = ntreeFromID(id);
248 deg_graph_id_tag_update(bmain, graph, &ntree->id, flag);
251 void depsgraph_update_editors_tag(Main *bmain, Depsgraph *graph, ID *id)
253 /* NOTE: We handle this immediately, without delaying anything, to be
254 * sure we don't cause threading issues with OpenGL.
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);
265 void deg_graph_id_tag_update_single_flag(Main *bmain,
271 if (tag == DEG_TAG_EDITORS_UPDATE) {
273 depsgraph_update_editors_tag(bmain, graph, id);
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,
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? */
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).
301 /* Tag corresponding dependency graph operation for update. */
302 if (component_type == DEG_NODE_TYPE_ID_REF) {
303 id_node->tag_update(graph);
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);
313 OperationDepsNode *operation_node =
314 component_node->find_operation(operation_code);
315 if (operation_node != NULL) {
316 operation_node->tag_update(graph);
323 void deg_graph_id_tag_update(Main *bmain, Depsgraph *graph, ID *id, int flag)
325 IDDepsNode *id_node = (graph != NULL) ? graph->find_id_node(id)
327 DEG_id_type_tag(bmain, GS(id->name));
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);
335 int current_flag = flag;
336 while (current_flag != 0) {
338 (eDepsgraph_Tag)(1 << bitscan_forward_clear_i(¤t_flag));
339 deg_graph_id_tag_update_single_flag(bmain,
345 /* Special case for nested node tree datablocks. */
346 id_tag_update_ntree_special(bmain, graph, id, flag);
349 void deg_id_tag_update(Main *bmain, ID *id, int flag)
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,
358 if (depsgraph != NULL) {
359 deg_graph_id_tag_update(bmain, depsgraph, id, flag);
365 void deg_graph_on_visible_update(Main *bmain, Depsgraph *graph)
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);
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).
375 * TODO(sergey): Need to generalize this somehow.
377 if (id_type == ID_OB) {
378 flag |= OB_RECALC_OB | OB_RECALC_DATA | DEG_TAG_COPY_ON_WRITE;
380 deg_graph_id_tag_update(bmain, graph, id_node->id_orig, flag);
382 /* Make sure collection properties are up to date. */
383 for (Scene *scene_iter = graph->scene;
385 scene_iter = scene_iter->set)
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);
397 /* Data-Based Tagging */
399 /* Tag given ID for an update in all the dependency graphs. */
400 void DEG_id_tag_update(ID *id, int flag)
402 DEG_id_tag_update_ex(G.main, id, flag);
405 void DEG_id_tag_update_ex(Main *bmain, ID *id, int flag)
408 /* Ideally should not happen, but old depsgraph allowed this. */
411 DEG_DEBUG_PRINTF("%s: id=%s flag=%d\n", __func__, id->name, flag);
412 DEG::deg_id_tag_update(bmain, id, flag);
415 void DEG_graph_id_tag_update(struct Main *bmain,
416 struct Depsgraph *depsgraph,
420 DEG::Depsgraph *graph = (DEG::Depsgraph *)depsgraph;
421 DEG::deg_graph_id_tag_update(bmain, graph, id, flag);
424 /* Mark a particular datablock type as having changing. */
425 void DEG_id_type_tag(Main *bmain, short id_type)
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.
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);
438 bmain->id_tag_update[BKE_idcode_to_index(id_type)] = 1;
441 void DEG_graph_flush_update(Main *bmain, Depsgraph *depsgraph)
443 if (depsgraph == NULL) {
446 DEG::deg_graph_flush_updates(bmain, (DEG::Depsgraph *)depsgraph);
449 /* Update dependency graph when visible scenes/layers changes. */
450 void DEG_graph_on_visible_update(Main *bmain, Depsgraph *depsgraph)
452 DEG::Depsgraph *graph = (DEG::Depsgraph *)depsgraph;
453 DEG::deg_graph_on_visible_update(bmain, graph);
456 void DEG_on_visible_update(Main *bmain, const bool UNUSED(do_time))
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,
464 if (depsgraph != NULL) {
465 DEG_graph_on_visible_update(bmain, depsgraph);
471 /* Check if something was changed in the database and inform
472 * editors about this.
474 void DEG_ids_check_recalc(Main *bmain,
475 Depsgraph *depsgraph,
477 ViewLayer *view_layer,
480 ListBase *lbarray[MAX_LIBARRAY];
482 bool updated = false;
484 /* Loop over all ID types. */
485 a = set_listbasepointers(bmain, lbarray);
487 ListBase *lb = lbarray[a];
488 ID *id = (ID *)lb->first;
490 if (id && bmain->id_tag_update[BKE_idcode_to_index(GS(id->name))]) {
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));
504 void DEG_ids_clear_recalc(Main *bmain)
506 ListBase *lbarray[MAX_LIBARRAY];
510 /* TODO(sergey): Re-implement POST_UPDATE_HANDLER_WORKAROUND using entry_tags
511 * and id_tags storage from the new dependency graph.
514 /* Loop over all ID types. */
515 a = set_listbasepointers(bmain, lbarray);
517 ListBase *lb = lbarray[a];
518 ID *id = (ID *)lb->first;
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;
524 /* Some ID's contain semi-datablock nodetree */
525 ntree = ntreeFromID(id);
527 ntree->id.recalc &= ~ID_RECALC_ALL;
533 memset(bmain->id_tag_update, 0, sizeof(bmain->id_tag_update));