[Bf-blender-cvs] [fffd4522319] temp-modifier-rm-cddm: Ported Build modifier to use Mesh instead of DerivedMesh.

Sybren A. Stüvel noreply at git.blender.org
Tue Apr 24 11:52:40 CEST 2018


Commit: fffd45223197f4611765b400c447de8cb3e40cf2
Author: Sybren A. Stüvel
Date:   Fri Apr 20 16:20:59 2018 +0200
Branches: temp-modifier-rm-cddm
https://developer.blender.org/rBfffd45223197f4611765b400c447de8cb3e40cf2

Ported Build modifier to use Mesh instead of DerivedMesh.

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

M	source/blender/blenkernel/BKE_mesh.h
M	source/blender/blenkernel/intern/customdata.c
M	source/blender/blenkernel/intern/mesh.c
M	source/blender/blenkernel/intern/modifier.c
M	source/blender/modifiers/intern/MOD_build.c

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

diff --git a/source/blender/blenkernel/BKE_mesh.h b/source/blender/blenkernel/BKE_mesh.h
index 9f92248ec16..e0e6cfdc11e 100644
--- a/source/blender/blenkernel/BKE_mesh.h
+++ b/source/blender/blenkernel/BKE_mesh.h
@@ -93,6 +93,11 @@ struct Mesh *BKE_mesh_copy(struct Main *bmain, const struct Mesh *me);
 void BKE_mesh_update_customdata_pointers(struct Mesh *me, const bool do_ensure_tess_cd);
 void BKE_mesh_ensure_skin_customdata(struct Mesh *me);
 
+struct Mesh * BKE_mesh_from_template(
+        struct Mesh *me_src,
+        int numVerts, int numEdges, int numTessFaces,
+        int numLoops, int numPolys);
+
 bool BKE_mesh_ensure_edit_data(struct Mesh *me);
 bool BKE_mesh_clear_edit_data(struct Mesh *me);
 
