Depsgraph: Be consistent about id type variable name
[blender.git] / source / blender / depsgraph / intern / depsgraph_tag.cc
index 07593cc55c8b5a250cf4334878f528717631c70f..ce80f30f3597079693ef4bd830d84ca560960d40 100644 (file)
@@ -67,6 +67,13 @@ extern "C" {
 #include "intern/depsgraph_intern.h"
 #include "util/deg_util_foreach.h"
 
+/* Define this in order to have more strict sanitization of what tagging flags
+ * are used for ID databnlocks. Ideally, we would always want this, but there
+ * are cases in generic modules (like IR remapping) where we don't want to spent
+ * lots of time trying to guess which components are to be updated.
+ */
+// #define STRICT_COMPONENT_TAGGING
+
 /* *********************** */
 /* Update Tagging/Flushing */
 
@@ -101,8 +108,8 @@ void lib_id_recalc_tag_flag(Main *bmain, ID *id, int flag)
         * after relations update and after layer visibility changes.
         */
        if (flag) {
-               short idtype = GS(id->name);
-               if (idtype == ID_OB) {
+               short id_type = GS(id->name);
+               if (id_type == ID_OB) {
                        Object *object = (Object *)id;
                        object->recalc |= (flag & OB_RECALC_ALL);
                }
@@ -131,9 +138,11 @@ void id_tag_update_object_transform(Depsgraph *graph, IDDepsNode *id_node)
        ComponentDepsNode *transform_comp =
                id_node->find_component(DEG_NODE_TYPE_TRANSFORM);
        if (transform_comp == NULL) {
+#ifdef STRICT_COMPONENT_TAGGING
                DEG_ERROR_PRINTF("ERROR: Unable to find transform component for %s\n",
                                 id_node->id_orig->name);
                BLI_assert(!"This is not supposed to happen!");
+#endif
                return;
        }
        transform_comp->tag_update(graph);
@@ -142,9 +151,9 @@ void id_tag_update_object_transform(Depsgraph *graph, IDDepsNode *id_node)
 /* Tag corresponding to OB_RECALC_DATA. */
 void id_tag_update_object_data(Depsgraph *graph, IDDepsNode *id_node)
 {
-       const short idtype = GS(id_node->id_orig->name);
+       const short id_type = GS(id_node->id_orig->name);
        ComponentDepsNode *data_comp = NULL;
-       switch (idtype) {
+       switch (id_type) {
                case ID_OB:
                {
                        const Object *object = (Object *)id_node->id_orig;
@@ -153,6 +162,7 @@ void id_tag_update_object_data(Depsgraph *graph, IDDepsNode *id_node)
                                case OB_CURVE:
                                case OB_SURF:
                                case OB_FONT:
+                               case OB_MBALL:
                                        data_comp = id_node->find_component(DEG_NODE_TYPE_GEOMETRY);
                                        break;
                                case OB_ARMATURE:
@@ -165,14 +175,31 @@ void id_tag_update_object_data(Depsgraph *graph, IDDepsNode *id_node)
                case ID_ME:
                        data_comp = id_node->find_component(DEG_NODE_TYPE_GEOMETRY);
                        break;
+               case ID_PA:
+                       return;
        }
        if (data_comp == NULL) {
+#ifdef STRICT_COMPONENT_TAGGING
                DEG_ERROR_PRINTF("ERROR: Unable to find data component for %s\n",
                                 id_node->id_orig->name);
                BLI_assert(!"This is not supposed to happen!");
+#endif
                return;
        }
        data_comp->tag_update(graph);
+       /* Special legacy compatibility code, tag data ID for update when object
+        * is tagged for data update.
+        */
+       if (id_type == ID_OB) {
+               Object *object = (Object *)id_node->id_orig;
+               ID *data_id = (ID *)object->data;
+               if (data_id != NULL) {
+                       IDDepsNode *data_id_node = graph->find_id_node(data_id);
+                       BLI_assert(data_id_node != NULL);
+                       /* TODO(sergey): Do we want more granular tags here? */
+                       data_id_node->tag_update(graph);
+               }
+       }
 }
 
 /* Tag corresponding to OB_RECALC_TIME. */
@@ -190,6 +217,38 @@ void id_tag_update_object_time(Depsgraph *graph, IDDepsNode *id_node)
        /* TODO(sergey): More components to tag here? */
 }
 
+void id_tag_update_particle(Depsgraph *graph, IDDepsNode *id_node, int tag)
+{
+       ComponentDepsNode *particle_comp =
+               id_node->find_component(DEG_NODE_TYPE_PARAMETERS);
+       ParticleSettings *particle_settings = (ParticleSettings *)id_node->id_orig;
+       particle_settings->recalc |= (tag & PSYS_RECALC);
+       if (particle_comp == NULL) {
+#ifdef STRICT_COMPONENT_TAGGING
+               DEG_ERROR_PRINTF("ERROR: Unable to find particle component for %s\n",
+                                id_node->id_orig->name);
+               BLI_assert(!"This is not supposed to happen!");
+#endif
+               return;
+       }
+       particle_comp->tag_update(graph);
+}
+
+void id_tag_update_shading(Depsgraph *graph, IDDepsNode *id_node)
+{
+       ComponentDepsNode *shading_comp =
+               id_node->find_component(DEG_NODE_TYPE_SHADING);
+       if (shading_comp == NULL) {
+#ifdef STRICT_COMPONENT_TAGGING
+               DEG_ERROR_PRINTF("ERROR: Unable to find shading component for %s\n",
+                                id_node->id_orig->name);
+               BLI_assert(!"This is not supposed to happen!");
+#endif
+               return;
+       }
+       shading_comp->tag_update(graph);
+}
+
 #ifdef WITH_COPY_ON_WRITE
 /* Tag corresponding to DEG_TAG_COPY_ON_WRITE. */
 void id_tag_update_copy_on_write(Depsgraph *graph, IDDepsNode *id_node)
@@ -198,7 +257,6 @@ void id_tag_update_copy_on_write(Depsgraph *graph, IDDepsNode *id_node)
                id_node->find_component(DEG_NODE_TYPE_COPY_ON_WRITE);
        OperationDepsNode *cow_node = cow_comp->get_entry_operation();
        cow_node->tag_update(graph);
-       cow_node->flag |= DEPSOP_FLAG_SKIP_FLUSH;
 }
 #endif
 
@@ -226,12 +284,17 @@ void deg_graph_id_tag_update(Main *bmain, Depsgraph *graph, ID *id, int flag)
        if (flag & OB_RECALC_TIME) {
                id_tag_update_object_time(graph, id_node);
        }
+       if (flag & PSYS_RECALC) {
+               id_tag_update_particle(graph, id_node, flag);
+       }
+       if (flag & DEG_TAG_SHADING_UPDATE) {
+               id_tag_update_shading(graph, id_node);
+       }
 #ifdef WITH_COPY_ON_WRITE
        if (flag & DEG_TAG_COPY_ON_WRITE) {
                id_tag_update_copy_on_write(graph, id_node);
        }
 #endif
-       // node->tag_update(deg_graph);
 }
 
 void deg_id_tag_update(Main *bmain, ID *id, int flag)
@@ -248,6 +311,44 @@ void deg_id_tag_update(Main *bmain, ID *id, int flag)
        }
 }
 
