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