Fix T57633: Particle texture update problem
[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_collection_types.h"
51 #include "DNA_constraint_types.h"
52 #include "DNA_curve_types.h"
53 #include "DNA_effect_types.h"
54 #include "DNA_gpencil_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_speaker_types.h"
69 #include "DNA_texture_types.h"
70 #include "DNA_world_types.h"
71
72 #include "BKE_action.h"
73 #include "BKE_armature.h"
74 #include "BKE_animsys.h"
75 #include "BKE_collection.h"
76 #include "BKE_constraint.h"
77 #include "BKE_curve.h"
78 #include "BKE_effect.h"
79 #include "BKE_fcurve.h"
80 #include "BKE_gpencil.h"
81 #include "BKE_gpencil_modifier.h"
82 #include "BKE_idcode.h"
83 #include "BKE_key.h"
84 #include "BKE_lattice.h"
85 #include "BKE_mask.h"
86 #include "BKE_material.h"
87 #include "BKE_mesh.h"
88 #include "BKE_mball.h"
89 #include "BKE_modifier.h"
90 #include "BKE_movieclip.h"
91 #include "BKE_node.h"
92 #include "BKE_object.h"
93 #include "BKE_particle.h"
94 #include "BKE_pointcache.h"
95 #include "BKE_rigidbody.h"
96 #include "BKE_shader_fx.h"
97 #include "BKE_sound.h"
98 #include "BKE_tracking.h"
99 #include "BKE_world.h"
100
101 #include "RNA_access.h"
102 #include "RNA_types.h"
103 } /* extern "C" */
104
105 #include "DEG_depsgraph.h"
106 #include "DEG_depsgraph_build.h"
107
108 #include "intern/builder/deg_builder.h"
109 #include "intern/eval/deg_eval_copy_on_write.h"
110 #include "intern/nodes/deg_node.h"
111 #include "intern/nodes/deg_node_component.h"
112 #include "intern/nodes/deg_node_id.h"
113 #include "intern/nodes/deg_node_operation.h"
114 #include "intern/depsgraph_types.h"
115 #include "intern/depsgraph_intern.h"
116
117 #include "util/deg_util_foreach.h"
118
119 namespace DEG {
120
121 namespace {
122
123 void free_copy_on_write_datablock(void *id_info_v)
124 {
125         DepsgraphNodeBuilder::IDInfo *id_info =
126             (DepsgraphNodeBuilder::IDInfo *)id_info_v;
127         if (id_info->id_cow != NULL) {
128                 deg_free_copy_on_write_datablock(id_info->id_cow);
129                 MEM_freeN(id_info->id_cow);
130         }
131         MEM_freeN(id_info);
132 }
133
134 }  /* namespace */
135
136 /* ************ */
137 /* Node Builder */
138
139 /* **** General purpose functions **** */
140
141 DepsgraphNodeBuilder::DepsgraphNodeBuilder(Main *bmain, Depsgraph *graph)
142     : bmain_(bmain),
143       graph_(graph),
144       scene_(NULL),
145       view_layer_(NULL),
146       view_layer_index_(-1),
147       collection_(NULL),
148       is_parent_collection_visible_(true),
149       id_info_hash_(NULL)
150 {
151 }
152
153 DepsgraphNodeBuilder::~DepsgraphNodeBuilder()
154 {
155         if (id_info_hash_ != NULL) {
156                 BLI_ghash_free(id_info_hash_, NULL, free_copy_on_write_datablock);
157         }
158 }
159
160 IDDepsNode *DepsgraphNodeBuilder::add_id_node(ID *id)
161 {
162         IDDepsNode *id_node = NULL;
163         ID *id_cow = NULL;
164         IDComponentsMask previously_visible_components_mask = 0;
165         uint32_t previous_eval_flags = 0;
166         uint64_t previous_customdata_mask = 0;
167         IDInfo *id_info = (IDInfo *)BLI_ghash_lookup(id_info_hash_, id);
168         if (id_info != NULL) {
169                 id_cow = id_info->id_cow;
170                 previously_visible_components_mask =
171                         id_info->previously_visible_components_mask;
172                 previous_eval_flags = id_info->previous_eval_flags;
173                 previous_customdata_mask = id_info->previous_customdata_mask;
174                 /* Tag ID info to not free the CoW ID pointer. */
175                 id_info->id_cow = NULL;
176         }
177         id_node = graph_->add_id_node(id, id_cow);
178         id_node->previously_visible_components_mask =
179                 previously_visible_components_mask;
180         id_node->previous_eval_flags = previous_eval_flags;
181         id_node->previous_customdata_mask = previous_customdata_mask;
182         /* Currently all ID nodes are supposed to have copy-on-write logic.
183          *
184          * NOTE: Zero number of components indicates that ID node was just created.
185          */
186         if (BLI_ghash_len(id_node->components) == 0) {
187                 ComponentDepsNode *comp_cow =
188                         id_node->add_component(DEG_NODE_TYPE_COPY_ON_WRITE);
189                 OperationDepsNode *op_cow = comp_cow->add_operation(
190                         function_bind(deg_evaluate_copy_on_write, _1, id_node),
191                         DEG_OPCODE_COPY_ON_WRITE,
192                         "", -1);
193                 graph_->operations.push_back(op_cow);
194         }
195         return id_node;
196 }
197
198 IDDepsNode *DepsgraphNodeBuilder::find_id_node(ID *id)
199 {
200         return graph_->find_id_node(id);
201 }
202
203 TimeSourceDepsNode *DepsgraphNodeBuilder::add_time_source()
204 {
205         return graph_->add_time_source();
206 }
207
208 ComponentDepsNode *DepsgraphNodeBuilder::add_component_node(
209         ID *id,
210         eDepsNode_Type comp_type,
211         const char *comp_name)
212 {
213         IDDepsNode *id_node = add_id_node(id);
214         ComponentDepsNode *comp_node = id_node->add_component(comp_type, comp_name);
215         comp_node->owner = id_node;
216         return comp_node;
217 }
218
219 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
220         ComponentDepsNode *comp_node,
221         const DepsEvalOperationCb& op,
222         eDepsOperation_Code opcode,
223         const char *name,
224         int name_tag)
225 {
226         OperationDepsNode *op_node = comp_node->find_operation(opcode,
227                                                                name,
228                                                                name_tag);
229         if (op_node == NULL) {
230                 op_node = comp_node->add_operation(op, opcode, name, name_tag);
231                 graph_->operations.push_back(op_node);
232         }
233         else {
234                 fprintf(stderr,
235                         "add_operation: Operation already exists - %s has %s at %p\n",
236                         comp_node->identifier().c_str(),
237                         op_node->identifier().c_str(),
238                         op_node);
239                 BLI_assert(!"Should not happen!");
240         }
241         return op_node;
242 }
243
244 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
245         ID *id,
246         eDepsNode_Type comp_type,
247         const char *comp_name,
248         const DepsEvalOperationCb& op,
249         eDepsOperation_Code opcode,
250         const char *name,
251         int name_tag)
252 {
253         ComponentDepsNode *comp_node = add_component_node(id, comp_type, comp_name);
254         return add_operation_node(comp_node, op, opcode, name, name_tag);
255 }
256
257 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
258         ID *id,
259         eDepsNode_Type comp_type,
260         const DepsEvalOperationCb& op,
261         eDepsOperation_Code opcode,
262         const char *name,
263         int name_tag)
264 {
265         return add_operation_node(id,
266                                   comp_type,
267                                   "",
268                                   op,
269                                   opcode,
270                                   name,
271                                   name_tag);
272 }
273
274 OperationDepsNode *DepsgraphNodeBuilder::ensure_operation_node(
275         ID *id,
276         eDepsNode_Type comp_type,
277         const DepsEvalOperationCb& op,
278         eDepsOperation_Code opcode,
279         const char *name,
280         int name_tag)
281 {
282         OperationDepsNode *operation =
283                 find_operation_node(id, comp_type, opcode, name, name_tag);
284         if (operation != NULL) {
285                 return operation;
286         }
287         return add_operation_node(id, comp_type, op, opcode, name, name_tag);
288 }
289
290 bool DepsgraphNodeBuilder::has_operation_node(ID *id,
291                                               eDepsNode_Type comp_type,
292                                               const char *comp_name,
293                                               eDepsOperation_Code opcode,
294                                               const char *name,
295                                               int name_tag)
296 {
297         return find_operation_node(id,
298                                    comp_type,
299                                    comp_name,
300                                    opcode,
301                                    name,
302                                    name_tag) != NULL;
303 }
304
305 OperationDepsNode *DepsgraphNodeBuilder::find_operation_node(
306         ID *id,
307         eDepsNode_Type comp_type,
308         const char *comp_name,
309         eDepsOperation_Code opcode,
310         const char *name,
311         int name_tag)
312 {
313         ComponentDepsNode *comp_node = add_component_node(id, comp_type, comp_name);
314         return comp_node->find_operation(opcode, name, name_tag);
315 }
316
317 OperationDepsNode *DepsgraphNodeBuilder::find_operation_node(
318         ID *id,
319         eDepsNode_Type comp_type,
320         eDepsOperation_Code opcode,
321         const char *name,
322         int name_tag)
323 {
324         return find_operation_node(id, comp_type, "", opcode, name, name_tag);
325 }
326
327 ID *DepsgraphNodeBuilder::get_cow_id(const ID *id_orig) const
328 {
329         return graph_->get_cow_id(id_orig);
330 }
331
332 ID *DepsgraphNodeBuilder::ensure_cow_id(ID *id_orig)
333 {
334         if (id_orig->tag & LIB_TAG_COPIED_ON_WRITE) {
335                 /* ID is already remapped to copy-on-write. */
336                 return id_orig;
337         }
338         IDDepsNode *id_node = add_id_node(id_orig);
339         return id_node->id_cow;
340 }
341
342 /* **** Build functions for entity nodes **** */
343
344 void DepsgraphNodeBuilder::begin_build()
345 {
346         /* Store existing copy-on-write versions of datablock, so we can re-use
347          * them for new ID nodes.
348          */
349         id_info_hash_ = BLI_ghash_ptr_new("Depsgraph id hash");
350         foreach (IDDepsNode *id_node, graph_->id_nodes) {
351                 IDInfo *id_info = (IDInfo *)MEM_mallocN(
352                         sizeof(IDInfo), "depsgraph id info");
353                 if (deg_copy_on_write_is_expanded(id_node->id_cow) &&
354                     id_node->id_orig != id_node->id_cow)
355                 {
356                         id_info->id_cow = id_node->id_cow;
357                 }
358                 else {
359                         id_info->id_cow = NULL;
360                 }
361                 id_info->previously_visible_components_mask =
362                         id_node->visible_components_mask;
363                 id_info->previous_eval_flags = id_node->eval_flags;
364                 id_info->previous_customdata_mask = id_node->customdata_mask;
365                 BLI_ghash_insert(id_info_hash_, id_node->id_orig, id_info);
366                 id_node->id_cow = NULL;
367         }
368
369         GSET_FOREACH_BEGIN(OperationDepsNode *, op_node, graph_->entry_tags)
370         {
371                 ComponentDepsNode *comp_node = op_node->owner;
372                 IDDepsNode *id_node = comp_node->owner;
373
374                 SavedEntryTag entry_tag;
375                 entry_tag.id_orig = id_node->id_orig;
376                 entry_tag.component_type = comp_node->type;
377                 entry_tag.opcode = op_node->opcode;
378                 entry_tag.name = op_node->name;
379                 entry_tag.name_tag = op_node->name_tag;
380                 saved_entry_tags_.push_back(entry_tag);
381         };
382         GSET_FOREACH_END();
383
384         /* Make sure graph has no nodes left from previous state. */
385         graph_->clear_all_nodes();
386         graph_->operations.clear();
387         BLI_gset_clear(graph_->entry_tags, NULL);
388 }
389
390 void DepsgraphNodeBuilder::end_build()
391 {
392         foreach (const SavedEntryTag& entry_tag, saved_entry_tags_) {
393                 IDDepsNode *id_node = find_id_node(entry_tag.id_orig);
394                 if (id_node == NULL) {
395                         continue;
396                 }
397                 ComponentDepsNode *comp_node =
398                         id_node->find_component(entry_tag.component_type);
399                 if (comp_node == NULL) {
400                         continue;
401                 }
402                 OperationDepsNode *op_node = comp_node->find_operation(entry_tag.opcode, entry_tag.name, entry_tag.name_tag);
403                 if (op_node == NULL) {
404                         continue;
405                 }
406                 /* Since the tag is coming from a saved copy of entry tags, this means
407                  * that originally node was explicitly tagged for user update.
408                  */
409                 op_node->tag_update(graph_, DEG_UPDATE_SOURCE_USER_EDIT);
410         }
411 }
412
413 void DepsgraphNodeBuilder::build_id(ID *id)
414 {
415         if (id == NULL) {
416                 return;
417         }
418         switch (GS(id->name)) {
419                 case ID_AC:
420                         build_action((bAction *)id);
421                         break;
422                 case ID_AR:
423                         build_armature((bArmature *)id);
424                         break;
425                 case ID_CA:
426                         build_camera((Camera *)id);
427                         break;
428                 case ID_GR:
429                         build_collection(NULL, (Collection *)id);
430                         break;
431                 case ID_OB:
432                         /* TODO(sergey): Get visibility from a "parent" somehow.
433                          *
434                          * NOTE: Using `false` visibility here should be fine, since if this
435                          * driver affects on something invisible we don't really care if the
436                          * driver gets evaluated (and even don't want this to force object
437                          * to become visible).
438                          *
439                          * If this happened to be affecting visible object, then it is up to
440                          * deg_graph_build_flush_visibility() to ensure visibility of the
441                          * object is true.
442                          */
443                         build_object(-1, (Object *)id, DEG_ID_LINKED_INDIRECTLY, false);
444                         break;
445                 case ID_KE:
446                         build_shapekeys((Key *)id);
447                         break;
448                 case ID_LA:
449                         build_lamp((Lamp *)id);
450                         break;
451                 case ID_LP:
452                         build_lightprobe((LightProbe *)id);
453                         break;
454                 case ID_NT:
455                         build_nodetree((bNodeTree *)id);
456                         break;
457                 case ID_MA:
458                         build_material((Material *)id);
459                         break;
460                 case ID_TE:
461                         build_texture((Tex *)id);
462                         break;
463                 case ID_IM:
464                         build_image((Image *)id);
465                         break;
466                 case ID_WO:
467                         build_world((World *)id);
468                         break;
469                 case ID_MSK:
470                         build_mask((Mask *)id);
471                         break;
472                 case ID_MC:
473                         build_movieclip((MovieClip *)id);
474                         break;
475                 case ID_ME:
476                 case ID_CU:
477                 case ID_MB:
478                 case ID_LT:
479                         /* TODO(sergey): Get visibility from a "parent" somehow.
480                          *
481                          * NOTE: Similarly to above, we don't want false-positives on
482                          * visibility.
483                          */
484                         build_object_data_geometry_datablock(id, false);
485                         break;
486                 case ID_SPK:
487                         build_speaker((Speaker *)id);
488                         break;
489                 case ID_TXT:
490                         /* Not a part of dependency graph. */
491                         break;
492                 case ID_CF:
493                         build_cachefile((CacheFile *)id);
494                         break;
495                 default:
496                         fprintf(stderr, "Unhandled ID %s\n", id->name);
497                         BLI_assert(!"Should never happen");
498                         break;
499         }
500 }
501
502 void DepsgraphNodeBuilder::build_collection(
503         LayerCollection *from_layer_collection,
504         Collection *collection)
505 {
506         const int restrict_flag = (graph_->mode == DAG_EVAL_VIEWPORT)
507                 ? COLLECTION_RESTRICT_VIEW
508                 : COLLECTION_RESTRICT_RENDER;
509         const bool is_collection_restricted = (collection->flag & restrict_flag);
510         const bool is_collection_visible =
511                 !is_collection_restricted && is_parent_collection_visible_;
512         IDDepsNode *id_node;
513         if (built_map_.checkIsBuiltAndTag(collection)) {
514                 id_node = find_id_node(&collection->id);
515                 if (is_collection_visible &&
516                     id_node->is_directly_visible == false &&
517                     id_node->is_collection_fully_expanded == true)
518                 {
519                         /* Collection became visible, make sure nested collections and
520                          * objects are poked with the new visibility flag, since they
521                          * might become visible too. */
522                 }
523                 else {
524                         return;
525                 }
526         }
527         else {
528                 /* Collection itself. */
529                 id_node = add_id_node(&collection->id);
530                 id_node->is_directly_visible = is_collection_visible;
531         }
532         if (from_layer_collection != NULL) {
533                 /* If we came from layer collection we don't go deeper, view layer
534                  * builder takes care of going deeper. */
535                 return;
536         }
537         /* Backup state. */
538         Collection *current_state_collection = collection_;
539         const bool is_current_parent_collection_visible =
540                 is_parent_collection_visible_;
541         /* Modify state as we've entered new collection/ */
542         collection_ = collection;
543         is_parent_collection_visible_ = is_collection_visible;
544         /* Build collection objects. */
545         LISTBASE_FOREACH (CollectionObject *, cob, &collection->gobject) {
546                 build_object(
547                         -1, cob->ob, DEG_ID_LINKED_INDIRECTLY, is_collection_visible);
548         }
549         /* Build child collections. */
550         LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
551                 build_collection(NULL, child->collection);
552         }
553         /* Restore state. */
554         collection_ = current_state_collection;
555         is_parent_collection_visible_ = is_current_parent_collection_visible;
556         id_node->is_collection_fully_expanded = true;
557 }
558
559 void DepsgraphNodeBuilder::build_object(int base_index,
560                                         Object *object,
561                                         eDepsNode_LinkedState_Type linked_state,
562                                         bool is_visible)
563 {
564         const bool has_object = built_map_.checkIsBuiltAndTag(object);
565         /* Skip rest of components if the ID node was already there. */
566         if (has_object) {
567                 IDDepsNode *id_node = find_id_node(&object->id);
568                 /* We need to build some extra stuff if object becomes linked
569                  * directly.
570                  */
571                 if (id_node->linked_state == DEG_ID_LINKED_INDIRECTLY) {
572                         build_object_flags(base_index, object, linked_state);
573                 }
574                 id_node->linked_state = max(id_node->linked_state, linked_state);
575                 if (id_node->linked_state == DEG_ID_LINKED_DIRECTLY) {
576                         id_node->is_directly_visible |= is_visible;
577                 }
578                 return;
579         }
580         /* Create ID node for object and begin init. */
581         IDDepsNode *id_node = add_id_node(&object->id);
582         Object *object_cow = get_cow_datablock(object);
583         id_node->linked_state = linked_state;
584         if (object == scene_->camera) {
585                 id_node->is_directly_visible = true;
586         }
587         else {
588                 id_node->is_directly_visible = is_visible;
589         }
590         /* Various flags, flushing from bases/collections. */
591         build_object_flags(base_index, object, linked_state);
592         /* Transform. */
593         build_object_transform(object);
594         /* Parent. */
595         if (object->parent != NULL) {
596                 build_object(
597                         -1, object->parent, DEG_ID_LINKED_INDIRECTLY, is_visible);
598         }
599         /* Modifiers. */
600         if (object->modifiers.first != NULL) {
601                 BuilderWalkUserData data;
602                 data.builder = this;
603                 data.is_parent_visible = is_visible;
604                 modifiers_foreachIDLink(object, modifier_walk, &data);
605         }
606         /* Grease Pencil Modifiers. */
607         if (object->greasepencil_modifiers.first != NULL) {
608                 BuilderWalkUserData data;
609                 data.builder = this;
610                 data.is_parent_visible = is_visible;
611                 BKE_gpencil_modifiers_foreachIDLink(object, modifier_walk, &data);
612         }
613         /* Shader FX. */
614         if (object->shader_fx.first != NULL) {
615                 BuilderWalkUserData data;
616                 data.builder = this;
617                 data.is_parent_visible = is_visible;
618                 BKE_shaderfx_foreachIDLink(object, modifier_walk, &data);
619         }
620         /* Constraints. */
621         if (object->constraints.first != NULL) {
622                 BuilderWalkUserData data;
623                 data.builder = this;
624                 data.is_parent_visible = is_visible;
625                 BKE_constraints_id_loop(&object->constraints, constraint_walk, &data);
626         }
627         /* Object data. */
628         build_object_data(object, is_visible);
629         /* Build animation data,
630          *
631          * Do it now because it's possible object data will affect
632          * on object's level animation, for example in case of rebuilding
633          * pose for proxy.
634          */
635         OperationDepsNode *op_node = add_operation_node(&object->id,
636                                                         DEG_NODE_TYPE_PARAMETERS,
637                                                         NULL,
638                                                         DEG_OPCODE_PARAMETERS_EVAL);
639         op_node->set_as_exit();
640         build_animdata(&object->id);
641         /* Particle systems. */
642         if (object->particlesystem.first != NULL) {
643                 build_particle_systems(object, is_visible);
644         }
645         /* Proxy object to copy from. */
646         if (object->proxy_from != NULL) {
647                 build_object(
648                         -1, object->proxy_from, DEG_ID_LINKED_INDIRECTLY, is_visible);
649         }
650         if (object->proxy_group != NULL) {
651                 build_object(
652                         -1, object->proxy_group, DEG_ID_LINKED_INDIRECTLY, is_visible);
653         }
654         /* Object dupligroup. */
655         if (object->dup_group != NULL) {
656                 const bool is_current_parent_collection_visible =
657                         is_parent_collection_visible_;
658                 is_parent_collection_visible_ = is_visible;
659                 build_collection(NULL, object->dup_group);
660                 is_parent_collection_visible_ = is_current_parent_collection_visible;
661                 add_operation_node(&object->id,
662                                    DEG_NODE_TYPE_DUPLI,
663                                    NULL,
664                                    DEG_OPCODE_PLACEHOLDER,
665                                    "Dupli");
666         }
667         /* Syncronization back to original object. */
668         add_operation_node(&object->id,
669                            DEG_NODE_TYPE_SYNCHRONIZE,
670                            function_bind(BKE_object_synchronize_to_original,
671                                          _1,
672                                          object_cow),
673                            DEG_OPCODE_SYNCHRONIZE_TO_ORIGINAL);
674 }
675
676 void DepsgraphNodeBuilder::build_object_flags(
677         int base_index,
678         Object *object,
679         eDepsNode_LinkedState_Type linked_state)
680 {
681         if (base_index == -1) {
682                 return;
683         }
684         Scene *scene_cow = get_cow_datablock(scene_);
685         Object *object_cow = get_cow_datablock(object);
686         const bool is_from_set = (linked_state == DEG_ID_LINKED_VIA_SET);
687         /* TODO(sergey): Is this really best component to be used? */
688         add_operation_node(&object->id,
689                            DEG_NODE_TYPE_OBJECT_FROM_LAYER,
690                            function_bind(BKE_object_eval_flush_base_flags,
691                                          _1,
692                                          scene_cow,
693                                          view_layer_index_,
694                                          object_cow, base_index,
695                                          is_from_set),
696                            DEG_OPCODE_OBJECT_BASE_FLAGS);
697 }
698
699 void DepsgraphNodeBuilder::build_object_data(
700         Object *object, bool is_object_visible)
701 {
702         if (object->data == NULL) {
703                 return;
704         }
705         /* type-specific data. */
706         switch (object->type) {
707                 case OB_MESH:
708                 case OB_CURVE:
709                 case OB_FONT:
710                 case OB_SURF:
711                 case OB_MBALL:
712                 case OB_LATTICE:
713                 case OB_GPENCIL:
714                         build_object_data_geometry(object, is_object_visible);
715                         break;
716                 case OB_ARMATURE:
717                         if (ID_IS_LINKED(object) && object->proxy_from != NULL) {
718                                 build_proxy_rig(object);
719                         }
720                         else {
721                                 build_rig(object, is_object_visible);
722                         }
723                         break;
724                 case OB_LAMP:
725                         build_object_data_lamp(object);
726                         break;
727                 case OB_CAMERA:
728                         build_object_data_camera(object);
729                         break;
730                 case OB_LIGHTPROBE:
731                         build_object_data_lightprobe(object);
732                         break;
733                 case OB_SPEAKER:
734                         build_object_data_speaker(object);
735                         break;
736                 default:
737                 {
738                         ID *obdata = (ID *)object->data;
739                         if (built_map_.checkIsBuilt(obdata) == 0) {
740                                 build_animdata(obdata);
741                         }
742                         break;
743                 }
744         }
745 }
746
747 void DepsgraphNodeBuilder::build_object_data_camera(Object *object)
748 {
749         Camera *camera = (Camera *)object->data;
750         build_camera(camera);
751 }
752
753 void DepsgraphNodeBuilder::build_object_data_lamp(Object *object)
754 {
755         Lamp *lamp = (Lamp *)object->data;
756         build_lamp(lamp);
757 }
758
759 void DepsgraphNodeBuilder::build_object_data_lightprobe(Object *object)
760 {
761         LightProbe *probe = (LightProbe *)object->data;
762         build_lightprobe(probe);
763         add_operation_node(&object->id,
764                            DEG_NODE_TYPE_PARAMETERS,
765                            NULL,
766                            DEG_OPCODE_LIGHT_PROBE_EVAL);
767 }
768
769 void DepsgraphNodeBuilder::build_object_data_speaker(Object *object)
770 {
771         Speaker *speaker = (Speaker *)object->data;
772         build_speaker(speaker);
773         add_operation_node(&object->id,
774                            DEG_NODE_TYPE_PARAMETERS,
775                            NULL,
776                            DEG_OPCODE_SPEAKER_EVAL);
777 }
778
779 void DepsgraphNodeBuilder::build_object_transform(Object *object)
780 {
781         OperationDepsNode *op_node;
782         Scene *scene_cow = get_cow_datablock(scene_);
783         Object *ob_cow = get_cow_datablock(object);
784
785         /* local transforms (from transform channels - loc/rot/scale + deltas) */
786         op_node = add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
787                                      function_bind(BKE_object_eval_local_transform,
788                                                    _1,
789                                                    ob_cow),
790                                      DEG_OPCODE_TRANSFORM_LOCAL);
791         op_node->set_as_entry();
792
793         /* object parent */
794         if (object->parent != NULL) {
795                 add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
796                                    function_bind(BKE_object_eval_parent,
797                                                  _1,
798                                                  scene_cow,
799                                                  ob_cow),
800                                    DEG_OPCODE_TRANSFORM_PARENT);
801         }
802
803         /* object constraints */
804         if (object->constraints.first != NULL) {
805                 build_object_constraints(object);
806         }
807
808         /* Rest of transformation update. */
809         add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
810                            function_bind(BKE_object_eval_uber_transform,
811                                          _1,
812                                          ob_cow),
813                            DEG_OPCODE_TRANSFORM_OBJECT_UBEREVAL);
814
815         /* object transform is done */
816         op_node = add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
817                                      function_bind(BKE_object_eval_transform_final,
818                                                    _1,
819                                                    ob_cow),
820                                      DEG_OPCODE_TRANSFORM_FINAL);
821         op_node->set_as_exit();
822 }
823
824 /**
825  * Constraints Graph Notes
826  *
827  * For constraints, we currently only add a operation node to the Transform
828  * or Bone components (depending on whichever type of owner we have).
829  * This represents the entire constraints stack, which is for now just
830  * executed as a single monolithic block. At least initially, this should
831  * be sufficient for ensuring that the porting/refactoring process remains
832  * manageable.
833  *
834  * However, when the time comes for developing "node-based" constraints,
835  * we'll need to split this up into pre/post nodes for "constraint stack
836  * evaluation" + operation nodes for each constraint (i.e. the contents
837  * of the loop body used in the current "solve_constraints()" operation).
838  *
839  * -- Aligorith, August 2013
840  */
841 void DepsgraphNodeBuilder::build_object_constraints(Object *object)
842 {
843         /* create node for constraint stack */
844         add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
845                            function_bind(BKE_object_eval_constraints,
846                                          _1,
847                                          get_cow_datablock(scene_),
848                                          get_cow_datablock(object)),
849                            DEG_OPCODE_TRANSFORM_CONSTRAINTS);
850 }
851
852 void DepsgraphNodeBuilder::build_object_pointcache(Object *object)
853 {
854         if (!BKE_ptcache_object_has(scene_, object, 0)) {
855                 return;
856         }
857         Scene *scene_cow = get_cow_datablock(scene_);
858         Object *object_cow = get_cow_datablock(object);
859         add_operation_node(&object->id,
860                            DEG_NODE_TYPE_POINT_CACHE,
861                            function_bind(BKE_object_eval_ptcache_reset,
862                                          _1,
863                                          scene_cow,
864                                          object_cow),
865                            DEG_OPCODE_POINT_CACHE_RESET);
866 }
867
868 /**
869  * Build graph nodes for AnimData block
870  * \param id: ID-Block which hosts the AnimData
871  */
872 void DepsgraphNodeBuilder::build_animdata(ID *id)
873 {
874         AnimData *adt = BKE_animdata_from_id(id);
875         if (adt == NULL) {
876                 return;
877         }
878         if (adt->action != NULL) {
879                 build_action(adt->action);
880         }
881         /* animation */
882         if (adt->action || adt->nla_tracks.first || adt->drivers.first) {
883                 (void) add_id_node(id);
884                 ID *id_cow = get_cow_id(id);
885
886                 // XXX: Hook up specific update callbacks for special properties which
887                 // may need it...
888
889                 /* actions and NLA - as a single unit for now, as it gets complicated to
890                  * schedule otherwise.
891                  */
892                 if ((adt->action) || (adt->nla_tracks.first)) {
893                         /* create the node */
894                         add_operation_node(id, DEG_NODE_TYPE_ANIMATION,
895                                            function_bind(BKE_animsys_eval_animdata,
896                                                          _1,
897                                                          id_cow),
898                                            DEG_OPCODE_ANIMATION,
899                                            id->name);
900
901                         /* TODO: for each channel affected, we might also want to add some
902                          * support for running RNA update callbacks on them
903                          * (which will be needed for proper handling of drivers later)
904                          */
905                 }
906
907                 /* NLA strips contain actions */
908                 LISTBASE_FOREACH (NlaTrack *, nlt, &adt->nla_tracks) {
909                         build_animdata_nlastrip_targets(&nlt->strips);
910                 }
911
912                 /* drivers */
913                 int driver_index = 0;
914                 LISTBASE_FOREACH (FCurve *, fcu, &adt->drivers) {
915                         /* create driver */
916                         build_driver(id, fcu, driver_index++);
917                 }
918         }
919 }
920
921 void DepsgraphNodeBuilder::build_animdata_nlastrip_targets(ListBase *strips)
922 {
923         LISTBASE_FOREACH (NlaStrip *, strip, strips) {
924                 if (strip->act != NULL) {
925                         build_action(strip->act);
926                 }
927                 else if (strip->strips.first != NULL) {
928                         build_animdata_nlastrip_targets(&strip->strips);
929                 }
930         }
931 }
932
933 void DepsgraphNodeBuilder::build_action(bAction *action)
934 {
935         if (built_map_.checkIsBuiltAndTag(action)) {
936                 return;
937         }
938         add_operation_node(&action->id,
939                            DEG_NODE_TYPE_ANIMATION,
940                            NULL,
941                            DEG_OPCODE_ANIMATION);
942 }
943
944 /**
945  * Build graph node(s) for Driver
946  * \param id: ID-Block that driver is attached to
947  * \param fcu: Driver-FCurve
948  * \param driver_index: Index in animation data drivers list
949  */
950 void DepsgraphNodeBuilder::build_driver(ID *id, FCurve *fcurve, int driver_index)
951 {
952         /* Create data node for this driver */
953         ID *id_cow = get_cow_id(id);
954         ChannelDriver *driver_orig = fcurve->driver;
955
956         /* TODO(sergey): ideally we could pass the COW of fcu, but since it
957          * has not yet been allocated at this point we can't. As a workaround
958          * the animation systems allocates an array so we can do a fast lookup
959          * with the driver index. */
960         ensure_operation_node(id,
961                               DEG_NODE_TYPE_PARAMETERS,
962                               function_bind(BKE_animsys_eval_driver, _1, id_cow, driver_index, driver_orig),
963                               DEG_OPCODE_DRIVER,
964                               fcurve->rna_path ? fcurve->rna_path : "",
965                               fcurve->array_index);
966         build_driver_variables(id, fcurve);
967 }
968
969 void DepsgraphNodeBuilder::build_driver_variables(ID * id, FCurve *fcurve)
970 {
971         build_driver_id_property(id, fcurve->rna_path);
972         LISTBASE_FOREACH (DriverVar *, dvar, &fcurve->driver->variables) {
973                 DRIVER_TARGETS_USED_LOOPER_BEGIN(dvar)
974                 {
975                         if (dtar->id == NULL) {
976                                 continue;
977                         }
978                         build_id(dtar->id);
979                         build_driver_id_property(dtar->id, dtar->rna_path);
980                         /* Corresponds to dtar_id_ensure_proxy_from(). */
981                         if ((GS(dtar->id->name) == ID_OB) &&
982                             (((Object *)dtar->id)->proxy_from != NULL))
983                         {
984                                 Object *proxy_from = ((Object *)dtar->id)->proxy_from;
985                                 build_id(&proxy_from->id);
986                                 build_driver_id_property(&proxy_from->id, dtar->rna_path);
987                         }
988                 }
989                 DRIVER_TARGETS_LOOPER_END;
990         }
991 }
992
993 void DepsgraphNodeBuilder::build_driver_id_property(ID *id,
994                                                     const char *rna_path)
995 {
996         if (id == NULL || rna_path == NULL) {
997                 return;
998         }
999         PointerRNA id_ptr, ptr;
1000         PropertyRNA *prop;
1001         RNA_id_pointer_create(id, &id_ptr);
1002         if (!RNA_path_resolve_full(&id_ptr, rna_path, &ptr, &prop, NULL)) {
1003                 return;
1004         }
1005         if (prop == NULL) {
1006                 return;
1007         }
1008         if (!RNA_property_is_idprop(prop)) {
1009                 return;
1010         }
1011         const char *prop_identifier = RNA_property_identifier((PropertyRNA *)prop);
1012         ensure_operation_node(id,
1013                               DEG_NODE_TYPE_PARAMETERS,
1014                               NULL,
1015                               DEG_OPCODE_ID_PROPERTY,
1016                               prop_identifier);
1017 }
1018
1019 /* Recursively build graph for world */
1020 void DepsgraphNodeBuilder::build_world(World *world)
1021 {
1022         if (built_map_.checkIsBuiltAndTag(world)) {
1023                 return;
1024         }
1025         /* World itself. */
1026         add_id_node(&world->id);
1027         World *world_cow = get_cow_datablock(world);
1028         /* Shading update. */
1029         add_operation_node(&world->id,
1030                            DEG_NODE_TYPE_SHADING,
1031                            function_bind(BKE_world_eval,
1032                                          _1,
1033                                          world_cow),
1034                            DEG_OPCODE_WORLD_UPDATE);
1035         /* Animation. */
1036         build_animdata(&world->id);
1037         /* World's nodetree. */
1038         build_nodetree(world->nodetree);
1039 }
1040
1041 /* Rigidbody Simulation - Scene Level */
1042 void DepsgraphNodeBuilder::build_rigidbody(Scene *scene)
1043 {
1044         RigidBodyWorld *rbw = scene->rigidbody_world;
1045         Scene *scene_cow = get_cow_datablock(scene);
1046
1047         /**
1048          * Rigidbody Simulation Nodes
1049          * ==========================
1050          *
1051          * There are 3 nodes related to Rigidbody Simulation:
1052          * 1) "Initialize/Rebuild World" - this is called sparingly, only when the
1053          *    simulation needs to be rebuilt (mainly after file reload, or moving
1054          *    back to start frame)
1055          * 2) "Do Simulation" - perform a simulation step - interleaved between the
1056          *    evaluation steps for clusters of objects (i.e. between those affected
1057          *    and/or not affected by the sim for instance).
1058          *
1059          * 3) "Pull Results" - grab the specific transforms applied for a specific
1060          *    object - performed as part of object's transform-stack building.
1061          */
1062
1063         /* Create nodes --------------------------------------------------------- */
1064
1065         /* XXX: is this the right component, or do we want to use another one
1066          * instead?
1067          */
1068
1069         /* init/rebuild operation */
1070         /*OperationDepsNode *init_node =*/ add_operation_node(
1071                 &scene->id, DEG_NODE_TYPE_TRANSFORM,
1072                 function_bind(BKE_rigidbody_rebuild_sim, _1, scene_cow),
1073                 DEG_OPCODE_RIGIDBODY_REBUILD);
1074
1075         /* do-sim operation */
1076         // XXX: what happens if we need to split into several groups?
1077         OperationDepsNode *sim_node = add_operation_node(
1078                 &scene->id, DEG_NODE_TYPE_TRANSFORM,
1079                 function_bind(BKE_rigidbody_eval_simulation, _1, scene_cow),
1080                 DEG_OPCODE_RIGIDBODY_SIM);
1081
1082         /* XXX: For now, the sim node is the only one that really matters here.
1083          * If any other sims get added later, we may have to remove these hacks...
1084          */
1085         sim_node->owner->entry_operation = sim_node;
1086         sim_node->owner->exit_operation  = sim_node;
1087
1088         /* objects - simulation participants */
1089         if (rbw->group) {
1090                 build_collection(NULL, rbw->group);
1091
1092                 FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(rbw->group, object)
1093                 {
1094                         if (object->type != OB_MESH)
1095                                 continue;
1096
1097                         /* 2) create operation for flushing results */
1098                         /* object's transform component - where the rigidbody operation
1099                          * lives. */
1100                         add_operation_node(&object->id, DEG_NODE_TYPE_TRANSFORM,
1101                                            function_bind(
1102                                                    BKE_rigidbody_object_sync_transforms,
1103                                                    _1,
1104                                                    scene_cow,
1105                                                    get_cow_datablock(object)),
1106                                            DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
1107                 }
1108                 FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
1109         }
1110 }
1111
1112 void DepsgraphNodeBuilder::build_particle_systems(Object *object,
1113                                                   bool is_object_visible)
1114 {
1115         /**
1116          * Particle Systems Nodes
1117          * ======================
1118          *
1119          * There are two types of nodes associated with representing
1120          * particle systems:
1121          *  1) Component (EVAL_PARTICLES) - This is the particle-system
1122          *     evaluation context for an object. It acts as the container
1123          *     for all the nodes associated with a particular set of particle
1124          *     systems.
1125          *  2) Particle System Eval Operation - This operation node acts as a
1126          *     blackbox evaluation step for one particle system referenced by
1127          *     the particle systems stack. All dependencies link to this operation.
1128          */
1129         /* Component for all particle systems. */
1130         ComponentDepsNode *psys_comp =
1131                 add_component_node(&object->id, DEG_NODE_TYPE_PARTICLE_SYSTEM);
1132
1133         Object *ob_cow = get_cow_datablock(object);
1134         OperationDepsNode *op_node;
1135         op_node = add_operation_node(psys_comp,
1136                                      function_bind(BKE_particle_system_eval_init,
1137                                                    _1,
1138                                                    ob_cow),
1139                                      DEG_OPCODE_PARTICLE_SYSTEM_INIT);
1140         op_node->set_as_entry();
1141         /* Build all particle systems. */
1142         LISTBASE_FOREACH (ParticleSystem *, psys, &object->particlesystem) {
1143                 ParticleSettings *part = psys->part;
1144                 /* Build particle settings operations.
1145                  *
1146                  * NOTE: The call itself ensures settings are only build once.  */
1147                 build_particle_settings(part);
1148                 /* Particle system evaluation. */
1149                 add_operation_node(psys_comp,
1150                                    NULL,
1151                                    DEG_OPCODE_PARTICLE_SYSTEM_EVAL,
1152                                    psys->name);
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                                      DEG_OPCODE_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         OperationDepsNode *op_node;
1189         op_node = add_operation_node(&particle_settings->id,
1190                                      DEG_NODE_TYPE_PARTICLE_SETTINGS,
1191                                      NULL,
1192                                      DEG_OPCODE_PARTICLE_SETTINGS_INIT);
1193         op_node->set_as_entry();
1194         add_operation_node(&particle_settings->id,
1195                            DEG_NODE_TYPE_PARTICLE_SETTINGS,
1196                            function_bind(BKE_particle_settings_eval_reset,
1197                                          _1,
1198                                          particle_settings_cow),
1199                            DEG_OPCODE_PARTICLE_SETTINGS_RESET);
1200         op_node = add_operation_node(&particle_settings->id,
1201                                      DEG_NODE_TYPE_PARTICLE_SETTINGS,
1202                                      NULL,
1203                                      DEG_OPCODE_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         add_operation_node(&key->id,
1223                            DEG_NODE_TYPE_GEOMETRY,
1224                            NULL,
1225                            DEG_OPCODE_GEOMETRY_SHAPEKEY);
1226 }
1227
1228 /* ObData Geometry Evaluation */
1229 // XXX: what happens if the datablock is shared!
1230 void DepsgraphNodeBuilder::build_object_data_geometry(
1231         Object *object,
1232         bool is_object_visible)
1233 {
1234         OperationDepsNode *op_node;
1235         Scene *scene_cow = get_cow_datablock(scene_);
1236         Object *object_cow = get_cow_datablock(object);
1237         /* Temporary uber-update node, which does everything.
1238          * It is for the being we're porting old dependencies into the new system.
1239          * We'll get rid of this node as soon as all the granular update functions
1240          * are filled in.
1241          *
1242          * TODO(sergey): Get rid of this node.
1243          */
1244         op_node = add_operation_node(&object->id,
1245                                      DEG_NODE_TYPE_GEOMETRY,
1246                                      function_bind(BKE_object_eval_uber_data,
1247                                                    _1,
1248                                                    scene_cow,
1249                                                    object_cow),
1250                                      DEG_OPCODE_GEOMETRY_UBEREVAL);
1251         op_node->set_as_exit();
1252
1253         op_node = add_operation_node(&object->id,
1254                                      DEG_NODE_TYPE_GEOMETRY,
1255                                      NULL,
1256                                      DEG_OPCODE_PLACEHOLDER,
1257                                      "Eval Init");
1258         op_node->set_as_entry();
1259         /* Materials. */
1260         if (object->totcol != 0) {
1261                 if (object->type == OB_MESH) {
1262                         add_operation_node(&object->id,
1263                                            DEG_NODE_TYPE_SHADING,
1264                                            function_bind(BKE_object_eval_update_shading,
1265                                                          _1,
1266                                                          object_cow),
1267                                            DEG_OPCODE_SHADING);
1268                 }
1269                 for (int a = 1; a <= object->totcol; a++) {
1270                         Material *ma = give_current_material(object, a);
1271                         if (ma != NULL) {
1272                                 build_material(ma);
1273                         }
1274                 }
1275         }
1276         /* Point caches. */
1277         build_object_pointcache(object);
1278         /* Geometry. */
1279         build_object_data_geometry_datablock((ID *)object->data, is_object_visible);
1280 }
1281
1282 void DepsgraphNodeBuilder::build_object_data_geometry_datablock(
1283         ID *obdata,
1284         bool is_object_visible)
1285 {
1286         if (built_map_.checkIsBuiltAndTag(obdata)) {
1287                 return;
1288         }
1289         OperationDepsNode *op_node;
1290         /* Make sure we've got an ID node before requesting CoW pointer. */
1291         (void) add_id_node((ID *)obdata);
1292         ID *obdata_cow = get_cow_id(obdata);
1293         /* Animation. */
1294         build_animdata(obdata);
1295         /* ShapeKeys */
1296         Key *key = BKE_key_from_id(obdata);
1297         if (key) {
1298                 build_shapekeys(key);
1299         }
1300         /* Nodes for result of obdata's evaluation, and geometry
1301          * evaluation on object.
1302          */
1303         const ID_Type id_type = GS(obdata->name);
1304         switch (id_type) {
1305                 case ID_ME:
1306                 {
1307                         op_node = add_operation_node(obdata,
1308                                                      DEG_NODE_TYPE_GEOMETRY,
1309                                                      function_bind(BKE_mesh_eval_geometry,
1310                                                                    _1,
1311                                                                    (Mesh *)obdata_cow),
1312                                                      DEG_OPCODE_PLACEHOLDER,
1313                                                      "Geometry Eval");
1314                         op_node->set_as_entry();
1315                         break;
1316                 }
1317                 case ID_MB:
1318                 {
1319                         op_node = add_operation_node(obdata,
1320                                                      DEG_NODE_TYPE_GEOMETRY,
1321                                                      NULL,
1322                                                      DEG_OPCODE_PLACEHOLDER,
1323                                                      "Geometry Eval");
1324                         op_node->set_as_entry();
1325                         break;
1326                 }
1327                 case ID_CU:
1328                 {
1329                         op_node = add_operation_node(obdata,
1330                                                      DEG_NODE_TYPE_GEOMETRY,
1331                                                      function_bind(BKE_curve_eval_geometry,
1332                                                                    _1,
1333                                                                    (Curve *)obdata_cow),
1334                                                                    DEG_OPCODE_PLACEHOLDER,
1335                                                                    "Geometry Eval");
1336                         op_node->set_as_entry();
1337                         /* Make sure objects used for bevel.taper are in the graph.
1338                          * NOTE: This objects might be not linked to the scene.
1339                          */
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                                                      DEG_NODE_TYPE_GEOMETRY,
1365                                                      function_bind(BKE_lattice_eval_geometry,
1366                                                                    _1,
1367                                                                    (Lattice *)obdata_cow),
1368                                                                    DEG_OPCODE_PLACEHOLDER,
1369                                                                    "Geometry Eval");
1370                         op_node->set_as_entry();
1371                         break;
1372                 }
1373
1374                 case ID_GD:
1375                 {
1376                         /* GPencil evaluation operations. */
1377                         op_node = add_operation_node(obdata,
1378                                                      DEG_NODE_TYPE_GEOMETRY,
1379                                                      function_bind(BKE_gpencil_eval_geometry,
1380                                                                    _1,
1381                                                                    (bGPdata *)obdata_cow),
1382                                                      DEG_OPCODE_PLACEHOLDER,
1383                                                      "Geometry Eval");
1384                         op_node->set_as_entry();
1385                         break;
1386                 }
1387                 default:
1388                         BLI_assert(!"Should not happen");
1389                         break;
1390         }
1391         op_node = add_operation_node(obdata, DEG_NODE_TYPE_GEOMETRY, NULL,
1392                                      DEG_OPCODE_PLACEHOLDER, "Eval Done");
1393         op_node->set_as_exit();
1394         /* Parameters for driver sources. */
1395         add_operation_node(obdata,
1396                            DEG_NODE_TYPE_PARAMETERS,
1397                            NULL,
1398                            DEG_OPCODE_PARAMETERS_EVAL);
1399         /* Batch cache. */
1400         add_operation_node(obdata,
1401                            DEG_NODE_TYPE_BATCH_CACHE,
1402                            function_bind(BKE_object_data_select_update,
1403                                          _1,
1404                                          obdata_cow),
1405                            DEG_OPCODE_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                            DEG_NODE_TYPE_PARAMETERS,
1417                            NULL,
1418                            DEG_OPCODE_PLACEHOLDER,
1419                            "Armature Eval");
1420 }
1421
1422 void DepsgraphNodeBuilder::build_camera(Camera *camera)
1423 {
1424         if (built_map_.checkIsBuiltAndTag(camera)) {
1425                 return;
1426         }
1427         OperationDepsNode *op_node;
1428         build_animdata(&camera->id);
1429         op_node = add_operation_node(&camera->id,
1430                                      DEG_NODE_TYPE_PARAMETERS,
1431                                      NULL,
1432                                      DEG_OPCODE_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         OperationDepsNode *op_node;
1442         build_animdata(&lamp->id);
1443         op_node = add_operation_node(&lamp->id,
1444                                      DEG_NODE_TYPE_PARAMETERS,
1445                                      NULL,
1446                                      DEG_OPCODE_PARAMETERS_EVAL);
1447         op_node->set_as_exit();
1448         /* lamp's nodetree */
1449         build_nodetree(lamp->nodetree);
1450 }
1451
1452 void DepsgraphNodeBuilder::build_nodetree(bNodeTree *ntree)
1453 {
1454         if (ntree == NULL) {
1455                 return;
1456         }
1457         if (built_map_.checkIsBuiltAndTag(ntree)) {
1458                 return;
1459         }
1460         /* nodetree itself */
1461         add_id_node(&ntree->id);
1462         bNodeTree *ntree_cow = get_cow_datablock(ntree);
1463         /* Animation, */
1464         build_animdata(&ntree->id);
1465         /* Shading update. */
1466         add_operation_node(&ntree->id,
1467                            DEG_NODE_TYPE_SHADING,
1468                            NULL,
1469                            DEG_OPCODE_MATERIAL_UPDATE);
1470         /* NOTE: We really pass original and CoW node trees here, this is how the
1471          * callback works. Ideally we need to find a better way for that.
1472          */
1473         add_operation_node(&ntree->id,
1474                            DEG_NODE_TYPE_SHADING_PARAMETERS,
1475                            function_bind(BKE_nodetree_shading_params_eval,
1476                                          _1,
1477                                          ntree_cow,
1478                                          ntree),
1479                            DEG_OPCODE_MATERIAL_UPDATE);
1480         /* nodetree's nodes... */
1481         LISTBASE_FOREACH (bNode *, bnode, &ntree->nodes) {
1482                 ID *id = bnode->id;
1483                 if (id == NULL) {
1484                         continue;
1485                 }
1486                 ID_Type id_type = GS(id->name);
1487                 if (id_type == ID_MA) {
1488                         build_material((Material *)id);
1489                 }
1490                 else if (id_type == ID_TE) {
1491                         build_texture((Tex *)id);
1492                 }
1493                 else if (id_type == ID_IM) {
1494                         build_image((Image *)id);
1495                 }
1496                 else if (id_type == ID_OB) {
1497                         /* TODO(sergey): Use visibility of owner of the node tree. */
1498                         build_object(-1, (Object *)id, DEG_ID_LINKED_INDIRECTLY, true);
1499                 }
1500                 else if (id_type == ID_SCE) {
1501                         /* Scenes are used by compositor trees, and handled by render
1502                          * pipeline. No need to build dependencies for them here.
1503                          */
1504                 }
1505                 else if (id_type == ID_TXT) {
1506                         /* Ignore script nodes. */
1507                 }
1508                 else if (id_type == ID_MSK) {
1509                         build_mask((Mask *)id);
1510                 }
1511                 else if (id_type == ID_MC) {
1512                         build_movieclip((MovieClip *)id);
1513                 }
1514                 else if (bnode->type == NODE_GROUP) {
1515                         bNodeTree *group_ntree = (bNodeTree *)id;
1516                         build_nodetree(group_ntree);
1517                 }
1518                 else {
1519                         BLI_assert(!"Unknown ID type used for node");
1520                 }
1521         }
1522
1523         // TODO: link from nodetree to owner_component?
1524 }
1525
1526 /* Recursively build graph for material */
1527 void DepsgraphNodeBuilder::build_material(Material *material)
1528 {
1529         if (built_map_.checkIsBuiltAndTag(material)) {
1530                 return;
1531         }
1532         /* Material itself. */
1533         add_id_node(&material->id);
1534         Material *material_cow = get_cow_datablock(material);
1535         /* Shading update. */
1536         add_operation_node(&material->id,
1537                            DEG_NODE_TYPE_SHADING,
1538                            function_bind(BKE_material_eval,
1539                                          _1,
1540                                          material_cow),
1541                            DEG_OPCODE_MATERIAL_UPDATE);
1542         /* Material animation. */
1543         build_animdata(&material->id);
1544         /* Material's nodetree. */
1545         build_nodetree(material->nodetree);
1546 }
1547
1548 /* Recursively build graph for texture */
1549 void DepsgraphNodeBuilder::build_texture(Tex *texture)
1550 {
1551         if (built_map_.checkIsBuiltAndTag(texture)) {
1552                 return;
1553         }
1554         /* Texture itself. */
1555         build_animdata(&texture->id);
1556         /* Texture's nodetree. */
1557         build_nodetree(texture->nodetree);
1558         /* Special cases for different IDs which texture uses. */
1559         if (texture->type == TEX_IMAGE) {
1560                 if (texture->ima != NULL) {
1561                         build_image(texture->ima);
1562                 }
1563         }
1564         add_operation_node(&texture->id,
1565                            DEG_NODE_TYPE_GENERIC_DATABLOCK,
1566                            NULL,
1567                            DEG_OPCODE_GENERIC_DATABLOCK_UPDATE);
1568 }
1569
1570 void DepsgraphNodeBuilder::build_image(Image *image) {
1571         if (built_map_.checkIsBuiltAndTag(image)) {
1572                 return;
1573         }
1574         add_operation_node(&image->id,
1575                            DEG_NODE_TYPE_GENERIC_DATABLOCK,
1576                            NULL,
1577                            DEG_OPCODE_GENERIC_DATABLOCK_UPDATE);
1578 }
1579
1580 void DepsgraphNodeBuilder::build_compositor(Scene *scene)
1581 {
1582         /* For now, just a plain wrapper? */
1583         // TODO: create compositing component?
1584         // XXX: component type undefined!
1585         //graph->get_node(&scene->id, NULL, DEG_NODE_TYPE_COMPOSITING, NULL);
1586
1587         /* for now, nodetrees are just parameters; compositing occurs in internals
1588          * of renderer...
1589          */
1590         add_component_node(&scene->id, DEG_NODE_TYPE_PARAMETERS);
1591         build_nodetree(scene->nodetree);
1592 }
1593
1594 void DepsgraphNodeBuilder::build_gpencil(bGPdata *gpd)
1595 {
1596         if (built_map_.checkIsBuiltAndTag(gpd)) {
1597                 return;
1598         }
1599         ID *gpd_id = &gpd->id;
1600
1601         /* TODO(sergey): what about multiple users of same datablock? This should
1602          * only get added once.
1603          */
1604
1605         /* The main reason Grease Pencil is included here is because the animation
1606          * (and drivers) need to be hosted somewhere.
1607          */
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, DEG_NODE_TYPE_CACHE, NULL,
1621                            DEG_OPCODE_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                            DEG_NODE_TYPE_ANIMATION,
1636                            function_bind(BKE_mask_eval_animation, _1, mask_cow),
1637                            DEG_OPCODE_MASK_ANIMATION);
1638         /* Final mask evaluation. */
1639         add_operation_node(mask_id,
1640                            DEG_NODE_TYPE_PARAMETERS,
1641                            function_bind(BKE_mask_eval_update, _1, mask_cow),
1642                            DEG_OPCODE_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                            DEG_NODE_TYPE_PARAMETERS,
1657                            function_bind(BKE_movieclip_eval_update, _1, clip_cow),
1658                            DEG_OPCODE_MOVIECLIP_EVAL);
1659
1660         add_operation_node(clip_id,
1661                            DEG_NODE_TYPE_BATCH_CACHE,
1662                            function_bind(BKE_movieclip_eval_selection_update, _1, clip_cow),
1663                            DEG_OPCODE_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                            DEG_NODE_TYPE_PARAMETERS,
1674                            NULL,
1675                            DEG_OPCODE_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                            DEG_NODE_TYPE_PARAMETERS,
1688                            NULL,
1689                            DEG_OPCODE_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