From 378bff0eddf004d131a4c83194fb3e83492c4c37 Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Wed, 12 Aug 2009 13:50:26 -0600 Subject: progs/util: added more shader utility functions --- progs/util/shaderutil.c | 197 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 179 insertions(+), 18 deletions(-) (limited to 'progs/util/shaderutil.c') diff --git a/progs/util/shaderutil.c b/progs/util/shaderutil.c index 13b68d90e0..bd04ce5c6a 100644 --- a/progs/util/shaderutil.c +++ b/progs/util/shaderutil.c @@ -9,21 +9,12 @@ #include #include #include +#include #include #include #include "shaderutil.h" -static void -Init(void) -{ - static GLboolean firstCall = GL_TRUE; - if (firstCall) { - firstCall = GL_FALSE; - } -} - - GLboolean ShadersSupported(void) { @@ -47,8 +38,6 @@ CompileShaderText(GLenum shaderType, const char *text) GLuint shader; GLint stat; - Init(); - shader = glCreateShader(shaderType); glShaderSource(shader, 1, (const GLchar **) &text, NULL); glCompileShader(shader); @@ -79,9 +68,6 @@ CompileShaderFile(GLenum shaderType, const char *filename) GLuint shader; FILE *f; - Init(); - - f = fopen(filename, "r"); if (!f) { fprintf(stderr, "Unable to open shader file %s\n", filename); @@ -144,9 +130,6 @@ InitUniforms(GLuint program, struct uniform_info uniforms[]) uniforms[i].location = glGetUniformLocation(program, uniforms[i].name); - printf("Uniform %s location: %d\n", uniforms[i].name, - uniforms[i].location); - switch (uniforms[i].size) { case 1: if (uniforms[i].type == GL_INT) @@ -169,3 +152,181 @@ InitUniforms(GLuint program, struct uniform_info uniforms[]) } } } + + +/** Get list of uniforms used in the program */ +GLuint +GetUniforms(GLuint program, struct uniform_info uniforms[]) +{ + GLint n, max, i; + + glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &n); + glGetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max); + + for (i = 0; i < n; i++) { + GLint size, len; + GLenum type; + char name[100]; + + glGetActiveUniform(program, i, 100, &len, &size, &type, name); + + uniforms[i].name = strdup(name); + switch (type) { + case GL_FLOAT: + size = 1; + type = GL_FLOAT; + break; + case GL_FLOAT_VEC2: + size = 2; + type = GL_FLOAT; + break; + case GL_FLOAT_VEC3: + size = 3; + type = GL_FLOAT; + break; + case GL_FLOAT_VEC4: + size = 4; + type = GL_FLOAT; + break; + case GL_INT: + size = 1; + type = GL_INT; + break; + case GL_INT_VEC2: + size = 2; + type = GL_INT; + break; + case GL_INT_VEC3: + size = 3; + type = GL_INT; + break; + case GL_INT_VEC4: + size = 4; + type = GL_INT; + break; + case GL_FLOAT_MAT3: + /* XXX fix me */ + size = 3; + type = GL_FLOAT; + break; + case GL_FLOAT_MAT4: + /* XXX fix me */ + size = 4; + type = GL_FLOAT; + break; + default: + abort(); + } + uniforms[i].size = size; + uniforms[i].type = type; + uniforms[i].location = glGetUniformLocation(program, name); + } + + uniforms[i].name = NULL; /* end of list */ + + return n; +} + + +void +PrintUniforms(const struct uniform_info uniforms[]) +{ + GLint i; + + printf("Uniforms:\n"); + + for (i = 0; uniforms[i].name; i++) { + printf(" %d: %s size=%d type=0x%x loc=%d value=%g, %g, %g, %g\n", + i, + uniforms[i].name, + uniforms[i].size, + uniforms[i].type, + uniforms[i].location, + uniforms[i].value[0], + uniforms[i].value[1], + uniforms[i].value[2], + uniforms[i].value[3]); + } +} + + +/** Get list of attribs used in the program */ +GLuint +GetAttribs(GLuint program, struct attrib_info attribs[]) +{ + GLint n, max, i; + + glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &n); + glGetProgramiv(program, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &max); + + for (i = 0; i < n; i++) { + GLint size, len; + GLenum type; + char name[100]; + + glGetActiveAttrib(program, i, 100, &len, &size, &type, name); + + attribs[i].name = strdup(name); + switch (type) { + case GL_FLOAT: + size = 1; + type = GL_FLOAT; + break; + case GL_FLOAT_VEC2: + size = 2; + type = GL_FLOAT; + break; + case GL_FLOAT_VEC3: + size = 3; + type = GL_FLOAT; + break; + case GL_FLOAT_VEC4: + size = 4; + type = GL_FLOAT; + break; + case GL_INT: + size = 1; + type = GL_INT; + break; + case GL_INT_VEC2: + size = 2; + type = GL_INT; + break; + case GL_INT_VEC3: + size = 3; + type = GL_INT; + break; + case GL_INT_VEC4: + size = 4; + type = GL_INT; + break; + default: + abort(); + } + attribs[i].size = size; + attribs[i].type = type; + attribs[i].location = glGetAttribLocation(program, name); + } + + attribs[i].name = NULL; /* end of list */ + + return n; +} + + +void +PrintAttribs(const struct attrib_info attribs[]) +{ + GLint i; + + printf("Attribs:\n"); + + for (i = 0; attribs[i].name; i++) { + printf(" %d: %s size=%d type=0x%x loc=%d\n", + i, + attribs[i].name, + attribs[i].size, + attribs[i].type, + attribs[i].location); + } +} -- cgit v1.2.3 From 684049d97d423a5a873aefc5313d0c4b22528b95 Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Wed, 12 Aug 2009 13:53:56 -0600 Subject: demos: rename InitUniforms() to SetUniformValues() And call new PrintUniforms() in demos. --- progs/util/shaderutil.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'progs/util/shaderutil.c') diff --git a/progs/util/shaderutil.c b/progs/util/shaderutil.c index bd04ce5c6a..f057adf5c7 100644 --- a/progs/util/shaderutil.c +++ b/progs/util/shaderutil.c @@ -122,7 +122,7 @@ LinkShaders(GLuint vertShader, GLuint fragShader) void -InitUniforms(GLuint program, struct uniform_info uniforms[]) +SetUniformValues(GLuint program, struct uniform_info uniforms[]) { GLuint i; -- cgit v1.2.3 From fdfb0d4b0e04bff2f3dbae2d1f8e3765fb4b0dce Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Wed, 12 Aug 2009 17:25:49 -0600 Subject: progs/glsl: change uniform_info::type field to use GLSL vector types --- progs/util/shaderutil.c | 107 +++++++----------------------------------------- 1 file changed, 15 insertions(+), 92 deletions(-) (limited to 'progs/util/shaderutil.c') diff --git a/progs/util/shaderutil.c b/progs/util/shaderutil.c index f057adf5c7..233252112a 100644 --- a/progs/util/shaderutil.c +++ b/progs/util/shaderutil.c @@ -130,21 +130,26 @@ SetUniformValues(GLuint program, struct uniform_info uniforms[]) uniforms[i].location = glGetUniformLocation(program, uniforms[i].name); - switch (uniforms[i].size) { - case 1: - if (uniforms[i].type == GL_INT) - glUniform1i(uniforms[i].location, - (GLint) uniforms[i].value[0]); - else - glUniform1fv(uniforms[i].location, 1, uniforms[i].value); + switch (uniforms[i].type) { + case GL_INT: + case GL_SAMPLER_1D: + case GL_SAMPLER_2D: + case GL_SAMPLER_3D: + case GL_SAMPLER_CUBE: + case GL_SAMPLER_2D_RECT_ARB: + glUniform1i(uniforms[i].location, + (GLint) uniforms[i].value[0]); + break; + case GL_FLOAT: + glUniform1fv(uniforms[i].location, 1, uniforms[i].value); break; - case 2: + case GL_FLOAT_VEC2: glUniform2fv(uniforms[i].location, 1, uniforms[i].value); break; - case 3: + case GL_FLOAT_VEC3: glUniform3fv(uniforms[i].location, 1, uniforms[i].value); break; - case 4: + case GL_FLOAT_VEC4: glUniform4fv(uniforms[i].location, 1, uniforms[i].value); break; default: @@ -171,52 +176,6 @@ GetUniforms(GLuint program, struct uniform_info uniforms[]) glGetActiveUniform(program, i, 100, &len, &size, &type, name); uniforms[i].name = strdup(name); - switch (type) { - case GL_FLOAT: - size = 1; - type = GL_FLOAT; - break; - case GL_FLOAT_VEC2: - size = 2; - type = GL_FLOAT; - break; - case GL_FLOAT_VEC3: - size = 3; - type = GL_FLOAT; - break; - case GL_FLOAT_VEC4: - size = 4; - type = GL_FLOAT; - break; - case GL_INT: - size = 1; - type = GL_INT; - break; - case GL_INT_VEC2: - size = 2; - type = GL_INT; - break; - case GL_INT_VEC3: - size = 3; - type = GL_INT; - break; - case GL_INT_VEC4: - size = 4; - type = GL_INT; - break; - case GL_FLOAT_MAT3: - /* XXX fix me */ - size = 3; - type = GL_FLOAT; - break; - case GL_FLOAT_MAT4: - /* XXX fix me */ - size = 4; - type = GL_FLOAT; - break; - default: - abort(); - } uniforms[i].size = size; uniforms[i].type = type; uniforms[i].location = glGetUniformLocation(program, name); @@ -267,42 +226,6 @@ GetAttribs(GLuint program, struct attrib_info attribs[]) glGetActiveAttrib(program, i, 100, &len, &size, &type, name); attribs[i].name = strdup(name); - switch (type) { - case GL_FLOAT: - size = 1; - type = GL_FLOAT; - break; - case GL_FLOAT_VEC2: - size = 2; - type = GL_FLOAT; - break; - case GL_FLOAT_VEC3: - size = 3; - type = GL_FLOAT; - break; - case GL_FLOAT_VEC4: - size = 4; - type = GL_FLOAT; - break; - case GL_INT: - size = 1; - type = GL_INT; - break; - case GL_INT_VEC2: - size = 2; - type = GL_INT; - break; - case GL_INT_VEC3: - size = 3; - type = GL_INT; - break; - case GL_INT_VEC4: - size = 4; - type = GL_INT; - break; - default: - abort(); - } attribs[i].size = size; attribs[i].type = type; attribs[i].location = glGetAttribLocation(program, name); -- cgit v1.2.3 From 741869d73aa8c9d0d9ae8f1c4ca2df32e235960a Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Thu, 13 Aug 2009 12:53:20 -0600 Subject: progs/util: ignore pre-defined uniforms in SetUniformValues() --- progs/util/shaderutil.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'progs/util/shaderutil.c') diff --git a/progs/util/shaderutil.c b/progs/util/shaderutil.c index 233252112a..489e71cc30 100644 --- a/progs/util/shaderutil.c +++ b/progs/util/shaderutil.c @@ -153,7 +153,14 @@ SetUniformValues(GLuint program, struct uniform_info uniforms[]) glUniform4fv(uniforms[i].location, 1, uniforms[i].value); break; default: - abort(); + if (strncmp(uniforms[i].name, "gl_", 3) == 0) { + /* built-in uniform: ignore */ + } + else { + fprintf(stderr, + "Unexpected uniform data type in SetUniformValues\n"); + abort(); + } } } } -- cgit v1.2.3