progs/util: added more shader utility functions
authorBrian Paul <brianp@vmware.com>
Wed, 12 Aug 2009 19:50:26 +0000 (13:50 -0600)
committerBrian Paul <brianp@vmware.com>
Wed, 12 Aug 2009 23:28:45 +0000 (17:28 -0600)
progs/util/shaderutil.c
progs/util/shaderutil.h

index 13b68d90e0bcd697597fcdad7514ba13d71da0eb..bd04ce5c6ad9fbdfb416f4d3cd36d18f1c28cc90 100644 (file)
@@ -9,21 +9,12 @@
 #include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <GL/glew.h>
 #include <GL/glut.h>
 #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);
+   }
+}
index cfb8c1f3b06b09554167c2fbc4fde8be52e49320..607ed284915713708ef47f94a52aaa49b348a796 100644 (file)
@@ -6,7 +6,7 @@
 struct uniform_info
 {
    const char *name;
-   GLuint size;
+   GLuint size;  /**< number of value[] elements: 1, 2, 3 or 4 */
    GLenum type;  /**< GL_FLOAT or GL_INT */
    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 or GL_INT */
+   GLint location;
+};
+
+
 extern GLboolean
 ShadersSupported(void);
 
@@ -30,5 +39,16 @@ LinkShaders(GLuint vertShader, GLuint fragShader);
 extern void
 InitUniforms(GLuint program, struct uniform_info uniforms[]);
 
+extern GLuint
+GetUniforms(GLuint program, struct uniform_info uniforms[]);
+
+extern void
+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 */