[Bf-blender-cvs] [e35b7e73267] master: GPUBuffers: Fix/cleanup multires implementation

Clément Foucault noreply at git.blender.org
Fri Feb 22 04:04:05 CET 2019


Commit: e35b7e73267fc02817538cd67e72e5c278dec878
Author: Clément Foucault
Date:   Fri Feb 22 04:00:19 2019 +0100
Branches: master
https://developer.blender.org/rBe35b7e73267fc02817538cd67e72e5c278dec878

GPUBuffers: Fix/cleanup multires implementation

The multires sculpt drawing was a not working in smooth mode.
Also hidding was not supported by the wireframe overlay and flat shaded
faces.

Codewise it is cleaner and index buffers are only updated if the
smoothing changes.

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

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

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

diff --git a/source/blender/blenkernel/intern/pbvh.c b/source/blender/blenkernel/intern/pbvh.c
index 69617a354a9..6947270d88e 100644
--- a/source/blender/blenkernel/intern/pbvh.c
+++ b/source/blender/blenkernel/intern/pbvh.c
@@ -1112,11 +1112,8 @@ static void pbvh_update_draw_buffers(PBVH *bvh, PBVHNode **nodes, int totnode)
 				case PBVH_GRIDS:
 					node->draw_buffers =
 						GPU_pbvh_grid_buffers_build(
-						        node->prim_indices,
 						        node->totprim,
-						        bvh->grid_hidden,
-						        bvh->gridkey.grid_size,
-						        &bvh->gridkey);
+						        bvh->grid_hidden);
 					break;
 				case PBVH_FACES:
 					node->draw_buffers =
diff --git a/source/blender/gpu/GPU_buffers.h b/source/blender/gpu/GPU_buffers.h
index dc634b91284..8762a1cb222 100644
--- a/source/blender/gpu/GPU_buffers.h
+++ b/source/blender/gpu/GPU_buffers.h
@@ -49,7 +49,8 @@ GPU_PBVH_Buffers *GPU_pbvh_mesh_buffers_build(
         const int  face_indices_len);
 
 GPU_PBVH_Buffers *GPU_pbvh_grid_buffers_build(
-        int *grid_indices, int totgrid, unsigned int **grid_hidden, int gridsize, const struct CCGKey *key);
+        int totgrid,
+        unsigned int **grid_hidden);
 
 GPU_PBVH_Buffers *GPU_pbvh_bmesh_buffers_build(bool smooth_shading);
 
