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(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 _mesa_align_free(paramList
->ParameterValues
);
92 * Make sure there are enough unused parameter slots. Reallocate the list
95 * \param paramList where to reserve parameter slots
96 * \param reserve_slots number of slots to reserve
99 _mesa_reserve_parameter_storage(struct gl_program_parameter_list
*paramList
,
100 unsigned reserve_slots
)
102 const GLuint oldNum
= paramList
->NumParameters
;
104 if (oldNum
+ reserve_slots
> paramList
->Size
) {
105 /* Need to grow the parameter list array (alloc some extra) */
106 paramList
->Size
= paramList
->Size
+ 4 * reserve_slots
;
109 paramList
->Parameters
=
110 realloc(paramList
->Parameters
,
111 paramList
->Size
* sizeof(struct gl_program_parameter
));
113 paramList
->ParameterValues
= (gl_constant_value (*)[4])
114 _mesa_align_realloc(paramList
->ParameterValues
, /* old buf */
115 oldNum
* 4 * sizeof(gl_constant_value
),/* old sz */
116 paramList
->Size
*4*sizeof(gl_constant_value
),/*new*/
123 * Add a new parameter to a parameter list.
124 * Note that parameter values are usually 4-element GLfloat vectors.
125 * When size > 4 we'll allocate a sequential block of parameters to
126 * store all the values (in blocks of 4).
128 * \param paramList the list to add the parameter to
129 * \param type type of parameter, such as
130 * \param name the parameter name, will be duplicated/copied!
131 * \param size number of elements in 'values' vector (1..4, or more)
132 * \param datatype GL_FLOAT, GL_FLOAT_VECx, GL_INT, GL_INT_VECx or GL_NONE.
133 * \param values initial parameter value, up to 4 gl_constant_values, or NULL
134 * \param state state indexes, or NULL
135 * \return index of new parameter in the list, or -1 if error (out of mem)
138 _mesa_add_parameter(struct gl_program_parameter_list
*paramList
,
139 gl_register_file type
, const char *name
,
140 GLuint size
, GLenum datatype
,
141 const gl_constant_value
*values
,
142 const gl_state_index state
[STATE_LENGTH
])
144 const GLuint oldNum
= paramList
->NumParameters
;
145 const GLuint sz4
= (size
+ 3) / 4; /* no. of new param slots needed */
149 _mesa_reserve_parameter_storage(paramList
, sz4
);
151 if (!paramList
->Parameters
||
152 !paramList
->ParameterValues
) {
154 paramList
->NumParameters
= 0;
161 paramList
->NumParameters
= oldNum
+ sz4
;
163 memset(¶mList
->Parameters
[oldNum
], 0,
164 sz4
* sizeof(struct gl_program_parameter
));
166 for (i
= 0; i
< sz4
; i
++) {
167 struct gl_program_parameter
*p
= paramList
->Parameters
+ oldNum
+ i
;
168 p
->Name
= name
? strdup(name
) : NULL
;
171 p
->DataType
= datatype
;
174 COPY_4V(paramList
->ParameterValues
[oldNum
+ i
], values
);
177 /* copy 1, 2 or 3 values */
178 GLuint remaining
= size
% 4;
179 assert(remaining
< 4);
180 for (j
= 0; j
< remaining
; j
++) {
181 paramList
->ParameterValues
[oldNum
+ i
][j
].f
= values
[j
].f
;
183 /* fill in remaining positions with zeros */
185 paramList
->ParameterValues
[oldNum
+ i
][j
].f
= 0.0f
;
189 p
->Initialized
= GL_TRUE
;
192 /* silence valgrind */
193 for (j
= 0; j
< 4; j
++)
194 paramList
->ParameterValues
[oldNum
+ i
][j
].f
= 0;
200 for (i
= 0; i
< STATE_LENGTH
; i
++)
201 paramList
->Parameters
[oldNum
].StateIndexes
[i
] = state
[i
];
204 return (GLint
) oldNum
;
210 * Add a new unnamed constant to the parameter list. This will be used
211 * when a fragment/vertex program contains something like this:
212 * MOV r, { 0, 1, 2, 3 };
213 * If swizzleOut is non-null we'll search the parameter list for an
214 * existing instance of the constant which matches with a swizzle.
216 * \param paramList the parameter list
217 * \param values four float values
218 * \param swizzleOut returns swizzle mask for accessing the constant
219 * \return index/position of the new parameter in the parameter list.
222 _mesa_add_typed_unnamed_constant(struct gl_program_parameter_list
*paramList
,
223 const gl_constant_value values
[4], GLuint size
,
224 GLenum datatype
, GLuint
*swizzleOut
)
231 _mesa_lookup_parameter_constant(paramList
, values
,
232 size
, &pos
, swizzleOut
)) {
236 /* Look for empty space in an already unnamed constant parameter
237 * to add this constant. This will only work for single-element
238 * constants because we rely on smearing (i.e. .yyyy or .zzzz).
240 if (size
== 1 && swizzleOut
) {
241 for (pos
= 0; pos
< (GLint
) paramList
->NumParameters
; pos
++) {
242 struct gl_program_parameter
*p
= paramList
->Parameters
+ pos
;
243 if (p
->Type
== PROGRAM_CONSTANT
&& p
->Size
+ size
<= 4) {
245 gl_constant_value
*pVal
= paramList
->ParameterValues
[pos
];
246 GLuint swz
= p
->Size
; /* 1, 2 or 3 for Y, Z, W */
247 pVal
[p
->Size
] = values
[0];
249 *swizzleOut
= MAKE_SWIZZLE4(swz
, swz
, swz
, swz
);
255 /* add a new parameter to store this constant */
256 pos
= _mesa_add_parameter(paramList
, PROGRAM_CONSTANT
, NULL
,
257 size
, datatype
, values
, NULL
);
258 if (pos
>= 0 && swizzleOut
) {
260 *swizzleOut
= SWIZZLE_XXXX
;
262 *swizzleOut
= SWIZZLE_NOOP
;
268 * Add a new unnamed constant to the parameter list. This will be used
269 * when a fragment/vertex program contains something like this:
270 * MOV r, { 0, 1, 2, 3 };
271 * If swizzleOut is non-null we'll search the parameter list for an
272 * existing instance of the constant which matches with a swizzle.
274 * \param paramList the parameter list
275 * \param values four float values
276 * \param swizzleOut returns swizzle mask for accessing the constant
277 * \return index/position of the new parameter in the parameter list.
278 * \sa _mesa_add_typed_unnamed_constant
281 _mesa_add_unnamed_constant(struct gl_program_parameter_list
*paramList
,
282 const gl_constant_value values
[4], GLuint size
,
285 return _mesa_add_typed_unnamed_constant(paramList
, values
, size
, GL_NONE
,
291 * Add a new state reference to the parameter list.
292 * This will be used when the program contains something like this:
293 * PARAM ambient = state.material.front.ambient;
295 * \param paramList the parameter list
296 * \param stateTokens an array of 5 (STATE_LENGTH) state tokens
297 * \return index of the new parameter.
300 _mesa_add_state_reference(struct gl_program_parameter_list
*paramList
,
301 const gl_state_index stateTokens
[STATE_LENGTH
])
303 const GLuint size
= 4; /* XXX fix */
307 /* Check if the state reference is already in the list */
308 for (index
= 0; index
< (GLint
) paramList
->NumParameters
; index
++) {
309 if (!memcmp(paramList
->Parameters
[index
].StateIndexes
,
310 stateTokens
, STATE_LENGTH
* sizeof(gl_state_index
))) {
315 name
= _mesa_program_state_string(stateTokens
);
316 index
= _mesa_add_parameter(paramList
, PROGRAM_STATE_VAR
, name
,
318 NULL
, (gl_state_index
*) stateTokens
);
319 paramList
->StateFlags
|= _mesa_program_state_flags(stateTokens
);
321 /* free name string here since we duplicated it in add_parameter() */
329 * Given a program parameter name, find its position in the list of parameters.
330 * \param paramList the parameter list to search
331 * \param nameLen length of name (in chars).
332 * If length is negative, assume that name is null-terminated.
333 * \param name the name to search for
334 * \return index of parameter in the list.
337 _mesa_lookup_parameter_index(const struct gl_program_parameter_list
*paramList
,
338 GLsizei nameLen
, const char *name
)
346 /* name is null-terminated */
347 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
348 if (paramList
->Parameters
[i
].Name
&&
349 strcmp(paramList
->Parameters
[i
].Name
, name
) == 0)
354 /* name is not null-terminated, use nameLen */
355 for (i
= 0; i
< (GLint
) paramList
->NumParameters
; i
++) {
356 if (paramList
->Parameters
[i
].Name
&&
357 strncmp(paramList
->Parameters
[i
].Name
, name
, nameLen
) == 0
358 && strlen(paramList
->Parameters
[i
].Name
) == (size_t)nameLen
)
367 * Look for a float vector in the given parameter list. The float vector
368 * may be of length 1, 2, 3 or 4. If swizzleOut is non-null, we'll try
369 * swizzling to find a match.
370 * \param list the parameter list to search
371 * \param v the float vector to search for
372 * \param vSize number of element in v
373 * \param posOut returns the position of the constant, if found
374 * \param swizzleOut returns a swizzle mask describing location of the
375 * vector elements if found.
376 * \return GL_TRUE if found, GL_FALSE if not found
379 _mesa_lookup_parameter_constant(const struct gl_program_parameter_list
*list
,
380 const gl_constant_value v
[], GLuint vSize
,
381 GLint
*posOut
, GLuint
*swizzleOut
)
393 for (i
= 0; i
< list
->NumParameters
; i
++) {
394 if (list
->Parameters
[i
].Type
== PROGRAM_CONSTANT
) {
396 /* swizzle not allowed */
398 for (j
= 0; j
< vSize
; j
++) {
399 if (v
[j
].u
== list
->ParameterValues
[i
][j
].u
)
402 if (match
== vSize
) {
408 /* try matching w/ swizzle */
410 /* look for v[0] anywhere within float[4] value */
412 for (j
= 0; j
< list
->Parameters
[i
].Size
; j
++) {
413 if (list
->ParameterValues
[i
][j
].u
== v
[0].u
) {
416 *swizzleOut
= MAKE_SWIZZLE4(j
, j
, j
, j
);
421 else if (vSize
<= list
->Parameters
[i
].Size
) {
422 /* see if we can match this constant (with a swizzle) */
424 GLuint match
= 0, j
, k
;
425 for (j
= 0; j
< vSize
; j
++) {
426 if (v
[j
].u
== list
->ParameterValues
[i
][j
].u
) {
431 for (k
= 0; k
< list
->Parameters
[i
].Size
; k
++) {
432 if (v
[j
].u
== list
->ParameterValues
[i
][k
].u
) {
440 /* smear last value to remaining positions */
444 if (match
== vSize
) {
446 *swizzleOut
= MAKE_SWIZZLE4(swz
[0], swz
[1], swz
[2], swz
[3]);