mesa: make _mesa_lookup_parameter_constant static
[mesa.git] / src / mesa / program / prog_parameter.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
23 */
24
25 /**
26 * \file prog_parameter.c
27 * Program parameter lists and functions.
28 * \author Brian Paul
29 */
30
31
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"
38
39
40 /**
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
51 */
52 static GLboolean
53 lookup_parameter_constant(const struct gl_program_parameter_list *list,
54 const gl_constant_value v[], GLuint vSize,
55 GLint *posOut, GLuint *swizzleOut)
56 {
57 GLuint i;
58
59 assert(vSize >= 1);
60 assert(vSize <= 4);
61
62 if (!list) {
63 *posOut = -1;
64 return GL_FALSE;
65 }
66
67 for (i = 0; i < list->NumParameters; i++) {
68 if (list->Parameters[i].Type == PROGRAM_CONSTANT) {
69 if (!swizzleOut) {
70 /* swizzle not allowed */
71 GLuint j, match = 0;
72 for (j = 0; j < vSize; j++) {
73 if (v[j].u == list->ParameterValues[i][j].u)
74 match++;
75 }
76 if (match == vSize) {
77 *posOut = i;
78 return GL_TRUE;
79 }
80 }
81 else {
82 /* try matching w/ swizzle */
83 if (vSize == 1) {
84 /* look for v[0] anywhere within float[4] value */
85 GLuint j;
86 for (j = 0; j < list->Parameters[i].Size; j++) {
87 if (list->ParameterValues[i][j].u == v[0].u) {
88 /* found it */
89 *posOut = i;
90 *swizzleOut = MAKE_SWIZZLE4(j, j, j, j);
91 return GL_TRUE;
92 }
93 }
94 }
95 else if (vSize <= list->Parameters[i].Size) {
96 /* see if we can match this constant (with a swizzle) */
97 GLuint swz[4];
98 GLuint match = 0, j, k;
99 for (j = 0; j < vSize; j++) {
100 if (v[j].u == list->ParameterValues[i][j].u) {
101 swz[j] = j;
102 match++;
103 }
104 else {
105 for (k = 0; k < list->Parameters[i].Size; k++) {
106 if (v[j].u == list->ParameterValues[i][k].u) {
107 swz[j] = k;
108 match++;
109 break;
110 }
111 }
112 }
113 }
114 /* smear last value to remaining positions */
115 for (; j < 4; j++)
116 swz[j] = swz[j-1];
117
118 if (match == vSize) {
119 *posOut = i;
120 *swizzleOut = MAKE_SWIZZLE4(swz[0], swz[1], swz[2], swz[3]);
121 return GL_TRUE;
122 }
123 }
124 }
125 }
126 }
127
128 *posOut = -1;
129 return GL_FALSE;
130 }
131
132
133 struct gl_program_parameter_list *
134 _mesa_new_parameter_list(void)
135 {
136 return CALLOC_STRUCT(gl_program_parameter_list);
137 }
138
139
140 struct gl_program_parameter_list *
141 _mesa_new_parameter_list_sized(unsigned size)
142 {
143 struct gl_program_parameter_list *p = _mesa_new_parameter_list();
144
145 if ((p != NULL) && (size != 0)) {
146 p->Size = size;
147
148 /* alloc arrays */
149 p->Parameters = (struct gl_program_parameter *)
150 calloc(size, sizeof(struct gl_program_parameter));
151
152 p->ParameterValues = (gl_constant_value (*)[4])
153 _mesa_align_malloc(size * 4 *sizeof(gl_constant_value), 16);
154
155
156 if ((p->Parameters == NULL) || (p->ParameterValues == NULL)) {
157 free(p->Parameters);
158 _mesa_align_free(p->ParameterValues);
159 free(p);
160 p = NULL;
161 }
162 }
163
164 return p;
165 }
166
167
168 /**
169 * Free a parameter list and all its parameters
170 */
171 void
172 _mesa_free_parameter_list(struct gl_program_parameter_list *paramList)
173 {
174 GLuint i;
175 for (i = 0; i < paramList->NumParameters; i++) {
176 free((void *)paramList->Parameters[i].Name);
177 }
178 free(paramList->Parameters);
179 _mesa_align_free(paramList->ParameterValues);
180 free(paramList);
181 }
182
183
184 /**
185 * Make sure there are enough unused parameter slots. Reallocate the list
186 * if needed.
187 *
188 * \param paramList where to reserve parameter slots
189 * \param reserve_slots number of slots to reserve
190 */
191 void
192 _mesa_reserve_parameter_storage(struct gl_program_parameter_list *paramList,
193 unsigned reserve_slots)
194 {
195 const GLuint oldNum = paramList->NumParameters;
196
197 if (oldNum + reserve_slots > paramList->Size) {
198 /* Need to grow the parameter list array (alloc some extra) */
199 paramList->Size = paramList->Size + 4 * reserve_slots;
200
201 /* realloc arrays */
202 paramList->Parameters =
203 realloc(paramList->Parameters,
204 paramList->Size * sizeof(struct gl_program_parameter));
205
206 paramList->ParameterValues = (gl_constant_value (*)[4])
207 _mesa_align_realloc(paramList->ParameterValues, /* old buf */
208 oldNum * 4 * sizeof(gl_constant_value),/* old sz */
209 paramList->Size*4*sizeof(gl_constant_value),/*new*/
210 16);
211 }
212 }
213
214
215 /**
216 * Add a new parameter to a parameter list.
217 * Note that parameter values are usually 4-element GLfloat vectors.
218 * When size > 4 we'll allocate a sequential block of parameters to
219 * store all the values (in blocks of 4).
220 *
221 * \param paramList the list to add the parameter to
222 * \param type type of parameter, such as
223 * \param name the parameter name, will be duplicated/copied!
224 * \param size number of elements in 'values' vector (1..4, or more)
225 * \param datatype GL_FLOAT, GL_FLOAT_VECx, GL_INT, GL_INT_VECx or GL_NONE.
226 * \param values initial parameter value, up to 4 gl_constant_values, or NULL
227 * \param state state indexes, or NULL
228 * \return index of new parameter in the list, or -1 if error (out of mem)
229 */
230 GLint
231 _mesa_add_parameter(struct gl_program_parameter_list *paramList,
232 gl_register_file type, const char *name,
233 GLuint size, GLenum datatype,
234 const gl_constant_value *values,
235 const gl_state_index state[STATE_LENGTH])
236 {
237 const GLuint oldNum = paramList->NumParameters;
238 const GLuint sz4 = (size + 3) / 4; /* no. of new param slots needed */
239
240 assert(size > 0);
241
242 _mesa_reserve_parameter_storage(paramList, sz4);
243
244 if (!paramList->Parameters ||
245 !paramList->ParameterValues) {
246 /* out of memory */
247 paramList->NumParameters = 0;
248 paramList->Size = 0;
249 return -1;
250 }
251 else {
252 GLuint i, j;
253
254 paramList->NumParameters = oldNum + sz4;
255
256 memset(&paramList->Parameters[oldNum], 0,
257 sz4 * sizeof(struct gl_program_parameter));
258
259 for (i = 0; i < sz4; i++) {
260 struct gl_program_parameter *p = paramList->Parameters + oldNum + i;
261 p->Name = name ? strdup(name) : NULL;
262 p->Type = type;
263 p->Size = size;
264 p->DataType = datatype;
265 if (values) {
266 if (size >= 4) {
267 COPY_4V(paramList->ParameterValues[oldNum + i], values);
268 }
269 else {
270 /* copy 1, 2 or 3 values */
271 GLuint remaining = size % 4;
272 assert(remaining < 4);
273 for (j = 0; j < remaining; j++) {
274 paramList->ParameterValues[oldNum + i][j].f = values[j].f;
275 }
276 /* fill in remaining positions with zeros */
277 for (; j < 4; j++) {
278 paramList->ParameterValues[oldNum + i][j].f = 0.0f;
279 }
280 }
281 values += 4;
282 p->Initialized = GL_TRUE;
283 }
284 else {
285 /* silence valgrind */
286 for (j = 0; j < 4; j++)
287 paramList->ParameterValues[oldNum + i][j].f = 0;
288 }
289 size -= 4;
290 }
291
292 if (state) {
293 for (i = 0; i < STATE_LENGTH; i++)
294 paramList->Parameters[oldNum].StateIndexes[i] = state[i];
295 }
296
297 return (GLint) oldNum;
298 }
299 }
300
301
302 /**
303 * Add a new unnamed constant to the parameter list. This will be used
304 * when a fragment/vertex program contains something like this:
305 * MOV r, { 0, 1, 2, 3 };
306 * If swizzleOut is non-null we'll search the parameter list for an
307 * existing instance of the constant which matches with a swizzle.
308 *
309 * \param paramList the parameter list
310 * \param values four float values
311 * \param swizzleOut returns swizzle mask for accessing the constant
312 * \return index/position of the new parameter in the parameter list.
313 */
314 GLint
315 _mesa_add_typed_unnamed_constant(struct gl_program_parameter_list *paramList,
316 const gl_constant_value values[4], GLuint size,
317 GLenum datatype, GLuint *swizzleOut)
318 {
319 GLint pos;
320 assert(size >= 1);
321 assert(size <= 4);
322
323 if (swizzleOut &&
324 lookup_parameter_constant(paramList, values, size, &pos, swizzleOut)) {
325 return pos;
326 }
327
328 /* Look for empty space in an already unnamed constant parameter
329 * to add this constant. This will only work for single-element
330 * constants because we rely on smearing (i.e. .yyyy or .zzzz).
331 */
332 if (size == 1 && swizzleOut) {
333 for (pos = 0; pos < (GLint) paramList->NumParameters; pos++) {
334 struct gl_program_parameter *p = paramList->Parameters + pos;
335 if (p->Type == PROGRAM_CONSTANT && p->Size + size <= 4) {
336 /* ok, found room */
337 gl_constant_value *pVal = paramList->ParameterValues[pos];
338 GLuint swz = p->Size; /* 1, 2 or 3 for Y, Z, W */
339 pVal[p->Size] = values[0];
340 p->Size++;
341 *swizzleOut = MAKE_SWIZZLE4(swz, swz, swz, swz);
342 return pos;
343 }
344 }
345 }
346
347 /* add a new parameter to store this constant */
348 pos = _mesa_add_parameter(paramList, PROGRAM_CONSTANT, NULL,
349 size, datatype, values, NULL);
350 if (pos >= 0 && swizzleOut) {
351 if (size == 1)
352 *swizzleOut = SWIZZLE_XXXX;
353 else
354 *swizzleOut = SWIZZLE_NOOP;
355 }
356 return pos;
357 }
358
359 /**
360 * Add a new unnamed constant to the parameter list. This will be used
361 * when a fragment/vertex program contains something like this:
362 * MOV r, { 0, 1, 2, 3 };
363 * If swizzleOut is non-null we'll search the parameter list for an
364 * existing instance of the constant which matches with a swizzle.
365 *
366 * \param paramList the parameter list
367 * \param values four float values
368 * \param swizzleOut returns swizzle mask for accessing the constant
369 * \return index/position of the new parameter in the parameter list.
370 * \sa _mesa_add_typed_unnamed_constant
371 */
372 GLint
373 _mesa_add_unnamed_constant(struct gl_program_parameter_list *paramList,
374 const gl_constant_value values[4], GLuint size,
375 GLuint *swizzleOut)
376 {
377 return _mesa_add_typed_unnamed_constant(paramList, values, size, GL_NONE,
378 swizzleOut);
379 }
380
381
382 /**
383 * Add a new state reference to the parameter list.
384 * This will be used when the program contains something like this:
385 * PARAM ambient = state.material.front.ambient;
386 *
387 * \param paramList the parameter list
388 * \param stateTokens an array of 5 (STATE_LENGTH) state tokens
389 * \return index of the new parameter.
390 */
391 GLint
392 _mesa_add_state_reference(struct gl_program_parameter_list *paramList,
393 const gl_state_index stateTokens[STATE_LENGTH])
394 {
395 const GLuint size = 4; /* XXX fix */
396 char *name;
397 GLint index;
398
399 /* Check if the state reference is already in the list */
400 for (index = 0; index < (GLint) paramList->NumParameters; index++) {
401 if (!memcmp(paramList->Parameters[index].StateIndexes,
402 stateTokens, STATE_LENGTH * sizeof(gl_state_index))) {
403 return index;
404 }
405 }
406
407 name = _mesa_program_state_string(stateTokens);
408 index = _mesa_add_parameter(paramList, PROGRAM_STATE_VAR, name,
409 size, GL_NONE,
410 NULL, (gl_state_index *) stateTokens);
411 paramList->StateFlags |= _mesa_program_state_flags(stateTokens);
412
413 /* free name string here since we duplicated it in add_parameter() */
414 free(name);
415
416 return index;
417 }
418
419
420 /**
421 * Given a program parameter name, find its position in the list of parameters.
422 * \param paramList the parameter list to search
423 * \param nameLen length of name (in chars).
424 * If length is negative, assume that name is null-terminated.
425 * \param name the name to search for
426 * \return index of parameter in the list.
427 */
428 GLint
429 _mesa_lookup_parameter_index(const struct gl_program_parameter_list *paramList,
430 GLsizei nameLen, const char *name)
431 {
432 GLint i;
433
434 if (!paramList)
435 return -1;
436
437 if (nameLen == -1) {
438 /* name is null-terminated */
439 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
440 if (paramList->Parameters[i].Name &&
441 strcmp(paramList->Parameters[i].Name, name) == 0)
442 return i;
443 }
444 }
445 else {
446 /* name is not null-terminated, use nameLen */
447 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
448 if (paramList->Parameters[i].Name &&
449 strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
450 && strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
451 return i;
452 }
453 }
454 return -1;
455 }