Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the "Software"),
  8.  * to deal in the Software without restriction, including without limitation
  9.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10.  * and/or sell copies of the Software, and to permit persons to whom the
  11.  * Software is furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included
  14.  * in all copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  19.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22.  * OTHER DEALINGS IN THE SOFTWARE.
  23.  */
  24.  
  25. /**
  26.  * \file prog_parameter.c
  27.  * Program parameter lists and functions.
  28.  * \author Brian Paul
  29.  */
  30.  
  31.  
  32. #include "main/glheader.h"
  33. #include "main/imports.h"
  34. #include "main/macros.h"
  35. #include "prog_instruction.h"
  36. #include "prog_parameter.h"
  37. #include "prog_statevars.h"
  38.  
  39.  
  40. struct gl_program_parameter_list *
  41. _mesa_new_parameter_list(void)
  42. {
  43.    return CALLOC_STRUCT(gl_program_parameter_list);
  44. }
  45.  
  46.  
  47. struct gl_program_parameter_list *
  48. _mesa_new_parameter_list_sized(unsigned size)
  49. {
  50.    struct gl_program_parameter_list *p = _mesa_new_parameter_list();
  51.  
  52.    if ((p != NULL) && (size != 0)) {
  53.       p->Size = size;
  54.  
  55.       /* alloc arrays */
  56.       p->Parameters = (struct gl_program_parameter *)
  57.          calloc(1, size * sizeof(struct gl_program_parameter));
  58.  
  59.       p->ParameterValues = (gl_constant_value (*)[4])
  60.          _mesa_align_malloc(size * 4 *sizeof(gl_constant_value), 16);
  61.  
  62.  
  63.       if ((p->Parameters == NULL) || (p->ParameterValues == NULL)) {
  64.          free(p->Parameters);
  65.          _mesa_align_free(p->ParameterValues);
  66.          free(p);
  67.          p = NULL;
  68.       }
  69.    }
  70.  
  71.    return p;
  72. }
  73.  
  74.  
  75. /**
  76.  * Free a parameter list and all its parameters
  77.  */
  78. void
  79. _mesa_free_parameter_list(struct gl_program_parameter_list *paramList)
  80. {
  81.    GLuint i;
  82.    for (i = 0; i < paramList->NumParameters; i++) {
  83.       free((void *)paramList->Parameters[i].Name);
  84.    }
  85.    free(paramList->Parameters);
  86.    if (paramList->ParameterValues)
  87.       _mesa_align_free(paramList->ParameterValues);
  88.    free(paramList);
  89. }
  90.  
  91.  
  92. /**
  93.  * Add a new parameter to a parameter list.
  94.  * Note that parameter values are usually 4-element GLfloat vectors.
  95.  * When size > 4 we'll allocate a sequential block of parameters to
  96.  * store all the values (in blocks of 4).
  97.  *
  98.  * \param paramList  the list to add the parameter to
  99.  * \param type  type of parameter, such as
  100.  * \param name  the parameter name, will be duplicated/copied!
  101.  * \param size  number of elements in 'values' vector (1..4, or more)
  102.  * \param datatype  GL_FLOAT, GL_FLOAT_VECx, GL_INT, GL_INT_VECx or GL_NONE.
  103.  * \param values  initial parameter value, up to 4 gl_constant_values, or NULL
  104.  * \param state  state indexes, or NULL
  105.  * \return  index of new parameter in the list, or -1 if error (out of mem)
  106.  */
  107. GLint
  108. _mesa_add_parameter(struct gl_program_parameter_list *paramList,
  109.                     gl_register_file type, const char *name,
  110.                     GLuint size, GLenum datatype,
  111.                     const gl_constant_value *values,
  112.                     const gl_state_index state[STATE_LENGTH])
  113. {
  114.    const GLuint oldNum = paramList->NumParameters;
  115.    const GLuint sz4 = (size + 3) / 4; /* no. of new param slots needed */
  116.  
  117.    assert(size > 0);
  118.  
  119.    if (oldNum + sz4 > paramList->Size) {
  120.       /* Need to grow the parameter list array (alloc some extra) */
  121.       paramList->Size = paramList->Size + 4 * sz4;
  122.  
  123.       /* realloc arrays */
  124.       paramList->Parameters = (struct gl_program_parameter *)
  125.          _mesa_realloc(paramList->Parameters,
  126.                        oldNum * sizeof(struct gl_program_parameter),
  127.                        paramList->Size * sizeof(struct gl_program_parameter));
  128.  
  129.       paramList->ParameterValues = (gl_constant_value (*)[4])
  130.          _mesa_align_realloc(paramList->ParameterValues,         /* old buf */
  131.                              oldNum * 4 * sizeof(gl_constant_value),/* old sz */
  132.                              paramList->Size*4*sizeof(gl_constant_value),/*new*/
  133.                              16);
  134.    }
  135.  
  136.    if (!paramList->Parameters ||
  137.        !paramList->ParameterValues) {
  138.       /* out of memory */
  139.       paramList->NumParameters = 0;
  140.       paramList->Size = 0;
  141.       return -1;
  142.    }
  143.    else {
  144.       GLuint i, j;
  145.  
  146.       paramList->NumParameters = oldNum + sz4;
  147.  
  148.       memset(&paramList->Parameters[oldNum], 0,
  149.              sz4 * sizeof(struct gl_program_parameter));
  150.  
  151.       for (i = 0; i < sz4; i++) {
  152.          struct gl_program_parameter *p = paramList->Parameters + oldNum + i;
  153.          p->Name = name ? _mesa_strdup(name) : NULL;
  154.          p->Type = type;
  155.          p->Size = size;
  156.          p->DataType = datatype;
  157.          if (values) {
  158.             if (size >= 4) {
  159.                COPY_4V(paramList->ParameterValues[oldNum + i], values);
  160.             }
  161.             else {
  162.                /* copy 1, 2 or 3 values */
  163.                GLuint remaining = size % 4;
  164.                assert(remaining < 4);
  165.                for (j = 0; j < remaining; j++) {
  166.                   paramList->ParameterValues[oldNum + i][j].f = values[j].f;
  167.                }
  168.                /* fill in remaining positions with zeros */
  169.                for (; j < 4; j++) {
  170.                   paramList->ParameterValues[oldNum + i][j].f = 0.0f;
  171.                }
  172.             }
  173.             values += 4;
  174.             p->Initialized = GL_TRUE;
  175.          }
  176.          else {
  177.             /* silence valgrind */
  178.             for (j = 0; j < 4; j++)
  179.                 paramList->ParameterValues[oldNum + i][j].f = 0;
  180.          }
  181.          size -= 4;
  182.       }
  183.  
  184.       if (state) {
  185.          for (i = 0; i < STATE_LENGTH; i++)
  186.             paramList->Parameters[oldNum].StateIndexes[i] = state[i];
  187.       }
  188.  
  189.       return (GLint) oldNum;
  190.    }
  191. }
  192.  
  193.  
  194. /**
  195.  * Add a new named constant to the parameter list.
  196.  * This will be used when the program contains something like this:
  197.  *    PARAM myVals = { 0, 1, 2, 3 };
  198.  *
  199.  * \param paramList  the parameter list
  200.  * \param name  the name for the constant
  201.  * \param values  four float values
  202.  * \return index/position of the new parameter in the parameter list
  203.  */
  204. GLint
  205. _mesa_add_named_constant(struct gl_program_parameter_list *paramList,
  206.                          const char *name, const gl_constant_value values[4],
  207.                          GLuint size)
  208. {
  209.    /* first check if this is a duplicate constant */
  210.    GLint pos;
  211.    for (pos = 0; pos < (GLint)paramList->NumParameters; pos++) {
  212.       const gl_constant_value *pvals = paramList->ParameterValues[pos];
  213.       if (pvals[0].u == values[0].u &&
  214.           pvals[1].u == values[1].u &&
  215.           pvals[2].u == values[2].u &&
  216.           pvals[3].u == values[3].u &&
  217.           strcmp(paramList->Parameters[pos].Name, name) == 0) {
  218.          /* Same name and value is already in the param list - reuse it */
  219.          return pos;
  220.       }
  221.    }
  222.    /* not found, add new parameter */
  223.    return _mesa_add_parameter(paramList, PROGRAM_CONSTANT, name,
  224.                               size, GL_NONE, values, NULL);
  225. }
  226.  
  227.  
  228. /**
  229.  * Add a new unnamed constant to the parameter list.  This will be used
  230.  * when a fragment/vertex program contains something like this:
  231.  *    MOV r, { 0, 1, 2, 3 };
  232.  * If swizzleOut is non-null we'll search the parameter list for an
  233.  * existing instance of the constant which matches with a swizzle.
  234.  *
  235.  * \param paramList  the parameter list
  236.  * \param values  four float values
  237.  * \param swizzleOut  returns swizzle mask for accessing the constant
  238.  * \return index/position of the new parameter in the parameter list.
  239.  */
  240. GLint
  241. _mesa_add_typed_unnamed_constant(struct gl_program_parameter_list *paramList,
  242.                            const gl_constant_value values[4], GLuint size,
  243.                            GLenum datatype, GLuint *swizzleOut)
  244. {
  245.    GLint pos;
  246.    ASSERT(size >= 1);
  247.    ASSERT(size <= 4);
  248.  
  249.    if (swizzleOut &&
  250.        _mesa_lookup_parameter_constant(paramList, values,
  251.                                        size, &pos, swizzleOut)) {
  252.       return pos;
  253.    }
  254.  
  255.    /* Look for empty space in an already unnamed constant parameter
  256.     * to add this constant.  This will only work for single-element
  257.     * constants because we rely on smearing (i.e. .yyyy or .zzzz).
  258.     */
  259.    if (size == 1 && swizzleOut) {
  260.       for (pos = 0; pos < (GLint) paramList->NumParameters; pos++) {
  261.          struct gl_program_parameter *p = paramList->Parameters + pos;
  262.          if (p->Type == PROGRAM_CONSTANT && p->Size + size <= 4) {
  263.             /* ok, found room */
  264.             gl_constant_value *pVal = paramList->ParameterValues[pos];
  265.             GLuint swz = p->Size; /* 1, 2 or 3 for Y, Z, W */
  266.             pVal[p->Size] = values[0];
  267.             p->Size++;
  268.             *swizzleOut = MAKE_SWIZZLE4(swz, swz, swz, swz);
  269.             return pos;
  270.          }
  271.       }
  272.    }
  273.  
  274.    /* add a new parameter to store this constant */
  275.    pos = _mesa_add_parameter(paramList, PROGRAM_CONSTANT, NULL,
  276.                              size, datatype, values, NULL);
  277.    if (pos >= 0 && swizzleOut) {
  278.       if (size == 1)
  279.          *swizzleOut = SWIZZLE_XXXX;
  280.       else
  281.          *swizzleOut = SWIZZLE_NOOP;
  282.    }
  283.    return pos;
  284. }
  285.  
  286. /**
  287.  * Add a new unnamed constant to the parameter list.  This will be used
  288.  * when a fragment/vertex program contains something like this:
  289.  *    MOV r, { 0, 1, 2, 3 };
  290.  * If swizzleOut is non-null we'll search the parameter list for an
  291.  * existing instance of the constant which matches with a swizzle.
  292.  *
  293.  * \param paramList  the parameter list
  294.  * \param values  four float values
  295.  * \param swizzleOut  returns swizzle mask for accessing the constant
  296.  * \return index/position of the new parameter in the parameter list.
  297.  * \sa _mesa_add_typed_unnamed_constant
  298.  */
  299. GLint
  300. _mesa_add_unnamed_constant(struct gl_program_parameter_list *paramList,
  301.                            const gl_constant_value values[4], GLuint size,
  302.                            GLuint *swizzleOut)
  303. {
  304.    return _mesa_add_typed_unnamed_constant(paramList, values, size, GL_NONE,
  305.                                            swizzleOut);
  306. }
  307.  
  308. #if 0 /* not used yet */
  309. /**
  310.  * Returns the number of 4-component registers needed to store a piece
  311.  * of GL state.  For matrices this may be as many as 4 registers,
  312.  * everything else needs
  313.  * just 1 register.
  314.  */
  315. static GLuint
  316. sizeof_state_reference(const GLint *stateTokens)
  317. {
  318.    if (stateTokens[0] == STATE_MATRIX) {
  319.       GLuint rows = stateTokens[4] - stateTokens[3] + 1;
  320.       assert(rows >= 1);
  321.       assert(rows <= 4);
  322.       return rows;
  323.    }
  324.    else {
  325.       return 1;
  326.    }
  327. }
  328. #endif
  329.  
  330.  
  331. /**
  332.  * Add a new state reference to the parameter list.
  333.  * This will be used when the program contains something like this:
  334.  *    PARAM ambient = state.material.front.ambient;
  335.  *
  336.  * \param paramList  the parameter list
  337.  * \param stateTokens  an array of 5 (STATE_LENGTH) state tokens
  338.  * \return index of the new parameter.
  339.  */
  340. GLint
  341. _mesa_add_state_reference(struct gl_program_parameter_list *paramList,
  342.                           const gl_state_index stateTokens[STATE_LENGTH])
  343. {
  344.    const GLuint size = 4; /* XXX fix */
  345.    char *name;
  346.    GLint index;
  347.  
  348.    /* Check if the state reference is already in the list */
  349.    for (index = 0; index < (GLint) paramList->NumParameters; index++) {
  350.       if (!memcmp(paramList->Parameters[index].StateIndexes,
  351.                   stateTokens, STATE_LENGTH * sizeof(gl_state_index))) {
  352.          return index;
  353.       }
  354.    }
  355.  
  356.    name = _mesa_program_state_string(stateTokens);
  357.    index = _mesa_add_parameter(paramList, PROGRAM_STATE_VAR, name,
  358.                                size, GL_NONE,
  359.                                NULL, (gl_state_index *) stateTokens);
  360.    paramList->StateFlags |= _mesa_program_state_flags(stateTokens);
  361.  
  362.    /* free name string here since we duplicated it in add_parameter() */
  363.    free(name);
  364.  
  365.    return index;
  366. }
  367.  
  368.  
  369. /**
  370.  * Lookup a parameter value by name in the given parameter list.
  371.  * \return pointer to the float[4] values.
  372.  */
  373. gl_constant_value *
  374. _mesa_lookup_parameter_value(const struct gl_program_parameter_list *paramList,
  375.                              GLsizei nameLen, const char *name)
  376. {
  377.    GLint i = _mesa_lookup_parameter_index(paramList, nameLen, name);
  378.    if (i < 0)
  379.       return NULL;
  380.    else
  381.       return paramList->ParameterValues[i];
  382. }
  383.  
  384.  
  385. /**
  386.  * Given a program parameter name, find its position in the list of parameters.
  387.  * \param paramList  the parameter list to search
  388.  * \param nameLen  length of name (in chars).
  389.  *                 If length is negative, assume that name is null-terminated.
  390.  * \param name  the name to search for
  391.  * \return index of parameter in the list.
  392.  */
  393. GLint
  394. _mesa_lookup_parameter_index(const struct gl_program_parameter_list *paramList,
  395.                              GLsizei nameLen, const char *name)
  396. {
  397.    GLint i;
  398.  
  399.    if (!paramList)
  400.       return -1;
  401.  
  402.    if (nameLen == -1) {
  403.       /* name is null-terminated */
  404.       for (i = 0; i < (GLint) paramList->NumParameters; i++) {
  405.          if (paramList->Parameters[i].Name &&
  406.              strcmp(paramList->Parameters[i].Name, name) == 0)
  407.             return i;
  408.       }
  409.    }
  410.    else {
  411.       /* name is not null-terminated, use nameLen */
  412.       for (i = 0; i < (GLint) paramList->NumParameters; i++) {
  413.          if (paramList->Parameters[i].Name &&
  414.              strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
  415.              && strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
  416.             return i;
  417.       }
  418.    }
  419.    return -1;
  420. }
  421.  
  422.  
  423. /**
  424.  * Look for a float vector in the given parameter list.  The float vector
  425.  * may be of length 1, 2, 3 or 4.  If swizzleOut is non-null, we'll try
  426.  * swizzling to find a match.
  427.  * \param list  the parameter list to search
  428.  * \param v  the float vector to search for
  429.  * \param vSize  number of element in v
  430.  * \param posOut  returns the position of the constant, if found
  431.  * \param swizzleOut  returns a swizzle mask describing location of the
  432.  *                    vector elements if found.
  433.  * \return GL_TRUE if found, GL_FALSE if not found
  434.  */
  435. GLboolean
  436. _mesa_lookup_parameter_constant(const struct gl_program_parameter_list *list,
  437.                                 const gl_constant_value v[], GLuint vSize,
  438.                                 GLint *posOut, GLuint *swizzleOut)
  439. {
  440.    GLuint i;
  441.  
  442.    assert(vSize >= 1);
  443.    assert(vSize <= 4);
  444.  
  445.    if (!list) {
  446.       *posOut = -1;
  447.       return GL_FALSE;
  448.    }
  449.  
  450.    for (i = 0; i < list->NumParameters; i++) {
  451.       if (list->Parameters[i].Type == PROGRAM_CONSTANT) {
  452.          if (!swizzleOut) {
  453.             /* swizzle not allowed */
  454.             GLuint j, match = 0;
  455.             for (j = 0; j < vSize; j++) {
  456.                if (v[j].u == list->ParameterValues[i][j].u)
  457.                   match++;
  458.             }
  459.             if (match == vSize) {
  460.                *posOut = i;
  461.                return GL_TRUE;
  462.             }
  463.          }
  464.          else {
  465.             /* try matching w/ swizzle */
  466.              if (vSize == 1) {
  467.                 /* look for v[0] anywhere within float[4] value */
  468.                 GLuint j;
  469.                 for (j = 0; j < list->Parameters[i].Size; j++) {
  470.                    if (list->ParameterValues[i][j].u == v[0].u) {
  471.                       /* found it */
  472.                       *posOut = i;
  473.                       *swizzleOut = MAKE_SWIZZLE4(j, j, j, j);
  474.                       return GL_TRUE;
  475.                    }
  476.                 }
  477.              }
  478.              else if (vSize <= list->Parameters[i].Size) {
  479.                 /* see if we can match this constant (with a swizzle) */
  480.                 GLuint swz[4];
  481.                 GLuint match = 0, j, k;
  482.                 for (j = 0; j < vSize; j++) {
  483.                    if (v[j].u == list->ParameterValues[i][j].u) {
  484.                       swz[j] = j;
  485.                       match++;
  486.                    }
  487.                    else {
  488.                       for (k = 0; k < list->Parameters[i].Size; k++) {
  489.                          if (v[j].u == list->ParameterValues[i][k].u) {
  490.                             swz[j] = k;
  491.                             match++;
  492.                             break;
  493.                          }
  494.                       }
  495.                    }
  496.                 }
  497.                 /* smear last value to remaining positions */
  498.                 for (; j < 4; j++)
  499.                    swz[j] = swz[j-1];
  500.  
  501.                 if (match == vSize) {
  502.                    *posOut = i;
  503.                    *swizzleOut = MAKE_SWIZZLE4(swz[0], swz[1], swz[2], swz[3]);
  504.                    return GL_TRUE;
  505.                 }
  506.              }
  507.          }
  508.       }
  509.    }
  510.  
  511.    *posOut = -1;
  512.    return GL_FALSE;
  513. }
  514.  
  515.  
  516. struct gl_program_parameter_list *
  517. _mesa_clone_parameter_list(const struct gl_program_parameter_list *list)
  518. {
  519.    struct gl_program_parameter_list *clone;
  520.    GLuint i;
  521.  
  522.    clone = _mesa_new_parameter_list();
  523.    if (!clone)
  524.       return NULL;
  525.  
  526.    /** Not too efficient, but correct */
  527.    for (i = 0; i < list->NumParameters; i++) {
  528.       struct gl_program_parameter *p = list->Parameters + i;
  529.       struct gl_program_parameter *pCopy;
  530.       GLuint size = MIN2(p->Size, 4);
  531.       GLint j = _mesa_add_parameter(clone, p->Type, p->Name, size, p->DataType,
  532.                                     list->ParameterValues[i], NULL);
  533.       ASSERT(j >= 0);
  534.       pCopy = clone->Parameters + j;
  535.       /* copy state indexes */
  536.       if (p->Type == PROGRAM_STATE_VAR) {
  537.          GLint k;
  538.          for (k = 0; k < STATE_LENGTH; k++) {
  539.             pCopy->StateIndexes[k] = p->StateIndexes[k];
  540.          }
  541.       }
  542.       else {
  543.          clone->Parameters[j].Size = p->Size;
  544.       }
  545.      
  546.    }
  547.  
  548.    clone->StateFlags = list->StateFlags;
  549.  
  550.    return clone;
  551. }
  552.  
  553.  
  554. /**
  555.  * Return a new parameter list which is listA + listB.
  556.  */
  557. struct gl_program_parameter_list *
  558. _mesa_combine_parameter_lists(const struct gl_program_parameter_list *listA,
  559.                               const struct gl_program_parameter_list *listB)
  560. {
  561.    struct gl_program_parameter_list *list;
  562.  
  563.    if (listA) {
  564.       list = _mesa_clone_parameter_list(listA);
  565.       if (list && listB) {
  566.          GLuint i;
  567.          for (i = 0; i < listB->NumParameters; i++) {
  568.             struct gl_program_parameter *param = listB->Parameters + i;
  569.             _mesa_add_parameter(list, param->Type, param->Name, param->Size,
  570.                                 param->DataType,
  571.                                 listB->ParameterValues[i],
  572.                                 param->StateIndexes);
  573.          }
  574.       }
  575.    }
  576.    else if (listB) {
  577.       list = _mesa_clone_parameter_list(listB);
  578.    }
  579.    else {
  580.       list = NULL;
  581.    }
  582.    return list;
  583. }
  584.