[Bf-blender-cvs] [8568d38f1b0] blender2.8: GWN: Add GWN_vertbuf_vertex_count_set.

Clément Foucault noreply at git.blender.org
Fri Mar 30 23:34:02 CEST 2018


Commit: 8568d38f1b0858a3138b72698babd6ba7b65d6b3
Author: Clément Foucault
Date:   Fri Mar 30 18:10:08 2018 +0200
Branches: blender2.8
https://developer.blender.org/rB8568d38f1b0858a3138b72698babd6ba7b65d6b3

GWN: Add GWN_vertbuf_vertex_count_set.

This allows us to specify a the number of vertices to upload to the gpu.
This is to keep the same allocation on the System Memory but send the least
amount of data to the GPU/Driver.

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

M	intern/gawain/gawain/gwn_vertex_buffer.h
M	intern/gawain/src/gwn_vertex_buffer.c

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

diff --git a/intern/gawain/gawain/gwn_vertex_buffer.h b/intern/gawain/gawain/gwn_vertex_buffer.h
index ddb77368a02..e9a37519b36 100644
--- a/intern/gawain/gawain/gwn_vertex_buffer.h
+++ b/intern/gawain/gawain/gwn_vertex_buffer.h
@@ -31,7 +31,8 @@ typedef enum {
 
 typedef struct Gwn_VertBuf {
 	Gwn_VertFormat format;
-	unsigned vertex_ct;
+	unsigned vertex_ct;    // number of verts we want to draw
+	unsigned vertex_alloc; // number of verts data
 	bool dirty;
 	GLubyte* data; // NULL indicates data in VRAM (unmapped)
 	GLuint vbo_id; // 0 indicates not yet allocated
@@ -55,6 +56,7 @@ void GWN_vertbuf_init_with_format_ex(Gwn_VertBuf*, const Gwn_VertFormat*, Gwn_Us
 unsigned GWN_vertbuf_size_get(const Gwn_VertBuf*);
 void GWN_vertbuf_data_alloc(Gwn_VertBuf*, unsigned v_ct);
 void GWN_vertbuf_data_resize(Gwn_VertBuf*, unsigned v_ct);
+void GWN_vertbuf_vertex_count_set(Gwn_VertBuf*, unsigned v_ct);
 
 // The most important set_attrib variant is the untyped one. Get it right first.
 // It takes a void* so the app developer is responsible for matching their app data types
diff --git a/intern/gawain/src/gwn_vertex_buffer.c b/intern/gawain/src/gwn_vertex_buffer.c
index 39fc1885b92..f621b4c01b9 100644
--- a/intern/gawain/src/gwn_vertex_buffer.c
+++ b/intern/gawain/src/gwn_vertex_buffer.c
@@ -93,7 +93,7 @@ void GWN_vertbuf_data_alloc(Gwn_VertBuf* verts, unsigned v_ct)
 
 #if TRUST_NO_ONE
 	// catch any unnecessary use
-	assert(verts->vertex_ct != v_ct || verts->data == NULL);
+	assert(verts->vertex_alloc != v_ct || verts->data == NULL);
 #endif
 
 	// only create the buffer the 1st time
@@ -105,16 +105,13 @@ void GWN_vertbuf_data_alloc(Gwn_VertBuf* verts, unsigned v_ct)
 		free(verts->data);
 
 #if VRAM_USAGE
-	vbo_memory_usage -= GWN_vertbuf_size_get(verts);
+	unsigned new_size = vertex_buffer_size(&verts->format, v_ct);
+	vbo_memory_usage += new_size - GWN_vertbuf_size_get(verts);
 #endif
 
 	verts->dirty = true;
-	verts->vertex_ct = v_ct;
+	verts->vertex_ct = verts->vertex_alloc = v_ct;
 	verts->data = malloc(sizeof(GLubyte) * GWN_vertbuf_size_get(verts));
-
-#if VRAM_USAGE
-	vbo_memory_usage += GWN_vertbuf_size_get(verts);
-#endif
 	}
 
 // resize buffer keeping existing data
@@ -122,20 +119,35 @@ void GWN_vertbuf_data_resize(Gwn_VertBuf* verts, unsigned v_ct)
 	{
 #if TRUST_NO_ONE
 	assert(verts->data != NULL);
-	assert(verts->vertex_ct != v_ct);
+	assert(verts->vertex_alloc != v_ct);
 #endif
 
 #if VRAM_USAGE
-	vbo_memory_usage -= GWN_vertbuf_size_get(verts);
+	unsigned new_size = vertex_buffer_size(&verts->format, v_ct);
+	vbo_memory_usage += new_size - GWN_vertbuf_size_get(verts);
 #endif
 
 	verts->dirty = true;
-	verts->vertex_ct = v_ct;
+	verts->vertex_ct = verts->vertex_alloc = v_ct;
 	verts->data = realloc(verts->data, sizeof(GLubyte) * GWN_vertbuf_size_get(verts));
+	}
+
+// set vertex count but does not change allocation
+// only this many verts will be uploaded to the GPU and rendered
+// this is usefull for streaming data
+void GWN_vertbuf_vertex_count_set(Gwn_VertBuf* verts, unsigned v_ct)
+	{
+#if TRUST_NO_ONE
+	assert(verts->data != NULL); // only for dynamic data
+	assert(v_ct <= verts->vertex_alloc);
+#endif
 
 #if VRAM_USAGE
-	vbo_memory_usage += GWN_vertbuf_size_get(verts);
+	unsigned new_size = vertex_buffer_size(&verts->format, v_ct);
+	vbo_memory_usage += new_size - GWN_vertbuf_size_get(verts);
 #endif
+
+	verts->vertex_ct = v_ct;
 	}
 
 void GWN_vertbuf_attr_set(Gwn_VertBuf* verts, unsigned a_idx, unsigned v_idx, const void* data)
@@ -145,7 +157,7 @@ void GWN_vertbuf_attr_set(Gwn_VertBuf* verts, unsigned a_idx, unsigned v_idx, co
 
 #if TRUST_NO_ONE
 	assert(a_idx < format->attrib_ct);
-	assert(v_idx < verts->vertex_ct);
+	assert(v_idx < verts->vertex_alloc);
 	assert(verts->data != NULL);
 #endif
 
@@ -210,7 +222,7 @@ void GWN_vertbuf_attr_get_raw_data(Gwn_VertBuf* verts, unsigned a_idx, Gwn_VertB
 	access->data = (GLubyte*)verts->data + a->offset;
 	access->data_init = access->data;
 #if TRUST_NO_ONE
-	access->_data_end = access->data_init + (size_t)(verts->vertex_ct * format->stride);
+	access->_data_end = access->data_init + (size_t)(verts->vertex_alloc * format->stride);
 #endif
 	}



More information about the Bf-blender-cvs mailing list