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