blob: 6b9e73b2cbdf4ee4fba5caa03ca79614c79e3a8d [file] [log] [blame]
Brian00cdc0a2006-12-14 15:01:06 -07001/*
2 * Mesa 3-D graphics library
Brian Paul84c18502008-11-24 08:33:49 -07003 * Version: 7.3
Brian00cdc0a2006-12-14 15:01:06 -07004 *
Brian Paul84c18502008-11-24 08:33:49 -07005 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
Brian00cdc0a2006-12-14 15:01:06 -07006 *
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
José Fonseca101d1a62008-07-23 21:06:01 +090032#include "main/glheader.h"
33#include "main/imports.h"
34#include "main/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{
Brian Paul26988c12008-05-14 16:05:48 -060043 return CALLOC_STRUCT(gl_program_parameter_list);
Brian00cdc0a2006-12-14 15:01:06 -070044}
45
46
Ian Romanick480567d2009-07-08 11:39:13 -070047struct gl_program_parameter_list *
48_mesa_new_parameter_list_sized(unsigned size)
49{
50 struct gl_program_parameter_list *p = _mesa_new_parameter_list();
51
52 if ((p != NULL) && (size != 0)) {
53 p->Size = size;
54
55 /* alloc arrays */
56 p->Parameters = (struct gl_program_parameter *)
57 _mesa_calloc(size * sizeof(struct gl_program_parameter));
58
59 p->ParameterValues = (GLfloat (*)[4])
60 _mesa_align_malloc(size * 4 *sizeof(GLfloat), 16);
61
62
63 if ((p->Parameters == NULL) || (p->ParameterValues == NULL)) {
64 _mesa_free(p->Parameters);
65 _mesa_align_free(p->ParameterValues);
66 _mesa_free(p);
67 p = NULL;
68 }
69 }
70
71 return p;
72}
73
74
Brian00cdc0a2006-12-14 15:01:06 -070075/**
76 * Free a parameter list and all its parameters
77 */
78void
79_mesa_free_parameter_list(struct gl_program_parameter_list *paramList)
80{
81 GLuint i;
82 for (i = 0; i < paramList->NumParameters; i++) {
83 if (paramList->Parameters[i].Name)
84 _mesa_free((void *) paramList->Parameters[i].Name);
85 }
86 _mesa_free(paramList->Parameters);
87 if (paramList->ParameterValues)
88 _mesa_align_free(paramList->ParameterValues);
89 _mesa_free(paramList);
90}
91
92
Brian00cdc0a2006-12-14 15:01:06 -070093/**
94 * Add a new parameter to a parameter list.
Brian3a8e2772006-12-20 17:19:16 -070095 * Note that parameter values are usually 4-element GLfloat vectors.
96 * When size > 4 we'll allocate a sequential block of parameters to
97 * store all the values (in blocks of 4).
98 *
Brian00cdc0a2006-12-14 15:01:06 -070099 * \param paramList the list to add the parameter to
Brian00cdc0a2006-12-14 15:01:06 -0700100 * \param type type of parameter, such as
Brianb7978af2007-01-09 19:17:17 -0700101 * \param name the parameter name, will be duplicated/copied!
102 * \param size number of elements in 'values' vector (1..4, or more)
103 * \param values initial parameter value, up to 4 GLfloats, or NULL
104 * \param state state indexes, or NULL
Brian00cdc0a2006-12-14 15:01:06 -0700105 * \return index of new parameter in the list, or -1 if error (out of mem)
106 */
107GLint
108_mesa_add_parameter(struct gl_program_parameter_list *paramList,
Brian Paulb4026d92009-03-07 12:02:52 -0700109 gl_register_file type, const char *name,
Brianc93e8832007-04-18 16:27:35 -0600110 GLuint size, GLenum datatype, const GLfloat *values,
Brian Paul84c18502008-11-24 08:33:49 -0700111 const gl_state_index state[STATE_LENGTH],
112 GLbitfield flags)
Brian00cdc0a2006-12-14 15:01:06 -0700113{
Brian3a8e2772006-12-20 17:19:16 -0700114 const GLuint oldNum = paramList->NumParameters;
115 const GLuint sz4 = (size + 3) / 4; /* no. of new param slots needed */
Brian00cdc0a2006-12-14 15:01:06 -0700116
Brian3a8e2772006-12-20 17:19:16 -0700117 assert(size > 0);
Brian3a8e2772006-12-20 17:19:16 -0700118
119 if (oldNum + sz4 > paramList->Size) {
120 /* Need to grow the parameter list array (alloc some extra) */
121 paramList->Size = paramList->Size + 4 * sz4;
Brian00cdc0a2006-12-14 15:01:06 -0700122
123 /* realloc arrays */
124 paramList->Parameters = (struct gl_program_parameter *)
125 _mesa_realloc(paramList->Parameters,
Brian3a8e2772006-12-20 17:19:16 -0700126 oldNum * sizeof(struct gl_program_parameter),
Brian00cdc0a2006-12-14 15:01:06 -0700127 paramList->Size * sizeof(struct gl_program_parameter));
128
129 paramList->ParameterValues = (GLfloat (*)[4])
130 _mesa_align_realloc(paramList->ParameterValues, /* old buf */
Brian3a8e2772006-12-20 17:19:16 -0700131 oldNum * 4 * sizeof(GLfloat), /* old size */
Brian00cdc0a2006-12-14 15:01:06 -0700132 paramList->Size * 4 *sizeof(GLfloat), /* new sz */
133 16);
134 }
135
136 if (!paramList->Parameters ||
137 !paramList->ParameterValues) {
138 /* out of memory */
139 paramList->NumParameters = 0;
140 paramList->Size = 0;
141 return -1;
142 }
143 else {
Brian3a8e2772006-12-20 17:19:16 -0700144 GLuint i;
Brian00cdc0a2006-12-14 15:01:06 -0700145
Brian3a8e2772006-12-20 17:19:16 -0700146 paramList->NumParameters = oldNum + sz4;
Brian00cdc0a2006-12-14 15:01:06 -0700147
Brian3a8e2772006-12-20 17:19:16 -0700148 _mesa_memset(&paramList->Parameters[oldNum], 0,
149 sz4 * sizeof(struct gl_program_parameter));
150
151 for (i = 0; i < sz4; i++) {
152 struct gl_program_parameter *p = paramList->Parameters + oldNum + i;
153 p->Name = name ? _mesa_strdup(name) : NULL;
154 p->Type = type;
155 p->Size = size;
Brianc93e8832007-04-18 16:27:35 -0600156 p->DataType = datatype;
Brian Paul84c18502008-11-24 08:33:49 -0700157 p->Flags = flags;
Brian3a8e2772006-12-20 17:19:16 -0700158 if (values) {
159 COPY_4V(paramList->ParameterValues[oldNum + i], values);
160 values += 4;
Brian Paul2d76a0d2008-11-10 12:33:17 -0700161 p->Initialized = GL_TRUE;
Brian3a8e2772006-12-20 17:19:16 -0700162 }
Briancf490a72007-02-09 12:04:52 -0700163 else {
164 /* silence valgrind */
165 ASSIGN_4V(paramList->ParameterValues[oldNum + i], 0, 0, 0, 0);
166 }
Brian3a8e2772006-12-20 17:19:16 -0700167 size -= 4;
168 }
Brianb7978af2007-01-09 19:17:17 -0700169
170 if (state) {
171 for (i = 0; i < STATE_LENGTH; i++)
172 paramList->Parameters[oldNum].StateIndexes[i] = state[i];
173 }
174
Brian3a8e2772006-12-20 17:19:16 -0700175 return (GLint) oldNum;
Brian00cdc0a2006-12-14 15:01:06 -0700176 }
177}
178
179
180/**
181 * Add a new named program parameter (Ex: NV_fragment_program DEFINE statement)
182 * \return index of the new entry in the parameter list
183 */
184GLint
185_mesa_add_named_parameter(struct gl_program_parameter_list *paramList,
186 const char *name, const GLfloat values[4])
187{
Brianb7978af2007-01-09 19:17:17 -0700188 return _mesa_add_parameter(paramList, PROGRAM_NAMED_PARAM, name,
Brian Paul84c18502008-11-24 08:33:49 -0700189 4, GL_NONE, values, NULL, 0x0);
Brianb7978af2007-01-09 19:17:17 -0700190
Brian00cdc0a2006-12-14 15:01:06 -0700191}
192
193
194/**
195 * Add a new named constant to the parameter list.
196 * This will be used when the program contains something like this:
197 * PARAM myVals = { 0, 1, 2, 3 };
198 *
199 * \param paramList the parameter list
200 * \param name the name for the constant
201 * \param values four float values
202 * \return index/position of the new parameter in the parameter list
203 */
204GLint
205_mesa_add_named_constant(struct gl_program_parameter_list *paramList,
206 const char *name, const GLfloat values[4],
207 GLuint size)
208{
Brian Paul80197a02009-04-03 15:42:14 -0600209 /* first check if this is a duplicate constant */
Brian00cdc0a2006-12-14 15:01:06 -0700210 GLint pos;
Brian Paul80197a02009-04-03 15:42:14 -0600211 for (pos = 0; pos < paramList->NumParameters; pos++) {
212 const GLfloat *pvals = paramList->ParameterValues[pos];
213 if (pvals[0] == values[0] &&
214 pvals[1] == values[1] &&
215 pvals[2] == values[2] &&
216 pvals[3] == values[3] &&
217 _mesa_strcmp(paramList->Parameters[pos].Name, name) == 0) {
218 /* Same name and value is already in the param list - reuse it */
219 return pos;
220 }
Brian00cdc0a2006-12-14 15:01:06 -0700221 }
Brian Paul80197a02009-04-03 15:42:14 -0600222 /* not found, add new parameter */
Brianb7978af2007-01-09 19:17:17 -0700223 return _mesa_add_parameter(paramList, PROGRAM_CONSTANT, name,
Brian Paul84c18502008-11-24 08:33:49 -0700224 size, GL_NONE, values, NULL, 0x0);
Brian00cdc0a2006-12-14 15:01:06 -0700225}
226
227
228/**
Brian1bf81e32007-03-22 09:07:27 -0600229 * Add a new unnamed constant to the parameter list. This will be used
230 * when a fragment/vertex program contains something like this:
Brian00cdc0a2006-12-14 15:01:06 -0700231 * MOV r, { 0, 1, 2, 3 };
Brian1bf81e32007-03-22 09:07:27 -0600232 * We'll search the parameter list for an existing instance of the
233 * constant. If swizzleOut is non-null, we'll try swizzling when
234 * looking for a match.
Brian00cdc0a2006-12-14 15:01:06 -0700235 *
236 * \param paramList the parameter list
237 * \param values four float values
238 * \param swizzleOut returns swizzle mask for accessing the constant
239 * \return index/position of the new parameter in the parameter list.
240 */
241GLint
242_mesa_add_unnamed_constant(struct gl_program_parameter_list *paramList,
243 const GLfloat values[4], GLuint size,
244 GLuint *swizzleOut)
245{
246 GLint pos;
Brian00cdc0a2006-12-14 15:01:06 -0700247 ASSERT(size >= 1);
248 ASSERT(size <= 4);
Briance664002007-01-18 17:23:48 -0700249
Brian00cdc0a2006-12-14 15:01:06 -0700250 if (_mesa_lookup_parameter_constant(paramList, values,
Briance664002007-01-18 17:23:48 -0700251 size, &pos, swizzleOut)) {
Brian00cdc0a2006-12-14 15:01:06 -0700252 return pos;
253 }
Briance664002007-01-18 17:23:48 -0700254
255 /* Look for empty space in an already unnamed constant parameter
256 * to add this constant. This will only work for single-element
257 * constants because we rely on smearing (i.e. .yyyy or .zzzz).
258 */
Brian1bf81e32007-03-22 09:07:27 -0600259 if (size == 1 && swizzleOut) {
Brian223d7cb2007-01-23 16:37:51 -0700260 for (pos = 0; pos < (GLint) paramList->NumParameters; pos++) {
Briance664002007-01-18 17:23:48 -0700261 struct gl_program_parameter *p = paramList->Parameters + pos;
262 if (p->Type == PROGRAM_CONSTANT && p->Size + size <= 4) {
263 /* ok, found room */
264 GLfloat *pVal = paramList->ParameterValues[pos];
265 GLuint swz = p->Size; /* 1, 2 or 3 for Y, Z, W */
266 pVal[p->Size] = values[0];
267 p->Size++;
268 *swizzleOut = MAKE_SWIZZLE4(swz, swz, swz, swz);
269 return pos;
270 }
271 }
272 }
273
274 /* add a new parameter to store this constant */
275 pos = _mesa_add_parameter(paramList, PROGRAM_CONSTANT, NULL,
Brian Paul84c18502008-11-24 08:33:49 -0700276 size, GL_NONE, values, NULL, 0x0);
Brian1bf81e32007-03-22 09:07:27 -0600277 if (pos >= 0 && swizzleOut) {
Briance664002007-01-18 17:23:48 -0700278 if (size == 1)
Brian1bf81e32007-03-22 09:07:27 -0600279 *swizzleOut = SWIZZLE_XXXX;
Briance664002007-01-18 17:23:48 -0700280 else
281 *swizzleOut = SWIZZLE_NOOP;
282 }
283 return pos;
Brian00cdc0a2006-12-14 15:01:06 -0700284}
285
286
Brian8d370fb2007-04-18 16:46:53 -0600287/**
288 * Add a uniform to the parameter list.
289 * Note that if the uniform is an array, size may be greater than
290 * what's implied by the datatype.
291 * \param name uniform's name
292 * \param size number of floats to allocate
293 * \param datatype GL_FLOAT_VEC3, GL_FLOAT_MAT4, etc.
294 */
Brian00cdc0a2006-12-14 15:01:06 -0700295GLint
296_mesa_add_uniform(struct gl_program_parameter_list *paramList,
Brian Paul2d76a0d2008-11-10 12:33:17 -0700297 const char *name, GLuint size, GLenum datatype,
298 const GLfloat *values)
Brian00cdc0a2006-12-14 15:01:06 -0700299{
300 GLint i = _mesa_lookup_parameter_index(paramList, -1, name);
Brianc93e8832007-04-18 16:27:35 -0600301 ASSERT(datatype != GL_NONE);
Brian00cdc0a2006-12-14 15:01:06 -0700302 if (i >= 0 && paramList->Parameters[i].Type == PROGRAM_UNIFORM) {
Brianc93e8832007-04-18 16:27:35 -0600303 ASSERT(paramList->Parameters[i].Size == size);
304 ASSERT(paramList->Parameters[i].DataType == datatype);
Brian00cdc0a2006-12-14 15:01:06 -0700305 /* already in list */
306 return i;
307 }
308 else {
Brianb7978af2007-01-09 19:17:17 -0700309 i = _mesa_add_parameter(paramList, PROGRAM_UNIFORM, name,
Brian Paul84c18502008-11-24 08:33:49 -0700310 size, datatype, values, NULL, 0x0);
Brian00cdc0a2006-12-14 15:01:06 -0700311 return i;
312 }
313}
314
315
Brian8d370fb2007-04-18 16:46:53 -0600316/**
Brian Paul3e1706f2008-08-19 10:27:06 -0600317 * Mark the named uniform as 'used'.
318 */
319void
320_mesa_use_uniform(struct gl_program_parameter_list *paramList,
321 const char *name)
322{
323 GLuint i;
324 for (i = 0; i < paramList->NumParameters; i++) {
325 struct gl_program_parameter *p = paramList->Parameters + i;
Brian Paul1fad6cc2009-01-02 12:26:15 -0700326 if ((p->Type == PROGRAM_UNIFORM || p->Type == PROGRAM_SAMPLER) &&
327 _mesa_strcmp(p->Name, name) == 0) {
Brian Paul3e1706f2008-08-19 10:27:06 -0600328 p->Used = GL_TRUE;
329 /* Note that large uniforms may occupy several slots so we're
330 * not done searching yet.
331 */
332 }
333 }
334}
335
336
337/**
Brian8d370fb2007-04-18 16:46:53 -0600338 * Add a sampler to the parameter list.
339 * \param name uniform's name
340 * \param datatype GL_SAMPLER_2D, GL_SAMPLER_2D_RECT_ARB, etc.
Brian8fed2462007-10-26 19:19:09 -0600341 * \param index the sampler number (as seen in TEX instructions)
Brian Paulc95c4ef2008-05-20 11:29:58 -0600342 * \return sampler index (starting at zero) or -1 if error
Brian8d370fb2007-04-18 16:46:53 -0600343 */
Brian00cdc0a2006-12-14 15:01:06 -0700344GLint
Brian9805e762007-01-05 16:00:57 -0700345_mesa_add_sampler(struct gl_program_parameter_list *paramList,
Brian Paulc95c4ef2008-05-20 11:29:58 -0600346 const char *name, GLenum datatype)
Brian9805e762007-01-05 16:00:57 -0700347{
348 GLint i = _mesa_lookup_parameter_index(paramList, -1, name);
349 if (i >= 0 && paramList->Parameters[i].Type == PROGRAM_SAMPLER) {
Brian6b3027e2007-04-18 16:48:55 -0600350 ASSERT(paramList->Parameters[i].Size == 1);
351 ASSERT(paramList->Parameters[i].DataType == datatype);
Brian9805e762007-01-05 16:00:57 -0700352 /* already in list */
Brian Paulc95c4ef2008-05-20 11:29:58 -0600353 return (GLint) paramList->ParameterValues[i][0];
Brian9805e762007-01-05 16:00:57 -0700354 }
355 else {
Michal Krolf9c574d2008-07-15 11:44:47 +0200356 GLuint i;
Brian6b3027e2007-04-18 16:48:55 -0600357 const GLint size = 1; /* a sampler is basically a texture unit number */
Brian Paul90513082009-04-17 13:55:58 -0600358 GLfloat value[4];
Brian Paulc95c4ef2008-05-20 11:29:58 -0600359 GLint numSamplers = 0;
360 for (i = 0; i < paramList->NumParameters; i++) {
361 if (paramList->Parameters[i].Type == PROGRAM_SAMPLER)
362 numSamplers++;
363 }
Brian Paul90513082009-04-17 13:55:58 -0600364 value[0] = (GLfloat) numSamplers;
365 value[1] = value[2] = value[3] = 0.0F;
Brian Paulc95c4ef2008-05-20 11:29:58 -0600366 (void) _mesa_add_parameter(paramList, PROGRAM_SAMPLER, name,
Brian Paul90513082009-04-17 13:55:58 -0600367 size, datatype, value, NULL, 0x0);
Brian Paulc95c4ef2008-05-20 11:29:58 -0600368 return numSamplers;
Brian9805e762007-01-05 16:00:57 -0700369 }
370}
371
372
Brian3209c3e2007-01-09 17:49:24 -0700373/**
374 * Add parameter representing a varying variable.
375 */
Brian9805e762007-01-05 16:00:57 -0700376GLint
Brian00cdc0a2006-12-14 15:01:06 -0700377_mesa_add_varying(struct gl_program_parameter_list *paramList,
Brian Paul84c18502008-11-24 08:33:49 -0700378 const char *name, GLuint size, GLbitfield flags)
Brian00cdc0a2006-12-14 15:01:06 -0700379{
380 GLint i = _mesa_lookup_parameter_index(paramList, -1, name);
381 if (i >= 0 && paramList->Parameters[i].Type == PROGRAM_VARYING) {
382 /* already in list */
383 return i;
384 }
385 else {
Brian Paul72e57b52008-07-18 19:46:19 -0600386 /*assert(size == 4);*/
Brianb7978af2007-01-09 19:17:17 -0700387 i = _mesa_add_parameter(paramList, PROGRAM_VARYING, name,
Brian Paul84c18502008-11-24 08:33:49 -0700388 size, GL_NONE, NULL, NULL, flags);
Brian00cdc0a2006-12-14 15:01:06 -0700389 return i;
390 }
391}
392
393
Brian3209c3e2007-01-09 17:49:24 -0700394/**
395 * Add parameter representing a vertex program attribute.
Brianb7978af2007-01-09 19:17:17 -0700396 * \param size size of attribute (in floats), may be -1 if unknown
397 * \param attrib the attribute index, or -1 if unknown
Brian3209c3e2007-01-09 17:49:24 -0700398 */
399GLint
400_mesa_add_attribute(struct gl_program_parameter_list *paramList,
Brian Paulfbf26e12008-07-21 13:58:50 -0600401 const char *name, GLint size, GLenum datatype, GLint attrib)
Brian3209c3e2007-01-09 17:49:24 -0700402{
Brian3209c3e2007-01-09 17:49:24 -0700403 GLint i = _mesa_lookup_parameter_index(paramList, -1, name);
404 if (i >= 0) {
405 /* replace */
Brianb7978af2007-01-09 19:17:17 -0700406 if (attrib < 0)
407 attrib = i;
Brian01a91eb2007-01-09 19:26:22 -0700408 paramList->Parameters[i].StateIndexes[0] = attrib;
Brian3209c3e2007-01-09 17:49:24 -0700409 }
410 else {
411 /* add */
Brianb7978af2007-01-09 19:17:17 -0700412 gl_state_index state[STATE_LENGTH];
Brian223d7cb2007-01-23 16:37:51 -0700413 state[0] = (gl_state_index) attrib;
Brianee118422007-01-10 12:18:33 -0700414 if (size < 0)
415 size = 4;
Brianb7978af2007-01-09 19:17:17 -0700416 i = _mesa_add_parameter(paramList, PROGRAM_INPUT, name,
Brian Paul84c18502008-11-24 08:33:49 -0700417 size, datatype, NULL, state, 0x0);
Brian3209c3e2007-01-09 17:49:24 -0700418 }
419 return i;
420}
421
Brian00cdc0a2006-12-14 15:01:06 -0700422
423
424#if 0 /* not used yet */
425/**
426 * Returns the number of 4-component registers needed to store a piece
427 * of GL state. For matrices this may be as many as 4 registers,
428 * everything else needs
429 * just 1 register.
430 */
431static GLuint
432sizeof_state_reference(const GLint *stateTokens)
433{
434 if (stateTokens[0] == STATE_MATRIX) {
435 GLuint rows = stateTokens[4] - stateTokens[3] + 1;
436 assert(rows >= 1);
437 assert(rows <= 4);
438 return rows;
439 }
440 else {
441 return 1;
442 }
443}
444#endif
445
446
447/**
448 * Add a new state reference to the parameter list.
449 * This will be used when the program contains something like this:
450 * PARAM ambient = state.material.front.ambient;
451 *
452 * \param paramList the parameter list
Brian4adee842008-03-27 15:54:44 -0600453 * \param stateTokens an array of 5 (STATE_LENGTH) state tokens
Brian00cdc0a2006-12-14 15:01:06 -0700454 * \return index of the new parameter.
455 */
456GLint
457_mesa_add_state_reference(struct gl_program_parameter_list *paramList,
Brianaa9d22a2007-02-23 11:21:03 -0700458 const gl_state_index stateTokens[STATE_LENGTH])
Brian00cdc0a2006-12-14 15:01:06 -0700459{
460 const GLuint size = 4; /* XXX fix */
Michal Krolce3cf632008-09-05 12:25:50 +0200461 char *name;
Brian00cdc0a2006-12-14 15:01:06 -0700462 GLint index;
463
464 /* Check if the state reference is already in the list */
Brian223d7cb2007-01-23 16:37:51 -0700465 for (index = 0; index < (GLint) paramList->NumParameters; index++) {
Brian00cdc0a2006-12-14 15:01:06 -0700466 GLuint i, match = 0;
Brian776bc9c2007-02-22 09:29:46 -0700467 for (i = 0; i < STATE_LENGTH; i++) {
Brian00cdc0a2006-12-14 15:01:06 -0700468 if (paramList->Parameters[index].StateIndexes[i] == stateTokens[i]) {
469 match++;
470 }
471 else {
472 break;
473 }
474 }
Brianb7978af2007-01-09 19:17:17 -0700475 if (match == STATE_LENGTH) {
Brian00cdc0a2006-12-14 15:01:06 -0700476 /* this state reference is already in the parameter list */
477 return index;
478 }
479 }
480
481 name = _mesa_program_state_string(stateTokens);
Brianb7978af2007-01-09 19:17:17 -0700482 index = _mesa_add_parameter(paramList, PROGRAM_STATE_VAR, name,
Brianc93e8832007-04-18 16:27:35 -0600483 size, GL_NONE,
Brian Paul84c18502008-11-24 08:33:49 -0700484 NULL, (gl_state_index *) stateTokens, 0x0);
Brian29bff4e2007-01-10 08:37:59 -0700485 paramList->StateFlags |= _mesa_program_state_flags(stateTokens);
Brian00cdc0a2006-12-14 15:01:06 -0700486
487 /* free name string here since we duplicated it in add_parameter() */
Michal Krolce3cf632008-09-05 12:25:50 +0200488 _mesa_free(name);
Brian00cdc0a2006-12-14 15:01:06 -0700489
490 return index;
491}
492
493
494/**
495 * Lookup a parameter value by name in the given parameter list.
496 * \return pointer to the float[4] values.
497 */
498GLfloat *
499_mesa_lookup_parameter_value(const struct gl_program_parameter_list *paramList,
500 GLsizei nameLen, const char *name)
501{
502 GLuint i = _mesa_lookup_parameter_index(paramList, nameLen, name);
503 if (i < 0)
504 return NULL;
505 else
506 return paramList->ParameterValues[i];
507}
508
509
510/**
511 * Given a program parameter name, find its position in the list of parameters.
512 * \param paramList the parameter list to search
513 * \param nameLen length of name (in chars).
514 * If length is negative, assume that name is null-terminated.
515 * \param name the name to search for
516 * \return index of parameter in the list.
517 */
518GLint
519_mesa_lookup_parameter_index(const struct gl_program_parameter_list *paramList,
520 GLsizei nameLen, const char *name)
521{
522 GLint i;
523
524 if (!paramList)
525 return -1;
526
527 if (nameLen == -1) {
528 /* name is null-terminated */
529 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
530 if (paramList->Parameters[i].Name &&
531 _mesa_strcmp(paramList->Parameters[i].Name, name) == 0)
532 return i;
533 }
534 }
535 else {
536 /* name is not null-terminated, use nameLen */
537 for (i = 0; i < (GLint) paramList->NumParameters; i++) {
538 if (paramList->Parameters[i].Name &&
539 _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0
540 && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen)
541 return i;
542 }
543 }
544 return -1;
545}
546
547
548/**
549 * Look for a float vector in the given parameter list. The float vector
Brian1bf81e32007-03-22 09:07:27 -0600550 * may be of length 1, 2, 3 or 4. If swizzleOut is non-null, we'll try
551 * swizzling to find a match.
Briance664002007-01-18 17:23:48 -0700552 * \param list the parameter list to search
Brian00cdc0a2006-12-14 15:01:06 -0700553 * \param v the float vector to search for
Vinson Lee1eee1ba2009-03-17 09:34:30 -0600554 * \param vSize number of element in v
Brian00cdc0a2006-12-14 15:01:06 -0700555 * \param posOut returns the position of the constant, if found
556 * \param swizzleOut returns a swizzle mask describing location of the
Brian1bf81e32007-03-22 09:07:27 -0600557 * vector elements if found.
Brian00cdc0a2006-12-14 15:01:06 -0700558 * \return GL_TRUE if found, GL_FALSE if not found
559 */
560GLboolean
Briance664002007-01-18 17:23:48 -0700561_mesa_lookup_parameter_constant(const struct gl_program_parameter_list *list,
Brian223d7cb2007-01-23 16:37:51 -0700562 const GLfloat v[], GLuint vSize,
Brian00cdc0a2006-12-14 15:01:06 -0700563 GLint *posOut, GLuint *swizzleOut)
564{
565 GLuint i;
566
567 assert(vSize >= 1);
568 assert(vSize <= 4);
569
Briance664002007-01-18 17:23:48 -0700570 if (!list)
Brian00cdc0a2006-12-14 15:01:06 -0700571 return -1;
572
Briance664002007-01-18 17:23:48 -0700573 for (i = 0; i < list->NumParameters; i++) {
574 if (list->Parameters[i].Type == PROGRAM_CONSTANT) {
Brian1bf81e32007-03-22 09:07:27 -0600575 if (!swizzleOut) {
576 /* swizzle not allowed */
577 GLuint j, match = 0;
Briance664002007-01-18 17:23:48 -0700578 for (j = 0; j < vSize; j++) {
Brian1bf81e32007-03-22 09:07:27 -0600579 if (v[j] == list->ParameterValues[i][j])
Briance664002007-01-18 17:23:48 -0700580 match++;
Briance664002007-01-18 17:23:48 -0700581 }
582 if (match == vSize) {
Brian00cdc0a2006-12-14 15:01:06 -0700583 *posOut = i;
Brian00cdc0a2006-12-14 15:01:06 -0700584 return GL_TRUE;
585 }
586 }
Brian1bf81e32007-03-22 09:07:27 -0600587 else {
588 /* try matching w/ swizzle */
589 if (vSize == 1) {
590 /* look for v[0] anywhere within float[4] value */
591 GLuint j;
592 for (j = 0; j < 4; j++) {
593 if (list->ParameterValues[i][j] == v[0]) {
594 /* found it */
595 *posOut = i;
596 *swizzleOut = MAKE_SWIZZLE4(j, j, j, j);
597 return GL_TRUE;
598 }
599 }
600 }
601 else if (vSize <= list->Parameters[i].Size) {
602 /* see if we can match this constant (with a swizzle) */
603 GLuint swz[4];
604 GLuint match = 0, j, k;
605 for (j = 0; j < vSize; j++) {
606 if (v[j] == list->ParameterValues[i][j]) {
607 swz[j] = j;
608 match++;
609 }
610 else {
611 for (k = 0; k < list->Parameters[i].Size; k++) {
612 if (v[j] == list->ParameterValues[i][k]) {
613 swz[j] = k;
614 match++;
615 break;
616 }
617 }
618 }
619 }
Brian680abf82007-03-26 13:04:57 -0600620 /* smear last value to remaining positions */
621 for (; j < 4; j++)
622 swz[j] = swz[j-1];
623
Brian1bf81e32007-03-22 09:07:27 -0600624 if (match == vSize) {
625 *posOut = i;
626 *swizzleOut = MAKE_SWIZZLE4(swz[0], swz[1], swz[2], swz[3]);
627 return GL_TRUE;
628 }
629 }
630 }
Brian00cdc0a2006-12-14 15:01:06 -0700631 }
632 }
633
634 *posOut = -1;
635 return GL_FALSE;
636}
637
638
639struct gl_program_parameter_list *
640_mesa_clone_parameter_list(const struct gl_program_parameter_list *list)
641{
642 struct gl_program_parameter_list *clone;
643 GLuint i;
644
645 clone = _mesa_new_parameter_list();
646 if (!clone)
647 return NULL;
648
649 /** Not too efficient, but correct */
650 for (i = 0; i < list->NumParameters; i++) {
651 struct gl_program_parameter *p = list->Parameters + i;
Brian Paul3e1706f2008-08-19 10:27:06 -0600652 struct gl_program_parameter *pCopy;
Brian3a8e2772006-12-20 17:19:16 -0700653 GLuint size = MIN2(p->Size, 4);
Brianc93e8832007-04-18 16:27:35 -0600654 GLint j = _mesa_add_parameter(clone, p->Type, p->Name, size, p->DataType,
Brian Paul84c18502008-11-24 08:33:49 -0700655 list->ParameterValues[i], NULL, 0x0);
Brian00cdc0a2006-12-14 15:01:06 -0700656 ASSERT(j >= 0);
Brian Paul3e1706f2008-08-19 10:27:06 -0600657 pCopy = clone->Parameters + j;
658 pCopy->Used = p->Used;
Brian Paula7264722008-11-24 09:04:11 -0700659 pCopy->Flags = p->Flags;
Brian00cdc0a2006-12-14 15:01:06 -0700660 /* copy state indexes */
661 if (p->Type == PROGRAM_STATE_VAR) {
662 GLint k;
Brianb7978af2007-01-09 19:17:17 -0700663 for (k = 0; k < STATE_LENGTH; k++) {
Brian Paul3e1706f2008-08-19 10:27:06 -0600664 pCopy->StateIndexes[k] = p->StateIndexes[k];
Brian00cdc0a2006-12-14 15:01:06 -0700665 }
666 }
Brian3a8e2772006-12-20 17:19:16 -0700667 else {
668 clone->Parameters[j].Size = p->Size;
669 }
Brian Paul3e1706f2008-08-19 10:27:06 -0600670
Brian00cdc0a2006-12-14 15:01:06 -0700671 }
672
Brian Paul34149ec2008-05-20 11:31:20 -0600673 clone->StateFlags = list->StateFlags;
674
Brian00cdc0a2006-12-14 15:01:06 -0700675 return clone;
676}
Brian5b01c5e2006-12-19 18:02:03 -0700677
678
679/**
Brian4b30d172007-10-29 16:00:08 -0600680 * Return a new parameter list which is listA + listB.
681 */
682struct gl_program_parameter_list *
683_mesa_combine_parameter_lists(const struct gl_program_parameter_list *listA,
684 const struct gl_program_parameter_list *listB)
685{
686 struct gl_program_parameter_list *list;
687
688 if (listA) {
689 list = _mesa_clone_parameter_list(listA);
690 if (list && listB) {
691 GLuint i;
692 for (i = 0; i < listB->NumParameters; i++) {
693 struct gl_program_parameter *param = listB->Parameters + i;
694 _mesa_add_parameter(list, param->Type, param->Name, param->Size,
695 param->DataType,
696 listB->ParameterValues[i],
Brian Paul84c18502008-11-24 08:33:49 -0700697 param->StateIndexes,
698 param->Flags);
Brian4b30d172007-10-29 16:00:08 -0600699 }
700 }
701 }
702 else if (listB) {
703 list = _mesa_clone_parameter_list(listB);
704 }
705 else {
706 list = NULL;
707 }
708 return list;
709}
710
711
712
713/**
Brian6d3d9c12007-04-18 14:19:17 -0600714 * Find longest name of all uniform parameters in list.
Brian5b01c5e2006-12-19 18:02:03 -0700715 */
716GLuint
Brian6d3d9c12007-04-18 14:19:17 -0600717_mesa_longest_parameter_name(const struct gl_program_parameter_list *list,
Brian Paulb4026d92009-03-07 12:02:52 -0700718 gl_register_file type)
Brian5b01c5e2006-12-19 18:02:03 -0700719{
720 GLuint i, maxLen = 0;
721 if (!list)
722 return 0;
723 for (i = 0; i < list->NumParameters; i++) {
Brian6d3d9c12007-04-18 14:19:17 -0600724 if (list->Parameters[i].Type == type) {
725 GLuint len = _mesa_strlen(list->Parameters[i].Name);
726 if (len > maxLen)
727 maxLen = len;
728 }
Brian5b01c5e2006-12-19 18:02:03 -0700729 }
730 return maxLen;
731}
732
Brian274ac7a2007-04-18 16:05:53 -0600733
734/**
735 * Count the number of parameters in the last that match the given type.
736 */
737GLuint
738_mesa_num_parameters_of_type(const struct gl_program_parameter_list *list,
Brian Paulb4026d92009-03-07 12:02:52 -0700739 gl_register_file type)
Brian274ac7a2007-04-18 16:05:53 -0600740{
741 GLuint i, count = 0;
742 if (list) {
743 for (i = 0; i < list->NumParameters; i++) {
744 if (list->Parameters[i].Type == type)
745 count++;
746 }
747 }
748 return count;
749}