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