Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / mesa / shader / prog_parameter.c
index 4633015de2849f0c81757c9796d57a0b20f103b4..e1729c7bff2fa530e7864ce65f489e8e159f19d2 100644 (file)
@@ -29,9 +29,9 @@
  */
 
 
-#include "glheader.h"
-#include "imports.h"
-#include "macros.h"
+#include "main/glheader.h"
+#include "main/imports.h"
+#include "main/macros.h"
 #include "prog_instruction.h"
 #include "prog_parameter.h"
 #include "prog_statevars.h"
@@ -40,8 +40,7 @@
 struct gl_program_parameter_list *
 _mesa_new_parameter_list(void)
 {
-   return (struct gl_program_parameter_list *)
-      _mesa_calloc(sizeof(struct gl_program_parameter_list));
+   return CALLOC_STRUCT(gl_program_parameter_list);
 }
 
 
@@ -63,7 +62,6 @@ _mesa_free_parameter_list(struct gl_program_parameter_list *paramList)
 }
 
 
-
 /**
  * Add a new parameter to a parameter list.
  * Note that parameter values are usually 4-element GLfloat vectors.
@@ -81,7 +79,7 @@ _mesa_free_parameter_list(struct gl_program_parameter_list *paramList)
 GLint
 _mesa_add_parameter(struct gl_program_parameter_list *paramList,
                     enum register_file type, const char *name,
-                    GLuint size, const GLfloat *values,
+                    GLuint size, GLenum datatype, const GLfloat *values,
                     const gl_state_index state[STATE_LENGTH])
 {
    const GLuint oldNum = paramList->NumParameters;
@@ -126,9 +124,11 @@ _mesa_add_parameter(struct gl_program_parameter_list *paramList,
          p->Name = name ? _mesa_strdup(name) : NULL;
          p->Type = type;
          p->Size = size;
+         p->DataType = datatype;
          if (values) {
             COPY_4V(paramList->ParameterValues[oldNum + i], values);
             values += 4;
+            p->Initialized = GL_TRUE;
          }
          else {
             /* silence valgrind */
@@ -156,7 +156,7 @@ _mesa_add_named_parameter(struct gl_program_parameter_list *paramList,
                           const char *name, const GLfloat values[4])
 {
    return _mesa_add_parameter(paramList, PROGRAM_NAMED_PARAM, name,
-                              4, values, NULL);
+                              4, GL_NONE, values, NULL);
                               
 }
 
@@ -187,14 +187,17 @@ _mesa_add_named_constant(struct gl_program_parameter_list *paramList,
 #endif
    size = 4; /** XXX fix */
    return _mesa_add_parameter(paramList, PROGRAM_CONSTANT, name,
-                              size, values, NULL);
+                              size, GL_NONE, values, NULL);
 }
 
 
 /**
- * Add a new unnamed constant to the parameter list.
- * This will be used when the program contains something like this:
+ * Add a new unnamed constant to the parameter list.  This will be used
+ * when a fragment/vertex program contains something like this:
  *    MOV r, { 0, 1, 2, 3 };
+ * We'll search the parameter list for an existing instance of the
+ * constant.  If swizzleOut is non-null, we'll try swizzling when
+ * looking for a match.
  *
  * \param paramList  the parameter list
  * \param values  four float values
@@ -219,7 +222,7 @@ _mesa_add_unnamed_constant(struct gl_program_parameter_list *paramList,
     * to add this constant.  This will only work for single-element
     * constants because we rely on smearing (i.e. .yyyy or .zzzz).
     */
