2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \file prog_parameter.c
27 * Program parameter lists and functions.
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"
40 struct gl_program_parameter_list
*
41 _mesa_new_parameter_list(void)
43 return CALLOC_STRUCT(gl_program_parameter_list
);
47 struct gl_program_parameter_list
*
48 _mesa_new_parameter_list_sized(unsigned size
)
50 struct gl_program_parameter_list
*p
= _mesa_new_parameter_list();
52 if ((p
!= NULL
) && (size
!= 0)) {
56 p
->Parameters
= (struct gl_program_parameter
*)
57 _mesa_calloc(size
* sizeof(struct gl_program_parameter
));
59 p
->ParameterValues
= (GLfloat (*)[4])
60 _mesa_align_malloc(size
* 4 *sizeof(GLfloat
), 16);
63 if ((p
->Parameters
== NULL
) || (p
->ParameterValues
== NULL
)) {
64 _mesa_free(p
->Parameters
);
65 _mesa_align_free(p
->ParameterValues
);
76 * Free a parameter list and all its parameters
79 _mesa_free_parameter_list(struct gl_program_parameter_list
*paramList
)
82 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
83 if (paramList
->Parameters
[i
].Name
)
84 _mesa_free((void *) paramList
->Parameters
[i
].Name
);
86 _mesa_free(paramList
->Parameters
);
87 if (paramList
->ParameterValues
)
88 _mesa_align_free(paramList
->ParameterValues
);
89 _mesa_free(paramList
);
94 * Add a new parameter to a parameter list.
95 * Note that parameter values are usually 4-element GLfloat vectors.
96 * When size > 4 we'll allocate a sequential block of parameters to
97 * store all the values (in blocks of 4).
99 * \param paramList the list to add the parameter to
100 * \param type type of parameter, such as
101 * \param name the parameter name, will be duplicated/copied!
102 * \param size number of elements in 'values' vector (1..4, or more)
103 * \param values initial parameter value, up to 4 GLfloats, 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)
108 _mesa_add_parameter(struct gl_program_parameter_list
*paramList
,
109 gl_register_file type
, const char *name
,
110 GLuint size
, GLenum datatype
, const GLfloat
*values
,
111 const gl_state_index state
[STATE_LENGTH
],
114 const GLuint oldNum
= paramList
->NumParameters
;
115 const GLuint sz4
= (size
+ 3) / 4; /* no. of new param slots needed */
119 if (oldNum
+ sz4
> paramList
->Size
) {
120 /* Need to grow the parameter list array (alloc some extra) */
121 paramList
->Size
= paramList
->Size
+ 4 * sz4
;
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
));
129 paramList
->ParameterValues
= (GLfloat (*)[4])
130 _mesa_align_realloc(paramList
->ParameterValues
, /* old buf */
131 oldNum
* 4 * sizeof(GLfloat
), /* old size */
132 paramList
->Size
* 4 *sizeof(GLfloat
), /* new sz */
136 if (!paramList
->Parameters
||
137 !paramList
->ParameterValues
) {
139 paramList
->NumParameters
= 0;
146 paramList
->NumParameters
= oldNum
+ sz4
;
148 _mesa_memset(¶mList
->Parameters
[oldNum
], 0,
149 sz4
* sizeof(struct gl_program_parameter
));
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
;
156 p
->DataType
= datatype
;
159 COPY_4V(paramList
->ParameterValues
[oldNum
+ i
], values
);
161 p
->Initialized
= GL_TRUE
;
164 /* silence valgrind */
165 ASSIGN_4V(paramList
->ParameterValues
[oldNum
+ i
], 0, 0, 0, 0);
171 for (i
= 0; i
< STATE_LENGTH
; i
++)
172 paramList
->Parameters
[oldNum
].StateIndexes
[i
] = state
[i
];
175 return (GLint
) oldNum
;
181 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
182 * \return index of the new entry in the parameter list
185 _mesa_add_named_parameter(struct gl_program_parameter_list
*paramList
,
186 const char *name
, const GLfloat values
[4])
188 return _mesa_add_parameter(paramList
, PROGRAM_NAMED_PARAM
, name
,
189 4, GL_NONE
, values
, NULL
, 0x0);
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 };
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
205 _mesa_add_named_constant(struct gl_program_parameter_list
*paramList
,
206 const char *name
, const GLfloat values
[4],
209 /* first check if this is a duplicate constant */
211 for (pos
= 0; pos
< paramList
->NumParameters
; pos
++) {
212 const GLfloat
*pvals
= paramList
->ParameterValues
[pos
];
213 if (pvals
[0] == values
[0] &&
214 pvals
[1] == values
[1] &&
215 pvals
[2] == values
[2] &&
216 pvals
[3] == values
[3] &&
217 _mesa_strcmp(paramList
->Parameters
[pos
].Name
, name
) == 0) {
218 /* Same name and value is already in the param list - reuse it */
222 /* not found, add new parameter */
223 return _mesa_add_parameter(paramList
, PROGRAM_CONSTANT
, name
,
224 size
, GL_NONE
, values
, NULL
, 0x0);
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 * We'll search the parameter list for an existing instance of the
233 * constant. If swizzleOut is non-null, we'll try swizzling when
234 * looking for a match.
236 * \param paramList the parameter list
237 * \param values four float values
238 * \param swizzleOut returns swizzle mask for accessing the constant
239 * \return index/position of the new parameter in the parameter list.
242 _mesa_add_unnamed_constant(struct gl_program_parameter_list
*paramList
,
243 const GLfloat values
[4], GLuint size
,
250 if (_mesa_lookup_parameter_constant(paramList
, values
,
251 size
, &pos
, swizzleOut
)) {
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).
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) {
264 GLfloat
*pVal
= paramList
->ParameterValues
[pos
];
265 GLuint swz
= p
->Size
; /* 1, 2 or 3 for Y, Z, W */
266 pVal
[p
->Size
] = values
[0];
268 *swizzleOut
= MAKE_SWIZZLE4(swz
, swz
, swz
, swz
);
274 /* add a new parameter to store this constant */
275 pos
= _mesa_add_parameter(paramList
, PROGRAM_CONSTANT
, NULL
,
276 size
, GL_NONE
, values
, NULL
, 0x0);
277 if (pos
>= 0 && swizzleOut
) {
279 *swizzleOut
= SWIZZLE_XXXX
;
281 *swizzleOut
= SWIZZLE_NOOP
;
288 * Add a uniform to the parameter list.
289 * Note that if the uniform is an array, size may be greater than
290 * what's implied by the datatype.
291 * \param name uniform's name
292 * \param size number of floats to allocate
293 * \param datatype GL_FLOAT_VEC3, GL_FLOAT_MAT4, etc.
296 _mesa_add_uniform(struct gl_program_parameter_list
*paramList
,
297 const char *name
, GLuint size
, GLenum datatype
,
298 const GLfloat
*values
)
300 GLint i
= _mesa_lookup_parameter_index(paramList
, -1, name
);
301 ASSERT(datatype
!= GL_NONE
);
302 if (i
>= 0 && paramList
->Parameters
[i
].Type
== PROGRAM_UNIFORM
) {
303 ASSERT(paramList
->Parameters
[i
].Size
== size
);
304 ASSERT(paramList
->Parameters
[i
].DataType
== datatype
);
305 /* already in list */
309 i
= _mesa_add_parameter(paramList
, PROGRAM_UNIFORM
, name
,
310 size
, datatype
, values
, NULL
, 0x0);
317 * Mark the named uniform as 'used'.
320 _mesa_use_uniform(struct gl_program_parameter_list
*paramList
,
324 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
325 struct gl_program_parameter
*p
= paramList
->Parameters
+ i
;
326 if ((p
->Type
== PROGRAM_UNIFORM
|| p
->Type
== PROGRAM_SAMPLER
) &&
327 _mesa_strcmp(p
->Name
, name
) == 0) {
329 /* Note that large uniforms may occupy several slots so we're
330 * not done searching yet.
338 * Add a sampler to the parameter list.
339 * \param name uniform's name
340 * \param datatype GL_SAMPLER_2D, GL_SAMPLER_2D_RECT_ARB, etc.
341 * \param index the sampler number (as seen in TEX instructions)
342 * \return sampler index (starting at zero) or -1 if error
345 _mesa_add_sampler(struct gl_program_parameter_list
*paramList
,
346 const char *name
, GLenum datatype
)
348 GLint i
= _mesa_lookup_parameter_index(paramList
, -1, name
);
349 if (i
>= 0 && paramList
->Parameters
[i
].Type
== PROGRAM_SAMPLER
) {
350 ASSERT(paramList
->Parameters
[i
].Size
== 1);
351 ASSERT(paramList
->Parameters
[i
].DataType
== datatype
);
352 /* already in list */
353 return (GLint
) paramList
->ParameterValues
[i
][0];
357 const GLint size
= 1; /* a sampler is basically a texture unit number */
359 GLint numSamplers
= 0;
360 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
361 if (paramList
->Parameters
[i
].Type
== PROGRAM_SAMPLER
)
364 value
[0] = (GLfloat
) numSamplers
;
365 value
[1] = value
[2] = value
[3] = 0.0F
;
366 (void) _mesa_add_parameter(paramList
, PROGRAM_SAMPLER
, name
,
367 size
, datatype
, value
, NULL
, 0x0);
374 * Add parameter representing a varying variable.
377 _mesa_add_varying(struct gl_program_parameter_list
*paramList
,
378 const char *name
, GLuint size
, GLbitfield flags
)
380 GLint i
= _mesa_lookup_parameter_index(paramList
, -1, name
);
381 if (i
>= 0 && paramList
->Parameters
[i
].Type
== PROGRAM_VARYING
) {
382 /* already in list */
386 /*assert(size == 4);*/
387 i
= _mesa_add_parameter(paramList
, PROGRAM_VARYING
, name
,
388 size
, GL_NONE
, NULL
, NULL
, flags
);
395 * Add parameter representing a vertex program attribute.
396 * \param size size of attribute (in floats), may be -1 if unknown
397 * \param attrib the attribute index, or -1 if unknown
400 _mesa_add_attribute(struct gl_program_parameter_list
*paramList
,
401 const char *name
, GLint size
, GLenum datatype
, GLint attrib
)
403 GLint i
= _mesa_lookup_parameter_index(paramList
, -1, name
);
408 paramList
->Parameters
[i
].StateIndexes
[0] = attrib
;
412 gl_state_index state
[STATE_LENGTH
];
413 state
[0] = (gl_state_index
) attrib
;
416 i
= _mesa_add_parameter(paramList
, PROGRAM_INPUT
, name
,
417 size
, datatype
, NULL
, state
, 0x0);
424 #if 0 /* not used yet */
426 * Returns the number of 4-component registers needed to store a piece
427 * of GL state. For matrices this may be as many as 4 registers,
428 * everything else needs
432 sizeof_state_reference(const GLint
*stateTokens
)
434 if (stateTokens
[0] == STATE_MATRIX
) {
435 GLuint rows
= stateTokens
[4] - stateTokens
[3] + 1;
448 * Add a new state reference to the parameter list.
449 * This will be used when the program contains something like this:
450 * PARAM ambient = state.material.front.ambient;
452 * \param paramList the parameter list
453 * \param stateTokens an array of 5 (STATE_LENGTH) state tokens
454 * \return index of the new parameter.
457 _mesa_add_state_reference(struct gl_program_parameter_list
*paramList
,
458 const gl_state_index stateTokens
[STATE_LENGTH
])
460 const GLuint size
= 4; /* XXX fix */
464 /* Check if the state reference is already in the list */
465 for (index
= 0; index
< (GLint
) paramList
->NumParameters
; index
++) {
467 for (i
= 0; i
< STATE_LENGTH
; i
++) {
468 if (paramList
->Parameters
[index
].StateIndexes
[i
] == stateTokens
[i
]) {
475 if (match
== STATE_LENGTH
) {
476 /* this state reference is already in the parameter list */
481 name
= _mesa_program_state_string(stateTokens
);
482 index
= _mesa_add_parameter(paramList
, PROGRAM_STATE_VAR
, name
,
484 NULL
, (gl_state_index
*) stateTokens
, 0x0);
485 paramList
->StateFlags
|= _mesa_program_state_flags(stateTokens
);
487 /* free name string here since we duplicated it in add_parameter() */
495 * Lookup a parameter value by name in the given parameter list.
496 * \return pointer to the float[4] values.
499 _mesa_lookup_parameter_value(const struct gl_program_parameter_list
*paramList
,
500 GLsizei nameLen
, const char *name
)
502 GLuint i
= _mesa_lookup_parameter_index(paramList
, nameLen
, name
);
506 return paramList
->ParameterValues
[i
];
511 * Given a program parameter name, find its position in the list of parameters.
512 * \param paramList the parameter list to search
513 * \param nameLen length of name (in chars).
514 * If length is negative, assume that name is null-terminated.
515 * \param name the name to search for
516 * \return index of parameter in the list.
519 _mesa_lookup_parameter_index(const struct gl_program_parameter_list
*paramList
,
520 GLsizei nameLen
, const char *name
)
528 /* name is null-terminated */
529 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
530 if (paramList
->Parameters
[i
].Name
&&
531 _mesa_strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
536 /* name is not null-terminated, use nameLen */
537 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
538 if (paramList
->Parameters
[i
].Name
&&
539 _mesa_strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
540 && _mesa_strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
549 * Look for a float vector in the given parameter list. The float vector
550 * may be of length 1, 2, 3 or 4. If swizzleOut is non-null, we'll try
551 * swizzling to find a match.
552 * \param list the parameter list to search
553 * \param v the float vector to search for
554 * \param vSize number of element in v
555 * \param posOut returns the position of the constant, if found
556 * \param swizzleOut returns a swizzle mask describing location of the
557 * vector elements if found.
558 * \return GL_TRUE if found, GL_FALSE if not found
561 _mesa_lookup_parameter_constant(const struct gl_program_parameter_list
*list
,
562 const GLfloat v
[], GLuint vSize
,
563 GLint
*posOut
, GLuint
*swizzleOut
)
573 for (i
= 0; i
< list
->NumParameters
; i
++) {
574 if (list
->Parameters
[i
].Type
== PROGRAM_CONSTANT
) {
576 /* swizzle not allowed */
578 for (j
= 0; j
< vSize
; j
++) {
579 if (v
[j
] == list
->ParameterValues
[i
][j
])
582 if (match
== vSize
) {
588 /* try matching w/ swizzle */
590 /* look for v[0] anywhere within float[4] value */
592 for (j
= 0; j
< 4; j
++) {
593 if (list
->ParameterValues
[i
][j
] == v
[0]) {
596 *swizzleOut
= MAKE_SWIZZLE4(j
, j
, j
, j
);
601 else if (vSize
<= list
->Parameters
[i
].Size
) {
602 /* see if we can match this constant (with a swizzle) */
604 GLuint match
= 0, j
, k
;
605 for (j
= 0; j
< vSize
; j
++) {
606 if (v
[j
] == list
->ParameterValues
[i
][j
]) {
611 for (k
= 0; k
< list
->Parameters
[i
].Size
; k
++) {
612 if (v
[j
] == list
->ParameterValues
[i
][k
]) {
620 /* smear last value to remaining positions */
624 if (match
== vSize
) {
626 *swizzleOut
= MAKE_SWIZZLE4(swz
[0], swz
[1], swz
[2], swz
[3]);
639 struct gl_program_parameter_list
*
640 _mesa_clone_parameter_list(const struct gl_program_parameter_list
*list
)
642 struct gl_program_parameter_list
*clone
;
645 clone
= _mesa_new_parameter_list();
649 /** Not too efficient, but correct */
650 for (i
= 0; i
< list
->NumParameters
; i
++) {
651 struct gl_program_parameter
*p
= list
->Parameters
+ i
;
652 struct gl_program_parameter
*pCopy
;
653 GLuint size
= MIN2(p
->Size
, 4);
654 GLint j
= _mesa_add_parameter(clone
, p
->Type
, p
->Name
, size
, p
->DataType
,
655 list
->ParameterValues
[i
], NULL
, 0x0);
657 pCopy
= clone
->Parameters
+ j
;
658 pCopy
->Used
= p
->Used
;
659 pCopy
->Flags
= p
->Flags
;
660 /* copy state indexes */
661 if (p
->Type
== PROGRAM_STATE_VAR
) {
663 for (k
= 0; k
< STATE_LENGTH
; k
++) {
664 pCopy
->StateIndexes
[k
] = p
->StateIndexes
[k
];
668 clone
->Parameters
[j
].Size
= p
->Size
;
673 clone
->StateFlags
= list
->StateFlags
;
680 * Return a new parameter list which is listA + listB.
682 struct gl_program_parameter_list
*
683 _mesa_combine_parameter_lists(const struct gl_program_parameter_list
*listA
,
684 const struct gl_program_parameter_list
*listB
)
686 struct gl_program_parameter_list
*list
;
689 list
= _mesa_clone_parameter_list(listA
);
692 for (i
= 0; i
< listB
->NumParameters
; i
++) {
693 struct gl_program_parameter
*param
= listB
->Parameters
+ i
;
694 _mesa_add_parameter(list
, param
->Type
, param
->Name
, param
->Size
,
696 listB
->ParameterValues
[i
],
703 list
= _mesa_clone_parameter_list(listB
);
714 * Find longest name of all uniform parameters in list.
717 _mesa_longest_parameter_name(const struct gl_program_parameter_list
*list
,
718 gl_register_file type
)
720 GLuint i
, maxLen
= 0;
723 for (i
= 0; i
< list
->NumParameters
; i
++) {
724 if (list
->Parameters
[i
].Type
== type
) {
725 GLuint len
= _mesa_strlen(list
->Parameters
[i
].Name
);
735 * Count the number of parameters in the last that match the given type.
738 _mesa_num_parameters_of_type(const struct gl_program_parameter_list
*list
,
739 gl_register_file type
)
743 for (i
= 0; i
< list
->NumParameters
; i
++) {
744 if (list
->Parameters
[i
].Type
== type
)