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