[Bf-blender-cvs] [f7c37c8344b] blender2.8: Modifiers: Port mesh_calc_modifiers from DerivedMesh to Mesh

Mai Lavelle noreply at git.blender.org
Wed May 16 12:08:33 CEST 2018


Commit: f7c37c8344b8eceaa6b1b8bd8c50daf4e2f0e787
Author: Mai Lavelle
Date:   Fri May 11 03:53:52 2018 -0400
Branches: blender2.8
https://developer.blender.org/rBf7c37c8344b8eceaa6b1b8bd8c50daf4e2f0e787

Modifiers: Port mesh_calc_modifiers from DerivedMesh to Mesh

This ports the main modifier eval loop.

A few things haven't been ported yet: tessface and shapekey related code. As
far as I can tell, shapekey code was never entered into.

https://developer.blender.org/D3342

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

M	source/blender/blenkernel/intern/DerivedMesh.c

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

diff --git a/source/blender/blenkernel/intern/DerivedMesh.c b/source/blender/blenkernel/intern/DerivedMesh.c
index 769c109cc31..c58d946cc99 100644
--- a/source/blender/blenkernel/intern/DerivedMesh.c
+++ b/source/blender/blenkernel/intern/DerivedMesh.c
@@ -84,8 +84,10 @@
 
 #ifdef USE_MODIFIER_VALIDATE
 #  define ASSERT_IS_VALID_DM(dm) (BLI_assert((dm == NULL) || (DM_is_valid(dm) == true)))
+#  define ASSERT_IS_VALID_MESH(mesh) (BLI_assert((mesh == NULL) || (BKE_mesh_is_valid(mesh) == true)))
 #else
 #  define ASSERT_IS_VALID_DM(dm)
+#  define ASSERT_IS_VALID_MESH(mesh)
 #endif
 
 
@@ -95,6 +97,8 @@ static ThreadRWMutex loops_cache_lock = PTHREAD_RWLOCK_INITIALIZER;
 static void add_shapekey_layers(DerivedMesh *dm, Mesh *me, Object *ob);
 static void shapekey_layers_to_keyblocks(DerivedMesh *dm, Mesh *me, int actshape_uid);
 
+static void mesh_init_origspace(Mesh *mesh);
+
 
 /* -------------------------------------------------------------------- */
 
@@ -891,6 +895,20 @@ void DM_set_only_copy(DerivedMesh *dm, CustomDataMask mask)
 #endif
 }
 
+void mesh_set_only_copy(Mesh *mesh, CustomDataMask mask)
+{
+	CustomData_set_only_copy(&mesh->vdata, mask);
+	CustomData_set_only_copy(&mesh->edata, mask);
+	CustomData_set_only_copy(&mesh->fdata, mask);
+	/* this wasn't in 2.63 and is disabled for 2.64 because it gives problems with
+	 * weight paint mode when there are modifiers applied, needs further investigation,
+	 * see replies to r50969, Campbell */
+#if 0
+	CustomData_set_only_copy(&mesh->ldata, mask);
+	CustomData_set_only_copy(&mesh->pdata, mask);
+#endif
+}
+
 void DM_add_vert_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
 {
 	CustomData_add_layer(&dm->vertData, type, alloctype, layer, dm->numVertData);
@@ -1265,6 +1283,30 @@ static DerivedMesh *create_orco_dm(Object *ob, Mesh *me, BMEditMesh *em, int lay
 	return dm;
 }
 
+static Mesh *create_orco_mesh(Object *ob, Mesh *me, BMEditMesh *em, int layer)
+{
+	Mesh *mesh;
+	float (*orco)[3];
+	int free;
+
+	if (em) {
+		mesh = BKE_bmesh_to_mesh_nomain(em->bm, &(struct BMeshToMeshParams){0});
+	}
+	else {
+		BKE_id_copy_ex(NULL, &me->id, (ID**)&mesh,
+			LIB_ID_CREATE_NO_MAIN | LIB_ID_CREATE_NO_USER_REFCOUNT | LIB_ID_CREATE_NO_DEG_TAG, false);
+	}
+
+	orco = get_orco_coords_dm(ob, em, layer, &free);
+
+	if (orco) {
+		BKE_mesh_apply_vert_coords(mesh, orco);
+		if (free) MEM_freeN(orco);
+	}
+
+	return mesh;
+}
+
 static void add_orco_dm(
         Object *ob, BMEditMesh *em, DerivedMesh *dm,
         DerivedMesh *orcodm, int layer)
@@ -1303,6 +1345,48 @@ static void add_orco_dm(
 	}
 }
 
