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.
31 #include "main/glheader.h"
32 #include "main/macros.h"
33 #include "util/u_memory.h"
34 #include "prog_instruction.h"
35 #include "prog_parameter.h"
36 #include "prog_statevars.h"
40 * Look for a float vector in the given parameter list. The float vector
41 * may be of length 1, 2, 3 or 4. If swizzleOut is non-null, we'll try
42 * swizzling to find a match.
43 * \param list the parameter list to search
44 * \param v the float vector to search for
45 * \param vSize number of element in v
46 * \param posOut returns the position of the constant, if found
47 * \param swizzleOut returns a swizzle mask describing location of the
48 * vector elements if found.
49 * \return GL_TRUE if found, GL_FALSE if not found
52 lookup_parameter_constant(const struct gl_program_parameter_list
*list
,
53 const gl_constant_value v
[], GLuint vSize
,
54 GLint
*posOut
, GLuint
*swizzleOut
)
66 for (i
= 0; i
< list
->NumParameters
; i
++) {
67 if (list
->Parameters
[i
].Type
== PROGRAM_CONSTANT
) {
68 unsigned offset
= list
->ParameterValueOffset
[i
];
71 /* swizzle not allowed */
73 for (j
= 0; j
< vSize
; j
++) {
74 if (v
[j
].u
== list
->ParameterValues
[offset
+ j
].u
)
83 /* try matching w/ swizzle */
85 /* look for v[0] anywhere within float[4] value */
87 for (j
= 0; j
< list
->Parameters
[i
].Size
; j
++) {
88 if (list
->ParameterValues
[offset
+ j
].u
== v
[0].u
) {
91 *swizzleOut
= MAKE_SWIZZLE4(j
, j
, j
, j
);
96 else if (vSize
<= list
->Parameters
[i
].Size
) {
97 /* see if we can match this constant (with a swizzle) */
99 GLuint match
= 0, j
, k
;
100 for (j
= 0; j
< vSize
; j
++) {
101 if (v
[j
].u
== list
->ParameterValues
[offset
+ j
].u
) {
106 for (k
= 0; k
< list
->Parameters
[i
].Size
; k
++) {
107 if (v
[j
].u
== list
->ParameterValues
[offset
+ k
].u
) {
115 /* smear last value to remaining positions */
119 if (match
== vSize
) {
121 *swizzleOut
= MAKE_SWIZZLE4(swz
[0], swz
[1], swz
[2], swz
[3]);
134 struct gl_program_parameter_list
*
135 _mesa_new_parameter_list(void)
137 return CALLOC_STRUCT(gl_program_parameter_list
);
141 struct gl_program_parameter_list
*
142 _mesa_new_parameter_list_sized(unsigned size
)
144 struct gl_program_parameter_list
*p
= _mesa_new_parameter_list();
146 if ((p
!= NULL
) && (size
!= 0)) {
150 p
->Parameters
= (struct gl_program_parameter
*)
151 calloc(size
, sizeof(struct gl_program_parameter
));
153 p
->ParameterValueOffset
= (unsigned *) calloc(size
, sizeof(unsigned));
155 p
->ParameterValues
= (gl_constant_value
*)
156 align_malloc(size
* 4 *sizeof(gl_constant_value
), 16);
159 if ((p
->Parameters
== NULL
) || (p
->ParameterValues
== NULL
)) {
161 align_free(p
->ParameterValues
);
172 * Free a parameter list and all its parameters
175 _mesa_free_parameter_list(struct gl_program_parameter_list
*paramList
)
178 for (i
= 0; i
< paramList
->NumParameters
; i
++) {
179 free((void *)paramList
->Parameters
[i
].Name
);
181 free(paramList
->Parameters
);
182 free(paramList
->ParameterValueOffset
);
183 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 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
= paramList
->NumParameterValues
;
251 oldValNum
= align(oldValNum
, 4); /* pad start to a vec4 boundary */
252 else if (_mesa_gl_datatype_is_64bit(datatype
))
253 oldValNum
= align(oldValNum
, 2); /* pad start to 64-bit */
255 _mesa_reserve_parameter_storage(paramList
, 1);
257 if (!paramList
->Parameters
|| !paramList
->ParameterValueOffset
||
258 !paramList
->ParameterValues
) {
260 paramList
->NumParameters
= 0;
265 paramList
->NumParameters
= oldNum
+ 1;
267 unsigned pad
= pad_and_align
? align(size
, 4) : size
;
268 paramList
->NumParameterValues
= oldValNum
+ pad
;
270 memset(¶mList
->Parameters
[oldNum
], 0,
271 sizeof(struct gl_program_parameter
));
273 struct gl_program_parameter
*p
= paramList
->Parameters
+ oldNum
;
274 p
->Name
= strdup(name
? name
: "");
277 p
->Padded
= pad_and_align
;
278 p
->DataType
= datatype
;
280 paramList
->ParameterValueOffset
[oldNum
] = oldValNum
;
283 COPY_4V(paramList
->ParameterValues
+ oldValNum
, values
);
285 /* copy 1, 2 or 3 values */
288 for (j
= 0; j
< size
; j
++) {
289 paramList
->ParameterValues
[oldValNum
+ j
].f
= values
[j
].f
;
292 /* Zero out padding (if any) to avoid valgrind errors */
293 for (; j
< pad
; j
++) {
294 paramList
->ParameterValues
[oldValNum
+ j
].f
= 0;
298 for (unsigned j
= 0; j
< 4; j
++) {
299 paramList
->ParameterValues
[oldValNum
+ j
].f
= 0;
304 for (unsigned i
= 0; i
< STATE_LENGTH
; i
++)
305 paramList
->Parameters
[oldNum
].StateIndexes
[i
] = state
[i
];
308 return (GLint
) oldNum
;
313 * Add a new unnamed constant to the parameter list. This will be used
314 * when a fragment/vertex program contains something like this:
315 * MOV r, { 0, 1, 2, 3 };
316 * If swizzleOut is non-null we'll search the parameter list for an
317 * existing instance of the constant which matches with a swizzle.
319 * \param paramList the parameter list
320 * \param values four float values
321 * \param swizzleOut returns swizzle mask for accessing the constant
322 * \return index/position of the new parameter in the parameter list.
325 _mesa_add_typed_unnamed_constant(struct gl_program_parameter_list
*paramList
,
326 const gl_constant_value values
[4], GLuint size
,
327 GLenum datatype
, GLuint
*swizzleOut
)
334 lookup_parameter_constant(paramList
, values
, size
, &pos
, swizzleOut
)) {
338 /* Look for empty space in an already unnamed constant parameter
339 * to add this constant. This will only work for single-element
340 * constants because we rely on smearing (i.e. .yyyy or .zzzz).
342 if (size
== 1 && swizzleOut
) {
343 for (pos
= 0; pos
< (GLint
) paramList
->NumParameters
; pos
++) {
344 struct gl_program_parameter
*p
= paramList
->Parameters
+ pos
;
345 unsigned offset
= paramList
->ParameterValueOffset
[pos
];
346 if (p
->Type
== PROGRAM_CONSTANT
&& p
->Size
+ size
<= 4) {
348 gl_constant_value
*pVal
= paramList
->ParameterValues
+ offset
;
349 GLuint swz
= p
->Size
; /* 1, 2 or 3 for Y, Z, W */
350 pVal
[p
->Size
] = values
[0];
352 *swizzleOut
= MAKE_SWIZZLE4(swz
, swz
, swz
, swz
);
358 /* add a new parameter to store this constant */
359 pos
= _mesa_add_parameter(paramList
, PROGRAM_CONSTANT
, NULL
,
360 size
, datatype
, values
, NULL
, true);
361 if (pos
>= 0 && swizzleOut
) {
363 *swizzleOut
= SWIZZLE_XXXX
;
365 *swizzleOut
= SWIZZLE_NOOP
;
371 _mesa_add_sized_state_reference(struct gl_program_parameter_list
*paramList
,
372 const gl_state_index16 stateTokens
[STATE_LENGTH
],
373 const unsigned size
, bool pad_and_align
)
378 /* Check if the state reference is already in the list */
379 for (index
= 0; index
< (GLint
) paramList
->NumParameters
; index
++) {
380 if (!memcmp(paramList
->Parameters
[index
].StateIndexes
,
382 sizeof(paramList
->Parameters
[index
].StateIndexes
))) {
387 name
= _mesa_program_state_string(stateTokens
);
388 index
= _mesa_add_parameter(paramList
, PROGRAM_STATE_VAR
, name
,
389 size
, GL_NONE
, NULL
, stateTokens
,
391 paramList
->StateFlags
|= _mesa_program_state_flags(stateTokens
);
393 /* free name string here since we duplicated it in add_parameter() */
401 * Add a new state reference to the parameter list.
402 * This will be used when the program contains something like this:
403 * PARAM ambient = state.material.front.ambient;
405 * \param paramList the parameter list
406 * \param stateTokens an array of 5 (STATE_LENGTH) state tokens
407 * \return index of the new parameter.
410 _mesa_add_state_reference(struct gl_program_parameter_list
*paramList
,
411 const gl_state_index16 stateTokens
[STATE_LENGTH
])
413 return _mesa_add_sized_state_reference(paramList
, stateTokens
, 4, true);