-   if (size == 1) {
+   if (size == 1 && swizzleOut) {
       for (pos = 0; pos < (GLint) paramList->NumParameters; pos++) {
          struct gl_program_parameter *p = paramList->Parameters + pos;
          if (p->Type == PROGRAM_CONSTANT && p->Size + size <= 4) {
@@ -236,11 +239,10 @@ _mesa_add_unnamed_constant(struct gl_program_parameter_list *paramList,
 
    /* add a new parameter to store this constant */
    pos = _mesa_add_parameter(paramList, PROGRAM_CONSTANT, NULL,
-                             size, values, NULL);
-   if (pos >= 0) {
+                             size, GL_NONE, values, NULL);
+   if (pos >= 0 && swizzleOut) {
       if (size == 1)
-         *swizzleOut = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X,
-                                     SWIZZLE_X, SWIZZLE_X);
+         *swizzleOut = SWIZZLE_XXXX;
       else
          *swizzleOut = SWIZZLE_NOOP;
    }
@@ -248,38 +250,86 @@ _mesa_add_unnamed_constant(struct gl_program_parameter_list *paramList,
 }
 
 
+/**
+ * Add a uniform to the parameter list.
+ * Note that if the uniform is an array, size may be greater than
+ * what's implied by the datatype.
+ * \param name  uniform's name
+ * \param size  number of floats to allocate
+ * \param datatype  GL_FLOAT_VEC3, GL_FLOAT_MAT4, etc.
+ */
 GLint
 _mesa_add_uniform(struct gl_program_parameter_list *paramList,
-                  const char *name, GLuint size)
+                  const char *name, GLuint size, GLenum datatype,
+                  const GLfloat *values)
 {
    GLint i = _mesa_lookup_parameter_index(paramList, -1, name);
+   ASSERT(datatype != GL_NONE);
    if (i >= 0 && paramList->Parameters[i].Type == PROGRAM_UNIFORM) {
+      ASSERT(paramList->Parameters[i].Size == size);
+      ASSERT(paramList->Parameters[i].DataType == datatype);
       /* already in list */
       return i;
    }
    else {
       i = _mesa_add_parameter(paramList, PROGRAM_UNIFORM, name,
-                              size, NULL, NULL);
-                              
+                              size, datatype, values, NULL);
       return i;
    }
 }
 
 
+/**
+ * Mark the named uniform as 'used'.
+ */
+void
+_mesa_use_uniform(struct gl_program_parameter_list *paramList,
+                  const char *name)
+{
+   GLuint i;
+   for (i = 0; i < paramList->NumParameters; i++) {
+      struct gl_program_parameter *p = paramList->Parameters + i;
+      if (p->Type == PROGRAM_UNIFORM && _mesa_strcmp(p->Name, name) == 0) {
+         p->Used = GL_TRUE;
+         /* Note that large uniforms may occupy several slots so we're
+          * not done searching yet.
+          */
+      }
+   }
+}
+
+
+/**
+ * Add a sampler to the parameter list.
+ * \param name  uniform's name
+ * \param datatype  GL_SAMPLER_2D, GL_SAMPLER_2D_RECT_ARB, etc.
+ * \param index  the sampler number (as seen in TEX instructions)
+ * \return  sampler index (starting at zero) or -1 if error
+ */
 GLint
 _mesa_add_sampler(struct gl_program_parameter_list *paramList,
-                  const char *name)
+                  const char *name, GLenum datatype)
 {
    GLint i = _mesa_lookup_parameter_index(paramList, -1, name);
    if (i >= 0 && paramList->Parameters[i].Type == PROGRAM_SAMPLER) {
+      ASSERT(paramList->Parameters[i].Size == 1);
+      ASSERT(paramList->Parameters[i].DataType == datatype);
       /* already in list */
-      return i;
+      return (GLint) paramList->ParameterValues[i][0];
    }
    else {
-      const GLint size = 1;
-      i = _mesa_add_parameter(paramList, PROGRAM_SAMPLER, name,
-                              size, NULL, NULL);
-      return i;
+      GLuint i;
+      const GLint size = 1; /* a sampler is basically a texture unit number */
+      GLfloat value;
+      GLint numSamplers = 0;
+      for (i = 0; i < paramList->NumParameters; i++) {
+         if (paramList->Parameters[i].Type == PROGRAM_SAMPLER)
+            numSamplers++;
+      }
+      value = (GLfloat) numSamplers;
+      (void) _mesa_add_parameter(paramList, PROGRAM_SAMPLER, name,
+                                 size, datatype, &value, NULL);
+      return numSamplers;
    }
 }
 
@@ -297,9 +347,9 @@ _mesa_add_varying(struct gl_program_parameter_list *paramList,
       return i;
    }
    else {
-      assert(size == 4);
+      /*assert(size == 4);*/
       i = _mesa_add_parameter(paramList, PROGRAM_VARYING, name,
-                              size, NULL, NULL);
+                              size, GL_NONE, NULL, NULL);
       return i;
    }
 }
@@ -312,7 +362,7 @@ _mesa_add_varying(struct gl_program_parameter_list *paramList,
  */
 GLint
 _mesa_add_attribute(struct gl_program_parameter_list *paramList,
-                    const char *name, GLint size, GLint attrib)
+                    const char *name, GLint size, GLenum datatype, GLint attrib)
 {
    GLint i = _mesa_lookup_parameter_index(paramList, -1, name);
    if (i >= 0) {
@@ -328,7 +378,7 @@ _mesa_add_attribute(struct gl_program_parameter_list *paramList,
       if (size < 0)
          size = 4;
       i = _mesa_add_parameter(paramList, PROGRAM_INPUT, name,
-                              size, NULL, state);
+                              size, datatype, NULL, state);
    }
    return i;
 }
