Depsgraph: remove EvaluationContext, pass Depsgraph instead.
[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_constraint_types.h"
51 #include "DNA_curve_types.h"
52 #include "DNA_effect_types.h"
53 #include "DNA_gpencil_types.h"
54 #include "DNA_group_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_texture_types.h"
69 #include "DNA_world_types.h"
70
71 #include "BKE_action.h"
72 #include "BKE_armature.h"
73 #include "BKE_animsys.h"
74 #include "BKE_constraint.h"
75 #include "BKE_curve.h"
76 #include "BKE_effect.h"
77 #include "BKE_fcurve.h"
78 #include "BKE_idcode.h"
79 #include "BKE_group.h"
80 #include "BKE_key.h"
81 #include "BKE_lattice.h"
82 #include "BKE_library.h"
83 #include "BKE_main.h"
84 #include "BKE_mask.h"
85 #include "BKE_material.h"
86 #include "BKE_mesh.h"
87 #include "BKE_mball.h"
88 #include "BKE_modifier.h"
89 #include "BKE_movieclip.h"
90 #include "BKE_node.h"
91 #include "BKE_object.h"
92 #include "BKE_particle.h"
93 #include "BKE_pointcache.h"
94 #include "BKE_rigidbody.h"
95 #include "BKE_sound.h"
96 #include "BKE_tracking.h"
97 #include "BKE_world.h"
98
99 #include "RNA_access.h"
100 #include "RNA_types.h"
101 } /* extern "C" */
102
103 #include "DEG_depsgraph.h"
104 #include "DEG_depsgraph_build.h"
105
106 #include "intern/builder/deg_builder.h"
107 #include "intern/eval/deg_eval_copy_on_write.h"
108 #include "intern/nodes/deg_node.h"
109 #include "intern/nodes/deg_node_component.h"
110 #include "intern/nodes/deg_node_id.h"
111 #include "intern/nodes/deg_node_operation.h"
112 #include "intern/depsgraph_types.h"
113 #include "intern/depsgraph_intern.h"
114
115 #include "util/deg_util_foreach.h"
116
117 namespace DEG {
118
119 namespace {
120
121 void free_copy_on_write_datablock(void *id_v)
122 {
123         ID *id = (ID *)id_v;
124         deg_free_copy_on_write_datablock(id);
125         MEM_freeN(id);
126 }
127
128 }  /* namespace */
129
130 /* ************ */
131 /* Node Builder */
132
133 /* **** General purpose functions **** */
134
135 DepsgraphNodeBuilder::DepsgraphNodeBuilder(Main *bmain, Depsgraph *graph)
136     : bmain_(bmain),
137       graph_(graph),
138       scene_(NULL),
139       view_layer_(NULL),
140       cow_id_hash_(NULL)
141 {
142 }
143
144 DepsgraphNodeBuilder::~DepsgraphNodeBuilder()
145 {
146         if (cow_id_hash_ != NULL) {
147                 BLI_ghash_free(cow_id_hash_, NULL, free_copy_on_write_datablock);
148         }
149 }
150
151 IDDepsNode *DepsgraphNodeBuilder::add_id_node(ID *id)
152 {
153         if (!DEG_depsgraph_use_copy_on_write()) {
154                 return graph_->add_id_node(id);
155         }
156         IDDepsNode *id_node = NULL;
157         ID *id_cow = (ID *)BLI_ghash_lookup(cow_id_hash_, id);
158         if (id_cow != NULL) {
159                 /* TODO(sergey): Is it possible to lookup and pop element from GHash
160                  * at the same time?
161                  */
162                 BLI_ghash_remove(cow_id_hash_, id, NULL, NULL);
163         }
164         id_node = graph_->add_id_node(id, id_cow);
165         /* Currently all ID nodes are supposed to have copy-on-write logic.
166          *
167          * NOTE: Zero number of components indicates that ID node was just created.
168          */
169         if (BLI_ghash_len(id_node->components) == 0) {
170                 ComponentDepsNode *comp_cow =
171                         id_node->add_component(DEG_NODE_TYPE_COPY_ON_WRITE);
172                 OperationDepsNode *op_cow = comp_cow->add_operation(
173                         function_bind(deg_evaluate_copy_on_write, _1, id_node),
174                         DEG_OPCODE_COPY_ON_WRITE,
175                         "", -1);
176                 graph_->operations.push_back(op_cow);
177         }
178         return id_node;
179 }
180
181 IDDepsNode *DepsgraphNodeBuilder::find_id_node(ID *id)
182 {
183         return graph_->find_id_node(id);
184 }
185
186 TimeSourceDepsNode *DepsgraphNodeBuilder::add_time_source()
187 {
188         return graph_->add_time_source();
189 }
190
191 ComponentDepsNode *DepsgraphNodeBuilder::add_component_node(
192         ID *id,
193         eDepsNode_Type comp_type,
194         const char *comp_name)
195 {
196         IDDepsNode *id_node = add_id_node(id);
197         ComponentDepsNode *comp_node = id_node->add_component(comp_type, comp_name);
198         comp_node->owner = id_node;
199         return comp_node;
200 }
201
202 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
203         ComponentDepsNode *comp_node,
204         const DepsEvalOperationCb& op,
205         eDepsOperation_Code opcode,
206         const char *name,
207         int name_tag)
208 {
209         OperationDepsNode *op_node = comp_node->find_operation(opcode,
210                                                                name,
211                                                                name_tag);
212         if (op_node == NULL) {
213                 op_node = comp_node->add_operation(op, opcode, name, name_tag);
214                 graph_->operations.push_back(op_node);
215         }
216         else {
217                 fprintf(stderr,
218                         "add_operation: Operation already exists - %s has %s at %p\n",
219                         comp_node->identifier().c_str(),
220                         op_node->identifier().c_str(),
221                         op_node);
222                 BLI_assert(!"Should not happen!");
223         }
224         return op_node;
225 }
226
227 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
228         ID *id,
229         eDepsNode_Type comp_type,
230         const char *comp_name,
231         const DepsEvalOperationCb& op,
232         eDepsOperation_Code opcode,
233         const char *name,
234         int name_tag)
235 {
236         ComponentDepsNode *comp_node = add_component_node(id, comp_type, comp_name);
237         return add_operation_node(comp_node, op, opcode, name, name_tag);
238 }
239
240 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
241         ID *id,
242         eDepsNode_Type comp_type,
243         const DepsEvalOperationCb& op,
244         eDepsOperation_Code opcode,
245         const char *name,
246         int name_tag)
247 {
248         return add_operation_node(id,
249                                   comp_type,
250                                   "",
251                                   op,
252                                   opcode,
253                                   name,
254                                   name_tag);
255 }
256
257 OperationDepsNode *DepsgraphNodeBuilder::ensure_operation_node(
258         ID *id,
259         eDepsNode_Type comp_type,
260         const DepsEvalOperationCb& op,
261         eDepsOperation_Code opcode,
262         const char *name,
263         int name_tag)
264 {
265         OperationDepsNode *operation =
266                 find_operation_node(id, comp_type, opcode, name, name_tag);
267         if (operation != NULL) {
268                 return operation;
269         }
270         return add_operation_node(id, comp_type, op, opcode, name, name_tag);
271 }
272
273 bool DepsgraphNodeBuilder::has_operation_node(ID *id,
274                                               eDepsNode_Type comp_type,
275                                               const char *comp_name,
276                                               eDepsOperation_Code opcode,
277                                               const char *name,
278                                               int name_tag)
279 {
280         return find_operation_node(id,
281                                    comp_type,
282                                    comp_name,
283                                    opcode,
284                                    name,
285                                    name_tag) != NULL;
286 }
287
288 OperationDepsNode *DepsgraphNodeBuilder::find_operation_node(
289         ID *id,
290         eDepsNode_Type comp_type,
291         const char *comp_name,
292         eDepsOperation_Code opcode,
293         const char *name,
294         int name_tag)
295 {
296         ComponentDepsNode *comp_node = add_component_node(id, comp_type, comp_name);
297         return comp_node->find_operation(opcode, name, name_tag);
298 }
299
300 OperationDepsNode *DepsgraphNodeBuilder::find_operation_node(
301         ID *id,
302         eDepsNode_Type comp_type,
303         eDepsOperation_Code opcode,
304         const char *name,
305         int name_tag)
306 {
307         return find_operation_node(id, comp_type, "", opcode, name, name_tag);
308 }
309
310 ID *DepsgraphNodeBuilder::get_cow_id(const ID *id_orig) const
311 {
312         return graph_->get_cow_id(id_orig);
313 }
314
315 ID *DepsgraphNodeBuilder::ensure_cow_id(ID *id_orig)
316 {
317         if (id_orig->tag & LIB_TAG_COPY_ON_WRITE) {
318                 /* ID is already remapped to copy-on-write. */
319                 return id_orig;
320         }
321         IDDepsNode *id_node = add_id_node(id_orig);
322         return id_node->id_cow;
323 }
324
325 /* **** Build functions for entity nodes **** */
326
327 void DepsgraphNodeBuilder::begin_build() {
328         if (DEG_depsgraph_use_copy_on_write()) {
329                 /* Store existing copy-on-write versions of datablock, so we can re-use
330                  * them for new ID nodes.
331                  */
332                 cow_id_hash_ = BLI_ghash_ptr_new("Depsgraph id hash");
333                 foreach (IDDepsNode *id_node, graph_->id_nodes) {
334                         if (deg_copy_on_write_is_expanded(id_node->id_cow)) {
335                                 if (id_node->id_orig == id_node->id_cow) {
336                                         continue;
337                                 }
338                                 BLI_ghash_insert(cow_id_hash_,
339                                                  id_node->id_orig,
340                                                  id_node->id_cow);
341                                 id_node->id_cow = NULL;
342                         }
343                 }
344         }
345
346         GSET_FOREACH_BEGIN(OperationDepsNode *, op_node, graph_->entry_tags)
347         {
348                 ComponentDepsNode *comp_node = op_node->owner;
349                 IDDepsNode *id_node = comp_node->owner;
350
351                 SavedEntryTag entry_tag;
352                 entry_tag.id = id_node->id_orig;
353                 entry_tag.component_type = comp_node->type;
354                 entry_tag.opcode = op_node->opcode;
355                 saved_entry_tags_.push_back(entry_tag);
356         };
357         GSET_FOREACH_END();
358
359         /* Make sure graph has no nodes left from previous state. */
360         graph_->clear_all_nodes();
361         graph_->operations.clear();
362         BLI_gset_clear(graph_->entry_tags, NULL);
363 }
364
365 void DepsgraphNodeBuilder::end_build()
366 {
367         foreach (const SavedEntryTag& entry_tag, saved_entry_tags_) {
368                 IDDepsNode *id_node = find_id_node(entry_tag.id);
369                 if (id_node == NULL) {
370                         continue;
371                 }
372                 ComponentDepsNode *comp_node =
373                         id_node->find_component(entry_tag.component_type);
374                 if (comp_node == NULL) {
375                         continue;
376                 }
377                 OperationDepsNode *op_node = comp_node->find_operation(entry_tag.opcode);
378                 if (op_node == NULL) {
379                         continue;
380                 }
381                 op_node->tag_update(graph_);
382         }
383 }
384
385 void DepsgraphNodeBuilder::build_id(ID* id) {
386         if (id == NULL) {
387                 return;
388         }
389         switch (GS(id->name)) {
390                 case ID_GR:
391                         build_group((Group *)id);
392                         break;
393                 case ID_OB:
394                         build_object(-1, (Object *)id, DEG_ID_LINKED_INDIRECTLY);
395                         break;
396                 case ID_NT:
397                         build_nodetree((bNodeTree *)id);
398                         break;
399                 case ID_MA:
400                         build_material((Material *)id);
401                         break;
402                 case ID_TE:
403                         build_texture((Tex *)id);
404                         break;
405                 case ID_IM:
406                         build_image((Image *)id);
407                         break;
408                 case ID_WO:
409                         build_world((World *)id);
410                         break;
411                 case ID_MSK:
412                         build_mask((Mask *)id);
413                         break;
414                 case ID_MC:
415                         build_movieclip((MovieClip *)id);
416                         break;
417                 default:
418                         fprintf(stderr, "Unhandled ID %s\n", id->name);
419         }
420 }
421
422 void DepsgraphNodeBuilder::build_group(Group *group)
423 {
424         if (built_map_.checkIsBuiltAndTag(group)) {
425                 return;
426         }
427         /* Build group objects. */
428         LISTBASE_FOREACH (Base *, base, &group->view_layer->object_bases) {
429                 build_object(-1, base->object, DEG_ID_LINKED_INDIRECTLY);
430         }
431         /* Operation to evaluate the whole view layer.
432          *
433          * NOTE: We re-use DONE opcode even though the function does everything.
434          * This way we wouldn't need to worry about possible relations from DONE,
435          * regardless whether it's a group or scene or something else.
436          */
437         add_id_node(&group->id);
438         Group *group_cow = get_cow_datablock(group);
439         add_operation_node(&group->id,
440                            DEG_NODE_TYPE_LAYER_COLLECTIONS,
441                            function_bind(BKE_group_eval_view_layers,
442                                          _1,
443                                          group_cow),
444                            DEG_OPCODE_VIEW_LAYER_EVAL);
445 }
446
447 void DepsgraphNodeBuilder::build_object(int base_index,
448                                         Object *object,
449                                         eDepsNode_LinkedState_Type linked_state)
450 {
451         const bool has_object = built_map_.checkIsBuiltAndTag(object);
452         /* Skip rest of components if the ID node was already there. */
453         if (has_object) {
454                 IDDepsNode *id_node = find_id_node(&object->id);
455                 /* We need to build some extra stuff if object becomes linked
456                  * directly.
457                  */
458                 if (id_node->linked_state == DEG_ID_LINKED_INDIRECTLY) {
459                         build_object_flags(base_index, object, linked_state);
460                 }
461                 id_node->linked_state = max(id_node->linked_state, linked_state);
462                 return;
463         }
464         /* Create ID node for object and begin init. */
465         IDDepsNode *id_node = add_id_node(&object->id);
466         id_node->linked_state = linked_state;
467         object->customdata_mask = 0;
468         /* Various flags, flushing from bases/collections. */
469         build_object_flags(base_index, object, linked_state);
470         /* Transform. */
471         build_object_transform(object);
472         /* Parent. */
473         if (object->parent != NULL) {
474                 build_object(-1, object->parent, DEG_ID_LINKED_INDIRECTLY);
475         }
476         /* Modifiers. */
477         if (object->modifiers.first != NULL) {
478                 BuilderWalkUserData data;
479                 data.builder = this;
480                 modifiers_foreachIDLink(object, modifier_walk, &data);
481         }
482         /* Constraints. */
483         if (object->constraints.first != NULL) {
484                 BuilderWalkUserData data;
485                 data.builder = this;
486                 BKE_constraints_id_loop(&object->constraints, constraint_walk, &data);
487         }
488         /* Object data. */
489         build_object_data(object);
490         /* Build animation data,
491          *
492          * Do it now because it's possible object data will affect
493          * on object's level animation, for example in case of rebuilding
494          * pose for proxy.
495          */
496         OperationDepsNode *op_node = add_operation_node(&object->id,
497                                                         DEG_NODE_TYPE_PARAMETERS,
498                                                         NULL,
499                                                         DEG_OPCODE_PARAMETERS_EVAL);
500         op_node->set_as_exit();
501         build_animdata(&object->id);
502         /* Particle systems. */
503         if (object->particlesystem.first != NULL) {
504                 build_particles(object);
505         }
506         /* Grease pencil. */
507         if (object->gpd != NULL) {
508                 build_gpencil(object->gpd);
509         }
510         /* Object that this is a proxy for. */
511         if (object->proxy) {
512                 object->proxy->proxy_from = object;
513                 build_object(-1, object->proxy, DEG_ID_LINKED_INDIRECTLY);
514         }
515         /* Object dupligroup. */
516         if (object->dup_group != NULL) {
517                 build_group(object->dup_group);
518         }
519 }
520
521 void DepsgraphNodeBuilder::build_object_flags(
522         int base_index,
523         Object *object,
524         eDepsNode_LinkedState_Type linked_state)
525 {
526         if (base_index == -1) {
527                 return;
528         }
529         /* TODO(sergey): Is this really best component to be used? */
530         Object *object_cow = get_cow_datablock(object);
531         const bool is_from_set = (linked_state == DEG_ID_LINKED_VIA_SET);
532         add_operation_node(&object->id,
533                            DEG_NODE_TYPE_LAYER_COLLECTIONS,
534                            function_bind(BKE_object_eval_flush_base_flags,
535                                          _1,
536                                          object_cow, base_index,
537                                          is_from_set),
538                            DEG_OPCODE_OBJECT_BASE_FLAGS);
539 }
540
541 void DepsgraphNodeBuilder::build_object_data(Object *object)
542 {
543         if (object->data == NULL) {
544                 return;
545         }
546         IDDepsNode *id_node = graph_->find_id_node(&object->id);
547         /* type-specific data. */
548         switch (object->type) {
549                 case OB_MESH:
550                 case OB_CURVE:
551                 case OB_FONT:
552                 case OB_SURF:
553                 case OB_MBALL:
554                 case OB_LATTICE:
555                         build_obdata_geom(object);
556                         /* TODO(sergey): Only for until we support granular
557                          * update of curves.
558                          */
559                         if (object->type == OB_FONT) {
560                                 Curve *curve = (Curve *)object->data;
561                                 if (curve->textoncurve) {
562                                         id_node->eval_flags |= DAG_EVAL_NEED_CURVE_PATH;
563                                 }
564                         }
565                         break;
566                 case OB_ARMATURE:
567                         if (ID_IS_LINKED(object) && object->proxy_from != NULL) {
568                                 build_proxy_rig(object);
569                         }
570                         else {
571                                 build_rig(object);
572                         }
573                         break;
574                 case OB_LAMP:
575                         build_lamp(object);
576                         break;
577                 case OB_CAMERA:
578                         build_camera(object);
579                         break;
580                 case OB_LIGHTPROBE:
581                         build_lightprobe(object);
582                         break;
583                 default:
584                 {
585                         ID *obdata = (ID *)object->data;
586                         if (built_map_.checkIsBuilt(obdata) == 0) {
587                                 build_animdata(obdata);
588                         }
589                         break;
590                 }
591         }
592 }
593
594 void DepsgraphNodeBuilder::build_object_transform(Object *object)
595 {
596         OperationDepsNode *op_node;
597         Scene *scene_cow = get_cow_datablock(scene_);
598         Object *ob_cow = get_cow_datablock(object);
599
600         /* local transforms (from transform channels - loc/rot/scale + deltas) */
601         op_node = add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
602                                      function_bind(BKE_object_eval_local_transform,
603                                                    _1,
604                                                    ob_cow),
605                                      DEG_OPCODE_TRANSFORM_LOCAL);
606         op_node->set_as_entry();
607
608         /* object parent */
609         if (object->parent != NULL) {
610                 add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
611                                    function_bind(BKE_object_eval_parent,
612                                                  _1,
613                                                  scene_cow,
614                                                  ob_cow),
615                                    DEG_OPCODE_TRANSFORM_PARENT);
616         }
617
618         /* object constraints */
619         if (object->constraints.first != NULL) {
620                 build_object_constraints(object);
621         }
622
623         /* Rest of transformation update. */
624         add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
625                            function_bind(BKE_object_eval_uber_transform,
626                                          _1,
627                                          ob_cow),
628                            DEG_OPCODE_TRANSFORM_OBJECT_UBEREVAL);
629
630         /* object transform is done */
631         op_node = add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
632                                      function_bind(BKE_object_eval_done,
633                                                    _1,
634                                                    ob_cow),
635                                      DEG_OPCODE_TRANSFORM_FINAL);
636         op_node->set_as_exit();
637 }
638
639 /**
640  * Constraints Graph Notes
641  *
642  * For constraints, we currently only add a operation node to the Transform
643  * or Bone components (depending on whichever type of owner we have).
644  * This represents the entire constraints stack, which is for now just
645  * executed as a single monolithic block. At least initially, this should
646  * be sufficient for ensuring that the porting/refactoring process remains
647  * manageable.
648  *
649  * However, when the time comes for developing "node-based" constraints,
650  * we'll need to split this up into pre/post nodes for "constraint stack
651  * evaluation" + operation nodes for each constraint (i.e. the contents
652  * of the loop body used in the current "solve_constraints()" operation).
653  *
654  * -- Aligorith, August 2013
655  */
656 void DepsgraphNodeBuilder::build_object_constraints(Object *object)
657 {
658         /* create node for constraint stack */
659         add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
660                            function_bind(BKE_object_eval_constraints,
661                                          _1,
662                                          get_cow_datablock(scene_),
663                                          get_cow_datablock(object)),
664                            DEG_OPCODE_TRANSFORM_CONSTRAINTS);
665 }
666
667 /**
668  * Build graph nodes for AnimData block
669  * \param id: ID-Block which hosts the AnimData
670  */
671 void DepsgraphNodeBuilder::build_animdata(ID *id)
672 {
673         AnimData *adt = BKE_animdata_from_id(id);
674         if (adt == NULL) {
675                 return;
676         }
677
678         /* animation */
679         if (adt->action || adt->nla_tracks.first || adt->drivers.first) {
680                 (void) add_id_node(id);
681                 ID *id_cow = get_cow_id(id);
682
683                 // XXX: Hook up specific update callbacks for special properties which
684                 // may need it...
685
686                 /* actions and NLA - as a single unit for now, as it gets complicated to
687                  * schedule otherwise.
688                  */
689                 if ((adt->action) || (adt->nla_tracks.first)) {
690                         /* create the node */
691                         add_operation_node(id, DEG_NODE_TYPE_ANIMATION,
692                                            function_bind(BKE_animsys_eval_animdata,
693                                                          _1,
694                                                          id_cow),
695                                            DEG_OPCODE_ANIMATION,
696                                            id->name);
697
698                         /* TODO: for each channel affected, we might also want to add some
699                          * support for running RNA update callbacks on them
700                          * (which will be needed for proper handling of drivers later)
701                          */
702                 }
703
704                 /* drivers */
705                 LISTBASE_FOREACH (FCurve *, fcu, &adt->drivers) {
706                         /* create driver */
707                         build_driver(id, fcu);
708                 }
709         }
710 }
711
712 /**
713  * Build graph node(s) for Driver
714  * \param id: ID-Block that driver is attached to
715  * \param fcu: Driver-FCurve
716  */
717 void DepsgraphNodeBuilder::build_driver(ID *id, FCurve *fcurve)
718 {
719         ID *id_cow = get_cow_id(id);
720
721         /* Create data node for this driver */
722         /* TODO(sergey): Shall we use COW of fcu itself here? */
723         ensure_operation_node(id,
724                               DEG_NODE_TYPE_PARAMETERS,
725                               function_bind(BKE_animsys_eval_driver, _1, id_cow, fcurve),
726                               DEG_OPCODE_DRIVER,
727                               fcurve->rna_path ? fcurve->rna_path : "",
728                               fcurve->array_index);
729         build_driver_variables(id, fcurve);
730 }
731
732 void DepsgraphNodeBuilder::build_driver_variables(ID * id, FCurve *fcurve)
733 {
734         build_driver_id_property(id, fcurve->rna_path);
735         LISTBASE_FOREACH (DriverVar *, dvar, &fcurve->driver->variables) {
736                 DRIVER_TARGETS_USED_LOOPER(dvar)
737                 {
738                         build_id(dtar->id);
739                         build_driver_id_property(dtar->id, dtar->rna_path);
740                 }
741                 DRIVER_TARGETS_LOOPER_END
742         }
743 }
744
745 void DepsgraphNodeBuilder::build_driver_id_property(ID *id,
746                                                     const char *rna_path)
747 {
748         if (id == NULL || rna_path == NULL) {
749                 return;
750         }
751         PointerRNA id_ptr, ptr;
752         PropertyRNA *prop;
753         RNA_id_pointer_create(id, &id_ptr);
754         if (!RNA_path_resolve_full(&id_ptr, rna_path, &ptr, &prop, NULL)) {
755                 return;
756         }
757         if (prop == NULL) {
758                 return;
759         }
760         if (!RNA_property_is_idprop(prop)) {
761                 return;
762         }
763         const char *prop_identifier = RNA_property_identifier((PropertyRNA *)prop);
764         ensure_operation_node(id,
765                               DEG_NODE_TYPE_PARAMETERS,
766                               NULL,
767                               DEG_OPCODE_ID_PROPERTY,
768                               prop_identifier);
769 }
770
771 /* Recursively build graph for world */
772 void DepsgraphNodeBuilder::build_world(World *world)
773 {
774         if (built_map_.checkIsBuiltAndTag(world)) {
775                 return;
776         }
777         /* Animation. */
778         build_animdata(&world->id);
779         /* world itself */
780         add_operation_node(&world->id,
781                            DEG_NODE_TYPE_SHADING,
782                            function_bind(BKE_world_eval,
783                                          _1,
784                                          get_cow_datablock(world)),
785                            DEG_OPCODE_WORLD_UPDATE);
786         /* textures */
787         build_texture_stack(world->mtex);
788         /* world's nodetree */
789         if (world->nodetree != NULL) {
790                 build_nodetree(world->nodetree);
791         }
792 }
793
794 /* Rigidbody Simulation - Scene Level */
795 void DepsgraphNodeBuilder::build_rigidbody(Scene *scene)
796 {
797         RigidBodyWorld *rbw = scene->rigidbody_world;
798         Scene *scene_cow = get_cow_datablock(scene);
799
800         /**
801          * Rigidbody Simulation Nodes
802          * ==========================
803          *
804          * There are 3 nodes related to Rigidbody Simulation:
805          * 1) "Initialize/Rebuild World" - this is called sparingly, only when the
806          *    simulation needs to be rebuilt (mainly after file reload, or moving
807          *    back to start frame)
808          * 2) "Do Simulation" - perform a simulation step - interleaved between the
809          *    evaluation steps for clusters of objects (i.e. between those affected
810          *    and/or not affected by the sim for instance).
811          *
812          * 3) "Pull Results" - grab the specific transforms applied for a specific
813          *    object - performed as part of object's transform-stack building.
814          */
815
816         /* Create nodes --------------------------------------------------------- */
817
818         /* XXX: is this the right component, or do we want to use another one
819          * instead?
820          */
821
822         /* init/rebuild operation */
823         /*OperationDepsNode *init_node =*/ add_operation_node(
824                 &scene->id, DEG_NODE_TYPE_TRANSFORM,
825                 function_bind(BKE_rigidbody_rebuild_sim, _1, scene_cow),
826                 DEG_OPCODE_RIGIDBODY_REBUILD);
827
828         /* do-sim operation */
829         // XXX: what happens if we need to split into several groups?
830         OperationDepsNode *sim_node = add_operation_node(
831                 &scene->id, DEG_NODE_TYPE_TRANSFORM,
832                 function_bind(BKE_rigidbody_eval_simulation, _1, scene_cow),
833                 DEG_OPCODE_RIGIDBODY_SIM);
834
835         /* XXX: For now, the sim node is the only one that really matters here.
836          * If any other sims get added later, we may have to remove these hacks...
837          */
838         sim_node->owner->entry_operation = sim_node;
839         sim_node->owner->exit_operation  = sim_node;
840
841         /* objects - simulation participants */
842         if (rbw->group) {
843                 LISTBASE_FOREACH (Base *, base, &rbw->group->view_layer->object_bases) {
844                         Object *object = base->object;
845
846                         if (!object || (object->type != OB_MESH))
847                                 continue;
848
849                         /* 2) create operation for flushing results */
850                         /* object's transform component - where the rigidbody operation
851                          * lives. */
852                         add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
853                                            function_bind(
854                                                    BKE_rigidbody_object_sync_transforms,
855                                                    _1,
856                                                    scene_cow,
857                                                    get_cow_datablock(object)),
858                                            DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
859                 }
860         }
861 }
862
863 void DepsgraphNodeBuilder::build_particles(Object *object)
864 {
865         /**
866          * Particle Systems Nodes
867          * ======================
868          *
869          * There are two types of nodes associated with representing
870          * particle systems:
871          *  1) Component (EVAL_PARTICLES) - This is the particle-system
872          *     evaluation context for an object. It acts as the container
873          *     for all the nodes associated with a particular set of particle
874          *     systems.
875          *  2) Particle System Eval Operation - This operation node acts as a
876          *     blackbox evaluation step for one particle system referenced by
877          *     the particle systems stack. All dependencies link to this operation.
878          */
879         /* Component for all particle systems. */
880         ComponentDepsNode *psys_comp =
881                 add_component_node(&object->id, DEG_NODE_TYPE_EVAL_PARTICLES);
882
883         /* TODO(sergey): Need to get COW of PSYS. */
884         Scene *scene_cow = get_cow_datablock(scene_);
885         Object *ob_cow = get_cow_datablock(object);
886
887         add_operation_node(psys_comp,
888                            function_bind(BKE_particle_system_eval_init,
889                                          _1,
890                                          scene_cow,
891                                          ob_cow),
892                            DEG_OPCODE_PARTICLE_SYSTEM_EVAL_INIT);
893         /* Build all particle systems. */
894         LISTBASE_FOREACH (ParticleSystem *, psys, &object->particlesystem) {
895                 ParticleSettings *part = psys->part;
896                 /* Build particle settings operations.
897                  *
898                  * NOTE: The call itself ensures settings are only build once.
899                  */
900                 build_particle_settings(part);
901                 /* Particle system evaluation. */
902                 add_operation_node(psys_comp,
903                                    NULL,
904                                    DEG_OPCODE_PARTICLE_SYSTEM_EVAL,
905                                    psys->name);
906                 /* Visualization of particle system. */
907                 switch (part->ren_as) {
908                         case PART_DRAW_OB:
909                                 if (part->dup_ob != NULL) {
910                                         build_object(-1,
911                                                      part->dup_ob,
912                                                      DEG_ID_LINKED_INDIRECTLY);
913                                 }
914                                 break;
915                         case PART_DRAW_GR:
916                                 if (part->dup_group != NULL) {
917                                         build_group(part->dup_group);
918                                 }
919                                 break;
920                 }
921         }
922
923         /* TODO(sergey): Do we need a point cache operations here? */
924         add_operation_node(&object->id,
925                            DEG_NODE_TYPE_CACHE,
926                            function_bind(BKE_ptcache_object_reset,
927                                          scene_cow,
928                                          ob_cow,
929                                          PTCACHE_RESET_DEPSGRAPH),
930                            DEG_OPCODE_POINT_CACHE_RESET);
931 }
932
933 void DepsgraphNodeBuilder::build_particle_settings(ParticleSettings *part) {
934         if (built_map_.checkIsBuiltAndTag(part)) {
935                 return;
936         }
937         /* Animation data. */
938         build_animdata(&part->id);
939         /* Parameters change. */
940         add_operation_node(&part->id,
941                            DEG_NODE_TYPE_PARAMETERS,
942                            NULL,
943                            DEG_OPCODE_PARTICLE_SETTINGS_EVAL);
944 }
945
946 void DepsgraphNodeBuilder::build_cloth(Object *object)
947 {
948         Scene *scene_cow = get_cow_datablock(scene_);
949         Object *object_cow = get_cow_datablock(object);
950         add_operation_node(&object->id,
951                            DEG_NODE_TYPE_CACHE,
952                            function_bind(BKE_object_eval_cloth,
953                                          _1,
954                                          scene_cow,
955                                          object_cow),
956                            DEG_OPCODE_GEOMETRY_CLOTH_MODIFIER);
957 }
958
959 /* Shapekeys */
960 void DepsgraphNodeBuilder::build_shapekeys(Key *key)
961 {
962         build_animdata(&key->id);
963         add_operation_node(&key->id,
964                            DEG_NODE_TYPE_GEOMETRY,
965                            NULL,
966                            DEG_OPCODE_GEOMETRY_SHAPEKEY);
967 }
968
969 /* ObData Geometry Evaluation */
970 // XXX: what happens if the datablock is shared!
971 void DepsgraphNodeBuilder::build_obdata_geom(Object *object)
972 {
973         OperationDepsNode *op_node;
974         Scene *scene_cow = get_cow_datablock(scene_);
975         Object *object_cow = get_cow_datablock(object);
976
977         /* Temporary uber-update node, which does everything.
978          * It is for the being we're porting old dependencies into the new system.
979          * We'll get rid of this node as soon as all the granular update functions
980          * are filled in.
981          *
982          * TODO(sergey): Get rid of this node.
983          */
984         op_node = add_operation_node(&object->id,
985                                      DEG_NODE_TYPE_GEOMETRY,
986                                      function_bind(BKE_object_eval_uber_data,
987                                                    _1,
988                                                    scene_cow,
989                                                    object_cow),
990                                      DEG_OPCODE_GEOMETRY_UBEREVAL);
991         op_node->set_as_exit();
992
993         op_node = add_operation_node(&object->id,
994                                      DEG_NODE_TYPE_GEOMETRY,
995                                      NULL,
996                                      DEG_OPCODE_PLACEHOLDER,
997                                      "Eval Init");
998         op_node->set_as_entry();
999
1000         // TODO: "Done" operation
1001
1002         /* Cloth modifier. */
1003         LISTBASE_FOREACH (ModifierData *, md, &object->modifiers) {
1004                 if (md->type == eModifierType_Cloth) {
1005                         build_cloth(object);
1006                 }
1007         }
1008
1009         /* materials */
1010         if (object->totcol != 0) {
1011                 if (object->type == OB_MESH) {
1012                         add_operation_node(&object->id,
1013                                            DEG_NODE_TYPE_SHADING,
1014                                            function_bind(BKE_object_eval_update_shading,
1015                                                          _1,
1016                                                          object_cow),
1017                                            DEG_OPCODE_SHADING);
1018                 }
1019
1020                 for (int a = 1; a <= object->totcol; a++) {
1021                         Material *ma = give_current_material(object, a);
1022                         if (ma != NULL) {
1023                                 build_material(ma);
1024                         }
1025                 }
1026         }
1027
1028         /* geometry collision */
1029         if (ELEM(object->type, OB_MESH, OB_CURVE, OB_LATTICE)) {
1030                 // add geometry collider relations
1031         }
1032
1033         ID *obdata = (ID *)object->data;
1034         if (built_map_.checkIsBuiltAndTag(obdata)) {
1035                 return;
1036         }
1037         /* Make sure we've got an ID node before requesting CoW pointer. */
1038         (void) add_id_node((ID *)obdata);
1039         ID *obdata_cow = get_cow_id(obdata);
1040
1041         /* ShapeKeys */
1042         Key *key = BKE_key_from_object(object);
1043         if (key) {
1044                 build_shapekeys(key);
1045         }
1046
1047         build_animdata(obdata);
1048
1049         /* Nodes for result of obdata's evaluation, and geometry
1050          * evaluation on object.
1051          */
1052         switch (object->type) {
1053                 case OB_MESH:
1054                 {
1055                         //Mesh *me = (Mesh *)object->data;
1056
1057                         /* evaluation operations */
1058                         op_node = add_operation_node(obdata,
1059                                                      DEG_NODE_TYPE_GEOMETRY,
1060                                                      function_bind(BKE_mesh_eval_geometry,
1061                                                                    _1,
1062                                                                    (Mesh *)obdata_cow),
1063                                                      DEG_OPCODE_PLACEHOLDER,
1064                                                      "Geometry Eval");
1065                         op_node->set_as_entry();
1066                         break;
1067                 }
1068
1069                 case OB_MBALL:
1070                 {
1071                         Object *mom = BKE_mball_basis_find(scene_, object);
1072                         /* NOTE: Only the motherball gets evaluated, it's children are
1073                          * having empty placeholders for the correct relations being built.
1074                          */
1075                         if (mom == object) {
1076                                 /* metaball evaluation operations */
1077                                 op_node = add_operation_node(obdata,
1078                                                              DEG_NODE_TYPE_GEOMETRY,
1079                                                              function_bind(
1080                                                                      BKE_mball_eval_geometry,
1081                                                                      _1,
1082                                                                      (MetaBall *)obdata_cow),
1083                                                              DEG_OPCODE_PLACEHOLDER,
1084                                                              "Geometry Eval");
1085                         }
1086                         else {
1087                                 op_node = add_operation_node(obdata,
1088                                                              DEG_NODE_TYPE_GEOMETRY,
1089                                                              NULL,
1090                                                              DEG_OPCODE_PLACEHOLDER,
1091                                                              "Geometry Eval");
1092                                 op_node->set_as_entry();
1093                         }
1094                         break;
1095                 }
1096
1097                 case OB_CURVE:
1098                 case OB_SURF:
1099                 case OB_FONT:
1100                 {
1101                         /* Curve/nurms evaluation operations. */
1102                         /* - calculate curve geometry (including path) */
1103                         op_node = add_operation_node(obdata,
1104                                                      DEG_NODE_TYPE_GEOMETRY,
1105                                                      function_bind(BKE_curve_eval_geometry,
1106                                                                    _1,
1107                                                                    (Curve *)obdata_cow),
1108                                                                    DEG_OPCODE_PLACEHOLDER,
1109                                                                    "Geometry Eval");
1110                         op_node->set_as_entry();
1111                         /* Make sure objects used for bevel.taper are in the graph.
1112                          * NOTE: This objects might be not linked to the scene.
1113                          */
1114                         Curve *cu = (Curve *)obdata;
1115                         if (cu->bevobj != NULL) {
1116                                 build_object(-1, cu->bevobj, DEG_ID_LINKED_INDIRECTLY);
1117                         }
1118                         if (cu->taperobj != NULL) {
1119                                 build_object(-1, cu->taperobj, DEG_ID_LINKED_INDIRECTLY);
1120                         }
1121                         if (object->type == OB_FONT && cu->textoncurve != NULL) {
1122                                 build_object(-1, cu->textoncurve, DEG_ID_LINKED_INDIRECTLY);
1123                         }
1124                         break;
1125                 }
1126
1127                 case OB_LATTICE:
1128                 {
1129                         /* Lattice evaluation operations. */
1130                         op_node = add_operation_node(obdata,
1131                                                      DEG_NODE_TYPE_GEOMETRY,
1132                                                      function_bind(BKE_lattice_eval_geometry,
1133                                                                    _1,
1134                                                                    (Lattice *)obdata_cow),
1135                                                                    DEG_OPCODE_PLACEHOLDER,
1136                                                                    "Geometry Eval");
1137                         op_node->set_as_entry();
1138                         break;
1139                 }
1140         }
1141
1142         op_node = add_operation_node(obdata, DEG_NODE_TYPE_GEOMETRY, NULL,
1143                                      DEG_OPCODE_PLACEHOLDER, "Eval Done");
1144         op_node->set_as_exit();
1145
1146         /* Parameters for driver sources. */
1147         add_operation_node(obdata,
1148                            DEG_NODE_TYPE_PARAMETERS,
1149                            NULL,
1150                            DEG_OPCODE_PARAMETERS_EVAL);
1151
1152         /* Batch cache. */
1153         add_operation_node(obdata,
1154                            DEG_NODE_TYPE_BATCH_CACHE,
1155                            function_bind(BKE_object_data_select_update,
1156                                          _1,
1157                                          obdata_cow),
1158                            DEG_OPCODE_GEOMETRY_SELECT_UPDATE);
1159 }
1160
1161 /* Cameras */
1162 void DepsgraphNodeBuilder::build_camera(Object *object)
1163 {
1164         /* Object data. */
1165         /* TODO: Link scene-camera links in somehow... */
1166         Camera *camera = (Camera *)object->data;
1167         if (built_map_.checkIsBuiltAndTag(camera)) {
1168                 return;
1169         }
1170         build_animdata(&camera->id);
1171         add_operation_node(&camera->id,
1172                            DEG_NODE_TYPE_PARAMETERS,
1173                            NULL,
1174                            DEG_OPCODE_PARAMETERS_EVAL);
1175 }
1176
1177 /* Lamps */
1178 void DepsgraphNodeBuilder::build_lamp(Object *object)
1179 {
1180         /* Object data. */
1181         Lamp *lamp = (Lamp *)object->data;
1182         if (built_map_.checkIsBuiltAndTag(lamp)) {
1183                 return;
1184         }
1185         build_animdata(&lamp->id);
1186         add_operation_node(&lamp->id,
1187                            DEG_NODE_TYPE_PARAMETERS,
1188                            NULL,
1189                            DEG_OPCODE_PARAMETERS_EVAL);
1190         /* lamp's nodetree */
1191         build_nodetree(lamp->nodetree);
1192         /* textures */
1193         build_texture_stack(lamp->mtex);
1194 }
1195
1196 void DepsgraphNodeBuilder::build_nodetree(bNodeTree *ntree)
1197 {
1198         if (ntree == NULL) {
1199                 return;
1200         }
1201         if (built_map_.checkIsBuiltAndTag(ntree)) {
1202                 return;
1203         }
1204         /* nodetree itself */
1205         add_id_node(&ntree->id);
1206         bNodeTree *ntree_cow = get_cow_datablock(ntree);
1207         /* Animation, */
1208         build_animdata(&ntree->id);
1209         /* Shading update. */
1210         add_operation_node(&ntree->id,
1211                            DEG_NODE_TYPE_SHADING,
1212                            NULL,
1213                            DEG_OPCODE_MATERIAL_UPDATE);
1214         /* NOTE: We really pass original and CoW node trees here, this is how the
1215          * callback works. Ideally we need to find a better way for that.
1216          */
1217         add_operation_node(&ntree->id,
1218                            DEG_NODE_TYPE_SHADING_PARAMETERS,
1219                            function_bind(BKE_nodetree_shading_params_eval,
1220                                          _1,
1221                                          ntree_cow,
1222                                          ntree),
1223                            DEG_OPCODE_MATERIAL_UPDATE);
1224         /* nodetree's nodes... */
1225         LISTBASE_FOREACH (bNode *, bnode, &ntree->nodes) {
1226                 ID *id = bnode->id;
1227                 if (id == NULL) {
1228                         continue;
1229                 }
1230                 ID_Type id_type = GS(id->name);
1231                 if (id_type == ID_MA) {
1232                         build_material((Material *)id);
1233                 }
1234                 else if (id_type == ID_TE) {
1235                         build_texture((Tex *)id);
1236                 }
1237                 else if (id_type == ID_IM) {
1238                         build_image((Image *)id);
1239                 }
1240                 else if (id_type == ID_OB) {
1241                         build_object(-1, (Object *)id, DEG_ID_LINKED_INDIRECTLY);
1242                 }
1243                 else if (id_type == ID_SCE) {
1244                         /* Scenes are used by compositor trees, and handled by render
1245                          * pipeline. No need to build dependencies for them here.
1246                          */
1247                 }
1248                 else if (id_type == ID_TXT) {
1249                         /* Ignore script nodes. */
1250                 }
1251                 else if (bnode->type == NODE_GROUP) {
1252                         bNodeTree *group_ntree = (bNodeTree *)id;
1253                         build_nodetree(group_ntree);
1254                 }
1255                 else {
1256                         BLI_assert(!"Unknown ID type used for node");
1257                 }
1258         }
1259
1260         // TODO: link from nodetree to owner_component?
1261 }
1262
1263 /* Recursively build graph for material */
1264 void DepsgraphNodeBuilder::build_material(Material *material)
1265 {
1266         if (built_map_.checkIsBuiltAndTag(material)) {
1267                 return;
1268         }
1269         /* Material itself. */
1270         add_id_node(&material->id);
1271         Material *material_cow = get_cow_datablock(material);
1272         /* Shading update. */
1273         add_operation_node(&material->id,
1274                            DEG_NODE_TYPE_SHADING,
1275                            function_bind(BKE_material_eval,
1276                                          _1,
1277                                          material_cow),
1278                            DEG_OPCODE_MATERIAL_UPDATE);
1279         /* Material animation. */
1280         build_animdata(&material->id);
1281         /* Textures. */
1282         build_texture_stack(material->mtex);
1283         /* Material's nodetree. */
1284         build_nodetree(material->nodetree);
1285 }
1286
1287 /* Texture-stack attached to some shading datablock */
1288 void DepsgraphNodeBuilder::build_texture_stack(MTex **texture_stack)
1289 {
1290         /* for now assume that all texture-stacks have same number of max items */
1291         for (int i = 0; i < MAX_MTEX; i++) {
1292                 MTex *mtex = texture_stack[i];
1293                 if (mtex && mtex->tex) {
1294                         build_texture(mtex->tex);
1295                 }
1296         }
1297 }
1298
1299 /* Recursively build graph for texture */
1300 void DepsgraphNodeBuilder::build_texture(Tex *texture)
1301 {
1302         if (built_map_.checkIsBuiltAndTag(texture)) {
1303                 return;
1304         }
1305         /* Texture itself. */
1306         build_animdata(&texture->id);
1307         /* Texture's nodetree. */
1308         build_nodetree(texture->nodetree);
1309         /* Special cases for different IDs which texture uses. */
1310         if (texture->type == TEX_IMAGE) {
1311                 if (texture->ima != NULL) {
1312                         build_image(texture->ima);
1313                 }
1314         }
1315         /* Placeholder so we can add relations and tag ID node for update. */
1316         add_operation_node(&texture->id,
1317                            DEG_NODE_TYPE_PARAMETERS,
1318                            NULL,
1319                            DEG_OPCODE_PLACEHOLDER);
1320 }
1321
1322 void DepsgraphNodeBuilder::build_image(Image *image) {
1323         if (built_map_.checkIsBuiltAndTag(image)) {
1324                 return;
1325         }
1326         /* Placeholder so we can add relations and tag ID node for update. */
1327         add_operation_node(&image->id,
1328                            DEG_NODE_TYPE_PARAMETERS,
1329                            NULL,
1330                            DEG_OPCODE_PLACEHOLDER,
1331                            "Image Eval");
1332 }
1333
1334 void DepsgraphNodeBuilder::build_compositor(Scene *scene)
1335 {
1336         /* For now, just a plain wrapper? */
1337         // TODO: create compositing component?
1338         // XXX: component type undefined!
1339         //graph->get_node(&scene->id, NULL, DEG_NODE_TYPE_COMPOSITING, NULL);
1340
1341         /* for now, nodetrees are just parameters; compositing occurs in internals
1342          * of renderer...
1343          */
1344         add_component_node(&scene->id, DEG_NODE_TYPE_PARAMETERS);
1345         build_nodetree(scene->nodetree);
1346 }
1347
1348 void DepsgraphNodeBuilder::build_gpencil(bGPdata *gpd)
1349 {
1350         ID *gpd_id = &gpd->id;
1351
1352         /* TODO(sergey): what about multiple users of same datablock? This should
1353          * only get added once.
1354          */
1355
1356         /* The main reason Grease Pencil is included here is because the animation
1357          * (and drivers) need to be hosted somewhere.
1358          */
1359         build_animdata(gpd_id);
1360 }
1361
1362 void DepsgraphNodeBuilder::build_cachefile(CacheFile *cache_file)
1363 {
1364         ID *cache_file_id = &cache_file->id;
1365         /* Animation, */
1366         build_animdata(cache_file_id);
1367         /* Cache evaluation itself. */
1368         add_operation_node(cache_file_id, DEG_NODE_TYPE_CACHE, NULL,
1369                            DEG_OPCODE_PLACEHOLDER, "Cache File Update");
1370 }
1371
1372 void DepsgraphNodeBuilder::build_mask(Mask *mask)
1373 {
1374         ID *mask_id = &mask->id;
1375         Mask *mask_cow = get_cow_datablock(mask);
1376         /* F-Curve based animation. */
1377         build_animdata(mask_id);
1378         /* Animation based on mask's shapes. */
1379         add_operation_node(mask_id,
1380                            DEG_NODE_TYPE_ANIMATION,
1381                            function_bind(BKE_mask_eval_animation, _1, mask_cow),
1382                            DEG_OPCODE_MASK_ANIMATION);
1383         /* Final mask evaluation. */
1384         add_operation_node(mask_id,
1385                            DEG_NODE_TYPE_PARAMETERS,
1386                            function_bind(BKE_mask_eval_update, _1, mask_cow),
1387                            DEG_OPCODE_MASK_EVAL);
1388 }
1389
1390 void DepsgraphNodeBuilder::build_movieclip(MovieClip *clip)
1391 {
1392         ID *clip_id = &clip->id;
1393         MovieClip *clip_cow = get_cow_datablock(clip);
1394         /* Animation. */
1395         build_animdata(clip_id);
1396         /* Movie clip evaluation. */
1397         add_operation_node(clip_id,
1398                            DEG_NODE_TYPE_PARAMETERS,
1399                            function_bind(BKE_movieclip_eval_update, _1, clip_cow),
1400                            DEG_OPCODE_MOVIECLIP_EVAL);
1401 }
1402
1403 void DepsgraphNodeBuilder::build_lightprobe(Object *object)
1404 {
1405         LightProbe *probe = (LightProbe *)object->data;
1406         if (built_map_.checkIsBuiltAndTag(probe)) {
1407                 return;
1408         }
1409         /* Placeholder so we can add relations and tag ID node for update. */
1410         add_operation_node(&probe->id,
1411                            DEG_NODE_TYPE_PARAMETERS,
1412                            NULL,
1413                            DEG_OPCODE_PLACEHOLDER,
1414                            "LightProbe Eval");
1415         add_operation_node(&object->id,
1416                            DEG_NODE_TYPE_PARAMETERS,
1417                            NULL,
1418                            DEG_OPCODE_PLACEHOLDER,
1419                            "LightProbe Eval");
1420
1421         build_animdata(&probe->id);
1422 }
1423
1424 /* **** ID traversal callbacks functions **** */
1425
1426 void DepsgraphNodeBuilder::modifier_walk(void *user_data,
1427                                          struct Object * /*object*/,
1428                                          struct ID **idpoin,
1429                                          int /*cb_flag*/)
1430 {
1431         BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
1432         ID *id = *idpoin;
1433         if (id == NULL) {
1434                 return;
1435         }
1436         switch (GS(id->name)) {
1437                 case ID_OB:
1438                         data->builder->build_object(-1,
1439                                                     (Object *)id,
1440                                                     DEG_ID_LINKED_INDIRECTLY);
1441                         break;
1442                 case ID_TE:
1443                         data->builder->build_texture((Tex *)id);
1444                         break;
1445                 default:
1446                         /* pass */
1447                         break;
1448         }
1449 }
1450
1451 void DepsgraphNodeBuilder::constraint_walk(bConstraint * /*con*/,
1452                                            ID **idpoin,
1453                                            bool /*is_reference*/,
1454                                            void *user_data)
1455 {
1456         BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
1457         ID *id = *idpoin;
1458         if (id == NULL) {
1459                 return;
1460         }
1461         switch (GS(id->name)) {
1462                 case ID_OB:
1463                         data->builder->build_object(-1,
1464                                                     (Object *)id,
1465                                                     DEG_ID_LINKED_INDIRECTLY);
1466                         break;
1467                 default:
1468                         /* pass */
1469                         break;
1470         }
1471 }
1472
1473 }  // namespace DEG