Brian | 00cdc0a | 2006-12-14 15:01:06 -0700 | [diff] [blame] | 1 | /* |
| 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" |
Brian | 00cdc0a | 2006-12-14 15:01:06 -0700 | [diff] [blame] | 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 (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 | */ |
| 51 | void |
| 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. |
Brian | 3a8e277 | 2006-12-20 17:19:16 -0700 | [diff] [blame] | 69 | * 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 | * |
Brian | 00cdc0a | 2006-12-14 15:01:06 -0700 | [diff] [blame] | 73 | * \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 |
Brian | 3a8e277 | 2006-12-20 17:19:16 -0700 | [diff] [blame] | 76 | * \param size number of elements in 'values' vector (1..4, or more) |
Brian | 00cdc0a | 2006-12-14 15:01:06 -0700 | [diff] [blame] | 77 | * \param type type of parameter, such as |
| 78 | * \return index of new parameter in the list, or -1 if error (out of mem) |
| 79 | */ |
| 80 | GLint |
| 81 | _mesa_add_parameter(struct gl_program_parameter_list *paramList, |
Brian | 3a8e277 | 2006-12-20 17:19:16 -0700 | [diff] [blame] | 82 | const char *name, const GLfloat *values, GLuint size, |
Brian | 00cdc0a | 2006-12-14 15:01:06 -0700 | [diff] [blame] | 83 | enum register_file type) |
| 84 | { |
Brian | 3a8e277 | 2006-12-20 17:19:16 -0700 | [diff] [blame] | 85 | const GLuint oldNum = paramList->NumParameters; |
| 86 | const GLuint sz4 = (size + 3) / 4; /* no. of new param slots needed */ |
Brian | 00cdc0a | 2006-12-14 15:01:06 -0700 | [diff] [blame] | 87 | |
Brian | 3a8e277 | 2006-12-20 17:19:16 -0700 | [diff] [blame] | 88 | 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; |
Brian | 00cdc0a | 2006-12-14 15:01:06 -0700 | [diff] [blame] | 94 | |
| 95 | /* realloc arrays */ |
| 96 | paramList->Parameters = (struct gl_program_parameter *) |
| 97 | _mesa_realloc(paramList->Parameters, |
Brian | 3a8e277 | 2006-12-20 17:19:16 -0700 | [diff] [blame] | 98 | oldNum * sizeof(struct gl_program_parameter), |
Brian | 00cdc0a | 2006-12-14 15:01:06 -0700 | [diff] [blame] | 99 | paramList->Size * sizeof(struct gl_program_parameter)); |
| 100 | |
| 101 | paramList->ParameterValues = (GLfloat (*)[4]) |
| 102 | _mesa_align_realloc(paramList->ParameterValues, /* old buf */ |
Brian | 3a8e277 | 2006-12-20 17:19:16 -0700 | [diff] [blame] | 103 | oldNum * 4 * sizeof(GLfloat), /* old size */ |
Brian | 00cdc0a | 2006-12-14 15:01:06 -0700 | [diff] [blame] | 104 | 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 { |
Brian | 3a8e277 | 2006-12-20 17:19:16 -0700 | [diff] [blame] | 116 | GLuint i; |
Brian | 00cdc0a | 2006-12-14 15:01:06 -0700 | [diff] [blame] | 117 | |
Brian | 3a8e277 | 2006-12-20 17:19:16 -0700 | [diff] [blame] | 118 | paramList->NumParameters = oldNum + sz4; |
Brian | 00cdc0a | 2006-12-14 15:01:06 -0700 | [diff] [blame] | 119 | |
Brian | 3a8e277 | 2006-12-20 17:19:16 -0700 | [diff] [blame] | 120 | _mesa_memset(¶mList->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; |
Brian | 00cdc0a | 2006-12-14 15:01:06 -0700 | [diff] [blame] | 135 | } |
| 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 | */ |
| 143 | GLint |
| 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 | */ |
| 161 | GLint |
| 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 | */ |
| 190 | GLint |
| 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 | |
| 209 | GLint |
| 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 { |
Brian | 00cdc0a | 2006-12-14 15:01:06 -0700 | [diff] [blame] | 219 | i = _mesa_add_parameter(paramList, name, NULL, size, PROGRAM_UNIFORM); |
| 220 | return i; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | GLint |
| 226 | _mesa_add_varying(struct gl_program_parameter_list *paramList, |
| 227 | const char *name, GLuint size) |
| 228 | { |
| 229 | GLint i = _mesa_lookup_parameter_index(paramList, -1, name); |
| 230 | if (i >= 0 && paramList->Parameters[i].Type == PROGRAM_VARYING) { |
| 231 | /* already in list */ |
| 232 | return i; |
| 233 | } |
| 234 | else { |
| 235 | assert(size == 4); |
| 236 | i = _mesa_add_parameter(paramList, name, NULL, size, PROGRAM_VARYING); |
| 237 | return i; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | #if 0 /* not used yet */ |
| 245 | /** |
| 246 | * Returns the number of 4-component registers needed to store a piece |
| 247 | * of GL state. For matrices this may be as many as 4 registers, |
| 248 | * everything else needs |
| 249 | * just 1 register. |
| 250 | */ |
| 251 | static GLuint |
| 252 | sizeof_state_reference(const GLint *stateTokens) |
| 253 | { |
| 254 | if (stateTokens[0] == STATE_MATRIX) { |
| 255 | GLuint rows = stateTokens[4] - stateTokens[3] + 1; |
| 256 | assert(rows >= 1); |
| 257 | assert(rows <= 4); |
| 258 | return rows; |
| 259 | } |
| 260 | else { |
| 261 | return 1; |
| 262 | } |
| 263 | } |
| 264 | #endif |
| 265 | |
| 266 | |
| 267 | /** |
| 268 | * Add a new state reference to the parameter list. |
| 269 | * This will be used when the program contains something like this: |
| 270 | * PARAM ambient = state.material.front.ambient; |
| 271 | * |
| 272 | * \param paramList the parameter list |
| 273 | * \param state an array of 6 state tokens |
| 274 | * \return index of the new parameter. |
| 275 | */ |
| 276 | GLint |
| 277 | _mesa_add_state_reference(struct gl_program_parameter_list *paramList, |
| 278 | const GLint *stateTokens) |
| 279 | { |
| 280 | const GLuint size = 4; /* XXX fix */ |
| 281 | const char *name; |
| 282 | GLint index; |
| 283 | |
| 284 | /* Check if the state reference is already in the list */ |
| 285 | for (index = 0; index < paramList->NumParameters; index++) { |
| 286 | GLuint i, match = 0; |
| 287 | for (i = 0; i < 6; i++) { |
| 288 | if (paramList->Parameters[index].StateIndexes[i] == stateTokens[i]) { |
| 289 | match++; |
| 290 | } |
| 291 | else { |
| 292 | break; |
| 293 | } |
| 294 | } |
| 295 | if (match == 6) { |
| 296 | /* this state reference is already in the parameter list */ |
| 297 | return index; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | name = _mesa_program_state_string(stateTokens); |
| 302 | index = _mesa_add_parameter(paramList, name, NULL, size, PROGRAM_STATE_VAR); |
| 303 | if (index >= 0) { |
| 304 | GLuint i; |
| 305 | for (i = 0; i < 6; i++) { |
| 306 | paramList->Parameters[index].StateIndexes[i] |
| 307 | = (gl_state_index) stateTokens[i]; |
| 308 | } |
| 309 | paramList->StateFlags |= _mesa_program_state_flags(stateTokens); |
| 310 | } |
| 311 | |
| 312 | /* free name string here since we duplicated it in add_parameter() */ |
| 313 | _mesa_free((void *) name); |
| 314 | |
| 315 | return index; |
| 316 | } |
| 317 | |
| 318 | |
| 319 | /** |
| 320 | * Lookup a parameter value by name in the given parameter list. |
| 321 | * \return pointer to the float[4] values. |
| 322 | */ |
| 323 | GLfloat * |
| 324 | _mesa_lookup_parameter_value(const struct gl_program_parameter_list *paramList, |
| 325 | GLsizei nameLen, const char *name) |
| 326 | { |
| 327 | GLuint i = _mesa_lookup_parameter_index(paramList, nameLen, name); |
| 328 | if (i < 0) |
| 329 | return NULL; |
| 330 | else |
| 331 | return paramList->ParameterValues[i]; |
| 332 | } |
| 333 | |
| 334 | |
| 335 | /** |
| 336 | * Given a program parameter name, find its position in the list of parameters. |
| 337 | * \param paramList the parameter list to search |
| 338 | * \param nameLen length of name (in chars). |
| 339 | * If length is negative, assume that name is null-terminated. |
| 340 | * \param name the name to search for |
| 341 | * \return index of parameter in the list. |
| 342 | */ |
| 343 | GLint |
| 344 | _mesa_lookup_parameter_index(const struct gl_program_parameter_list *paramList, |
| 345 | GLsizei nameLen, const char *name) |
| 346 | { |
| 347 | GLint i; |
| 348 | |
| 349 | if (!paramList) |
| 350 | return -1; |
| 351 | |
| 352 | if (nameLen == -1) { |
| 353 | /* name is null-terminated */ |
| 354 | for (i = 0; i < (GLint) paramList->NumParameters; i++) { |
| 355 | if (paramList->Parameters[i].Name && |
| 356 | _mesa_strcmp(paramList->Parameters[i].Name, name) == 0) |
| 357 | return i; |
| 358 | } |
| 359 | } |
| 360 | else { |
| 361 | /* name is not null-terminated, use nameLen */ |
| 362 | for (i = 0; i < (GLint) paramList->NumParameters; i++) { |
| 363 | if (paramList->Parameters[i].Name && |
| 364 | _mesa_strncmp(paramList->Parameters[i].Name, name, nameLen) == 0 |
| 365 | && _mesa_strlen(paramList->Parameters[i].Name) == (size_t)nameLen) |
| 366 | return i; |
| 367 | } |
| 368 | } |
| 369 | return -1; |
| 370 | } |
| 371 | |
| 372 | |
| 373 | /** |
| 374 | * Look for a float vector in the given parameter list. The float vector |
| 375 | * may be of length 1, 2, 3 or 4. |
| 376 | * \param paramList the parameter list to search |
| 377 | * \param v the float vector to search for |
| 378 | * \param size number of element in v |
| 379 | * \param posOut returns the position of the constant, if found |
| 380 | * \param swizzleOut returns a swizzle mask describing location of the |
| 381 | * vector elements if found |
| 382 | * \return GL_TRUE if found, GL_FALSE if not found |
| 383 | */ |
| 384 | GLboolean |
| 385 | _mesa_lookup_parameter_constant(const struct gl_program_parameter_list *paramList, |
| 386 | const GLfloat v[], GLsizei vSize, |
| 387 | GLint *posOut, GLuint *swizzleOut) |
| 388 | { |
| 389 | GLuint i; |
| 390 | |
| 391 | assert(vSize >= 1); |
| 392 | assert(vSize <= 4); |
| 393 | |
| 394 | if (!paramList) |
| 395 | return -1; |
| 396 | |
| 397 | for (i = 0; i < paramList->NumParameters; i++) { |
| 398 | if (paramList->Parameters[i].Type == PROGRAM_CONSTANT) { |
| 399 | const GLint maxShift = 4 - vSize; |
| 400 | GLint shift, j; |
| 401 | for (shift = 0; shift <= maxShift; shift++) { |
| 402 | GLint matched = 0; |
| 403 | GLuint swizzle[4]; |
| 404 | swizzle[0] = swizzle[1] = swizzle[2] = swizzle[3] = 0; |
| 405 | /* XXX we could do out-of-order swizzle matches too, someday */ |
| 406 | for (j = 0; j < vSize; j++) { |
| 407 | assert(shift + j < 4); |
| 408 | if (paramList->ParameterValues[i][shift + j] == v[j]) { |
| 409 | matched++; |
| 410 | swizzle[j] = shift + j; |
| 411 | ASSERT(swizzle[j] >= SWIZZLE_X); |
| 412 | ASSERT(swizzle[j] <= SWIZZLE_W); |
| 413 | } |
| 414 | } |
| 415 | if (matched == vSize) { |
| 416 | /* found! */ |
| 417 | *posOut = i; |
| 418 | *swizzleOut = MAKE_SWIZZLE4(swizzle[0], swizzle[1], |
| 419 | swizzle[2], swizzle[3]); |
| 420 | return GL_TRUE; |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | *posOut = -1; |
| 427 | return GL_FALSE; |
| 428 | } |
| 429 | |
| 430 | |
| 431 | struct gl_program_parameter_list * |
| 432 | _mesa_clone_parameter_list(const struct gl_program_parameter_list *list) |
| 433 | { |
| 434 | struct gl_program_parameter_list *clone; |
| 435 | GLuint i; |
| 436 | |
| 437 | clone = _mesa_new_parameter_list(); |
| 438 | if (!clone) |
| 439 | return NULL; |
| 440 | |
| 441 | /** Not too efficient, but correct */ |
| 442 | for (i = 0; i < list->NumParameters; i++) { |
| 443 | struct gl_program_parameter *p = list->Parameters + i; |
Brian | 3a8e277 | 2006-12-20 17:19:16 -0700 | [diff] [blame] | 444 | GLuint size = MIN2(p->Size, 4); |
Brian | 00cdc0a | 2006-12-14 15:01:06 -0700 | [diff] [blame] | 445 | GLint j = _mesa_add_parameter(clone, p->Name, list->ParameterValues[i], |
Brian | 3a8e277 | 2006-12-20 17:19:16 -0700 | [diff] [blame] | 446 | size, p->Type); |
Brian | 00cdc0a | 2006-12-14 15:01:06 -0700 | [diff] [blame] | 447 | ASSERT(j >= 0); |
| 448 | /* copy state indexes */ |
| 449 | if (p->Type == PROGRAM_STATE_VAR) { |
| 450 | GLint k; |
| 451 | struct gl_program_parameter *q = clone->Parameters + j; |
| 452 | for (k = 0; k < 6; k++) { |
| 453 | q->StateIndexes[k] = p->StateIndexes[k]; |
| 454 | } |
| 455 | } |
Brian | 3a8e277 | 2006-12-20 17:19:16 -0700 | [diff] [blame] | 456 | else { |
| 457 | clone->Parameters[j].Size = p->Size; |
| 458 | } |
Brian | 00cdc0a | 2006-12-14 15:01:06 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | return clone; |
| 462 | } |
Brian | 5b01c5e | 2006-12-19 18:02:03 -0700 | [diff] [blame] | 463 | |
| 464 | |
| 465 | /** |
| 466 | * Find longest name of any parameter in list. |
| 467 | */ |
| 468 | GLuint |
| 469 | _mesa_parameter_longest_name(const struct gl_program_parameter_list *list) |
| 470 | { |
| 471 | GLuint i, maxLen = 0; |
| 472 | if (!list) |
| 473 | return 0; |
| 474 | for (i = 0; i < list->NumParameters; i++) { |
| 475 | GLuint len = _mesa_strlen(list->Parameters[i].Name); |
| 476 | if (len > maxLen) |
| 477 | maxLen = len; |
| 478 | } |
| 479 | return maxLen; |
| 480 | } |
| 481 | |