3 * Copyright © 2009 Intel Corporation
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:
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
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.
30 #include "main/errors.h"
31 #include "main/mtypes.h"
32 #include "main/imports.h"
33 #include "program/program.h"
34 #include "program/prog_parameter.h"
35 #include "program/prog_parameter_layout.h"
36 #include "program/prog_statevars.h"
37 #include "program/prog_instruction.h"
39 #include "program/symbol_table.h"
40 #include "program/program_parser.h"
42 extern void *yy_scan_string(char *);
43 extern void yy_delete_buffer(void *);
45 static struct asm_symbol *declare_variable(struct asm_parser_state *state,
46 char *name, enum asm_type t, struct YYLTYPE *locp);
48 static int add_state_reference(struct gl_program_parameter_list *param_list,
49 const gl_state_index16 tokens[STATE_LENGTH]);
51 static int initialize_symbol_from_state(struct gl_program *prog,
52 struct asm_symbol *param_var, const gl_state_index16 tokens[STATE_LENGTH]);
54 static int initialize_symbol_from_param(struct gl_program *prog,
55 struct asm_symbol *param_var, const gl_state_index16 tokens[STATE_LENGTH]);
57 static int initialize_symbol_from_const(struct gl_program *prog,
58 struct asm_symbol *param_var, const struct asm_vector *vec,
59 GLboolean allowSwizzle);
61 static int yyparse(struct asm_parser_state *state);
63 static char *make_error_string(const char *fmt, ...);
65 static void yyerror(struct YYLTYPE *locp, struct asm_parser_state *state,
68 static int validate_inputs(struct YYLTYPE *locp,
69 struct asm_parser_state *state);
71 static void init_dst_reg(struct prog_dst_register *r);
73 static void set_dst_reg(struct prog_dst_register *r,
74 gl_register_file file, GLint index);
76 static void init_src_reg(struct asm_src_register *r);
78 static void set_src_reg(struct asm_src_register *r,
79 gl_register_file file, GLint index);
81 static void set_src_reg_swz(struct asm_src_register *r,
82 gl_register_file file, GLint index, GLuint swizzle);
84 static void asm_instruction_set_operands(struct asm_instruction *inst,
85 const struct prog_dst_register *dst, const struct asm_src_register *src0,
86 const struct asm_src_register *src1, const struct asm_src_register *src2);
88 static struct asm_instruction *asm_instruction_ctor(enum prog_opcode op,
89 const struct prog_dst_register *dst, const struct asm_src_register *src0,
90 const struct asm_src_register *src1, const struct asm_src_register *src2);
92 static struct asm_instruction *asm_instruction_copy_ctor(
93 const struct prog_instruction *base, const struct prog_dst_register *dst,
94 const struct asm_src_register *src0, const struct asm_src_register *src1,
95 const struct asm_src_register *src2);
102 #define YYLLOC_DEFAULT(Current, Rhs, N) \
105 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
106 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
107 (Current).position = YYRHSLOC(Rhs, 1).position; \
108 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
109 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
111 (Current).first_line = YYRHSLOC(Rhs, 0).last_line; \
112 (Current).last_line = (Current).first_line; \
113 (Current).first_column = YYRHSLOC(Rhs, 0).last_column; \
114 (Current).last_column = (Current).first_column; \
115 (Current).position = YYRHSLOC(Rhs, 0).position \
116 + (Current).first_column; \
123 %lex-param { struct asm_parser_state *state }
124 %parse-param { struct asm_parser_state *state }
128 struct asm_instruction *inst;
129 struct asm_symbol *sym;
130 struct asm_symbol temp_sym;
131 struct asm_swizzle_mask swiz_mask;
132 struct asm_src_register src_reg;
133 struct prog_dst_register dst_reg;
134 struct prog_instruction temp_inst;
140 gl_state_index16 state[STATE_LENGTH];
142 struct asm_vector vector;
143 enum prog_opcode opcode;
147 unsigned rgba_valid:1;
148 unsigned xyzw_valid:1;
153 %token ARBvp_10 ARBfp_10
155 /* Tokens for assembler pseudo-ops */
156 %token <integer> ADDRESS
160 %token <integer> TEMP
163 /* Tokens for instructions */
164 %token <temp_inst> BIN_OP BINSC_OP SAMPLE_OP SCALAR_OP TRI_OP VECTOR_OP
165 %token <temp_inst> ARL KIL SWZ TXD_OP
167 %token <integer> INTEGER
170 %token AMBIENT ATTENUATION
173 %token DEPTH DIFFUSE DIRECTION
174 %token EMISSION ENV EYE
175 %token FOG FOGCOORD FRAGMENT FRONT
177 %token INVERSE INVTRANS
178 %token LIGHT LIGHTMODEL LIGHTPROD LOCAL
179 %token MATERIAL MAT_PROGRAM MATRIX MATRIXINDEX MODELVIEW MVP
182 %token PALETTE PARAMS PLANE POINT_TOK POINTSIZE POSITION PRIMARY PROGRAM PROJECTION
183 %token RANGE RESULT ROW
184 %token SCENECOLOR SECONDARY SHININESS SIZE_TOK SPECULAR SPOT STATE
185 %token TEXCOORD TEXENV TEXGEN TEXGEN_Q TEXGEN_R TEXGEN_S TEXGEN_T TEXTURE TRANSPOSE
186 %token TEXTURE_UNIT TEX_1D TEX_2D TEX_3D TEX_CUBE TEX_RECT
187 %token TEX_SHADOW1D TEX_SHADOW2D TEX_SHADOWRECT
188 %token TEX_ARRAY1D TEX_ARRAY2D TEX_ARRAYSHADOW1D TEX_ARRAYSHADOW2D
189 %token VERTEX VTXATTRIB
191 %token <string> IDENTIFIER USED_IDENTIFIER
192 %type <string> string
193 %token <swiz_mask> MASK4 MASK3 MASK2 MASK1 SWIZZLE
197 %type <inst> instruction ALU_instruction TexInstruction
198 %type <inst> ARL_instruction VECTORop_instruction
199 %type <inst> SCALARop_instruction BINSCop_instruction BINop_instruction
200 %type <inst> TRIop_instruction TXD_instruction SWZ_instruction SAMPLE_instruction
201 %type <inst> KIL_instruction
203 %type <dst_reg> dstReg maskedDstReg maskedAddrReg
204 %type <src_reg> srcReg scalarUse scalarSrcReg swizzleSrcReg
205 %type <swiz_mask> scalarSuffix swizzleSuffix extendedSwizzle
206 %type <ext_swizzle> extSwizComp extSwizSel
207 %type <swiz_mask> optionalMask
209 %type <sym> progParamArray
210 %type <integer> addrRegRelOffset addrRegPosOffset addrRegNegOffset
211 %type <src_reg> progParamArrayMem progParamArrayAbs progParamArrayRel
213 %type <swiz_mask> addrComponent addrWriteMask
215 %type <result> resultBinding resultColBinding
216 %type <integer> optFaceType optColorType
217 %type <integer> optResultFaceType optResultColorType
219 %type <integer> optTexImageUnitNum texImageUnitNum
220 %type <integer> optTexCoordUnitNum texCoordUnitNum
221 %type <integer> optLegacyTexUnitNum legacyTexUnitNum
222 %type <integer> texImageUnit texTarget
223 %type <integer> vtxAttribNum
225 %type <attrib> attribBinding vtxAttribItem fragAttribItem
227 %type <temp_sym> paramSingleInit paramSingleItemDecl
228 %type <integer> optArraySize
230 %type <state> stateSingleItem stateMultipleItem
231 %type <state> stateMaterialItem
232 %type <state> stateLightItem stateLightModelItem stateLightProdItem
233 %type <state> stateTexGenItem stateFogItem stateClipPlaneItem statePointItem
234 %type <state> stateMatrixItem stateMatrixRow stateMatrixRows
235 %type <state> stateTexEnvItem stateDepthItem
237 %type <state> stateLModProperty
238 %type <state> stateMatrixName optMatrixRows
240 %type <integer> stateMatProperty
241 %type <integer> stateLightProperty stateSpotProperty
242 %type <integer> stateLightNumber stateLProdProperty
243 %type <integer> stateTexGenType stateTexGenCoord
244 %type <integer> stateTexEnvProperty
245 %type <integer> stateFogProperty
246 %type <integer> stateClipPlaneNum
247 %type <integer> statePointProperty
249 %type <integer> stateOptMatModifier stateMatModifier stateMatrixRowNum
250 %type <integer> stateOptModMatNum stateModMatNum statePaletteMatNum
251 %type <integer> stateProgramMatNum
253 %type <integer> ambDiffSpecProperty
255 %type <state> programSingleItem progEnvParam progLocalParam
256 %type <state> programMultipleItem progEnvParams progLocalParams
258 %type <temp_sym> paramMultipleInit paramMultInitList paramMultipleItem
259 %type <temp_sym> paramSingleItemUse
261 %type <integer> progEnvParamNum progLocalParamNum
262 %type <state> progEnvParamNums progLocalParamNums
264 %type <vector> paramConstDecl paramConstUse
265 %type <vector> paramConstScalarDecl paramConstScalarUse paramConstVector
266 %type <real> signedFloatConstant
267 %type <negate> optionalSign
271 _mesa_program_lexer_lex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param,
275 yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param,
276 struct asm_parser_state *state)
278 return _mesa_program_lexer_lex(yylval_param, yylloc_param, state->scanner);
284 program: language optionSequence statementSequence END
289 if (state->prog->Target != GL_VERTEX_PROGRAM_ARB) {
290 yyerror(& @1, state, "invalid fragment program header");
293 state->mode = ARB_vertex;
297 if (state->prog->Target != GL_FRAGMENT_PROGRAM_ARB) {
298 yyerror(& @1, state, "invalid vertex program header");
300 state->mode = ARB_fragment;
302 state->option.TexRect =
303 (state->ctx->Extensions.NV_texture_rectangle != GL_FALSE);
307 optionSequence: optionSequence option
311 option: OPTION string ';'
315 if (state->mode == ARB_vertex) {
316 valid = _mesa_ARBvp_parse_option(state, $2);
317 } else if (state->mode == ARB_fragment) {
318 valid = _mesa_ARBfp_parse_option(state, $2);
325 const char *const err_str = (state->mode == ARB_vertex)
326 ? "invalid ARB vertex program option"
327 : "invalid ARB fragment program option";
329 yyerror(& @2, state, err_str);
335 statementSequence: statementSequence statement
339 statement: instruction ';'
342 if (state->inst_tail == NULL) {
343 state->inst_head = $1;
345 state->inst_tail->next = $1;
348 state->inst_tail = $1;
351 state->prog->arb.NumInstructions++;
354 | namingStatement ';'
357 instruction: ALU_instruction
360 state->prog->arb.NumAluInstructions++;
365 state->prog->arb.NumTexInstructions++;
369 ALU_instruction: ARL_instruction
370 | VECTORop_instruction
371 | SCALARop_instruction
372 | BINSCop_instruction
378 TexInstruction: SAMPLE_instruction
383 ARL_instruction: ARL maskedAddrReg ',' scalarSrcReg
385 $$ = asm_instruction_ctor(OPCODE_ARL, & $2, & $4, NULL, NULL);
389 VECTORop_instruction: VECTOR_OP maskedDstReg ',' swizzleSrcReg
391 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
395 SCALARop_instruction: SCALAR_OP maskedDstReg ',' scalarSrcReg
397 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
401 BINSCop_instruction: BINSC_OP maskedDstReg ',' scalarSrcReg ',' scalarSrcReg
403 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
408 BINop_instruction: BIN_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg
410 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
414 TRIop_instruction: TRI_OP maskedDstReg ','
415 swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg
417 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
421 SAMPLE_instruction: SAMPLE_OP maskedDstReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
423 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
425 const GLbitfield tex_mask = (1U << $6);
426 GLbitfield shadow_tex = 0;
427 GLbitfield target_mask = 0;
430 $$->Base.TexSrcUnit = $6;
433 shadow_tex = tex_mask;
435 $$->Base.TexSrcTarget = -$8;
436 $$->Base.TexShadow = 1;
438 $$->Base.TexSrcTarget = $8;
441 target_mask = (1U << $$->Base.TexSrcTarget);
443 /* If this texture unit was previously accessed and that access
444 * had a different texture target, generate an error.
446 * If this texture unit was previously accessed and that access
447 * had a different shadow mode, generate an error.
449 if ((state->prog->TexturesUsed[$6] != 0)
450 && ((state->prog->TexturesUsed[$6] != target_mask)
451 || ((state->prog->ShadowSamplers & tex_mask)
454 "multiple targets used on one texture image unit");
459 state->prog->TexturesUsed[$6] |= target_mask;
460 state->prog->ShadowSamplers |= shadow_tex;
465 KIL_instruction: KIL swizzleSrcReg
467 $$ = asm_instruction_ctor(OPCODE_KIL, NULL, & $2, NULL, NULL);
468 state->fragment.UsesKill = 1;
472 TXD_instruction: TXD_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
474 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
476 const GLbitfield tex_mask = (1U << $10);
477 GLbitfield shadow_tex = 0;
478 GLbitfield target_mask = 0;
481 $$->Base.TexSrcUnit = $10;
484 shadow_tex = tex_mask;
486 $$->Base.TexSrcTarget = -$12;
487 $$->Base.TexShadow = 1;
489 $$->Base.TexSrcTarget = $12;
492 target_mask = (1U << $$->Base.TexSrcTarget);
494 /* If this texture unit was previously accessed and that access
495 * had a different texture target, generate an error.
497 * If this texture unit was previously accessed and that access
498 * had a different shadow mode, generate an error.
500 if ((state->prog->TexturesUsed[$10] != 0)
501 && ((state->prog->TexturesUsed[$10] != target_mask)
502 || ((state->prog->ShadowSamplers & tex_mask)
504 yyerror(& @12, state,
505 "multiple targets used on one texture image unit");
510 state->prog->TexturesUsed[$10] |= target_mask;
511 state->prog->ShadowSamplers |= shadow_tex;
516 texImageUnit: TEXTURE_UNIT optTexImageUnitNum
522 texTarget: TEX_1D { $$ = TEXTURE_1D_INDEX; }
523 | TEX_2D { $$ = TEXTURE_2D_INDEX; }
524 | TEX_3D { $$ = TEXTURE_3D_INDEX; }
525 | TEX_CUBE { $$ = TEXTURE_CUBE_INDEX; }
526 | TEX_RECT { $$ = TEXTURE_RECT_INDEX; }
527 | TEX_SHADOW1D { $$ = -TEXTURE_1D_INDEX; }
528 | TEX_SHADOW2D { $$ = -TEXTURE_2D_INDEX; }
529 | TEX_SHADOWRECT { $$ = -TEXTURE_RECT_INDEX; }
530 | TEX_ARRAY1D { $$ = TEXTURE_1D_ARRAY_INDEX; }
531 | TEX_ARRAY2D { $$ = TEXTURE_2D_ARRAY_INDEX; }
532 | TEX_ARRAYSHADOW1D { $$ = -TEXTURE_1D_ARRAY_INDEX; }
533 | TEX_ARRAYSHADOW2D { $$ = -TEXTURE_2D_ARRAY_INDEX; }
536 SWZ_instruction: SWZ maskedDstReg ',' srcReg ',' extendedSwizzle
538 /* FIXME: Is this correct? Should the extenedSwizzle be applied
539 * FIXME: to the existing swizzle?
541 $4.Base.Swizzle = $6.swizzle;
542 $4.Base.Negate = $6.mask;
544 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
548 scalarSrcReg: optionalSign scalarUse
553 $$.Base.Negate = ~$$.Base.Negate;
558 scalarUse: srcReg scalarSuffix
562 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
567 swizzleSrcReg: optionalSign srcReg swizzleSuffix
572 $$.Base.Negate = ~$$.Base.Negate;
575 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
580 maskedDstReg: dstReg optionalMask
583 $$.WriteMask = $2.mask;
585 if ($$.File == PROGRAM_OUTPUT) {
586 /* Technically speaking, this should check that it is in
587 * vertex program mode. However, PositionInvariant can never be
588 * set in fragment program mode, so it is somewhat irrelevant.
590 if (state->option.PositionInvariant
591 && ($$.Index == VARYING_SLOT_POS)) {
592 yyerror(& @1, state, "position-invariant programs cannot "
597 state->prog->info.outputs_written |= BITFIELD64_BIT($$.Index);
602 maskedAddrReg: addrReg addrWriteMask
604 set_dst_reg(& $$, PROGRAM_ADDRESS, 0);
605 $$.WriteMask = $2.mask;
609 extendedSwizzle: extSwizComp ',' extSwizComp ',' extSwizComp ',' extSwizComp
611 const unsigned xyzw_valid =
613 | ($3.xyzw_valid << 1)
614 | ($5.xyzw_valid << 2)
615 | ($7.xyzw_valid << 3);
616 const unsigned rgba_valid =
618 | ($3.rgba_valid << 1)
619 | ($5.rgba_valid << 2)
620 | ($7.rgba_valid << 3);
622 /* All of the swizzle components have to be valid in either RGBA
623 * or XYZW. Note that 0 and 1 are valid in both, so both masks
624 * can have some bits set.
626 * We somewhat deviate from the spec here. It would be really hard
627 * to figure out which component is the error, and there probably
628 * isn't a lot of benefit.
630 if ((rgba_valid != 0x0f) && (xyzw_valid != 0x0f)) {
631 yyerror(& @1, state, "cannot combine RGBA and XYZW swizzle "
636 $$.swizzle = MAKE_SWIZZLE4($1.swz, $3.swz, $5.swz, $7.swz);
637 $$.mask = ($1.negate) | ($3.negate << 1) | ($5.negate << 2)
642 extSwizComp: optionalSign extSwizSel
645 $$.negate = ($1) ? 1 : 0;
651 if (($1 != 0) && ($1 != 1)) {
652 yyerror(& @1, state, "invalid extended swizzle selector");
656 $$.swz = ($1 == 0) ? SWIZZLE_ZERO : SWIZZLE_ONE;
659 /* 0 and 1 are valid for both RGBA swizzle names and XYZW
669 if (strlen($1) > 1) {
670 yyerror(& @1, state, "invalid extended swizzle selector");
717 yyerror(& @1, state, "invalid extended swizzle selector");
724 srcReg: USED_IDENTIFIER /* temporaryReg | progParamSingle */
726 struct asm_symbol *const s = (struct asm_symbol *)
727 _mesa_symbol_table_find_symbol(state->st, $1);
732 yyerror(& @1, state, "invalid operand variable");
734 } else if ((s->type != at_param) && (s->type != at_temp)
735 && (s->type != at_attrib)) {
736 yyerror(& @1, state, "invalid operand variable");
738 } else if ((s->type == at_param) && s->param_is_array) {
739 yyerror(& @1, state, "non-array access to array PARAM");
746 set_src_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding);
749 set_src_reg_swz(& $$, s->param_binding_type,
750 s->param_binding_begin,
751 s->param_binding_swizzle);
754 set_src_reg(& $$, PROGRAM_INPUT, s->attrib_binding);
755 state->prog->info.inputs_read |= BITFIELD64_BIT($$.Base.Index);
757 if (!validate_inputs(& @1, state)) {
769 set_src_reg(& $$, PROGRAM_INPUT, $1);
770 state->prog->info.inputs_read |= BITFIELD64_BIT($$.Base.Index);
772 if (!validate_inputs(& @1, state)) {
776 | progParamArray '[' progParamArrayMem ']'
778 if (! $3.Base.RelAddr
779 && ((unsigned) $3.Base.Index >= $1->param_binding_length)) {
780 yyerror(& @3, state, "out of bounds array access");
785 $$.Base.File = $1->param_binding_type;
787 if ($3.Base.RelAddr) {
788 state->prog->arb.IndirectRegisterFiles |= (1 << $$.Base.File);
789 $1->param_accessed_indirectly = 1;
792 $$.Base.Index = $3.Base.Index;
795 $$.Base.Index = $1->param_binding_begin + $3.Base.Index;
800 gl_register_file file = ($1.name != NULL)
801 ? $1.param_binding_type
803 set_src_reg_swz(& $$, file, $1.param_binding_begin,
804 $1.param_binding_swizzle);
808 dstReg: resultBinding
810 set_dst_reg(& $$, PROGRAM_OUTPUT, $1);
812 | USED_IDENTIFIER /* temporaryReg | vertexResultReg */
814 struct asm_symbol *const s = (struct asm_symbol *)
815 _mesa_symbol_table_find_symbol(state->st, $1);
820 yyerror(& @1, state, "invalid operand variable");
822 } else if ((s->type != at_output) && (s->type != at_temp)) {
823 yyerror(& @1, state, "invalid operand variable");
829 set_dst_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding);
832 set_dst_reg(& $$, PROGRAM_OUTPUT, s->output_binding);
835 set_dst_reg(& $$, s->param_binding_type, s->param_binding_begin);
841 progParamArray: USED_IDENTIFIER
843 struct asm_symbol *const s = (struct asm_symbol *)
844 _mesa_symbol_table_find_symbol(state->st, $1);
849 yyerror(& @1, state, "invalid operand variable");
851 } else if ((s->type != at_param) || !s->param_is_array) {
852 yyerror(& @1, state, "array access to non-PARAM variable");
860 progParamArrayMem: progParamArrayAbs | progParamArrayRel;
862 progParamArrayAbs: INTEGER
869 progParamArrayRel: addrReg addrComponent addrRegRelOffset
871 /* FINISHME: Add support for multiple address registers.
873 /* FINISHME: Add support for 4-component address registers.
881 addrRegRelOffset: { $$ = 0; }
882 | '+' addrRegPosOffset { $$ = $2; }
883 | '-' addrRegNegOffset { $$ = -$2; }
886 addrRegPosOffset: INTEGER
888 if (($1 < 0) || ($1 > (state->limits->MaxAddressOffset - 1))) {
890 _mesa_snprintf(s, sizeof(s),
891 "relative address offset too large (%d)", $1);
892 yyerror(& @1, state, s);
900 addrRegNegOffset: INTEGER
902 if (($1 < 0) || ($1 > state->limits->MaxAddressOffset)) {
904 _mesa_snprintf(s, sizeof(s),
905 "relative address offset too large (%d)", $1);
906 yyerror(& @1, state, s);
914 addrReg: USED_IDENTIFIER
916 struct asm_symbol *const s = (struct asm_symbol *)
917 _mesa_symbol_table_find_symbol(state->st, $1);
922 yyerror(& @1, state, "invalid array member");
924 } else if (s->type != at_address) {
926 "invalid variable for indexed array access");
936 if ($1.mask != WRITEMASK_X) {
937 yyerror(& @1, state, "invalid address component selector");
947 if ($1.mask != WRITEMASK_X) {
949 "address register write mask must be \".x\"");
962 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
965 optionalMask: MASK4 | MASK3 | MASK2 | MASK1
966 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
969 namingStatement: ATTRIB_statement
977 ATTRIB_statement: ATTRIB IDENTIFIER '=' attribBinding
979 struct asm_symbol *const s =
980 declare_variable(state, $2, at_attrib, & @2);
986 s->attrib_binding = $4;
987 state->InputsBound |= BITFIELD64_BIT(s->attrib_binding);
989 if (!validate_inputs(& @4, state)) {
996 attribBinding: VERTEX vtxAttribItem
1000 | FRAGMENT fragAttribItem
1006 vtxAttribItem: POSITION
1008 $$ = VERT_ATTRIB_POS;
1012 $$ = VERT_ATTRIB_NORMAL;
1014 | COLOR optColorType
1016 $$ = VERT_ATTRIB_COLOR0 + $2;
1020 $$ = VERT_ATTRIB_FOG;
1022 | TEXCOORD optTexCoordUnitNum
1024 $$ = VERT_ATTRIB_TEX0 + $2;
1026 | MATRIXINDEX '[' vtxWeightNum ']'
1028 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1031 | VTXATTRIB '[' vtxAttribNum ']'
1033 $$ = VERT_ATTRIB_GENERIC0 + $3;
1037 vtxAttribNum: INTEGER
1039 if ((unsigned) $1 >= state->limits->MaxAttribs) {
1040 yyerror(& @1, state, "invalid vertex attribute reference");
1048 vtxWeightNum: INTEGER;
1050 fragAttribItem: POSITION
1052 $$ = VARYING_SLOT_POS;
1054 | COLOR optColorType
1056 $$ = VARYING_SLOT_COL0 + $2;
1060 $$ = VARYING_SLOT_FOGC;
1062 | TEXCOORD optTexCoordUnitNum
1064 $$ = VARYING_SLOT_TEX0 + $2;
1068 PARAM_statement: PARAM_singleStmt | PARAM_multipleStmt;
1070 PARAM_singleStmt: PARAM IDENTIFIER paramSingleInit
1072 struct asm_symbol *const s =
1073 declare_variable(state, $2, at_param, & @2);
1079 s->param_binding_type = $3.param_binding_type;
1080 s->param_binding_begin = $3.param_binding_begin;
1081 s->param_binding_length = $3.param_binding_length;
1082 s->param_binding_swizzle = $3.param_binding_swizzle;
1083 s->param_is_array = 0;
1088 PARAM_multipleStmt: PARAM IDENTIFIER '[' optArraySize ']' paramMultipleInit
1090 if (($4 != 0) && ((unsigned) $4 != $6.param_binding_length)) {
1092 yyerror(& @4, state,
1093 "parameter array size and number of bindings must match");
1096 struct asm_symbol *const s =
1097 declare_variable(state, $2, $6.type, & @2);
1103 s->param_binding_type = $6.param_binding_type;
1104 s->param_binding_begin = $6.param_binding_begin;
1105 s->param_binding_length = $6.param_binding_length;
1106 s->param_binding_swizzle = SWIZZLE_XYZW;
1107 s->param_is_array = 1;
1119 if (($1 < 1) || ((unsigned) $1 > state->limits->MaxParameters)) {
1121 _mesa_snprintf(msg, sizeof(msg),
1122 "invalid parameter array size (size=%d max=%u)",
1123 $1, state->limits->MaxParameters);
1124 yyerror(& @1, state, msg);
1132 paramSingleInit: '=' paramSingleItemDecl
1138 paramMultipleInit: '=' '{' paramMultInitList '}'
1144 paramMultInitList: paramMultipleItem
1145 | paramMultInitList ',' paramMultipleItem
1147 $1.param_binding_length += $3.param_binding_length;
1152 paramSingleItemDecl: stateSingleItem
1154 memset(& $$, 0, sizeof($$));
1155 $$.param_binding_begin = ~0;
1156 initialize_symbol_from_state(state->prog, & $$, $1);
1160 memset(& $$, 0, sizeof($$));
1161 $$.param_binding_begin = ~0;
1162 initialize_symbol_from_param(state->prog, & $$, $1);
1166 memset(& $$, 0, sizeof($$));
1167 $$.param_binding_begin = ~0;
1168 initialize_symbol_from_const(state->prog, & $$, & $1, GL_TRUE);
1172 paramSingleItemUse: stateSingleItem
1174 memset(& $$, 0, sizeof($$));
1175 $$.param_binding_begin = ~0;
1176 initialize_symbol_from_state(state->prog, & $$, $1);
1180 memset(& $$, 0, sizeof($$));
1181 $$.param_binding_begin = ~0;
1182 initialize_symbol_from_param(state->prog, & $$, $1);
1186 memset(& $$, 0, sizeof($$));
1187 $$.param_binding_begin = ~0;
1188 initialize_symbol_from_const(state->prog, & $$, & $1, GL_TRUE);
1192 paramMultipleItem: stateMultipleItem
1194 memset(& $$, 0, sizeof($$));
1195 $$.param_binding_begin = ~0;
1196 initialize_symbol_from_state(state->prog, & $$, $1);
1198 | programMultipleItem
1200 memset(& $$, 0, sizeof($$));
1201 $$.param_binding_begin = ~0;
1202 initialize_symbol_from_param(state->prog, & $$, $1);
1206 memset(& $$, 0, sizeof($$));
1207 $$.param_binding_begin = ~0;
1208 initialize_symbol_from_const(state->prog, & $$, & $1, GL_FALSE);
1212 stateMultipleItem: stateSingleItem { memcpy($$, $1, sizeof($$)); }
1213 | STATE stateMatrixRows { memcpy($$, $2, sizeof($$)); }
1216 stateSingleItem: STATE stateMaterialItem { memcpy($$, $2, sizeof($$)); }
1217 | STATE stateLightItem { memcpy($$, $2, sizeof($$)); }
1218 | STATE stateLightModelItem { memcpy($$, $2, sizeof($$)); }
1219 | STATE stateLightProdItem { memcpy($$, $2, sizeof($$)); }
1220 | STATE stateTexGenItem { memcpy($$, $2, sizeof($$)); }
1221 | STATE stateTexEnvItem { memcpy($$, $2, sizeof($$)); }
1222 | STATE stateFogItem { memcpy($$, $2, sizeof($$)); }
1223 | STATE stateClipPlaneItem { memcpy($$, $2, sizeof($$)); }
1224 | STATE statePointItem { memcpy($$, $2, sizeof($$)); }
1225 | STATE stateMatrixRow { memcpy($$, $2, sizeof($$)); }
1226 | STATE stateDepthItem { memcpy($$, $2, sizeof($$)); }
1229 stateMaterialItem: MATERIAL optFaceType stateMatProperty
1231 memset($$, 0, sizeof($$));
1232 $$[0] = STATE_MATERIAL;
1238 stateMatProperty: ambDiffSpecProperty
1244 $$ = STATE_EMISSION;
1248 $$ = STATE_SHININESS;
1252 stateLightItem: LIGHT '[' stateLightNumber ']' stateLightProperty
1254 memset($$, 0, sizeof($$));
1255 $$[0] = STATE_LIGHT;
1261 stateLightProperty: ambDiffSpecProperty
1267 $$ = STATE_POSITION;
1271 if (!state->ctx->Extensions.EXT_point_parameters) {
1272 yyerror(& @1, state, "GL_ARB_point_parameters not supported");
1276 $$ = STATE_ATTENUATION;
1278 | SPOT stateSpotProperty
1284 $$ = STATE_HALF_VECTOR;
1288 stateSpotProperty: DIRECTION
1290 $$ = STATE_SPOT_DIRECTION;
1294 stateLightModelItem: LIGHTMODEL stateLModProperty
1301 stateLModProperty: AMBIENT
1303 memset($$, 0, sizeof($$));
1304 $$[0] = STATE_LIGHTMODEL_AMBIENT;
1306 | optFaceType SCENECOLOR
1308 memset($$, 0, sizeof($$));
1309 $$[0] = STATE_LIGHTMODEL_SCENECOLOR;
1314 stateLightProdItem: LIGHTPROD '[' stateLightNumber ']' optFaceType stateLProdProperty
1316 memset($$, 0, sizeof($$));
1317 $$[0] = STATE_LIGHTPROD;
1324 stateLProdProperty: ambDiffSpecProperty;
1326 stateTexEnvItem: TEXENV optLegacyTexUnitNum stateTexEnvProperty
1328 memset($$, 0, sizeof($$));
1334 stateTexEnvProperty: COLOR
1336 $$ = STATE_TEXENV_COLOR;
1340 ambDiffSpecProperty: AMBIENT
1350 $$ = STATE_SPECULAR;
1354 stateLightNumber: INTEGER
1356 if ((unsigned) $1 >= state->MaxLights) {
1357 yyerror(& @1, state, "invalid light selector");
1365 stateTexGenItem: TEXGEN optTexCoordUnitNum stateTexGenType stateTexGenCoord
1367 memset($$, 0, sizeof($$));
1368 $$[0] = STATE_TEXGEN;
1374 stateTexGenType: EYE
1376 $$ = STATE_TEXGEN_EYE_S;
1380 $$ = STATE_TEXGEN_OBJECT_S;
1383 stateTexGenCoord: TEXGEN_S
1385 $$ = STATE_TEXGEN_EYE_S - STATE_TEXGEN_EYE_S;
1389 $$ = STATE_TEXGEN_EYE_T - STATE_TEXGEN_EYE_S;
1393 $$ = STATE_TEXGEN_EYE_R - STATE_TEXGEN_EYE_S;
1397 $$ = STATE_TEXGEN_EYE_Q - STATE_TEXGEN_EYE_S;
1401 stateFogItem: FOG stateFogProperty
1403 memset($$, 0, sizeof($$));
1408 stateFogProperty: COLOR
1410 $$ = STATE_FOG_COLOR;
1414 $$ = STATE_FOG_PARAMS;
1418 stateClipPlaneItem: CLIP '[' stateClipPlaneNum ']' PLANE
1420 memset($$, 0, sizeof($$));
1421 $$[0] = STATE_CLIPPLANE;
1426 stateClipPlaneNum: INTEGER
1428 if ((unsigned) $1 >= state->MaxClipPlanes) {
1429 yyerror(& @1, state, "invalid clip plane selector");
1437 statePointItem: POINT_TOK statePointProperty
1439 memset($$, 0, sizeof($$));
1444 statePointProperty: SIZE_TOK
1446 $$ = STATE_POINT_SIZE;
1450 $$ = STATE_POINT_ATTENUATION;
1454 stateMatrixRow: stateMatrixItem ROW '[' stateMatrixRowNum ']'
1464 stateMatrixRows: stateMatrixItem optMatrixRows
1479 | ROW '[' stateMatrixRowNum DOT_DOT stateMatrixRowNum ']'
1481 /* It seems logical that the matrix row range specifier would have
1482 * to specify a range or more than one row (i.e., $5 > $3).
1483 * However, the ARB_vertex_program spec says "a program will fail
1484 * to load if <a> is greater than <b>." This means that $3 == $5
1488 yyerror(& @3, state, "invalid matrix row range");
1497 stateMatrixItem: MATRIX stateMatrixName stateOptMatModifier
1505 stateOptMatModifier:
1515 stateMatModifier: INVERSE
1517 $$ = STATE_MATRIX_INVERSE;
1521 $$ = STATE_MATRIX_TRANSPOSE;
1525 $$ = STATE_MATRIX_INVTRANS;
1529 stateMatrixRowNum: INTEGER
1532 yyerror(& @1, state, "invalid matrix row reference");
1540 stateMatrixName: MODELVIEW stateOptModMatNum
1542 $$[0] = STATE_MODELVIEW_MATRIX;
1547 $$[0] = STATE_PROJECTION_MATRIX;
1552 $$[0] = STATE_MVP_MATRIX;
1555 | TEXTURE optTexCoordUnitNum
1557 $$[0] = STATE_TEXTURE_MATRIX;
1560 | PALETTE '[' statePaletteMatNum ']'
1562 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1565 | MAT_PROGRAM '[' stateProgramMatNum ']'
1567 $$[0] = STATE_PROGRAM_MATRIX;
1576 | '[' stateModMatNum ']'
1581 stateModMatNum: INTEGER
1583 /* Since GL_ARB_vertex_blend isn't supported, only modelview matrix
1587 yyerror(& @1, state, "invalid modelview matrix index");
1594 statePaletteMatNum: INTEGER
1596 /* Since GL_ARB_matrix_palette isn't supported, just let any value
1597 * through here. The error will be generated later.
1602 stateProgramMatNum: INTEGER
1604 if ((unsigned) $1 >= state->MaxProgramMatrices) {
1605 yyerror(& @1, state, "invalid program matrix selector");
1613 stateDepthItem: DEPTH RANGE
1615 memset($$, 0, sizeof($$));
1616 $$[0] = STATE_DEPTH_RANGE;
1621 programSingleItem: progEnvParam | progLocalParam;
1623 programMultipleItem: progEnvParams | progLocalParams;
1625 progEnvParams: PROGRAM ENV '[' progEnvParamNums ']'
1627 memset($$, 0, sizeof($$));
1628 $$[0] = state->state_param_enum;
1635 progEnvParamNums: progEnvParamNum
1640 | progEnvParamNum DOT_DOT progEnvParamNum
1647 progEnvParam: PROGRAM ENV '[' progEnvParamNum ']'
1649 memset($$, 0, sizeof($$));
1650 $$[0] = state->state_param_enum;
1657 progLocalParams: PROGRAM LOCAL '[' progLocalParamNums ']'
1659 memset($$, 0, sizeof($$));
1660 $$[0] = state->state_param_enum;
1661 $$[1] = STATE_LOCAL;
1666 progLocalParamNums: progLocalParamNum
1671 | progLocalParamNum DOT_DOT progLocalParamNum
1678 progLocalParam: PROGRAM LOCAL '[' progLocalParamNum ']'
1680 memset($$, 0, sizeof($$));
1681 $$[0] = state->state_param_enum;
1682 $$[1] = STATE_LOCAL;
1688 progEnvParamNum: INTEGER
1690 if ((unsigned) $1 >= state->limits->MaxEnvParams) {
1691 yyerror(& @1, state, "invalid environment parameter reference");
1698 progLocalParamNum: INTEGER
1700 if ((unsigned) $1 >= state->limits->MaxLocalParams) {
1701 yyerror(& @1, state, "invalid local parameter reference");
1710 paramConstDecl: paramConstScalarDecl | paramConstVector;
1711 paramConstUse: paramConstScalarUse | paramConstVector;
1713 paramConstScalarDecl: signedFloatConstant
1723 paramConstScalarUse: REAL
1734 $$.data[0].f = (float) $1;
1735 $$.data[1].f = (float) $1;
1736 $$.data[2].f = (float) $1;
1737 $$.data[3].f = (float) $1;
1741 paramConstVector: '{' signedFloatConstant '}'
1745 $$.data[1].f = 0.0f;
1746 $$.data[2].f = 0.0f;
1747 $$.data[3].f = 1.0f;
1749 | '{' signedFloatConstant ',' signedFloatConstant '}'
1754 $$.data[2].f = 0.0f;
1755 $$.data[3].f = 1.0f;
1757 | '{' signedFloatConstant ',' signedFloatConstant ','
1758 signedFloatConstant '}'
1764 $$.data[3].f = 1.0f;
1766 | '{' signedFloatConstant ',' signedFloatConstant ','
1767 signedFloatConstant ',' signedFloatConstant '}'
1777 signedFloatConstant: optionalSign REAL
1779 $$ = ($1) ? -$2 : $2;
1781 | optionalSign INTEGER
1783 $$ = (float)(($1) ? -$2 : $2);
1787 optionalSign: '+' { $$ = FALSE; }
1788 | '-' { $$ = TRUE; }
1792 TEMP_statement: TEMP { $<integer>$ = $1; } varNameList
1795 ADDRESS_statement: ADDRESS { $<integer>$ = $1; } varNameList
1798 varNameList: varNameList ',' IDENTIFIER
1800 if (!declare_variable(state, $3, $<integer>0, & @3)) {
1807 if (!declare_variable(state, $1, $<integer>0, & @1)) {
1814 OUTPUT_statement: OUTPUT IDENTIFIER '=' resultBinding
1816 struct asm_symbol *const s =
1817 declare_variable(state, $2, at_output, & @2);
1823 s->output_binding = $4;
1828 resultBinding: RESULT POSITION
1830 if (state->mode == ARB_vertex) {
1831 $$ = VARYING_SLOT_POS;
1833 yyerror(& @2, state, "invalid program result name");
1839 if (state->mode == ARB_vertex) {
1840 $$ = VARYING_SLOT_FOGC;
1842 yyerror(& @2, state, "invalid program result name");
1846 | RESULT resultColBinding
1852 if (state->mode == ARB_vertex) {
1853 $$ = VARYING_SLOT_PSIZ;
1855 yyerror(& @2, state, "invalid program result name");
1859 | RESULT TEXCOORD optTexCoordUnitNum
1861 if (state->mode == ARB_vertex) {
1862 $$ = VARYING_SLOT_TEX0 + $3;
1864 yyerror(& @2, state, "invalid program result name");
1870 if (state->mode == ARB_fragment) {
1871 $$ = FRAG_RESULT_DEPTH;
1873 yyerror(& @2, state, "invalid program result name");
1879 resultColBinding: COLOR optResultFaceType optResultColorType
1887 if (state->mode == ARB_vertex) {
1888 $$ = VARYING_SLOT_COL0;
1890 if (state->option.DrawBuffers)
1891 $$ = FRAG_RESULT_DATA0;
1893 $$ = FRAG_RESULT_COLOR;
1898 if (state->mode == ARB_vertex) {
1899 yyerror(& @1, state, "invalid program result name");
1902 if (!state->option.DrawBuffers) {
1903 /* From the ARB_draw_buffers spec (same text exists
1904 * for ATI_draw_buffers):
1906 * If this option is not specified, a fragment
1907 * program that attempts to bind
1908 * "result.color[n]" will fail to load, and only
1909 * "result.color" will be allowed.
1911 yyerror(& @1, state,
1912 "result.color[] used without "
1913 "`OPTION ARB_draw_buffers' or "
1914 "`OPTION ATI_draw_buffers'");
1916 } else if ($2 >= state->MaxDrawBuffers) {
1917 yyerror(& @1, state,
1918 "result.color[] exceeds MAX_DRAW_BUFFERS_ARB");
1921 $$ = FRAG_RESULT_DATA0 + $2;
1926 if (state->mode == ARB_vertex) {
1927 $$ = VARYING_SLOT_COL0;
1929 yyerror(& @1, state, "invalid program result name");
1935 if (state->mode == ARB_vertex) {
1936 $$ = VARYING_SLOT_BFC0;
1938 yyerror(& @1, state, "invalid program result name");
1950 if (state->mode == ARB_vertex) {
1953 yyerror(& @1, state, "invalid program result name");
1959 if (state->mode == ARB_vertex) {
1962 yyerror(& @1, state, "invalid program result name");
1968 optFaceType: { $$ = 0; }
1973 optColorType: { $$ = 0; }
1974 | PRIMARY { $$ = 0; }
1975 | SECONDARY { $$ = 1; }
1978 optTexCoordUnitNum: { $$ = 0; }
1979 | '[' texCoordUnitNum ']' { $$ = $2; }
1982 optTexImageUnitNum: { $$ = 0; }
1983 | '[' texImageUnitNum ']' { $$ = $2; }
1986 optLegacyTexUnitNum: { $$ = 0; }
1987 | '[' legacyTexUnitNum ']' { $$ = $2; }
1990 texCoordUnitNum: INTEGER
1992 if ((unsigned) $1 >= state->MaxTextureCoordUnits) {
1993 yyerror(& @1, state, "invalid texture coordinate unit selector");
2001 texImageUnitNum: INTEGER
2003 if ((unsigned) $1 >= state->MaxTextureImageUnits) {
2004 yyerror(& @1, state, "invalid texture image unit selector");
2012 legacyTexUnitNum: INTEGER
2014 if ((unsigned) $1 >= state->MaxTextureUnits) {
2015 yyerror(& @1, state, "invalid texture unit selector");
2023 ALIAS_statement: ALIAS IDENTIFIER '=' USED_IDENTIFIER
2025 struct asm_symbol *exist = (struct asm_symbol *)
2026 _mesa_symbol_table_find_symbol(state->st, $2);
2027 struct asm_symbol *target = (struct asm_symbol *)
2028 _mesa_symbol_table_find_symbol(state->st, $4);
2032 if (exist != NULL) {
2034 _mesa_snprintf(m, sizeof(m), "redeclared identifier: %s", $2);
2036 yyerror(& @2, state, m);
2038 } else if (target == NULL) {
2040 yyerror(& @4, state,
2041 "undefined variable binding in ALIAS statement");
2044 _mesa_symbol_table_add_symbol(state->st, $2, target);
2056 asm_instruction_set_operands(struct asm_instruction *inst,
2057 const struct prog_dst_register *dst,
2058 const struct asm_src_register *src0,
2059 const struct asm_src_register *src1,
2060 const struct asm_src_register *src2)
2062 /* In the core ARB extensions only the KIL instruction doesn't have a
2063 * destination register.
2066 init_dst_reg(& inst->Base.DstReg);
2068 inst->Base.DstReg = *dst;
2072 inst->Base.SrcReg[0] = src0->Base;
2073 inst->SrcReg[0] = *src0;
2075 init_src_reg(& inst->SrcReg[0]);
2079 inst->Base.SrcReg[1] = src1->Base;
2080 inst->SrcReg[1] = *src1;
2082 init_src_reg(& inst->SrcReg[1]);
2086 inst->Base.SrcReg[2] = src2->Base;
2087 inst->SrcReg[2] = *src2;
2089 init_src_reg(& inst->SrcReg[2]);
2094 struct asm_instruction *
2095 asm_instruction_ctor(enum prog_opcode op,
2096 const struct prog_dst_register *dst,
2097 const struct asm_src_register *src0,
2098 const struct asm_src_register *src1,
2099 const struct asm_src_register *src2)
2101 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2104 _mesa_init_instructions(& inst->Base, 1);
2105 inst->Base.Opcode = op;
2107 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2114 struct asm_instruction *
2115 asm_instruction_copy_ctor(const struct prog_instruction *base,
2116 const struct prog_dst_register *dst,
2117 const struct asm_src_register *src0,
2118 const struct asm_src_register *src1,
2119 const struct asm_src_register *src2)
2121 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2124 _mesa_init_instructions(& inst->Base, 1);
2125 inst->Base.Opcode = base->Opcode;
2126 inst->Base.Saturate = base->Saturate;
2128 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2136 init_dst_reg(struct prog_dst_register *r)
2138 memset(r, 0, sizeof(*r));
2139 r->File = PROGRAM_UNDEFINED;
2140 r->WriteMask = WRITEMASK_XYZW;
2144 /** Like init_dst_reg() but set the File and Index fields. */
2146 set_dst_reg(struct prog_dst_register *r, gl_register_file file, GLint index)
2148 const GLint maxIndex = 1 << INST_INDEX_BITS;
2149 const GLint minIndex = 0;
2150 assert(index >= minIndex);
2152 assert(index <= maxIndex);
2154 assert(file == PROGRAM_TEMPORARY ||
2155 file == PROGRAM_ADDRESS ||
2156 file == PROGRAM_OUTPUT);
2157 memset(r, 0, sizeof(*r));
2160 r->WriteMask = WRITEMASK_XYZW;
2165 init_src_reg(struct asm_src_register *r)
2167 memset(r, 0, sizeof(*r));
2168 r->Base.File = PROGRAM_UNDEFINED;
2169 r->Base.Swizzle = SWIZZLE_NOOP;
2174 /** Like init_src_reg() but set the File and Index fields.
2175 * \return GL_TRUE if a valid src register, GL_FALSE otherwise
2178 set_src_reg(struct asm_src_register *r, gl_register_file file, GLint index)
2180 set_src_reg_swz(r, file, index, SWIZZLE_XYZW);
2185 set_src_reg_swz(struct asm_src_register *r, gl_register_file file, GLint index,
2188 const GLint maxIndex = (1 << INST_INDEX_BITS) - 1;
2189 const GLint minIndex = -(1 << INST_INDEX_BITS);
2190 assert(file < PROGRAM_FILE_MAX);
2191 assert(index >= minIndex);
2193 assert(index <= maxIndex);
2195 memset(r, 0, sizeof(*r));
2196 r->Base.File = file;
2197 r->Base.Index = index;
2198 r->Base.Swizzle = swizzle;
2204 * Validate the set of inputs used by a program
2206 * Validates that legal sets of inputs are used by the program. In this case
2207 * "used" included both reading the input or binding the input to a name using
2208 * the \c ATTRIB command.
2211 * \c TRUE if the combination of inputs used is valid, \c FALSE otherwise.
2214 validate_inputs(struct YYLTYPE *locp, struct asm_parser_state *state)
2216 const GLbitfield64 inputs = state->prog->info.inputs_read | state->InputsBound;
2217 GLbitfield ff_inputs = 0;
2219 /* Since Mesa internal attribute indices are different from
2220 * how NV_vertex_program defines attribute aliasing, we have to construct
2221 * a separate usage mask based on how the aliasing is defined.
2223 * Note that attribute aliasing is optional if NV_vertex_program is
2226 if (inputs & VERT_BIT_POS)
2227 ff_inputs |= 1 << 0;
2228 if (inputs & VERT_BIT_NORMAL)
2229 ff_inputs |= 1 << 2;
2230 if (inputs & VERT_BIT_COLOR0)
2231 ff_inputs |= 1 << 3;
2232 if (inputs & VERT_BIT_COLOR1)
2233 ff_inputs |= 1 << 4;
2234 if (inputs & VERT_BIT_FOG)
2235 ff_inputs |= 1 << 5;
2237 ff_inputs |= ((inputs & VERT_BIT_TEX_ALL) >> VERT_ATTRIB_TEX0) << 8;
2239 if ((ff_inputs & (inputs >> VERT_ATTRIB_GENERIC0)) != 0) {
2240 yyerror(locp, state, "illegal use of generic attribute and name attribute");
2249 declare_variable(struct asm_parser_state *state, char *name, enum asm_type t,
2250 struct YYLTYPE *locp)
2252 struct asm_symbol *s = NULL;
2253 struct asm_symbol *exist = (struct asm_symbol *)
2254 _mesa_symbol_table_find_symbol(state->st, name);
2257 if (exist != NULL) {
2258 yyerror(locp, state, "redeclared identifier");
2260 s = calloc(1, sizeof(struct asm_symbol));
2266 if (state->prog->arb.NumTemporaries >= state->limits->MaxTemps) {
2267 yyerror(locp, state, "too many temporaries declared");
2272 s->temp_binding = state->prog->arb.NumTemporaries;
2273 state->prog->arb.NumTemporaries++;
2277 if (state->prog->arb.NumAddressRegs >=
2278 state->limits->MaxAddressRegs) {
2279 yyerror(locp, state, "too many address registers declared");
2284 /* FINISHME: Add support for multiple address registers.
2286 state->prog->arb.NumAddressRegs++;
2293 _mesa_symbol_table_add_symbol(state->st, s->name, s);
2294 s->next = state->sym;
2302 int add_state_reference(struct gl_program_parameter_list *param_list,
2303 const gl_state_index16 tokens[STATE_LENGTH])
2305 const GLuint size = 4; /* XXX fix */
2309 name = _mesa_program_state_string(tokens);
2310 index = _mesa_add_parameter(param_list, PROGRAM_STATE_VAR, name,
2311 size, GL_NONE, NULL, tokens, true);
2312 param_list->StateFlags |= _mesa_program_state_flags(tokens);
2314 /* free name string here since we duplicated it in add_parameter() */
2322 initialize_symbol_from_state(struct gl_program *prog,
2323 struct asm_symbol *param_var,
2324 const gl_state_index16 tokens[STATE_LENGTH])
2327 gl_state_index16 state_tokens[STATE_LENGTH];
2330 memcpy(state_tokens, tokens, sizeof(state_tokens));
2332 param_var->type = at_param;
2333 param_var->param_binding_type = PROGRAM_STATE_VAR;
2335 /* If we are adding a STATE_MATRIX that has multiple rows, we need to
2336 * unroll it and call add_state_reference() for each row
2338 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
2339 state_tokens[0] == STATE_PROJECTION_MATRIX ||
2340 state_tokens[0] == STATE_MVP_MATRIX ||
2341 state_tokens[0] == STATE_TEXTURE_MATRIX ||
2342 state_tokens[0] == STATE_PROGRAM_MATRIX)
2343 && (state_tokens[2] != state_tokens[3])) {
2345 const int first_row = state_tokens[2];
2346 const int last_row = state_tokens[3];
2348 for (row = first_row; row <= last_row; row++) {
2349 state_tokens[2] = state_tokens[3] = row;
2351 idx = add_state_reference(prog->Parameters, state_tokens);
2352 if (param_var->param_binding_begin == ~0U) {
2353 param_var->param_binding_begin = idx;
2354 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2357 param_var->param_binding_length++;
2361 idx = add_state_reference(prog->Parameters, state_tokens);
2362 if (param_var->param_binding_begin == ~0U) {
2363 param_var->param_binding_begin = idx;
2364 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2366 param_var->param_binding_length++;
2374 initialize_symbol_from_param(struct gl_program *prog,
2375 struct asm_symbol *param_var,
2376 const gl_state_index16 tokens[STATE_LENGTH])
2379 gl_state_index16 state_tokens[STATE_LENGTH];
2382 memcpy(state_tokens, tokens, sizeof(state_tokens));
2384 assert((state_tokens[0] == STATE_VERTEX_PROGRAM)
2385 || (state_tokens[0] == STATE_FRAGMENT_PROGRAM));
2386 assert((state_tokens[1] == STATE_ENV)
2387 || (state_tokens[1] == STATE_LOCAL));
2390 * The param type is STATE_VAR. The program parameter entry will
2391 * effectively be a pointer into the LOCAL or ENV parameter array.
2393 param_var->type = at_param;
2394 param_var->param_binding_type = PROGRAM_STATE_VAR;
2396 /* If we are adding a STATE_ENV or STATE_LOCAL that has multiple elements,
2397 * we need to unroll it and call add_state_reference() for each row
2399 if (state_tokens[2] != state_tokens[3]) {
2401 const int first_row = state_tokens[2];
2402 const int last_row = state_tokens[3];
2404 for (row = first_row; row <= last_row; row++) {
2405 state_tokens[2] = state_tokens[3] = row;
2407 idx = add_state_reference(prog->Parameters, state_tokens);
2408 if (param_var->param_binding_begin == ~0U) {
2409 param_var->param_binding_begin = idx;
2410 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2412 param_var->param_binding_length++;
2416 idx = add_state_reference(prog->Parameters, state_tokens);
2417 if (param_var->param_binding_begin == ~0U) {
2418 param_var->param_binding_begin = idx;
2419 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2421 param_var->param_binding_length++;
2429 * Put a float/vector constant/literal into the parameter list.
2430 * \param param_var returns info about the parameter/constant's location,
2431 * binding, type, etc.
2432 * \param vec the vector/constant to add
2433 * \param allowSwizzle if true, try to consolidate constants which only differ
2434 * by a swizzle. We don't want to do this when building
2435 * arrays of constants that may be indexed indirectly.
2436 * \return index of the constant in the parameter list.
2439 initialize_symbol_from_const(struct gl_program *prog,
2440 struct asm_symbol *param_var,
2441 const struct asm_vector *vec,
2442 GLboolean allowSwizzle)
2445 const int idx = _mesa_add_unnamed_constant(prog->Parameters,
2446 vec->data, vec->count,
2447 allowSwizzle ? &swizzle : NULL);
2449 param_var->type = at_param;
2450 param_var->param_binding_type = PROGRAM_CONSTANT;
2452 if (param_var->param_binding_begin == ~0U) {
2453 param_var->param_binding_begin = idx;
2454 param_var->param_binding_swizzle = allowSwizzle ? swizzle : SWIZZLE_XYZW;
2456 param_var->param_binding_length++;
2463 make_error_string(const char *fmt, ...)
2470 /* Call vsnprintf once to determine how large the final string is. Call it
2471 * again to do the actual formatting. from the vsnprintf manual page:
2473 * Upon successful return, these functions return the number of
2474 * characters printed (not including the trailing '\0' used to end
2475 * output to strings).
2477 va_start(args, fmt);
2478 length = 1 + vsnprintf(NULL, 0, fmt, args);
2481 str = malloc(length);
2483 va_start(args, fmt);
2484 vsnprintf(str, length, fmt, args);
2493 yyerror(YYLTYPE *locp, struct asm_parser_state *state, const char *s)
2498 err_str = make_error_string("glProgramStringARB(%s)\n", s);
2500 _mesa_error(state->ctx, GL_INVALID_OPERATION, "%s", err_str);
2504 err_str = make_error_string("line %u, char %u: error: %s\n",
2505 locp->first_line, locp->first_column, s);
2506 _mesa_set_program_error(state->ctx, locp->position, err_str);
2515 _mesa_parse_arb_program(struct gl_context *ctx, GLenum target, const GLubyte *str,
2516 GLsizei len, struct asm_parser_state *state)
2518 struct asm_instruction *inst;
2521 GLboolean result = GL_FALSE;
2523 struct asm_symbol *sym;
2526 state->prog->Target = target;
2527 state->prog->Parameters = _mesa_new_parameter_list();
2529 /* Make a copy of the program string and force it to be NUL-terminated.
2531 strz = (GLubyte *) ralloc_size(state->mem_ctx, len + 1);
2533 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
2536 memcpy (strz, str, len);
2539 state->prog->String = strz;
2541 state->st = _mesa_symbol_table_ctor();
2543 state->limits = (target == GL_VERTEX_PROGRAM_ARB)
2544 ? & ctx->Const.Program[MESA_SHADER_VERTEX]
2545 : & ctx->Const.Program[MESA_SHADER_FRAGMENT];
2547 state->MaxTextureImageUnits = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits;
2548 state->MaxTextureCoordUnits = ctx->Const.MaxTextureCoordUnits;
2549 state->MaxTextureUnits = ctx->Const.MaxTextureUnits;
2550 state->MaxClipPlanes = ctx->Const.MaxClipPlanes;
2551 state->MaxLights = ctx->Const.MaxLights;
2552 state->MaxProgramMatrices = ctx->Const.MaxProgramMatrices;
2553 state->MaxDrawBuffers = ctx->Const.MaxDrawBuffers;
2555 state->state_param_enum = (target == GL_VERTEX_PROGRAM_ARB)
2556 ? STATE_VERTEX_PROGRAM : STATE_FRAGMENT_PROGRAM;
2558 _mesa_set_program_error(ctx, -1, NULL);
2560 _mesa_program_lexer_ctor(& state->scanner, state, (const char *) str, len);
2562 _mesa_program_lexer_dtor(state->scanner);
2565 if (ctx->Program.ErrorPos != -1) {
2569 if (! _mesa_layout_parameters(state)) {
2573 loc.first_column = 0;
2576 yyerror(& loc, state, "invalid PARAM usage");
2582 /* Add one instruction to store the "END" instruction.
2584 state->prog->arb.Instructions =
2585 rzalloc_array(state->mem_ctx, struct prog_instruction,
2586 state->prog->arb.NumInstructions + 1);
2588 if (state->prog->arb.Instructions == NULL) {
2592 inst = state->inst_head;
2593 for (i = 0; i < state->prog->arb.NumInstructions; i++) {
2594 struct asm_instruction *const temp = inst->next;
2596 state->prog->arb.Instructions[i] = inst->Base;
2600 /* Finally, tag on an OPCODE_END instruction */
2602 const GLuint numInst = state->prog->arb.NumInstructions;
2603 _mesa_init_instructions(state->prog->arb.Instructions + numInst, 1);
2604 state->prog->arb.Instructions[numInst].Opcode = OPCODE_END;
2606 state->prog->arb.NumInstructions++;
2608 state->prog->arb.NumParameters = state->prog->Parameters->NumParameters;
2609 state->prog->arb.NumAttributes =
2610 _mesa_bitcount_64(state->prog->info.inputs_read);
2613 * Initialize native counts to logical counts. The device driver may
2614 * change them if program is translated into a hardware program.
2616 state->prog->arb.NumNativeInstructions = state->prog->arb.NumInstructions;
2617 state->prog->arb.NumNativeTemporaries = state->prog->arb.NumTemporaries;
2618 state->prog->arb.NumNativeParameters = state->prog->arb.NumParameters;
2619 state->prog->arb.NumNativeAttributes = state->prog->arb.NumAttributes;
2620 state->prog->arb.NumNativeAddressRegs = state->prog->arb.NumAddressRegs;
2625 for (inst = state->inst_head; inst != NULL; inst = temp) {
2630 state->inst_head = NULL;
2631 state->inst_tail = NULL;
2633 for (sym = state->sym; sym != NULL; sym = temp) {
2636 free((void *) sym->name);
2641 _mesa_symbol_table_dtor(state->st);