[Bf-blender-cvs] [84536d1d3ce] blender2.8: GPU Bufferes: Small optimization when updating buffers

Germano noreply at git.blender.org
Mon Mar 19 22:09:11 CET 2018


Commit: 84536d1d3ce217a44877acdc9845254249c040ce
Author: Germano
Date:   Mon Mar 19 18:09:00 2018 -0300
Branches: blender2.8
https://developer.blender.org/rB84536d1d3ce217a44877acdc9845254249c040ce

GPU Bufferes: Small optimization when updating buffers

With the API recently added to gawain, it is now possible to update the vbos linked to the batch.
So the batch does not have to be destroyed.
The optimization is more sensitive when sculpt is made on low poly meshs

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

M	source/blender/gpu/intern/gpu_buffers.c

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

diff --git a/source/blender/gpu/intern/gpu_buffers.c b/source/blender/gpu/intern/gpu_buffers.c
index 3043b3ab686..3367a568bc5 100644
--- a/source/blender/gpu/intern/gpu_buffers.c
+++ b/source/blender/gpu/intern/gpu_buffers.c
@@ -986,15 +986,37 @@ struct GPU_PBVH_Buffers {
 	float diffuse_color[4];
 };
 
-typedef struct {
+static struct {
 	uint pos, nor, col;
-} VertexBufferAttrID;
+} g_vbo_id = {0};
 
-static void gpu_pbvh_vert_format_init__gwn(Gwn_VertFormat *format, VertexBufferAttrID *vbo_id)
+/* Allocates a non-initialized buffer to be sent to GPU.
+ * Return is false it indicates that the memory map failed. */
+static bool gpu_pbvh_vert_buf_data_set(GPU_PBVH_Buffers *buffers, unsigned int vert_ct)
 {
-	vbo_id->pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
-	vbo_id->nor = GWN_vertformat_attr_add(format, "nor", GWN_COMP_I16, 3, GWN_FETCH_INT_TO_FLOAT_UNIT);
-	vbo_id->col = GWN_vertformat_attr_add(format, "color", GWN_COMP_U8, 3, GWN_FETCH_INT_TO_FLOAT_UNIT);
+	if (buffers->vert_buf == NULL) {
+		/* Initialize vertex buffer */
+		/* match 'VertexBufferFormat' */
+
+		static Gwn_VertFormat format = {0};
+		if (format.attrib_ct == 0) {
+			g_vbo_id.pos = GWN_vertformat_attr_add(&format, "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
+			g_vbo_id.nor = GWN_vertformat_attr_add(&format, "nor", GWN_COMP_I16, 3, GWN_FETCH_INT_TO_FLOAT_UNIT);
+			g_vbo_id.col = GWN_vertformat_attr_add(&format, "color", GWN_COMP_U8, 3, GWN_FETCH_INT_TO_FLOAT_UNIT);
+		}
+#if 0
+		buffers->vert_buf = GWN_vertbuf_create_with_format_ex(&format, GWN_USAGE_DYNAMIC);
+		GWN_vertbuf_data_alloc(buffers->vert_buf, vert_ct);
+	}
+	else if (vert_ct != buffers->vert_buf->vertex_ct) {
+		GWN_vertbuf_data_resize(buffers->vert_buf, vert_ct);
+	}
+#else
+		buffers->vert_buf = GWN_vertbuf_create_with_format_ex(&format, GWN_USAGE_STATIC);
+	}
+	GWN_vertbuf_data_alloc(buffers->vert_buf, vert_ct);
+#endif
+	return buffers->vert_buf->data != NULL;
 }
 
 static void gpu_pbvh_batch_init(GPU_PBVH_Buffers *buffers)
@@ -1004,14 +1026,14 @@ static void gpu_pbvh_batch_init(GPU_PBVH_Buffers *buffers)
 		GWN_vertbuf_use(buffers->vert_buf);
 	}
 
-	GWN_BATCH_DISCARD_SAFE(buffers->triangles);
-	buffers->triangles = GWN_batch_create(
-	        GWN_PRIM_TRIS, buffers->vert_buf,
-	        /* can be NULL */
-	        buffers->index_buf);
+	if (buffers->triangles == NULL) {
+		buffers->triangles = GWN_batch_create(
+		        GWN_PRIM_TRIS, buffers->vert_buf,
+		        /* can be NULL */
+		        buffers->index_buf);
+	}
 