diff --git a/source/blender/gpu/intern/gpu_buffers.c b/source/blender/gpu/intern/gpu_buffers.c
index 71c55da690a..85459c4de40 100644
--- a/source/blender/gpu/intern/gpu_buffers.c
+++ b/source/blender/gpu/intern/gpu_buffers.c
@@ -364,79 +364,134 @@ GPU_PBVH_Buffers *GPU_pbvh_mesh_buffers_build(
 /** \name Grid PBVH
  * \{ */
 
-static void gpu_pbvh_grid_fill_fast_buffer(GPU_PBVH_Buffers *buffers, int totgrid, int gridsize)
+static void gpu_pbvh_grid_fill_index_buffers(
+        GPU_PBVH_Buffers *buffers,
+        int *grid_indices,
+        uint visible_quad_len,
+        int totgrid,
+        int gridsize)
 {
 	GPUIndexBufBuilder elb, elb_lines;
+	GPUIndexBufBuilder elb_fast, elb_lines_fast;
+
+	GPU_indexbuf_init(&elb, GPU_PRIM_TRIS, 2 * visible_quad_len, INT_MAX);
+	GPU_indexbuf_init(&elb_fast, GPU_PRIM_TRIS, 2 * totgrid, INT_MAX);
+	GPU_indexbuf_init(&elb_lines, GPU_PRIM_LINES, 2 * totgrid * gridsize * (gridsize - 1), INT_MAX);
+	GPU_indexbuf_init(&elb_lines_fast, GPU_PRIM_LINES, 4 * totgrid, INT_MAX);
+
 	if (buffers->smooth) {
-		GPU_indexbuf_init(&elb_lines, GPU_PRIM_LINES, 4 * totgrid, INT_MAX);
-		GPU_indexbuf_init(&elb, GPU_PRIM_TRIS, 2 * totgrid, INT_MAX);
-		for (int i = 0; i < totgrid; i++) {
-			const uint v0 = i * gridsize * gridsize + gridsize - 1;
-			const uint v1 = i * gridsize * gridsize;
-			const uint v2 = (i + 1) * gridsize * gridsize - gridsize;
-			const uint v3 = (i + 1) * gridsize * gridsize - 1;
-
-			GPU_indexbuf_add_tri_verts(&elb, v0, v1, v2);
-			GPU_indexbuf_add_tri_verts(&elb, v3, v0, v2);
-
-			GPU_indexbuf_add_line_verts(&elb_lines, v0, v1);
-			GPU_indexbuf_add_line_verts(&elb_lines, v1, v2);
-			GPU_indexbuf_add_line_verts(&elb_lines, v2, v3);
-			GPU_indexbuf_add_line_verts(&elb_lines, v3, v0);
+		uint offset = 0;
+		const uint grid_vert_len = gridsize * gridsize;
+		for (int i = 0; i < totgrid; i++, offset += grid_vert_len) {
+			uint v0, v1, v2, v3;
+			bool grid_visible = false;
+
+			BLI_bitmap *gh = buffers->grid_hidden[grid_indices[i]];
+
+			for (int j = 0; j < gridsize - 1; ++j) {
+				for (int k = 0; k < gridsize - 1; ++k) {
+					/* Skip hidden grid face */
+					if (gh && paint_is_grid_face_hidden(
+					        gh, gridsize, k, j))
+					{
+						continue;
+					}
+					/* Indices in a Clockwise QUAD disposition. */
+					v0 = offset + j * gridsize + k;
+					v1 = v0 + 1;
+					v2 = v1 + gridsize;
+					v3 = v2 - 1;
+
+					GPU_indexbuf_add_tri_verts(&elb, v0, v2, v1);
+					GPU_indexbuf_add_tri_verts(&elb, v0, v3, v2);
+
+					GPU_indexbuf_add_line_verts(&elb_lines, v0, v1);
+					GPU_indexbuf_add_line_verts(&elb_lines, v0, v3);
+
+					if (j + 2 == gridsize) {
+						GPU_indexbuf_add_line_verts(&elb_lines, v2, v3);
+					}
+					grid_visible = true;
+				}
+				GPU_indexbuf_add_line_verts(&elb_lines, v1, v2);
+			}
+
+			if (grid_visible) {
+				/* Grid corners */
+				v0 = offset + gridsize - 1;
+				v1 = offset;
+				v2 = offset + grid_vert_len - 1;
+				v3 = offset + grid_vert_len - gridsize;
+
+				GPU_indexbuf_add_tri_verts(&elb_fast, v0, v2, v1);
+				GPU_indexbuf_add_tri_verts(&elb_fast, v0, v3, v2);
+
+				GPU_indexbuf_add_line_verts(&elb_lines_fast, v0, v1);
+				GPU_indexbuf_add_line_verts(&elb_lines_fast, v1, v2);
+				GPU_indexbuf_add_line_verts(&elb_lines_fast, v2, v3);
+				GPU_indexbuf_add_line_verts(&elb_lines_fast, v3, v0);
+			}
 		}
 	}
 	else {
-		GPU_indexbuf_init(&elb_lines, GPU_PRIM_LINES, 4 * totgrid, INT_MAX);
-		GPU_indexbuf_init_ex(&elb, GPU_PRIM_TRI_STRIP, 5 * totgrid, INT_MAX, true);
-		uint vbo_index_offset = 0;
-		for (int i = 0; i < totgrid; i++) {
-			uint grid_indices[4] = {0, 0, 0, 0};
+		uint offset = 0;
+		const uint grid_vert_len = SQUARE(gridsize - 1) * 4;
+		for (int i = 0; i < totgrid; i++, offset += grid_vert_len) {
+			bool grid_visible = false;
+
+			BLI_bitmap *gh = buffers->grid_hidden[grid_indices[i]];
+
+			uint v0, v1, v2, v3;
 			for (int j = 0; j < gridsize - 1; j++) {
 				for (int k = 0; k < gridsize - 1; k++) {
-					const bool is_row_start = (k == 0);
-					const bool is_row_end = (k == gridsize - 2);
-					const bool is_grid_start = (j == 0);
-					const bool is_grid_end = (j == gridsize - 2);
-					const bool is_first_grid = (i == 0);
-					const bool is_last_grid = (i == totgrid - 1);
-
-					if (is_row_start && !(is_grid_start && is_first_grid)) {
-						vbo_index_offset += 1;
+					/* Skip hidden grid face */
+					if (gh && paint_is_grid_face_hidden(
+					        gh, gridsize, k, j))
+					{
+						continue;
 					}
+					/* VBO data are in a Clockwise QUAD disposition. */
+					v0 = offset + (j * (gridsize - 1) + k) * 4;
+					v1 = v0 + 1;
+					v2 = v0 + 2;
+					v3 = v0 + 3;
 
-					if (is_grid_start && is_row_start) {
-						grid_indices[0] = vbo_index_offset + 0;
-					}
-					if (is_grid_start && is_row_end) {
-						grid_indices[1] = vbo_index_offset + 2;
-					}
-					if (is_grid_end && is_row_start) {
-						grid_indices[2] = vbo_index_offset + 1;
-					}
-					if (is_grid_end && is_row_end) {
-						grid_indices[3] = vbo_index_offset + 3;
-					}
-					vbo_index_offset += 4;
+					GPU_indexbuf_add_tri_verts(&elb, v0, v2, v1);
+					GPU_indexbuf_add_tri_verts(&elb, v0, v3, v2);
+
+					GPU_indexbuf_add_line_verts(&elb_lines, v0, v1);
+					GPU_indexbuf_add_line_verts(&elb_lines, v0, v3);
 
-					if (is_row_end && !(is_grid_end && is_last_grid)) {
-						vbo_index_offset += 1;
+					if (j + 2 == gridsize) {
+						GPU_indexbuf_add_line_verts(&elb_lines, v2, v3);
 					}
+					grid_visible = true;
 				}
+				GPU_indexbuf_add_line_verts(&elb_lines, v1, v2);
+			}
+	
+			if (grid_visible) {
+				/* Grid corners */
+				v0 = offset;
+				v1 = offset + (gridsize - 1) * 4 - 3;
+				v2 = offset + grid_vert_len - 2;
+				v3 = offset + grid_vert_len - (gridsize - 1) * 4 + 3;
+
+				GPU_indexbuf_add_tri_verts(&elb_fast, v0, v2, v1);
+				GPU_indexbuf_add_tri_verts(&elb_fast, v0, v3, v2);
+
+				GPU_indexbuf_add_line_verts(&elb_lines_fast, v0, v1);
+				GPU_indexbuf_add_line_verts(&elb_lines_fast, v1, v2);
+				GPU_indexbuf_add_line_verts(&elb_lines_fast, v2, v3);
+				GPU_indexbuf_add_line_verts(&elb_lines_fast, v3, v0);
 			}
-			GPU_indexbuf_add_generic_vert(&elb, grid_indices[1]);
-			GPU_indexbuf_add_generic_vert(&elb, grid_indices[0]);
-			GPU_indexbuf_add_generic_vert(&elb, grid_indices[3]);
-			GPU_indexbuf_add_generic_vert(&elb, grid_indices[2]);
-			GPU_indexbuf_add_primitive_restart(&elb);
-
-			GPU_indexbuf_add_line_verts(&elb_lines, grid_indices[0], grid_indices[1]);
-			GPU_indexbuf_add_line_verts(&elb_lines, grid_indices[1], grid_indices[3]);
-			GPU_indexbuf_add_line_verts(&elb_lines, grid_indices[2], grid_indices[3]);
-			GPU_indexbuf_add_line_verts(&elb_lines, grid_indices[2], grid_indices[0]);
 		}
 	}
-	buffers->index_buf_fast = GPU_indexbuf_build(&elb);
-	buffers->index_lines_buf_fast = GPU_indexbuf_build(&elb_lines);
+
+	buffers->index_buf = GPU_indexbuf_build(&elb);
+	buffers->index_buf_fast = GPU_indexbuf_build(&elb_fast);
+	buffers->index_lines_buf = GPU_indexbuf_build(&elb_lines);
+	buffers->index_lines_buf_fast = GPU_indexbuf_build(&elb_lines_fast);
 }
 
 void GPU_pbvh_grid_buffers_update(
@@ -449,37 +504,49 @@ void GPU_pbvh_grid_buffers_update(
 	bool empty_mask = true;
 	int i, j, k, x, y;
 
-	buffers->smooth = grid_flag_mats[grid_indices[0]].flag & ME_SMOOTH;
+	const bool smooth = true;//grid_flag_mats[grid_indices[0]].flag & ME_SMOOTH;
 
 	/* Build VBO */
 	const int has_mask = key->has_mask;
 
-	uint vert_count = totgrid * key->grid_area;
+	uint vert_per_grid = (smooth) ? key->grid_area : (SQUARE(key->grid_size - 1) * 4);
+	uint vert_count = totgrid * vert_per_grid;
+
+	if (buffers->smooth != smooth) {
+		buffers->smooth = smooth;
+		GPU_BATCH_DISCARD_SAFE(buffers->triangles);
+		GPU_BATCH_DISCARD_SAFE(buffers->triangles_fast);
+		GPU_BATCH_DISCARD_SAFE(buffers->lines);
+		GPU_BATCH_DISCARD_SAFE(buffers->lines_fast);
 
-	if (!buffers->smooth) {
-		vert_count = totgrid * SQUARE(key->grid_size - 1) * 4;
-		/* Count strip restart verts (2 verts between each row and grid) */
-		vert_count += ((totgrid - 1) + totgrid * (key->grid_size - 2)) * 2;
+		GPU_INDEXBUF_DISCARD_SAFE(buffers->index_buf);
+		GPU_INDEXBUF_DISCARD_SAFE(buffers->index_buf_fast);
+		GPU_INDEXBUF_DISCARD_SAFE(buffers->index_lines_buf);
+		GPU_INDEXBUF_DISCARD_SAFE(buffers->index_lines_buf_fast);
 	}
 
-	GPU_BATCH_DISCARD_SAFE(buffers->triangles);
-	GPU_BATCH_DISCARD_SAFE(buffers->triangles_fast);
-	GPU_BATCH_DISCARD_SAFE(buffers->lines);
-	GPU_BATCH_DISCARD_SAFE(buffers->lines_fast);
-	GPU_INDEXBUF_DISCARD_SAFE(buffers->index_buf);
-	GPU_INDEXBUF_DISCARD_SAFE(buffers->index_buf_fast);
-	GPU_INDEXBUF_DISCARD_SAFE(buffers->index_lines_buf);
-	GPU_INDEXBUF_DISCARD_SAFE(buffers->index_lines_buf_fast);
+	if (buffers->index_buf == NULL) {
+		uint visible_quad_len = BKE_pbvh_count_grid_quads((BLI_bitmap **)buffers->grid_hidden,
+		                                                  grid_indices,
+		     

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list