@@ -364,7 +414,7 @@ sizeof_state_reference(const GLint *stateTokens)
  *    PARAM ambient = state.material.front.ambient;
  *
  * \param paramList  the parameter list
- * \param state  an array of 6 (STATE_LENGTH) state tokens
+ * \param stateTokens  an array of 5 (STATE_LENGTH) state tokens
  * \return index of the new parameter.
  */
 GLint
@@ -372,7 +422,7 @@ _mesa_add_state_reference(struct gl_program_parameter_list *paramList,
                           const gl_state_index stateTokens[STATE_LENGTH])
 {
    const GLuint size = 4; /* XXX fix */
-   const char *name;
+   char *name;
    GLint index;
 
    /* Check if the state reference is already in the list */
@@ -394,11 +444,12 @@ _mesa_add_state_reference(struct gl_program_parameter_list *paramList,
 
    name = _mesa_program_state_string(stateTokens);
    index = _mesa_add_parameter(paramList, PROGRAM_STATE_VAR, name,
-                               size, NULL, (gl_state_index *) stateTokens);
+                               size, GL_NONE,
+                               NULL, (gl_state_index *) stateTokens);
    paramList->StateFlags |= _mesa_program_state_flags(stateTokens);
 
    /* free name string here since we duplicated it in add_parameter() */
-   _mesa_free((void *) name);
+   _mesa_free(name);
 
    return index;
 }
