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