diff --git a/source/blender/blenkernel/intern/customdata.c b/source/blender/blenkernel/intern/customdata.c
index 9c0ace2f654..40416d67c99 100644
--- a/source/blender/blenkernel/intern/customdata.c
+++ b/source/blender/blenkernel/intern/customdata.c
@@ -2206,6 +2206,10 @@ void CustomData_copy_data(const CustomData *source, CustomData *dest,
 {
 	int src_i, dest_i;
 
+//	printf("Copying custom data %p -> %p (totlayer = %d -> %d) source_index=%d  dest_index=%d  count=%d\n",
+//	       source, dest, source->totlayer, dest->totlayer,
+//	       source_index, dest_index, count);
+
 	/* copies a layer at a time */
 	dest_i = 0;
 	for (src_i = 0; src_i < source->totlayer; ++src_i) {
@@ -2222,6 +2226,8 @@ void CustomData_copy_data(const CustomData *source, CustomData *dest,
 
 		/* if we found a matching layer, copy the data */
 		if (dest->layers[dest_i].type == source->layers[src_i].type) {
+//			printf("    CustomData_copy_data_layer(%p, %p, %d, %d, %d, %d, %d)\n",
+//			       source, dest, src_i, dest_i, source_index, dest_index, count);
 			CustomData_copy_data_layer(source, dest, src_i, dest_i, source_index, dest_index, count);
 			
 			/* if there are multiple source & dest layers of the same type,
diff --git a/source/blender/blenkernel/intern/mesh.c b/source/blender/blenkernel/intern/mesh.c
index 8f771fa6ffe..b204d26ddb7 100644
--- a/source/blender/blenkernel/intern/mesh.c
+++ b/source/blender/blenkernel/intern/mesh.c
@@ -543,7 +543,6 @@ void BKE_mesh_init(Mesh *me)
 	me->size[0] = me->size[1] = me->size[2] = 1.0;
 	me->smoothresh = DEG2RADF(30);
 	me->texflag = ME_AUTOSPACE;
-//	me->emd = NULL;
 
 	/* disable because its slow on many GPU's, see [#37518] */
 #if 0
@@ -608,6 +607,53 @@ void BKE_mesh_copy_data(Main *bmain, Mesh *me_dst, const Mesh *me_src, const int
 	}
 }
 
+Mesh *BKE_mesh_from_template_ex(
+        Mesh *me_src,
+        int numVerts, int numEdges, int numTessFaces,
+        int numLoops, int numPolys,
+        CustomDataMask mask)
+{
+	const bool do_tessface = ((me_src->totface != 0) && (me_src->totpoly == 0)); /* only do tessface if we have no polys */
+
+	Mesh *me_dst = MEM_callocN(sizeof(struct Mesh), "Mesh");
+	BKE_mesh_init(me_dst);
+
+	me_dst->mat = MEM_dupallocN(me_src->mat);
+	me_dst->mselect = MEM_dupallocN(me_dst->mselect);
+//	me_dst->bb = MEM_dupallocN(me_dst->bb);
+
+	me_dst->totvert = numVerts;
+	me_dst->totedge = numEdges;
+	me_dst->totloop = numLoops;
+	me_dst->totpoly = numPolys;
+
+	CustomData_copy(&me_src->vdata, &me_dst->vdata, mask, CD_CALLOC, numVerts);
+	CustomData_copy(&me_src->edata, &me_dst->edata, mask, CD_CALLOC, numEdges);
+	CustomData_copy(&me_src->ldata, &me_dst->ldata, mask, CD_CALLOC, numLoops);
+	CustomData_copy(&me_src->pdata, &me_dst->pdata, mask, CD_CALLOC, numPolys);
+	if (do_tessface) {
+		CustomData_copy(&me_src->fdata, &me_dst->fdata, mask, CD_CALLOC, numTessFaces);
+	}
+	else {
+		mesh_tessface_clear_intern(me_dst, false);
+	}
+
+	BKE_mesh_update_customdata_pointers(me_dst, false);
+
+	return me_dst;
+}
+
+Mesh * BKE_mesh_from_template(Mesh *me_src,
+                              int numVerts, int numEdges, int numTessFaces,
+                              int numLoops, int numPolys)
+{
+	printf("BKE_mesh_from_template(%p, %d, %d, %d, %d, %d)\n", me_src, numVerts, numEdges, numTessFaces, numLoops, numPolys);
+	return BKE_mesh_from_template_ex(me_src,
+	                                 numVerts, numEdges, numTessFaces,
+	                                 numLoops, numPolys,
+	                                 CD_MASK_EVERYTHING);
+}
+
 Mesh *BKE_mesh_copy(Main *bmain, const Mesh *me)
 {
 	Mesh *me_copy;
diff --git a/source/blender/blenkernel/intern/modifier.c b/source/blender/blenkernel/intern/modifier.c
index 0ad80d491b0..1744c3f2990 100644
--- a/source/blender/blenkernel/intern/modifier.c
+++ b/source/blender/blenkernel/intern/modifier.c
@@ -1005,6 +1005,7 @@ void modifier_deformVerts_DM_deprecated(struct ModifierData *md, struct Depsgrap
 		mti->deformVerts_DM(md, depsgraph, ob, dm, vertexCos, numVerts, flag);
 	}
 	else {
+		/* TODO(sybren): deduplicate all the copies of this code in this file. */
 		struct Mesh *mesh = ob->data;
 		BLI_assert(DEG_depsgraph_use_copy_on_write());
 		BLI_assert(mesh->id.tag & LIB_TAG_COPY_ON_WRITE); /* This should be a CoW mesh */
@@ -1097,20 +1098,25 @@ struct DerivedMesh *modifier_applyModifier_DM_deprecated(struct ModifierData *md
 		return mti->applyModifier_DM(md, depsgraph, ob, dm, flag);
 	}
 	else {
-		struct Mesh mesh;
-		BKE_mesh_init(&mesh);
-
-		DM_to_mesh(dm, &mesh, ob, CD_MASK_EVERYTHING, false);
+		/* TODO(sybren): deduplicate all the copies of this code in this file. */
+		struct Mesh *mesh = ob->data;
+		BLI_assert(DEG_depsgraph_use_copy_on_write());
+		BLI_assert(mesh->id.tag & LIB_TAG_COPY_ON_WRITE); /* This should be a CoW mesh */
+//		if (dm != NULL) {
+////			BKE_mesh_free(mesh);
+//			printf("Converting DM_to_mesh(dm=%p, mesh=%s=%p)\n", dm, mesh->id.name, mesh);
+//			DM_to_mesh(dm, mesh, ob, CD_MASK_EVERYTHING, false);
+//		}
 
-		struct Mesh *new_mesh = mti->applyModifier(md, depsgraph, ob, &mesh, flag);
+		struct Mesh *new_mesh = mti->applyModifier(md, depsgraph, ob, mesh, flag);
+		printf("    created new mesh %s=%p\n", new_mesh->id.name, new_mesh);
 
 		DerivedMesh *ndm = CDDM_from_mesh(new_mesh);
 
-		if(new_mesh != &mesh) {
-			BKE_mesh_free(&mesh);
-
-			/* XXX free new_mesh? */
-		}
+//		if(new_mesh != mesh) {
+//			BKE_mesh_free(new_mesh);
+//			MEM_freeN(new_mesh);
+//		}
 
 		return ndm;
 	}
diff --git a/source/blender/modifiers/intern/MOD_build.c b/source/blender/modifiers/intern/MOD_build.c
index 16b40f29cc1..42e65f01363 100644
--- a/source/blender/modifiers/intern/MOD_build.c
+++ b/source/blender/modifiers/intern/MOD_build.c
@@ -41,8 +41,13 @@
 #include "BLI_ghash.h"
 
 #include "DNA_meshdata_types.h"
+#include "DNA_mesh_types.h"
+#include "DNA_object_types.h"
+
+#include "DEG_depsgraph_query.h"
 
 #include "BKE_cdderivedmesh.h"
+#include "BKE_mesh.h"
 #include "BKE_modifier.h"
 #include "BKE_particle.h"
 #include "BKE_scene.h"
@@ -75,59 +80,57 @@ static bool dependsOnTime(ModifierData *UNUSED(md))
 	return true;
 }
 
-static DerivedMesh *applyModifier(ModifierData *md, struct Depsgraph *UNUSED(depsgraph),
-                                  Object *UNUSED(ob), DerivedMesh *derivedData,
-                                  ModifierApplyFlag UNUSED(flag))
+static Mesh *applyModifier(ModifierData *md, struct Depsgraph *depsgraph,
+                           Object *UNUSED(ob), struct Mesh *mesh,
+                           ModifierApplyFlag UNUSED(flag))
 {
-	DerivedMesh *dm = derivedData;
-	DerivedMesh *result;
+	Mesh *result; /* TODO(sybren): remove and replace with direct modification of mesh? */
 	BuildModifierData *bmd = (BuildModifierData *) md;
-	int i, j, k;
-	int numFaces_dst, numEdges_dst, numLoops_dst = 0;
-	int *vertMap, *edgeMap, *faceMap;
+	unsigned int i, j, k;
+	unsigned int numFaces_dst, numEdges_dst, numLoops_dst = 0;
+	unsigned int *vertMap, *edgeMap, *faceMap;
 	float frac;
 	MPoly *mpoly_dst;
 	MLoop *ml_dst, *ml_src /*, *mloop_dst */;
 	GHashIterator gh_iter;
 	/* maps vert indices in old mesh to indices in new mesh */
 	GHash *vertHash = BLI_ghash_int_new("build ve apply gh");
-	/* maps edge indices in new mesh to indices in old mesh */
+	/* maps edge indices in old mesh to indices in new mesh */
 	GHash *edgeHash = BLI_ghash_int_new("build ed apply gh");
-	GHash *edgeHash2 = BLI_ghash_int_new("build ed apply gh");
-
-	const int numVert_src = dm->getNumVerts(dm);
-	const int numEdge_src = dm->getNumEdges(dm);
-	const int numPoly_src = dm->getNumPolys(dm);
-	MPoly *mpoly_src = dm->getPolyArray(dm);
-	MLoop *mloop_src = dm->getLoopArray(dm);
-	MEdge *medge_src = dm->getEdgeArray(dm);
-	MVert *mvert_src = dm->getVertArray(dm);
 
+	const unsigned int numVert_src = mesh->totvert;
+	const unsigned int numEdge_src = mesh->totedge;
+	const unsigned int numPoly_src = mesh->totpoly;
+	MPoly *mpoly_src = mesh->mpoly;
+	MLoop *mloop_src = mesh->mloop;
+	MEdge *medge_src = mesh->medge;
+	MVert *mvert_src = mesh->mvert;
 
 	vertMap = MEM_malloc_arrayN(numVert_src, sizeof(*vertMap), "build modifier vertMap");
 	edgeMap = MEM_malloc_arrayN(numEdge_src, sizeof(*edgeMap), "build modifier edgeMap");
 	faceMap = MEM_malloc_arrayN(numPoly_src, sizeof(*faceMap), "build modifier faceMap");
 
-	range_vn_i(vertMap, numVert_src, 0);
-	range_vn_i(edgeMap, numEdge_src, 0);
-	range_vn_i(faceMap, numPoly_src, 0);
+	range_vn_u(vertMap, numVert_src, 0);
+	range_vn_u(edgeMap, numEdge_src, 0);
+	range_vn_u(faceMap, numPoly_src, 0);
 
-	frac = (BKE_scene_frame_get(md->scene) - bmd->start) / bmd->length;
+	struct Scene *scene = DEG_get_input_scene(depsgraph);
+	frac = (BKE_scene_frame_get(scene) - bmd->start) / bmd->length;
 	CLAMP(frac, 0.0f, 1.0f);
-	
 	if (bmd->flag & MOD_BUILD_FLAG_REVERSE) {
 		frac = 1.0f - frac;
 	}
-	
+
 	numFaces_dst = numPoly_src * frac;
 	numEdges_dst = numEdge_src * frac;
 
+	BKE_mesh_tessface_ensure(mesh);
+
 	/* if there's at least one face, build based on faces */
 	if (numFaces_dst) {
 		MPoly *mpoly, *mp;
 		MLoop *ml, *mloop;
-		MEdge *medge;
-		uintptr_t hash_num, hash_num_alt;
+		unsigned int new_idx;
 		
 		if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
 			BLI_array_randomize(faceMap, sizeof(*faceMap),
@@ -139,44 +142,42 @@ static DerivedMesh *applyModifier(ModifierData *md, struct Depsgraph *UNUSED(dep
 		 */
 		mpoly = mpoly_src;
 		mloop = mloop_src;
-		hash_num = 0;
+		new_idx = 0;
 		for (i =

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list