Depsgraph: Cleanup, remove redundant calls to add_component
[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_rigidbody_types.h"
66 #include "DNA_scene_types.h"
67 #include "DNA_texture_types.h"
68 #include "DNA_world_types.h"
69
70 #include "BKE_action.h"
71 #include "BKE_armature.h"
72 #include "BKE_animsys.h"
73 #include "BKE_constraint.h"
74 #include "BKE_curve.h"
75 #include "BKE_depsgraph.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_rigidbody.h"
94 #include "BKE_sound.h"
95 #include "BKE_texture.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/nodes/deg_node.h"
108 #include "intern/nodes/deg_node_component.h"
109 #include "intern/nodes/deg_node_operation.h"
110 #include "intern/depsgraph_types.h"
111 #include "intern/depsgraph_intern.h"
112 #include "util/deg_util_foreach.h"
113
114 namespace DEG {
115
116 namespace {
117
118 struct BuilderWalkUserData {
119         DepsgraphNodeBuilder *builder;
120         Scene *scene;
121 };
122
123 static void modifier_walk(void *user_data,
124                           struct Object * /*ob*/,
125                           struct Object **obpoin,
126                           int /*cb_flag*/)
127 {
128         BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
129         if (*obpoin) {
130                 data->builder->build_object(data->scene, NULL, *obpoin);
131         }
132 }
133
134 void constraint_walk(bConstraint * /*con*/,
135                      ID **idpoin,
136                      bool /*is_reference*/,
137                      void *user_data)
138 {
139         BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
140         if (*idpoin) {
141                 ID *id = *idpoin;
142                 if (GS(id->name) == ID_OB) {
143                         data->builder->build_object(data->scene, NULL, (Object *)id);
144                 }
145         }
146 }
147
148 }  /* namespace */
149
150 /* ************ */
151 /* Node Builder */
152
153 /* **** General purpose functions **** */
154
155 DepsgraphNodeBuilder::DepsgraphNodeBuilder(Main *bmain, Depsgraph *graph) :
156     m_bmain(bmain),
157     m_graph(graph)
158 {
159 }
160
161 DepsgraphNodeBuilder::~DepsgraphNodeBuilder()
162 {
163 }
164
165 IDDepsNode *DepsgraphNodeBuilder::add_id_node(ID *id)
166 {
167         return m_graph->add_id_node(id, id->name);
168 }
169
170 TimeSourceDepsNode *DepsgraphNodeBuilder::add_time_source()
171 {
172         return m_graph->add_time_source();
173 }
174
175 ComponentDepsNode *DepsgraphNodeBuilder::add_component_node(
176         ID *id,
177         eDepsNode_Type comp_type,
178         const char *comp_name)
179 {
180         IDDepsNode *id_node = add_id_node(id);
181         ComponentDepsNode *comp_node = id_node->add_component(comp_type, comp_name);
182         comp_node->owner = id_node;
183         return comp_node;
184 }
185
186 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
187         ComponentDepsNode *comp_node,
188         const DepsEvalOperationCb& op,
189         eDepsOperation_Code opcode,
190         const char *name,
191         int name_tag)
192 {
193         OperationDepsNode *op_node = comp_node->has_operation(opcode,
194                                                               name,
195                                                               name_tag);
196         if (op_node == NULL) {
197                 op_node = comp_node->add_operation(op, opcode, name, name_tag);
198                 m_graph->operations.push_back(op_node);
199         }
200         else {
201                 fprintf(stderr,
202                         "add_operation: Operation already exists - %s has %s at %p\n",
203                         comp_node->identifier().c_str(),
204                         op_node->identifier().c_str(),
205                         op_node);
206                 BLI_assert(!"Should not happen!");
207         }
208         return op_node;
209 }
210
211 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
212         ID *id,
213         eDepsNode_Type comp_type,
214         const char *comp_name,
215         const DepsEvalOperationCb& op,
216         eDepsOperation_Code opcode,
217         const char *name,
218         int name_tag)
219 {
220         ComponentDepsNode *comp_node = add_component_node(id, comp_type, comp_name);
221         return add_operation_node(comp_node, op, opcode, name, name_tag);
222 }
223
224 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
225         ID *id,
226         eDepsNode_Type comp_type,
227         const DepsEvalOperationCb& op,
228         eDepsOperation_Code opcode,
229         const char *name,
230         int name_tag)
231 {
232         return add_operation_node(id,
233                                   comp_type,
234                                   "",
235                                   op,
236                                   opcode,
237                                   name,
238                                   name_tag);
239 }
240
241 bool DepsgraphNodeBuilder::has_operation_node(ID *id,
242                                               eDepsNode_Type comp_type,
243                                               const char *comp_name,
244                                               eDepsOperation_Code opcode,
245                                               const char *name,
246                                               int name_tag)
247 {
248         return find_operation_node(id,
249                                    comp_type,
250                                    comp_name,
251                                    opcode,
252                                    name,
253                                    name_tag) != NULL;
254 }
255
256 OperationDepsNode *DepsgraphNodeBuilder::find_operation_node(
257         ID *id,
258         eDepsNode_Type comp_type,
259         const char *comp_name,
260         eDepsOperation_Code opcode,
261         const char *name,
262         int name_tag)
263 {
264         ComponentDepsNode *comp_node = add_component_node(id, comp_type, comp_name);
265         return comp_node->has_operation(opcode, name, name_tag);
266 }
267
268 OperationDepsNode *DepsgraphNodeBuilder::find_operation_node(
269         ID *id,
270         eDepsNode_Type comp_type,
271         eDepsOperation_Code opcode,
272         const char *name,
273         int name_tag)
274 {
275         return find_operation_node(id, comp_type, "", opcode, name, name_tag);
276 }
277
278 /* **** Build functions for entity nodes **** */
279
280 void DepsgraphNodeBuilder::begin_build(Main *bmain) {
281         /* LIB_TAG_DOIT is used to indicate whether node for given ID was already
282          * created or not. This flag is being set in add_id_node(), so functions
283          * shouldn't bother with setting it, they only might query this flag when
284          * needed.
285          */
286         BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
287         /* XXX nested node trees are not included in tag-clearing above,
288          * so we need to do this manually.
289          */
290         FOREACH_NODETREE(bmain, nodetree, id)
291         {
292                 if (id != (ID *)nodetree) {
293                         nodetree->id.tag &= ~LIB_TAG_DOIT;
294                 }
295         }
296         FOREACH_NODETREE_END;
297 }
298
299 void DepsgraphNodeBuilder::build_group(Scene *scene,
300                                        Base *base,
301                                        Group *group)
302 {
303         ID *group_id = &group->id;
304         if (group_id->tag & LIB_TAG_DOIT) {
305                 return;
306         }
307         group_id->tag |= LIB_TAG_DOIT;
308
309         LINKLIST_FOREACH (GroupObject *, go, &group->gobject) {
310                 build_object(scene, base, go->ob);
311         }
312 }
313
314 void DepsgraphNodeBuilder::build_object(Scene *scene, Base *base, Object *ob)
315 {
316         const bool has_object = (ob->id.tag & LIB_TAG_DOIT);
317         IDDepsNode *id_node = (has_object)
318                 ? m_graph->find_id_node(&ob->id)
319                 : add_id_node(&ob->id);
320         /* Update node layers.
321          * Do it for both new and existing ID nodes. This is so because several
322          * bases might be sharing same object.
323          */
324         if (base != NULL) {
325                 id_node->layers |= base->lay;
326         }
327         if (ob->type == OB_CAMERA) {
328                 /* Camera should always be updated, it used directly by viewport.
329                  *
330                  * TODO(sergey): Make it only for active scene camera.
331                  */
332                 id_node->layers |= (unsigned int)(-1);
333         }
334         /* Skip rest of components if the ID node was already there. */
335         if (has_object) {
336                 return;
337         }
338         ob->id.tag |= LIB_TAG_DOIT;
339         ob->customdata_mask = 0;
340
341         /* Standard components. */
342         build_object_transform(scene, ob);
343
344         if (ob->parent != NULL) {
345                 build_object(scene, NULL, ob->parent);
346         }
347         if (ob->modifiers.first != NULL) {
348                 BuilderWalkUserData data;
349                 data.builder = this;
350                 data.scene = scene;
351                 modifiers_foreachObjectLink(ob, modifier_walk, &data);
352         }
353         if (ob->constraints.first != NULL) {
354                 BuilderWalkUserData data;
355                 data.builder = this;
356                 data.scene = scene;
357                 BKE_constraints_id_loop(&ob->constraints, constraint_walk, &data);
358         }
359
360         /* Object data. */
361         if (ob->data != NULL) {
362                 /* type-specific data... */
363                 switch (ob->type) {
364                         case OB_MESH:     /* Geometry */
365                         case OB_CURVE:
366                         case OB_FONT:
367                         case OB_SURF:
368                         case OB_MBALL:
369                         case OB_LATTICE:
370                                 build_obdata_geom(scene, ob);
371                                 /* TODO(sergey): Only for until we support granular
372                                  * update of curves.
373                                  */
374                                 if (ob->type == OB_FONT) {
375                                         Curve *curve = (Curve *)ob->data;
376                                         if (curve->textoncurve) {
377                                                 id_node->eval_flags |= DAG_EVAL_NEED_CURVE_PATH;
378                                         }
379                                 }
380                                 break;
381
382                         case OB_ARMATURE: /* Pose */
383                                 if (ID_IS_LINKED_DATABLOCK(ob) && ob->proxy_from != NULL) {
384                                         build_proxy_rig(ob);
385                                 }
386                                 else {
387                                         build_rig(scene, ob);
388                                 }
389                                 break;
390
391                         case OB_LAMP:   /* Lamp */
392                                 build_lamp(ob);
393                                 break;
394
395                         case OB_CAMERA: /* Camera */
396                                 build_camera(ob);
397                                 break;
398
399                         default:
400                         {
401                                 ID *obdata = (ID *)ob->data;
402                                 if ((obdata->tag & LIB_TAG_DOIT) == 0) {
403                                         build_animdata(obdata);
404                                 }
405                                 break;
406                         }
407                 }
408         }
409
410         /* Build animation data,
411          *
412          * Do it now because it's possible object data will affect
413          * on object's level animation, for example in case of rebuilding
414          * pose for proxy.
415          */
416         build_animdata(&ob->id);
417
418         /* particle systems */
419         if (ob->particlesystem.first != NULL) {
420                 build_particles(scene, ob);
421         }
422
423         /* Grease pencil. */
424         if (ob->gpd != NULL) {
425                 build_gpencil(ob->gpd);
426         }
427
428         /* Object that this is a proxy for. */
429         if (ob->proxy) {
430                 ob->proxy->proxy_from = ob;
431                 build_object(scene, base, ob->proxy);
432         }
433
434         /* Object dupligroup. */
435         if (ob->dup_group != NULL) {
436                 build_group(scene, base, ob->dup_group);
437         }
438 }
439
440 void DepsgraphNodeBuilder::build_object_transform(Scene *scene, Object *ob)
441 {
442         OperationDepsNode *op_node;
443
444         /* local transforms (from transform channels - loc/rot/scale + deltas) */
445         op_node = add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
446                                      function_bind(BKE_object_eval_local_transform, _1, scene, ob),
447                                      DEG_OPCODE_TRANSFORM_LOCAL);
448         op_node->set_as_entry();
449
450         /* object parent */
451         if (ob->parent) {
452                 add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
453                                    function_bind(BKE_object_eval_parent, _1, scene, ob),
454                                    DEG_OPCODE_TRANSFORM_PARENT);
455         }
456
457         /* object constraints */
458         if (ob->constraints.first) {
459                 build_object_constraints(scene, ob);
460         }
461
462         /* Temporary uber-update node, which does everything.
463          * It is for the being we're porting old dependencies into the new system.
464          * We'll get rid of this node as soon as all the granular update functions
465          * are filled in.
466          *
467          * TODO(sergey): Get rid of this node.
468          */
469         add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
470                            function_bind(BKE_object_eval_uber_transform, _1, scene, ob),
471                            DEG_OPCODE_TRANSFORM_OBJECT_UBEREVAL);
472
473         /* object transform is done */
474         op_node = add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
475                                      function_bind(BKE_object_eval_done, _1, ob),
476                                      DEG_OPCODE_TRANSFORM_FINAL);
477         op_node->set_as_exit();
478 }
479
480 /**
481  * Constraints Graph Notes
482  *
483  * For constraints, we currently only add a operation node to the Transform
484  * or Bone components (depending on whichever type of owner we have).
485  * This represents the entire constraints stack, which is for now just
486  * executed as a single monolithic block. At least initially, this should
487  * be sufficient for ensuring that the porting/refactoring process remains
488  * manageable.
489  *
490  * However, when the time comes for developing "node-based" constraints,
491  * we'll need to split this up into pre/post nodes for "constraint stack
492  * evaluation" + operation nodes for each constraint (i.e. the contents
493  * of the loop body used in the current "solve_constraints()" operation).
494  *
495  * -- Aligorith, August 2013
496  */
497 void DepsgraphNodeBuilder::build_object_constraints(Scene *scene, Object *ob)
498 {
499         /* create node for constraint stack */
500         add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
501                            function_bind(BKE_object_eval_constraints, _1, scene, ob),
502                            DEG_OPCODE_TRANSFORM_CONSTRAINTS);
503 }
504
505 /**
506  * Build graph nodes for AnimData block
507  * \param id: ID-Block which hosts the AnimData
508  */
509 void DepsgraphNodeBuilder::build_animdata(ID *id)
510 {
511         AnimData *adt = BKE_animdata_from_id(id);
512
513         if (adt == NULL)
514                 return;
515
516         /* animation */
517         if (adt->action || adt->nla_tracks.first || adt->drivers.first) {
518                 // XXX: Hook up specific update callbacks for special properties which may need it...
519
520                 /* actions and NLA - as a single unit for now, as it gets complicated to schedule otherwise */
521                 if ((adt->action) || (adt->nla_tracks.first)) {
522                         /* create the node */
523                         add_operation_node(id, DEG_NODE_TYPE_ANIMATION,
524                                            function_bind(BKE_animsys_eval_animdata, _1, id),
525                                            DEG_OPCODE_ANIMATION, id->name);
526
527                         // TODO: for each channel affected, we might also want to add some support for running RNA update callbacks on them
528                         // (which will be needed for proper handling of drivers later)
529                 }
530
531                 /* drivers */
532                 LINKLIST_FOREACH (FCurve *, fcu, &adt->drivers) {
533                         /* create driver */
534                         build_driver(id, fcu);
535                 }
536         }
537 }
538
539 /**
540  * Build graph node(s) for Driver
541  * \param id: ID-Block that driver is attached to
542  * \param fcu: Driver-FCurve
543  */
544 OperationDepsNode *DepsgraphNodeBuilder::build_driver(ID *id, FCurve *fcu)
545 {
546         /* Create data node for this driver */
547         /* TODO(sergey): Avoid creating same operation multiple times,
548          * in the future we need to avoid lookup of the operation as well
549          * and use some tagging magic instead.
550          */
551         OperationDepsNode *driver_op = find_operation_node(id,
552                                                            DEG_NODE_TYPE_PARAMETERS,
553                                                            DEG_OPCODE_DRIVER,
554                                                            fcu->rna_path ? fcu->rna_path : "",
555                                                            fcu->array_index);
556
557         if (driver_op == NULL) {
558                 driver_op = add_operation_node(id,
559                                                DEG_NODE_TYPE_PARAMETERS,
560                                                function_bind(BKE_animsys_eval_driver, _1, id, fcu),
561                                                DEG_OPCODE_DRIVER,
562                                                fcu->rna_path ? fcu->rna_path : "",
563                                                fcu->array_index);
564         }
565
566         /* return driver node created */
567         return driver_op;
568 }
569
570 /* Recursively build graph for world */
571 void DepsgraphNodeBuilder::build_world(World *world)
572 {
573         ID *world_id = &world->id;
574         if (world_id->tag & LIB_TAG_DOIT) {
575                 return;
576         }
577
578         build_animdata(world_id);
579
580         /* world itself */
581         add_operation_node(world_id,
582                            DEG_NODE_TYPE_PARAMETERS,
583                            NULL,
584                            DEG_OPCODE_PARAMETERS_EVAL);
585
586         /* textures */
587         build_texture_stack(world->mtex);
588
589         /* world's nodetree */
590         if (world->nodetree) {
591                 build_nodetree(world->nodetree);
592         }
593 }
594
595 /* Rigidbody Simulation - Scene Level */
596 void DepsgraphNodeBuilder::build_rigidbody(Scene *scene)
597 {
598         RigidBodyWorld *rbw = scene->rigidbody_world;
599
600         /**
601          * Rigidbody Simulation Nodes
602          * ==========================
603          *
604          * There are 3 nodes related to Rigidbody Simulation:
605          * 1) "Initialize/Rebuild World" - this is called sparingly, only when the simulation
606          *    needs to be rebuilt (mainly after file reload, or moving back to start frame)
607          * 2) "Do Simulation" - perform a simulation step - interleaved between the evaluation
608          *    steps for clusters of objects (i.e. between those affected and/or not affected by
609          *    the sim for instance)
610          *
611          * 3) "Pull Results" - grab the specific transforms applied for a specific object -
612          *    performed as part of object's transform-stack building
613          */
614
615         /* create nodes ------------------------------------------------------------------------ */
616         /* XXX: is this the right component, or do we want to use another one instead? */
617
618         /* init/rebuild operation */
619         /*OperationDepsNode *init_node =*/ add_operation_node(&scene->id, DEG_NODE_TYPE_TRANSFORM,
620                                                               function_bind(BKE_rigidbody_rebuild_sim, _1, scene),
621                                                               DEG_OPCODE_RIGIDBODY_REBUILD);
622
623         /* do-sim operation */
624         // XXX: what happens if we need to split into several groups?
625         OperationDepsNode *sim_node     = add_operation_node(&scene->id, DEG_NODE_TYPE_TRANSFORM,
626                                                              function_bind(BKE_rigidbody_eval_simulation, _1, scene),
627                                                              DEG_OPCODE_RIGIDBODY_SIM);
628
629         /* XXX: For now, the sim node is the only one that really matters here. If any other
630          * sims get added later, we may have to remove these hacks...
631          */
632         sim_node->owner->entry_operation = sim_node;
633         sim_node->owner->exit_operation  = sim_node;
634
635
636         /* objects - simulation participants */
637         if (rbw->group) {
638                 LINKLIST_FOREACH (GroupObject *, go, &rbw->group->gobject) {
639                         Object *ob = go->ob;
640
641                         if (!ob || (ob->type != OB_MESH))
642                                 continue;
643
644                         /* 2) create operation for flushing results */
645                         /* object's transform component - where the rigidbody operation lives */
646                         add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
647                                            function_bind(BKE_rigidbody_object_sync_transforms, _1, scene, ob),
648                                            DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
649                 }
650         }
651 }
652
653 void DepsgraphNodeBuilder::build_particles(Scene *scene, Object *ob)
654 {
655         /**
656          * Particle Systems Nodes
657          * ======================
658          *
659          * There are two types of nodes associated with representing
660          * particle systems:
661          *  1) Component (EVAL_PARTICLES) - This is the particle-system
662          *     evaluation context for an object. It acts as the container
663          *     for all the nodes associated with a particular set of particle
664          *     systems.
665          *  2) Particle System Eval Operation - This operation node acts as a
666          *     blackbox evaluation step for one particle system referenced by
667          *     the particle systems stack. All dependencies link to this operation.
668          */
669
670         /* component for all particle systems */
671         ComponentDepsNode *psys_comp =
672                 add_component_node(&ob->id, DEG_NODE_TYPE_EVAL_PARTICLES);
673
674         add_operation_node(psys_comp,
675                            function_bind(BKE_particle_system_eval_init,
676                                          _1,
677                                          scene,
678                                          ob),
679                            DEG_OPCODE_PARTICLE_SYSTEM_EVAL_INIT);
680
681         /* particle systems */
682         LINKLIST_FOREACH (ParticleSystem *, psys, &ob->particlesystem) {
683                 ParticleSettings *part = psys->part;
684
685                 /* particle settings */
686                 // XXX: what if this is used more than once!
687                 build_animdata(&part->id);
688
689                 /* this particle system */
690                 // TODO: for now, this will just be a placeholder "ubereval" node
691                 add_operation_node(psys_comp,
692                                    NULL,
693                                    DEG_OPCODE_PARTICLE_SYSTEM_EVAL,
694                                    psys->name);
695         }
696
697         /* pointcache */
698         // TODO...
699 }
700
701 void DepsgraphNodeBuilder::build_cloth(Scene *scene, Object *object)
702 {
703         add_operation_node(&object->id,
704                            DEG_NODE_TYPE_CACHE,
705                            function_bind(BKE_object_eval_cloth,
706                                          _1,
707                                          scene,
708                                          object),
709                            DEG_OPCODE_GEOMETRY_CLOTH_MODIFIER);
710 }
711
712 /* Shapekeys */
713 void DepsgraphNodeBuilder::build_shapekeys(Key *key)
714 {
715         build_animdata(&key->id);
716         add_operation_node(&key->id,
717                            DEG_NODE_TYPE_GEOMETRY,
718                            NULL,
719                            DEG_OPCODE_GEOMETRY_SHAPEKEY);
720 }
721
722 /* ObData Geometry Evaluation */
723 // XXX: what happens if the datablock is shared!
724 void DepsgraphNodeBuilder::build_obdata_geom(Scene *scene, Object *ob)
725 {
726         ID *obdata = (ID *)ob->data;
727         OperationDepsNode *op_node;
728
729         /* TODO(sergey): This way using this object's properties as driver target
730          * works fine.
731          *
732          * Does this depend on other nodes?
733          */
734         op_node = add_operation_node(&ob->id,
735                                      DEG_NODE_TYPE_PARAMETERS,
736                                      NULL,
737                                      DEG_OPCODE_PARAMETERS_EVAL);
738         op_node->set_as_exit();
739
740         /* Temporary uber-update node, which does everything.
741          * It is for the being we're porting old dependencies into the new system.
742          * We'll get rid of this node as soon as all the granular update functions
743          * are filled in.
744          *
745          * TODO(sergey): Get rid of this node.
746          */
747         op_node = add_operation_node(&ob->id,
748                                      DEG_NODE_TYPE_GEOMETRY,
749                                      function_bind(BKE_object_eval_uber_data, _1, scene, ob),
750                                      DEG_OPCODE_GEOMETRY_UBEREVAL);
751         op_node->set_as_exit();
752
753         op_node = add_operation_node(&ob->id,
754                                      DEG_NODE_TYPE_GEOMETRY,
755                                      NULL,
756                                      DEG_OPCODE_PLACEHOLDER,
757                                      "Eval Init");
758         op_node->set_as_entry();
759
760         // TODO: "Done" operation
761
762         /* Cloyth modifier. */
763         LINKLIST_FOREACH (ModifierData *, md, &ob->modifiers) {
764                 if (md->type == eModifierType_Cloth) {
765                         build_cloth(scene, ob);
766                 }
767         }
768
769         /* materials */
770         for (int a = 1; a <= ob->totcol; a++) {
771                 Material *ma = give_current_material(ob, a);
772                 if (ma != NULL) {
773                         build_material(ma);
774                 }
775         }
776
777         /* geometry collision */
778         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_LATTICE)) {
779                 // add geometry collider relations
780         }
781
782         if (obdata->tag & LIB_TAG_DOIT) {
783                 return;
784         }
785
786         /* ShapeKeys */
787         Key *key = BKE_key_from_object(ob);
788         if (key) {
789                 build_shapekeys(key);
790         }
791
792         build_animdata(obdata);
793
794         /* Nodes for result of obdata's evaluation, and geometry
795          * evaluation on object.
796          */
797         switch (ob->type) {
798                 case OB_MESH:
799                 {
800                         //Mesh *me = (Mesh *)ob->data;
801
802                         /* evaluation operations */
803                         op_node = add_operation_node(obdata,
804                                                      DEG_NODE_TYPE_GEOMETRY,
805                                                      function_bind(BKE_mesh_eval_geometry,
806                                                                    _1,
807                                                                    (Mesh *)obdata),
808                                                      DEG_OPCODE_PLACEHOLDER,
809                                                      "Geometry Eval");
810                         op_node->set_as_entry();
811                         break;
812                 }
813
814                 case OB_MBALL:
815                 {
816                         Object *mom = BKE_mball_basis_find(scene, ob);
817
818                         /* Motherball - mom depends on children! */
819                         if (mom == ob) {
820                                 /* metaball evaluation operations */
821                                 /* NOTE: only the motherball gets evaluated! */
822                                 op_node = add_operation_node(obdata,
823                                                              DEG_NODE_TYPE_GEOMETRY,
824                                                              function_bind(BKE_mball_eval_geometry,
825                                                                            _1,
826                                                                            (MetaBall *)obdata),
827                                                              DEG_OPCODE_PLACEHOLDER,
828                                                              "Geometry Eval");
829                                 op_node->set_as_entry();
830                         }
831                         break;
832                 }
833
834                 case OB_CURVE:
835                 case OB_SURF:
836                 case OB_FONT:
837                 {
838                         /* Curve/nurms evaluation operations. */
839                         /* - calculate curve geometry (including path) */
840                         op_node = add_operation_node(obdata,
841                                                      DEG_NODE_TYPE_GEOMETRY,
842                                                      function_bind(BKE_curve_eval_geometry,
843                                                                    _1,
844                                                                    (Curve *)obdata),
845                                                                    DEG_OPCODE_PLACEHOLDER,
846                                                                    "Geometry Eval");
847                         op_node->set_as_entry();
848
849                         /* Make sure objects used for bevel.taper are in the graph.
850                          * NOTE: This objects might be not linked to the scene.
851                          */
852                         Curve *cu = (Curve *)obdata;
853                         if (cu->bevobj != NULL) {
854                                 build_object(scene, NULL, cu->bevobj);
855                         }
856                         if (cu->taperobj != NULL) {
857                                 build_object(scene, NULL, cu->taperobj);
858                         }
859                         if (ob->type == OB_FONT && cu->textoncurve != NULL) {
860                                 build_object(scene, NULL, cu->textoncurve);
861                         }
862                         break;
863                 }
864
865                 case OB_LATTICE:
866                 {
867                         /* Lattice evaluation operations. */
868                         op_node = add_operation_node(obdata,
869                                                      DEG_NODE_TYPE_GEOMETRY,
870                                                      function_bind(BKE_lattice_eval_geometry,
871                                                                    _1,
872                                                                    (Lattice *)obdata),
873                                                                    DEG_OPCODE_PLACEHOLDER,
874                                                                    "Geometry Eval");
875                         op_node->set_as_entry();
876                         break;
877                 }
878         }
879
880         op_node = add_operation_node(obdata, DEG_NODE_TYPE_GEOMETRY, NULL,
881                                      DEG_OPCODE_PLACEHOLDER, "Eval Done");
882         op_node->set_as_exit();
883
884         /* Parameters for driver sources. */
885         add_operation_node(obdata,
886                            DEG_NODE_TYPE_PARAMETERS,
887                            NULL,
888                            DEG_OPCODE_PARAMETERS_EVAL);
889 }
890
891 /* Cameras */
892 void DepsgraphNodeBuilder::build_camera(Object *ob)
893 {
894         /* TODO: Link scene-camera links in somehow... */
895         Camera *cam = (Camera *)ob->data;
896         ID *camera_id = &cam->id;
897         if (camera_id->tag & LIB_TAG_DOIT) {
898                 return;
899         }
900
901         build_animdata(&cam->id);
902
903         add_operation_node(camera_id,
904                            DEG_NODE_TYPE_PARAMETERS,
905                            NULL,
906                            DEG_OPCODE_PARAMETERS_EVAL);
907
908         if (cam->dof_ob != NULL) {
909                 /* TODO(sergey): For now parametrs are on object level. */
910                 add_operation_node(&ob->id, DEG_NODE_TYPE_PARAMETERS, NULL,
911                                    DEG_OPCODE_PLACEHOLDER, "Camera DOF");
912         }
913 }
914
915 /* Lamps */
916 void DepsgraphNodeBuilder::build_lamp(Object *ob)
917 {
918         Lamp *la = (Lamp *)ob->data;
919         ID *lamp_id = &la->id;
920         if (lamp_id->tag & LIB_TAG_DOIT) {
921                 return;
922         }
923
924         build_animdata(&la->id);
925
926         /* TODO(sergey): Is it really how we're supposed to work with drivers? */
927         add_operation_node(lamp_id,
928                            DEG_NODE_TYPE_PARAMETERS,
929                            NULL,
930                            DEG_OPCODE_PARAMETERS_EVAL);
931
932         /* lamp's nodetree */
933         if (la->nodetree) {
934                 build_nodetree(la->nodetree);
935         }
936
937         /* textures */
938         build_texture_stack(la->mtex);
939 }
940
941 void DepsgraphNodeBuilder::build_nodetree(bNodeTree *ntree)
942 {
943         if (!ntree)
944                 return;
945
946         /* nodetree itself */
947         ID *ntree_id = &ntree->id;
948         OperationDepsNode *op_node;
949
950         build_animdata(ntree_id);
951
952         /* Parameters for drivers. */
953         op_node = add_operation_node(ntree_id,
954                                      DEG_NODE_TYPE_PARAMETERS,
955                                      NULL,
956                                      DEG_OPCODE_PARAMETERS_EVAL);
957         op_node->set_as_exit();
958
959         /* nodetree's nodes... */
960         LINKLIST_FOREACH (bNode *, bnode, &ntree->nodes) {
961                 ID *id = bnode->id;
962                 if (id != NULL) {
963                         ID_Type id_type = GS(id->name);
964                         if (id_type == ID_MA) {
965                                 build_material((Material *)id);
966                         }
967                         else if (id_type == ID_TE) {
968                                 build_texture((Tex *)id);
969                         }
970                         else if (id_type == ID_IM) {
971                                 build_image((Image *)id);
972                         }
973                         else if (bnode->type == NODE_GROUP) {
974                                 bNodeTree *group_ntree = (bNodeTree *)id;
975                                 if ((group_ntree->id.tag & LIB_TAG_DOIT) == 0) {
976                                         build_nodetree(group_ntree);
977                                 }
978                         }
979                 }
980         }
981
982         // TODO: link from nodetree to owner_component?
983 }
984
985 /* Recursively build graph for material */
986 void DepsgraphNodeBuilder::build_material(Material *ma)
987 {
988         ID *ma_id = &ma->id;
989         if (ma_id->tag & LIB_TAG_DOIT) {
990                 return;
991         }
992
993         add_operation_node(ma_id, DEG_NODE_TYPE_SHADING, NULL,
994                            DEG_OPCODE_PLACEHOLDER, "Material Update");
995
996         /* material animation */
997         build_animdata(ma_id);
998
999         /* textures */
1000         build_texture_stack(ma->mtex);
1001
1002         /* material's nodetree */
1003         build_nodetree(ma->nodetree);
1004 }
1005
1006 /* Texture-stack attached to some shading datablock */
1007 void DepsgraphNodeBuilder::build_texture_stack(MTex **texture_stack)
1008 {
1009         int i;
1010
1011         /* for now assume that all texture-stacks have same number of max items */
1012         for (i = 0; i < MAX_MTEX; i++) {
1013                 MTex *mtex = texture_stack[i];
1014                 if (mtex && mtex->tex)
1015                         build_texture(mtex->tex);
1016         }
1017 }
1018
1019 /* Recursively build graph for texture */
1020 void DepsgraphNodeBuilder::build_texture(Tex *tex)
1021 {
1022         ID *tex_id = &tex->id;
1023         if (tex_id->tag & LIB_TAG_DOIT) {
1024                 return;
1025         }
1026         tex_id->tag |= LIB_TAG_DOIT;
1027         /* Texture itself. */
1028         build_animdata(tex_id);
1029         /* Texture's nodetree. */
1030         build_nodetree(tex->nodetree);
1031         /* Special cases for different IDs which texture uses. */
1032         if (tex->type == TEX_IMAGE) {
1033                 if (tex->ima != NULL) {
1034                         build_image(tex->ima);
1035                 }
1036         }
1037 }
1038
1039 void DepsgraphNodeBuilder::build_image(Image *image) {
1040         ID *image_id = &image->id;
1041         if (image_id->tag & LIB_TAG_DOIT) {
1042                 return;
1043         }
1044         image_id->tag |= LIB_TAG_DOIT;
1045         /* Placeholder so we can add relations and tag ID node for update. */
1046         add_operation_node(image_id,
1047                            DEG_NODE_TYPE_PARAMETERS,
1048                            NULL,
1049                            DEG_OPCODE_PLACEHOLDER,
1050                            "Image Eval");
1051 }
1052
1053 void DepsgraphNodeBuilder::build_compositor(Scene *scene)
1054 {
1055         /* For now, just a plain wrapper? */
1056         // TODO: create compositing component?
1057         // XXX: component type undefined!
1058         //graph->get_node(&scene->id, NULL, DEG_NODE_TYPE_COMPOSITING, NULL);
1059
1060         /* for now, nodetrees are just parameters; compositing occurs in internals of renderer... */
1061         add_component_node(&scene->id, DEG_NODE_TYPE_PARAMETERS);
1062         build_nodetree(scene->nodetree);
1063 }
1064
1065 void DepsgraphNodeBuilder::build_gpencil(bGPdata *gpd)
1066 {
1067         ID *gpd_id = &gpd->id;
1068
1069         /* TODO(sergey): what about multiple users of same datablock? This should
1070          * only get added once.
1071          */
1072
1073         /* The main reason Grease Pencil is included here is because the animation
1074          * (and drivers) need to be hosted somewhere.
1075          */
1076         build_animdata(gpd_id);
1077 }
1078
1079 void DepsgraphNodeBuilder::build_cachefile(CacheFile *cache_file)
1080 {
1081         ID *cache_file_id = &cache_file->id;
1082         /* Animation, */
1083         build_animdata(cache_file_id);
1084         /* Cache evaluation itself. */
1085         add_operation_node(cache_file_id, DEG_NODE_TYPE_CACHE, NULL,
1086                            DEG_OPCODE_PLACEHOLDER, "Cache File Update");
1087 }
1088
1089 void DepsgraphNodeBuilder::build_mask(Mask *mask)
1090 {
1091         ID *mask_id = &mask->id;
1092         /* F-Curve based animation. */
1093         build_animdata(mask_id);
1094         /* Animation based on mask's shapes. */
1095         add_operation_node(mask_id,
1096                            DEG_NODE_TYPE_ANIMATION,
1097                            function_bind(BKE_mask_eval_animation, _1, mask),
1098                            DEG_OPCODE_MASK_ANIMATION);
1099         /* Final mask evaluation. */
1100         add_operation_node(mask_id,
1101                            DEG_NODE_TYPE_PARAMETERS,
1102                            function_bind(BKE_mask_eval_update, _1, mask),
1103                            DEG_OPCODE_MASK_EVAL);
1104 }
1105
1106 void DepsgraphNodeBuilder::build_movieclip(MovieClip *clip) {
1107         ID *clip_id = &clip->id;
1108         /* Animation. */
1109         build_animdata(clip_id);
1110         /* Movie clip evaluation. */
1111         add_operation_node(clip_id,
1112                            DEG_NODE_TYPE_PARAMETERS,
1113                            function_bind(BKE_movieclip_eval_update, _1, clip),
1114                            DEG_OPCODE_MOVIECLIP_EVAL);
1115 }
1116
1117 }  // namespace DEG