[Bf-blender-cvs] [31298b7] strand_gpu: Generate a buffer texture for passing the control strand locations to the shader.

Lukas Tönne noreply at git.blender.org
Tue Jul 5 09:57:09 CEST 2016


Commit: 31298b75499fdeeaace834dec51583dd5c560dc5
Author: Lukas Tönne
Date:   Fri Jul 1 17:12:12 2016 +0200
Branches: strand_gpu
https://developer.blender.org/rB31298b75499fdeeaace834dec51583dd5c560dc5

Generate a buffer texture for passing the control strand locations to the shader.

This should enable the strand geometry shader to interpolate control strands
with appropriate vertex attributes for indices and weights.

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

M	source/blender/editors/space_view3d/drawstrands.c
M	source/blender/gpu/GPU_buffers.h
M	source/blender/gpu/intern/gpu_buffers.c

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

diff --git a/source/blender/editors/space_view3d/drawstrands.c b/source/blender/editors/space_view3d/drawstrands.c
index 0bb9c1f..ecb810e 100644
--- a/source/blender/editors/space_view3d/drawstrands.c
+++ b/source/blender/editors/space_view3d/drawstrands.c
@@ -76,7 +76,7 @@ void draw_strands(Strands *strands, StrandData *data, Object *ob, RegionView3D *
 		if (gds->root_points) {
 			glDrawArrays(GL_POINTS, 0, gds->totroots * 3);
 		}
-		GPU_buffers_unbind();
+		GPU_strands_buffer_unbind();
 		
 		GPU_strand_shader_unbind(gpu_shader);
 	}
diff --git a/source/blender/gpu/GPU_buffers.h b/source/blender/gpu/GPU_buffers.h
index 155a7eb..71829b7 100644
--- a/source/blender/gpu/GPU_buffers.h
+++ b/source/blender/gpu/GPU_buffers.h
@@ -144,11 +144,18 @@ typedef struct GPUVertPointLink {
 	int point_index;
 } GPUVertPointLink;
 
