Blender V5.3
deg_eval_flush.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
12
13#include <algorithm>
14#include <deque>
15
16#include "BLI_listbase.hh"
17#include "BLI_task_c.hh"
18#include "BLI_utildefines.hh"
19
20#include "BKE_global.hh"
21#include "BKE_key.hh"
22#include "BKE_object.hh"
23#include "BKE_scene.hh"
24
25#include "DRW_engine.hh"
26
27#include "DEG_depsgraph.hh"
29
31#include "intern/depsgraph.hh"
40
42
43/* Invalidate data-block data when update is flushed on it.
44 *
45 * The idea of this is to help catching cases when area is accessing data which
46 * is not yet evaluated, which could happen due to missing relations. The issue
47 * is that usually that data will be kept from previous frame, and it looks to
48 * be plausible.
49 *
50 * This ensures that data does not look plausible, making it much easier to
51 * catch usage of invalid state. */
52#undef INVALIDATE_ON_FLUSH
53
54namespace blender::deg {
55
56enum {
59};
60
61enum {
65};
66
67using FlushQueue = std::deque<OperationNode *>;
68
69namespace {
70
71void flush_init_id_node_func(void *__restrict data_v,
72 const int i,
73 const TaskParallelTLS *__restrict /*tls*/)
74{
75 Depsgraph *graph = static_cast<Depsgraph *>(data_v);
76 IDNode *id_node = graph->id_nodes[i];
77 id_node->custom_flags = ID_STATE_NONE;
78 for (ComponentNode *comp_node : id_node->components.values()) {
79 comp_node->custom_flags = COMPONENT_STATE_NONE;
80 }
81}
82
83inline void flush_prepare(Depsgraph *graph)
84{
85 for (OperationNode *node : graph->operations) {
86 node->scheduled = false;
87 }
88
89 {
90 const int num_id_nodes = graph->id_nodes.size();
91 TaskParallelSettings settings;
93 settings.min_iter_per_thread = 1024;
94 BLI_task_parallel_range(0, num_id_nodes, graph, flush_init_id_node_func, &settings);
95 }
96}
97
98inline void flush_schedule_entrypoints(Depsgraph *graph, FlushQueue *queue)
99{
100 for (OperationNode *op_node : graph->entry_tags) {
101 queue->push_back(op_node);
102 op_node->scheduled = true;
103 DEG_DEBUG_PRINTF((::blender::Depsgraph *)graph,
104 EVAL,
105 "Operation is entry point for update: %s\n",
106 op_node->identifier().c_str());
107 }
108}
109
110inline void flush_handle_id_node(IDNode *id_node)
111{
113}
114
115/* TODO(sergey): We can reduce number of arguments here. */
116inline void flush_handle_component_node(IDNode *id_node,
117 ComponentNode *comp_node,
118 FlushQueue *queue)
119{
120 /* We only handle component once. */
121 if (comp_node->custom_flags == COMPONENT_STATE_DONE) {
122 return;
123 }
125 /* Tag all required operations in component for update, unless this is a
126 * special component where we don't want all operations to be tagged.
127 *
128 * TODO(sergey): Make this a more generic solution. */
130 const bool is_geometry_component = comp_node->type == NodeType::GEOMETRY;
131 for (OperationNode *op : comp_node->operations) {
132 /* Special case for the visibility operation in the geometry component.
133 *
134 * This operation is a part of the geometry component so that manual tag for geometry recalc
135 * ensures that the visibility is re-evaluated. This operation is not to be re-evaluated when
136 * an update is flushed to the geometry component via a time dependency or a driver targeting
137 * a modifier. Skipping update in this case avoids CPU time unnecessarily spent looping over
138 * modifiers and looking up operations by name in the visibility evaluation function. */
139 if (is_geometry_component && op->opcode == OperationCode::VISIBILITY) {
140 continue;
141 }
143 }
144 }
145 /* when some target changes bone, we might need to re-run the
146 * whole IK solver, otherwise result might be unpredictable. */
147 if (comp_node->type == NodeType::BONE) {
148 ComponentNode *pose_comp = id_node->find_component(NodeType::EVAL_POSE);
149 BLI_assert(pose_comp != nullptr);
150 if (pose_comp->custom_flags == COMPONENT_STATE_NONE) {
151 queue->push_front(pose_comp->get_entry_operation());
153 }
154 }
155}
156
157/* Schedule children of the given operation node for traversal.
158 *
159 * One of the children will bypass the queue and will be returned as a function
160 * return value, so it can start being handled right away, without building too
161 * much of a queue.
162 */
163inline OperationNode *flush_schedule_children(OperationNode *op_node, FlushQueue *queue)
164{
165 if (op_node->flag & DEPSOP_FLAG_USER_MODIFIED) {
166 ComponentNode *component_node = op_node->owner;
167 component_node->is_user_modified = true;
168 component_node->owner->is_user_modified = true;
169 }
170
171 OperationNode *result = nullptr;
172 for (Relation *rel : op_node->outlinks) {
173 /* Flush is forbidden, completely. */
174 if (rel->flag & RELATION_FLAG_NO_FLUSH) {
175 continue;
176 }
177 /* Relation only allows flushes on user changes, but the node was not
178 * affected by user. */
180 (op_node->flag & DEPSOP_FLAG_USER_MODIFIED) == 0)
181 {
182 continue;
183 }
184 OperationNode *to_node = static_cast<OperationNode *>(rel->to);
185 /* Always flush flushable flags, so children always know what happened
186 * to their parents. */
187 to_node->flag |= (op_node->flag & DEPSOP_FLAG_FLUSH);
188 /* Flush update over the relation, if it was not flushed yet. */
189 if (to_node->scheduled) {
190 continue;
191 }
192 if (result != nullptr) {
193 queue->push_front(to_node);
194 }
195 else {
196 result = to_node;
197 }
198 to_node->scheduled = true;
199 }
200 return result;
201}
202
203/* NOTE: It will also accumulate flags from changed components. */
204void flush_editors_id_update(Depsgraph *graph, const DEGEditorUpdateContext *update_ctx)
205{
206 for (IDNode *id_node : graph->id_nodes) {
207 if (id_node->custom_flags != ID_STATE_MODIFIED) {
208 continue;
209 }
210 DEG_graph_id_type_tag(reinterpret_cast<::blender::Depsgraph *>(graph),
211 GS(id_node->id_orig->name));
212 /* TODO(sergey): Do we need to pass original or evaluated ID here? */
213 ID *id_orig = id_node->id_orig;
214 ID *id_cow = id_node->id_cow;
215 /* Gather recalc flags from all changed components. */
216 for (ComponentNode *comp_node : id_node->components.values()) {
217 if (comp_node->custom_flags != COMPONENT_STATE_DONE) {
218 continue;
219 }
220 DepsNodeFactory *factory = type_get_factory(comp_node->type);
221 BLI_assert(factory != nullptr);
222 id_cow->recalc |= factory->id_recalc_tag();
223 }
224 DEG_DEBUG_PRINTF((blender::Depsgraph *)graph,
225 EVAL,
226 "Accumulated recalc bits for %s: %u\n",
227 id_orig->name,
228 uint(id_cow->recalc));
229
230 /* Inform editors. Only if the data-block is being evaluated a second
231 * time, to distinguish between user edits and initial evaluation when
232 * the data-block becomes visible.
233 *
234 * TODO: image data-blocks do not use copy-on-eval, so might not be detected
235 * correctly. */
236 if (deg_eval_copy_is_expanded(id_cow)) {
237 if (graph->is_active && id_node->is_user_modified) {
238 deg_editors_id_update(update_ctx, id_orig);
239 }
240 }
241 }
242}
243
244#ifdef INVALIDATE_ON_FLUSH
245void invalidate_tagged_evaluated_transform(ID *id)
246{
247 const ID_Type id_type = GS(id->name);
248 switch (id_type) {
249 case ID_OB: {
250 Object *object = (Object *)id;
251 std::fill_n((float *)object->object_to_world().ptr(), 16, NAN);
252 break;
253 }
254 default:
255 break;
256 }
257}
258
259void invalidate_tagged_evaluated_geometry(ID *id)
260{
261 const ID_Type id_type = GS(id->name);
262 switch (id_type) {
263 case ID_OB: {
264 Object *object = (Object *)id;
266 break;
267 }
268 default:
269 break;
270 }
271}
272#endif
273
274void invalidate_tagged_evaluated_data(Depsgraph *graph)
275{
276#ifdef INVALIDATE_ON_FLUSH
277 for (IDNode *id_node : graph->id_nodes) {
278 if (id_node->custom_flags != ID_STATE_MODIFIED) {
279 continue;
280 }
281 ID *id_cow = id_node->id_cow;
282 if (!deg_eval_copy_is_expanded(id_cow)) {
283 continue;
284 }
285 for (ComponentNode *comp_node : id_node->components.values()) {
286 if (comp_node->custom_flags != COMPONENT_STATE_DONE) {
287 continue;
288 }
289 switch (comp_node->type) {
291 invalidate_tagged_evaluated_transform(id_cow);
292 break;
294 invalidate_tagged_evaluated_geometry(id_cow);
295 break;
296 default:
297 break;
298 }
299 }
300 }
301#else
302 (void)graph;
303#endif
304}
305
306} // namespace
307
309{
310 /* Sanity checks. */
311 BLI_assert(graph != nullptr);
312 Main *bmain = graph->bmain;
313
314 graph->time_source->flush_update_tag(graph);
315
316 /* Nothing to update, early out. */
317 if (graph->entry_tags.is_empty()) {
318 return;
319 }
320 /* Reset all flags, get ready for the flush. */
321 flush_prepare(graph);
322 /* Starting from the tagged "entry" nodes, flush outwards. */
323 FlushQueue queue;
324 flush_schedule_entrypoints(graph, &queue);
325 /* Prepare update context for editors. */
326 DEGEditorUpdateContext update_ctx;
327 update_ctx.bmain = bmain;
328 update_ctx.depsgraph = reinterpret_cast<::blender::Depsgraph *>(graph);
329 update_ctx.scene = graph->scene;
330 update_ctx.view_layer = graph->view_layer;
331 /* Do actual flush. */
332 while (!queue.empty()) {
333 OperationNode *op_node = queue.front();
334 queue.pop_front();
335 while (op_node != nullptr) {
336 /* Tag operation as required for update. */
337 op_node->flag |= DEPSOP_FLAG_NEEDS_UPDATE;
338 /* Inform corresponding ID and component nodes about the change. */
339 ComponentNode *comp_node = op_node->owner;
340 IDNode *id_node = comp_node->owner;
341 flush_handle_id_node(id_node);
342 flush_handle_component_node(id_node, comp_node, &queue);
343 /* Flush to nodes along links. */
344 op_node = flush_schedule_children(op_node, &queue);
345 }
346 }
347 /* Inform editors about all changes. */
348 flush_editors_id_update(graph, &update_ctx);
349 /* Reset evaluation result tagged which is tagged for update to some state
350 * which is obvious to catch. */
351 invalidate_tagged_evaluated_data(graph);
352}
353
355{
356 /* Clear any entry tags which haven't been flushed. */
357 graph->entry_tags.clear();
358
359 graph->time_source->tagged_for_update = false;
360}
361
362} // namespace blender::deg
General operations, lookup, etc. for blender objects.
#define ELEM(...)
int64_t size() const
#define DEG_DEBUG_PRINTF(depsgraph, type,...)
Definition deg_debug.h:47
void deg_editors_id_update(const DEGEditorUpdateContext *update_ctx, ID *id)
std::deque< OperationNode * > FlushQueue
bool deg_eval_copy_is_expanded(const ID *id_cow)
void deg_graph_clear_tags(Depsgraph *graph)
DepsNodeFactory * type_get_factory(const NodeType type)
void deg_graph_flush_updates(Depsgraph *graph)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
unsigned int uint
Definition BKE_text.h:55
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
void BKE_object_free_derived_caches(Object *ob)
Definition object.cc:1810
void DEG_graph_id_type_tag(Depsgraph *depsgraph, short id_type)
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
ID_Type GS(const char *name)
Definition DNA_ID.h:410
char name[258]
Definition DNA_ID.h:433
unsigned int recalc
Definition DNA_ID.h:446
OperationNode * get_entry_operation() override
Vector< OperationNode * > operations
virtual int id_recalc_tag() const =0
OperationNodes operations
Definition depsgraph.hh:135
Set< OperationNode * > entry_tags
Definition depsgraph.hh:129
TimeSourceNode * time_source
Definition depsgraph.hh:102
Map< ComponentIDKey, ComponentNode * > components
ComponentNode * find_component(NodeType type, StringRef name="") const
Relations outlinks
Definition deg_node.hh:189
std::string identifier() const override
void flush_update_tag(Depsgraph *graph)