Merge branch 'master' into blender2.8
[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         IDInfo *id_info = (IDInfo *)BLI_ghash_lookup(id_info_hash_, id);
167         if (id_info != NULL) {
168                 id_cow = id_info->id_cow;
169                 previously_visible_components_mask =
170                         id_info->previously_visible_components_mask;
171                 previous_eval_flags = id_info->previous_eval_flags;
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         /* Currently all ID nodes are supposed to have copy-on-write logic.
180          *
181          * NOTE: Zero number of components indicates that ID node was just created.
182          */
183         if (BLI_ghash_len(id_node->components) == 0) {
184                 ComponentDepsNode *comp_cow =
185                         id_node->add_component(DEG_NODE_TYPE_COPY_ON_WRITE);
186                 OperationDepsNode *op_cow = comp_cow->add_operation(
187                         function_bind(deg_evaluate_copy_on_write, _1, id_node),
188                         DEG_OPCODE_COPY_ON_WRITE,
189                         "", -1);
190                 graph_->operations.push_back(op_cow);
191         }
192         return id_node;
193 }
194
195 IDDepsNode *DepsgraphNodeBuilder::find_id_node(ID *id)
196 {
197         return graph_->find_id_node(id);
198 }
199
200 TimeSourceDepsNode *DepsgraphNodeBuilder::add_time_source()
201 {
202         return graph_->add_time_source();
203 }
204
205 ComponentDepsNode *DepsgraphNodeBuilder::add_component_node(
206         ID *id,
207         eDepsNode_Type comp_type,
208         const char *comp_name)
209 {
210         IDDepsNode *id_node = add_id_node(id);
211         ComponentDepsNode *comp_node = id_node->add_component(comp_type, comp_name);
212         comp_node->owner = id_node;
213         return comp_node;
214 }
215
216 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
217         ComponentDepsNode *comp_node,
218         const DepsEvalOperationCb& op,
219         eDepsOperation_Code opcode,
220         const char *name,
221         int name_tag)
222 {
223         OperationDepsNode *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 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
242         ID *id,
243         eDepsNode_Type comp_type,
244         const char *comp_name,
245         const DepsEvalOperationCb& op,
246         eDepsOperation_Code opcode,
247         const char *name,
248         int name_tag)
249 {
250         ComponentDepsNode *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 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
255         ID *id,
256         eDepsNode_Type comp_type,
257         const DepsEvalOperationCb& op,
258         eDepsOperation_Code 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 OperationDepsNode *DepsgraphNodeBuilder::ensure_operation_node(
272         ID *id,
273         eDepsNode_Type comp_type,
274         const DepsEvalOperationCb& op,
275         eDepsOperation_Code opcode,
276         const char *name,
277         int name_tag)
278 {
279         OperationDepsNode *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                                               eDepsNode_Type comp_type,
289                                               const char *comp_name,
290                                               eDepsOperation_Code 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 OperationDepsNode *DepsgraphNodeBuilder::find_operation_node(
303         ID *id,
304         eDepsNode_Type comp_type,
305         const char *comp_name,
306         eDepsOperation_Code opcode,
307         const char *name,
308         int name_tag)
309 {
310         ComponentDepsNode *comp_node = add_component_node(id, comp_type, comp_name);
311         return comp_node->find_operation(opcode, name, name_tag);
312 }
313
314 OperationDepsNode *DepsgraphNodeBuilder::find_operation_node(
315         ID *id,
316         eDepsNode_Type comp_type,
317         eDepsOperation_Code 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         IDDepsNode *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          */
346         id_info_hash_ = BLI_ghash_ptr_new("Depsgraph id hash");
347         foreach (IDDepsNode *id_node, graph_->id_nodes) {
348                 IDInfo *id_info = (IDInfo *)MEM_mallocN(
349                         sizeof(IDInfo), "depsgraph id info");
350                 if (deg_copy_on_write_is_expanded(id_node->id_cow) &&
351                     id_node->id_orig != id_node->id_cow)
352                 {
353                         id_info->id_cow = id_node->id_cow;
354                 }
355                 else {
356                         id_info->id_cow = NULL;
357                 }
358                 id_info->previously_visible_components_mask =
359                         id_node->visible_components_mask;
360                 id_info->previous_eval_flags = id_node->eval_flags;
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(OperationDepsNode *, op_node, graph_->entry_tags)
366         {
367                 ComponentDepsNode *comp_node = op_node->owner;
368                 IDDepsNode *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         foreach (const SavedEntryTag& entry_tag, saved_entry_tags_) {
389                 IDDepsNode *id_node = find_id_node(entry_tag.id_orig);
390                 if (id_node == NULL) {
391                         continue;
392                 }
393                 ComponentDepsNode *comp_node =
394                         id_node->find_component(entry_tag.component_type);
395                 if (comp_node == NULL) {
396                         continue;
397                 }
398                 OperationDepsNode *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                  */
405                 op_node->tag_update(graph_, DEG_UPDATE_SOURCE_USER_EDIT);
406         }
407 }
408
409 void DepsgraphNodeBuilder::build_id(ID *id)
410 {
411         if (id == NULL) {
412                 return;
413         }
414         switch (GS(id->name)) {
415                 case ID_AC:
416                         build_action((bAction *)id);
417                         break;
418                 case ID_AR:
419                         build_armature((bArmature *)id);
420                         break;
421                 case ID_CA:
422                         build_camera((Camera *)id);
423                         break;
424                 case ID_GR:
425                         build_collection(NULL, (Collection *)id);
426                         break;
427                 case ID_OB:
428                         /* TODO(sergey): Get visibility from a "parent" somehow.
429                          *
430                          * NOTE: Using `false` visibility here should be fine, since if this
431                          * driver affects on something invisible we don't really care if the
432                          * driver gets evaluated (and even don't want this to force object
433                          * to become visible).
434                          *
435                          * If this happened to be affecting visible object, then it is up to
436                          * deg_graph_build_flush_visibility() to ensure visibility of the
437                          * object is true.
438                          */
439                         build_object(-1, (Object *)id, DEG_ID_LINKED_INDIRECTLY, false);
440                         break;
441                 case ID_KE:
442                         build_shapekeys((Key *)id);
443                         break;
444                 case ID_LA:
445                         build_lamp((Lamp *)id);
446                         break;
447                 case ID_LP:
448                         build_lightprobe((LightProbe *)id);
449                         break;
450                 case ID_NT:
451                         build_nodetree((bNodeTree *)id);
452                         break;
453                 case ID_MA:
454                         build_material((Material *)id);
455                         break;
456                 case ID_TE:
457                         build_texture((Tex *)id);
458                         break;
459                 case ID_IM:
460                         build_image((Image *)id);
461                         break;
462                 case ID_WO:
463                         build_world((World *)id);
464                         break;
465                 case ID_MSK:
466                         build_mask((Mask *)id);
467                         break;
468                 case ID_MC:
469                         build_movieclip((MovieClip *)id);
470                         break;
471                 case ID_ME:
472                 case ID_CU:
473                 case ID_MB:
474                 case ID_LT:
475                         /* TODO(sergey): Get visibility from a "parent" somehow.
476                          *
477                          * NOTE: Similarly to above, we don't want false-positives on
478                          * visibility.
479                          */
480                         build_object_data_geometry_datablock(id, false);
481                         break;
482                 case ID_SPK:
483                         build_speaker((Speaker *)id);
484                         break;
485                 case ID_TXT:
486                         /* Not a part of dependency graph. */
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         IDDepsNode *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                 IDDepsNode *id_node = find_id_node(&object->id);
561                 /* We need to build some extra stuff if object becomes linked
562                  * directly.
563                  */
564                 if (id_node->linked_state == DEG_ID_LINKED_INDIRECTLY) {
565                         build_object_flags(base_index, object, linked_state);
566                 }
567                 id_node->linked_state = max(id_node->linked_state, linked_state);
568                 if (id_node->linked_state == DEG_ID_LINKED_DIRECTLY) {
569                         id_node->is_directly_visible |= is_visible;
570                 }
571                 return;
572         }
573         /* Create ID node for object and begin init. */
574         IDDepsNode *id_node = add_id_node(&object->id);
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         object->customdata_mask = 0;
583         /* Various flags, flushing from bases/collections. */
584         build_object_flags(base_index, object, linked_state);
585         /* Transform. */
586         build_object_transform(object);
587         /* Parent. */
588         if (object->parent != NULL) {
589                 build_object(
590                         -1, object->parent, DEG_ID_LINKED_INDIRECTLY, is_visible);
591         }
592         /* Modifiers. */
593         if (object->modifiers.first != NULL) {
594                 BuilderWalkUserData data;
595                 data.builder = this;
596                 data.is_parent_visible = is_visible;
597                 modifiers_foreachIDLink(object, modifier_walk, &data);
598         }
599         /* Grease Pencil Modifiers. */
600         if (object->greasepencil_modifiers.first != NULL) {
601                 BuilderWalkUserData data;
602                 data.builder = this;
603                 data.is_parent_visible = is_visible;
604                 BKE_gpencil_modifiers_foreachIDLink(object, modifier_walk, &data);
605         }
606         /* Shader FX. */
607         if (object->shader_fx.first != NULL) {
608                 BuilderWalkUserData data;
609                 data.builder = this;
610                 data.is_parent_visible = is_visible;
611                 BKE_shaderfx_foreachIDLink(object, modifier_walk, &data);
612         }
613         /* Constraints. */
614         if (object->constraints.first != NULL) {
615                 BuilderWalkUserData data;
616                 data.builder = this;
617                 data.is_parent_visible = is_visible;
618                 BKE_constraints_id_loop(&object->constraints, constraint_walk, &data);
619         }
620         /* Object data. */
621         build_object_data(object, is_visible);
622         /* Build animation data,
623          *
624          * Do it now because it's possible object data will affect
625          * on object's level animation, for example in case of rebuilding
626          * pose for proxy.
627          */
628         OperationDepsNode *op_node = add_operation_node(&object->id,
629                                                         DEG_NODE_TYPE_PARAMETERS,
630                                                         NULL,
631                                                         DEG_OPCODE_PARAMETERS_EVAL);
632         op_node->set_as_exit();
633         build_animdata(&object->id);
634         /* Particle systems. */
635         if (object->particlesystem.first != NULL) {
636                 build_particles(object, is_visible);
637         }
638         /* Proxy object to copy from. */
639         if (object->proxy_from != NULL) {
640                 build_object(
641                         -1, object->proxy_from, DEG_ID_LINKED_INDIRECTLY, is_visible);
642         }
643         if (object->proxy_group != NULL) {
644                 build_object(
645                         -1, object->proxy_group, DEG_ID_LINKED_INDIRECTLY, is_visible);
646         }
647         /* Object dupligroup. */
648         if (object->dup_group != NULL) {
649                 const bool is_current_parent_collection_visible =
650                         is_parent_collection_visible_;
651                 is_parent_collection_visible_ = is_visible;
652                 build_collection(NULL, object->dup_group);
653                 is_parent_collection_visible_ = is_current_parent_collection_visible;
654                 add_operation_node(&object->id,
655                                    DEG_NODE_TYPE_DUPLI,
656                                    NULL,
657                                    DEG_OPCODE_PLACEHOLDER,
658                                    "Dupli");
659         }
660 }
661
662 void DepsgraphNodeBuilder::build_object_flags(
663         int base_index,
664         Object *object,
665         eDepsNode_LinkedState_Type linked_state)
666 {
667         if (base_index == -1) {
668                 return;
669         }
670         Scene *scene_cow = get_cow_datablock(scene_);
671         Object *object_cow = get_cow_datablock(object);
672         const bool is_from_set = (linked_state == DEG_ID_LINKED_VIA_SET);
673         /* TODO(sergey): Is this really best component to be used? */
674         add_operation_node(&object->id,
675                            DEG_NODE_TYPE_OBJECT_FROM_LAYER,
676                            function_bind(BKE_object_eval_flush_base_flags,
677                                          _1,
678                                          scene_cow,
679                                          view_layer_index_,
680                                          object_cow, base_index,
681                                          is_from_set),
682                            DEG_OPCODE_OBJECT_BASE_FLAGS);
683 }
684
685 void DepsgraphNodeBuilder::build_object_data(
686         Object *object, bool is_object_visible)
687 {
688         if (object->data == NULL) {
689                 return;
690         }
691         /* type-specific data. */
692         switch (object->type) {
693                 case OB_MESH:
694                 case OB_CURVE:
695                 case OB_FONT:
696                 case OB_SURF:
697                 case OB_MBALL:
698                 case OB_LATTICE:
699                 case OB_GPENCIL:
700                         build_object_data_geometry(object, is_object_visible);
701                         break;
702                 case OB_ARMATURE:
703                         if (ID_IS_LINKED(object) && object->proxy_from != NULL) {
704                                 build_proxy_rig(object);
705                         }
706                         else {
707                                 build_rig(object, is_object_visible);
708                         }
709                         break;
710                 case OB_LAMP:
711                         build_object_data_lamp(object);
712                         break;
713                 case OB_CAMERA:
714                         build_object_data_camera(object);
715                         break;
716                 case OB_LIGHTPROBE:
717                         build_object_data_lightprobe(object);
718                         break;
719                 case OB_SPEAKER:
720                         build_object_data_speaker(object);
721                         break;
722                 default:
723                 {
724                         ID *obdata = (ID *)object->data;
725                         if (built_map_.checkIsBuilt(obdata) == 0) {
726                                 build_animdata(obdata);
727                         }
728                         break;
729                 }
730         }
731 }
732
733 void DepsgraphNodeBuilder::build_object_data_camera(Object *object)
734 {
735         Camera *camera = (Camera *)object->data;
736         build_camera(camera);
737 }
738
739 void DepsgraphNodeBuilder::build_object_data_lamp(Object *object)
740 {
741         Lamp *lamp = (Lamp *)object->data;
742         build_lamp(lamp);
743 }
744
745 void DepsgraphNodeBuilder::build_object_data_lightprobe(Object *object)
746 {
747         LightProbe *probe = (LightProbe *)object->data;
748         build_lightprobe(probe);
749         add_operation_node(&object->id,
750                            DEG_NODE_TYPE_PARAMETERS,
751                            NULL,
752                            DEG_OPCODE_LIGHT_PROBE_EVAL);
753 }
754
755 void DepsgraphNodeBuilder::build_object_data_speaker(Object *object)
756 {
757         Speaker *speaker = (Speaker *)object->data;
758         build_speaker(speaker);
759         add_operation_node(&object->id,
760                            DEG_NODE_TYPE_PARAMETERS,
761                            NULL,
762                            DEG_OPCODE_SPEAKER_EVAL);
763 }
764
765 void DepsgraphNodeBuilder::build_object_transform(Object *object)
766 {
767         OperationDepsNode *op_node;
768         Scene *scene_cow = get_cow_datablock(scene_);
769         Object *ob_cow = get_cow_datablock(object);
770
771         /* local transforms (from transform channels - loc/rot/scale + deltas) */
772         op_node = add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
773                                      function_bind(BKE_object_eval_local_transform,
774                                                    _1,
775                                                    ob_cow),
776                                      DEG_OPCODE_TRANSFORM_LOCAL);
777         op_node->set_as_entry();
778
779         /* object parent */
780         if (object->parent != NULL) {
781                 add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
782                                    function_bind(BKE_object_eval_parent,
783                                                  _1,
784                                                  scene_cow,
785                                                  ob_cow),
786                                    DEG_OPCODE_TRANSFORM_PARENT);
787         }
788
789         /* object constraints */
790         if (object->constraints.first != NULL) {
791                 build_object_constraints(object);
792         }
793
794         /* Rest of transformation update. */
795         add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
796                            function_bind(BKE_object_eval_uber_transform,
797                                          _1,
798                                          ob_cow),
799                            DEG_OPCODE_TRANSFORM_OBJECT_UBEREVAL);
800
801         /* object transform is done */
802         op_node = add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
803                                      function_bind(BKE_object_eval_transform_final,
804                                                    _1,
805                                                    ob_cow),
806                                      DEG_OPCODE_TRANSFORM_FINAL);
807         op_node->set_as_exit();
808 }
809
810 /**
811  * Constraints Graph Notes
812  *
813  * For constraints, we currently only add a operation node to the Transform
814  * or Bone components (depending on whichever type of owner we have).
815  * This represents the entire constraints stack, which is for now just
816  * executed as a single monolithic block. At least initially, this should
817  * be sufficient for ensuring that the porting/refactoring process remains
818  * manageable.
819  *
820  * However, when the time comes for developing "node-based" constraints,
821  * we'll need to split this up into pre/post nodes for "constraint stack
822  * evaluation" + operation nodes for each constraint (i.e. the contents
823  * of the loop body used in the current "solve_constraints()" operation).
824  *
825  * -- Aligorith, August 2013
826  */
827 void DepsgraphNodeBuilder::build_object_constraints(Object *object)
828 {
829         /* create node for constraint stack */
830         add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
831                            function_bind(BKE_object_eval_constraints,
832                                          _1,
833                                          get_cow_datablock(scene_),
834                                          get_cow_datablock(object)),
835                            DEG_OPCODE_TRANSFORM_CONSTRAINTS);
836 }
837
838 void DepsgraphNodeBuilder::build_object_pointcache(Object *object)
839 {
840         if (!BKE_ptcache_object_has(scene_, object, 0)) {
841                 return;
842         }
843         Scene *scene_cow = get_cow_datablock(scene_);
844         Object *object_cow = get_cow_datablock(object);
845         add_operation_node(&object->id,
846                            DEG_NODE_TYPE_POINT_CACHE,
847                            function_bind(BKE_object_eval_ptcache_reset,
848                                          _1,
849                                          scene_cow,
850                                          object_cow),
851                            DEG_OPCODE_POINT_CACHE_RESET);
852 }
853
854 /**
855  * Build graph nodes for AnimData block
856  * \param id: ID-Block which hosts the AnimData
857  */
858 void DepsgraphNodeBuilder::build_animdata(ID *id)
859 {
860         AnimData *adt = BKE_animdata_from_id(id);
861         if (adt == NULL) {
862                 return;
863         }
864         if (adt->action != NULL) {
865                 build_action(adt->action);
866         }
867         /* animation */
868         if (adt->action || adt->nla_tracks.first || adt->drivers.first) {
869                 (void) add_id_node(id);
870                 ID *id_cow = get_cow_id(id);
871
872                 // XXX: Hook up specific update callbacks for special properties which
873                 // may need it...
874
875                 /* actions and NLA - as a single unit for now, as it gets complicated to
876                  * schedule otherwise.
877                  */
878                 if ((adt->action) || (adt->nla_tracks.first)) {
879                         /* create the node */
880                         add_operation_node(id, DEG_NODE_TYPE_ANIMATION,
881                                            function_bind(BKE_animsys_eval_animdata,
882                                                          _1,
883                                                          id_cow),
884                                            DEG_OPCODE_ANIMATION,
885                                            id->name);
886
887                         /* TODO: for each channel affected, we might also want to add some
888                          * support for running RNA update callbacks on them
889                          * (which will be needed for proper handling of drivers later)
890                          */
891                 }
892
893                 /* NLA strips contain actions */
894                 LISTBASE_FOREACH (NlaTrack *, nlt, &adt->nla_tracks) {
895                         build_animdata_nlastrip_targets(&nlt->strips);
896                 }
897
898                 /* drivers */
899                 int driver_index = 0;
900                 LISTBASE_FOREACH (FCurve *, fcu, &adt->drivers) {
901                         /* create driver */
902                         build_driver(id, fcu, driver_index++);
903                 }
904         }
905 }
906
907 void DepsgraphNodeBuilder::build_animdata_nlastrip_targets(ListBase *strips)
908 {
909         LISTBASE_FOREACH (NlaStrip *, strip, strips) {
910                 if (strip->act != NULL) {
911                         build_action(strip->act);
912                 }
913                 else if (strip->strips.first != NULL) {
914                         build_animdata_nlastrip_targets(&strip->strips);
915                 }
916         }
917 }
918
919 void DepsgraphNodeBuilder::build_action(bAction *action)
920 {
921         if (built_map_.checkIsBuiltAndTag(action)) {
922                 return;
923         }
924         add_operation_node(&action->id,
925                            DEG_NODE_TYPE_ANIMATION,
926                            NULL,
927                            DEG_OPCODE_ANIMATION);
928 }
929
930 /**
931  * Build graph node(s) for Driver
932  * \param id: ID-Block that driver is attached to
933  * \param fcu: Driver-FCurve
934  * \param driver_index: Index in animation data drivers list
935  */
936 void DepsgraphNodeBuilder::build_driver(ID *id, FCurve *fcurve, int driver_index)
937 {
938         /* Create data node for this driver */
939         ID *id_cow = get_cow_id(id);
940         ChannelDriver *driver_orig = fcurve->driver;
941
942         /* TODO(sergey): ideally we could pass the COW of fcu, but since it
943          * has not yet been allocated at this point we can't. As a workaround
944          * the animation systems allocates an array so we can do a fast lookup
945          * with the driver index. */
946         ensure_operation_node(id,
947                               DEG_NODE_TYPE_PARAMETERS,
948                               function_bind(BKE_animsys_eval_driver, _1, id_cow, driver_index, driver_orig),
949                               DEG_OPCODE_DRIVER,
950                               fcurve->rna_path ? fcurve->rna_path : "",
951                               fcurve->array_index);
952         build_driver_variables(id, fcurve);
953 }
954
955 void DepsgraphNodeBuilder::build_driver_variables(ID * id, FCurve *fcurve)
956 {
957         build_driver_id_property(id, fcurve->rna_path);
958         LISTBASE_FOREACH (DriverVar *, dvar, &fcurve->driver->variables) {
959                 DRIVER_TARGETS_USED_LOOPER_BEGIN(dvar)
960                 {
961                         if (dtar->id == NULL) {
962                                 continue;
963                         }
964                         build_id(dtar->id);
965                         build_driver_id_property(dtar->id, dtar->rna_path);
966                         /* Corresponds to dtar_id_ensure_proxy_from(). */
967                         if ((GS(dtar->id->name) == ID_OB) &&
968                             (((Object *)dtar->id)->proxy_from != NULL))
969                         {
970                                 Object *proxy_from = ((Object *)dtar->id)->proxy_from;
971                                 build_id(&proxy_from->id);
972                                 build_driver_id_property(&proxy_from->id, dtar->rna_path);
973                         }
974                 }
975                 DRIVER_TARGETS_LOOPER_END;
976         }
977 }
978
979 void DepsgraphNodeBuilder::build_driver_id_property(ID *id,
980                                                     const char *rna_path)
981 {
982         if (id == NULL || rna_path == NULL) {
983                 return;
984         }
985         PointerRNA id_ptr, ptr;
986         PropertyRNA *prop;
987         RNA_id_pointer_create(id, &id_ptr);
988         if (!RNA_path_resolve_full(&id_ptr, rna_path, &ptr, &prop, NULL)) {
989                 return;
990         }
991         if (prop == NULL) {
992                 return;
993         }
994         if (!RNA_property_is_idprop(prop)) {
995                 return;
996         }
997         const char *prop_identifier = RNA_property_identifier((PropertyRNA *)prop);
998         ensure_operation_node(id,
999                               DEG_NODE_TYPE_PARAMETERS,
1000                               NULL,
1001                               DEG_OPCODE_ID_PROPERTY,
1002                               prop_identifier);
1003 }
1004
1005 /* Recursively build graph for world */
1006 void DepsgraphNodeBuilder::build_world(World *world)
1007 {
1008         if (built_map_.checkIsBuiltAndTag(world)) {
1009                 return;
1010         }
1011         /* World itself. */
1012         add_id_node(&world->id);
1013         World *world_cow = get_cow_datablock(world);
1014         /* Shading update. */
1015         add_operation_node(&world->id,
1016                            DEG_NODE_TYPE_SHADING,
1017                            function_bind(BKE_world_eval,
1018                                          _1,
1019                                          world_cow),
1020                            DEG_OPCODE_WORLD_UPDATE);
1021         /* Animation. */
1022         build_animdata(&world->id);
1023         /* World's nodetree. */
1024         build_nodetree(world->nodetree);
1025 }
1026
1027 /* Rigidbody Simulation - Scene Level */
1028 void DepsgraphNodeBuilder::build_rigidbody(Scene *scene)
1029 {
1030         RigidBodyWorld *rbw = scene->rigidbody_world;
1031         Scene *scene_cow = get_cow_datablock(scene);
1032
1033         /**
1034          * Rigidbody Simulation Nodes
1035          * ==========================
1036          *
1037          * There are 3 nodes related to Rigidbody Simulation:
1038          * 1) "Initialize/Rebuild World" - this is called sparingly, only when the
1039          *    simulation needs to be rebuilt (mainly after file reload, or moving
1040          *    back to start frame)
1041          * 2) "Do Simulation" - perform a simulation step - interleaved between the
1042          *    evaluation steps for clusters of objects (i.e. between those affected
1043          *    and/or not affected by the sim for instance).
1044          *
1045          * 3) "Pull Results" - grab the specific transforms applied for a specific
1046          *    object - performed as part of object's transform-stack building.
1047          */
1048
1049         /* Create nodes --------------------------------------------------------- */
1050
1051         /* XXX: is this the right component, or do we want to use another one
1052          * instead?
1053          */
1054
1055         /* init/rebuild operation */
1056         /*OperationDepsNode *init_node =*/ add_operation_node(
1057                 &scene->id, DEG_NODE_TYPE_TRANSFORM,
1058                 function_bind(BKE_rigidbody_rebuild_sim, _1, scene_cow),
1059                 DEG_OPCODE_RIGIDBODY_REBUILD);
1060
1061         /* do-sim operation */
1062         // XXX: what happens if we need to split into several groups?
1063         OperationDepsNode *sim_node = add_operation_node(
1064                 &scene->id, DEG_NODE_TYPE_TRANSFORM,
1065                 function_bind(BKE_rigidbody_eval_simulation, _1, scene_cow),
1066                 DEG_OPCODE_RIGIDBODY_SIM);
1067
1068         /* XXX: For now, the sim node is the only one that really matters here.
1069          * If any other sims get added later, we may have to remove these hacks...
1070          */
1071         sim_node->owner->entry_operation = sim_node;
1072         sim_node->owner->exit_operation  = sim_node;
1073
1074         /* objects - simulation participants */
1075         if (rbw->group) {
1076                 build_collection(NULL, rbw->group);
1077
1078                 FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(rbw->group, object)
1079                 {
1080                         if (object->type != OB_MESH)
1081                                 continue;
1082
1083                         /* 2) create operation for flushing results */
1084                         /* object's transform component - where the rigidbody operation
1085                          * lives. */
1086                         add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
1087                                            function_bind(
1088                                                    BKE_rigidbody_object_sync_transforms,
1089                                                    _1,
1090                                                    scene_cow,
1091                                                    get_cow_datablock(object)),
1092                                            DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
1093                 }
1094                 FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
1095         }
1096 }
1097
1098 void DepsgraphNodeBuilder::build_particles(Object *object,
1099                                            bool is_object_visible)
1100 {
1101         /**
1102          * Particle Systems Nodes
1103          * ======================
1104          *
1105          * There are two types of nodes associated with representing
1106          * particle systems:
1107          *  1) Component (EVAL_PARTICLES) - This is the particle-system
1108          *     evaluation context for an object. It acts as the container
1109          *     for all the nodes associated with a particular set of particle
1110          *     systems.
1111          *  2) Particle System Eval Operation - This operation node acts as a
1112          *     blackbox evaluation step for one particle system referenced by
1113          *     the particle systems stack. All dependencies link to this operation.
1114          */
1115         /* Component for all particle systems. */
1116         ComponentDepsNode *psys_comp =
1117                 add_component_node(&object->id, DEG_NODE_TYPE_EVAL_PARTICLES);
1118
1119         /* TODO(sergey): Need to get COW of PSYS. */
1120         Scene *scene_cow = get_cow_datablock(scene_);
1121         Object *ob_cow = get_cow_datablock(object);
1122
1123         add_operation_node(psys_comp,
1124                            function_bind(BKE_particle_system_eval_init,
1125                                          _1,
1126                                          scene_cow,
1127                                          ob_cow),
1128                            DEG_OPCODE_PARTICLE_SYSTEM_EVAL_INIT);
1129         /* Build all particle systems. */
1130         LISTBASE_FOREACH (ParticleSystem *, psys, &object->particlesystem) {
1131                 ParticleSettings *part = psys->part;
1132                 /* Build particle settings operations.
1133                  *
1134                  * NOTE: The call itself ensures settings are only build once.
1135                  */
1136                 build_particle_settings(part);
1137                 /* Particle system evaluation. */
1138                 add_operation_node(psys_comp,
1139                                    NULL,
1140                                    DEG_OPCODE_PARTICLE_SYSTEM_EVAL,
1141                                    psys->name);
1142                 /* Visualization of particle system. */
1143                 switch (part->ren_as) {
1144                         case PART_DRAW_OB:
1145                                 if (part->dup_ob != NULL) {
1146                                         build_object(-1,
1147                                                      part->dup_ob,
1148                                                      DEG_ID_LINKED_INDIRECTLY,
1149                                                      is_object_visible);
1150                                 }
1151                                 break;
1152                         case PART_DRAW_GR:
1153                                 if (part->dup_group != NULL) {
1154                                         build_collection(NULL, part->dup_group);
1155                                 }
1156                                 break;
1157                 }
1158         }
1159 }
1160
1161 void DepsgraphNodeBuilder::build_particle_settings(ParticleSettings *part) {
1162         if (built_map_.checkIsBuiltAndTag(part)) {
1163                 return;
1164         }
1165         /* Animation data. */
1166         build_animdata(&part->id);
1167         /* Parameters change. */
1168         add_operation_node(&part->id,
1169                            DEG_NODE_TYPE_PARAMETERS,
1170                            NULL,
1171                            DEG_OPCODE_PARTICLE_SETTINGS_EVAL);
1172 }
1173
1174 /* Shapekeys */
1175 void DepsgraphNodeBuilder::build_shapekeys(Key *key)
1176 {
1177         if (built_map_.checkIsBuiltAndTag(key)) {
1178                 return;
1179         }
1180         build_animdata(&key->id);
1181         add_operation_node(&key->id,
1182                            DEG_NODE_TYPE_GEOMETRY,
1183                            NULL,
1184                            DEG_OPCODE_GEOMETRY_SHAPEKEY);
1185 }
1186
1187 /* ObData Geometry Evaluation */
1188 // XXX: what happens if the datablock is shared!
1189 void DepsgraphNodeBuilder::build_object_data_geometry(
1190         Object *object,
1191         bool is_object_visible)
1192 {
1193         OperationDepsNode *op_node;
1194         Scene *scene_cow = get_cow_datablock(scene_);
1195         Object *object_cow = get_cow_datablock(object);
1196         /* Temporary uber-update node, which does everything.
1197          * It is for the being we're porting old dependencies into the new system.
1198          * We'll get rid of this node as soon as all the granular update functions
1199          * are filled in.
1200          *
1201          * TODO(sergey): Get rid of this node.
1202          */
1203         op_node = add_operation_node(&object->id,
1204                                      DEG_NODE_TYPE_GEOMETRY,
1205                                      function_bind(BKE_object_eval_uber_data,
1206                                                    _1,
1207                                                    scene_cow,
1208                                                    object_cow),
1209                                      DEG_OPCODE_GEOMETRY_UBEREVAL);
1210         op_node->set_as_exit();
1211
1212         op_node = add_operation_node(&object->id,
1213                                      DEG_NODE_TYPE_GEOMETRY,
1214                                      NULL,
1215                                      DEG_OPCODE_PLACEHOLDER,
1216                                      "Eval Init");
1217         op_node->set_as_entry();
1218         /* Materials. */
1219         if (object->totcol != 0) {
1220                 if (object->type == OB_MESH) {
1221                         add_operation_node(&object->id,
1222                                            DEG_NODE_TYPE_SHADING,
1223                                            function_bind(BKE_object_eval_update_shading,
1224                                                          _1,
1225                                                          object_cow),
1226                                            DEG_OPCODE_SHADING);
1227                 }
1228                 for (int a = 1; a <= object->totcol; a++) {
1229                         Material *ma = give_current_material(object, a);
1230                         if (ma != NULL) {
1231                                 build_material(ma);
1232                         }
1233                 }
1234         }
1235         /* Point caches. */
1236         build_object_pointcache(object);
1237         /* Geometry. */
1238         build_object_data_geometry_datablock((ID *)object->data, is_object_visible);
1239 }
1240
1241 void DepsgraphNodeBuilder::build_object_data_geometry_datablock(
1242         ID *obdata,
1243         bool is_object_visible)
1244 {
1245         if (built_map_.checkIsBuiltAndTag(obdata)) {
1246                 return;
1247         }
1248         OperationDepsNode *op_node;
1249         /* Make sure we've got an ID node before requesting CoW pointer. */
1250         (void) add_id_node((ID *)obdata);
1251         ID *obdata_cow = get_cow_id(obdata);
1252         /* Animation. */
1253         build_animdata(obdata);
1254         /* ShapeKeys */
1255         Key *key = BKE_key_from_id(obdata);
1256         if (key) {
1257                 build_shapekeys(key);
1258         }
1259         /* Nodes for result of obdata's evaluation, and geometry
1260          * evaluation on object.
1261          */
1262         const ID_Type id_type = GS(obdata->name);
1263         switch (id_type) {
1264                 case ID_ME:
1265                 {
1266                         op_node = add_operation_node(obdata,
1267                                                      DEG_NODE_TYPE_GEOMETRY,
1268                                                      function_bind(BKE_mesh_eval_geometry,
1269                                                                    _1,
1270                                                                    (Mesh *)obdata_cow),
1271                                                      DEG_OPCODE_PLACEHOLDER,
1272                                                      "Geometry Eval");
1273                         op_node->set_as_entry();
1274                         break;
1275                 }
1276                 case ID_MB:
1277                 {
1278                         op_node = add_operation_node(obdata,
1279                                                      DEG_NODE_TYPE_GEOMETRY,
1280                                                      NULL,
1281                                                      DEG_OPCODE_PLACEHOLDER,
1282                                                      "Geometry Eval");
1283                         op_node->set_as_entry();
1284                         break;
1285                 }
1286                 case ID_CU:
1287                 {
1288                         op_node = add_operation_node(obdata,
1289                                                      DEG_NODE_TYPE_GEOMETRY,
1290                                                      function_bind(BKE_curve_eval_geometry,
1291                                                                    _1,
1292                                                                    (Curve *)obdata_cow),
1293                                                                    DEG_OPCODE_PLACEHOLDER,
1294                                                                    "Geometry Eval");
1295                         op_node->set_as_entry();
1296                         /* Make sure objects used for bevel.taper are in the graph.
1297                          * NOTE: This objects might be not linked to the scene.
1298                          */
1299                         Curve *cu = (Curve *)obdata;
1300                         if (cu->bevobj != NULL) {
1301                                 build_object(-1,
1302                                              cu->bevobj,
1303                                              DEG_ID_LINKED_INDIRECTLY,
1304                                              is_object_visible);
1305                         }
1306                         if (cu->taperobj != NULL) {
1307                                 build_object(-1,
1308                                              cu->taperobj,
1309                                              DEG_ID_LINKED_INDIRECTLY,
1310                                              is_object_visible);
1311                         }
1312                         if (cu->textoncurve != NULL) {
1313                                 build_object(-1,
1314                                              cu->textoncurve,
1315                                              DEG_ID_LINKED_INDIRECTLY,
1316                                              is_object_visible);
1317                         }
1318                         break;
1319                 }
1320                 case ID_LT:
1321                 {
1322                         op_node = add_operation_node(obdata,
1323                                                      DEG_NODE_TYPE_GEOMETRY,
1324                                                      function_bind(BKE_lattice_eval_geometry,
1325                                                                    _1,
1326                                                                    (Lattice *)obdata_cow),
1327                                                                    DEG_OPCODE_PLACEHOLDER,
1328                                                                    "Geometry Eval");
1329                         op_node->set_as_entry();
1330                         break;
1331                 }
1332
1333                 case ID_GD:
1334                 {
1335                         /* GPencil evaluation operations. */
1336                         op_node = add_operation_node(obdata,
1337                                                      DEG_NODE_TYPE_GEOMETRY,
1338                                                      function_bind(BKE_gpencil_eval_geometry,
1339                                                                    _1,
1340                                                                    (bGPdata *)obdata_cow),
1341                                                      DEG_OPCODE_PLACEHOLDER,
1342                                                      "Geometry Eval");
1343                         op_node->set_as_entry();
1344                         break;
1345                 }
1346                 default:
1347                         BLI_assert(!"Should not happen");
1348                         break;
1349         }
1350         op_node = add_operation_node(obdata, DEG_NODE_TYPE_GEOMETRY, NULL,
1351                                      DEG_OPCODE_PLACEHOLDER, "Eval Done");
1352         op_node->set_as_exit();
1353         /* Parameters for driver sources. */
1354         add_operation_node(obdata,
1355                            DEG_NODE_TYPE_PARAMETERS,
1356                            NULL,
1357                            DEG_OPCODE_PARAMETERS_EVAL);
1358         /* Batch cache. */
1359         add_operation_node(obdata,
1360                            DEG_NODE_TYPE_BATCH_CACHE,
1361                            function_bind(BKE_object_data_select_update,
1362                                          _1,
1363                                          obdata_cow),
1364                            DEG_OPCODE_GEOMETRY_SELECT_UPDATE);
1365 }
1366
1367 void DepsgraphNodeBuilder::build_armature(bArmature *armature)
1368 {
1369         if (built_map_.checkIsBuiltAndTag(armature)) {
1370                 return;
1371         }
1372         build_animdata(&armature->id);
1373         /* Make sure pose is up-to-date with armature updates. */
1374         add_operation_node(&armature->id,
1375                            DEG_NODE_TYPE_PARAMETERS,
1376                            NULL,
1377                            DEG_OPCODE_PLACEHOLDER,
1378                            "Armature Eval");
1379 }
1380
1381 void DepsgraphNodeBuilder::build_camera(Camera *camera)
1382 {
1383         if (built_map_.checkIsBuiltAndTag(camera)) {
1384                 return;
1385         }
1386         OperationDepsNode *op_node;
1387         build_animdata(&camera->id);
1388         op_node = add_operation_node(&camera->id,
1389                                      DEG_NODE_TYPE_PARAMETERS,
1390                                      NULL,
1391                                      DEG_OPCODE_PARAMETERS_EVAL);
1392         op_node->set_as_exit();
1393 }
1394
1395 void DepsgraphNodeBuilder::build_lamp(Lamp *lamp)
1396 {
1397         if (built_map_.checkIsBuiltAndTag(lamp)) {
1398                 return;
1399         }
1400         OperationDepsNode *op_node;
1401         build_animdata(&lamp->id);
1402         op_node = add_operation_node(&lamp->id,
1403                                      DEG_NODE_TYPE_PARAMETERS,
1404                                      NULL,
1405                                      DEG_OPCODE_PARAMETERS_EVAL);
1406         op_node->set_as_exit();
1407         /* lamp's nodetree */
1408         build_nodetree(lamp->nodetree);
1409 }
1410
1411 void DepsgraphNodeBuilder::build_nodetree(bNodeTree *ntree)
1412 {
1413         if (ntree == NULL) {
1414                 return;
1415         }
1416         if (built_map_.checkIsBuiltAndTag(ntree)) {
1417                 return;
1418         }
1419         /* nodetree itself */
1420         add_id_node(&ntree->id);
1421         bNodeTree *ntree_cow = get_cow_datablock(ntree);
1422         /* Animation, */
1423         build_animdata(&ntree->id);
1424         /* Shading update. */
1425         add_operation_node(&ntree->id,
1426                            DEG_NODE_TYPE_SHADING,
1427                            NULL,
1428                            DEG_OPCODE_MATERIAL_UPDATE);
1429         /* NOTE: We really pass original and CoW node trees here, this is how the
1430          * callback works. Ideally we need to find a better way for that.
1431          */
1432         add_operation_node(&ntree->id,
1433                            DEG_NODE_TYPE_SHADING_PARAMETERS,
1434                            function_bind(BKE_nodetree_shading_params_eval,
1435                                          _1,
1436                                          ntree_cow,
1437                                          ntree),
1438                            DEG_OPCODE_MATERIAL_UPDATE);
1439         /* nodetree's nodes... */
1440         LISTBASE_FOREACH (bNode *, bnode, &ntree->nodes) {
1441                 ID *id = bnode->id;
1442                 if (id == NULL) {
1443                         continue;
1444                 }
1445                 ID_Type id_type = GS(id->name);
1446                 if (id_type == ID_MA) {
1447                         build_material((Material *)id);
1448                 }
1449                 else if (id_type == ID_TE) {
1450                         build_texture((Tex *)id);
1451                 }
1452                 else if (id_type == ID_IM) {
1453                         build_image((Image *)id);
1454                 }
1455                 else if (id_type == ID_OB) {
1456                         /* TODO(sergey): Use visibility of owner of the node tree. */
1457                         build_object(-1, (Object *)id, DEG_ID_LINKED_INDIRECTLY, true);
1458                 }
1459                 else if (id_type == ID_SCE) {
1460                         /* Scenes are used by compositor trees, and handled by render
1461                          * pipeline. No need to build dependencies for them here.
1462                          */
1463                 }
1464                 else if (id_type == ID_TXT) {
1465                         /* Ignore script nodes. */
1466                 }
1467                 else if (id_type == ID_MSK) {
1468                         build_mask((Mask *)id);
1469                 }
1470                 else if (id_type == ID_MC) {
1471                         build_movieclip((MovieClip *)id);
1472                 }
1473                 else if (bnode->type == NODE_GROUP) {
1474                         bNodeTree *group_ntree = (bNodeTree *)id;
1475                         build_nodetree(group_ntree);
1476                 }
1477                 else {
1478                         BLI_assert(!"Unknown ID type used for node");
1479                 }
1480         }
1481
1482         // TODO: link from nodetree to owner_component?
1483 }
1484
1485 /* Recursively build graph for material */
1486 void DepsgraphNodeBuilder::build_material(Material *material)
1487 {
1488         if (built_map_.checkIsBuiltAndTag(material)) {
1489                 return;
1490         }
1491         /* Material itself. */
1492         add_id_node(&material->id);
1493         Material *material_cow = get_cow_datablock(material);
1494         /* Shading update. */
1495         add_operation_node(&material->id,
1496                            DEG_NODE_TYPE_SHADING,
1497                            function_bind(BKE_material_eval,
1498                                          _1,
1499                                          material_cow),
1500                            DEG_OPCODE_MATERIAL_UPDATE);
1501         /* Material animation. */
1502         build_animdata(&material->id);
1503         /* Material's nodetree. */
1504         build_nodetree(material->nodetree);
1505 }
1506
1507 /* Recursively build graph for texture */
1508 void DepsgraphNodeBuilder::build_texture(Tex *texture)
1509 {
1510         if (built_map_.checkIsBuiltAndTag(texture)) {
1511                 return;
1512         }
1513         /* Texture itself. */
1514         build_animdata(&texture->id);
1515         /* Texture's nodetree. */
1516         build_nodetree(texture->nodetree);
1517         /* Special cases for different IDs which texture uses. */
1518         if (texture->type == TEX_IMAGE) {
1519                 if (texture->ima != NULL) {
1520                         build_image(texture->ima);
1521                 }
1522         }
1523         /* Placeholder so we can add relations and tag ID node for update. */
1524         add_operation_node(&texture->id,
1525                            DEG_NODE_TYPE_PARAMETERS,
1526                            NULL,
1527                            DEG_OPCODE_PLACEHOLDER);
1528 }
1529
1530 void DepsgraphNodeBuilder::build_image(Image *image) {
1531         if (built_map_.checkIsBuiltAndTag(image)) {
1532                 return;
1533         }
1534         /* Placeholder so we can add relations and tag ID node for update. */
1535         add_operation_node(&image->id,
1536                            DEG_NODE_TYPE_PARAMETERS,
1537                            NULL,
1538                            DEG_OPCODE_PLACEHOLDER,
1539                            "Image Eval");
1540 }
1541
1542 void DepsgraphNodeBuilder::build_compositor(Scene *scene)
1543 {
1544         /* For now, just a plain wrapper? */
1545         // TODO: create compositing component?
1546         // XXX: component type undefined!
1547         //graph->get_node(&scene->id, NULL, DEG_NODE_TYPE_COMPOSITING, NULL);
1548
1549         /* for now, nodetrees are just parameters; compositing occurs in internals
1550          * of renderer...
1551          */
1552         add_component_node(&scene->id, DEG_NODE_TYPE_PARAMETERS);
1553         build_nodetree(scene->nodetree);
1554 }
1555
1556 void DepsgraphNodeBuilder::build_gpencil(bGPdata *gpd)
1557 {
1558         if (built_map_.checkIsBuiltAndTag(gpd)) {
1559                 return;
1560         }
1561         ID *gpd_id = &gpd->id;
1562
1563         /* TODO(sergey): what about multiple users of same datablock? This should
1564          * only get added once.
1565          */
1566
1567         /* The main reason Grease Pencil is included here is because the animation
1568          * (and drivers) need to be hosted somewhere.
1569          */
1570         build_animdata(gpd_id);
1571 }
1572
1573 void DepsgraphNodeBuilder::build_cachefile(CacheFile *cache_file)
1574 {
1575         if (built_map_.checkIsBuiltAndTag(cache_file)) {
1576                 return;
1577         }
1578         ID *cache_file_id = &cache_file->id;
1579         /* Animation, */
1580         build_animdata(cache_file_id);
1581         /* Cache evaluation itself. */
1582         add_operation_node(cache_file_id, DEG_NODE_TYPE_CACHE, NULL,
1583                            DEG_OPCODE_PLACEHOLDER, "Cache File Update");
1584 }
1585
1586 void DepsgraphNodeBuilder::build_mask(Mask *mask)
1587 {
1588         if (built_map_.checkIsBuiltAndTag(mask)) {
1589                 return;
1590         }
1591         ID *mask_id = &mask->id;
1592         Mask *mask_cow = get_cow_datablock(mask);
1593         /* F-Curve based animation. */
1594         build_animdata(mask_id);
1595         /* Animation based on mask's shapes. */
1596         add_operation_node(mask_id,
1597                            DEG_NODE_TYPE_ANIMATION,
1598                            function_bind(BKE_mask_eval_animation, _1, mask_cow),
1599                            DEG_OPCODE_MASK_ANIMATION);
1600         /* Final mask evaluation. */
1601         add_operation_node(mask_id,
1602                            DEG_NODE_TYPE_PARAMETERS,
1603                            function_bind(BKE_mask_eval_update, _1, mask_cow),
1604                            DEG_OPCODE_MASK_EVAL);
1605 }
1606
1607 void DepsgraphNodeBuilder::build_movieclip(MovieClip *clip)
1608 {
1609         if (built_map_.checkIsBuiltAndTag(clip)) {
1610                 return;
1611         }
1612         ID *clip_id = &clip->id;
1613         MovieClip *clip_cow = (MovieClip *)ensure_cow_id(clip_id);
1614         /* Animation. */
1615         build_animdata(clip_id);
1616         /* Movie clip evaluation. */
1617         add_operation_node(clip_id,
1618                            DEG_NODE_TYPE_PARAMETERS,
1619                            function_bind(BKE_movieclip_eval_update, _1, clip_cow),
1620                            DEG_OPCODE_MOVIECLIP_EVAL);
1621
1622         add_operation_node(clip_id,
1623                            DEG_NODE_TYPE_BATCH_CACHE,
1624                            function_bind(BKE_movieclip_eval_selection_update, _1, clip_cow),
1625                            DEG_OPCODE_MOVIECLIP_SELECT_UPDATE);
1626 }
1627
1628 void DepsgraphNodeBuilder::build_lightprobe(LightProbe *probe)
1629 {
1630         if (built_map_.checkIsBuiltAndTag(probe)) {
1631                 return;
1632         }
1633         /* Placeholder so we can add relations and tag ID node for update. */
1634         add_operation_node(&probe->id,
1635                            DEG_NODE_TYPE_PARAMETERS,
1636                            NULL,
1637                            DEG_OPCODE_LIGHT_PROBE_EVAL);
1638
1639         build_animdata(&probe->id);
1640 }
1641
1642 void DepsgraphNodeBuilder::build_speaker(Speaker *speaker)
1643 {
1644         if (built_map_.checkIsBuiltAndTag(speaker)) {
1645                 return;
1646         }
1647         /* Placeholder so we can add relations and tag ID node for update. */
1648         add_operation_node(&speaker->id,
1649                            DEG_NODE_TYPE_PARAMETERS,
1650                            NULL,
1651                            DEG_OPCODE_SPEAKER_EVAL);
1652         build_animdata(&speaker->id);
1653 }
1654
1655 /* **** ID traversal callbacks functions **** */
1656
1657 void DepsgraphNodeBuilder::modifier_walk(void *user_data,
1658                                          struct Object * /*object*/,
1659                                          struct ID **idpoin,
1660                                          int /*cb_flag*/)
1661 {
1662         BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
1663         ID *id = *idpoin;
1664         if (id == NULL) {
1665                 return;
1666         }
1667         switch (GS(id->name)) {
1668                 case ID_OB:
1669                         /* Special case for object, so we take owner visibility into
1670                          * account. */
1671                         data->builder->build_object(-1,
1672                                                     (Object *)id,
1673                                                     DEG_ID_LINKED_INDIRECTLY,
1674                                                     data->is_parent_visible);
1675                         break;
1676                 default:
1677                         data->builder->build_id(id);
1678                         break;
1679         }
1680 }
1681
1682 void DepsgraphNodeBuilder::constraint_walk(bConstraint * /*con*/,
1683                                            ID **idpoin,
1684                                            bool /*is_reference*/,
1685                                            void *user_data)
1686 {
1687         BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
1688         ID *id = *idpoin;
1689         if (id == NULL) {
1690                 return;
1691         }
1692         switch (GS(id->name)) {
1693                 case ID_OB:
1694                         /* Special case for object, so we take owner visibility into
1695                          * account. */
1696                         data->builder->build_object(-1,
1697                                                     (Object *)id,
1698                                                     DEG_ID_LINKED_INDIRECTLY,
1699                                                     data->is_parent_visible);
1700                         break;
1701                 default:
1702                         data->builder->build_id(id);
1703                         break;
1704         }
1705 }
1706
1707 }  // namespace DEG