Depsgraph: New dependency graph integration commit
[blender.git] / source / blender / depsgraph / intern / depsgraph_build_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  * Methods for constructing depsgraph's nodes
27  */
28
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "MEM_guardedalloc.h"
35
36 extern "C" {
37 #include "BLI_blenlib.h"
38 #include "BLI_string.h"
39 #include "BLI_utildefines.h"
40
41 #include "DNA_action_types.h"
42 #include "DNA_anim_types.h"
43 #include "DNA_armature_types.h"
44 #include "DNA_camera_types.h"
45 #include "DNA_constraint_types.h"
46 #include "DNA_curve_types.h"
47 #include "DNA_effect_types.h"
48 #include "DNA_gpencil_types.h"
49 #include "DNA_group_types.h"
50 #include "DNA_key_types.h"
51 #include "DNA_lamp_types.h"
52 #include "DNA_material_types.h"
53 #include "DNA_mesh_types.h"
54 #include "DNA_meta_types.h"
55 #include "DNA_node_types.h"
56 #include "DNA_particle_types.h"
57 #include "DNA_object_types.h"
58 #include "DNA_rigidbody_types.h"
59 #include "DNA_scene_types.h"
60 #include "DNA_texture_types.h"
61 #include "DNA_world_types.h"
62
63 #include "BKE_action.h"
64 #include "BKE_armature.h"
65 #include "BKE_animsys.h"
66 #include "BKE_constraint.h"
67 #include "BKE_curve.h"
68 #include "BKE_depsgraph.h"
69 #include "BKE_effect.h"
70 #include "BKE_fcurve.h"
71 #include "BKE_idcode.h"
72 #include "BKE_group.h"
73 #include "BKE_key.h"
74 #include "BKE_lattice.h"
75 #include "BKE_library.h"
76 #include "BKE_main.h"
77 #include "BKE_material.h"
78 #include "BKE_mesh.h"
79 #include "BKE_mball.h"
80 #include "BKE_modifier.h"
81 #include "BKE_node.h"
82 #include "BKE_object.h"
83 #include "BKE_particle.h"
84 #include "BKE_rigidbody.h"
85 #include "BKE_sound.h"
86 #include "BKE_texture.h"
87 #include "BKE_tracking.h"
88 #include "BKE_world.h"
89
90 #include "DEG_depsgraph.h"
91 #include "DEG_depsgraph_build.h"
92
93 #include "RNA_access.h"
94 #include "RNA_types.h"
95 } /* extern "C" */
96
97 #include "depsnode.h"
98 #include "depsnode_component.h"
99 #include "depsnode_operation.h"
100 #include "depsgraph_types.h"
101 #include "depsgraph_build.h"
102 #include "depsgraph_intern.h"
103
104 /* ************ */
105 /* Node Builder */
106
107 /* **** General purpose functions **** */
108
109 DepsgraphNodeBuilder::DepsgraphNodeBuilder(Main *bmain, Depsgraph *graph) :
110     m_bmain(bmain),
111     m_graph(graph)
112 {
113 }
114
115 DepsgraphNodeBuilder::~DepsgraphNodeBuilder()
116 {
117 }
118
119 RootDepsNode *DepsgraphNodeBuilder::add_root_node()
120 {
121         return m_graph->add_root_node();
122 }
123
124 IDDepsNode *DepsgraphNodeBuilder::add_id_node(ID *id)
125 {
126         const char *idtype_name = BKE_idcode_to_name(GS(id->name));
127         return m_graph->add_id_node(id, string(id->name+2) + "[" + idtype_name + "]");
128 }
129
130 TimeSourceDepsNode *DepsgraphNodeBuilder::add_time_source(ID *id)
131 {
132         /* determine which node to attach timesource to */
133         if (id) {
134 #if 0 /* XXX TODO */
135                 /* get ID node */
136                 IDDepsNode id_node = m_graph->find_id_node(id);
137
138                 /* depends on what this is... */
139                 switch (GS(id->name)) {
140                         case ID_SCE: /* Scene - Usually sequencer strip causing time remapping... */
141                         {
142                                 // TODO...
143                         }
144                         break;
145
146                         case ID_GR: /* Group */
147                         {
148                                 // TODO...
149                         }
150                         break;
151
152                         // XXX: time source...
153
154                         default:     /* Unhandled */
155                                 printf("%s(): Unhandled ID - %s \n", __func__, id->name);
156                                 break;
157                 }
158 #endif
159         }
160         else {
161                 /* root-node */
162                 RootDepsNode *root_node = m_graph->root_node;
163                 if (root_node) {
164                         return root_node->add_time_source("Time Source");
165                 }
166         }
167
168         return NULL;
169 }
170
171 ComponentDepsNode *DepsgraphNodeBuilder::add_component_node(
172         ID *id,
173         eDepsNode_Type comp_type,
174         const string &comp_name)
175 {
176         IDDepsNode *id_node = add_id_node(id);
177         ComponentDepsNode *comp_node = id_node->add_component(comp_type, comp_name);
178         comp_node->owner = id_node;
179         return comp_node;
180 }
181
182 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
183         ComponentDepsNode *comp_node,
184         eDepsOperation_Type optype,
185         DepsEvalOperationCb op,
186         eDepsOperation_Code opcode,
187         const string &description)
188 {
189         OperationDepsNode *op_node = comp_node->has_operation(opcode, description);
190         if (op_node == NULL) {
191                 op_node = comp_node->add_operation(optype, op, opcode, description);
192                 m_graph->operations.push_back(op_node);
193         }
194         else {
195                 fprintf(stderr, "add_operation: Operation already exists - %s has %s at %p\n",
196                         comp_node->identifier().c_str(),
197                         op_node->identifier().c_str(),
198                         op_node);
199                 BLI_assert(!"Should not happen!");
200         }
201         return op_node;
202 }
203
204 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
205         ID *id,
206         eDepsNode_Type comp_type,
207         const string &comp_name,
208         eDepsOperation_Type optype,
209         DepsEvalOperationCb op,
210         eDepsOperation_Code opcode,
211         const string &description)
212 {
213         ComponentDepsNode *comp_node = add_component_node(id, comp_type, comp_name);
214         return add_operation_node(comp_node, optype, op, opcode, description);
215 }
216
217 bool DepsgraphNodeBuilder::has_operation_node(ID *id,
218                                               eDepsNode_Type comp_type,
219                                               const string &comp_name,
220                                               eDepsOperation_Code opcode,
221                                               const string &description)
222 {
223         return find_operation_node(id, comp_type, comp_name, opcode, description) != NULL;
224 }
225
226 OperationDepsNode *DepsgraphNodeBuilder::find_operation_node(
227         ID *id,
228         eDepsNode_Type comp_type,
229         const string &comp_name,
230         eDepsOperation_Code opcode,
231         const string &description)
232 {
233         ComponentDepsNode *comp_node = add_component_node(id, comp_type, comp_name);
234         return comp_node->has_operation(opcode, description);
235 }
236
237
238 /* **** Build functions for entity nodes **** */
239
240 void DepsgraphNodeBuilder::build_scene(Main *bmain, Scene *scene)
241 {
242         /* LIB_DOIT is used to indicate whether node for given ID was already
243          * created or not. This flag is being set in add_id_node(), so functions
244          * shouldn't bother with setting it, they only might query this flag when
245          * needed.
246          */
247         BKE_main_id_tag_all(bmain, false);
248
249         /* scene ID block */
250         add_id_node(&scene->id);
251
252         /* timesource */
253         add_time_source(NULL);
254
255         /* build subgraph for set, and link this in... */
256         // XXX: depending on how this goes, that scene itself could probably store its
257         //      own little partial depsgraph?
258         if (scene->set) {
259                 build_scene(bmain, scene->set);
260         }
261
262         /* scene objects */
263         for (Base *base = (Base *)scene->base.first; base; base = base->next) {
264                 Object *ob = base->object;
265
266                 /* object itself */
267                 build_object(scene, base, ob);
268
269                 /* object that this is a proxy for */
270                 // XXX: the way that proxies work needs to be completely reviewed!
271                 if (ob->proxy) {
272                         build_object(scene, base, ob->proxy);
273                 }
274
275                 /* Object dupligroup. */
276                 if (ob->dup_group) {
277                         build_group(scene, base, ob->dup_group);
278                 }
279         }
280
281         /* rigidbody */
282         if (scene->rigidbody_world) {
283                 build_rigidbody(scene);
284         }
285
286         /* scene's animation and drivers */
287         if (scene->adt) {
288                 build_animdata(&scene->id);
289         }
290
291         /* world */
292         if (scene->world) {
293                 build_world(scene->world);
294         }
295
296         /* compo nodes */
297         if (scene->nodetree) {
298                 build_compositor(scene);
299         }
300
301         /* sequencer */
302         // XXX...
303
304         /* grease pencil */
305         if (scene->gpd) {
306                 build_gpencil(scene->gpd);
307         }
308 }
309
310 void DepsgraphNodeBuilder::build_group(Scene *scene,
311                                        Base *base,
312                                        Group *group)
313 {
314         ID *group_id = &group->id;
315         if (group_id->flag & LIB_DOIT) {
316                 return;
317         }
318         group_id->flag |= LIB_DOIT;
319
320         for (GroupObject *go = (GroupObject *)group->gobject.first;
321              go != NULL;
322              go = go->next)
323         {
324                 build_object(scene, base, go->ob);
325         }
326 }
327
328 SubgraphDepsNode *DepsgraphNodeBuilder::build_subgraph(Group *group)
329 {
330         /* sanity checks */
331         if (!group)
332                 return NULL;
333
334         /* create new subgraph's data */
335         Depsgraph *subgraph = DEG_graph_new();
336
337         DepsgraphNodeBuilder subgraph_builder(m_bmain, subgraph);
338
339         /* add group objects */
340         for (GroupObject *go = (GroupObject *)group->gobject.first;
341              go != NULL;
342              go = go->next)
343         {
344                 /*Object *ob = go->ob;*/
345
346                 /* Each "group object" is effectively a separate instance of the underlying
347                  * object data. When the group is evaluated, the transform results and/or
348                  * some other attributes end up getting overridden by the group
349                  */
350         }
351
352         /* create a node for representing subgraph */
353         SubgraphDepsNode *subgraph_node = m_graph->add_subgraph_node(&group->id);
354         subgraph_node->graph = subgraph;
355
356         /* make a copy of the data this node will need? */
357         // XXX: do we do this now, or later?
358         // TODO: need API function which queries graph's ID's hash, and duplicates those blocks thoroughly with all outside links removed...
359
360         return subgraph_node;
361 }
362
363 void DepsgraphNodeBuilder::build_object(Scene *scene, Base *base, Object *ob)
364 {
365         if (ob->id.flag & LIB_DOIT) {
366                 IDDepsNode *id_node = m_graph->find_id_node(&ob->id);
367                 id_node->layers = base->lay;
368                 return;
369         }
370
371         IDDepsNode *id_node = add_id_node(&ob->id);
372         id_node->layers = base->lay;
373
374         /* standard components */
375         build_object_transform(scene, ob);
376
377
378         /* object data */
379         if (ob->data) {
380                 /* type-specific data... */
381                 switch (ob->type) {
382                         case OB_MESH:     /* Geometry */
383                         case OB_CURVE:
384                         case OB_FONT:
385                         case OB_SURF:
386                         case OB_MBALL:
387                         case OB_LATTICE:
388                         {
389                                 /* TODO(sergey): This way using this object's
390                                  * properties as driver target works fine.
391                                  *
392                                  * Does this depend on other nodes?
393                                  */
394                                 add_operation_node(&ob->id, DEPSNODE_TYPE_PARAMETERS, DEPSOP_TYPE_POST, NULL,
395                                                    DEG_OPCODE_PLACEHOLDER, "Parameters Eval");
396
397                                 build_obdata_geom(scene, ob);
398                                 /* TODO(sergey): Only for until we support granular
399                                  * update of curves.
400                                  */
401                                 if (ob->type == OB_FONT) {
402                                         Curve *curve = (Curve *)ob->data;
403                                         if (curve->textoncurve) {
404                                                 id_node->eval_flags |= DAG_EVAL_NEED_CURVE_PATH;
405                                         }
406                                 }
407                         }
408                         break;
409
410                         case OB_ARMATURE: /* Pose */
411                                 if (ob->id.lib != NULL && ob->proxy_from != NULL) {
412                                         build_proxy_rig(ob);
413                                 }
414                                 else {
415                                         build_rig(scene, ob);
416                                 }
417                                 break;
418
419                         case OB_LAMP:   /* Lamp */
420                                 build_lamp(ob);
421                                 break;
422
423                         case OB_CAMERA: /* Camera */
424                                 build_camera(ob);
425                                 break;
426
427                         default:
428                         {
429                                 ID *obdata = (ID *)ob->data;
430                                 if ((obdata->flag & LIB_DOIT) == 0) {
431                                         build_animdata(obdata);
432                                 }
433                                 break;
434                         }
435                 }
436         }
437
438         /* Build animation data,
439          *
440          * Do it now because it's possible object data will affect
441          * on object's level animation, for example in case of rebuilding
442          * pose for proxy.
443          */
444         build_animdata(&ob->id);
445
446         /* particle systems */
447         if (ob->particlesystem.first) {
448                 build_particles(ob);
449         }
450
451         /* grease pencil */
452         if (ob->gpd) {
453                 build_gpencil(ob->gpd);
454         }
455 }
456
457 void DepsgraphNodeBuilder::build_object_transform(Scene *scene, Object *ob)
458 {
459         /* local transforms (from transform channels - loc/rot/scale + deltas) */
460         add_operation_node(&ob->id, DEPSNODE_TYPE_TRANSFORM,
461                            DEPSOP_TYPE_INIT, function_bind(BKE_object_eval_local_transform, _1, scene, ob),
462                            DEG_OPCODE_TRANSFORM_LOCAL);
463
464         /* object parent */
465         if (ob->parent) {
466                 add_operation_node(&ob->id, DEPSNODE_TYPE_TRANSFORM,
467                                    DEPSOP_TYPE_EXEC, function_bind(BKE_object_eval_parent, _1, scene, ob),
468                                    DEG_OPCODE_TRANSFORM_PARENT);
469         }
470
471         /* object constraints */
472         if (ob->constraints.first) {
473                 build_object_constraints(scene, ob);
474         }
475
476         /* Temporary uber-update node, which does everything.
477          * It is for the being we're porting old dependencies into the new system.
478          * We'll get rid of this node as soon as all the granular update functions
479          * are filled in.
480          *
481          * TODO(sergey): Get rid of this node.
482          */
483         add_operation_node(&ob->id, DEPSNODE_TYPE_TRANSFORM,
484                            DEPSOP_TYPE_EXEC, function_bind(BKE_object_eval_uber_transform, _1, scene, ob),
485                            DEG_OPCODE_OBJECT_UBEREVAL);
486
487         /* object transform is done */
488         add_operation_node(&ob->id, DEPSNODE_TYPE_TRANSFORM,
489                            DEPSOP_TYPE_POST, function_bind(BKE_object_eval_done, _1, ob),
490                            DEG_OPCODE_TRANSFORM_FINAL);
491 }
492
493 /**
494  * Constraints Graph Notes
495  *
496  * For constraints, we currently only add a operation node to the Transform
497  * or Bone components (depending on whichever type of owner we have).
498  * This represents the entire constraints stack, which is for now just
499  * executed as a single monolithic block. At least initially, this should
500  * be sufficient for ensuring that the porting/refactoring process remains
501  * manageable.
502  *
503  * However, when the time comes for developing "node-based" constraints,
504  * we'll need to split this up into pre/post nodes for "constraint stack
505  * evaluation" + operation nodes for each constraint (i.e. the contents
506  * of the loop body used in the current "solve_constraints()" operation).
507  *
508  * -- Aligorith, August 2013
509  */
510 void DepsgraphNodeBuilder::build_object_constraints(Scene *scene, Object *ob)
511 {
512         /* create node for constraint stack */
513         add_operation_node(&ob->id, DEPSNODE_TYPE_TRANSFORM,
514                            DEPSOP_TYPE_EXEC, function_bind(BKE_object_eval_constraints, _1, scene, ob),
515                            DEG_OPCODE_TRANSFORM_CONSTRAINTS);
516 }
517
518 void DepsgraphNodeBuilder::build_pose_constraints(Object *ob, bPoseChannel *pchan)
519 {
520         /* create node for constraint stack */
521         add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
522                            DEPSOP_TYPE_EXEC, function_bind(BKE_pose_constraints_evaluate, _1, ob, pchan),
523                            DEG_OPCODE_BONE_CONSTRAINTS);
524 }
525
526 /**
527  * Build graph nodes for AnimData block
528  * \param scene_node: Scene that ID-block this lives on belongs to
529  * \param id: ID-Block which hosts the AnimData
530  */
531 void DepsgraphNodeBuilder::build_animdata(ID *id)
532 {
533         AnimData *adt = BKE_animdata_from_id(id);
534
535         if (adt == NULL)
536                 return;
537
538         /* animation */
539         if (adt->action || adt->nla_tracks.first || adt->drivers.first) {
540                 // XXX: Hook up specific update callbacks for special properties which may need it...
541
542                 /* actions and NLA - as a single unit for now, as it gets complicated to schedule otherwise */
543                 if ((adt->action) || (adt->nla_tracks.first)) {
544                         /* create the node */
545                         add_operation_node(id, DEPSNODE_TYPE_ANIMATION,
546                                            DEPSOP_TYPE_EXEC, function_bind(BKE_animsys_eval_animdata, _1, id),
547                                            DEG_OPCODE_ANIMATION, id->name);
548
549                         // TODO: for each channel affected, we might also want to add some support for running RNA update callbacks on them
550                         // (which will be needed for proper handling of drivers later)
551                 }
552
553                 /* drivers */
554                 for (FCurve *fcu = (FCurve *)adt->drivers.first; fcu; fcu = fcu->next) {
555                         /* create driver */
556                         build_driver(id, fcu);
557                 }
558         }
559 }
560
561 /**
562  * Build graph node(s) for Driver
563  * \param id: ID-Block that driver is attached to
564  * \param fcu: Driver-FCurve
565  */
566 OperationDepsNode *DepsgraphNodeBuilder::build_driver(ID *id, FCurve *fcu)
567 {
568         ChannelDriver *driver = fcu->driver;
569
570         /* Create data node for this driver */
571         /* TODO(sergey): Avoid creating same operation multiple times,
572          * in the future we need to avoid lookup of the operation as well
573          * and use some tagging magic instead.
574          */
575         OperationDepsNode *driver_op = find_operation_node(id,
576                                                            DEPSNODE_TYPE_PARAMETERS,
577                                                            DEG_OPCODE_DRIVER,
578                                                            deg_fcurve_id_name(fcu));
579
580         if (driver_op == NULL) {
581                 driver_op = add_operation_node(id, DEPSNODE_TYPE_PARAMETERS,
582                                                DEPSOP_TYPE_EXEC, function_bind(BKE_animsys_eval_driver, _1, id, fcu),
583                                                DEG_OPCODE_DRIVER, deg_fcurve_id_name(fcu));
584         }
585
586         /* tag "scripted expression" drivers as needing Python (due to GIL issues, etc.) */
587         if (driver->type == DRIVER_TYPE_PYTHON) {
588                 driver_op->flag |= DEPSOP_FLAG_USES_PYTHON;
589         }
590
591         /* return driver node created */
592         return driver_op;
593 }
594
595 /* Recursively build graph for world */
596 void DepsgraphNodeBuilder::build_world(World *world)
597 {
598         ID *world_id = &world->id;
599         if (world_id->flag & LIB_DOIT) {
600                 return;
601         }
602
603         /* world itself */
604         IDDepsNode *world_node = add_id_node(world_id); /* world shading/params? */
605
606         build_animdata(world_id);
607
608         /* TODO: other settings? */
609
610         /* textures */
611         build_texture_stack(world_node, world->mtex);
612
613         /* world's nodetree */
614         if (world->nodetree) {
615                 build_nodetree(world_node, world->nodetree);
616         }
617 }
618
619 /* Rigidbody Simulation - Scene Level */
620 void DepsgraphNodeBuilder::build_rigidbody(Scene *scene)
621 {
622         RigidBodyWorld *rbw = scene->rigidbody_world;
623
624         /**
625          * Rigidbody Simulation Nodes
626          * ==========================
627          *
628          * There are 3 nodes related to Rigidbody Simulation:
629          * 1) "Initialize/Rebuild World" - this is called sparingly, only when the simulation
630          *    needs to be rebuilt (mainly after file reload, or moving back to start frame)
631          * 2) "Do Simulation" - perform a simulation step - interleaved between the evaluation
632          *    steps for clusters of objects (i.e. between those affected and/or not affected by
633          *    the sim for instance)
634          *
635          * 3) "Pull Results" - grab the specific transforms applied for a specific object -
636          *    performed as part of object's transform-stack building
637          */
638
639         /* create nodes ------------------------------------------------------------------------ */
640         /* XXX: is this the right component, or do we want to use another one instead? */
641
642         /* init/rebuild operation */
643         /*OperationDepsNode *init_node =*/ add_operation_node(&scene->id, DEPSNODE_TYPE_TRANSFORM,
644                                                               DEPSOP_TYPE_REBUILD, function_bind(BKE_rigidbody_rebuild_sim, _1, scene),
645                                                               DEG_OPCODE_RIGIDBODY_REBUILD);
646
647         /* do-sim operation */
648         // XXX: what happens if we need to split into several groups?
649         OperationDepsNode *sim_node     = add_operation_node(&scene->id, DEPSNODE_TYPE_TRANSFORM,
650                                                              DEPSOP_TYPE_SIM, function_bind(BKE_rigidbody_eval_simulation, _1, scene),
651                                                              DEG_OPCODE_RIGIDBODY_SIM);
652
653         /* XXX: For now, the sim node is the only one that really matters here. If any other
654          * sims get added later, we may have to remove these hacks...
655          */
656         sim_node->owner->entry_operation = sim_node;
657         sim_node->owner->exit_operation  = sim_node;
658
659
660         /* objects - simulation participants */
661         if (rbw->group) {
662                 for (GroupObject *go = (GroupObject *)rbw->group->gobject.first; go; go = go->next) {
663                         Object *ob = go->ob;
664
665                         if (!ob || (ob->type != OB_MESH))
666                                 continue;
667
668                         /* 2) create operation for flushing results */
669                         /* object's transform component - where the rigidbody operation lives */
670                         add_operation_node(&ob->id, DEPSNODE_TYPE_TRANSFORM,
671                                            DEPSOP_TYPE_EXEC, function_bind(BKE_rigidbody_object_sync_transforms, _1, scene, ob),
672                                            DEG_OPCODE_TRANSFORM_RIGIDBODY);
673                 }
674         }
675 }
676
677 void DepsgraphNodeBuilder::build_particles(Object *ob)
678 {
679         /**
680          * Particle Systems Nodes
681          * ======================
682          *
683          * There are two types of nodes associated with representing
684          * particle systems:
685          *  1) Component (EVAL_PARTICLES) - This is the particle-system
686          *     evaluation context for an object. It acts as the container
687          *     for all the nodes associated with a particular set of particle
688          *     systems.
689          *  2) Particle System Eval Operation - This operation node acts as a
690          *     blackbox evaluation step for one particle system referenced by
691          *     the particle systems stack. All dependencies link to this operation.
692          */
693
694         /* component for all particle systems */
695         ComponentDepsNode *psys_comp = add_component_node(&ob->id, DEPSNODE_TYPE_EVAL_PARTICLES);
696
697         /* particle systems */
698         for (ParticleSystem *psys = (ParticleSystem *)ob->particlesystem.first; psys; psys = psys->next) {
699                 ParticleSettings *part = psys->part;
700
701                 /* particle settings */
702                 // XXX: what if this is used more than once!
703                 build_animdata(&part->id);
704
705                 /* this particle system */
706                 // TODO: for now, this will just be a placeholder "ubereval" node
707                 add_operation_node(psys_comp,
708                                    DEPSOP_TYPE_EXEC, function_bind(BKE_particle_system_eval, _1, ob, psys),
709                                    DEG_OPCODE_PSYS_EVAL,
710                                    psys->name);
711         }
712
713         /* pointcache */
714         // TODO...
715 }
716
717 /* IK Solver Eval Steps */
718 void DepsgraphNodeBuilder::build_ik_pose(Scene *scene, Object *ob, bPoseChannel *pchan, bConstraint *con)
719 {
720         bKinematicConstraint *data = (bKinematicConstraint *)con->data;
721
722         /* Find the chain's root. */
723         bPoseChannel *rootchan = BKE_armature_ik_solver_find_root(pchan, data);
724
725         if (has_operation_node(&ob->id, DEPSNODE_TYPE_EVAL_POSE, rootchan->name,
726                                DEG_OPCODE_POSE_IK_SOLVER))
727         {
728                 return;
729         }
730
731         /* Operation node for evaluating/running IK Solver. */
732         add_operation_node(&ob->id, DEPSNODE_TYPE_EVAL_POSE, rootchan->name,
733                            DEPSOP_TYPE_SIM, function_bind(BKE_pose_iktree_evaluate, _1, scene, ob, rootchan),
734                            DEG_OPCODE_POSE_IK_SOLVER);
735 }
736
737 /* Spline IK Eval Steps */
738 void DepsgraphNodeBuilder::build_splineik_pose(Scene *scene, Object *ob, bPoseChannel *pchan, bConstraint *con)
739 {
740         bSplineIKConstraint *data = (bSplineIKConstraint *)con->data;
741
742         /* Find the chain's root. */
743         bPoseChannel *rootchan = BKE_armature_splineik_solver_find_root(pchan, data);
744
745         /* Operation node for evaluating/running Spline IK Solver.
746          * Store the "root bone" of this chain in the solver, so it knows where to start.
747          */
748         add_operation_node(&ob->id, DEPSNODE_TYPE_EVAL_POSE, rootchan->name,
749                            DEPSOP_TYPE_SIM, function_bind(BKE_pose_splineik_evaluate, _1, scene, ob, rootchan),
750                            DEG_OPCODE_POSE_SPLINE_IK_SOLVER);
751 }
752
753 /* Pose/Armature Bones Graph */
754 void DepsgraphNodeBuilder::build_rig(Scene *scene, Object *ob)
755 {
756         bArmature *arm = (bArmature *)ob->data;
757
758         /* animation and/or drivers linking posebones to base-armature used to define them
759          * NOTE: AnimData here is really used to control animated deform properties,
760          *       which ideally should be able to be unique across different instances.
761          *       Eventually, we need some type of proxy/isolation mechanism in-between here
762          *       to ensure that we can use same rig multiple times in same scene...
763          */
764         build_animdata(&arm->id);
765
766         /* Rebuild pose if not up to date. */
767         if (ob->pose == NULL || (ob->pose->flag & POSE_RECALC)) {
768                 BKE_pose_rebuild(ob, arm);
769                 /* XXX: Without this animation gets lost in certain circumstances
770                  * after loading file. Need to investigate further since it does
771                  * not happen with simple scenes..
772                  */
773                 if (ob->adt) {
774                         ob->adt->recalc |= ADT_RECALC_ANIM;
775                 }
776         }
777
778         /**
779          * Pose Rig Graph
780          * ==============
781          *
782          * Pose Component:
783          * - Mainly used for referencing Bone components.
784          * - This is where the evaluation operations for init/exec/cleanup
785          *   (ik) solvers live, and are later hooked up (so that they can be
786          *   interleaved during runtime) with bone-operations they depend on/affect.
787          * - init_pose_eval() and cleanup_pose_eval() are absolute first and last
788          *   steps of pose eval process. ALL bone operations must be performed
789          *   between these two...
790          *
791          * Bone Component:
792          * - Used for representing each bone within the rig
793          * - Acts to encapsulate the evaluation operations (base matrix + parenting,
794          *   and constraint stack) so that they can be easily found.
795          * - Everything else which depends on bone-results hook up to the component only
796          *   so that we can redirect those to point at either the the post-IK/
797          *   post-constraint/post-matrix steps, as needed.
798          */
799
800         /* pose eval context */
801         add_operation_node(&ob->id, DEPSNODE_TYPE_EVAL_POSE,
802                            DEPSOP_TYPE_INIT, function_bind(BKE_pose_eval_init, _1, scene, ob, ob->pose), DEG_OPCODE_POSE_INIT);
803
804         add_operation_node(&ob->id, DEPSNODE_TYPE_EVAL_POSE,
805                            DEPSOP_TYPE_POST, function_bind(BKE_pose_eval_flush, _1, scene, ob, ob->pose), DEG_OPCODE_POSE_DONE);
806
807         /* bones */
808         for (bPoseChannel *pchan = (bPoseChannel *)ob->pose->chanbase.first; pchan; pchan = pchan->next) {
809                 /* node for bone eval */
810                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
811                                    DEPSOP_TYPE_INIT, NULL, // XXX: BKE_pose_eval_bone_local
812                                    DEG_OPCODE_BONE_LOCAL);
813
814                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
815                                    DEPSOP_TYPE_EXEC, function_bind(BKE_pose_eval_bone, _1, scene, ob, pchan), // XXX: BKE_pose_eval_bone_pose
816                                    DEG_OPCODE_BONE_POSE_PARENT);
817
818                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
819                                    DEPSOP_TYPE_OUT, NULL, /* NOTE: dedicated noop for easier relationship construction */
820                                    DEG_OPCODE_BONE_READY);
821
822                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
823                                    DEPSOP_TYPE_POST, function_bind(BKE_pose_bone_done, _1, pchan),
824                                    DEG_OPCODE_BONE_DONE);
825
826                 /* constraints */
827                 if (pchan->constraints.first != NULL) {
828                         build_pose_constraints(ob, pchan);
829                 }
830
831                 /**
832                  * IK Solvers...
833                  *
834                  * - These require separate processing steps are pose-level
835                  *   to be executed between chains of bones (i.e. once the
836                  *   base transforms of a bunch of bones is done)
837                  *
838                  * Unsolved Issues:
839                  * - Care is needed to ensure that multi-headed trees work out the same as in ik-tree building
840                  * - Animated chain-lengths are a problem...
841                  */
842                 for (bConstraint *con = (bConstraint *)pchan->constraints.first; con; con = con->next) {
843                         switch (con->type) {
844                                 case CONSTRAINT_TYPE_KINEMATIC:
845                                         build_ik_pose(scene, ob, pchan, con);
846                                         break;
847
848                                 case CONSTRAINT_TYPE_SPLINEIK:
849                                         build_splineik_pose(scene, ob, pchan, con);
850                                         break;
851
852                                 default:
853                                         break;
854                         }
855                 }
856         }
857 }
858
859 void DepsgraphNodeBuilder::build_proxy_rig(Object *ob)
860 {
861         ID *obdata = (ID *)ob->data;
862         build_animdata(obdata);
863
864         add_operation_node(&ob->id,
865                            DEPSNODE_TYPE_EVAL_POSE,
866                            DEPSOP_TYPE_INIT,
867                            function_bind(BKE_pose_eval_proxy_copy, _1, ob),
868                            DEG_OPCODE_POSE_INIT);
869
870         for (bPoseChannel *pchan = (bPoseChannel *)ob->pose->chanbase.first;
871              pchan != NULL;
872              pchan = pchan->next)
873         {
874                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
875                                    DEPSOP_TYPE_INIT, NULL,
876                                    DEG_OPCODE_BONE_LOCAL);
877
878                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
879                                    DEPSOP_TYPE_EXEC, NULL,
880                                    DEG_OPCODE_BONE_READY);
881
882                 add_operation_node(&ob->id, DEPSNODE_TYPE_BONE, pchan->name,
883                                    DEPSOP_TYPE_POST, NULL,
884                                    DEG_OPCODE_BONE_DONE);
885         }
886
887         add_operation_node(&ob->id,
888                            DEPSNODE_TYPE_EVAL_POSE,
889                            DEPSOP_TYPE_POST,
890                            NULL,
891                            DEG_OPCODE_POSE_DONE);
892 }
893
894 /* Shapekeys */
895 void DepsgraphNodeBuilder::build_shapekeys(Key *key)
896 {
897         build_animdata(&key->id);
898 }
899
900 /* ObData Geometry Evaluation */
901 // XXX: what happens if the datablock is shared!
902 void DepsgraphNodeBuilder::build_obdata_geom(Scene *scene, Object *ob)
903 {
904         ID *obdata = (ID *)ob->data;
905
906         /* Temporary uber-update node, which does everything.
907          * It is for the being we're porting old dependencies into the new system.
908          * We'll get rid of this node as soon as all the granular update functions
909          * are filled in.
910          *
911          * TODO(sergey): Get rid of this node.
912          */
913         add_operation_node(&ob->id, DEPSNODE_TYPE_GEOMETRY,
914                            DEPSOP_TYPE_POST, function_bind(BKE_object_eval_uber_data, _1, scene, ob),
915                            DEG_OPCODE_GEOMETRY_UBEREVAL);
916
917         add_operation_node(&ob->id, DEPSNODE_TYPE_GEOMETRY,
918                            DEPSOP_TYPE_INIT, NULL,
919                            DEG_OPCODE_PLACEHOLDER, "Eval Init");
920
921         // TODO: "Done" operation
922
923         /* ShapeKeys */
924         Key *key = BKE_key_from_object(ob);
925         if (key)
926                 build_shapekeys(key);
927
928         /* Modifiers */
929         if (ob->modifiers.first) {
930                 ModifierData *md;
931
932                 for (md = (ModifierData *)ob->modifiers.first; md; md = md->next) {
933                         add_operation_node(&ob->id, DEPSNODE_TYPE_GEOMETRY,
934                                            DEPSOP_TYPE_EXEC, function_bind(BKE_object_eval_modifier, _1, scene, ob, md),
935                                            DEG_OPCODE_GEOMETRY_MODIFIER, md->name);
936                 }
937         }
938
939         /* materials */
940         if (ob->totcol) {
941                 int a;
942
943                 for (a = 1; a <= ob->totcol; a++) {
944                         Material *ma = give_current_material(ob, a);
945
946                         if (ma) {
947                                 // XXX?!
948                                 ComponentDepsNode *geom_node = add_component_node(&ob->id, DEPSNODE_TYPE_GEOMETRY);
949                                 build_material(geom_node, ma);
950                         }
951                 }
952         }
953
954         /* geometry collision */
955         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_LATTICE)) {
956                 // add geometry collider relations
957         }
958
959         if (obdata->flag & LIB_DOIT) {
960                 return;
961         }
962
963         build_animdata(obdata);
964
965         /* nodes for result of obdata's evaluation, and geometry evaluation on object */
966         switch (ob->type) {
967                 case OB_MESH:
968                 {
969                         //Mesh *me = (Mesh *)ob->data;
970
971                         /* evaluation operations */
972                         add_operation_node(obdata, DEPSNODE_TYPE_GEOMETRY,
973                                            DEPSOP_TYPE_INIT, function_bind(BKE_mesh_eval_geometry, _1, (Mesh *)obdata),
974                                            DEG_OPCODE_PLACEHOLDER, "Geometry Eval");
975                 }
976                 break;
977
978                 case OB_MBALL:
979                 {
980                         Object *mom = BKE_mball_basis_find(scene, ob);
981
982                         /* motherball - mom depends on children! */
983                         if (mom == ob) {
984                                 /* metaball evaluation operations */
985                                 /* NOTE: only the motherball gets evaluated! */
986                                 add_operation_node(obdata, DEPSNODE_TYPE_GEOMETRY,
987                                                    DEPSOP_TYPE_INIT, function_bind(BKE_mball_eval_geometry, _1, (MetaBall *)obdata),
988                                                    DEG_OPCODE_PLACEHOLDER, "Geometry Eval");
989                         }
990                 }
991                 break;
992
993                 case OB_CURVE:
994                 case OB_FONT:
995                 {
996                         /* curve evaluation operations */
997                         /* - calculate curve geometry (including path) */
998                         add_operation_node(obdata, DEPSNODE_TYPE_GEOMETRY,
999                                            DEPSOP_TYPE_INIT, function_bind(BKE_curve_eval_geometry, _1, (Curve *)obdata),
1000                                            DEG_OPCODE_PLACEHOLDER, "Geometry Eval");
1001
1002                         /* - calculate curve path - this is used by constraints, etc. */
1003                         add_operation_node(obdata, DEPSNODE_TYPE_GEOMETRY,
1004                                            DEPSOP_TYPE_EXEC, function_bind(BKE_curve_eval_path, _1, (Curve *)obdata),
1005                                            DEG_OPCODE_GEOMETRY_PATH, "Path");
1006                 }
1007                 break;
1008
1009                 case OB_SURF: /* Nurbs Surface */
1010                 {
1011                         /* nurbs evaluation operations */
1012                         add_operation_node(obdata, DEPSNODE_TYPE_GEOMETRY,
1013                                            DEPSOP_TYPE_INIT, function_bind(BKE_curve_eval_geometry, _1, (Curve *)obdata),
1014                                            DEG_OPCODE_PLACEHOLDER, "Geometry Eval");
1015                 }
1016                 break;
1017
1018                 case OB_LATTICE: /* Lattice */
1019                 {
1020                         /* lattice evaluation operations */
1021                         add_operation_node(obdata, DEPSNODE_TYPE_GEOMETRY,
1022                                            DEPSOP_TYPE_INIT, function_bind(BKE_lattice_eval_geometry, _1, (Lattice *)obdata),
1023                                            DEG_OPCODE_PLACEHOLDER, "Geometry Eval");
1024                 }
1025                 break;
1026         }
1027
1028         add_operation_node(obdata, DEPSNODE_TYPE_GEOMETRY,
1029                            DEPSOP_TYPE_POST, NULL,
1030                            DEG_OPCODE_PLACEHOLDER, "Eval Done");
1031
1032         /* Parameters for driver sources. */
1033         add_operation_node(obdata, DEPSNODE_TYPE_PARAMETERS, DEPSOP_TYPE_EXEC, NULL,
1034                            DEG_OPCODE_PLACEHOLDER, "Parameters Eval");
1035 }
1036
1037 /* Cameras */
1038 void DepsgraphNodeBuilder::build_camera(Object *ob)
1039 {
1040         /* TODO: Link scene-camera links in somehow... */
1041         Camera *cam = (Camera *)ob->data;
1042         ID *camera_id = &cam->id;
1043         if (camera_id->flag & LIB_DOIT) {
1044                 return;
1045         }
1046
1047         build_animdata(&cam->id);
1048
1049         add_operation_node(camera_id, DEPSNODE_TYPE_PARAMETERS, DEPSOP_TYPE_EXEC, NULL,
1050                            DEG_OPCODE_PLACEHOLDER, "Parameters Eval");
1051
1052         if (cam->dof_ob != NULL) {
1053                 /* TODO(sergey): For now parametrs are on object level. */
1054                 add_operation_node(&ob->id, DEPSNODE_TYPE_PARAMETERS,
1055                                    DEPSOP_TYPE_EXEC, NULL,
1056                                    DEG_OPCODE_PLACEHOLDER,
1057                                    "Camera DOF");
1058         }
1059 }
1060
1061 /* Lamps */
1062 void DepsgraphNodeBuilder::build_lamp(Object *ob)
1063 {
1064         Lamp *la = (Lamp *)ob->data;
1065         ID *lamp_id = &la->id;
1066         if (lamp_id->flag & LIB_DOIT) {
1067                 return;
1068         }
1069
1070         build_animdata(&la->id);
1071
1072         /* node for obdata */
1073         ComponentDepsNode *param_node = add_component_node(lamp_id, DEPSNODE_TYPE_PARAMETERS);
1074
1075         /* TODO(sergey): Is it really how we're supposed to work with drivers? */
1076         add_operation_node(lamp_id, DEPSNODE_TYPE_PARAMETERS, DEPSOP_TYPE_EXEC, NULL,
1077                            DEG_OPCODE_PLACEHOLDER, "Parameters Eval");
1078
1079         /* lamp's nodetree */
1080         if (la->nodetree) {
1081                 build_nodetree(param_node, la->nodetree);
1082         }
1083
1084         /* textures */
1085         build_texture_stack(param_node, la->mtex);
1086 }
1087
1088 void DepsgraphNodeBuilder::build_nodetree(DepsNode *owner_node, bNodeTree *ntree)
1089 {
1090         if (!ntree)
1091                 return;
1092
1093         /* nodetree itself */
1094         ID *ntree_id = &ntree->id;
1095
1096         build_animdata(ntree_id);
1097
1098         /* Parameters for drivers. */
1099         add_operation_node(ntree_id, DEPSNODE_TYPE_PARAMETERS, DEPSOP_TYPE_EXEC, NULL,
1100                            DEG_OPCODE_PLACEHOLDER, "Parameters Eval");
1101
1102         /* nodetree's nodes... */
1103         for (bNode *bnode = (bNode *)ntree->nodes.first; bnode; bnode = bnode->next) {
1104                 if (bnode->id) {
1105                         if (GS(bnode->id->name) == ID_MA) {
1106                                 build_material(owner_node, (Material *)bnode->id);
1107                         }
1108                         else if (bnode->type == ID_TE) {
1109                                 build_texture(owner_node, (Tex *)bnode->id);
1110                         }
1111                         else if (bnode->type == NODE_GROUP) {
1112                                 bNodeTree *ntree = (bNodeTree *)bnode->id;
1113                                 if ((ntree_id->flag & LIB_DOIT) == 0) {
1114                                         build_nodetree(owner_node, ntree);
1115                                 }
1116                         }
1117                 }
1118         }
1119
1120         // TODO: link from nodetree to owner_component?
1121 }
1122
1123 /* Recursively build graph for material */
1124 void DepsgraphNodeBuilder::build_material(DepsNode *owner_node, Material *ma)
1125 {
1126         ID *ma_id = &ma->id;
1127         if (ma_id->flag & LIB_DOIT) {
1128                 return;
1129         }
1130
1131         /* material itself */
1132         add_id_node(ma_id);
1133
1134         add_operation_node(ma_id, DEPSNODE_TYPE_SHADING,
1135                            DEPSOP_TYPE_EXEC, NULL,
1136                            DEG_OPCODE_PLACEHOLDER, "Material Update");
1137
1138         /* material animation */
1139         build_animdata(ma_id);
1140
1141         /* textures */
1142         build_texture_stack(owner_node, ma->mtex);
1143
1144         /* material's nodetree */
1145         build_nodetree(owner_node, ma->nodetree);
1146 }
1147
1148 /* Texture-stack attached to some shading datablock */
1149 void DepsgraphNodeBuilder::build_texture_stack(DepsNode *owner_node, MTex **texture_stack)
1150 {
1151         int i;
1152
1153         /* for now assume that all texture-stacks have same number of max items */
1154         for (i = 0; i < MAX_MTEX; i++) {
1155                 MTex *mtex = texture_stack[i];
1156                 if (mtex && mtex->tex)
1157                         build_texture(owner_node, mtex->tex);
1158         }
1159 }
1160
1161 /* Recursively build graph for texture */
1162 void DepsgraphNodeBuilder::build_texture(DepsNode *owner_node, Tex *tex)
1163 {
1164         ID *tex_id = &tex->id;
1165         if (tex_id->flag & LIB_DOIT) {
1166                 return;
1167         }
1168         tex_id->flag |= LIB_DOIT;
1169         /* texture itself */
1170         build_animdata(tex_id);
1171         /* texture's nodetree */
1172         build_nodetree(owner_node, tex->nodetree);
1173 }
1174
1175 void DepsgraphNodeBuilder::build_compositor(Scene *scene)
1176 {
1177         /* For now, just a plain wrapper? */
1178         // TODO: create compositing component?
1179         // XXX: component type undefined!
1180         //graph->get_node(&scene->id, NULL, DEPSNODE_TYPE_COMPOSITING, NULL);
1181
1182         /* for now, nodetrees are just parameters; compositing occurs in internals of renderer... */
1183         ComponentDepsNode *owner_node = add_component_node(&scene->id, DEPSNODE_TYPE_PARAMETERS);
1184         build_nodetree(owner_node, scene->nodetree);
1185 }
1186
1187 void DepsgraphNodeBuilder::build_gpencil(bGPdata *gpd)
1188 {
1189         ID *gpd_id = &gpd->id;
1190
1191         /* gpencil itself */
1192         // XXX: what about multiple users of same datablock? This should only get added once
1193         add_id_node(gpd_id);
1194
1195         /* The main reason Grease Pencil is included here is because the animation (and drivers)
1196          * need to be hosted somewhere...
1197          */
1198         build_animdata(gpd_id);
1199 }