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
21 * \ingroup depsgraph
22 *
23 * Methods for constructing depsgraph.
24 */
25
26 #include "MEM_guardedalloc.h"
27
28 #include "BLI_listbase.h"
29 #include "BLI_utildefines.h"
30
31 #include "PIL_time.h"
32 #include "PIL_time_utildefines.h"
33
34 #include "DNA_cachefile_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_simulation_types.h"
38
39 #include "BKE_main.h"
40 #include "BKE_scene.h"
41
42 #include "DEG_depsgraph.h"
43 #include "DEG_depsgraph_build.h"
44 #include "DEG_depsgraph_debug.h"
45
46 #include "builder/deg_builder_relations.h"
47 #include "builder/pipeline_all_objects.h"
48 #include "builder/pipeline_compositor.h"
49 #include "builder/pipeline_from_ids.h"
50 #include "builder/pipeline_render.h"
51 #include "builder/pipeline_view_layer.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_registry.h"
61 #include "intern/depsgraph_relation.h"
62 #include "intern/depsgraph_type.h"
63
64 /* ****************** */
65 /* External Build API */
66
67 namespace deg = blender::deg;
68
deg_build_scene_component_type(eDepsSceneComponentType component)69 static deg::NodeType deg_build_scene_component_type(eDepsSceneComponentType component)
70 {
71 switch (component) {
72 case DEG_SCENE_COMP_PARAMETERS:
73 return deg::NodeType::PARAMETERS;
74 case DEG_SCENE_COMP_ANIMATION:
75 return deg::NodeType::ANIMATION;
76 case DEG_SCENE_COMP_SEQUENCER:
77 return deg::NodeType::SEQUENCER;
78 }
79 return deg::NodeType::UNDEFINED;
80 }
81
get_node_handle(DepsNodeHandle * node_handle)82 static deg::DepsNodeHandle *get_node_handle(DepsNodeHandle *node_handle)
83 {
84 return reinterpret_cast<deg::DepsNodeHandle *>(node_handle);
85 }
86
DEG_add_scene_relation(DepsNodeHandle * node_handle,Scene * scene,eDepsSceneComponentType component,const char * description)87 void DEG_add_scene_relation(DepsNodeHandle *node_handle,
88 Scene *scene,
89 eDepsSceneComponentType component,
90 const char *description)
91 {
92 deg::NodeType type = deg_build_scene_component_type(component);
93 deg::ComponentKey comp_key(&scene->id, type);
94 deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
95 deg_node_handle->builder->add_node_handle_relation(comp_key, deg_node_handle, description);
96 }
97
DEG_add_object_relation(DepsNodeHandle * node_handle,Object * object,eDepsObjectComponentType component,const char * description)98 void DEG_add_object_relation(DepsNodeHandle *node_handle,
99 Object *object,
100 eDepsObjectComponentType component,
101 const char *description)
102 {
103 deg::NodeType type = deg::nodeTypeFromObjectComponent(component);
104 deg::ComponentKey comp_key(&object->id, type);
105 deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
106 deg_node_handle->builder->add_node_handle_relation(comp_key, deg_node_handle, description);
107 }
108
DEG_add_simulation_relation(DepsNodeHandle * node_handle,Simulation * simulation,const char * description)109 void DEG_add_simulation_relation(DepsNodeHandle *node_handle,
110 Simulation *simulation,
111 const char *description)
112 {
113 deg::OperationKey operation_key(
114 &simulation->id, deg::NodeType::SIMULATION, deg::OperationCode::SIMULATION_EVAL);
115 deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
116 deg_node_handle->builder->add_node_handle_relation(operation_key, deg_node_handle, description);
117 }
118
DEG_add_object_cache_relation(DepsNodeHandle * node_handle,CacheFile * cache_file,eDepsObjectComponentType component,const char * description)119 void DEG_add_object_cache_relation(DepsNodeHandle *node_handle,
120 CacheFile *cache_file,
121 eDepsObjectComponentType component,
122 const char *description)
123 {
124 deg::NodeType type = deg::nodeTypeFromObjectComponent(component);
125 deg::ComponentKey comp_key(&cache_file->id, type);
126 deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
127 deg_node_handle->builder->add_node_handle_relation(comp_key, deg_node_handle, description);
128 }
129
DEG_add_bone_relation(DepsNodeHandle * node_handle,Object * object,const char * bone_name,eDepsObjectComponentType component,const char * description)130 void DEG_add_bone_relation(DepsNodeHandle *node_handle,
131 Object *object,
132 const char *bone_name,
133 eDepsObjectComponentType component,
134 const char *description)
135 {
136 deg::NodeType type = deg::nodeTypeFromObjectComponent(component);
137 deg::ComponentKey comp_key(&object->id, type, bone_name);
138 deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
139 deg_node_handle->builder->add_node_handle_relation(comp_key, deg_node_handle, description);
140 }
141
DEG_add_object_pointcache_relation(struct DepsNodeHandle * node_handle,struct Object * object,eDepsObjectComponentType component,const char * description)142 void DEG_add_object_pointcache_relation(struct DepsNodeHandle *node_handle,
143 struct Object *object,
144 eDepsObjectComponentType component,
145 const char *description)
146 {
147 deg::NodeType type = deg::nodeTypeFromObjectComponent(component);
148 deg::ComponentKey comp_key(&object->id, type);
149 deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
150 deg::DepsgraphRelationBuilder *relation_builder = deg_node_handle->builder;
151 /* Add relation from source to the node handle. */
152 relation_builder->add_node_handle_relation(comp_key, deg_node_handle, description);
153 /* Node deduct point cache component and connect source to it. */
154 ID *id = DEG_get_id_from_handle(node_handle);
155 deg::ComponentKey point_cache_key(id, deg::NodeType::POINT_CACHE);
156 deg::Relation *rel = relation_builder->add_relation(comp_key, point_cache_key, "Point Cache");
157 if (rel != nullptr) {
158 rel->flag |= deg::RELATION_FLAG_FLUSH_USER_EDIT_ONLY;
159 }
160 else {
161 fprintf(stderr, "Error in point cache relation from %s to ^%s.\n", object->id.name, id->name);
162 }
163 }
164
DEG_add_generic_id_relation(struct DepsNodeHandle * node_handle,struct ID * id,const char * description)165 void DEG_add_generic_id_relation(struct DepsNodeHandle *node_handle,
166 struct ID *id,
167 const char *description)
168 {
169 deg::OperationKey operation_key(
170 id, deg::NodeType::GENERIC_DATABLOCK, deg::OperationCode::GENERIC_DATABLOCK_UPDATE);
171 deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
172 deg_node_handle->builder->add_node_handle_relation(operation_key, deg_node_handle, description);
173 }
174
DEG_add_modifier_to_transform_relation(struct DepsNodeHandle * node_handle,const char * description)175 void DEG_add_modifier_to_transform_relation(struct DepsNodeHandle *node_handle,
176 const char *description)
177 {
178 deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
179 deg_node_handle->builder->add_modifier_to_transform_relation(deg_node_handle, description);
180 }
181
DEG_add_special_eval_flag(struct DepsNodeHandle * node_handle,ID * id,uint32_t flag)182 void DEG_add_special_eval_flag(struct DepsNodeHandle *node_handle, ID *id, uint32_t flag)
183 {
184 deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
185 deg_node_handle->builder->add_special_eval_flag(id, flag);
186 }
187
DEG_add_customdata_mask(struct DepsNodeHandle * node_handle,struct Object * object,const CustomData_MeshMasks * masks)188 void DEG_add_customdata_mask(struct DepsNodeHandle *node_handle,
189 struct Object *object,
190 const CustomData_MeshMasks *masks)
191 {
192 deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
193 deg_node_handle->builder->add_customdata_mask(object, deg::DEGCustomDataMeshMasks(masks));
194 }
195
DEG_get_id_from_handle(struct DepsNodeHandle * node_handle)196 struct ID *DEG_get_id_from_handle(struct DepsNodeHandle *node_handle)
197 {
198 deg::DepsNodeHandle *deg_handle = get_node_handle(node_handle);
199 return deg_handle->node->owner->owner->id_orig;
200 }
201
DEG_get_graph_from_handle(struct DepsNodeHandle * node_handle)202 struct Depsgraph *DEG_get_graph_from_handle(struct DepsNodeHandle *node_handle)
203 {
204 deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
205 deg::DepsgraphRelationBuilder *relation_builder = deg_node_handle->builder;
206 return reinterpret_cast<Depsgraph *>(relation_builder->getGraph());
207 }
208
209 /* ******************** */
210 /* Graph Building API's */
211
212 /* Build depsgraph for the given scene layer, and dump results in given graph container. */
DEG_graph_build_from_view_layer(Depsgraph * graph)213 void DEG_graph_build_from_view_layer(Depsgraph *graph)
214 {
215 deg::ViewLayerBuilderPipeline builder(graph);
216 builder.build();
217 }
218
DEG_graph_build_for_all_objects(struct Depsgraph * graph)219 void DEG_graph_build_for_all_objects(struct Depsgraph *graph)
220 {
221 deg::AllObjectsBuilderPipeline builder(graph);
222 builder.build();
223 }
224
DEG_graph_build_for_render_pipeline(Depsgraph * graph)225 void DEG_graph_build_for_render_pipeline(Depsgraph *graph)
226 {
227 deg::RenderBuilderPipeline builder(graph);
228 builder.build();
229 }
230
DEG_graph_build_for_compositor_preview(Depsgraph * graph,bNodeTree * nodetree)231 void DEG_graph_build_for_compositor_preview(Depsgraph *graph, bNodeTree *nodetree)
232 {
233 deg::CompositorBuilderPipeline builder(graph, nodetree);
234 builder.build();
235 }
236
DEG_graph_build_from_ids(Depsgraph * graph,ID ** ids,const int num_ids)237 void DEG_graph_build_from_ids(Depsgraph *graph, ID **ids, const int num_ids)
238 {
239 deg::FromIDsBuilderPipeline builder(graph, blender::Span(ids, num_ids));
240 builder.build();
241 }
242
243 /* Tag graph relations for update. */
DEG_graph_tag_relations_update(Depsgraph * graph)244 void DEG_graph_tag_relations_update(Depsgraph *graph)
245 {
246 DEG_DEBUG_PRINTF(graph, TAG, "%s: Tagging relations for update.\n", __func__);
247 deg::Depsgraph *deg_graph = reinterpret_cast<deg::Depsgraph *>(graph);
248 deg_graph->need_update = true;
249 /* NOTE: When relations are updated, it's quite possible that
250 * we've got new bases in the scene. This means, we need to
251 * re-create flat array of bases in view layer.
252 *
253 * TODO(sergey): Try to make it so we don't flush updates
254 * to the whole depsgraph. */
255 deg::IDNode *id_node = deg_graph->find_id_node(°_graph->scene->id);
256 if (id_node != nullptr) {
257 id_node->tag_update(deg_graph, deg::DEG_UPDATE_SOURCE_RELATIONS);
258 }
259 }
260
261 /* Create or update relations in the specified graph. */
DEG_graph_relations_update(Depsgraph * graph)262 void DEG_graph_relations_update(Depsgraph *graph)
263 {
264 deg::Depsgraph *deg_graph = (deg::Depsgraph *)graph;
265 if (!deg_graph->need_update) {
266 /* Graph is up to date, nothing to do. */
267 return;
268 }
269 DEG_graph_build_from_view_layer(graph);
270 }
271
272 /* Tag all relations for update. */
DEG_relations_tag_update(Main * bmain)273 void DEG_relations_tag_update(Main *bmain)
274 {
275 DEG_GLOBAL_DEBUG_PRINTF(TAG, "%s: Tagging relations for update.\n", __func__);
276 for (deg::Depsgraph *depsgraph : deg::get_all_registered_graphs(bmain)) {
277 DEG_graph_tag_relations_update(reinterpret_cast<Depsgraph *>(depsgraph));
278 }
279 }
280