[Bf-blender-cvs] [f997449] master: OpenSubdiv: support OpenGL 3.x

Mike Erwin noreply at git.blender.org
Mon Nov 23 09:36:42 CET 2015


Commit: f997449f841f8229680f9e0f0beed4809b9111ab
Author: Mike Erwin
Date:   Mon Nov 23 03:33:49 2015 -0500
Branches: master
https://developer.blender.org/rBf997449f841f8229680f9e0f0beed4809b9111ab

OpenSubdiv: support OpenGL 3.x

GLSL 130, 140, 150 with extensions as needed.

Similar logic to my recent gpu_extensions changes.

Partially fixes T46706. Matcaps now work with OpenSubdiv, as do basic
materials. Anything with UV coordinates is still broken.

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

M	intern/opensubdiv/gpu_shader_opensubd_display.glsl
M	intern/opensubdiv/opensubdiv_capi.cc
M	intern/opensubdiv/opensubdiv_gpu_capi.cc

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

diff --git a/intern/opensubdiv/gpu_shader_opensubd_display.glsl b/intern/opensubdiv/gpu_shader_opensubd_display.glsl
index e2574b5..d32a178 100644
--- a/intern/opensubdiv/gpu_shader_opensubd_display.glsl
+++ b/intern/opensubdiv/gpu_shader_opensubd_display.glsl
@@ -23,20 +23,13 @@
  * ***** END GPL LICENSE BLOCK *****
  */
 
-/* ***** Vertex shader ***** */
-
-#extension GL_EXT_geometry_shader4 : enable
-#extension GL_ARB_gpu_shader5 : enable
-#extension GL_ARB_explicit_attrib_location : require
-#extension GL_ARB_uniform_buffer_object : require
-
 struct VertexData {
 	vec4 position;
 	vec3 normal;
 	vec2 uv;
 };
 
-#ifdef VERTEX_SHADER
+#ifdef VERTEX_SHADER // ---------------------
 
 in vec3 normal;
 in vec4 position;
@@ -52,24 +45,32 @@ void main()
 {
 	outpt.v.position = modelViewMatrix * position;
 	outpt.v.normal = normalize(normalMatrix * normal);
+
+#if __VERSION__ < 140
 	/* Some compilers expects gl_Position to be written.
 	 * It's not needed once we explicitly switch to GLSL 1.40 or above.
 	 */
 	gl_Position = outpt.v.position;
+#endif
 }
 
-#endif  /* VERTEX_SHADER */
+#elif defined GEOMETRY_SHADER // ---------------------
 
-/* ***** geometry shader ***** */
-#ifdef GEOMETRY_SHADER
-
-#ifndef GLSL_COMPAT_WORKAROUND
-layout(lines_adjacency) in;
-#ifndef WIREFRAME
-layout(triangle_strip, max_vertices = 4) out;
+#if __VERSION__ >= 150
+  layout(lines_adjacency) in;
+  #ifdef WIREFRAME
+    layout(line_strip, max_vertices = 8) out;
+  #else
+    layout(triangle_strip, max_vertices = 4) out;
+  #endif
 #else
-layout(line_strip, max_vertices = 8) out;
+  #extension GL_EXT_geometry_shader4: require
+  /* application provides input/output layout info */
 #endif
+
+#if __VERSION__ < 140
+  #extension GL_ARB_uniform_buffer_object: require
+  #extension GL_ARB_texture_buffer_object: require
 #endif
 
 uniform mat4 modelViewMatrix;
@@ -188,10 +189,7 @@ void main()
 	EndPrimitive();
 }
 
-#endif  /* GEOMETRY_SHADER */
-
-/* ***** Fragment shader ***** */
-#ifdef FRAGMENT_SHADER
+#elif defined FRAGMENT_SHADER // ---------------------
 
 #define MAX_LIGHTS 8
 #define NUM_SOLID_LIGHTS 3
@@ -330,4 +328,4 @@ void main()
 #endif
 }
 
-#endif  // FRAGMENT_SHADER
+#endif // ---------------------
diff --git a/intern/opensubdiv/opensubdiv_capi.cc b/intern/opensubdiv/opensubdiv_capi.cc
index 1fd9c0b..f715bf3 100644
--- a/intern/opensubdiv/opensubdiv_capi.cc
+++ b/intern/opensubdiv/opensubdiv_capi.cc
@@ -296,9 +296,8 @@ const struct OpenSubdiv_TopologyRefinerDescr *openSubdiv_getGLMeshTopologyRefine
 int openSubdiv_supportGPUDisplay(void)
 {
 	// TODO: simplify extension check once Blender adopts GL 3.2
-	return GLEW_VERSION_4_0 || (
-	       GLEW_EXT_geometry_shader4 && // ARB version core in 3.2
-	       GLEW_ARB_gpu_shader5 && // written against 3.2, core in 4.0
-	       GLEW_ARB_uniform_buffer_object // core in 3.1
-	       );
+	return (GLEW_VERSION_3_2 && GLEW_ARB_compatibility) ||
+	       (GLEW_VERSION_3_1 && GLEW_ARB_compatibility && GLEW_EXT_geometry_shader4) ||
+	       (GLEW_VERSION_3_0 && GLEW_EXT_geometry_shader4 && GLEW_ARB_uniform_buffer_object && GLEW_ARB_texture_buffer_object);
+	/* also ARB_explicit_attrib_location? */
 }
