Depsgrapgh: Use more distinctive opcode for texture and image evaluation
[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 #include "BLI_utildefines.h"
36 #include "BLI_ghash.h"
37 #include "BLI_listbase.h"
38
39 #include "PIL_time.h"
40 #include "PIL_time_utildefines.h"
41
42 extern "C" {
43 #include "DNA_cachefile_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_scene_types.h"
46
47 #include "BKE_main.h"
48 #include "BKE_scene.h"
49 } /* extern "C" */
50
51 #include "DEG_depsgraph.h"
52 #include "DEG_depsgraph_debug.h"
53 #include "DEG_depsgraph_build.h"
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_id.h"
64 #include "intern/nodes/deg_node_operation.h"
65
66 #include "intern/depsgraph_types.h"
67 #include "intern/depsgraph_intern.h"
68
69 #include "util/deg_util_foreach.h"
70
71 /* ****************** */
72 /* External Build API */
73
74 static DEG::eDepsNode_Type deg_build_scene_component_type(
75         eDepsSceneComponentType component)
76 {
77         switch (component) {
78                 case DEG_SCENE_COMP_PARAMETERS:     return DEG::DEG_NODE_TYPE_PARAMETERS;
79                 case DEG_SCENE_COMP_ANIMATION:      return DEG::DEG_NODE_TYPE_ANIMATION;
80                 case DEG_SCENE_COMP_SEQUENCER:      return DEG::DEG_NODE_TYPE_SEQUENCER;
81         }
82         return DEG::DEG_NODE_TYPE_UNDEFINED;
83 }
84
85 static DEG::eDepsNode_Type deg_build_object_component_type(
86         eDepsObjectComponentType component)
87 {
88         switch (component) {
89                 case DEG_OB_COMP_PARAMETERS:        return DEG::DEG_NODE_TYPE_PARAMETERS;
90                 case DEG_OB_COMP_PROXY:             return DEG::DEG_NODE_TYPE_PROXY;
91                 case DEG_OB_COMP_ANIMATION:         return DEG::DEG_NODE_TYPE_ANIMATION;
92                 case DEG_OB_COMP_TRANSFORM:         return DEG::DEG_NODE_TYPE_TRANSFORM;
93                 case DEG_OB_COMP_GEOMETRY:          return DEG::DEG_NODE_TYPE_GEOMETRY;
94                 case DEG_OB_COMP_EVAL_POSE:         return DEG::DEG_NODE_TYPE_EVAL_POSE;
95                 case DEG_OB_COMP_BONE:              return DEG::DEG_NODE_TYPE_BONE;
96                 case DEG_OB_COMP_SHADING:           return DEG::DEG_NODE_TYPE_SHADING;
97                 case DEG_OB_COMP_CACHE:             return DEG::DEG_NODE_TYPE_CACHE;
98         }
99         return DEG::DEG_NODE_TYPE_UNDEFINED;
100 }
101
102 static DEG::DepsNodeHandle *get_node_handle(DepsNodeHandle *node_handle)
103 {
104         return reinterpret_cast<DEG::DepsNodeHandle *>(node_handle);
105 }
106
107 void DEG_add_scene_relation(DepsNodeHandle *node_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_node_handle = get_node_handle(node_handle);
115         deg_node_handle->builder->add_node_handle_relation(comp_key,
116                                                            deg_node_handle,
117                                                            description);
118 }
119
120 void DEG_add_object_relation(DepsNodeHandle *node_handle,
121                              Object *object,
122                              eDepsObjectComponentType component,
123                              const char *description)
124 {
125         DEG::eDepsNode_Type type = deg_build_object_component_type(component);
126         DEG::ComponentKey comp_key(&object->id, type);
127         DEG::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
128         deg_node_handle->builder->add_node_handle_relation(comp_key,
129                                                            deg_node_handle,
130                                                            description);
131 }
132
133 void DEG_add_object_cache_relation(DepsNodeHandle *node_handle,
134                                    CacheFile *cache_file,
135                                    eDepsObjectComponentType component,
136                                    const char *description)
137 {
138         DEG::eDepsNode_Type type = deg_build_object_component_type(component);
139         DEG::ComponentKey comp_key(&cache_file->id, type);
140         DEG::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
141         deg_node_handle->builder->add_node_handle_relation(comp_key,
142                                                            deg_node_handle,
143                                                            description);
144 }
145
146 void DEG_add_bone_relation(DepsNodeHandle *node_handle,
147                            Object *object,
148                            const char *bone_name,
149                            eDepsObjectComponentType component,
150                            const char *description)
151 {
152         DEG::eDepsNode_Type type = deg_build_object_component_type(component);
153         DEG::ComponentKey comp_key(&object->id, type, bone_name);
154         DEG::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
155         deg_node_handle->builder->add_node_handle_relation(comp_key,
156                                                            deg_node_handle,
157                                                            description);
158 }
159
160 void DEG_add_object_pointcache_relation(struct DepsNodeHandle *node_handle,
161                                         struct Object *object,
162                                         eDepsObjectComponentType component,
163                                         const char *description)
164 {
165         DEG::eDepsNode_Type type = deg_build_object_component_type(component);
166         DEG::ComponentKey comp_key(&object->id, type);
167         DEG::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
168         DEG::DepsgraphRelationBuilder *relation_builder = deg_node_handle->builder;
169         /* Add relation from source to the node handle. */
170         relation_builder->add_node_handle_relation(
171                 comp_key, deg_node_handle, description);
172         /* Node deduct point cache component and connect source to it. */
173         ID *id = DEG_get_id_from_handle(node_handle);
174         DEG::ComponentKey point_cache_key(id, DEG::DEG_NODE_TYPE_POINT_CACHE);
175         DEG::DepsRelation *rel = relation_builder->add_relation(
176                 comp_key, point_cache_key, "Point Cache");
177         if (rel != NULL) {
178                 rel->flag |= DEG::DEPSREL_FLAG_FLUSH_USER_EDIT_ONLY;
179         }
180         else {
181                 fprintf(stderr,
182                         "Error in point cache relation from %s to ^%s.\n",
183                         object->id.name,
184                         id->name);
185         }
186 }
187
188 void DEG_add_special_eval_flag(struct DepsNodeHandle *node_handle,
189                                ID *id,
190                                uint32_t flag)
191 {
192         DEG::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
193         deg_node_handle->builder->add_special_eval_flag(id, flag);
194 }
195
196 void DEG_add_customdata_mask(struct DepsNodeHandle *node_handle,
197                              struct Object *object,
198                              uint64_t mask)
199 {
200         DEG::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
201         deg_node_handle->builder->add_customdata_mask(object, mask);
202 }
203
204 struct ID *DEG_get_id_from_handle(struct DepsNodeHandle *node_handle)
205 {
206         DEG::DepsNodeHandle *deg_handle = get_node_handle(node_handle);
207         return deg_handle->node->owner->owner->id_orig;
208 }
209
210 struct Depsgraph *DEG_get_graph_from_handle(struct DepsNodeHandle *node_handle)
211 {
212         DEG::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
213         DEG::DepsgraphRelationBuilder *relation_builder = deg_node_handle->builder;
214         return reinterpret_cast<Depsgraph *>(relation_builder->getGraph());
215 }
216
217 /* ******************** */
218 /* Graph Building API's */
219
220 /* Build depsgraph for the given scene layer, and dump results in given
221  * graph container.
222  */
223 void DEG_graph_build_from_view_layer(Depsgraph *graph,
224                                       Main *bmain,
225                                       Scene *scene,
226                                       ViewLayer *view_layer)
227 {
228         double start_time = 0.0;
229         if (G.debug & G_DEBUG_DEPSGRAPH_BUILD) {
230                 start_time = PIL_check_seconds_timer();
231         }
232         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
233         /* Perform sanity checks. */
234         BLI_assert(BLI_findindex(&scene->view_layers, view_layer) != -1);
235         BLI_assert(deg_graph->scene == scene);
236         BLI_assert(deg_graph->view_layer == view_layer);
237         /* Generate all the nodes in the graph first */
238         DEG::DepsgraphNodeBuilder node_builder(bmain, deg_graph);
239         node_builder.begin_build();
240         node_builder.build_view_layer(scene,
241                                        view_layer,
242                                        DEG::DEG_ID_LINKED_DIRECTLY);
243         node_builder.end_build();
244         /* Hook up relationships between operations - to determine evaluation
245          * order.
246          */
247         DEG::DepsgraphRelationBuilder relation_builder(bmain, deg_graph);
248         relation_builder.begin_build();
249         relation_builder.build_view_layer(scene, view_layer);
250         relation_builder.build_copy_on_write_relations();
251         /* Detect and solve cycles. */
252         DEG::deg_graph_detect_cycles(deg_graph);
253         /* Simplify the graph by removing redundant relations (to optimize
254          * traversal later). */
255         /* TODO: it would be useful to have an option to disable this in cases where
256          *       it is causing trouble.
257          */
258         if (G.debug_value == 799) {
259                 DEG::deg_graph_transitive_reduction(deg_graph);
260         }
261         /* Store pointers to commonly used valuated datablocks. */
262         deg_graph->scene_cow = (Scene *)deg_graph->get_cow_id(&deg_graph->scene->id);
263         /* Flush visibility layer and re-schedule nodes for update. */
264         DEG::deg_graph_build_finalize(bmain, deg_graph);
265         DEG_graph_on_visible_update(bmain, graph);
266 #if 0
267         if (!DEG_debug_consistency_check(deg_graph)) {
268                 printf("Consistency validation failed, ABORTING!\n");
269                 abort();
270         }
271 #endif
272         /* Relations are up to date. */
273         deg_graph->need_update = false;
274         /* Finish statistics. */
275         if (G.debug & G_DEBUG_DEPSGRAPH_BUILD) {
276                 printf("Depsgraph built in %f seconds.\n",
277                        PIL_check_seconds_timer() - start_time);
278         }
279 }
280
281 /* Tag graph relations for update. */
282 void DEG_graph_tag_relations_update(Depsgraph *graph)
283 {
284         DEG_DEBUG_PRINTF(graph, TAG, "%s: Tagging relations for update.\n", __func__);
285         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(graph);
286         deg_graph->need_update = true;
287         /* NOTE: When relations are updated, it's quite possible that
288          * we've got new bases in the scene. This means, we need to
289          * re-create flat array of bases in view layer.
290          *
291          * TODO(sergey): Try to make it so we don't flush updates
292          * to the whole depsgraph.
293          */
294         DEG::IDDepsNode *id_node = deg_graph->find_id_node(&deg_graph->scene->id);
295         if (id_node != NULL) {
296                 id_node->tag_update(deg_graph, DEG::DEG_UPDATE_SOURCE_RELATIONS);
297         }
298 }
299
300 /* Create or update relations in the specified graph. */
301 void DEG_graph_relations_update(Depsgraph *graph,
302                                 Main *bmain,
303                                 Scene *scene,
304                                 ViewLayer *view_layer)
305 {
306         DEG::Depsgraph *deg_graph = (DEG::Depsgraph *)graph;
307         if (!deg_graph->need_update) {
308                 /* Graph is up to date, nothing to do. */
309                 return;
310         }
311         DEG_graph_build_from_view_layer(graph, bmain, scene, view_layer);
312 }
313
314 /* Tag all relations for update. */
315 void DEG_relations_tag_update(Main *bmain)
316 {
317         DEG_GLOBAL_DEBUG_PRINTF(TAG, "%s: Tagging relations for update.\n", __func__);
318         LISTBASE_FOREACH (Scene *, scene, &bmain->scene) {
319                 LISTBASE_FOREACH (ViewLayer *, view_layer, &scene->view_layers) {
320                         Depsgraph *depsgraph =
321                                 (Depsgraph *)BKE_scene_get_depsgraph(scene,
322                                                                      view_layer,
323                                                                      false);
324                         if (depsgraph != NULL) {
325                                 DEG_graph_tag_relations_update(depsgraph);
326                         }
327                 }
328         }
329 }