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/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"
38 #include "program/symbol_table.h"
39 #include "program/program_parser.h"
41 extern void *yy_scan_string(char *);
42 extern void yy_delete_buffer(void *);
44 static struct asm_symbol *declare_variable(struct asm_parser_state *state,
45 char *name, enum asm_type t, struct YYLTYPE *locp);
47 static int add_state_reference(struct gl_program_parameter_list *param_list,
48 const gl_state_index tokens[STATE_LENGTH]);
50 static int initialize_symbol_from_state(struct gl_program *prog,
51 struct asm_symbol *param_var, const gl_state_index tokens[STATE_LENGTH]);
53 static int initialize_symbol_from_param(struct gl_program *prog,
54 struct asm_symbol *param_var, const gl_state_index tokens[STATE_LENGTH]);
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);
60 static int yyparse(struct asm_parser_state *state);
62 static char *make_error_string(const char *fmt, ...);
64 static void yyerror(struct YYLTYPE *locp, struct asm_parser_state *state,
67 static int validate_inputs(struct YYLTYPE *locp,
68 struct asm_parser_state *state);
70 static void init_dst_reg(struct prog_dst_register *r);
72 static void set_dst_reg(struct prog_dst_register *r,
73 gl_register_file file, GLint index);
75 static void init_src_reg(struct asm_src_register *r);
77 static void set_src_reg(struct asm_src_register *r,
78 gl_register_file file, GLint index);
80 static void set_src_reg_swz(struct asm_src_register *r,
81 gl_register_file file, GLint index, GLuint swizzle);
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);
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);
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);
101 #define YYLLOC_DEFAULT(Current, Rhs, 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; \
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; \
122 %lex-param { struct asm_parser_state *state }
123 %parse-param { struct asm_parser_state *state }
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;
139 gl_state_index state[STATE_LENGTH];
141 struct asm_vector vector;
142 enum prog_opcode opcode;
146 unsigned rgba_valid:1;
147 unsigned xyzw_valid:1;
152 %token ARBvp_10 ARBfp_10
154 /* Tokens for assembler pseudo-ops */
155 %token <integer> ADDRESS
159 %token <integer> TEMP
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
166 %token <integer> INTEGER
169 %token AMBIENT ATTENUATION
172 %token DEPTH DIFFUSE DIRECTION
173 %token EMISSION ENV EYE
174 %token FOG FOGCOORD FRAGMENT FRONT
176 %token INVERSE INVTRANS
177 %token LIGHT LIGHTMODEL LIGHTPROD LOCAL
178 %token MATERIAL MAT_PROGRAM MATRIX MATRIXINDEX MODELVIEW MVP
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
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;
1010 | WEIGHT vtxOptWeightNum
1012 $$ = VERT_ATTRIB_WEIGHT;
1016 $$ = VERT_ATTRIB_NORMAL;
1018 | COLOR optColorType
1020 $$ = VERT_ATTRIB_COLOR0 + $2;
1024 $$ = VERT_ATTRIB_FOG;
1026 | TEXCOORD optTexCoordUnitNum
1028 $$ = VERT_ATTRIB_TEX0 + $2;
1030 | MATRIXINDEX '[' vtxWeightNum ']'
1032 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1035 | VTXATTRIB '[' vtxAttribNum ']'
1037 $$ = VERT_ATTRIB_GENERIC0 + $3;
1041 vtxAttribNum: INTEGER
1043 if ((unsigned) $1 >= state->limits->MaxAttribs) {
1044 yyerror(& @1, state, "invalid vertex attribute reference");
1052 vtxOptWeightNum: | '[' vtxWeightNum ']';
1053 vtxWeightNum: INTEGER;
1055 fragAttribItem: POSITION
1057 $$ = VARYING_SLOT_POS;
1059 | COLOR optColorType
1061 $$ = VARYING_SLOT_COL0 + $2;
1065 $$ = VARYING_SLOT_FOGC;
1067 | TEXCOORD optTexCoordUnitNum
1069 $$ = VARYING_SLOT_TEX0 + $2;
1073 PARAM_statement: PARAM_singleStmt | PARAM_multipleStmt;
1075 PARAM_singleStmt: PARAM IDENTIFIER paramSingleInit
1077 struct asm_symbol *const s =
1078 declare_variable(state, $2, at_param, & @2);
1084 s->param_binding_type = $3.param_binding_type;
1085 s->param_binding_begin = $3.param_binding_begin;
1086 s->param_binding_length = $3.param_binding_length;
1087 s->param_binding_swizzle = $3.param_binding_swizzle;
1088 s->param_is_array = 0;
1093 PARAM_multipleStmt: PARAM IDENTIFIER '[' optArraySize ']' paramMultipleInit
1095 if (($4 != 0) && ((unsigned) $4 != $6.param_binding_length)) {
1097 yyerror(& @4, state,
1098 "parameter array size and number of bindings must match");
1101 struct asm_symbol *const s =
1102 declare_variable(state, $2, $6.type, & @2);
1108 s->param_binding_type = $6.param_binding_type;
1109 s->param_binding_begin = $6.param_binding_begin;
1110 s->param_binding_length = $6.param_binding_length;
1111 s->param_binding_swizzle = SWIZZLE_XYZW;
1112 s->param_is_array = 1;
1124 if (($1 < 1) || ((unsigned) $1 > state->limits->MaxParameters)) {
1126 _mesa_snprintf(msg, sizeof(msg),
1127 "invalid parameter array size (size=%d max=%u)",
1128 $1, state->limits->MaxParameters);
1129 yyerror(& @1, state, msg);
1137 paramSingleInit: '=' paramSingleItemDecl
1143 paramMultipleInit: '=' '{' paramMultInitList '}'
1149 paramMultInitList: paramMultipleItem
1150 | paramMultInitList ',' paramMultipleItem
1152 $1.param_binding_length += $3.param_binding_length;
1157 paramSingleItemDecl: stateSingleItem
1159 memset(& $$, 0, sizeof($$));
1160 $$.param_binding_begin = ~0;
1161 initialize_symbol_from_state(state->prog, & $$, $1);
1165 memset(& $$, 0, sizeof($$));
1166 $$.param_binding_begin = ~0;
1167 initialize_symbol_from_param(state->prog, & $$, $1);
1171 memset(& $$, 0, sizeof($$));
1172 $$.param_binding_begin = ~0;
1173 initialize_symbol_from_const(state->prog, & $$, & $1, GL_TRUE);
1177 paramSingleItemUse: stateSingleItem
1179 memset(& $$, 0, sizeof($$));
1180 $$.param_binding_begin = ~0;
1181 initialize_symbol_from_state(state->prog, & $$, $1);
1185 memset(& $$, 0, sizeof($$));
1186 $$.param_binding_begin = ~0;
1187 initialize_symbol_from_param(state->prog, & $$, $1);
1191 memset(& $$, 0, sizeof($$));
1192 $$.param_binding_begin = ~0;
1193 initialize_symbol_from_const(state->prog, & $$, & $1, GL_TRUE);
1197 paramMultipleItem: stateMultipleItem
1199 memset(& $$, 0, sizeof($$));
1200 $$.param_binding_begin = ~0;
1201 initialize_symbol_from_state(state->prog, & $$, $1);
1203 | programMultipleItem
1205 memset(& $$, 0, sizeof($$));
1206 $$.param_binding_begin = ~0;
1207 initialize_symbol_from_param(state->prog, & $$, $1);
1211 memset(& $$, 0, sizeof($$));
1212 $$.param_binding_begin = ~0;
1213 initialize_symbol_from_const(state->prog, & $$, & $1, GL_FALSE);
1217 stateMultipleItem: stateSingleItem { memcpy($$, $1, sizeof($$)); }
1218 | STATE stateMatrixRows { memcpy($$, $2, sizeof($$)); }
1221 stateSingleItem: STATE stateMaterialItem { memcpy($$, $2, sizeof($$)); }
1222 | STATE stateLightItem { memcpy($$, $2, sizeof($$)); }
1223 | STATE stateLightModelItem { memcpy($$, $2, sizeof($$)); }
1224 | STATE stateLightProdItem { memcpy($$, $2, sizeof($$)); }
1225 | STATE stateTexGenItem { memcpy($$, $2, sizeof($$)); }
1226 | STATE stateTexEnvItem { memcpy($$, $2, sizeof($$)); }
1227 | STATE stateFogItem { memcpy($$, $2, sizeof($$)); }
1228 | STATE stateClipPlaneItem { memcpy($$, $2, sizeof($$)); }
1229 | STATE statePointItem { memcpy($$, $2, sizeof($$)); }
1230 | STATE stateMatrixRow { memcpy($$, $2, sizeof($$)); }
1231 | STATE stateDepthItem { memcpy($$, $2, sizeof($$)); }
1234 stateMaterialItem: MATERIAL optFaceType stateMatProperty
1236 memset($$, 0, sizeof($$));
1237 $$[0] = STATE_MATERIAL;
1243 stateMatProperty: ambDiffSpecProperty
1249 $$ = STATE_EMISSION;
1253 $$ = STATE_SHININESS;
1257 stateLightItem: LIGHT '[' stateLightNumber ']' stateLightProperty
1259 memset($$, 0, sizeof($$));
1260 $$[0] = STATE_LIGHT;
1266 stateLightProperty: ambDiffSpecProperty
1272 $$ = STATE_POSITION;
1276 if (!state->ctx->Extensions.EXT_point_parameters) {
1277 yyerror(& @1, state, "GL_ARB_point_parameters not supported");
1281 $$ = STATE_ATTENUATION;
1283 | SPOT stateSpotProperty
1289 $$ = STATE_HALF_VECTOR;
1293 stateSpotProperty: DIRECTION
1295 $$ = STATE_SPOT_DIRECTION;
1299 stateLightModelItem: LIGHTMODEL stateLModProperty
1306 stateLModProperty: AMBIENT
1308 memset($$, 0, sizeof($$));
1309 $$[0] = STATE_LIGHTMODEL_AMBIENT;
1311 | optFaceType SCENECOLOR
1313 memset($$, 0, sizeof($$));
1314 $$[0] = STATE_LIGHTMODEL_SCENECOLOR;
1319 stateLightProdItem: LIGHTPROD '[' stateLightNumber ']' optFaceType stateLProdProperty
1321 memset($$, 0, sizeof($$));
1322 $$[0] = STATE_LIGHTPROD;
1329 stateLProdProperty: ambDiffSpecProperty;
1331 stateTexEnvItem: TEXENV optLegacyTexUnitNum stateTexEnvProperty
1333 memset($$, 0, sizeof($$));
1339 stateTexEnvProperty: COLOR
1341 $$ = STATE_TEXENV_COLOR;
1345 ambDiffSpecProperty: AMBIENT
1355 $$ = STATE_SPECULAR;
1359 stateLightNumber: INTEGER
1361 if ((unsigned) $1 >= state->MaxLights) {
1362 yyerror(& @1, state, "invalid light selector");
1370 stateTexGenItem: TEXGEN optTexCoordUnitNum stateTexGenType stateTexGenCoord
1372 memset($$, 0, sizeof($$));
1373 $$[0] = STATE_TEXGEN;
1379 stateTexGenType: EYE
1381 $$ = STATE_TEXGEN_EYE_S;
1385 $$ = STATE_TEXGEN_OBJECT_S;
1388 stateTexGenCoord: TEXGEN_S
1390 $$ = STATE_TEXGEN_EYE_S - STATE_TEXGEN_EYE_S;
1394 $$ = STATE_TEXGEN_EYE_T - STATE_TEXGEN_EYE_S;
1398 $$ = STATE_TEXGEN_EYE_R - STATE_TEXGEN_EYE_S;
1402 $$ = STATE_TEXGEN_EYE_Q - STATE_TEXGEN_EYE_S;
1406 stateFogItem: FOG stateFogProperty
1408 memset($$, 0, sizeof($$));
1413 stateFogProperty: COLOR
1415 $$ = STATE_FOG_COLOR;
1419 $$ = STATE_FOG_PARAMS;
1423 stateClipPlaneItem: CLIP '[' stateClipPlaneNum ']' PLANE
1425 memset($$, 0, sizeof($$));
1426 $$[0] = STATE_CLIPPLANE;
1431 stateClipPlaneNum: INTEGER
1433 if ((unsigned) $1 >= state->MaxClipPlanes) {
1434 yyerror(& @1, state, "invalid clip plane selector");
1442 statePointItem: POINT_TOK statePointProperty
1444 memset($$, 0, sizeof($$));
1449 statePointProperty: SIZE_TOK
1451 $$ = STATE_POINT_SIZE;
1455 $$ = STATE_POINT_ATTENUATION;
1459 stateMatrixRow: stateMatrixItem ROW '[' stateMatrixRowNum ']'
1469 stateMatrixRows: stateMatrixItem optMatrixRows
1484 | ROW '[' stateMatrixRowNum DOT_DOT stateMatrixRowNum ']'
1486 /* It seems logical that the matrix row range specifier would have
1487 * to specify a range or more than one row (i.e., $5 > $3).
1488 * However, the ARB_vertex_program spec says "a program will fail
1489 * to load if <a> is greater than <b>." This means that $3 == $5
1493 yyerror(& @3, state, "invalid matrix row range");
1502 stateMatrixItem: MATRIX stateMatrixName stateOptMatModifier
1510 stateOptMatModifier:
1520 stateMatModifier: INVERSE
1522 $$ = STATE_MATRIX_INVERSE;
1526 $$ = STATE_MATRIX_TRANSPOSE;
1530 $$ = STATE_MATRIX_INVTRANS;
1534 stateMatrixRowNum: INTEGER
1537 yyerror(& @1, state, "invalid matrix row reference");
1545 stateMatrixName: MODELVIEW stateOptModMatNum
1547 $$[0] = STATE_MODELVIEW_MATRIX;
1552 $$[0] = STATE_PROJECTION_MATRIX;
1557 $$[0] = STATE_MVP_MATRIX;
1560 | TEXTURE optTexCoordUnitNum
1562 $$[0] = STATE_TEXTURE_MATRIX;
1565 | PALETTE '[' statePaletteMatNum ']'
1567 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1570 | MAT_PROGRAM '[' stateProgramMatNum ']'
1572 $$[0] = STATE_PROGRAM_MATRIX;
1581 | '[' stateModMatNum ']'
1586 stateModMatNum: INTEGER
1588 /* Since GL_ARB_vertex_blend isn't supported, only modelview matrix
1592 yyerror(& @1, state, "invalid modelview matrix index");
1599 statePaletteMatNum: INTEGER
1601 /* Since GL_ARB_matrix_palette isn't supported, just let any value
1602 * through here. The error will be generated later.
1607 stateProgramMatNum: INTEGER
1609 if ((unsigned) $1 >= state->MaxProgramMatrices) {
1610 yyerror(& @1, state, "invalid program matrix selector");
1618 stateDepthItem: DEPTH RANGE
1620 memset($$, 0, sizeof($$));
1621 $$[0] = STATE_DEPTH_RANGE;
1626 programSingleItem: progEnvParam | progLocalParam;
1628 programMultipleItem: progEnvParams | progLocalParams;
1630 progEnvParams: PROGRAM ENV '[' progEnvParamNums ']'
1632 memset($$, 0, sizeof($$));
1633 $$[0] = state->state_param_enum;
1640 progEnvParamNums: progEnvParamNum
1645 | progEnvParamNum DOT_DOT progEnvParamNum
1652 progEnvParam: PROGRAM ENV '[' progEnvParamNum ']'
1654 memset($$, 0, sizeof($$));
1655 $$[0] = state->state_param_enum;
1662 progLocalParams: PROGRAM LOCAL '[' progLocalParamNums ']'
1664 memset($$, 0, sizeof($$));
1665 $$[0] = state->state_param_enum;
1666 $$[1] = STATE_LOCAL;
1671 progLocalParamNums: progLocalParamNum
1676 | progLocalParamNum DOT_DOT progLocalParamNum
1683 progLocalParam: PROGRAM LOCAL '[' progLocalParamNum ']'
1685 memset($$, 0, sizeof($$));
1686 $$[0] = state->state_param_enum;
1687 $$[1] = STATE_LOCAL;
1693 progEnvParamNum: INTEGER
1695 if ((unsigned) $1 >= state->limits->MaxEnvParams) {
1696 yyerror(& @1, state, "invalid environment parameter reference");
1703 progLocalParamNum: INTEGER
1705 if ((unsigned) $1 >= state->limits->MaxLocalParams) {
1706 yyerror(& @1, state, "invalid local parameter reference");
1715 paramConstDecl: paramConstScalarDecl | paramConstVector;
1716 paramConstUse: paramConstScalarUse | paramConstVector;
1718 paramConstScalarDecl: signedFloatConstant
1728 paramConstScalarUse: REAL
1739 $$.data[0].f = (float) $1;
1740 $$.data[1].f = (float) $1;
1741 $$.data[2].f = (float) $1;
1742 $$.data[3].f = (float) $1;
1746 paramConstVector: '{' signedFloatConstant '}'
1750 $$.data[1].f = 0.0f;
1751 $$.data[2].f = 0.0f;
1752 $$.data[3].f = 1.0f;
1754 | '{' signedFloatConstant ',' signedFloatConstant '}'
1759 $$.data[2].f = 0.0f;
1760 $$.data[3].f = 1.0f;
1762 | '{' signedFloatConstant ',' signedFloatConstant ','
1763 signedFloatConstant '}'
1769 $$.data[3].f = 1.0f;
1771 | '{' signedFloatConstant ',' signedFloatConstant ','
1772 signedFloatConstant ',' signedFloatConstant '}'
1782 signedFloatConstant: optionalSign REAL
1784 $$ = ($1) ? -$2 : $2;
1786 | optionalSign INTEGER
1788 $$ = (float)(($1) ? -$2 : $2);
1792 optionalSign: '+' { $$ = FALSE; }
1793 | '-' { $$ = TRUE; }
1797 TEMP_statement: TEMP { $<integer>$ = $1; } varNameList
1800 ADDRESS_statement: ADDRESS { $<integer>$ = $1; } varNameList
1803 varNameList: varNameList ',' IDENTIFIER
1805 if (!declare_variable(state, $3, $<integer>0, & @3)) {
1812 if (!declare_variable(state, $1, $<integer>0, & @1)) {
1819 OUTPUT_statement: OUTPUT IDENTIFIER '=' resultBinding
1821 struct asm_symbol *const s =
1822 declare_variable(state, $2, at_output, & @2);
1828 s->output_binding = $4;
1833 resultBinding: RESULT POSITION
1835 if (state->mode == ARB_vertex) {
1836 $$ = VARYING_SLOT_POS;
1838 yyerror(& @2, state, "invalid program result name");
1844 if (state->mode == ARB_vertex) {
1845 $$ = VARYING_SLOT_FOGC;
1847 yyerror(& @2, state, "invalid program result name");
1851 | RESULT resultColBinding
1857 if (state->mode == ARB_vertex) {
1858 $$ = VARYING_SLOT_PSIZ;
1860 yyerror(& @2, state, "invalid program result name");
1864 | RESULT TEXCOORD optTexCoordUnitNum
1866 if (state->mode == ARB_vertex) {
1867 $$ = VARYING_SLOT_TEX0 + $3;
1869 yyerror(& @2, state, "invalid program result name");
1875 if (state->mode == ARB_fragment) {
1876 $$ = FRAG_RESULT_DEPTH;
1878 yyerror(& @2, state, "invalid program result name");
1884 resultColBinding: COLOR optResultFaceType optResultColorType
1892 if (state->mode == ARB_vertex) {
1893 $$ = VARYING_SLOT_COL0;
1895 if (state->option.DrawBuffers)
1896 $$ = FRAG_RESULT_DATA0;
1898 $$ = FRAG_RESULT_COLOR;
1903 if (state->mode == ARB_vertex) {
1904 yyerror(& @1, state, "invalid program result name");
1907 if (!state->option.DrawBuffers) {
1908 /* From the ARB_draw_buffers spec (same text exists
1909 * for ATI_draw_buffers):
1911 * If this option is not specified, a fragment
1912 * program that attempts to bind
1913 * "result.color[n]" will fail to load, and only
1914 * "result.color" will be allowed.
1916 yyerror(& @1, state,
1917 "result.color[] used without "
1918 "`OPTION ARB_draw_buffers' or "
1919 "`OPTION ATI_draw_buffers'");
1921 } else if ($2 >= state->MaxDrawBuffers) {
1922 yyerror(& @1, state,
1923 "result.color[] exceeds MAX_DRAW_BUFFERS_ARB");
1926 $$ = FRAG_RESULT_DATA0 + $2;
1931 if (state->mode == ARB_vertex) {
1932 $$ = VARYING_SLOT_COL0;
1934 yyerror(& @1, state, "invalid program result name");
1940 if (state->mode == ARB_vertex) {
1941 $$ = VARYING_SLOT_BFC0;
1943 yyerror(& @1, state, "invalid program result name");
1955 if (state->mode == ARB_vertex) {
1958 yyerror(& @1, state, "invalid program result name");
1964 if (state->mode == ARB_vertex) {
1967 yyerror(& @1, state, "invalid program result name");
1973 optFaceType: { $$ = 0; }
1978 optColorType: { $$ = 0; }
1979 | PRIMARY { $$ = 0; }
1980 | SECONDARY { $$ = 1; }
1983 optTexCoordUnitNum: { $$ = 0; }
1984 | '[' texCoordUnitNum ']' { $$ = $2; }
1987 optTexImageUnitNum: { $$ = 0; }
1988 | '[' texImageUnitNum ']' { $$ = $2; }
1991 optLegacyTexUnitNum: { $$ = 0; }
1992 | '[' legacyTexUnitNum ']' { $$ = $2; }
1995 texCoordUnitNum: INTEGER
1997 if ((unsigned) $1 >= state->MaxTextureCoordUnits) {
1998 yyerror(& @1, state, "invalid texture coordinate unit selector");
2006 texImageUnitNum: INTEGER
2008 if ((unsigned) $1 >= state->MaxTextureImageUnits) {
2009 yyerror(& @1, state, "invalid texture image unit selector");
2017 legacyTexUnitNum: INTEGER
2019 if ((unsigned) $1 >= state->MaxTextureUnits) {
2020 yyerror(& @1, state, "invalid texture unit selector");
2028 ALIAS_statement: ALIAS IDENTIFIER '=' USED_IDENTIFIER
2030 struct asm_symbol *exist = (struct asm_symbol *)
2031 _mesa_symbol_table_find_symbol(state->st, $2);
2032 struct asm_symbol *target = (struct asm_symbol *)
2033 _mesa_symbol_table_find_symbol(state->st, $4);
2037 if (exist != NULL) {
2039 _mesa_snprintf(m, sizeof(m), "redeclared identifier: %s", $2);
2041 yyerror(& @2, state, m);
2043 } else if (target == NULL) {
2045 yyerror(& @4, state,
2046 "undefined variable binding in ALIAS statement");
2049 _mesa_symbol_table_add_symbol(state->st, $2, target);
2061 asm_instruction_set_operands(struct asm_instruction *inst,
2062 const struct prog_dst_register *dst,
2063 const struct asm_src_register *src0,
2064 const struct asm_src_register *src1,
2065 const struct asm_src_register *src2)
2067 /* In the core ARB extensions only the KIL instruction doesn't have a
2068 * destination register.
2071 init_dst_reg(& inst->Base.DstReg);
2073 inst->Base.DstReg = *dst;
2077 inst->Base.SrcReg[0] = src0->Base;
2078 inst->SrcReg[0] = *src0;
2080 init_src_reg(& inst->SrcReg[0]);
2084 inst->Base.SrcReg[1] = src1->Base;
2085 inst->SrcReg[1] = *src1;
2087 init_src_reg(& inst->SrcReg[1]);
2091 inst->Base.SrcReg[2] = src2->Base;
2092 inst->SrcReg[2] = *src2;
2094 init_src_reg(& inst->SrcReg[2]);
2099 struct asm_instruction *
2100 asm_instruction_ctor(enum prog_opcode op,
2101 const struct prog_dst_register *dst,
2102 const struct asm_src_register *src0,
2103 const struct asm_src_register *src1,
2104 const struct asm_src_register *src2)
2106 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2109 _mesa_init_instructions(& inst->Base, 1);
2110 inst->Base.Opcode = op;
2112 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2119 struct asm_instruction *
2120 asm_instruction_copy_ctor(const struct prog_instruction *base,
2121 const struct prog_dst_register *dst,
2122 const struct asm_src_register *src0,
2123 const struct asm_src_register *src1,
2124 const struct asm_src_register *src2)
2126 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2129 _mesa_init_instructions(& inst->Base, 1);
2130 inst->Base.Opcode = base->Opcode;
2131 inst->Base.Saturate = base->Saturate;
2133 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2141 init_dst_reg(struct prog_dst_register *r)
2143 memset(r, 0, sizeof(*r));
2144 r->File = PROGRAM_UNDEFINED;
2145 r->WriteMask = WRITEMASK_XYZW;
2149 /** Like init_dst_reg() but set the File and Index fields. */
2151 set_dst_reg(struct prog_dst_register *r, gl_register_file file, GLint index)
2153 const GLint maxIndex = 1 << INST_INDEX_BITS;
2154 const GLint minIndex = 0;
2155 assert(index >= minIndex);
2157 assert(index <= maxIndex);
2159 assert(file == PROGRAM_TEMPORARY ||
2160 file == PROGRAM_ADDRESS ||
2161 file == PROGRAM_OUTPUT);
2162 memset(r, 0, sizeof(*r));
2165 r->WriteMask = WRITEMASK_XYZW;
2170 init_src_reg(struct asm_src_register *r)
2172 memset(r, 0, sizeof(*r));
2173 r->Base.File = PROGRAM_UNDEFINED;
2174 r->Base.Swizzle = SWIZZLE_NOOP;
2179 /** Like init_src_reg() but set the File and Index fields.
2180 * \return GL_TRUE if a valid src register, GL_FALSE otherwise
2183 set_src_reg(struct asm_src_register *r, gl_register_file file, GLint index)
2185 set_src_reg_swz(r, file, index, SWIZZLE_XYZW);
2190 set_src_reg_swz(struct asm_src_register *r, gl_register_file file, GLint index,
2193 const GLint maxIndex = (1 << INST_INDEX_BITS) - 1;
2194 const GLint minIndex = -(1 << INST_INDEX_BITS);
2195 assert(file < PROGRAM_FILE_MAX);
2196 assert(index >= minIndex);
2198 assert(index <= maxIndex);
2200 memset(r, 0, sizeof(*r));
2201 r->Base.File = file;
2202 r->Base.Index = index;
2203 r->Base.Swizzle = swizzle;
2209 * Validate the set of inputs used by a program
2211 * Validates that legal sets of inputs are used by the program. In this case
2212 * "used" included both reading the input or binding the input to a name using
2213 * the \c ATTRIB command.
2216 * \c TRUE if the combination of inputs used is valid, \c FALSE otherwise.
2219 validate_inputs(struct YYLTYPE *locp, struct asm_parser_state *state)
2221 const GLbitfield64 inputs = state->prog->info.inputs_read | state->InputsBound;
2223 if (((inputs & VERT_BIT_FF_ALL) & (inputs >> VERT_ATTRIB_GENERIC0)) != 0) {
2224 yyerror(locp, state, "illegal use of generic attribute and name attribute");
2233 declare_variable(struct asm_parser_state *state, char *name, enum asm_type t,
2234 struct YYLTYPE *locp)
2236 struct asm_symbol *s = NULL;
2237 struct asm_symbol *exist = (struct asm_symbol *)
2238 _mesa_symbol_table_find_symbol(state->st, name);
2241 if (exist != NULL) {
2242 yyerror(locp, state, "redeclared identifier");
2244 s = calloc(1, sizeof(struct asm_symbol));
2250 if (state->prog->arb.NumTemporaries >= state->limits->MaxTemps) {
2251 yyerror(locp, state, "too many temporaries declared");
2256 s->temp_binding = state->prog->arb.NumTemporaries;
2257 state->prog->arb.NumTemporaries++;
2261 if (state->prog->arb.NumAddressRegs >=
2262 state->limits->MaxAddressRegs) {
2263 yyerror(locp, state, "too many address registers declared");
2268 /* FINISHME: Add support for multiple address registers.
2270 state->prog->arb.NumAddressRegs++;
2277 _mesa_symbol_table_add_symbol(state->st, s->name, s);
2278 s->next = state->sym;
2286 int add_state_reference(struct gl_program_parameter_list *param_list,
2287 const gl_state_index tokens[STATE_LENGTH])
2289 const GLuint size = 4; /* XXX fix */
2293 name = _mesa_program_state_string(tokens);
2294 index = _mesa_add_parameter(param_list, PROGRAM_STATE_VAR, name,
2295 size, GL_NONE, NULL, tokens);
2296 param_list->StateFlags |= _mesa_program_state_flags(tokens);
2298 /* free name string here since we duplicated it in add_parameter() */
2306 initialize_symbol_from_state(struct gl_program *prog,
2307 struct asm_symbol *param_var,
2308 const gl_state_index tokens[STATE_LENGTH])
2311 gl_state_index state_tokens[STATE_LENGTH];
2314 memcpy(state_tokens, tokens, sizeof(state_tokens));
2316 param_var->type = at_param;
2317 param_var->param_binding_type = PROGRAM_STATE_VAR;
2319 /* If we are adding a STATE_MATRIX that has multiple rows, we need to
2320 * unroll it and call add_state_reference() for each row
2322 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
2323 state_tokens[0] == STATE_PROJECTION_MATRIX ||
2324 state_tokens[0] == STATE_MVP_MATRIX ||
2325 state_tokens[0] == STATE_TEXTURE_MATRIX ||
2326 state_tokens[0] == STATE_PROGRAM_MATRIX)
2327 && (state_tokens[2] != state_tokens[3])) {
2329 const int first_row = state_tokens[2];
2330 const int last_row = state_tokens[3];
2332 for (row = first_row; row <= last_row; row++) {
2333 state_tokens[2] = state_tokens[3] = row;
2335 idx = add_state_reference(prog->Parameters, state_tokens);
2336 if (param_var->param_binding_begin == ~0U) {
2337 param_var->param_binding_begin = idx;
2338 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2341 param_var->param_binding_length++;
2345 idx = add_state_reference(prog->Parameters, state_tokens);
2346 if (param_var->param_binding_begin == ~0U) {
2347 param_var->param_binding_begin = idx;
2348 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2350 param_var->param_binding_length++;
2358 initialize_symbol_from_param(struct gl_program *prog,
2359 struct asm_symbol *param_var,
2360 const gl_state_index tokens[STATE_LENGTH])
2363 gl_state_index state_tokens[STATE_LENGTH];
2366 memcpy(state_tokens, tokens, sizeof(state_tokens));
2368 assert((state_tokens[0] == STATE_VERTEX_PROGRAM)
2369 || (state_tokens[0] == STATE_FRAGMENT_PROGRAM));
2370 assert((state_tokens[1] == STATE_ENV)
2371 || (state_tokens[1] == STATE_LOCAL));
2374 * The param type is STATE_VAR. The program parameter entry will
2375 * effectively be a pointer into the LOCAL or ENV parameter array.
2377 param_var->type = at_param;
2378 param_var->param_binding_type = PROGRAM_STATE_VAR;
2380 /* If we are adding a STATE_ENV or STATE_LOCAL that has multiple elements,
2381 * we need to unroll it and call add_state_reference() for each row
2383 if (state_tokens[2] != state_tokens[3]) {
2385 const int first_row = state_tokens[2];
2386 const int last_row = state_tokens[3];
2388 for (row = first_row; row <= last_row; row++) {
2389 state_tokens[2] = state_tokens[3] = row;
2391 idx = add_state_reference(prog->Parameters, state_tokens);
2392 if (param_var->param_binding_begin == ~0U) {
2393 param_var->param_binding_begin = idx;
2394 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2396 param_var->param_binding_length++;
2400 idx = add_state_reference(prog->Parameters, state_tokens);
2401 if (param_var->param_binding_begin == ~0U) {
2402 param_var->param_binding_begin = idx;
2403 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2405 param_var->param_binding_length++;
2413 * Put a float/vector constant/literal into the parameter list.
2414 * \param param_var returns info about the parameter/constant's location,
2415 * binding, type, etc.
2416 * \param vec the vector/constant to add
2417 * \param allowSwizzle if true, try to consolidate constants which only differ
2418 * by a swizzle. We don't want to do this when building
2419 * arrays of constants that may be indexed indirectly.
2420 * \return index of the constant in the parameter list.
2423 initialize_symbol_from_const(struct gl_program *prog,
2424 struct asm_symbol *param_var,
2425 const struct asm_vector *vec,
2426 GLboolean allowSwizzle)
2429 const int idx = _mesa_add_unnamed_constant(prog->Parameters,
2430 vec->data, vec->count,
2431 allowSwizzle ? &swizzle : NULL);
2433 param_var->type = at_param;
2434 param_var->param_binding_type = PROGRAM_CONSTANT;
2436 if (param_var->param_binding_begin == ~0U) {
2437 param_var->param_binding_begin = idx;
2438 param_var->param_binding_swizzle = allowSwizzle ? swizzle : SWIZZLE_XYZW;
2440 param_var->param_binding_length++;
2447 make_error_string(const char *fmt, ...)
2454 /* Call vsnprintf once to determine how large the final string is. Call it
2455 * again to do the actual formatting. from the vsnprintf manual page:
2457 * Upon successful return, these functions return the number of
2458 * characters printed (not including the trailing '\0' used to end
2459 * output to strings).
2461 va_start(args, fmt);
2462 length = 1 + vsnprintf(NULL, 0, fmt, args);
2465 str = malloc(length);
2467 va_start(args, fmt);
2468 vsnprintf(str, length, fmt, args);
2477 yyerror(YYLTYPE *locp, struct asm_parser_state *state, const char *s)
2482 err_str = make_error_string("glProgramStringARB(%s)\n", s);
2484 _mesa_error(state->ctx, GL_INVALID_OPERATION, "%s", err_str);
2488 err_str = make_error_string("line %u, char %u: error: %s\n",
2489 locp->first_line, locp->first_column, s);
2490 _mesa_set_program_error(state->ctx, locp->position, err_str);
2499 _mesa_parse_arb_program(struct gl_context *ctx, GLenum target, const GLubyte *str,
2500 GLsizei len, struct asm_parser_state *state)
2502 struct asm_instruction *inst;
2505 GLboolean result = GL_FALSE;
2507 struct asm_symbol *sym;
2510 state->prog->Target = target;
2511 state->prog->Parameters = _mesa_new_parameter_list();
2513 /* Make a copy of the program string and force it to be NUL-terminated.
2515 strz = (GLubyte *) ralloc_size(state->mem_ctx, len + 1);
2517 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
2520 memcpy (strz, str, len);
2523 state->prog->String = strz;
2525 state->st = _mesa_symbol_table_ctor();
2527 state->limits = (target == GL_VERTEX_PROGRAM_ARB)
2528 ? & ctx->Const.Program[MESA_SHADER_VERTEX]
2529 : & ctx->Const.Program[MESA_SHADER_FRAGMENT];
2531 state->MaxTextureImageUnits = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits;
2532 state->MaxTextureCoordUnits = ctx->Const.MaxTextureCoordUnits;
2533 state->MaxTextureUnits = ctx->Const.MaxTextureUnits;
2534 state->MaxClipPlanes = ctx->Const.MaxClipPlanes;
2535 state->MaxLights = ctx->Const.MaxLights;
2536 state->MaxProgramMatrices = ctx->Const.MaxProgramMatrices;
2537 state->MaxDrawBuffers = ctx->Const.MaxDrawBuffers;
2539 state->state_param_enum = (target == GL_VERTEX_PROGRAM_ARB)
2540 ? STATE_VERTEX_PROGRAM : STATE_FRAGMENT_PROGRAM;
2542 _mesa_set_program_error(ctx, -1, NULL);
2544 _mesa_program_lexer_ctor(& state->scanner, state, (const char *) str, len);
2546 _mesa_program_lexer_dtor(state->scanner);
2549 if (ctx->Program.ErrorPos != -1) {
2553 if (! _mesa_layout_parameters(state)) {
2557 loc.first_column = 0;
2560 yyerror(& loc, state, "invalid PARAM usage");
2566 /* Add one instruction to store the "END" instruction.
2568 state->prog->arb.Instructions =
2569 rzalloc_array(state->mem_ctx, struct prog_instruction,
2570 state->prog->arb.NumInstructions + 1);
2572 if (state->prog->arb.Instructions == NULL) {
2576 inst = state->inst_head;
2577 for (i = 0; i < state->prog->arb.NumInstructions; i++) {
2578 struct asm_instruction *const temp = inst->next;
2580 state->prog->arb.Instructions[i] = inst->Base;
2584 /* Finally, tag on an OPCODE_END instruction */
2586 const GLuint numInst = state->prog->arb.NumInstructions;
2587 _mesa_init_instructions(state->prog->arb.Instructions + numInst, 1);
2588 state->prog->arb.Instructions[numInst].Opcode = OPCODE_END;
2590 state->prog->arb.NumInstructions++;
2592 state->prog->arb.NumParameters = state->prog->Parameters->NumParameters;
2593 state->prog->arb.NumAttributes =
2594 _mesa_bitcount_64(state->prog->info.inputs_read);
2597 * Initialize native counts to logical counts. The device driver may
2598 * change them if program is translated into a hardware program.
2600 state->prog->arb.NumNativeInstructions = state->prog->arb.NumInstructions;
2601 state->prog->arb.NumNativeTemporaries = state->prog->arb.NumTemporaries;
2602 state->prog->arb.NumNativeParameters = state->prog->arb.NumParameters;
2603 state->prog->arb.NumNativeAttributes = state->prog->arb.NumAttributes;
2604 state->prog->arb.NumNativeAddressRegs = state->prog->arb.NumAddressRegs;
2609 for (inst = state->inst_head; inst != NULL; inst = temp) {
2614 state->inst_head = NULL;
2615 state->inst_tail = NULL;
2617 for (sym = state->sym; sym != NULL; sym = temp) {
2620 free((void *) sym->name);
2625 _mesa_symbol_table_dtor(state->st);