+static void add_orco_mesh(
+        Object *ob, BMEditMesh *em, Mesh *mesh,
+        Mesh *orco_mesh, int layer)
+{
+	float (*orco)[3], (*layerorco)[3];
+	int totvert, free;
+
+	totvert = mesh->totvert;
+
+	if (orco_mesh) {
+		free = 1;
+
+		if (orco_mesh->totvert == totvert) {
+			orco = BKE_mesh_vertexCos_get(orco_mesh, NULL);
+		}
+		else {
+			orco = BKE_mesh_vertexCos_get(mesh, NULL);
+		}
+	}
+	else {
+		/* TODO(sybren): totvert should potentially change here, as ob->data
+		 * or em may have a different number of vertices than dm. */
+		orco = get_orco_coords_dm(ob, em, layer, &free);
+	}
+
+	if (orco) {
+		if (layer == CD_ORCO) {
+			BKE_mesh_orco_verts_transform(ob->data, orco, totvert, 0);
+		}
+
+		if (!(layerorco = CustomData_get_layer(&mesh->vdata, layer))) {
+			CustomData_add_layer(&mesh->vdata, layer, CD_CALLOC, NULL, mesh->totvert);
+			BKE_mesh_update_customdata_pointers(mesh, false);
+
+			layerorco = CustomData_get_layer(&mesh->vdata, layer);
+		}
+
+		memcpy(layerorco, orco, sizeof(float) * 3 * totvert);
+		if (free) MEM_freeN(orco);
+	}
+}
+
 /* weight paint colors */
 
 /* Something of a hack, at the moment deal with weightpaint
@@ -1528,6 +1612,81 @@ static void calc_weightpaint_vert_array(
 	}
 }
 
+static void calc_weightpaint_vert_array_mesh(
+        Object *ob, Mesh *mesh, int const draw_flag, DMWeightColorInfo *dm_wcinfo,
+        unsigned char (*r_wtcol_v)[4])
+{
+	BMEditMesh *em = BKE_editmesh_from_object(ob);
+	const int numVerts = mesh->totvert;
+
+	if ((ob->actdef != 0) &&
+	    (CustomData_has_layer(em ? &em->bm->vdata : &mesh->vdata, CD_MDEFORMVERT)))
+	{
+		unsigned char (*wc)[4] = r_wtcol_v;
+		unsigned int i;
+
+		/* variables for multipaint */
+		const int defbase_tot = BLI_listbase_count(&ob->defbase);
+		const int defbase_act = ob->actdef - 1;
+
+		int defbase_sel_tot = 0;
+		bool *defbase_sel = NULL;
+
+		if (draw_flag & CALC_WP_MULTIPAINT) {
+			defbase_sel = BKE_object_defgroup_selected_get(ob, defbase_tot, &defbase_sel_tot);
+
+			if (defbase_sel_tot > 1 && (draw_flag & CALC_WP_MIRROR_X)) {
+				BKE_object_defgroup_mirror_selection(ob, defbase_tot, defbase_sel, defbase_sel, &defbase_sel_tot);
+			}
+		}
+
+		/* editmesh won't have deform verts unless modifiers require it,
+		 * avoid having to create an array of deform-verts only for drawing
+		 * by reading from the bmesh directly. */
+		if (em) {
+			BMIter iter;
+			BMVert *eve;
+			const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
+			BLI_assert(cd_dvert_offset != -1);
+
+			BM_ITER_MESH_INDEX (eve, &iter, em->bm, BM_VERTS_OF_MESH, i) {
+				const MDeformVert *dv = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
+				calc_weightpaint_vert_color(
+				        (unsigned char *)wc, dv, dm_wcinfo,
+				        defbase_tot, defbase_act, defbase_sel, defbase_sel_tot, draw_flag);
+				wc++;
+			}
+		}
+		else {
+			const MDeformVert *dv = CustomData_get_layer(&mesh->vdata, CD_MDEFORMVERT);
+			for (i = numVerts; i != 0; i--, wc++, dv++) {
+				calc_weightpaint_vert_color(
+				        (unsigned char *)wc, dv, dm_wcinfo,
+				        defbase_tot, defbase_act, defbase_sel, defbase_sel_tot, draw_flag);
+			}
+		}
+
+		if (defbase_sel) {
+			MEM_freeN(defbase_sel);
+		}
+	}
+	else {
+		unsigned char col[4];
+		if ((ob->actdef == 0) && !BLI_listbase_is_empty(&ob->defbase)) {
+			/* color-code for missing data (full brightness isn't easy on the eye). */
+			ARRAY_SET_ITEMS(col, 0xa0, 0, 0xa0, 0xff);
+		}
+		else if (draw_flag & (CALC_WP_GROUP_USER_ACTIVE | CALC_WP_GROUP_USER_ALL)) {
+			copy_v3_v3_char((char *)col, dm_wcinfo->alert_color);
+			col[3] = 255;
+		}
+		else {
+			weightpaint_color(col, dm_wcinfo, 0.0f);
+		}
+		copy_vn_i((int *)r_wtcol_v, numVerts, *((int *)col));
+	}
+}
+
 /** return an array of vertex weight colors from given weights, caller must free.
  *
  * \note that we could save some memory and allocate RGB only but then we'd need to
@@ -1620,6 +1779,80 @@ void DM_update_weight_mcol(
 	}
 }
 
+void mesh_update_weight_mcol(
+        Object *ob, Mesh *mesh, int const draw_flag,
+        float *weights, int num, const int *indices)
+{
+	BMEditMesh *em = BKE_editmesh_from_object(ob);
+	unsigned char (*wtcol_v)[4];
+	int numVerts = mesh->totvert;
+	int i;
+
+	if (em) {
+		BKE_editmesh_color_ensure(em, BM_VERT);
+		wtcol_v = em->derivedVertColor;
+	}
+	else {
+		wtcol_v = MEM_malloc_arrayN(numVerts, sizeof(*wtcol_v), __func__);
+	}
+
+	/* Weights are given by caller. */
+	if (weights) {
+		float *w = weights;
+		/* If indices is not NULL, it means we do not have weights for all vertices,
+		 * so we must create them (and set them to zero)... */
+		if (indices) {
+			w = MEM_calloc_arrayN(numVerts, sizeof(float), "Temp weight array DM_update_weight_mcol");
+			i = num;
+			while (i--)
+				w[indices[i]] = weights[i];
+		}
+
+		/* Convert float weights to colors. */
+		calc_colors_from_weights_array(numVerts, w, wtcol_v);
+
+		if (indices)
+			MEM_freeN(w);
+	}
+	else {
+		/* No weights given, take them from active vgroup(s). */
+		calc_weightpaint_vert_array_mesh(ob, mesh, draw_flag, &G_dm_wcinfo, wtcol_v);
+	}
+
+	if (em) {
+		/* editmesh draw function checks specifically for this */
+	}
+	else {
+		const int totpoly = mesh->totpoly;
+		const int totloop = mesh->totloop;
+		unsigned char(*wtcol_l)[4] = CustomData_get_layer(&mesh->ldata, CD_PREVIEW_MLOOPCOL);
+		MLoop *mloop = mesh->mloop, *ml;
+		MPoly *mp = mesh->mpoly;
+		int l_index;
+		int j;
+
+		/* now add to loops, so the data can be passed through the modifier stack
+		 * If no CD_PREVIEW_MLOOPCOL existed yet, we have to add a new one! */
+		if (!wtcol_l) {
+			wtcol_l = MEM_malloc_arrayN(totloop, sizeof(*wtcol_l), __func__);
+			CustomData_add_layer(&mesh->ldata, CD_PREVIEW_MLOOPCOL, CD_ASSIGN, wtcol_l, totloop);
+		}
+
+		l_index = 0;
+		for (i = 0; i < totpoly; i++, mp++) {
+			ml = mloop + mp->loopstart;
+
+			for (j = 0; j < mp->totloop; j++, ml++, l_index++) {
+				copy_v4_v4_uchar(&wtcol_l[l_index][0],
+				                 &wtcol_v[ml->v][0]);
+			}
+		}
+		MEM_freeN(wtcol_v);
+
+		//dm->dirty |= DM_DIRTY_TESS_CDLAYERS; // XXX: Does Mesh need this?
+	}
+}
+
 static void DM_update_statvis_color(const Scene *scene, Object *ob, DerivedMesh *dm)
 {
 	BMEditMesh *em = BKE_editmesh_from_object(ob);
@@ -1742,6 +1975,32 @@ static void dm_ensure_display_normals(DerivedMesh *dm)
 	}
 }
 
