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