Overhaul of GLSL API functions, dispatching, etc.
[mesa.git] / src / mesa / shader / prog_parameter.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
4 *
5 * Copyright (C) 1999-2006 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 * \param paramList the list to add the parameter to
70 * \param name the parameter name, will be duplicated/copied!
71 * \param values initial parameter value, up to 4 GLfloats
72 * \param size number of elements in 'values' vector (1..4)
73 * \param type type of parameter, such as
74 * \return index of new parameter in the list, or -1 if error (out of mem)
75 */
76 GLint
77 _mesa_add_parameter(struct gl_program_parameter_list *paramList,
78 const char *name, const GLfloat values[4], GLuint size,
79 enum register_file type)
80 {
81 const GLuint n = paramList->NumParameters;
82
83 if (n == paramList->Size) {
84 /* Need to grow the parameter list array */
85 if (paramList->Size == 0)
86 paramList->Size = 8;
87 else
88 paramList->Size *= 2;
89
90 /* realloc arrays */
91 paramList->Parameters = (struct gl_program_parameter *)
92 _mesa_realloc(paramList->Parameters,
93 n * sizeof(struct gl_program_parameter),
94 paramList->Size * sizeof(struct gl_program_parameter));
95
96 paramList->ParameterValues = (GLfloat (*)[4])
97 _mesa_align_realloc(paramList->ParameterValues, /* old buf */
98 n * 4 * sizeof(GLfloat), /* old size */
99 paramList->Size * 4 *sizeof(GLfloat), /* new sz */
100 16);
101 }
102
103 if (!paramList->Parameters ||
104 !paramList->ParameterValues) {
105 /* out of memory */
106 paramList->NumParameters = 0;
107 paramList->Size = 0;
108 return -1;
109 }
110 else {
111 paramList->NumParameters = n + 1;
112
113 _mesa_memset(&paramList->Parameters[n], 0,
114 sizeof(struct gl_program_parameter));
115
116 paramList->Parameters[n].Name = name ? _mesa_strdup(name) : NULL;
117 paramList->Parameters[n].Type = type;
118 paramList->Parameters[n].Size = size;
119 if (values)
120 COPY_4V(paramList->ParameterValues[n], values);
121 return (GLint) n;
122 }
123 }
124
125
126 /**
127 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
128 * \return index of the new entry in the parameter list
129 */
130 GLint
131 _mesa_add_named_parameter(struct gl_program_parameter_list *paramList,
132 const char *name, const GLfloat values[4])
133 {
134 return _mesa_add_parameter(paramList, name, values, 4, PROGRAM_NAMED_PARAM);
135 }
136
137
138 /**
139 * Add a new named constant to the parameter list.
140 * This will be used when the program contains something like this:
141 * PARAM myVals = { 0, 1, 2, 3 };
142 *
143 * \param paramList the parameter list
144 * \param name the name for the constant
145 * \param values four float values
146 * \return index/position of the new parameter in the parameter list
147 */
148 GLint
149 _mesa_add_named_constant(struct gl_program_parameter_list *paramList,
150 const char *name, const GLfloat values[4],
151 GLuint size)
152 {
153 #if 0 /* disable this for now -- we need to save the name! */
154 GLint pos;
155 GLuint swizzle;
156 ASSERT(size == 4); /* XXX future feature */
157 /* check if we already have this constant */
158 if (_mesa_lookup_parameter_constant(paramList, values, 4, &pos, &swizzle)) {
159 return pos;
160 }
161 #endif
162 size = 4; /** XXX fix */
163 return _mesa_add_parameter(paramList, name, values, size, PROGRAM_CONSTANT);
164 }
165
166
167 /**
168 * Add a new unnamed constant to the parameter list.
169 * This will be used when the program contains something like this:
170 * MOV r, { 0, 1, 2, 3 };
171 *
172 * \param paramList the parameter list
173 * \param values four float values
174 * \param swizzleOut returns swizzle mask for accessing the constant
175 * \return index/position of the new parameter in the parameter list.
176 */
177 GLint
178 _mesa_add_unnamed_constant(struct gl_program_parameter_list *paramList,
179 const GLfloat values[4], GLuint size,
180 GLuint *swizzleOut)
181 {
182 GLint pos;
183 GLuint swizzle;
184 ASSERT(size >= 1);
185 ASSERT(size <= 4);
186 size = 4; /* XXX temporary */
187 /* check if we already have this constant */
188 if (_mesa_lookup_parameter_constant(paramList, values,
189 size, &pos, &swizzle)) {
190 return pos;
191 }
192 return _mesa_add_parameter(paramList, NULL, values, size, PROGRAM_CONSTANT);
193 }
194
195
196 GLint
197 _mesa_add_uniform(struct gl_program_parameter_list *paramList,
198 const char *name, GLuint size)
199 {
200 GLint i = _mesa_lookup_parameter_index(paramList, -1, name);
201 if (i >= 0 && paramList->Parameters[i].Type == PROGRAM_UNIFORM) {
202 /* already in list */
203 return i;
204 }
205 else {
206 assert(size == 4);
207 i = _mesa_add_parameter(paramList, name, NULL, size, PROGRAM_UNIFORM);
208 return i;
209 }
210 }
211
212
213 GLint
214 _mesa_add_varying(struct gl_program_parameter_list *paramList,
215 const char *name, GLuint size)
216 {
217 GLint i = _mesa_lookup_parameter_index(paramList, -1, name);
218 if (i >= 0 && paramList->Parameters[i].Type == PROGRAM_VARYING) {
219 /* already in list */
220 return i;
221 }
222 else {
223 assert(size == 4);
224 i = _mesa_add_parameter(paramList, name, NULL, size, PROGRAM_VARYING);
225 return i;
226 }
227 }
228
229
230
231
232 #if 0 /* not used yet */
233 /**
234 * Returns the number of 4-component registers needed to store a piece
235 * of GL state. For matrices this may be as many as 4 registers,
236 * everything else needs
237 * just 1 register.
238 */
239 static GLuint
240 sizeof_state_reference(const GLint *stateTokens)
241 {
242 if (stateTokens[0] == STATE_MATRIX) {
243 GLuint rows = stateTokens[4] - stateTokens[3] + 1;
244 assert(rows >= 1);
245 assert(rows <= 4);
246 return rows;
247 }
248 else {
249 return 1;
250 }
251 }
252 #endif
253
254
255 /**
256 * Add a new state reference to the parameter list.
257 * This will be used when the program contains something like this:
258 * PARAM ambient = state.material.front.ambient;
259 *
260 * \param paramList the parameter list
261 * \param state an array of 6 state tokens
262 * \return index of the new parameter.
263 */
264 GLint
265 _mesa_add_state_reference(struct gl_program_parameter_list *paramList,
266 const GLint *stateTokens)
267 {
268 const GLuint size = 4; /* XXX fix */
269 const char *name;
270 GLint index;
271
272 /* Check if the state reference is already in the list */
273 for (index = 0; index < paramList->NumParameters; index++) {
274 GLuint i, match = 0;
275 for (i = 0; i < 6; i++) {
276 if (paramList->Parameters[index].StateIndexes[i] == stateTokens[i]) {
277 match++;
278 }
279 else {
280 break;
281 }
282 }
283 if (match == 6) {
284 /* this state reference is already in the parameter list */
285 return index;
286 }
287 }
288
289 name = _mesa_program_state_string(stateTokens);
290 index = _mesa_add_parameter(paramList, name, NULL, size, PROGRAM_STATE_VAR);
291 if (index >= 0) {
292 GLuint i;
293 for (i = 0; i < 6; i++) {
294 paramList->Parameters[index].StateIndexes[i]
295 = (gl_state_index) stateTokens[i];
296 }
297 paramList->StateFlags |= _mesa_program_state_flags(stateTokens);
298 }
299
300 /* free name string here since we duplicated it in add_parameter() */
301 _mesa_free((void *) name);
302
303 return index;
304 }
305
306
307 /**
308 * Lookup a parameter value by name in the given parameter list.
309 * \return pointer to the float[4] values.
310 */
311 GLfloat *
312 _mesa_lookup_parameter_value(const struct gl_program_parameter_list *paramList,
313 GLsizei nameLen, const char *name)
314 {
315 GLuint i = _mesa_lookup_parameter_index(paramList, nameLen, name);
316 if (i < 0)
317 return NULL;
318 else
319 return paramList->ParameterValues[i];
320 }
321
322
323 /**
324 * Given a program parameter name, find its position in the list of parameters.
325 * \param paramList the parameter list to search
326 * \param nameLen length of name (in chars).
327 * If length is negative, assume that name is null-terminated.
328 * \param name the name to search for
329 * \return index of parameter in the list.
330 */
331 GLint
332 _mesa_lookup_parameter_index(const struct gl_program_parameter_list *paramList,
333 GLsizei nameLen, const char *name)
334 {
335 GLint i;
336
337 if (!paramList)
338 return -1;
339
340 if (nameLen == -1) {
341 /* name is null-terminated */
342 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
343 if (paramList->Parameters[i].Name &&
344 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
345 return i;
346 }
347 }
348 else {
349 /* name is not null-terminated, use nameLen */
350 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
351 if (paramList->Parameters[i].Name &&
352 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
353 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
354 return i;
355 }
356 }
357 return -1;
358 }
359
360
361 /**
362 * Look for a float vector in the given parameter list. The float vector
363 * may be of length 1, 2, 3 or 4.
364 * \param paramList the parameter list to search
365 * \param v the float vector to search for
366 * \param size number of element in v
367 * \param posOut returns the position of the constant, if found
368 * \param swizzleOut returns a swizzle mask describing location of the
369 * vector elements if found
370 * \return GL_TRUE if found, GL_FALSE if not found
371 */
372 GLboolean
373 _mesa_lookup_parameter_constant(const struct gl_program_parameter_list *paramList,
374 const GLfloat v[], GLsizei vSize,
375 GLint *posOut, GLuint *swizzleOut)
376 {
377 GLuint i;
378
379 assert(vSize >= 1);
380 assert(vSize <= 4);
381
382 if (!paramList)
383 return -1;
384
385 for (i = 0; i < paramList->NumParameters; i++) {
386 if (paramList->Parameters[i].Type == PROGRAM_CONSTANT) {
387 const GLint maxShift = 4 - vSize;
388 GLint shift, j;
389 for (shift = 0; shift <= maxShift; shift++) {
390 GLint matched = 0;
391 GLuint swizzle[4];
392 swizzle[0] = swizzle[1] = swizzle[2] = swizzle[3] = 0;
393 /* XXX we could do out-of-order swizzle matches too, someday */
394 for (j = 0; j < vSize; j++) {
395 assert(shift + j < 4);
396 if (paramList->ParameterValues[i][shift + j] == v[j]) {
397 matched++;
398 swizzle[j] = shift + j;
399 ASSERT(swizzle[j] >= SWIZZLE_X);
400 ASSERT(swizzle[j] <= SWIZZLE_W);
401 }
402 }
403 if (matched == vSize) {
404 /* found! */
405 *posOut = i;
406 *swizzleOut = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
407 swizzle[2], swizzle[3]);
408 return GL_TRUE;
409 }
410 }
411 }
412 }
413
414 *posOut = -1;
415 return GL_FALSE;
416 }
417
418
419 struct gl_program_parameter_list *
420 _mesa_clone_parameter_list(const struct gl_program_parameter_list *list)
421 {
422 struct gl_program_parameter_list *clone;
423 GLuint i;
424
425 clone = _mesa_new_parameter_list();
426 if (!clone)
427 return NULL;
428
429 /** Not too efficient, but correct */
430 for (i = 0; i < list->NumParameters; i++) {
431 struct gl_program_parameter *p = list->Parameters + i;
432 GLint j = _mesa_add_parameter(clone, p->Name, list->ParameterValues[i],
433 p->Size, p->Type);
434 ASSERT(j >= 0);
435 /* copy state indexes */
436 if (p->Type == PROGRAM_STATE_VAR) {
437 GLint k;
438 struct gl_program_parameter *q = clone->Parameters + j;
439 for (k = 0; k < 6; k++) {
440 q->StateIndexes[k] = p->StateIndexes[k];
441 }
442 }
443 }
444
445 return clone;
446 }
447
448
449 /**
450 * Find longest name of any parameter in list.
451 */
452 GLuint
453 _mesa_parameter_longest_name(const struct gl_program_parameter_list *list)
454 {
455 GLuint i, maxLen = 0;
456 if (!list)
457 return 0;
458 for (i = 0; i < list->NumParameters; i++) {
459 GLuint len = _mesa_strlen(list->Parameters[i].Name);
460 if (len > maxLen)
461 maxLen = len;
462 }
463 return maxLen;
464 }
465