Fix depsgraph to compute more accurate links for collision & force.
[blender.git] / source / blender / depsgraph / intern / depsgraph_build.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): Based on original depsgraph.c code - Blender Foundation (2005-2013)
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/depsgraph/intern/depsgraph_build.cc
28  *  \ingroup depsgraph
29  *
30  * Methods for constructing depsgraph.
31  */
32
33 #include "MEM_guardedalloc.h"
34
35 extern "C" {
36 #include "DNA_cachefile_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_object_force.h"
40
41 #include "BLI_utildefines.h"
42 #include "BLI_ghash.h"
43
44 #include "BKE_main.h"
45 #include "BKE_collision.h"
46 #include "BKE_effect.h"
47 #include "BKE_modifier.h"
48
49 #include "DEG_depsgraph.h"
50 #include "DEG_depsgraph_debug.h"
51 #include "DEG_depsgraph_build.h"
52
53 } /* extern "C" */
54
55 #include "builder/deg_builder.h"
56 #include "builder/deg_builder_cycle.h"
57 #include "builder/deg_builder_nodes.h"
58 #include "builder/deg_builder_relations.h"
59 #include "builder/deg_builder_transitive.h"
60
61 #include "intern/nodes/deg_node.h"
62 #include "intern/nodes/deg_node_component.h"
63 #include "intern/nodes/deg_node_operation.h"
64
65 #include "intern/depsgraph_types.h"
66 #include "intern/depsgraph_intern.h"
67
68 #include "util/deg_util_foreach.h"
69
70 /* ****************** */
71 /* External Build API */
72
73 static DEG::eDepsNode_Type deg_build_scene_component_type(
74         eDepsSceneComponentType component)
75 {
76         switch (component) {
77                 case DEG_SCENE_COMP_PARAMETERS:     return DEG::DEPSNODE_TYPE_PARAMETERS;
78                 case DEG_SCENE_COMP_ANIMATION:      return DEG::DEPSNODE_TYPE_ANIMATION;
79                 case DEG_SCENE_COMP_SEQUENCER:      return DEG::DEPSNODE_TYPE_SEQUENCER;
80         }
81         return DEG::DEPSNODE_TYPE_UNDEFINED;
82 }
83
84 static DEG::eDepsNode_Type deg_build_object_component_type(
85         eDepsObjectComponentType component)
86 {
87         switch (component) {
88                 case DEG_OB_COMP_PARAMETERS:        return DEG::DEPSNODE_TYPE_PARAMETERS;
89                 case DEG_OB_COMP_PROXY:             return DEG::DEPSNODE_TYPE_PROXY;
90                 case DEG_OB_COMP_ANIMATION:         return DEG::DEPSNODE_TYPE_ANIMATION;
91                 case DEG_OB_COMP_TRANSFORM:         return DEG::DEPSNODE_TYPE_TRANSFORM;
92                 case DEG_OB_COMP_GEOMETRY:          return DEG::DEPSNODE_TYPE_GEOMETRY;
93                 case DEG_OB_COMP_EVAL_POSE:         return DEG::DEPSNODE_TYPE_EVAL_POSE;
94                 case DEG_OB_COMP_BONE:              return DEG::DEPSNODE_TYPE_BONE;
95                 case DEG_OB_COMP_EVAL_PARTICLES:    return DEG::DEPSNODE_TYPE_EVAL_PARTICLES;
96                 case DEG_OB_COMP_SHADING:           return DEG::DEPSNODE_TYPE_SHADING;
97                 case DEG_OB_COMP_CACHE:             return DEG::DEPSNODE_TYPE_CACHE;
98         }
99         return DEG::DEPSNODE_TYPE_UNDEFINED;
100 }
101
102 static DEG::DepsNodeHandle *get_handle(DepsNodeHandle *handle)
103 {
104         return reinterpret_cast<DEG::DepsNodeHandle *>(handle);
105 }
106
107 void DEG_add_scene_relation(DepsNodeHandle *handle,
108                             Scene *scene,
109                             eDepsSceneComponentType component,
110                             const char *description)
111 {
112         DEG::eDepsNode_Type type = deg_build_scene_component_type(component);
113         DEG::ComponentKey comp_key(&scene->id, type);
114         DEG::DepsNodeHandle *deg_handle = get_handle(handle);
115         deg_handle->builder->add_node_handle_relation(comp_key,
116                                                       deg_handle,
117                                                       DEG::DEPSREL_TYPE_GEOMETRY_EVAL,
118                                                       description);
119 }
120
121 void DEG_add_object_relation(DepsNodeHandle *handle,
122                              Object *ob,
123                              eDepsObjectComponentType component,
124                              const char *description)
125 {
126         DEG::eDepsNode_Type type = deg_build_object_component_type(component);
127         DEG::ComponentKey comp_key(&ob->id, type);
128         DEG::DepsNodeHandle *deg_handle = get_handle(handle);
129         deg_handle->builder->add_node_handle_relation(comp_key,
130                                                       deg_handle,
131                                                       DEG::DEPSREL_TYPE_GEOMETRY_EVAL,
132                                                       description);
133 }
134
135 void DEG_add_object_cache_relation(DepsNodeHandle *handle,
136                                    CacheFile *cache_file,
137                                    eDepsObjectComponentType component,
138                                    const char *description)
139 {
140         DEG::eDepsNode_Type type = deg_build_object_component_type(component);
141         DEG::ComponentKey comp_key(&cache_file->id, type);
142         DEG::DepsNodeHandle *deg_handle = get_handle(handle);
143         deg_handle->builder->add_node_handle_relation(comp_key,
144                                                       deg_handle,
145                                                       DEG::DEPSREL_TYPE_CACHE,
146                                                       description);
147 }
148
149 void DEG_add_bone_relation(DepsNodeHandle *handle,
150                            Object *ob,
151                            const char *bone_name,
152                            eDepsObjectComponentType component,
153                            const char *description)
154 {
155         DEG::eDepsNode_Type type = deg_build_object_component_type(component);
156         DEG::ComponentKey comp_key(&ob->id, type, bone_name);
157         DEG::DepsNodeHandle *deg_handle = get_handle(handle);
158         /* XXX: "Geometry Eval" might not always be true, but this only gets called
159          * from modifier building now.
160          */
161         deg_handle->builder->add_node_handle_relation(comp_key,
162                                                       deg_handle,
163                                                       DEG::DEPSREL_TYPE_GEOMETRY_EVAL,
164                                                       description);
165 }
166
167 void DEG_add_special_eval_flag(Depsgraph *graph, ID *id, short flag)
168 {
169         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
170         if (graph == NULL) {
171                 BLI_assert(!"Graph should always be valid");
172                 return;
173         }
174         DEG::IDDepsNode *id_node = deg_graph->find_id_node(id);
175         if (id_node == NULL) {
176                 BLI_assert(!"ID should always be valid");
177                 return;
178         }
179         id_node->eval_flags |= flag;
180 }
181
182 /* ******************** */
183 /* Graph Building API's */
184
185 /* Build depsgraph for the given scene, and dump results in given
186  * graph container.
187  */
188 /* XXX: assume that this is called from outside, given the current scene as
189  * the "main" scene.
190  */
191 void DEG_graph_build_from_scene(Depsgraph *graph, Main *bmain, Scene *scene)
192 {
193         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
194
195         /* 1) Generate all the nodes in the graph first */
196         DEG::DepsgraphNodeBuilder node_builder(bmain, deg_graph);
197         /* create root node for scene first
198          * - this way it should be the first in the graph,
199          *   reflecting its role as the entrypoint
200          */
201         node_builder.add_root_node();
202         node_builder.build_scene(bmain, scene);
203
204         /* 2) Hook up relationships between operations - to determine evaluation
205          *    order.
206          */
207         DEG::DepsgraphRelationBuilder relation_builder(deg_graph);
208         /* Hook scene up to the root node as entrypoint to graph. */
209         /* XXX what does this relation actually mean?
210          * it doesnt add any operations anyway and is not clear what part of the
211          * scene is to be connected.
212          */
213 #if 0
214         relation_builder.add_relation(RootKey(),
215                                       IDKey(scene),
216                                       DEPSREL_TYPE_ROOT_TO_ACTIVE,
217                                       "Root to Active Scene");
218 #endif
219         relation_builder.build_scene(bmain, scene);
220
221         /* Detect and solve cycles. */
222         DEG::deg_graph_detect_cycles(deg_graph);
223
224         /* 3) Simplify the graph by removing redundant relations (to optimize
225          *    traversal later). */
226         /* TODO: it would be useful to have an option to disable this in cases where
227          *       it is causing trouble.
228          */
229         if (G.debug_value == 799) {
230                 DEG::deg_graph_transitive_reduction(deg_graph);
231         }
232
233         /* 4) Flush visibility layer and re-schedule nodes for update. */
234         DEG::deg_graph_build_finalize(deg_graph);
235
236 #if 0
237         if (!DEG_debug_consistency_check(deg_graph)) {
238                 printf("Consistency validation failed, ABORTING!\n");
239                 abort();
240         }
241 #endif
242 }
243
244 /* Tag graph relations for update. */
245 void DEG_graph_tag_relations_update(Depsgraph *graph)
246 {
247         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
248         deg_graph->need_update = true;
249 }
250
251 /* Tag all relations for update. */
252 void DEG_relations_tag_update(Main *bmain)
253 {
254         for (Scene *scene = (Scene *)bmain->scene.first;
255              scene != NULL;
256              scene = (Scene *)scene->id.next)
257         {
258                 if (scene->depsgraph != NULL) {
259                         DEG_graph_tag_relations_update(scene->depsgraph);
260                 }
261         }
262 }
263
264 /* Create new graph if didn't exist yet,
265  * or update relations if graph was tagged for update.
266  */
267 void DEG_scene_relations_update(Main *bmain, Scene *scene)
268 {
269         if (scene->depsgraph == NULL) {
270                 /* Rebuild graph from scratch and exit. */
271                 scene->depsgraph = DEG_graph_new();
272                 DEG_graph_build_from_scene(scene->depsgraph, bmain, scene);
273                 return;
274         }
275
276         DEG::Depsgraph *graph = reinterpret_cast<DEG::Depsgraph *>(scene->depsgraph);
277         if (!graph->need_update) {
278                 /* Graph is up to date, nothing to do. */
279                 return;
280         }
281
282         /* Clear all previous nodes and operations. */
283         graph->clear_all_nodes();
284         graph->operations.clear();
285         BLI_gset_clear(graph->entry_tags, NULL);
286
287         /* Build new nodes and relations. */
288         DEG_graph_build_from_scene(reinterpret_cast< ::Depsgraph * >(graph),
289                                    bmain,
290                                    scene);
291
292         graph->need_update = false;
293 }
294
295 /* Rebuild dependency graph only for a given scene. */
296 void DEG_scene_relations_rebuild(Main *bmain, Scene *scene)
297 {
298         if (scene->depsgraph != NULL) {
299                 DEG_graph_tag_relations_update(scene->depsgraph);
300         }
301         DEG_scene_relations_update(bmain, scene);
302 }
303
304 void DEG_scene_graph_free(Scene *scene)
305 {
306         if (scene->depsgraph) {
307                 DEG_graph_free(scene->depsgraph);
308                 scene->depsgraph = NULL;
309         }
310 }
311
312 void DEG_add_collision_relations(DepsNodeHandle *handle, Scene *scene, Object *ob, Group *group, int layer, unsigned int modifier_type, DEG_CollobjFilterFunction fn, bool dupli, const char *name)
313 {
314         unsigned int numcollobj;
315         Object **collobjs = get_collisionobjects_ext(scene, ob, group, layer, &numcollobj, modifier_type, dupli);
316
317         for (unsigned int i = 0; i < numcollobj; i++) {
318                 Object *ob1 = collobjs[i];
319
320                 if (!fn || fn(ob1, modifiers_findByType(ob1, (ModifierType)modifier_type))) {
321                         DEG_add_object_relation(handle, ob1, DEG_OB_COMP_TRANSFORM, name);
322                         DEG_add_object_relation(handle, ob1, DEG_OB_COMP_GEOMETRY, name);
323                 }
324         }
325
326         if (collobjs)
327                 MEM_freeN(collobjs);
328 }
329
330 void DEG_add_forcefield_relations(DepsNodeHandle *handle, Scene *scene, Object *ob, EffectorWeights *effector_weights, bool add_absorption, int skip_forcefield, const char *name)
331 {
332         ListBase *effectors = pdInitEffectors(scene, ob, NULL, effector_weights, false);
333
334         if (effectors) {
335                 for (EffectorCache *eff = (EffectorCache*)effectors->first; eff; eff = eff->next) {
336                         if (eff->ob != ob && eff->pd->forcefield != skip_forcefield) {
337                                 DEG_add_object_relation(handle, eff->ob, DEG_OB_COMP_TRANSFORM, name);
338
339                                 if (eff->psys) {
340                                         DEG_add_object_relation(handle, eff->ob, DEG_OB_COMP_EVAL_PARTICLES, name);
341
342                                         /* TODO: remove this when/if EVAL_PARTICLES is sufficient for up to date particles */
343                                         DEG_add_object_relation(handle, eff->ob, DEG_OB_COMP_GEOMETRY, name);
344                                 }
345
346                                 if (eff->pd->forcefield == PFIELD_SMOKEFLOW && eff->pd->f_source) {
347                                         DEG_add_object_relation(handle, eff->pd->f_source, DEG_OB_COMP_TRANSFORM, "Smoke Force Domain");
348                                         DEG_add_object_relation(handle, eff->pd->f_source, DEG_OB_COMP_GEOMETRY, "Smoke Force Domain");
349                                 }
350
351                                 if (add_absorption && (eff->pd->flag & PFIELD_VISIBILITY)) {
352                                         DEG_add_collision_relations(handle, scene, ob, NULL, eff->ob->lay, eModifierType_Collision, NULL, true, "Force Absorption");
353                                 }
354                         }
355                 }
356         }
357
358         pdEndEffectors(&effectors);
359 }