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