mesa: added _mesa_print_vp/p_inputs() functions (debug aids)
[mesa.git] / src / mesa / shader / prog_parameter.c
index 6744ad93b81af55baf9b3007fd191b773b004103..5822510701bd88dac446cdccb4062f38cc3074e2 100644 (file)
@@ -44,6 +44,34 @@ _mesa_new_parameter_list(void)
 }
 
 
+struct gl_program_parameter_list *
+_mesa_new_parameter_list_sized(unsigned size)
+{
+   struct gl_program_parameter_list *p = _mesa_new_parameter_list();
+
+   if ((p != NULL) && (size != 0)) {
+      p->Size = size;
+
+      /* alloc arrays */
+      p->Parameters = (struct gl_program_parameter *)
+        _mesa_calloc(size * sizeof(struct gl_program_parameter));
+
+      p->ParameterValues = (GLfloat (*)[4])
+         _mesa_align_malloc(size * 4 *sizeof(GLfloat), 16);
+
+
+      if ((p->Parameters == NULL) || (p->ParameterValues == NULL)) {
+        _mesa_free(p->Parameters);
+        _mesa_align_free(p->ParameterValues);
+        _mesa_free(p);
+        p = NULL;
+      }
+   }
+
+   return p;
+}
+
+
 /**
  * Free a parameter list and all its parameters
  */
@@ -72,13 +100,14 @@ _mesa_free_parameter_list(struct gl_program_parameter_list *paramList)
  * \param type  type of parameter, such as 
  * \param name  the parameter name, will be duplicated/copied!
  * \param size  number of elements in 'values' vector (1..4, or more)
+ * \param datatype  GL_FLOAT, GL_FLOAT_VECx, GL_INT, GL_INT_VECx or GL_NONE.
  * \param values  initial parameter value, up to 4 GLfloats, or NULL
  * \param state  state indexes, or NULL
  * \return  index of new parameter in the list, or -1 if error (out of mem)
  */
 GLint
 _mesa_add_parameter(struct gl_program_parameter_list *paramList,
-                    enum register_file type, const char *name,
+                    gl_register_file type, const char *name,
                     GLuint size, GLenum datatype, const GLfloat *values,
                     const gl_state_index state[STATE_LENGTH],
                     GLbitfield flags)
@@ -178,16 +207,20 @@ _mesa_add_named_constant(struct gl_program_parameter_list *paramList,
                          const char *name, const GLfloat values[4],
                          GLuint size)
 {
-#if 0 /* disable this for now -- we need to save the name! */
+   /* first check if this is a duplicate constant */
    GLint pos;
-   GLuint swizzle;
-   ASSERT(size == 4); /* XXX future feature */
-   /* check if we already have this constant */
-   if (_mesa_lookup_parameter_constant(paramList, values, 4, &pos, &swizzle)) {
-      return pos;
+   for (pos = 0; pos < paramList->NumParameters; pos++) {
+      const GLfloat *pvals = paramList->ParameterValues[pos];
+      if (pvals[0] == values[0] &&
+          pvals[1] == values[1] &&
+          pvals[2] == values[2] &&
+          pvals[3] == values[3] &&
+          _mesa_strcmp(paramList->Parameters[pos].Name, name) == 0) {
+         /* Same name and value is already in the param list - reuse it */
+         return pos;
+      }
    }
-#endif
-   size = 4; /** XXX fix */
+   /* not found, add new parameter */
    return _mesa_add_parameter(paramList, PROGRAM_CONSTANT, name,
                               size, GL_NONE, values, NULL, 0x0);
 }
@@ -197,9 +230,8 @@ _mesa_add_named_constant(struct gl_program_parameter_list *paramList,
  * 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.
+ * If swizzleOut is non-null we'll search the parameter list for an
+ * existing instance of the constant which matches with a swizzle.
  *
  * \param paramList  the parameter list
  * \param values  four float values
@@ -215,7 +247,8 @@ _mesa_add_unnamed_constant(struct gl_program_parameter_list *paramList,
    ASSERT(size >= 1);
    ASSERT(size <= 4);
 
-   if (_mesa_lookup_parameter_constant(paramList, values,
+   if (swizzleOut &&
+       _mesa_lookup_parameter_constant(paramList, values,
                                        size, &pos, swizzleOut)) {
       return pos;
    }
@@ -291,7 +324,8 @@ _mesa_use_uniform(struct gl_program_parameter_list *paramList,
    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) {
+      if ((p->Type == PROGRAM_UNIFORM || p->Type == PROGRAM_SAMPLER) &&
+          _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.
@@ -322,15 +356,16 @@ _mesa_add_sampler(struct gl_program_parameter_list *paramList,
    else {
       GLuint i;
       const GLint size = 1; /* a sampler is basically a texture unit number */
-      GLfloat value;
+      GLfloat value[4];
       GLint numSamplers = 0;
       for (i = 0; i < paramList->NumParameters; i++) {
          if (paramList->Parameters[i].Type == PROGRAM_SAMPLER)
             numSamplers++;
       }
-      value = (GLfloat) numSamplers;
+      value[0] = (GLfloat) numSamplers;
+      value[1] = value[2] = value[3] = 0.0F;
       (void) _mesa_add_parameter(paramList, PROGRAM_SAMPLER, name,
-                                 size, datatype, &value, NULL, 0x0);
+                                 size, datatype, value, NULL, 0x0);
       return numSamplers;
    }
 }
@@ -465,7 +500,7 @@ GLfloat *
 _mesa_lookup_parameter_value(const struct gl_program_parameter_list *paramList,
                              GLsizei nameLen, const char *name)
 {
-   GLuint i = _mesa_lookup_parameter_index(paramList, nameLen, name);
+   GLint i = _mesa_lookup_parameter_index(paramList, nameLen, name);
    if (i < 0)
       return NULL;
    else
@@ -517,7 +552,7 @@ _mesa_lookup_parameter_index(const struct gl_program_parameter_list *paramList,
  * 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 vSize  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.
@@ -681,7 +716,7 @@ _mesa_combine_parameter_lists(const struct gl_program_parameter_list *listA,
  */
 GLuint
 _mesa_longest_parameter_name(const struct gl_program_parameter_list *list,
-                             enum register_file type)
+                             gl_register_file type)
 {
    GLuint i, maxLen = 0;
    if (!list)
@@ -702,7 +737,7 @@ _mesa_longest_parameter_name(const struct gl_program_parameter_list *list,
  */
 GLuint
 _mesa_num_parameters_of_type(const struct gl_program_parameter_list *list,
-                             enum register_file type)
+                             gl_register_file type)
 {
    GLuint i, count = 0;
    if (list) {