Depsgraph: Use operation code for dupli-group
[blender.git] / source / blender / depsgraph / intern / builder / deg_builder_nodes.cc
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2013 Blender Foundation.
17  * All rights reserved.
18  *
19  * Original Author: Joshua Leung
20  */
21
22 /** \file blender/depsgraph/intern/builder/deg_builder_nodes.cc
23  *  \ingroup depsgraph
24  *
25  * Methods for constructing depsgraph's nodes
26  */
27
28 #include "intern/builder/deg_builder_nodes.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_blenlib.h"
36 #include "BLI_string.h"
37 #include "BLI_utildefines.h"
38
39 extern "C" {
40 #include "DNA_action_types.h"
41 #include "DNA_anim_types.h"
42 #include "DNA_armature_types.h"
43 #include "DNA_cachefile_types.h"
44 #include "DNA_camera_types.h"
45 #include "DNA_collection_types.h"
46 #include "DNA_constraint_types.h"
47 #include "DNA_curve_types.h"
48 #include "DNA_effect_types.h"
49 #include "DNA_gpencil_types.h"
50 #include "DNA_key_types.h"
51 #include "DNA_lamp_types.h"
52 #include "DNA_material_types.h"
53 #include "DNA_mask_types.h"
54 #include "DNA_mesh_types.h"
55 #include "DNA_meta_types.h"
56 #include "DNA_movieclip_types.h"
57 #include "DNA_node_types.h"
58 #include "DNA_particle_types.h"
59 #include "DNA_object_types.h"
60 #include "DNA_lightprobe_types.h"
61 #include "DNA_rigidbody_types.h"
62 #include "DNA_scene_types.h"
63 #include "DNA_speaker_types.h"
64 #include "DNA_texture_types.h"
65 #include "DNA_world_types.h"
66
67 #include "BKE_action.h"
68 #include "BKE_armature.h"
69 #include "BKE_animsys.h"
70 #include "BKE_collection.h"
71 #include "BKE_constraint.h"
72 #include "BKE_curve.h"
73 #include "BKE_effect.h"
74 #include "BKE_fcurve.h"
75 #include "BKE_gpencil.h"
76 #include "BKE_gpencil_modifier.h"
77 #include "BKE_idcode.h"
78 #include "BKE_key.h"
79 #include "BKE_lattice.h"
80 #include "BKE_mask.h"
81 #include "BKE_material.h"
82 #include "BKE_mesh.h"
83 #include "BKE_mball.h"
84 #include "BKE_modifier.h"
85 #include "BKE_movieclip.h"
86 #include "BKE_node.h"
87 #include "BKE_object.h"
88 #include "BKE_particle.h"
89 #include "BKE_pointcache.h"
90 #include "BKE_rigidbody.h"
91 #include "BKE_shader_fx.h"
92 #include "BKE_sound.h"
93 #include "BKE_tracking.h"
94 #include "BKE_world.h"
95
96 #include "RNA_access.h"
97 #include "RNA_types.h"
98 } /* extern "C" */
99
100 #include "DEG_depsgraph.h"
101 #include "DEG_depsgraph_build.h"
102
103 #include "intern/builder/deg_builder.h"
104 #include "intern/depsgraph.h"
105 #include "intern/eval/deg_eval_copy_on_write.h"
106 #include "intern/node/deg_node.h"
107 #include "intern/node/deg_node_component.h"
108 #include "intern/node/deg_node_id.h"
109 #include "intern/node/deg_node_operation.h"
110 #include "intern/depsgraph_type.h"
111
112 namespace DEG {
113
114 namespace {
115
116 void free_copy_on_write_datablock(void *id_info_v)
117 {
118         DepsgraphNodeBuilder::IDInfo *id_info =
119             (DepsgraphNodeBuilder::IDInfo *)id_info_v;
120         if (id_info->id_cow != NULL) {
121                 deg_free_copy_on_write_datablock(id_info->id_cow);
122                 MEM_freeN(id_info->id_cow);
123         }
124         MEM_freeN(id_info);
125 }
126
127 }  /* namespace */
128
129 /* ************ */
130 /* Node Builder */
131
132 /* **** General purpose functions **** */
133
134 DepsgraphNodeBuilder::DepsgraphNodeBuilder(Main *bmain, Depsgraph *graph)
135     : bmain_(bmain),
136       graph_(graph),
137       scene_(NULL),
138       view_layer_(NULL),
139       view_layer_index_(-1),
140       collection_(NULL),
141       is_parent_collection_visible_(true),
142       id_info_hash_(NULL)
143 {
144 }
145
146 DepsgraphNodeBuilder::~DepsgraphNodeBuilder()
147 {
148         if (id_info_hash_ != NULL) {
149                 BLI_ghash_free(id_info_hash_, NULL, free_copy_on_write_datablock);
150         }
151 }
152
153 IDNode *DepsgraphNodeBuilder::add_id_node(ID *id)
154 {
155         IDNode *id_node = NULL;
156         ID *id_cow = NULL;
157         IDComponentsMask previously_visible_components_mask = 0;
158         uint32_t previous_eval_flags = 0;
159         uint64_t previous_customdata_mask = 0;
160         IDInfo *id_info = (IDInfo *)BLI_ghash_lookup(id_info_hash_, id);
161         if (id_info != NULL) {
162                 id_cow = id_info->id_cow;
163                 previously_visible_components_mask =
164                         id_info->previously_visible_components_mask;
165                 previous_eval_flags = id_info->previous_eval_flags;
166                 previous_customdata_mask = id_info->previous_customdata_mask;
167                 /* Tag ID info to not free the CoW ID pointer. */
168                 id_info->id_cow = NULL;
169         }
170         id_node = graph_->add_id_node(id, id_cow);
171         id_node->previously_visible_components_mask =
172                 previously_visible_components_mask;
173         id_node->previous_eval_flags = previous_eval_flags;
174         id_node->previous_customdata_mask = previous_customdata_mask;
175         /* Currently all ID nodes are supposed to have copy-on-write logic.
176          *
177          * NOTE: Zero number of components indicates that ID node was just created. */
178         if (BLI_ghash_len(id_node->components) == 0) {
179                 ComponentNode *comp_cow =
180                         id_node->add_component(NodeType::COPY_ON_WRITE);
181                 OperationNode *op_cow = comp_cow->add_operation(
182                         function_bind(deg_evaluate_copy_on_write, _1, id_node),
183                         OperationCode::COPY_ON_WRITE,
184                         "", -1);
185                 graph_->operations.push_back(op_cow);
186         }
187         return id_node;
188 }
189
190 IDNode *DepsgraphNodeBuilder::find_id_node(ID *id)
191 {
192         return graph_->find_id_node(id);
193 }
194
195 TimeSourceNode *DepsgraphNodeBuilder::add_time_source()
196 {
197         return graph_->add_time_source();
198 }
199
200 ComponentNode *DepsgraphNodeBuilder::add_component_node(
201         ID *id,
202         NodeType comp_type,
203         const char *comp_name)
204 {
205         IDNode *id_node = add_id_node(id);
206         ComponentNode *comp_node = id_node->add_component(comp_type, comp_name);
207         comp_node->owner = id_node;
208         return comp_node;
209 }
210
211 OperationNode *DepsgraphNodeBuilder::add_operation_node(
212         ComponentNode *comp_node,
213         const DepsEvalOperationCb& op,
214         OperationCode opcode,
215         const char *name,
216         int name_tag)
217 {
218         OperationNode *op_node = comp_node->find_operation(opcode,
219                                                                name,
220                                                                name_tag);
221         if (op_node == NULL) {
222                 op_node = comp_node->add_operation(op, opcode, name, name_tag);
223                 graph_->operations.push_back(op_node);
224         }
225         else {
226                 fprintf(stderr,
227                         "add_operation: Operation already exists - %s has %s at %p\n",
228                         comp_node->identifier().c_str(),
229                         op_node->identifier().c_str(),
230                         op_node);
231                 BLI_assert(!"Should not happen!");
232         }
233         return op_node;
234 }
235
236 OperationNode *DepsgraphNodeBuilder::add_operation_node(
237         ID *id,
238         NodeType comp_type,
239         const char *comp_name,
240         const DepsEvalOperationCb& op,
241         OperationCode opcode,
242         const char *name,
243         int name_tag)
244 {
245         ComponentNode *comp_node = add_component_node(id, comp_type, comp_name);
246         return add_operation_node(comp_node, op, opcode, name, name_tag);
247 }
248
249 OperationNode *DepsgraphNodeBuilder::add_operation_node(
250         ID *id,
251         NodeType comp_type,
252         const DepsEvalOperationCb& op,
253         OperationCode opcode,
254         const char *name,
255         int name_tag)
256 {
257         return add_operation_node(id,
258                                   comp_type,
259                                   "",
260                                   op,
261                                   opcode,
262                                   name,
263                                   name_tag);
264 }
265
266 OperationNode *DepsgraphNodeBuilder::ensure_operation_node(
267         ID *id,
268         NodeType comp_type,
269         const DepsEvalOperationCb& op,
270         OperationCode opcode,
271         const char *name,
272         int name_tag)
273 {
274         OperationNode *operation =
275                 find_operation_node(id, comp_type, opcode, name, name_tag);
276         if (operation != NULL) {
277                 return operation;
278         }
279         return add_operation_node(id, comp_type, op, opcode, name, name_tag);
280 }
281
282 bool DepsgraphNodeBuilder::has_operation_node(ID *id,
283                                               NodeType comp_type,
284                                               const char *comp_name,
285                                               OperationCode 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 OperationNode *DepsgraphNodeBuilder::find_operation_node(
298         ID *id,
299         NodeType comp_type,
300         const char *comp_name,
301         OperationCode opcode,
302         const char *name,
303         int name_tag)
304 {
305         ComponentNode *comp_node = add_component_node(id, comp_type, comp_name);
306         return comp_node->find_operation(opcode, name, name_tag);
307 }
308
309 OperationNode *DepsgraphNodeBuilder::find_operation_node(
310         ID *id,
311         NodeType comp_type,
312         OperationCode 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_COPIED_ON_WRITE) {
327                 /* ID is already remapped to copy-on-write. */
328                 return id_orig;
329         }
330         IDNode *id_node = add_id_node(id_orig);
331         return id_node->id_cow;
332 }
333
334 /* **** Build functions for entity nodes **** */
335
336 void DepsgraphNodeBuilder::begin_build()
337 {
338         /* Store existing copy-on-write versions of datablock, so we can re-use
339          * them for new ID nodes. */
340         id_info_hash_ = BLI_ghash_ptr_new("Depsgraph id hash");
341         for (IDNode *id_node : graph_->id_nodes) {
342                 IDInfo *id_info = (IDInfo *)MEM_mallocN(
343                         sizeof(IDInfo), "depsgraph id info");
344                 if (deg_copy_on_write_is_expanded(id_node->id_cow) &&
345                     id_node->id_orig != id_node->id_cow)
346                 {
347                         id_info->id_cow = id_node->id_cow;
348                 }
349                 else {
350                         id_info->id_cow = NULL;
351                 }
352                 id_info->previously_visible_components_mask =
353                         id_node->visible_components_mask;
354                 id_info->previous_eval_flags = id_node->eval_flags;
355                 id_info->previous_customdata_mask = id_node->customdata_mask;
356                 BLI_ghash_insert(id_info_hash_, id_node->id_orig, id_info);
357                 id_node->id_cow = NULL;
358         }
359
360         GSET_FOREACH_BEGIN(OperationNode *, op_node, graph_->entry_tags)
361         {
362                 ComponentNode *comp_node = op_node->owner;
363                 IDNode *id_node = comp_node->owner;
364
365                 SavedEntryTag entry_tag;
366                 entry_tag.id_orig = id_node->id_orig;
367                 entry_tag.component_type = comp_node->type;
368                 entry_tag.opcode = op_node->opcode;
369                 entry_tag.name = op_node->name;
370                 entry_tag.name_tag = op_node->name_tag;
371                 saved_entry_tags_.push_back(entry_tag);
372         };
373         GSET_FOREACH_END();
374
375         /* Make sure graph has no nodes left from previous state. */
376         graph_->clear_all_nodes();
377         graph_->operations.clear();
378         BLI_gset_clear(graph_->entry_tags, NULL);
379 }
380
381 void DepsgraphNodeBuilder::end_build()
382 {
383         for (const SavedEntryTag& entry_tag : saved_entry_tags_) {
384                 IDNode *id_node = find_id_node(entry_tag.id_orig);
385                 if (id_node == NULL) {
386                         continue;
387                 }
388                 ComponentNode *comp_node =
389                         id_node->find_component(entry_tag.component_type);
390                 if (comp_node == NULL) {
391                         continue;
392                 }
393                 OperationNode *op_node = comp_node->find_operation(entry_tag.opcode, entry_tag.name, entry_tag.name_tag);
394                 if (op_node == NULL) {
395                         continue;
396                 }
397                 /* Since the tag is coming from a saved copy of entry tags, this means
398                  * that originally node was explicitly tagged for user update. */
399                 op_node->tag_update(graph_, DEG_UPDATE_SOURCE_USER_EDIT);
400         }
401 }
402
403 void DepsgraphNodeBuilder::build_id(ID *id)
404 {
405         if (id == NULL) {
406                 return;
407         }
408         switch (GS(id->name)) {
409                 case ID_AC:
410                         build_action((bAction *)id);
411                         break;
412                 case ID_AR:
413                         build_armature((bArmature *)id);
414                         break;
415                 case ID_CA:
416                         build_camera((Camera *)id);
417                         break;
418                 case ID_GR:
419                         build_collection(NULL, (Collection *)id);
420                         break;
421                 case ID_OB:
422                         /* TODO(sergey): Get visibility from a "parent" somehow.
423                          *
424                          * NOTE: Using `false` visibility here should be fine, since if this
425                          * driver affects on something invisible we don't really care if the
426                          * driver gets evaluated (and even don't want this to force object
427                          * to become visible).
428                          *
429                          * If this happened to be affecting visible object, then it is up to
430                          * deg_graph_build_flush_visibility() to ensure visibility of the
431                          * object is true. */
432                         build_object(-1, (Object *)id, DEG_ID_LINKED_INDIRECTLY, false);
433                         break;
434                 case ID_KE:
435                         build_shapekeys((Key *)id);
436                         break;
437                 case ID_LA:
438                         build_lamp((Lamp *)id);
439                         break;
440                 case ID_LP:
441                         build_lightprobe((LightProbe *)id);
442                         break;
443                 case ID_NT:
444                         build_nodetree((bNodeTree *)id);
445                         break;
446                 case ID_MA:
447                         build_material((Material *)id);
448                         break;
449                 case ID_TE:
450                         build_texture((Tex *)id);
451                         break;
452                 case ID_IM:
453                         build_image((Image *)id);
454                         break;
455                 case ID_WO:
456                         build_world((World *)id);
457                         break;
458                 case ID_MSK:
459                         build_mask((Mask *)id);
460                         break;
461                 case ID_MC:
462                         build_movieclip((MovieClip *)id);
463                         break;
464                 case ID_ME:
465                 case ID_CU:
466                 case ID_MB:
467                 case ID_LT:
468                         /* TODO(sergey): Get visibility from a "parent" somehow.
469                          *
470                          * NOTE: Similarly to above, we don't want false-positives on
471                          * visibility. */
472                         build_object_data_geometry_datablock(id, false);
473                         break;
474                 case ID_SPK:
475                         build_speaker((Speaker *)id);
476                         break;
477                 case ID_TXT:
478                         /* Not a part of dependency graph. */
479                         break;
480                 case ID_CF:
481                         build_cachefile((CacheFile *)id);
482                         break;
483                 default:
484                         fprintf(stderr, "Unhandled ID %s\n", id->name);
485                         BLI_assert(!"Should never happen");
486                         break;
487         }
488 }
489
490 void DepsgraphNodeBuilder::build_collection(
491         LayerCollection *from_layer_collection,
492         Collection *collection)
493 {
494         const int restrict_flag = (graph_->mode == DAG_EVAL_VIEWPORT)
495                 ? COLLECTION_RESTRICT_VIEW
496                 : COLLECTION_RESTRICT_RENDER;
497         const bool is_collection_restricted = (collection->flag & restrict_flag);
498         const bool is_collection_visible =
499                 !is_collection_restricted && is_parent_collection_visible_;
500         IDNode *id_node;
501         if (built_map_.checkIsBuiltAndTag(collection)) {
502                 id_node = find_id_node(&collection->id);
503                 if (is_collection_visible &&
504                     id_node->is_directly_visible == false &&
505                     id_node->is_collection_fully_expanded == true)
506                 {
507                         /* Collection became visible, make sure nested collections and
508                          * objects are poked with the new visibility flag, since they
509                          * might become visible too. */
510                 }
511                 else {
512                         return;
513                 }
514         }
515         else {
516                 /* Collection itself. */
517                 id_node = add_id_node(&collection->id);
518                 id_node->is_directly_visible = is_collection_visible;
519         }
520         if (from_layer_collection != NULL) {
521                 /* If we came from layer collection we don't go deeper, view layer
522                  * builder takes care of going deeper. */
523                 return;
524         }
525         /* Backup state. */
526         Collection *current_state_collection = collection_;
527         const bool is_current_parent_collection_visible =
528                 is_parent_collection_visible_;
529         /* Modify state as we've entered new collection/ */
530         collection_ = collection;
531         is_parent_collection_visible_ = is_collection_visible;
532         /* Build collection objects. */
533         LISTBASE_FOREACH (CollectionObject *, cob, &collection->gobject) {
534                 build_object(
535                         -1, cob->ob, DEG_ID_LINKED_INDIRECTLY, is_collection_visible);
536         }
537         /* Build child collections. */
538         LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
539                 build_collection(NULL, child->collection);
540         }
541         /* Restore state. */
542         collection_ = current_state_collection;
543         is_parent_collection_visible_ = is_current_parent_collection_visible;
544         id_node->is_collection_fully_expanded = true;
545 }
546
547 void DepsgraphNodeBuilder::build_object(int base_index,
548                                         Object *object,
549                                         eDepsNode_LinkedState_Type linked_state,
550                                         bool is_visible)
551 {
552         const bool has_object = built_map_.checkIsBuiltAndTag(object);
553         /* Skip rest of components if the ID node was already there. */
554         if (has_object) {
555                 IDNode *id_node = find_id_node(&object->id);
556                 /* We need to build some extra stuff if object becomes linked
557                  * directly. */
558                 if (id_node->linked_state == DEG_ID_LINKED_INDIRECTLY) {
559                         build_object_flags(base_index, object, linked_state);
560                 }
561                 id_node->linked_state = max(id_node->linked_state, linked_state);
562                 if (id_node->linked_state == DEG_ID_LINKED_DIRECTLY) {
563                         id_node->is_directly_visible |= is_visible;
564                 }
565                 return;
566         }
567         /* Create ID node for object and begin init. */
568         IDNode *id_node = add_id_node(&object->id);
569         Object *object_cow = get_cow_datablock(object);
570         id_node->linked_state = linked_state;
571         if (object == scene_->camera) {
572                 id_node->is_directly_visible = true;
573         }
574         else {
575                 id_node->is_directly_visible = is_visible;
576         }
577         /* Various flags, flushing from bases/collections. */
578         build_object_flags(base_index, object, linked_state);
579         /* Transform. */
580         build_object_transform(object);
581         /* Parent. */
582         if (object->parent != NULL) {
583                 build_object(
584                         -1, object->parent, DEG_ID_LINKED_INDIRECTLY, is_visible);
585         }
586         /* Modifiers. */
587         if (object->modifiers.first != NULL) {
588                 BuilderWalkUserData data;
589                 data.builder = this;
590                 data.is_parent_visible = is_visible;
591                 modifiers_foreachIDLink(object, modifier_walk, &data);
592         }
593         /* Grease Pencil Modifiers. */
594         if (object->greasepencil_modifiers.first != NULL) {
595                 BuilderWalkUserData data;
596                 data.builder = this;
597                 data.is_parent_visible = is_visible;
598                 BKE_gpencil_modifiers_foreachIDLink(object, modifier_walk, &data);
599         }
600         /* Shader FX. */
601         if (object->shader_fx.first != NULL) {
602                 BuilderWalkUserData data;
603                 data.builder = this;
604                 data.is_parent_visible = is_visible;
605                 BKE_shaderfx_foreachIDLink(object, modifier_walk, &data);
606         }
607         /* Constraints. */
608         if (object->constraints.first != NULL) {
609                 BuilderWalkUserData data;
610                 data.builder = this;
611                 data.is_parent_visible = is_visible;
612                 BKE_constraints_id_loop(&object->constraints, constraint_walk, &data);
613         }
614         /* Object data. */
615         build_object_data(object, is_visible);
616         /* Build animation data,
617          *
618          * Do it now because it's possible object data will affect
619          * on object's level animation, for example in case of rebuilding
620          * pose for proxy. */
621         OperationNode *op_node = add_operation_node(&object->id,
622                                                         NodeType::PARAMETERS,
623                                                         NULL,
624                                                         OperationCode::PARAMETERS_EVAL);
625         op_node->set_as_exit();
626         build_animdata(&object->id);
627         /* Particle systems. */
628         if (object->particlesystem.first != NULL) {
629                 build_particle_systems(object, is_visible);
630         }
631         /* Proxy object to copy from. */
632         if (object->proxy_from != NULL) {
633                 build_object(
634                         -1, object->proxy_from, DEG_ID_LINKED_INDIRECTLY, is_visible);
635         }
636         if (object->proxy_group != NULL) {
637                 build_object(
638                         -1, object->proxy_group, DEG_ID_LINKED_INDIRECTLY, is_visible);
639         }
640         /* Object dupligroup. */
641         if (object->dup_group != NULL) {
642                 const bool is_current_parent_collection_visible =
643                         is_parent_collection_visible_;
644                 is_parent_collection_visible_ = is_visible;
645                 build_collection(NULL, object->dup_group);
646                 is_parent_collection_visible_ = is_current_parent_collection_visible;
647                 add_operation_node(&object->id,
648                                    NodeType::DUPLI,
649                                    NULL,
650                                    OperationCode::DUPLI);
651         }
652         /* Syncronization back to original object. */
653         add_operation_node(&object->id,
654                            NodeType::SYNCHRONIZATION,
655                            function_bind(BKE_object_synchronize_to_original,
656                                          _1,
657                                          object_cow),
658                            OperationCode::SYNCHRONIZE_TO_ORIGINAL);
659 }
660
661 void DepsgraphNodeBuilder::build_object_flags(
662         int base_index,
663         Object *object,
664         eDepsNode_LinkedState_Type linked_state)
665 {
666         if (base_index == -1) {
667                 return;
668         }
669         Scene *scene_cow = get_cow_datablock(scene_);
670         Object *object_cow = get_cow_datablock(object);
671         const bool is_from_set = (linked_state == DEG_ID_LINKED_VIA_SET);
672         /* TODO(sergey): Is this really best component to be used? */
673         add_operation_node(&object->id,
674                            NodeType::OBJECT_FROM_LAYER,
675                            function_bind(BKE_object_eval_flush_base_flags,
676                                          _1,
677                                          scene_cow,
678                                          view_layer_index_,
679                                          object_cow, base_index,
680                                          is_from_set),
681                            OperationCode::OBJECT_BASE_FLAGS);
682 }
683
684 void DepsgraphNodeBuilder::build_object_data(
685         Object *object, bool is_object_visible)
686 {
687         if (object->data == NULL) {
688                 return;
689         }
690         /* type-specific data. */
691         switch (object->type) {
692                 case OB_MESH:
693                 case OB_CURVE:
694                 case OB_FONT:
695                 case OB_SURF:
696                 case OB_MBALL:
697                 case OB_LATTICE:
698                 case OB_GPENCIL:
699                         build_object_data_geometry(object, is_object_visible);
700                         break;
701                 case OB_ARMATURE:
702                         if (ID_IS_LINKED(object) && object->proxy_from != NULL) {
703                                 build_proxy_rig(object);
704                         }
705                         else {
706                                 build_rig(object, is_object_visible);
707                         }
708                         break;
709                 case OB_LAMP:
710                         build_object_data_lamp(object);
711                         break;
712                 case OB_CAMERA:
713                         build_object_data_camera(object);
714                         break;
715                 case OB_LIGHTPROBE:
716                         build_object_data_lightprobe(object);
717                         break;
718                 case OB_SPEAKER:
719                         build_object_data_speaker(object);
720                         break;
721                 default:
722                 {
723                         ID *obdata = (ID *)object->data;
724                         if (built_map_.checkIsBuilt(obdata) == 0) {
725                                 build_animdata(obdata);
726                         }
727                         break;
728                 }
729         }
730 }
731
732 void DepsgraphNodeBuilder::build_object_data_camera(Object *object)
733 {
734         Camera *camera = (Camera *)object->data;
735         build_camera(camera);
736 }
737
738 void DepsgraphNodeBuilder::build_object_data_lamp(Object *object)
739 {
740         Lamp *lamp = (Lamp *)object->data;
741         build_lamp(lamp);
742 }
743
744 void DepsgraphNodeBuilder::build_object_data_lightprobe(Object *object)
745 {
746         LightProbe *probe = (LightProbe *)object->data;
747         build_lightprobe(probe);
748         add_operation_node(&object->id,
749                            NodeType::PARAMETERS,
750                            NULL,
751                            OperationCode::LIGHT_PROBE_EVAL);
752 }
753
754 void DepsgraphNodeBuilder::build_object_data_speaker(Object *object)
755 {
756         Speaker *speaker = (Speaker *)object->data;
757         build_speaker(speaker);
758         add_operation_node(&object->id,
759                            NodeType::PARAMETERS,
760                            NULL,
761                            OperationCode::SPEAKER_EVAL);
762 }
763
764 void DepsgraphNodeBuilder::build_object_transform(Object *object)
765 {
766         OperationNode *op_node;
767         Object *ob_cow = get_cow_datablock(object);
768
769         /* local transforms (from transform channels - loc/rot/scale + deltas) */
770         op_node = add_operation_node(&object->id, NodeType::TRANSFORM,
771                                      function_bind(BKE_object_eval_local_transform,
772                                                    _1,
773                                                    ob_cow),
774                                      OperationCode::TRANSFORM_LOCAL);
775         op_node->set_as_entry();
776
777         /* object parent */
778         if (object->parent != NULL) {
779                 add_operation_node(&object->id, NodeType::TRANSFORM,
780                                    function_bind(BKE_object_eval_parent,
781                                                  _1,
782                                                  ob_cow),
783                                    OperationCode::TRANSFORM_PARENT);
784         }
785
786         /* object constraints */
787         if (object->constraints.first != NULL) {
788                 build_object_constraints(object);
789         }
790
791         /* Rest of transformation update. */
792         add_operation_node(&object->id, NodeType::TRANSFORM,
793                            function_bind(BKE_object_eval_uber_transform,
794                                          _1,
795                                          ob_cow),
796                            OperationCode::TRANSFORM_OBJECT_UBEREVAL);
797
798         /* object transform is done */
799         op_node = add_operation_node(&object->id, NodeType::TRANSFORM,
800                                      function_bind(BKE_object_eval_transform_final,
801                                                    _1,
802                                                    ob_cow),
803                                      OperationCode::TRANSFORM_FINAL);
804         op_node->set_as_exit();
805 }
806
807 /**
808  * Constraints Graph Notes
809  *
810  * For constraints, we currently only add a operation node to the Transform
811  * or Bone components (depending on whichever type of owner we have).
812  * This represents the entire constraints stack, which is for now just
813  * executed as a single monolithic block. At least initially, this should
814  * be sufficient for ensuring that the porting/refactoring process remains
815  * manageable.
816  *
817  * However, when the time comes for developing "node-based" constraints,
818  * we'll need to split this up into pre/post nodes for "constraint stack
819  * evaluation" + operation nodes for each constraint (i.e. the contents
820  * of the loop body used in the current "solve_constraints()" operation).
821  *
822  * -- Aligorith, August 2013
823  */
824 void DepsgraphNodeBuilder::build_object_constraints(Object *object)
825 {
826         /* create node for constraint stack */
827         add_operation_node(&object->id, NodeType::TRANSFORM,
828                            function_bind(BKE_object_eval_constraints,
829                                          _1,
830                                          get_cow_datablock(scene_),
831                                          get_cow_datablock(object)),
832                            OperationCode::TRANSFORM_CONSTRAINTS);
833 }
834
835 void DepsgraphNodeBuilder::build_object_pointcache(Object *object)
836 {
837         if (!BKE_ptcache_object_has(scene_, object, 0)) {
838                 return;
839         }
840         Scene *scene_cow = get_cow_datablock(scene_);
841         Object *object_cow = get_cow_datablock(object);
842         add_operation_node(&object->id,
843                            NodeType::POINT_CACHE,
844                            function_bind(BKE_object_eval_ptcache_reset,
845                                          _1,
846                                          scene_cow,
847                                          object_cow),
848                            OperationCode::POINT_CACHE_RESET);
849 }
850
851 /**
852  * Build graph nodes for AnimData block
853  * \param id: ID-Block which hosts the AnimData
854  */
855 void DepsgraphNodeBuilder::build_animdata(ID *id)
856 {
857         AnimData *adt = BKE_animdata_from_id(id);
858         if (adt == NULL) {
859                 return;
860         }
861         if (adt->action != NULL) {
862                 build_action(adt->action);
863         }
864         /* animation */
865         if (adt->action || adt->nla_tracks.first || adt->drivers.first) {
866                 (void) add_id_node(id);
867                 ID *id_cow = get_cow_id(id);
868
869                 // XXX: Hook up specific update callbacks for special properties which
870                 // may need it...
871
872                 /* actions and NLA - as a single unit for now, as it gets complicated to
873                  * schedule otherwise.  */
874                 if ((adt->action) || (adt->nla_tracks.first)) {
875                         /* create the node */
876                         add_operation_node(id, NodeType::ANIMATION,
877                                            function_bind(BKE_animsys_eval_animdata,
878                                                          _1,
879                                                          id_cow),
880                                            OperationCode::ANIMATION,
881                                            id->name);
882
883                         /* TODO: for each channel affected, we might also want to add some
884                          * support for running RNA update callbacks on them
885                          * (which will be needed for proper handling of drivers later) */
886                 }
887
888                 /* NLA strips contain actions */
889                 LISTBASE_FOREACH (NlaTrack *, nlt, &adt->nla_tracks) {
890                         build_animdata_nlastrip_targets(&nlt->strips);
891                 }
892
893                 /* drivers */
894                 int driver_index = 0;
895                 LISTBASE_FOREACH (FCurve *, fcu, &adt->drivers) {
896                         /* create driver */
897                         build_driver(id, fcu, driver_index++);
898                 }
899         }
900 }
901
902 void DepsgraphNodeBuilder::build_animdata_nlastrip_targets(ListBase *strips)
903 {
904         LISTBASE_FOREACH (NlaStrip *, strip, strips) {
905                 if (strip->act != NULL) {
906                         build_action(strip->act);
907                 }
908                 else if (strip->strips.first != NULL) {
909                         build_animdata_nlastrip_targets(&strip->strips);
910                 }
911         }
912 }
913
914 void DepsgraphNodeBuilder::build_action(bAction *action)
915 {
916         if (built_map_.checkIsBuiltAndTag(action)) {
917                 return;
918         }
919         add_operation_node(&action->id,
920                            NodeType::ANIMATION,
921                            NULL,
922                            OperationCode::ANIMATION);
923 }
924
925 /**
926  * Build graph node(s) for Driver
927  * \param id: ID-Block that driver is attached to
928  * \param fcu: Driver-FCurve
929  * \param driver_index: Index in animation data drivers list
930  */
931 void DepsgraphNodeBuilder::build_driver(ID *id, FCurve *fcurve, int driver_index)
932 {
933         /* Create data node for this driver */
934         ID *id_cow = get_cow_id(id);
935         ChannelDriver *driver_orig = fcurve->driver;
936
937         /* TODO(sergey): ideally we could pass the COW of fcu, but since it
938          * has not yet been allocated at this point we can't. As a workaround
939          * the animation systems allocates an array so we can do a fast lookup
940          * with the driver index. */
941         ensure_operation_node(id,
942                               NodeType::PARAMETERS,
943                               function_bind(BKE_animsys_eval_driver, _1, id_cow, driver_index, driver_orig),
944                               OperationCode::DRIVER,
945                               fcurve->rna_path ? fcurve->rna_path : "",
946                               fcurve->array_index);
947         build_driver_variables(id, fcurve);
948 }
949
950 void DepsgraphNodeBuilder::build_driver_variables(ID * id, FCurve *fcurve)
951 {
952         build_driver_id_property(id, fcurve->rna_path);
953         LISTBASE_FOREACH (DriverVar *, dvar, &fcurve->driver->variables) {
954                 DRIVER_TARGETS_USED_LOOPER_BEGIN(dvar)
955                 {
956                         if (dtar->id == NULL) {
957                                 continue;
958                         }
959                         build_id(dtar->id);
960                         build_driver_id_property(dtar->id, dtar->rna_path);
961                         /* Corresponds to dtar_id_ensure_proxy_from(). */
962                         if ((GS(dtar->id->name) == ID_OB) &&
963                             (((Object *)dtar->id)->proxy_from != NULL))
964                         {
965                                 Object *proxy_from = ((Object *)dtar->id)->proxy_from;
966                                 build_id(&proxy_from->id);
967                                 build_driver_id_property(&proxy_from->id, dtar->rna_path);
968                         }
969                 }
970                 DRIVER_TARGETS_LOOPER_END;
971         }
972 }
973
974 void DepsgraphNodeBuilder::build_driver_id_property(ID *id,
975                                                     const char *rna_path)
976 {
977         if (id == NULL || rna_path == NULL) {
978                 return;
979         }
980         PointerRNA id_ptr, ptr;
981         PropertyRNA *prop;
982         RNA_id_pointer_create(id, &id_ptr);
983         if (!RNA_path_resolve_full(&id_ptr, rna_path, &ptr, &prop, NULL)) {
984                 return;
985         }
986         if (prop == NULL) {
987                 return;
988         }
989         if (!RNA_property_is_idprop(prop)) {
990                 return;
991         }
992         const char *prop_identifier = RNA_property_identifier((PropertyRNA *)prop);
993         ensure_operation_node(id,
994                               NodeType::PARAMETERS,
995                               NULL,
996                               OperationCode::ID_PROPERTY,
997                               prop_identifier);
998 }
999
1000 /* Recursively build graph for world */
1001 void DepsgraphNodeBuilder::build_world(World *world)
1002 {
1003         if (built_map_.checkIsBuiltAndTag(world)) {
1004                 return;
1005         }
1006         /* World itself. */
1007         add_id_node(&world->id);
1008         World *world_cow = get_cow_datablock(world);
1009         /* Shading update. */
1010         add_operation_node(&world->id,
1011                            NodeType::SHADING,
1012                            function_bind(BKE_world_eval,
1013                                          _1,
1014                                          world_cow),
1015                            OperationCode::WORLD_UPDATE);
1016         /* Animation. */
1017         build_animdata(&world->id);
1018         /* World's nodetree. */
1019         build_nodetree(world->nodetree);
1020 }
1021
1022 /* Rigidbody Simulation - Scene Level */
1023 void DepsgraphNodeBuilder::build_rigidbody(Scene *scene)
1024 {
1025         RigidBodyWorld *rbw = scene->rigidbody_world;
1026         Scene *scene_cow = get_cow_datablock(scene);
1027
1028         /**
1029          * Rigidbody Simulation Nodes
1030          * ==========================
1031          *
1032          * There are 3 nodes related to Rigidbody Simulation:
1033          * 1) "Initialize/Rebuild World" - this is called sparingly, only when the
1034          *    simulation needs to be rebuilt (mainly after file reload, or moving
1035          *    back to start frame)
1036          * 2) "Do Simulation" - perform a simulation step - interleaved between the
1037          *    evaluation steps for clusters of objects (i.e. between those affected
1038          *    and/or not affected by the sim for instance).
1039          *
1040          * 3) "Pull Results" - grab the specific transforms applied for a specific
1041          *    object - performed as part of object's transform-stack building. */
1042
1043         /* Create nodes --------------------------------------------------------- */
1044
1045         /* XXX: is this the right component, or do we want to use another one
1046          * instead? */
1047
1048         /* Init/rebuild operation. */
1049         add_operation_node(&scene->id, NodeType::TRANSFORM,
1050                           function_bind(BKE_rigidbody_rebuild_sim, _1, scene_cow),
1051                           OperationCode::RIGIDBODY_REBUILD);
1052         /* Do-sim operation. */
1053         OperationNode *sim_node = add_operation_node(
1054                 &scene->id, NodeType::TRANSFORM,
1055                 function_bind(BKE_rigidbody_eval_simulation, _1, scene_cow),
1056                 OperationCode::RIGIDBODY_SIM);
1057         sim_node->set_as_entry();
1058         sim_node->set_as_exit();
1059         sim_node->owner->entry_operation = sim_node;
1060         /* Objects - simulation participants. */
1061         if (rbw->group  != NULL) {
1062                 build_collection(NULL, rbw->group);
1063                 FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(rbw->group, object)
1064                 {
1065                         if (object->type != OB_MESH) {
1066                                 continue;
1067                         }
1068                         /* 2) create operation for flushing results */
1069                         /* object's transform component - where the rigidbody operation
1070                          * lives. */
1071                         add_operation_node(&object->id, NodeType::TRANSFORM,
1072                                            function_bind(
1073                                                    BKE_rigidbody_object_sync_transforms,
1074                                                    _1,
1075                                                    scene_cow,
1076                                                    get_cow_datablock(object)),
1077                                            OperationCode::RIGIDBODY_TRANSFORM_COPY);
1078                 }
1079                 FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
1080         }
1081         /* Constraints. */
1082         if (rbw->constraints != NULL) {
1083                 FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(rbw->constraints, object)
1084                 {
1085                         RigidBodyCon *rbc = object->rigidbody_constraint;
1086                         if (rbc == NULL || rbc->ob1 == NULL || rbc->ob2 == NULL) {
1087                                 /* When either ob1 or ob2 is NULL, the constraint doesn't work. */
1088                                 continue;
1089                         }
1090                         /* Make sure indirectly linked objects are fully built. */
1091                         build_object(-1, object, DEG_ID_LINKED_INDIRECTLY, false);
1092                         build_object(-1, rbc->ob1, DEG_ID_LINKED_INDIRECTLY, false);
1093                         build_object(-1, rbc->ob2, DEG_ID_LINKED_INDIRECTLY, false);
1094                 }
1095                 FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
1096         }
1097 }
1098
1099 void DepsgraphNodeBuilder::build_particle_systems(Object *object,
1100                                                   bool is_object_visible)
1101 {
1102         /**
1103          * Particle Systems Nodes
1104          * ======================
1105          *
1106          * There are two types of nodes associated with representing
1107          * particle systems:
1108          *  1) Component (EVAL_PARTICLES) - This is the particle-system
1109          *     evaluation context for an object. It acts as the container
1110          *     for all the nodes associated with a particular set of particle
1111          *     systems.
1112          *  2) Particle System Eval Operation - This operation node acts as a
1113          *     blackbox evaluation step for one particle system referenced by
1114          *     the particle systems stack. All dependencies link to this operation. */
1115         /* Component for all particle systems. */
1116         ComponentNode *psys_comp =
1117                 add_component_node(&object->id, NodeType::PARTICLE_SYSTEM);
1118
1119         Object *ob_cow = get_cow_datablock(object);
1120         OperationNode *op_node;
1121         op_node = add_operation_node(psys_comp,
1122                                      function_bind(BKE_particle_system_eval_init,
1123                                                    _1,
1124                                                    ob_cow),
1125                                      OperationCode::PARTICLE_SYSTEM_INIT);
1126         op_node->set_as_entry();
1127         /* Build all particle systems. */
1128         LISTBASE_FOREACH (ParticleSystem *, psys, &object->particlesystem) {
1129                 ParticleSettings *part = psys->part;
1130                 /* Build particle settings operations.
1131                  *
1132                  * NOTE: The call itself ensures settings are only build once.  */
1133                 build_particle_settings(part);
1134                 /* Particle system evaluation. */
1135                 add_operation_node(psys_comp,
1136                                    NULL,
1137                                    OperationCode::PARTICLE_SYSTEM_EVAL,
1138                                    psys->name);
1139                 /* Keyed particle targets. */
1140                 if (part->phystype == PART_PHYS_KEYED) {
1141                         LISTBASE_FOREACH (ParticleTarget *, particle_target, &psys->targets) {
1142                                 if (particle_target->ob == NULL ||
1143                                     particle_target->ob == object)
1144                                 {
1145                                         continue;
1146                                 }
1147                                 build_object(-1,
1148                                              particle_target->ob,
1149                                              DEG_ID_LINKED_INDIRECTLY,
1150                                              is_object_visible);
1151                         }
1152                 }
1153                 /* Visualization of particle system. */
1154                 switch (part->ren_as) {
1155                         case PART_DRAW_OB:
1156                                 if (part->dup_ob != NULL) {
1157                                         build_object(-1,
1158                                                      part->dup_ob,
1159                                                      DEG_ID_LINKED_INDIRECTLY,
1160                                                      is_object_visible);
1161                                 }
1162                                 break;
1163                         case PART_DRAW_GR:
1164                                 if (part->dup_group != NULL) {
1165                                         build_collection(NULL, part->dup_group);
1166                                 }
1167                                 break;
1168                 }
1169         }
1170         op_node = add_operation_node(psys_comp,
1171                                      NULL,
1172                                      OperationCode::PARTICLE_SYSTEM_DONE);
1173         op_node->set_as_exit();
1174 }
1175
1176 void DepsgraphNodeBuilder::build_particle_settings(
1177         ParticleSettings *particle_settings) {
1178         if (built_map_.checkIsBuiltAndTag(particle_settings)) {
1179                 return;
1180         }
1181         /* Make sure we've got proper copied ID pointer. */
1182         add_id_node(&particle_settings->id);
1183         ParticleSettings *particle_settings_cow =
1184                 get_cow_datablock(particle_settings);
1185         /* Animation data. */
1186         build_animdata(&particle_settings->id);
1187         /* Parameters change. */
1188         OperationNode *op_node;
1189         op_node = add_operation_node(&particle_settings->id,
1190                                      NodeType::PARTICLE_SETTINGS,
1191                                      NULL,
1192                                      OperationCode::PARTICLE_SETTINGS_INIT);
1193         op_node->set_as_entry();
1194         add_operation_node(&particle_settings->id,
1195                            NodeType::PARTICLE_SETTINGS,
1196                            function_bind(BKE_particle_settings_eval_reset,
1197                                          _1,
1198                                          particle_settings_cow),
1199                            OperationCode::PARTICLE_SETTINGS_RESET);
1200         op_node = add_operation_node(&particle_settings->id,
1201                                      NodeType::PARTICLE_SETTINGS,
1202                                      NULL,
1203                                      OperationCode::PARTICLE_SETTINGS_EVAL);
1204         op_node->set_as_exit();
1205         /* Texture slots. */
1206         for (int mtex_index = 0; mtex_index < MAX_MTEX; ++mtex_index) {
1207                 MTex *mtex = particle_settings->mtex[mtex_index];
1208                 if (mtex == NULL || mtex->tex == NULL) {
1209                         continue;
1210                 }
1211                 build_texture(mtex->tex);
1212         }
1213 }
1214
1215 /* Shapekeys */
1216 void DepsgraphNodeBuilder::build_shapekeys(Key *key)
1217 {
1218         if (built_map_.checkIsBuiltAndTag(key)) {
1219                 return;
1220         }
1221         build_animdata(&key->id);
1222         /* This is an exit operation for the entire key datablock, is what is used
1223          * as dependency for modifiers evaluation. */
1224         add_operation_node(&key->id,
1225                            NodeType::GEOMETRY,
1226                            NULL,
1227                            OperationCode::GEOMETRY_SHAPEKEY);
1228         /* Create per-key block properties, allowing tricky inter-dependnecies for
1229          * drivers evaluation. */
1230         LISTBASE_FOREACH (KeyBlock *, key_block, &key->block) {
1231                 add_operation_node(&key->id,
1232                                    NodeType::PARAMETERS,
1233                                    NULL,
1234                                    OperationCode::PARAMETERS_EVAL,
1235                                    key_block->name);
1236         }
1237 }
1238
1239 /* ObData Geometry Evaluation */
1240 // XXX: what happens if the datablock is shared!
1241 void DepsgraphNodeBuilder::build_object_data_geometry(
1242         Object *object,
1243         bool is_object_visible)
1244 {
1245         OperationNode *op_node;
1246         Scene *scene_cow = get_cow_datablock(scene_);
1247         Object *object_cow = get_cow_datablock(object);
1248         /* Entry operation, takes care of initialization, and some other
1249          * relations which needs to be run prior actual geometry evaluation. */
1250         op_node = add_operation_node(&object->id,
1251                                      NodeType::GEOMETRY,
1252                                      NULL,
1253                                      OperationCode::GEOMETRY_EVAL_INIT);
1254         op_node->set_as_entry();
1255         /* Geometry evaluation. */
1256         op_node = add_operation_node(&object->id,
1257                                      NodeType::GEOMETRY,
1258                                      function_bind(BKE_object_eval_uber_data,
1259                                                    _1,
1260                                                    scene_cow,
1261                                                    object_cow),
1262                                      OperationCode::GEOMETRY_EVAL);
1263         op_node->set_as_exit();
1264         /* Materials. */
1265         if (object->totcol != 0) {
1266                 if (object->type == OB_MESH) {
1267                         add_operation_node(&object->id,
1268                                            NodeType::SHADING,
1269                                            function_bind(BKE_object_eval_update_shading,
1270                                                          _1,
1271                                                          object_cow),
1272                                            OperationCode::SHADING);
1273                 }
1274                 for (int a = 1; a <= object->totcol; a++) {
1275                         Material *ma = give_current_material(object, a);
1276                         if (ma != NULL) {
1277                                 build_material(ma);
1278                         }
1279                 }
1280         }
1281         /* Point caches. */
1282         build_object_pointcache(object);
1283         /* Geometry. */
1284         build_object_data_geometry_datablock((ID *)object->data, is_object_visible);
1285 }
1286
1287 void DepsgraphNodeBuilder::build_object_data_geometry_datablock(
1288         ID *obdata,
1289         bool is_object_visible)
1290 {
1291         if (built_map_.checkIsBuiltAndTag(obdata)) {
1292                 return;
1293         }
1294         OperationNode *op_node;
1295         /* Make sure we've got an ID node before requesting CoW pointer. */
1296         (void) add_id_node((ID *)obdata);
1297         ID *obdata_cow = get_cow_id(obdata);
1298         /* Animation. */
1299         build_animdata(obdata);
1300         /* ShapeKeys */
1301         Key *key = BKE_key_from_id(obdata);
1302         if (key) {
1303                 build_shapekeys(key);
1304         }
1305         /* Nodes for result of obdata's evaluation, and geometry
1306          * evaluation on object. */
1307         const ID_Type id_type = GS(obdata->name);
1308         switch (id_type) {
1309                 case ID_ME:
1310                 {
1311                         op_node = add_operation_node(obdata,
1312                                                      NodeType::GEOMETRY,
1313                                                      function_bind(BKE_mesh_eval_geometry,
1314                                                                    _1,
1315                                                                    (Mesh *)obdata_cow),
1316                                                      OperationCode::GEOMETRY_EVAL);
1317                         op_node->set_as_entry();
1318                         break;
1319                 }
1320                 case ID_MB:
1321                 {
1322                         op_node = add_operation_node(obdata,
1323                                                      NodeType::GEOMETRY,
1324                                                      NULL,
1325                                                      OperationCode::GEOMETRY_EVAL);
1326                         op_node->set_as_entry();
1327                         break;
1328                 }
1329                 case ID_CU:
1330                 {
1331                         op_node = add_operation_node(obdata,
1332                                                      NodeType::GEOMETRY,
1333                                                      function_bind(BKE_curve_eval_geometry,
1334                                                                    _1,
1335                                                                    (Curve *)obdata_cow),
1336                                                                    OperationCode::GEOMETRY_EVAL);
1337                         op_node->set_as_entry();
1338                         /* Make sure objects used for bevel.taper are in the graph.
1339                          * NOTE: This objects might be not linked to the scene. */
1340                         Curve *cu = (Curve *)obdata;
1341                         if (cu->bevobj != NULL) {
1342                                 build_object(-1,
1343                                              cu->bevobj,
1344                                              DEG_ID_LINKED_INDIRECTLY,
1345                                              is_object_visible);
1346                         }
1347                         if (cu->taperobj != NULL) {
1348                                 build_object(-1,
1349                                              cu->taperobj,
1350                                              DEG_ID_LINKED_INDIRECTLY,
1351                                              is_object_visible);
1352                         }
1353                         if (cu->textoncurve != NULL) {
1354                                 build_object(-1,
1355                                              cu->textoncurve,
1356                                              DEG_ID_LINKED_INDIRECTLY,
1357                                              is_object_visible);
1358                         }
1359                         break;
1360                 }
1361                 case ID_LT:
1362                 {
1363                         op_node = add_operation_node(obdata,
1364                                                      NodeType::GEOMETRY,
1365                                                      function_bind(BKE_lattice_eval_geometry,
1366                                                                    _1,
1367                                                                    (Lattice *)obdata_cow),
1368                                                                    OperationCode::GEOMETRY_EVAL);
1369                         op_node->set_as_entry();
1370                         break;
1371                 }
1372
1373                 case ID_GD:
1374                 {
1375                         /* GPencil evaluation operations. */
1376                         op_node = add_operation_node(obdata,
1377                                                      NodeType::GEOMETRY,
1378                                                      function_bind(BKE_gpencil_eval_geometry,
1379                                                                    _1,
1380                                                                    (bGPdata *)obdata_cow),
1381                                                      OperationCode::GEOMETRY_EVAL);
1382                         op_node->set_as_entry();
1383                         break;
1384                 }
1385                 default:
1386                         BLI_assert(!"Should not happen");
1387                         break;
1388         }
1389         op_node = add_operation_node(obdata,
1390                                      NodeType::GEOMETRY,
1391                                      NULL,
1392                                      OperationCode::GEOMETRY_EVAL_DONE);
1393         op_node->set_as_exit();
1394         /* Parameters for driver sources. */
1395         add_operation_node(obdata,
1396                            NodeType::PARAMETERS,
1397                            NULL,
1398                            OperationCode::PARAMETERS_EVAL);
1399         /* Batch cache. */
1400         add_operation_node(obdata,
1401                            NodeType::BATCH_CACHE,
1402                            function_bind(BKE_object_data_select_update,
1403                                          _1,
1404                                          obdata_cow),
1405                            OperationCode::GEOMETRY_SELECT_UPDATE);
1406 }
1407
1408 void DepsgraphNodeBuilder::build_armature(bArmature *armature)
1409 {
1410         if (built_map_.checkIsBuiltAndTag(armature)) {
1411                 return;
1412         }
1413         build_animdata(&armature->id);
1414         /* Make sure pose is up-to-date with armature updates. */
1415         add_operation_node(&armature->id,
1416                            NodeType::PARAMETERS,
1417                            NULL,
1418                            OperationCode::PLACEHOLDER,
1419                            "Armature Eval");
1420 }
1421
1422 void DepsgraphNodeBuilder::build_camera(Camera *camera)
1423 {
1424         if (built_map_.checkIsBuiltAndTag(camera)) {
1425                 return;
1426         }
1427         OperationNode *op_node;
1428         build_animdata(&camera->id);
1429         op_node = add_operation_node(&camera->id,
1430                                      NodeType::PARAMETERS,
1431                                      NULL,
1432                                      OperationCode::PARAMETERS_EVAL);
1433         op_node->set_as_exit();
1434 }
1435
1436 void DepsgraphNodeBuilder::build_lamp(Lamp *lamp)
1437 {
1438         if (built_map_.checkIsBuiltAndTag(lamp)) {
1439                 return;
1440         }
1441         OperationNode *op_node;
1442         build_animdata(&lamp->id);
1443         op_node = add_operation_node(&lamp->id,
1444                                      NodeType::PARAMETERS,
1445                                      NULL,
1446                                      OperationCode::PARAMETERS_EVAL);
1447         /* NOTE: We mark this node as both entry and exit. This way we have a
1448          * node to link all dependencies for shading (which includes relation to the
1449          * lamp object, and incldues relation from node tree) without adding a
1450          * dedicated component type. */
1451         op_node->set_as_entry();
1452         op_node->set_as_exit();
1453         /* lamp's nodetree */
1454         build_nodetree(lamp->nodetree);
1455 }
1456
1457 void DepsgraphNodeBuilder::build_nodetree(bNodeTree *ntree)
1458 {
1459         if (ntree == NULL) {
1460                 return;
1461         }
1462         if (built_map_.checkIsBuiltAndTag(ntree)) {
1463                 return;
1464         }
1465         /* nodetree itself */
1466         add_id_node(&ntree->id);
1467         bNodeTree *ntree_cow = get_cow_datablock(ntree);
1468         /* Animation, */
1469         build_animdata(&ntree->id);
1470         /* Shading update. */
1471         add_operation_node(&ntree->id,
1472                            NodeType::SHADING,
1473                            NULL,
1474                            OperationCode::MATERIAL_UPDATE);
1475         /* NOTE: We really pass original and CoW node trees here, this is how the
1476          * callback works. Ideally we need to find a better way for that. */
1477         add_operation_node(&ntree->id,
1478                            NodeType::SHADING_PARAMETERS,
1479                            function_bind(BKE_nodetree_shading_params_eval,
1480                                          _1,
1481                                          ntree_cow,
1482                                          ntree),
1483                            OperationCode::MATERIAL_UPDATE);
1484         /* nodetree's nodes... */
1485         LISTBASE_FOREACH (bNode *, bnode, &ntree->nodes) {
1486                 ID *id = bnode->id;
1487                 if (id == NULL) {
1488                         continue;
1489                 }
1490                 ID_Type id_type = GS(id->name);
1491                 if (id_type == ID_MA) {
1492                         build_material((Material *)id);
1493                 }
1494                 else if (id_type == ID_TE) {
1495                         build_texture((Tex *)id);
1496                 }
1497                 else if (id_type == ID_IM) {
1498                         build_image((Image *)id);
1499                 }
1500                 else if (id_type == ID_OB) {
1501                         /* TODO(sergey): Use visibility of owner of the node tree. */
1502                         build_object(-1, (Object *)id, DEG_ID_LINKED_INDIRECTLY, true);
1503                 }
1504                 else if (id_type == ID_SCE) {
1505                         /* Scenes are used by compositor trees, and handled by render
1506                          * pipeline. No need to build dependencies for them here. */
1507                 }
1508                 else if (id_type == ID_TXT) {
1509                         /* Ignore script nodes. */
1510                 }
1511                 else if (id_type == ID_MSK) {
1512                         build_mask((Mask *)id);
1513                 }
1514                 else if (id_type == ID_MC) {
1515                         build_movieclip((MovieClip *)id);
1516                 }
1517                 else if (bnode->type == NODE_GROUP) {
1518                         bNodeTree *group_ntree = (bNodeTree *)id;
1519                         build_nodetree(group_ntree);
1520                 }
1521                 else {
1522                         BLI_assert(!"Unknown ID type used for node");
1523                 }
1524         }
1525
1526         // TODO: link from nodetree to owner_component?
1527 }
1528
1529 /* Recursively build graph for material */
1530 void DepsgraphNodeBuilder::build_material(Material *material)
1531 {
1532         if (built_map_.checkIsBuiltAndTag(material)) {
1533                 return;
1534         }
1535         /* Material itself. */
1536         add_id_node(&material->id);
1537         Material *material_cow = get_cow_datablock(material);
1538         /* Shading update. */
1539         add_operation_node(&material->id,
1540                            NodeType::SHADING,
1541                            function_bind(BKE_material_eval,
1542                                          _1,
1543                                          material_cow),
1544                            OperationCode::MATERIAL_UPDATE);
1545         /* Material animation. */
1546         build_animdata(&material->id);
1547         /* Material's nodetree. */
1548         build_nodetree(material->nodetree);
1549 }
1550
1551 /* Recursively build graph for texture */
1552 void DepsgraphNodeBuilder::build_texture(Tex *texture)
1553 {
1554         if (built_map_.checkIsBuiltAndTag(texture)) {
1555                 return;
1556         }
1557         /* Texture itself. */
1558         build_animdata(&texture->id);
1559         /* Texture's nodetree. */
1560         build_nodetree(texture->nodetree);
1561         /* Special cases for different IDs which texture uses. */
1562         if (texture->type == TEX_IMAGE) {
1563                 if (texture->ima != NULL) {
1564                         build_image(texture->ima);
1565                 }
1566         }
1567         add_operation_node(&texture->id,
1568                            NodeType::GENERIC_DATABLOCK,
1569                            NULL,
1570                            OperationCode::GENERIC_DATABLOCK_UPDATE);
1571 }
1572
1573 void DepsgraphNodeBuilder::build_image(Image *image) {
1574         if (built_map_.checkIsBuiltAndTag(image)) {
1575                 return;
1576         }
1577         add_operation_node(&image->id,
1578                            NodeType::GENERIC_DATABLOCK,
1579                            NULL,
1580                            OperationCode::GENERIC_DATABLOCK_UPDATE);
1581 }
1582
1583 void DepsgraphNodeBuilder::build_compositor(Scene *scene)
1584 {
1585         /* For now, just a plain wrapper? */
1586         // TODO: create compositing component?
1587         // XXX: component type undefined!
1588         //graph->get_node(&scene->id, NULL, NodeType::COMPOSITING, NULL);
1589
1590         /* for now, nodetrees are just parameters; compositing occurs in internals
1591          * of renderer... */
1592         add_component_node(&scene->id, NodeType::PARAMETERS);
1593         build_nodetree(scene->nodetree);
1594 }
1595
1596 void DepsgraphNodeBuilder::build_gpencil(bGPdata *gpd)
1597 {
1598         if (built_map_.checkIsBuiltAndTag(gpd)) {
1599                 return;
1600         }
1601         ID *gpd_id = &gpd->id;
1602
1603         /* TODO(sergey): what about multiple users of same datablock? This should
1604          * only get added once. */
1605
1606         /* The main reason Grease Pencil is included here is because the animation
1607          * (and drivers) need to be hosted somewhere. */
1608         build_animdata(gpd_id);
1609 }
1610
1611 void DepsgraphNodeBuilder::build_cachefile(CacheFile *cache_file)
1612 {
1613         if (built_map_.checkIsBuiltAndTag(cache_file)) {
1614                 return;
1615         }
1616         ID *cache_file_id = &cache_file->id;
1617         /* Animation, */
1618         build_animdata(cache_file_id);
1619         /* Cache evaluation itself. */
1620         add_operation_node(cache_file_id, NodeType::CACHE, NULL,
1621                            OperationCode::PLACEHOLDER, "Cache File Update");
1622 }
1623
1624 void DepsgraphNodeBuilder::build_mask(Mask *mask)
1625 {
1626         if (built_map_.checkIsBuiltAndTag(mask)) {
1627                 return;
1628         }
1629         ID *mask_id = &mask->id;
1630         Mask *mask_cow = get_cow_datablock(mask);
1631         /* F-Curve based animation. */
1632         build_animdata(mask_id);
1633         /* Animation based on mask's shapes. */
1634         add_operation_node(mask_id,
1635                            NodeType::ANIMATION,
1636                            function_bind(BKE_mask_eval_animation, _1, mask_cow),
1637                            OperationCode::MASK_ANIMATION);
1638         /* Final mask evaluation. */
1639         add_operation_node(mask_id,
1640                            NodeType::PARAMETERS,
1641                            function_bind(BKE_mask_eval_update, _1, mask_cow),
1642                            OperationCode::MASK_EVAL);
1643 }
1644
1645 void DepsgraphNodeBuilder::build_movieclip(MovieClip *clip)
1646 {
1647         if (built_map_.checkIsBuiltAndTag(clip)) {
1648                 return;
1649         }
1650         ID *clip_id = &clip->id;
1651         MovieClip *clip_cow = (MovieClip *)ensure_cow_id(clip_id);
1652         /* Animation. */
1653         build_animdata(clip_id);
1654         /* Movie clip evaluation. */
1655         add_operation_node(clip_id,
1656                            NodeType::PARAMETERS,
1657                            function_bind(BKE_movieclip_eval_update, _1, clip_cow),
1658                            OperationCode::MOVIECLIP_EVAL);
1659
1660         add_operation_node(clip_id,
1661                            NodeType::BATCH_CACHE,
1662                            function_bind(BKE_movieclip_eval_selection_update, _1, clip_cow),
1663                            OperationCode::MOVIECLIP_SELECT_UPDATE);
1664 }
1665
1666 void DepsgraphNodeBuilder::build_lightprobe(LightProbe *probe)
1667 {
1668         if (built_map_.checkIsBuiltAndTag(probe)) {
1669                 return;
1670         }
1671         /* Placeholder so we can add relations and tag ID node for update. */
1672         add_operation_node(&probe->id,
1673                            NodeType::PARAMETERS,
1674                            NULL,
1675                            OperationCode::LIGHT_PROBE_EVAL);
1676
1677         build_animdata(&probe->id);
1678 }
1679
1680 void DepsgraphNodeBuilder::build_speaker(Speaker *speaker)
1681 {
1682         if (built_map_.checkIsBuiltAndTag(speaker)) {
1683                 return;
1684         }
1685         /* Placeholder so we can add relations and tag ID node for update. */
1686         add_operation_node(&speaker->id,
1687                            NodeType::PARAMETERS,
1688                            NULL,
1689                            OperationCode::SPEAKER_EVAL);
1690         build_animdata(&speaker->id);
1691 }
1692
1693 /* **** ID traversal callbacks functions **** */
1694
1695 void DepsgraphNodeBuilder::modifier_walk(void *user_data,
1696                                          struct Object * /*object*/,
1697                                          struct ID **idpoin,
1698                                          int /*cb_flag*/)
1699 {
1700         BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
1701         ID *id = *idpoin;
1702         if (id == NULL) {
1703                 return;
1704         }
1705         switch (GS(id->name)) {
1706                 case ID_OB:
1707                         /* Special case for object, so we take owner visibility into
1708                          * account. */
1709                         data->builder->build_object(-1,
1710                                                     (Object *)id,
1711                                                     DEG_ID_LINKED_INDIRECTLY,
1712                                                     data->is_parent_visible);
1713                         break;
1714                 default:
1715                         data->builder->build_id(id);
1716                         break;
1717         }
1718 }
1719
1720 void DepsgraphNodeBuilder::constraint_walk(bConstraint * /*con*/,
1721                                            ID **idpoin,
1722                                            bool /*is_reference*/,
1723                                            void *user_data)
1724 {
1725         BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
1726         ID *id = *idpoin;
1727         if (id == NULL) {
1728                 return;
1729         }
1730         switch (GS(id->name)) {
1731                 case ID_OB:
1732                         /* Special case for object, so we take owner visibility into
1733                          * account. */
1734                         data->builder->build_object(-1,
1735                                                     (Object *)id,
1736                                                     DEG_ID_LINKED_INDIRECTLY,
1737                                                     data->is_parent_visible);
1738                         break;
1739                 default:
1740                         data->builder->build_id(id);
1741                         break;
1742         }
1743 }
1744
1745 }  // namespace DEG