1 %{ 2 /* 3 * Copyright © 2009 Intel Corporation 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice (including the next 13 * paragraph) shall be included in all copies or substantial portions of the 14 * Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 * DEALINGS IN THE SOFTWARE. 23 */ 24 25 #include <stdarg.h> 26 #include <stdio.h> 27 #include <stdlib.h> 28 #include <string.h> 29 30 #include "main/mtypes.h" 31 #include "main/imports.h" 32 #include "program/program.h" 33 #include "program/prog_parameter.h" 34 #include "program/prog_parameter_layout.h" 35 #include "program/prog_statevars.h" 36 #include "program/prog_instruction.h" 37 38 #include "program/symbol_table.h" 39 #include "program/program_parser.h" 40 41 extern void *yy_scan_string(char *); 42 extern void yy_delete_buffer(void *); 43 44 static struct asm_symbol *declare_variable(struct asm_parser_state *state, 45 char *name, enum asm_type t, struct YYLTYPE *locp); 46 47 static int add_state_reference(struct gl_program_parameter_list *param_list, 48 const gl_state_index tokens[STATE_LENGTH]); 49 50 static int initialize_symbol_from_state(struct gl_program *prog, 51 struct asm_symbol *param_var, const gl_state_index tokens[STATE_LENGTH]); 52 53 static int initialize_symbol_from_param(struct gl_program *prog, 54 struct asm_symbol *param_var, const gl_state_index tokens[STATE_LENGTH]); 55 56 static int initialize_symbol_from_const(struct gl_program *prog, 57 struct asm_symbol *param_var, const struct asm_vector *vec, 58 GLboolean allowSwizzle); 59 60 static int yyparse(struct asm_parser_state *state); 61 62 static char *make_error_string(const char *fmt, ...); 63 64 static void yyerror(struct YYLTYPE *locp, struct asm_parser_state *state, 65 const char *s); 66 67 static int validate_inputs(struct YYLTYPE *locp, 68 struct asm_parser_state *state); 69 70 static void init_dst_reg(struct prog_dst_register *r); 71 72 static void set_dst_reg(struct prog_dst_register *r, 73 gl_register_file file, GLint index); 74 75 static void init_src_reg(struct asm_src_register *r); 76 77 static void set_src_reg(struct asm_src_register *r, 78 gl_register_file file, GLint index); 79 80 static void set_src_reg_swz(struct asm_src_register *r, 81 gl_register_file file, GLint index, GLuint swizzle); 82 83 static void asm_instruction_set_operands(struct asm_instruction *inst, 84 const struct prog_dst_register *dst, const struct asm_src_register *src0, 85 const struct asm_src_register *src1, const struct asm_src_register *src2); 86 87 static struct asm_instruction *asm_instruction_ctor(enum prog_opcode op, 88 const struct prog_dst_register *dst, const struct asm_src_register *src0, 89 const struct asm_src_register *src1, const struct asm_src_register *src2); 90 91 static struct asm_instruction *asm_instruction_copy_ctor( 92 const struct prog_instruction *base, const struct prog_dst_register *dst, 93 const struct asm_src_register *src0, const struct asm_src_register *src1, 94 const struct asm_src_register *src2); 95 96 #ifndef FALSE 97 #define FALSE 0 98 #define TRUE (!FALSE) 99 #endif 100 101 #define YYLLOC_DEFAULT(Current, Rhs, N) \ 102 do { \ 103 if (N) { \ 104 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \ 105 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \ 106 (Current).position = YYRHSLOC(Rhs, 1).position; \ 107 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \ 108 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \ 109 } else { \ 110 (Current).first_line = YYRHSLOC(Rhs, 0).last_line; \ 111 (Current).last_line = (Current).first_line; \ 112 (Current).first_column = YYRHSLOC(Rhs, 0).last_column; \ 113 (Current).last_column = (Current).first_column; \ 114 (Current).position = YYRHSLOC(Rhs, 0).position \ 115 + (Current).first_column; \ 116 } \ 117 } while(0) 118 %} 119 120 %pure-parser 121 %locations 122 %lex-param { struct asm_parser_state *state } 123 %parse-param { struct asm_parser_state *state } 124 %error-verbose 125 126 %union { 127 struct asm_instruction *inst; 128 struct asm_symbol *sym; 129 struct asm_symbol temp_sym; 130 struct asm_swizzle_mask swiz_mask; 131 struct asm_src_register src_reg; 132 struct prog_dst_register dst_reg; 133 struct prog_instruction temp_inst; 134 char *string; 135 unsigned result; 136 unsigned attrib; 137 int integer; 138 float real; 139 gl_state_index state[STATE_LENGTH]; 140 int negate; 141 struct asm_vector vector; 142 enum prog_opcode opcode; 143 144 struct { 145 unsigned swz; 146 unsigned rgba_valid:1; 147 unsigned xyzw_valid:1; 148 unsigned negate:1; 149 } ext_swizzle; 150 } 151 152 %token ARBvp_10 ARBfp_10 153 154 /* Tokens for assembler pseudo-ops */ 155 %token <integer> ADDRESS 156 %token ALIAS ATTRIB 157 %token OPTION OUTPUT 158 %token PARAM 159 %token <integer> TEMP 160 %token END 161 162 /* Tokens for instructions */ 163 %token <temp_inst> BIN_OP BINSC_OP SAMPLE_OP SCALAR_OP TRI_OP VECTOR_OP 164 %token <temp_inst> ARL KIL SWZ TXD_OP 165 166 %token <integer> INTEGER 167 %token <real> REAL 168 169 %token AMBIENT ATTENUATION 170 %token BACK 171 %token CLIP COLOR 172 %token DEPTH DIFFUSE DIRECTION 173 %token EMISSION ENV EYE 174 %token FOG FOGCOORD FRAGMENT FRONT 175 %token HALF 176 %token INVERSE INVTRANS 177 %token LIGHT LIGHTMODEL LIGHTPROD LOCAL 178 %token MATERIAL MAT_PROGRAM MATRIX MATRIXINDEX MODELVIEW MVP 179 %token NORMAL 180 %token OBJECT 181 %token PALETTE PARAMS PLANE POINT_TOK POINTSIZE POSITION PRIMARY PROGRAM PROJECTION 182 %token RANGE RESULT ROW 183 %token SCENECOLOR SECONDARY SHININESS SIZE_TOK SPECULAR SPOT STATE 184 %token TEXCOORD TEXENV TEXGEN TEXGEN_Q TEXGEN_R TEXGEN_S TEXGEN_T TEXTURE TRANSPOSE 185 %token TEXTURE_UNIT TEX_1D TEX_2D TEX_3D TEX_CUBE TEX_RECT 186 %token TEX_SHADOW1D TEX_SHADOW2D TEX_SHADOWRECT 187 %token TEX_ARRAY1D TEX_ARRAY2D TEX_ARRAYSHADOW1D TEX_ARRAYSHADOW2D 188 %token VERTEX VTXATTRIB 189 190 %token <string> IDENTIFIER USED_IDENTIFIER 191 %type <string> string 192 %token <swiz_mask> MASK4 MASK3 MASK2 MASK1 SWIZZLE 193 %token DOT_DOT 194 %token DOT 195 196 %type <inst> instruction ALU_instruction TexInstruction 197 %type <inst> ARL_instruction VECTORop_instruction 198 %type <inst> SCALARop_instruction BINSCop_instruction BINop_instruction 199 %type <inst> TRIop_instruction TXD_instruction SWZ_instruction SAMPLE_instruction 200 %type <inst> KIL_instruction 201 202 %type <dst_reg> dstReg maskedDstReg maskedAddrReg 203 %type <src_reg> srcReg scalarUse scalarSrcReg swizzleSrcReg 204 %type <swiz_mask> scalarSuffix swizzleSuffix extendedSwizzle 205 %type <ext_swizzle> extSwizComp extSwizSel 206 %type <swiz_mask> optionalMask 207 208 %type <sym> progParamArray 209 %type <integer> addrRegRelOffset addrRegPosOffset addrRegNegOffset 210 %type <src_reg> progParamArrayMem progParamArrayAbs progParamArrayRel 211 %type <sym> addrReg 212 %type <swiz_mask> addrComponent addrWriteMask 213 214 %type <result> resultBinding resultColBinding 215 %type <integer> optFaceType optColorType 216 %type <integer> optResultFaceType optResultColorType 217 218 %type <integer> optTexImageUnitNum texImageUnitNum 219 %type <integer> optTexCoordUnitNum texCoordUnitNum 220 %type <integer> optLegacyTexUnitNum legacyTexUnitNum 221 %type <integer> texImageUnit texTarget 222 %type <integer> vtxAttribNum 223 224 %type <attrib> attribBinding vtxAttribItem fragAttribItem 225 226 %type <temp_sym> paramSingleInit paramSingleItemDecl 227 %type <integer> optArraySize 228 229 %type <state> stateSingleItem stateMultipleItem 230 %type <state> stateMaterialItem 231 %type <state> stateLightItem stateLightModelItem stateLightProdItem 232 %type <state> stateTexGenItem stateFogItem stateClipPlaneItem statePointItem 233 %type <state> stateMatrixItem stateMatrixRow stateMatrixRows 234 %type <state> stateTexEnvItem stateDepthItem 235 236 %type <state> stateLModProperty 237 %type <state> stateMatrixName optMatrixRows 238 239 %type <integer> stateMatProperty 240 %type <integer> stateLightProperty stateSpotProperty 241 %type <integer> stateLightNumber stateLProdProperty 242 %type <integer> stateTexGenType stateTexGenCoord 243 %type <integer> stateTexEnvProperty 244 %type <integer> stateFogProperty 245 %type <integer> stateClipPlaneNum 246 %type <integer> statePointProperty 247 248 %type <integer> stateOptMatModifier stateMatModifier stateMatrixRowNum 249 %type <integer> stateOptModMatNum stateModMatNum statePaletteMatNum 250 %type <integer> stateProgramMatNum 251 252 %type <integer> ambDiffSpecProperty 253 254 %type <state> programSingleItem progEnvParam progLocalParam 255 %type <state> programMultipleItem progEnvParams progLocalParams 256 257 %type <temp_sym> paramMultipleInit paramMultInitList paramMultipleItem 258 %type <temp_sym> paramSingleItemUse 259 260 %type <integer> progEnvParamNum progLocalParamNum 261 %type <state> progEnvParamNums progLocalParamNums 262 263 %type <vector> paramConstDecl paramConstUse 264 %type <vector> paramConstScalarDecl paramConstScalarUse paramConstVector 265 %type <real> signedFloatConstant 266 %type <negate> optionalSign 267 268 %{ 269 extern int 270 _mesa_program_lexer_lex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param, 271 void *yyscanner); 272 273 static int 274 yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param, 275 struct asm_parser_state *state) 276 { 277 return _mesa_program_lexer_lex(yylval_param, yylloc_param, state->scanner); 278 } 279 %} 280 281 %% 282 283 program: language optionSequence statementSequence END 284 ; 285 286 language: ARBvp_10 287 { 288 if (state->prog->Target != GL_VERTEX_PROGRAM_ARB) { 289 yyerror(& @1, state, "invalid fragment program header"); 290 291 } 292 state->mode = ARB_vertex; 293 } 294 | ARBfp_10 295 { 296 if (state->prog->Target != GL_FRAGMENT_PROGRAM_ARB) { 297 yyerror(& @1, state, "invalid vertex program header"); 298 } 299 state->mode = ARB_fragment; 300 301 state->option.TexRect = 302 (state->ctx->Extensions.NV_texture_rectangle != GL_FALSE); 303 } 304 ; 305 306 optionSequence: optionSequence option 307 | 308 ; 309 310 option: OPTION string ';' 311 { 312 int valid = 0; 313 314 if (state->mode == ARB_vertex) { 315 valid = _mesa_ARBvp_parse_option(state, $2); 316 } else if (state->mode == ARB_fragment) { 317 valid = _mesa_ARBfp_parse_option(state, $2); 318 } 319 320 321 free($2); 322 323 if (!valid) { 324 const char *const err_str = (state->mode == ARB_vertex) 325 ? "invalid ARB vertex program option" 326 : "invalid ARB fragment program option"; 327 328 yyerror(& @2, state, err_str); 329 YYERROR; 330 } 331 } 332 ; 333 334 statementSequence: statementSequence statement 335 | 336 ; 337 338 statement: instruction ';' 339 { 340 if ($1 != NULL) { 341 if (state->inst_tail == NULL) { 342 state->inst_head = $1; 343 } else { 344 state->inst_tail->next = $1; 345 } 346 347 state->inst_tail = $1; 348 $1->next = NULL; 349 350 state->prog->arb.NumInstructions++; 351 } 352 } 353 | namingStatement ';' 354 ; 355 356 instruction: ALU_instruction 357 { 358 $$ = $1; 359 state->prog->arb.NumAluInstructions++; 360 } 361 | TexInstruction 362 { 363 $$ = $1; 364 state->prog->arb.NumTexInstructions++; 365 } 366 ; 367 368 ALU_instruction: ARL_instruction 369 | VECTORop_instruction 370 | SCALARop_instruction 371 | BINSCop_instruction 372 | BINop_instruction 373 | TRIop_instruction 374 | SWZ_instruction 375 ; 376 377 TexInstruction: SAMPLE_instruction 378 | KIL_instruction 379 | TXD_instruction 380 ; 381 382 ARL_instruction: ARL maskedAddrReg ',' scalarSrcReg 383 { 384 $$ = asm_instruction_ctor(OPCODE_ARL, & $2, & $4, NULL, NULL); 385 } 386 ; 387 388 VECTORop_instruction: VECTOR_OP maskedDstReg ',' swizzleSrcReg 389 { 390 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL); 391 } 392 ; 393 394 SCALARop_instruction: SCALAR_OP maskedDstReg ',' scalarSrcReg 395 { 396 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL); 397 } 398 ; 399 400 BINSCop_instruction: BINSC_OP maskedDstReg ',' scalarSrcReg ',' scalarSrcReg 401 { 402 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL); 403 } 404 ; 405 406 407 BINop_instruction: BIN_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg 408 { 409 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL); 410 } 411 ; 412 413 TRIop_instruction: TRI_OP maskedDstReg ',' 414 swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg 415 { 416 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8); 417 } 418 ; 419 420 SAMPLE_instruction: SAMPLE_OP maskedDstReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget 421 { 422 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL); 423 if ($$ != NULL) { 424 const GLbitfield tex_mask = (1U << $6); 425 GLbitfield shadow_tex = 0; 426 GLbitfield target_mask = 0; 427 428 429 $$->Base.TexSrcUnit = $6; 430 431 if ($8 < 0) { 432 shadow_tex = tex_mask; 433 434 $$->Base.TexSrcTarget = -$8; 435 $$->Base.TexShadow = 1; 436 } else { 437 $$->Base.TexSrcTarget = $8; 438 } 439 440 target_mask = (1U << $$->Base.TexSrcTarget); 441 442 /* If this texture unit was previously accessed and that access 443 * had a different texture target, generate an error. 444 * 445 * If this texture unit was previously accessed and that access 446 * had a different shadow mode, generate an error. 447 */ 448 if ((state->prog->TexturesUsed[$6] != 0) 449 && ((state->prog->TexturesUsed[$6] != target_mask) 450 || ((state->prog->ShadowSamplers & tex_mask) 451 != shadow_tex))) { 452 yyerror(& @8, state, 453 "multiple targets used on one texture image unit"); 454 YYERROR; 455 } 456 457 458 state->prog->TexturesUsed[$6] |= target_mask; 459 state->prog->ShadowSamplers |= shadow_tex; 460 } 461 } 462 ; 463 464 KIL_instruction: KIL swizzleSrcReg 465 { 466 $$ = asm_instruction_ctor(OPCODE_KIL, NULL, & $2, NULL, NULL); 467 state->fragment.UsesKill = 1; 468 } 469 ; 470 471 TXD_instruction: TXD_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget 472 { 473 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8); 474 if ($$ != NULL) { 475 const GLbitfield tex_mask = (1U << $10); 476 GLbitfield shadow_tex = 0; 477 GLbitfield target_mask = 0; 478 479 480 $$->Base.TexSrcUnit = $10; 481 482 if ($12 < 0) { 483 shadow_tex = tex_mask; 484 485 $$->Base.TexSrcTarget = -$12; 486 $$->Base.TexShadow = 1; 487 } else { 488 $$->Base.TexSrcTarget = $12; 489 } 490 491 target_mask = (1U << $$->Base.TexSrcTarget); 492 493 /* If this texture unit was previously accessed and that access 494 * had a different texture target, generate an error. 495 * 496 * If this texture unit was previously accessed and that access 497 * had a different shadow mode, generate an error. 498 */ 499 if ((state->prog->TexturesUsed[$10] != 0) 500 && ((state->prog->TexturesUsed[$10] != target_mask) 501 || ((state->prog->ShadowSamplers & tex_mask) 502 != shadow_tex))) { 503 yyerror(& @12, state, 504 "multiple targets used on one texture image unit"); 505 YYERROR; 506 } 507 508 509 state->prog->TexturesUsed[$10] |= target_mask; 510 state->prog->ShadowSamplers |= shadow_tex; 511 } 512 } 513 ; 514 515 texImageUnit: TEXTURE_UNIT optTexImageUnitNum 516 { 517 $$ = $2; 518 } 519 ; 520 521 texTarget: TEX_1D { $$ = TEXTURE_1D_INDEX; } 522 | TEX_2D { $$ = TEXTURE_2D_INDEX; } 523 | TEX_3D { $$ = TEXTURE_3D_INDEX; } 524 | TEX_CUBE { $$ = TEXTURE_CUBE_INDEX; } 525 | TEX_RECT { $$ = TEXTURE_RECT_INDEX; } 526 | TEX_SHADOW1D { $$ = -TEXTURE_1D_INDEX; } 527 | TEX_SHADOW2D { $$ = -TEXTURE_2D_INDEX; } 528 | TEX_SHADOWRECT { $$ = -TEXTURE_RECT_INDEX; } 529 | TEX_ARRAY1D { $$ = TEXTURE_1D_ARRAY_INDEX; } 530 | TEX_ARRAY2D { $$ = TEXTURE_2D_ARRAY_INDEX; } 531 | TEX_ARRAYSHADOW1D { $$ = -TEXTURE_1D_ARRAY_INDEX; } 532 | TEX_ARRAYSHADOW2D { $$ = -TEXTURE_2D_ARRAY_INDEX; } 533 ; 534 535 SWZ_instruction: SWZ maskedDstReg ',' srcReg ',' extendedSwizzle 536 { 537 /* FIXME: Is this correct? Should the extenedSwizzle be applied 538 * FIXME: to the existing swizzle? 539 */ 540 $4.Base.Swizzle = $6.swizzle; 541 $4.Base.Negate = $6.mask; 542 543 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL); 544 } 545 ; 546 547 scalarSrcReg: optionalSign scalarUse 548 { 549 $$ = $2; 550 551 if ($1) { 552 $$.Base.Negate = ~$$.Base.Negate; 553 } 554 } 555 ; 556 557 scalarUse: srcReg scalarSuffix 558 { 559 $$ = $1; 560 561 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle, 562 $2.swizzle); 563 } 564 ; 565 566 swizzleSrcReg: optionalSign srcReg swizzleSuffix 567 { 568 $$ = $2; 569 570 if ($1) { 571 $$.Base.Negate = ~$$.Base.Negate; 572 } 573 574 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle, 575 $3.swizzle); 576 } 577 ; 578 579 maskedDstReg: dstReg optionalMask 580 { 581 $$ = $1; 582 $$.WriteMask = $2.mask; 583 584 if ($$.File == PROGRAM_OUTPUT) { 585 /* Technically speaking, this should check that it is in 586 * vertex program mode. However, PositionInvariant can never be 587 * set in fragment program mode, so it is somewhat irrelevant. 588 */ 589 if (state->option.PositionInvariant 590 && ($$.Index == VARYING_SLOT_POS)) { 591 yyerror(& @1, state, "position-invariant programs cannot " 592 "write position"); 593 YYERROR; 594 } 595 596 state->prog->info.outputs_written |= BITFIELD64_BIT($$.Index); 597 } 598 } 599 ; 600 601 maskedAddrReg: addrReg addrWriteMask 602 { 603 set_dst_reg(& $$, PROGRAM_ADDRESS, 0); 604 $$.WriteMask = $2.mask; 605 } 606 ; 607 608 extendedSwizzle: extSwizComp ',' extSwizComp ',' extSwizComp ',' extSwizComp 609 { 610 const unsigned xyzw_valid = 611 ($1.xyzw_valid << 0) 612 | ($3.xyzw_valid << 1) 613 | ($5.xyzw_valid << 2) 614 | ($7.xyzw_valid << 3); 615 const unsigned rgba_valid = 616 ($1.rgba_valid << 0) 617 | ($3.rgba_valid << 1) 618 | ($5.rgba_valid << 2) 619 | ($7.rgba_valid << 3); 620 621 /* All of the swizzle components have to be valid in either RGBA 622 * or XYZW. Note that 0 and 1 are valid in both, so both masks 623 * can have some bits set. 624 * 625 * We somewhat deviate from the spec here. It would be really hard 626 * to figure out which component is the error, and there probably 627 * isn't a lot of benefit. 628 */ 629 if ((rgba_valid != 0x0f) && (xyzw_valid != 0x0f)) { 630 yyerror(& @1, state, "cannot combine RGBA and XYZW swizzle " 631 "components"); 632 YYERROR; 633 } 634 635 $$.swizzle = MAKE_SWIZZLE4($1.swz, $3.swz, $5.swz, $7.swz); 636 $$.mask = ($1.negate) | ($3.negate << 1) | ($5.negate << 2) 637 | ($7.negate << 3); 638 } 639 ; 640 641 extSwizComp: optionalSign extSwizSel 642 { 643 $$ = $2; 644 $$.negate = ($1) ? 1 : 0; 645 } 646 ; 647 648 extSwizSel: INTEGER 649 { 650 if (($1 != 0) && ($1 != 1)) { 651 yyerror(& @1, state, "invalid extended swizzle selector"); 652 YYERROR; 653 } 654 655 $$.swz = ($1 == 0) ? SWIZZLE_ZERO : SWIZZLE_ONE; 656 $$.negate = 0; 657 658 /* 0 and 1 are valid for both RGBA swizzle names and XYZW 659 * swizzle names. 660 */ 661 $$.xyzw_valid = 1; 662 $$.rgba_valid = 1; 663 } 664 | string 665 { 666 char s; 667 668 if (strlen($1) > 1) { 669 yyerror(& @1, state, "invalid extended swizzle selector"); 670 YYERROR; 671 } 672 673 s = $1[0]; 674 free($1); 675 676 $$.rgba_valid = 0; 677 $$.xyzw_valid = 0; 678 $$.negate = 0; 679 680 switch (s) { 681 case 'x': 682 $$.swz = SWIZZLE_X; 683 $$.xyzw_valid = 1; 684 break; 685 case 'y': 686 $$.swz = SWIZZLE_Y; 687 $$.xyzw_valid = 1; 688 break; 689 case 'z': 690 $$.swz = SWIZZLE_Z; 691 $$.xyzw_valid = 1; 692 break; 693 case 'w': 694 $$.swz = SWIZZLE_W; 695 $$.xyzw_valid = 1; 696 break; 697 698 case 'r': 699 $$.swz = SWIZZLE_X; 700 $$.rgba_valid = 1; 701 break; 702 case 'g': 703 $$.swz = SWIZZLE_Y; 704 $$.rgba_valid = 1; 705 break; 706 case 'b': 707 $$.swz = SWIZZLE_Z; 708 $$.rgba_valid = 1; 709 break; 710 case 'a': 711 $$.swz = SWIZZLE_W; 712 $$.rgba_valid = 1; 713 break; 714 715 default: 716 yyerror(& @1, state, "invalid extended swizzle selector"); 717 YYERROR; 718 break; 719 } 720 } 721 ; 722 723 srcReg: USED_IDENTIFIER /* temporaryReg | progParamSingle */ 724 { 725 struct asm_symbol *const s = (struct asm_symbol *) 726 _mesa_symbol_table_find_symbol(state->st, $1); 727 728 free($1); 729 730 if (s == NULL) { 731 yyerror(& @1, state, "invalid operand variable"); 732 YYERROR; 733 } else if ((s->type != at_param) && (s->type != at_temp) 734 && (s->type != at_attrib)) { 735 yyerror(& @1, state, "invalid operand variable"); 736 YYERROR; 737 } else if ((s->type == at_param) && s->param_is_array) { 738 yyerror(& @1, state, "non-array access to array PARAM"); 739 YYERROR; 740 } 741 742 init_src_reg(& $$); 743 switch (s->type) { 744 case at_temp: 745 set_src_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding); 746 break; 747 case at_param: 748 set_src_reg_swz(& $$, s->param_binding_type, 749 s->param_binding_begin, 750 s->param_binding_swizzle); 751 break; 752 case at_attrib: 753 set_src_reg(& $$, PROGRAM_INPUT, s->attrib_binding); 754 state->prog->info.inputs_read |= BITFIELD64_BIT($$.Base.Index); 755 756 if (!validate_inputs(& @1, state)) { 757 YYERROR; 758 } 759 break; 760 761 default: 762 YYERROR; 763 break; 764 } 765 } 766 | attribBinding 767 { 768 set_src_reg(& $$, PROGRAM_INPUT, $1); 769 state->prog->info.inputs_read |= BITFIELD64_BIT($$.Base.Index); 770 771 if (!validate_inputs(& @1, state)) { 772 YYERROR; 773 } 774 } 775 | progParamArray '[' progParamArrayMem ']' 776 { 777 if (! $3.Base.RelAddr 778 && ((unsigned) $3.Base.Index >= $1->param_binding_length)) { 779 yyerror(& @3, state, "out of bounds array access"); 780 YYERROR; 781 } 782 783 init_src_reg(& $$); 784 $$.Base.File = $1->param_binding_type; 785 786 if ($3.Base.RelAddr) { 787 state->prog->arb.IndirectRegisterFiles |= (1 << $$.Base.File); 788 $1->param_accessed_indirectly = 1; 789 790 $$.Base.RelAddr = 1; 791 $$.Base.Index = $3.Base.Index; 792 $$.Symbol = $1; 793 } else { 794 $$.Base.Index = $1->param_binding_begin + $3.Base.Index; 795 } 796 } 797 | paramSingleItemUse 798 { 799 gl_register_file file = ($1.name != NULL) 800 ? $1.param_binding_type 801 : PROGRAM_CONSTANT; 802 set_src_reg_swz(& $$, file, $1.param_binding_begin, 803 $1.param_binding_swizzle); 804 } 805 ; 806 807 dstReg: resultBinding 808 { 809 set_dst_reg(& $$, PROGRAM_OUTPUT, $1); 810 } 811 | USED_IDENTIFIER /* temporaryReg | vertexResultReg */ 812 { 813 struct asm_symbol *const s = (struct asm_symbol *) 814 _mesa_symbol_table_find_symbol(state->st, $1); 815 816 free($1); 817 818 if (s == NULL) { 819 yyerror(& @1, state, "invalid operand variable"); 820 YYERROR; 821 } else if ((s->type != at_output) && (s->type != at_temp)) { 822 yyerror(& @1, state, "invalid operand variable"); 823 YYERROR; 824 } 825 826 switch (s->type) { 827 case at_temp: 828 set_dst_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding); 829 break; 830 case at_output: 831 set_dst_reg(& $$, PROGRAM_OUTPUT, s->output_binding); 832 break; 833 default: 834 set_dst_reg(& $$, s->param_binding_type, s->param_binding_begin); 835 break; 836 } 837 } 838 ; 839 840 progParamArray: USED_IDENTIFIER 841 { 842 struct asm_symbol *const s = (struct asm_symbol *) 843 _mesa_symbol_table_find_symbol(state->st, $1); 844 845 free($1); 846 847 if (s == NULL) { 848 yyerror(& @1, state, "invalid operand variable"); 849 YYERROR; 850 } else if ((s->type != at_param) || !s->param_is_array) { 851 yyerror(& @1, state, "array access to non-PARAM variable"); 852 YYERROR; 853 } else { 854 $$ = s; 855 } 856 } 857 ; 858 859 progParamArrayMem: progParamArrayAbs | progParamArrayRel; 860 861 progParamArrayAbs: INTEGER 862 { 863 init_src_reg(& $$); 864 $$.Base.Index = $1; 865 } 866 ; 867 868 progParamArrayRel: addrReg addrComponent addrRegRelOffset 869 { 870 /* FINISHME: Add support for multiple address registers. 871 */ 872 /* FINISHME: Add support for 4-component address registers. 873 */ 874 init_src_reg(& $$); 875 $$.Base.RelAddr = 1; 876 $$.Base.Index = $3; 877 } 878 ; 879 880 addrRegRelOffset: { $$ = 0; } 881 | '+' addrRegPosOffset { $$ = $2; } 882 | '-' addrRegNegOffset { $$ = -$2; } 883 ; 884 885 addrRegPosOffset: INTEGER 886 { 887 if (($1 < 0) || ($1 > (state->limits->MaxAddressOffset - 1))) { 888 char s[100]; 889 _mesa_snprintf(s, sizeof(s), 890 "relative address offset too large (%d)", $1); 891 yyerror(& @1, state, s); 892 YYERROR; 893 } else { 894 $$ = $1; 895 } 896 } 897 ; 898 899 addrRegNegOffset: INTEGER 900 { 901 if (($1 < 0) || ($1 > state->limits->MaxAddressOffset)) { 902 char s[100]; 903 _mesa_snprintf(s, sizeof(s), 904 "relative address offset too large (%d)", $1); 905 yyerror(& @1, state, s); 906 YYERROR; 907 } else { 908 $$ = $1; 909 } 910 } 911 ; 912 913 addrReg: USED_IDENTIFIER 914 { 915 struct asm_symbol *const s = (struct asm_symbol *) 916 _mesa_symbol_table_find_symbol(state->st, $1); 917 918 free($1); 919 920 if (s == NULL) { 921 yyerror(& @1, state, "invalid array member"); 922 YYERROR; 923 } else if (s->type != at_address) { 924 yyerror(& @1, state, 925 "invalid variable for indexed array access"); 926 YYERROR; 927 } else { 928 $$ = s; 929 } 930 } 931 ; 932 933 addrComponent: MASK1 934 { 935 if ($1.mask != WRITEMASK_X) { 936 yyerror(& @1, state, "invalid address component selector"); 937 YYERROR; 938 } else { 939 $$ = $1; 940 } 941 } 942 ; 943 944 addrWriteMask: MASK1 945 { 946 if ($1.mask != WRITEMASK_X) { 947 yyerror(& @1, state, 948 "address register write mask must be \".x\""); 949 YYERROR; 950 } else { 951 $$ = $1; 952 } 953 } 954 ; 955 956 scalarSuffix: MASK1; 957 958 swizzleSuffix: MASK1 959 | MASK4 960 | SWIZZLE 961 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; } 962 ; 963 964 optionalMask: MASK4 | MASK3 | MASK2 | MASK1 965 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; } 966 ; 967 968 namingStatement: ATTRIB_statement 969 | PARAM_statement 970 | TEMP_statement 971 | ADDRESS_statement 972 | OUTPUT_statement 973 | ALIAS_statement 974 ; 975 976 ATTRIB_statement: ATTRIB IDENTIFIER '=' attribBinding 977 { 978 struct asm_symbol *const s = 979 declare_variable(state, $2, at_attrib, & @2); 980 981 if (s == NULL) { 982 free($2); 983 YYERROR; 984 } else { 985 s->attrib_binding = $4; 986 state->InputsBound |= BITFIELD64_BIT(s->attrib_binding); 987 988 if (!validate_inputs(& @4, state)) { 989 YYERROR; 990 } 991 } 992 } 993 ; 994 995 attribBinding: VERTEX vtxAttribItem 996 { 997 $$ = $2; 998 } 999 | FRAGMENT fragAttribItem 1000 { 1001 $$ = $2; 1002 } 1003 ; 1004 1005 vtxAttribItem: POSITION 1006 { 1007 $$ = VERT_ATTRIB_POS; 1008 } 1009 | NORMAL 1010 { 1011 $$ = VERT_ATTRIB_NORMAL; 1012 } 1013 | COLOR optColorType 1014 { 1015 $$ = VERT_ATTRIB_COLOR0 + $2; 1016 } 1017 | FOGCOORD 1018 { 1019 $$ = VERT_ATTRIB_FOG; 1020 } 1021 | TEXCOORD optTexCoordUnitNum 1022 { 1023 $$ = VERT_ATTRIB_TEX0 + $2; 1024 } 1025 | MATRIXINDEX '[' vtxWeightNum ']' 1026 { 1027 yyerror(& @1, state, "GL_ARB_matrix_palette not supported"); 1028 YYERROR; 1029 } 1030 | VTXATTRIB '[' vtxAttribNum ']' 1031 { 1032 $$ = VERT_ATTRIB_GENERIC0 + $3; 1033 } 1034 ; 1035 1036 vtxAttribNum: INTEGER 1037 { 1038 if ((unsigned) $1 >= state->limits->MaxAttribs) { 1039 yyerror(& @1, state, "invalid vertex attribute reference"); 1040 YYERROR; 1041 } 1042 1043 $$ = $1; 1044 } 1045 ; 1046 1047 vtxWeightNum: INTEGER; 1048 1049 fragAttribItem: POSITION 1050 { 1051 $$ = VARYING_SLOT_POS; 1052 } 1053 | COLOR optColorType 1054 { 1055 $$ = VARYING_SLOT_COL0 + $2; 1056 } 1057 | FOGCOORD 1058 { 1059 $$ = VARYING_SLOT_FOGC; 1060 } 1061 | TEXCOORD optTexCoordUnitNum 1062 { 1063 $$ = VARYING_SLOT_TEX0 + $2; 1064 } 1065 ; 1066 1067 PARAM_statement: PARAM_singleStmt | PARAM_multipleStmt; 1068 1069 PARAM_singleStmt: PARAM IDENTIFIER paramSingleInit 1070 { 1071 struct asm_symbol *const s = 1072 declare_variable(state, $2, at_param, & @2); 1073 1074 if (s == NULL) { 1075 free($2); 1076 YYERROR; 1077 } else { 1078 s->param_binding_type = $3.param_binding_type; 1079 s->param_binding_begin = $3.param_binding_begin; 1080 s->param_binding_length = $3.param_binding_length; 1081 s->param_binding_swizzle = $3.param_binding_swizzle; 1082 s->param_is_array = 0; 1083 } 1084 } 1085 ; 1086 1087 PARAM_multipleStmt: PARAM IDENTIFIER '[' optArraySize ']' paramMultipleInit 1088 { 1089 if (($4 != 0) && ((unsigned) $4 != $6.param_binding_length)) { 1090 free($2); 1091 yyerror(& @4, state, 1092 "parameter array size and number of bindings must match"); 1093 YYERROR; 1094 } else { 1095 struct asm_symbol *const s = 1096 declare_variable(state, $2, $6.type, & @2); 1097 1098 if (s == NULL) { 1099 free($2); 1100 YYERROR; 1101 } else { 1102 s->param_binding_type = $6.param_binding_type; 1103 s->param_binding_begin = $6.param_binding_begin; 1104 s->param_binding_length = $6.param_binding_length; 1105 s->param_binding_swizzle = SWIZZLE_XYZW; 1106 s->param_is_array = 1; 1107 } 1108 } 1109 } 1110 ; 1111 1112 optArraySize: 1113 { 1114 $$ = 0; 1115 } 1116 | INTEGER 1117 { 1118 if (($1 < 1) || ((unsigned) $1 > state->limits->MaxParameters)) { 1119 char msg[100]; 1120 _mesa_snprintf(msg, sizeof(msg), 1121 "invalid parameter array size (size=%d max=%u)", 1122 $1, state->limits->MaxParameters); 1123 yyerror(& @1, state, msg); 1124 YYERROR; 1125 } else { 1126 $$ = $1; 1127 } 1128 } 1129 ; 1130 1131 paramSingleInit: '=' paramSingleItemDecl 1132 { 1133 $$ = $2; 1134 } 1135 ; 1136 1137 paramMultipleInit: '=' '{' paramMultInitList '}' 1138 { 1139 $$ = $3; 1140 } 1141 ; 1142 1143 paramMultInitList: paramMultipleItem 1144 | paramMultInitList ',' paramMultipleItem 1145 { 1146 $1.param_binding_length += $3.param_binding_length; 1147 $$ = $1; 1148 } 1149 ; 1150 1151 paramSingleItemDecl: stateSingleItem 1152 { 1153 memset(& $$, 0, sizeof($$)); 1154 $$.param_binding_begin = ~0; 1155 initialize_symbol_from_state(state->prog, & $$, $1); 1156 } 1157 | programSingleItem 1158 { 1159 memset(& $$, 0, sizeof($$)); 1160 $$.param_binding_begin = ~0; 1161 initialize_symbol_from_param(state->prog, & $$, $1); 1162 } 1163 | paramConstDecl 1164 { 1165 memset(& $$, 0, sizeof($$)); 1166 $$.param_binding_begin = ~0; 1167 initialize_symbol_from_const(state->prog, & $$, & $1, GL_TRUE); 1168 } 1169 ; 1170 1171 paramSingleItemUse: stateSingleItem 1172 { 1173 memset(& $$, 0, sizeof($$)); 1174 $$.param_binding_begin = ~0; 1175 initialize_symbol_from_state(state->prog, & $$, $1); 1176 } 1177 | programSingleItem 1178 { 1179 memset(& $$, 0, sizeof($$)); 1180 $$.param_binding_begin = ~0; 1181 initialize_symbol_from_param(state->prog, & $$, $1); 1182 } 1183 | paramConstUse 1184 { 1185 memset(& $$, 0, sizeof($$)); 1186 $$.param_binding_begin = ~0; 1187 initialize_symbol_from_const(state->prog, & $$, & $1, GL_TRUE); 1188 } 1189 ; 1190 1191 paramMultipleItem: stateMultipleItem 1192 { 1193 memset(& $$, 0, sizeof($$)); 1194 $$.param_binding_begin = ~0; 1195 initialize_symbol_from_state(state->prog, & $$, $1); 1196 } 1197 | programMultipleItem 1198 { 1199 memset(& $$, 0, sizeof($$)); 1200 $$.param_binding_begin = ~0; 1201 initialize_symbol_from_param(state->prog, & $$, $1); 1202 } 1203 | paramConstDecl 1204 { 1205 memset(& $$, 0, sizeof($$)); 1206 $$.param_binding_begin = ~0; 1207 initialize_symbol_from_const(state->prog, & $$, & $1, GL_FALSE); 1208 } 1209 ; 1210 1211 stateMultipleItem: stateSingleItem { memcpy($$, $1, sizeof($$)); } 1212 | STATE stateMatrixRows { memcpy($$, $2, sizeof($$)); } 1213 ; 1214 1215 stateSingleItem: STATE stateMaterialItem { memcpy($$, $2, sizeof($$)); } 1216 | STATE stateLightItem { memcpy($$, $2, sizeof($$)); } 1217 | STATE stateLightModelItem { memcpy($$, $2, sizeof($$)); } 1218 | STATE stateLightProdItem { memcpy($$, $2, sizeof($$)); } 1219 | STATE stateTexGenItem { memcpy($$, $2, sizeof($$)); } 1220 | STATE stateTexEnvItem { memcpy($$, $2, sizeof($$)); } 1221 | STATE stateFogItem { memcpy($$, $2, sizeof($$)); } 1222 | STATE stateClipPlaneItem { memcpy($$, $2, sizeof($$)); } 1223 | STATE statePointItem { memcpy($$, $2, sizeof($$)); } 1224 | STATE stateMatrixRow { memcpy($$, $2, sizeof($$)); } 1225 | STATE stateDepthItem { memcpy($$, $2, sizeof($$)); } 1226 ; 1227 1228 stateMaterialItem: MATERIAL optFaceType stateMatProperty 1229 { 1230 memset($$, 0, sizeof($$)); 1231 $$[0] = STATE_MATERIAL; 1232 $$[1] = $2; 1233 $$[2] = $3; 1234 } 1235 ; 1236 1237 stateMatProperty: ambDiffSpecProperty 1238 { 1239 $$ = $1; 1240 } 1241 | EMISSION 1242 { 1243 $$ = STATE_EMISSION; 1244 } 1245 | SHININESS 1246 { 1247 $$ = STATE_SHININESS; 1248 } 1249 ; 1250 1251 stateLightItem: LIGHT '[' stateLightNumber ']' stateLightProperty 1252 { 1253 memset($$, 0, sizeof($$)); 1254 $$[0] = STATE_LIGHT; 1255 $$[1] = $3; 1256 $$[2] = $5; 1257 } 1258 ; 1259 1260 stateLightProperty: ambDiffSpecProperty 1261 { 1262 $$ = $1; 1263 } 1264 | POSITION 1265 { 1266 $$ = STATE_POSITION; 1267 } 1268 | ATTENUATION 1269 { 1270 if (!state->ctx->Extensions.EXT_point_parameters) { 1271 yyerror(& @1, state, "GL_ARB_point_parameters not supported"); 1272 YYERROR; 1273 } 1274 1275 $$ = STATE_ATTENUATION; 1276 } 1277 | SPOT stateSpotProperty 1278 { 1279 $$ = $2; 1280 } 1281 | HALF 1282 { 1283 $$ = STATE_HALF_VECTOR; 1284 } 1285 ; 1286 1287 stateSpotProperty: DIRECTION 1288 { 1289 $$ = STATE_SPOT_DIRECTION; 1290 } 1291 ; 1292 1293 stateLightModelItem: LIGHTMODEL stateLModProperty 1294 { 1295 $$[0] = $2[0]; 1296 $$[1] = $2[1]; 1297 } 1298 ; 1299 1300 stateLModProperty: AMBIENT 1301 { 1302 memset($$, 0, sizeof($$)); 1303 $$[0] = STATE_LIGHTMODEL_AMBIENT; 1304 } 1305 | optFaceType SCENECOLOR 1306 { 1307 memset($$, 0, sizeof($$)); 1308 $$[0] = STATE_LIGHTMODEL_SCENECOLOR; 1309 $$[1] = $1; 1310 } 1311 ; 1312 1313 stateLightProdItem: LIGHTPROD '[' stateLightNumber ']' optFaceType stateLProdProperty 1314 { 1315 memset($$, 0, sizeof($$)); 1316 $$[0] = STATE_LIGHTPROD; 1317 $$[1] = $3; 1318 $$[2] = $5; 1319 $$[3] = $6; 1320 } 1321 ; 1322 1323 stateLProdProperty: ambDiffSpecProperty; 1324 1325 stateTexEnvItem: TEXENV optLegacyTexUnitNum stateTexEnvProperty 1326 { 1327 memset($$, 0, sizeof($$)); 1328 $$[0] = $3; 1329 $$[1] = $2; 1330 } 1331 ; 1332 1333 stateTexEnvProperty: COLOR 1334 { 1335 $$ = STATE_TEXENV_COLOR; 1336 } 1337 ; 1338 1339 ambDiffSpecProperty: AMBIENT 1340 { 1341 $$ = STATE_AMBIENT; 1342 } 1343 | DIFFUSE 1344 { 1345 $$ = STATE_DIFFUSE; 1346 } 1347 | SPECULAR 1348 { 1349 $$ = STATE_SPECULAR; 1350 } 1351 ; 1352 1353 stateLightNumber: INTEGER 1354 { 1355 if ((unsigned) $1 >= state->MaxLights) { 1356 yyerror(& @1, state, "invalid light selector"); 1357 YYERROR; 1358 } 1359 1360 $$ = $1; 1361 } 1362 ; 1363 1364 stateTexGenItem: TEXGEN optTexCoordUnitNum stateTexGenType stateTexGenCoord 1365 { 1366 memset($$, 0, sizeof($$)); 1367 $$[0] = STATE_TEXGEN; 1368 $$[1] = $2; 1369 $$[2] = $3 + $4; 1370 } 1371 ; 1372 1373 stateTexGenType: EYE 1374 { 1375 $$ = STATE_TEXGEN_EYE_S; 1376 } 1377 | OBJECT 1378 { 1379 $$ = STATE_TEXGEN_OBJECT_S; 1380 } 1381 ; 1382 stateTexGenCoord: TEXGEN_S 1383 { 1384 $$ = STATE_TEXGEN_EYE_S - STATE_TEXGEN_EYE_S; 1385 } 1386 | TEXGEN_T 1387 { 1388 $$ = STATE_TEXGEN_EYE_T - STATE_TEXGEN_EYE_S; 1389 } 1390 | TEXGEN_R 1391 { 1392 $$ = STATE_TEXGEN_EYE_R - STATE_TEXGEN_EYE_S; 1393 } 1394 | TEXGEN_Q 1395 { 1396 $$ = STATE_TEXGEN_EYE_Q - STATE_TEXGEN_EYE_S; 1397 } 1398 ; 1399 1400 stateFogItem: FOG stateFogProperty 1401 { 1402 memset($$, 0, sizeof($$)); 1403 $$[0] = $2; 1404 } 1405 ; 1406 1407 stateFogProperty: COLOR 1408 { 1409 $$ = STATE_FOG_COLOR; 1410 } 1411 | PARAMS 1412 { 1413 $$ = STATE_FOG_PARAMS; 1414 } 1415 ; 1416 1417 stateClipPlaneItem: CLIP '[' stateClipPlaneNum ']' PLANE 1418 { 1419 memset($$, 0, sizeof($$)); 1420 $$[0] = STATE_CLIPPLANE; 1421 $$[1] = $3; 1422 } 1423 ; 1424 1425 stateClipPlaneNum: INTEGER 1426 { 1427 if ((unsigned) $1 >= state->MaxClipPlanes) { 1428 yyerror(& @1, state, "invalid clip plane selector"); 1429 YYERROR; 1430 } 1431 1432 $$ = $1; 1433 } 1434 ; 1435 1436 statePointItem: POINT_TOK statePointProperty 1437 { 1438 memset($$, 0, sizeof($$)); 1439 $$[0] = $2; 1440 } 1441 ; 1442 1443 statePointProperty: SIZE_TOK 1444 { 1445 $$ = STATE_POINT_SIZE; 1446 } 1447 | ATTENUATION 1448 { 1449 $$ = STATE_POINT_ATTENUATION; 1450 } 1451 ; 1452 1453 stateMatrixRow: stateMatrixItem ROW '[' stateMatrixRowNum ']' 1454 { 1455 $$[0] = $1[0]; 1456 $$[1] = $1[1]; 1457 $$[2] = $4; 1458 $$[3] = $4; 1459 $$[4] = $1[2]; 1460 } 1461 ; 1462 1463 stateMatrixRows: stateMatrixItem optMatrixRows 1464 { 1465 $$[0] = $1[0]; 1466 $$[1] = $1[1]; 1467 $$[2] = $2[2]; 1468 $$[3] = $2[3]; 1469 $$[4] = $1[2]; 1470 } 1471 ; 1472 1473 optMatrixRows: 1474 { 1475 $$[2] = 0; 1476 $$[3] = 3; 1477 } 1478 | ROW '[' stateMatrixRowNum DOT_DOT stateMatrixRowNum ']' 1479 { 1480 /* It seems logical that the matrix row range specifier would have 1481 * to specify a range or more than one row (i.e., $5 > $3). 1482 * However, the ARB_vertex_program spec says "a program will fail 1483 * to load if <a> is greater than <b>." This means that $3 == $5 1484 * is valid. 1485 */ 1486 if ($3 > $5) { 1487 yyerror(& @3, state, "invalid matrix row range"); 1488 YYERROR; 1489 } 1490 1491 $$[2] = $3; 1492 $$[3] = $5; 1493 } 1494 ; 1495 1496 stateMatrixItem: MATRIX stateMatrixName stateOptMatModifier 1497 { 1498 $$[0] = $2[0]; 1499 $$[1] = $2[1]; 1500 $$[2] = $3; 1501 } 1502 ; 1503 1504 stateOptMatModifier: 1505 { 1506 $$ = 0; 1507 } 1508 | stateMatModifier 1509 { 1510 $$ = $1; 1511 } 1512 ; 1513 1514 stateMatModifier: INVERSE 1515 { 1516 $$ = STATE_MATRIX_INVERSE; 1517 } 1518 | TRANSPOSE 1519 { 1520 $$ = STATE_MATRIX_TRANSPOSE; 1521 } 1522 | INVTRANS 1523 { 1524 $$ = STATE_MATRIX_INVTRANS; 1525 } 1526 ; 1527 1528 stateMatrixRowNum: INTEGER 1529 { 1530 if ($1 > 3) { 1531 yyerror(& @1, state, "invalid matrix row reference"); 1532 YYERROR; 1533 } 1534 1535 $$ = $1; 1536 } 1537 ; 1538 1539 stateMatrixName: MODELVIEW stateOptModMatNum 1540 { 1541 $$[0] = STATE_MODELVIEW_MATRIX; 1542 $$[1] = $2; 1543 } 1544 | PROJECTION 1545 { 1546 $$[0] = STATE_PROJECTION_MATRIX; 1547 $$[1] = 0; 1548 } 1549 | MVP 1550 { 1551 $$[0] = STATE_MVP_MATRIX; 1552 $$[1] = 0; 1553 } 1554 | TEXTURE optTexCoordUnitNum 1555 { 1556 $$[0] = STATE_TEXTURE_MATRIX; 1557 $$[1] = $2; 1558 } 1559 | PALETTE '[' statePaletteMatNum ']' 1560 { 1561 yyerror(& @1, state, "GL_ARB_matrix_palette not supported"); 1562 YYERROR; 1563 } 1564 | MAT_PROGRAM '[' stateProgramMatNum ']' 1565 { 1566 $$[0] = STATE_PROGRAM_MATRIX; 1567 $$[1] = $3; 1568 } 1569 ; 1570 1571 stateOptModMatNum: 1572 { 1573 $$ = 0; 1574 } 1575 | '[' stateModMatNum ']' 1576 { 1577 $$ = $2; 1578 } 1579 ; 1580 stateModMatNum: INTEGER 1581 { 1582 /* Since GL_ARB_vertex_blend isn't supported, only modelview matrix 1583 * zero is valid. 1584 */ 1585 if ($1 != 0) { 1586 yyerror(& @1, state, "invalid modelview matrix index"); 1587 YYERROR; 1588 } 1589 1590 $$ = $1; 1591 } 1592 ; 1593 statePaletteMatNum: INTEGER 1594 { 1595 /* Since GL_ARB_matrix_palette isn't supported, just let any value 1596 * through here. The error will be generated later. 1597 */ 1598 $$ = $1; 1599 } 1600 ; 1601 stateProgramMatNum: INTEGER 1602 { 1603 if ((unsigned) $1 >= state->MaxProgramMatrices) { 1604 yyerror(& @1, state, "invalid program matrix selector"); 1605 YYERROR; 1606 } 1607 1608 $$ = $1; 1609 } 1610 ; 1611 1612 stateDepthItem: DEPTH RANGE 1613 { 1614 memset($$, 0, sizeof($$)); 1615 $$[0] = STATE_DEPTH_RANGE; 1616 } 1617 ; 1618 1619 1620 programSingleItem: progEnvParam | progLocalParam; 1621 1622 programMultipleItem: progEnvParams | progLocalParams; 1623 1624 progEnvParams: PROGRAM ENV '[' progEnvParamNums ']' 1625 { 1626 memset($$, 0, sizeof($$)); 1627 $$[0] = state->state_param_enum; 1628 $$[1] = STATE_ENV; 1629 $$[2] = $4[0]; 1630 $$[3] = $4[1]; 1631 } 1632 ; 1633 1634 progEnvParamNums: progEnvParamNum 1635 { 1636 $$[0] = $1; 1637 $$[1] = $1; 1638 } 1639 | progEnvParamNum DOT_DOT progEnvParamNum 1640 { 1641 $$[0] = $1; 1642 $$[1] = $3; 1643 } 1644 ; 1645 1646 progEnvParam: PROGRAM ENV '[' progEnvParamNum ']' 1647 { 1648 memset($$, 0, sizeof($$)); 1649 $$[0] = state->state_param_enum; 1650 $$[1] = STATE_ENV; 1651 $$[2] = $4; 1652 $$[3] = $4; 1653 } 1654 ; 1655 1656 progLocalParams: PROGRAM LOCAL '[' progLocalParamNums ']' 1657 { 1658 memset($$, 0, sizeof($$)); 1659 $$[0] = state->state_param_enum; 1660 $$[1] = STATE_LOCAL; 1661 $$[2] = $4[0]; 1662 $$[3] = $4[1]; 1663 } 1664 1665 progLocalParamNums: progLocalParamNum 1666 { 1667 $$[0] = $1; 1668 $$[1] = $1; 1669 } 1670 | progLocalParamNum DOT_DOT progLocalParamNum 1671 { 1672 $$[0] = $1; 1673 $$[1] = $3; 1674 } 1675 ; 1676 1677 progLocalParam: PROGRAM LOCAL '[' progLocalParamNum ']' 1678 { 1679 memset($$, 0, sizeof($$)); 1680 $$[0] = state->state_param_enum; 1681 $$[1] = STATE_LOCAL; 1682 $$[2] = $4; 1683 $$[3] = $4; 1684 } 1685 ; 1686 1687 progEnvParamNum: INTEGER 1688 { 1689 if ((unsigned) $1 >= state->limits->MaxEnvParams) { 1690 yyerror(& @1, state, "invalid environment parameter reference"); 1691 YYERROR; 1692 } 1693 $$ = $1; 1694 } 1695 ; 1696 1697 progLocalParamNum: INTEGER 1698 { 1699 if ((unsigned) $1 >= state->limits->MaxLocalParams) { 1700 yyerror(& @1, state, "invalid local parameter reference"); 1701 YYERROR; 1702 } 1703 $$ = $1; 1704 } 1705 ; 1706 1707 1708 1709 paramConstDecl: paramConstScalarDecl | paramConstVector; 1710 paramConstUse: paramConstScalarUse | paramConstVector; 1711 1712 paramConstScalarDecl: signedFloatConstant 1713 { 1714 $$.count = 4; 1715 $$.data[0].f = $1; 1716 $$.data[1].f = $1; 1717 $$.data[2].f = $1; 1718 $$.data[3].f = $1; 1719 } 1720 ; 1721 1722 paramConstScalarUse: REAL 1723 { 1724 $$.count = 1; 1725 $$.data[0].f = $1; 1726 $$.data[1].f = $1; 1727 $$.data[2].f = $1; 1728 $$.data[3].f = $1; 1729 } 1730 | INTEGER 1731 { 1732 $$.count = 1; 1733 $$.data[0].f = (float) $1; 1734 $$.data[1].f = (float) $1; 1735 $$.data[2].f = (float) $1; 1736 $$.data[3].f = (float) $1; 1737 } 1738 ; 1739 1740 paramConstVector: '{' signedFloatConstant '}' 1741 { 1742 $$.count = 4; 1743 $$.data[0].f = $2; 1744 $$.data[1].f = 0.0f; 1745 $$.data[2].f = 0.0f; 1746 $$.data[3].f = 1.0f; 1747 } 1748 | '{' signedFloatConstant ',' signedFloatConstant '}' 1749 { 1750 $$.count = 4; 1751 $$.data[0].f = $2; 1752 $$.data[1].f = $4; 1753 $$.data[2].f = 0.0f; 1754 $$.data[3].f = 1.0f; 1755 } 1756 | '{' signedFloatConstant ',' signedFloatConstant ',' 1757 signedFloatConstant '}' 1758 { 1759 $$.count = 4; 1760 $$.data[0].f = $2; 1761 $$.data[1].f = $4; 1762 $$.data[2].f = $6; 1763 $$.data[3].f = 1.0f; 1764 } 1765 | '{' signedFloatConstant ',' signedFloatConstant ',' 1766 signedFloatConstant ',' signedFloatConstant '}' 1767 { 1768 $$.count = 4; 1769 $$.data[0].f = $2; 1770 $$.data[1].f = $4; 1771 $$.data[2].f = $6; 1772 $$.data[3].f = $8; 1773 } 1774 ; 1775 1776 signedFloatConstant: optionalSign REAL 1777 { 1778 $$ = ($1) ? -$2 : $2; 1779 } 1780 | optionalSign INTEGER 1781 { 1782 $$ = (float)(($1) ? -$2 : $2); 1783 } 1784 ; 1785 1786 optionalSign: '+' { $$ = FALSE; } 1787 | '-' { $$ = TRUE; } 1788 | { $$ = FALSE; } 1789 ; 1790 1791 TEMP_statement: TEMP { $<integer>$ = $1; } varNameList 1792 ; 1793 1794 ADDRESS_statement: ADDRESS { $<integer>$ = $1; } varNameList 1795 ; 1796 1797 varNameList: varNameList ',' IDENTIFIER 1798 { 1799 if (!declare_variable(state, $3, $<integer>0, & @3)) { 1800 free($3); 1801 YYERROR; 1802 } 1803 } 1804 | IDENTIFIER 1805 { 1806 if (!declare_variable(state, $1, $<integer>0, & @1)) { 1807 free($1); 1808 YYERROR; 1809 } 1810 } 1811 ; 1812 1813 OUTPUT_statement: OUTPUT IDENTIFIER '=' resultBinding 1814 { 1815 struct asm_symbol *const s = 1816 declare_variable(state, $2, at_output, & @2); 1817 1818 if (s == NULL) { 1819 free($2); 1820 YYERROR; 1821 } else { 1822 s->output_binding = $4; 1823 } 1824 } 1825 ; 1826 1827 resultBinding: RESULT POSITION 1828 { 1829 if (state->mode == ARB_vertex) { 1830 $$ = VARYING_SLOT_POS; 1831 } else { 1832 yyerror(& @2, state, "invalid program result name"); 1833 YYERROR; 1834 } 1835 } 1836 | RESULT FOGCOORD 1837 { 1838 if (state->mode == ARB_vertex) { 1839 $$ = VARYING_SLOT_FOGC; 1840 } else { 1841 yyerror(& @2, state, "invalid program result name"); 1842 YYERROR; 1843 } 1844 } 1845 | RESULT resultColBinding 1846 { 1847 $$ = $2; 1848 } 1849 | RESULT POINTSIZE 1850 { 1851 if (state->mode == ARB_vertex) { 1852 $$ = VARYING_SLOT_PSIZ; 1853 } else { 1854 yyerror(& @2, state, "invalid program result name"); 1855 YYERROR; 1856 } 1857 } 1858 | RESULT TEXCOORD optTexCoordUnitNum 1859 { 1860 if (state->mode == ARB_vertex) { 1861 $$ = VARYING_SLOT_TEX0 + $3; 1862 } else { 1863 yyerror(& @2, state, "invalid program result name"); 1864 YYERROR; 1865 } 1866 } 1867 | RESULT DEPTH 1868 { 1869 if (state->mode == ARB_fragment) { 1870 $$ = FRAG_RESULT_DEPTH; 1871 } else { 1872 yyerror(& @2, state, "invalid program result name"); 1873 YYERROR; 1874 } 1875 } 1876 ; 1877 1878 resultColBinding: COLOR optResultFaceType optResultColorType 1879 { 1880 $$ = $2 + $3; 1881 } 1882 ; 1883 1884 optResultFaceType: 1885 { 1886 if (state->mode == ARB_vertex) { 1887 $$ = VARYING_SLOT_COL0; 1888 } else { 1889 if (state->option.DrawBuffers) 1890 $$ = FRAG_RESULT_DATA0; 1891 else 1892 $$ = FRAG_RESULT_COLOR; 1893 } 1894 } 1895 | '[' INTEGER ']' 1896 { 1897 if (state->mode == ARB_vertex) { 1898 yyerror(& @1, state, "invalid program result name"); 1899 YYERROR; 1900 } else { 1901 if (!state->option.DrawBuffers) { 1902 /* From the ARB_draw_buffers spec (same text exists 1903 * for ATI_draw_buffers): 1904 * 1905 * If this option is not specified, a fragment 1906 * program that attempts to bind 1907 * "result.color[n]" will fail to load, and only 1908 * "result.color" will be allowed. 1909 */ 1910 yyerror(& @1, state, 1911 "result.color[] used without " 1912 "`OPTION ARB_draw_buffers' or " 1913 "`OPTION ATI_draw_buffers'"); 1914 YYERROR; 1915 } else if ($2 >= state->MaxDrawBuffers) { 1916 yyerror(& @1, state, 1917 "result.color[] exceeds MAX_DRAW_BUFFERS_ARB"); 1918 YYERROR; 1919 } 1920 $$ = FRAG_RESULT_DATA0 + $2; 1921 } 1922 } 1923 | FRONT 1924 { 1925 if (state->mode == ARB_vertex) { 1926 $$ = VARYING_SLOT_COL0; 1927 } else { 1928 yyerror(& @1, state, "invalid program result name"); 1929 YYERROR; 1930 } 1931 } 1932 | BACK 1933 { 1934 if (state->mode == ARB_vertex) { 1935 $$ = VARYING_SLOT_BFC0; 1936 } else { 1937 yyerror(& @1, state, "invalid program result name"); 1938 YYERROR; 1939 } 1940 } 1941 ; 1942 1943 optResultColorType: 1944 { 1945 $$ = 0; 1946 } 1947 | PRIMARY 1948 { 1949 if (state->mode == ARB_vertex) { 1950 $$ = 0; 1951 } else { 1952 yyerror(& @1, state, "invalid program result name"); 1953 YYERROR; 1954 } 1955 } 1956 | SECONDARY 1957 { 1958 if (state->mode == ARB_vertex) { 1959 $$ = 1; 1960 } else { 1961 yyerror(& @1, state, "invalid program result name"); 1962 YYERROR; 1963 } 1964 } 1965 ; 1966 1967 optFaceType: { $$ = 0; } 1968 | FRONT { $$ = 0; } 1969 | BACK { $$ = 1; } 1970 ; 1971 1972 optColorType: { $$ = 0; } 1973 | PRIMARY { $$ = 0; } 1974 | SECONDARY { $$ = 1; } 1975 ; 1976 1977 optTexCoordUnitNum: { $$ = 0; } 1978 | '[' texCoordUnitNum ']' { $$ = $2; } 1979 ; 1980 1981 optTexImageUnitNum: { $$ = 0; } 1982 | '[' texImageUnitNum ']' { $$ = $2; } 1983 ; 1984 1985 optLegacyTexUnitNum: { $$ = 0; } 1986 | '[' legacyTexUnitNum ']' { $$ = $2; } 1987 ; 1988 1989 texCoordUnitNum: INTEGER 1990 { 1991 if ((unsigned) $1 >= state->MaxTextureCoordUnits) { 1992 yyerror(& @1, state, "invalid texture coordinate unit selector"); 1993 YYERROR; 1994 } 1995 1996 $$ = $1; 1997 } 1998 ; 1999 2000 texImageUnitNum: INTEGER 2001 { 2002 if ((unsigned) $1 >= state->MaxTextureImageUnits) { 2003 yyerror(& @1, state, "invalid texture image unit selector"); 2004 YYERROR; 2005 } 2006 2007 $$ = $1; 2008 } 2009 ; 2010 2011 legacyTexUnitNum: INTEGER 2012 { 2013 if ((unsigned) $1 >= state->MaxTextureUnits) { 2014 yyerror(& @1, state, "invalid texture unit selector"); 2015 YYERROR; 2016 } 2017 2018 $$ = $1; 2019 } 2020 ; 2021 2022 ALIAS_statement: ALIAS IDENTIFIER '=' USED_IDENTIFIER 2023 { 2024 struct asm_symbol *exist = (struct asm_symbol *) 2025 _mesa_symbol_table_find_symbol(state->st, $2); 2026 struct asm_symbol *target = (struct asm_symbol *) 2027 _mesa_symbol_table_find_symbol(state->st, $4); 2028 2029 free($4); 2030 2031 if (exist != NULL) { 2032 char m[1000]; 2033 _mesa_snprintf(m, sizeof(m), "redeclared identifier: %s", $2); 2034 free($2); 2035 yyerror(& @2, state, m); 2036 YYERROR; 2037 } else if (target == NULL) { 2038 free($2); 2039 yyerror(& @4, state, 2040 "undefined variable binding in ALIAS statement"); 2041 YYERROR; 2042 } else { 2043 _mesa_symbol_table_add_symbol(state->st, $2, target); 2044 } 2045 } 2046 ; 2047 2048 string: IDENTIFIER 2049 | USED_IDENTIFIER 2050 ; 2051 2052 %% 2053 2054 void 2055 asm_instruction_set_operands(struct asm_instruction *inst, 2056 const struct prog_dst_register *dst, 2057 const struct asm_src_register *src0, 2058 const struct asm_src_register *src1, 2059 const struct asm_src_register *src2) 2060 { 2061 /* In the core ARB extensions only the KIL instruction doesn't have a 2062 * destination register. 2063 */ 2064 if (dst == NULL) { 2065 init_dst_reg(& inst->Base.DstReg); 2066 } else { 2067 inst->Base.DstReg = *dst; 2068 } 2069 2070 if (src0 != NULL) { 2071 inst->Base.SrcReg[0] = src0->Base; 2072 inst->SrcReg[0] = *src0; 2073 } else { 2074 init_src_reg(& inst->SrcReg[0]); 2075 } 2076 2077 if (src1 != NULL) { 2078 inst->Base.SrcReg[1] = src1->Base; 2079 inst->SrcReg[1] = *src1; 2080 } else { 2081 init_src_reg(& inst->SrcReg[1]); 2082 } 2083 2084 if (src2 != NULL) { 2085 inst->Base.SrcReg[2] = src2->Base; 2086 inst->SrcReg[2] = *src2; 2087 } else { 2088 init_src_reg(& inst->SrcReg[2]); 2089 } 2090 } 2091 2092 2093 struct asm_instruction * 2094 asm_instruction_ctor(enum prog_opcode op, 2095 const struct prog_dst_register *dst, 2096 const struct asm_src_register *src0, 2097 const struct asm_src_register *src1, 2098 const struct asm_src_register *src2) 2099 { 2100 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction); 2101 2102 if (inst) { 2103 _mesa_init_instructions(& inst->Base, 1); 2104 inst->Base.Opcode = op; 2105 2106 asm_instruction_set_operands(inst, dst, src0, src1, src2); 2107 } 2108 2109 return inst; 2110 } 2111 2112 2113 struct asm_instruction * 2114 asm_instruction_copy_ctor(const struct prog_instruction *base, 2115 const struct prog_dst_register *dst, 2116 const struct asm_src_register *src0, 2117 const struct asm_src_register *src1, 2118 const struct asm_src_register *src2) 2119 { 2120 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction); 2121 2122 if (inst) { 2123 _mesa_init_instructions(& inst->Base, 1); 2124 inst->Base.Opcode = base->Opcode; 2125 inst->Base.Saturate = base->Saturate; 2126 2127 asm_instruction_set_operands(inst, dst, src0, src1, src2); 2128 } 2129 2130 return inst; 2131 } 2132 2133 2134 void 2135 init_dst_reg(struct prog_dst_register *r) 2136 { 2137 memset(r, 0, sizeof(*r)); 2138 r->File = PROGRAM_UNDEFINED; 2139 r->WriteMask = WRITEMASK_XYZW; 2140 } 2141 2142 2143 /** Like init_dst_reg() but set the File and Index fields. */ 2144 void 2145 set_dst_reg(struct prog_dst_register *r, gl_register_file file, GLint index) 2146 { 2147 const GLint maxIndex = 1 << INST_INDEX_BITS; 2148 const GLint minIndex = 0; 2149 assert(index >= minIndex); 2150 (void) minIndex; 2151 assert(index <= maxIndex); 2152 (void) maxIndex; 2153 assert(file == PROGRAM_TEMPORARY || 2154 file == PROGRAM_ADDRESS || 2155 file == PROGRAM_OUTPUT); 2156 memset(r, 0, sizeof(*r)); 2157 r->File = file; 2158 r->Index = index; 2159 r->WriteMask = WRITEMASK_XYZW; 2160 } 2161 2162 2163 void 2164 init_src_reg(struct asm_src_register *r) 2165 { 2166 memset(r, 0, sizeof(*r)); 2167 r->Base.File = PROGRAM_UNDEFINED; 2168 r->Base.Swizzle = SWIZZLE_NOOP; 2169 r->Symbol = NULL; 2170 } 2171 2172 2173 /** Like init_src_reg() but set the File and Index fields. 2174 * \return GL_TRUE if a valid src register, GL_FALSE otherwise 2175 */ 2176 void 2177 set_src_reg(struct asm_src_register *r, gl_register_file file, GLint index) 2178 { 2179 set_src_reg_swz(r, file, index, SWIZZLE_XYZW); 2180 } 2181 2182 2183 void 2184 set_src_reg_swz(struct asm_src_register *r, gl_register_file file, GLint index, 2185 GLuint swizzle) 2186 { 2187 const GLint maxIndex = (1 << INST_INDEX_BITS) - 1; 2188 const GLint minIndex = -(1 << INST_INDEX_BITS); 2189 assert(file < PROGRAM_FILE_MAX); 2190 assert(index >= minIndex); 2191 (void) minIndex; 2192 assert(index <= maxIndex); 2193 (void) maxIndex; 2194 memset(r, 0, sizeof(*r)); 2195 r->Base.File = file; 2196 r->Base.Index = index; 2197 r->Base.Swizzle = swizzle; 2198 r->Symbol = NULL; 2199 } 2200 2201 2202 /** 2203 * Validate the set of inputs used by a program 2204 * 2205 * Validates that legal sets of inputs are used by the program. In this case 2206 * "used" included both reading the input or binding the input to a name using 2207 * the \c ATTRIB command. 2208 * 2209 * \return 2210 * \c TRUE if the combination of inputs used is valid, \c FALSE otherwise. 2211 */ 2212 int 2213 validate_inputs(struct YYLTYPE *locp, struct asm_parser_state *state) 2214 { 2215 const GLbitfield64 inputs = state->prog->info.inputs_read | state->InputsBound; 2216 GLbitfield ff_inputs = 0; 2217 2218 /* Since Mesa internal attribute indices are different from 2219 * how NV_vertex_program defines attribute aliasing, we have to construct 2220 * a separate usage mask based on how the aliasing is defined. 2221 * 2222 * Note that attribute aliasing is optional if NV_vertex_program is 2223 * unsupported. 2224 */ 2225 if (inputs & VERT_BIT_POS) 2226 ff_inputs |= 1 << 0; 2227 if (inputs & VERT_BIT_NORMAL) 2228 ff_inputs |= 1 << 2; 2229 if (inputs & VERT_BIT_COLOR0) 2230 ff_inputs |= 1 << 3; 2231 if (inputs & VERT_BIT_COLOR1) 2232 ff_inputs |= 1 << 4; 2233 if (inputs & VERT_BIT_FOG) 2234 ff_inputs |= 1 << 5; 2235 2236 ff_inputs |= ((inputs & VERT_BIT_TEX_ALL) >> VERT_ATTRIB_TEX0) << 8; 2237 2238 if ((ff_inputs & (inputs >> VERT_ATTRIB_GENERIC0)) != 0) { 2239 yyerror(locp, state, "illegal use of generic attribute and name attribute"); 2240 return 0; 2241 } 2242 2243 return 1; 2244 } 2245 2246 2247 struct asm_symbol * 2248 declare_variable(struct asm_parser_state *state, char *name, enum asm_type t, 2249 struct YYLTYPE *locp) 2250 { 2251 struct asm_symbol *s = NULL; 2252 struct asm_symbol *exist = (struct asm_symbol *) 2253 _mesa_symbol_table_find_symbol(state->st, name); 2254 2255 2256 if (exist != NULL) { 2257 yyerror(locp, state, "redeclared identifier"); 2258 } else { 2259 s = calloc(1, sizeof(struct asm_symbol)); 2260 s->name = name; 2261 s->type = t; 2262 2263 switch (t) { 2264 case at_temp: 2265 if (state->prog->arb.NumTemporaries >= state->limits->MaxTemps) { 2266 yyerror(locp, state, "too many temporaries declared"); 2267 free(s); 2268 return NULL; 2269 } 2270 2271 s->temp_binding = state->prog->arb.NumTemporaries; 2272 state->prog->arb.NumTemporaries++; 2273 break; 2274 2275 case at_address: 2276 if (state->prog->arb.NumAddressRegs >= 2277 state->limits->MaxAddressRegs) { 2278 yyerror(locp, state, "too many address registers declared"); 2279 free(s); 2280 return NULL; 2281 } 2282 2283 /* FINISHME: Add support for multiple address registers. 2284 */ 2285 state->prog->arb.NumAddressRegs++; 2286 break; 2287 2288 default: 2289 break; 2290 } 2291 2292 _mesa_symbol_table_add_symbol(state->st, s->name, s); 2293 s->next = state->sym; 2294 state->sym = s; 2295 } 2296 2297 return s; 2298 } 2299 2300 2301 int add_state_reference(struct gl_program_parameter_list *param_list, 2302 const gl_state_index tokens[STATE_LENGTH]) 2303 { 2304 const GLuint size = 4; /* XXX fix */ 2305 char *name; 2306 GLint index; 2307 2308 name = _mesa_program_state_string(tokens); 2309 index = _mesa_add_parameter(param_list, PROGRAM_STATE_VAR, name, 2310 size, GL_NONE, NULL, tokens); 2311 param_list->StateFlags |= _mesa_program_state_flags(tokens); 2312 2313 /* free name string here since we duplicated it in add_parameter() */ 2314 free(name); 2315 2316 return index; 2317 } 2318 2319 2320 int 2321 initialize_symbol_from_state(struct gl_program *prog, 2322 struct asm_symbol *param_var, 2323 const gl_state_index tokens[STATE_LENGTH]) 2324 { 2325 int idx = -1; 2326 gl_state_index state_tokens[STATE_LENGTH]; 2327 2328 2329 memcpy(state_tokens, tokens, sizeof(state_tokens)); 2330 2331 param_var->type = at_param; 2332 param_var->param_binding_type = PROGRAM_STATE_VAR; 2333 2334 /* If we are adding a STATE_MATRIX that has multiple rows, we need to 2335 * unroll it and call add_state_reference() for each row 2336 */ 2337 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX || 2338 state_tokens[0] == STATE_PROJECTION_MATRIX || 2339 state_tokens[0] == STATE_MVP_MATRIX || 2340 state_tokens[0] == STATE_TEXTURE_MATRIX || 2341 state_tokens[0] == STATE_PROGRAM_MATRIX) 2342 && (state_tokens[2] != state_tokens[3])) { 2343 int row; 2344 const int first_row = state_tokens[2]; 2345 const int last_row = state_tokens[3]; 2346 2347 for (row = first_row; row <= last_row; row++) { 2348 state_tokens[2] = state_tokens[3] = row; 2349 2350 idx = add_state_reference(prog->Parameters, state_tokens); 2351 if (param_var->param_binding_begin == ~0U) { 2352 param_var->param_binding_begin = idx; 2353 param_var->param_binding_swizzle = SWIZZLE_XYZW; 2354 } 2355 2356 param_var->param_binding_length++; 2357 } 2358 } 2359 else { 2360 idx = add_state_reference(prog->Parameters, state_tokens); 2361 if (param_var->param_binding_begin == ~0U) { 2362 param_var->param_binding_begin = idx; 2363 param_var->param_binding_swizzle = SWIZZLE_XYZW; 2364 } 2365 param_var->param_binding_length++; 2366 } 2367 2368 return idx; 2369 } 2370 2371 2372 int 2373 initialize_symbol_from_param(struct gl_program *prog, 2374 struct asm_symbol *param_var, 2375 const gl_state_index tokens[STATE_LENGTH]) 2376 { 2377 int idx = -1; 2378 gl_state_index state_tokens[STATE_LENGTH]; 2379 2380 2381 memcpy(state_tokens, tokens, sizeof(state_tokens)); 2382 2383 assert((state_tokens[0] == STATE_VERTEX_PROGRAM) 2384 || (state_tokens[0] == STATE_FRAGMENT_PROGRAM)); 2385 assert((state_tokens[1] == STATE_ENV) 2386 || (state_tokens[1] == STATE_LOCAL)); 2387 2388 /* 2389 * The param type is STATE_VAR. The program parameter entry will 2390 * effectively be a pointer into the LOCAL or ENV parameter array. 2391 */ 2392 param_var->type = at_param; 2393 param_var->param_binding_type = PROGRAM_STATE_VAR; 2394 2395 /* If we are adding a STATE_ENV or STATE_LOCAL that has multiple elements, 2396 * we need to unroll it and call add_state_reference() for each row 2397 */ 2398 if (state_tokens[2] != state_tokens[3]) { 2399 int row; 2400 const int first_row = state_tokens[2]; 2401 const int last_row = state_tokens[3]; 2402 2403 for (row = first_row; row <= last_row; row++) { 2404 state_tokens[2] = state_tokens[3] = row; 2405 2406 idx = add_state_reference(prog->Parameters, state_tokens); 2407 if (param_var->param_binding_begin == ~0U) { 2408 param_var->param_binding_begin = idx; 2409 param_var->param_binding_swizzle = SWIZZLE_XYZW; 2410 } 2411 param_var->param_binding_length++; 2412 } 2413 } 2414 else { 2415 idx = add_state_reference(prog->Parameters, state_tokens); 2416 if (param_var->param_binding_begin == ~0U) { 2417 param_var->param_binding_begin = idx; 2418 param_var->param_binding_swizzle = SWIZZLE_XYZW; 2419 } 2420 param_var->param_binding_length++; 2421 } 2422 2423 return idx; 2424 } 2425 2426 2427 /** 2428 * Put a float/vector constant/literal into the parameter list. 2429 * \param param_var returns info about the parameter/constant's location, 2430 * binding, type, etc. 2431 * \param vec the vector/constant to add 2432 * \param allowSwizzle if true, try to consolidate constants which only differ 2433 * by a swizzle. We don't want to do this when building 2434 * arrays of constants that may be indexed indirectly. 2435 * \return index of the constant in the parameter list. 2436 */ 2437 int 2438 initialize_symbol_from_const(struct gl_program *prog, 2439 struct asm_symbol *param_var, 2440 const struct asm_vector *vec, 2441 GLboolean allowSwizzle) 2442 { 2443 unsigned swizzle; 2444 const int idx = _mesa_add_unnamed_constant(prog->Parameters, 2445 vec->data, vec->count, 2446 allowSwizzle ? &swizzle : NULL); 2447 2448 param_var->type = at_param; 2449 param_var->param_binding_type = PROGRAM_CONSTANT; 2450 2451 if (param_var->param_binding_begin == ~0U) { 2452 param_var->param_binding_begin = idx; 2453 param_var->param_binding_swizzle = allowSwizzle ? swizzle : SWIZZLE_XYZW; 2454 } 2455 param_var->param_binding_length++; 2456 2457 return idx; 2458 } 2459 2460 2461 char * 2462 make_error_string(const char *fmt, ...) 2463 { 2464 int length; 2465 char *str; 2466 va_list args; 2467 2468 2469 /* Call vsnprintf once to determine how large the final string is. Call it 2470 * again to do the actual formatting. from the vsnprintf manual page: 2471 * 2472 * Upon successful return, these functions return the number of 2473 * characters printed (not including the trailing '\0' used to end 2474 * output to strings). 2475 */ 2476 va_start(args, fmt); 2477 length = 1 + vsnprintf(NULL, 0, fmt, args); 2478 va_end(args); 2479 2480 str = malloc(length); 2481 if (str) { 2482 va_start(args, fmt); 2483 vsnprintf(str, length, fmt, args); 2484 va_end(args); 2485 } 2486 2487 return str; 2488 } 2489 2490 2491 void 2492 yyerror(YYLTYPE *locp, struct asm_parser_state *state, const char *s) 2493 { 2494 char *err_str; 2495 2496 2497 err_str = make_error_string("glProgramStringARB(%s)\n", s); 2498 if (err_str) { 2499 _mesa_error(state->ctx, GL_INVALID_OPERATION, "%s", err_str); 2500 free(err_str); 2501 } 2502 2503 err_str = make_error_string("line %u, char %u: error: %s\n", 2504 locp->first_line, locp->first_column, s); 2505 _mesa_set_program_error(state->ctx, locp->position, err_str); 2506 2507 if (err_str) { 2508 free(err_str); 2509 } 2510 } 2511 2512 2513 GLboolean 2514 _mesa_parse_arb_program(struct gl_context *ctx, GLenum target, const GLubyte *str, 2515 GLsizei len, struct asm_parser_state *state) 2516 { 2517 struct asm_instruction *inst; 2518 unsigned i; 2519 GLubyte *strz; 2520 GLboolean result = GL_FALSE; 2521 void *temp; 2522 struct asm_symbol *sym; 2523 2524 state->ctx = ctx; 2525 state->prog->Target = target; 2526 state->prog->Parameters = _mesa_new_parameter_list(); 2527 2528 /* Make a copy of the program string and force it to be NUL-terminated. 2529 */ 2530 strz = (GLubyte *) ralloc_size(state->mem_ctx, len + 1); 2531 if (strz == NULL) { 2532 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB"); 2533 return GL_FALSE; 2534 } 2535 memcpy (strz, str, len); 2536 strz[len] = '\0'; 2537 2538 state->prog->String = strz; 2539 2540 state->st = _mesa_symbol_table_ctor(); 2541 2542 state->limits = (target == GL_VERTEX_PROGRAM_ARB) 2543 ? & ctx->Const.Program[MESA_SHADER_VERTEX] 2544 : & ctx->Const.Program[MESA_SHADER_FRAGMENT]; 2545 2546 state->MaxTextureImageUnits = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits; 2547 state->MaxTextureCoordUnits = ctx->Const.MaxTextureCoordUnits; 2548 state->MaxTextureUnits = ctx->Const.MaxTextureUnits; 2549 state->MaxClipPlanes = ctx->Const.MaxClipPlanes; 2550 state->MaxLights = ctx->Const.MaxLights; 2551 state->MaxProgramMatrices = ctx->Const.MaxProgramMatrices; 2552 state->MaxDrawBuffers = ctx->Const.MaxDrawBuffers; 2553 2554 state->state_param_enum = (target == GL_VERTEX_PROGRAM_ARB) 2555 ? STATE_VERTEX_PROGRAM : STATE_FRAGMENT_PROGRAM; 2556 2557 _mesa_set_program_error(ctx, -1, NULL); 2558 2559 _mesa_program_lexer_ctor(& state->scanner, state, (const char *) str, len); 2560 yyparse(state); 2561 _mesa_program_lexer_dtor(state->scanner); 2562 2563 2564 if (ctx->Program.ErrorPos != -1) { 2565 goto error; 2566 } 2567 2568 if (! _mesa_layout_parameters(state)) { 2569 struct YYLTYPE loc; 2570 2571 loc.first_line = 0; 2572 loc.first_column = 0; 2573 loc.position = len; 2574 2575 yyerror(& loc, state, "invalid PARAM usage"); 2576 goto error; 2577 } 2578 2579 2580 2581 /* Add one instruction to store the "END" instruction. 2582 */ 2583 state->prog->arb.Instructions = 2584 rzalloc_array(state->mem_ctx, struct prog_instruction, 2585 state->prog->arb.NumInstructions + 1); 2586 2587 if (state->prog->arb.Instructions == NULL) { 2588 goto error; 2589 } 2590 2591 inst = state->inst_head; 2592 for (i = 0; i < state->prog->arb.NumInstructions; i++) { 2593 struct asm_instruction *const temp = inst->next; 2594 2595 state->prog->arb.Instructions[i] = inst->Base; 2596 inst = temp; 2597 } 2598 2599 /* Finally, tag on an OPCODE_END instruction */ 2600 { 2601 const GLuint numInst = state->prog->arb.NumInstructions; 2602 _mesa_init_instructions(state->prog->arb.Instructions + numInst, 1); 2603 state->prog->arb.Instructions[numInst].Opcode = OPCODE_END; 2604 } 2605 state->prog->arb.NumInstructions++; 2606 2607 state->prog->arb.NumParameters = state->prog->Parameters->NumParameters; 2608 state->prog->arb.NumAttributes = 2609 _mesa_bitcount_64(state->prog->info.inputs_read); 2610 2611 /* 2612 * Initialize native counts to logical counts. The device driver may 2613 * change them if program is translated into a hardware program. 2614 */ 2615 state->prog->arb.NumNativeInstructions = state->prog->arb.NumInstructions; 2616 state->prog->arb.NumNativeTemporaries = state->prog->arb.NumTemporaries; 2617 state->prog->arb.NumNativeParameters = state->prog->arb.NumParameters; 2618 state->prog->arb.NumNativeAttributes = state->prog->arb.NumAttributes; 2619 state->prog->arb.NumNativeAddressRegs = state->prog->arb.NumAddressRegs; 2620 2621 result = GL_TRUE; 2622 2623 error: 2624 for (inst = state->inst_head; inst != NULL; inst = temp) { 2625 temp = inst->next; 2626 free(inst); 2627 } 2628 2629 state->inst_head = NULL; 2630 state->inst_tail = NULL; 2631 2632 for (sym = state->sym; sym != NULL; sym = temp) { 2633 temp = sym->next; 2634 2635 free((void *) sym->name); 2636 free(sym); 2637 } 2638 state->sym = NULL; 2639 2640 _mesa_symbol_table_dtor(state->st); 2641 state->st = NULL; 2642 2643 return result; 2644 } 2645