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