Fix T52705: Lamps shadows are not refreshed when tweaking lamps parameters
[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 void free_copy_on_write_datablock(void *id_v)
148 {
149         ID *id = (ID *)id_v;
150         deg_free_copy_on_write_datablock(id);
151 }
152
153 }  /* namespace */
154
155 /* ************ */
156 /* Node Builder */
157
158 /* **** General purpose functions **** */
159
160 DepsgraphNodeBuilder::DepsgraphNodeBuilder(Main *bmain, Depsgraph *graph) :
161     m_bmain(bmain),
162     m_graph(graph),
163     m_cow_id_hash(NULL)
164 {
165 }
166
167 DepsgraphNodeBuilder::~DepsgraphNodeBuilder()
168 {
169         if (m_cow_id_hash != NULL) {
170                 BLI_ghash_free(m_cow_id_hash, NULL, free_copy_on_write_datablock);
171         }
172 }
173
174 IDDepsNode *DepsgraphNodeBuilder::add_id_node(ID *id, bool do_tag)
175 {
176 #ifdef WITH_COPY_ON_WRITE
177         IDDepsNode *id_node = NULL;
178         ID *id_cow = (ID *)BLI_ghash_lookup(m_cow_id_hash, id);
179         if (id_cow != NULL) {
180                 /* TODO(sergey): Is it possible to lookup and pop element from GHash
181                  * at the same time?
182                  */
183                 BLI_ghash_remove(m_cow_id_hash, id, NULL, NULL);
184         }
185         id_node = m_graph->add_id_node(id, do_tag, id_cow);
186         /* Currently all ID nodes are supposed to have copy-on-write logic.
187          *
188          * NOTE: Zero number of components indicates that ID node was just created.
189          */
190         if (BLI_ghash_size(id_node->components) == 0) {
191                 ComponentDepsNode *comp_cow =
192                         id_node->add_component(DEG_NODE_TYPE_COPY_ON_WRITE);
193                 OperationDepsNode *op_cow = comp_cow->add_operation(
194                     function_bind(deg_evaluate_copy_on_write, _1, m_graph, id_node),
195                     DEG_OPCODE_COPY_ON_WRITE,
196                     "", -1);
197                 m_graph->operations.push_back(op_cow);
198         }
199 #else
200         IDDepsNode *id_node = m_graph->add_id_node(id);
201         UNUSED_VARS(do_tag);
202 #endif
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 #ifdef WITH_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, id_node->id_orig, id_node->id_cow);
372                         id_node->id_cow = NULL;
373                 }
374         }
375         GHASH_FOREACH_END();
376 #endif
377
378         /* Make sure graph has no nodes left from previous state. */
379         m_graph->clear_all_nodes();
380         m_graph->operations.clear();
381         BLI_gset_clear(m_graph->entry_tags, NULL);
382 }
383
384 void DepsgraphNodeBuilder::build_group(Scene *scene, Group *group)
385 {
386         ID *group_id = &group->id;
387         if (group_id->tag & LIB_TAG_DOIT) {
388                 return;
389         }
390         group_id->tag |= LIB_TAG_DOIT;
391
392         LINKLIST_FOREACH (GroupObject *, go, &group->gobject) {
393                 build_object(scene, go->ob);
394         }
395 }
396
397 void DepsgraphNodeBuilder::build_object(Scene *scene, Object *ob)
398 {
399         /* Skip rest of components if the ID node was already there. */
400         if (ob->id.tag & LIB_TAG_DOIT) {
401                 return;
402         }
403         ob->id.tag |= LIB_TAG_DOIT;
404
405         /* Create ID node for obejct and begin init. */
406         IDDepsNode *id_node = add_id_node(&ob->id);
407         ob->customdata_mask = 0;
408
409         /* Standard components. */
410         build_object_transform(scene, ob);
411
412         if (ob->parent != NULL) {
413                 build_object(scene, ob->parent);
414         }
415         if (ob->modifiers.first != NULL) {
416                 BuilderWalkUserData data;
417                 data.builder = this;
418                 data.scene = scene;
419                 modifiers_foreachObjectLink(ob, modifier_walk, &data);
420         }
421         if (ob->constraints.first != NULL) {
422                 BuilderWalkUserData data;
423                 data.builder = this;
424                 data.scene = scene;
425                 BKE_constraints_id_loop(&ob->constraints, constraint_walk, &data);
426         }
427
428         /* Object data. */
429         if (ob->data != NULL) {
430                 /* type-specific data... */
431                 switch (ob->type) {
432                         case OB_MESH:     /* Geometry */
433                         case OB_CURVE:
434                         case OB_FONT:
435                         case OB_SURF:
436                         case OB_MBALL:
437                         case OB_LATTICE:
438                                 build_obdata_geom(scene, ob);
439                                 /* TODO(sergey): Only for until we support granular
440                                  * update of curves.
441                                  */
442                                 if (ob->type == OB_FONT) {
443                                         Curve *curve = (Curve *)ob->data;
444                                         if (curve->textoncurve) {
445                                                 id_node->eval_flags |= DAG_EVAL_NEED_CURVE_PATH;
446                                         }
447                                 }
448                                 break;
449
450                         case OB_ARMATURE: /* Pose */
451                                 if (ID_IS_LINKED_DATABLOCK(ob) && ob->proxy_from != NULL) {
452                                         build_proxy_rig(ob);
453                                 }
454                                 else {
455                                         build_rig(scene, ob);
456                                 }
457                                 break;
458
459                         case OB_LAMP:   /* Lamp */
460                                 build_lamp(ob);
461                                 break;
462
463                         case OB_CAMERA: /* Camera */
464                                 build_camera(ob);
465                                 break;
466
467                         case OB_LIGHTPROBE:
468                                 build_lightprobe(ob);
469                                 break;
470
471                         default:
472                         {
473                                 ID *obdata = (ID *)ob->data;
474                                 if ((obdata->tag & LIB_TAG_DOIT) == 0) {
475                                         build_animdata(obdata);
476                                 }
477                                 break;
478                         }
479                 }
480         }
481
482         /* Build animation data,
483          *
484          * Do it now because it's possible object data will affect
485          * on object's level animation, for example in case of rebuilding
486          * pose for proxy.
487          */
488         build_animdata(&ob->id);
489
490         /* particle systems */
491         if (ob->particlesystem.first != NULL) {
492                 build_particles(scene, ob);
493         }
494
495         /* Grease pencil. */
496         if (ob->gpd != NULL) {
497                 build_gpencil(ob->gpd);
498         }
499
500         /* Object that this is a proxy for. */
501         if (ob->proxy) {
502                 ob->proxy->proxy_from = ob;
503                 build_object(scene, ob->proxy);
504         }
505
506         /* Object dupligroup. */
507         if (ob->dup_group != NULL) {
508                 build_group(scene, ob->dup_group);
509         }
510 }
511
512 void DepsgraphNodeBuilder::build_object_transform(Scene *scene, Object *ob)
513 {
514         OperationDepsNode *op_node;
515         Scene *scene_cow = get_cow_datablock(scene);
516         Object *ob_cow = get_cow_datablock(ob);
517
518         /* local transforms (from transform channels - loc/rot/scale + deltas) */
519         op_node = add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
520                                      function_bind(BKE_object_eval_local_transform,
521                                                    _1,
522                                                    scene_cow, ob_cow),
523                                      DEG_OPCODE_TRANSFORM_LOCAL);
524         op_node->set_as_entry();
525
526         /* object parent */
527         if (ob->parent != NULL) {
528                 add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
529                                    function_bind(BKE_object_eval_parent,
530                                                  _1,
531                                                  scene_cow, ob_cow),
532                                    DEG_OPCODE_TRANSFORM_PARENT);
533         }
534
535         /* object constraints */
536         if (ob->constraints.first != NULL) {
537                 build_object_constraints(scene, ob);
538         }
539
540         /* Rest of transformation update. */
541         add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
542                            function_bind(BKE_object_eval_uber_transform,
543                                          _1,
544                                          scene_cow, ob_cow),
545                            DEG_OPCODE_TRANSFORM_OBJECT_UBEREVAL);
546
547         /* object transform is done */
548         op_node = add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
549                                      function_bind(BKE_object_eval_done, _1, ob_cow),
550                                      DEG_OPCODE_TRANSFORM_FINAL);
551         op_node->set_as_exit();
552 }
553
554 /**
555  * Constraints Graph Notes
556  *
557  * For constraints, we currently only add a operation node to the Transform
558  * or Bone components (depending on whichever type of owner we have).
559  * This represents the entire constraints stack, which is for now just
560  * executed as a single monolithic block. At least initially, this should
561  * be sufficient for ensuring that the porting/refactoring process remains
562  * manageable.
563  *
564  * However, when the time comes for developing "node-based" constraints,
565  * we'll need to split this up into pre/post nodes for "constraint stack
566  * evaluation" + operation nodes for each constraint (i.e. the contents
567  * of the loop body used in the current "solve_constraints()" operation).
568  *
569  * -- Aligorith, August 2013
570  */
571 void DepsgraphNodeBuilder::build_object_constraints(Scene *scene, Object *ob)
572 {
573         /* create node for constraint stack */
574         add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
575                            function_bind(BKE_object_eval_constraints, _1,
576                                          get_cow_datablock(scene),
577                                          get_cow_datablock(ob)),
578                            DEG_OPCODE_TRANSFORM_CONSTRAINTS);
579 }
580
581 /**
582  * Build graph nodes for AnimData block
583  * \param id: ID-Block which hosts the AnimData
584  */
585 void DepsgraphNodeBuilder::build_animdata(ID *id)
586 {
587         AnimData *adt = BKE_animdata_from_id(id);
588         if (adt == NULL) {
589                 return;
590         }
591
592         /* animation */
593         if (adt->action || adt->nla_tracks.first || adt->drivers.first) {
594                 (void) add_id_node(id);
595                 ID *id_cow = get_cow_id(id);
596
597                 // XXX: Hook up specific update callbacks for special properties which
598                 // may need it...
599
600                 /* actions and NLA - as a single unit for now, as it gets complicated to
601                  * schedule otherwise.
602                  */
603                 if ((adt->action) || (adt->nla_tracks.first)) {
604                         /* create the node */
605                         add_operation_node(id, DEG_NODE_TYPE_ANIMATION,
606                                            function_bind(BKE_animsys_eval_animdata, _1, id_cow),
607                                            DEG_OPCODE_ANIMATION, id->name);
608
609                         /* TODO: for each channel affected, we might also want to add some
610                          * support for running RNA update callbacks on them
611                          * (which will be needed for proper handling of drivers later)
612                          */
613                 }
614
615                 /* drivers */
616                 LINKLIST_FOREACH (FCurve *, fcu, &adt->drivers) {
617                         /* create driver */
618                         build_driver(id, fcu);
619                 }
620         }
621 }
622
623 /**
624  * Build graph node(s) for Driver
625  * \param id: ID-Block that driver is attached to
626  * \param fcu: Driver-FCurve
627  */
628 OperationDepsNode *DepsgraphNodeBuilder::build_driver(ID *id, FCurve *fcu)
629 {
630         ID *id_cow = get_cow_id(id);
631
632         /* Create data node for this driver */
633         /* TODO(sergey): Avoid creating same operation multiple times,
634          * in the future we need to avoid lookup of the operation as well
635          * and use some tagging magic instead.
636          */
637         OperationDepsNode *driver_op = find_operation_node(id,
638                                                            DEG_NODE_TYPE_PARAMETERS,
639                                                            DEG_OPCODE_DRIVER,
640                                                            fcu->rna_path ? fcu->rna_path : "",
641                                                            fcu->array_index);
642
643         if (driver_op == NULL) {
644                 /* TODO(sergey): Shall we use COW of fcu itself here? */
645                 driver_op = add_operation_node(id,
646                                                DEG_NODE_TYPE_PARAMETERS,
647                                                function_bind(BKE_animsys_eval_driver, _1, id_cow, fcu),
648                                                DEG_OPCODE_DRIVER,
649                                                fcu->rna_path ? fcu->rna_path : "",
650                                                fcu->array_index);
651         }
652
653         /* return driver node created */
654         return driver_op;
655 }
656
657 /* Recursively build graph for world */
658 void DepsgraphNodeBuilder::build_world(World *world)
659 {
660         ID *world_id = &world->id;
661         if (world_id->tag & LIB_TAG_DOIT) {
662                 return;
663         }
664
665         build_animdata(world_id);
666
667         /* world itself */
668         add_operation_node(world_id,
669                            DEG_NODE_TYPE_SHADING,
670                            function_bind(BKE_world_eval, _1, world),
671                            DEG_OPCODE_WORLD_UPDATE);
672
673         /* textures */
674         build_texture_stack(world->mtex);
675
676         /* world's nodetree */
677         if (world->nodetree) {
678                 build_nodetree(world->nodetree);
679         }
680 }
681
682 /* Rigidbody Simulation - Scene Level */
683 void DepsgraphNodeBuilder::build_rigidbody(Scene *scene)
684 {
685         RigidBodyWorld *rbw = scene->rigidbody_world;
686         Scene *scene_cow = get_cow_datablock(scene);
687
688         /**
689          * Rigidbody Simulation Nodes
690          * ==========================
691          *
692          * There are 3 nodes related to Rigidbody Simulation:
693          * 1) "Initialize/Rebuild World" - this is called sparingly, only when the
694          *    simulation needs to be rebuilt (mainly after file reload, or moving
695          *    back to start frame)
696          * 2) "Do Simulation" - perform a simulation step - interleaved between the
697          *    evaluation steps for clusters of objects (i.e. between those affected
698          *    and/or not affected by the sim for instance).
699          *
700          * 3) "Pull Results" - grab the specific transforms applied for a specific
701          *    object - performed as part of object's transform-stack building.
702          */
703
704         /* Create nodes --------------------------------------------------------- */
705
706         /* XXX: is this the right component, or do we want to use another one
707          * instead?
708          */
709
710         /* init/rebuild operation */
711         /*OperationDepsNode *init_node =*/ add_operation_node(
712                 &scene->id, DEG_NODE_TYPE_TRANSFORM,
713                 function_bind(BKE_rigidbody_rebuild_sim, _1, scene_cow),
714                 DEG_OPCODE_RIGIDBODY_REBUILD);
715
716         /* do-sim operation */
717         // XXX: what happens if we need to split into several groups?
718         OperationDepsNode *sim_node = add_operation_node(
719                 &scene->id, DEG_NODE_TYPE_TRANSFORM,
720                 function_bind(BKE_rigidbody_eval_simulation, _1, scene_cow),
721                 DEG_OPCODE_RIGIDBODY_SIM);
722
723         /* XXX: For now, the sim node is the only one that really matters here.
724          * If any other sims get added later, we may have to remove these hacks...
725          */
726         sim_node->owner->entry_operation = sim_node;
727         sim_node->owner->exit_operation  = sim_node;
728
729         /* objects - simulation participants */
730         if (rbw->group) {
731                 LINKLIST_FOREACH (GroupObject *, go, &rbw->group->gobject) {
732                         Object *ob = go->ob;
733
734                         if (!ob || (ob->type != OB_MESH))
735                                 continue;
736
737                         /* 2) create operation for flushing results */
738                         /* object's transform component - where the rigidbody operation
739                          * lives. */
740                         add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
741                                            function_bind(BKE_rigidbody_object_sync_transforms,
742                                                          _1,
743                                                          scene_cow,
744                                                          get_cow_datablock(ob)),
745                                            DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
746                 }
747         }
748 }
749
750 void DepsgraphNodeBuilder::build_particles(Scene *scene, Object *ob)
751 {
752         /**
753          * Particle Systems Nodes
754          * ======================
755          *
756          * There are two types of nodes associated with representing
757          * particle systems:
758          *  1) Component (EVAL_PARTICLES) - This is the particle-system
759          *     evaluation context for an object. It acts as the container
760          *     for all the nodes associated with a particular set of particle
761          *     systems.
762          *  2) Particle System Eval Operation - This operation node acts as a
763          *     blackbox evaluation step for one particle system referenced by
764          *     the particle systems stack. All dependencies link to this operation.
765          */
766
767         /* component for all particle systems */
768         ComponentDepsNode *psys_comp =
769                 add_component_node(&ob->id, DEG_NODE_TYPE_EVAL_PARTICLES);
770
771         /* TODO(sergey): Need to get COW of PSYS. */
772         Scene *scene_cow = get_cow_datablock(scene);
773         Object *ob_cow = get_cow_datablock(ob);
774
775         add_operation_node(psys_comp,
776                            function_bind(BKE_particle_system_eval_init,
777                                          _1,
778                                          scene_cow,
779                                          ob_cow),
780                            DEG_OPCODE_PARTICLE_SYSTEM_EVAL_INIT);
781
782         /* particle systems */
783         LINKLIST_FOREACH (ParticleSystem *, psys, &ob->particlesystem) {
784                 ParticleSettings *part = psys->part;
785
786                 /* Build particle settings operations.
787                  *
788                  * NOTE: The call itself ensures settings are only build once.
789                  */
790                 build_particle_settings(part);
791
792                 /* Update on particle settings change. */
793                 add_operation_node(psys_comp,
794                                    function_bind(BKE_particle_system_settings_eval,
795                                                  _1,
796                                                  psys),
797                                    DEG_OPCODE_PARTICLE_SETTINGS_EVAL,
798                                    psys->name);
799
800                 /* Particle system evaluation. */
801                 add_operation_node(psys_comp,
802                                    NULL,
803                                    DEG_OPCODE_PARTICLE_SYSTEM_EVAL,
804                                    psys->name);
805         }
806
807         /* TODO(sergey): Do we need a point cache operations here? */
808 }
809
810 void DepsgraphNodeBuilder::build_particle_settings(ParticleSettings *part) {
811         ID *part_id = &part->id;
812         if (part_id->tag & LIB_TAG_DOIT) {
813                 return;
814         }
815         part_id->tag |= LIB_TAG_DOIT;
816         /* Animation data. */
817         build_animdata(part_id);
818         /* Parameters change. */
819         add_operation_node(part_id,
820                            DEG_NODE_TYPE_PARAMETERS,
821                            NULL,
822                            DEG_OPCODE_PARTICLE_SETTINGS_EVAL);
823         add_operation_node(part_id,
824                            DEG_NODE_TYPE_PARAMETERS,
825                            function_bind(BKE_particle_system_settings_recalc_clear,
826                                          _1,
827                                          part),
828                            DEG_OPCODE_PARTICLE_SETTINGS_RECALC_CLEAR);
829 }
830
831 void DepsgraphNodeBuilder::build_cloth(Scene *scene, Object *object)
832 {
833         Scene *scene_cow = get_cow_datablock(scene);
834         Object *object_cow = get_cow_datablock(object);
835
836         ComponentDepsNode *cache_comp = add_component_node(&object->id,
837                                                            DEG_NODE_TYPE_CACHE);
838         add_operation_node(cache_comp,
839                            function_bind(BKE_object_eval_cloth,
840                                          _1,
841                                          scene_cow,
842                                          object_cow),
843                            DEG_OPCODE_PLACEHOLDER,
844                            "Cloth Modifier");
845 }
846
847 /* Shapekeys */
848 void DepsgraphNodeBuilder::build_shapekeys(Key *key)
849 {
850         build_animdata(&key->id);
851
852         add_operation_node(&key->id, DEG_NODE_TYPE_GEOMETRY, NULL,
853                            DEG_OPCODE_PLACEHOLDER, "Shapekey Eval");
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         /* node for obdata */
1089         add_operation_node(lamp_id,
1090                            DEG_NODE_TYPE_PARAMETERS,
1091                            NULL,
1092                            DEG_OPCODE_PARAMETERS_EVAL);
1093
1094         /* lamp's nodetree */
1095         if (la->nodetree) {
1096                 build_nodetree(la->nodetree);
1097         }
1098
1099         /* textures */
1100         build_texture_stack(la->mtex);
1101 }
1102
1103 void DepsgraphNodeBuilder::build_nodetree(bNodeTree *ntree)
1104 {
1105         if (ntree == NULL) {
1106                 return;
1107         }
1108         /* nodetree itself */
1109         ID *ntree_id = &ntree->id;
1110         add_id_node(ntree_id);
1111         bNodeTree *ntree_cow = get_cow_datablock(ntree);
1112         /* Animation, */
1113         build_animdata(ntree_id);
1114         /* Shading update. */
1115         add_operation_node(ntree_id,
1116                            DEG_NODE_TYPE_SHADING,
1117                            NULL,
1118                            DEG_OPCODE_MATERIAL_UPDATE);
1119         add_operation_node(ntree_id,
1120                            DEG_NODE_TYPE_SHADING_PARAMETERS,
1121                            function_bind(BKE_nodetree_shading_params_eval,
1122                                          _1, ntree_cow, ntree),
1123                            DEG_OPCODE_MATERIAL_UPDATE);
1124         /* nodetree's nodes... */
1125         LINKLIST_FOREACH (bNode *, bnode, &ntree->nodes) {
1126                 ID *id = bnode->id;
1127                 if (id != NULL) {
1128                         ID_Type id_type = GS(id->name);
1129                         if (id_type == ID_MA) {
1130                                 build_material((Material *)id);
1131                         }
1132                         else if (id_type == ID_TE) {
1133                                 build_texture((Tex *)id);
1134                         }
1135                         else if (id_type == ID_IM) {
1136                                 build_image((Image *)id);
1137                         }
1138                         else if (bnode->type == NODE_GROUP) {
1139                                 bNodeTree *group_ntree = (bNodeTree *)id;
1140                                 if ((group_ntree->id.tag & LIB_TAG_DOIT) == 0) {
1141                                         build_nodetree(group_ntree);
1142                                 }
1143                         }
1144                 }
1145         }
1146
1147         // TODO: link from nodetree to owner_component?
1148 }
1149
1150 /* Recursively build graph for material */
1151 void DepsgraphNodeBuilder::build_material(Material *material)
1152 {
1153         ID *material_id = &material->id;
1154         if (material_id->tag & LIB_TAG_DOIT) {
1155                 return;
1156         }
1157         material_id->tag |= LIB_TAG_DOIT;
1158         /* Material itself. */
1159         add_id_node(material_id);
1160         Material *material_cow = get_cow_datablock(material);
1161         /* Shading update. */
1162         add_operation_node(material_id,
1163                            DEG_NODE_TYPE_SHADING,
1164                            function_bind(BKE_material_eval, _1, material_cow),
1165                            DEG_OPCODE_MATERIAL_UPDATE);
1166         /* Material animation. */
1167         build_animdata(material_id);
1168         /* Textures. */
1169         build_texture_stack(material->mtex);
1170         /* Material's nodetree. */
1171         build_nodetree(material->nodetree);
1172 }
1173
1174 /* Texture-stack attached to some shading datablock */
1175 void DepsgraphNodeBuilder::build_texture_stack(MTex **texture_stack)
1176 {
1177         int i;
1178
1179         /* for now assume that all texture-stacks have same number of max items */
1180         for (i = 0; i < MAX_MTEX; i++) {
1181                 MTex *mtex = texture_stack[i];
1182                 if (mtex && mtex->tex)
1183                         build_texture(mtex->tex);
1184         }
1185 }
1186
1187 /* Recursively build graph for texture */
1188 void DepsgraphNodeBuilder::build_texture(Tex *tex)
1189 {
1190         ID *tex_id = &tex->id;
1191         if (tex_id->tag & LIB_TAG_DOIT) {
1192                 return;
1193         }
1194         tex_id->tag |= LIB_TAG_DOIT;
1195         /* Texture itself. */
1196         build_animdata(tex_id);
1197         /* Texture's nodetree. */
1198         build_nodetree(tex->nodetree);
1199         /* Special cases for different IDs which texture uses. */
1200         if (tex->type == TEX_IMAGE) {
1201                 if (tex->ima != NULL) {
1202                         build_image(tex->ima);
1203                 }
1204         }
1205 }
1206
1207 void DepsgraphNodeBuilder::build_image(Image *image) {
1208         ID *image_id = &image->id;
1209         if (image_id->tag & LIB_TAG_DOIT) {
1210                 return;
1211         }
1212         image_id->tag |= LIB_TAG_DOIT;
1213         /* Image ID node itself. */
1214         add_id_node(image_id);
1215         /* Placeholder so we can add relations and tag ID node for update. */
1216         add_operation_node(image_id,
1217                            DEG_NODE_TYPE_PARAMETERS,
1218                            NULL,
1219                            DEG_OPCODE_PLACEHOLDER,
1220                            "Image Eval");
1221 }
1222
1223 void DepsgraphNodeBuilder::build_compositor(Scene *scene)
1224 {
1225         /* For now, just a plain wrapper? */
1226         // TODO: create compositing component?
1227         // XXX: component type undefined!
1228         //graph->get_node(&scene->id, NULL, DEG_NODE_TYPE_COMPOSITING, NULL);
1229
1230         /* for now, nodetrees are just parameters; compositing occurs in internals of renderer... */
1231         add_component_node(&scene->id, DEG_NODE_TYPE_PARAMETERS);
1232         build_nodetree(scene->nodetree);
1233 }
1234
1235 void DepsgraphNodeBuilder::build_gpencil(bGPdata *gpd)
1236 {
1237         ID *gpd_id = &gpd->id;
1238
1239         /* gpencil itself */
1240         // XXX: what about multiple users of same datablock? This should only get added once
1241         add_id_node(gpd_id);
1242
1243         /* The main reason Grease Pencil is included here is because the animation (and drivers)
1244          * need to be hosted somewhere...
1245          */
1246         build_animdata(gpd_id);
1247 }
1248
1249 void DepsgraphNodeBuilder::build_cachefile(CacheFile *cache_file)
1250 {
1251         ID *cache_file_id = &cache_file->id;
1252
1253         add_component_node(cache_file_id, DEG_NODE_TYPE_CACHE);
1254         add_operation_node(cache_file_id, DEG_NODE_TYPE_CACHE, NULL,
1255                            DEG_OPCODE_PLACEHOLDER, "Cache File Update");
1256
1257         add_id_node(cache_file_id);
1258         build_animdata(cache_file_id);
1259 }
1260
1261 void DepsgraphNodeBuilder::build_mask(Mask *mask)
1262 {
1263         ID *mask_id = &mask->id;
1264         add_id_node(mask_id);
1265         build_animdata(mask_id);
1266 }
1267
1268 void DepsgraphNodeBuilder::build_movieclip(MovieClip *clip)
1269 {
1270         ID *clip_id = &clip->id;
1271         add_id_node(clip_id);
1272         build_animdata(clip_id);
1273 }
1274
1275 void DepsgraphNodeBuilder::build_lightprobe(Object *object)
1276 {
1277         LightProbe *probe = (LightProbe *)object->data;
1278         ID *probe_id = &probe->id;
1279         if (probe_id->tag & LIB_TAG_DOIT) {
1280                 return;
1281         }
1282         probe_id->tag |= LIB_TAG_DOIT;
1283         /* Placeholder so we can add relations and tag ID node for update. */
1284         add_operation_node(probe_id,
1285                            DEG_NODE_TYPE_PARAMETERS,
1286                            NULL,
1287                            DEG_OPCODE_PLACEHOLDER,
1288                            "LightProbe Eval");
1289
1290         add_operation_node(&object->id,
1291                            DEG_NODE_TYPE_PARAMETERS,
1292                            NULL,
1293                            DEG_OPCODE_PLACEHOLDER,
1294                            "LightProbe Eval");
1295
1296         build_animdata(probe_id);
1297 }
1298
1299 }  // namespace DEG