[Bf-blender-cvs] [f1f4a16] master: Convert CCGSubsurf VBO system to also sort hidden and visible faces.

Antony Riakiotakis noreply at git.blender.org
Sat Jul 25 22:03:02 CEST 2015


Commit: f1f4a16eab6cf685281c0fbecf4ac1a1f9976487
Author: Antony Riakiotakis
Date:   Sat Jul 25 22:02:15 2015 +0200
Branches: master
https://developer.blender.org/rBf1f4a16eab6cf685281c0fbecf4ac1a1f9976487

Convert CCGSubsurf VBO system to also sort hidden and visible faces.

It's not actually used during drawing though.

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

M	source/blender/blenkernel/intern/cdderivedmesh.c
M	source/blender/blenkernel/intern/subsurf_ccg.c
M	source/blender/gpu/GPU_buffers.h
M	source/blender/gpu/intern/gpu_buffers.c

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

diff --git a/source/blender/blenkernel/intern/cdderivedmesh.c b/source/blender/blenkernel/intern/cdderivedmesh.c
index 8f04c19..2a3bcca 100644
--- a/source/blender/blenkernel/intern/cdderivedmesh.c
+++ b/source/blender/blenkernel/intern/cdderivedmesh.c
@@ -1291,7 +1291,6 @@ static void cdDM_buffer_copy_triangles(
 		gpumaterials[i].totvisiblepolys = fc[i].i_visible;
 	}
 
-
 	MEM_freeN(fc);
 }
 
diff --git a/source/blender/blenkernel/intern/subsurf_ccg.c b/source/blender/blenkernel/intern/subsurf_ccg.c
index 2a69ba4..dca61f8 100644
--- a/source/blender/blenkernel/intern/subsurf_ccg.c
+++ b/source/blender/blenkernel/intern/subsurf_ccg.c
@@ -1973,12 +1973,21 @@ static void ccgDM_buffer_copy_normal(
 	}
 }
 
