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