blob: d09cc65937684fb7eabf0f729b7225ee53420334 [file] [log] [blame]
Brian00cdc0a2006-12-14 15:01:06 -07001/*
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"
Brian00cdc0a2006-12-14 15:01:06 -070035#include "prog_instruction.h"
36#include "prog_parameter.h"
37#include "prog_statevars.h"
38
39
40struct 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 */
51void
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.
Brian3a8e2772006-12-20 17:19:16 -070069 * 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 *
Brian00cdc0a2006-12-14 15:01:06 -070073 * \param paramList the list to add the parameter to
74 * \param name the parameter name, will be duplicated/copied!
75 * \param values initial parameter value, up to 4 GLfloats
Brian3a8e2772006-12-20 17:19:16 -070076 * \param size number of elements in 'values' vector (1..4, or more)
Brian00cdc0a2006-12-14 15:01:06 -070077 * \param type type of parameter, such as
78 * \return index of new parameter in the list, or -1 if error (out of mem)
79 */
80GLint
81_mesa_add_parameter(struct gl_program_parameter_list *paramList,
Brian3a8e2772006-12-20 17:19:16 -070082 const char *name, const GLfloat *values, GLuint size,
Brian00cdc0a2006-12-14 15:01:06 -070083 enum register_file type)
84{
Brian3a8e2772006-12-20 17:19:16 -070085 const GLuint oldNum = paramList->NumParameters;
86 const GLuint sz4 = (size + 3) / 4; /* no. of new param slots needed */
Brian00cdc0a2006-12-14 15:01:06 -070087
Brian3a8e2772006-12-20 17:19:16 -070088 assert(size > 0);
89 assert(size <= 16); /* XXX anything larger than a matrix??? */
90
91 if (oldNum + sz4 > paramList->Size) {
92 /* Need to grow the parameter list array (alloc some extra) */
93 paramList->Size = paramList->Size + 4 * sz4;
Brian00cdc0a2006-12-14 15:01:06 -070094
95 /* realloc arrays */
96 paramList->Parameters = (struct gl_program_parameter *)
97 _mesa_realloc(paramList->Parameters,
Brian3a8e2772006-12-20 17:19:16 -070098 oldNum * sizeof(struct gl_program_parameter),
Brian00cdc0a2006-12-14 15:01:06 -070099 paramList->Size * sizeof(struct gl_program_parameter));
100
101 paramList->ParameterValues = (GLfloat (*)[4])
102 _mesa_align_realloc(paramList->ParameterValues, /* old buf */
Brian3a8e2772006-12-20 17:19:16 -0700103 oldNum * 4 * sizeof(GLfloat), /* old size */
Brian00cdc0a2006-12-14 15:01:06 -0700104 paramList->Size * 4 *sizeof(GLfloat), /* new sz */
105 16);
106 }
107
108 if (!paramList->Parameters ||
109 !paramList->ParameterValues) {
110 /* out of memory */
111 paramList->NumParameters = 0;
112 paramList->Size = 0;
113 return -1;
114 }
115 else {
Brian3a8e2772006-12-20 17:19:16 -0700116 GLuint i;
Brian00cdc0a2006-12-14 15:01:06 -0700117
Brian3a8e2772006-12-20 17:19:16 -0700118 paramList->NumParameters = oldNum + sz4;
Brian00cdc0a2006-12-14 15:01:06 -0700119
Brian3a8e2772006-12-20 17:19:16 -0700120 _mesa_memset(&paramList->Parameters[oldNum], 0,
121 sz4 * sizeof(struct gl_program_parameter));
122
123 for (i = 0; i < sz4; i++) {
124 struct gl_program_parameter *p = paramList->Parameters + oldNum + i;
125 p->Name = name ? _mesa_strdup(name) : NULL;
126 p->Type = type;
127 p->Size = size;
128 if (values) {
129 COPY_4V(paramList->ParameterValues[oldNum + i], values);
130 values += 4;
131 }
132 size -= 4;
133 }
134 return (GLint) oldNum;
Brian00cdc0a2006-12-14 15:01:06 -0700135 }
136}
137
138
139/**
140 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
141 * \return index of the new entry in the parameter list
142 */
143GLint
144_mesa_add_named_parameter(struct gl_program_parameter_list *paramList,
145 const char *name, const GLfloat values[4])
146{
147 return _mesa_add_parameter(paramList, name, values, 4, PROGRAM_NAMED_PARAM);
148}
149
150
151/**
152 * Add a new named constant to the parameter list.
153 * This will be used when the program contains something like this:
154 * PARAM myVals = { 0, 1, 2, 3 };
155 *
156 * \param paramList the parameter list
157 * \param name the name for the constant
158 * \param values four float values
159 * \return index/position of the new parameter in the parameter list
160 */
161GLint
162_mesa_add_named_constant(struct gl_program_parameter_list *paramList,
163 const char *name, const GLfloat values[4],
164 GLuint size)
165{
166#if 0 /* disable this for now -- we need to save the name! */
167 GLint pos;
168 GLuint swizzle;
169 ASSERT(size == 4); /* XXX future feature */
170 /* check if we already have this constant */
171 if (_mesa_lookup_parameter_constant(paramList, values, 4, &pos, &swizzle)) {
172 return pos;
173 }
174#endif
175 size = 4; /** XXX fix */
176 return _mesa_add_parameter(paramList, name, values, size, PROGRAM_CONSTANT);
177}
178
179
180/**
181 * Add a new unnamed constant to the parameter list.
182 * This will be used when the program contains something like this:
183 * MOV r, { 0, 1, 2, 3 };
184 *
185 * \param paramList the parameter list
186 * \param values four float values
187 * \param swizzleOut returns swizzle mask for accessing the constant
188 * \return index/position of the new parameter in the parameter list.
189 */
190GLint
191_mesa_add_unnamed_constant(struct gl_program_parameter_list *paramList,
192 const GLfloat values[4], GLuint size,
193 GLuint *swizzleOut)
194{
195 GLint pos;
196 GLuint swizzle;
197 ASSERT(size >= 1);
198 ASSERT(size <= 4);
199 size = 4; /* XXX temporary */
200 /* check if we already have this constant */
201 if (_mesa_lookup_parameter_constant(paramList, values,
202 size, &pos, &swizzle)) {
203 return pos;
204 }
205 return _mesa_add_parameter(paramList, NULL, values, size, PROGRAM_CONSTANT);
206}
207
208
209GLint
210_mesa_add_uniform(struct gl_program_parameter_list *paramList,
211 const char *name, GLuint size)
212{
213 GLint i = _mesa_lookup_parameter_index(paramList, -1, name);
214 if (i >= 0 && paramList->Parameters[i].Type == PROGRAM_UNIFORM) {
215 /* already in list */
216 return i;
217 }
218 else {
Brian00cdc0a2006-12-14 15:01:06 -0700219 i = _mesa_add_parameter(paramList, name, NULL, size, PROGRAM_UNIFORM);
220 return i;
221 }
222}
223
224
225GLint
Brian9805e762007-01-05 16:00:57 -0700226_mesa_add_sampler(struct gl_program_parameter_list *paramList,
227 const char *name)
228{
229 GLint i = _mesa_lookup_parameter_index(paramList, -1, name);
230 if (i >= 0 && paramList->Parameters[i].Type == PROGRAM_SAMPLER) {
231 /* already in list */
232 return i;
233 }
234 else {
235 const GLint size = 1;
236 i = _mesa_add_parameter(paramList, name, NULL, size, PROGRAM_SAMPLER);
237 return i;
238 }
239}
240
241
242GLint
Brian00cdc0a2006-12-14 15:01:06 -0700243_mesa_add_varying(struct gl_program_parameter_list *paramList,
244 const char *name, GLuint size)
245{
246 GLint i = _mesa_lookup_parameter_index(paramList, -1, name);
247 if (i >= 0 && paramList->Parameters[i].Type == PROGRAM_VARYING) {
248 /* already in list */
249 return i;
250 }
251 else {
252 assert(size == 4);
253 i = _mesa_add_parameter(paramList, name, NULL, size, PROGRAM_VARYING);
254 return i;
255 }
256}
257
258
259
260
261#if 0 /* not used yet */
262/**
263 * Returns the number of 4-component registers needed to store a piece
264 * of GL state. For matrices this may be as many as 4 registers,
265 * everything else needs
266 * just 1 register.
267 */
268static GLuint
269sizeof_state_reference(const GLint *stateTokens)
270{
271 if (stateTokens[0] == STATE_MATRIX) {
272 GLuint rows = stateTokens[4] - stateTokens[3] + 1;
273 assert(rows >= 1);
274 assert(rows <= 4);
275 return rows;
276 }
277 else {
278 return 1;
279 }
280}
281#endif
282
283
284/**
285 * Add a new state reference to the parameter list.
286 * This will be used when the program contains something like this:
287 * PARAM ambient = state.material.front.ambient;
288 *
289 * \param paramList the parameter list
290 * \param state an array of 6 state tokens
291 * \return index of the new parameter.
292 */
293GLint
294_mesa_add_state_reference(struct gl_program_parameter_list *paramList,
295 const GLint *stateTokens)
296{
297 const GLuint size = 4; /* XXX fix */
298 const char *name;
299 GLint index;
300
301 /* Check if the state reference is already in the list */
302 for (index = 0; index < paramList->NumParameters; index++) {
303 GLuint i, match = 0;
304 for (i = 0; i < 6; i++) {
305 if (paramList->Parameters[index].StateIndexes[i] == stateTokens[i]) {
306 match++;
307 }
308 else {
309 break;
310 }
311 }
312 if (match == 6) {
313 /* this state reference is already in the parameter list */
314 return index;
315 }
316 }
317
318 name = _mesa_program_state_string(stateTokens);
319 index = _mesa_add_parameter(paramList, name, NULL, size, PROGRAM_STATE_VAR);
320 if (index >= 0) {
321 GLuint i;
322 for (i = 0; i < 6; i++) {
323 paramList->Parameters[index].StateIndexes[i]
324 = (gl_state_index) stateTokens[i];
325 }
326 paramList->StateFlags |= _mesa_program_state_flags(stateTokens);
327 }
328
329 /* free name string here since we duplicated it in add_parameter() */
330 _mesa_free((void *) name);
331
332 return index;
333}
334
335
336/**
337 * Lookup a parameter value by name in the given parameter list.
338 * \return pointer to the float[4] values.
339 */
340GLfloat *
341_mesa_lookup_parameter_value(const struct gl_program_parameter_list *paramList,
342 GLsizei nameLen, const char *name)
343{
344 GLuint i = _mesa_lookup_parameter_index(paramList, nameLen, name);
345 if (i < 0)
346 return NULL;
347 else
348 return paramList->ParameterValues[i];
349}
350
351
352/**
353 * Given a program parameter name, find its position in the list of parameters.
354 * \param paramList the parameter list to search
355 * \param nameLen length of name (in chars).
356 * If length is negative, assume that name is null-terminated.
357 * \param name the name to search for
358 * \return index of parameter in the list.
359 */
360GLint
361_mesa_lookup_parameter_index(const struct gl_program_parameter_list *paramList,
362 GLsizei nameLen, const char *name)
363{
364 GLint i;
365
366 if (!paramList)
367 return -1;
368
369 if (nameLen == -1) {
370 /* name is null-terminated */
371 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
372 if (paramList->Parameters[i].Name &&
373 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
374 return i;
375 }
376 }
377 else {
378 /* name is not null-terminated, use nameLen */
379 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
380 if (paramList->Parameters[i].Name &&
381 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
382 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
383 return i;
384 }
385 }
386 return -1;
387}
388
389
390/**
391 * Look for a float vector in the given parameter list. The float vector
392 * may be of length 1, 2, 3 or 4.
393 * \param paramList the parameter list to search
394 * \param v the float vector to search for
395 * \param size number of element in v
396 * \param posOut returns the position of the constant, if found
397 * \param swizzleOut returns a swizzle mask describing location of the
398 * vector elements if found
399 * \return GL_TRUE if found, GL_FALSE if not found
400 */
401GLboolean
402_mesa_lookup_parameter_constant(const struct gl_program_parameter_list *paramList,
403 const GLfloat v[], GLsizei vSize,
404 GLint *posOut, GLuint *swizzleOut)
405{
406 GLuint i;
407
408 assert(vSize >= 1);
409 assert(vSize <= 4);
410
411 if (!paramList)
412 return -1;
413
414 for (i = 0; i < paramList->NumParameters; i++) {
415 if (paramList->Parameters[i].Type == PROGRAM_CONSTANT) {
416 const GLint maxShift = 4 - vSize;
417 GLint shift, j;
418 for (shift = 0; shift <= maxShift; shift++) {
419 GLint matched = 0;
420 GLuint swizzle[4];
421 swizzle[0] = swizzle[1] = swizzle[2] = swizzle[3] = 0;
422 /* XXX we could do out-of-order swizzle matches too, someday */
423 for (j = 0; j < vSize; j++) {
424 assert(shift + j < 4);
425 if (paramList->ParameterValues[i][shift + j] == v[j]) {
426 matched++;
427 swizzle[j] = shift + j;
428 ASSERT(swizzle[j] >= SWIZZLE_X);
429 ASSERT(swizzle[j] <= SWIZZLE_W);
430 }
431 }
432 if (matched == vSize) {
433 /* found! */
434 *posOut = i;
435 *swizzleOut = MAKE_SWIZZLE4(swizzle[0], swizzle[1],
436 swizzle[2], swizzle[3]);
437 return GL_TRUE;
438 }
439 }
440 }
441 }
442
443 *posOut = -1;
444 return GL_FALSE;
445}
446
447
448struct gl_program_parameter_list *
449_mesa_clone_parameter_list(const struct gl_program_parameter_list *list)
450{
451 struct gl_program_parameter_list *clone;
452 GLuint i;
453
454 clone = _mesa_new_parameter_list();
455 if (!clone)
456 return NULL;
457
458 /** Not too efficient, but correct */
459 for (i = 0; i < list->NumParameters; i++) {
460 struct gl_program_parameter *p = list->Parameters + i;
Brian3a8e2772006-12-20 17:19:16 -0700461 GLuint size = MIN2(p->Size, 4);
Brian00cdc0a2006-12-14 15:01:06 -0700462 GLint j = _mesa_add_parameter(clone, p->Name, list->ParameterValues[i],
Brian3a8e2772006-12-20 17:19:16 -0700463 size, p->Type);
Brian00cdc0a2006-12-14 15:01:06 -0700464 ASSERT(j >= 0);
465 /* copy state indexes */
466 if (p->Type == PROGRAM_STATE_VAR) {
467 GLint k;
468 struct gl_program_parameter *q = clone->Parameters + j;
469 for (k = 0; k < 6; k++) {
470 q->StateIndexes[k] = p->StateIndexes[k];
471 }
472 }
Brian3a8e2772006-12-20 17:19:16 -0700473 else {
474 clone->Parameters[j].Size = p->Size;
475 }
Brian00cdc0a2006-12-14 15:01:06 -0700476 }
477
478 return clone;
479}
Brian5b01c5e2006-12-19 18:02:03 -0700480
481
482/**
483 * Find longest name of any parameter in list.
484 */
485GLuint
486_mesa_parameter_longest_name(const struct gl_program_parameter_list *list)
487{
488 GLuint i, maxLen = 0;
489 if (!list)
490 return 0;
491 for (i = 0; i < list->NumParameters; i++) {
492 GLuint len = _mesa_strlen(list->Parameters[i].Name);
493 if (len > maxLen)
494 maxLen = len;
495 }
496 return maxLen;
497}
498