[Bf-blender-cvs] [4d972a5] GPU_data_request: keep vertex data in main mem or VRAM (not both)

Mike Erwin noreply at git.blender.org
Thu Apr 23 11:03:55 CEST 2015


Commit: 4d972a5606c300cfe19f299d913d09e6270e61da
Author: Mike Erwin
Date:   Thu Apr 23 03:06:30 2015 -0400
Branches: GPU_data_request
https://developer.blender.org/rB4d972a5606c300cfe19f299d913d09e6270e61da

keep vertex data in main mem or VRAM (not both)

If we’re using VBOs, free our own copy once GL has the data.

Data always lives in main memory while being constructed, so that we
don’t need to call any OpenGL functions until it’s time to draw.

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

M	source/blender/gpu/intern/gpux_element.c
M	source/blender/gpu/intern/gpux_vbo.c

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

diff --git a/source/blender/gpu/intern/gpux_element.c b/source/blender/gpu/intern/gpux_element.c
index 74c6e7b..1864b68 100644
--- a/source/blender/gpu/intern/gpux_element.c
+++ b/source/blender/gpu/intern/gpux_element.c
@@ -3,6 +3,11 @@
 #include "gpux_buffer_id.h"
 #include "MEM_guardedalloc.h"
 
+#ifdef USE_ELEM_VBO
+  /* keep index data in main mem or VRAM (not both) */
+  #define KEEP_SINGLE_COPY
+#endif
+
 /* private functions */
 
 #ifdef TRACK_INDEX_RANGE
@@ -86,7 +91,8 @@ void GPUx_element_list_discard(ElementList *el)
 		buffer_id_free(el->vbo_id);
 #endif /* USE_ELEM_VBO */
 
-	MEM_freeN(el->indices);
+	if (el->indices)
+		MEM_freeN(el->indices);
 	MEM_freeN(el);
 }
 
@@ -252,6 +258,11 @@ void GPUx_element_list_prime(ElementList *el)
 	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, el->vbo_id);
 	/* fill with delicious data & send to GPU the first time only */
 	glBufferData(GL_ELEMENT_ARRAY_BUFFER, GPUx_element_list_size(el), el->indices, GL_STATIC_DRAW);
+  #ifdef KEEP_SINGLE_COPY
+	/* now that GL has a copy, discard original */
+	MEM_freeN(el->indices);
+	el->indices = NULL;
+  #endif /* KEEP_SINGLE_COPY */
 	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
 #else
 	(void)el;
diff --git a/source/blender/gpu/intern/gpux_vbo.c b/source/blender/gpu/intern/gpux_vbo.c
index cce55b5..6beec63 100644
--- a/source/blender/gpu/intern/gpux_vbo.c
+++ b/source/blender/gpu/intern/gpux_vbo.c
@@ -15,6 +15,9 @@
 #ifdef USE_VBO
   #define USE_VAO
 
+  /* keep vertex data in main mem or VRAM (not both) */
+  #define KEEP_SINGLE_COPY
+
   #ifdef __linux__
     #define MESA_WORKAROUND
     /* For padded attributes (stride > size) Mesa likes the VBO to have some extra
@@ -121,7 +124,7 @@ void GPUx_attrib_print(const VertexBuffer *buff, unsigned attrib_num)
 #ifdef TRUST_NO_ONE
 	BLI_assert(attrib_num < buff->attrib_ct);
 	BLI_assert(a->comp_type >= GL_BYTE && a->comp_type <= GL_FLOAT);
-	BLI_assert(a->data != NULL); /* attribute must be specified */
+	BLI_assert(a->data != NULL); /* attribute must be specified & in main mem */
 #endif /* TRUST_NO_ONE */
 	if (a->comp_ct == 1)
 		printf("attrib %s %s = {\n", singular[type_idx], var_name);
@@ -168,7 +171,8 @@ void GPUx_vertex_buffer_discard(VertexBuffer *buff)
 #ifdef GENERIC_ATTRIB
 		MEM_freeN(a->name);
 #endif /* GENERIC_ATTRIB */
-		MEM_freeN(a->data);
+		if (a->data)
+			MEM_freeN(a->data);
 	}
 #ifdef USE_VAO
 	if (buff->vao_id)
@@ -285,7 +289,7 @@ void GPUx_set_attrib(VertexBuffer *buff, unsigned attrib_num, unsigned vertex_nu
 #ifdef TRUST_NO_ONE
 	BLI_assert(attrib_num < buff->attrib_ct);
 	BLI_assert(vertex_num < buff->vertex_ct);
-	BLI_assert(attrib->data != NULL); /* attribute must be specified */
+	BLI_assert(attrib->data != NULL); /* attribute must be specified & in main mem */
 #endif /* TRUST_NO_ONE */
 	memcpy((byte*)attrib->data + vertex_num * attrib->stride, data, attrib->sz);
 }
@@ -317,7 +321,7 @@ void GPUx_fill_attrib(VertexBuffer *buff, unsigned attrib_num, const void *data)
 	Attrib *attrib = buff->attribs + attrib_num;
 #ifdef TRUST_NO_ONE
 	BLI_assert(attrib_num < buff->attrib_ct);
-	BLI_assert(attrib->data != NULL); /* attribute must be specified */
+	BLI_assert(attrib->data != NULL); /* attribute must be specified & in main mem */
 #endif /* TRUST_NO_ONE */
 	if (attrib->sz == attrib->stride) {
 		/* tightly packed, so we can copy it all at once */
@@ -337,7 +341,7 @@ void GPUx_fill_attrib_stride(VertexBuffer *buff, unsigned attrib_num, const void
 	Attrib *attrib = buff->attribs + attrib_num;
 #ifdef TRUST_NO_ONE
 	BLI_assert(attrib_num < buff->attrib_ct);
-	BLI_assert(attrib->data != NULL); /* attribute must be specified */
+	BLI_assert(attrib->data != NULL); /* attribute must be specified & in main mem */
 	BLI_assert(stride >= attrib->sz); /* no overlapping attributes (legal but weird) */
 #endif /* TRUST_NO_ONE */
 	if (stride == attrib->stride) {
@@ -385,6 +389,11 @@ void GPUx_vertex_buffer_use(VertexBuffer *buff)
 			glBindBuffer(GL_ARRAY_BUFFER, a->vbo_id);
 			/* fill with delicious data & send to GPU the first time only */
 			glBufferData(GL_ARRAY_BUFFER, attrib_total_size(buff, a_idx), a->data, GL_STATIC_DRAW);
+  #ifdef KEEP_SINGLE_COPY
+			/* now that GL has a copy, discard original */
+			MEM_freeN(a->data);
+			a->data = NULL;
+  #endif /* KEEP_SINGLE_COPY */
 		}
 
 		data = 0;
@@ -465,6 +474,11 @@ void GPUx_vertex_buffer_prime(VertexBuffer *buff)
 		glBindBuffer(GL_ARRAY_BUFFER, a->vbo_id);
 		/* fill with delicious data & send to GPU the first time only */
 		glBufferData(GL_ARRAY_BUFFER, attrib_total_size(buff, a_idx), a->data, GL_STATIC_DRAW);
+  #ifdef KEEP_SINGLE_COPY
+		/* now that GL has a copy, discard original */
+		MEM_freeN(a->data);
+		a->data = NULL;
+  #endif /* KEEP_SINGLE_COPY */
 
   #ifdef USE_VAO
     #ifdef GENERIC_ATTRIB




More information about the Bf-blender-cvs mailing list