Cleanup: remove redundant doxygen \file argument
[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_special_eval_flag(struct DepsNodeHandle *node_handle,
194                                ID *id,
195                                uint32_t flag)
196 {
197         DEG::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
198         deg_node_handle->builder->add_special_eval_flag(id, flag);
199 }
200
201 void DEG_add_customdata_mask(struct DepsNodeHandle *node_handle,
202                              struct Object *object,
203                              uint64_t mask)
204 {
205         DEG::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
206         deg_node_handle->builder->add_customdata_mask(object, mask);
207 }
208
209 struct ID *DEG_get_id_from_handle(struct DepsNodeHandle *node_handle)
210 {
211         DEG::DepsNodeHandle *deg_handle = get_node_handle(node_handle);
212         return deg_handle->node->owner->owner->id_orig;
213 }
214
215 struct Depsgraph *DEG_get_graph_from_handle(struct DepsNodeHandle *node_handle)
216 {
217         DEG::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
218         DEG::DepsgraphRelationBuilder *relation_builder = deg_node_handle->builder;
219         return reinterpret_cast<Depsgraph *>(relation_builder->getGraph());
220 }
221
222 /* ******************** */
223 /* Graph Building API's */
224
225 /* Build depsgraph for the given scene layer, and dump results in given
226  * graph container.
227  */
228 void DEG_graph_build_from_view_layer(Depsgraph *graph,
229                                       Main *bmain,
230                                       Scene *scene,
231                                       ViewLayer *view_layer)
232 {
233         double start_time = 0.0;
234         if (G.debug & G_DEBUG_DEPSGRAPH_BUILD) {
235                 start_time = PIL_check_seconds_timer();
236         }
237         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
238         /* Perform sanity checks. */
239         BLI_assert(BLI_findindex(&scene->view_layers, view_layer) != -1);
240         BLI_assert(deg_graph->scene == scene);
241         BLI_assert(deg_graph->view_layer == view_layer);
242         /* Generate all the nodes in the graph first */
243         DEG::DepsgraphNodeBuilder node_builder(bmain, deg_graph);
244         node_builder.begin_build();
245         node_builder.build_view_layer(scene,
246                                        view_layer,
247                                        DEG::DEG_ID_LINKED_DIRECTLY);
248         node_builder.end_build();
249         /* Hook up relationships between operations - to determine evaluation
250          * order. */
251         DEG::DepsgraphRelationBuilder relation_builder(bmain, deg_graph);
252         relation_builder.begin_build();
253         relation_builder.build_view_layer(scene, view_layer);
254         relation_builder.build_copy_on_write_relations();
255         /* Detect and solve cycles. */
256         DEG::deg_graph_detect_cycles(deg_graph);
257         /* Simplify the graph by removing redundant relations (to optimize
258          * traversal later). */
259         /* TODO: it would be useful to have an option to disable this in cases where
260          *       it is causing trouble. */
261         if (G.debug_value == 799) {
262                 DEG::deg_graph_transitive_reduction(deg_graph);
263         }
264         /* Store pointers to commonly used valuated datablocks. */
265         deg_graph->scene_cow = (Scene *)deg_graph->get_cow_id(&deg_graph->scene->id);
266         /* Flush visibility layer and re-schedule nodes for update. */
267         DEG::deg_graph_build_finalize(bmain, deg_graph);
268         DEG_graph_on_visible_update(bmain, graph);
269 #if 0
270         if (!DEG_debug_consistency_check(deg_graph)) {
271                 printf("Consistency validation failed, ABORTING!\n");
272                 abort();
273         }
274 #endif
275         /* Relations are up to date. */
276         deg_graph->need_update = false;
277         /* Finish statistics. */
278         if (G.debug & G_DEBUG_DEPSGRAPH_BUILD) {
279                 printf("Depsgraph built in %f seconds.\n",
280                        PIL_check_seconds_timer() - start_time);
281         }
282 }
283
284 /* Tag graph relations for update. */
285 void DEG_graph_tag_relations_update(Depsgraph *graph)
286 {
287         DEG_DEBUG_PRINTF(graph, TAG, "%s: Tagging relations for update.\n", __func__);
288         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
289         deg_graph->need_update = true;
290         /* NOTE: When relations are updated, it's quite possible that
291          * we've got new bases in the scene. This means, we need to
292          * re-create flat array of bases in view layer.
293          *
294          * TODO(sergey): Try to make it so we don't flush updates
295          * to the whole depsgraph. */
296         DEG::IDNode *id_node = deg_graph->find_id_node(&deg_graph->scene->id);
297         if (id_node != NULL) {
298                 id_node->tag_update(deg_graph, DEG::DEG_UPDATE_SOURCE_RELATIONS);
299         }
300 }
301
302 /* Create or update relations in the specified graph. */
303 void DEG_graph_relations_update(Depsgraph *graph,
304                                 Main *bmain,
305                                 Scene *scene,
306                                 ViewLayer *view_layer)
307 {
308         DEG::Depsgraph *deg_graph = (DEG::Depsgraph *)graph;
309         if (!deg_graph->need_update) {
310                 /* Graph is up to date, nothing to do. */
311                 return;
312         }
313         DEG_graph_build_from_view_layer(graph, bmain, scene, view_layer);
314 }
315
316 /* Tag all relations for update. */
317 void DEG_relations_tag_update(Main *bmain)
318 {
319         DEG_GLOBAL_DEBUG_PRINTF(TAG, "%s: Tagging relations for update.\n", __func__);
320         LISTBASE_FOREACH (Scene *, scene, &bmain->scene) {
321                 LISTBASE_FOREACH (ViewLayer *, view_layer, &scene->view_layers) {
322                         Depsgraph *depsgraph =
323                                 (Depsgraph *)BKE_scene_get_depsgraph(scene,
324                                                                      view_layer,
325                                                                      false);
326                         if (depsgraph != NULL) {
327                                 DEG_graph_tag_relations_update(depsgraph);
328                         }
329                 }
330         }
331 }