Merge branch 'origin' into glsl-compiler-1
[mesa.git] / src / mesa / shader / prog_parameter.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.3
4 *
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR 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 "glheader.h"
33 #include "imports.h"
34 #include "macros.h"
35 #include "prog_instruction.h"
36 #include "prog_parameter.h"
37 #include "prog_statevars.h"
38
39
40 struct gl_program_parameter_list *
41 _mesa_new_parameter_list(void)
42 {
43 return (struct gl_program_parameter_list *)
44 _mesa_calloc(sizeof(struct gl_program_parameter_list));
45 }
46
47
48 /**
49 * Free a parameter list and all its parameters
50 */
51 void
52 _mesa_free_parameter_list(struct gl_program_parameter_list *paramList)
53 {
54 GLuint i;
55 for (i = 0; i < paramList->NumParameters; i++) {
56 if (paramList->Parameters[i].Name)
57 _mesa_free((void *) paramList->Parameters[i].Name);
58 }
59 _mesa_free(paramList->Parameters);
60 if (paramList->ParameterValues)
61 _mesa_align_free(paramList->ParameterValues);
62 _mesa_free(paramList);
63 }
64
65
66
67 /**
68 * Add a new parameter to a parameter list.
69 * Note that parameter values are usually 4-element GLfloat vectors.
70 * When size > 4 we'll allocate a sequential block of parameters to
71 * store all the values (in blocks of 4).
72 *
73 * \param paramList the list to add the parameter to
74 * \param type type of parameter, such as
75 * \param name the parameter name, will be duplicated/copied!
76 * \param size number of elements in 'values' vector (1..4, or more)
77 * \param values initial parameter value, up to 4 GLfloats, or NULL
78 * \param state state indexes, or NULL
79 * \return index of new parameter in the list, or -1 if error (out of mem)
80 */
81 GLint
82 _mesa_add_parameter(struct gl_program_parameter_list *paramList,
83 enum register_file type, const char *name,
84 GLuint size, const GLfloat *values,
85 const gl_state_index state[STATE_LENGTH])
86 {
87 const GLuint oldNum = paramList->NumParameters;
88 const GLuint sz4 = (size + 3) / 4; /* no. of new param slots needed */
89
90 assert(size > 0);
91 assert(size <= 16); /* XXX anything larger than a matrix??? */
92
93 if (oldNum + sz4 > paramList->Size) {
94 /* Need to grow the parameter list array (alloc some extra) */
95 paramList->Size = paramList->Size + 4 * sz4;
96
97 /* realloc arrays */
98 paramList->Parameters = (struct gl_program_parameter *)
99 _mesa_realloc(paramList->Parameters,
100 oldNum * sizeof(struct gl_program_parameter),
101 paramList->Size * sizeof(struct gl_program_parameter));
102
103 paramList->ParameterValues = (GLfloat (*)[4])
104 _mesa_align_realloc(paramList->ParameterValues, /* old buf */
105 oldNum * 4 * sizeof(GLfloat), /* old size */
106 paramList->Size * 4 *sizeof(GLfloat), /* new sz */
107 16);
108 }
109
110 if (!paramList->Parameters ||
111 !paramList->ParameterValues) {
112 /* out of memory */
113 paramList->NumParameters = 0;
114 paramList->Size = 0;
115 return -1;
116 }
117 else {
118 GLuint i;
119
120 paramList->NumParameters = oldNum + sz4;
121
122 _mesa_memset(&paramList->Parameters[oldNum], 0,
123 sz4 * sizeof(struct gl_program_parameter));
124
125 for (i = 0; i < sz4; i++) {
126 struct gl_program_parameter *p = paramList->Parameters + oldNum + i;
127 p->Name = name ? _mesa_strdup(name) : NULL;
128 p->Type = type;
129 p->Size = size;
130 if (values) {
131 COPY_4V(paramList->ParameterValues[oldNum + i], values);
132 values += 4;
133 }
134 else {
135 /* silence valgrind */
136 ASSIGN_4V(paramList->ParameterValues[oldNum + i], 0, 0, 0, 0);
137 }
138 size -= 4;
139 }
140
141 if (state) {
142 for (i = 0; i < STATE_LENGTH; i++)
143 paramList->Parameters[oldNum].StateIndexes[i] = state[i];
144 }
145
146 return (GLint) oldNum;
147 }
148 }
149
150
151 /**
152 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
153 * \return index of the new entry in the parameter list
154 */
155 GLint
156 _mesa_add_named_parameter(struct gl_program_parameter_list *paramList,
157 const char *name, const GLfloat values[4])
158 {
159 return _mesa_add_parameter(paramList, PROGRAM_NAMED_PARAM, name,
160 4, values, NULL);
161
162 }
163
164
165 /**
166 * Add a new named constant to the parameter list.
167 * This will be used when the program contains something like this:
168 * PARAM myVals = { 0, 1, 2, 3 };
169 *
170 * \param paramList the parameter list
171 * \param name the name for the constant
172 * \param values four float values
173 * \return index/position of the new parameter in the parameter list
174 */
175 GLint
176 _mesa_add_named_constant(struct gl_program_parameter_list *paramList,
177 const char *name, const GLfloat values[4],
178 GLuint size)
179 {
180 #if 0 /* disable this for now -- we need to save the name! */
181 GLint pos;
182 GLuint swizzle;
183 ASSERT(size == 4); /* XXX future feature */
184 /* check if we already have this constant */
185 if (_mesa_lookup_parameter_constant(paramList, values, 4, &pos, &swizzle)) {
186 return pos;
187 }
188 #endif
189 size = 4; /** XXX fix */
190 return _mesa_add_parameter(paramList, PROGRAM_CONSTANT, name,
191 size, values, NULL);
192 }
193
194
195 /**
196 * Add a new unnamed constant to the parameter list.
197 * This will be used when the program contains something like this:
198 * MOV r, { 0, 1, 2, 3 };
199 *
200 * \param paramList the parameter list
201 * \param values four float values
202 * \param swizzleOut returns swizzle mask for accessing the constant
203 * \return index/position of the new parameter in the parameter list.
204 */
205 GLint
206 _mesa_add_unnamed_constant(struct gl_program_parameter_list *paramList,
207 const GLfloat values[4], GLuint size,
208 GLuint *swizzleOut)
209 {
210 GLint pos;
211 ASSERT(size >= 1);
212 ASSERT(size <= 4);
213
214 if (_mesa_lookup_parameter_constant(paramList, values,
215 size, &pos, swizzleOut)) {
216 return pos;
217 }
218
219 /* Look for empty space in an already unnamed constant parameter
220 * to add this constant. This will only work for single-element
221 * constants because we rely on smearing (i.e. .yyyy or .zzzz).
222 */
223 if (size == 1) {
224 for (pos = 0; pos < (GLint) paramList->NumParameters; pos++) {
225 struct gl_program_parameter *p = paramList->Parameters + pos;
226 if (p->Type == PROGRAM_CONSTANT && p->Size + size <= 4) {
227 /* ok, found room */
228 GLfloat *pVal = paramList->ParameterValues[pos];
229 GLuint swz = p->Size; /* 1, 2 or 3 for Y, Z, W */
230 pVal[p->Size] = values[0];
231 p->Size++;
232 *swizzleOut = MAKE_SWIZZLE4(swz, swz, swz, swz);
233 return pos;
234 }
235 }
236 }
237
238 /* add a new parameter to store this constant */
239 pos = _mesa_add_parameter(paramList, PROGRAM_CONSTANT, NULL,
240 size, values, NULL);
241 if (pos >= 0) {
242 if (size == 1)
243 *swizzleOut = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X,
244 SWIZZLE_X, SWIZZLE_X);
245 else
246 *swizzleOut = SWIZZLE_NOOP;
247 }
248 return pos;
249 }
250
251
252 GLint
253 _mesa_add_uniform(struct gl_program_parameter_list *paramList,
254 const char *name, GLuint size)
255 {
256 GLint i = _mesa_lookup_parameter_index(paramList, -1, name);
257 if (i >= 0 && paramList->Parameters[i].Type == PROGRAM_UNIFORM) {
258 /* already in list */
259 return i;
260 }
261 else {
262 i = _mesa_add_parameter(paramList, PROGRAM_UNIFORM, name,
263 size, NULL, NULL);
264
265 return i;
266 }
267 }
268
269
270 GLint
271 _mesa_add_sampler(struct gl_program_parameter_list *paramList,
272 const char *name)
273 {
274 GLint i = _mesa_lookup_parameter_index(paramList, -1, name);
275 if (i >= 0 && paramList->Parameters[i].Type == PROGRAM_SAMPLER) {
276 /* already in list */
277 return i;
278 }
279 else {
280 const GLint size = 1;
281 i = _mesa_add_parameter(paramList, PROGRAM_SAMPLER, name,
282 size, NULL, NULL);
283 return i;
284 }
285 }
286
287
288 /**
289 * Add parameter representing a varying variable.
290 */
291 GLint
292 _mesa_add_varying(struct gl_program_parameter_list *paramList,
293 const char *name, GLuint size)
294 {
295 GLint i = _mesa_lookup_parameter_index(paramList, -1, name);
296 if (i >= 0 && paramList->Parameters[i].Type == PROGRAM_VARYING) {
297 /* already in list */
298 return i;
299 }
300 else {
301 assert(size == 4);
302 i = _mesa_add_parameter(paramList, PROGRAM_VARYING, name,
303 size, NULL, NULL);
304 return i;
305 }
306 }
307
308
309 /**
310 * Add parameter representing a vertex program attribute.
311 * \param size size of attribute (in floats), may be -1 if unknown
312 * \param attrib the attribute index, or -1 if unknown
313 */
314 GLint
315 _mesa_add_attribute(struct gl_program_parameter_list *paramList,
316 const char *name, GLint size, GLint attrib)
317 {
318 GLint i = _mesa_lookup_parameter_index(paramList, -1, name);
319 if (i >= 0) {
320 /* replace */
321 if (attrib < 0)
322 attrib = i;
323 paramList->Parameters[i].StateIndexes[0] = attrib;
324 }
325 else {
326 /* add */
327 gl_state_index state[STATE_LENGTH];
328 state[0] = (gl_state_index) attrib;
329 if (size < 0)
330 size = 4;
331 i = _mesa_add_parameter(paramList, PROGRAM_INPUT, name,
332 size, NULL, state);
333 }
334 return i;
335 }
336
337
338
339 #if 0 /* not used yet */
340 /**
341 * Returns the number of 4-component registers needed to store a piece
342 * of GL state. For matrices this may be as many as 4 registers,
343 * everything else needs
344 * just 1 register.
345 */
346 static GLuint
347 sizeof_state_reference(const GLint *stateTokens)
348 {
349 if (stateTokens[0] == STATE_MATRIX) {
350 GLuint rows = stateTokens[4] - stateTokens[3] + 1;
351 assert(rows >= 1);
352 assert(rows <= 4);
353 return rows;
354 }
355 else {
356 return 1;
357 }
358 }
359 #endif
360
361
362 /**
363 * Add a new state reference to the parameter list.
364 * This will be used when the program contains something like this:
365 * PARAM ambient = state.material.front.ambient;
366 *
367 * \param paramList the parameter list
368 * \param state an array of 6 (STATE_LENGTH) state tokens
369 * \return index of the new parameter.
370 */
371 GLint
372 _mesa_add_state_reference(struct gl_program_parameter_list *paramList,
373 const GLint stateTokens[STATE_LENGTH])
374 {
375 const GLuint size = 4; /* XXX fix */
376 const char *name;
377 GLint index;
378
379 /* Check if the state reference is already in the list */
380 for (index = 0; index < (GLint) paramList->NumParameters; index++) {
381 GLuint i, match = 0;
382 for (i = 0; i < 6; i++) {
383 if (paramList->Parameters[index].StateIndexes[i] == stateTokens[i]) {
384 match++;
385 }
386 else {
387 break;
388 }
389 }
390 if (match == STATE_LENGTH) {
391 /* this state reference is already in the parameter list */
392 return index;
393 }
394 }
395
396 name = _mesa_program_state_string(stateTokens);
397 index = _mesa_add_parameter(paramList, PROGRAM_STATE_VAR, name,
398 size, NULL, (gl_state_index *) stateTokens);
399 paramList->StateFlags |= _mesa_program_state_flags(stateTokens);
400
401 /* free name string here since we duplicated it in add_parameter() */
402 _mesa_free((void *) name);
403
404 return index;
405 }
406
407
408 /**
409 * Lookup a parameter value by name in the given parameter list.
410 * \return pointer to the float[4] values.
411 */
412 GLfloat *
413 _mesa_lookup_parameter_value(const struct gl_program_parameter_list *paramList,
414 GLsizei nameLen, const char *name)
415 {
416 GLuint i = _mesa_lookup_parameter_index(paramList, nameLen, name);
417 if (i < 0)
418 return NULL;
419 else
420 return paramList->ParameterValues[i];
421 }
422
423
424 /**
425 * Given a program parameter name, find its position in the list of parameters.
426 * \param paramList the parameter list to search
427 * \param nameLen length of name (in chars).
428 * If length is negative, assume that name is null-terminated.
429 * \param name the name to search for
430 * \return index of parameter in the list.
431 */
432 GLint
433 _mesa_lookup_parameter_index(const struct gl_program_parameter_list *paramList,
434 GLsizei nameLen, const char *name)
435 {
436 GLint i;
437
438 if (!paramList)
439 return -1;
440
441 if (nameLen == -1) {
442 /* name is null-terminated */
443 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
444 if (paramList->Parameters[i].Name &&
445 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
446 return i;
447 }
448 }
449 else {
450 /* name is not null-terminated, use nameLen */
451 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
452 if (paramList->Parameters[i].Name &&
453 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
454 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
455 return i;
456 }
457 }
458 return -1;
459 }
460
461
462 /**
463 * Look for a float vector in the given parameter list. The float vector
464 * may be of length 1, 2, 3 or 4.
465 * \param list the parameter list to search
466 * \param v the float vector to search for
467 * \param size number of element in v
468 * \param posOut returns the position of the constant, if found
469 * \param swizzleOut returns a swizzle mask describing location of the
470 * vector elements if found
471 * \return GL_TRUE if found, GL_FALSE if not found
472 */
473 GLboolean
474 _mesa_lookup_parameter_constant(const struct gl_program_parameter_list *list,
475 const GLfloat v[], GLuint vSize,
476 GLint *posOut, GLuint *swizzleOut)
477 {
478 GLuint i;
479
480 assert(vSize >= 1);
481 assert(vSize <= 4);
482
483 if (!list)
484 return -1;
485
486 for (i = 0; i < list->NumParameters; i++) {
487 if (list->Parameters[i].Type == PROGRAM_CONSTANT) {
488 if (vSize == 1) {
489 /* look for v[0] anywhere within float[4] value */
490 GLuint j;
491 for (j = 0; j < 4; j++) {
492 if (list->ParameterValues[i][j] == v[0]) {
493 /* found it */
494 *posOut = i;
495 *swizzleOut = MAKE_SWIZZLE4(j, j, j, j);
496 return GL_TRUE;
497 }
498 }
499 }
500 else if (vSize <= list->Parameters[i].Size) {
501 /* see if we can match this constant (with a swizzle) */
502 GLuint swz[4];
503 GLuint match = 0, j, k;
504 for (j = 0; j < vSize; j++) {
505 if (v[j] == list->ParameterValues[i][j]) {
506 swz[j] = j;
507 match++;
508 }
509 else {
510 for (k = 0; k < list->Parameters[i].Size; k++) {
511 if (v[j] == list->ParameterValues[i][k]) {
512 swz[j] = k;
513 match++;
514 break;
515 }
516 }
517 }
518 }
519 if (match == vSize) {
520 *posOut = i;
521 *swizzleOut = MAKE_SWIZZLE4(swz[0], swz[1], swz[2], swz[3]);
522 return GL_TRUE;
523 }
524 }
525 }
526 }
527
528 *posOut = -1;
529 return GL_FALSE;
530 }
531
532
533 struct gl_program_parameter_list *
534 _mesa_clone_parameter_list(const struct gl_program_parameter_list *list)
535 {
536 struct gl_program_parameter_list *clone;
537 GLuint i;
538
539 clone = _mesa_new_parameter_list();
540 if (!clone)
541 return NULL;
542
543 /** Not too efficient, but correct */
544 for (i = 0; i < list->NumParameters; i++) {
545 struct gl_program_parameter *p = list->Parameters + i;
546 GLuint size = MIN2(p->Size, 4);
547 GLint j = _mesa_add_parameter(clone, p->Type, p->Name,
548 size, list->ParameterValues[i], NULL);
549 ASSERT(j >= 0);
550 /* copy state indexes */
551 if (p->Type == PROGRAM_STATE_VAR) {
552 GLint k;
553 struct gl_program_parameter *q = clone->Parameters + j;
554 for (k = 0; k < STATE_LENGTH; k++) {
555 q->StateIndexes[k] = p->StateIndexes[k];
556 }
557 }
558 else {
559 clone->Parameters[j].Size = p->Size;
560 }
561 }
562
563 return clone;
564 }
565
566
567 /**
568 * Find longest name of any parameter in list.
569 */
570 GLuint
571 _mesa_parameter_longest_name(const struct gl_program_parameter_list *list)
572 {
573 GLuint i, maxLen = 0;
574 if (!list)
575 return 0;
576 for (i = 0; i < list->NumParameters; i++) {
577 GLuint len = _mesa_strlen(list->Parameters[i].Name);
578 if (len > maxLen)
579 maxLen = len;
580 }
581 return maxLen;
582 }
583