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