Depsgraph: Cleanup, reduce indentation level
[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 // #define DEBUG_TIME
36
37 #include "BLI_utildefines.h"
38 #include "BLI_ghash.h"
39
40 #ifdef DEBUG_TIME
41 #  include "PIL_time.h"
42 #  include "PIL_time_utildefines.h"
43 #endif
44
45 extern "C" {
46 #include "DNA_cachefile_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_object_force_types.h"
50
51 #include "BKE_main.h"
52 #include "BKE_collision.h"
53 #include "BKE_effect.h"
54 #include "BKE_modifier.h"
55 } /* extern "C" */
56
57 #include "DEG_depsgraph.h"
58 #include "DEG_depsgraph_debug.h"
59 #include "DEG_depsgraph_build.h"
60
61 #include "builder/deg_builder.h"
62 #include "builder/deg_builder_cycle.h"
63 #include "builder/deg_builder_nodes.h"
64 #include "builder/deg_builder_relations.h"
65 #include "builder/deg_builder_transitive.h"
66
67 #include "intern/nodes/deg_node.h"
68 #include "intern/nodes/deg_node_component.h"
69 #include "intern/nodes/deg_node_id.h"
70 #include "intern/nodes/deg_node_operation.h"
71
72 #include "intern/depsgraph_types.h"
73 #include "intern/depsgraph_intern.h"
74
75 #include "util/deg_util_foreach.h"
76
77 /* ****************** */
78 /* External Build API */
79
80 static DEG::eDepsNode_Type deg_build_scene_component_type(
81         eDepsSceneComponentType component)
82 {
83         switch (component) {
84                 case DEG_SCENE_COMP_PARAMETERS:     return DEG::DEG_NODE_TYPE_PARAMETERS;
85                 case DEG_SCENE_COMP_ANIMATION:      return DEG::DEG_NODE_TYPE_ANIMATION;
86                 case DEG_SCENE_COMP_SEQUENCER:      return DEG::DEG_NODE_TYPE_SEQUENCER;
87         }
88         return DEG::DEG_NODE_TYPE_UNDEFINED;
89 }
90
91 static DEG::eDepsNode_Type deg_build_object_component_type(
92         eDepsObjectComponentType component)
93 {
94         switch (component) {
95                 case DEG_OB_COMP_PARAMETERS:        return DEG::DEG_NODE_TYPE_PARAMETERS;
96                 case DEG_OB_COMP_PROXY:             return DEG::DEG_NODE_TYPE_PROXY;
97                 case DEG_OB_COMP_ANIMATION:         return DEG::DEG_NODE_TYPE_ANIMATION;
98                 case DEG_OB_COMP_TRANSFORM:         return DEG::DEG_NODE_TYPE_TRANSFORM;
99                 case DEG_OB_COMP_GEOMETRY:          return DEG::DEG_NODE_TYPE_GEOMETRY;
100                 case DEG_OB_COMP_EVAL_POSE:         return DEG::DEG_NODE_TYPE_EVAL_POSE;
101                 case DEG_OB_COMP_BONE:              return DEG::DEG_NODE_TYPE_BONE;
102                 case DEG_OB_COMP_EVAL_PARTICLES:    return DEG::DEG_NODE_TYPE_EVAL_PARTICLES;
103                 case DEG_OB_COMP_SHADING:           return DEG::DEG_NODE_TYPE_SHADING;
104                 case DEG_OB_COMP_CACHE:             return DEG::DEG_NODE_TYPE_CACHE;
105         }
106         return DEG::DEG_NODE_TYPE_UNDEFINED;
107 }
108
109 static DEG::DepsNodeHandle *get_handle(DepsNodeHandle *handle)
110 {
111         return reinterpret_cast<DEG::DepsNodeHandle *>(handle);
112 }
113
114 void DEG_add_scene_relation(DepsNodeHandle *handle,
115                             Scene *scene,
116                             eDepsSceneComponentType component,
117                             const char *description)
118 {
119         DEG::eDepsNode_Type type = deg_build_scene_component_type(component);
120         DEG::ComponentKey comp_key(&scene->id, type);
121         DEG::DepsNodeHandle *deg_handle = get_handle(handle);
122         deg_handle->builder->add_node_handle_relation(comp_key,
123                                                       deg_handle,
124                                                       description);
125 }
126
127 void DEG_add_object_relation(DepsNodeHandle *handle,
128                              Object *object,
129                              eDepsObjectComponentType component,
130                              const char *description)
131 {
132         DEG::eDepsNode_Type type = deg_build_object_component_type(component);
133         DEG::ComponentKey comp_key(&object->id, type);
134         DEG::DepsNodeHandle *deg_handle = get_handle(handle);
135         deg_handle->builder->add_node_handle_relation(comp_key,
136                                                       deg_handle,
137                                                       description);
138 }
139
140 void DEG_add_object_cache_relation(DepsNodeHandle *handle,
141                                    CacheFile *cache_file,
142                                    eDepsObjectComponentType component,
143                                    const char *description)
144 {
145         DEG::eDepsNode_Type type = deg_build_object_component_type(component);
146         DEG::ComponentKey comp_key(&cache_file->id, type);
147         DEG::DepsNodeHandle *deg_handle = get_handle(handle);
148         deg_handle->builder->add_node_handle_relation(comp_key,
149                                                       deg_handle,
150                                                       description);
151 }
152
153 void DEG_add_bone_relation(DepsNodeHandle *handle,
154                            Object *object,
155                            const char *bone_name,
156                            eDepsObjectComponentType component,
157                            const char *description)
158 {
159         DEG::eDepsNode_Type type = deg_build_object_component_type(component);
160         DEG::ComponentKey comp_key(&object->id, type, bone_name);
161         DEG::DepsNodeHandle *deg_handle = get_handle(handle);
162         /* XXX: "Geometry Eval" might not always be true, but this only gets called
163          * from modifier building now.
164          */
165         deg_handle->builder->add_node_handle_relation(comp_key,
166                                                       deg_handle,
167                                                       description);
168 }
169
170 struct Depsgraph *DEG_get_graph_from_handle(struct DepsNodeHandle *handle)
171 {
172         DEG::DepsNodeHandle *deg_handle = get_handle(handle);
173         DEG::DepsgraphRelationBuilder *relation_builder = deg_handle->builder;
174         return reinterpret_cast<Depsgraph *>(relation_builder->getGraph());
175 }
176
177 void DEG_add_special_eval_flag(Depsgraph *graph, ID *id, short flag)
178 {
179         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
180         if (graph == NULL) {
181                 BLI_assert(!"Graph should always be valid");
182                 return;
183         }
184         DEG::IDDepsNode *id_node = deg_graph->find_id_node(id);
185         if (id_node == NULL) {
186                 BLI_assert(!"ID should always be valid");
187                 return;
188         }
189         id_node->eval_flags |= flag;
190 }
191
192 /* ******************** */
193 /* Graph Building API's */
194
195 /* Build depsgraph for the given scene, and dump results in given
196  * graph container.
197  */
198 /* XXX: assume that this is called from outside, given the current scene as
199  * the "main" scene.
200  */
201 void DEG_graph_build_from_scene(Depsgraph *graph, Main *bmain, Scene *scene)
202 {
203 #ifdef DEBUG_TIME
204         TIMEIT_START(DEG_graph_build_from_scene);
205 #endif
206
207         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
208
209         /* 1) Generate all the nodes in the graph first */
210         DEG::DepsgraphNodeBuilder node_builder(bmain, deg_graph);
211         node_builder.begin_build();
212         node_builder.build_scene(scene);
213
214         /* 2) Hook up relationships between operations - to determine evaluation
215          *    order.
216          */
217         DEG::DepsgraphRelationBuilder relation_builder(bmain, deg_graph);
218         relation_builder.begin_build();
219         relation_builder.build_scene(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 #ifdef DEBUG_TIME
244         TIMEIT_END(DEG_graph_build_from_scene);
245 #endif
246 }
247
248 /* Tag graph relations for update. */
249 void DEG_graph_tag_relations_update(Depsgraph *graph)
250 {
251         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
252         deg_graph->need_update = true;
253 }
254
255 /* Tag all relations for update. */
256 void DEG_relations_tag_update(Main *bmain)
257 {
258         for (Scene *scene = (Scene *)bmain->scene.first;
259              scene != NULL;
260              scene = (Scene *)scene->id.next)
261         {
262                 if (scene->depsgraph != NULL) {
263                         DEG_graph_tag_relations_update(scene->depsgraph);
264                 }
265         }
266 }
267
268 /* Create new graph if didn't exist yet,
269  * or update relations if graph was tagged for update.
270  */
271 void DEG_scene_relations_update(Main *bmain, Scene *scene)
272 {
273         if (scene->depsgraph == NULL) {
274                 /* Rebuild graph from scratch and exit. */
275                 scene->depsgraph = DEG_graph_new();
276                 DEG_graph_build_from_scene(scene->depsgraph, bmain, scene);
277                 return;
278         }
279
280         DEG::Depsgraph *graph = reinterpret_cast<DEG::Depsgraph *>(scene->depsgraph);
281         if (!graph->need_update) {
282                 /* Graph is up to date, nothing to do. */
283                 return;
284         }
285
286         /* Clear all previous nodes and operations. */
287         graph->clear_all_nodes();
288         graph->operations.clear();
289         BLI_gset_clear(graph->entry_tags, NULL);
290
291         /* Build new nodes and relations. */
292         DEG_graph_build_from_scene(reinterpret_cast< ::Depsgraph * >(graph),
293                                    bmain,
294                                    scene);
295
296         graph->need_update = false;
297 }
298
299 /* Rebuild dependency graph only for a given scene. */
300 void DEG_scene_relations_rebuild(Main *bmain, Scene *scene)
301 {
302         if (scene->depsgraph != NULL) {
303                 DEG_graph_tag_relations_update(scene->depsgraph);
304         }
305         DEG_scene_relations_update(bmain, scene);
306 }
307
308 void DEG_scene_graph_free(Scene *scene)
309 {
310         if (scene->depsgraph) {
311                 DEG_graph_free(scene->depsgraph);
312                 scene->depsgraph = NULL;
313         }
314 }
315
316 void DEG_add_collision_relations(DepsNodeHandle *handle,
317                                  Scene *scene,
318                                  Object *object,
319                                  Group *group,
320                                  int layer,
321                                  unsigned int modifier_type,
322                                  DEG_CollobjFilterFunction fn,
323                                  bool dupli,
324                                  const char *name)
325 {
326         unsigned int numcollobj;
327         Object **collobjs = get_collisionobjects_ext(scene, object, group, layer, &numcollobj, modifier_type, dupli);
328
329         for (unsigned int i = 0; i < numcollobj; i++) {
330                 Object *ob1 = collobjs[i];
331
332                 if (!fn || fn(ob1, modifiers_findByType(ob1, (ModifierType)modifier_type))) {
333                         DEG_add_object_relation(handle, ob1, DEG_OB_COMP_TRANSFORM, name);
334                         DEG_add_object_relation(handle, ob1, DEG_OB_COMP_GEOMETRY, name);
335                 }
336         }
337
338         if (collobjs)
339                 MEM_freeN(collobjs);
340 }
341
342 void DEG_add_forcefield_relations(DepsNodeHandle *handle,
343                                   Scene *scene,
344                                   Object *object,
345                                   EffectorWeights *effector_weights,
346                                   bool add_absorption,
347                                   int skip_forcefield,
348                                   const char *name)
349 {
350         ListBase *effectors = pdInitEffectors(scene, object, NULL, effector_weights, false);
351         if (effectors == NULL) {
352                 return;
353         }
354         for (EffectorCache *eff = (EffectorCache*)effectors->first; eff; eff = eff->next) {
355                 if (eff->ob != object && eff->pd->forcefield != skip_forcefield) {
356                         DEG_add_object_relation(handle, eff->ob, DEG_OB_COMP_TRANSFORM, name);
357                         if (eff->psys) {
358                                 DEG_add_object_relation(handle, eff->ob, DEG_OB_COMP_EVAL_PARTICLES, name);
359                                 /* TODO: remove this when/if EVAL_PARTICLES is sufficient
360                                  * for up to date particles.
361                                  */
362                                 DEG_add_object_relation(handle, eff->ob, DEG_OB_COMP_GEOMETRY, name);
363                         }
364                         if (eff->pd->forcefield == PFIELD_SMOKEFLOW && eff->pd->f_source) {
365                                 DEG_add_object_relation(handle,
366                                                         eff->pd->f_source,
367                                                         DEG_OB_COMP_TRANSFORM,
368                                                         "Smoke Force Domain");
369                                 DEG_add_object_relation(handle,
370                                                         eff->pd->f_source,
371                                                         DEG_OB_COMP_GEOMETRY,
372                                                         "Smoke Force Domain");
373                         }
374                         if (add_absorption && (eff->pd->flag & PFIELD_VISIBILITY)) {
375                                 DEG_add_collision_relations(handle,
376                                                             scene,
377                                                             object,
378                                                             NULL,
379                                                             eff->ob->lay,
380                                                             eModifierType_Collision,
381                                                             NULL,
382                                                             true,
383                                                             "Force Absorption");
384                         }
385                 }
386         }
387         pdEndEffectors(&effectors);
388 }