@@ -460,13 +511,14 @@ _mesa_lookup_parameter_index(const struct gl_program_parameter_list *paramList,
 
 /**
  * Look for a float vector in the given parameter list.  The float vector
- * may be of length 1, 2, 3 or 4.
+ * may be of length 1, 2, 3 or 4.  If swizzleOut is non-null, we'll try
+ * swizzling to find a match.
  * \param list  the parameter list to search
  * \param v  the float vector to search for
  * \param size  number of element in v
  * \param posOut  returns the position of the constant, if found
  * \param swizzleOut  returns a swizzle mask describing location of the
- *                    vector elements if found
+ *                    vector elements if found.
  * \return GL_TRUE if found, GL_FALSE if not found
  */
 GLboolean
@@ -484,43 +536,62 @@ _mesa_lookup_parameter_constant(const struct gl_program_parameter_list *list,
 
    for (i = 0; i < list->NumParameters; i++) {
       if (list->Parameters[i].Type == PROGRAM_CONSTANT) {
-         if (vSize == 1) {
-            /* look for v[0] anywhere within float[4] value */
-            GLuint j;
-            for (j = 0; j < 4; j++) {
-               if (list->ParameterValues[i][j] == v[0]) {
-                  /* found it */
-                  *posOut = i;
-                  *swizzleOut = MAKE_SWIZZLE4(j, j, j, j);
-                  return GL_TRUE;
-               }
-            }
-         }
-         else if (vSize <= list->Parameters[i].Size) {
-            /* see if we can match this constant (with a swizzle) */
-            GLuint swz[4];
-            GLuint match = 0, j, k;
+         if (!swizzleOut) {
+            /* swizzle not allowed */
+            GLuint j, match = 0;
             for (j = 0; j < vSize; j++) {
-               if (v[j] == list->ParameterValues[i][j]) {
-                  swz[j] = j;
+               if (v[j] == list->ParameterValues[i][j])
                   match++;
-               }
-               else {
-                  for (k = 0; k < list->Parameters[i].Size; k++) {
-                     if (v[j] == list->ParameterValues[i][k]) {
-                        swz[j] = k;
-                        match++;
-                        break;
-                     }
-                  }
-               }
             }
             if (match == vSize) {
                *posOut = i;
-               *swizzleOut = MAKE_SWIZZLE4(swz[0], swz[1], swz[2], swz[3]);
                return GL_TRUE;
             }
          }
+         else {
+            /* try matching w/ swizzle */
+             if (vSize == 1) {
+                /* look for v[0] anywhere within float[4] value */
+                GLuint j;
+                for (j = 0; j < 4; j++) {
+                   if (list->ParameterValues[i][j] == v[0]) {
+                      /* found it */
+                      *posOut = i;
+                      *swizzleOut = MAKE_SWIZZLE4(j, j, j, j);
+                      return GL_TRUE;
+                   }
+                }
+             }
+             else if (vSize <= list->Parameters[i].Size) {
+                /* see if we can match this constant (with a swizzle) */
+                GLuint swz[4];
+                GLuint match = 0, j, k;
+                for (j = 0; j < vSize; j++) {
+                   if (v[j] == list->ParameterValues[i][j]) {
+                      swz[j] = j;
+                      match++;
+                   }
+                   else {
+                      for (k = 0; k < list->Parameters[i].Size; k++) {
+                         if (v[j] == list->ParameterValues[i][k]) {
+                            swz[j] = k;
+                            match++;
+                            break;
+                         }
+                      }
+                   }
+                }
+                /* smear last value to remaining positions */
+                for (; j < 4; j++)
+                   swz[j] = swz[j-1];
+
+                if (match == vSize) {
+                   *posOut = i;
+                   *swizzleOut = MAKE_SWIZZLE4(swz[0], swz[1], swz[2], swz[3]);
+                   return GL_TRUE;
+                }
+             }
+         }
       }
    }
 
@@ -542,41 +613,99 @@ _mesa_clone_parameter_list(const struct gl_program_parameter_list *list)
    /** Not too efficient, but correct */
    for (i = 0; i < list->NumParameters; i++) {
       struct gl_program_parameter *p = list->Parameters + i;
+      struct gl_program_parameter *pCopy;
       GLuint size = MIN2(p->Size, 4);
-      GLint j = _mesa_add_parameter(clone, p->Type, p->Name,
-                                    size, list->ParameterValues[i], NULL);
+      GLint j = _mesa_add_parameter(clone, p->Type, p->Name, size, p->DataType,
+                                    list->ParameterValues[i], NULL);
       ASSERT(j >= 0);
+      pCopy = clone->Parameters + j;
+      pCopy->Used = p->Used;
       /* copy state indexes */
       if (p->Type == PROGRAM_STATE_VAR) {
          GLint k;
-         struct gl_program_parameter *q = clone->Parameters + j;
          for (k = 0; k < STATE_LENGTH; k++) {
-            q->StateIndexes[k] = p->StateIndexes[k];
+            pCopy->StateIndexes[k] = p->StateIndexes[k];
          }
       }
       else {
          clone->Parameters[j].Size = p->Size;
       }
+      
    }
 
+   clone->StateFlags = list->StateFlags;
+
    return clone;
 }
 
 
 /**
- * Find longest name of any parameter in list.
+ * Return a new parameter list which is listA + listB.
+ */
+struct gl_program_parameter_list *
+_mesa_combine_parameter_lists(const struct gl_program_parameter_list *listA,
+                              const struct gl_program_parameter_list *listB)
+{
+   struct gl_program_parameter_list *list;
+
+   if (listA) {
+      list = _mesa_clone_parameter_list(listA);
+      if (list && listB) {
+         GLuint i;
+         for (i = 0; i < listB->NumParameters; i++) {
+            struct gl_program_parameter *param = listB->Parameters + i;
+            _mesa_add_parameter(list, param->Type, param->Name, param->Size,
+                                param->DataType,
+                                listB->ParameterValues[i],
+                                param->StateIndexes);
+         }
+      }
+   }
+   else if (listB) {
+      list = _mesa_clone_parameter_list(listB);
+   }
+   else {
+      list = NULL;
+   }
+   return list;
+}
+
+
+
+/**
+ * Find longest name of all uniform parameters in list.
  */
 GLuint
-_mesa_parameter_longest_name(const struct gl_program_parameter_list *list)
+_mesa_longest_parameter_name(const struct gl_program_parameter_list *list,
+                             enum register_file type)
 {
    GLuint i, maxLen = 0;
    if (!list)
       return 0;
    for (i = 0; i < list->NumParameters; i++) {
-      GLuint len = _mesa_strlen(list->Parameters[i].Name);
-      if (len > maxLen)
-         maxLen = len;
+      if (list->Parameters[i].Type == type) {
+         GLuint len = _mesa_strlen(list->Parameters[i].Name);
+         if (len > maxLen)
+            maxLen = len;
+      }
    }
    return maxLen;
 }
 
+
+/**
+ * Count the number of parameters in the last that match the given type.
+ */
+GLuint
+_mesa_num_parameters_of_type(const struct gl_program_parameter_list *list,
+                             enum register_file type)
+{
+   GLuint i, count = 0;
+   if (list) {
+      for (i = 0; i < list->NumParameters; i++) {
+         if (list->Parameters[i].Type == type)
+            count++;
+      }
+   }
+   return count;
+}