3fcaa12f03607086ab59274d181319308d2e82e9
[blender.git] / source / blender / depsgraph / intern / builder / deg_builder_nodes.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): Based on original depsgraph.c code - Blender Foundation (2005-2013)
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/depsgraph/intern/builder/deg_builder_nodes.cc
28  *  \ingroup depsgraph
29  *
30  * Methods for constructing depsgraph's nodes
31  */
32
33 #include "intern/builder/deg_builder_nodes.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_string.h"
42 #include "BLI_utildefines.h"
43
44 extern "C" {
45 #include "DNA_action_types.h"
46 #include "DNA_anim_types.h"
47 #include "DNA_armature_types.h"
48 #include "DNA_cachefile_types.h"
49 #include "DNA_camera_types.h"
50 #include "DNA_collection_types.h"
51 #include "DNA_constraint_types.h"
52 #include "DNA_curve_types.h"
53 #include "DNA_effect_types.h"
54 #include "DNA_gpencil_types.h"
55 #include "DNA_key_types.h"
56 #include "DNA_lamp_types.h"
57 #include "DNA_material_types.h"
58 #include "DNA_mask_types.h"
59 #include "DNA_mesh_types.h"
60 #include "DNA_meta_types.h"
61 #include "DNA_movieclip_types.h"
62 #include "DNA_node_types.h"
63 #include "DNA_particle_types.h"
64 #include "DNA_object_types.h"
65 #include "DNA_lightprobe_types.h"
66 #include "DNA_rigidbody_types.h"
67 #include "DNA_scene_types.h"
68 #include "DNA_speaker_types.h"
69 #include "DNA_texture_types.h"
70 #include "DNA_world_types.h"
71
72 #include "BKE_action.h"
73 #include "BKE_armature.h"
74 #include "BKE_animsys.h"
75 #include "BKE_collection.h"
76 #include "BKE_constraint.h"
77 #include "BKE_curve.h"
78 #include "BKE_effect.h"
79 #include "BKE_fcurve.h"
80 #include "BKE_gpencil.h"
81 #include "BKE_gpencil_modifier.h"
82 #include "BKE_idcode.h"
83 #include "BKE_key.h"
84 #include "BKE_lattice.h"
85 #include "BKE_mask.h"
86 #include "BKE_material.h"
87 #include "BKE_mesh.h"
88 #include "BKE_mball.h"
89 #include "BKE_modifier.h"
90 #include "BKE_movieclip.h"
91 #include "BKE_node.h"
92 #include "BKE_object.h"
93 #include "BKE_particle.h"
94 #include "BKE_pointcache.h"
95 #include "BKE_rigidbody.h"
96 #include "BKE_shader_fx.h"
97 #include "BKE_sound.h"
98 #include "BKE_tracking.h"
99 #include "BKE_world.h"
100
101 #include "RNA_access.h"
102 #include "RNA_types.h"
103 } /* extern "C" */
104
105 #include "DEG_depsgraph.h"
106 #include "DEG_depsgraph_build.h"
107
108 #include "intern/builder/deg_builder.h"
109 #include "intern/eval/deg_eval_copy_on_write.h"
110 #include "intern/nodes/deg_node.h"
111 #include "intern/nodes/deg_node_component.h"
112 #include "intern/nodes/deg_node_id.h"
113 #include "intern/nodes/deg_node_operation.h"
114 #include "intern/depsgraph_types.h"
115 #include "intern/depsgraph_intern.h"
116
117 #include "util/deg_util_foreach.h"
118
119 namespace DEG {
120
121 namespace {
122
123 void free_copy_on_write_datablock(void *id_info_v)
124 {
125         DepsgraphNodeBuilder::IDInfo *id_info =
126             (DepsgraphNodeBuilder::IDInfo *)id_info_v;
127         if (id_info->id_cow != NULL) {
128                 deg_free_copy_on_write_datablock(id_info->id_cow);
129                 MEM_freeN(id_info->id_cow);
130         }
131         MEM_freeN(id_info);
132 }
133
134 }  /* namespace */
135
136 /* ************ */
137 /* Node Builder */
138
139 /* **** General purpose functions **** */
140
141 DepsgraphNodeBuilder::DepsgraphNodeBuilder(Main *bmain, Depsgraph *graph)
142     : bmain_(bmain),
143       graph_(graph),
144       scene_(NULL),
145       view_layer_(NULL),
146       view_layer_index_(-1),
147       collection_(NULL),
148       is_parent_collection_visible_(true),
149       id_info_hash_(NULL)
150 {
151 }
152
153 DepsgraphNodeBuilder::~DepsgraphNodeBuilder()
154 {
155         if (id_info_hash_ != NULL) {
156                 BLI_ghash_free(id_info_hash_, NULL, free_copy_on_write_datablock);
157         }
158 }
159
160 IDDepsNode *DepsgraphNodeBuilder::add_id_node(ID *id)
161 {
162         IDDepsNode *id_node = NULL;
163         ID *id_cow = NULL;
164         IDComponentsMask previously_visible_components_mask = 0;
165         uint32_t previous_eval_flags = 0;
166         uint64_t previous_customdata_mask = 0;
167         IDInfo *id_info = (IDInfo *)BLI_ghash_lookup(id_info_hash_, id);
168         if (id_info != NULL) {
169                 id_cow = id_info->id_cow;
170                 previously_visible_components_mask =
171                         id_info->previously_visible_components_mask;
172                 previous_eval_flags = id_info->previous_eval_flags;
173                 previous_customdata_mask = id_info->previous_customdata_mask;
174                 /* Tag ID info to not free the CoW ID pointer. */
175                 id_info->id_cow = NULL;
176         }
177         id_node = graph_->add_id_node(id, id_cow);
178         id_node->previously_visible_components_mask =
179                 previously_visible_components_mask;
180         id_node->previous_eval_flags = previous_eval_flags;
181         id_node->previous_customdata_mask = previous_customdata_mask;
182         /* Currently all ID nodes are supposed to have copy-on-write logic.
183          *
184          * NOTE: Zero number of components indicates that ID node was just created.
185          */
186         if (BLI_ghash_len(id_node->components) == 0) {
187                 ComponentDepsNode *comp_cow =
188                         id_node->add_component(DEG_NODE_TYPE_COPY_ON_WRITE);
189                 OperationDepsNode *op_cow = comp_cow->add_operation(
190                         function_bind(deg_evaluate_copy_on_write, _1, id_node),
191                         DEG_OPCODE_COPY_ON_WRITE,
192                         "", -1);
193                 graph_->operations.push_back(op_cow);
194         }
195         return id_node;
196 }
197
198 IDDepsNode *DepsgraphNodeBuilder::find_id_node(ID *id)
199 {
200         return graph_->find_id_node(id);
201 }
202
203 TimeSourceDepsNode *DepsgraphNodeBuilder::add_time_source()
204 {
205         return graph_->add_time_source();
206 }
207
208 ComponentDepsNode *DepsgraphNodeBuilder::add_component_node(
209         ID *id,
210         eDepsNode_Type comp_type,
211         const char *comp_name)
212 {
213         IDDepsNode *id_node = add_id_node(id);
214         ComponentDepsNode *comp_node = id_node->add_component(comp_type, comp_name);
215         comp_node->owner = id_node;
216         return comp_node;
217 }
218
219 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
220         ComponentDepsNode *comp_node,
221         const DepsEvalOperationCb& op,
222         eDepsOperation_Code opcode,
223         const char *name,
224         int name_tag)
225 {
226         OperationDepsNode *op_node = comp_node->find_operation(opcode,
227                                                                name,
228                                                                name_tag);
229         if (op_node == NULL) {
230                 op_node = comp_node->add_operation(op, opcode, name, name_tag);
231                 graph_->operations.push_back(op_node);
232         }
233         else {
234                 fprintf(stderr,
235                         "add_operation: Operation already exists - %s has %s at %p\n",
236                         comp_node->identifier().c_str(),
237                         op_node->identifier().c_str(),
238                         op_node);
239                 BLI_assert(!"Should not happen!");
240         }
241         return op_node;
242 }
243
244 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
245         ID *id,
246         eDepsNode_Type comp_type,
247         const char *comp_name,
248         const DepsEvalOperationCb& op,
249         eDepsOperation_Code opcode,
250         const char *name,
251         int name_tag)
252 {
253         ComponentDepsNode *comp_node = add_component_node(id, comp_type, comp_name);
254         return add_operation_node(comp_node, op, opcode, name, name_tag);
255 }
256
257 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
258         ID *id,
259         eDepsNode_Type comp_type,
260         const DepsEvalOperationCb& op,
261         eDepsOperation_Code opcode,
262         const char *name,
263         int name_tag)
264 {
265         return add_operation_node(id,
266                                   comp_type,
267                                   "",
268                                   op,
269                                   opcode,
270                                   name,
271                                   name_tag);
272 }
273
274 OperationDepsNode *DepsgraphNodeBuilder::ensure_operation_node(
275         ID *id,
276         eDepsNode_Type comp_type,
277         const DepsEvalOperationCb& op,
278         eDepsOperation_Code opcode,
279         const char *name,
280         int name_tag)
281 {
282         OperationDepsNode *operation =
283                 find_operation_node(id, comp_type, opcode, name, name_tag);
284         if (operation != NULL) {
285                 return operation;
286         }
287         return add_operation_node(id, comp_type, op, opcode, name, name_tag);
288 }
289
290 bool DepsgraphNodeBuilder::has_operation_node(ID *id,
291                                               eDepsNode_Type comp_type,
292                                               const char *comp_name,
293                                               eDepsOperation_Code opcode,
294                                               const char *name,
295                                               int name_tag)
296 {
297         return find_operation_node(id,
298                                    comp_type,
299                                    comp_name,
300                                    opcode,
301                                    name,
302                                    name_tag) != NULL;
303 }
304
305 OperationDepsNode *DepsgraphNodeBuilder::find_operation_node(
306         ID *id,
307         eDepsNode_Type comp_type,
308         const char *comp_name,
309         eDepsOperation_Code opcode,
310         const char *name,
311         int name_tag)
312 {
313         ComponentDepsNode *comp_node = add_component_node(id, comp_type, comp_name);
314         return comp_node->find_operation(opcode, name, name_tag);
315 }
316
317 OperationDepsNode *DepsgraphNodeBuilder::find_operation_node(
318         ID *id,
319         eDepsNode_Type comp_type,
320         eDepsOperation_Code opcode,
321         const char *name,
322         int name_tag)
323 {
324         return find_operation_node(id, comp_type, "", opcode, name, name_tag);
325 }
326
327 ID *DepsgraphNodeBuilder::get_cow_id(const ID *id_orig) const
328 {
329         return graph_->get_cow_id(id_orig);
330 }
331
332 ID *DepsgraphNodeBuilder::ensure_cow_id(ID *id_orig)
333 {
334         if (id_orig->tag & LIB_TAG_COPIED_ON_WRITE) {
335                 /* ID is already remapped to copy-on-write. */
336                 return id_orig;
337         }
338         IDDepsNode *id_node = add_id_node(id_orig);
339         return id_node->id_cow;
340 }
341
342 /* **** Build functions for entity nodes **** */
343
344 void DepsgraphNodeBuilder::begin_build()
345 {
346         /* Store existing copy-on-write versions of datablock, so we can re-use
347          * them for new ID nodes.
348          */
349         id_info_hash_ = BLI_ghash_ptr_new("Depsgraph id hash");
350         foreach (IDDepsNode *id_node, graph_->id_nodes) {
351                 IDInfo *id_info = (IDInfo *)MEM_mallocN(
352                         sizeof(IDInfo), "depsgraph id info");
353                 if (deg_copy_on_write_is_expanded(id_node->id_cow) &&
354                     id_node->id_orig != id_node->id_cow)
355                 {
356                         id_info->id_cow = id_node->id_cow;
357                 }
358                 else {
359                         id_info->id_cow = NULL;
360                 }
361                 id_info->previously_visible_components_mask =
362                         id_node->visible_components_mask;
363                 id_info->previous_eval_flags = id_node->eval_flags;
364                 id_info->previous_customdata_mask = id_node->customdata_mask;
365                 BLI_ghash_insert(id_info_hash_, id_node->id_orig, id_info);
366                 id_node->id_cow = NULL;
367         }
368
369         GSET_FOREACH_BEGIN(OperationDepsNode *, op_node, graph_->entry_tags)
370         {
371                 ComponentDepsNode *comp_node = op_node->owner;
372                 IDDepsNode *id_node = comp_node->owner;
373
374                 SavedEntryTag entry_tag;
375                 entry_tag.id_orig = id_node->id_orig;
376                 entry_tag.component_type = comp_node->type;
377                 entry_tag.opcode = op_node->opcode;
378                 entry_tag.name = op_node->name;
379                 entry_tag.name_tag = op_node->name_tag;
380                 saved_entry_tags_.push_back(entry_tag);
381         };
382         GSET_FOREACH_END();
383
384         /* Make sure graph has no nodes left from previous state. */
385         graph_->clear_all_nodes();
386         graph_->operations.clear();
387         BLI_gset_clear(graph_->entry_tags, NULL);
388 }
389
390 void DepsgraphNodeBuilder::end_build()
391 {
392         foreach (const SavedEntryTag& entry_tag, saved_entry_tags_) {
393                 IDDepsNode *id_node = find_id_node(entry_tag.id_orig);
394                 if (id_node == NULL) {
395                         continue;
396                 }
397                 ComponentDepsNode *comp_node =
398                         id_node->find_component(entry_tag.component_type);
399                 if (comp_node == NULL) {
400                         continue;
401                 }
402                 OperationDepsNode *op_node = comp_node->find_operation(entry_tag.opcode, entry_tag.name, entry_tag.name_tag);
403                 if (op_node == NULL) {
404                         continue;
405                 }
406                 /* Since the tag is coming from a saved copy of entry tags, this means
407                  * that originally node was explicitly tagged for user update.
408                  */
409                 op_node->tag_update(graph_, DEG_UPDATE_SOURCE_USER_EDIT);
410         }
411 }
412
413 void DepsgraphNodeBuilder::build_id(ID *id)
414 {
415         if (id == NULL) {
416                 return;
417         }
418         switch (GS(id->name)) {
419                 case ID_AC:
420                         build_action((bAction *)id);
421                         break;
422                 case ID_AR:
423                         build_armature((bArmature *)id);
424                         break;
425                 case ID_CA:
426                         build_camera((Camera *)id);
427                         break;
428                 case ID_GR:
429                         build_collection(NULL, (Collection *)id);
430                         break;
431                 case ID_OB:
432                         /* TODO(sergey): Get visibility from a "parent" somehow.
433                          *
434                          * NOTE: Using `false` visibility here should be fine, since if this
435                          * driver affects on something invisible we don't really care if the
436                          * driver gets evaluated (and even don't want this to force object
437                          * to become visible).
438                          *
439                          * If this happened to be affecting visible object, then it is up to
440                          * deg_graph_build_flush_visibility() to ensure visibility of the
441                          * object is true.
442                          */
443                         build_object(-1, (Object *)id, DEG_ID_LINKED_INDIRECTLY, false);
444                         break;
445                 case ID_KE:
446                         build_shapekeys((Key *)id);
447                         break;
448                 case ID_LA:
449                         build_lamp((Lamp *)id);
450                         break;
451                 case ID_LP:
452                         build_lightprobe((LightProbe *)id);
453                         break;
454                 case ID_NT:
455                         build_nodetree((bNodeTree *)id);
456                         break;
457                 case ID_MA:
458                         build_material((Material *)id);
459                         break;
460                 case ID_TE:
461                         build_texture((Tex *)id);
462                         break;
463                 case ID_IM:
464                         build_image((Image *)id);
465                         break;
466                 case ID_WO:
467                         build_world((World *)id);
468                         break;
469                 case ID_MSK:
470                         build_mask((Mask *)id);
471                         break;
472                 case ID_MC:
473                         build_movieclip((MovieClip *)id);
474                         break;
475                 case ID_ME:
476                 case ID_CU:
477                 case ID_MB:
478                 case ID_LT:
479                         /* TODO(sergey): Get visibility from a "parent" somehow.
480                          *
481                          * NOTE: Similarly to above, we don't want false-positives on
482                          * visibility.
483                          */
484                         build_object_data_geometry_datablock(id, false);
485                         break;
486                 case ID_SPK:
487                         build_speaker((Speaker *)id);
488                         break;
489                 case ID_TXT:
490                         /* Not a part of dependency graph. */
491                         break;
492                 case ID_CF:
493                         build_cachefile((CacheFile *)id);
494                         break;
495                 default:
496                         fprintf(stderr, "Unhandled ID %s\n", id->name);
497                         BLI_assert(!"Should never happen");
498                         break;
499         }
500 }
501
502 void DepsgraphNodeBuilder::build_collection(
503         LayerCollection *from_layer_collection,
504         Collection *collection)
505 {
506         const int restrict_flag = (graph_->mode == DAG_EVAL_VIEWPORT)
507                 ? COLLECTION_RESTRICT_VIEW
508                 : COLLECTION_RESTRICT_RENDER;
509         const bool is_collection_restricted = (collection->flag & restrict_flag);
510         const bool is_collection_visible =
511                 !is_collection_restricted && is_parent_collection_visible_;
512         IDDepsNode *id_node;
513         if (built_map_.checkIsBuiltAndTag(collection)) {
514                 id_node = find_id_node(&collection->id);
515                 if (is_collection_visible &&
516                     id_node->is_directly_visible == false &&
517                     id_node->is_collection_fully_expanded == true)
518                 {
519                         /* Collection became visible, make sure nested collections and
520                          * objects are poked with the new visibility flag, since they
521                          * might become visible too. */
522                 }
523                 else {
524                         return;
525                 }
526         }
527         else {
528                 /* Collection itself. */
529                 id_node = add_id_node(&collection->id);
530                 id_node->is_directly_visible = is_collection_visible;
531         }
532         if (from_layer_collection != NULL) {
533                 /* If we came from layer collection we don't go deeper, view layer
534                  * builder takes care of going deeper. */
535                 return;
536         }
537         /* Backup state. */
538         Collection *current_state_collection = collection_;
539         const bool is_current_parent_collection_visible =
540                 is_parent_collection_visible_;
541         /* Modify state as we've entered new collection/ */
542         collection_ = collection;
543         is_parent_collection_visible_ = is_collection_visible;
544         /* Build collection objects. */
545         LISTBASE_FOREACH (CollectionObject *, cob, &collection->gobject) {
546                 build_object(
547                         -1, cob->ob, DEG_ID_LINKED_INDIRECTLY, is_collection_visible);
548         }
549         /* Build child collections. */
550         LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
551                 build_collection(NULL, child->collection);
552         }
553         /* Restore state. */
554         collection_ = current_state_collection;
555         is_parent_collection_visible_ = is_current_parent_collection_visible;
556         id_node->is_collection_fully_expanded = true;
557 }
558
559 void DepsgraphNodeBuilder::build_object(int base_index,
560                                         Object *object,
561                                         eDepsNode_LinkedState_Type linked_state,
562                                         bool is_visible)
563 {
564         const bool has_object = built_map_.checkIsBuiltAndTag(object);
565         /* Skip rest of components if the ID node was already there. */
566         if (has_object) {
567                 IDDepsNode *id_node = find_id_node(&object->id);
568                 /* We need to build some extra stuff if object becomes linked
569                  * directly.
570                  */
571                 if (id_node->linked_state == DEG_ID_LINKED_INDIRECTLY) {
572                         build_object_flags(base_index, object, linked_state);
573                 }
574                 id_node->linked_state = max(id_node->linked_state, linked_state);
575                 if (id_node->linked_state == DEG_ID_LINKED_DIRECTLY) {
576                         id_node->is_directly_visible |= is_visible;
577                 }
578                 return;
579         }
580         /* Create ID node for object and begin init. */
581         IDDepsNode *id_node = add_id_node(&object->id);
582         Object *object_cow = get_cow_datablock(object);
583         id_node->linked_state = linked_state;
584         if (object == scene_->camera) {
585                 id_node->is_directly_visible = true;
586         }
587         else {
588                 id_node->is_directly_visible = is_visible;
589         }
590         /* Various flags, flushing from bases/collections. */
591         build_object_flags(base_index, object, linked_state);
592         /* Transform. */
593         build_object_transform(object);
594         /* Parent. */
595         if (object->parent != NULL) {
596                 build_object(
597                         -1, object->parent, DEG_ID_LINKED_INDIRECTLY, is_visible);
598         }
599         /* Modifiers. */
600         if (object->modifiers.first != NULL) {
601                 BuilderWalkUserData data;
602                 data.builder = this;
603                 data.is_parent_visible = is_visible;
604                 modifiers_foreachIDLink(object, modifier_walk, &data);
605         }
606         /* Grease Pencil Modifiers. */
607         if (object->greasepencil_modifiers.first != NULL) {
608                 BuilderWalkUserData data;
609                 data.builder = this;
610                 data.is_parent_visible = is_visible;
611                 BKE_gpencil_modifiers_foreachIDLink(object, modifier_walk, &data);
612         }
613         /* Shader FX. */
614         if (object->shader_fx.first != NULL) {
615                 BuilderWalkUserData data;
616                 data.builder = this;
617                 data.is_parent_visible = is_visible;
618                 BKE_shaderfx_foreachIDLink(object, modifier_walk, &data);
619         }
620         /* Constraints. */
621         if (object->constraints.first != NULL) {
622                 BuilderWalkUserData data;
623                 data.builder = this;
624                 data.is_parent_visible = is_visible;
625                 BKE_constraints_id_loop(&object->constraints, constraint_walk, &data);
626         }
627         /* Object data. */
628         build_object_data(object, is_visible);
629         /* Build animation data,
630          *
631          * Do it now because it's possible object data will affect
632          * on object's level animation, for example in case of rebuilding
633          * pose for proxy.
634          */
635         OperationDepsNode *op_node = add_operation_node(&object->id,
636                                                         DEG_NODE_TYPE_PARAMETERS,
637                                                         NULL,
638                                                         DEG_OPCODE_PARAMETERS_EVAL);
639         op_node->set_as_exit();
640         build_animdata(&object->id);
641         /* Particle systems. */
642         if (object->particlesystem.first != NULL) {
643                 build_particles(object, is_visible);
644         }
645         /* Proxy object to copy from. */
646         if (object->proxy_from != NULL) {
647                 build_object(
648                         -1, object->proxy_from, DEG_ID_LINKED_INDIRECTLY, is_visible);
649         }
650         if (object->proxy_group != NULL) {
651                 build_object(
652                         -1, object->proxy_group, DEG_ID_LINKED_INDIRECTLY, is_visible);
653         }
654         /* Object dupligroup. */
655         if (object->dup_group != NULL) {
656                 const bool is_current_parent_collection_visible =
657                         is_parent_collection_visible_;
658                 is_parent_collection_visible_ = is_visible;
659                 build_collection(NULL, object->dup_group);
660                 is_parent_collection_visible_ = is_current_parent_collection_visible;
661                 add_operation_node(&object->id,
662                                    DEG_NODE_TYPE_DUPLI,
663                                    NULL,
664                                    DEG_OPCODE_PLACEHOLDER,
665                                    "Dupli");
666         }
667         /* Syncronization back to original object. */
668         add_operation_node(&object->id,
669                            DEG_NODE_TYPE_SYNCHRONIZE,
670                            function_bind(BKE_object_synchronize_to_original,
671                                          _1,
672                                          object_cow),
673                            DEG_OPCODE_SYNCHRONIZE_TO_ORIGINAL);
674 }
675
676 void DepsgraphNodeBuilder::build_object_flags(
677         int base_index,
678         Object *object,
679         eDepsNode_LinkedState_Type linked_state)
680 {
681         if (base_index == -1) {
682                 return;
683         }
684         Scene *scene_cow = get_cow_datablock(scene_);
685         Object *object_cow = get_cow_datablock(object);
686         const bool is_from_set = (linked_state == DEG_ID_LINKED_VIA_SET);
687         /* TODO(sergey): Is this really best component to be used? */
688         add_operation_node(&object->id,
689                            DEG_NODE_TYPE_OBJECT_FROM_LAYER,
690                            function_bind(BKE_object_eval_flush_base_flags,
691                                          _1,
692                                          scene_cow,
693                                          view_layer_index_,
694                                          object_cow, base_index,
695                                          is_from_set),
696                            DEG_OPCODE_OBJECT_BASE_FLAGS);
697 }
698
699 void DepsgraphNodeBuilder::build_object_data(
700         Object *object, bool is_object_visible)
701 {
702         if (object->data == NULL) {
703                 return;
704         }
705         /* type-specific data. */
706         switch (object->type) {
707                 case OB_MESH:
708                 case OB_CURVE:
709                 case OB_FONT:
710                 case OB_SURF:
711                 case OB_MBALL:
712                 case OB_LATTICE:
713                 case OB_GPENCIL:
714                         build_object_data_geometry(object, is_object_visible);
715                         break;
716                 case OB_ARMATURE:
717                         if (ID_IS_LINKED(object) && object->proxy_from != NULL) {
718                                 build_proxy_rig(object);
719                         }
720                         else {
721                                 build_rig(object, is_object_visible);
722                         }
723                         break;
724                 case OB_LAMP:
725                         build_object_data_lamp(object);
726                         break;
727                 case OB_CAMERA:
728                         build_object_data_camera(object);
729                         break;
730                 case OB_LIGHTPROBE:
731                         build_object_data_lightprobe(object);
732                         break;
733                 case OB_SPEAKER:
734                         build_object_data_speaker(object);
735                         break;
736                 default:
737                 {
738                         ID *obdata = (ID *)object->data;
739                         if (built_map_.checkIsBuilt(obdata) == 0) {
740                                 build_animdata(obdata);
741                         }
742                         break;
743                 }
744         }
745 }
746
747 void DepsgraphNodeBuilder::build_object_data_camera(Object *object)
748 {
749         Camera *camera = (Camera *)object->data;
750         build_camera(camera);
751 }
752
753 void DepsgraphNodeBuilder::build_object_data_lamp(Object *object)
754 {
755         Lamp *lamp = (Lamp *)object->data;
756         build_lamp(lamp);
757 }
758
759 void DepsgraphNodeBuilder::build_object_data_lightprobe(Object *object)
760 {
761         LightProbe *probe = (LightProbe *)object->data;
762         build_lightprobe(probe);
763         add_operation_node(&object->id,
764                            DEG_NODE_TYPE_PARAMETERS,
765                            NULL,
766                            DEG_OPCODE_LIGHT_PROBE_EVAL);
767 }
768
769 void DepsgraphNodeBuilder::build_object_data_speaker(Object *object)
770 {
771         Speaker *speaker = (Speaker *)object->data;
772         build_speaker(speaker);
773         add_operation_node(&object->id,
774                            DEG_NODE_TYPE_PARAMETERS,
775                            NULL,
776                            DEG_OPCODE_SPEAKER_EVAL);
777 }
778
779 void DepsgraphNodeBuilder::build_object_transform(Object *object)
780 {
781         OperationDepsNode *op_node;
782         Scene *scene_cow = get_cow_datablock(scene_);
783         Object *ob_cow = get_cow_datablock(object);
784
785         /* local transforms (from transform channels - loc/rot/scale + deltas) */
786         op_node = add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
787                                      function_bind(BKE_object_eval_local_transform,
788                                                    _1,
789                                                    ob_cow),
790                                      DEG_OPCODE_TRANSFORM_LOCAL);
791         op_node->set_as_entry();
792
793         /* object parent */
794         if (object->parent != NULL) {
795                 add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
796                                    function_bind(BKE_object_eval_parent,
797                                                  _1,
798                                                  scene_cow,
799                                                  ob_cow),
800                                    DEG_OPCODE_TRANSFORM_PARENT);
801         }
802
803         /* object constraints */
804         if (object->constraints.first != NULL) {
805                 build_object_constraints(object);
806         }
807
808         /* Rest of transformation update. */
809         add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
810                            function_bind(BKE_object_eval_uber_transform,
811                                          _1,
812                                          ob_cow),
813                            DEG_OPCODE_TRANSFORM_OBJECT_UBEREVAL);
814
815         /* object transform is done */
816         op_node = add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
817                                      function_bind(BKE_object_eval_transform_final,
818                                                    _1,
819                                                    ob_cow),
820                                      DEG_OPCODE_TRANSFORM_FINAL);
821         op_node->set_as_exit();
822 }
823
824 /**
825  * Constraints Graph Notes
826  *
827  * For constraints, we currently only add a operation node to the Transform
828  * or Bone components (depending on whichever type of owner we have).
829  * This represents the entire constraints stack, which is for now just
830  * executed as a single monolithic block. At least initially, this should
831  * be sufficient for ensuring that the porting/refactoring process remains
832  * manageable.
833  *
834  * However, when the time comes for developing "node-based" constraints,
835  * we'll need to split this up into pre/post nodes for "constraint stack
836  * evaluation" + operation nodes for each constraint (i.e. the contents
837  * of the loop body used in the current "solve_constraints()" operation).
838  *
839  * -- Aligorith, August 2013
840  */
841 void DepsgraphNodeBuilder::build_object_constraints(Object *object)
842 {
843         /* create node for constraint stack */
844         add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
845                            function_bind(BKE_object_eval_constraints,
846                                          _1,
847                                          get_cow_datablock(scene_),
848                                          get_cow_datablock(object)),
849                            DEG_OPCODE_TRANSFORM_CONSTRAINTS);
850 }
851
852 void DepsgraphNodeBuilder::build_object_pointcache(Object *object)
853 {
854         if (!BKE_ptcache_object_has(scene_, object, 0)) {
855                 return;
856         }
857         Scene *scene_cow = get_cow_datablock(scene_);
858         Object *object_cow = get_cow_datablock(object);
859         add_operation_node(&object->id,
860                            DEG_NODE_TYPE_POINT_CACHE,
861                            function_bind(BKE_object_eval_ptcache_reset,
862                                          _1,
863                                          scene_cow,
864                                          object_cow),
865                            DEG_OPCODE_POINT_CACHE_RESET);
866 }
867
868 /**
869  * Build graph nodes for AnimData block
870  * \param id: ID-Block which hosts the AnimData
871  */
872 void DepsgraphNodeBuilder::build_animdata(ID *id)
873 {
874         AnimData *adt = BKE_animdata_from_id(id);
875         if (adt == NULL) {
876                 return;
877         }
878         if (adt->action != NULL) {
879                 build_action(adt->action);
880         }
881         /* animation */
882         if (adt->action || adt->nla_tracks.first || adt->drivers.first) {
883                 (void) add_id_node(id);
884                 ID *id_cow = get_cow_id(id);
885
886                 // XXX: Hook up specific update callbacks for special properties which
887                 // may need it...
888
889                 /* actions and NLA - as a single unit for now, as it gets complicated to
890                  * schedule otherwise.
891                  */
892                 if ((adt->action) || (adt->nla_tracks.first)) {
893                         /* create the node */
894                         add_operation_node(id, DEG_NODE_TYPE_ANIMATION,
895                                            function_bind(BKE_animsys_eval_animdata,
896                                                          _1,
897                                                          id_cow),
898                                            DEG_OPCODE_ANIMATION,
899                                            id->name);
900
901                         /* TODO: for each channel affected, we might also want to add some
902                          * support for running RNA update callbacks on them
903                          * (which will be needed for proper handling of drivers later)
904                          */
905                 }
906
907                 /* NLA strips contain actions */
908                 LISTBASE_FOREACH (NlaTrack *, nlt, &adt->nla_tracks) {
909                         build_animdata_nlastrip_targets(&nlt->strips);
910                 }
911
912                 /* drivers */
913                 int driver_index = 0;
914                 LISTBASE_FOREACH (FCurve *, fcu, &adt->drivers) {
915                         /* create driver */
916                         build_driver(id, fcu, driver_index++);
917                 }
918         }
919 }
920
921 void DepsgraphNodeBuilder::build_animdata_nlastrip_targets(ListBase *strips)
922 {
923         LISTBASE_FOREACH (NlaStrip *, strip, strips) {
924                 if (strip->act != NULL) {
925                         build_action(strip->act);
926                 }
927                 else if (strip->strips.first != NULL) {
928                         build_animdata_nlastrip_targets(&strip->strips);
929                 }
930         }
931 }
932
933 void DepsgraphNodeBuilder::build_action(bAction *action)
934 {
935         if (built_map_.checkIsBuiltAndTag(action)) {
936                 return;
937         }
938         add_operation_node(&action->id,
939                            DEG_NODE_TYPE_ANIMATION,
940                            NULL,
941                            DEG_OPCODE_ANIMATION);
942 }
943
944 /**
945  * Build graph node(s) for Driver
946  * \param id: ID-Block that driver is attached to
947  * \param fcu: Driver-FCurve
948  * \param driver_index: Index in animation data drivers list
949  */
950 void DepsgraphNodeBuilder::build_driver(ID *id, FCurve *fcurve, int driver_index)
951 {
952         /* Create data node for this driver */
953         ID *id_cow = get_cow_id(id);
954         ChannelDriver *driver_orig = fcurve->driver;
955
956         /* TODO(sergey): ideally we could pass the COW of fcu, but since it
957          * has not yet been allocated at this point we can't. As a workaround
958          * the animation systems allocates an array so we can do a fast lookup
959          * with the driver index. */
960         ensure_operation_node(id,
961                               DEG_NODE_TYPE_PARAMETERS,
962                               function_bind(BKE_animsys_eval_driver, _1, id_cow, driver_index, driver_orig),
963                               DEG_OPCODE_DRIVER,
964                               fcurve->rna_path ? fcurve->rna_path : "",
965                               fcurve->array_index);
966         build_driver_variables(id, fcurve);
967 }
968
969 void DepsgraphNodeBuilder::build_driver_variables(ID * id, FCurve *fcurve)
970 {
971         build_driver_id_property(id, fcurve->rna_path);
972         LISTBASE_FOREACH (DriverVar *, dvar, &fcurve->driver->variables) {
973                 DRIVER_TARGETS_USED_LOOPER_BEGIN(dvar)
974                 {
975                         if (dtar->id == NULL) {
976                                 continue;
977                         }
978                         build_id(dtar->id);
979                         build_driver_id_property(dtar->id, dtar->rna_path);
980                         /* Corresponds to dtar_id_ensure_proxy_from(). */
981                         if ((GS(dtar->id->name) == ID_OB) &&
982                             (((Object *)dtar->id)->proxy_from != NULL))
983                         {
984                                 Object *proxy_from = ((Object *)dtar->id)->proxy_from;
985                                 build_id(&proxy_from->id);
986                                 build_driver_id_property(&proxy_from->id, dtar->rna_path);
987                         }
988                 }
989                 DRIVER_TARGETS_LOOPER_END;
990         }
991 }
992
993 void DepsgraphNodeBuilder::build_driver_id_property(ID *id,
994                                                     const char *rna_path)
995 {
996         if (id == NULL || rna_path == NULL) {
997                 return;
998         }
999         PointerRNA id_ptr, ptr;
1000         PropertyRNA *prop;
1001         RNA_id_pointer_create(id, &id_ptr);
1002         if (!RNA_path_resolve_full(&id_ptr, rna_path, &ptr, &prop, NULL)) {
1003                 return;
1004         }
1005         if (prop == NULL) {
1006                 return;
1007         }
1008         if (!RNA_property_is_idprop(prop)) {
1009                 return;
1010         }
1011         const char *prop_identifier = RNA_property_identifier((PropertyRNA *)prop);
1012         ensure_operation_node(id,
1013                               DEG_NODE_TYPE_PARAMETERS,
1014                               NULL,
1015                               DEG_OPCODE_ID_PROPERTY,
1016                               prop_identifier);
1017 }
1018
1019 /* Recursively build graph for world */
1020 void DepsgraphNodeBuilder::build_world(World *world)
1021 {
1022         if (built_map_.checkIsBuiltAndTag(world)) {
1023                 return;
1024         }
1025         /* World itself. */
1026         add_id_node(&world->id);
1027         World *world_cow = get_cow_datablock(world);
1028         /* Shading update. */
1029         add_operation_node(&world->id,
1030                            DEG_NODE_TYPE_SHADING,
1031                            function_bind(BKE_world_eval,
1032                                          _1,
1033                                          world_cow),
1034                            DEG_OPCODE_WORLD_UPDATE);
1035         /* Animation. */
1036         build_animdata(&world->id);
1037         /* World's nodetree. */
1038         build_nodetree(world->nodetree);
1039 }
1040
1041 /* Rigidbody Simulation - Scene Level */
1042 void DepsgraphNodeBuilder::build_rigidbody(Scene *scene)
1043 {
1044         RigidBodyWorld *rbw = scene->rigidbody_world;
1045         Scene *scene_cow = get_cow_datablock(scene);
1046
1047         /**
1048          * Rigidbody Simulation Nodes
1049          * ==========================
1050          *
1051          * There are 3 nodes related to Rigidbody Simulation:
1052          * 1) "Initialize/Rebuild World" - this is called sparingly, only when the
1053          *    simulation needs to be rebuilt (mainly after file reload, or moving
1054          *    back to start frame)
1055          * 2) "Do Simulation" - perform a simulation step - interleaved between the
1056          *    evaluation steps for clusters of objects (i.e. between those affected
1057          *    and/or not affected by the sim for instance).
1058          *
1059          * 3) "Pull Results" - grab the specific transforms applied for a specific
1060          *    object - performed as part of object's transform-stack building.
1061          */
1062
1063         /* Create nodes --------------------------------------------------------- */
1064
1065         /* XXX: is this the right component, or do we want to use another one
1066          * instead?
1067          */
1068
1069         /* init/rebuild operation */
1070         /*OperationDepsNode *init_node =*/ add_operation_node(
1071                 &scene->id, DEG_NODE_TYPE_TRANSFORM,
1072                 function_bind(BKE_rigidbody_rebuild_sim, _1, scene_cow),
1073                 DEG_OPCODE_RIGIDBODY_REBUILD);
1074
1075         /* do-sim operation */
1076         // XXX: what happens if we need to split into several groups?
1077         OperationDepsNode *sim_node = add_operation_node(
1078                 &scene->id, DEG_NODE_TYPE_TRANSFORM,
1079                 function_bind(BKE_rigidbody_eval_simulation, _1, scene_cow),
1080                 DEG_OPCODE_RIGIDBODY_SIM);
1081
1082         /* XXX: For now, the sim node is the only one that really matters here.
1083          * If any other sims get added later, we may have to remove these hacks...
1084          */
1085         sim_node->owner->entry_operation = sim_node;
1086         sim_node->owner->exit_operation  = sim_node;
1087
1088         /* objects - simulation participants */
1089         if (rbw->group) {
1090                 build_collection(NULL, rbw->group);
1091
1092                 FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(rbw->group, object)
1093                 {
1094                         if (object->type != OB_MESH)
1095                                 continue;
1096
1097                         /* 2) create operation for flushing results */
1098                         /* object's transform component - where the rigidbody operation
1099                          * lives. */
1100                         add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
1101                                            function_bind(
1102                                                    BKE_rigidbody_object_sync_transforms,
1103                                                    _1,
1104                                                    scene_cow,
1105                                                    get_cow_datablock(object)),
1106                                            DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
1107                 }
1108                 FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
1109         }
1110 }
1111
1112 void DepsgraphNodeBuilder::build_particles(Object *object,
1113                                            bool is_object_visible)
1114 {
1115         /**
1116          * Particle Systems Nodes
1117          * ======================
1118          *
1119          * There are two types of nodes associated with representing
1120          * particle systems:
1121          *  1) Component (EVAL_PARTICLES) - This is the particle-system
1122          *     evaluation context for an object. It acts as the container
1123          *     for all the nodes associated with a particular set of particle
1124          *     systems.
1125          *  2) Particle System Eval Operation - This operation node acts as a
1126          *     blackbox evaluation step for one particle system referenced by
1127          *     the particle systems stack. All dependencies link to this operation.
1128          */
1129         /* Component for all particle systems. */
1130         ComponentDepsNode *psys_comp =
1131                 add_component_node(&object->id, DEG_NODE_TYPE_EVAL_PARTICLES);
1132
1133         /* TODO(sergey): Need to get COW of PSYS. */
1134         Scene *scene_cow = get_cow_datablock(scene_);
1135         Object *ob_cow = get_cow_datablock(object);
1136
1137         add_operation_node(psys_comp,
1138                            function_bind(BKE_particle_system_eval_init,
1139                                          _1,
1140                                          scene_cow,
1141                                          ob_cow),
1142                            DEG_OPCODE_PARTICLE_SYSTEM_EVAL_INIT);
1143         /* Build all particle systems. */
1144         LISTBASE_FOREACH (ParticleSystem *, psys, &object->particlesystem) {
1145                 ParticleSettings *part = psys->part;
1146                 /* Build particle settings operations.
1147                  *
1148                  * NOTE: The call itself ensures settings are only build once.
1149                  */
1150                 build_particle_settings(part);
1151                 /* Particle system evaluation. */
1152                 add_operation_node(psys_comp,
1153                                    NULL,
1154                                    DEG_OPCODE_PARTICLE_SYSTEM_EVAL,
1155                                    psys->name);
1156                 /* Visualization of particle system. */
1157                 switch (part->ren_as) {
1158                         case PART_DRAW_OB:
1159                                 if (part->dup_ob != NULL) {
1160                                         build_object(-1,
1161                                                      part->dup_ob,
1162                                                      DEG_ID_LINKED_INDIRECTLY,
1163                                                      is_object_visible);
1164                                 }
1165                                 break;
1166                         case PART_DRAW_GR:
1167                                 if (part->dup_group != NULL) {
1168                                         build_collection(NULL, part->dup_group);
1169                                 }
1170                                 break;
1171                 }
1172         }
1173 }
1174
1175 void DepsgraphNodeBuilder::build_particle_settings(ParticleSettings *part) {
1176         if (built_map_.checkIsBuiltAndTag(part)) {
1177                 return;
1178         }
1179         /* Animation data. */
1180         build_animdata(&part->id);
1181         /* Parameters change. */
1182         add_operation_node(&part->id,
1183                            DEG_NODE_TYPE_PARAMETERS,
1184                            NULL,
1185                            DEG_OPCODE_PARTICLE_SETTINGS_EVAL);
1186 }
1187
1188 /* Shapekeys */
1189 void DepsgraphNodeBuilder::build_shapekeys(Key *key)
1190 {
1191         if (built_map_.checkIsBuiltAndTag(key)) {
1192                 return;
1193         }
1194         build_animdata(&key->id);
1195         add_operation_node(&key->id,
1196                            DEG_NODE_TYPE_GEOMETRY,
1197                            NULL,
1198                            DEG_OPCODE_GEOMETRY_SHAPEKEY);
1199 }
1200
1201 /* ObData Geometry Evaluation */
1202 // XXX: what happens if the datablock is shared!
1203 void DepsgraphNodeBuilder::build_object_data_geometry(
1204         Object *object,
1205         bool is_object_visible)
1206 {
1207         OperationDepsNode *op_node;
1208         Scene *scene_cow = get_cow_datablock(scene_);
1209         Object *object_cow = get_cow_datablock(object);
1210         /* Temporary uber-update node, which does everything.
1211          * It is for the being we're porting old dependencies into the new system.
1212          * We'll get rid of this node as soon as all the granular update functions
1213          * are filled in.
1214          *
1215          * TODO(sergey): Get rid of this node.
1216          */
1217         op_node = add_operation_node(&object->id,
1218                                      DEG_NODE_TYPE_GEOMETRY,
1219                                      function_bind(BKE_object_eval_uber_data,
1220                                                    _1,
1221                                                    scene_cow,
1222                                                    object_cow),
1223                                      DEG_OPCODE_GEOMETRY_UBEREVAL);
1224         op_node->set_as_exit();
1225
1226         op_node = add_operation_node(&object->id,
1227                                      DEG_NODE_TYPE_GEOMETRY,
1228                                      NULL,
1229                                      DEG_OPCODE_PLACEHOLDER,
1230                                      "Eval Init");
1231         op_node->set_as_entry();
1232         /* Materials. */
1233         if (object->totcol != 0) {
1234                 if (object->type == OB_MESH) {
1235                         add_operation_node(&object->id,
1236                                            DEG_NODE_TYPE_SHADING,
1237                                            function_bind(BKE_object_eval_update_shading,
1238                                                          _1,
1239                                                          object_cow),
1240                                            DEG_OPCODE_SHADING);
1241                 }
1242                 for (int a = 1; a <= object->totcol; a++) {
1243                         Material *ma = give_current_material(object, a);
1244                         if (ma != NULL) {
1245                                 build_material(ma);
1246                         }
1247                 }
1248         }
1249         /* Point caches. */
1250         build_object_pointcache(object);
1251         /* Geometry. */
1252         build_object_data_geometry_datablock((ID *)object->data, is_object_visible);
1253 }
1254
1255 void DepsgraphNodeBuilder::build_object_data_geometry_datablock(
1256         ID *obdata,
1257         bool is_object_visible)
1258 {
1259         if (built_map_.checkIsBuiltAndTag(obdata)) {
1260                 return;
1261         }
1262         OperationDepsNode *op_node;
1263         /* Make sure we've got an ID node before requesting CoW pointer. */
1264         (void) add_id_node((ID *)obdata);
1265         ID *obdata_cow = get_cow_id(obdata);
1266         /* Animation. */
1267         build_animdata(obdata);
1268         /* ShapeKeys */
1269         Key *key = BKE_key_from_id(obdata);
1270         if (key) {
1271                 build_shapekeys(key);
1272         }
1273         /* Nodes for result of obdata's evaluation, and geometry
1274          * evaluation on object.
1275          */
1276         const ID_Type id_type = GS(obdata->name);
1277         switch (id_type) {
1278                 case ID_ME:
1279                 {
1280                         op_node = add_operation_node(obdata,
1281                                                      DEG_NODE_TYPE_GEOMETRY,
1282                                                      function_bind(BKE_mesh_eval_geometry,
1283                                                                    _1,
1284                                                                    (Mesh *)obdata_cow),
1285                                                      DEG_OPCODE_PLACEHOLDER,
1286                                                      "Geometry Eval");
1287                         op_node->set_as_entry();
1288                         break;
1289                 }
1290                 case ID_MB:
1291                 {
1292                         op_node = add_operation_node(obdata,
1293                                                      DEG_NODE_TYPE_GEOMETRY,
1294                                                      NULL,
1295                                                      DEG_OPCODE_PLACEHOLDER,
1296                                                      "Geometry Eval");
1297                         op_node->set_as_entry();
1298                         break;
1299                 }
1300                 case ID_CU:
1301                 {
1302                         op_node = add_operation_node(obdata,
1303                                                      DEG_NODE_TYPE_GEOMETRY,
1304                                                      function_bind(BKE_curve_eval_geometry,
1305                                                                    _1,
1306                                                                    (Curve *)obdata_cow),
1307                                                                    DEG_OPCODE_PLACEHOLDER,
1308                                                                    "Geometry Eval");
1309                         op_node->set_as_entry();
1310                         /* Make sure objects used for bevel.taper are in the graph.
1311                          * NOTE: This objects might be not linked to the scene.
1312                          */
1313                         Curve *cu = (Curve *)obdata;
1314                         if (cu->bevobj != NULL) {
1315                                 build_object(-1,
1316                                              cu->bevobj,
1317                                              DEG_ID_LINKED_INDIRECTLY,
1318                                              is_object_visible);
1319                         }
1320                         if (cu->taperobj != NULL) {
1321                                 build_object(-1,
1322                                              cu->taperobj,
1323                                              DEG_ID_LINKED_INDIRECTLY,
1324                                              is_object_visible);
1325                         }
1326                         if (cu->textoncurve != NULL) {
1327                                 build_object(-1,
1328                                              cu->textoncurve,
1329                                              DEG_ID_LINKED_INDIRECTLY,
1330                                              is_object_visible);
1331                         }
1332                         break;
1333                 }
1334                 case ID_LT:
1335                 {
1336                         op_node = add_operation_node(obdata,
1337                                                      DEG_NODE_TYPE_GEOMETRY,
1338                                                      function_bind(BKE_lattice_eval_geometry,
1339                                                                    _1,
1340                                                                    (Lattice *)obdata_cow),
1341                                                                    DEG_OPCODE_PLACEHOLDER,
1342                                                                    "Geometry Eval");
1343                         op_node->set_as_entry();
1344                         break;
1345                 }
1346
1347                 case ID_GD:
1348                 {
1349                         /* GPencil evaluation operations. */
1350                         op_node = add_operation_node(obdata,
1351                                                      DEG_NODE_TYPE_GEOMETRY,
1352                                                      function_bind(BKE_gpencil_eval_geometry,
1353                                                                    _1,
1354                                                                    (bGPdata *)obdata_cow),
1355                                                      DEG_OPCODE_PLACEHOLDER,
1356                                                      "Geometry Eval");
1357                         op_node->set_as_entry();
1358                         break;
1359                 }
1360                 default:
1361                         BLI_assert(!"Should not happen");
1362                         break;
1363         }
1364         op_node = add_operation_node(obdata, DEG_NODE_TYPE_GEOMETRY, NULL,
1365                                      DEG_OPCODE_PLACEHOLDER, "Eval Done");
1366         op_node->set_as_exit();
1367         /* Parameters for driver sources. */
1368         add_operation_node(obdata,
1369                            DEG_NODE_TYPE_PARAMETERS,
1370                            NULL,
1371                            DEG_OPCODE_PARAMETERS_EVAL);
1372         /* Batch cache. */
1373         add_operation_node(obdata,
1374                            DEG_NODE_TYPE_BATCH_CACHE,
1375                            function_bind(BKE_object_data_select_update,
1376                                          _1,
1377                                          obdata_cow),
1378                            DEG_OPCODE_GEOMETRY_SELECT_UPDATE);
1379 }
1380
1381 void DepsgraphNodeBuilder::build_armature(bArmature *armature)
1382 {
1383         if (built_map_.checkIsBuiltAndTag(armature)) {
1384                 return;
1385         }
1386         build_animdata(&armature->id);
1387         /* Make sure pose is up-to-date with armature updates. */
1388         add_operation_node(&armature->id,
1389                            DEG_NODE_TYPE_PARAMETERS,
1390                            NULL,
1391                            DEG_OPCODE_PLACEHOLDER,
1392                            "Armature Eval");
1393 }
1394
1395 void DepsgraphNodeBuilder::build_camera(Camera *camera)
1396 {
1397         if (built_map_.checkIsBuiltAndTag(camera)) {
1398                 return;
1399         }
1400         OperationDepsNode *op_node;
1401         build_animdata(&camera->id);
1402         op_node = add_operation_node(&camera->id,
1403                                      DEG_NODE_TYPE_PARAMETERS,
1404                                      NULL,
1405                                      DEG_OPCODE_PARAMETERS_EVAL);
1406         op_node->set_as_exit();
1407 }
1408
1409 void DepsgraphNodeBuilder::build_lamp(Lamp *lamp)
1410 {
1411         if (built_map_.checkIsBuiltAndTag(lamp)) {
1412                 return;
1413         }
1414         OperationDepsNode *op_node;
1415         build_animdata(&lamp->id);
1416         op_node = add_operation_node(&lamp->id,
1417                                      DEG_NODE_TYPE_PARAMETERS,
1418                                      NULL,
1419                                      DEG_OPCODE_PARAMETERS_EVAL);
1420         op_node->set_as_exit();
1421         /* lamp's nodetree */
1422         build_nodetree(lamp->nodetree);
1423 }
1424
1425 void DepsgraphNodeBuilder::build_nodetree(bNodeTree *ntree)
1426 {
1427         if (ntree == NULL) {
1428                 return;
1429         }
1430         if (built_map_.checkIsBuiltAndTag(ntree)) {
1431                 return;
1432         }
1433         /* nodetree itself */
1434         add_id_node(&ntree->id);
1435         bNodeTree *ntree_cow = get_cow_datablock(ntree);
1436         /* Animation, */
1437         build_animdata(&ntree->id);
1438         /* Shading update. */
1439         add_operation_node(&ntree->id,
1440                            DEG_NODE_TYPE_SHADING,
1441                            NULL,
1442                            DEG_OPCODE_MATERIAL_UPDATE);
1443         /* NOTE: We really pass original and CoW node trees here, this is how the
1444          * callback works. Ideally we need to find a better way for that.
1445          */
1446         add_operation_node(&ntree->id,
1447                            DEG_NODE_TYPE_SHADING_PARAMETERS,
1448                            function_bind(BKE_nodetree_shading_params_eval,
1449                                          _1,
1450                                          ntree_cow,
1451                                          ntree),
1452                            DEG_OPCODE_MATERIAL_UPDATE);
1453         /* nodetree's nodes... */
1454         LISTBASE_FOREACH (bNode *, bnode, &ntree->nodes) {
1455                 ID *id = bnode->id;
1456                 if (id == NULL) {
1457                         continue;
1458                 }
1459                 ID_Type id_type = GS(id->name);
1460                 if (id_type == ID_MA) {
1461                         build_material((Material *)id);
1462                 }
1463                 else if (id_type == ID_TE) {
1464                         build_texture((Tex *)id);
1465                 }
1466                 else if (id_type == ID_IM) {
1467                         build_image((Image *)id);
1468                 }
1469                 else if (id_type == ID_OB) {
1470                         /* TODO(sergey): Use visibility of owner of the node tree. */
1471                         build_object(-1, (Object *)id, DEG_ID_LINKED_INDIRECTLY, true);
1472                 }
1473                 else if (id_type == ID_SCE) {
1474                         /* Scenes are used by compositor trees, and handled by render
1475                          * pipeline. No need to build dependencies for them here.
1476                          */
1477                 }
1478                 else if (id_type == ID_TXT) {
1479                         /* Ignore script nodes. */
1480                 }
1481                 else if (id_type == ID_MSK) {
1482                         build_mask((Mask *)id);
1483                 }
1484                 else if (id_type == ID_MC) {
1485                         build_movieclip((MovieClip *)id);
1486                 }
1487                 else if (bnode->type == NODE_GROUP) {
1488                         bNodeTree *group_ntree = (bNodeTree *)id;
1489                         build_nodetree(group_ntree);
1490                 }
1491                 else {
1492                         BLI_assert(!"Unknown ID type used for node");
1493                 }
1494         }
1495
1496         // TODO: link from nodetree to owner_component?
1497 }
1498
1499 /* Recursively build graph for material */
1500 void DepsgraphNodeBuilder::build_material(Material *material)
1501 {
1502         if (built_map_.checkIsBuiltAndTag(material)) {
1503                 return;
1504         }
1505         /* Material itself. */
1506         add_id_node(&material->id);
1507         Material *material_cow = get_cow_datablock(material);
1508         /* Shading update. */
1509         add_operation_node(&material->id,
1510                            DEG_NODE_TYPE_SHADING,
1511                            function_bind(BKE_material_eval,
1512                                          _1,
1513                                          material_cow),
1514                            DEG_OPCODE_MATERIAL_UPDATE);
1515         /* Material animation. */
1516         build_animdata(&material->id);
1517         /* Material's nodetree. */
1518         build_nodetree(material->nodetree);
1519 }
1520
1521 /* Recursively build graph for texture */
1522 void DepsgraphNodeBuilder::build_texture(Tex *texture)
1523 {
1524         if (built_map_.checkIsBuiltAndTag(texture)) {
1525                 return;
1526         }
1527         /* Texture itself. */
1528         build_animdata(&texture->id);
1529         /* Texture's nodetree. */
1530         build_nodetree(texture->nodetree);
1531         /* Special cases for different IDs which texture uses. */
1532         if (texture->type == TEX_IMAGE) {
1533                 if (texture->ima != NULL) {
1534                         build_image(texture->ima);
1535                 }
1536         }
1537         add_operation_node(&texture->id,
1538                            DEG_NODE_TYPE_GENERIC_DATABLOCK,
1539                            NULL,
1540                            DEG_OPCODE_GENERIC_DATABLOCK_UPDATE);
1541 }
1542
1543 void DepsgraphNodeBuilder::build_image(Image *image) {
1544         if (built_map_.checkIsBuiltAndTag(image)) {
1545                 return;
1546         }
1547         add_operation_node(&image->id,
1548                            DEG_NODE_TYPE_GENERIC_DATABLOCK,
1549                            NULL,
1550                            DEG_OPCODE_GENERIC_DATABLOCK_UPDATE);
1551 }
1552
1553 void DepsgraphNodeBuilder::build_compositor(Scene *scene)
1554 {
1555         /* For now, just a plain wrapper? */
1556         // TODO: create compositing component?
1557         // XXX: component type undefined!
1558         //graph->get_node(&scene->id, NULL, DEG_NODE_TYPE_COMPOSITING, NULL);
1559
1560         /* for now, nodetrees are just parameters; compositing occurs in internals
1561          * of renderer...
1562          */
1563         add_component_node(&scene->id, DEG_NODE_TYPE_PARAMETERS);
1564         build_nodetree(scene->nodetree);
1565 }
1566
1567 void DepsgraphNodeBuilder::build_gpencil(bGPdata *gpd)
1568 {
1569         if (built_map_.checkIsBuiltAndTag(gpd)) {
1570                 return;
1571         }
1572         ID *gpd_id = &gpd->id;
1573
1574         /* TODO(sergey): what about multiple users of same datablock? This should
1575          * only get added once.
1576          */
1577
1578         /* The main reason Grease Pencil is included here is because the animation
1579          * (and drivers) need to be hosted somewhere.
1580          */
1581         build_animdata(gpd_id);
1582 }
1583
1584 void DepsgraphNodeBuilder::build_cachefile(CacheFile *cache_file)
1585 {
1586         if (built_map_.checkIsBuiltAndTag(cache_file)) {
1587                 return;
1588         }
1589         ID *cache_file_id = &cache_file->id;
1590         /* Animation, */
1591         build_animdata(cache_file_id);
1592         /* Cache evaluation itself. */
1593         add_operation_node(cache_file_id, DEG_NODE_TYPE_CACHE, NULL,
1594                            DEG_OPCODE_PLACEHOLDER, "Cache File Update");
1595 }
1596
1597 void DepsgraphNodeBuilder::build_mask(Mask *mask)
1598 {
1599         if (built_map_.checkIsBuiltAndTag(mask)) {
1600                 return;
1601         }
1602         ID *mask_id = &mask->id;
1603         Mask *mask_cow = get_cow_datablock(mask);
1604         /* F-Curve based animation. */
1605         build_animdata(mask_id);
1606         /* Animation based on mask's shapes. */
1607         add_operation_node(mask_id,
1608                            DEG_NODE_TYPE_ANIMATION,
1609                            function_bind(BKE_mask_eval_animation, _1, mask_cow),
1610                            DEG_OPCODE_MASK_ANIMATION);
1611         /* Final mask evaluation. */
1612         add_operation_node(mask_id,
1613                            DEG_NODE_TYPE_PARAMETERS,
1614                            function_bind(BKE_mask_eval_update, _1, mask_cow),
1615                            DEG_OPCODE_MASK_EVAL);
1616 }
1617
1618 void DepsgraphNodeBuilder::build_movieclip(MovieClip *clip)
1619 {
1620         if (built_map_.checkIsBuiltAndTag(clip)) {
1621                 return;
1622         }
1623         ID *clip_id = &clip->id;
1624         MovieClip *clip_cow = (MovieClip *)ensure_cow_id(clip_id);
1625         /* Animation. */
1626         build_animdata(clip_id);
1627         /* Movie clip evaluation. */
1628         add_operation_node(clip_id,
1629                            DEG_NODE_TYPE_PARAMETERS,
1630                            function_bind(BKE_movieclip_eval_update, _1, clip_cow),
1631                            DEG_OPCODE_MOVIECLIP_EVAL);
1632
1633         add_operation_node(clip_id,
1634                            DEG_NODE_TYPE_BATCH_CACHE,
1635                            function_bind(BKE_movieclip_eval_selection_update, _1, clip_cow),
1636                            DEG_OPCODE_MOVIECLIP_SELECT_UPDATE);
1637 }
1638
1639 void DepsgraphNodeBuilder::build_lightprobe(LightProbe *probe)
1640 {
1641         if (built_map_.checkIsBuiltAndTag(probe)) {
1642                 return;
1643         }
1644         /* Placeholder so we can add relations and tag ID node for update. */
1645         add_operation_node(&probe->id,
1646                            DEG_NODE_TYPE_PARAMETERS,
1647                            NULL,
1648                            DEG_OPCODE_LIGHT_PROBE_EVAL);
1649
1650         build_animdata(&probe->id);
1651 }
1652
1653 void DepsgraphNodeBuilder::build_speaker(Speaker *speaker)
1654 {
1655         if (built_map_.checkIsBuiltAndTag(speaker)) {
1656                 return;
1657         }
1658         /* Placeholder so we can add relations and tag ID node for update. */
1659         add_operation_node(&speaker->id,
1660                            DEG_NODE_TYPE_PARAMETERS,
1661                            NULL,
1662                            DEG_OPCODE_SPEAKER_EVAL);
1663         build_animdata(&speaker->id);
1664 }
1665
1666 /* **** ID traversal callbacks functions **** */
1667
1668 void DepsgraphNodeBuilder::modifier_walk(void *user_data,
1669                                          struct Object * /*object*/,
1670                                          struct ID **idpoin,
1671                                          int /*cb_flag*/)
1672 {
1673         BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
1674         ID *id = *idpoin;
1675         if (id == NULL) {
1676                 return;
1677         }
1678         switch (GS(id->name)) {
1679                 case ID_OB:
1680                         /* Special case for object, so we take owner visibility into
1681                          * account. */
1682                         data->builder->build_object(-1,
1683                                                     (Object *)id,
1684                                                     DEG_ID_LINKED_INDIRECTLY,
1685                                                     data->is_parent_visible);
1686                         break;
1687                 default:
1688                         data->builder->build_id(id);
1689                         break;
1690         }
1691 }
1692
1693 void DepsgraphNodeBuilder::constraint_walk(bConstraint * /*con*/,
1694                                            ID **idpoin,
1695                                            bool /*is_reference*/,
1696                                            void *user_data)
1697 {
1698         BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
1699         ID *id = *idpoin;
1700         if (id == NULL) {
1701                 return;
1702         }
1703         switch (GS(id->name)) {
1704                 case ID_OB:
1705                         /* Special case for object, so we take owner visibility into
1706                          * account. */
1707                         data->builder->build_object(-1,
1708                                                     (Object *)id,
1709                                                     DEG_ID_LINKED_INDIRECTLY,
1710                                                     data->is_parent_visible);
1711                         break;
1712                 default:
1713                         data->builder->build_id(id);
1714                         break;
1715         }
1716 }
1717
1718 }  // namespace DEG