-	GWN_BATCH_DISCARD_SAFE(buffers->triangles_fast);
-	if (buffers->index_buf_fast) {
+	if ((buffers->triangles_fast == NULL) && buffers->index_buf_fast) {
 		buffers->triangles_fast = GWN_batch_create(
 		        GWN_PRIM_TRIS, buffers->vert_buf,
 		        /* can be NULL */
@@ -1085,25 +1107,15 @@ void GPU_pbvh_mesh_buffers_update(
 		rgba_float_to_uchar(diffuse_color_ub, diffuse_color);
 
 		/* Build VBO */
-		GWN_VERTBUF_DISCARD_SAFE(buffers->vert_buf);
-
-		/* match 'VertexBufferFormat' */
-		Gwn_VertFormat format = {0};
-		VertexBufferAttrID vbo_id;
-		gpu_pbvh_vert_format_init__gwn(&format, &vbo_id);
-
-		buffers->vert_buf = GWN_vertbuf_create_with_format(&format);
-		GWN_vertbuf_data_alloc(buffers->vert_buf, totelem);
-
-		if (buffers->vert_buf->data) {
+		if (gpu_pbvh_vert_buf_data_set(buffers, totelem)) {
 			/* Vertex data is shared if smooth-shaded, but separate
 			 * copies are made for flat shading because normals
 			 * shouldn't be shared. */
 			if (buffers->smooth) {
 				for (uint i = 0; i < totvert; ++i) {
 					const MVert *v = &mvert[vert_indices[i]];
-					GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.pos, i, v->co);
-					GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.nor, i, v->no);
+					GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.pos, i, v->co);
+					GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.nor, i, v->no);
 				}
 
 				for (uint i = 0; i < buffers->face_indices_len; i++) {
@@ -1114,10 +1126,10 @@ void GPU_pbvh_mesh_buffers_update(
 							int v_index = buffers->mloop[lt->tri[j]].v;
 							uchar color_ub[3];
 							gpu_color_from_mask_copy(vmask[v_index], diffuse_color, color_ub);
-							GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.col, vidx, color_ub);
+							GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.col, vidx, color_ub);
 						}
 						else {
-							GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.col, vidx, diffuse_color_ub);
+							GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.col, vidx, diffuse_color_ub);
 						}
 					}
 				}
@@ -1160,9 +1172,9 @@ void GPU_pbvh_mesh_buffers_update(
 					for (uint j = 0; j < 3; j++) {
 						const MVert *v = &mvert[vtri[j]];
 
-						GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.pos, vbo_index, v->co);
-						GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.nor, vbo_index, no);
-						GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.col, vbo_index, color_ub);
+						GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.pos, vbo_index, v->co);
+						GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.nor, vbo_index, no);
+						GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.col, vbo_index, color_ub);
 
 						vbo_index++;
 					}
@@ -1171,9 +1183,6 @@ void GPU_pbvh_mesh_buffers_update(
 
 			gpu_pbvh_batch_init(buffers);
 		}
-		else {
-			GWN_VERTBUF_DISCARD_SAFE(buffers->vert_buf);
-		}
 	}
 
 	buffers->mvert = mvert;
@@ -1293,17 +1302,9 @@ void GPU_pbvh_grid_buffers_update(
 
 		copy_v4_v4(buffers->diffuse_color, diffuse_color);
 
-		Gwn_VertFormat format = {0};
-		VertexBufferAttrID vbo_id;
-		gpu_pbvh_vert_format_init__gwn(&format, &vbo_id);
-
-		/* Build coord/normal VBO */
-		GWN_VERTBUF_DISCARD_SAFE(buffers->vert_buf);
-		buffers->vert_buf = GWN_vertbuf_create_with_format(&format);
-		GWN_vertbuf_data_alloc(buffers->vert_buf, totgrid * key->grid_area);
-
 		uint vbo_index_offset = 0;
-		if (buffers->vert_buf->data) {
+		/* Build VBO */
+		if (gpu_pbvh_vert_buf_data_set(buffers, totgrid * key->grid_area)) {
 			for (i = 0; i < totgrid; ++i) {
 				CCGElem *grid = grids[grid_indices[i]];
 				int vbo_index = vbo_index_offset;
@@ -1311,12 +1312,12 @@ void GPU_pbvh_grid_buffers_update(
 				for (y = 0; y < key->grid_size; y++) {
 					for (x = 0; x < key->grid_size; x++) {
 						CCGElem *elem = CCG_grid_elem(key, grid, x, y);
-						GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.pos, vbo_index, CCG_elem_co(key, elem));
+						GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.pos, vbo_index, CCG_elem_co(key, elem));
 
 						if (buffers->smooth) {
 							short no_short[3];
 							normal_float_to_short_v3(no_short, CCG_elem_no(key, elem));
-							GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.nor, vbo_index, no_short);
+							GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.nor, vbo_index, no_short);
 
 							if (has_mask) {
 								uchar color_ub[3];
@@ -1327,7 +1328,7 @@ void GPU_pbvh_grid_buffers_update(
 								else {
 									F3TOCHAR3(diffuse_color, color_ub);
 								}
-								GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.col, vbo_index, color_ub);
+								GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.col, vbo_index, color_ub);
 							}
 						}
 						vbo_index += 1;
@@ -1354,7 +1355,7 @@ void GPU_pbvh_grid_buffers_update(
 							vbo_index = vbo_index_offset + ((j + 1) * key->grid_size + k);
 							short no_short[3];
 							normal_float_to_short_v3(no_short, fno);
-							GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.nor, vbo_index, no_short);
+							GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.nor, vbo_index, no_short);
 
 							if (has_mask) {
 								uchar color_ub[3];
@@ -1370,7 +1371,7 @@ void GPU_pbvh_grid_buffers_update(
 								else {
 									F3TOCHAR3(diffuse_color, color_ub);
 								}
-								GWN_vertbuf_attr_set(buffers->vert_buf, vbo_id.col, vbo_index, color_ub);
+								GWN_vertbuf_attr_set(buffers->vert_buf, g_vbo_id.col, vbo_index, color_ub);
 							}
 						}
 					}