+void deg_graph_on_visible_update(Main *bmain, Scene *scene, Depsgraph *graph)
+{
+       /* Make sure objects are up to date. */
+       GHASH_FOREACH_BEGIN(DEG::IDDepsNode *, id_node, graph->id_hash)
+       {
+               const short id_type = GS(id_node->id_orig->name);
+               if (id_type != ID_OB) {
+                       /* Ignore non-object nodes on visibility changes. */
+                       continue;
+               }
+               int flag = 0;
+               /* We only tag components which needs an update. Tagging everything is
+                * not a good idea because that might reset particles cache (or any
+                * other type of cache).
+                *
+                * TODO(sergey): Need to generalize this somehow.
+                */
+               if (id_type == ID_OB) {
+                       Object *object = (Object *)id_node->id_orig;
+                       flag |= OB_RECALC_OB;
+                       if (ELEM(object->type, OB_MESH,
+                                              OB_CURVE,
+                                              OB_SURF,
+                                              OB_FONT,
+                                              OB_MBALL))
+                       {
+                               flag |= OB_RECALC_DATA;
+                       }
+               }
+               deg_graph_id_tag_update(bmain, graph, id_node->id_orig, flag);
+       }
+       GHASH_FOREACH_END();
+       /* Make sure collection properties are up to date. */
+       IDDepsNode *scene_id_node = graph->find_id_node(&scene->id);
+       BLI_assert(scene_id_node != NULL);
+       scene_id_node->tag_update(graph);
+}
+
 }  /* namespace */
 
 }  // namespace DEG
@@ -269,9 +370,9 @@ void DEG_id_tag_update_ex(Main *bmain, ID *id, int flag)
 }
 
 /* Tag given ID type for update. */
-void DEG_id_type_tag(Main *bmain, short idtype)
+void DEG_id_type_tag(Main *bmain, short id_type)
 {
-       if (idtype == ID_NT) {
+       if (id_type == ID_NT) {
                /* Stupid workaround so parent datablocks of nested nodetree get looped
                 * over when we loop over tagged datablock types.
                 */
@@ -282,7 +383,7 @@ void DEG_id_type_tag(Main *bmain, short idtype)
                DEG_id_type_tag(bmain, ID_SCE);
        }
 
-       bmain->id_tag_update[BKE_idcode_to_index(idtype)] = 1;
+       bmain->id_tag_update[BKE_idcode_to_index(id_type)] = 1;
 }
 
 /* Recursively push updates out to all nodes dependent on this,
@@ -301,13 +402,8 @@ void DEG_ids_flush_tagged(Main *bmain, Scene *scene)
 /* Update dependency graph when visible scenes/layers changes. */
 void DEG_graph_on_visible_update(Main *bmain, Scene *scene)
 {
-       (void) bmain;
-       DEG::Depsgraph *graph = reinterpret_cast<DEG::Depsgraph *>(scene->depsgraph_legacy);
-       GHASH_FOREACH_BEGIN(DEG::IDDepsNode *, id_node, graph->id_hash)
-       {
-               id_node->tag_update(graph);
-       }
-       GHASH_FOREACH_END();
+       DEG::Depsgraph *graph = (DEG::Depsgraph *)scene->depsgraph_legacy;
+       DEG::deg_graph_on_visible_update(bmain, scene, graph);
 }
 
 void DEG_on_visible_update(Main *bmain, const bool UNUSED(do_time))