Blender V5.3
depsgraph.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2013 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "intern/depsgraph.hh" /* own include */
12
13#include <cstring>
14#include <type_traits>
15
16#include "BLI_utildefines.hh"
17
18#include "BKE_global.hh"
19#include "BKE_idtype.hh"
20#include "BKE_scene.hh"
21
22#include "DEG_depsgraph.hh"
24
28
30
37
38namespace blender {
39
40namespace deg {
41
73
80
81/* Node Management ---------------------------- */
82
84{
85 if (time_source == nullptr) {
87 time_source = static_cast<TimeSourceNode *>(factory->create_node(nullptr, "", "Time Source"));
88 }
89 return time_source;
90}
91
96
101
103{
104 return id_hash.lookup_default(id, nullptr);
105}
106
108{
109 BLI_assert((id->tag & ID_TAG_COPIED_ON_EVAL) == 0);
110 IDNode *id_node = find_id_node(id);
111 if (!id_node) {
113 id_node = static_cast<IDNode *>(factory->create_node(id, "", id->name));
114 id_node->init_copy_on_write(id_cow_hint);
115 /* Register node in ID hash.
116 *
117 * NOTE: We address ID nodes by the original ID pointer they are
118 * referencing to. */
119 id_hash.add_new(id, id_node);
120 id_nodes.append(id_node);
121
123 }
124 return id_node;
125}
126
127template<typename FilterFunc>
128static void clear_id_nodes_conditional(Depsgraph::IDDepsNodes *id_nodes, const FilterFunc &filter)
129{
130 for (IDNode *id_node : *id_nodes) {
131 if (id_node->id_cow == nullptr) {
132 /* This means builder "stole" ownership of the evaluated
133 * datablock for its own dirty needs. */
134 continue;
135 }
136 if (id_node->id_cow == id_node->id_orig) {
137 /* Evaluated copy is not needed for this ID type.
138 *
139 * NOTE: Is important to not de-reference the original datablock here because it might be
140 * freed already (happens during main database free when some IDs are freed prior to a
141 * scene). */
142 continue;
143 }
144 if (!deg_eval_copy_is_expanded(id_node->id_cow)) {
145 continue;
146 }
147 const ID_Type id_type = GS(id_node->id_cow->name);
148 if (filter(id_type)) {
149 id_node->destroy();
150 }
151 }
152}
153
155{
156 /* Free memory used by ID nodes. */
157
158 /* Stupid workaround to ensure we free IDs in a proper order. */
159 clear_id_nodes_conditional(&id_nodes, [](ID_Type id_type) { return id_type == ID_SCE; });
160 clear_id_nodes_conditional(&id_nodes, [](ID_Type id_type) { return id_type != ID_PA; });
161
162 for (IDNode *id_node : id_nodes) {
163 delete id_node;
164 }
165 /* Clear containers. */
166 id_hash.clear();
167 id_nodes.clear();
168 /* Clear physics relation caches. */
170
172}
173
174Relation *Depsgraph::add_new_relation(Node *from, Node *to, const char *description, int flags)
175{
176 Relation *rel = nullptr;
177 if (flags & RELATION_CHECK_BEFORE_ADD) {
178 rel = check_nodes_connected(from, to, description);
179 }
180 if (rel != nullptr) {
181 rel->flag |= flags;
182 return rel;
183 }
184
185#ifndef NDEBUG
186 if (from->type == NodeType::OPERATION && to->type == NodeType::OPERATION) {
187 OperationNode *operation_from = static_cast<OperationNode *>(from);
188 OperationNode *operation_to = static_cast<OperationNode *>(to);
189 BLI_assert(operation_to->owner->type != NodeType::COPY_ON_EVAL ||
190 operation_from->owner->type == NodeType::COPY_ON_EVAL);
191 }
192#endif
193
194 /* Create new relation, and add it to the graph. The type must be trivially destructible for
195 * `.release()` to be okay. If it weren't, we could store the relations with #destruct_ptr on
196 * either the `inlinks` or `outlinks`. But since so many #Relation structs are allocated, it's
197 * probably better for it be a simple type anyway. */
198 static_assert(std::is_trivially_destructible_v<Relation>);
199 rel = this->build_allocator.construct<Relation>(from, to, description).release();
200 from->outlinks.append(rel);
201 to->inlinks.append(rel);
202 rel->flag |= flags;
203 return rel;
204}
205
207 const Node *to,
208 const char *description)
209{
210 for (Relation *rel : from->outlinks) {
211 BLI_assert(rel->from == from);
212 if (rel->to != to) {
213 continue;
214 }
215 if (description != nullptr && !STREQ(rel->name, description)) {
216 continue;
217 }
218 return rel;
219 }
220 return nullptr;
221}
222
223/* Low level tagging -------------------------------------- */
224
226{
227 /* Sanity check. */
228 if (node == nullptr) {
229 return;
230 }
231 /* Add to graph-level set of directly modified nodes to start searching
232 * from.
233 * NOTE: this is necessary since we have several thousand nodes to play
234 * with. */
235 entry_tags.add(node);
236}
237
239{
241 delete time_source;
242 time_source = nullptr;
243 /* Memory used by the build allocator is now unused. Rebuild it from scratch. */
244 std::destroy_at(&this->build_allocator);
245 new (&this->build_allocator) LinearAllocator<>();
246}
247
248ID *Depsgraph::get_cow_id(const ID *id_orig) const
249{
250 IDNode *id_node = find_id_node(id_orig);
251 if (id_node == nullptr) {
252 /* This function is used from places where we expect ID to be either
253 * already a copy-on-evaluation version or have a corresponding copy-on-evaluation
254 * version.
255 *
256 * We try to enforce that in debug builds, for release we play a bit
257 * safer game here. */
258 if ((id_orig->tag & ID_TAG_COPIED_ON_EVAL) == 0) {
259 /* TODO(sergey): This is nice sanity check to have, but it fails
260 * in following situations:
261 *
262 * - Material has link to texture, which is not needed by new
263 * shading system and hence can be ignored at construction.
264 * - Object or mesh has material at a slot which is not used (for
265 * example, object has material slot by materials are set to
266 * object data). */
267 // BLI_assert_msg(0, "Request for non-existing copy-on-evaluation ID");
268 }
269 return const_cast<ID *>(id_orig);
270 }
271 return id_node->id_cow;
272}
273
274} // namespace deg
275
276/* **************** */
277/* Public Graph API */
278
279Depsgraph *DEG_graph_new(Main *bmain, Scene *scene, ViewLayer *view_layer, eEvaluationMode mode)
280{
281 deg::Depsgraph *deg_depsgraph = new deg::Depsgraph(bmain, scene, view_layer, mode);
282 deg::register_graph(deg_depsgraph);
283 return reinterpret_cast<Depsgraph *>(deg_depsgraph);
284}
285
286void DEG_graph_replace_owners(Depsgraph *depsgraph,
287 Main *bmain,
288 Scene *scene,
289 ViewLayer *view_layer)
290{
291 deg::Depsgraph *deg_graph = reinterpret_cast<deg::Depsgraph *>(depsgraph);
292
293 const bool do_update_register = deg_graph->bmain != bmain;
294 if (do_update_register && deg_graph->bmain != nullptr) {
295 deg::unregister_graph(deg_graph);
296 }
297
298 deg_graph->bmain = bmain;
299 deg_graph->scene = scene;
300 deg_graph->view_layer = view_layer;
301
302 if (do_update_register) {
303 deg::register_graph(deg_graph);
304 }
305}
306
307void DEG_graph_free(Depsgraph *graph)
308{
309 if (graph == nullptr) {
310 return;
311 }
312 using deg::Depsgraph;
313 deg::Depsgraph *deg_depsgraph = reinterpret_cast<deg::Depsgraph *>(graph);
314 deg::unregister_graph(deg_depsgraph);
315 delete deg_depsgraph;
316}
317
318bool DEG_is_evaluating(const Depsgraph *depsgraph)
319{
320 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(depsgraph);
321 return deg_graph->is_evaluating;
322}
323
324bool DEG_is_active(const Depsgraph *depsgraph)
325{
326 if (depsgraph == nullptr) {
327 /* Happens for such cases as work object in what_does_obaction(),
328 * and sine render pipeline parts. Shouldn't really be accepting
329 * nullptr depsgraph, but is quite hard to get proper one in those
330 * cases. */
331 return false;
332 }
333 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(depsgraph);
334 return deg_graph->is_active;
335}
336
337void DEG_make_active(Depsgraph *depsgraph)
338{
339 deg::Depsgraph *deg_graph = reinterpret_cast<deg::Depsgraph *>(depsgraph);
340 deg_graph->is_active = true;
341 /* TODO(sergey): Copy data from evaluated state to original. */
342}
343
344void DEG_make_inactive(Depsgraph *depsgraph)
345{
346 deg::Depsgraph *deg_graph = reinterpret_cast<deg::Depsgraph *>(depsgraph);
347 deg_graph->is_active = false;
348}
349
350void DEG_disable_visibility_optimization(Depsgraph *depsgraph)
351{
352 deg::Depsgraph *deg_graph = reinterpret_cast<deg::Depsgraph *>(depsgraph);
353 deg_graph->use_visibility_optimization = false;
354}
355
356uint64_t DEG_get_update_count(const Depsgraph *depsgraph)
357{
358 const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(depsgraph);
359 return deg_graph->update_count;
360}
361
362} // namespace blender
#define STREQ(a, b)
destruct_ptr< T > construct(Args &&...args)
void append(const T &value)
bool deg_eval_copy_is_expanded(const ID *id_cow)
static void clear_id_nodes_conditional(Depsgraph::IDDepsNodes *id_nodes, const FilterFunc &filter)
Definition depsgraph.cc:128
void clear_physics_relations(Depsgraph *graph)
DepsNodeFactory * type_get_factory(const NodeType type)
void unregister_graph(Depsgraph *depsgraph)
void register_graph(Depsgraph *depsgraph)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
bool DEG_is_active(const Depsgraph *depsgraph)
Definition depsgraph.cc:324
void DEG_graph_free(Depsgraph *graph)
Definition depsgraph.cc:307
uint64_t DEG_get_update_count(const Depsgraph *depsgraph)
Definition depsgraph.cc:356
Depsgraph * DEG_graph_new(Main *bmain, Scene *scene, ViewLayer *view_layer, eEvaluationMode mode)
Definition depsgraph.cc:279
void DEG_make_inactive(Depsgraph *depsgraph)
Definition depsgraph.cc:344
void DEG_make_active(Depsgraph *depsgraph)
Definition depsgraph.cc:337
void BLI_spin_end(SpinLock *spin)
Definition threads.cc:448
float BKE_scene_frame_get(const Scene *scene)
Definition scene.cc:2595
void DEG_graph_replace_owners(Depsgraph *depsgraph, Main *bmain, Scene *scene, ViewLayer *view_layer)
Definition depsgraph.cc:286
int BKE_idtype_idcode_to_index(short idcode)
Definition idtype.cc:338
void BLI_spin_init(SpinLock *spin)
Definition threads.cc:394
float BKE_scene_ctime_get(const Scene *scene)
Definition scene.cc:2581
ID_Type GS(const char *name)
Definition DNA_ID.h:410
@ ID_TAG_COPIED_ON_EVAL
Definition DNA_ID.h:1000
@ DEG_EVALUATE_SYNC_WRITEBACK_NO
bool DEG_is_evaluating(const Depsgraph *depsgraph)
Definition depsgraph.cc:318
void DEG_disable_visibility_optimization(Depsgraph *depsgraph)
Definition depsgraph.cc:350
virtual Node * create_node(const ID *id, const char *subdata, StringRef name) const =0
ID * get_cow_id(const ID *id_orig) const
Definition depsgraph.cc:248
IDNode * find_id_node(const ID *id) const
Definition depsgraph.cc:102
Map< const ID *, ListBaseT< CollisionRelation > * > * physics_relations_collision[DEG_PHYSICS_COLLISION_NUM]
Definition depsgraph.hh:185
char id_type_updated[INDEX_ID_MAX]
Definition depsgraph.hh:119
bool need_tag_id_on_graph_visibility_time_update
Definition depsgraph.hh:116
LinearAllocator build_allocator
Definition depsgraph.hh:90
light_linking::Cache light_linking_cache
Definition depsgraph.hh:187
bool need_tag_id_on_graph_visibility_update
Definition depsgraph.hh:115
TimeSourceNode * find_time_source() const
Definition depsgraph.cc:92
IDNode * add_id_node(ID *id, ID *id_cow_hint=nullptr)
Definition depsgraph.cc:107
Vector< IDNode * > IDDepsNodes
Definition depsgraph.hh:52
eEvaluationMode mode
Definition depsgraph.hh:145
char id_type_updated_backup[INDEX_ID_MAX]
Definition depsgraph.hh:121
Depsgraph(Main *bmain, Scene *scene, ViewLayer *view_layer, eEvaluationMode mode)
Definition depsgraph.cc:42
DepsgraphEvaluateSyncWriteback sync_writeback
Definition depsgraph.hh:193
Relation * add_new_relation(Node *from, Node *to, const char *description, int flags=0)
Definition depsgraph.cc:174
char id_type_exist[INDEX_ID_MAX]
Definition depsgraph.hh:124
Map< const ID *, IDNode * > id_hash
Definition depsgraph.hh:94
Map< const ID *, ListBaseT< EffectorRelation > * > * physics_relations_effector
Definition depsgraph.hh:183
Relation * check_nodes_connected(const Node *from, const Node *to, const char *description)
Definition depsgraph.cc:206
TimeSourceNode * add_time_source()
Definition depsgraph.cc:83
Set< OperationNode * > entry_tags
Definition depsgraph.hh:129
TimeSourceNode * time_source
Definition depsgraph.hh:102
void add_entry_tag(OperationNode *node)
Definition depsgraph.cc:225
void init_copy_on_write(ID *id_cow_hint=nullptr)
Relations inlinks
Definition deg_node.hh:188
Relations outlinks
Definition deg_node.hh:189
void tag_update(Depsgraph *graph, eUpdateSource source) override