mesa: track initialization status of uniform variables. Plus, asst clean-ups.
authorBrian Paul <brian.paul@tungstengraphics.com>
Mon, 10 Nov 2008 19:33:17 +0000 (12:33 -0700)
committerBrian Paul <brian.paul@tungstengraphics.com>
Mon, 10 Nov 2008 19:39:36 +0000 (12:39 -0700)
src/mesa/shader/prog_parameter.c
src/mesa/shader/prog_parameter.h
src/mesa/shader/prog_uniform.c
src/mesa/shader/prog_uniform.h
src/mesa/shader/shader_api.c
src/mesa/shader/slang/slang_codegen.c
src/mesa/shader/slang/slang_link.c

index 2dfd923a0f3d86b0a8378f311fd576a6b0283b7b..29c5b33f6cd44196733cd04e04abb97e1d22eaf4 100644 (file)
@@ -128,6 +128,7 @@ _mesa_add_parameter(struct gl_program_parameter_list *paramList,
          if (values) {
             COPY_4V(paramList->ParameterValues[oldNum + i], values);
             values += 4;
+            p->Initialized = GL_TRUE;
          }
          else {
             /* silence valgrind */
@@ -259,7 +260,8 @@ _mesa_add_unnamed_constant(struct gl_program_parameter_list *paramList,
  */
 GLint
 _mesa_add_uniform(struct gl_program_parameter_list *paramList,
-                  const char *name, GLuint size, GLenum datatype)
+                  const char *name, GLuint size, GLenum datatype,
+                  const GLfloat *values)
 {
    GLint i = _mesa_lookup_parameter_index(paramList, -1, name);
    ASSERT(datatype != GL_NONE);
@@ -271,7 +273,7 @@ _mesa_add_uniform(struct gl_program_parameter_list *paramList,
    }
    else {
       i = _mesa_add_parameter(paramList, PROGRAM_UNIFORM, name,
-                              size, datatype, NULL, NULL);
+                              size, datatype, values, NULL);
       return i;
    }
 }
index e8d3e9ec3362515e115d0358966f7d383a5fb728..0c35198de7e8bf83511ad92a6e52ab8f22c5a7e6 100644 (file)
@@ -49,6 +49,7 @@ struct gl_program_parameter
    GLenum DataType;         /**< GL_FLOAT, GL_FLOAT_VEC2, etc */
    GLuint Size;             /**< Number of components (1..4) */
    GLboolean Used;          /**< Helper flag for GLSL uniform tracking */
+   GLboolean Initialized;   /**< Has the ParameterValue[] been set? */
    /**
     * A sequence of STATE_* tokens and integers to identify GL state.
     */
@@ -111,7 +112,8 @@ _mesa_add_unnamed_constant(struct gl_program_parameter_list *paramList,
 
 extern GLint
 _mesa_add_uniform(struct gl_program_parameter_list *paramList,
-                  const char *name, GLuint size, GLenum datatype);
+                  const char *name, GLuint size, GLenum datatype,
+                  const GLfloat *values);
 
 extern void
 _mesa_use_uniform(struct gl_program_parameter_list *paramList,
index a0aa615c5f62c4a93596dccecaea3b500200764d..dc76be8e46c3c15781893eebd23b11d0f274f70a 100644 (file)
@@ -52,11 +52,12 @@ _mesa_free_uniform_list(struct gl_uniform_list *list)
 }
 
 
-GLboolean
+struct gl_uniform *
 _mesa_append_uniform(struct gl_uniform_list *list,
                      const char *name, GLenum target, GLuint progPos)
 {
    const GLuint oldNum = list->NumUniforms;
+   struct gl_uniform *uniform;
    GLint index;
 
    assert(target == GL_VERTEX_PROGRAM_ARB ||
@@ -84,31 +85,37 @@ _mesa_append_uniform(struct gl_uniform_list *list,
          return GL_FALSE;
       }
 
-      list->Uniforms[oldNum].Name = _mesa_strdup(name);
-      list->Uniforms[oldNum].VertPos = -1;
-      list->Uniforms[oldNum].FragPos = -1;
-      list->Uniforms[oldNum].Initialized = GL_FALSE;
-      index = oldNum;
+      uniform = list->Uniforms + oldNum;
+
+      uniform->Name = _mesa_strdup(name);
+      uniform->VertPos = -1;
+      uniform->FragPos = -1;
+      uniform->Initialized = GL_FALSE;
+
       list->NumUniforms++;
    }
+   else {
+      /* found */
+      uniform = list->Uniforms + index;
+   }
 
    /* update position for the vertex or fragment program */
    if (target == GL_VERTEX_PROGRAM_ARB) {
-      if (list->Uniforms[index].VertPos != -1) {
+      if (uniform->VertPos != -1) {
          /* this uniform is already in the list - that shouldn't happen */
          return GL_FALSE;
       }
-      list->Uniforms[index].VertPos = progPos;
+      uniform->VertPos = progPos;
    }
    else {
-      if (list->Uniforms[index].FragPos != -1) {
+      if (uniform->FragPos != -1) {
          /* this uniform is already in the list - that shouldn't happen */
          return GL_FALSE;
       }
-      list->Uniforms[index].FragPos = progPos;
+      uniform->FragPos = progPos;
    }
 
-   return GL_TRUE;
+   return uniform;
 }
 
 
index deea7329912b2f905e2ca1f364498cbdf0e0f9d1..22a2bfd9701173cf99a5a10bed0c3fa146f052d3 100644 (file)
@@ -75,7 +75,7 @@ _mesa_new_uniform_list(void);
 extern void
 _mesa_free_uniform_list(struct gl_uniform_list *list);
 
-extern GLboolean
+extern struct gl_uniform *
 _mesa_append_uniform(struct gl_uniform_list *list,
                      const char *name, GLenum target, GLuint progPos);
 
index e883f8b8be1ffe43d66b439d1dfa83f7d73b3076..b3d66c5babd882c4bed4e17a8a445f6a7afb8900 100644 (file)
@@ -1608,6 +1608,7 @@ _mesa_uniform(GLcontext *ctx, GLint location, GLsizei count,
               const GLvoid *values, GLenum type)
 {
    struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
+   struct gl_uniform *uniform;
    GLint elems, offset;
 
    if (!shProg || !shProg->LinkStatus) {
@@ -1654,12 +1655,14 @@ _mesa_uniform(GLcontext *ctx, GLint location, GLsizei count,
 
    FLUSH_VERTICES(ctx, _NEW_PROGRAM);
 
+   uniform = &shProg->Uniforms->Uniforms[location];
+
    /* A uniform var may be used by both a vertex shader and a fragment
     * shader.  We may need to update one or both shader's uniform here:
     */
    if (shProg->VertexProgram) {
       /* convert uniform location to program parameter index */
-      GLint index = shProg->Uniforms->Uniforms[location].VertPos;
+      GLint index = uniform->VertPos;
       if (index >= 0) {
          set_program_uniform(ctx, &shProg->VertexProgram->Base,
                              index, offset, type, count, elems, values);
@@ -1668,14 +1671,14 @@ _mesa_uniform(GLcontext *ctx, GLint location, GLsizei count,
 
    if (shProg->FragmentProgram) {
       /* convert uniform location to program parameter index */
-      GLint index = shProg->Uniforms->Uniforms[location].FragPos;
+      GLint index = uniform->FragPos;
       if (index >= 0) {
          set_program_uniform(ctx, &shProg->FragmentProgram->Base,
                              index, offset, type, count, elems, values);
       }
    }
 
-   shProg->Uniforms->Uniforms[location].Initialized = GL_TRUE;
+   uniform->Initialized = GL_TRUE;
 }
 
 
@@ -1742,8 +1745,9 @@ _mesa_uniform_matrix(GLcontext *ctx, GLint cols, GLint rows,
                      GLenum matrixType, GLint location, GLsizei count,
                      GLboolean transpose, const GLfloat *values)
 {
-   GLint offset;
    struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
+   struct gl_uniform *uniform;
+   GLint offset;
 
    if (!shProg || !shProg->LinkStatus) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
@@ -1767,9 +1771,11 @@ _mesa_uniform_matrix(GLcontext *ctx, GLint cols, GLint rows,
 
    FLUSH_VERTICES(ctx, _NEW_PROGRAM);
 
+   uniform = &shProg->Uniforms->Uniforms[location];
+
    if (shProg->VertexProgram) {
       /* convert uniform location to program parameter index */
-      GLint index = shProg->Uniforms->Uniforms[location].VertPos;
+      GLint index = uniform->VertPos;
       if (index >= 0) {
          set_program_uniform_matrix(ctx, &shProg->VertexProgram->Base,
                                     index, offset,
@@ -1779,7 +1785,7 @@ _mesa_uniform_matrix(GLcontext *ctx, GLint cols, GLint rows,
 
    if (shProg->FragmentProgram) {
       /* convert uniform location to program parameter index */
-      GLint index = shProg->Uniforms->Uniforms[location].FragPos;
+      GLint index = uniform->FragPos;
       if (index >= 0) {
          set_program_uniform_matrix(ctx, &shProg->FragmentProgram->Base,
                                     index, offset,
@@ -1787,7 +1793,7 @@ _mesa_uniform_matrix(GLcontext *ctx, GLint cols, GLint rows,
       }
    }
 
-   shProg->Uniforms->Uniforms[location].Initialized = GL_TRUE;
+   uniform->Initialized = GL_TRUE;
 }
 
 
index 8ab1fa8f2a6d169118e009239dcf999e304d1322..dea1fcf087450e37ae8955d296f4c2f99d2fe6dc 100644 (file)
@@ -3715,7 +3715,7 @@ _slang_codegen_global_variable(slang_assemble_ctx *A, slang_variable *var,
                /* temporary work-around */
                GLenum datatype = GL_FLOAT;
                GLint uniformLoc = _mesa_add_uniform(prog->Parameters, varName,
-                                                    totalSize, datatype);
+                                                    totalSize, datatype, NULL);
                store = _slang_new_ir_storage_swz(PROGRAM_UNIFORM, uniformLoc,
                                                  totalSize, swizzle);
 
@@ -3747,18 +3747,14 @@ _slang_codegen_global_variable(slang_assemble_ctx *A, slang_variable *var,
             }
          }
          else {
-            GLint uniformLoc = _mesa_add_uniform(prog->Parameters, varName,
-                                                 totalSize, datatype);
-            store = _slang_new_ir_storage_swz(PROGRAM_UNIFORM, uniformLoc,
-                                              totalSize, swizzle);
+            GLint uniformLoc;
+            const GLfloat *initialValues = NULL;
             if (var->initializer) {
                _slang_simplify(var->initializer, &A->space, A->atoms);
                if (var->initializer->type == SLANG_OPER_LITERAL_FLOAT ||
                    var->initializer->type == SLANG_OPER_LITERAL_INT) {
                   /* simple float/vector initializer */
-                  GLfloat *uniformValue =
-                     prog->Parameters->ParameterValues[uniformLoc];
-                  COPY_4V(uniformValue, var->initializer->literal);
+                  initialValues = var->initializer->literal;
                }
                else {
                   /* complex initializer */
@@ -3767,6 +3763,11 @@ _slang_codegen_global_variable(slang_assemble_ctx *A, slang_variable *var,
                   return GL_FALSE;
                }
             }
+
+            uniformLoc = _mesa_add_uniform(prog->Parameters, varName,
+                                           totalSize, datatype, initialValues);
+            store = _slang_new_ir_storage_swz(PROGRAM_UNIFORM, uniformLoc,
+                                              totalSize, swizzle);
          }
       }
       else {
index 4361efc56e53f90ecf0cd3be4ee43860ac4a6b0b..79fd9a064f5f9bf373c2f2a8d47519cfa14c2b5f 100644 (file)
@@ -193,7 +193,10 @@ link_uniform_vars(struct gl_shader_program *shProg,
 
       if ((p->Type == PROGRAM_UNIFORM && p->Used) ||
           p->Type == PROGRAM_SAMPLER) {
-         _mesa_append_uniform(shProg->Uniforms, p->Name, prog->Target, i);
+         struct gl_uniform *uniform =
+            _mesa_append_uniform(shProg->Uniforms, p->Name, prog->Target, i);
+         if (uniform)
+            uniform->Initialized = p->Initialized;
       }
 
       if (p->Type == PROGRAM_SAMPLER) {