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