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->NumInstructions++;
354 | namingStatement ';'
357 instruction: ALU_instruction
360 state->prog->NumAluInstructions++;
365 state->prog->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 if ($1.Opcode == OPCODE_DDY)
392 state->fragment.UsesDFdy = 1;
393 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
397 SCALARop_instruction: SCALAR_OP maskedDstReg ',' scalarSrcReg
399 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
403 BINSCop_instruction: BINSC_OP maskedDstReg ',' scalarSrcReg ',' scalarSrcReg
405 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
410 BINop_instruction: BIN_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg
412 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, NULL);
416 TRIop_instruction: TRI_OP maskedDstReg ','
417 swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg
419 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
423 SAMPLE_instruction: SAMPLE_OP maskedDstReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
425 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
427 const GLbitfield tex_mask = (1U << $6);
428 GLbitfield shadow_tex = 0;
429 GLbitfield target_mask = 0;
432 $$->Base.TexSrcUnit = $6;
435 shadow_tex = tex_mask;
437 $$->Base.TexSrcTarget = -$8;
438 $$->Base.TexShadow = 1;
440 $$->Base.TexSrcTarget = $8;
443 target_mask = (1U << $$->Base.TexSrcTarget);
445 /* If this texture unit was previously accessed and that access
446 * had a different texture target, generate an error.
448 * If this texture unit was previously accessed and that access
449 * had a different shadow mode, generate an error.
451 if ((state->prog->TexturesUsed[$6] != 0)
452 && ((state->prog->TexturesUsed[$6] != target_mask)
453 || ((state->prog->ShadowSamplers & tex_mask)
456 "multiple targets used on one texture image unit");
461 state->prog->TexturesUsed[$6] |= target_mask;
462 state->prog->ShadowSamplers |= shadow_tex;
467 KIL_instruction: KIL swizzleSrcReg
469 $$ = asm_instruction_ctor(OPCODE_KIL, NULL, & $2, NULL, NULL);
470 state->fragment.UsesKill = 1;
474 TXD_instruction: TXD_OP maskedDstReg ',' swizzleSrcReg ',' swizzleSrcReg ',' swizzleSrcReg ',' texImageUnit ',' texTarget
476 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, & $6, & $8);
478 const GLbitfield tex_mask = (1U << $10);
479 GLbitfield shadow_tex = 0;
480 GLbitfield target_mask = 0;
483 $$->Base.TexSrcUnit = $10;
486 shadow_tex = tex_mask;
488 $$->Base.TexSrcTarget = -$12;
489 $$->Base.TexShadow = 1;
491 $$->Base.TexSrcTarget = $12;
494 target_mask = (1U << $$->Base.TexSrcTarget);
496 /* If this texture unit was previously accessed and that access
497 * had a different texture target, generate an error.
499 * If this texture unit was previously accessed and that access
500 * had a different shadow mode, generate an error.
502 if ((state->prog->TexturesUsed[$10] != 0)
503 && ((state->prog->TexturesUsed[$10] != target_mask)
504 || ((state->prog->ShadowSamplers & tex_mask)
506 yyerror(& @12, state,
507 "multiple targets used on one texture image unit");
512 state->prog->TexturesUsed[$10] |= target_mask;
513 state->prog->ShadowSamplers |= shadow_tex;
518 texImageUnit: TEXTURE_UNIT optTexImageUnitNum
524 texTarget: TEX_1D { $$ = TEXTURE_1D_INDEX; }
525 | TEX_2D { $$ = TEXTURE_2D_INDEX; }
526 | TEX_3D { $$ = TEXTURE_3D_INDEX; }
527 | TEX_CUBE { $$ = TEXTURE_CUBE_INDEX; }
528 | TEX_RECT { $$ = TEXTURE_RECT_INDEX; }
529 | TEX_SHADOW1D { $$ = -TEXTURE_1D_INDEX; }
530 | TEX_SHADOW2D { $$ = -TEXTURE_2D_INDEX; }
531 | TEX_SHADOWRECT { $$ = -TEXTURE_RECT_INDEX; }
532 | TEX_ARRAY1D { $$ = TEXTURE_1D_ARRAY_INDEX; }
533 | TEX_ARRAY2D { $$ = TEXTURE_2D_ARRAY_INDEX; }
534 | TEX_ARRAYSHADOW1D { $$ = -TEXTURE_1D_ARRAY_INDEX; }
535 | TEX_ARRAYSHADOW2D { $$ = -TEXTURE_2D_ARRAY_INDEX; }
538 SWZ_instruction: SWZ maskedDstReg ',' srcReg ',' extendedSwizzle
540 /* FIXME: Is this correct? Should the extenedSwizzle be applied
541 * FIXME: to the existing swizzle?
543 $4.Base.Swizzle = $6.swizzle;
544 $4.Base.Negate = $6.mask;
546 $$ = asm_instruction_copy_ctor(& $1, & $2, & $4, NULL, NULL);
550 scalarSrcReg: optionalSign scalarUse
555 $$.Base.Negate = ~$$.Base.Negate;
560 scalarUse: srcReg scalarSuffix
564 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
567 | paramConstScalarUse
569 struct asm_symbol temp_sym;
571 if (!state->option.NV_fragment) {
572 yyerror(& @1, state, "expected scalar suffix");
576 memset(& temp_sym, 0, sizeof(temp_sym));
577 temp_sym.param_binding_begin = ~0;
578 initialize_symbol_from_const(state->prog, & temp_sym, & $1, GL_TRUE);
580 set_src_reg_swz(& $$, PROGRAM_CONSTANT,
581 temp_sym.param_binding_begin,
582 temp_sym.param_binding_swizzle);
586 swizzleSrcReg: optionalSign srcReg swizzleSuffix
591 $$.Base.Negate = ~$$.Base.Negate;
594 $$.Base.Swizzle = _mesa_combine_swizzles($$.Base.Swizzle,
599 maskedDstReg: dstReg optionalMask
602 $$.WriteMask = $2.mask;
604 if ($$.File == PROGRAM_OUTPUT) {
605 /* Technically speaking, this should check that it is in
606 * vertex program mode. However, PositionInvariant can never be
607 * set in fragment program mode, so it is somewhat irrelevant.
609 if (state->option.PositionInvariant
610 && ($$.Index == VARYING_SLOT_POS)) {
611 yyerror(& @1, state, "position-invariant programs cannot "
616 state->prog->OutputsWritten |= BITFIELD64_BIT($$.Index);
621 maskedAddrReg: addrReg addrWriteMask
623 set_dst_reg(& $$, PROGRAM_ADDRESS, 0);
624 $$.WriteMask = $2.mask;
628 extendedSwizzle: extSwizComp ',' extSwizComp ',' extSwizComp ',' extSwizComp
630 const unsigned xyzw_valid =
632 | ($3.xyzw_valid << 1)
633 | ($5.xyzw_valid << 2)
634 | ($7.xyzw_valid << 3);
635 const unsigned rgba_valid =
637 | ($3.rgba_valid << 1)
638 | ($5.rgba_valid << 2)
639 | ($7.rgba_valid << 3);
641 /* All of the swizzle components have to be valid in either RGBA
642 * or XYZW. Note that 0 and 1 are valid in both, so both masks
643 * can have some bits set.
645 * We somewhat deviate from the spec here. It would be really hard
646 * to figure out which component is the error, and there probably
647 * isn't a lot of benefit.
649 if ((rgba_valid != 0x0f) && (xyzw_valid != 0x0f)) {
650 yyerror(& @1, state, "cannot combine RGBA and XYZW swizzle "
655 $$.swizzle = MAKE_SWIZZLE4($1.swz, $3.swz, $5.swz, $7.swz);
656 $$.mask = ($1.negate) | ($3.negate << 1) | ($5.negate << 2)
661 extSwizComp: optionalSign extSwizSel
664 $$.negate = ($1) ? 1 : 0;
670 if (($1 != 0) && ($1 != 1)) {
671 yyerror(& @1, state, "invalid extended swizzle selector");
675 $$.swz = ($1 == 0) ? SWIZZLE_ZERO : SWIZZLE_ONE;
678 /* 0 and 1 are valid for both RGBA swizzle names and XYZW
688 if (strlen($1) > 1) {
689 yyerror(& @1, state, "invalid extended swizzle selector");
736 yyerror(& @1, state, "invalid extended swizzle selector");
743 srcReg: USED_IDENTIFIER /* temporaryReg | progParamSingle */
745 struct asm_symbol *const s = (struct asm_symbol *)
746 _mesa_symbol_table_find_symbol(state->st, 0, $1);
751 yyerror(& @1, state, "invalid operand variable");
753 } else if ((s->type != at_param) && (s->type != at_temp)
754 && (s->type != at_attrib)) {
755 yyerror(& @1, state, "invalid operand variable");
757 } else if ((s->type == at_param) && s->param_is_array) {
758 yyerror(& @1, state, "non-array access to array PARAM");
765 set_src_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding);
768 set_src_reg_swz(& $$, s->param_binding_type,
769 s->param_binding_begin,
770 s->param_binding_swizzle);
773 set_src_reg(& $$, PROGRAM_INPUT, s->attrib_binding);
774 state->prog->InputsRead |= BITFIELD64_BIT($$.Base.Index);
776 if (!validate_inputs(& @1, state)) {
788 set_src_reg(& $$, PROGRAM_INPUT, $1);
789 state->prog->InputsRead |= BITFIELD64_BIT($$.Base.Index);
791 if (!validate_inputs(& @1, state)) {
795 | progParamArray '[' progParamArrayMem ']'
797 if (! $3.Base.RelAddr
798 && ((unsigned) $3.Base.Index >= $1->param_binding_length)) {
799 yyerror(& @3, state, "out of bounds array access");
804 $$.Base.File = $1->param_binding_type;
806 if ($3.Base.RelAddr) {
807 state->prog->IndirectRegisterFiles |= (1 << $$.Base.File);
808 $1->param_accessed_indirectly = 1;
811 $$.Base.Index = $3.Base.Index;
814 $$.Base.Index = $1->param_binding_begin + $3.Base.Index;
819 gl_register_file file = ($1.name != NULL)
820 ? $1.param_binding_type
822 set_src_reg_swz(& $$, file, $1.param_binding_begin,
823 $1.param_binding_swizzle);
827 dstReg: resultBinding
829 set_dst_reg(& $$, PROGRAM_OUTPUT, $1);
831 | USED_IDENTIFIER /* temporaryReg | vertexResultReg */
833 struct asm_symbol *const s = (struct asm_symbol *)
834 _mesa_symbol_table_find_symbol(state->st, 0, $1);
839 yyerror(& @1, state, "invalid operand variable");
841 } else if ((s->type != at_output) && (s->type != at_temp)) {
842 yyerror(& @1, state, "invalid operand variable");
848 set_dst_reg(& $$, PROGRAM_TEMPORARY, s->temp_binding);
851 set_dst_reg(& $$, PROGRAM_OUTPUT, s->output_binding);
854 set_dst_reg(& $$, s->param_binding_type, s->param_binding_begin);
860 progParamArray: USED_IDENTIFIER
862 struct asm_symbol *const s = (struct asm_symbol *)
863 _mesa_symbol_table_find_symbol(state->st, 0, $1);
868 yyerror(& @1, state, "invalid operand variable");
870 } else if ((s->type != at_param) || !s->param_is_array) {
871 yyerror(& @1, state, "array access to non-PARAM variable");
879 progParamArrayMem: progParamArrayAbs | progParamArrayRel;
881 progParamArrayAbs: INTEGER
888 progParamArrayRel: addrReg addrComponent addrRegRelOffset
890 /* FINISHME: Add support for multiple address registers.
892 /* FINISHME: Add support for 4-component address registers.
900 addrRegRelOffset: { $$ = 0; }
901 | '+' addrRegPosOffset { $$ = $2; }
902 | '-' addrRegNegOffset { $$ = -$2; }
905 addrRegPosOffset: INTEGER
907 if (($1 < 0) || ($1 > (state->limits->MaxAddressOffset - 1))) {
909 _mesa_snprintf(s, sizeof(s),
910 "relative address offset too large (%d)", $1);
911 yyerror(& @1, state, s);
919 addrRegNegOffset: INTEGER
921 if (($1 < 0) || ($1 > state->limits->MaxAddressOffset)) {
923 _mesa_snprintf(s, sizeof(s),
924 "relative address offset too large (%d)", $1);
925 yyerror(& @1, state, s);
933 addrReg: USED_IDENTIFIER
935 struct asm_symbol *const s = (struct asm_symbol *)
936 _mesa_symbol_table_find_symbol(state->st, 0, $1);
941 yyerror(& @1, state, "invalid array member");
943 } else if (s->type != at_address) {
945 "invalid variable for indexed array access");
955 if ($1.mask != WRITEMASK_X) {
956 yyerror(& @1, state, "invalid address component selector");
966 if ($1.mask != WRITEMASK_X) {
968 "address register write mask must be \".x\"");
981 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
984 optionalMask: MASK4 | MASK3 | MASK2 | MASK1
985 | { $$.swizzle = SWIZZLE_NOOP; $$.mask = WRITEMASK_XYZW; }
988 namingStatement: ATTRIB_statement
996 ATTRIB_statement: ATTRIB IDENTIFIER '=' attribBinding
998 struct asm_symbol *const s =
999 declare_variable(state, $2, at_attrib, & @2);
1005 s->attrib_binding = $4;
1006 state->InputsBound |= BITFIELD64_BIT(s->attrib_binding);
1008 if (!validate_inputs(& @4, state)) {
1015 attribBinding: VERTEX vtxAttribItem
1019 | FRAGMENT fragAttribItem
1025 vtxAttribItem: POSITION
1027 $$ = VERT_ATTRIB_POS;
1029 | WEIGHT vtxOptWeightNum
1031 $$ = VERT_ATTRIB_WEIGHT;
1035 $$ = VERT_ATTRIB_NORMAL;
1037 | COLOR optColorType
1039 $$ = VERT_ATTRIB_COLOR0 + $2;
1043 $$ = VERT_ATTRIB_FOG;
1045 | TEXCOORD optTexCoordUnitNum
1047 $$ = VERT_ATTRIB_TEX0 + $2;
1049 | MATRIXINDEX '[' vtxWeightNum ']'
1051 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1054 | VTXATTRIB '[' vtxAttribNum ']'
1056 $$ = VERT_ATTRIB_GENERIC0 + $3;
1060 vtxAttribNum: INTEGER
1062 if ((unsigned) $1 >= state->limits->MaxAttribs) {
1063 yyerror(& @1, state, "invalid vertex attribute reference");
1071 vtxOptWeightNum: | '[' vtxWeightNum ']';
1072 vtxWeightNum: INTEGER;
1074 fragAttribItem: POSITION
1076 $$ = VARYING_SLOT_POS;
1078 | COLOR optColorType
1080 $$ = VARYING_SLOT_COL0 + $2;
1084 $$ = VARYING_SLOT_FOGC;
1086 | TEXCOORD optTexCoordUnitNum
1088 $$ = VARYING_SLOT_TEX0 + $2;
1092 PARAM_statement: PARAM_singleStmt | PARAM_multipleStmt;
1094 PARAM_singleStmt: PARAM IDENTIFIER paramSingleInit
1096 struct asm_symbol *const s =
1097 declare_variable(state, $2, at_param, & @2);
1103 s->param_binding_type = $3.param_binding_type;
1104 s->param_binding_begin = $3.param_binding_begin;
1105 s->param_binding_length = $3.param_binding_length;
1106 s->param_binding_swizzle = $3.param_binding_swizzle;
1107 s->param_is_array = 0;
1112 PARAM_multipleStmt: PARAM IDENTIFIER '[' optArraySize ']' paramMultipleInit
1114 if (($4 != 0) && ((unsigned) $4 != $6.param_binding_length)) {
1116 yyerror(& @4, state,
1117 "parameter array size and number of bindings must match");
1120 struct asm_symbol *const s =
1121 declare_variable(state, $2, $6.type, & @2);
1127 s->param_binding_type = $6.param_binding_type;
1128 s->param_binding_begin = $6.param_binding_begin;
1129 s->param_binding_length = $6.param_binding_length;
1130 s->param_binding_swizzle = SWIZZLE_XYZW;
1131 s->param_is_array = 1;
1143 if (($1 < 1) || ((unsigned) $1 > state->limits->MaxParameters)) {
1145 _mesa_snprintf(msg, sizeof(msg),
1146 "invalid parameter array size (size=%d max=%u)",
1147 $1, state->limits->MaxParameters);
1148 yyerror(& @1, state, msg);
1156 paramSingleInit: '=' paramSingleItemDecl
1162 paramMultipleInit: '=' '{' paramMultInitList '}'
1168 paramMultInitList: paramMultipleItem
1169 | paramMultInitList ',' paramMultipleItem
1171 $1.param_binding_length += $3.param_binding_length;
1176 paramSingleItemDecl: stateSingleItem
1178 memset(& $$, 0, sizeof($$));
1179 $$.param_binding_begin = ~0;
1180 initialize_symbol_from_state(state->prog, & $$, $1);
1184 memset(& $$, 0, sizeof($$));
1185 $$.param_binding_begin = ~0;
1186 initialize_symbol_from_param(state->prog, & $$, $1);
1190 memset(& $$, 0, sizeof($$));
1191 $$.param_binding_begin = ~0;
1192 initialize_symbol_from_const(state->prog, & $$, & $1, GL_TRUE);
1196 paramSingleItemUse: stateSingleItem
1198 memset(& $$, 0, sizeof($$));
1199 $$.param_binding_begin = ~0;
1200 initialize_symbol_from_state(state->prog, & $$, $1);
1204 memset(& $$, 0, sizeof($$));
1205 $$.param_binding_begin = ~0;
1206 initialize_symbol_from_param(state->prog, & $$, $1);
1210 memset(& $$, 0, sizeof($$));
1211 $$.param_binding_begin = ~0;
1212 initialize_symbol_from_const(state->prog, & $$, & $1, GL_TRUE);
1216 paramMultipleItem: stateMultipleItem
1218 memset(& $$, 0, sizeof($$));
1219 $$.param_binding_begin = ~0;
1220 initialize_symbol_from_state(state->prog, & $$, $1);
1222 | programMultipleItem
1224 memset(& $$, 0, sizeof($$));
1225 $$.param_binding_begin = ~0;
1226 initialize_symbol_from_param(state->prog, & $$, $1);
1230 memset(& $$, 0, sizeof($$));
1231 $$.param_binding_begin = ~0;
1232 initialize_symbol_from_const(state->prog, & $$, & $1, GL_FALSE);
1236 stateMultipleItem: stateSingleItem { memcpy($$, $1, sizeof($$)); }
1237 | STATE stateMatrixRows { memcpy($$, $2, sizeof($$)); }
1240 stateSingleItem: STATE stateMaterialItem { memcpy($$, $2, sizeof($$)); }
1241 | STATE stateLightItem { memcpy($$, $2, sizeof($$)); }
1242 | STATE stateLightModelItem { memcpy($$, $2, sizeof($$)); }
1243 | STATE stateLightProdItem { memcpy($$, $2, sizeof($$)); }
1244 | STATE stateTexGenItem { memcpy($$, $2, sizeof($$)); }
1245 | STATE stateTexEnvItem { memcpy($$, $2, sizeof($$)); }
1246 | STATE stateFogItem { memcpy($$, $2, sizeof($$)); }
1247 | STATE stateClipPlaneItem { memcpy($$, $2, sizeof($$)); }
1248 | STATE statePointItem { memcpy($$, $2, sizeof($$)); }
1249 | STATE stateMatrixRow { memcpy($$, $2, sizeof($$)); }
1250 | STATE stateDepthItem { memcpy($$, $2, sizeof($$)); }
1253 stateMaterialItem: MATERIAL optFaceType stateMatProperty
1255 memset($$, 0, sizeof($$));
1256 $$[0] = STATE_MATERIAL;
1262 stateMatProperty: ambDiffSpecProperty
1268 $$ = STATE_EMISSION;
1272 $$ = STATE_SHININESS;
1276 stateLightItem: LIGHT '[' stateLightNumber ']' stateLightProperty
1278 memset($$, 0, sizeof($$));
1279 $$[0] = STATE_LIGHT;
1285 stateLightProperty: ambDiffSpecProperty
1291 $$ = STATE_POSITION;
1295 if (!state->ctx->Extensions.EXT_point_parameters) {
1296 yyerror(& @1, state, "GL_ARB_point_parameters not supported");
1300 $$ = STATE_ATTENUATION;
1302 | SPOT stateSpotProperty
1308 $$ = STATE_HALF_VECTOR;
1312 stateSpotProperty: DIRECTION
1314 $$ = STATE_SPOT_DIRECTION;
1318 stateLightModelItem: LIGHTMODEL stateLModProperty
1325 stateLModProperty: AMBIENT
1327 memset($$, 0, sizeof($$));
1328 $$[0] = STATE_LIGHTMODEL_AMBIENT;
1330 | optFaceType SCENECOLOR
1332 memset($$, 0, sizeof($$));
1333 $$[0] = STATE_LIGHTMODEL_SCENECOLOR;
1338 stateLightProdItem: LIGHTPROD '[' stateLightNumber ']' optFaceType stateLProdProperty
1340 memset($$, 0, sizeof($$));
1341 $$[0] = STATE_LIGHTPROD;
1348 stateLProdProperty: ambDiffSpecProperty;
1350 stateTexEnvItem: TEXENV optLegacyTexUnitNum stateTexEnvProperty
1352 memset($$, 0, sizeof($$));
1358 stateTexEnvProperty: COLOR
1360 $$ = STATE_TEXENV_COLOR;
1364 ambDiffSpecProperty: AMBIENT
1374 $$ = STATE_SPECULAR;
1378 stateLightNumber: INTEGER
1380 if ((unsigned) $1 >= state->MaxLights) {
1381 yyerror(& @1, state, "invalid light selector");
1389 stateTexGenItem: TEXGEN optTexCoordUnitNum stateTexGenType stateTexGenCoord
1391 memset($$, 0, sizeof($$));
1392 $$[0] = STATE_TEXGEN;
1398 stateTexGenType: EYE
1400 $$ = STATE_TEXGEN_EYE_S;
1404 $$ = STATE_TEXGEN_OBJECT_S;
1407 stateTexGenCoord: TEXGEN_S
1409 $$ = STATE_TEXGEN_EYE_S - STATE_TEXGEN_EYE_S;
1413 $$ = STATE_TEXGEN_EYE_T - STATE_TEXGEN_EYE_S;
1417 $$ = STATE_TEXGEN_EYE_R - STATE_TEXGEN_EYE_S;
1421 $$ = STATE_TEXGEN_EYE_Q - STATE_TEXGEN_EYE_S;
1425 stateFogItem: FOG stateFogProperty
1427 memset($$, 0, sizeof($$));
1432 stateFogProperty: COLOR
1434 $$ = STATE_FOG_COLOR;
1438 $$ = STATE_FOG_PARAMS;
1442 stateClipPlaneItem: CLIP '[' stateClipPlaneNum ']' PLANE
1444 memset($$, 0, sizeof($$));
1445 $$[0] = STATE_CLIPPLANE;
1450 stateClipPlaneNum: INTEGER
1452 if ((unsigned) $1 >= state->MaxClipPlanes) {
1453 yyerror(& @1, state, "invalid clip plane selector");
1461 statePointItem: POINT_TOK statePointProperty
1463 memset($$, 0, sizeof($$));
1468 statePointProperty: SIZE_TOK
1470 $$ = STATE_POINT_SIZE;
1474 $$ = STATE_POINT_ATTENUATION;
1478 stateMatrixRow: stateMatrixItem ROW '[' stateMatrixRowNum ']'
1488 stateMatrixRows: stateMatrixItem optMatrixRows
1503 | ROW '[' stateMatrixRowNum DOT_DOT stateMatrixRowNum ']'
1505 /* It seems logical that the matrix row range specifier would have
1506 * to specify a range or more than one row (i.e., $5 > $3).
1507 * However, the ARB_vertex_program spec says "a program will fail
1508 * to load if <a> is greater than <b>." This means that $3 == $5
1512 yyerror(& @3, state, "invalid matrix row range");
1521 stateMatrixItem: MATRIX stateMatrixName stateOptMatModifier
1529 stateOptMatModifier:
1539 stateMatModifier: INVERSE
1541 $$ = STATE_MATRIX_INVERSE;
1545 $$ = STATE_MATRIX_TRANSPOSE;
1549 $$ = STATE_MATRIX_INVTRANS;
1553 stateMatrixRowNum: INTEGER
1556 yyerror(& @1, state, "invalid matrix row reference");
1564 stateMatrixName: MODELVIEW stateOptModMatNum
1566 $$[0] = STATE_MODELVIEW_MATRIX;
1571 $$[0] = STATE_PROJECTION_MATRIX;
1576 $$[0] = STATE_MVP_MATRIX;
1579 | TEXTURE optTexCoordUnitNum
1581 $$[0] = STATE_TEXTURE_MATRIX;
1584 | PALETTE '[' statePaletteMatNum ']'
1586 yyerror(& @1, state, "GL_ARB_matrix_palette not supported");
1589 | MAT_PROGRAM '[' stateProgramMatNum ']'
1591 $$[0] = STATE_PROGRAM_MATRIX;
1600 | '[' stateModMatNum ']'
1605 stateModMatNum: INTEGER
1607 /* Since GL_ARB_vertex_blend isn't supported, only modelview matrix
1611 yyerror(& @1, state, "invalid modelview matrix index");
1618 statePaletteMatNum: INTEGER
1620 /* Since GL_ARB_matrix_palette isn't supported, just let any value
1621 * through here. The error will be generated later.
1626 stateProgramMatNum: INTEGER
1628 if ((unsigned) $1 >= state->MaxProgramMatrices) {
1629 yyerror(& @1, state, "invalid program matrix selector");
1637 stateDepthItem: DEPTH RANGE
1639 memset($$, 0, sizeof($$));
1640 $$[0] = STATE_DEPTH_RANGE;
1645 programSingleItem: progEnvParam | progLocalParam;
1647 programMultipleItem: progEnvParams | progLocalParams;
1649 progEnvParams: PROGRAM ENV '[' progEnvParamNums ']'
1651 memset($$, 0, sizeof($$));
1652 $$[0] = state->state_param_enum;
1659 progEnvParamNums: progEnvParamNum
1664 | progEnvParamNum DOT_DOT progEnvParamNum
1671 progEnvParam: PROGRAM ENV '[' progEnvParamNum ']'
1673 memset($$, 0, sizeof($$));
1674 $$[0] = state->state_param_enum;
1681 progLocalParams: PROGRAM LOCAL '[' progLocalParamNums ']'
1683 memset($$, 0, sizeof($$));
1684 $$[0] = state->state_param_enum;
1685 $$[1] = STATE_LOCAL;
1690 progLocalParamNums: progLocalParamNum
1695 | progLocalParamNum DOT_DOT progLocalParamNum
1702 progLocalParam: PROGRAM LOCAL '[' progLocalParamNum ']'
1704 memset($$, 0, sizeof($$));
1705 $$[0] = state->state_param_enum;
1706 $$[1] = STATE_LOCAL;
1712 progEnvParamNum: INTEGER
1714 if ((unsigned) $1 >= state->limits->MaxEnvParams) {
1715 yyerror(& @1, state, "invalid environment parameter reference");
1722 progLocalParamNum: INTEGER
1724 if ((unsigned) $1 >= state->limits->MaxLocalParams) {
1725 yyerror(& @1, state, "invalid local parameter reference");
1734 paramConstDecl: paramConstScalarDecl | paramConstVector;
1735 paramConstUse: paramConstScalarUse | paramConstVector;
1737 paramConstScalarDecl: signedFloatConstant
1747 paramConstScalarUse: REAL
1758 $$.data[0].f = (float) $1;
1759 $$.data[1].f = (float) $1;
1760 $$.data[2].f = (float) $1;
1761 $$.data[3].f = (float) $1;
1765 paramConstVector: '{' signedFloatConstant '}'
1769 $$.data[1].f = 0.0f;
1770 $$.data[2].f = 0.0f;
1771 $$.data[3].f = 1.0f;
1773 | '{' signedFloatConstant ',' signedFloatConstant '}'
1778 $$.data[2].f = 0.0f;
1779 $$.data[3].f = 1.0f;
1781 | '{' signedFloatConstant ',' signedFloatConstant ','
1782 signedFloatConstant '}'
1788 $$.data[3].f = 1.0f;
1790 | '{' signedFloatConstant ',' signedFloatConstant ','
1791 signedFloatConstant ',' signedFloatConstant '}'
1801 signedFloatConstant: optionalSign REAL
1803 $$ = ($1) ? -$2 : $2;
1805 | optionalSign INTEGER
1807 $$ = (float)(($1) ? -$2 : $2);
1811 optionalSign: '+' { $$ = FALSE; }
1812 | '-' { $$ = TRUE; }
1816 TEMP_statement: TEMP { $<integer>$ = $1; } varNameList
1819 ADDRESS_statement: ADDRESS { $<integer>$ = $1; } varNameList
1822 varNameList: varNameList ',' IDENTIFIER
1824 if (!declare_variable(state, $3, $<integer>0, & @3)) {
1831 if (!declare_variable(state, $1, $<integer>0, & @1)) {
1838 OUTPUT_statement: OUTPUT IDENTIFIER '=' resultBinding
1840 struct asm_symbol *const s =
1841 declare_variable(state, $2, at_output, & @2);
1847 s->output_binding = $4;
1852 resultBinding: RESULT POSITION
1854 if (state->mode == ARB_vertex) {
1855 $$ = VARYING_SLOT_POS;
1857 yyerror(& @2, state, "invalid program result name");
1863 if (state->mode == ARB_vertex) {
1864 $$ = VARYING_SLOT_FOGC;
1866 yyerror(& @2, state, "invalid program result name");
1870 | RESULT resultColBinding
1876 if (state->mode == ARB_vertex) {
1877 $$ = VARYING_SLOT_PSIZ;
1879 yyerror(& @2, state, "invalid program result name");
1883 | RESULT TEXCOORD optTexCoordUnitNum
1885 if (state->mode == ARB_vertex) {
1886 $$ = VARYING_SLOT_TEX0 + $3;
1888 yyerror(& @2, state, "invalid program result name");
1894 if (state->mode == ARB_fragment) {
1895 $$ = FRAG_RESULT_DEPTH;
1897 yyerror(& @2, state, "invalid program result name");
1903 resultColBinding: COLOR optResultFaceType optResultColorType
1911 if (state->mode == ARB_vertex) {
1912 $$ = VARYING_SLOT_COL0;
1914 if (state->option.DrawBuffers)
1915 $$ = FRAG_RESULT_DATA0;
1917 $$ = FRAG_RESULT_COLOR;
1922 if (state->mode == ARB_vertex) {
1923 yyerror(& @1, state, "invalid program result name");
1926 if (!state->option.DrawBuffers) {
1927 /* From the ARB_draw_buffers spec (same text exists
1928 * for ATI_draw_buffers):
1930 * If this option is not specified, a fragment
1931 * program that attempts to bind
1932 * "result.color[n]" will fail to load, and only
1933 * "result.color" will be allowed.
1935 yyerror(& @1, state,
1936 "result.color[] used without "
1937 "`OPTION ARB_draw_buffers' or "
1938 "`OPTION ATI_draw_buffers'");
1940 } else if ($2 >= state->MaxDrawBuffers) {
1941 yyerror(& @1, state,
1942 "result.color[] exceeds MAX_DRAW_BUFFERS_ARB");
1945 $$ = FRAG_RESULT_DATA0 + $2;
1950 if (state->mode == ARB_vertex) {
1951 $$ = VARYING_SLOT_COL0;
1953 yyerror(& @1, state, "invalid program result name");
1959 if (state->mode == ARB_vertex) {
1960 $$ = VARYING_SLOT_BFC0;
1962 yyerror(& @1, state, "invalid program result name");
1974 if (state->mode == ARB_vertex) {
1977 yyerror(& @1, state, "invalid program result name");
1983 if (state->mode == ARB_vertex) {
1986 yyerror(& @1, state, "invalid program result name");
1992 optFaceType: { $$ = 0; }
1997 optColorType: { $$ = 0; }
1998 | PRIMARY { $$ = 0; }
1999 | SECONDARY { $$ = 1; }
2002 optTexCoordUnitNum: { $$ = 0; }
2003 | '[' texCoordUnitNum ']' { $$ = $2; }
2006 optTexImageUnitNum: { $$ = 0; }
2007 | '[' texImageUnitNum ']' { $$ = $2; }
2010 optLegacyTexUnitNum: { $$ = 0; }
2011 | '[' legacyTexUnitNum ']' { $$ = $2; }
2014 texCoordUnitNum: INTEGER
2016 if ((unsigned) $1 >= state->MaxTextureCoordUnits) {
2017 yyerror(& @1, state, "invalid texture coordinate unit selector");
2025 texImageUnitNum: INTEGER
2027 if ((unsigned) $1 >= state->MaxTextureImageUnits) {
2028 yyerror(& @1, state, "invalid texture image unit selector");
2036 legacyTexUnitNum: INTEGER
2038 if ((unsigned) $1 >= state->MaxTextureUnits) {
2039 yyerror(& @1, state, "invalid texture unit selector");
2047 ALIAS_statement: ALIAS IDENTIFIER '=' USED_IDENTIFIER
2049 struct asm_symbol *exist = (struct asm_symbol *)
2050 _mesa_symbol_table_find_symbol(state->st, 0, $2);
2051 struct asm_symbol *target = (struct asm_symbol *)
2052 _mesa_symbol_table_find_symbol(state->st, 0, $4);
2056 if (exist != NULL) {
2058 _mesa_snprintf(m, sizeof(m), "redeclared identifier: %s", $2);
2060 yyerror(& @2, state, m);
2062 } else if (target == NULL) {
2064 yyerror(& @4, state,
2065 "undefined variable binding in ALIAS statement");
2068 _mesa_symbol_table_add_symbol(state->st, 0, $2, target);
2080 asm_instruction_set_operands(struct asm_instruction *inst,
2081 const struct prog_dst_register *dst,
2082 const struct asm_src_register *src0,
2083 const struct asm_src_register *src1,
2084 const struct asm_src_register *src2)
2086 /* In the core ARB extensions only the KIL instruction doesn't have a
2087 * destination register.
2090 init_dst_reg(& inst->Base.DstReg);
2092 inst->Base.DstReg = *dst;
2096 inst->Base.SrcReg[0] = src0->Base;
2097 inst->SrcReg[0] = *src0;
2099 init_src_reg(& inst->SrcReg[0]);
2103 inst->Base.SrcReg[1] = src1->Base;
2104 inst->SrcReg[1] = *src1;
2106 init_src_reg(& inst->SrcReg[1]);
2110 inst->Base.SrcReg[2] = src2->Base;
2111 inst->SrcReg[2] = *src2;
2113 init_src_reg(& inst->SrcReg[2]);
2118 struct asm_instruction *
2119 asm_instruction_ctor(enum prog_opcode op,
2120 const struct prog_dst_register *dst,
2121 const struct asm_src_register *src0,
2122 const struct asm_src_register *src1,
2123 const struct asm_src_register *src2)
2125 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2128 _mesa_init_instructions(& inst->Base, 1);
2129 inst->Base.Opcode = op;
2131 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2138 struct asm_instruction *
2139 asm_instruction_copy_ctor(const struct prog_instruction *base,
2140 const struct prog_dst_register *dst,
2141 const struct asm_src_register *src0,
2142 const struct asm_src_register *src1,
2143 const struct asm_src_register *src2)
2145 struct asm_instruction *inst = CALLOC_STRUCT(asm_instruction);
2148 _mesa_init_instructions(& inst->Base, 1);
2149 inst->Base.Opcode = base->Opcode;
2150 inst->Base.Saturate = base->Saturate;
2152 asm_instruction_set_operands(inst, dst, src0, src1, src2);
2160 init_dst_reg(struct prog_dst_register *r)
2162 memset(r, 0, sizeof(*r));
2163 r->File = PROGRAM_UNDEFINED;
2164 r->WriteMask = WRITEMASK_XYZW;
2168 /** Like init_dst_reg() but set the File and Index fields. */
2170 set_dst_reg(struct prog_dst_register *r, gl_register_file file, GLint index)
2172 const GLint maxIndex = 1 << INST_INDEX_BITS;
2173 const GLint minIndex = 0;
2174 assert(index >= minIndex);
2176 assert(index <= maxIndex);
2178 assert(file == PROGRAM_TEMPORARY ||
2179 file == PROGRAM_ADDRESS ||
2180 file == PROGRAM_OUTPUT);
2181 memset(r, 0, sizeof(*r));
2184 r->WriteMask = WRITEMASK_XYZW;
2189 init_src_reg(struct asm_src_register *r)
2191 memset(r, 0, sizeof(*r));
2192 r->Base.File = PROGRAM_UNDEFINED;
2193 r->Base.Swizzle = SWIZZLE_NOOP;
2198 /** Like init_src_reg() but set the File and Index fields.
2199 * \return GL_TRUE if a valid src register, GL_FALSE otherwise
2202 set_src_reg(struct asm_src_register *r, gl_register_file file, GLint index)
2204 set_src_reg_swz(r, file, index, SWIZZLE_XYZW);
2209 set_src_reg_swz(struct asm_src_register *r, gl_register_file file, GLint index,
2212 const GLint maxIndex = (1 << INST_INDEX_BITS) - 1;
2213 const GLint minIndex = -(1 << INST_INDEX_BITS);
2214 assert(file < PROGRAM_FILE_MAX);
2215 assert(index >= minIndex);
2217 assert(index <= maxIndex);
2219 memset(r, 0, sizeof(*r));
2220 r->Base.File = file;
2221 r->Base.Index = index;
2222 r->Base.Swizzle = swizzle;
2228 * Validate the set of inputs used by a program
2230 * Validates that legal sets of inputs are used by the program. In this case
2231 * "used" included both reading the input or binding the input to a name using
2232 * the \c ATTRIB command.
2235 * \c TRUE if the combination of inputs used is valid, \c FALSE otherwise.
2238 validate_inputs(struct YYLTYPE *locp, struct asm_parser_state *state)
2240 const GLbitfield64 inputs = state->prog->InputsRead | state->InputsBound;
2242 if (((inputs & VERT_BIT_FF_ALL) & (inputs >> VERT_ATTRIB_GENERIC0)) != 0) {
2243 yyerror(locp, state, "illegal use of generic attribute and name attribute");
2252 declare_variable(struct asm_parser_state *state, char *name, enum asm_type t,
2253 struct YYLTYPE *locp)
2255 struct asm_symbol *s = NULL;
2256 struct asm_symbol *exist = (struct asm_symbol *)
2257 _mesa_symbol_table_find_symbol(state->st, 0, name);
2260 if (exist != NULL) {
2261 yyerror(locp, state, "redeclared identifier");
2263 s = calloc(1, sizeof(struct asm_symbol));
2269 if (state->prog->NumTemporaries >= state->limits->MaxTemps) {
2270 yyerror(locp, state, "too many temporaries declared");
2275 s->temp_binding = state->prog->NumTemporaries;
2276 state->prog->NumTemporaries++;
2280 if (state->prog->NumAddressRegs >= state->limits->MaxAddressRegs) {
2281 yyerror(locp, state, "too many address registers declared");
2286 /* FINISHME: Add support for multiple address registers.
2288 state->prog->NumAddressRegs++;
2295 _mesa_symbol_table_add_symbol(state->st, 0, s->name, s);
2296 s->next = state->sym;
2304 int add_state_reference(struct gl_program_parameter_list *param_list,
2305 const gl_state_index tokens[STATE_LENGTH])
2307 const GLuint size = 4; /* XXX fix */
2311 name = _mesa_program_state_string(tokens);
2312 index = _mesa_add_parameter(param_list, PROGRAM_STATE_VAR, name,
2313 size, GL_NONE, NULL, tokens);
2314 param_list->StateFlags |= _mesa_program_state_flags(tokens);
2316 /* free name string here since we duplicated it in add_parameter() */
2324 initialize_symbol_from_state(struct gl_program *prog,
2325 struct asm_symbol *param_var,
2326 const gl_state_index tokens[STATE_LENGTH])
2329 gl_state_index state_tokens[STATE_LENGTH];
2332 memcpy(state_tokens, tokens, sizeof(state_tokens));
2334 param_var->type = at_param;
2335 param_var->param_binding_type = PROGRAM_STATE_VAR;
2337 /* If we are adding a STATE_MATRIX that has multiple rows, we need to
2338 * unroll it and call add_state_reference() for each row
2340 if ((state_tokens[0] == STATE_MODELVIEW_MATRIX ||
2341 state_tokens[0] == STATE_PROJECTION_MATRIX ||
2342 state_tokens[0] == STATE_MVP_MATRIX ||
2343 state_tokens[0] == STATE_TEXTURE_MATRIX ||
2344 state_tokens[0] == STATE_PROGRAM_MATRIX)
2345 && (state_tokens[2] != state_tokens[3])) {
2347 const int first_row = state_tokens[2];
2348 const int last_row = state_tokens[3];
2350 for (row = first_row; row <= last_row; row++) {
2351 state_tokens[2] = state_tokens[3] = row;
2353 idx = add_state_reference(prog->Parameters, state_tokens);
2354 if (param_var->param_binding_begin == ~0U) {
2355 param_var->param_binding_begin = idx;
2356 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2359 param_var->param_binding_length++;
2363 idx = add_state_reference(prog->Parameters, state_tokens);
2364 if (param_var->param_binding_begin == ~0U) {
2365 param_var->param_binding_begin = idx;
2366 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2368 param_var->param_binding_length++;
2376 initialize_symbol_from_param(struct gl_program *prog,
2377 struct asm_symbol *param_var,
2378 const gl_state_index tokens[STATE_LENGTH])
2381 gl_state_index state_tokens[STATE_LENGTH];
2384 memcpy(state_tokens, tokens, sizeof(state_tokens));
2386 assert((state_tokens[0] == STATE_VERTEX_PROGRAM)
2387 || (state_tokens[0] == STATE_FRAGMENT_PROGRAM));
2388 assert((state_tokens[1] == STATE_ENV)
2389 || (state_tokens[1] == STATE_LOCAL));
2392 * The param type is STATE_VAR. The program parameter entry will
2393 * effectively be a pointer into the LOCAL or ENV parameter array.
2395 param_var->type = at_param;
2396 param_var->param_binding_type = PROGRAM_STATE_VAR;
2398 /* If we are adding a STATE_ENV or STATE_LOCAL that has multiple elements,
2399 * we need to unroll it and call add_state_reference() for each row
2401 if (state_tokens[2] != state_tokens[3]) {
2403 const int first_row = state_tokens[2];
2404 const int last_row = state_tokens[3];
2406 for (row = first_row; row <= last_row; row++) {
2407 state_tokens[2] = state_tokens[3] = row;
2409 idx = add_state_reference(prog->Parameters, state_tokens);
2410 if (param_var->param_binding_begin == ~0U) {
2411 param_var->param_binding_begin = idx;
2412 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2414 param_var->param_binding_length++;
2418 idx = add_state_reference(prog->Parameters, state_tokens);
2419 if (param_var->param_binding_begin == ~0U) {
2420 param_var->param_binding_begin = idx;
2421 param_var->param_binding_swizzle = SWIZZLE_XYZW;
2423 param_var->param_binding_length++;
2431 * Put a float/vector constant/literal into the parameter list.
2432 * \param param_var returns info about the parameter/constant's location,
2433 * binding, type, etc.
2434 * \param vec the vector/constant to add
2435 * \param allowSwizzle if true, try to consolidate constants which only differ
2436 * by a swizzle. We don't want to do this when building
2437 * arrays of constants that may be indexed indirectly.
2438 * \return index of the constant in the parameter list.
2441 initialize_symbol_from_const(struct gl_program *prog,
2442 struct asm_symbol *param_var,
2443 const struct asm_vector *vec,
2444 GLboolean allowSwizzle)
2447 const int idx = _mesa_add_unnamed_constant(prog->Parameters,
2448 vec->data, vec->count,
2449 allowSwizzle ? &swizzle : NULL);
2451 param_var->type = at_param;
2452 param_var->param_binding_type = PROGRAM_CONSTANT;
2454 if (param_var->param_binding_begin == ~0U) {
2455 param_var->param_binding_begin = idx;
2456 param_var->param_binding_swizzle = allowSwizzle ? swizzle : SWIZZLE_XYZW;
2458 param_var->param_binding_length++;
2465 make_error_string(const char *fmt, ...)
2472 /* Call vsnprintf once to determine how large the final string is. Call it
2473 * again to do the actual formatting. from the vsnprintf manual page:
2475 * Upon successful return, these functions return the number of
2476 * characters printed (not including the trailing '\0' used to end
2477 * output to strings).
2479 va_start(args, fmt);
2480 length = 1 + vsnprintf(NULL, 0, fmt, args);
2483 str = malloc(length);
2485 va_start(args, fmt);
2486 vsnprintf(str, length, fmt, args);
2495 yyerror(YYLTYPE *locp, struct asm_parser_state *state, const char *s)
2500 err_str = make_error_string("glProgramStringARB(%s)\n", s);
2502 _mesa_error(state->ctx, GL_INVALID_OPERATION, "%s", err_str);
2506 err_str = make_error_string("line %u, char %u: error: %s\n",
2507 locp->first_line, locp->first_column, s);
2508 _mesa_set_program_error(state->ctx, locp->position, err_str);
2517 _mesa_parse_arb_program(struct gl_context *ctx, GLenum target, const GLubyte *str,
2518 GLsizei len, struct asm_parser_state *state)
2520 struct asm_instruction *inst;
2523 GLboolean result = GL_FALSE;
2525 struct asm_symbol *sym;
2528 state->prog->Target = target;
2529 state->prog->Parameters = _mesa_new_parameter_list();
2531 /* Make a copy of the program string and force it to be NUL-terminated.
2533 strz = (GLubyte *) malloc(len + 1);
2535 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
2538 memcpy (strz, str, len);
2541 state->prog->String = strz;
2543 state->st = _mesa_symbol_table_ctor();
2545 state->limits = (target == GL_VERTEX_PROGRAM_ARB)
2546 ? & ctx->Const.Program[MESA_SHADER_VERTEX]
2547 : & ctx->Const.Program[MESA_SHADER_FRAGMENT];
2549 state->MaxTextureImageUnits = ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits;
2550 state->MaxTextureCoordUnits = ctx->Const.MaxTextureCoordUnits;
2551 state->MaxTextureUnits = ctx->Const.MaxTextureUnits;
2552 state->MaxClipPlanes = ctx->Const.MaxClipPlanes;
2553 state->MaxLights = ctx->Const.MaxLights;
2554 state->MaxProgramMatrices = ctx->Const.MaxProgramMatrices;
2555 state->MaxDrawBuffers = ctx->Const.MaxDrawBuffers;
2557 state->state_param_enum = (target == GL_VERTEX_PROGRAM_ARB)
2558 ? STATE_VERTEX_PROGRAM : STATE_FRAGMENT_PROGRAM;
2560 _mesa_set_program_error(ctx, -1, NULL);
2562 _mesa_program_lexer_ctor(& state->scanner, state, (const char *) str, len);
2564 _mesa_program_lexer_dtor(state->scanner);
2567 if (ctx->Program.ErrorPos != -1) {
2571 if (! _mesa_layout_parameters(state)) {
2575 loc.first_column = 0;
2578 yyerror(& loc, state, "invalid PARAM usage");
2584 /* Add one instruction to store the "END" instruction.
2586 state->prog->Instructions =
2587 _mesa_alloc_instructions(state->prog->NumInstructions + 1);
2589 if (state->prog->Instructions == NULL) {
2593 inst = state->inst_head;
2594 for (i = 0; i < state->prog->NumInstructions; i++) {
2595 struct asm_instruction *const temp = inst->next;
2597 state->prog->Instructions[i] = inst->Base;
2601 /* Finally, tag on an OPCODE_END instruction */
2603 const GLuint numInst = state->prog->NumInstructions;
2604 _mesa_init_instructions(state->prog->Instructions + numInst, 1);
2605 state->prog->Instructions[numInst].Opcode = OPCODE_END;
2607 state->prog->NumInstructions++;
2609 state->prog->NumParameters = state->prog->Parameters->NumParameters;
2610 state->prog->NumAttributes = _mesa_bitcount_64(state->prog->InputsRead);
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->NumNativeInstructions = state->prog->NumInstructions;
2617 state->prog->NumNativeTemporaries = state->prog->NumTemporaries;
2618 state->prog->NumNativeParameters = state->prog->NumParameters;
2619 state->prog->NumNativeAttributes = state->prog->NumAttributes;
2620 state->prog->NumNativeAddressRegs = state->prog->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);