Depsgraph: Add utility function for transform dependency
[blender.git] / source / blender / depsgraph / intern / depsgraph_build.cc
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2013 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file \ingroup depsgraph
21  *
22  * Methods for constructing depsgraph.
23  */
24
25 #include "MEM_guardedalloc.h"
26
27 #include "BLI_utildefines.h"
28 #include "BLI_ghash.h"
29 #include "BLI_listbase.h"
30
31 #include "PIL_time.h"
32 #include "PIL_time_utildefines.h"
33
34 extern "C" {
35 #include "DNA_cachefile_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_scene_types.h"
38
39 #include "BKE_main.h"
40 #include "BKE_scene.h"
41 } /* extern "C" */
42
43 #include "DEG_depsgraph.h"
44 #include "DEG_depsgraph_debug.h"
45 #include "DEG_depsgraph_build.h"
46
47 #include "builder/deg_builder.h"
48 #include "builder/deg_builder_cycle.h"
49 #include "builder/deg_builder_nodes.h"
50 #include "builder/deg_builder_relations.h"
51 #include "builder/deg_builder_transitive.h"
52
53 #include "intern/debug/deg_debug.h"
54
55 #include "intern/node/deg_node.h"
56 #include "intern/node/deg_node_component.h"
57 #include "intern/node/deg_node_id.h"
58 #include "intern/node/deg_node_operation.h"
59
60 #include "intern/depsgraph_type.h"
61
62 /* ****************** */
63 /* External Build API */
64
65 static DEG::NodeType deg_build_scene_component_type(
66         eDepsSceneComponentType component)
67 {
68         switch (component) {
69                 case DEG_SCENE_COMP_PARAMETERS:     return DEG::NodeType::PARAMETERS;
70                 case DEG_SCENE_COMP_ANIMATION:      return DEG::NodeType::ANIMATION;
71                 case DEG_SCENE_COMP_SEQUENCER:      return DEG::NodeType::SEQUENCER;
72         }
73         return DEG::NodeType::UNDEFINED;
74 }
75
76 static DEG::NodeType deg_build_object_component_type(
77         eDepsObjectComponentType component)
78 {
79         switch (component) {
80                 case DEG_OB_COMP_PARAMETERS:        return DEG::NodeType::PARAMETERS;
81                 case DEG_OB_COMP_PROXY:             return DEG::NodeType::PROXY;
82                 case DEG_OB_COMP_ANIMATION:         return DEG::NodeType::ANIMATION;
83                 case DEG_OB_COMP_TRANSFORM:         return DEG::NodeType::TRANSFORM;
84                 case DEG_OB_COMP_GEOMETRY:          return DEG::NodeType::GEOMETRY;
85                 case DEG_OB_COMP_EVAL_POSE:         return DEG::NodeType::EVAL_POSE;
86                 case DEG_OB_COMP_BONE:              return DEG::NodeType::BONE;
87                 case DEG_OB_COMP_SHADING:           return DEG::NodeType::SHADING;
88                 case DEG_OB_COMP_CACHE:             return DEG::NodeType::CACHE;
89         }
90         return DEG::NodeType::UNDEFINED;
91 }
92
93 static DEG::DepsNodeHandle *get_node_handle(DepsNodeHandle *node_handle)
94 {
95         return reinterpret_cast<DEG::DepsNodeHandle *>(node_handle);
96 }
97
98 void DEG_add_scene_relation(DepsNodeHandle *node_handle,
99                             Scene *scene,
100                             eDepsSceneComponentType component,
101                             const char *description)
102 {
103         DEG::NodeType type = deg_build_scene_component_type(component);
104         DEG::ComponentKey comp_key(&scene->id, type);
105         DEG::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
106         deg_node_handle->builder->add_node_handle_relation(comp_key,
107                                                            deg_node_handle,
108                                                            description);
109 }
110
111 void DEG_add_object_relation(DepsNodeHandle *node_handle,
112                              Object *object,
113                              eDepsObjectComponentType component,
114                              const char *description)
115 {
116         DEG::NodeType type = deg_build_object_component_type(component);
117         DEG::ComponentKey comp_key(&object->id, type);
118         DEG::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
119         deg_node_handle->builder->add_node_handle_relation(comp_key,
120                                                            deg_node_handle,
121                                                            description);
122 }
123
124 void DEG_add_object_cache_relation(DepsNodeHandle *node_handle,
125                                    CacheFile *cache_file,
126                                    eDepsObjectComponentType component,
127                                    const char *description)
128 {
129         DEG::NodeType type = deg_build_object_component_type(component);
130         DEG::ComponentKey comp_key(&cache_file->id, type);
131         DEG::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
132         deg_node_handle->builder->add_node_handle_relation(comp_key,
133                                                            deg_node_handle,
134                                                            description);
135 }
136
137 void DEG_add_bone_relation(DepsNodeHandle *node_handle,
138                            Object *object,
139                            const char *bone_name,
140                            eDepsObjectComponentType component,
141                            const char *description)
142 {
143         DEG::NodeType type = deg_build_object_component_type(component);
144         DEG::ComponentKey comp_key(&object->id, type, bone_name);
145         DEG::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
146         deg_node_handle->builder->add_node_handle_relation(comp_key,
147                                                            deg_node_handle,
148                                                            description);
149 }
150
151 void DEG_add_object_pointcache_relation(struct DepsNodeHandle *node_handle,
152                                         struct Object *object,
153                                         eDepsObjectComponentType component,
154                                         const char *description)
155 {
156         DEG::NodeType type = deg_build_object_component_type(component);
157         DEG::ComponentKey comp_key(&object->id, type);
158         DEG::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
159         DEG::DepsgraphRelationBuilder *relation_builder = deg_node_handle->builder;
160         /* Add relation from source to the node handle. */
161         relation_builder->add_node_handle_relation(
162                 comp_key, deg_node_handle, description);
163         /* Node deduct point cache component and connect source to it. */
164         ID *id = DEG_get_id_from_handle(node_handle);
165         DEG::ComponentKey point_cache_key(id, DEG::NodeType::POINT_CACHE);
166         DEG::Relation *rel = relation_builder->add_relation(
167                 comp_key, point_cache_key, "Point Cache");
168         if (rel != NULL) {
169                 rel->flag |= DEG::RELATION_FLAG_FLUSH_USER_EDIT_ONLY;
170         }
171         else {
172                 fprintf(stderr,
173                         "Error in point cache relation from %s to ^%s.\n",
174                         object->id.name,
175                         id->name);
176         }
177 }
178
179 void DEG_add_generic_id_relation(struct DepsNodeHandle *node_handle,
180                                  struct ID *id,
181                                  const char *description)
182 {
183         DEG::OperationKey operation_key(
184                 id,
185                 DEG::NodeType::GENERIC_DATABLOCK,
186                 DEG::OperationCode::GENERIC_DATABLOCK_UPDATE);
187         DEG::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
188         deg_node_handle->builder->add_node_handle_relation(operation_key,
189                                                            deg_node_handle,
190                                                            description);
191 }
192
193 void DEG_add_modifier_to_transform_relation(
194         struct DepsNodeHandle *node_handle,
195         const char *description)
196 {
197         DEG::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
198         deg_node_handle->builder->add_modifier_to_transform_relation(
199                 deg_node_handle,
200                 description);
201 }
202
203 void DEG_add_special_eval_flag(struct DepsNodeHandle *node_handle,
204                                ID *id,
205                                uint32_t flag)
206 {
207         DEG::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
208         deg_node_handle->builder->add_special_eval_flag(id, flag);
209 }
210
211 void DEG_add_customdata_mask(struct DepsNodeHandle *node_handle,
212                              struct Object *object,
213                              uint64_t mask)
214 {
215         DEG::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
216         deg_node_handle->builder->add_customdata_mask(object, mask);
217 }
218
219 struct ID *DEG_get_id_from_handle(struct DepsNodeHandle *node_handle)
220 {
221         DEG::DepsNodeHandle *deg_handle = get_node_handle(node_handle);
222         return deg_handle->node->owner->owner->id_orig;
223 }
224
225 struct Depsgraph *DEG_get_graph_from_handle(struct DepsNodeHandle *node_handle)
226 {
227         DEG::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
228         DEG::DepsgraphRelationBuilder *relation_builder = deg_node_handle->builder;
229         return reinterpret_cast<Depsgraph *>(relation_builder->getGraph());
230 }
231
232 /* ******************** */
233 /* Graph Building API's */
234
235 /* Build depsgraph for the given scene layer, and dump results in given
236  * graph container.
237  */
238 void DEG_graph_build_from_view_layer(Depsgraph *graph,
239                                       Main *bmain,
240                                       Scene *scene,
241                                       ViewLayer *view_layer)
242 {
243         double start_time = 0.0;
244         if (G.debug & G_DEBUG_DEPSGRAPH_BUILD) {
245                 start_time = PIL_check_seconds_timer();
246         }
247         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
248         /* Perform sanity checks. */
249         BLI_assert(BLI_findindex(&scene->view_layers, view_layer) != -1);
250         BLI_assert(deg_graph->scene == scene);
251         BLI_assert(deg_graph->view_layer == view_layer);
252         /* Generate all the nodes in the graph first */
253         DEG::DepsgraphNodeBuilder node_builder(bmain, deg_graph);
254         node_builder.begin_build();
255         node_builder.build_view_layer(scene,
256                                        view_layer,
257                                        DEG::DEG_ID_LINKED_DIRECTLY);
258         node_builder.end_build();
259         /* Hook up relationships between operations - to determine evaluation
260          * order. */
261         DEG::DepsgraphRelationBuilder relation_builder(bmain, deg_graph);
262         relation_builder.begin_build();
263         relation_builder.build_view_layer(scene, view_layer);
264         relation_builder.build_copy_on_write_relations();
265         /* Detect and solve cycles. */
266         DEG::deg_graph_detect_cycles(deg_graph);
267         /* Simplify the graph by removing redundant relations (to optimize
268          * traversal later). */
269         /* TODO: it would be useful to have an option to disable this in cases where
270          *       it is causing trouble. */
271         if (G.debug_value == 799) {
272                 DEG::deg_graph_transitive_reduction(deg_graph);
273         }
274         /* Store pointers to commonly used valuated datablocks. */
275         deg_graph->scene_cow = (Scene *)deg_graph->get_cow_id(&deg_graph->scene->id);
276         /* Flush visibility layer and re-schedule nodes for update. */
277         DEG::deg_graph_build_finalize(bmain, deg_graph);
278         DEG_graph_on_visible_update(bmain, graph);
279 #if 0
280         if (!DEG_debug_consistency_check(deg_graph)) {
281                 printf("Consistency validation failed, ABORTING!\n");
282                 abort();
283         }
284 #endif
285         /* Relations are up to date. */
286         deg_graph->need_update = false;
287         /* Finish statistics. */
288         if (G.debug & G_DEBUG_DEPSGRAPH_BUILD) {
289                 printf("Depsgraph built in %f seconds.\n",
290                        PIL_check_seconds_timer() - start_time);
291         }
292 }
293
294 /* Tag graph relations for update. */
295 void DEG_graph_tag_relations_update(Depsgraph *graph)
296 {
297         DEG_DEBUG_PRINTF(graph, TAG, "%s: Tagging relations for update.\n", __func__);
298         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
299         deg_graph->need_update = true;
300         /* NOTE: When relations are updated, it's quite possible that
301          * we've got new bases in the scene. This means, we need to
302          * re-create flat array of bases in view layer.
303          *
304          * TODO(sergey): Try to make it so we don't flush updates
305          * to the whole depsgraph. */
306         DEG::IDNode *id_node = deg_graph->find_id_node(&deg_graph->scene->id);
307         if (id_node != NULL) {
308                 id_node->tag_update(deg_graph, DEG::DEG_UPDATE_SOURCE_RELATIONS);
309         }
310 }
311
312 /* Create or update relations in the specified graph. */
313 void DEG_graph_relations_update(Depsgraph *graph,
314                                 Main *bmain,
315                                 Scene *scene,
316                                 ViewLayer *view_layer)
317 {
318         DEG::Depsgraph *deg_graph = (DEG::Depsgraph *)graph;
319         if (!deg_graph->need_update) {
320                 /* Graph is up to date, nothing to do. */
321                 return;
322         }
323         DEG_graph_build_from_view_layer(graph, bmain, scene, view_layer);
324 }
325
326 /* Tag all relations for update. */
327 void DEG_relations_tag_update(Main *bmain)
328 {
329         DEG_GLOBAL_DEBUG_PRINTF(TAG, "%s: Tagging relations for update.\n", __func__);
330         LISTBASE_FOREACH (Scene *, scene, &bmain->scene) {
331                 LISTBASE_FOREACH (ViewLayer *, view_layer, &scene->view_layers) {
332                         Depsgraph *depsgraph =
333                                 (Depsgraph *)BKE_scene_get_depsgraph(scene,
334                                                                      view_layer,
335                                                                      false);
336                         if (depsgraph != NULL) {
337                                 DEG_graph_tag_relations_update(depsgraph);
338                         }
339                 }
340         }
341 }