Cleanup: remove redundant doxygen \file argument
[blender.git] / source / blender / depsgraph / intern / eval / deg_eval_flush.cc
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 \ingroup depsgraph
21  *
22  * Core routines for how the Depsgraph works.
23  */
24
25 #include "intern/eval/deg_eval_flush.h"
26
27 // TODO(sergey): Use some sort of wrapper.
28 #include <deque>
29 #include <cmath>
30
31 #include "BKE_object.h"
32
33 #include "BLI_utildefines.h"
34 #include "BLI_listbase.h"
35 #include "BLI_math_vector.h"
36 #include "BLI_task.h"
37 #include "BLI_ghash.h"
38
39 extern "C" {
40 #include "DNA_object_types.h"
41
42 #include "DRW_engine.h"
43 } /* extern "C" */
44
45 #include "DEG_depsgraph.h"
46
47 #include "intern/debug/deg_debug.h"
48 #include "intern/depsgraph.h"
49 #include "intern/depsgraph_update.h"
50 #include "intern/node/deg_node.h"
51 #include "intern/node/deg_node_component.h"
52 #include "intern/node/deg_node_factory.h"
53 #include "intern/node/deg_node_id.h"
54 #include "intern/node/deg_node_operation.h"
55
56 #include "intern/eval/deg_eval_copy_on_write.h"
57
58 // Invalidate datablock data when update is flushed on it.
59 //
60 // The idea of this is to help catching cases when area is accessing data which
61 // is not yet evaluated, which could happen due to missing relations. The issue
62 // is that usually that data will be kept from previous frame, and it looks to
63 // be plausible.
64 //
65 // This ensures that data does not look plausible, making it much easier to
66 // catch usage of invalid state.
67 #undef INVALIDATE_ON_FLUSH
68
69 namespace DEG {
70
71 enum {
72         ID_STATE_NONE     = 0,
73         ID_STATE_MODIFIED = 1,
74 };
75
76 enum {
77         COMPONENT_STATE_NONE      = 0,
78         COMPONENT_STATE_SCHEDULED = 1,
79         COMPONENT_STATE_DONE      = 2,
80 };
81
82 typedef std::deque<OperationNode *> FlushQueue;
83
84 namespace {
85
86 void flush_init_operation_node_func(
87         void *__restrict data_v,
88         const int i,
89         const ParallelRangeTLS *__restrict /*tls*/)
90 {
91         Depsgraph *graph = (Depsgraph *)data_v;
92         OperationNode *node = graph->operations[i];
93         node->scheduled = false;
94 }
95
96 void flush_init_id_node_func(
97         void *__restrict data_v,
98         const int i,
99         const ParallelRangeTLS *__restrict /*tls*/)
100 {
101         Depsgraph *graph = (Depsgraph *)data_v;
102         IDNode *id_node = graph->id_nodes[i];
103         id_node->custom_flags = ID_STATE_NONE;
104         GHASH_FOREACH_BEGIN(ComponentNode *, comp_node, id_node->components)
105                 comp_node->custom_flags = COMPONENT_STATE_NONE;
106         GHASH_FOREACH_END();
107 }
108
109 BLI_INLINE void flush_prepare(Depsgraph *graph)
110 {
111         {
112                 const int num_operations = graph->operations.size();
113                 ParallelRangeSettings settings;
114                 BLI_parallel_range_settings_defaults(&settings);
115                 settings.min_iter_per_thread = 1024;
116                 BLI_task_parallel_range(0, num_operations,
117                                         graph,
118                                         flush_init_operation_node_func,
119                                         &settings);
120         }
121         {
122                 const int num_id_nodes = graph->id_nodes.size();
123                 ParallelRangeSettings settings;
124                 BLI_parallel_range_settings_defaults(&settings);
125                 settings.min_iter_per_thread = 1024;
126                 BLI_task_parallel_range(0, num_id_nodes,
127                                         graph,
128                                         flush_init_id_node_func,
129                                         &settings);
130         }
131 }
132
133 BLI_INLINE void flush_schedule_entrypoints(Depsgraph *graph, FlushQueue *queue)
134 {
135         GSET_FOREACH_BEGIN(OperationNode *, op_node, graph->entry_tags)
136         {
137                 queue->push_back(op_node);
138                 op_node->scheduled = true;
139                 DEG_DEBUG_PRINTF((::Depsgraph *)graph,
140                                  EVAL, "Operation is entry point for update: %s\n",
141                                  op_node->identifier().c_str());
142         }
143         GSET_FOREACH_END();
144 }
145
146 BLI_INLINE void flush_handle_id_node(IDNode *id_node)
147 {
148         id_node->custom_flags = ID_STATE_MODIFIED;
149 }
150
151 /* TODO(sergey): We can reduce number of arguments here. */
152 BLI_INLINE void flush_handle_component_node(IDNode *id_node,
153                                             ComponentNode *comp_node,
154                                             FlushQueue *queue)
155 {
156         /* We only handle component once. */
157         if (comp_node->custom_flags == COMPONENT_STATE_DONE) {
158                 return;
159         }
160         comp_node->custom_flags = COMPONENT_STATE_DONE;
161         /* Tag all required operations in component for update, unless this is a
162          * special component where we don't want all operations to be tagged.
163          *
164          * TODO(sergey): Make this a more generic solution. */
165         if (comp_node->type != NodeType::PARTICLE_SETTINGS &&
166             comp_node->type != NodeType::PARTICLE_SYSTEM)
167         {
168                 for (OperationNode *op : comp_node->operations) {
169                         op->flag |= DEPSOP_FLAG_NEEDS_UPDATE;
170                 }
171         }
172         /* when some target changes bone, we might need to re-run the
173          * whole IK solver, otherwise result might be unpredictable. */
174         if (comp_node->type == NodeType::BONE) {
175                 ComponentNode *pose_comp =
176                         id_node->find_component(NodeType::EVAL_POSE);
177                 BLI_assert(pose_comp != NULL);
178                 if (pose_comp->custom_flags == COMPONENT_STATE_NONE) {
179                         queue->push_front(pose_comp->get_entry_operation());
180                         pose_comp->custom_flags = COMPONENT_STATE_SCHEDULED;
181                 }
182         }
183 }
184
185 /* Schedule children of the given operation node for traversal.
186  *
187  * One of the children will by-pass the queue and will be returned as a function
188  * return value, so it can start being handled right away, without building too
189  * much of a queue.
190  */
191 BLI_INLINE OperationNode *flush_schedule_children(
192         OperationNode *op_node,
193         FlushQueue *queue)
194 {
195         OperationNode *result = NULL;
196         for (Relation *rel : op_node->outlinks) {
197                 /* Flush is forbidden, completely. */
198                 if (rel->flag & RELATION_FLAG_NO_FLUSH) {
199                         continue;
200                 }
201                 /* Relation only allows flushes on user changes, but the node was not
202                  * affected by user. */
203                 if ((rel->flag & RELATION_FLAG_FLUSH_USER_EDIT_ONLY) &&
204                     (op_node->flag & DEPSOP_FLAG_USER_MODIFIED) == 0)
205                 {
206                         continue;
207                 }
208                 OperationNode *to_node = (OperationNode *)rel->to;
209                 /* Always flush flushable flags, so children always know what happened
210                  * to their parents. */
211                 to_node->flag |= (op_node->flag & DEPSOP_FLAG_FLUSH);
212                 /* Flush update over the relation, if it was not flushed yet. */
213                 if (to_node->scheduled) {
214                         continue;
215                 }
216                 if (result != NULL) {
217                         queue->push_front(to_node);
218                 }
219                 else {
220                         result = to_node;
221                 }
222                 to_node->scheduled = true;
223         }
224         return result;
225 }
226
227 void flush_engine_data_update(ID *id)
228 {
229         DrawDataList *draw_data_list = DRW_drawdatalist_from_id(id);
230         if (draw_data_list == NULL) {
231                 return;
232         }
233         LISTBASE_FOREACH (DrawData *, draw_data, draw_data_list) {
234                 draw_data->recalc |= id->recalc;
235         }
236 }
237
238 /* NOTE: It will also accumulate flags from changed components. */
239 void flush_editors_id_update(Main *bmain,
240                              Depsgraph *graph,
241                              const DEGEditorUpdateContext *update_ctx)
242 {
243         for (IDNode *id_node : graph->id_nodes) {
244                 if (id_node->custom_flags != ID_STATE_MODIFIED) {
245                         continue;
246                 }
247                 DEG_id_type_tag(bmain, GS(id_node->id_orig->name));
248                 /* TODO(sergey): Do we need to pass original or evaluated ID here? */
249                 ID *id_orig = id_node->id_orig;
250                 ID *id_cow = id_node->id_cow;
251                 /* Copy tag from original data to CoW storage.
252                  * This is because DEG_id_tag_update() sets tags on original
253                  * data. */
254                 id_cow->recalc |= (id_orig->recalc & ID_RECALC_ALL);
255                 /* Gather recalc flags from all changed components. */
256                 GHASH_FOREACH_BEGIN(ComponentNode *, comp_node, id_node->components)
257                 {
258                         if (comp_node->custom_flags != COMPONENT_STATE_DONE) {
259                                 continue;
260                         }
261                         DepsNodeFactory *factory = type_get_factory(comp_node->type);
262                         BLI_assert(factory != NULL);
263                         id_cow->recalc |= factory->id_recalc_tag();
264                 }
265                 GHASH_FOREACH_END();
266                 DEG_DEBUG_PRINTF((::Depsgraph *)graph,
267                                  EVAL, "Accumulated recalc bits for %s: %u\n",
268                                  id_orig->name, (unsigned int)id_cow->recalc);
269
270                 /* Inform editors. Only if the datablock is being evaluated a second
271                  * time, to distinguish between user edits and initial evaluation when
272                  * the datablock becomes visible.
273                  *
274                  * TODO: image datablocks do not use COW, so might not be detected
275                  * correctly. */
276                 if (deg_copy_on_write_is_expanded(id_cow)) {
277                         if (graph->is_active) {
278                                 deg_editors_id_update(update_ctx, id_orig);
279                         }
280                         /* ID may need to get its auto-override operations refreshed. */
281                         if (ID_IS_STATIC_OVERRIDE_AUTO(id_orig)) {
282                                 id_orig->tag |= LIB_TAG_OVERRIDESTATIC_AUTOREFRESH;
283                         }
284                         /* Inform draw engines that something was changed. */
285                         flush_engine_data_update(id_cow);
286                 }
287         }
288 }
289
290 #ifdef INVALIDATE_ON_FLUSH
291 void invalidate_tagged_evaluated_transform(ID *id)
292 {
293         const ID_Type id_type = GS(id->name);
294         switch (id_type) {
295                 case ID_OB:
296                 {
297                         Object *object = (Object *)id;
298                         copy_vn_fl((float *)object->obmat, 16, NAN);
299                         break;
300                 }
301                 default:
302                         break;
303         }
304 }
305
306 void invalidate_tagged_evaluated_geometry(ID *id)
307 {
308         const ID_Type id_type = GS(id->name);
309         switch (id_type) {
310                 case ID_OB:
311                 {
312                         Object *object = (Object *)id;
313                         BKE_object_free_derived_caches(object);
314                         break;
315                 }
316                 default:
317                         break;
318         }
319 }
320 #endif
321
322 void invalidate_tagged_evaluated_data(Depsgraph *graph)
323 {
324 #ifdef INVALIDATE_ON_FLUSH
325         for (IDNode *id_node : graph->id_nodes) {
326                 if (id_node->custom_flags != ID_STATE_MODIFIED) {
327                         continue;
328                 }
329                 ID *id_cow = id_node->id_cow;
330                 if (!deg_copy_on_write_is_expanded(id_cow)) {
331                         continue;
332                 }
333                 GHASH_FOREACH_BEGIN(ComponentNode *, comp_node, id_node->components)
334                 {
335                         if (comp_node->custom_flags != COMPONENT_STATE_DONE) {
336                                 continue;
337                         }
338                         switch (comp_node->type) {
339                                 case ID_RECALC_TRANSFORM:
340                                         invalidate_tagged_evaluated_transform(id_cow);
341                                         break;
342                                 case ID_RECALC_GEOMETRY:
343                                         invalidate_tagged_evaluated_geometry(id_cow);
344                                         break;
345                                 default:
346                                         break;
347                         }
348                 }
349                 GHASH_FOREACH_END();
350         }
351 #else
352         (void) graph;
353 #endif
354 }
355
356 }  // namespace
357
358 /* Flush updates from tagged nodes outwards until all affected nodes
359  * are tagged.
360  */
361 void deg_graph_flush_updates(Main *bmain, Depsgraph *graph)
362 {
363         /* Sanity checks. */
364         BLI_assert(bmain != NULL);
365         BLI_assert(graph != NULL);
366         /* Nothing to update, early out. */
367         if (BLI_gset_len(graph->entry_tags) == 0) {
368                 return;
369         }
370         /* Reset all flags, get ready for the flush. */
371         flush_prepare(graph);
372         /* Starting from the tagged "entry" nodes, flush outwards. */
373         FlushQueue queue;
374         flush_schedule_entrypoints(graph, &queue);
375         /* Prepare update context for editors. */
376         DEGEditorUpdateContext update_ctx;
377         update_ctx.bmain = bmain;
378         update_ctx.depsgraph = (::Depsgraph *)graph;
379         update_ctx.scene = graph->scene;
380         update_ctx.view_layer = graph->view_layer;
381         /* Do actual flush. */
382         while (!queue.empty()) {
383                 OperationNode *op_node = queue.front();
384                 queue.pop_front();
385                 while (op_node != NULL) {
386                         /* Tag operation as required for update. */
387                         op_node->flag |= DEPSOP_FLAG_NEEDS_UPDATE;
388                         /* Inform corresponding ID and component nodes about the change. */
389                         ComponentNode *comp_node = op_node->owner;
390                         IDNode *id_node = comp_node->owner;
391                         flush_handle_id_node(id_node);
392                         flush_handle_component_node(id_node,
393                                                     comp_node,
394                                                     &queue);
395                         /* Flush to nodes along links. */
396                         op_node = flush_schedule_children(op_node, &queue);
397                 }
398         }
399         /* Inform editors about all changes. */
400         flush_editors_id_update(bmain, graph, &update_ctx);
401         /* Reset evaluation result tagged which is tagged for update to some state
402          * which is obvious to catch. */
403         invalidate_tagged_evaluated_data(graph);
404 }
405
406 static void graph_clear_operation_func(
407         void *__restrict data_v,
408         const int i,
409         const ParallelRangeTLS *__restrict /*tls*/)
410 {
411         Depsgraph *graph = (Depsgraph *)data_v;
412         OperationNode *node = graph->operations[i];
413         /* Clear node's "pending update" settings. */
414         node->flag &= ~(DEPSOP_FLAG_DIRECTLY_MODIFIED |
415                         DEPSOP_FLAG_NEEDS_UPDATE |
416                         DEPSOP_FLAG_USER_MODIFIED);
417 }
418
419 /* Clear tags from all operation nodes. */
420 void deg_graph_clear_tags(Depsgraph *graph)
421 {
422         /* Go over all operation nodes, clearing tags. */
423         {
424                 const int num_operations = graph->operations.size();
425                 ParallelRangeSettings settings;
426                 BLI_parallel_range_settings_defaults(&settings);
427                 settings.min_iter_per_thread = 1024;
428                 BLI_task_parallel_range(0, num_operations,
429                                         graph,
430                                         graph_clear_operation_func,
431                                         &settings);
432         }
433         /* Clear any entry tags which haven't been flushed. */
434         BLI_gset_clear(graph->entry_tags, NULL);
435 }
436
437 }  // namespace DEG