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