13464ebd5Sriastradh/*
23464ebd5Sriastradh * Mesa 3-D graphics library
33464ebd5Sriastradh *
43464ebd5Sriastradh * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
53464ebd5Sriastradh *
63464ebd5Sriastradh * Permission is hereby granted, free of charge, to any person obtaining a
73464ebd5Sriastradh * copy of this software and associated documentation files (the "Software"),
83464ebd5Sriastradh * to deal in the Software without restriction, including without limitation
93464ebd5Sriastradh * the rights to use, copy, modify, merge, publish, distribute, sublicense,
103464ebd5Sriastradh * and/or sell copies of the Software, and to permit persons to whom the
113464ebd5Sriastradh * Software is furnished to do so, subject to the following conditions:
123464ebd5Sriastradh *
133464ebd5Sriastradh * The above copyright notice and this permission notice shall be included
143464ebd5Sriastradh * in all copies or substantial portions of the Software.
153464ebd5Sriastradh *
163464ebd5Sriastradh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
173464ebd5Sriastradh * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
183464ebd5Sriastradh * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20af69d88dSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21af69d88dSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22af69d88dSmrg * OTHER DEALINGS IN THE SOFTWARE.
233464ebd5Sriastradh */
243464ebd5Sriastradh
253464ebd5Sriastradh/**
263464ebd5Sriastradh * \file prog_parameter.c
273464ebd5Sriastradh * Program parameter lists and functions.
283464ebd5Sriastradh * \author Brian Paul
293464ebd5Sriastradh */
303464ebd5Sriastradh
313464ebd5Sriastradh#include "main/glheader.h"
323464ebd5Sriastradh#include "main/macros.h"
337ec681f3Smrg#include "main/errors.h"
347ec681f3Smrg#include "util/u_memory.h"
353464ebd5Sriastradh#include "prog_instruction.h"
363464ebd5Sriastradh#include "prog_parameter.h"
373464ebd5Sriastradh#include "prog_statevars.h"
383464ebd5Sriastradh
393464ebd5Sriastradh
4001e04c3fSmrg/**
4101e04c3fSmrg * Look for a float vector in the given parameter list.  The float vector
4201e04c3fSmrg * may be of length 1, 2, 3 or 4.  If swizzleOut is non-null, we'll try
4301e04c3fSmrg * swizzling to find a match.
4401e04c3fSmrg * \param list  the parameter list to search
4501e04c3fSmrg * \param v  the float vector to search for
4601e04c3fSmrg * \param vSize  number of element in v
4701e04c3fSmrg * \param posOut  returns the position of the constant, if found
4801e04c3fSmrg * \param swizzleOut  returns a swizzle mask describing location of the
4901e04c3fSmrg *                    vector elements if found.
5001e04c3fSmrg * \return GL_TRUE if found, GL_FALSE if not found
5101e04c3fSmrg */
5201e04c3fSmrgstatic GLboolean
5301e04c3fSmrglookup_parameter_constant(const struct gl_program_parameter_list *list,
5401e04c3fSmrg                          const gl_constant_value v[], GLuint vSize,
5501e04c3fSmrg                          GLint *posOut, GLuint *swizzleOut)
5601e04c3fSmrg{
5701e04c3fSmrg   GLuint i;
5801e04c3fSmrg
5901e04c3fSmrg   assert(vSize >= 1);
6001e04c3fSmrg   assert(vSize <= 4);
6101e04c3fSmrg
6201e04c3fSmrg   if (!list) {
6301e04c3fSmrg      *posOut = -1;
6401e04c3fSmrg      return GL_FALSE;
6501e04c3fSmrg   }
6601e04c3fSmrg
6701e04c3fSmrg   for (i = 0; i < list->NumParameters; i++) {
6801e04c3fSmrg      if (list->Parameters[i].Type == PROGRAM_CONSTANT) {
697ec681f3Smrg         unsigned offset = list->Parameters[i].ValueOffset;
7001e04c3fSmrg
7101e04c3fSmrg         if (!swizzleOut) {
7201e04c3fSmrg            /* swizzle not allowed */
7301e04c3fSmrg            GLuint j, match = 0;
7401e04c3fSmrg            for (j = 0; j < vSize; j++) {
7501e04c3fSmrg               if (v[j].u == list->ParameterValues[offset + j].u)
7601e04c3fSmrg                  match++;
7701e04c3fSmrg            }
7801e04c3fSmrg            if (match == vSize) {
7901e04c3fSmrg               *posOut = i;
8001e04c3fSmrg               return GL_TRUE;
8101e04c3fSmrg            }
8201e04c3fSmrg         }
8301e04c3fSmrg         else {
8401e04c3fSmrg            /* try matching w/ swizzle */
8501e04c3fSmrg             if (vSize == 1) {
8601e04c3fSmrg                /* look for v[0] anywhere within float[4] value */
8701e04c3fSmrg                GLuint j;
8801e04c3fSmrg                for (j = 0; j < list->Parameters[i].Size; j++) {
8901e04c3fSmrg                   if (list->ParameterValues[offset + j].u == v[0].u) {
9001e04c3fSmrg                      /* found it */
9101e04c3fSmrg                      *posOut = i;
9201e04c3fSmrg                      *swizzleOut = MAKE_SWIZZLE4(j, j, j, j);
9301e04c3fSmrg                      return GL_TRUE;
9401e04c3fSmrg                   }
9501e04c3fSmrg                }
9601e04c3fSmrg             }
9701e04c3fSmrg             else if (vSize <= list->Parameters[i].Size) {
9801e04c3fSmrg                /* see if we can match this constant (with a swizzle) */
9901e04c3fSmrg                GLuint swz[4];
10001e04c3fSmrg                GLuint match = 0, j, k;
10101e04c3fSmrg                for (j = 0; j < vSize; j++) {
10201e04c3fSmrg                   if (v[j].u == list->ParameterValues[offset + j].u) {
10301e04c3fSmrg                      swz[j] = j;
10401e04c3fSmrg                      match++;
10501e04c3fSmrg                   }
10601e04c3fSmrg                   else {
10701e04c3fSmrg                      for (k = 0; k < list->Parameters[i].Size; k++) {
10801e04c3fSmrg                         if (v[j].u == list->ParameterValues[offset + k].u) {
10901e04c3fSmrg                            swz[j] = k;
11001e04c3fSmrg                            match++;
11101e04c3fSmrg                            break;
11201e04c3fSmrg                         }
11301e04c3fSmrg                      }
11401e04c3fSmrg                   }
11501e04c3fSmrg                }
11601e04c3fSmrg                /* smear last value to remaining positions */
11701e04c3fSmrg                for (; j < 4; j++)
11801e04c3fSmrg                   swz[j] = swz[j-1];
11901e04c3fSmrg
12001e04c3fSmrg                if (match == vSize) {
12101e04c3fSmrg                   *posOut = i;
12201e04c3fSmrg                   *swizzleOut = MAKE_SWIZZLE4(swz[0], swz[1], swz[2], swz[3]);
12301e04c3fSmrg                   return GL_TRUE;
12401e04c3fSmrg                }
12501e04c3fSmrg             }
12601e04c3fSmrg         }
12701e04c3fSmrg      }
12801e04c3fSmrg   }
12901e04c3fSmrg
13001e04c3fSmrg   *posOut = -1;
13101e04c3fSmrg   return GL_FALSE;
13201e04c3fSmrg}
13301e04c3fSmrg
13401e04c3fSmrg
1353464ebd5Sriastradhstruct gl_program_parameter_list *
1363464ebd5Sriastradh_mesa_new_parameter_list(void)
1373464ebd5Sriastradh{
1387ec681f3Smrg   struct gl_program_parameter_list *list =
1397ec681f3Smrg      CALLOC_STRUCT(gl_program_parameter_list);
1407ec681f3Smrg   if (!list)
1417ec681f3Smrg      return NULL;
1427ec681f3Smrg
1437ec681f3Smrg   list->UniformBytes = 0;
1447ec681f3Smrg   list->FirstStateVarIndex = INT_MAX;
1457ec681f3Smrg   list->LastStateVarIndex = 0;
1467ec681f3Smrg   return list;
1473464ebd5Sriastradh}
1483464ebd5Sriastradh
1493464ebd5Sriastradh
1503464ebd5Sriastradhstruct gl_program_parameter_list *
1513464ebd5Sriastradh_mesa_new_parameter_list_sized(unsigned size)
1523464ebd5Sriastradh{
1533464ebd5Sriastradh   struct gl_program_parameter_list *p = _mesa_new_parameter_list();
1543464ebd5Sriastradh
1553464ebd5Sriastradh
1567ec681f3Smrg   if ((p != NULL) && (size != 0)) {
1577ec681f3Smrg      _mesa_reserve_parameter_storage(p, size, size);
1583464ebd5Sriastradh
1593464ebd5Sriastradh      if ((p->Parameters == NULL) || (p->ParameterValues == NULL)) {
16001e04c3fSmrg         free(p->Parameters);
1617ec681f3Smrg         align_free(p->ParameterValues);
16201e04c3fSmrg         free(p);
16301e04c3fSmrg         p = NULL;
1643464ebd5Sriastradh      }
1653464ebd5Sriastradh   }
1663464ebd5Sriastradh
1673464ebd5Sriastradh   return p;
1683464ebd5Sriastradh}
1693464ebd5Sriastradh
1703464ebd5Sriastradh
1713464ebd5Sriastradh/**
1723464ebd5Sriastradh * Free a parameter list and all its parameters
1733464ebd5Sriastradh */
1743464ebd5Sriastradhvoid
1753464ebd5Sriastradh_mesa_free_parameter_list(struct gl_program_parameter_list *paramList)
1763464ebd5Sriastradh{
1773464ebd5Sriastradh   GLuint i;
1783464ebd5Sriastradh   for (i = 0; i < paramList->NumParameters; i++) {
179af69d88dSmrg      free((void *)paramList->Parameters[i].Name);
1803464ebd5Sriastradh   }
1813464ebd5Sriastradh   free(paramList->Parameters);
1827ec681f3Smrg   align_free(paramList->ParameterValues);
1833464ebd5Sriastradh   free(paramList);
1843464ebd5Sriastradh}
1853464ebd5Sriastradh
1863464ebd5Sriastradh
18701e04c3fSmrg/**
18801e04c3fSmrg * Make sure there are enough unused parameter slots. Reallocate the list
18901e04c3fSmrg * if needed.
19001e04c3fSmrg *
19101e04c3fSmrg * \param paramList        where to reserve parameter slots
1927ec681f3Smrg * \param reserve_params   number of parameter description slots
1937ec681f3Smrg * \param reserve_values   number of parameter vec4 slots
19401e04c3fSmrg */
19501e04c3fSmrgvoid
19601e04c3fSmrg_mesa_reserve_parameter_storage(struct gl_program_parameter_list *paramList,
1977ec681f3Smrg                                unsigned reserve_params,
1987ec681f3Smrg                                unsigned reserve_values)
19901e04c3fSmrg{
20001e04c3fSmrg   const GLuint oldNum = paramList->NumParameters;
2017ec681f3Smrg   const unsigned oldValNum = paramList->NumParameterValues;
2027ec681f3Smrg   const unsigned needSizeValues = oldValNum + reserve_values * 4;
2037ec681f3Smrg
2047ec681f3Smrg   if (paramList->DisallowRealloc &&
2057ec681f3Smrg       (oldNum + reserve_params > paramList->Size ||
2067ec681f3Smrg        needSizeValues > paramList->SizeValues)) {
2077ec681f3Smrg      _mesa_problem(NULL, "Parameter storage reallocation disallowed. This "
2087ec681f3Smrg              "is a Mesa bug. Increase the reservation size in the code.");
2097ec681f3Smrg      abort();
2107ec681f3Smrg   }
21101e04c3fSmrg
2127ec681f3Smrg   if (oldNum + reserve_params > paramList->Size) {
21301e04c3fSmrg      /* Need to grow the parameter list array (alloc some extra) */
2147ec681f3Smrg      paramList->Size += 4 * reserve_params;
21501e04c3fSmrg
21601e04c3fSmrg      /* realloc arrays */
21701e04c3fSmrg      paramList->Parameters =
21801e04c3fSmrg         realloc(paramList->Parameters,
21901e04c3fSmrg                 paramList->Size * sizeof(struct gl_program_parameter));
2207ec681f3Smrg   }
22101e04c3fSmrg
2227ec681f3Smrg   if (needSizeValues > paramList->SizeValues) {
2237ec681f3Smrg      unsigned oldSize = paramList->SizeValues;
2247ec681f3Smrg      paramList->SizeValues = needSizeValues + 16; /* alloc some extra */
22501e04c3fSmrg
22601e04c3fSmrg      paramList->ParameterValues = (gl_constant_value *)
2277ec681f3Smrg         align_realloc(paramList->ParameterValues,         /* old buf */
2287ec681f3Smrg                       oldValNum * sizeof(gl_constant_value),/* old sz */
2297ec681f3Smrg                       /* Overallocate the size by 12 because matrix rows can
2307ec681f3Smrg                        * be allocated partially but fetch_state always writes
2317ec681f3Smrg                        * 4 components (16 bytes).
2327ec681f3Smrg                        */
2337ec681f3Smrg                       paramList->SizeValues * sizeof(gl_constant_value) +
2347ec681f3Smrg                       12, 16);
2357ec681f3Smrg      /* The values are written to the shader cache, so clear them. */
2367ec681f3Smrg      memset(paramList->ParameterValues + oldSize, 0,
2377ec681f3Smrg             (paramList->SizeValues - oldSize) * sizeof(gl_constant_value));
23801e04c3fSmrg   }
23901e04c3fSmrg}
24001e04c3fSmrg
24101e04c3fSmrg
2427ec681f3Smrg/**
2437ec681f3Smrg * Disallow reallocating the parameter storage, so that uniform storage
2447ec681f3Smrg * can have pointers pointing to it.
2457ec681f3Smrg */
2467ec681f3Smrgvoid
2477ec681f3Smrg_mesa_disallow_parameter_storage_realloc(struct gl_program_parameter_list *paramList)
2487ec681f3Smrg{
2497ec681f3Smrg   paramList->DisallowRealloc = true;
2507ec681f3Smrg}
2517ec681f3Smrg
2527ec681f3Smrg
2533464ebd5Sriastradh/**
2543464ebd5Sriastradh * Add a new parameter to a parameter list.
2553464ebd5Sriastradh * Note that parameter values are usually 4-element GLfloat vectors.
2563464ebd5Sriastradh * When size > 4 we'll allocate a sequential block of parameters to
2573464ebd5Sriastradh * store all the values (in blocks of 4).
2583464ebd5Sriastradh *
2593464ebd5Sriastradh * \param paramList  the list to add the parameter to
2607ec681f3Smrg * \param type  type of parameter, such as
2613464ebd5Sriastradh * \param name  the parameter name, will be duplicated/copied!
2623464ebd5Sriastradh * \param size  number of elements in 'values' vector (1..4, or more)
2633464ebd5Sriastradh * \param datatype  GL_FLOAT, GL_FLOAT_VECx, GL_INT, GL_INT_VECx or GL_NONE.
264af69d88dSmrg * \param values  initial parameter value, up to 4 gl_constant_values, or NULL
2653464ebd5Sriastradh * \param state  state indexes, or NULL
2663464ebd5Sriastradh * \return  index of new parameter in the list, or -1 if error (out of mem)
2673464ebd5Sriastradh */
2683464ebd5SriastradhGLint
2693464ebd5Sriastradh_mesa_add_parameter(struct gl_program_parameter_list *paramList,
2703464ebd5Sriastradh                    gl_register_file type, const char *name,
271af69d88dSmrg                    GLuint size, GLenum datatype,
272af69d88dSmrg                    const gl_constant_value *values,
27301e04c3fSmrg                    const gl_state_index16 state[STATE_LENGTH],
27401e04c3fSmrg                    bool pad_and_align)
2753464ebd5Sriastradh{
2767ec681f3Smrg   assert(0 < size);
2777ec681f3Smrg   const int oldNum = paramList->NumParameters;
278b9abf16eSmaya   unsigned oldValNum = paramList->NumParameterValues;
2797ec681f3Smrg   const unsigned padded_size = pad_and_align ? align(size, 4) : size;
280b9abf16eSmaya
281b9abf16eSmaya   if (pad_and_align)
282b9abf16eSmaya      oldValNum = align(oldValNum, 4); /* pad start to a vec4 boundary */
283b9abf16eSmaya   else if (_mesa_gl_datatype_is_64bit(datatype))
284b9abf16eSmaya      oldValNum = align(oldValNum, 2); /* pad start to 64-bit */
2853464ebd5Sriastradh
2867ec681f3Smrg   unsigned elements = (oldValNum - paramList->NumParameterValues) + padded_size;
2877ec681f3Smrg   _mesa_reserve_parameter_storage(paramList, 1, DIV_ROUND_UP(elements, 4));
2883464ebd5Sriastradh
2897ec681f3Smrg   if (!paramList->Parameters ||
2903464ebd5Sriastradh       !paramList->ParameterValues) {
2913464ebd5Sriastradh      /* out of memory */
2923464ebd5Sriastradh      paramList->NumParameters = 0;
2933464ebd5Sriastradh      paramList->Size = 0;
2947ec681f3Smrg      paramList->SizeValues = 0;
2953464ebd5Sriastradh      return -1;
2963464ebd5Sriastradh   }
29701e04c3fSmrg
29801e04c3fSmrg   paramList->NumParameters = oldNum + 1;
29901e04c3fSmrg
3007ec681f3Smrg   paramList->NumParameterValues = oldValNum + padded_size;
30101e04c3fSmrg
30201e04c3fSmrg   memset(&paramList->Parameters[oldNum], 0,
30301e04c3fSmrg          sizeof(struct gl_program_parameter));
30401e04c3fSmrg
30501e04c3fSmrg   struct gl_program_parameter *p = paramList->Parameters + oldNum;
30601e04c3fSmrg   p->Name = strdup(name ? name : "");
30701e04c3fSmrg   p->Type = type;
30801e04c3fSmrg   p->Size = size;
309993e1d59Smrg   p->Padded = pad_and_align;
31001e04c3fSmrg   p->DataType = datatype;
31101e04c3fSmrg
3127ec681f3Smrg   paramList->Parameters[oldNum].ValueOffset = oldValNum;
31301e04c3fSmrg   if (values) {
31401e04c3fSmrg      if (size >= 4) {
3157ec681f3Smrg         memcpy(paramList->ParameterValues + oldValNum, values,
3167ec681f3Smrg                size * sizeof(values[0]));
31701e04c3fSmrg      } else {
31801e04c3fSmrg         /* copy 1, 2 or 3 values */
31901e04c3fSmrg         assert(size < 4);
32001e04c3fSmrg         unsigned j;
32101e04c3fSmrg         for (j = 0; j < size; j++) {
32201e04c3fSmrg            paramList->ParameterValues[oldValNum + j].f = values[j].f;
3233464ebd5Sriastradh         }
32401e04c3fSmrg
32501e04c3fSmrg         /* Zero out padding (if any) to avoid valgrind errors */
3267ec681f3Smrg         for (; j < padded_size; j++) {
32701e04c3fSmrg            paramList->ParameterValues[oldValNum + j].f = 0;
3283464ebd5Sriastradh         }
3293464ebd5Sriastradh      }
33001e04c3fSmrg   } else {
3317ec681f3Smrg      for (unsigned j = 0; j < padded_size; j++) {
33201e04c3fSmrg         paramList->ParameterValues[oldValNum + j].f = 0;
3333464ebd5Sriastradh      }
3343464ebd5Sriastradh   }
3353464ebd5Sriastradh
33601e04c3fSmrg   if (state) {
33701e04c3fSmrg      for (unsigned i = 0; i < STATE_LENGTH; i++)
33801e04c3fSmrg         paramList->Parameters[oldNum].StateIndexes[i] = state[i];
3397ec681f3Smrg   } else {
3407ec681f3Smrg      paramList->Parameters[oldNum].StateIndexes[0] = STATE_NOT_STATE_VAR;
3413464ebd5Sriastradh   }
34201e04c3fSmrg
3437ec681f3Smrg   if (type == PROGRAM_UNIFORM || type == PROGRAM_CONSTANT) {
3447ec681f3Smrg      paramList->UniformBytes =
3457ec681f3Smrg         MAX2(paramList->UniformBytes,
3467ec681f3Smrg              (paramList->Parameters[oldNum].ValueOffset +
3477ec681f3Smrg               paramList->Parameters[oldNum].Size) * 4);
3487ec681f3Smrg   } else if (type == PROGRAM_STATE_VAR) {
3497ec681f3Smrg      paramList->FirstStateVarIndex =
3507ec681f3Smrg         MIN2(paramList->FirstStateVarIndex, oldNum);
3517ec681f3Smrg      paramList->LastStateVarIndex =
3527ec681f3Smrg         MAX2(paramList->LastStateVarIndex, oldNum);
3537ec681f3Smrg   } else {
3547ec681f3Smrg      unreachable("invalid parameter type");
3557ec681f3Smrg   }
3567ec681f3Smrg
3577ec681f3Smrg   assert(paramList->NumParameters <= paramList->Size);
3587ec681f3Smrg   assert(paramList->NumParameterValues <= paramList->SizeValues);
3597ec681f3Smrg
36001e04c3fSmrg   return (GLint) oldNum;
3613464ebd5Sriastradh}
3623464ebd5Sriastradh
3633464ebd5Sriastradh
3643464ebd5Sriastradh/**
3653464ebd5Sriastradh * Add a new unnamed constant to the parameter list.  This will be used
3663464ebd5Sriastradh * when a fragment/vertex program contains something like this:
3673464ebd5Sriastradh *    MOV r, { 0, 1, 2, 3 };
3683464ebd5Sriastradh * If swizzleOut is non-null we'll search the parameter list for an
3693464ebd5Sriastradh * existing instance of the constant which matches with a swizzle.
3703464ebd5Sriastradh *
3713464ebd5Sriastradh * \param paramList  the parameter list
3723464ebd5Sriastradh * \param values  four float values
3733464ebd5Sriastradh * \param swizzleOut  returns swizzle mask for accessing the constant
3743464ebd5Sriastradh * \return index/position of the new parameter in the parameter list.
3753464ebd5Sriastradh */
3763464ebd5SriastradhGLint
377af69d88dSmrg_mesa_add_typed_unnamed_constant(struct gl_program_parameter_list *paramList,
3787ec681f3Smrg                           const gl_constant_value *values, GLuint size,
379af69d88dSmrg                           GLenum datatype, GLuint *swizzleOut)
3803464ebd5Sriastradh{
3813464ebd5Sriastradh   GLint pos;
38201e04c3fSmrg   assert(size >= 1);
38301e04c3fSmrg   assert(size <= 4);
3843464ebd5Sriastradh
3853464ebd5Sriastradh   if (swizzleOut &&
38601e04c3fSmrg       lookup_parameter_constant(paramList, values, size, &pos, swizzleOut)) {
3873464ebd5Sriastradh      return pos;
3883464ebd5Sriastradh   }
3893464ebd5Sriastradh
3903464ebd5Sriastradh   /* Look for empty space in an already unnamed constant parameter
3913464ebd5Sriastradh    * to add this constant.  This will only work for single-element
3923464ebd5Sriastradh    * constants because we rely on smearing (i.e. .yyyy or .zzzz).
3933464ebd5Sriastradh    */
3943464ebd5Sriastradh   if (size == 1 && swizzleOut) {
3953464ebd5Sriastradh      for (pos = 0; pos < (GLint) paramList->NumParameters; pos++) {
3963464ebd5Sriastradh         struct gl_program_parameter *p = paramList->Parameters + pos;
3977ec681f3Smrg         unsigned offset = paramList->Parameters[pos].ValueOffset;
3983464ebd5Sriastradh         if (p->Type == PROGRAM_CONSTANT && p->Size + size <= 4) {
3993464ebd5Sriastradh            /* ok, found room */
40001e04c3fSmrg            gl_constant_value *pVal = paramList->ParameterValues + offset;
4013464ebd5Sriastradh            GLuint swz = p->Size; /* 1, 2 or 3 for Y, Z, W */
4023464ebd5Sriastradh            pVal[p->Size] = values[0];
4033464ebd5Sriastradh            p->Size++;
4043464ebd5Sriastradh            *swizzleOut = MAKE_SWIZZLE4(swz, swz, swz, swz);
4053464ebd5Sriastradh            return pos;
4063464ebd5Sriastradh         }
4073464ebd5Sriastradh      }
4083464ebd5Sriastradh   }
4093464ebd5Sriastradh
4103464ebd5Sriastradh   /* add a new parameter to store this constant */
4113464ebd5Sriastradh   pos = _mesa_add_parameter(paramList, PROGRAM_CONSTANT, NULL,
41201e04c3fSmrg                             size, datatype, values, NULL, true);
4133464ebd5Sriastradh   if (pos >= 0 && swizzleOut) {
4143464ebd5Sriastradh      if (size == 1)
4153464ebd5Sriastradh         *swizzleOut = SWIZZLE_XXXX;
4163464ebd5Sriastradh      else
4173464ebd5Sriastradh         *swizzleOut = SWIZZLE_NOOP;
4183464ebd5Sriastradh   }
4193464ebd5Sriastradh   return pos;
4203464ebd5Sriastradh}
4213464ebd5Sriastradh
4223464ebd5SriastradhGLint
42301e04c3fSmrg_mesa_add_sized_state_reference(struct gl_program_parameter_list *paramList,
42401e04c3fSmrg                                const gl_state_index16 stateTokens[STATE_LENGTH],
42501e04c3fSmrg                                const unsigned size, bool pad_and_align)
4263464ebd5Sriastradh{
4273464ebd5Sriastradh   char *name;
4283464ebd5Sriastradh   GLint index;
4293464ebd5Sriastradh
4303464ebd5Sriastradh   /* Check if the state reference is already in the list */
4313464ebd5Sriastradh   for (index = 0; index < (GLint) paramList->NumParameters; index++) {
4323464ebd5Sriastradh      if (!memcmp(paramList->Parameters[index].StateIndexes,
43301e04c3fSmrg                  stateTokens,
43401e04c3fSmrg                  sizeof(paramList->Parameters[index].StateIndexes))) {
43501e04c3fSmrg         return index;
4363464ebd5Sriastradh      }
4373464ebd5Sriastradh   }
4383464ebd5Sriastradh
4393464ebd5Sriastradh   name = _mesa_program_state_string(stateTokens);
4403464ebd5Sriastradh   index = _mesa_add_parameter(paramList, PROGRAM_STATE_VAR, name,
44101e04c3fSmrg                               size, GL_NONE, NULL, stateTokens,
44201e04c3fSmrg                               pad_and_align);
4433464ebd5Sriastradh   paramList->StateFlags |= _mesa_program_state_flags(stateTokens);
4443464ebd5Sriastradh
4453464ebd5Sriastradh   /* free name string here since we duplicated it in add_parameter() */
4463464ebd5Sriastradh   free(name);
4473464ebd5Sriastradh
4483464ebd5Sriastradh   return index;
4493464ebd5Sriastradh}
4503464ebd5Sriastradh
4513464ebd5Sriastradh
4523464ebd5Sriastradh/**
45301e04c3fSmrg * Add a new state reference to the parameter list.
45401e04c3fSmrg * This will be used when the program contains something like this:
45501e04c3fSmrg *    PARAM ambient = state.material.front.ambient;
45601e04c3fSmrg *
45701e04c3fSmrg * \param paramList  the parameter list
4587ec681f3Smrg * \param stateTokens  an array of STATE_LENGTH state tokens
45901e04c3fSmrg * \return index of the new parameter.
4603464ebd5Sriastradh */
4613464ebd5SriastradhGLint
46201e04c3fSmrg_mesa_add_state_reference(struct gl_program_parameter_list *paramList,
46301e04c3fSmrg                          const gl_state_index16 stateTokens[STATE_LENGTH])
4643464ebd5Sriastradh{
46501e04c3fSmrg   return _mesa_add_sized_state_reference(paramList, stateTokens, 4, true);
4663464ebd5Sriastradh}
4677ec681f3Smrg
4687ec681f3Smrgvoid
4697ec681f3Smrg_mesa_recompute_parameter_bounds(struct gl_program_parameter_list *list)
4707ec681f3Smrg{
4717ec681f3Smrg   list->UniformBytes = 0;
4727ec681f3Smrg   list->FirstStateVarIndex = INT_MAX;
4737ec681f3Smrg   list->LastStateVarIndex = 0;
4747ec681f3Smrg
4757ec681f3Smrg   for (int i = 0; i < (int)list->NumParameters; i++) {
4767ec681f3Smrg      if (list->Parameters[i].Type == PROGRAM_STATE_VAR) {
4777ec681f3Smrg         list->FirstStateVarIndex = MIN2(list->FirstStateVarIndex, i);
4787ec681f3Smrg         list->LastStateVarIndex = MAX2(list->LastStateVarIndex, i);
4797ec681f3Smrg      } else {
4807ec681f3Smrg         list->UniformBytes = MAX2(list->UniformBytes,
4817ec681f3Smrg                                   (list->Parameters[i].ValueOffset +
4827ec681f3Smrg                                    list->Parameters[i].Size) * 4);
4837ec681f3Smrg      }
4847ec681f3Smrg   }
4857ec681f3Smrg}
486