2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
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 calloc(1, size
* sizeof(struct gl_program_parameter
));
59 p
->ParameterValues
= (gl_constant_value (*)[4])
60 _mesa_align_malloc(size
* 4 *sizeof(gl_constant_value
), 16);
63 if ((p
->Parameters
== NULL
) || (p
->ParameterValues
== NULL
)) {
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 free((void *)paramList
->Parameters
[i
].Name
);
85 free(paramList
->Parameters
);
86 if (paramList
->ParameterValues
)
87 _mesa_align_free(paramList
->ParameterValues
);
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).
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)
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
])
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
= (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*/
136 if (!paramList
->Parameters
||
137 !paramList
->ParameterValues
) {
139 paramList
->NumParameters
= 0;
146 paramList
->NumParameters
= oldNum
+ sz4
;
148 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
);
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
;
168 /* fill in remaining positions with zeros */
170 paramList
->ParameterValues
[oldNum
+ i
][j
].f
= 0.0f
;
174 p
->Initialized
= GL_TRUE
;
177 /* silence valgrind */
178 for (j
= 0; j
< 4; j
++)
179 paramList
->ParameterValues
[oldNum
+ i
][j
].f
= 0;
185 for (i
= 0; i
< STATE_LENGTH
; i
++)
186 paramList
->Parameters
[oldNum
].StateIndexes
[i
] = state
[i
];
189 return (GLint
) oldNum
;
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 gl_constant_value values
[4],
209 /* first check if this is a duplicate constant */
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 */
222 /* not found, add new parameter */
223 return _mesa_add_parameter(paramList
, PROGRAM_CONSTANT
, name
,
224 size
, GL_NONE
, values
, NULL
);
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.
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.
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
)
250 _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 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];
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
, datatype
, values
, NULL
);
277 if (pos
>= 0 && swizzleOut
) {
279 *swizzleOut
= SWIZZLE_XXXX
;
281 *swizzleOut
= SWIZZLE_NOOP
;
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.
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
300 _mesa_add_unnamed_constant(struct gl_program_parameter_list
*paramList
,
301 const gl_constant_value values
[4], GLuint size
,
304 return _mesa_add_typed_unnamed_constant(paramList
, values
, size
, GL_NONE
,
308 #if 0 /* not used yet */
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
316 sizeof_state_reference(const GLint
*stateTokens
)
318 if (stateTokens
[0] == STATE_MATRIX
) {
319 GLuint rows
= stateTokens
[4] - stateTokens
[3] + 1;
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;
336 * \param paramList the parameter list
337 * \param stateTokens an array of 5 (STATE_LENGTH) state tokens
338 * \return index of the new parameter.
341 _mesa_add_state_reference(struct gl_program_parameter_list
*paramList
,
342 const gl_state_index stateTokens
[STATE_LENGTH
])
344 const GLuint size
= 4; /* XXX fix */
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
))) {
356 name
= _mesa_program_state_string(stateTokens
);
357 index
= _mesa_add_parameter(paramList
, PROGRAM_STATE_VAR
, name
,
359 NULL
, (gl_state_index
*) stateTokens
);
360 paramList
->StateFlags
|= _mesa_program_state_flags(stateTokens
);
362 /* free name string here since we duplicated it in add_parameter() */
370 * Lookup a parameter value by name in the given parameter list.
371 * \return pointer to the float[4] values.
374 _mesa_lookup_parameter_value(const struct gl_program_parameter_list
*paramList
,
375 GLsizei nameLen
, const char *name
)
377 GLint i
= _mesa_lookup_parameter_index(paramList
, nameLen
, name
);
381 return paramList
->ParameterValues
[i
];
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.
394 _mesa_lookup_parameter_index(const struct gl_program_parameter_list
*paramList
,
395 GLsizei nameLen
, const char *name
)
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)
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
)
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
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
)
450 for (i
= 0; i
< list
->NumParameters
; i
++) {
451 if (list
->Parameters
[i
].Type
== PROGRAM_CONSTANT
) {
453 /* swizzle not allowed */
455 for (j
= 0; j
< vSize
; j
++) {
456 if (v
[j
].u
== list
->ParameterValues
[i
][j
].u
)
459 if (match
== vSize
) {
465 /* try matching w/ swizzle */
467 /* look for v[0] anywhere within float[4] value */
469 for (j
= 0; j
< list
->Parameters
[i
].Size
; j
++) {
470 if (list
->ParameterValues
[i
][j
].u
== v
[0].u
) {
473 *swizzleOut
= MAKE_SWIZZLE4(j
, j
, j
, j
);
478 else if (vSize
<= list
->Parameters
[i
].Size
) {
479 /* see if we can match this constant (with a swizzle) */
481 GLuint match
= 0, j
, k
;
482 for (j
= 0; j
< vSize
; j
++) {
483 if (v
[j
].u
== list
->ParameterValues
[i
][j
].u
) {
488 for (k
= 0; k
< list
->Parameters
[i
].Size
; k
++) {
489 if (v
[j
].u
== list
->ParameterValues
[i
][k
].u
) {
497 /* smear last value to remaining positions */
501 if (match
== vSize
) {
503 *swizzleOut
= MAKE_SWIZZLE4(swz
[0], swz
[1], swz
[2], swz
[3]);
516 struct gl_program_parameter_list
*
517 _mesa_clone_parameter_list(const struct gl_program_parameter_list
*list
)
519 struct gl_program_parameter_list
*clone
;
522 clone
= _mesa_new_parameter_list();
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
);
534 pCopy
= clone
->Parameters
+ j
;
535 /* copy state indexes */
536 if (p
->Type
== PROGRAM_STATE_VAR
) {
538 for (k
= 0; k
< STATE_LENGTH
; k
++) {
539 pCopy
->StateIndexes
[k
] = p
->StateIndexes
[k
];
543 clone
->Parameters
[j
].Size
= p
->Size
;
548 clone
->StateFlags
= list
->StateFlags
;
555 * Return a new parameter list which is listA + listB.
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
)
561 struct gl_program_parameter_list
*list
;
564 list
= _mesa_clone_parameter_list(listA
);
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
,
571 listB
->ParameterValues
[i
],
572 param
->StateIndexes
);
577 list
= _mesa_clone_parameter_list(listB
);