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