[Bf-blender-cvs] [cb5515f] temp-derivedmesh-looptri: Initial looptri addition

Campbell Barton noreply at git.blender.org
Fri Jul 10 23:20:11 CEST 2015


Commit: cb5515fceae5ef8dde132791ff0560bd88c19b39
Author: Campbell Barton
Date:   Sat Jul 11 07:10:43 2015 +1000
Branches: temp-derivedmesh-looptri
https://developer.blender.org/rBcb5515fceae5ef8dde132791ff0560bd88c19b39

Initial looptri addition

This is intended to remove use of MFace for runtime tessellation,
and instead cache loop tris.

This has some advantages for OpenGL and should reduce memory use
since we won't need to store customdata layers (UV's, colors, normals) for MFace and MPoly+MLoop.

Currently looptri are stored in the DerivedMesh and accessed directly by the struct member,
though they may be moved behind API access.

These areas are working on a basic level:

- GPU buffers
- CDDM drawing
- sculpting

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

M	source/blender/blenkernel/BKE_DerivedMesh.h
M	source/blender/blenkernel/BKE_cdderivedmesh.h
M	source/blender/blenkernel/BKE_mesh.h
M	source/blender/blenkernel/BKE_paint.h
M	source/blender/blenkernel/BKE_pbvh.h
M	source/blender/blenkernel/intern/DerivedMesh.c
M	source/blender/blenkernel/intern/cdderivedmesh.c
M	source/blender/blenkernel/intern/mesh_evaluate.c
M	source/blender/blenkernel/intern/paint.c
M	source/blender/blenkernel/intern/pbvh.c
M	source/blender/blenkernel/intern/pbvh_intern.h
M	source/blender/blenkernel/intern/subsurf_ccg.c
M	source/blender/blenlib/BLI_math_geom.h
M	source/blender/blenlib/intern/math_geom.c
M	source/blender/editors/space_view3d/drawmesh.c
M	source/blender/gpu/GPU_buffers.h
M	source/blender/gpu/intern/gpu_buffers.c
M	source/blender/makesdna/DNA_meshdata_types.h

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

