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): Based on original depsgraph.c code - Blender Foundation (2005-2013)
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/depsgraph/intern/depsgraph_build.cc
30 * Methods for constructing depsgraph.
33 #include "MEM_guardedalloc.h"
37 #include "BLI_utildefines.h"
38 #include "BLI_ghash.h"
39 #include "BLI_listbase.h"
42 # include "PIL_time.h"
43 # include "PIL_time_utildefines.h"
47 #include "DNA_cachefile_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_object_force.h"
53 #include "BKE_collision.h"
54 #include "BKE_effect.h"
55 #include "BKE_modifier.h"
56 #include "BKE_scene.h"
59 #include "DEG_depsgraph.h"
60 #include "DEG_depsgraph_debug.h"
61 #include "DEG_depsgraph_build.h"
63 #include "builder/deg_builder.h"
64 #include "builder/deg_builder_cycle.h"
65 #include "builder/deg_builder_nodes.h"
66 #include "builder/deg_builder_relations.h"
67 #include "builder/deg_builder_transitive.h"
69 #include "intern/nodes/deg_node.h"
70 #include "intern/nodes/deg_node_component.h"
71 #include "intern/nodes/deg_node_id.h"
72 #include "intern/nodes/deg_node_operation.h"
74 #include "intern/depsgraph_types.h"
75 #include "intern/depsgraph_intern.h"
77 #include "util/deg_util_foreach.h"
79 /* ****************** */
80 /* External Build API */
82 static DEG::eDepsNode_Type deg_build_scene_component_type(
83 eDepsSceneComponentType component)
86 case DEG_SCENE_COMP_PARAMETERS: return DEG::DEG_NODE_TYPE_PARAMETERS;
87 case DEG_SCENE_COMP_ANIMATION: return DEG::DEG_NODE_TYPE_ANIMATION;
88 case DEG_SCENE_COMP_SEQUENCER: return DEG::DEG_NODE_TYPE_SEQUENCER;
90 return DEG::DEG_NODE_TYPE_UNDEFINED;
93 static DEG::eDepsNode_Type deg_build_object_component_type(
94 eDepsObjectComponentType component)
97 case DEG_OB_COMP_PARAMETERS: return DEG::DEG_NODE_TYPE_PARAMETERS;
98 case DEG_OB_COMP_PROXY: return DEG::DEG_NODE_TYPE_PROXY;
99 case DEG_OB_COMP_ANIMATION: return DEG::DEG_NODE_TYPE_ANIMATION;
100 case DEG_OB_COMP_TRANSFORM: return DEG::DEG_NODE_TYPE_TRANSFORM;
101 case DEG_OB_COMP_GEOMETRY: return DEG::DEG_NODE_TYPE_GEOMETRY;
102 case DEG_OB_COMP_EVAL_POSE: return DEG::DEG_NODE_TYPE_EVAL_POSE;
103 case DEG_OB_COMP_BONE: return DEG::DEG_NODE_TYPE_BONE;
104 case DEG_OB_COMP_EVAL_PARTICLES: return DEG::DEG_NODE_TYPE_EVAL_PARTICLES;
105 case DEG_OB_COMP_SHADING: return DEG::DEG_NODE_TYPE_SHADING;
106 case DEG_OB_COMP_CACHE: return DEG::DEG_NODE_TYPE_CACHE;
108 return DEG::DEG_NODE_TYPE_UNDEFINED;
111 static DEG::DepsNodeHandle *get_handle(DepsNodeHandle *handle)
113 return reinterpret_cast<DEG::DepsNodeHandle *>(handle);
116 void DEG_add_scene_relation(DepsNodeHandle *handle,
118 eDepsSceneComponentType component,
119 const char *description)
121 DEG::eDepsNode_Type type = deg_build_scene_component_type(component);
122 DEG::ComponentKey comp_key(&scene->id, type);
123 DEG::DepsNodeHandle *deg_handle = get_handle(handle);
124 deg_handle->builder->add_node_handle_relation(comp_key,
129 void DEG_add_object_relation(DepsNodeHandle *handle,
131 eDepsObjectComponentType component,
132 const char *description)
134 DEG::eDepsNode_Type type = deg_build_object_component_type(component);
135 DEG::ComponentKey comp_key(&object->id, type);
136 DEG::DepsNodeHandle *deg_handle = get_handle(handle);
137 deg_handle->builder->add_node_handle_relation(comp_key,
142 void DEG_add_object_cache_relation(DepsNodeHandle *handle,
143 CacheFile *cache_file,
144 eDepsObjectComponentType component,
145 const char *description)
147 DEG::eDepsNode_Type type = deg_build_object_component_type(component);
148 DEG::ComponentKey comp_key(&cache_file->id, type);
149 DEG::DepsNodeHandle *deg_handle = get_handle(handle);
150 deg_handle->builder->add_node_handle_relation(comp_key,
155 void DEG_add_bone_relation(DepsNodeHandle *handle,
157 const char *bone_name,
158 eDepsObjectComponentType component,
159 const char *description)
161 DEG::eDepsNode_Type type = deg_build_object_component_type(component);
162 DEG::ComponentKey comp_key(&object->id, type, bone_name);
163 DEG::DepsNodeHandle *deg_handle = get_handle(handle);
164 /* XXX: "Geometry Eval" might not always be true, but this only gets called
165 * from modifier building now.
167 deg_handle->builder->add_node_handle_relation(comp_key,
172 struct Depsgraph *DEG_get_graph_from_handle(struct DepsNodeHandle *handle)
174 DEG::DepsNodeHandle *deg_handle = get_handle(handle);
175 DEG::DepsgraphRelationBuilder *relation_builder = deg_handle->builder;
176 return reinterpret_cast<Depsgraph *>(relation_builder->getGraph());
179 void DEG_add_special_eval_flag(Depsgraph *graph, ID *id, short flag)
181 DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
183 BLI_assert(!"Graph should always be valid");
186 DEG::IDDepsNode *id_node = deg_graph->find_id_node(id);
187 if (id_node == NULL) {
188 BLI_assert(!"ID should always be valid");
191 id_node->eval_flags |= flag;
194 /* ******************** */
195 /* Graph Building API's */
197 /* Build depsgraph for the given scene layer, and dump results in given
200 void DEG_graph_build_from_view_layer(Depsgraph *graph,
203 ViewLayer *view_layer)
206 TIMEIT_START(DEG_graph_build_from_view_layer);
209 DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
210 BLI_assert(BLI_findindex(&scene->view_layers, view_layer) != -1);
212 /* TODO(sergey): This is a bit tricky, but ensures that all the data
213 * is evaluated properly when depsgraph is becoming "visible".
215 * This now could happen for both visible scene is changed and extra
216 * dependency graph was created for render engine.
218 const bool need_on_visible_update = (deg_graph->scene == NULL);
220 /* 1) Generate all the nodes in the graph first */
221 DEG::DepsgraphNodeBuilder node_builder(bmain, deg_graph);
222 node_builder.begin_build();
223 node_builder.build_view_layer(scene,
225 DEG::DEG_ID_LINKED_DIRECTLY);
226 node_builder.end_build();
228 /* 2) Hook up relationships between operations - to determine evaluation
231 DEG::DepsgraphRelationBuilder relation_builder(bmain, deg_graph);
232 relation_builder.begin_build();
233 relation_builder.build_view_layer(scene, view_layer);
234 if (DEG_depsgraph_use_copy_on_write()) {
235 relation_builder.build_copy_on_write_relations();
238 /* Detect and solve cycles. */
239 DEG::deg_graph_detect_cycles(deg_graph);
241 /* 3) Simplify the graph by removing redundant relations (to optimize
242 * traversal later). */
243 /* TODO: it would be useful to have an option to disable this in cases where
244 * it is causing trouble.
246 if (G.debug_value == 799) {
247 DEG::deg_graph_transitive_reduction(deg_graph);
250 /* 4) Flush visibility layer and re-schedule nodes for update. */
251 DEG::deg_graph_build_finalize(bmain, deg_graph);
254 if (!DEG_debug_consistency_check(deg_graph)) {
255 printf("Consistency validation failed, ABORTING!\n");
261 TIMEIT_END(DEG_graph_build_from_view_layer);
264 /* Relations are up to date. */
265 deg_graph->need_update = false;
267 if (need_on_visible_update) {
268 DEG_graph_on_visible_update(bmain, graph);
272 /* Tag graph relations for update. */
273 void DEG_graph_tag_relations_update(Depsgraph *graph)
275 DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
276 deg_graph->need_update = true;
279 /* Create or update relations in the specified graph. */
280 void DEG_graph_relations_update(Depsgraph *graph,
283 ViewLayer *view_layer)
285 DEG::Depsgraph *deg_graph = (DEG::Depsgraph *)graph;
286 if (!deg_graph->need_update) {
287 /* Graph is up to date, nothing to do. */
290 DEG_graph_build_from_view_layer(graph, bmain, scene, view_layer);
293 /* Tag all relations for update. */
294 void DEG_relations_tag_update(Main *bmain)
296 DEG_DEBUG_PRINTF("%s: Tagging relations for update.\n", __func__);
297 LINKLIST_FOREACH(Scene *, scene, &bmain->scene) {
298 LINKLIST_FOREACH(ViewLayer *, view_layer, &scene->view_layers) {
299 Depsgraph *depsgraph =
300 (Depsgraph *)BKE_scene_get_depsgraph(scene,
303 if (depsgraph != NULL) {
304 DEG_graph_tag_relations_update(depsgraph);
310 void DEG_add_collision_relations(DepsNodeHandle *handle,
314 unsigned int modifier_type,
315 DEG_CollobjFilterFunction fn,
319 unsigned int numcollobj;
320 Object **collobjs = get_collisionobjects_ext(scene, object, group, &numcollobj, modifier_type, dupli);
322 for (unsigned int i = 0; i < numcollobj; i++) {
323 Object *ob1 = collobjs[i];
325 if (!fn || fn(ob1, modifiers_findByType(ob1, (ModifierType)modifier_type))) {
326 DEG_add_object_relation(handle, ob1, DEG_OB_COMP_TRANSFORM, name);
327 DEG_add_object_relation(handle, ob1, DEG_OB_COMP_GEOMETRY, name);
335 void DEG_add_forcefield_relations(DepsNodeHandle *handle,
338 EffectorWeights *effector_weights,
343 ListBase *effectors = pdInitEffectors(NULL, scene, object, NULL, effector_weights, false);
346 for (EffectorCache *eff = (EffectorCache*)effectors->first; eff; eff = eff->next) {
347 if (eff->ob != object && eff->pd->forcefield != skip_forcefield) {
348 DEG_add_object_relation(handle, eff->ob, DEG_OB_COMP_TRANSFORM, name);
351 DEG_add_object_relation(handle, eff->ob, DEG_OB_COMP_EVAL_PARTICLES, name);
353 /* TODO: remove this when/if EVAL_PARTICLES is sufficient
354 * for up to date particles.
356 DEG_add_object_relation(handle, eff->ob, DEG_OB_COMP_GEOMETRY, name);
359 if (eff->pd->forcefield == PFIELD_SMOKEFLOW && eff->pd->f_source) {
360 DEG_add_object_relation(handle,
362 DEG_OB_COMP_TRANSFORM,
363 "Smoke Force Domain");
364 DEG_add_object_relation(handle,
366 DEG_OB_COMP_GEOMETRY,
367 "Smoke Force Domain");
370 if (add_absorption && (eff->pd->flag & PFIELD_VISIBILITY)) {
371 DEG_add_collision_relations(handle,
375 eModifierType_Collision,
384 pdEndEffectors(&effectors);