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