[Bf-blender-cvs] [8fb0b9aebbd] master: Subdiv: Enable topology cache in edit mode

Sergey Sharybin noreply at git.blender.org
Mon Mar 18 17:16:08 CET 2019


Commit: 8fb0b9aebbd409216dec77c4b1bf2a2fbba80698
Author: Sergey Sharybin
Date:   Mon Mar 18 15:56:16 2019 +0100
Branches: master
https://developer.blender.org/rB8fb0b9aebbd409216dec77c4b1bf2a2fbba80698

Subdiv: Enable topology cache in edit mode

The general idea of this change is to have a runtime data pointer
in the ModifierData, so it can be preserved through copy-on-write
updates by the dependency graph.

This is where subdivision surface modifier can store its topology
cache, so it is not getting trashed on every copy-on-write which
is happening when moving a vertex.

Similar mechanism should be used by multiresolution, dynamic paint
and some other modifiers which cache evaluated data.

This fixes T61746.

Thing to keep in mind, that there are more reports about slow
subdivision surface in the tracker, but that boils down to the
fact that those have a lot of extraordinary vertices, and hence
a lot slower to evaluated topology.
Other thing is, this speeds up oeprations which doesn't change
topology (i.e. moving vertices).

Reviewers: brecht

Reviewed By: brecht

Maniphest Tasks: T61746

Differential Revision: https://developer.blender.org/D4541

===================================================================