+static void mesh_ensure_display_normals(Mesh *mesh)
+{
+	/* Note: mesh *may* have a poly CD_NORMAL layer (generated by a modifier needing poly normals e.g.).
+	 *       We do not use it here, though. And it should be tagged as temp!
+	 */
+	/* BLI_assert((CustomData_has_layer(&mesh->pdata, CD_NORMAL) == false)); */
+
+	if(mesh->runtime.cd_dirty_vert & CD_MASK_NORMAL || !CustomData_has_layer(&mesh->pdata, CD_NORMAL)) {
+		float (*face_nors)[3] = NULL;
+		face_nors = MEM_malloc_arrayN(mesh->totpoly, sizeof(*face_nors), "face_nors");
+
+		/* if normals are dirty we want to calculate vertex normals too */
+		bool only_face_normals = !(mesh->runtime.cd_dirty_vert & CD_MASK_NORMAL);
+
+		/* calculate face normals */
+		BKE_mesh_calc_normals_poly(
+				mesh->mvert, NULL, mesh->totvert, mesh->mloop, mesh->mpoly,
+				mesh->totloop, mesh->totpoly, face_nors,
+				only_face_normals);
+
+		CustomData_add_layer(&mesh->pdata, CD_NORMAL, CD_ASSIGN, face_nors, mesh->totpoly);
+
+		mesh->runtime.cd_dirty_vert &= ~CD_MASK_NORMAL;
+	}
+}
+
 static void mesh_calc_modifiers(
         struct Depsgraph *depsgraph, Scene *scene, Object *ob, float (*inputVertexCos)[3],
         const bool useRenderParams, int useDeform,
@@ -1749,7 +2008,7 @@ static vo

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list