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