@@ -1381,9 +1382,6 @@ void GPU_pbvh_grid_buffers_update(
 
 			gpu_pbvh_batch_init(buffers);
 		}
-		else {
-			GWN_VERTBUF_DISCARD_SAFE(buffers->vert_buf);
-		}
 	}
 
 	buffers->grids = grids;
@@ -1562,7 +1560,6 @@ GPU_PBVH_Buffers *GPU_pbvh_grid_buffers_build(
 static void gpu_bmesh_vert_to_buffer_copy__gwn(
         BMVert *v,
         Gwn_VertBuf *vert_buf,
-        const VertexBufferAttrID *vbo_id,
         int *v_index,
         const float fno[3],
         const float *fmask,
@@ -1573,12 +1570,12 @@ static void gpu_bmesh_vert_to_buffer_copy__gwn(
 	if (!BM_elem_flag_test(v, BM_ELEM_HIDDEN)) {
 
 		/* Set coord, normal, and mask */
-		GWN_vertbuf_attr_set(vert_buf, vbo_id->pos, *v_index, v->co);
+		GWN_vertbuf_attr_set(vert_buf, g_vbo_id.pos, *v_index, v->co);
 
 		{
 			short no_short[3];
 			normal_float_to_short_v3(no_short, fno ? fno : v->no);
-			GWN_vertbuf_attr_set(vert_buf, vbo_id->nor, *v_index, no_short);
+			GWN_vertbuf_attr_set(vert_buf, g_vbo_id.nor, *v_index, no_short);
 		}
 
 		{
@@ -1596,7 +1593,7 @@ static void gpu_bmesh_vert_to_buffer_copy__gwn(
 			        effective_mask,
 			        diffuse_color,
 			        color_ub);
-			GWN_vertbuf_attr_set(vert_buf, vbo_id->col, *v_index, color_ub);
+			GWN_vertbuf_attr_set(vert_buf, g_vbo_id.col, *v_index, color_ub);
 		}
 
 		/* Assign index for use in the triangle index buffer */
@@ -1694,18 +1691,8 @@ void GPU_pbvh_bmesh_buffers_update(
 
 	copy_v4_v4(buffers->diffuse_color, diffuse_color);
 
-	/* Initialize vertex buffer */
-	GWN_VERTBUF_DISCARD_SAFE(buffers->vert_buf);
-	/* match 'VertexBufferFormat' */
-	Gwn_VertFormat format = {0};
-	VertexBufferAttrID vbo_id;
-	gpu_pbvh_vert_format_init__gwn(&format, &vbo_id);
-
-	buffers->vert_buf = GWN_vertbuf_create_with_format(&format);
-	GWN_vertbuf_data_alloc(buffers->vert_buf, totvert);
-
 	/* Fill vertex buffer */
-	if (buffers->vert_buf->data) {
+	if (gpu_pbvh_vert_buf_data_set(buffers, totvert)) {
 		int v_index = 0;
 
 		if (buffers->smooth) {
@@ -1718,7 +1705,7 @@ void GPU_pbvh_bmesh_buffers_update(
 			GSET_ITER (gs_iter, bm_unique_verts) {
 				gpu_bmesh_vert_to_buffer_copy__gwn(
 				        BLI_gsetIterator_getKey(&gs_iter),
-				        buffers->vert_buf, &vbo_id, &v_index, NULL, NULL,
+				        buffers->vert_buf, &

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list