Rename any instance of scene layer or render layer in code with view layer
[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 #include "BLI_listbase.h"
40
41 #ifdef DEBUG_TIME
42 #  include "PIL_time.h"
43 #  include "PIL_time_utildefines.h"
44 #endif
45
46 extern "C" {
47 #include "DNA_cachefile_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_object_force.h"
51
52 #include "BKE_main.h"
53 #include "BKE_collision.h"
54 #include "BKE_effect.h"
55 #include "BKE_modifier.h"
56 #include "BKE_scene.h"
57 } /* extern "C" */
58
59 #include "DEG_depsgraph.h"
60 #include "DEG_depsgraph_debug.h"
61 #include "DEG_depsgraph_build.h"
62
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"
68
69 #include "intern/nodes/deg_node.h"
70 #include "intern/nodes/deg_node_component.h"
71 #include "intern/nodes/deg_node_operation.h"
72
73 #include "intern/depsgraph_types.h"
74 #include "intern/depsgraph_intern.h"
75
76 #include "util/deg_util_foreach.h"
77
78 /* ****************** */
79 /* External Build API */
80
81 static DEG::eDepsNode_Type deg_build_scene_component_type(
82         eDepsSceneComponentType component)
83 {
84         switch (component) {
85                 case DEG_SCENE_COMP_PARAMETERS:     return DEG::DEG_NODE_TYPE_PARAMETERS;
86                 case DEG_SCENE_COMP_ANIMATION:      return DEG::DEG_NODE_TYPE_ANIMATION;
87                 case DEG_SCENE_COMP_SEQUENCER:      return DEG::DEG_NODE_TYPE_SEQUENCER;
88         }
89         return DEG::DEG_NODE_TYPE_UNDEFINED;
90 }
91
92 static DEG::eDepsNode_Type deg_build_object_component_type(
93         eDepsObjectComponentType component)
94 {
95         switch (component) {
96                 case DEG_OB_COMP_PARAMETERS:        return DEG::DEG_NODE_TYPE_PARAMETERS;
97                 case DEG_OB_COMP_PROXY:             return DEG::DEG_NODE_TYPE_PROXY;
98                 case DEG_OB_COMP_ANIMATION:         return DEG::DEG_NODE_TYPE_ANIMATION;
99                 case DEG_OB_COMP_TRANSFORM:         return DEG::DEG_NODE_TYPE_TRANSFORM;
100                 case DEG_OB_COMP_GEOMETRY:          return DEG::DEG_NODE_TYPE_GEOMETRY;
101                 case DEG_OB_COMP_EVAL_POSE:         return DEG::DEG_NODE_TYPE_EVAL_POSE;
102                 case DEG_OB_COMP_BONE:              return DEG::DEG_NODE_TYPE_BONE;
103                 case DEG_OB_COMP_EVAL_PARTICLES:    return DEG::DEG_NODE_TYPE_EVAL_PARTICLES;
104                 case DEG_OB_COMP_SHADING:           return DEG::DEG_NODE_TYPE_SHADING;
105                 case DEG_OB_COMP_CACHE:             return DEG::DEG_NODE_TYPE_CACHE;
106         }
107         return DEG::DEG_NODE_TYPE_UNDEFINED;
108 }
109
110 static DEG::DepsNodeHandle *get_handle(DepsNodeHandle *handle)
111 {
112         return reinterpret_cast<DEG::DepsNodeHandle *>(handle);
113 }
114
115 void DEG_add_scene_relation(DepsNodeHandle *handle,
116                             Scene *scene,
117                             eDepsSceneComponentType component,
118                             const char *description)
119 {
120         DEG::eDepsNode_Type type = deg_build_scene_component_type(component);
121         DEG::ComponentKey comp_key(&scene->id, type);
122         DEG::DepsNodeHandle *deg_handle = get_handle(handle);
123         deg_handle->builder->add_node_handle_relation(comp_key,
124                                                       deg_handle,
125                                                       description);
126 }
127
128 void DEG_add_object_relation(DepsNodeHandle *handle,
129                              Object *ob,
130                              eDepsObjectComponentType component,
131                              const char *description)
132 {
133         DEG::eDepsNode_Type type = deg_build_object_component_type(component);
134         DEG::ComponentKey comp_key(&ob->id, type);
135         DEG::DepsNodeHandle *deg_handle = get_handle(handle);
136         deg_handle->builder->add_node_handle_relation(comp_key,
137                                                       deg_handle,
138                                                       description);
139 }
140
141 void DEG_add_object_cache_relation(DepsNodeHandle *handle,
142                                    CacheFile *cache_file,
143                                    eDepsObjectComponentType component,
144                                    const char *description)
145 {
146         DEG::eDepsNode_Type type = deg_build_object_component_type(component);
147         DEG::ComponentKey comp_key(&cache_file->id, type);
148         DEG::DepsNodeHandle *deg_handle = get_handle(handle);
149         deg_handle->builder->add_node_handle_relation(comp_key,
150                                                       deg_handle,
151                                                       description);
152 }
153
154 void DEG_add_bone_relation(DepsNodeHandle *handle,
155                            Object *ob,
156                            const char *bone_name,
157                            eDepsObjectComponentType component,
158                            const char *description)
159 {
160         DEG::eDepsNode_Type type = deg_build_object_component_type(component);
161         DEG::ComponentKey comp_key(&ob->id, type, bone_name);
162         DEG::DepsNodeHandle *deg_handle = get_handle(handle);
163         /* XXX: "Geometry Eval" might not always be true, but this only gets called
164          * from modifier building now.
165          */
166         deg_handle->builder->add_node_handle_relation(comp_key,
167                                                       deg_handle,
168                                                       description);
169 }
170
171 struct Depsgraph *DEG_get_graph_from_handle(struct DepsNodeHandle *handle)
172 {
173         DEG::DepsNodeHandle *deg_handle = get_handle(handle);
174         DEG::DepsgraphRelationBuilder *relation_builder = deg_handle->builder;
175         return reinterpret_cast<Depsgraph *>(relation_builder->getGraph());
176 }
177
178 void DEG_add_special_eval_flag(Depsgraph *graph, ID *id, short flag)
179 {
180         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
181         if (graph == NULL) {
182                 BLI_assert(!"Graph should always be valid");
183                 return;
184         }
185         DEG::IDDepsNode *id_node = deg_graph->find_id_node(id);
186         if (id_node == NULL) {
187                 BLI_assert(!"ID should always be valid");
188                 return;
189         }
190         id_node->eval_flags |= flag;
191 }
192
193 /* ******************** */
194 /* Graph Building API's */
195
196 /* Build depsgraph for the given scene layer, and dump results in given
197  * graph container.
198  */
199 void DEG_graph_build_from_view_layer(Depsgraph *graph,
200                                       Main *bmain,
201                                       Scene *scene,
202                                       ViewLayer *view_layer)
203 {
204 #ifdef DEBUG_TIME
205         TIMEIT_START(DEG_graph_build_from_view_layer);
206 #endif
207
208         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
209
210         /* TODO(sergey): This is a bit tricky, but ensures that all the data
211          * is evaluated properly when depsgraph is becoming "visible".
212          *
213          * This now could happen for both visible scene is changed and extra
214          * dependency graph was created for render engine.
215          */
216         const bool need_on_visible_update = (deg_graph->scene == NULL);
217
218         /* 1) Generate all the nodes in the graph first */
219         DEG::DepsgraphNodeBuilder node_builder(bmain, deg_graph);
220         node_builder.begin_build();
221         node_builder.build_view_layer(scene,
222                                        view_layer,
223                                        DEG::DEG_ID_LINKED_DIRECTLY);
224
225         /* 2) Hook up relationships between operations - to determine evaluation
226          *    order.
227          */
228         DEG::DepsgraphRelationBuilder relation_builder(bmain, deg_graph);
229         relation_builder.begin_build();
230         relation_builder.build_view_layer(scene, view_layer);
231         if (DEG_depsgraph_use_copy_on_write()) {
232                 relation_builder.build_copy_on_write_relations();
233         }
234
235         /* Detect and solve cycles. */
236         DEG::deg_graph_detect_cycles(deg_graph);
237
238         /* 3) Simplify the graph by removing redundant relations (to optimize
239          *    traversal later). */
240         /* TODO: it would be useful to have an option to disable this in cases where
241          *       it is causing trouble.
242          */
243         if (G.debug_value == 799) {
244                 DEG::deg_graph_transitive_reduction(deg_graph);
245         }
246
247         /* 4) Flush visibility layer and re-schedule nodes for update. */
248         DEG::deg_graph_build_finalize(bmain, deg_graph);
249
250 #if 0
251         if (!DEG_debug_consistency_check(deg_graph)) {
252                 printf("Consistency validation failed, ABORTING!\n");
253                 abort();
254         }
255 #endif
256
257 #ifdef DEBUG_TIME
258         TIMEIT_END(DEG_graph_build_from_view_layer);
259 #endif
260
261         /* Relations are up to date. */
262         deg_graph->need_update = false;
263
264         if (need_on_visible_update) {
265                 DEG_graph_on_visible_update(bmain, graph);
266         }
267 }
268
269 /* Tag graph relations for update. */
270 void DEG_graph_tag_relations_update(Depsgraph *graph)
271 {
272         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
273         deg_graph->need_update = true;
274 }
275
276 /* Create or update relations in the specified graph. */
277 void DEG_graph_relations_update(Depsgraph *graph,
278                                 Main *bmain,
279                                 Scene *scene,
280                                 ViewLayer *view_layer)
281 {
282         DEG::Depsgraph *deg_graph = (DEG::Depsgraph *)graph;
283         if (!deg_graph->need_update) {
284                 /* Graph is up to date, nothing to do. */
285                 return;
286         }
287         DEG_graph_build_from_view_layer(graph, bmain, scene, view_layer);
288 }
289
290 /* Tag all relations for update. */
291 void DEG_relations_tag_update(Main *bmain)
292 {
293         DEG_DEBUG_PRINTF("%s: Tagging relations for update.\n", __func__);
294         LINKLIST_FOREACH(Scene *, scene, &bmain->scene) {
295                 LINKLIST_FOREACH(ViewLayer *, view_layer, &scene->view_layers) {
296                         Depsgraph *depsgraph =
297                                 (Depsgraph *)BKE_scene_get_depsgraph(scene,
298                                                                      view_layer,
299                                                                      false);
300                         if (depsgraph != NULL) {
301                                 DEG_graph_tag_relations_update(depsgraph);
302                         }
303                 }
304         }
305 }
306
307 void DEG_add_collision_relations(DepsNodeHandle *handle,
308                                  Scene *scene,
309                                  Object *ob,
310                                  Group *group,
311                                  unsigned int modifier_type,
312                                  DEG_CollobjFilterFunction fn,
313                                  bool dupli,
314                                  const char *name)
315 {
316         unsigned int numcollobj;
317         Object **collobjs = get_collisionobjects_ext(scene, ob, group, &numcollobj, modifier_type, dupli);
318
319         for (unsigned int i = 0; i < numcollobj; i++) {
320                 Object *ob1 = collobjs[i];
321
322                 if (!fn || fn(ob1, modifiers_findByType(ob1, (ModifierType)modifier_type))) {
323                         DEG_add_object_relation(handle, ob1, DEG_OB_COMP_TRANSFORM, name);
324                         DEG_add_object_relation(handle, ob1, DEG_OB_COMP_GEOMETRY, name);
325                 }
326         }
327
328         if (collobjs)
329                 MEM_freeN(collobjs);
330 }
331
332 void DEG_add_forcefield_relations(DepsNodeHandle *handle,
333                                   Scene *scene,
334                                   Object *ob,
335                                   EffectorWeights *effector_weights,
336                                   bool add_absorption,
337                                   int skip_forcefield,
338                                   const char *name)
339 {
340         ListBase *effectors = pdInitEffectors(NULL, scene, ob, NULL, effector_weights, false);
341
342         if (effectors) {
343                 for (EffectorCache *eff = (EffectorCache*)effectors->first; eff; eff = eff->next) {
344                         if (eff->ob != ob && eff->pd->forcefield != skip_forcefield) {
345                                 DEG_add_object_relation(handle, eff->ob, DEG_OB_COMP_TRANSFORM, name);
346
347                                 if (eff->psys) {
348                                         DEG_add_object_relation(handle, eff->ob, DEG_OB_COMP_EVAL_PARTICLES, name);
349
350                                         /* TODO: remove this when/if EVAL_PARTICLES is sufficient
351                                          * for up to date particles.
352                                          */
353                                         DEG_add_object_relation(handle, eff->ob, DEG_OB_COMP_GEOMETRY, name);
354                                 }
355
356                                 if (eff->pd->forcefield == PFIELD_SMOKEFLOW && eff->pd->f_source) {
357                                         DEG_add_object_relation(handle,
358                                                                 eff->pd->f_source,
359                                                                 DEG_OB_COMP_TRANSFORM,
360                                                                 "Smoke Force Domain");
361                                         DEG_add_object_relation(handle,
362                                                                 eff->pd->f_source,
363                                                                 DEG_OB_COMP_GEOMETRY,
364                                                                 "Smoke Force Domain");
365                                 }
366
367                                 if (add_absorption && (eff->pd->flag & PFIELD_VISIBILITY)) {
368                                         DEG_add_collision_relations(handle,
369                                                                     scene,
370                                                                     ob,
371                                                                     NULL,
372                                                                     eModifierType_Collision,
373                                                                     NULL,
374                                                                     true,
375                                                                     "Force Absorption");
376                                 }
377                         }
378                 }
379         }
380
381         pdEndEffectors(&effectors);
382 }