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