+typedef struct FaceCount {
+	unsigned int i_visible;
+	unsigned int i_hidden;
+	unsigned int i_tri_visible;
+	unsigned int i_tri_hidden;
+} FaceCount;
+
+
 /* Only used by non-editmesh types */
 static void ccgDM_buffer_copy_triangles(
         DerivedMesh *dm, unsigned int *varray,
         const int *mat_orig_to_new)
 {
-	GPUBufferMaterial *gpumat;
+	GPUBufferMaterial *gpumat, *gpumaterials = dm->drawObject->materials;
+	const int totmat = dm->drawObject->totmaterial;
 	CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
 	CCGSubSurf *ss = ccgdm->ss;
 	CCGKey key;
@@ -1988,41 +1997,87 @@ static void ccgDM_buffer_copy_triangles(
 	int i, totface = ccgSubSurf_getNumFaces(ss);
 	int matnr = -1, start;
 	int totloops = 0;
+	FaceCount *fc = MEM_mallocN(sizeof(*fc) * totmat, "gpumaterial.facecount");
 
 	CCG_key_top_level(&key, ss);
 
+	for (i = 0; i < totmat; i++) {
+		fc[i].i_visible = 0;
+		fc[i].i_tri_visible = 0;
+		fc[i].i_hidden = gpumaterials[i].totpolys - 1;
+		fc[i].i_tri_hidden = gpumaterials[i].totelements - 1;
+	}
+
 	for (i = 0; i < totface; i++) {
 		CCGFace *f = ccgdm->faceMap[i].face;
 		int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
 		int index = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(f));
+		bool is_hidden;
+		int mati;
 
 		if (faceFlags) {
 			matnr = faceFlags[index].mat_nr;
+			is_hidden = (faceFlags[index].flag & ME_HIDE) != 0;
 		}
 		else {
 			matnr = 0;
+			is_hidden = false;
 		}
+		mati = mat_orig_to_new[matnr];
+		gpumat = dm->drawObject->materials + mati;
 
-		for (S = 0; S < numVerts; S++) {
-			for (y = 0; y < gridFaces; y++) {
-				for (x = 0; x < gridFaces; x++) {
-					gpumat = dm->drawObject->materials + mat_orig_to_new[matnr];
-					start = gpumat->counter;
+		if (is_hidden) {
+			for (S = 0; S < numVerts; S++) {
+				for (y = 0; y < gridFaces; y++) {
+					for (x = 0; x < gridFaces; x++) {
+						start = gpumat->start + fc[mati].i_tri_hidden;
+
+						varray[start--] = totloops + 1;
+						varray[start--] = totloops + 2;
+						varray[start--] = totloops + 3;
+
+						varray[start--] = totloops;
+						varray[start--] = totloops + 1;
+						varray[start--] = totloops + 3;
 
-					varray[start] = totloops + 3;
-					varray[start + 1] = totloops + 2;
-					varray[start + 2] = totloops + 1;
+						fc[mati].i_tri_hidden -= 6;
 
-					varray[start + 3] = totloops + 3;
-					varray[start + 4] = totloops + 1;
-					varray[start + 5] = totloops;
+						totloops += 4;
+					}
+				}
+			}
+			gpumat->polys[fc[mati].i_hidden--] = i;
+		}
+		else {
+			for (S = 0; S < numVerts; S++) {
+				for (y = 0; y < gridFaces; y++) {
+					for (x = 0; x < gridFaces; x++) {
+						start = gpumat->start + fc[mati].i_tri_visible;
+
+						varray[start++] = totloops + 3;
+						varray[start++] = totloops + 2;
+						varray[start++] = totloops + 1;
+
+						varray[start++] = totloops + 3;
+						varray[start++] = totloops + 1;
+						varray[start++] = totloops;
 
-					gpumat->counter += 6;
-					totloops += 4;
+						fc[mati].i_tri_visible += 6;
+
+						totloops += 4;
+					}
 				}
 			}
+			gpumat->polys[fc[mati].i_visible++] = i;
 		}
 	}
+
+	/* set the visible polygons */
+	for (i = 0; i < totmat; i++) {
+		gpumaterials[i].totvisiblepolys = fc[i].i_visible;
+	}
+
+	MEM_freeN(fc);
 }
 
 
@@ -2460,8 +2515,6 @@ static void ccgDM_copy_gpu_data(
 
 static GPUDrawObject *ccgDM_GPUObjectNew(DerivedMesh *dm)
 {
-	GPUBufferMaterial *mat;
-	int *mat_orig_to_new;
 	CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
 	CCGSubSurf *ss = ccgdm->ss;
 	GPUDrawObject *gdo;
@@ -2514,36 +2567,8 @@ static GPUDrawObject *ccgDM_GPUObjectNew(DerivedMesh *dm)
 
 	/* store total number of points used for triangles */
 	gdo->tot_triangle_point = ccgSubSurf_getNumFinalFaces(ss) * 6;
-
 	gdo->tot_loop_verts = ccgSubSurf_getNumFinalFaces(ss) * 4;
 
-	mat_orig_to_new = MEM_callocN(sizeof(*mat_orig_to_new) * totmat,
-	                                             "GPUDrawObject.mat_orig_to_new");
-
-	/* build a map from the original material indices to the new
-	 * GPUBufferMaterial indices */
-	for (i = 0; i < gdo->totmaterial; i++) {
-		mat_orig_to_new[gdo->materials[i].mat_nr] = i;
-		gdo->materials[i].counter = 0;
-	}
-
-	if (faceFlags) {
-		for (i = 0; i < totface; i++) {
-			CCGFace *f = ccgdm->faceMap[i].face;
-			int index = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(f));
-			int new_matnr = faceFlags[index].mat_nr;
-
-			mat = &gdo->materials[mat_orig_to_new[new_matnr]];
-			mat->polys[mat->counter++] = i;
-		}
-	}
-	else {
-		mat = &gdo->materials[0];
-		for (i = 0; i < totface; i++) {
-			mat->polys[mat->counter++] = i;
-		}
-	}
-
 	/* finally, count loose points */
 	for (i = 0; i < totedge; i++) {
 		CCGEdge *e = ccgdm->edgeMap[i].edge;
@@ -2552,8 +2577,6 @@ static GPUDrawObject *ccgDM_GPUObjectNew(DerivedMesh *dm)
 			gdo->tot_loose_point += edgeVerts;
 	}
 
-	MEM_freeN(mat_orig_to_new);
-
 	return gdo;
 }
 
diff --git a/source/blender/gpu/GPU_buffers.h b/source/blender/gpu/GPU_buffers.h
index 1a85d37..e653af0 100644
--- a/source/blender/gpu/GPU_buffers.h
+++ b/source/blender/gpu/GPU_buffers.h
@@ -66,7 +66,6 @@ typedef struct GPUBufferMaterial {
 	unsigned int totloops;
 	unsigned int *polys; /* array of polygons for this material */
 	unsigned int totpolys; /* total polygons in polys */
-	unsigned int counter; /* general purpose counter, initialize first! */
 	unsigned int totvisiblepolys; /* total visible polygons */
 
 	/* original material index */
diff --git a/source/blender/gpu/intern/gpu_buffers.c b/source/blender/gpu/intern/gpu_buffers.c
index bbffa5a..11697c5 100644
--- a/source/blender/gpu/intern/gpu_buffers.c
+++ b/source/blender/gpu/intern/gpu_buffers.c
@@ -529,7 +529,6 @@ static GPUBuffer *gpu_buffer_setup(DerivedMesh *dm, GPUDrawObject *object,
 	int i;
 	const GPUBufferTypeSettings *ts = &gpu_buffer_type_settings[type];
 	GLenum target = ts->gl_buffer_type;
-	int num_components = ts->num_components;
 	size_t size = gpu_buffer_size_from_type(dm, type);
 	bool use_VBOs = (GLEW_ARB_vertex_buffer_object) && !(U.gameflags & USER_DISABLE_VBO);
 	GLboolean uploaded;
@@ -547,9 +546,6 @@ static GPUBuffer *gpu_buffer_setup(DerivedMesh *dm, GPUDrawObject *object,
 	mat_orig_to_new = MEM_mallocN(sizeof(*mat_orig_to_new) * dm->totmat,
 	                              "GPU_buffer_setup.mat_orig_to_new");
 	for (i = 0; i < object->totmaterial; i++) {
-		/* for each material, the current index to copy data to */
-		object->materials[i].counter = object->materials[i].start * num_components;
-
 		/* map from original material index to new
 		 * GPUBufferMaterial index */
 		mat_orig_to_new[object->materials[i].mat_nr] = i;




More information about the Bf-blender-cvs mailing list