fixes to _mesa_combine_programs(), from gallium-0.1
[mesa.git] / src / mesa / shader / shader_api.c
index 3a54e68d0de8374fb31a2aca2dd6efc0b21fcfe0..8bb160026790e17957cb2a165f8d81eb899719d1 100644 (file)
 #include "prog_parameter.h"
 #include "prog_print.h"
 #include "prog_statevars.h"
-#include "shader_api.h"
-
-#include "slang_compile.h"
-#include "slang_link.h"
+#include "prog_uniform.h"
+#include "shader/shader_api.h"
+#include "shader/slang/slang_compile.h"
+#include "shader/slang/slang_link.h"
 
 
 
@@ -75,26 +75,11 @@ void
 _mesa_clear_shader_program_data(GLcontext *ctx,
                                 struct gl_shader_program *shProg)
 {
-   if (shProg->VertexProgram) {
-      if (shProg->VertexProgram->Base.Parameters == shProg->Uniforms) {
-         /* to prevent a double-free in the next call */
-         shProg->VertexProgram->Base.Parameters = NULL;
-      }
-      _mesa_delete_program(ctx, &shProg->VertexProgram->Base);
-      shProg->VertexProgram = NULL;
-   }
-
-   if (shProg->FragmentProgram) {
-      if (shProg->FragmentProgram->Base.Parameters == shProg->Uniforms) {
-         /* to prevent a double-free in the next call */
-         shProg->FragmentProgram->Base.Parameters = NULL;
-      }
-      _mesa_delete_program(ctx, &shProg->FragmentProgram->Base);
-      shProg->FragmentProgram = NULL;
-   }
+   _mesa_reference_vertprog(ctx, &shProg->VertexProgram, NULL);
+   _mesa_reference_fragprog(ctx, &shProg->FragmentProgram, NULL);
 
    if (shProg->Uniforms) {
-      _mesa_free_parameter_list(shProg->Uniforms);
+      _mesa_free_uniform_list(shProg->Uniforms);
       shProg->Uniforms = NULL;
    }
 
@@ -128,10 +113,17 @@ _mesa_free_shader_program_data(GLcontext *ctx,
    for (i = 0; i < shProg->NumShaders; i++) {
       _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
    }
+   shProg->NumShaders = 0;
+
    if (shProg->Shaders) {
       _mesa_free(shProg->Shaders);
       shProg->Shaders = NULL;
    }
+
+   if (shProg->InfoLog) {
+      _mesa_free(shProg->InfoLog);
+      shProg->InfoLog = NULL;
+   }
 }
 
 
@@ -142,10 +134,7 @@ void
 _mesa_free_shader_program(GLcontext *ctx, struct gl_shader_program *shProg)
 {
    _mesa_free_shader_program_data(ctx, shProg);
-   if (shProg->Shaders) {
-      _mesa_free(shProg->Shaders);
-      shProg->Shaders = NULL;
-   }
+
    _mesa_free(shProg);
 }
 
@@ -174,8 +163,10 @@ _mesa_reference_shader_program(GLcontext *ctx,
 
       ASSERT(old->RefCount > 0);
       old->RefCount--;
-      /*printf("SHPROG DECR %p (%d) to %d\n",
-        (void*) old, old->Name, old->RefCount);*/
+#if 0
+      printf("ShaderProgram %p ID=%u  RefCount-- to %d\n",
+             (void *) old, old->Name, old->RefCount);
+#endif
       deleteFlag = (old->RefCount == 0);
 
       if (deleteFlag) {
@@ -189,8 +180,10 @@ _mesa_reference_shader_program(GLcontext *ctx,
 
    if (shProg) {
       shProg->RefCount++;
-      /*printf("SHPROG INCR %p (%d) to %d\n",
-        (void*) shProg, shProg->Name, shProg->RefCount);*/
+#if 0
+      printf("ShaderProgram %p ID=%u  RefCount++ to %d\n",
+             (void *) shProg, shProg->Name, shProg->RefCount);
+#endif
       *ptr = shProg;
    }
 }
@@ -245,10 +238,8 @@ _mesa_free_shader(GLcontext *ctx, struct gl_shader *sh)
       _mesa_free((void *) sh->Source);
    if (sh->InfoLog)
       _mesa_free(sh->InfoLog);
-   for (i = 0; i < sh->NumPrograms; i++) {
-      assert(sh->Programs[i]);
-      _mesa_delete_program(ctx, sh->Programs[i]);
-   }
+   for (i = 0; i < sh->NumPrograms; i++)
+      _mesa_reference_program(ctx, &sh->Programs[i], NULL);
    if (sh->Programs)
       _mesa_free(sh->Programs);
    _mesa_free(sh);
@@ -378,7 +369,7 @@ _mesa_attach_shader(GLcontext *ctx, GLuint program, GLuint shader)
    struct gl_shader_program *shProg
       = _mesa_lookup_shader_program(ctx, program);
    struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
-   const GLuint n = shProg->NumShaders;
+   GLuint n;
    GLuint i;
 
    if (!shProg || !sh) {
@@ -387,6 +378,8 @@ _mesa_attach_shader(GLcontext *ctx, GLuint program, GLuint shader)
       return;
    }
 
+   n = shProg->NumShaders;
+
    for (i = 0; i < n; i++) {
       if (shProg->Shaders[i] == sh) {
          /* already attached */
@@ -411,6 +404,37 @@ _mesa_attach_shader(GLcontext *ctx, GLuint program, GLuint shader)
 }
 
 
+GLint
+_mesa_get_attrib_location(GLcontext *ctx, GLuint program,
+                          const GLchar *name)
+{
+   struct gl_shader_program *shProg
+      = _mesa_lookup_shader_program(ctx, program);
+
+   if (!shProg) {
+      _mesa_error(ctx, GL_INVALID_VALUE, "glGetAttribLocation");
+      return -1;
+   }
+
+   if (!shProg->LinkStatus) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glGetAttribLocation(program not linked)");
+      return -1;
+   }
+
+   if (!name)
+      return -1;
+
+   if (shProg->Attributes) {
+      GLint i = _mesa_lookup_parameter_index(shProg->Attributes, -1, name);
+      if (i >= 0) {
+         return shProg->Attributes->Parameters[i].StateIndexes[0];
+      }
+   }
+   return -1;
+}
+
+
 void
 _mesa_bind_attrib_location(GLcontext *ctx, GLuint program, GLuint index,
                            const GLchar *name)
@@ -548,7 +572,7 @@ _mesa_detach_shader(GLcontext *ctx, GLuint program, GLuint shader)
 {
    struct gl_shader_program *shProg
       = _mesa_lookup_shader_program(ctx, program);
-   const GLuint n = shProg->NumShaders;
+   GLuint n;
    GLuint i, j;
 
    if (!shProg) {
@@ -557,6 +581,8 @@ _mesa_detach_shader(GLcontext *ctx, GLuint program, GLuint shader)
       return;
    }
 
+   n = shProg->NumShaders;
+
    for (i = 0; i < n; i++) {
       if (shProg->Shaders[i]->Name == shader) {
          /* found it */
@@ -643,39 +669,43 @@ _mesa_get_active_uniform(GLcontext *ctx, GLuint program, GLuint index,
                          GLsizei maxLength, GLsizei *length, GLint *size,
                          GLenum *type, GLchar *nameOut)
 {
-   struct gl_shader_program *shProg
+   const struct gl_shader_program *shProg
       = _mesa_lookup_shader_program(ctx, program);
-   GLuint ind, j;
+   const struct gl_program *prog;
+   GLint progPos;
 
    if (!shProg) {
       _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform");
       return;
    }
 
-   if (!shProg->Uniforms || index >= shProg->Uniforms->NumParameters) {
+   if (!shProg->Uniforms || index >= shProg->Uniforms->NumUniforms) {
       _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform(index)");
       return;
    }
 
-   ind = 0;
-   for (j = 0; j < shProg->Uniforms->NumParameters; j++) {
-      if (shProg->Uniforms->Parameters[j].Type == PROGRAM_UNIFORM ||
-          shProg->Uniforms->Parameters[j].Type == PROGRAM_SAMPLER) {
-         if (ind == index) {
-            /* found it */
-            copy_string(nameOut, maxLength, length,
-                        shProg->Uniforms->Parameters[j].Name);
-            if (size)
-               *size = shProg->Uniforms->Parameters[j].Size;
-            if (type)
-               *type = shProg->Uniforms->Parameters[j].DataType;
-            return;
-         }
-         ind++;
+   progPos = shProg->Uniforms->Uniforms[index].VertPos;
+   if (progPos >= 0) {
+      prog = &shProg->VertexProgram->Base;
+   }
+   else {
+      progPos = shProg->Uniforms->Uniforms[index].FragPos;
+      if (progPos >= 0) {
+         prog = &shProg->FragmentProgram->Base;
       }
    }
 
-   _mesa_error(ctx, GL_INVALID_VALUE, "glGetActiveUniform(index)");
+   if (!prog || progPos < 0)
+      return; /* should never happen */
+
+   if (nameOut)
+      copy_string(nameOut, maxLength, length,
+                  prog->Parameters->Parameters[progPos].Name);
+   if (size)
+      *size = prog->Parameters->Parameters[progPos].Size;
+
+   if (type)
+      *type = prog->Parameters->Parameters[progPos].DataType;
 }
 
 
@@ -702,37 +732,6 @@ _mesa_get_attached_shaders(GLcontext *ctx, GLuint program, GLsizei maxCount,
 }
 
 
-GLint
-_mesa_get_attrib_location(GLcontext *ctx, GLuint program,
-                          const GLchar *name)
-{
-   struct gl_shader_program *shProg
-      = _mesa_lookup_shader_program(ctx, program);
-
-   if (!shProg) {
-      _mesa_error(ctx, GL_INVALID_VALUE, "glGetAttribLocation");
-      return -1;
-   }
-
-   if (!shProg->LinkStatus) {
-      _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "glGetAttribLocation(program not linked)");
-      return -1;
-   }
-
-   if (!name)
-      return -1;
-
-   if (shProg->Attributes) {
-      GLint i = _mesa_lookup_parameter_index(shProg->Attributes, -1, name);
-      if (i >= 0) {
-         return shProg->Attributes->Parameters[i].StateIndexes[0];
-      }
-   }
-   return -1;
-}
-
-
 GLuint
 _mesa_get_handle(GLcontext *ctx, GLenum pname)
 {
@@ -780,7 +779,7 @@ _mesa_get_programiv(GLcontext *ctx, GLuint program,
       *params = shProg->Validated;
       break;
    case GL_INFO_LOG_LENGTH:
-      *params = shProg->InfoLog ? strlen(shProg->InfoLog) : 0;
+      *params = shProg->InfoLog ? strlen(shProg->InfoLog) + 1 : 0;
       break;
    case GL_ATTACHED_SHADERS:
       *params = shProg->NumShaders;
@@ -793,14 +792,10 @@ _mesa_get_programiv(GLcontext *ctx, GLuint program,
                                              PROGRAM_INPUT) + 1;
       break;
    case GL_ACTIVE_UNIFORMS:
-      *params
-         = _mesa_num_parameters_of_type(shProg->Uniforms, PROGRAM_UNIFORM)
-         + _mesa_num_parameters_of_type(shProg->Uniforms, PROGRAM_SAMPLER);
+      *params = shProg->Uniforms ? shProg->Uniforms->NumUniforms : 0;
       break;
    case GL_ACTIVE_UNIFORM_MAX_LENGTH:
-      *params = MAX2(
-             _mesa_longest_parameter_name(shProg->Uniforms, PROGRAM_UNIFORM),
-             _mesa_longest_parameter_name(shProg->Uniforms, PROGRAM_SAMPLER));
+      *params = _mesa_longest_uniform_name(shProg->Uniforms);
       if (*params > 0)
          (*params)++;  /* add one for terminating zero */
       break;
@@ -832,10 +827,10 @@ _mesa_get_shaderiv(GLcontext *ctx, GLuint name, GLenum pname, GLint *params)
       *params = shader->CompileStatus;
       break;
    case GL_INFO_LOG_LENGTH:
-      *params = shader->InfoLog ? strlen(shader->InfoLog) : 0;
+      *params = shader->InfoLog ? strlen(shader->InfoLog) + 1 : 0;
       break;
    case GL_SHADER_SOURCE_LENGTH:
-      *params = shader->Source ? strlen((char *) shader->Source) : 0;
+      *params = shader->Source ? strlen((char *) shader->Source) + 1 : 0;
       break;
    default:
       _mesa_error(ctx, GL_INVALID_ENUM, "glGetShaderiv(pname)");
@@ -897,10 +892,26 @@ _mesa_get_uniformfv(GLcontext *ctx, GLuint program, GLint location,
    struct gl_shader_program *shProg
       = _mesa_lookup_shader_program(ctx, program);
    if (shProg) {
-      GLint i;
-      if (location >= 0 && location < shProg->Uniforms->NumParameters) {
-         for (i = 0; i < shProg->Uniforms->Parameters[location].Size; i++) {
-            params[i] = shProg->Uniforms->ParameterValues[location][i];
+      if (location < shProg->Uniforms->NumUniforms) {
+         GLint progPos, i;
+         const struct gl_program *prog = NULL;
+
+         progPos = shProg->Uniforms->Uniforms[location].VertPos;
+         if (progPos >= 0) {
+            prog = &shProg->VertexProgram->Base;
+         }
+         else {
+            progPos = shProg->Uniforms->Uniforms[location].FragPos;
+            if (progPos >= 0) {
+               prog = &shProg->FragmentProgram->Base;
+            }
+         }
+
+         ASSERT(prog);
+         if (prog) {
+            for (i = 0; i < prog->Parameters->Parameters[progPos].Size; i++) {
+               params[i] = prog->Parameters->ParameterValues[progPos][i];
+            }
          }
       }
       else {
@@ -921,23 +932,10 @@ _mesa_get_uniform_location(GLcontext *ctx, GLuint program, const GLchar *name)
 {
    struct gl_shader_program *shProg
       = _mesa_lookup_shader_program(ctx, program);
-   if (shProg) {
-      GLuint loc;
-      for (loc = 0; loc < shProg->Uniforms->NumParameters; loc++) {
-         const struct gl_program_parameter *u
-            = shProg->Uniforms->Parameters + loc;
-         /* XXX this is a temporary simplification / short-cut.
-          * We need to handle things like "e.c[0].b" as seen in the
-          * GLSL orange book, page 189.
-          */
-         if ((u->Type == PROGRAM_UNIFORM ||
-              u->Type == PROGRAM_SAMPLER) && !strcmp(u->Name, name)) {
-            return loc;
-         }
-      }
-   }
-   return -1;
+   if (!shProg)
+      return -1;
 
+   return _mesa_lookup_uniform(shProg->Uniforms, name);
 }
 
 
@@ -1010,6 +1008,8 @@ _mesa_link_program(GLcontext *ctx, GLuint program)
       return;
    }
 
+   FLUSH_VERTICES(ctx, _NEW_PROGRAM);
+
    _slang_link(ctx, program, shProg);
 }
 
@@ -1046,46 +1046,121 @@ _mesa_use_program(GLcontext *ctx, GLuint program)
 }
 
 
+
 /**
- * Called via ctx->Driver.Uniform().
+ * Update the vertex and fragment program's TexturesUsed arrays.
  */
-void
-_mesa_uniform(GLcontext *ctx, GLint location, GLsizei count,
-              const GLvoid *values, GLenum type)
+static void
+update_textures_used(struct gl_program *prog)
 {
-   struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
-   GLint elems, i, k;
+   GLuint s;
 
-   if (!shProg || !shProg->LinkStatus) {
-      _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(program not linked)");
-      return;
-   }
+   memset(prog->TexturesUsed, 0, sizeof(prog->TexturesUsed));
 
-   if (location < 0 || location >= (GLint) shProg->Uniforms->NumParameters) {
-      _mesa_error(ctx, GL_INVALID_VALUE, "glUniform(location)");
-      return;
+   for (s = 0; s < MAX_SAMPLERS; s++) {
+      if (prog->SamplersUsed & (1 << s)) {
+         GLuint u = prog->SamplerUnits[s];
+         GLuint t = prog->SamplerTargets[s];
+         assert(u < MAX_TEXTURE_IMAGE_UNITS);
+         prog->TexturesUsed[u] |= (1 << t);
+      }
    }
+}
 
-   FLUSH_VERTICES(ctx, _NEW_PROGRAM);
 
-   /*
-    * If we're setting a sampler, we must use glUniformi1()!
-    */
-   if (shProg->Uniforms->Parameters[location].Type == PROGRAM_SAMPLER) {
-      GLint unit;
+/**
+ * Set the value of a program's uniform variable.
+ * \param program  the program whose uniform to update
+ * \param location  the location/index of the uniform
+ * \param type  the datatype of the uniform
+ * \param count  the number of uniforms to set
+ * \param elems  number of elements per uniform
+ * \param values  the new values
+ */
+static void
+set_program_uniform(GLcontext *ctx, struct gl_program *program, GLint location,
+                    GLenum type, GLint count, GLint elems, const void *values)
+{
+   if (program->Parameters->Parameters[location].Type == PROGRAM_SAMPLER) {
+      /* This controls which texture unit which is used by a sampler */
+      GLuint texUnit, sampler;
+
+      /* data type for setting samplers must be int */
       if (type != GL_INT || count != 1) {
          _mesa_error(ctx, GL_INVALID_OPERATION,
                      "glUniform(only glUniform1i can be used "
                      "to set sampler uniforms)");
          return;
       }
+
+      sampler = (GLuint) program->Parameters->ParameterValues[location][0];
+      texUnit = ((GLuint *) values)[0];
+
       /* check that the sampler (tex unit index) is legal */
-      unit = ((GLint *) values)[0];
-      if (unit >= ctx->Const.MaxTextureImageUnits) {
+      if (texUnit >= ctx->Const.MaxTextureImageUnits) {
          _mesa_error(ctx, GL_INVALID_VALUE,
                      "glUniform1(invalid sampler/tex unit index)");
          return;
       }
+
+      /* This maps a sampler to a texture unit: */
+      program->SamplerUnits[sampler] = texUnit;
+      update_textures_used(program);
+
+      FLUSH_VERTICES(ctx, _NEW_TEXTURE);
+   }
+   else {
+      /* ordinary uniform variable */
+      GLint k, i;
+
+      if (count * elems > program->Parameters->Parameters[location].Size) {
+         _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(count too large)");
+         return;
+      }
+
+      for (k = 0; k < count; k++) {
+         GLfloat *uniformVal = program->Parameters->ParameterValues[location + k];
+         if (type == GL_INT ||
+             type == GL_INT_VEC2 ||
+             type == GL_INT_VEC3 ||
+             type == GL_INT_VEC4) {
+            const GLint *iValues = ((const GLint *) values) + k * elems;
+            for (i = 0; i < elems; i++) {
+               uniformVal[i] = (GLfloat) iValues[i];
+            }
+         }
+         else {
+            const GLfloat *fValues = ((const GLfloat *) values) + k * elems;
+            for (i = 0; i < elems; i++) {
+               uniformVal[i] = fValues[i];
+            }
+         }
+      }
+   }
+}
+
+
+/**
+ * Called via ctx->Driver.Uniform().
+ */
+void
+_mesa_uniform(GLcontext *ctx, GLint location, GLsizei count,
+              const GLvoid *values, GLenum type)
+{
+   struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
+   GLint elems;
+
+   if (!shProg || !shProg->LinkStatus) {
+      _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(program not linked)");
+      return;
+   }
+
+   if (location == -1)
+      return;   /* The standard specifies this as a no-op */
+
+   if (location < 0 || location >= (GLint) shProg->Uniforms->NumUniforms) {
+      _mesa_error(ctx, GL_INVALID_VALUE, "glUniform(location)");
+      return;
    }
 
    if (count < 0) {
@@ -1115,36 +1190,56 @@ _mesa_uniform(GLcontext *ctx, GLint location, GLsizei count,
       return;
    }
 
-   if (count * elems > shProg->Uniforms->Parameters[location].Size) {
-      _mesa_error(ctx, GL_INVALID_OPERATION, "glUniform(count too large)");
-      return;
+   FLUSH_VERTICES(ctx, _NEW_PROGRAM);
+
+   /* 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) {
+      GLint loc = shProg->Uniforms->Uniforms[location].VertPos;
+      if (loc >= 0) {
+         set_program_uniform(ctx, &shProg->VertexProgram->Base,
+                             loc, type, count, elems, values);
+      }
    }
 
-   for (k = 0; k < count; k++) {
-      GLfloat *uniformVal = shProg->Uniforms->ParameterValues[location + k];
-      if (type == GL_INT ||
-          type == GL_INT_VEC2 ||
-          type == GL_INT_VEC3 ||
-          type == GL_INT_VEC4) {
-         const GLint *iValues = ((const GLint *) values) + k * elems;
-         for (i = 0; i < elems; i++) {
-            uniformVal[i] = (GLfloat) iValues[i];
-         }
+   if (shProg->FragmentProgram) {
+      GLint loc = shProg->Uniforms->Uniforms[location].FragPos;
+      if (loc >= 0) {
+         set_program_uniform(ctx, &shProg->FragmentProgram->Base,
+                             loc, type, count, elems, values);
       }
-      else {
-         const GLfloat *fValues = ((const GLfloat *) values) + k * elems;
-         for (i = 0; i < elems; i++) {
-            uniformVal[i] = fValues[i];
+   }
+}
+
+
+static void
+set_program_uniform_matrix(GLcontext *ctx, struct gl_program *program,
+                           GLuint location, GLuint rows, GLuint cols,
+                           GLboolean transpose, const GLfloat *values)
+{
+   /*
+    * Note: the _columns_ of a matrix are stored in program registers, not
+    * the rows.
+    */
+   /* XXXX need to test 3x3 and 2x2 matrices... */
+   if (transpose) {
+      GLuint row, col;
+      for (col = 0; col < cols; col++) {
+         GLfloat *v = program->Parameters->ParameterValues[location + col];
+         for (row = 0; row < rows; row++) {
+            v[row] = values[row * cols + col];
          }
       }
    }
-
-   if (shProg->Uniforms->Parameters[location].Type == PROGRAM_SAMPLER) {
-      if (shProg->VertexProgram)
-         _slang_resolve_samplers(shProg, &shProg->VertexProgram->Base);
-      if (shProg->FragmentProgram)
-         _slang_resolve_samplers(shProg, &shProg->FragmentProgram->Base);
-      FLUSH_VERTICES(ctx, _NEW_TEXTURE);
+   else {
+      GLuint row, col;
+      for (col = 0; col < cols; col++) {
+         GLfloat *v = program->Parameters->ParameterValues[location + col];
+         for (row = 0; row < rows; row++) {
+            v[row] = values[col * rows + row];
+         }
+      }
    }
 }
 
@@ -1158,12 +1253,17 @@ _mesa_uniform_matrix(GLcontext *ctx, GLint cols, GLint rows,
                      GLboolean transpose, const GLfloat *values)
 {
    struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
+
    if (!shProg || !shProg->LinkStatus) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
          "glUniformMatrix(program not linked)");
       return;
    }
-   if (location < 0 || location >= shProg->Uniforms->NumParameters) {
+
+   if (location == -1)
+      return;   /* The standard specifies this as a no-op */
+
+   if (location < 0 || location >= shProg->Uniforms->NumUniforms) {
       _mesa_error(ctx, GL_INVALID_VALUE, "glUniformMatrix(location)");
       return;
    }
@@ -1174,27 +1274,19 @@ _mesa_uniform_matrix(GLcontext *ctx, GLint cols, GLint rows,
 
    FLUSH_VERTICES(ctx, _NEW_PROGRAM);
 
-   /*
-    * Note: the _columns_ of a matrix are stored in program registers, not
-    * the rows.
-    */
-   /* XXXX need to test 3x3 and 2x2 matrices... */
-   if (transpose) {
-      GLuint row, col;
-      for (col = 0; col < cols; col++) {
-         GLfloat *v = shProg->Uniforms->ParameterValues[location + col];
-         for (row = 0; row < rows; row++) {
-            v[row] = values[row * cols + col];
-         }
+   if (shProg->VertexProgram) {
+      GLint loc = shProg->Uniforms->Uniforms[location].VertPos;
+      if (loc >= 0) {
+         set_program_uniform_matrix(ctx, &shProg->VertexProgram->Base,
+                                    loc, rows, cols, transpose, values);
       }
    }
-   else {
-      GLuint row, col;
-      for (col = 0; col < cols; col++) {
-         GLfloat *v = shProg->Uniforms->ParameterValues[location + col];
-         for (row = 0; row < rows; row++) {
-            v[row] = values[col * rows + row];
-         }
+
+   if (shProg->FragmentProgram) {
+      GLint loc = shProg->Uniforms->Uniforms[location].FragPos;
+      if (loc >= 0) {
+         set_program_uniform_matrix(ctx, &shProg->FragmentProgram->Base,
+                                    loc, rows, cols, transpose, values);
       }
    }
 }