Blender  V2.93
depsgraph_build.cc
Go to the documentation of this file.
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 
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_collection_types.h"
36 #include "DNA_node_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_simulation_types.h"
40 
41 #include "BKE_collection.h"
42 #include "BKE_main.h"
43 #include "BKE_scene.h"
44 
45 #include "DEG_depsgraph.h"
46 #include "DEG_depsgraph_build.h"
47 #include "DEG_depsgraph_debug.h"
48 
55 
56 #include "intern/debug/deg_debug.h"
57 
58 #include "intern/node/deg_node.h"
62 
65 #include "intern/depsgraph_tag.h"
66 #include "intern/depsgraph_type.h"
67 
68 /* ****************** */
69 /* External Build API */
70 
71 namespace deg = blender::deg;
72 
74 {
75  switch (component) {
77  return deg::NodeType::PARAMETERS;
79  return deg::NodeType::ANIMATION;
81  return deg::NodeType::SEQUENCER;
82  }
84 }
85 
86 static deg::DepsNodeHandle *get_node_handle(DepsNodeHandle *node_handle)
87 {
88  return reinterpret_cast<deg::DepsNodeHandle *>(node_handle);
89 }
90 
91 void DEG_add_scene_relation(DepsNodeHandle *node_handle,
92  Scene *scene,
94  const char *description)
95 {
97  deg::ComponentKey comp_key(&scene->id, type);
98  deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
99  deg_node_handle->builder->add_node_handle_relation(comp_key, deg_node_handle, description);
100 }
101 
102 void DEG_add_object_relation(DepsNodeHandle *node_handle,
103  Object *object,
105  const char *description)
106 {
108  deg::ComponentKey comp_key(&object->id, type);
109  deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
110  deg_node_handle->builder->add_node_handle_relation(comp_key, deg_node_handle, description);
111 }
112 
114 {
116 }
117 
118 void DEG_add_collection_geometry_relation(DepsNodeHandle *node_handle,
119  Collection *collection,
120  const char *description)
121 {
122  deg::OperationKey operation_key{
123  &collection->id, deg::NodeType::GEOMETRY, deg::OperationCode::GEOMETRY_EVAL_DONE};
124  deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
125  deg_node_handle->builder->add_node_handle_relation(operation_key, deg_node_handle, description);
126 }
127 
128 void DEG_add_collection_geometry_customdata_mask(DepsNodeHandle *node_handle,
129  Collection *collection,
130  const CustomData_MeshMasks *masks)
131 {
133  DEG_add_customdata_mask(node_handle, ob, masks);
134  if (ob->type == OB_EMPTY && ob->instance_collection != nullptr) {
135  DEG_add_collection_geometry_customdata_mask(node_handle, ob->instance_collection, masks);
136  }
137  }
139 }
140 
141 void DEG_add_simulation_relation(DepsNodeHandle *node_handle,
143  const char *description)
144 {
145  deg::OperationKey operation_key(
146  &simulation->id, deg::NodeType::SIMULATION, deg::OperationCode::SIMULATION_EVAL);
147  deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
148  deg_node_handle->builder->add_node_handle_relation(operation_key, deg_node_handle, description);
149 }
150 
151 void DEG_add_node_tree_relation(DepsNodeHandle *node_handle,
153  const char *description)
154 {
155  /* Using shading key, because that's the one that exists right now. Should use something else in
156  * the future. */
157  deg::ComponentKey shading_key(&node_tree->id, deg::NodeType::SHADING);
158  deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
159  deg_node_handle->builder->add_node_handle_relation(shading_key, deg_node_handle, description);
160 }
161 
162 void DEG_add_object_cache_relation(DepsNodeHandle *node_handle,
163  CacheFile *cache_file,
165  const char *description)
166 {
168  deg::ComponentKey comp_key(&cache_file->id, type);
169  deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
170  deg_node_handle->builder->add_node_handle_relation(comp_key, deg_node_handle, description);
171 }
172 
173 void DEG_add_bone_relation(DepsNodeHandle *node_handle,
174  Object *object,
175  const char *bone_name,
177  const char *description)
178 {
180  deg::ComponentKey comp_key(&object->id, type, bone_name);
181  deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
182  deg_node_handle->builder->add_node_handle_relation(comp_key, deg_node_handle, description);
183 }
184 
185 void DEG_add_object_pointcache_relation(struct DepsNodeHandle *node_handle,
186  struct Object *object,
188  const char *description)
189 {
191  deg::ComponentKey comp_key(&object->id, type);
192  deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
193  deg::DepsgraphRelationBuilder *relation_builder = deg_node_handle->builder;
194  /* Add relation from source to the node handle. */
195  relation_builder->add_node_handle_relation(comp_key, deg_node_handle, description);
196  /* Node deduct point cache component and connect source to it. */
197  ID *id = DEG_get_id_from_handle(node_handle);
198  deg::ComponentKey point_cache_key(id, deg::NodeType::POINT_CACHE);
199  deg::Relation *rel = relation_builder->add_relation(comp_key, point_cache_key, "Point Cache");
200  if (rel != nullptr) {
202  }
203  else {
204  fprintf(stderr, "Error in point cache relation from %s to ^%s.\n", object->id.name, id->name);
205  }
206 }
207 
208 void DEG_add_generic_id_relation(struct DepsNodeHandle *node_handle,
209  struct ID *id,
210  const char *description)
211 {
212  deg::OperationKey operation_key(
213  id, deg::NodeType::GENERIC_DATABLOCK, deg::OperationCode::GENERIC_DATABLOCK_UPDATE);
214  deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
215  deg_node_handle->builder->add_node_handle_relation(operation_key, deg_node_handle, description);
216 }
217 
218 void DEG_add_modifier_to_transform_relation(struct DepsNodeHandle *node_handle,
219  const char *description)
220 {
221  deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
222  deg_node_handle->builder->add_modifier_to_transform_relation(deg_node_handle, description);
223 }
224 
225 void DEG_add_special_eval_flag(struct DepsNodeHandle *node_handle, ID *id, uint32_t flag)
226 {
227  deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
228  deg_node_handle->builder->add_special_eval_flag(id, flag);
229 }
230 
231 void DEG_add_customdata_mask(struct DepsNodeHandle *node_handle,
232  struct Object *object,
233  const CustomData_MeshMasks *masks)
234 {
235  deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
236  deg_node_handle->builder->add_customdata_mask(object, deg::DEGCustomDataMeshMasks(masks));
237 }
238 
239 struct ID *DEG_get_id_from_handle(struct DepsNodeHandle *node_handle)
240 {
241  deg::DepsNodeHandle *deg_handle = get_node_handle(node_handle);
242  return deg_handle->node->owner->owner->id_orig;
243 }
244 
245 struct Depsgraph *DEG_get_graph_from_handle(struct DepsNodeHandle *node_handle)
246 {
247  deg::DepsNodeHandle *deg_node_handle = get_node_handle(node_handle);
248  deg::DepsgraphRelationBuilder *relation_builder = deg_node_handle->builder;
249  return reinterpret_cast<Depsgraph *>(relation_builder->getGraph());
250 }
251 
252 /* ******************** */
253 /* Graph Building API's */
254 
255 /* Build depsgraph for the given scene layer, and dump results in given graph container. */
257 {
259  builder.build();
260 }
261 
263 {
265  builder.build();
266 }
267 
269 {
271  builder.build();
272 }
273 
275 {
276  deg::CompositorBuilderPipeline builder(graph, nodetree);
277  builder.build();
278 }
279 
280 void DEG_graph_build_from_ids(Depsgraph *graph, ID **ids, const int num_ids)
281 {
282  deg::FromIDsBuilderPipeline builder(graph, blender::Span(ids, num_ids));
283  builder.build();
284 }
285 
286 /* Tag graph relations for update. */
288 {
289  DEG_DEBUG_PRINTF(graph, TAG, "%s: Tagging relations for update.\n", __func__);
290  deg::Depsgraph *deg_graph = reinterpret_cast<deg::Depsgraph *>(graph);
291  deg_graph->need_update = true;
292  /* NOTE: When relations are updated, it's quite possible that
293  * we've got new bases in the scene. This means, we need to
294  * re-create flat array of bases in view layer.
295  *
296  * TODO(sergey): Try to make it so we don't flush updates
297  * to the whole depsgraph. */
298  deg::IDNode *id_node = deg_graph->find_id_node(&deg_graph->scene->id);
299  if (id_node != nullptr) {
300  id_node->tag_update(deg_graph, deg::DEG_UPDATE_SOURCE_RELATIONS);
301  }
302 }
303 
304 /* Create or update relations in the specified graph. */
306 {
307  deg::Depsgraph *deg_graph = (deg::Depsgraph *)graph;
308  if (!deg_graph->need_update) {
309  /* Graph is up to date, nothing to do. */
310  return;
311  }
313 }
314 
315 /* Tag all relations for update. */
317 {
318  DEG_GLOBAL_DEBUG_PRINTF(TAG, "%s: Tagging relations for update.\n", __func__);
320  DEG_graph_tag_relations_update(reinterpret_cast<Depsgraph *>(depsgraph));
321  }
322 }
#define FOREACH_COLLECTION_OBJECT_RECURSIVE_END
#define FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(_collection, _object)
static uint8 component(Color32 c, uint i)
Definition: ColorBlock.cpp:126
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
eDepsSceneComponentType
@ DEG_SCENE_COMP_ANIMATION
@ DEG_SCENE_COMP_PARAMETERS
@ DEG_SCENE_COMP_SEQUENCER
eDepsObjectComponentType
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
@ OB_EMPTY
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
Read Guarded memory(de)allocation.
Platform independent time functions.
Utility defines for timing/benchmarks.
void add_customdata_mask(Object *object, const DEGCustomDataMeshMasks &customdata_masks)
Relation * add_node_handle_relation(const KeyType &key_from, const DepsNodeHandle *handle, const char *description, int flags=0)
void add_special_eval_flag(ID *id, uint32_t flag)
Relation * add_relation(const KeyFrom &key_from, const KeyTo &key_to, const char *description, int flags=0)
void add_modifier_to_transform_relation(const DepsNodeHandle *handle, const char *description)
Depsgraph * graph
#define DEG_GLOBAL_DEBUG_PRINTF(type,...)
Definition: deg_debug.h:76
#define DEG_DEBUG_PRINTF(depsgraph, type,...)
Definition: deg_debug.h:68
const IDNode * id_node
Scene scene
Simulation simulation
const Depsgraph * depsgraph
static deg::DepsNodeHandle * get_node_handle(DepsNodeHandle *node_handle)
void DEG_relations_tag_update(Main *bmain)
void DEG_graph_build_for_compositor_preview(Depsgraph *graph, bNodeTree *nodetree)
void DEG_add_collection_geometry_relation(DepsNodeHandle *node_handle, Collection *collection, const char *description)
void DEG_add_object_cache_relation(DepsNodeHandle *node_handle, CacheFile *cache_file, eDepsObjectComponentType component, const char *description)
void DEG_graph_tag_relations_update(Depsgraph *graph)
void DEG_add_modifier_to_transform_relation(struct DepsNodeHandle *node_handle, const char *description)
void DEG_add_generic_id_relation(struct DepsNodeHandle *node_handle, struct ID *id, const char *description)
void DEG_add_scene_relation(DepsNodeHandle *node_handle, Scene *scene, eDepsSceneComponentType component, const char *description)
void DEG_add_customdata_mask(struct DepsNodeHandle *node_handle, struct Object *object, const CustomData_MeshMasks *masks)
void DEG_add_special_eval_flag(struct DepsNodeHandle *node_handle, ID *id, uint32_t flag)
void DEG_add_node_tree_relation(DepsNodeHandle *node_handle, bNodeTree *node_tree, const char *description)
bool DEG_object_has_geometry_component(Object *object)
void DEG_graph_build_from_ids(Depsgraph *graph, ID **ids, const int num_ids)
void DEG_add_object_relation(DepsNodeHandle *node_handle, Object *object, eDepsObjectComponentType component, const char *description)
struct ID * DEG_get_id_from_handle(struct DepsNodeHandle *node_handle)
void DEG_add_bone_relation(DepsNodeHandle *node_handle, Object *object, const char *bone_name, eDepsObjectComponentType component, const char *description)
void DEG_graph_build_for_all_objects(struct Depsgraph *graph)
void DEG_graph_build_for_render_pipeline(Depsgraph *graph)
void DEG_add_object_pointcache_relation(struct DepsNodeHandle *node_handle, struct Object *object, eDepsObjectComponentType component, const char *description)
void DEG_graph_relations_update(Depsgraph *graph)
void DEG_add_collection_geometry_customdata_mask(DepsNodeHandle *node_handle, Collection *collection, const CustomData_MeshMasks *masks)
void DEG_graph_build_from_view_layer(Depsgraph *graph)
struct Depsgraph * DEG_get_graph_from_handle(struct DepsNodeHandle *node_handle)
static deg::NodeType deg_build_scene_component_type(eDepsSceneComponentType component)
void DEG_add_simulation_relation(DepsNodeHandle *node_handle, Simulation *simulation, const char *description)
Span< Depsgraph * > get_all_registered_graphs(Main *bmain)
NodeType geometry_tag_to_component(const ID *id)
@ RELATION_FLAG_FLUSH_USER_EDIT_ONLY
NodeType nodeTypeFromObjectComponent(eDepsObjectComponentType component_type)
Definition: deg_node.cc:184
@ DEG_UPDATE_SOURCE_RELATIONS
unsigned int uint32_t
Definition: stdint.h:83
Definition: DNA_ID.h:273
char name[66]
Definition: DNA_ID.h:283
Definition: BKE_main.h:116
DepsgraphRelationBuilder * builder
IDNode * find_id_node(const ID *id) const
Definition: depsgraph.cc:112