diff --git a/intern/opensubdiv/opensubdiv_gpu_capi.cc b/intern/opensubdiv/opensubdiv_gpu_capi.cc
index 27c6d10..fc46ad0 100644
--- a/intern/opensubdiv/opensubdiv_gpu_capi.cc
+++ b/intern/opensubdiv/opensubdiv_gpu_capi.cc
@@ -23,11 +23,6 @@
  * ***** END GPL LICENSE BLOCK *****
  */
 
-/* Do some compatibility hacks in order to make
- * the code working with GPU_material pipeline.
- */
-#define GLSL_COMPAT_WORKAROUND
-
 #include "opensubdiv_capi.h"
 
 #ifdef _MSC_VER
@@ -193,16 +188,31 @@ GLuint compileShader(GLenum shaderType,
                      const char *section,
                      const char *define)
 {
-	const char *sources[3];
 	char sdefine[64];
-	sprintf(sdefine, "#define %s\n#define GLSL_COMPAT_WORKAROUND\n", section);
+	sprintf(sdefine, "#define %s\n", section);
 
-	sources[0] = define;
-	sources[1] = sdefine;
-	sources[2] = datatoc_gpu_shader_opensubd_display_glsl;
+	const char *version;
+	if (GLEW_VERSION_3_2 && GLEW_ARB_compatibility) {
+		version = "#version 150 compatibility\n";
+	}
+	else if (GLEW_VERSION_3_1 && GLEW_ARB_compatibility) {
+		version = "#version 140\n"
+		          "#extension GL_ARB_compatibility: enable\n";
+	}
+	else if (GLEW_VERSION_3_0) {
+		version = "#version 130\n";
+		/* minimum supported for OpenSubdiv */
+	}
+
+	const char *sources[] = {
+		version,
+		define,
+		sdefine,
+		datatoc_gpu_shader_opensubd_display_glsl
+	};
 
 	GLuint shader = glCreateShader(shaderType);
-	glShaderSource(shader, 3, sources, NULL);
+	glShaderSource(shader, 4, sources, NULL);
 	glCompileShader(shader);
 
 	GLint status;
@@ -210,10 +220,10 @@ GLuint compileShader(GLenum shaderType,
 	if (status == GL_FALSE) {
 		GLchar emsg[1024];
 		glGetShaderInfoLog(shader, sizeof(emsg), 0, emsg);
-		fprintf(stderr, "Error compiling GLSL shader (%s): %s\n", section, emsg);
-		fprintf(stderr, "Section: %s\n", sdefine);
+		fprintf(stderr, "Error compiling GLSL %s: %s\n", section, emsg);
+		fprintf(stderr, "Version: %s\n", version);
 		fprintf(stderr, "Defines: %s\n", define);
-		fprintf(stderr, "Source: %s\n", sources[2]);
+		fprintf(stderr, "Source: %s\n", datatoc_gpu_shader_opensubd_display_glsl);
 		return 0;
 	}
 
@@ -250,30 +260,23 @@ GLuint linkProgram(const char *define)
 	glBindAttribLocation(program, 0, "position");
 	glBindAttribLocation(program, 1, "normal");
 
-#ifdef GLSL_COMPAT_WORKAROUND
-	glProgramParameteriEXT(program,
-	                       GL_GEOMETRY_INPUT_TYPE_EXT,
-	                       GL_LINES_ADJACENCY_EXT);
 
-	if (strstr(define, "WIREFRAME") == NULL) {
+	if (!(GLEW_VERSION_3_2 && GLEW_ARB_compatibility)) {
+		/* provide input/output layout info */
 		glProgramParameteriEXT(program,
-		                       GL_GEOMETRY_OUTPUT_TYPE_EXT,
-		                       GL_TRIANGLE_STRIP);
+		                       GL_GEOMETRY_INPUT_TYPE_EXT,
+		                       GL_LINES_ADJACENCY_EXT);
+
+		bool wireframe = strstr(define, "WIREFRAME") != NULL;
 
-		glProgramParameteriEXT(program,
-		                       GL_GEOMETRY_VERTICES_OUT_EXT,
-		                       4);
-	}
-	else {
 		glProgramParameteriEXT(program,
 		                       GL_GEOMETRY_OUTPUT_TYPE_EXT,
-		                       GL_LINE_STRIP);
+		                       wireframe ? GL_LINE_STRIP : GL_TRIANGLE_STRIP);
 
 		glProgramParameteriEXT(program,
 		                       GL_GEOMETRY_VERTICES_OUT_EXT,
 		                       8);
 	}
-#endif
 
 	glLinkProgram(program);




More information about the Bf-blender-cvs mailing list