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