Multi-Object Pose: Reset unkeyed works now (Clear User Transforms)
[blender.git] / source / blender / depsgraph / intern / depsgraph.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): Sergey Sharybin
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/depsgraph/intern/depsgraph.cc
28  *  \ingroup depsgraph
29  *
30  * Core routines for how the Depsgraph works.
31  */
32
33 #include "intern/depsgraph.h" /* own include */
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_utildefines.h"
38 #include "BLI_console.h"
39 #include "BLI_hash.h"
40 #include "BLI_ghash.h"
41 #include "BLI_listbase.h"
42
43 extern "C" {
44 #include "DNA_action_types.h"
45 #include "DNA_armature_types.h"
46 #include "DNA_constraint_types.h"
47 #include "DNA_key_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_sequence_types.h"
50
51 #include "RNA_access.h"
52
53 #include "BKE_scene.h"
54 }
55
56 #include <algorithm>
57 #include <cstring>
58
59 #include "DEG_depsgraph.h"
60
61 #include "intern/eval/deg_eval_copy_on_write.h"
62
63 #include "intern/nodes/deg_node.h"
64 #include "intern/nodes/deg_node_component.h"
65 #include "intern/nodes/deg_node_id.h"
66 #include "intern/nodes/deg_node_operation.h"
67 #include "intern/nodes/deg_node_time.h"
68
69 #include "intern/depsgraph_intern.h"
70 #include "util/deg_util_foreach.h"
71
72 static bool use_copy_on_write = false;
73
74 bool DEG_depsgraph_use_copy_on_write(void)
75 {
76         return use_copy_on_write;
77 }
78
79 void DEG_depsgraph_enable_copy_on_write(void)
80 {
81         use_copy_on_write = true;
82 }
83
84 namespace DEG {
85
86 static DEG_EditorUpdateIDCb deg_editor_update_id_cb = NULL;
87 static DEG_EditorUpdateSceneCb deg_editor_update_scene_cb = NULL;
88
89 /* TODO(sergey): Find a better place for this. */
90 template <typename T>
91 static void remove_from_vector(vector<T> *vector, const T& value)
92 {
93         vector->erase(std::remove(vector->begin(), vector->end(), value),
94                       vector->end());
95 }
96
97 Depsgraph::Depsgraph(Scene *scene,
98                      ViewLayer *view_layer,
99                      eEvaluationMode mode)
100   : time_source(NULL),
101     need_update(true),
102     scene(scene),
103     view_layer(view_layer),
104     mode(mode),
105     ctime(BKE_scene_frame_get(scene))
106 {
107         BLI_spin_init(&lock);
108         id_hash = BLI_ghash_ptr_new("Depsgraph id hash");
109         entry_tags = BLI_gset_ptr_new("Depsgraph entry_tags");
110 }
111
112 Depsgraph::~Depsgraph()
113 {
114         clear_id_nodes();
115         BLI_ghash_free(id_hash, NULL, NULL);
116         BLI_gset_free(entry_tags, NULL);
117         if (time_source != NULL) {
118                 OBJECT_GUARDED_DELETE(time_source, TimeSourceDepsNode);
119         }
120         BLI_spin_end(&lock);
121 }
122
123 /* Query Conditions from RNA ----------------------- */
124
125 static bool pointer_to_component_node_criteria(
126         const PointerRNA *ptr,
127         const PropertyRNA *prop,
128         ID **id,
129         eDepsNode_Type *type,
130         const char **subdata,
131         eDepsOperation_Code *operation_code,
132         const char **operation_name,
133         int *operation_name_tag)
134 {
135         if (ptr->type == NULL) {
136                 return false;
137         }
138         /* Set default values for returns. */
139         *id = (ID *)ptr->id.data;
140         *subdata = "";
141         *operation_code = DEG_OPCODE_OPERATION;
142         *operation_name = "";
143         *operation_name_tag = -1;
144         /* Handling of commonly known scenarios. */
145         if (ptr->type == &RNA_PoseBone) {
146                 bPoseChannel *pchan = (bPoseChannel *)ptr->data;
147                 if (prop != NULL && RNA_property_is_idprop(prop)) {
148                         *type = DEG_NODE_TYPE_PARAMETERS;
149                         *subdata = "";
150                         *operation_code = DEG_OPCODE_PARAMETERS_EVAL;
151                         *operation_name = pchan->name;
152                 }
153                 else {
154                         /* Bone - generally, we just want the bone component. */
155                         *type = DEG_NODE_TYPE_BONE;
156                         *subdata = pchan->name;
157                 }
158                 return true;
159         }
160         else if (ptr->type == &RNA_Bone) {
161                 Bone *bone = (Bone *)ptr->data;
162                 /* armature-level bone, but it ends up going to bone component anyway */
163                 // NOTE: the ID in thise case will end up being bArmature.
164                 *type = DEG_NODE_TYPE_BONE;
165                 *subdata = bone->name;
166                 return true;
167         }
168         else if (RNA_struct_is_a(ptr->type, &RNA_Constraint)) {
169                 Object *object = (Object *)ptr->id.data;
170                 bConstraint *con = (bConstraint *)ptr->data;
171                 /* Check whether is object or bone constraint. */
172                 /* NOTE: Currently none of the area can address transform of an object
173                  * at a given constraint, but for rigging one might use constraint
174                  * influence to be used to drive some corrective shape keys or so.
175                  */
176                 if (BLI_findindex(&object->constraints, con) != -1) {
177                         *type = DEG_NODE_TYPE_TRANSFORM;
178                         *operation_code = DEG_OPCODE_TRANSFORM_LOCAL;
179                         return true;
180                 }
181                 else if (object->pose != NULL) {
182                         LISTBASE_FOREACH(bPoseChannel *, pchan, &object->pose->chanbase) {
183                                 if (BLI_findindex(&pchan->constraints, con) != -1) {
184                                         *type = DEG_NODE_TYPE_BONE;
185                                         *operation_code = DEG_OPCODE_BONE_LOCAL;
186                                         *subdata = pchan->name;
187                                         return true;
188                                 }
189                         }
190                 }
191         }
192         else if (RNA_struct_is_a(ptr->type, &RNA_Modifier)) {
193                 *type = DEG_NODE_TYPE_GEOMETRY;
194                 return true;
195         }
196         else if (ptr->type == &RNA_Object) {
197                 /* Transforms props? */
198                 if (prop != NULL) {
199                         const char *prop_identifier = RNA_property_identifier((PropertyRNA *)prop);
200                         /* TODO(sergey): How to optimize this? */
201                         if (strstr(prop_identifier, "location") ||
202                             strstr(prop_identifier, "rotation") ||
203                             strstr(prop_identifier, "scale") ||
204                             strstr(prop_identifier, "matrix_"))
205                         {
206                                 *type = DEG_NODE_TYPE_TRANSFORM;
207                                 return true;
208                         }
209                         else if (strstr(prop_identifier, "data")) {
210                                 /* We access object.data, most likely a geometry.
211                                  * Might be a bone tho..
212                                  */
213                                 *type = DEG_NODE_TYPE_GEOMETRY;
214                                 return true;
215                         }
216                 }
217         }
218         else if (ptr->type == &RNA_ShapeKey) {
219                 *id = (ID *)ptr->id.data;
220                 *type = DEG_NODE_TYPE_GEOMETRY;
221                 return true;
222         }
223         else if (ptr->type == &RNA_Key) {
224                 *id = (ID *)ptr->id.data;
225                 *type = DEG_NODE_TYPE_GEOMETRY;
226                 return true;
227         }
228         else if (RNA_struct_is_a(ptr->type, &RNA_Sequence)) {
229                 Sequence *seq = (Sequence *)ptr->data;
230                 /* Sequencer strip */
231                 *type = DEG_NODE_TYPE_SEQUENCER;
232                 *subdata = seq->name; // xxx?
233                 return true;
234         }
235         else if (RNA_struct_is_a(ptr->type, &RNA_NodeSocket)) {
236                 *type = DEG_NODE_TYPE_SHADING;
237                 return true;
238         }
239         if (prop != NULL) {
240                 /* All unknown data effectively falls under "parameter evaluation". */
241                 if (RNA_property_is_idprop(prop)) {
242                         *type = DEG_NODE_TYPE_PARAMETERS;
243                         *operation_code = DEG_OPCODE_ID_PROPERTY;
244                         *operation_name = RNA_property_identifier((PropertyRNA *)prop);
245                         *operation_name_tag = -1;
246                 }
247                 else {
248                         *type = DEG_NODE_TYPE_PARAMETERS;
249                         *operation_code = DEG_OPCODE_PARAMETERS_EVAL;
250                         *operation_name = "";
251                         *operation_name_tag = -1;
252                 }
253                 return true;
254         }
255         return false;
256 }
257
258 /* Convenience wrapper to find node given just pointer + property. */
259 DepsNode *Depsgraph::find_node_from_pointer(const PointerRNA *ptr,
260                                             const PropertyRNA *prop) const
261 {
262         ID *id;
263         eDepsNode_Type node_type;
264         const char *component_name, *operation_name;
265         eDepsOperation_Code operation_code;
266         int operation_name_tag;
267
268         if (pointer_to_component_node_criteria(
269                          ptr, prop,
270                          &id, &node_type, &component_name,
271                          &operation_code, &operation_name, &operation_name_tag))
272         {
273                 IDDepsNode *id_node = find_id_node(id);
274                 if (id_node == NULL) {
275                         return NULL;
276                 }
277                 ComponentDepsNode *comp_node =
278                         id_node->find_component(node_type, component_name);
279                 if (comp_node == NULL) {
280                         return NULL;
281                 }
282                 if (operation_code == DEG_OPCODE_OPERATION) {
283                         return comp_node;
284                 }
285                 return comp_node->find_operation(operation_code,
286                                                  operation_name,
287                                                  operation_name_tag);
288         }
289         return NULL;
290 }
291
292 /* Node Management ---------------------------- */
293
294 TimeSourceDepsNode *Depsgraph::add_time_source()
295 {
296         if (time_source == NULL) {
297                 DepsNodeFactory *factory = deg_type_get_factory(DEG_NODE_TYPE_TIMESOURCE);
298                 time_source = (TimeSourceDepsNode *)factory->create_node(NULL, "", "Time Source");
299         }
300         return time_source;
301 }
302
303 TimeSourceDepsNode *Depsgraph::find_time_source() const
304 {
305         return time_source;
306 }
307
308 IDDepsNode *Depsgraph::find_id_node(const ID *id) const
309 {
310         return reinterpret_cast<IDDepsNode *>(BLI_ghash_lookup(id_hash, id));
311 }
312
313 IDDepsNode *Depsgraph::add_id_node(ID *id, ID *id_cow_hint)
314 {
315         BLI_assert((id->tag & LIB_TAG_COPY_ON_WRITE) == 0);
316         IDDepsNode *id_node = find_id_node(id);
317         if (!id_node) {
318                 DepsNodeFactory *factory = deg_type_get_factory(DEG_NODE_TYPE_ID_REF);
319                 id_node = (IDDepsNode *)factory->create_node(id, "", id->name);
320                 id_node->init_copy_on_write(id_cow_hint);
321                 /* Register node in ID hash.
322                  *
323                  * NOTE: We address ID nodes by the original ID pointer they are
324                  * referencing to.
325                  */
326                 BLI_ghash_insert(id_hash, id, id_node);
327                 id_nodes.push_back(id_node);
328         }
329         return id_node;
330 }
331
332 void Depsgraph::clear_id_nodes()
333 {
334         /* Free memory used by ID nodes. */
335         if (use_copy_on_write) {
336                 /* Stupid workaround to ensure we free IDs in a proper order. */
337                 foreach (IDDepsNode *id_node, id_nodes) {
338                         if (id_node->id_cow == NULL) {
339                                 /* This means builder "stole" ownership of the copy-on-written
340                                  * datablock for her own dirty needs.
341                                  */
342                                 continue;
343                         }
344                         if (!deg_copy_on_write_is_expanded(id_node->id_cow)) {
345                                 continue;
346                         }
347                         const ID_Type id_type = GS(id_node->id_cow->name);
348                         if (id_type != ID_PA) {
349                                 id_node->destroy();
350                         }
351                 }
352         }
353         foreach (IDDepsNode *id_node, id_nodes) {
354                 OBJECT_GUARDED_DELETE(id_node, IDDepsNode);
355         }
356         /* Clear containers. */
357         BLI_ghash_clear(id_hash, NULL, NULL);
358         id_nodes.clear();
359 }
360
361 /* Add new relationship between two nodes. */
362 DepsRelation *Depsgraph::add_new_relation(OperationDepsNode *from,
363                                           OperationDepsNode *to,
364                                           const char *description,
365                                           bool check_unique)
366 {
367         DepsRelation *rel = NULL;
368         if (check_unique) {
369                 rel = check_nodes_connected(from, to, description);
370         }
371         if (rel != NULL) {
372                 return rel;
373         }
374         /* Create new relation, and add it to the graph. */
375         rel = OBJECT_GUARDED_NEW(DepsRelation, from, to, description);
376         /* TODO(sergey): Find a better place for this. */
377 #ifdef WITH_OPENSUBDIV
378         ComponentDepsNode *comp_node = from->owner;
379         if (comp_node->type == DEG_NODE_TYPE_GEOMETRY) {
380                 IDDepsNode *id_to = to->owner->owner;
381                 IDDepsNode *id_from = from->owner->owner;
382                 if (id_to != id_from && (id_to->id_orig->recalc & ID_RECALC_ALL)) {
383                         if ((id_from->eval_flags & DAG_EVAL_NEED_CPU) == 0) {
384                                 id_from->tag_update(this);
385                                 id_from->eval_flags |= DAG_EVAL_NEED_CPU;
386                         }
387                 }
388         }
389 #endif
390         return rel;
391 }
392
393 /* Add new relation between two nodes */
394 DepsRelation *Depsgraph::add_new_relation(DepsNode *from, DepsNode *to,
395                                           const char *description,
396                                           bool check_unique)
397 {
398         DepsRelation *rel = NULL;
399         if (check_unique) {
400                 rel = check_nodes_connected(from, to, description);
401         }
402         if (rel != NULL) {
403                 return rel;
404         }
405         /* Create new relation, and add it to the graph. */
406         rel = OBJECT_GUARDED_NEW(DepsRelation, from, to, description);
407         return rel;
408 }
409
410 DepsRelation *Depsgraph::check_nodes_connected(const DepsNode *from,
411                                                const DepsNode *to,
412                                                const char *description)
413 {
414         foreach (DepsRelation *rel, from->outlinks) {
415                 BLI_assert(rel->from == from);
416                 if (rel->to != to) {
417                         continue;
418                 }
419                 if (description != NULL && !STREQ(rel->name, description)) {
420                         continue;
421                 }
422                 return rel;
423         }
424         return NULL;
425 }
426
427 /* ************************ */
428 /* Relationships Management */
429
430 DepsRelation::DepsRelation(DepsNode *from,
431                            DepsNode *to,
432                            const char *description)
433   : from(from),
434     to(to),
435     name(description),
436     flag(0)
437 {
438         /* Hook it up to the nodes which use it.
439          *
440          * NOTE: We register relation in the nodes which this link connects to here
441          * in constructor but we don't unregister it in the destructor.
442          *
443          * Reasoning:
444          *
445          * - Destructor is currently used on global graph destruction, so there's no
446          *   real need in avoiding dangling pointers, all the memory is to be freed
447          *   anyway.
448          *
449          * - Unregistering relation is not a cheap operation, so better to have it
450          *   as an explicit call if we need this.
451          */
452         from->outlinks.push_back(this);
453         to->inlinks.push_back(this);
454 }
455
456 DepsRelation::~DepsRelation()
457 {
458         /* Sanity check. */
459         BLI_assert(from != NULL && to != NULL);
460 }
461
462 void DepsRelation::unlink()
463 {
464         /* Sanity check. */
465         BLI_assert(from != NULL && to != NULL);
466         remove_from_vector(&from->outlinks, this);
467         remove_from_vector(&to->inlinks, this);
468 }
469
470 /* Low level tagging -------------------------------------- */
471
472 /* Tag a specific node as needing updates. */
473 void Depsgraph::add_entry_tag(OperationDepsNode *node)
474 {
475         /* Sanity check. */
476         if (node == NULL) {
477                 return;
478         }
479         /* Add to graph-level set of directly modified nodes to start searching from.
480          * NOTE: this is necessary since we have several thousand nodes to play with...
481          */
482         BLI_gset_insert(entry_tags, node);
483 }
484
485 void Depsgraph::clear_all_nodes()
486 {
487         clear_id_nodes();
488         if (time_source != NULL) {
489                 OBJECT_GUARDED_DELETE(time_source, TimeSourceDepsNode);
490                 time_source = NULL;
491         }
492 }
493
494 ID *Depsgraph::get_cow_id(const ID *id_orig) const
495 {
496         IDDepsNode *id_node = find_id_node(id_orig);
497         if (id_node == NULL) {
498                 /* This function is used from places where we expect ID to be either
499                  * already a copy-on-write version or have a corresponding copy-on-write
500                  * version.
501                  *
502                  * We try to enforce that in debug builds, for for release we play a bit
503                  * safer game here.
504                  */
505                 if ((id_orig->tag & LIB_TAG_COPY_ON_WRITE) == 0) {
506                         /* TODO(sergey): This is nice sanity check to have, but it fails
507                          * in following situations:
508                          *
509                          * - Material has link to texture, which is not needed by new
510                          *   shading system and hence can be ignored at construction.
511                          * - Object or mesh has material at a slot which is not used (for
512                          *   example, object has material slot by materials are set to
513                          *   object data).
514                          */
515                         // BLI_assert(!"Request for non-existing copy-on-write ID");
516                 }
517                 return (ID *)id_orig;
518         }
519         return id_node->id_cow;
520 }
521
522 void deg_editors_id_update(const DEGEditorUpdateContext *update_ctx, ID *id)
523 {
524         if (deg_editor_update_id_cb != NULL) {
525                 deg_editor_update_id_cb(update_ctx, id);
526         }
527 }
528
529 void deg_editors_scene_update(const DEGEditorUpdateContext *update_ctx,
530                               bool updated)
531 {
532         if (deg_editor_update_scene_cb != NULL) {
533                 deg_editor_update_scene_cb(update_ctx, updated);
534         }
535 }
536
537 bool deg_terminal_do_color(void)
538 {
539         return (G.debug & G_DEBUG_DEPSGRAPH_PRETTY) != 0;
540 }
541
542 string deg_color_for_pointer(const void *pointer)
543 {
544         if (!deg_terminal_do_color()) {
545                 return "";
546         }
547         int r, g, b;
548         BLI_hash_pointer_to_color(pointer, &r, &g, &b);
549         char buffer[64];
550         BLI_snprintf(buffer, sizeof(buffer), TRUECOLOR_ANSI_COLOR_FORMAT, r, g, b);
551         return string(buffer);
552 }
553
554 string deg_color_end(void)
555 {
556         if (!deg_terminal_do_color()) {
557                 return "";
558         }
559         return string(TRUECOLOR_ANSI_COLOR_FINISH);
560 }
561
562 }  // namespace DEG
563
564 /* **************** */
565 /* Public Graph API */
566
567 /* Initialize a new Depsgraph */
568 Depsgraph *DEG_graph_new(Scene *scene,
569                          ViewLayer *view_layer,
570                          eEvaluationMode mode)
571 {
572         DEG::Depsgraph *deg_depsgraph = OBJECT_GUARDED_NEW(DEG::Depsgraph,
573                                                            scene,
574                                                            view_layer,
575                                                            mode);
576         return reinterpret_cast<Depsgraph *>(deg_depsgraph);
577 }
578
579 /* Free graph's contents and graph itself */
580 void DEG_graph_free(Depsgraph *graph)
581 {
582         using DEG::Depsgraph;
583         DEG::Depsgraph *deg_depsgraph = reinterpret_cast<DEG::Depsgraph *>(graph);
584         OBJECT_GUARDED_DELETE(deg_depsgraph, Depsgraph);
585 }
586
587 /* Set callbacks which are being called when depsgraph changes. */
588 void DEG_editors_set_update_cb(DEG_EditorUpdateIDCb id_func,
589                                DEG_EditorUpdateSceneCb scene_func)
590 {
591         DEG::deg_editor_update_id_cb = id_func;
592         DEG::deg_editor_update_scene_cb = scene_func;
593 }
594
595 /* Evaluation and debug */
596
597 void DEG_debug_print_eval(const char *function_name,
598                           const char *object_name,
599                           const void *object_address)
600 {
601         if ((G.debug & G_DEBUG_DEPSGRAPH_EVAL) == 0) {
602                 return;
603         }
604         fprintf(stdout,
605                 "%s on %s %s(%p)%s\n",
606                 function_name,
607                 object_name,
608                 DEG::deg_color_for_pointer(object_address).c_str(),
609                 object_address,
610                 DEG::deg_color_end().c_str());
611         fflush(stdout);
612 }
613
614 void DEG_debug_print_eval_subdata(const char *function_name,
615                                   const char *object_name,
616                                   const void *object_address,
617                                   const char *subdata_comment,
618                                   const char *subdata_name,
619                                   const void *subdata_address)
620 {
621         if ((G.debug & G_DEBUG_DEPSGRAPH_EVAL) == 0) {
622                 return;
623         }
624         fprintf(stdout,
625                 "%s on %s %s(%p)%s %s %s %s(%p)%s\n",
626                 function_name,
627                 object_name,
628                 DEG::deg_color_for_pointer(object_address).c_str(),
629                 object_address,
630                 DEG::deg_color_end().c_str(),
631                 subdata_comment,
632                 subdata_name,
633                 DEG::deg_color_for_pointer(subdata_address).c_str(),
634                 subdata_address,
635                 DEG::deg_color_end().c_str());
636         fflush(stdout);
637 }
638
639 void DEG_debug_print_eval_subdata_index(const char *function_name,
640                                         const char *object_name,
641                                         const void *object_address,
642                                         const char *subdata_comment,
643                                         const char *subdata_name,
644                                         const void *subdata_address,
645                                         const int subdata_index)
646 {
647         if ((G.debug & G_DEBUG_DEPSGRAPH_EVAL) == 0) {
648                 return;
649         }
650         fprintf(stdout,
651                 "%s on %s %s(%p)^%s %s %s[%d] %s(%p)%s\n",
652                 function_name,
653                 object_name,
654                 DEG::deg_color_for_pointer(object_address).c_str(),
655                 object_address,
656                 DEG::deg_color_end().c_str(),
657                 subdata_comment,
658                 subdata_name,
659                 subdata_index,
660                 DEG::deg_color_for_pointer(subdata_address).c_str(),
661                 subdata_address,
662                 DEG::deg_color_end().c_str());
663         fflush(stdout);
664 }
665
666 void DEG_debug_print_eval_time(const char *function_name,
667                                const char *object_name,
668                                const void *object_address,
669                                float time)
670 {
671         if ((G.debug & G_DEBUG_DEPSGRAPH_EVAL) == 0) {
672                 return;
673         }
674         fprintf(stdout,
675                 "%s on %s %s(%p)%s at time %f\n",
676                 function_name,
677                 object_name,
678                 DEG::deg_color_for_pointer(object_address).c_str(),
679                 object_address,
680                 DEG::deg_color_end().c_str(),
681                 time);
682         fflush(stdout);
683 }