Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mesa 3-D graphics library |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 3 | * Version: 6.5 |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 4 | * |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2005 Brian Paul All Rights Reserved. |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 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 nvfragparse.c |
| 27 | * NVIDIA fragment program parser. |
| 28 | * \author Brian Paul |
| 29 | */ |
| 30 | |
| 31 | /* |
| 32 | * Regarding GL_NV_fragment_program: |
| 33 | * |
| 34 | * Portions of this software may use or implement intellectual |
| 35 | * property owned and licensed by NVIDIA Corporation. NVIDIA disclaims |
| 36 | * any and all warranties with respect to such intellectual property, |
| 37 | * including any use thereof or modifications thereto. |
| 38 | */ |
| 39 | |
| 40 | #include "glheader.h" |
| 41 | #include "context.h" |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 42 | #include "imports.h" |
| 43 | #include "macros.h" |
Brian | c0551f0 | 2006-12-14 15:02:37 -0700 | [diff] [blame] | 44 | #include "prog_parameter.h" |
| 45 | #include "prog_instruction.h" |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 46 | #include "nvfragparse.h" |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 47 | #include "program.h" |
| 48 | |
| 49 | |
| 50 | #define INPUT_1V 1 |
| 51 | #define INPUT_2V 2 |
| 52 | #define INPUT_3V 3 |
| 53 | #define INPUT_1S 4 |
| 54 | #define INPUT_2S 5 |
| 55 | #define INPUT_CC 6 |
| 56 | #define INPUT_1V_T 7 /* one source vector, plus textureId */ |
| 57 | #define INPUT_3V_T 8 /* one source vector, plus textureId */ |
| 58 | #define INPUT_NONE 9 |
Brian Paul | 2a5afe3 | 2004-12-18 16:18:00 +0000 | [diff] [blame] | 59 | #define INPUT_1V_S 10 /* a string and a vector register */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 60 | #define OUTPUT_V 20 |
| 61 | #define OUTPUT_S 21 |
| 62 | #define OUTPUT_NONE 22 |
| 63 | |
| 64 | /* IRIX defines some of these */ |
| 65 | #undef _R |
| 66 | #undef _H |
| 67 | #undef _X |
| 68 | #undef _C |
| 69 | #undef _S |
| 70 | |
| 71 | /* Optional suffixes */ |
| 72 | #define _R FLOAT32 /* float */ |
| 73 | #define _H FLOAT16 /* half-float */ |
| 74 | #define _X FIXED12 /* fixed */ |
| 75 | #define _C 0x08 /* set cond codes */ |
| 76 | #define _S 0x10 /* saturate, clamp result to [0,1] */ |
| 77 | |
| 78 | struct instruction_pattern { |
| 79 | const char *name; |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 80 | enum prog_opcode opcode; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 81 | GLuint inputs; |
| 82 | GLuint outputs; |
| 83 | GLuint suffixes; |
| 84 | }; |
| 85 | |
| 86 | static const struct instruction_pattern Instructions[] = { |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 87 | { "ADD", OPCODE_ADD, INPUT_2V, OUTPUT_V, _R | _H | _X | _C | _S }, |
| 88 | { "COS", OPCODE_COS, INPUT_1S, OUTPUT_S, _R | _H | _C | _S }, |
| 89 | { "DDX", OPCODE_DDX, INPUT_1V, OUTPUT_V, _R | _H | _C | _S }, |
| 90 | { "DDY", OPCODE_DDY, INPUT_1V, OUTPUT_V, _R | _H | _C | _S }, |
| 91 | { "DP3", OPCODE_DP3, INPUT_2V, OUTPUT_S, _R | _H | _X | _C | _S }, |
| 92 | { "DP4", OPCODE_DP4, INPUT_2V, OUTPUT_S, _R | _H | _X | _C | _S }, |
| 93 | { "DST", OPCODE_DP4, INPUT_2V, OUTPUT_V, _R | _H | _C | _S }, |
| 94 | { "EX2", OPCODE_DP4, INPUT_1S, OUTPUT_S, _R | _H | _C | _S }, |
| 95 | { "FLR", OPCODE_FLR, INPUT_1V, OUTPUT_V, _R | _H | _X | _C | _S }, |
| 96 | { "FRC", OPCODE_FRC, INPUT_1V, OUTPUT_V, _R | _H | _X | _C | _S }, |
| 97 | { "KIL", OPCODE_KIL_NV, INPUT_CC, OUTPUT_NONE, 0 }, |
| 98 | { "LG2", OPCODE_LG2, INPUT_1S, OUTPUT_S, _R | _H | _C | _S }, |
| 99 | { "LIT", OPCODE_LIT, INPUT_1V, OUTPUT_V, _R | _H | _C | _S }, |
| 100 | { "LRP", OPCODE_LRP, INPUT_3V, OUTPUT_V, _R | _H | _X | _C | _S }, |
| 101 | { "MAD", OPCODE_MAD, INPUT_3V, OUTPUT_V, _R | _H | _X | _C | _S }, |
| 102 | { "MAX", OPCODE_MAX, INPUT_2V, OUTPUT_V, _R | _H | _X | _C | _S }, |
| 103 | { "MIN", OPCODE_MIN, INPUT_2V, OUTPUT_V, _R | _H | _X | _C | _S }, |
| 104 | { "MOV", OPCODE_MOV, INPUT_1V, OUTPUT_V, _R | _H | _X | _C | _S }, |
| 105 | { "MUL", OPCODE_MUL, INPUT_2V, OUTPUT_V, _R | _H | _X | _C | _S }, |
| 106 | { "PK2H", OPCODE_PK2H, INPUT_1V, OUTPUT_S, 0 }, |
| 107 | { "PK2US", OPCODE_PK2US, INPUT_1V, OUTPUT_S, 0 }, |
| 108 | { "PK4B", OPCODE_PK4B, INPUT_1V, OUTPUT_S, 0 }, |
| 109 | { "PK4UB", OPCODE_PK4UB, INPUT_1V, OUTPUT_S, 0 }, |
| 110 | { "POW", OPCODE_POW, INPUT_2S, OUTPUT_S, _R | _H | _C | _S }, |
| 111 | { "RCP", OPCODE_RCP, INPUT_1S, OUTPUT_S, _R | _H | _C | _S }, |
| 112 | { "RFL", OPCODE_RFL, INPUT_2V, OUTPUT_V, _R | _H | _C | _S }, |
| 113 | { "RSQ", OPCODE_RSQ, INPUT_1S, OUTPUT_S, _R | _H | _C | _S }, |
| 114 | { "SEQ", OPCODE_SEQ, INPUT_2V, OUTPUT_V, _R | _H | _X | _C | _S }, |
| 115 | { "SFL", OPCODE_SFL, INPUT_2V, OUTPUT_V, _R | _H | _X | _C | _S }, |
| 116 | { "SGE", OPCODE_SGE, INPUT_2V, OUTPUT_V, _R | _H | _X | _C | _S }, |
| 117 | { "SGT", OPCODE_SGT, INPUT_2V, OUTPUT_V, _R | _H | _X | _C | _S }, |
| 118 | { "SIN", OPCODE_SIN, INPUT_1S, OUTPUT_S, _R | _H | _C | _S }, |
| 119 | { "SLE", OPCODE_SLE, INPUT_2V, OUTPUT_V, _R | _H | _X | _C | _S }, |
| 120 | { "SLT", OPCODE_SLT, INPUT_2V, OUTPUT_V, _R | _H | _X | _C | _S }, |
| 121 | { "SNE", OPCODE_SNE, INPUT_2V, OUTPUT_V, _R | _H | _X | _C | _S }, |
| 122 | { "STR", OPCODE_STR, INPUT_2V, OUTPUT_V, _R | _H | _X | _C | _S }, |
| 123 | { "SUB", OPCODE_SUB, INPUT_2V, OUTPUT_V, _R | _H | _X | _C | _S }, |
| 124 | { "TEX", OPCODE_TEX, INPUT_1V_T, OUTPUT_V, _C | _S }, |
| 125 | { "TXD", OPCODE_TXD, INPUT_3V_T, OUTPUT_V, _C | _S }, |
| 126 | { "TXP", OPCODE_TXP_NV, INPUT_1V_T, OUTPUT_V, _C | _S }, |
| 127 | { "UP2H", OPCODE_UP2H, INPUT_1S, OUTPUT_V, _C | _S }, |
| 128 | { "UP2US", OPCODE_UP2US, INPUT_1S, OUTPUT_V, _C | _S }, |
| 129 | { "UP4B", OPCODE_UP4B, INPUT_1S, OUTPUT_V, _C | _S }, |
| 130 | { "UP4UB", OPCODE_UP4UB, INPUT_1S, OUTPUT_V, _C | _S }, |
| 131 | { "X2D", OPCODE_X2D, INPUT_3V, OUTPUT_V, _R | _H | _C | _S }, |
| 132 | { "PRINT", OPCODE_PRINT, INPUT_1V_S, OUTPUT_NONE, 0 }, |
| 133 | { NULL, (enum prog_opcode) -1, 0, 0, 0 } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | |
| 137 | /* |
| 138 | * Information needed or computed during parsing. |
| 139 | * Remember, we can't modify the target program object until we've |
| 140 | * _successfully_ parsed the program text. |
| 141 | */ |
| 142 | struct parse_state { |
| 143 | GLcontext *ctx; |
| 144 | const GLubyte *start; /* start of program string */ |
| 145 | const GLubyte *pos; /* current position */ |
| 146 | const GLubyte *curLine; |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 147 | struct gl_fragment_program *program; /* current program */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 148 | |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 149 | struct gl_program_parameter_list *parameters; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 150 | |
| 151 | GLuint numInst; /* number of instructions parsed */ |
| 152 | GLuint inputsRead; /* bitmask of input registers used */ |
| 153 | GLuint outputsWritten; /* bitmask of 1 << FRAG_OUTPUT_* bits */ |
| 154 | GLuint texturesUsed[MAX_TEXTURE_IMAGE_UNITS]; |
| 155 | }; |
| 156 | |
| 157 | |
| 158 | |
| 159 | /* |
| 160 | * Called whenever we find an error during parsing. |
| 161 | */ |
| 162 | static void |
| 163 | record_error(struct parse_state *parseState, const char *msg, int lineNo) |
| 164 | { |
| 165 | #ifdef DEBUG |
| 166 | GLint line, column; |
| 167 | const GLubyte *lineStr; |
| 168 | lineStr = _mesa_find_line_column(parseState->start, |
| 169 | parseState->pos, &line, &column); |
| 170 | _mesa_debug(parseState->ctx, |
| 171 | "nvfragparse.c(%d): line %d, column %d:%s (%s)\n", |
| 172 | lineNo, line, column, (char *) lineStr, msg); |
| 173 | _mesa_free((void *) lineStr); |
| 174 | #else |
| 175 | (void) lineNo; |
| 176 | #endif |
| 177 | |
| 178 | /* Check that no error was already recorded. Only record the first one. */ |
| 179 | if (parseState->ctx->Program.ErrorString[0] == 0) { |
| 180 | _mesa_set_program_error(parseState->ctx, |
| 181 | parseState->pos - parseState->start, |
| 182 | msg); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | |
| 187 | #define RETURN_ERROR \ |
| 188 | do { \ |
| 189 | record_error(parseState, "Unexpected end of input.", __LINE__); \ |
| 190 | return GL_FALSE; \ |
| 191 | } while(0) |
| 192 | |
| 193 | #define RETURN_ERROR1(msg) \ |
| 194 | do { \ |
| 195 | record_error(parseState, msg, __LINE__); \ |
| 196 | return GL_FALSE; \ |
| 197 | } while(0) |
| 198 | |
| 199 | #define RETURN_ERROR2(msg1, msg2) \ |
| 200 | do { \ |
| 201 | char err[1000]; \ |
| 202 | _mesa_sprintf(err, "%s %s", msg1, msg2); \ |
| 203 | record_error(parseState, err, __LINE__); \ |
| 204 | return GL_FALSE; \ |
| 205 | } while(0) |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | /* |
| 211 | * Search a list of instruction structures for a match. |
| 212 | */ |
| 213 | static struct instruction_pattern |
| 214 | MatchInstruction(const GLubyte *token) |
| 215 | { |
| 216 | const struct instruction_pattern *inst; |
| 217 | struct instruction_pattern result; |
| 218 | |
| 219 | for (inst = Instructions; inst->name; inst++) { |
| 220 | if (_mesa_strncmp((const char *) token, inst->name, 3) == 0) { |
| 221 | /* matched! */ |
| 222 | int i = 3; |
| 223 | result = *inst; |
| 224 | result.suffixes = 0; |
| 225 | /* look at suffix */ |
| 226 | if (token[i] == 'R') { |
| 227 | result.suffixes |= _R; |
| 228 | i++; |
| 229 | } |
| 230 | else if (token[i] == 'H') { |
| 231 | result.suffixes |= _H; |
| 232 | i++; |
| 233 | } |
| 234 | else if (token[i] == 'X') { |
| 235 | result.suffixes |= _X; |
| 236 | i++; |
| 237 | } |
| 238 | if (token[i] == 'C') { |
| 239 | result.suffixes |= _C; |
| 240 | i++; |
| 241 | } |
| 242 | if (token[i] == '_' && token[i+1] == 'S' && |
| 243 | token[i+2] == 'A' && token[i+3] == 'T') { |
| 244 | result.suffixes |= _S; |
| 245 | } |
| 246 | return result; |
| 247 | } |
| 248 | } |
Brian Paul | 28b014e | 2006-04-05 03:05:17 +0000 | [diff] [blame] | 249 | result.opcode = MAX_OPCODE; /* i.e. invalid instruction */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 250 | return result; |
| 251 | } |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | /**********************************************************************/ |
| 257 | |
| 258 | |
| 259 | static GLboolean IsLetter(GLubyte b) |
| 260 | { |
| 261 | return (b >= 'a' && b <= 'z') || |
| 262 | (b >= 'A' && b <= 'Z') || |
| 263 | (b == '_') || |
| 264 | (b == '$'); |
| 265 | } |
| 266 | |
| 267 | |
| 268 | static GLboolean IsDigit(GLubyte b) |
| 269 | { |
| 270 | return b >= '0' && b <= '9'; |
| 271 | } |
| 272 | |
| 273 | |
| 274 | static GLboolean IsWhitespace(GLubyte b) |
| 275 | { |
| 276 | return b == ' ' || b == '\t' || b == '\n' || b == '\r'; |
| 277 | } |
| 278 | |
| 279 | |
| 280 | /** |
| 281 | * Starting at 'str' find the next token. A token can be an integer, |
| 282 | * an identifier or punctuation symbol. |
| 283 | * \return <= 0 we found an error, else, return number of characters parsed. |
| 284 | */ |
| 285 | static GLint |
| 286 | GetToken(struct parse_state *parseState, GLubyte *token) |
| 287 | { |
| 288 | const GLubyte *str = parseState->pos; |
| 289 | GLint i = 0, j = 0; |
| 290 | |
| 291 | token[0] = 0; |
| 292 | |
| 293 | /* skip whitespace and comments */ |
| 294 | while (str[i] && (IsWhitespace(str[i]) || str[i] == '#')) { |
| 295 | if (str[i] == '#') { |
| 296 | /* skip comment */ |
| 297 | while (str[i] && (str[i] != '\n' && str[i] != '\r')) { |
| 298 | i++; |
| 299 | } |
| 300 | if (str[i] == '\n' || str[i] == '\r') |
| 301 | parseState->curLine = str + i + 1; |
| 302 | } |
| 303 | else { |
| 304 | /* skip whitespace */ |
| 305 | if (str[i] == '\n' || str[i] == '\r') |
| 306 | parseState->curLine = str + i + 1; |
| 307 | i++; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | if (str[i] == 0) |
| 312 | return -i; |
| 313 | |
| 314 | /* try matching an integer */ |
| 315 | while (str[i] && IsDigit(str[i])) { |
| 316 | token[j++] = str[i++]; |
| 317 | } |
| 318 | if (j > 0 || !str[i]) { |
| 319 | token[j] = 0; |
| 320 | return i; |
| 321 | } |
| 322 | |
| 323 | /* try matching an identifier */ |
| 324 | if (IsLetter(str[i])) { |
| 325 | while (str[i] && (IsLetter(str[i]) || IsDigit(str[i]))) { |
| 326 | token[j++] = str[i++]; |
| 327 | } |
| 328 | token[j] = 0; |
| 329 | return i; |
| 330 | } |
| 331 | |
| 332 | /* punctuation character */ |
| 333 | if (str[i]) { |
| 334 | token[0] = str[i++]; |
| 335 | token[1] = 0; |
| 336 | return i; |
| 337 | } |
| 338 | |
| 339 | /* end of input */ |
| 340 | token[0] = 0; |
| 341 | return i; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | /** |
| 346 | * Get next token from input stream and increment stream pointer past token. |
| 347 | */ |
| 348 | static GLboolean |
| 349 | Parse_Token(struct parse_state *parseState, GLubyte *token) |
| 350 | { |
| 351 | GLint i; |
| 352 | i = GetToken(parseState, token); |
| 353 | if (i <= 0) { |
| 354 | parseState->pos += (-i); |
| 355 | return GL_FALSE; |
| 356 | } |
| 357 | parseState->pos += i; |
| 358 | return GL_TRUE; |
| 359 | } |
| 360 | |
| 361 | |
| 362 | /** |
| 363 | * Get next token from input stream but don't increment stream pointer. |
| 364 | */ |
| 365 | static GLboolean |
| 366 | Peek_Token(struct parse_state *parseState, GLubyte *token) |
| 367 | { |
| 368 | GLint i, len; |
| 369 | i = GetToken(parseState, token); |
| 370 | if (i <= 0) { |
| 371 | parseState->pos += (-i); |
| 372 | return GL_FALSE; |
| 373 | } |
Karl Schultz | 6258b76 | 2005-05-05 21:08:07 +0000 | [diff] [blame] | 374 | len = (GLint)_mesa_strlen((const char *) token); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 375 | parseState->pos += (i - len); |
| 376 | return GL_TRUE; |
| 377 | } |
| 378 | |
| 379 | |
| 380 | /**********************************************************************/ |
| 381 | |
| 382 | static const char *InputRegisters[MAX_NV_FRAGMENT_PROGRAM_INPUTS + 1] = { |
| 383 | "WPOS", "COL0", "COL1", "FOGC", |
| 384 | "TEX0", "TEX1", "TEX2", "TEX3", "TEX4", "TEX5", "TEX6", "TEX7", NULL |
| 385 | }; |
| 386 | |
| 387 | static const char *OutputRegisters[MAX_NV_FRAGMENT_PROGRAM_OUTPUTS + 1] = { |
| 388 | "COLR", "COLH", |
| 389 | /* These are only allows for register combiners */ |
| 390 | /* |
| 391 | "TEX0", "TEX1", "TEX2", "TEX3", |
| 392 | */ |
| 393 | "DEPR", NULL |
| 394 | }; |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | /**********************************************************************/ |
| 400 | |
| 401 | /** |
| 402 | * Try to match 'pattern' as the next token after any whitespace/comments. |
| 403 | */ |
| 404 | static GLboolean |
| 405 | Parse_String(struct parse_state *parseState, const char *pattern) |
| 406 | { |
| 407 | const GLubyte *m; |
| 408 | GLint i; |
| 409 | |
| 410 | /* skip whitespace and comments */ |
| 411 | while (IsWhitespace(*parseState->pos) || *parseState->pos == '#') { |
| 412 | if (*parseState->pos == '#') { |
| 413 | while (*parseState->pos && (*parseState->pos != '\n' && *parseState->pos != '\r')) { |
| 414 | parseState->pos += 1; |
| 415 | } |
| 416 | if (*parseState->pos == '\n' || *parseState->pos == '\r') |
| 417 | parseState->curLine = parseState->pos + 1; |
| 418 | } |
| 419 | else { |
| 420 | /* skip whitespace */ |
| 421 | if (*parseState->pos == '\n' || *parseState->pos == '\r') |
| 422 | parseState->curLine = parseState->pos + 1; |
| 423 | parseState->pos += 1; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | /* Try to match the pattern */ |
| 428 | m = parseState->pos; |
| 429 | for (i = 0; pattern[i]; i++) { |
| 430 | if (*m != (GLubyte) pattern[i]) |
| 431 | return GL_FALSE; |
| 432 | m += 1; |
| 433 | } |
| 434 | parseState->pos = m; |
| 435 | |
| 436 | return GL_TRUE; /* success */ |
| 437 | } |
| 438 | |
| 439 | |
| 440 | static GLboolean |
| 441 | Parse_Identifier(struct parse_state *parseState, GLubyte *ident) |
| 442 | { |
| 443 | if (!Parse_Token(parseState, ident)) |
| 444 | RETURN_ERROR; |
| 445 | if (IsLetter(ident[0])) |
| 446 | return GL_TRUE; |
| 447 | else |
| 448 | RETURN_ERROR1("Expected an identfier"); |
| 449 | } |
| 450 | |
| 451 | |
| 452 | /** |
| 453 | * Parse a floating point constant, or a defined symbol name. |
| 454 | * [+/-]N[.N[eN]] |
| 455 | * Output: number[0 .. 3] will get the value. |
| 456 | */ |
| 457 | static GLboolean |
| 458 | Parse_ScalarConstant(struct parse_state *parseState, GLfloat *number) |
| 459 | { |
| 460 | char *end = NULL; |
| 461 | |
| 462 | *number = (GLfloat) _mesa_strtod((const char *) parseState->pos, &end); |
| 463 | |
| 464 | if (end && end > (char *) parseState->pos) { |
| 465 | /* got a number */ |
| 466 | parseState->pos = (GLubyte *) end; |
| 467 | number[1] = *number; |
| 468 | number[2] = *number; |
| 469 | number[3] = *number; |
| 470 | return GL_TRUE; |
| 471 | } |
| 472 | else { |
| 473 | /* should be an identifier */ |
| 474 | GLubyte ident[100]; |
| 475 | const GLfloat *constant; |
| 476 | if (!Parse_Identifier(parseState, ident)) |
| 477 | RETURN_ERROR1("Expected an identifier"); |
| 478 | constant = _mesa_lookup_parameter_value(parseState->parameters, |
| 479 | -1, (const char *) ident); |
| 480 | /* XXX Check that it's a constant and not a parameter */ |
| 481 | if (!constant) { |
| 482 | RETURN_ERROR1("Undefined symbol"); |
| 483 | } |
| 484 | else { |
| 485 | COPY_4V(number, constant); |
| 486 | return GL_TRUE; |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | |
| 492 | |
| 493 | /** |
| 494 | * Parse a vector constant, one of: |
| 495 | * { float } |
| 496 | * { float, float } |
| 497 | * { float, float, float } |
| 498 | * { float, float, float, float } |
| 499 | */ |
| 500 | static GLboolean |
| 501 | Parse_VectorConstant(struct parse_state *parseState, GLfloat *vec) |
| 502 | { |
| 503 | /* "{" was already consumed */ |
| 504 | |
| 505 | ASSIGN_4V(vec, 0.0, 0.0, 0.0, 1.0); |
| 506 | |
| 507 | if (!Parse_ScalarConstant(parseState, vec+0)) /* X */ |
| 508 | return GL_FALSE; |
| 509 | |
| 510 | if (Parse_String(parseState, "}")) { |
| 511 | return GL_TRUE; |
| 512 | } |
| 513 | |
| 514 | if (!Parse_String(parseState, ",")) |
| 515 | RETURN_ERROR1("Expected comma in vector constant"); |
| 516 | |
| 517 | if (!Parse_ScalarConstant(parseState, vec+1)) /* Y */ |
| 518 | return GL_FALSE; |
| 519 | |
| 520 | if (Parse_String(parseState, "}")) { |
| 521 | return GL_TRUE; |
| 522 | } |
| 523 | |
| 524 | if (!Parse_String(parseState, ",")) |
| 525 | RETURN_ERROR1("Expected comma in vector constant"); |
| 526 | |
| 527 | if (!Parse_ScalarConstant(parseState, vec+2)) /* Z */ |
| 528 | return GL_FALSE; |
| 529 | |
| 530 | if (Parse_String(parseState, "}")) { |
| 531 | return GL_TRUE; |
| 532 | } |
| 533 | |
| 534 | if (!Parse_String(parseState, ",")) |
| 535 | RETURN_ERROR1("Expected comma in vector constant"); |
| 536 | |
| 537 | if (!Parse_ScalarConstant(parseState, vec+3)) /* W */ |
| 538 | return GL_FALSE; |
| 539 | |
| 540 | if (!Parse_String(parseState, "}")) |
| 541 | RETURN_ERROR1("Expected closing brace in vector constant"); |
| 542 | |
| 543 | return GL_TRUE; |
| 544 | } |
| 545 | |
| 546 | |
| 547 | /** |
| 548 | * Parse <number>, <varname> or {a, b, c, d}. |
| 549 | * Return number of values in the vector or scalar, or zero if parse error. |
| 550 | */ |
| 551 | static GLuint |
| 552 | Parse_VectorOrScalarConstant(struct parse_state *parseState, GLfloat *vec) |
| 553 | { |
| 554 | if (Parse_String(parseState, "{")) { |
| 555 | return Parse_VectorConstant(parseState, vec); |
| 556 | } |
| 557 | else { |
| 558 | GLboolean b = Parse_ScalarConstant(parseState, vec); |
| 559 | if (b) { |
| 560 | vec[1] = vec[2] = vec[3] = vec[0]; |
| 561 | } |
| 562 | return b; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | |
| 567 | /** |
| 568 | * Parse a texture image source: |
| 569 | * [TEX0 | TEX1 | .. | TEX15] , [1D | 2D | 3D | CUBE | RECT] |
| 570 | */ |
| 571 | static GLboolean |
| 572 | Parse_TextureImageId(struct parse_state *parseState, |
| 573 | GLubyte *texUnit, GLubyte *texTargetBit) |
| 574 | { |
| 575 | GLubyte imageSrc[100]; |
| 576 | GLint unit; |
| 577 | |
| 578 | if (!Parse_Token(parseState, imageSrc)) |
| 579 | RETURN_ERROR; |
| 580 | |
| 581 | if (imageSrc[0] != 'T' || |
| 582 | imageSrc[1] != 'E' || |
| 583 | imageSrc[2] != 'X') { |
| 584 | RETURN_ERROR1("Expected TEX# source"); |
| 585 | } |
| 586 | unit = _mesa_atoi((const char *) imageSrc + 3); |
| 587 | if ((unit < 0 || unit > MAX_TEXTURE_IMAGE_UNITS) || |
| 588 | (unit == 0 && (imageSrc[3] != '0' || imageSrc[4] != 0))) { |
| 589 | RETURN_ERROR1("Invalied TEX# source index"); |
| 590 | } |
| 591 | *texUnit = unit; |
| 592 | |
| 593 | if (!Parse_String(parseState, ",")) |
| 594 | RETURN_ERROR1("Expected ,"); |
| 595 | |
| 596 | if (Parse_String(parseState, "1D")) { |
| 597 | *texTargetBit = TEXTURE_1D_BIT; |
| 598 | } |
| 599 | else if (Parse_String(parseState, "2D")) { |
| 600 | *texTargetBit = TEXTURE_2D_BIT; |
| 601 | } |
| 602 | else if (Parse_String(parseState, "3D")) { |
| 603 | *texTargetBit = TEXTURE_3D_BIT; |
| 604 | } |
| 605 | else if (Parse_String(parseState, "CUBE")) { |
| 606 | *texTargetBit = TEXTURE_CUBE_BIT; |
| 607 | } |
| 608 | else if (Parse_String(parseState, "RECT")) { |
| 609 | *texTargetBit = TEXTURE_RECT_BIT; |
| 610 | } |
| 611 | else { |
| 612 | RETURN_ERROR1("Invalid texture target token"); |
| 613 | } |
| 614 | |
| 615 | /* update record of referenced texture units */ |
| 616 | parseState->texturesUsed[*texUnit] |= *texTargetBit; |
| 617 | if (_mesa_bitcount(parseState->texturesUsed[*texUnit]) > 1) { |
| 618 | RETURN_ERROR1("Only one texture target can be used per texture unit."); |
| 619 | } |
| 620 | |
| 621 | return GL_TRUE; |
| 622 | } |
| 623 | |
| 624 | |
| 625 | /** |
| 626 | * Parse a scalar suffix like .x, .y, .z or .w or parse a swizzle suffix |
| 627 | * like .wxyz, .xxyy, etc and return the swizzle indexes. |
| 628 | */ |
| 629 | static GLboolean |
| 630 | Parse_SwizzleSuffix(const GLubyte *token, GLuint swizzle[4]) |
| 631 | { |
| 632 | if (token[1] == 0) { |
| 633 | /* single letter swizzle (scalar) */ |
| 634 | if (token[0] == 'x') |
| 635 | ASSIGN_4V(swizzle, 0, 0, 0, 0); |
| 636 | else if (token[0] == 'y') |
| 637 | ASSIGN_4V(swizzle, 1, 1, 1, 1); |
| 638 | else if (token[0] == 'z') |
| 639 | ASSIGN_4V(swizzle, 2, 2, 2, 2); |
| 640 | else if (token[0] == 'w') |
| 641 | ASSIGN_4V(swizzle, 3, 3, 3, 3); |
| 642 | else |
| 643 | return GL_FALSE; |
| 644 | } |
| 645 | else { |
| 646 | /* 4-component swizzle (vector) */ |
| 647 | GLint k; |
| 648 | for (k = 0; token[k] && k < 4; k++) { |
| 649 | if (token[k] == 'x') |
| 650 | swizzle[k] = 0; |
| 651 | else if (token[k] == 'y') |
| 652 | swizzle[k] = 1; |
| 653 | else if (token[k] == 'z') |
| 654 | swizzle[k] = 2; |
| 655 | else if (token[k] == 'w') |
| 656 | swizzle[k] = 3; |
| 657 | else |
| 658 | return GL_FALSE; |
| 659 | } |
| 660 | if (k != 4) |
| 661 | return GL_FALSE; |
| 662 | } |
| 663 | return GL_TRUE; |
| 664 | } |
| 665 | |
| 666 | |
| 667 | static GLboolean |
| 668 | Parse_CondCodeMask(struct parse_state *parseState, |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 669 | struct prog_dst_register *dstReg) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 670 | { |
| 671 | if (Parse_String(parseState, "EQ")) |
| 672 | dstReg->CondMask = COND_EQ; |
| 673 | else if (Parse_String(parseState, "GE")) |
| 674 | dstReg->CondMask = COND_GE; |
| 675 | else if (Parse_String(parseState, "GT")) |
| 676 | dstReg->CondMask = COND_GT; |
| 677 | else if (Parse_String(parseState, "LE")) |
| 678 | dstReg->CondMask = COND_LE; |
| 679 | else if (Parse_String(parseState, "LT")) |
| 680 | dstReg->CondMask = COND_LT; |
| 681 | else if (Parse_String(parseState, "NE")) |
| 682 | dstReg->CondMask = COND_NE; |
| 683 | else if (Parse_String(parseState, "TR")) |
| 684 | dstReg->CondMask = COND_TR; |
| 685 | else if (Parse_String(parseState, "FL")) |
| 686 | dstReg->CondMask = COND_FL; |
| 687 | else |
| 688 | RETURN_ERROR1("Invalid condition code mask"); |
| 689 | |
| 690 | /* look for optional .xyzw swizzle */ |
| 691 | if (Parse_String(parseState, ".")) { |
| 692 | GLubyte token[100]; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 693 | GLuint swz[4]; |
| 694 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 695 | if (!Parse_Token(parseState, token)) /* get xyzw suffix */ |
| 696 | RETURN_ERROR; |
| 697 | |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 698 | if (!Parse_SwizzleSuffix(token, swz)) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 699 | RETURN_ERROR1("Invalid swizzle suffix"); |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 700 | |
Brian Paul | fd4395b | 2005-11-05 03:02:28 +0000 | [diff] [blame] | 701 | dstReg->CondSwizzle = MAKE_SWIZZLE4(swz[0], swz[1], swz[2], swz[3]); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | return GL_TRUE; |
| 705 | } |
| 706 | |
| 707 | |
| 708 | /** |
| 709 | * Parse a temporary register: Rnn or Hnn |
| 710 | */ |
| 711 | static GLboolean |
| 712 | Parse_TempReg(struct parse_state *parseState, GLint *tempRegNum) |
| 713 | { |
| 714 | GLubyte token[100]; |
| 715 | |
| 716 | /* Should be 'R##' or 'H##' */ |
| 717 | if (!Parse_Token(parseState, token)) |
| 718 | RETURN_ERROR; |
| 719 | if (token[0] != 'R' && token[0] != 'H') |
| 720 | RETURN_ERROR1("Expected R## or H##"); |
| 721 | |
| 722 | if (IsDigit(token[1])) { |
| 723 | GLint reg = _mesa_atoi((const char *) (token + 1)); |
| 724 | if (token[0] == 'H') |
| 725 | reg += 32; |
| 726 | if (reg >= MAX_NV_FRAGMENT_PROGRAM_TEMPS) |
| 727 | RETURN_ERROR1("Invalid temporary register name"); |
| 728 | *tempRegNum = reg; |
| 729 | } |
| 730 | else { |
| 731 | RETURN_ERROR1("Invalid temporary register name"); |
| 732 | } |
| 733 | |
| 734 | return GL_TRUE; |
| 735 | } |
| 736 | |
| 737 | |
| 738 | /** |
| 739 | * Parse a write-only dummy register: RC or HC. |
| 740 | */ |
| 741 | static GLboolean |
| 742 | Parse_DummyReg(struct parse_state *parseState, GLint *regNum) |
| 743 | { |
| 744 | if (Parse_String(parseState, "RC")) { |
| 745 | *regNum = 0; |
| 746 | } |
| 747 | else if (Parse_String(parseState, "HC")) { |
| 748 | *regNum = 1; |
| 749 | } |
| 750 | else { |
| 751 | RETURN_ERROR1("Invalid write-only register name"); |
| 752 | } |
| 753 | |
| 754 | return GL_TRUE; |
| 755 | } |
| 756 | |
| 757 | |
| 758 | /** |
| 759 | * Parse a program local parameter register "p[##]" |
| 760 | */ |
| 761 | static GLboolean |
| 762 | Parse_ProgramParamReg(struct parse_state *parseState, GLint *regNum) |
| 763 | { |
| 764 | GLubyte token[100]; |
| 765 | |
| 766 | if (!Parse_String(parseState, "p[")) |
| 767 | RETURN_ERROR1("Expected p["); |
| 768 | |
| 769 | if (!Parse_Token(parseState, token)) |
| 770 | RETURN_ERROR; |
| 771 | |
| 772 | if (IsDigit(token[0])) { |
| 773 | /* a numbered program parameter register */ |
| 774 | GLint reg = _mesa_atoi((const char *) token); |
| 775 | if (reg >= MAX_NV_FRAGMENT_PROGRAM_PARAMS) |
| 776 | RETURN_ERROR1("Invalid constant program number"); |
| 777 | *regNum = reg; |
| 778 | } |
| 779 | else { |
| 780 | RETURN_ERROR; |
| 781 | } |
| 782 | |
| 783 | if (!Parse_String(parseState, "]")) |
| 784 | RETURN_ERROR1("Expected ]"); |
| 785 | |
| 786 | return GL_TRUE; |
| 787 | } |
| 788 | |
| 789 | |
| 790 | /** |
| 791 | * Parse f[name] - fragment input register |
| 792 | */ |
| 793 | static GLboolean |
| 794 | Parse_FragReg(struct parse_state *parseState, GLint *tempRegNum) |
| 795 | { |
| 796 | GLubyte token[100]; |
| 797 | GLint j; |
| 798 | |
| 799 | /* Match 'f[' */ |
| 800 | if (!Parse_String(parseState, "f[")) |
| 801 | RETURN_ERROR1("Expected f["); |
| 802 | |
| 803 | /* get <name> and look for match */ |
| 804 | if (!Parse_Token(parseState, token)) { |
| 805 | RETURN_ERROR; |
| 806 | } |
| 807 | for (j = 0; InputRegisters[j]; j++) { |
| 808 | if (_mesa_strcmp((const char *) token, InputRegisters[j]) == 0) { |
| 809 | *tempRegNum = j; |
| 810 | parseState->inputsRead |= (1 << j); |
| 811 | break; |
| 812 | } |
| 813 | } |
| 814 | if (!InputRegisters[j]) { |
| 815 | /* unknown input register label */ |
| 816 | RETURN_ERROR2("Invalid register name", token); |
| 817 | } |
| 818 | |
| 819 | /* Match '[' */ |
| 820 | if (!Parse_String(parseState, "]")) |
| 821 | RETURN_ERROR1("Expected ]"); |
| 822 | |
| 823 | return GL_TRUE; |
| 824 | } |
| 825 | |
| 826 | |
| 827 | static GLboolean |
| 828 | Parse_OutputReg(struct parse_state *parseState, GLint *outputRegNum) |
| 829 | { |
| 830 | GLubyte token[100]; |
| 831 | GLint j; |
| 832 | |
| 833 | /* Match "o[" */ |
| 834 | if (!Parse_String(parseState, "o[")) |
| 835 | RETURN_ERROR1("Expected o["); |
| 836 | |
| 837 | /* Get output reg name */ |
| 838 | if (!Parse_Token(parseState, token)) |
| 839 | RETURN_ERROR; |
| 840 | |
| 841 | /* try to match an output register name */ |
| 842 | for (j = 0; OutputRegisters[j]; j++) { |
| 843 | if (_mesa_strcmp((const char *) token, OutputRegisters[j]) == 0) { |
Brian Paul | 90ebb58 | 2005-11-02 18:06:12 +0000 | [diff] [blame] | 844 | static GLuint bothColors = (1 << FRAG_RESULT_COLR) | (1 << FRAG_RESULT_COLH); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 845 | *outputRegNum = j; |
| 846 | parseState->outputsWritten |= (1 << j); |
| 847 | if ((parseState->outputsWritten & bothColors) == bothColors) { |
| 848 | RETURN_ERROR1("Illegal to write to both o[COLR] and o[COLH]"); |
| 849 | } |
| 850 | break; |
| 851 | } |
| 852 | } |
| 853 | if (!OutputRegisters[j]) |
| 854 | RETURN_ERROR1("Invalid output register name"); |
| 855 | |
| 856 | /* Match ']' */ |
| 857 | if (!Parse_String(parseState, "]")) |
| 858 | RETURN_ERROR1("Expected ]"); |
| 859 | |
| 860 | return GL_TRUE; |
| 861 | } |
| 862 | |
| 863 | |
| 864 | static GLboolean |
| 865 | Parse_MaskedDstReg(struct parse_state *parseState, |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 866 | struct prog_dst_register *dstReg) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 867 | { |
| 868 | GLubyte token[100]; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 869 | GLint idx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 870 | |
| 871 | /* Dst reg can be R<n>, H<n>, o[n], RC or HC */ |
| 872 | if (!Peek_Token(parseState, token)) |
| 873 | RETURN_ERROR; |
| 874 | |
| 875 | if (_mesa_strcmp((const char *) token, "RC") == 0 || |
| 876 | _mesa_strcmp((const char *) token, "HC") == 0) { |
| 877 | /* a write-only register */ |
| 878 | dstReg->File = PROGRAM_WRITE_ONLY; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 879 | if (!Parse_DummyReg(parseState, &idx)) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 880 | RETURN_ERROR; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 881 | dstReg->Index = idx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 882 | } |
| 883 | else if (token[0] == 'R' || token[0] == 'H') { |
| 884 | /* a temporary register */ |
| 885 | dstReg->File = PROGRAM_TEMPORARY; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 886 | if (!Parse_TempReg(parseState, &idx)) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 887 | RETURN_ERROR; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 888 | dstReg->Index = idx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 889 | } |
| 890 | else if (token[0] == 'o') { |
| 891 | /* an output register */ |
| 892 | dstReg->File = PROGRAM_OUTPUT; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 893 | if (!Parse_OutputReg(parseState, &idx)) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 894 | RETURN_ERROR; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 895 | dstReg->Index = idx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 896 | } |
| 897 | else { |
| 898 | RETURN_ERROR1("Invalid destination register name"); |
| 899 | } |
| 900 | |
| 901 | /* Parse optional write mask */ |
| 902 | if (Parse_String(parseState, ".")) { |
| 903 | /* got a mask */ |
| 904 | GLint k = 0; |
| 905 | |
| 906 | if (!Parse_Token(parseState, token)) /* get xyzw writemask */ |
| 907 | RETURN_ERROR; |
| 908 | |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 909 | dstReg->WriteMask = 0; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 910 | |
| 911 | if (token[k] == 'x') { |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 912 | dstReg->WriteMask |= WRITEMASK_X; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 913 | k++; |
| 914 | } |
| 915 | if (token[k] == 'y') { |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 916 | dstReg->WriteMask |= WRITEMASK_Y; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 917 | k++; |
| 918 | } |
| 919 | if (token[k] == 'z') { |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 920 | dstReg->WriteMask |= WRITEMASK_Z; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 921 | k++; |
| 922 | } |
| 923 | if (token[k] == 'w') { |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 924 | dstReg->WriteMask |= WRITEMASK_W; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 925 | k++; |
| 926 | } |
| 927 | if (k == 0) { |
| 928 | RETURN_ERROR1("Invalid writemask character"); |
| 929 | } |
| 930 | |
| 931 | } |
| 932 | else { |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 933 | dstReg->WriteMask = WRITEMASK_XYZW; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | /* optional condition code mask */ |
| 937 | if (Parse_String(parseState, "(")) { |
| 938 | /* ("EQ" | "GE" | "GT" | "LE" | "LT" | "NE" | "TR" | "FL".x|y|z|w) */ |
| 939 | /* ("EQ" | "GE" | "GT" | "LE" | "LT" | "NE" | "TR" | "FL".[xyzw]) */ |
| 940 | if (!Parse_CondCodeMask(parseState, dstReg)) |
| 941 | RETURN_ERROR; |
| 942 | |
| 943 | if (!Parse_String(parseState, ")")) /* consume ")" */ |
| 944 | RETURN_ERROR1("Expected )"); |
| 945 | |
| 946 | return GL_TRUE; |
| 947 | } |
| 948 | else { |
| 949 | /* no cond code mask */ |
| 950 | dstReg->CondMask = COND_TR; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 951 | dstReg->CondSwizzle = SWIZZLE_NOOP; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 952 | return GL_TRUE; |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | |
| 957 | /** |
| 958 | * Parse a vector source (register, constant, etc): |
| 959 | * <vectorSrc> ::= <absVectorSrc> |
| 960 | * | <baseVectorSrc> |
| 961 | * <absVectorSrc> ::= <negate> "|" <baseVectorSrc> "|" |
| 962 | */ |
| 963 | static GLboolean |
| 964 | Parse_VectorSrc(struct parse_state *parseState, |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 965 | struct prog_src_register *srcReg) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 966 | { |
| 967 | GLfloat sign = 1.0F; |
| 968 | GLubyte token[100]; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 969 | GLint idx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 970 | |
| 971 | /* |
| 972 | * First, take care of +/- and absolute value stuff. |
| 973 | */ |
| 974 | if (Parse_String(parseState, "-")) |
| 975 | sign = -1.0F; |
| 976 | else if (Parse_String(parseState, "+")) |
| 977 | sign = +1.0F; |
| 978 | |
| 979 | if (Parse_String(parseState, "|")) { |
| 980 | srcReg->Abs = GL_TRUE; |
| 981 | srcReg->NegateAbs = (sign < 0.0F) ? GL_TRUE : GL_FALSE; |
| 982 | |
| 983 | if (Parse_String(parseState, "-")) |
Brian Paul | a8c4242 | 2006-05-30 22:17:35 +0000 | [diff] [blame] | 984 | srcReg->NegateBase = NEGATE_XYZW; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 985 | else if (Parse_String(parseState, "+")) |
Brian Paul | a8c4242 | 2006-05-30 22:17:35 +0000 | [diff] [blame] | 986 | srcReg->NegateBase = NEGATE_NONE; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 987 | else |
Brian Paul | a8c4242 | 2006-05-30 22:17:35 +0000 | [diff] [blame] | 988 | srcReg->NegateBase = NEGATE_NONE; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 989 | } |
| 990 | else { |
| 991 | srcReg->Abs = GL_FALSE; |
| 992 | srcReg->NegateAbs = GL_FALSE; |
Brian Paul | a8c4242 | 2006-05-30 22:17:35 +0000 | [diff] [blame] | 993 | srcReg->NegateBase = (sign < 0.0F) ? NEGATE_XYZW : NEGATE_NONE; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /* This should be the real src vector/register name */ |
| 997 | if (!Peek_Token(parseState, token)) |
| 998 | RETURN_ERROR; |
| 999 | |
| 1000 | /* Src reg can be Rn, Hn, f[n], p[n], a named parameter, a scalar |
| 1001 | * literal or vector literal. |
| 1002 | */ |
| 1003 | if (token[0] == 'R' || token[0] == 'H') { |
| 1004 | srcReg->File = PROGRAM_TEMPORARY; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1005 | if (!Parse_TempReg(parseState, &idx)) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1006 | RETURN_ERROR; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1007 | srcReg->Index = idx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1008 | } |
| 1009 | else if (token[0] == 'f') { |
Brian Paul | e6940f0 | 2006-08-24 23:08:01 +0000 | [diff] [blame] | 1010 | /* XXX this might be an identifier! */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1011 | srcReg->File = PROGRAM_INPUT; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1012 | if (!Parse_FragReg(parseState, &idx)) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1013 | RETURN_ERROR; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1014 | srcReg->Index = idx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1015 | } |
| 1016 | else if (token[0] == 'p') { |
Brian Paul | e6940f0 | 2006-08-24 23:08:01 +0000 | [diff] [blame] | 1017 | /* XXX this might be an identifier! */ |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1018 | srcReg->File = PROGRAM_LOCAL_PARAM; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1019 | if (!Parse_ProgramParamReg(parseState, &idx)) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1020 | RETURN_ERROR; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1021 | srcReg->Index = idx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1022 | } |
| 1023 | else if (IsLetter(token[0])){ |
| 1024 | GLubyte ident[100]; |
| 1025 | GLint paramIndex; |
| 1026 | if (!Parse_Identifier(parseState, ident)) |
| 1027 | RETURN_ERROR; |
| 1028 | paramIndex = _mesa_lookup_parameter_index(parseState->parameters, |
| 1029 | -1, (const char *) ident); |
| 1030 | if (paramIndex < 0) { |
| 1031 | RETURN_ERROR2("Undefined constant or parameter: ", ident); |
| 1032 | } |
| 1033 | srcReg->File = PROGRAM_NAMED_PARAM; |
| 1034 | srcReg->Index = paramIndex; |
| 1035 | } |
| 1036 | else if (IsDigit(token[0]) || token[0] == '-' || token[0] == '+' || token[0] == '.'){ |
| 1037 | /* literal scalar constant */ |
| 1038 | GLfloat values[4]; |
Brian | fe1d01c | 2006-12-13 14:54:47 -0700 | [diff] [blame] | 1039 | GLuint paramIndex, swizzle; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1040 | if (!Parse_ScalarConstant(parseState, values)) |
| 1041 | RETURN_ERROR; |
Brian | fe1d01c | 2006-12-13 14:54:47 -0700 | [diff] [blame] | 1042 | paramIndex = _mesa_add_unnamed_constant(parseState->parameters, |
Brian | 1bf81e3 | 2007-03-22 09:07:27 -0600 | [diff] [blame] | 1043 | values, 4, NULL); |
Brian | fe1d01c | 2006-12-13 14:54:47 -0700 | [diff] [blame] | 1044 | ASSERT(swizzle == SWIZZLE_NOOP); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1045 | srcReg->File = PROGRAM_NAMED_PARAM; |
| 1046 | srcReg->Index = paramIndex; |
| 1047 | } |
| 1048 | else if (token[0] == '{'){ |
| 1049 | /* literal vector constant */ |
| 1050 | GLfloat values[4]; |
Brian | fe1d01c | 2006-12-13 14:54:47 -0700 | [diff] [blame] | 1051 | GLuint paramIndex, swizzle; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1052 | (void) Parse_String(parseState, "{"); |
| 1053 | if (!Parse_VectorConstant(parseState, values)) |
| 1054 | RETURN_ERROR; |
Brian | fe1d01c | 2006-12-13 14:54:47 -0700 | [diff] [blame] | 1055 | paramIndex = _mesa_add_unnamed_constant(parseState->parameters, |
Brian | 1bf81e3 | 2007-03-22 09:07:27 -0600 | [diff] [blame] | 1056 | values, 4, NULL); |
Brian | fe1d01c | 2006-12-13 14:54:47 -0700 | [diff] [blame] | 1057 | ASSERT(swizzle == SWIZZLE_NOOP); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1058 | srcReg->File = PROGRAM_NAMED_PARAM; |
| 1059 | srcReg->Index = paramIndex; |
| 1060 | } |
| 1061 | else { |
| 1062 | RETURN_ERROR2("Invalid source register name", token); |
| 1063 | } |
| 1064 | |
| 1065 | /* init swizzle fields */ |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1066 | srcReg->Swizzle = SWIZZLE_NOOP; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1067 | |
| 1068 | /* Look for optional swizzle suffix */ |
| 1069 | if (Parse_String(parseState, ".")) { |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1070 | GLuint swz[4]; |
| 1071 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1072 | if (!Parse_Token(parseState, token)) |
| 1073 | RETURN_ERROR; |
| 1074 | |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1075 | if (!Parse_SwizzleSuffix(token, swz)) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1076 | RETURN_ERROR1("Invalid swizzle suffix"); |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1077 | |
Brian Paul | fd4395b | 2005-11-05 03:02:28 +0000 | [diff] [blame] | 1078 | srcReg->Swizzle = MAKE_SWIZZLE4(swz[0], swz[1], swz[2], swz[3]); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | /* Finish absolute value */ |
| 1082 | if (srcReg->Abs && !Parse_String(parseState, "|")) { |
| 1083 | RETURN_ERROR1("Expected |"); |
| 1084 | } |
| 1085 | |
| 1086 | return GL_TRUE; |
| 1087 | } |
| 1088 | |
| 1089 | |
| 1090 | static GLboolean |
| 1091 | Parse_ScalarSrcReg(struct parse_state *parseState, |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 1092 | struct prog_src_register *srcReg) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1093 | { |
| 1094 | GLubyte token[100]; |
| 1095 | GLfloat sign = 1.0F; |
| 1096 | GLboolean needSuffix = GL_TRUE; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1097 | GLint idx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1098 | |
| 1099 | /* |
| 1100 | * First, take care of +/- and absolute value stuff. |
| 1101 | */ |
| 1102 | if (Parse_String(parseState, "-")) |
| 1103 | sign = -1.0F; |
| 1104 | else if (Parse_String(parseState, "+")) |
| 1105 | sign = +1.0F; |
| 1106 | |
| 1107 | if (Parse_String(parseState, "|")) { |
| 1108 | srcReg->Abs = GL_TRUE; |
| 1109 | srcReg->NegateAbs = (sign < 0.0F) ? GL_TRUE : GL_FALSE; |
| 1110 | |
| 1111 | if (Parse_String(parseState, "-")) |
Brian Paul | a8c4242 | 2006-05-30 22:17:35 +0000 | [diff] [blame] | 1112 | srcReg->NegateBase = NEGATE_XYZW; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1113 | else if (Parse_String(parseState, "+")) |
Brian Paul | a8c4242 | 2006-05-30 22:17:35 +0000 | [diff] [blame] | 1114 | srcReg->NegateBase = NEGATE_NONE; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1115 | else |
Brian Paul | a8c4242 | 2006-05-30 22:17:35 +0000 | [diff] [blame] | 1116 | srcReg->NegateBase = NEGATE_NONE; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1117 | } |
| 1118 | else { |
| 1119 | srcReg->Abs = GL_FALSE; |
| 1120 | srcReg->NegateAbs = GL_FALSE; |
Brian Paul | a8c4242 | 2006-05-30 22:17:35 +0000 | [diff] [blame] | 1121 | srcReg->NegateBase = (sign < 0.0F) ? NEGATE_XYZW : NEGATE_NONE; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | if (!Peek_Token(parseState, token)) |
| 1125 | RETURN_ERROR; |
| 1126 | |
| 1127 | /* Src reg can be R<n>, H<n> or a named fragment attrib */ |
| 1128 | if (token[0] == 'R' || token[0] == 'H') { |
| 1129 | srcReg->File = PROGRAM_TEMPORARY; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1130 | if (!Parse_TempReg(parseState, &idx)) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1131 | RETURN_ERROR; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1132 | srcReg->Index = idx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1133 | } |
| 1134 | else if (token[0] == 'f') { |
| 1135 | srcReg->File = PROGRAM_INPUT; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1136 | if (!Parse_FragReg(parseState, &idx)) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1137 | RETURN_ERROR; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1138 | srcReg->Index = idx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1139 | } |
| 1140 | else if (token[0] == '{') { |
| 1141 | /* vector literal */ |
| 1142 | GLfloat values[4]; |
Brian | 81c4fee | 2007-04-20 11:53:48 -0600 | [diff] [blame^] | 1143 | GLuint paramIndex; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1144 | (void) Parse_String(parseState, "{"); |
| 1145 | if (!Parse_VectorConstant(parseState, values)) |
| 1146 | RETURN_ERROR; |
Brian | fe1d01c | 2006-12-13 14:54:47 -0700 | [diff] [blame] | 1147 | paramIndex = _mesa_add_unnamed_constant(parseState->parameters, |
Brian | 1bf81e3 | 2007-03-22 09:07:27 -0600 | [diff] [blame] | 1148 | values, 4, NULL); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1149 | srcReg->File = PROGRAM_NAMED_PARAM; |
| 1150 | srcReg->Index = paramIndex; |
| 1151 | } |
Brian Paul | e6940f0 | 2006-08-24 23:08:01 +0000 | [diff] [blame] | 1152 | else if (IsLetter(token[0])){ |
| 1153 | /* named param/constant */ |
| 1154 | GLubyte ident[100]; |
| 1155 | GLint paramIndex; |
| 1156 | if (!Parse_Identifier(parseState, ident)) |
| 1157 | RETURN_ERROR; |
| 1158 | paramIndex = _mesa_lookup_parameter_index(parseState->parameters, |
| 1159 | -1, (const char *) ident); |
| 1160 | if (paramIndex < 0) { |
| 1161 | RETURN_ERROR2("Undefined constant or parameter: ", ident); |
| 1162 | } |
| 1163 | srcReg->File = PROGRAM_NAMED_PARAM; |
| 1164 | srcReg->Index = paramIndex; |
| 1165 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1166 | else if (IsDigit(token[0])) { |
| 1167 | /* scalar literal */ |
| 1168 | GLfloat values[4]; |
Brian | 81c4fee | 2007-04-20 11:53:48 -0600 | [diff] [blame^] | 1169 | GLuint paramIndex; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1170 | if (!Parse_ScalarConstant(parseState, values)) |
| 1171 | RETURN_ERROR; |
Brian | fe1d01c | 2006-12-13 14:54:47 -0700 | [diff] [blame] | 1172 | paramIndex = _mesa_add_unnamed_constant(parseState->parameters, |
Brian | 1bf81e3 | 2007-03-22 09:07:27 -0600 | [diff] [blame] | 1173 | values, 4, NULL); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1174 | srcReg->Index = paramIndex; |
| 1175 | srcReg->File = PROGRAM_NAMED_PARAM; |
| 1176 | needSuffix = GL_FALSE; |
| 1177 | } |
| 1178 | else { |
| 1179 | RETURN_ERROR2("Invalid scalar source argument", token); |
| 1180 | } |
| 1181 | |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1182 | srcReg->Swizzle = 0; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1183 | if (needSuffix) { |
| 1184 | /* parse .[xyzw] suffix */ |
| 1185 | if (!Parse_String(parseState, ".")) |
| 1186 | RETURN_ERROR1("Expected ."); |
| 1187 | |
| 1188 | if (!Parse_Token(parseState, token)) |
| 1189 | RETURN_ERROR; |
| 1190 | |
| 1191 | if (token[0] == 'x' && token[1] == 0) { |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1192 | srcReg->Swizzle = 0; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1193 | } |
| 1194 | else if (token[0] == 'y' && token[1] == 0) { |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1195 | srcReg->Swizzle = 1; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1196 | } |
| 1197 | else if (token[0] == 'z' && token[1] == 0) { |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1198 | srcReg->Swizzle = 2; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1199 | } |
| 1200 | else if (token[0] == 'w' && token[1] == 0) { |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1201 | srcReg->Swizzle = 3; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1202 | } |
| 1203 | else { |
| 1204 | RETURN_ERROR1("Invalid scalar source suffix"); |
| 1205 | } |
| 1206 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1207 | |
| 1208 | /* Finish absolute value */ |
| 1209 | if (srcReg->Abs && !Parse_String(parseState, "|")) { |
| 1210 | RETURN_ERROR1("Expected |"); |
| 1211 | } |
| 1212 | |
| 1213 | return GL_TRUE; |
| 1214 | } |
| 1215 | |
| 1216 | |
Brian Paul | 2a5afe3 | 2004-12-18 16:18:00 +0000 | [diff] [blame] | 1217 | static GLboolean |
| 1218 | Parse_PrintInstruction(struct parse_state *parseState, |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 1219 | struct prog_instruction *inst) |
Brian Paul | 2a5afe3 | 2004-12-18 16:18:00 +0000 | [diff] [blame] | 1220 | { |
| 1221 | const GLubyte *str; |
| 1222 | GLubyte *msg; |
| 1223 | GLuint len; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1224 | GLint idx; |
Brian Paul | 2a5afe3 | 2004-12-18 16:18:00 +0000 | [diff] [blame] | 1225 | |
| 1226 | /* The first argument is a literal string 'just like this' */ |
| 1227 | if (!Parse_String(parseState, "'")) |
| 1228 | RETURN_ERROR1("Expected '"); |
| 1229 | |
| 1230 | str = parseState->pos; |
| 1231 | for (len = 0; str[len] != '\''; len++) /* find closing quote */ |
| 1232 | ; |
| 1233 | parseState->pos += len + 1; |
Brian Paul | 9580179 | 2005-12-06 15:41:43 +0000 | [diff] [blame] | 1234 | msg = (GLubyte*) _mesa_malloc(len + 1); |
Brian Paul | 2a5afe3 | 2004-12-18 16:18:00 +0000 | [diff] [blame] | 1235 | |
| 1236 | _mesa_memcpy(msg, str, len); |
| 1237 | msg[len] = 0; |
| 1238 | inst->Data = msg; |
| 1239 | |
| 1240 | if (Parse_String(parseState, ",")) { |
| 1241 | /* got an optional register to print */ |
| 1242 | GLubyte token[100]; |
| 1243 | GetToken(parseState, token); |
| 1244 | if (token[0] == 'o') { |
| 1245 | /* dst reg */ |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1246 | if (!Parse_OutputReg(parseState, &idx)) |
Brian Paul | 2a5afe3 | 2004-12-18 16:18:00 +0000 | [diff] [blame] | 1247 | RETURN_ERROR; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1248 | inst->SrcReg[0].Index = idx; |
Brian Paul | 2a5afe3 | 2004-12-18 16:18:00 +0000 | [diff] [blame] | 1249 | inst->SrcReg[0].File = PROGRAM_OUTPUT; |
| 1250 | } |
| 1251 | else { |
| 1252 | /* src reg */ |
| 1253 | if (!Parse_VectorSrc(parseState, &inst->SrcReg[0])) |
| 1254 | RETURN_ERROR; |
| 1255 | } |
| 1256 | } |
| 1257 | else { |
Brian Paul | 8cdf372 | 2005-09-02 13:40:09 +0000 | [diff] [blame] | 1258 | inst->SrcReg[0].File = PROGRAM_UNDEFINED; |
Brian Paul | 2a5afe3 | 2004-12-18 16:18:00 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1261 | inst->SrcReg[0].Swizzle = SWIZZLE_NOOP; |
Brian Paul | a8c4242 | 2006-05-30 22:17:35 +0000 | [diff] [blame] | 1262 | inst->SrcReg[0].NegateBase = NEGATE_NONE; |
Brian Paul | 2a5afe3 | 2004-12-18 16:18:00 +0000 | [diff] [blame] | 1263 | inst->SrcReg[0].Abs = GL_FALSE; |
| 1264 | inst->SrcReg[0].NegateAbs = GL_FALSE; |
| 1265 | |
| 1266 | return GL_TRUE; |
| 1267 | } |
| 1268 | |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1269 | |
| 1270 | static GLboolean |
| 1271 | Parse_InstructionSequence(struct parse_state *parseState, |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 1272 | struct prog_instruction program[]) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1273 | { |
| 1274 | while (1) { |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 1275 | struct prog_instruction *inst = program + parseState->numInst; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1276 | struct instruction_pattern instMatch; |
| 1277 | GLubyte token[100]; |
| 1278 | |
| 1279 | /* Initialize the instruction */ |
Brian Paul | d6272e0 | 2006-10-29 18:03:16 +0000 | [diff] [blame] | 1280 | _mesa_init_instructions(inst, 1); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1281 | |
| 1282 | /* special instructions */ |
| 1283 | if (Parse_String(parseState, "DEFINE")) { |
| 1284 | GLubyte id[100]; |
| 1285 | GLfloat value[7]; /* yes, 7 to be safe */ |
| 1286 | if (!Parse_Identifier(parseState, id)) |
| 1287 | RETURN_ERROR; |
| 1288 | /* XXX make sure id is not a reserved identifer, like R9 */ |
| 1289 | if (!Parse_String(parseState, "=")) |
| 1290 | RETURN_ERROR1("Expected ="); |
| 1291 | if (!Parse_VectorOrScalarConstant(parseState, value)) |
| 1292 | RETURN_ERROR; |
| 1293 | if (!Parse_String(parseState, ";")) |
| 1294 | RETURN_ERROR1("Expected ;"); |
| 1295 | if (_mesa_lookup_parameter_index(parseState->parameters, |
| 1296 | -1, (const char *) id) >= 0) { |
| 1297 | RETURN_ERROR2(id, "already defined"); |
| 1298 | } |
| 1299 | _mesa_add_named_parameter(parseState->parameters, |
| 1300 | (const char *) id, value); |
| 1301 | } |
| 1302 | else if (Parse_String(parseState, "DECLARE")) { |
| 1303 | GLubyte id[100]; |
| 1304 | GLfloat value[7] = {0, 0, 0, 0, 0, 0, 0}; /* yes, to be safe */ |
| 1305 | if (!Parse_Identifier(parseState, id)) |
| 1306 | RETURN_ERROR; |
| 1307 | /* XXX make sure id is not a reserved identifer, like R9 */ |
| 1308 | if (Parse_String(parseState, "=")) { |
| 1309 | if (!Parse_VectorOrScalarConstant(parseState, value)) |
| 1310 | RETURN_ERROR; |
| 1311 | } |
| 1312 | if (!Parse_String(parseState, ";")) |
| 1313 | RETURN_ERROR1("Expected ;"); |
| 1314 | if (_mesa_lookup_parameter_index(parseState->parameters, |
| 1315 | -1, (const char *) id) >= 0) { |
| 1316 | RETURN_ERROR2(id, "already declared"); |
| 1317 | } |
| 1318 | _mesa_add_named_parameter(parseState->parameters, |
| 1319 | (const char *) id, value); |
| 1320 | } |
| 1321 | else if (Parse_String(parseState, "END")) { |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 1322 | inst->Opcode = OPCODE_END; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1323 | inst->StringPos = parseState->curLine - parseState->start; |
| 1324 | assert(inst->StringPos >= 0); |
| 1325 | parseState->numInst++; |
| 1326 | if (Parse_Token(parseState, token)) { |
| 1327 | RETURN_ERROR1("Code after END opcode."); |
| 1328 | } |
| 1329 | break; |
| 1330 | } |
| 1331 | else { |
| 1332 | /* general/arithmetic instruction */ |
| 1333 | |
| 1334 | /* get token */ |
| 1335 | if (!Parse_Token(parseState, token)) { |
| 1336 | RETURN_ERROR1("Missing END instruction."); |
| 1337 | } |
| 1338 | |
| 1339 | /* try to find matching instuction */ |
| 1340 | instMatch = MatchInstruction(token); |
Brian Paul | 28b014e | 2006-04-05 03:05:17 +0000 | [diff] [blame] | 1341 | if (instMatch.opcode >= MAX_OPCODE) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1342 | /* bad instruction name */ |
| 1343 | RETURN_ERROR2("Unexpected token: ", token); |
| 1344 | } |
| 1345 | |
| 1346 | inst->Opcode = instMatch.opcode; |
| 1347 | inst->Precision = instMatch.suffixes & (_R | _H | _X); |
Brian Paul | e31ac05 | 2005-11-20 17:52:40 +0000 | [diff] [blame] | 1348 | inst->SaturateMode = (instMatch.suffixes & (_S)) |
| 1349 | ? SATURATE_ZERO_ONE : SATURATE_OFF; |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 1350 | inst->CondUpdate = (instMatch.suffixes & (_C)) ? GL_TRUE : GL_FALSE; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1351 | inst->StringPos = parseState->curLine - parseState->start; |
| 1352 | assert(inst->StringPos >= 0); |
| 1353 | |
| 1354 | /* |
| 1355 | * parse the input and output operands |
| 1356 | */ |
| 1357 | if (instMatch.outputs == OUTPUT_S || instMatch.outputs == OUTPUT_V) { |
| 1358 | if (!Parse_MaskedDstReg(parseState, &inst->DstReg)) |
| 1359 | RETURN_ERROR; |
| 1360 | if (!Parse_String(parseState, ",")) |
| 1361 | RETURN_ERROR1("Expected ,"); |
| 1362 | } |
| 1363 | else if (instMatch.outputs == OUTPUT_NONE) { |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 1364 | if (instMatch.opcode == OPCODE_KIL_NV) { |
Brian Paul | 2a5afe3 | 2004-12-18 16:18:00 +0000 | [diff] [blame] | 1365 | /* This is a little weird, the cond code info is in |
| 1366 | * the dest register. |
| 1367 | */ |
| 1368 | if (!Parse_CondCodeMask(parseState, &inst->DstReg)) |
| 1369 | RETURN_ERROR; |
| 1370 | } |
| 1371 | else { |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 1372 | ASSERT(instMatch.opcode == OPCODE_PRINT); |
Brian Paul | 2a5afe3 | 2004-12-18 16:18:00 +0000 | [diff] [blame] | 1373 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | if (instMatch.inputs == INPUT_1V) { |
| 1377 | if (!Parse_VectorSrc(parseState, &inst->SrcReg[0])) |
| 1378 | RETURN_ERROR; |
| 1379 | } |
| 1380 | else if (instMatch.inputs == INPUT_2V) { |
| 1381 | if (!Parse_VectorSrc(parseState, &inst->SrcReg[0])) |
| 1382 | RETURN_ERROR; |
| 1383 | if (!Parse_String(parseState, ",")) |
| 1384 | RETURN_ERROR1("Expected ,"); |
| 1385 | if (!Parse_VectorSrc(parseState, &inst->SrcReg[1])) |
| 1386 | RETURN_ERROR; |
| 1387 | } |
| 1388 | else if (instMatch.inputs == INPUT_3V) { |
| 1389 | if (!Parse_VectorSrc(parseState, &inst->SrcReg[0])) |
| 1390 | RETURN_ERROR; |
| 1391 | if (!Parse_String(parseState, ",")) |
| 1392 | RETURN_ERROR1("Expected ,"); |
| 1393 | if (!Parse_VectorSrc(parseState, &inst->SrcReg[1])) |
| 1394 | RETURN_ERROR; |
| 1395 | if (!Parse_String(parseState, ",")) |
| 1396 | RETURN_ERROR1("Expected ,"); |
| 1397 | if (!Parse_VectorSrc(parseState, &inst->SrcReg[2])) |
| 1398 | RETURN_ERROR; |
| 1399 | } |
| 1400 | else if (instMatch.inputs == INPUT_1S) { |
| 1401 | if (!Parse_ScalarSrcReg(parseState, &inst->SrcReg[0])) |
| 1402 | RETURN_ERROR; |
| 1403 | } |
| 1404 | else if (instMatch.inputs == INPUT_2S) { |
| 1405 | if (!Parse_ScalarSrcReg(parseState, &inst->SrcReg[0])) |
| 1406 | RETURN_ERROR; |
| 1407 | if (!Parse_String(parseState, ",")) |
| 1408 | RETURN_ERROR1("Expected ,"); |
| 1409 | if (!Parse_ScalarSrcReg(parseState, &inst->SrcReg[1])) |
| 1410 | RETURN_ERROR; |
| 1411 | } |
| 1412 | else if (instMatch.inputs == INPUT_CC) { |
| 1413 | /* XXX to-do */ |
| 1414 | } |
| 1415 | else if (instMatch.inputs == INPUT_1V_T) { |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1416 | GLubyte unit, idx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1417 | if (!Parse_VectorSrc(parseState, &inst->SrcReg[0])) |
| 1418 | RETURN_ERROR; |
| 1419 | if (!Parse_String(parseState, ",")) |
| 1420 | RETURN_ERROR1("Expected ,"); |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1421 | if (!Parse_TextureImageId(parseState, &unit, &idx)) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1422 | RETURN_ERROR; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1423 | inst->TexSrcUnit = unit; |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 1424 | inst->TexSrcTarget = idx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1425 | } |
| 1426 | else if (instMatch.inputs == INPUT_3V_T) { |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1427 | GLubyte unit, idx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1428 | if (!Parse_VectorSrc(parseState, &inst->SrcReg[0])) |
| 1429 | RETURN_ERROR; |
| 1430 | if (!Parse_String(parseState, ",")) |
| 1431 | RETURN_ERROR1("Expected ,"); |
| 1432 | if (!Parse_VectorSrc(parseState, &inst->SrcReg[1])) |
| 1433 | RETURN_ERROR; |
| 1434 | if (!Parse_String(parseState, ",")) |
| 1435 | RETURN_ERROR1("Expected ,"); |
| 1436 | if (!Parse_VectorSrc(parseState, &inst->SrcReg[2])) |
| 1437 | RETURN_ERROR; |
| 1438 | if (!Parse_String(parseState, ",")) |
| 1439 | RETURN_ERROR1("Expected ,"); |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1440 | if (!Parse_TextureImageId(parseState, &unit, &idx)) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1441 | RETURN_ERROR; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1442 | inst->TexSrcUnit = unit; |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 1443 | inst->TexSrcTarget = idx; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1444 | } |
Brian Paul | 2a5afe3 | 2004-12-18 16:18:00 +0000 | [diff] [blame] | 1445 | else if (instMatch.inputs == INPUT_1V_S) { |
| 1446 | if (!Parse_PrintInstruction(parseState, inst)) |
| 1447 | RETURN_ERROR; |
| 1448 | } |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1449 | |
| 1450 | /* end of statement semicolon */ |
| 1451 | if (!Parse_String(parseState, ";")) |
| 1452 | RETURN_ERROR1("Expected ;"); |
| 1453 | |
| 1454 | parseState->numInst++; |
| 1455 | |
| 1456 | if (parseState->numInst >= MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS) |
| 1457 | RETURN_ERROR1("Program too long"); |
| 1458 | } |
| 1459 | } |
| 1460 | return GL_TRUE; |
| 1461 | } |
| 1462 | |
| 1463 | |
| 1464 | |
| 1465 | /** |
| 1466 | * Parse/compile the 'str' returning the compiled 'program'. |
| 1467 | * ctx->Program.ErrorPos will be -1 if successful. Otherwise, ErrorPos |
| 1468 | * indicates the position of the error in 'str'. |
| 1469 | */ |
| 1470 | void |
| 1471 | _mesa_parse_nv_fragment_program(GLcontext *ctx, GLenum dstTarget, |
| 1472 | const GLubyte *str, GLsizei len, |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 1473 | struct gl_fragment_program *program) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1474 | { |
| 1475 | struct parse_state parseState; |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 1476 | struct prog_instruction instBuffer[MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS]; |
| 1477 | struct prog_instruction *newInst; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1478 | GLenum target; |
| 1479 | GLubyte *programString; |
| 1480 | |
| 1481 | /* Make a null-terminated copy of the program string */ |
| 1482 | programString = (GLubyte *) MALLOC(len + 1); |
| 1483 | if (!programString) { |
| 1484 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glLoadProgramNV"); |
| 1485 | return; |
| 1486 | } |
| 1487 | MEMCPY(programString, str, len); |
| 1488 | programString[len] = 0; |
| 1489 | |
| 1490 | /* Get ready to parse */ |
| 1491 | _mesa_bzero(&parseState, sizeof(struct parse_state)); |
| 1492 | parseState.ctx = ctx; |
| 1493 | parseState.start = programString; |
| 1494 | parseState.program = program; |
| 1495 | parseState.numInst = 0; |
| 1496 | parseState.curLine = programString; |
| 1497 | parseState.parameters = _mesa_new_parameter_list(); |
| 1498 | |
| 1499 | /* Reset error state */ |
| 1500 | _mesa_set_program_error(ctx, -1, NULL); |
| 1501 | |
| 1502 | /* check the program header */ |
| 1503 | if (_mesa_strncmp((const char *) programString, "!!FP1.0", 7) == 0) { |
| 1504 | target = GL_FRAGMENT_PROGRAM_NV; |
| 1505 | parseState.pos = programString + 7; |
| 1506 | } |
| 1507 | else if (_mesa_strncmp((const char *) programString, "!!FCP1.0", 8) == 0) { |
| 1508 | /* fragment / register combiner program - not supported */ |
| 1509 | _mesa_set_program_error(ctx, 0, "Invalid fragment program header"); |
| 1510 | _mesa_error(ctx, GL_INVALID_OPERATION, "glLoadProgramNV(bad header)"); |
| 1511 | return; |
| 1512 | } |
| 1513 | else { |
| 1514 | /* invalid header */ |
| 1515 | _mesa_set_program_error(ctx, 0, "Invalid fragment program header"); |
| 1516 | _mesa_error(ctx, GL_INVALID_OPERATION, "glLoadProgramNV(bad header)"); |
| 1517 | return; |
| 1518 | } |
| 1519 | |
| 1520 | /* make sure target and header match */ |
| 1521 | if (target != dstTarget) { |
| 1522 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 1523 | "glLoadProgramNV(target mismatch 0x%x != 0x%x)", |
| 1524 | target, dstTarget); |
| 1525 | return; |
| 1526 | } |
| 1527 | |
| 1528 | if (Parse_InstructionSequence(&parseState, instBuffer)) { |
| 1529 | GLuint u; |
| 1530 | /* successful parse! */ |
| 1531 | |
| 1532 | if (parseState.outputsWritten == 0) { |
| 1533 | /* must write at least one output! */ |
| 1534 | _mesa_error(ctx, GL_INVALID_OPERATION, |
| 1535 | "Invalid fragment program - no outputs written."); |
| 1536 | return; |
| 1537 | } |
| 1538 | |
| 1539 | /* copy the compiled instructions */ |
| 1540 | assert(parseState.numInst <= MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS); |
Brian Paul | 383c39e | 2006-08-25 15:14:25 +0000 | [diff] [blame] | 1541 | newInst = _mesa_alloc_instructions(parseState.numInst); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1542 | if (!newInst) { |
| 1543 | _mesa_error(ctx, GL_OUT_OF_MEMORY, "glLoadProgramNV"); |
| 1544 | return; /* out of memory */ |
| 1545 | } |
Brian | 12229f1 | 2007-03-22 09:11:26 -0600 | [diff] [blame] | 1546 | _mesa_copy_instructions(newInst, instBuffer, parseState.numInst); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1547 | |
| 1548 | /* install the program */ |
| 1549 | program->Base.Target = target; |
| 1550 | if (program->Base.String) { |
| 1551 | FREE(program->Base.String); |
| 1552 | } |
| 1553 | program->Base.String = programString; |
| 1554 | program->Base.Format = GL_PROGRAM_FORMAT_ASCII_ARB; |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 1555 | if (program->Base.Instructions) { |
| 1556 | _mesa_free(program->Base.Instructions); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1557 | } |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 1558 | program->Base.Instructions = newInst; |
Brian Paul | ee40c4fb3 | 2006-02-15 15:59:37 +0000 | [diff] [blame] | 1559 | program->Base.NumInstructions = parseState.numInst; |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 1560 | program->Base.InputsRead = parseState.inputsRead; |
| 1561 | program->Base.OutputsWritten = parseState.outputsWritten; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1562 | for (u = 0; u < ctx->Const.MaxTextureImageUnits; u++) |
Brian | c9db223 | 2007-01-04 17:22:19 -0700 | [diff] [blame] | 1563 | program->Base.TexturesUsed[u] = parseState.texturesUsed[u]; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1564 | |
| 1565 | /* save program parameters */ |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 1566 | program->Base.Parameters = parseState.parameters; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1567 | |
| 1568 | /* allocate registers for declared program parameters */ |
| 1569 | #if 00 |
| 1570 | _mesa_assign_program_registers(&(program->SymbolTable)); |
| 1571 | #endif |
| 1572 | |
Brian Paul | ac33dd1 | 2004-06-29 00:00:29 +0000 | [diff] [blame] | 1573 | #ifdef DEBUG_foo |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1574 | _mesa_printf("--- glLoadProgramNV(%d) result ---\n", program->Base.Id); |
| 1575 | _mesa_print_nv_fragment_program(program); |
| 1576 | _mesa_printf("----------------------------------\n"); |
| 1577 | #endif |
| 1578 | } |
| 1579 | else { |
| 1580 | /* Error! */ |
| 1581 | _mesa_error(ctx, GL_INVALID_OPERATION, "glLoadProgramNV"); |
| 1582 | /* NOTE: _mesa_set_program_error would have been called already */ |
| 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | |
| 1587 | static void |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 1588 | PrintSrcReg(const struct gl_fragment_program *program, |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 1589 | const struct prog_src_register *src) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1590 | { |
| 1591 | static const char comps[5] = "xyzw"; |
| 1592 | |
| 1593 | if (src->NegateAbs) { |
| 1594 | _mesa_printf("-"); |
| 1595 | } |
| 1596 | if (src->Abs) { |
| 1597 | _mesa_printf("|"); |
| 1598 | } |
| 1599 | if (src->NegateBase) { |
| 1600 | _mesa_printf("-"); |
| 1601 | } |
| 1602 | if (src->File == PROGRAM_NAMED_PARAM) { |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 1603 | if (program->Base.Parameters->Parameters[src->Index].Type |
| 1604 | == PROGRAM_CONSTANT) { |
| 1605 | const GLfloat *v; |
| 1606 | v = program->Base.Parameters->ParameterValues[src->Index]; |
| 1607 | _mesa_printf("{%g, %g, %g, %g}", v[0], v[1], v[2], v[3]); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1608 | } |
| 1609 | else { |
Brian Paul | bed8363 | 2005-11-12 17:56:18 +0000 | [diff] [blame] | 1610 | ASSERT(program->Base.Parameters->Parameters[src->Index].Type |
Brian Paul | 613e1ad | 2005-11-05 02:15:21 +0000 | [diff] [blame] | 1611 | == PROGRAM_NAMED_PARAM); |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 1612 | _mesa_printf("%s", program->Base.Parameters->Parameters[src->Index].Name); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1613 | } |
| 1614 | } |
| 1615 | else if (src->File == PROGRAM_OUTPUT) { |
| 1616 | _mesa_printf("o[%s]", OutputRegisters[src->Index]); |
| 1617 | } |
| 1618 | else if (src->File == PROGRAM_INPUT) { |
| 1619 | _mesa_printf("f[%s]", InputRegisters[src->Index]); |
| 1620 | } |
| 1621 | else if (src->File == PROGRAM_LOCAL_PARAM) { |
| 1622 | _mesa_printf("p[%d]", src->Index); |
| 1623 | } |
| 1624 | else if (src->File == PROGRAM_TEMPORARY) { |
| 1625 | if (src->Index >= 32) |
| 1626 | _mesa_printf("H%d", src->Index); |
| 1627 | else |
| 1628 | _mesa_printf("R%d", src->Index); |
| 1629 | } |
| 1630 | else if (src->File == PROGRAM_WRITE_ONLY) { |
| 1631 | _mesa_printf("%cC", "HR"[src->Index]); |
| 1632 | } |
| 1633 | else { |
| 1634 | _mesa_problem(NULL, "Invalid fragment register %d", src->Index); |
| 1635 | return; |
| 1636 | } |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1637 | if (GET_SWZ(src->Swizzle, 0) == GET_SWZ(src->Swizzle, 1) && |
| 1638 | GET_SWZ(src->Swizzle, 0) == GET_SWZ(src->Swizzle, 2) && |
| 1639 | GET_SWZ(src->Swizzle, 0) == GET_SWZ(src->Swizzle, 3)) { |
| 1640 | _mesa_printf(".%c", comps[GET_SWZ(src->Swizzle, 0)]); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1641 | } |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1642 | else if (src->Swizzle != SWIZZLE_NOOP) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1643 | _mesa_printf(".%c%c%c%c", |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1644 | comps[GET_SWZ(src->Swizzle, 0)], |
| 1645 | comps[GET_SWZ(src->Swizzle, 1)], |
| 1646 | comps[GET_SWZ(src->Swizzle, 2)], |
| 1647 | comps[GET_SWZ(src->Swizzle, 3)]); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1648 | } |
| 1649 | if (src->Abs) { |
| 1650 | _mesa_printf("|"); |
| 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | static void |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 1655 | PrintTextureSrc(const struct prog_instruction *inst) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1656 | { |
| 1657 | _mesa_printf("TEX%d, ", inst->TexSrcUnit); |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 1658 | switch (inst->TexSrcTarget) { |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1659 | case TEXTURE_1D_INDEX: |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1660 | _mesa_printf("1D"); |
| 1661 | break; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1662 | case TEXTURE_2D_INDEX: |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1663 | _mesa_printf("2D"); |
| 1664 | break; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1665 | case TEXTURE_3D_INDEX: |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1666 | _mesa_printf("3D"); |
| 1667 | break; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1668 | case TEXTURE_RECT_INDEX: |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1669 | _mesa_printf("RECT"); |
| 1670 | break; |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1671 | case TEXTURE_CUBE_INDEX: |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1672 | _mesa_printf("CUBE"); |
| 1673 | break; |
| 1674 | default: |
| 1675 | _mesa_problem(NULL, "Invalid textue target in PrintTextureSrc"); |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | static void |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 1680 | PrintCondCode(const struct prog_dst_register *dst) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1681 | { |
| 1682 | static const char *comps = "xyzw"; |
| 1683 | static const char *ccString[] = { |
| 1684 | "??", "GT", "EQ", "LT", "UN", "GE", "LE", "NE", "TR", "FL", "??" |
| 1685 | }; |
| 1686 | |
| 1687 | _mesa_printf("%s", ccString[dst->CondMask]); |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1688 | if (GET_SWZ(dst->CondSwizzle, 0) == GET_SWZ(dst->CondSwizzle, 1) && |
| 1689 | GET_SWZ(dst->CondSwizzle, 0) == GET_SWZ(dst->CondSwizzle, 2) && |
| 1690 | GET_SWZ(dst->CondSwizzle, 0) == GET_SWZ(dst->CondSwizzle, 3)) { |
| 1691 | _mesa_printf(".%c", comps[GET_SWZ(dst->CondSwizzle, 0)]); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1692 | } |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1693 | else if (dst->CondSwizzle != SWIZZLE_NOOP) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1694 | _mesa_printf(".%c%c%c%c", |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1695 | comps[GET_SWZ(dst->CondSwizzle, 0)], |
| 1696 | comps[GET_SWZ(dst->CondSwizzle, 1)], |
| 1697 | comps[GET_SWZ(dst->CondSwizzle, 2)], |
| 1698 | comps[GET_SWZ(dst->CondSwizzle, 3)]); |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | |
| 1703 | static void |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 1704 | PrintDstReg(const struct prog_dst_register *dst) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1705 | { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1706 | if (dst->File == PROGRAM_OUTPUT) { |
| 1707 | _mesa_printf("o[%s]", OutputRegisters[dst->Index]); |
| 1708 | } |
| 1709 | else if (dst->File == PROGRAM_TEMPORARY) { |
| 1710 | if (dst->Index >= 32) |
| 1711 | _mesa_printf("H%d", dst->Index); |
| 1712 | else |
| 1713 | _mesa_printf("R%d", dst->Index); |
| 1714 | } |
| 1715 | else if (dst->File == PROGRAM_LOCAL_PARAM) { |
| 1716 | _mesa_printf("p[%d]", dst->Index); |
| 1717 | } |
| 1718 | else if (dst->File == PROGRAM_WRITE_ONLY) { |
| 1719 | _mesa_printf("%cC", "HR"[dst->Index]); |
| 1720 | } |
| 1721 | else { |
| 1722 | _mesa_printf("???"); |
| 1723 | } |
| 1724 | |
Brian Paul | ccfe3d4 | 2005-11-03 02:35:15 +0000 | [diff] [blame] | 1725 | if (dst->WriteMask != 0 && dst->WriteMask != WRITEMASK_XYZW) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1726 | _mesa_printf("."); |
Brian Paul | ccfe3d4 | 2005-11-03 02:35:15 +0000 | [diff] [blame] | 1727 | if (dst->WriteMask & WRITEMASK_X) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1728 | _mesa_printf("x"); |
Brian Paul | ccfe3d4 | 2005-11-03 02:35:15 +0000 | [diff] [blame] | 1729 | if (dst->WriteMask & WRITEMASK_Y) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1730 | _mesa_printf("y"); |
Brian Paul | ccfe3d4 | 2005-11-03 02:35:15 +0000 | [diff] [blame] | 1731 | if (dst->WriteMask & WRITEMASK_Z) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1732 | _mesa_printf("z"); |
Brian Paul | ccfe3d4 | 2005-11-03 02:35:15 +0000 | [diff] [blame] | 1733 | if (dst->WriteMask & WRITEMASK_W) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1734 | _mesa_printf("w"); |
| 1735 | } |
| 1736 | |
| 1737 | if (dst->CondMask != COND_TR || |
Keith Whitwell | 7c26b61 | 2005-04-21 14:46:57 +0000 | [diff] [blame] | 1738 | dst->CondSwizzle != SWIZZLE_NOOP) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1739 | _mesa_printf(" ("); |
| 1740 | PrintCondCode(dst); |
| 1741 | _mesa_printf(")"); |
| 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | |
| 1746 | /** |
| 1747 | * Print (unparse) the given vertex program. Just for debugging. |
| 1748 | */ |
| 1749 | void |
Brian Paul | 122629f | 2006-07-20 16:49:57 +0000 | [diff] [blame] | 1750 | _mesa_print_nv_fragment_program(const struct gl_fragment_program *program) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1751 | { |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 1752 | const struct prog_instruction *inst; |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1753 | |
Brian Paul | de99760 | 2005-11-12 17:53:14 +0000 | [diff] [blame] | 1754 | for (inst = program->Base.Instructions; inst->Opcode != OPCODE_END; inst++) { |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1755 | int i; |
| 1756 | for (i = 0; Instructions[i].name; i++) { |
| 1757 | if (inst->Opcode == Instructions[i].opcode) { |
| 1758 | /* print instruction name */ |
| 1759 | _mesa_printf("%s", Instructions[i].name); |
| 1760 | if (inst->Precision == FLOAT16) |
| 1761 | _mesa_printf("H"); |
| 1762 | else if (inst->Precision == FIXED12) |
| 1763 | _mesa_printf("X"); |
Brian Paul | 7e80751 | 2005-11-05 17:10:45 +0000 | [diff] [blame] | 1764 | if (inst->CondUpdate) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1765 | _mesa_printf("C"); |
Brian Paul | e31ac05 | 2005-11-20 17:52:40 +0000 | [diff] [blame] | 1766 | if (inst->SaturateMode == SATURATE_ZERO_ONE) |
Michal Krol | 2861e73 | 2004-03-29 11:09:34 +0000 | [diff] [blame] | 1767 | _mesa_printf("_SAT"); |
| 1768 | _mesa_printf(" "); |
| 1769 | |
| 1770 | if (Instructions[i].inputs == INPUT_CC) { |
| 1771 | PrintCondCode(&inst->DstReg); |
| 1772 | } |
| 1773 | else if (Instructions[i].outputs == OUTPUT_V || |
| 1774 | Instructions[i].outputs == OUTPUT_S) { |
| 1775 | /* print dest register */ |
| 1776 | PrintDstReg(&inst->DstReg); |
| 1777 | _mesa_printf(", "); |
| 1778 | } |
| 1779 | |
| 1780 | /* print source register(s) */ |
| 1781 | if (Instructions[i].inputs == INPUT_1V || |
| 1782 | Instructions[i].inputs == INPUT_1S) { |
| 1783 | PrintSrcReg(program, &inst->SrcReg[0]); |
| 1784 | } |
| 1785 | else if (Instructions[i].inputs == INPUT_2V || |
| 1786 | Instructions[i].inputs == INPUT_2S) { |
| 1787 | PrintSrcReg(program, &inst->SrcReg[0]); |
| 1788 | _mesa_printf(", "); |
| 1789 | PrintSrcReg(program, &inst->SrcReg[1]); |
| 1790 | } |
| 1791 | else if (Instructions[i].inputs == INPUT_3V) { |
| 1792 | PrintSrcReg(program, &inst->SrcReg[0]); |
| 1793 | _mesa_printf(", "); |
| 1794 | PrintSrcReg(program, &inst->SrcReg[1]); |
| 1795 | _mesa_printf(", "); |
| 1796 | PrintSrcReg(program, &inst->SrcReg[2]); |
| 1797 | } |
| 1798 | else if (Instructions[i].inputs == INPUT_1V_T) { |
| 1799 | PrintSrcReg(program, &inst->SrcReg[0]); |
| 1800 | _mesa_printf(", "); |
| 1801 | PrintTextureSrc(inst); |
| 1802 | } |
| 1803 | else if (Instructions[i].inputs == INPUT_3V_T) { |
| 1804 | PrintSrcReg(program, &inst->SrcReg[0]); |
| 1805 | _mesa_printf(", "); |
| 1806 | PrintSrcReg(program, &inst->SrcReg[1]); |
| 1807 | _mesa_printf(", "); |
| 1808 | PrintSrcReg(program, &inst->SrcReg[2]); |
| 1809 | _mesa_printf(", "); |
| 1810 | PrintTextureSrc(inst); |
| 1811 | } |
| 1812 | _mesa_printf(";\n"); |
| 1813 | break; |
| 1814 | } |
| 1815 | } |
| 1816 | if (!Instructions[i].name) { |
| 1817 | _mesa_printf("Invalid opcode %d\n", inst->Opcode); |
| 1818 | } |
| 1819 | } |
| 1820 | _mesa_printf("END\n"); |
| 1821 | } |
| 1822 | |
| 1823 | |
| 1824 | const char * |
| 1825 | _mesa_nv_fragment_input_register_name(GLuint i) |
| 1826 | { |
| 1827 | ASSERT(i < MAX_NV_FRAGMENT_PROGRAM_INPUTS); |
| 1828 | return InputRegisters[i]; |
| 1829 | } |
| 1830 | |
| 1831 | |
| 1832 | const char * |
| 1833 | _mesa_nv_fragment_output_register_name(GLuint i) |
| 1834 | { |
| 1835 | ASSERT(i < MAX_NV_FRAGMENT_PROGRAM_OUTPUTS); |
| 1836 | return OutputRegisters[i]; |
| 1837 | } |