+typedef struct GPUBufferTexture {
+	unsigned int id;
+} GPUBufferTexture;
+
 typedef struct GPUDrawStrands {
 	GPUBuffer *control_points;
 	GPUBuffer *control_edges;
 	GPUBuffer *root_points;
 
+	/* GL texture id for root point texture buffer */
+	GPUBufferTexture root_tex;
+
 	unsigned int totverts;
 	unsigned int totcurves;
 	unsigned int totroots;
@@ -284,6 +291,9 @@ void GPU_free_pbvh_buffer_multires(struct GridCommonGPUBuffer **grid_common_gpu_
 void GPU_strands_setup_verts(struct StrandData *strands);
 void GPU_strands_setup_edges(struct StrandData *strands);
 void GPU_strands_setup_roots(struct StrandData *strands);
+
+void GPU_strands_buffer_unbind(void);
+
 void GPU_strands_buffer_free(struct StrandData *strands);
 
 #endif
diff --git a/source/blender/gpu/intern/gpu_buffers.c b/source/blender/gpu/intern/gpu_buffers.c
index 0d7bbc6..8656e3b 100644
--- a/source/blender/gpu/intern/gpu_buffers.c
+++ b/source/blender/gpu/intern/gpu_buffers.c
@@ -2102,11 +2102,11 @@ typedef enum GPUStrandBufferType {
 } GPUStrandBufferType;
 
 const GPUBufferTypeSettings gpu_strand_buffer_type_settings[] = {
-    /* control vertex */
+    /* CONTROL_VERTEX */
     {GL_ARRAY_BUFFER, 3},
-    /* control edge */
+    /* CONTROL_EDGE */
     {GL_ELEMENT_ARRAY_BUFFER, 2},
-    /* root vertex */
+    /* ROOT_VERTEX */
     {GL_ARRAY_BUFFER, 3},
 };
 
@@ -2125,6 +2125,19 @@ static GPUBuffer **gpu_strands_buffer_from_type(GPUDrawStrands *gds, GPUStrandBu
 	}
 }
 
+static GPUBufferTexture *gpu_strands_buffer_texture_from_type(GPUDrawStrands *gds, GPUStrandBufferType type,
+                                                              GLenum *format)
+{
+	switch (type) {
+		case GPU_STRAND_BUFFER_ROOT_VERTEX:
+			*format = GL_RGB32F;
+			return &gds->root_tex;
+		default:
+			*format = 0;
+			return NULL;
+	}
+}
+
 /* get the amount of space to allocate for a buffer of a particular type */
 static size_t gpu_strands_buffer_size_from_type(StrandData *strands, GPUStrandBufferType type)
 {
@@ -2267,9 +2280,21 @@ static GPUBuffer *strands_setup_buffer_type(StrandData *strands, GPUStrandBuffer
 	return buffer;
 }
 
+static void strands_setup_buffer_texture(StrandData *UNUSED(strands), GPUBuffer *buffer, GLenum format,
+                                         GPUBufferTexture *tex)
+{
+	glGenTextures(1, &tex->id);
+	glBindTexture(GL_TEXTURE_BUFFER, tex->id);
+	
+	glTexBuffer(GL_TEXTURE_BUFFER, format, buffer->id);
+	
+	glBindTexture(GL_TEXTURE_BUFFER, 0);
+}
+
 static bool strands_setup_buffer_common(StrandData *strands, GPUStrandBufferType type, bool update)
 {
 	GPUBuffer **buf;
+	GPUBufferTexture *tex;
 	
 	if (!strands->gpu_buffer)
 		strands->gpu_buffer = strands_buffer_create(strands);
@@ -2277,6 +2302,13 @@ static bool strands_setup_buffer_common(StrandData *strands, GPUStrandBufferType
 	buf = gpu_strands_buffer_from_type(strands->gpu_buffer, type);
 	if (*buf == NULL || update) {
 		*buf = strands_setup_buffer_type(strands, type, *buf);
+		
+		if (*buf) {
+			GLenum tex_format;
+			tex = gpu_strands_buffer_texture_from_type(strands->gpu_buffer, type, &tex_format);
+			if (tex)
+				strands_setup_buffer_texture(strands, *buf, tex_format, tex);
+		}
 	}
 	
 	return *buf != NULL;
@@ -2320,6 +2352,20 @@ void GPU_strands_setup_roots(StrandData *strands)
 	glVertexPointer(3, GL_FLOAT, 0, NULL);
 
 	GLStates |= (GPU_BUFFER_VERTEX_STATE);
+
+	glActiveTexture(GL_TEXTURE0);
+	if (strands->gpu_buffer->root_tex.id != 0) {
+		glBindTexture(GL_TEXTURE_BUFFER, strands->gpu_buffer->root_tex.id);
+	}
+}
+
+void GPU_strands_buffer_unbind(void)
+{
+	GPU_buffers_unbind();
+	
+	glActiveTexture(GL_TEXTURE0);
+	glBindTexture(GL_TEXTURE_BUFFER, 0);
+	glDisable(GL_TEXTURE_BUFFER);
 }
 
 void GPU_strands_buffer_free(StrandData *strands)
@@ -2327,10 +2373,15 @@ void GPU_strands_buffer_free(StrandData *strands)
 	if (strands && strands->gpu_buffer) {
 		GPUDrawStrands *gds = strands->gpu_buffer;
 		
+#if 0 /* XXX crashes, maybe not needed for buffer textures? */
+		if (gds->root_tex.id)
+			glDeleteTextures(1, &gds->root_tex.id);
+#endif
+		
 		GPU_buffer_free(gds->control_points);
 		GPU_buffer_free(gds->control_edges);
 		GPU_buffer_free(gds->root_points);
-	
+		
 		MEM_freeN(gds);
 		strands->gpu_buffer = NULL;
 	}




More information about the Bf-blender-cvs mailing list