diff options
Diffstat (limited to 'progs/util')
-rw-r--r-- | progs/util/extfuncs.h | 1 | ||||
-rw-r--r-- | progs/util/glutskel.c | 64 | ||||
-rw-r--r-- | progs/util/shaderutil.c | 200 | ||||
-rw-r--r-- | progs/util/shaderutil.h | 35 |
4 files changed, 237 insertions, 63 deletions
diff --git a/progs/util/extfuncs.h b/progs/util/extfuncs.h index 0469e2f2c4..2bb57030a8 100644 --- a/progs/util/extfuncs.h +++ b/progs/util/extfuncs.h @@ -137,7 +137,6 @@ static PFNGLRENDERBUFFERSTORAGEMULTISAMPLEPROC glRenderbufferStorageMultisample_ static PFNGLFRAMEBUFFERTEXTURELAYERPROC glFramebufferTextureLayer_func = NULL; - static void GetExtensionFuncs(void) { diff --git a/progs/util/glutskel.c b/progs/util/glutskel.c index 273ed9a2f5..8499e12a9d 100644 --- a/progs/util/glutskel.c +++ b/progs/util/glutskel.c @@ -11,6 +11,7 @@ #include <GL/glut.h> static int Win; +static int WinWidth = 400, WinHeight = 400; static GLfloat Xrot = 0, Yrot = 0, Zrot = 0; static GLboolean Anim = GL_FALSE; @@ -46,6 +47,8 @@ Draw(void) static void Reshape(int width, int height) { + WinWidth = width; + WinHeight = height; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); @@ -63,23 +66,23 @@ Key(unsigned char key, int x, int y) (void) x; (void) y; switch (key) { - case 'a': - Anim = !Anim; - if (Anim) - glutIdleFunc(Idle); - else - glutIdleFunc(NULL); - break; - case 'z': - Zrot -= step; - break; - case 'Z': - Zrot += step; - break; - case 27: - glutDestroyWindow(Win); - exit(0); - break; + case 'a': + Anim = !Anim; + if (Anim) + glutIdleFunc(Idle); + else + glutIdleFunc(NULL); + break; + case 'z': + Zrot -= step; + break; + case 'Z': + Zrot += step; + break; + case 27: + glutDestroyWindow(Win); + exit(0); + break; } glutPostRedisplay(); } @@ -92,18 +95,18 @@ SpecialKey(int key, int x, int y) (void) x; (void) y; switch (key) { - case GLUT_KEY_UP: - Xrot -= step; - break; - case GLUT_KEY_DOWN: - Xrot += step; - break; - case GLUT_KEY_LEFT: - Yrot -= step; - break; - case GLUT_KEY_RIGHT: - Yrot += step; - break; + case GLUT_KEY_UP: + Xrot -= step; + break; + case GLUT_KEY_DOWN: + Xrot += step; + break; + case GLUT_KEY_LEFT: + Yrot -= step; + break; + case GLUT_KEY_RIGHT: + Yrot += step; + break; } glutPostRedisplay(); } @@ -123,8 +126,7 @@ int main(int argc, char *argv[]) { glutInit(&argc, argv); - glutInitWindowPosition(0, 0); - glutInitWindowSize(400, 400); + glutInitWindowSize(WinWidth, WinHeight); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); Win = glutCreateWindow(argv[0]); glutReshapeFunc(Reshape); diff --git a/progs/util/shaderutil.c b/progs/util/shaderutil.c index 13b68d90e0..4db950016b 100644 --- a/progs/util/shaderutil.c +++ b/progs/util/shaderutil.c @@ -9,19 +9,16 @@ #include <assert.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <GL/glew.h> #include <GL/glut.h> #include "shaderutil.h" +/** time to compile previous shader */ +static GLdouble CompileTime = 0.0; -static void -Init(void) -{ - static GLboolean firstCall = GL_TRUE; - if (firstCall) { - firstCall = GL_FALSE; - } -} +/** time to linke previous program */ +static GLdouble LinkTime = 0.0; GLboolean @@ -46,12 +43,17 @@ CompileShaderText(GLenum shaderType, const char *text) { GLuint shader; GLint stat; - - Init(); + GLdouble t0, t1; shader = glCreateShader(shaderType); glShaderSource(shader, 1, (const GLchar **) &text, NULL); + + t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001; glCompileShader(shader); + t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001; + + CompileTime = t1 - t0; + glGetShaderiv(shader, GL_COMPILE_STATUS, &stat); if (!stat) { GLchar log[1000]; @@ -79,9 +81,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); @@ -109,6 +108,7 @@ GLuint LinkShaders(GLuint vertShader, GLuint fragShader) { GLuint program = glCreateProgram(); + GLdouble t0, t1; assert(vertShader || fragShader); @@ -116,7 +116,12 @@ LinkShaders(GLuint vertShader, GLuint fragShader) glAttachShader(program, fragShader); if (vertShader) glAttachShader(program, vertShader); + + t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001; glLinkProgram(program); + t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001; + + LinkTime = t1 - t0; /* check link */ { @@ -135,8 +140,41 @@ LinkShaders(GLuint vertShader, GLuint fragShader) } +GLboolean +ValidateShaderProgram(GLuint program) +{ + GLint stat; + glValidateProgramARB(program); + glGetProgramiv(program, GL_VALIDATE_STATUS, &stat); + + if (!stat) { + GLchar log[1000]; + GLsizei len; + glGetProgramInfoLog(program, 1000, &len, log); + fprintf(stderr, "Program validation error:\n%s\n", log); + return 0; + } + + return (GLboolean) stat; +} + + +GLdouble +GetShaderCompileTime(void) +{ + return CompileTime; +} + + +GLdouble +GetShaderLinkTime(void) +{ + return LinkTime; +} + + void -InitUniforms(GLuint program, struct uniform_info uniforms[]) +SetUniformValues(GLuint program, struct uniform_info uniforms[]) { GLuint i; @@ -144,28 +182,134 @@ 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) - 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: + assert(uniforms[i].value[0] >= 0.0F); + 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: - abort(); + if (strncmp(uniforms[i].name, "gl_", 3) == 0) { + /* built-in uniform: ignore */ + } + else { + fprintf(stderr, + "Unexpected uniform data type in SetUniformValues\n"); + abort(); + } } } } + + +/** 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); + 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); + 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); + } +} diff --git a/progs/util/shaderutil.h b/progs/util/shaderutil.h index cfb8c1f3b0..98c7181156 100644 --- a/progs/util/shaderutil.h +++ b/progs/util/shaderutil.h @@ -6,8 +6,8 @@ struct uniform_info { const char *name; - GLuint size; - GLenum type; /**< GL_FLOAT or GL_INT */ + GLuint size; /**< number of value[] elements: 1, 2, 3 or 4 */ + GLenum type; /**< GL_FLOAT, GL_FLOAT_VEC4, GL_INT, etc */ GLfloat value[4]; GLint location; /**< filled in by InitUniforms() */ }; @@ -15,6 +15,15 @@ struct uniform_info #define END_OF_UNIFORMS { NULL, 0, GL_NONE, { 0, 0, 0, 0 }, -1 } +struct attrib_info +{ + const char *name; + GLuint size; /**< number of value[] elements: 1, 2, 3 or 4 */ + GLenum type; /**< GL_FLOAT, GL_FLOAT_VEC4, GL_INT, etc */ + GLint location; +}; + + extern GLboolean ShadersSupported(void); @@ -27,8 +36,28 @@ CompileShaderFile(GLenum shaderType, const char *filename); extern GLuint LinkShaders(GLuint vertShader, GLuint fragShader); +extern GLboolean +ValidateShaderProgram(GLuint program); + +extern GLdouble +GetShaderCompileTime(void); + +extern GLdouble +GetShaderLinkTime(void); + +extern void +SetUniformValues(GLuint program, struct uniform_info uniforms[]); + +extern GLuint +GetUniforms(GLuint program, struct uniform_info uniforms[]); + extern void -InitUniforms(GLuint program, struct uniform_info uniforms[]); +PrintUniforms(const struct uniform_info uniforms[]); + +extern GLuint +GetAttribs(GLuint program, struct attrib_info attribs[]); +extern void +PrintAttribs(const struct attrib_info attribs[]); #endif /* SHADER_UTIL_H */ |