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