M	source/blender/blenkernel/BKE_modifier.h
M	source/blender/blenloader/intern/readfile.c
M	source/blender/depsgraph/intern/depsgraph_type.h
M	source/blender/depsgraph/intern/eval/deg_eval_copy_on_write.cc
M	source/blender/makesdna/DNA_modifier_types.h
M	source/blender/modifiers/intern/MOD_armature.c
M	source/blender/modifiers/intern/MOD_array.c
M	source/blender/modifiers/intern/MOD_bevel.c
M	source/blender/modifiers/intern/MOD_boolean.c
M	source/blender/modifiers/intern/MOD_build.c
M	source/blender/modifiers/intern/MOD_cast.c
M	source/blender/modifiers/intern/MOD_cloth.c
M	source/blender/modifiers/intern/MOD_collision.c
M	source/blender/modifiers/intern/MOD_correctivesmooth.c
M	source/blender/modifiers/intern/MOD_curve.c
M	source/blender/modifiers/intern/MOD_datatransfer.c
M	source/blender/modifiers/intern/MOD_decimate.c
M	source/blender/modifiers/intern/MOD_displace.c
M	source/blender/modifiers/intern/MOD_dynamicpaint.c
M	source/blender/modifiers/intern/MOD_edgesplit.c
M	source/blender/modifiers/intern/MOD_explode.c
M	source/blender/modifiers/intern/MOD_fluidsim.c
M	source/blender/modifiers/intern/MOD_hook.c
M	source/blender/modifiers/intern/MOD_laplaciandeform.c
M	source/blender/modifiers/intern/MOD_laplaciansmooth.c
M	source/blender/modifiers/intern/MOD_lattice.c
M	source/blender/modifiers/intern/MOD_mask.c
M	source/blender/modifiers/intern/MOD_meshcache.c
M	source/blender/modifiers/intern/MOD_meshdeform.c
M	source/blender/modifiers/intern/MOD_meshsequencecache.c
M	source/blender/modifiers/intern/MOD_mirror.c
M	source/blender/modifiers/intern/MOD_multires.c
M	source/blender/modifiers/intern/MOD_none.c
M	source/blender/modifiers/intern/MOD_normal_edit.c
M	source/blender/modifiers/intern/MOD_ocean.c
M	source/blender/modifiers/intern/MOD_particleinstance.c
M	source/blender/modifiers/intern/MOD_particlesystem.c
M	source/blender/modifiers/intern/MOD_remesh.c
M	source/blender/modifiers/intern/MOD_screw.c
M	source/blender/modifiers/intern/MOD_shapekey.c
M	source/blender/modifiers/intern/MOD_shrinkwrap.c
M	source/blender/modifiers/intern/MOD_simpledeform.c
M	source/blender/modifiers/intern/MOD_skin.c
M	source/blender/modifiers/intern/MOD_smoke.c
M	source/blender/modifiers/intern/MOD_smooth.c
M	source/blender/modifiers/intern/MOD_softbody.c
M	source/blender/modifiers/intern/MOD_solidify.c
M	source/blender/modifiers/intern/MOD_subsurf.c
M	source/blender/modifiers/intern/MOD_surface.c
M	source/blender/modifiers/intern/MOD_surfacedeform.c
M	source/blender/modifiers/intern/MOD_triangulate.c
M	source/blender/modifiers/intern/MOD_uvproject.c
M	source/blender/modifiers/intern/MOD_uvwarp.c
M	source/blender/modifiers/intern/MOD_warp.c
M	source/blender/modifiers/intern/MOD_wave.c
M	source/blender/modifiers/intern/MOD_weighted_normal.c
M	source/blender/modifiers/intern/MOD_weightvgedit.c
M	source/blender/modifiers/intern/MOD_weightvgmix.c
M	source/blender/modifiers/intern/MOD_weightvgproximity.c
M	source/blender/modifiers/intern/MOD_wireframe.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_modifier.h b/source/blender/blenkernel/BKE_modifier.h
index 1a1e510b9e1..22782b675f5 100644
--- a/source/blender/blenkernel/BKE_modifier.h
+++ b/source/blender/blenkernel/BKE_modifier.h
@@ -233,6 +233,8 @@ typedef struct ModifierTypeInfo {
 	/* Free internal modifier data variables, this function should
 	 * not free the md variable itself.
 	 *
+	 * This function is responsible for freeing the runtime data as well.
+	 *
 	 * This function is optional.
 	 */
 	void (*freeData)(struct ModifierData *md);
@@ -301,6 +303,18 @@ typedef struct ModifierTypeInfo {
 	 */
 	void (*foreachTexLink)(struct ModifierData *md, struct Object *ob,
 	                       TexWalkFunc walk, void *userData);
+
+	/* Free given runtime data.
+	 *
+	 * This data is coming from a modifier of the corresponding type, but actual
+	 * modifier data is not known here.
+	 *
+	 * Notes:
+	 *  - The data itself is to be de-allocated as well.
+	 *  - This calback is allowed to receive NULL pointer as a data, so it's
+	 *    more like "ensure the data is freed".
+	 */
+	void (*freeRuntimeData)(void *runtime_data);
 } ModifierTypeInfo;
 
 /* Initialize modifier's global data (type info and some common global storages). */
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 9a886f494cf..93ef7fe5b73 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -5458,6 +5458,7 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
 
 	for (md = lb->first; md; md = md->next) {
 		md->error = NULL;
+		md->runtime = NULL;
 
 		/* if modifiers disappear, or for upward compatibility */
 		if (NULL == modifierType_getInfo(md->type))
@@ -5467,7 +5468,6 @@ static void direct_link_modifiers(FileData *fd, ListBase *lb)
 			SubsurfModifierData *smd = (SubsurfModifierData *)md;
 
 			smd->emCache = smd->mCache = NULL;
-			smd->subdiv = NULL;
 		}
 		else if (md->type == eModifierType_Armature) {
 			ArmatureModifierData *amd = (ArmatureModifierData *)md;
diff --git a/source/blender/depsgraph/intern/depsgraph_type.h b/source/blender/depsgraph/intern/depsgraph_type.h
index d34b6d724cb..d96c621f4b2 100644
--- a/source/blender/depsgraph/intern/depsgraph_type.h
+++ b/source/blender/depsgraph/intern/depsgraph_type.h
@@ -36,6 +36,8 @@
 #include <string>
 #include <vector>
 #include <algorithm>
+#include <map>
+#include <unordered_map>
 
 struct Depsgraph;
 
@@ -46,10 +48,14 @@ namespace DEG {
 /* Commonly used types. */
 using std::string;
 using std::vector;
+using std::map;
+using std::pair;
+using std::unordered_map;
 
 /* Commonly used functions. */
 using std::max;
 using std::to_string;
+using std::make_pair;
 
 /* Function bindings. */
 using std::function;
diff --git a/source/blender/depsgraph/intern/eval/deg_eval_copy_on_write.cc b/source/blender/depsgraph/intern/eval/deg_eval_copy_on_write.cc
index cae84c3e535..5e8516d744c 100644
--- a/source/blender/depsgraph/intern/eval/deg_eval_copy_on_write.cc
+++ b/source/blender/depsgraph/intern/eval/deg_eval_copy_on_write.cc
@@ -83,6 +83,7 @@ extern "C" {
 #include "BKE_armature.h"
 #include "BKE_editmesh.h"
 #include "BKE_library_query.h"
+#include "BKE_modifier.h"
 #include "BKE_object.h"
 }
 
@@ -846,43 +847,119 @@ ID *deg_expand_copy_on_write_datablock(const Depsgraph *depsgraph,
 	                                          create_placeholders);
 }
 
+namespace {
+
+/* Identifier used to match modifiers to backup/restore their runtime data.
+ * Identification is happening using original modifier data pointer and the
+ * modifier type.
+ * It is not enough to only pointer, since it's possible to have a situation
+ * when modifier is removed and a new one added, and due to memory allocation
+ * policy they might have same pointer.
+ * By adding type into matching we are at least ensuring that modifier will not
+ * try to interpret runtime data created by another modifier type. */
+class ModifierDataBackupID {
+public:
+	ModifierDataBackupID() : ModifierDataBackupID(NULL, eModifierType_None)
+	{
+	}
+
+	ModifierDataBackupID(ModifierData *modifier_data, ModifierType type)
+	        : modifier_data(modifier_data),
+	          type(type)
+	{
+	}
+
+	bool operator <(const ModifierDataBackupID& other) const {
+		if (modifier_data < other.modifier_data) {
+			return true;
+		}
+		if (modifier_data == other.modifier_data) {
+			return static_cast<int>(type) < static_cast<int>(other.type);
+		}
+		return false;
+	}
+
+	ModifierData *modifier_data;
+	ModifierType type;
+};
+
+/* Storage for backed up runtime modifier data. */
+typedef map<ModifierDataBackupID, void*> ModifierRuntimeDataBackup;
+
 struct ObjectRuntimeBackup {
+	ObjectRuntimeBackup()
+	        : base_flag(0),
+	          base_local_view_bits(0)
+	{
+		/* TODO(sergey): Use something like BKE_object_runtime_reset(). */
+		memset(&runtime, 0, sizeof(runtime));
+	}
+
+	/* Make a backup of object's evaluation runtime data, additionally
+	 * make object to be safe for free without invalidating backed up
+	 * pointers. */
+	void init_from_object(Object *object);
+	void backup_modifier_runtime_data(Object *object);
+
+	/* Restore all fields to the given object. */
+	void restore_to_object(Object *object);
+	/* NOTE: Will free all runtime data which has not been restored. */
+	void restore_modifier_runtime_data(Object *object);
+
 	Object_Runtime runtime;
 	short base_flag;
 	unsigned short base_local_view_bits;
+	ModifierRuntimeDataBackup modifier_runtime_data;
 };
 
-/* Make a backup of object's evaluation runtime data, additionally
- * make object to be safe for free without invalidating backed up
- * pointers. */
-static void deg_backup_object_runtime(
-        Object *object,
-        ObjectRuntimeBackup *object_runtime_backup)
+void ObjectRuntimeBackup::init_from_object(Object *object)
 {
 	/* Store evaluated mesh and curve_cache, and make sure we don't free it. */
 	Mesh *mesh_eval = object->runtime.mesh_eval;
-	object_runtime_backup->runtime = object->runtime;
+	runtime = object->runtime;
 	BKE_object_runtime_reset(object);
-	/* Keep bbox (for now at least...). */
-	object->runtime.bb = object_runtime_backup->runtime.bb;
+	/* Keep bbox (for now at least). */
+	object->runtime.bb = runtime.bb;
 	/* Object update will override actual object->data to an evaluated version.
 	 * Need to make sure we don't have data set to evaluated one before free
 	 * anything. */
 	if (mesh_eval != NULL && object->data == mesh_eval) {
-		object->data = object_runtime_backup->runtime.mesh_orig;
+		object->data = runtime.mesh_orig;
 	}
 	/* Make a backup of base flags. */
-	object_runtime_backup->base_flag = object->base_flag;
-	object_runtime_backup->base_local_view_bits = object->base_local_view_bits;
+	base_flag = object->base_flag;
+	base_local_view_bits = object->base_local_view_bits;
+	/* Backup tuntime data of all modifiers. */
+	backup_modifier_runtime_data(object);
 }
 
-static void deg_restore_object_runtime(
-        Object *object,
-        const ObjectRuntimeBackup *object_runtime_backup)
+inline ModifierDataBackupID create_modifier_data_id(
+        const ModifierData* modifier_data)
+{
+	return ModifierDataBackupID(modifier_data->orig_modifier_data,
+	                            static_cast<ModifierType>(modifier_data->type));
+}
+
+void ObjectRuntimeBackup::backup_modifier_runtime_data(Object *object)
+{
+	LISTBASE_FOREACH(ModifierData *, modifier_data, &object->modifiers) {
+		if (modifier_data->runtime == NULL) {
+			continue;
+		}
+		BLI_assert(modifier_data->orig_modifier_data != NULL);
+		ModifierDataBackupID modifier_data_id =
+		        create_modifier_data_id(modifier_data);
+		modifier_runtime_data.insert(
+		        make_pair(modifier_data_id, modifier_data->runtime));
+		modifier_data->runtime = NULL;
+	}
+}
+
+void ObjectRuntimeBackup::restore_to_object(Object *object)
 {
 	Mesh *mesh_orig = object->runtime.mesh_orig;
 	BoundBox *bb = object->runtime.bb;
-	object->runtime = object_runtime_backup->runtime;
+	object->runtime = runtime;
 	object->runtime.mesh_orig = mesh_orig;
 	object->runtime.bb = bb;
 	if (object->type == OB_MESH && object->runtime.mesh_eval != NULL) {
@@ -909,60 +986,108 @@ static void deg_restore_object_runtime(
 			mesh_eval->edit_mesh = mesh_orig->edit_mesh;
 		}
 	}
-	object->base_flag = object_runtime_backup->base_flag;
-	object->base_local_view_bits = object_runtime_backup->base_local_view_bits;
+	object->base_flag = base_flag;
+	object->base_local_view_bits = base_local_view_bits;
+	/* Restore modifier's runtime data.
+	 * NOTE: Data of unused modifiers will be freed there. */
+	restore_modifier_runtime_data(object);
+}
+
+void ObjectRuntimeBackup::restore_modifier_runtime_data(Object *object) {
+	LISTBASE_FOREACH(ModifierData *, modifier_data, &object->modifiers) {
+		BLI_assert(modifier_data->orig_modifier_data != NULL);
+		ModifierDataBackupID modifier_data_id =
+		        create_modifier_data_id(modifier_data);
+		ModifierRuntimeDataBackup::iterator runtime_data_iterator =
+		        modifier_runtime_data.find(modifier_data_id);
+		if (runtime_data_iterator != modifier_runtime_data.end()) {
+			modifier_data->runtime = runtime_data_iterator->second;
+			runtime_data_iterator->second = NULL;
+		}
+	}
+	for (ModifierRuntimeDataBackup::value_type value : modifier_runtime_data) {
+		const ModifierDataBackupID modifier_data_id = value.first;
+		void *runtime = value.second;
+		if (value.second == NULL) {
+			continue;
+		}
+		const ModifierTypeInfo *modifier_type_info =
+		        modifierType_getInfo(modifier_data_id.type);
+		BLI_assert(modifier_type_info != NULL);
+		modifier_type_info->freeRuntimeData(runtime);
+	}
+}
+
+class RuntimeBackup {
+public:
+	RuntimeBackup() : drawdata_ptr(NULL) {
+		drawdata_backup.first = drawdata_backup.last = NULL;
+	}
+
+	/* NOTE: Will reset all runbtime fields which has been backed up to NULL. */
+	void init_from_id(ID *id);
+
+	/* Restore fields to the given ID. */
+	void restore_to_id(ID *id);
+
+	ObjectRuntimeBackup object_backup;
+	DrawDataList drawdata_backup;
+	DrawDataList *drawdata_ptr;
+};
+
+void RuntimeBackup::init_from_id(ID *id)
+{
+	if (!check_datablock_expand

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list