diff --git a/source/blender/blenkernel/BKE_DerivedMesh.h b/source/blender/blenkernel/BKE_DerivedMesh.h
index 70ae9cd..6af693a 100644
--- a/source/blender/blenkernel/BKE_DerivedMesh.h
+++ b/source/blender/blenkernel/BKE_DerivedMesh.h
@@ -184,6 +184,12 @@ struct DerivedMesh {
 	int totmat; /* total materials. Will be valid only before object drawing. */
 	struct Material **mat; /* material array. Will be valid only before object drawing */
 
+	struct {
+		struct MLoopTri *array;
+		int num;
+		int num_alloc;
+	} looptris;
+
 	/* use for converting to BMesh which doesn't store bevel weight and edge crease by default */
 	char cd_flag;
 
@@ -200,6 +206,8 @@ struct DerivedMesh {
 	/** Recalculates mesh tessellation */
 	void (*recalcTessellation)(DerivedMesh *dm);
 
+	void (*recalcLooptri)(DerivedMesh *dm);
+
 	/* Misc. Queries */
 
 	/* Also called in Editmode */
@@ -589,10 +597,14 @@ void DM_DupPolys(DerivedMesh *source, DerivedMesh *target);
 void DM_ensure_normals(DerivedMesh *dm);
 void DM_ensure_tessface(DerivedMesh *dm);
 
+void DM_ensure_looptri_data(DerivedMesh *dm);
+void DM_ensure_looptri(DerivedMesh *dm);
+
 void DM_update_tessface_data(DerivedMesh *dm);
 
 void DM_update_materials(DerivedMesh *dm, struct Object *ob);
 struct MTFace *DM_paint_uvlayer_active_get(DerivedMesh *dm, int mat_nr);
+struct MLoopUV *DM_paint_uvlayer_active_get_mloopuv(DerivedMesh *dm, int mat_nr);
 
 void DM_interp_vert_data(
         struct DerivedMesh *source, struct DerivedMesh *dest,
@@ -716,12 +728,12 @@ void DM_update_weight_mcol(
  * the DerivedMesh, with both a pointer for arrays and an offset for editmesh */
 typedef struct DMVertexAttribs {
 	struct {
-		struct MTFace *array;
+		struct MLoopUV *array;
 		int em_offset, gl_index, gl_texco;
 	} tface[MAX_MTFACE];
 
 	struct {
-		struct MCol *array;
+		struct MLoopCol *array;
 		int em_offset, gl_index;
 	} mcol[MAX_MCOL];
 
diff --git a/source/blender/blenkernel/BKE_cdderivedmesh.h b/source/blender/blenkernel/BKE_cdderivedmesh.h
index 59ec316..c7ad641 100644
--- a/source/blender/blenkernel/BKE_cdderivedmesh.h
+++ b/source/blender/blenkernel/BKE_cdderivedmesh.h
@@ -125,6 +125,8 @@ void CDDM_calc_edges(struct DerivedMesh *dm);
 void CDDM_recalc_tessellation(struct DerivedMesh *dm);
 void CDDM_recalc_tessellation_ex(struct DerivedMesh *dm, const bool do_face_nor_cpy);
 
+void CDDM_recalc_looptri(struct DerivedMesh *dm);
+
 /* lowers the number of vertices/edges/faces in a CDDerivedMesh
  * the layer data stays the same size
  */
diff --git a/source/blender/blenkernel/BKE_mesh.h b/source/blender/blenkernel/BKE_mesh.h
index 1ff2c6b..afe6fda 100644
--- a/source/blender/blenkernel/BKE_mesh.h
+++ b/source/blender/blenkernel/BKE_mesh.h
@@ -39,6 +39,7 @@ struct LinkNode;
 struct BLI_Stack;
 struct MemArena;
 struct BMesh;
+struct MLoopTri;
 struct Main;
 struct Mesh;
 struct MPoly;
@@ -178,6 +179,11 @@ void BKE_mesh_calc_normals_tessface(
         struct MVert *mverts, int numVerts,
         const struct MFace *mfaces, int numFaces,
         float (*r_faceNors)[3]);
+void BKE_mesh_calc_normals_looptri(
+        struct MVert *mverts, int numVerts,
+        const struct MLoop *mloop,
+        const struct MLoopTri *looptri, int looptri_num,
+        float (*r_tri_nors)[3]);
 void BKE_mesh_loop_tangents_ex(
         const struct MVert *mverts, const int numVerts, const struct MLoop *mloops,
         float (*r_looptangent)[4], float (*loopnors)[3], const struct MLoopUV *loopuv,
@@ -285,6 +291,11 @@ int BKE_mesh_recalc_tessellation(
         struct MVert *mvert,
         int totface, int totloop, int totpoly,
         const bool do_face_nor_copy);
+void BKE_mesh_recalc_looptri(
+        const struct MLoop *mloop, const struct MPoly *mpoly,
+        const struct MVert *mvert,
+        int totloop, int totpoly,
+        struct MLoopTri *mlooptri);
 int BKE_mesh_mpoly_to_mface(
         struct CustomData *fdata, struct CustomData *ldata,
         struct CustomData *pdata, int totface, int totloop, int totpoly);
diff --git a/source/blender/blenkernel/BKE_paint.h b/source/blender/blenkernel/BKE_paint.h
index 09ccc3d..c9192fd 100644
--- a/source/blender/blenkernel/BKE_paint.h
+++ b/source/blender/blenkernel/BKE_paint.h
@@ -40,6 +40,8 @@ struct CurveMapping;
 struct MeshElemMap;
 struct GridPaintMask;
 struct Main;
+struct MLoop;
+struct MLoopTri;
 struct MFace;
 struct MVert;
 struct Object;
@@ -133,7 +135,7 @@ bool BKE_paint_select_vert_test(struct Object *ob);
 bool BKE_paint_select_elem_test(struct Object *ob);
 
 /* partial visibility */
-bool paint_is_face_hidden(const struct MFace *f, const struct MVert *mvert);
+bool paint_is_face_hidden(const struct MLoopTri *lt, const struct MVert *mvert, const struct MLoop *mloop);
 bool paint_is_grid_face_hidden(const unsigned int *grid_hidden,
                                int gridsize, int x, int y);
 bool paint_is_bmesh_face_hidden(struct BMFace *f);
diff --git a/source/blender/blenkernel/BKE_pbvh.h b/source/blender/blenkernel/BKE_pbvh.h
index 9cefdd4..e42d497 100644
--- a/source/blender/blenkernel/BKE_pbvh.h
+++ b/source/blender/blenkernel/BKE_pbvh.h
@@ -34,7 +34,9 @@ struct CCGElem;
 struct CCGKey;
 struct CustomData;
 struct DMFlagMat;
-struct MFace;
+struct MPoly;
+struct MLoop;
+struct MLoopTri;
 struct MVert;
 struct PBVH;
 struct PBVHNode;
@@ -60,8 +62,10 @@ typedef void (*BKE_pbvh_HitOccludedCallback)(PBVHNode *node, void *data, float *
 
 PBVH *BKE_pbvh_new(void);
 void BKE_pbvh_build_mesh(
-        PBVH *bvh, const struct MFace *faces, struct MVert *verts,
-        int totface, int totvert, struct CustomData *vdata);
+        PBVH *bvh,
+        const struct MPoly *mpoly, const struct MLoop *mloop,
+        struct MVert *verts, int totvert, struct CustomData *vdata,
+        const struct MLoopTri *looptri, int looptri_num);
 void BKE_pbvh_build_grids(PBVH *bvh, struct CCGElem **grid_elems,
                           int totgrid,
                           struct CCGKey *key, void **gridfaces, struct DMFlagMat *flagmats,
diff --git a/source/blender/blenkernel/intern/DerivedMesh.c b/source/blender/blenkernel/intern/DerivedMesh.c
index 7ced227..68c3080 100644
--- a/source/blender/blenkernel/intern/DerivedMesh.c
+++ b/source/blender/blenkernel/intern/DerivedMesh.c
@@ -364,6 +364,10 @@ int DM_release(DerivedMesh *dm)
 			dm->totmat = 0;
 		}
 
+		MEM_SAFE_FREE(dm->looptris.array);
+		dm->looptris.num = 0;
+		dm->looptris.num_alloc = 0;
+
 		return 1;
 	}
 	else {
@@ -438,6 +442,46 @@ void DM_ensure_tessface(DerivedMesh *dm)
 	}
 
 	dm->dirty &= ~DM_DIRTY_TESS_CDLAYERS;
+
+	/* -------------------------------------------------------------------- */
+	/* ---------------------- WATCH IT! THIS IS ONLY HERE FOR TESTING ----- */
+	/* -------------------------------------------------------------------- */
+	DM_ensure_looptri(dm);
+}
+
+/* ensure the array is large enough */
+void DM_ensure_looptri_data(DerivedMesh *dm)
+{
+	const unsigned int totpoly = dm->numPolyData;
+	const unsigned int totloop = dm->numLoopData;
+	const int looptris_num = poly_to_tri_count(totpoly, totloop);
+
+	if ((looptris_num > dm->looptris.num_alloc) ||
+	    (looptris_num < dm->looptris.num_alloc * 2) ||
+	    (totpoly == 0))
+	{
+		MEM_SAFE_FREE(dm->looptris.array);
+		dm->looptris.num_alloc = 0;
+		dm->looptris.num = 0;
+	}
+
+	if (totpoly) {
+		if (dm->looptris.array == NULL) {
+			dm->looptris.array = MEM_mallocN(sizeof(*dm->looptris.array) * looptris_num, __func__);
+			dm->looptris.num_alloc = looptris_num;
+		}
+
+		dm->looptris.num = looptris_num;
+	}
+}
+
+void DM_ensure_looptri(DerivedMesh *dm)
+{
+	const int numPolys =  dm->getNumPolys(dm);
+
+	if ((dm->looptris.num == 0) && (numPolys != 0)) {
+		dm->recalcLooptri(dm);
+	}
 }
 
 /* Update tessface CD data from loop/poly ones. Needed when not retessellating after modstack evaluation. */
@@ -546,6 +590,29 @@ MTFace *DM_paint_uvlayer_active_get(DerivedMesh *dm, int mat_nr)
 	return tf_base;
 }
 
+MLoopUV *DM_paint_uvlayer_active_get_mloopuv(DerivedMesh *dm, int mat_nr)
+{
+	MLoopUV *uv_base;
+
+	BLI_assert(mat_nr < dm->totmat);
+
+	if (dm->mat[mat_nr] && dm->mat[mat_nr]->texpaintslot &&
+	    dm->mat[mat_nr]->texpaintslot[dm->mat[mat_nr]->paint_active_slot].uvname)
+	{
+		uv_base = CustomData_get_layer_named(&dm->loopData, CD_MLOOPUV,
+		                                     dm->mat[mat_nr]->texpaintslot[dm->mat[mat_nr]->paint_active_slot].uvname);
+		/* This can fail if we have changed the name in the UV layer list and have assigned the old name in the material
+		 * texture slot.*/
+		if (!uv_base)
+			uv_base = CustomData_get_layer(&dm->loopData, CD_MLOOPUV);
+	}
+	else {
+		uv_base = CustomData_get_layer(&dm->loopData, CD_MLOOPUV);
+	}
+
+	return uv_base;
+}
+
 void DM_to_mesh(DerivedMesh *dm, Mesh *me, Object *ob, CustomDataMask mask, bool take_ownership)
 {
 	/* dm might depend on me, so we need to do everything with a local copy */
@@ -3266,6 +3333,7 @@ void DM_vertex_attributes_from_gpu(DerivedMesh *dm, GPUVertexAttribs *gattribs,
 	}
 }
 
+#if 0
 /* Set vertex shader attribute inputs for a particular tessface vert
  *
  * a: tessface index
@@ -3328,6 +3396,14 @@ void DM_draw_attrib_vertex(DMVertexAttribs *attribs, int a, int index, int vert)
 		glVertexAttrib4fvARB(attribs->tang.gl_index, tang);
 	}
 }
+#else
+
+void DM_draw_attrib_vertex(DMVertexAttribs *attribs, int a, int index, int vert)
+{
+	UNUSED_VARS(attribs, a, index, vert);
+}
+
+#endif
 
 /* Set object's bounding box based on DerivedMesh min/max data */
 void DM_set_object_boundbox(Object *ob, DerivedMesh *dm)
diff --git a/source/blender/blenkernel/intern/cdderivedmesh.c b/source/blender/blenkernel/intern/cdderivedmesh.c
index d60c5a5..993a011 100644
--- a/source/blender/blenkernel/intern/cdderivedmesh.c
+++ b/source/blender/blenkernel/intern/cdderivedmesh.c
@@ -293,15 +293,26 @@ static PBVH *cdDM_getPBVH(Object *ob, DerivedMesh *dm)
 	 * that this is actually for, to support a pbvh on a modified mesh */
 	if (!cddm->pbvh && ob->type == OB_MESH) {
 		Mesh *me = ob->data;
+		const int looptris_num = poly_to_tri_count(me->totpoly, me->totloop);
+		MLoopTri *looptri;
 		bool deformed;
 
 		cddm->pbvh = BKE_pbvh_new();
 		cddm->pbvh_draw = can_pbvh_draw(ob, dm);
 
-		BKE_mesh_tess

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list