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"
41 * Look for a float vector in the given parameter list. The float vector
42 * may be of length 1, 2, 3 or 4. If swizzleOut is non-null, we'll try
43 * swizzling to find a match.
44 * \param list the parameter list to search
45 * \param v the float vector to search for
46 * \param vSize number of element in v
47 * \param posOut returns the position of the constant, if found
48 * \param swizzleOut returns a swizzle mask describing location of the
49 * vector elements if found.
50 * \return GL_TRUE if found, GL_FALSE if not found
53 lookup_parameter_constant(const struct gl_program_parameter_list
*list
,
54 const gl_constant_value v
[], GLuint vSize
,
55 GLint
*posOut
, GLuint
*swizzleOut
)
67 for (i
= 0; i
< list
->NumParameters
; i
++) {
68 if (list
->Parameters
[i
].Type
== PROGRAM_CONSTANT
) {
69 unsigned offset
= list
->ParameterValueOffset
[i
];
72 /* swizzle not allowed */
74 for (j
= 0; j
< vSize
; j
++) {
75 if (v
[j
].u
== list
->ParameterValues
[offset
+ j
].u
)
84 /* try matching w/ swizzle */
86 /* look for v[0] anywhere within float[4] value */
88 for (j
= 0; j
< list
->Parameters
[i
].Size
; j
++) {
89 if (list
->ParameterValues
[offset
+ j
].u
== v
[0].u
) {
92 *swizzleOut
= MAKE_SWIZZLE4(j
, j
, j
, j
);
97 else if (vSize
<= list
->Parameters
[i
].Size
) {
98 /* see if we can match this constant (with a swizzle) */
100 GLuint match
= 0, j
, k
;
101 for (j
= 0; j
< vSize
; j
++) {
102 if (v
[j
].u
== list
->ParameterValues
[offset
+ j
].u
) {
107 for (k
= 0; k
< list
->Parameters
[i
].Size
; k
++) {
108 if (v
[j
].u
== list
->ParameterValues
[offset
+ k
].u
) {
116 /* smear last value to remaining positions */
120 if (match
== vSize
) {
122 *swizzleOut
= MAKE_SWIZZLE4(swz
[0], swz
[1], swz
[2], swz
[3]);
135 struct gl_program_parameter_list
*
136 _mesa_new_parameter_list(void)
138 return CALLOC_STRUCT(gl_program_parameter_list
);
142 struct gl_program_parameter_list
*
143 _mesa_new_parameter_list_sized(unsigned size
)
145 struct gl_program_parameter_list
*p
= _mesa_new_parameter_list();
147 if ((p
!= NULL
) && (size
!= 0)) {
151 p
->Parameters
= (struct gl_program_parameter
*)
152 calloc(size
, sizeof(struct gl_program_parameter
));
154 p
->ParameterValueOffset
= (unsigned *) calloc(size
, sizeof(unsigned));
156 p
->ParameterValues
= (gl_constant_value
*)
157 _mesa_align_malloc(size
* 4 *sizeof(gl_constant_value
), 16);
160 if ((p
->Parameters
== NULL
) || (p
->ParameterValues
== NULL
)) {
162 _mesa_align_free(p
->ParameterValues
);
173 * Free a parameter list and all its parameters
176 _mesa_free_parameter_list(struct gl_program_parameter_list
*paramList
)
179 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
180 free((void *)paramList
->Parameters
[i
].Name
);
182 free(paramList
->Parameters
);
183 _mesa_align_free(paramList
->ParameterValues
);
189 * Make sure there are enough unused parameter slots. Reallocate the list
192 * \param paramList where to reserve parameter slots
193 * \param reserve_slots number of slots to reserve
196 _mesa_reserve_parameter_storage(struct gl_program_parameter_list
*paramList
,
197 unsigned reserve_slots
)
199 const GLuint oldNum
= paramList
->NumParameters
;
201 if (oldNum
+ reserve_slots
> paramList
->Size
) {
202 /* Need to grow the parameter list array (alloc some extra) */
203 paramList
->Size
= paramList
->Size
+ 4 * reserve_slots
;
206 paramList
->Parameters
=
207 realloc(paramList
->Parameters
,
208 paramList
->Size
* sizeof(struct gl_program_parameter
));
210 paramList
->ParameterValueOffset
=
211 realloc(paramList
->ParameterValueOffset
,
212 paramList
->Size
* sizeof(unsigned));
214 paramList
->ParameterValues
= (gl_constant_value
*)
215 _mesa_align_realloc(paramList
->ParameterValues
, /* old buf */
216 oldNum
* 4 * sizeof(gl_constant_value
),/* old sz */
217 paramList
->Size
*4*sizeof(gl_constant_value
),/*new*/
224 * Add a new parameter to a parameter list.
225 * Note that parameter values are usually 4-element GLfloat vectors.
226 * When size > 4 we'll allocate a sequential block of parameters to
227 * store all the values (in blocks of 4).
229 * \param paramList the list to add the parameter to
230 * \param type type of parameter, such as
231 * \param name the parameter name, will be duplicated/copied!
232 * \param size number of elements in 'values' vector (1..4, or more)
233 * \param datatype GL_FLOAT, GL_FLOAT_VECx, GL_INT, GL_INT_VECx or GL_NONE.
234 * \param values initial parameter value, up to 4 gl_constant_values, or NULL
235 * \param state state indexes, or NULL
236 * \return index of new parameter in the list, or -1 if error (out of mem)
239 _mesa_add_parameter(struct gl_program_parameter_list
*paramList
,
240 gl_register_file type
, const char *name
,
241 GLuint size
, GLenum datatype
,
242 const gl_constant_value
*values
,
243 const gl_state_index16 state
[STATE_LENGTH
],
246 assert(0 < size
&& size
<=4);
247 const GLuint oldNum
= paramList
->NumParameters
;
248 unsigned oldValNum
= pad_and_align
?
249 align(paramList
->NumParameterValues
, 4) : paramList
->NumParameterValues
;
251 _mesa_reserve_parameter_storage(paramList
, 1);
253 if (!paramList
->Parameters
|| !paramList
->ParameterValueOffset
||
254 !paramList
->ParameterValues
) {
256 paramList
->NumParameters
= 0;
261 paramList
->NumParameters
= oldNum
+ 1;
263 unsigned pad
= pad_and_align
? align(size
, 4) : size
;
264 paramList
->NumParameterValues
= oldValNum
+ pad
;
266 memset(¶mList
->Parameters
[oldNum
], 0,
267 sizeof(struct gl_program_parameter
));
269 struct gl_program_parameter
*p
= paramList
->Parameters
+ oldNum
;
270 p
->Name
= strdup(name
? name
: "");
273 p
->DataType
= datatype
;
275 paramList
->ParameterValueOffset
[oldNum
] = oldValNum
;
278 COPY_4V(paramList
->ParameterValues
+ oldValNum
, values
);
280 /* copy 1, 2 or 3 values */
283 for (j
= 0; j
< size
; j
++) {
284 paramList
->ParameterValues
[oldValNum
+ j
].f
= values
[j
].f
;
287 /* Zero out padding (if any) to avoid valgrind errors */
288 for (; j
< pad
; j
++) {
289 paramList
->ParameterValues
[oldValNum
+ j
].f
= 0;
293 for (unsigned j
= 0; j
< 4; j
++) {
294 paramList
->ParameterValues
[oldValNum
+ j
].f
= 0;
299 for (unsigned i
= 0; i
< STATE_LENGTH
; i
++)
300 paramList
->Parameters
[oldNum
].StateIndexes
[i
] = state
[i
];
303 return (GLint
) oldNum
;
308 * Add a new unnamed constant to the parameter list. This will be used
309 * when a fragment/vertex program contains something like this:
310 * MOV r, { 0, 1, 2, 3 };
311 * If swizzleOut is non-null we'll search the parameter list for an
312 * existing instance of the constant which matches with a swizzle.
314 * \param paramList the parameter list
315 * \param values four float values
316 * \param swizzleOut returns swizzle mask for accessing the constant
317 * \return index/position of the new parameter in the parameter list.
320 _mesa_add_typed_unnamed_constant(struct gl_program_parameter_list
*paramList
,
321 const gl_constant_value values
[4], GLuint size
,
322 GLenum datatype
, GLuint
*swizzleOut
)
329 lookup_parameter_constant(paramList
, values
, size
, &pos
, swizzleOut
)) {
333 /* Look for empty space in an already unnamed constant parameter
334 * to add this constant. This will only work for single-element
335 * constants because we rely on smearing (i.e. .yyyy or .zzzz).
337 if (size
== 1 && swizzleOut
) {
338 for (pos
= 0; pos
< (GLint
) paramList
->NumParameters
; pos
++) {
339 struct gl_program_parameter
*p
= paramList
->Parameters
+ pos
;
340 unsigned offset
= paramList
->ParameterValueOffset
[pos
];
341 if (p
->Type
== PROGRAM_CONSTANT
&& p
->Size
+ size
<= 4) {
343 gl_constant_value
*pVal
= paramList
->ParameterValues
+ offset
;
344 GLuint swz
= p
->Size
; /* 1, 2 or 3 for Y, Z, W */
345 pVal
[p
->Size
] = values
[0];
347 *swizzleOut
= MAKE_SWIZZLE4(swz
, swz
, swz
, swz
);
353 /* add a new parameter to store this constant */
354 pos
= _mesa_add_parameter(paramList
, PROGRAM_CONSTANT
, NULL
,
355 size
, datatype
, values
, NULL
, true);
356 if (pos
>= 0 && swizzleOut
) {
358 *swizzleOut
= SWIZZLE_XXXX
;
360 *swizzleOut
= SWIZZLE_NOOP
;
367 * Add a new state reference to the parameter list.
368 * This will be used when the program contains something like this:
369 * PARAM ambient = state.material.front.ambient;
371 * \param paramList the parameter list
372 * \param stateTokens an array of 5 (STATE_LENGTH) state tokens
373 * \return index of the new parameter.
376 _mesa_add_state_reference(struct gl_program_parameter_list
*paramList
,
377 const gl_state_index16 stateTokens
[STATE_LENGTH
])
379 const GLuint size
= 4; /* XXX fix */
383 /* Check if the state reference is already in the list */
384 for (index
= 0; index
< (GLint
) paramList
->NumParameters
; index
++) {
385 if (!memcmp(paramList
->Parameters
[index
].StateIndexes
,
387 sizeof(paramList
->Parameters
[index
].StateIndexes
))) {
392 name
= _mesa_program_state_string(stateTokens
);
393 index
= _mesa_add_parameter(paramList
, PROGRAM_STATE_VAR
, name
,
394 size
, GL_NONE
, NULL
, stateTokens
, true);
395 paramList
->StateFlags
|= _mesa_program_state_flags(stateTokens
);
397 /* free name string here since we duplicated it in add_parameter() */