2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #define DEBUG_PARSING 0
28 * \file arbprogparse.c
29 * ARB_*_program parser core
34 Notes on program parameters, etc.
36 The instructions we emit will use six kinds of source registers:
38 PROGRAM_INPUT - input registers
39 PROGRAM_TEMPORARY - temp registers
40 PROGRAM_ADDRESS - address/indirect register
41 PROGRAM_SAMPLER - texture sampler
42 PROGRAM_CONSTANT - indexes into program->Parameters, a known constant/literal
43 PROGRAM_STATE_VAR - indexes into program->Parameters, and may actually be:
44 + a state variable, like "state.fog.color", or
45 + a pointer to a "program.local[k]" parameter, or
46 + a pointer to a "program.env[k]" parameter
48 Basically, all the program.local[] and program.env[] values will get mapped
49 into the unified gl_program->Parameters array. This solves the problem of
50 having three separate program parameter arrays.
54 #include "main/glheader.h"
55 #include "main/imports.h"
56 #include "main/context.h"
57 #include "main/macros.h"
58 #include "main/mtypes.h"
59 #include "shader/grammar/grammar_mesa.h"
60 #include "arbprogparse.h"
62 #include "programopt.h"
63 #include "prog_parameter.h"
64 #include "prog_statevars.h"
65 #include "prog_instruction.h"
66 #include "program_parser.h"
69 * This is basically a union of the vertex_program and fragment_program
70 * structs that we can use to parse the program into
72 * XXX we can probably get rid of this entirely someday.
76 struct gl_program Base
;
78 GLuint Position
; /* Just used for error reporting while parsing */
82 /* ARB_vertex_progmra options */
83 GLboolean HintPositionInvariant
;
85 /* ARB_fragment_progmra options */
86 GLenum PrecisionOption
; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
87 GLenum FogOption
; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
89 /* ARB_fragment_program specifics */
90 GLbitfield TexturesUsed
[MAX_TEXTURE_IMAGE_UNITS
];
91 GLbitfield ShadowSamplers
;
92 GLuint NumAluInstructions
;
93 GLuint NumTexInstructions
;
94 GLuint NumTexIndirections
;
102 * Fragment Program Stuff:
103 * -----------------------------------------------------
105 * - things from Michal's email
107 * + not-overflowing floats (don't use parse_integer..)
108 * + can remove range checking in arbparse.c
110 * - check all limits of number of various variables
113 * - test! test! test!
115 * Vertex Program Stuff:
116 * -----------------------------------------------------
117 * - Optimize param array usage and count limits correctly, see spec,
119 * + Record if an array is reference absolutly or relatively (or both)
120 * + For absolute arrays, store a bitmap of accesses
121 * + For single parameters, store an access flag
122 * + After parsing, make a parameter cleanup and merging pass, where
123 * relative arrays are layed out first, followed by abs arrays, and
124 * finally single state.
125 * + Remap offsets for param src and dst registers
126 * + Now we can properly count parameter usage
128 * - Multiple state binding errors in param arrays (see spec, just before
133 * -----------------------------------------------------
134 * - User clipping planes vs. PositionInvariant
135 * - Is it sufficient to just multiply by the mvp to transform in the
136 * PositionInvariant case? Or do we need something more involved?
138 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
139 * - fetch state listed in program_parameters list
140 * + WTF should this go???
141 * + currently in nvvertexec.c and s_nvfragprog.c
143 * - allow for multiple address registers (and fetch address regs properly)
146 * -----------------------------------------------------
147 * - remove any leftover unused grammer.c stuff (dict_ ?)
148 * - fix grammer.c error handling so its not static
149 * - #ifdef around stuff pertaining to extentions
151 * Outstanding Questions:
152 * -----------------------------------------------------
153 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
154 * what gets hacked off because of this:
155 * + VERTEX_ATTRIB_MATRIXINDEX
156 * + VERTEX_ATTRIB_WEIGHT
160 * - When can we fetch env/local params from their own register files, and
161 * when to we have to fetch them into the main state register file?
165 * -----------------------------------------------------
168 /* Changes since moving the file to shader directory
170 2004-III-4 ------------------------------------------------------------
171 - added #include "grammar_mesa.h"
172 - removed grammar specific code part (it resides now in grammar.c)
173 - added GL_ARB_fragment_program_shadow tokens
174 - modified #include "arbparse_syn.h"
175 - major changes inside _mesa_parse_arb_program()
176 - check the program string for '\0' characters
177 - copy the program string to a one-byte-longer location to have
179 - position invariance test (not writing to result.position) moved
183 typedef GLubyte
*production
;
187 * This is the text describing the rules to parse the grammar
189 LONGSTRING
static char arb_grammar_text
[] =
190 #include "arbprogram_syn.h"
194 * These should match up with the values defined in arbprogram.syn
199 - changed and merged V_* and F_* opcode values to OP_*.
200 - added GL_ARB_fragment_program_shadow specific tokens (michal)
202 #define REVISION 0x0a
205 #define FRAGMENT_PROGRAM 0x01
206 #define VERTEX_PROGRAM 0x02
208 /* program section */
210 #define INSTRUCTION 0x02
211 #define DECLARATION 0x03
214 /* GL_ARB_fragment_program option */
215 #define ARB_PRECISION_HINT_FASTEST 0x00
216 #define ARB_PRECISION_HINT_NICEST 0x01
217 #define ARB_FOG_EXP 0x02
218 #define ARB_FOG_EXP2 0x03
219 #define ARB_FOG_LINEAR 0x04
221 /* GL_ARB_vertex_program option */
222 #define ARB_POSITION_INVARIANT 0x05
224 /* GL_ARB_fragment_program_shadow option */
225 #define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
227 /* GL_ARB_draw_buffers option */
228 #define ARB_DRAW_BUFFERS 0x07
230 /* GL_MESA_texture_array option */
231 #define MESA_TEXTURE_ARRAY 0x08
233 /* GL_ARB_fragment_program instruction class */
234 #define OP_ALU_INST 0x00
235 #define OP_TEX_INST 0x01
237 /* GL_ARB_vertex_program instruction class */
240 /* GL_ARB_fragment_program instruction type */
241 #define OP_ALU_VECTOR 0x00
242 #define OP_ALU_SCALAR 0x01
243 #define OP_ALU_BINSC 0x02
244 #define OP_ALU_BIN 0x03
245 #define OP_ALU_TRI 0x04
246 #define OP_ALU_SWZ 0x05
247 #define OP_TEX_SAMPLE 0x06
248 #define OP_TEX_KIL 0x07
250 /* GL_ARB_vertex_program instruction type */
251 #define OP_ALU_ARL 0x08
259 /* GL_ARB_fragment_program instruction code */
261 #define OP_ABS_SAT 0x1B
263 #define OP_FLR_SAT 0x26
265 #define OP_FRC_SAT 0x27
267 #define OP_LIT_SAT 0x2A
269 #define OP_MOV_SAT 0x30
271 #define OP_COS_SAT 0x20
273 #define OP_EX2_SAT 0x25
275 #define OP_LG2_SAT 0x29
277 #define OP_RCP_SAT 0x33
279 #define OP_RSQ_SAT 0x34
281 #define OP_SIN_SAT 0x39
283 #define OP_SCS_SAT 0x36
285 #define OP_POW_SAT 0x32
287 #define OP_ADD_SAT 0x1C
289 #define OP_DP3_SAT 0x21
291 #define OP_DP4_SAT 0x22
293 #define OP_DPH_SAT 0x23
295 #define OP_DST_SAT 0x24
297 #define OP_MAX_SAT 0x2E
299 #define OP_MIN_SAT 0x2F
301 #define OP_MUL_SAT 0x31
303 #define OP_SGE_SAT 0x37
305 #define OP_SLT_SAT 0x3A
307 #define OP_SUB_SAT 0x3B
309 #define OP_XPD_SAT 0x43
311 #define OP_CMP_SAT 0x1E
313 #define OP_LRP_SAT 0x2C
315 #define OP_MAD_SAT 0x2D
317 #define OP_SWZ_SAT 0x3C
319 #define OP_TEX_SAT 0x3E
321 #define OP_TXB_SAT 0x40
323 #define OP_TXP_SAT 0x42
326 /* GL_ARB_vertex_program instruction code */
355 /* fragment attribute binding */
356 #define FRAGMENT_ATTRIB_COLOR 0x01
357 #define FRAGMENT_ATTRIB_TEXCOORD 0x02
358 #define FRAGMENT_ATTRIB_FOGCOORD 0x03
359 #define FRAGMENT_ATTRIB_POSITION 0x04
361 /* vertex attribute binding */
362 #define VERTEX_ATTRIB_POSITION 0x01
363 #define VERTEX_ATTRIB_WEIGHT 0x02
364 #define VERTEX_ATTRIB_NORMAL 0x03
365 #define VERTEX_ATTRIB_COLOR 0x04
366 #define VERTEX_ATTRIB_FOGCOORD 0x05
367 #define VERTEX_ATTRIB_TEXCOORD 0x06
368 #define VERTEX_ATTRIB_MATRIXINDEX 0x07
369 #define VERTEX_ATTRIB_GENERIC 0x08
371 /* fragment result binding */
372 #define FRAGMENT_RESULT_COLOR 0x01
373 #define FRAGMENT_RESULT_DEPTH 0x02
375 /* vertex result binding */
376 #define VERTEX_RESULT_POSITION 0x01
377 #define VERTEX_RESULT_COLOR 0x02
378 #define VERTEX_RESULT_FOGCOORD 0x03
379 #define VERTEX_RESULT_POINTSIZE 0x04
380 #define VERTEX_RESULT_TEXCOORD 0x05
383 #define TEXTARGET_1D 0x01
384 #define TEXTARGET_2D 0x02
385 #define TEXTARGET_3D 0x03
386 #define TEXTARGET_RECT 0x04
387 #define TEXTARGET_CUBE 0x05
388 /* GL_ARB_fragment_program_shadow */
389 #define TEXTARGET_SHADOW1D 0x06
390 #define TEXTARGET_SHADOW2D 0x07
391 #define TEXTARGET_SHADOWRECT 0x08
392 /* GL_MESA_texture_array */
393 #define TEXTARGET_1D_ARRAY 0x09
394 #define TEXTARGET_2D_ARRAY 0x0a
395 #define TEXTARGET_SHADOW1D_ARRAY 0x0b
396 #define TEXTARGET_SHADOW2D_ARRAY 0x0c
399 #define FACE_FRONT 0x00
400 #define FACE_BACK 0x01
403 #define COLOR_PRIMARY 0x00
404 #define COLOR_SECONDARY 0x01
407 #define COMPONENT_X 0x00
408 #define COMPONENT_Y 0x01
409 #define COMPONENT_Z 0x02
410 #define COMPONENT_W 0x03
411 #define COMPONENT_0 0x04
412 #define COMPONENT_1 0x05
414 /* array index type */
415 #define ARRAY_INDEX_ABSOLUTE 0x00
416 #define ARRAY_INDEX_RELATIVE 0x01
419 #define MATRIX_MODELVIEW 0x01
420 #define MATRIX_PROJECTION 0x02
421 #define MATRIX_MVP 0x03
422 #define MATRIX_TEXTURE 0x04
423 #define MATRIX_PALETTE 0x05
424 #define MATRIX_PROGRAM 0x06
426 /* matrix modifier */
427 #define MATRIX_MODIFIER_IDENTITY 0x00
428 #define MATRIX_MODIFIER_INVERSE 0x01
429 #define MATRIX_MODIFIER_TRANSPOSE 0x02
430 #define MATRIX_MODIFIER_INVTRANS 0x03
433 #define CONSTANT_SCALAR 0x01
434 #define CONSTANT_VECTOR 0x02
436 /* program param type */
437 #define PROGRAM_PARAM_ENV 0x01
438 #define PROGRAM_PARAM_LOCAL 0x02
441 #define REGISTER_ATTRIB 0x01
442 #define REGISTER_PARAM 0x02
443 #define REGISTER_RESULT 0x03
444 #define REGISTER_ESTABLISHED_NAME 0x04
447 #define PARAM_NULL 0x00
448 #define PARAM_ARRAY_ELEMENT 0x01
449 #define PARAM_STATE_ELEMENT 0x02
450 #define PARAM_PROGRAM_ELEMENT 0x03
451 #define PARAM_PROGRAM_ELEMENTS 0x04
452 #define PARAM_CONSTANT 0x05
454 /* param state property */
455 #define STATE_MATERIAL_PARSER 0x01
456 #define STATE_LIGHT_PARSER 0x02
457 #define STATE_LIGHT_MODEL 0x03
458 #define STATE_LIGHT_PROD 0x04
459 #define STATE_FOG 0x05
460 #define STATE_MATRIX_ROWS 0x06
461 /* GL_ARB_fragment_program */
462 #define STATE_TEX_ENV 0x07
463 #define STATE_DEPTH 0x08
464 /* GL_ARB_vertex_program */
465 #define STATE_TEX_GEN 0x09
466 #define STATE_CLIP_PLANE 0x0A
467 #define STATE_POINT 0x0B
469 /* state material property */
470 #define MATERIAL_AMBIENT 0x01
471 #define MATERIAL_DIFFUSE 0x02
472 #define MATERIAL_SPECULAR 0x03
473 #define MATERIAL_EMISSION 0x04
474 #define MATERIAL_SHININESS 0x05
476 /* state light property */
477 #define LIGHT_AMBIENT 0x01
478 #define LIGHT_DIFFUSE 0x02
479 #define LIGHT_SPECULAR 0x03
480 #define LIGHT_POSITION 0x04
481 #define LIGHT_ATTENUATION 0x05
482 #define LIGHT_HALF 0x06
483 #define LIGHT_SPOT_DIRECTION 0x07
485 /* state light model property */
486 #define LIGHT_MODEL_AMBIENT 0x01
487 #define LIGHT_MODEL_SCENECOLOR 0x02
489 /* state light product property */
490 #define LIGHT_PROD_AMBIENT 0x01
491 #define LIGHT_PROD_DIFFUSE 0x02
492 #define LIGHT_PROD_SPECULAR 0x03
494 /* state texture environment property */
495 #define TEX_ENV_COLOR 0x01
497 /* state texture generation coord property */
498 #define TEX_GEN_EYE 0x01
499 #define TEX_GEN_OBJECT 0x02
501 /* state fog property */
502 #define FOG_COLOR 0x01
503 #define FOG_PARAMS 0x02
505 /* state depth property */
506 #define DEPTH_RANGE 0x01
508 /* state point parameters property */
509 #define POINT_SIZE 0x01
510 #define POINT_ATTENUATION 0x02
518 /* GL_ARB_vertex_program */
521 /*-----------------------------------------------------------------------
522 * From here on down is the semantic checking portion
527 * Variable Table Handling functions
542 * Setting an explicit field for each of the binding properties is a bit
543 * wasteful of space, but it should be much more clear when reading later on..
547 const GLubyte
*name
; /* don't free() - no need */
549 GLuint address_binding
; /* The index of the address register we should
551 GLuint attrib_binding
; /* For type vt_attrib, see nvfragprog.h for values */
552 GLuint attrib_is_generic
; /* If the attrib was specified through a generic
554 GLuint temp_binding
; /* The index of the temp register we are to use */
555 GLuint output_binding
; /* Output/result register number */
556 struct var_cache
*alias_binding
; /* For type vt_alias, points to the var_cache entry
557 * that this is aliased to */
558 GLuint param_binding_type
; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
559 * PROGRAM_ENV_PARAM} */
560 GLuint param_binding_begin
; /* This is the offset into the program_parameter_list where
561 * the tokens representing our bound state (or constants)
563 GLuint param_binding_length
; /* This is how many entries in the the program_parameter_list
564 * we take up with our state tokens or constants. Note that
565 * this is _not_ the same as the number of param registers
566 * we eventually use */
567 GLuint swizzle
; /**< swizzle to access this variable */
568 struct var_cache
*next
;
572 var_cache_create (struct var_cache
**va
)
574 *va
= (struct var_cache
*) _mesa_malloc (sizeof (struct var_cache
));
577 (**va
).type
= vt_none
;
578 (**va
).attrib_binding
= ~0;
579 (**va
).attrib_is_generic
= 0;
580 (**va
).temp_binding
= ~0;
581 (**va
).output_binding
= ~0;
582 (**va
).param_binding_type
= ~0;
583 (**va
).param_binding_begin
= ~0;
584 (**va
).param_binding_length
= ~0;
585 (**va
).alias_binding
= NULL
;
586 (**va
).swizzle
= SWIZZLE_XYZW
;
592 var_cache_destroy (struct var_cache
**va
)
595 var_cache_destroy (&(**va
).next
);
602 var_cache_append (struct var_cache
**va
, struct var_cache
*nv
)
605 var_cache_append (&(**va
).next
, nv
);
610 static struct var_cache
*
611 var_cache_find (struct var_cache
*va
, const GLubyte
* name
)
613 /*struct var_cache *first = va;*/
616 if (!_mesa_strcmp ( (const char*) name
, (const char*) va
->name
)) {
617 if (va
->type
== vt_alias
)
618 return va
->alias_binding
;
631 * Called when an error is detected while parsing/compiling a program.
632 * Sets the ctx->Program.ErrorString field to descript and records a
633 * GL_INVALID_OPERATION error.
634 * \param position position of error in program string
635 * \param descrip verbose error description
638 program_error(GLcontext
*ctx
, GLint position
, const char *descrip
)
641 const char *prefix
= "glProgramString(", *suffix
= ")";
642 char *str
= (char *) _mesa_malloc(_mesa_strlen(descrip
) +
643 _mesa_strlen(prefix
) +
644 _mesa_strlen(suffix
) + 1);
646 _mesa_sprintf(str
, "%s%s%s", prefix
, descrip
, suffix
);
647 _mesa_error(ctx
, GL_INVALID_OPERATION
, str
);
651 _mesa_set_program_error(ctx
, position
, descrip
);
656 * As above, but with an extra string parameter for more info.
659 program_error2(GLcontext
*ctx
, GLint position
, const char *descrip
,
663 const char *prefix
= "glProgramString(", *suffix
= ")";
664 char *str
= (char *) _mesa_malloc(_mesa_strlen(descrip
) +
667 _mesa_strlen(prefix
) +
668 _mesa_strlen(suffix
) + 1);
670 _mesa_sprintf(str
, "%s%s: %s%s", prefix
, descrip
, var
, suffix
);
671 _mesa_error(ctx
, GL_INVALID_OPERATION
, str
);
676 char *str
= (char *) _mesa_malloc(_mesa_strlen(descrip
) +
678 _mesa_strlen(var
) + 1);
680 _mesa_sprintf(str
, "%s: %s", descrip
, var
);
682 _mesa_set_program_error(ctx
, position
, str
);
692 * constructs an integer from 4 GLubytes in LE format
695 parse_position (const GLubyte
** inst
)
699 value
= (GLuint
) (*(*inst
)++);
700 value
+= (GLuint
) (*(*inst
)++) * 0x100;
701 value
+= (GLuint
) (*(*inst
)++) * 0x10000;
702 value
+= (GLuint
) (*(*inst
)++) * 0x1000000;
708 * This will, given a string, lookup the string as a variable name in the
709 * var cache. If the name is found, the var cache node corresponding to the
710 * var name is returned. If it is not found, a new entry is allocated
712 * \param I Points into the binary array where the string identifier begins
713 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
714 * \return The location on the var_cache corresponding the the string starting at I
716 static struct var_cache
*
717 parse_string (const GLubyte
** inst
, struct var_cache
**vc_head
,
718 struct arb_program
*Program
, GLuint
* found
)
720 const GLubyte
*i
= *inst
;
721 struct var_cache
*va
= NULL
;
724 *inst
+= _mesa_strlen ((char *) i
) + 1;
726 va
= var_cache_find (*vc_head
, i
);
734 var_cache_create (&va
);
735 va
->name
= (const GLubyte
*) i
;
737 var_cache_append (vc_head
, va
);
743 parse_string_without_adding (const GLubyte
** inst
, struct arb_program
*Program
)
745 const GLubyte
*i
= *inst
;
748 *inst
+= _mesa_strlen ((char *) i
) + 1;
754 * \return -1 if we parse '-', return 1 otherwise
757 parse_sign (const GLubyte
** inst
)
759 /*return *(*inst)++ != '+'; */
765 else if (**inst
== '+') {
774 * parses and returns signed integer
777 parse_integer (const GLubyte
** inst
, struct arb_program
*Program
)
782 /* check if *inst points to '+' or '-'
783 * if yes, grab the sign and increment *inst
785 sign
= parse_sign (inst
);
787 /* now check if *inst points to 0
788 * if yes, increment the *inst and return the default value
795 /* parse the integer as you normally would do it */
796 value
= _mesa_atoi (parse_string_without_adding (inst
, Program
));
798 /* now, after terminating 0 there is a position
799 * to parse it - parse_position()
801 Program
->Position
= parse_position (inst
);
807 Accumulate this string of digits, and return them as
808 a large integer represented in floating point (for range).
809 If scale is not NULL, also accumulates a power-of-ten
810 integer scale factor that represents the number of digits
814 parse_float_string(const GLubyte
** inst
, struct arb_program
*Program
, GLdouble
*scale
)
816 GLdouble value
= 0.0;
817 GLdouble oscale
= 1.0;
819 if (**inst
== 0) { /* this string of digits is empty-- do nothing */
822 else { /* nonempty string-- parse out the digits */
823 while (**inst
>= '0' && **inst
<= '9') {
824 GLubyte digit
= *((*inst
)++);
825 value
= value
* 10.0 + (GLint
) (digit
- '0');
828 assert(**inst
== 0); /* integer string should end with 0 */
829 (*inst
)++; /* skip over terminating 0 */
830 Program
->Position
= parse_position(inst
); /* skip position (from integer) */
838 Parse an unsigned floating-point number from this stream of tokenized
839 characters. Example floating-point formats supported:
847 parse_float (const GLubyte
** inst
, struct arb_program
*Program
)
850 GLdouble whole
, fraction
, fracScale
= 1.0;
852 whole
= parse_float_string(inst
, Program
, 0);
853 fraction
= parse_float_string(inst
, Program
, &fracScale
);
855 /* Parse signed exponent */
856 exponent
= parse_integer(inst
, Program
); /* This is the exponent */
858 /* Assemble parts of floating-point number: */
859 return (GLfloat
) ((whole
+ fraction
/ fracScale
) *
860 _mesa_pow(10.0, (GLfloat
) exponent
));
867 parse_signed_float (const GLubyte
** inst
, struct arb_program
*Program
)
869 GLint sign
= parse_sign (inst
);
870 GLfloat value
= parse_float (inst
, Program
);
875 * This picks out a constant value from the parsed array. The constant vector is r
876 * returned in the *values array, which should be of length 4.
878 * \param values - return the vector constant values.
879 * \param size - returns the number elements in valuesOut [1..4]
882 parse_constant(const GLubyte
** inst
, GLfloat
*values
, GLint
*size
,
883 struct arb_program
*Program
,
886 GLuint components
, i
;
888 switch (*(*inst
)++) {
889 case CONSTANT_SCALAR
:
890 if (use
== GL_TRUE
) {
893 values
[2] = values
[3] = parse_float (inst
, Program
);
898 values
[2] = values
[3] = parse_signed_float (inst
, Program
);
902 case CONSTANT_VECTOR
:
903 values
[0] = values
[1] = values
[2] = 0;
905 components
= *(*inst
)++;
906 for (i
= 0; i
< components
; i
++) {
907 values
[i
] = parse_signed_float (inst
, Program
);
912 _mesa_problem(NULL
, "unexpected case in parse_constant()");
919 * \param offset The offset from the address register that we should
922 * \return 0 on sucess, 1 on error
925 parse_relative_offset(GLcontext
*ctx
, const GLubyte
**inst
,
926 struct arb_program
*Program
, GLint
*offset
)
929 *offset
= parse_integer(inst
, Program
);
934 * \param color 0 if color type is primary, 1 if color type is secondary
935 * \return 0 on sucess, 1 on error
938 parse_color_type (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
941 (void) ctx
; (void) Program
;
942 *color
= *(*inst
)++ != COLOR_PRIMARY
;
947 * Get an integer corresponding to a generic vertex attribute.
949 * \return 0 on sucess, 1 on error
952 parse_generic_attrib_num(GLcontext
*ctx
, const GLubyte
** inst
,
953 struct arb_program
*Program
, GLuint
*attrib
)
955 GLint i
= parse_integer(inst
, Program
);
957 if ((i
< 0) || (i
>= MAX_VERTEX_GENERIC_ATTRIBS
))
959 program_error(ctx
, Program
->Position
,
960 "Invalid generic vertex attribute index");
964 *attrib
= (GLuint
) i
;
971 * \param color The index of the color buffer to write into
972 * \return 0 on sucess, 1 on error
975 parse_output_color_num (GLcontext
* ctx
, const GLubyte
** inst
,
976 struct arb_program
*Program
, GLuint
* color
)
978 GLint i
= parse_integer (inst
, Program
);
980 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxDrawBuffers
)) {
982 program_error(ctx
, Program
->Position
, "Invalid draw buffer index");
992 * Validate the index of a texture coordinate
994 * \param coord The texture unit index
995 * \return 0 on sucess, 1 on error
998 parse_texcoord_num (GLcontext
* ctx
, const GLubyte
** inst
,
999 struct arb_program
*Program
, GLuint
* coord
)
1001 GLint i
= parse_integer (inst
, Program
);
1003 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxTextureCoordUnits
)) {
1004 program_error(ctx
, Program
->Position
, "Invalid texture coordinate index");
1008 *coord
= (GLuint
) i
;
1014 * Validate the index of a texture image unit
1016 * \param coord The texture unit index
1017 * \return 0 on sucess, 1 on error
1020 parse_teximage_num (GLcontext
* ctx
, const GLubyte
** inst
,
1021 struct arb_program
*Program
, GLuint
* coord
)
1023 GLint i
= parse_integer (inst
, Program
);
1025 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxTextureImageUnits
)) {
1027 _mesa_snprintf(s
, sizeof(s
), "Invalid texture image index %d (%u is max)",
1028 i
, ctx
->Const
.MaxTextureImageUnits
);
1029 program_error(ctx
, Program
->Position
, s
);
1033 *coord
= (GLuint
) i
;
1039 * \param coord The weight index
1040 * \return 0 on sucess, 1 on error
1043 parse_weight_num (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
1046 *coord
= parse_integer (inst
, Program
);
1048 if ((*coord
< 0) || (*coord
>= 1)) {
1049 program_error(ctx
, Program
->Position
, "Invalid weight index");
1057 * \param coord The clip plane index
1058 * \return 0 on sucess, 1 on error
1061 parse_clipplane_num (GLcontext
* ctx
, const GLubyte
** inst
,
1062 struct arb_program
*Program
, GLint
* coord
)
1064 *coord
= parse_integer (inst
, Program
);
1066 if ((*coord
< 0) || (*coord
>= (GLint
) ctx
->Const
.MaxClipPlanes
)) {
1067 program_error(ctx
, Program
->Position
, "Invalid clip plane index");
1076 * \return 0 on front face, 1 on back face
1079 parse_face_type (const GLubyte
** inst
)
1081 switch (*(*inst
)++) {
1093 * Given a matrix and a modifier token on the binary array, return tokens
1094 * that _mesa_fetch_state() [program.c] can understand.
1096 * \param matrix - the matrix we are talking about
1097 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
1098 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
1099 * \return 0 on sucess, 1 on failure
1102 parse_matrix (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
1103 GLint
* matrix
, GLint
* matrix_idx
, GLint
* matrix_modifier
)
1105 GLubyte mat
= *(*inst
)++;
1110 case MATRIX_MODELVIEW
:
1111 *matrix
= STATE_MODELVIEW_MATRIX
;
1112 *matrix_idx
= parse_integer (inst
, Program
);
1113 if (*matrix_idx
> 0) {
1114 program_error(ctx
, Program
->Position
,
1115 "ARB_vertex_blend not supported");
1120 case MATRIX_PROJECTION
:
1121 *matrix
= STATE_PROJECTION_MATRIX
;
1125 *matrix
= STATE_MVP_MATRIX
;
1128 case MATRIX_TEXTURE
:
1129 *matrix
= STATE_TEXTURE_MATRIX
;
1130 *matrix_idx
= parse_integer (inst
, Program
);
1131 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxTextureUnits
) {
1132 program_error(ctx
, Program
->Position
, "Invalid Texture Unit");
1133 /* bad *matrix_id */
1138 /* This is not currently supported (ARB_matrix_palette) */
1139 case MATRIX_PALETTE
:
1140 *matrix_idx
= parse_integer (inst
, Program
);
1141 program_error(ctx
, Program
->Position
,
1142 "ARB_matrix_palette not supported");
1146 case MATRIX_PROGRAM
:
1147 *matrix
= STATE_PROGRAM_MATRIX
;
1148 *matrix_idx
= parse_integer (inst
, Program
);
1149 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxProgramMatrices
) {
1150 program_error(ctx
, Program
->Position
, "Invalid Program Matrix");
1151 /* bad *matrix_idx */
1157 switch (*(*inst
)++) {
1158 case MATRIX_MODIFIER_IDENTITY
:
1159 *matrix_modifier
= 0;
1161 case MATRIX_MODIFIER_INVERSE
:
1162 *matrix_modifier
= STATE_MATRIX_INVERSE
;
1164 case MATRIX_MODIFIER_TRANSPOSE
:
1165 *matrix_modifier
= STATE_MATRIX_TRANSPOSE
;
1167 case MATRIX_MODIFIER_INVTRANS
:
1168 *matrix_modifier
= STATE_MATRIX_INVTRANS
;
1177 * This parses a state string (rather, the binary version of it) into
1178 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1180 * \param inst - the start in the binary arry to start working from
1181 * \param state_tokens - the storage for the 6-token state description
1182 * \return - 0 on sucess, 1 on error
1185 parse_state_single_item (GLcontext
* ctx
, const GLubyte
** inst
,
1186 struct arb_program
*Program
,
1187 gl_state_index state_tokens
[STATE_LENGTH
])
1189 GLubyte token
= *(*inst
)++;
1192 case STATE_MATERIAL_PARSER
:
1193 state_tokens
[0] = STATE_MATERIAL
;
1194 state_tokens
[1] = parse_face_type (inst
);
1195 switch (*(*inst
)++) {
1196 case MATERIAL_AMBIENT
:
1197 state_tokens
[2] = STATE_AMBIENT
;
1199 case MATERIAL_DIFFUSE
:
1200 state_tokens
[2] = STATE_DIFFUSE
;
1202 case MATERIAL_SPECULAR
:
1203 state_tokens
[2] = STATE_SPECULAR
;
1205 case MATERIAL_EMISSION
:
1206 state_tokens
[2] = STATE_EMISSION
;
1208 case MATERIAL_SHININESS
:
1209 state_tokens
[2] = STATE_SHININESS
;
1214 case STATE_LIGHT_PARSER
:
1215 state_tokens
[0] = STATE_LIGHT
;
1216 state_tokens
[1] = parse_integer (inst
, Program
);
1218 /* Check the value of state_tokens[1] against the # of lights */
1219 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1220 program_error(ctx
, Program
->Position
, "Invalid Light Number");
1221 /* bad state_tokens[1] */
1225 switch (*(*inst
)++) {
1227 state_tokens
[2] = STATE_AMBIENT
;
1230 state_tokens
[2] = STATE_DIFFUSE
;
1232 case LIGHT_SPECULAR
:
1233 state_tokens
[2] = STATE_SPECULAR
;
1235 case LIGHT_POSITION
:
1236 state_tokens
[2] = STATE_POSITION
;
1238 case LIGHT_ATTENUATION
:
1239 state_tokens
[2] = STATE_ATTENUATION
;
1242 state_tokens
[2] = STATE_HALF_VECTOR
;
1244 case LIGHT_SPOT_DIRECTION
:
1245 state_tokens
[2] = STATE_SPOT_DIRECTION
;
1250 case STATE_LIGHT_MODEL
:
1251 switch (*(*inst
)++) {
1252 case LIGHT_MODEL_AMBIENT
:
1253 state_tokens
[0] = STATE_LIGHTMODEL_AMBIENT
;
1255 case LIGHT_MODEL_SCENECOLOR
:
1256 state_tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
1257 state_tokens
[1] = parse_face_type (inst
);
1262 case STATE_LIGHT_PROD
:
1263 state_tokens
[0] = STATE_LIGHTPROD
;
1264 state_tokens
[1] = parse_integer (inst
, Program
);
1266 /* Check the value of state_tokens[1] against the # of lights */
1267 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1268 program_error(ctx
, Program
->Position
, "Invalid Light Number");
1269 /* bad state_tokens[1] */
1273 state_tokens
[2] = parse_face_type (inst
);
1274 switch (*(*inst
)++) {
1275 case LIGHT_PROD_AMBIENT
:
1276 state_tokens
[3] = STATE_AMBIENT
;
1278 case LIGHT_PROD_DIFFUSE
:
1279 state_tokens
[3] = STATE_DIFFUSE
;
1281 case LIGHT_PROD_SPECULAR
:
1282 state_tokens
[3] = STATE_SPECULAR
;
1289 switch (*(*inst
)++) {
1291 state_tokens
[0] = STATE_FOG_COLOR
;
1294 state_tokens
[0] = STATE_FOG_PARAMS
;
1300 state_tokens
[1] = parse_integer (inst
, Program
);
1301 switch (*(*inst
)++) {
1303 state_tokens
[0] = STATE_TEXENV_COLOR
;
1312 state_tokens
[0] = STATE_TEXGEN
;
1313 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1315 if (parse_texcoord_num (ctx
, inst
, Program
, &coord
))
1317 state_tokens
[1] = coord
;
1322 /* 0 - s, 1 - t, 2 - r, 3 - q */
1325 if (type
== TEX_GEN_EYE
) {
1328 state_tokens
[2] = STATE_TEXGEN_EYE_S
;
1331 state_tokens
[2] = STATE_TEXGEN_EYE_T
;
1334 state_tokens
[2] = STATE_TEXGEN_EYE_R
;
1337 state_tokens
[2] = STATE_TEXGEN_EYE_Q
;
1340 _mesa_problem(ctx
, "bad texgen component in "
1341 "parse_state_single_item()");
1347 state_tokens
[2] = STATE_TEXGEN_OBJECT_S
;
1350 state_tokens
[2] = STATE_TEXGEN_OBJECT_T
;
1353 state_tokens
[2] = STATE_TEXGEN_OBJECT_R
;
1356 state_tokens
[2] = STATE_TEXGEN_OBJECT_Q
;
1359 _mesa_problem(ctx
, "bad texgen component in "
1360 "parse_state_single_item()");
1367 switch (*(*inst
)++) {
1369 state_tokens
[0] = STATE_DEPTH_RANGE
;
1374 case STATE_CLIP_PLANE
:
1375 state_tokens
[0] = STATE_CLIPPLANE
;
1376 if (parse_clipplane_num (ctx
, inst
, Program
,
1377 (GLint
*) &state_tokens
[1]))
1382 switch (*(*inst
)++) {
1384 state_tokens
[0] = STATE_POINT_SIZE
;
1387 case POINT_ATTENUATION
:
1388 state_tokens
[0] = STATE_POINT_ATTENUATION
;
1393 /* XXX: I think this is the correct format for a matrix row */
1394 case STATE_MATRIX_ROWS
:
1395 if (parse_matrix(ctx
, inst
, Program
,
1396 (GLint
*) &state_tokens
[0],
1397 (GLint
*) &state_tokens
[1],
1398 (GLint
*) &state_tokens
[4]))
1401 state_tokens
[2] = parse_integer (inst
, Program
); /* The first row to grab */
1403 if ((**inst
) != 0) { /* Either the last row, 0 */
1404 state_tokens
[3] = parse_integer (inst
, Program
);
1405 if (state_tokens
[3] < state_tokens
[2]) {
1406 program_error(ctx
, Program
->Position
,
1407 "Second matrix index less than the first");
1408 /* state_tokens[4] vs. state_tokens[3] */
1413 state_tokens
[3] = state_tokens
[2];
1423 * This parses a state string (rather, the binary version of it) into
1424 * a 6-token similar for the state fetching code in program.c
1426 * One might ask, why fetch these parameters into just like you fetch
1427 * state when they are already stored in other places?
1429 * Because of array offsets -> We can stick env/local parameters in the
1430 * middle of a parameter array and then index someplace into the array
1433 * One optimization might be to only do this for the cases where the
1434 * env/local parameters end up inside of an array, and leave the
1435 * single parameters (or arrays of pure env/local pareameters) in their
1436 * respective register files.
1438 * For ENV parameters, the format is:
1439 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1440 * state_tokens[1] = STATE_ENV
1441 * state_tokens[2] = the parameter index
1443 * for LOCAL parameters, the format is:
1444 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1445 * state_tokens[1] = STATE_LOCAL
1446 * state_tokens[2] = the parameter index
1448 * \param inst - the start in the binary arry to start working from
1449 * \param state_tokens - the storage for the 6-token state description
1450 * \return - 0 on sucess, 1 on failure
1453 parse_program_single_item (GLcontext
* ctx
, const GLubyte
** inst
,
1454 struct arb_program
*Program
,
1455 gl_state_index state_tokens
[STATE_LENGTH
])
1457 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1458 state_tokens
[0] = STATE_FRAGMENT_PROGRAM
;
1460 state_tokens
[0] = STATE_VERTEX_PROGRAM
;
1463 switch (*(*inst
)++) {
1464 case PROGRAM_PARAM_ENV
:
1465 state_tokens
[1] = STATE_ENV
;
1466 state_tokens
[2] = parse_integer (inst
, Program
);
1468 /* Check state_tokens[2] against the number of ENV parameters available */
1469 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1470 (state_tokens
[2] >= (GLint
) ctx
->Const
.FragmentProgram
.MaxEnvParams
))
1472 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1473 (state_tokens
[2] >= (GLint
) ctx
->Const
.VertexProgram
.MaxEnvParams
))) {
1474 program_error(ctx
, Program
->Position
,
1475 "Invalid Program Env Parameter");
1476 /* bad state_tokens[2] */
1482 case PROGRAM_PARAM_LOCAL
:
1483 state_tokens
[1] = STATE_LOCAL
;
1484 state_tokens
[2] = parse_integer (inst
, Program
);
1486 /* Check state_tokens[2] against the number of LOCAL parameters available */
1487 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1488 (state_tokens
[2] >= (GLint
) ctx
->Const
.FragmentProgram
.MaxLocalParams
))
1490 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1491 (state_tokens
[2] >= (GLint
) ctx
->Const
.VertexProgram
.MaxLocalParams
))) {
1492 program_error(ctx
, Program
->Position
,
1493 "Invalid Program Local Parameter");
1494 /* bad state_tokens[2] */
1504 * For ARB_vertex_program, programs are not allowed to use both an explicit
1505 * vertex attribute and a generic vertex attribute corresponding to the same
1506 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1508 * This will walk our var_cache and make sure that nobody does anything fishy.
1510 * \return 0 on sucess, 1 on error
1513 generic_attrib_check(struct var_cache
*vc_head
)
1516 struct var_cache
*curr
;
1517 GLboolean explicitAttrib
[MAX_VERTEX_GENERIC_ATTRIBS
],
1518 genericAttrib
[MAX_VERTEX_GENERIC_ATTRIBS
];
1520 for (a
=0; a
<MAX_VERTEX_GENERIC_ATTRIBS
; a
++) {
1521 explicitAttrib
[a
] = GL_FALSE
;
1522 genericAttrib
[a
] = GL_FALSE
;
1527 if (curr
->type
== vt_attrib
) {
1528 if (curr
->attrib_is_generic
) {
1529 GLuint attr
= (curr
->attrib_binding
== 0)
1530 ? 0 : (curr
->attrib_binding
- VERT_ATTRIB_GENERIC0
);
1531 assert(attr
< MAX_VERTEX_GENERIC_ATTRIBS
);
1532 genericAttrib
[attr
] = GL_TRUE
;
1535 assert(curr
->attrib_binding
< MAX_VERTEX_GENERIC_ATTRIBS
);
1536 explicitAttrib
[ curr
->attrib_binding
] = GL_TRUE
;
1543 for (a
=0; a
<MAX_VERTEX_GENERIC_ATTRIBS
; a
++) {
1544 if ((explicitAttrib
[a
]) && (genericAttrib
[a
]))
1552 * This will handle the binding side of an ATTRIB var declaration
1554 * \param inputReg returns the input register index, one of the
1555 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
1556 * \return returns 0 on success, 1 on error
1559 parse_attrib_binding(GLcontext
* ctx
, const GLubyte
** inst
,
1560 struct arb_program
*Program
,
1561 GLuint
*inputReg
, GLuint
*is_generic
)
1567 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1568 switch (*(*inst
)++) {
1569 case FRAGMENT_ATTRIB_COLOR
:
1572 err
= parse_color_type (ctx
, inst
, Program
, &coord
);
1573 *inputReg
= FRAG_ATTRIB_COL0
+ coord
;
1576 case FRAGMENT_ATTRIB_TEXCOORD
:
1578 GLuint texcoord
= 0;
1579 err
= parse_texcoord_num (ctx
, inst
, Program
, &texcoord
);
1580 *inputReg
= FRAG_ATTRIB_TEX0
+ texcoord
;
1583 case FRAGMENT_ATTRIB_FOGCOORD
:
1584 *inputReg
= FRAG_ATTRIB_FOGC
;
1586 case FRAGMENT_ATTRIB_POSITION
:
1587 *inputReg
= FRAG_ATTRIB_WPOS
;
1595 switch (*(*inst
)++) {
1596 case VERTEX_ATTRIB_POSITION
:
1597 *inputReg
= VERT_ATTRIB_POS
;
1600 case VERTEX_ATTRIB_WEIGHT
:
1603 err
= parse_weight_num (ctx
, inst
, Program
, &weight
);
1604 *inputReg
= VERT_ATTRIB_WEIGHT
;
1606 /* hack for Warcraft (see bug 8060) */
1607 _mesa_warning(ctx
, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
1610 program_error(ctx
, Program
->Position
,
1611 "ARB_vertex_blend not supported");
1616 case VERTEX_ATTRIB_NORMAL
:
1617 *inputReg
= VERT_ATTRIB_NORMAL
;
1620 case VERTEX_ATTRIB_COLOR
:
1623 err
= parse_color_type (ctx
, inst
, Program
, &color
);
1625 *inputReg
= VERT_ATTRIB_COLOR1
;
1628 *inputReg
= VERT_ATTRIB_COLOR0
;
1633 case VERTEX_ATTRIB_FOGCOORD
:
1634 *inputReg
= VERT_ATTRIB_FOG
;
1637 case VERTEX_ATTRIB_TEXCOORD
:
1640 err
= parse_texcoord_num (ctx
, inst
, Program
, &unit
);
1641 *inputReg
= VERT_ATTRIB_TEX0
+ unit
;
1645 case VERTEX_ATTRIB_MATRIXINDEX
:
1646 /* Not supported at this time */
1648 const char *msg
= "ARB_palette_matrix not supported";
1649 parse_integer (inst
, Program
);
1650 program_error(ctx
, Program
->Position
, msg
);
1654 case VERTEX_ATTRIB_GENERIC
:
1657 err
= parse_generic_attrib_num(ctx
, inst
, Program
, &attrib
);
1660 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1661 * attributes do not alias the conventional vertex
1665 *inputReg
= attrib
+ VERT_ATTRIB_GENERIC0
;
1679 program_error(ctx
, Program
->Position
, "Bad attribute binding");
1687 * This translates between a binary token for an output variable type
1688 * and the mesa token for the same thing.
1690 * \param inst The parsed tokens
1691 * \param outputReg Returned index/number of the output register,
1692 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
1695 parse_result_binding(GLcontext
*ctx
, const GLubyte
**inst
,
1696 GLuint
*outputReg
, struct arb_program
*Program
)
1698 const GLubyte token
= *(*inst
)++;
1701 case FRAGMENT_RESULT_COLOR
:
1702 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1705 /* This gets result of the color buffer we're supposed to
1706 * draw into. This pertains to GL_ARB_draw_buffers.
1708 parse_output_color_num(ctx
, inst
, Program
, &out_color
);
1709 ASSERT(out_color
< MAX_DRAW_BUFFERS
);
1710 *outputReg
= FRAG_RESULT_COLOR
;
1713 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1714 *outputReg
= VERT_RESULT_HPOS
;
1718 case FRAGMENT_RESULT_DEPTH
:
1719 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1720 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
1721 *outputReg
= FRAG_RESULT_DEPTH
;
1724 /* for vtx programs, this is VERTEX_RESULT_COLOR */
1726 GLuint face_type
= parse_face_type(inst
);
1727 GLint err
= parse_color_type(ctx
, inst
, Program
, &color_type
);
1734 *outputReg
= VERT_RESULT_BFC1
; /* secondary color */
1737 *outputReg
= VERT_RESULT_BFC0
; /* primary color */
1743 *outputReg
= VERT_RESULT_COL1
; /* secondary color */
1747 *outputReg
= VERT_RESULT_COL0
; /* primary color */
1753 case VERTEX_RESULT_FOGCOORD
:
1754 *outputReg
= VERT_RESULT_FOGC
;
1757 case VERTEX_RESULT_POINTSIZE
:
1758 *outputReg
= VERT_RESULT_PSIZ
;
1761 case VERTEX_RESULT_TEXCOORD
:
1764 if (parse_texcoord_num (ctx
, inst
, Program
, &unit
))
1766 *outputReg
= VERT_RESULT_TEX0
+ unit
;
1771 Program
->Base
.OutputsWritten
|= (1 << *outputReg
);
1778 * This handles the declaration of ATTRIB variables
1781 * parse_vert_attrib_binding(), or something like that
1783 * \return 0 on sucess, 1 on error
1786 parse_attrib (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1787 struct arb_program
*Program
)
1790 struct var_cache
*attrib_var
;
1792 attrib_var
= parse_string (inst
, vc_head
, Program
, &found
);
1793 Program
->Position
= parse_position (inst
);
1795 program_error2(ctx
, Program
->Position
,
1796 "Duplicate variable declaration",
1797 (char *) attrib_var
->name
);
1801 attrib_var
->type
= vt_attrib
;
1803 if (parse_attrib_binding(ctx
, inst
, Program
, &attrib_var
->attrib_binding
,
1804 &attrib_var
->attrib_is_generic
))
1807 if (generic_attrib_check(*vc_head
)) {
1808 program_error(ctx
, Program
->Position
,
1809 "Cannot use both a generic vertex attribute "
1810 "and a specific attribute of the same type");
1814 Program
->Base
.NumAttributes
++;
1819 * \param use -- TRUE if we're called when declaring implicit parameters,
1820 * FALSE if we're declaraing variables. This has to do with
1821 * if we get a signed or unsigned float for scalar constants
1824 parse_param_elements (GLcontext
* ctx
, const GLubyte
** inst
,
1825 struct var_cache
*param_var
,
1826 struct arb_program
*Program
, GLboolean use
)
1830 gl_state_index state_tokens
[STATE_LENGTH
] = {0, 0, 0, 0, 0};
1832 GLubyte token
= *(*inst
)++;
1835 case PARAM_STATE_ELEMENT
:
1836 if (parse_state_single_item (ctx
, inst
, Program
, state_tokens
))
1839 /* If we adding STATE_MATRIX that has multiple rows, we need to
1840 * unroll it and call _mesa_add_state_reference() for each row
1842 if ((state_tokens
[0] == STATE_MODELVIEW_MATRIX
||
1843 state_tokens
[0] == STATE_PROJECTION_MATRIX
||
1844 state_tokens
[0] == STATE_MVP_MATRIX
||
1845 state_tokens
[0] == STATE_TEXTURE_MATRIX
||
1846 state_tokens
[0] == STATE_PROGRAM_MATRIX
)
1847 && (state_tokens
[2] != state_tokens
[3])) {
1849 const GLint first_row
= state_tokens
[2];
1850 const GLint last_row
= state_tokens
[3];
1852 for (row
= first_row
; row
<= last_row
; row
++) {
1853 state_tokens
[2] = state_tokens
[3] = row
;
1855 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1857 if (param_var
->param_binding_begin
== ~0U)
1858 param_var
->param_binding_begin
= idx
;
1859 param_var
->param_binding_length
++;
1863 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1865 if (param_var
->param_binding_begin
== ~0U)
1866 param_var
->param_binding_begin
= idx
;
1867 param_var
->param_binding_length
++;
1871 case PARAM_PROGRAM_ELEMENT
:
1872 if (parse_program_single_item (ctx
, inst
, Program
, state_tokens
))
1874 idx
= _mesa_add_state_reference (Program
->Base
.Parameters
, state_tokens
);
1875 if (param_var
->param_binding_begin
== ~0U)
1876 param_var
->param_binding_begin
= idx
;
1877 param_var
->param_binding_length
++;
1879 /* Check if there is more: 0 -> we're done, else its an integer */
1881 GLuint out_of_range
, new_idx
;
1882 GLuint start_idx
= state_tokens
[2] + 1;
1883 GLuint end_idx
= parse_integer (inst
, Program
);
1886 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1887 if (((state_tokens
[1] == STATE_ENV
)
1888 && (end_idx
>= ctx
->Const
.FragmentProgram
.MaxEnvParams
))
1889 || ((state_tokens
[1] == STATE_LOCAL
)
1891 ctx
->Const
.FragmentProgram
.MaxLocalParams
)))
1895 if (((state_tokens
[1] == STATE_ENV
)
1896 && (end_idx
>= ctx
->Const
.VertexProgram
.MaxEnvParams
))
1897 || ((state_tokens
[1] == STATE_LOCAL
)
1899 ctx
->Const
.VertexProgram
.MaxLocalParams
)))
1903 program_error(ctx
, Program
->Position
,
1904 "Invalid Program Parameter"); /*end_idx*/
1908 for (new_idx
= start_idx
; new_idx
<= end_idx
; new_idx
++) {
1909 state_tokens
[2] = new_idx
;
1910 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1912 param_var
->param_binding_length
++;
1920 case PARAM_CONSTANT
:
1921 /* parsing something like {1.0, 2.0, 3.0, 4.0} */
1923 GLfloat const_values
[4];
1925 parse_constant(inst
, const_values
, &size
, Program
, use
);
1926 if (param_var
->name
[0] == ' ') {
1927 /* this is an unnamed constant */
1928 idx
= _mesa_add_unnamed_constant(Program
->Base
.Parameters
,
1930 ¶m_var
->swizzle
);
1933 /* named parameter/constant */
1934 idx
= _mesa_add_named_constant(Program
->Base
.Parameters
,
1935 (char *) param_var
->name
,
1936 const_values
, size
);
1938 if (param_var
->param_binding_begin
== ~0U)
1939 param_var
->param_binding_begin
= idx
;
1940 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1941 /* Note: when we reference this parameter in an instruction later,
1942 * we'll check if it's really a constant/immediate and set the
1943 * instruction register type appropriately.
1945 param_var
->param_binding_length
++;
1950 program_error(ctx
, Program
->Position
,
1951 "Unexpected token (in parse_param_elements())");
1955 Program
->Base
.NumParameters
= Program
->Base
.Parameters
->NumParameters
;
1957 /* Make sure we haven't blown past our parameter limits */
1958 if (((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1959 (Program
->Base
.NumParameters
>
1960 ctx
->Const
.VertexProgram
.MaxLocalParams
))
1961 || ((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1962 && (Program
->Base
.NumParameters
>
1963 ctx
->Const
.FragmentProgram
.MaxLocalParams
))) {
1964 program_error(ctx
, Program
->Position
, "Too many parameter variables");
1973 * This picks out PARAM program parameter bindings.
1975 * XXX: This needs to be stressed & tested
1977 * \return 0 on sucess, 1 on error
1980 parse_param (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1981 struct arb_program
*Program
)
1984 GLint specified_length
;
1985 struct var_cache
*param_var
;
1988 param_var
= parse_string (inst
, vc_head
, Program
, &found
);
1989 Program
->Position
= parse_position (inst
);
1992 program_error2(ctx
, Program
->Position
,
1993 "Duplicate variable declaration",
1994 (char *) param_var
->name
);
1998 specified_length
= parse_integer (inst
, Program
);
2000 if (specified_length
< 0) {
2001 program_error(ctx
, Program
->Position
, "Negative parameter array length");
2005 param_var
->type
= vt_param
;
2006 param_var
->param_binding_length
= 0;
2008 /* Right now, everything is shoved into the main state register file.
2010 * In the future, it would be nice to leave things ENV/LOCAL params
2011 * in their respective register files, if possible
2013 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
2016 * * - add each guy to the parameter list
2017 * * - increment the param_var->param_binding_len
2018 * * - store the param_var->param_binding_begin for the first one
2019 * * - compare the actual len to the specified len at the end
2021 while (**inst
!= PARAM_NULL
) {
2022 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_FALSE
))
2026 /* Test array length here! */
2027 if (specified_length
) {
2028 if (specified_length
!= (int)param_var
->param_binding_length
) {
2029 program_error(ctx
, Program
->Position
,
2030 "Declared parameter array length does not match parameter list");
2043 parse_param_use (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2044 struct arb_program
*Program
, struct var_cache
**new_var
)
2046 struct var_cache
*param_var
;
2048 /* First, insert a dummy entry into the var_cache */
2049 var_cache_create (¶m_var
);
2050 param_var
->name
= (const GLubyte
*) " ";
2051 param_var
->type
= vt_param
;
2053 param_var
->param_binding_length
= 0;
2054 /* Don't fill in binding_begin; We use the default value of -1
2055 * to tell if its already initialized, elsewhere.
2057 * param_var->param_binding_begin = 0;
2059 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
2061 var_cache_append (vc_head
, param_var
);
2063 /* Then fill it with juicy parameter goodness */
2064 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_TRUE
))
2067 *new_var
= param_var
;
2074 * This handles the declaration of TEMP variables
2076 * \return 0 on sucess, 1 on error
2079 parse_temp (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2080 struct arb_program
*Program
)
2083 struct var_cache
*temp_var
;
2085 while (**inst
!= 0) {
2086 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2087 Program
->Position
= parse_position (inst
);
2089 program_error2(ctx
, Program
->Position
,
2090 "Duplicate variable declaration",
2091 (char *) temp_var
->name
);
2095 temp_var
->type
= vt_temp
;
2097 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
2098 (Program
->Base
.NumTemporaries
>=
2099 ctx
->Const
.FragmentProgram
.MaxTemps
))
2100 || ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
2101 && (Program
->Base
.NumTemporaries
>=
2102 ctx
->Const
.VertexProgram
.MaxTemps
))) {
2103 program_error(ctx
, Program
->Position
,
2104 "Too many TEMP variables declared");
2108 temp_var
->temp_binding
= Program
->Base
.NumTemporaries
;
2109 Program
->Base
.NumTemporaries
++;
2117 * This handles variables of the OUTPUT variety
2119 * \return 0 on sucess, 1 on error
2122 parse_output (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2123 struct arb_program
*Program
)
2126 struct var_cache
*output_var
;
2129 output_var
= parse_string (inst
, vc_head
, Program
, &found
);
2130 Program
->Position
= parse_position (inst
);
2132 program_error2(ctx
, Program
->Position
,
2133 "Duplicate variable declaration",
2134 (char *) output_var
->name
);
2138 output_var
->type
= vt_output
;
2140 err
= parse_result_binding(ctx
, inst
, &output_var
->output_binding
, Program
);
2145 * This handles variables of the ALIAS kind
2147 * \return 0 on sucess, 1 on error
2150 parse_alias (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2151 struct arb_program
*Program
)
2154 struct var_cache
*temp_var
;
2156 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2157 Program
->Position
= parse_position (inst
);
2160 program_error2(ctx
, Program
->Position
,
2161 "Duplicate variable declaration",
2162 (char *) temp_var
->name
);
2166 temp_var
->type
= vt_alias
;
2167 temp_var
->alias_binding
= parse_string (inst
, vc_head
, Program
, &found
);
2168 Program
->Position
= parse_position (inst
);
2172 program_error2(ctx
, Program
->Position
,
2173 "Undefined alias value",
2174 (char *) temp_var
->alias_binding
->name
);
2182 * This handles variables of the ADDRESS kind
2184 * \return 0 on sucess, 1 on error
2187 parse_address (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2188 struct arb_program
*Program
)
2191 struct var_cache
*temp_var
;
2193 while (**inst
!= 0) {
2194 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2195 Program
->Position
= parse_position (inst
);
2197 program_error2(ctx
, Program
->Position
,
2198 "Duplicate variable declaration",
2199 (char *) temp_var
->name
);
2203 temp_var
->type
= vt_address
;
2205 if (Program
->Base
.NumAddressRegs
>=
2206 ctx
->Const
.VertexProgram
.MaxAddressRegs
) {
2207 const char *msg
= "Too many ADDRESS variables declared";
2208 program_error(ctx
, Program
->Position
, msg
);
2212 temp_var
->address_binding
= Program
->Base
.NumAddressRegs
;
2213 Program
->Base
.NumAddressRegs
++;
2221 * Parse a program declaration
2223 * \return 0 on sucess, 1 on error
2226 parse_declaration (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2227 struct arb_program
*Program
)
2231 switch (*(*inst
)++) {
2233 err
= parse_address (ctx
, inst
, vc_head
, Program
);
2237 err
= parse_alias (ctx
, inst
, vc_head
, Program
);
2241 err
= parse_attrib (ctx
, inst
, vc_head
, Program
);
2245 err
= parse_output (ctx
, inst
, vc_head
, Program
);
2249 err
= parse_param (ctx
, inst
, vc_head
, Program
);
2253 err
= parse_temp (ctx
, inst
, vc_head
, Program
);
2261 * Handle the parsing out of a masked destination register, either for a
2262 * vertex or fragment program.
2264 * If we are a vertex program, make sure we don't write to
2265 * result.position if we have specified that the program is
2266 * position invariant
2268 * \param File - The register file we write to
2269 * \param Index - The register index we write to
2270 * \param WriteMask - The mask controlling which components we write (1->write)
2272 * \return 0 on sucess, 1 on error
2275 parse_masked_dst_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2276 struct var_cache
**vc_head
, struct arb_program
*Program
,
2277 gl_register_file
*File
, GLuint
*Index
, GLint
*WriteMask
)
2280 struct var_cache
*dst
;
2282 /* We either have a result register specified, or a
2283 * variable that may or may not be writable
2285 switch (*(*inst
)++) {
2286 case REGISTER_RESULT
:
2287 if (parse_result_binding(ctx
, inst
, Index
, Program
))
2289 *File
= PROGRAM_OUTPUT
;
2292 case REGISTER_ESTABLISHED_NAME
:
2293 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2294 Program
->Position
= parse_position (inst
);
2296 /* If the name has never been added to our symbol table, we're hosed */
2298 program_error(ctx
, Program
->Position
, "0: Undefined variable");
2302 switch (dst
->type
) {
2304 *File
= PROGRAM_OUTPUT
;
2305 *Index
= dst
->output_binding
;
2309 *File
= PROGRAM_TEMPORARY
;
2310 *Index
= dst
->temp_binding
;
2313 /* If the var type is not vt_output or vt_temp, no go */
2315 program_error(ctx
, Program
->Position
,
2316 "Destination register is read only");
2322 program_error(ctx
, Program
->Position
,
2323 "Unexpected opcode in parse_masked_dst_reg()");
2328 /* Position invariance test */
2329 /* This test is done now in syntax portion - when position invariance OPTION
2330 is specified, "result.position" rule is disabled so there is no way
2331 to write the position
2333 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2335 program_error(ctx, Program->Position,
2336 "Vertex program specified position invariance and wrote vertex position");
2339 /* And then the mask.
2345 * ==> Need to reverse the order of bits for this!
2347 tmp
= (GLint
) *(*inst
)++;
2348 *WriteMask
= (((tmp
>>3) & 0x1) |
2358 * Handle the parsing of a address register
2360 * \param Index - The register index we write to
2362 * \return 0 on sucess, 1 on error
2365 parse_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2366 struct var_cache
**vc_head
,
2367 struct arb_program
*Program
, GLint
* Index
)
2369 struct var_cache
*dst
;
2372 *Index
= 0; /* XXX */
2374 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2375 Program
->Position
= parse_position (inst
);
2377 /* If the name has never been added to our symbol table, we're hosed */
2379 program_error(ctx
, Program
->Position
, "Undefined variable");
2383 if (dst
->type
!= vt_address
) {
2384 program_error(ctx
, Program
->Position
, "Variable is not of type ADDRESS");
2393 * Handle the parsing out of a masked address register
2395 * \param Index - The register index we write to
2396 * \param WriteMask - The mask controlling which components we write (1->write)
2398 * \return 0 on sucess, 1 on error
2401 parse_masked_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2402 struct var_cache
**vc_head
,
2403 struct arb_program
*Program
, GLint
* Index
,
2404 GLboolean
* WriteMask
)
2406 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, Index
))
2409 /* This should be 0x8 */
2412 /* Writemask of .x is implied */
2414 WriteMask
[1] = WriteMask
[2] = WriteMask
[3] = 0;
2421 * Parse out a swizzle mask.
2423 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
2425 * The len parameter allows us to grab 4 components for a vector
2426 * swizzle, or just 1 component for a scalar src register selection
2429 parse_swizzle_mask(const GLubyte
** inst
, GLubyte
*swizzle
, GLint len
)
2433 for (i
= 0; i
< 4; i
++)
2436 for (i
= 0; i
< len
; i
++) {
2437 switch (*(*inst
)++) {
2439 swizzle
[i
] = SWIZZLE_X
;
2442 swizzle
[i
] = SWIZZLE_Y
;
2445 swizzle
[i
] = SWIZZLE_Z
;
2448 swizzle
[i
] = SWIZZLE_W
;
2451 _mesa_problem(NULL
, "bad component in parse_swizzle_mask()");
2457 swizzle
[1] = swizzle
[2] = swizzle
[3] = swizzle
[0];
2462 * Parse an extended swizzle mask which is a sequence of
2463 * four x/y/z/w/0/1 tokens.
2464 * \return swizzle four swizzle values
2465 * \return negateMask four element bitfield
2468 parse_extended_swizzle_mask(const GLubyte
**inst
, GLubyte swizzle
[4],
2469 GLubyte
*negateMask
)
2474 for (i
= 0; i
< 4; i
++) {
2476 if (parse_sign(inst
) == -1)
2477 *negateMask
|= (1 << i
);
2483 swizzle
[i
] = SWIZZLE_ZERO
;
2486 swizzle
[i
] = SWIZZLE_ONE
;
2489 swizzle
[i
] = SWIZZLE_X
;
2492 swizzle
[i
] = SWIZZLE_Y
;
2495 swizzle
[i
] = SWIZZLE_Z
;
2498 swizzle
[i
] = SWIZZLE_W
;
2501 _mesa_problem(NULL
, "bad case in parse_extended_swizzle_mask()");
2509 parse_src_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2510 struct var_cache
**vc_head
,
2511 struct arb_program
*Program
,
2512 gl_register_file
* File
, GLint
* Index
, GLuint
*swizzle
,
2513 GLboolean
*IsRelOffset
)
2515 struct var_cache
*src
;
2516 GLuint binding
, is_generic
, found
;
2521 *swizzle
= SWIZZLE_XYZW
; /* default */
2523 /* And the binding for the src */
2524 switch (*(*inst
)++) {
2525 case REGISTER_ATTRIB
:
2526 if (parse_attrib_binding
2527 (ctx
, inst
, Program
, &binding
, &is_generic
))
2529 *File
= PROGRAM_INPUT
;
2532 /* We need to insert a dummy variable into the var_cache so we can
2533 * catch generic vertex attrib aliasing errors
2535 var_cache_create(&src
);
2536 src
->type
= vt_attrib
;
2537 src
->name
= (const GLubyte
*) "Dummy Attrib Variable";
2538 src
->attrib_binding
= binding
;
2539 src
->attrib_is_generic
= is_generic
;
2540 var_cache_append(vc_head
, src
);
2541 if (generic_attrib_check(*vc_head
)) {
2542 program_error(ctx
, Program
->Position
,
2543 "Cannot use both a generic vertex attribute "
2544 "and a specific attribute of the same type");
2549 case REGISTER_PARAM
:
2551 case PARAM_ARRAY_ELEMENT
:
2553 src
= parse_string (inst
, vc_head
, Program
, &found
);
2554 Program
->Position
= parse_position (inst
);
2557 program_error2(ctx
, Program
->Position
,
2558 "Undefined variable",
2559 (char *) src
->name
);
2563 *File
= (gl_register_file
) src
->param_binding_type
;
2565 switch (*(*inst
)++) {
2566 case ARRAY_INDEX_ABSOLUTE
:
2567 offset
= parse_integer (inst
, Program
);
2570 || (offset
>= (int)src
->param_binding_length
)) {
2571 program_error(ctx
, Program
->Position
,
2572 "Index out of range");
2573 /* offset, src->name */
2577 *Index
= src
->param_binding_begin
+ offset
;
2578 *swizzle
= src
->swizzle
;
2581 case ARRAY_INDEX_RELATIVE
:
2583 GLint addr_reg_idx
, rel_off
;
2585 /* First, grab the address regiseter */
2586 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &addr_reg_idx
))
2595 /* Then the relative offset */
2596 if (parse_relative_offset(ctx
, inst
, Program
, &rel_off
)) return 1;
2598 /* And store it properly */
2599 *Index
= src
->param_binding_begin
+ rel_off
;
2601 *swizzle
= src
->swizzle
;
2608 if (parse_param_use (ctx
, inst
, vc_head
, Program
, &src
))
2611 *File
= (gl_register_file
) src
->param_binding_type
;
2612 *Index
= src
->param_binding_begin
;
2613 *swizzle
= src
->swizzle
;
2618 case REGISTER_ESTABLISHED_NAME
:
2619 src
= parse_string (inst
, vc_head
, Program
, &found
);
2620 Program
->Position
= parse_position (inst
);
2622 /* If the name has never been added to our symbol table, we're hosed */
2624 program_error(ctx
, Program
->Position
,
2625 "3: Undefined variable"); /* src->name */
2629 switch (src
->type
) {
2631 *File
= PROGRAM_INPUT
;
2632 *Index
= src
->attrib_binding
;
2635 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2637 *File
= (gl_register_file
) src
->param_binding_type
;
2638 *Index
= src
->param_binding_begin
;
2642 *File
= PROGRAM_TEMPORARY
;
2643 *Index
= src
->temp_binding
;
2646 /* If the var type is vt_output no go */
2648 program_error(ctx
, Program
->Position
,
2649 "destination register is read only");
2656 program_error(ctx
, Program
->Position
,
2657 "Unknown token in parse_src_reg");
2661 if (*File
== PROGRAM_STATE_VAR
) {
2662 gl_register_file file
;
2664 /* If we're referencing the Program->Parameters[] array, check if the
2665 * parameter is really a constant/literal. If so, set File to CONSTANT.
2667 assert(*Index
< (GLint
) Program
->Base
.Parameters
->NumParameters
);
2668 file
= Program
->Base
.Parameters
->Parameters
[*Index
].Type
;
2669 if (file
== PROGRAM_CONSTANT
)
2670 *File
= PROGRAM_CONSTANT
;
2673 /* Add attributes to InputsRead only if they are used the program.
2674 * This avoids the handling of unused ATTRIB declarations in the drivers. */
2675 if (*File
== PROGRAM_INPUT
)
2676 Program
->Base
.InputsRead
|= (1 << *Index
);
2683 swizzle_swizzle(GLuint baseSwizzle
, const GLubyte swizzle
[4])
2685 GLuint i
, swz
, s
[4];
2686 for (i
= 0; i
< 4; i
++) {
2687 GLuint c
= swizzle
[i
];
2689 s
[i
] = GET_SWZ(baseSwizzle
, c
);
2693 swz
= MAKE_SWIZZLE4(s
[0], s
[1], s
[2], s
[3]);
2698 * Parse vertex/fragment program vector source register.
2701 parse_vector_src_reg(GLcontext
*ctx
, const GLubyte
**inst
,
2702 struct var_cache
**vc_head
,
2703 struct arb_program
*program
,
2704 struct prog_src_register
*reg
)
2706 gl_register_file file
;
2710 GLboolean isRelOffset
;
2714 negateMask
= (parse_sign (inst
) == -1) ? NEGATE_XYZW
: NEGATE_NONE
;
2716 /* And the src reg */
2717 if (parse_src_reg(ctx
, inst
, vc_head
, program
, &file
, &index
, &baseSwizzle
,
2721 /* finally, the swizzle */
2722 parse_swizzle_mask(inst
, swizzle
, 4);
2726 reg
->Swizzle
= swizzle_swizzle(baseSwizzle
, swizzle
);
2727 reg
->Negate
= negateMask
;
2728 reg
->RelAddr
= isRelOffset
;
2734 * Parse vertex/fragment program scalar source register.
2737 parse_scalar_src_reg(GLcontext
*ctx
, const GLubyte
**inst
,
2738 struct var_cache
**vc_head
,
2739 struct arb_program
*program
,
2740 struct prog_src_register
*reg
)
2742 gl_register_file file
;
2746 GLboolean isRelOffset
;
2750 negateMask
= (parse_sign (inst
) == -1) ? NEGATE_XYZW
: NEGATE_NONE
;
2752 /* And the src reg */
2753 if (parse_src_reg(ctx
, inst
, vc_head
, program
, &file
, &index
, &baseSwizzle
,
2757 /* finally, the swizzle */
2758 parse_swizzle_mask(inst
, swizzle
, 1);
2762 reg
->Swizzle
= swizzle_swizzle(baseSwizzle
, swizzle
);
2763 reg
->Negate
= negateMask
;
2764 reg
->RelAddr
= isRelOffset
;
2770 * Parse vertex/fragment program destination register.
2771 * \return 1 if error, 0 if no error.
2774 parse_dst_reg(GLcontext
* ctx
, const GLubyte
** inst
,
2775 struct var_cache
**vc_head
, struct arb_program
*program
,
2776 struct prog_dst_register
*reg
)
2780 gl_register_file file
;
2782 if (parse_masked_dst_reg (ctx
, inst
, vc_head
, program
, &file
, &idx
, &mask
))
2787 reg
->WriteMask
= mask
;
2793 * This is a big mother that handles getting opcodes into the instruction
2794 * and handling the src & dst registers for fragment program instructions
2795 * \return 1 if error, 0 if no error
2798 parse_fp_instruction (GLcontext
* ctx
, const GLubyte
** inst
,
2799 struct var_cache
**vc_head
, struct arb_program
*Program
,
2800 struct prog_instruction
*fp
)
2804 GLubyte instClass
, type
, code
;
2806 GLuint shadow_tex
= 0;
2808 _mesa_init_instructions(fp
, 1);
2810 /* OP_ALU_INST or OP_TEX_INST */
2811 instClass
= *(*inst
)++;
2813 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2814 * OP_TEX_{SAMPLE, KIL}
2818 /* The actual opcode name */
2821 /* Increment the correct count */
2822 switch (instClass
) {
2824 Program
->NumAluInstructions
++;
2827 Program
->NumTexInstructions
++;
2835 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2837 fp
->Opcode
= OPCODE_ABS
;
2841 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2843 fp
->Opcode
= OPCODE_FLR
;
2847 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2849 fp
->Opcode
= OPCODE_FRC
;
2853 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2855 fp
->Opcode
= OPCODE_LIT
;
2859 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2861 fp
->Opcode
= OPCODE_MOV
;
2865 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2868 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2875 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2877 fp
->Opcode
= OPCODE_COS
;
2881 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2883 fp
->Opcode
= OPCODE_EX2
;
2887 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2889 fp
->Opcode
= OPCODE_LG2
;
2893 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2895 fp
->Opcode
= OPCODE_RCP
;
2899 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2901 fp
->Opcode
= OPCODE_RSQ
;
2905 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2907 fp
->Opcode
= OPCODE_SIN
;
2911 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2914 fp
->Opcode
= OPCODE_SCS
;
2918 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2921 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2928 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2930 fp
->Opcode
= OPCODE_POW
;
2934 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2937 for (a
= 0; a
< 2; a
++) {
2938 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2947 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2949 fp
->Opcode
= OPCODE_ADD
;
2953 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2955 fp
->Opcode
= OPCODE_DP3
;
2959 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2961 fp
->Opcode
= OPCODE_DP4
;
2965 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2967 fp
->Opcode
= OPCODE_DPH
;
2971 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2973 fp
->Opcode
= OPCODE_DST
;
2977 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2979 fp
->Opcode
= OPCODE_MAX
;
2983 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2985 fp
->Opcode
= OPCODE_MIN
;
2989 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2991 fp
->Opcode
= OPCODE_MUL
;
2995 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2997 fp
->Opcode
= OPCODE_SGE
;
3001 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3003 fp
->Opcode
= OPCODE_SLT
;
3007 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3009 fp
->Opcode
= OPCODE_SUB
;
3013 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3015 fp
->Opcode
= OPCODE_XPD
;
3019 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3021 for (a
= 0; a
< 2; a
++) {
3022 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
3030 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3032 fp
->Opcode
= OPCODE_CMP
;
3036 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3038 fp
->Opcode
= OPCODE_LRP
;
3042 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3044 fp
->Opcode
= OPCODE_MAD
;
3048 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3051 for (a
= 0; a
< 3; a
++) {
3052 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
3060 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3062 fp
->Opcode
= OPCODE_SWZ
;
3065 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3071 gl_register_file file
;
3075 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
,
3076 &baseSwizzle
, &rel
))
3078 parse_extended_swizzle_mask(inst
, swizzle
, &negateMask
);
3079 fp
->SrcReg
[0].File
= file
;
3080 fp
->SrcReg
[0].Index
= index
;
3081 fp
->SrcReg
[0].Negate
= negateMask
;
3082 fp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
3092 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3094 fp
->Opcode
= OPCODE_TEX
;
3098 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3100 fp
->Opcode
= OPCODE_TXP
;
3104 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3106 fp
->Opcode
= OPCODE_TXB
;
3110 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3113 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
3117 if (parse_teximage_num (ctx
, inst
, Program
, &texcoord
))
3119 fp
->TexSrcUnit
= texcoord
;
3122 switch (*(*inst
)++) {
3123 case TEXTARGET_SHADOW1D
:
3124 shadow_tex
= 1 << texcoord
;
3127 fp
->TexSrcTarget
= TEXTURE_1D_INDEX
;
3129 case TEXTARGET_SHADOW2D
:
3130 shadow_tex
= 1 << texcoord
;
3133 fp
->TexSrcTarget
= TEXTURE_2D_INDEX
;
3136 fp
->TexSrcTarget
= TEXTURE_3D_INDEX
;
3138 case TEXTARGET_SHADOWRECT
:
3139 shadow_tex
= 1 << texcoord
;
3141 case TEXTARGET_RECT
:
3142 fp
->TexSrcTarget
= TEXTURE_RECT_INDEX
;
3144 case TEXTARGET_CUBE
:
3145 fp
->TexSrcTarget
= TEXTURE_CUBE_INDEX
;
3147 case TEXTARGET_SHADOW1D_ARRAY
:
3148 shadow_tex
= 1 << texcoord
;
3150 case TEXTARGET_1D_ARRAY
:
3151 fp
->TexSrcTarget
= TEXTURE_1D_ARRAY_INDEX
;
3153 case TEXTARGET_SHADOW2D_ARRAY
:
3154 shadow_tex
= 1 << texcoord
;
3156 case TEXTARGET_2D_ARRAY
:
3157 fp
->TexSrcTarget
= TEXTURE_2D_ARRAY_INDEX
;
3164 /* Don't test the first time a particular sampler is seen. Each time
3165 * after that, make sure the shadow state is the same.
3167 if ((_mesa_bitcount(Program
->TexturesUsed
[texcoord
]) > 0)
3168 && ((Program
->ShadowSamplers
& (1 << texcoord
)) != shadow_tex
)) {
3169 program_error(ctx
, Program
->Position
,
3170 "texture image unit used for shadow sampling and non-shadow sampling");
3174 Program
->TexturesUsed
[texcoord
] |= (1 << fp
->TexSrcTarget
);
3175 /* Check that both "2D" and "CUBE" (for example) aren't both used */
3176 if (_mesa_bitcount(Program
->TexturesUsed
[texcoord
]) > 1) {
3177 program_error(ctx
, Program
->Position
,
3178 "multiple targets used on one texture image unit");
3183 Program
->ShadowSamplers
|= shadow_tex
;
3187 Program
->UsesKill
= 1;
3188 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
3190 fp
->Opcode
= OPCODE_KIL
;
3193 _mesa_problem(ctx
, "bad type 0x%x in parse_fp_instruction()", type
);
3202 * Handle the parsing out of a masked address register
3204 * \param Index - The register index we write to
3205 * \param WriteMask - The mask controlling which components we write (1->write)
3207 * \return 0 on sucess, 1 on error
3210 parse_vp_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
3211 struct var_cache
**vc_head
,
3212 struct arb_program
*Program
,
3213 struct prog_dst_register
*reg
)
3217 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &idx
))
3220 /* This should be 0x8 */
3223 reg
->File
= PROGRAM_ADDRESS
;
3226 /* Writemask of .x is implied */
3227 reg
->WriteMask
= 0x1;
3233 * This is a big mother that handles getting opcodes into the instruction
3234 * and handling the src & dst registers for vertex program instructions
3237 parse_vp_instruction (GLcontext
* ctx
, const GLubyte
** inst
,
3238 struct var_cache
**vc_head
, struct arb_program
*Program
,
3239 struct prog_instruction
*vp
)
3244 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3247 /* The actual opcode name */
3250 _mesa_init_instructions(vp
, 1);
3255 vp
->Opcode
= OPCODE_ARL
;
3257 /* Remember to set SrcReg.RelAddr; */
3259 /* Get the masked address register [dst] */
3260 if (parse_vp_address_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3263 vp
->DstReg
.File
= PROGRAM_ADDRESS
;
3265 /* Get a scalar src register */
3266 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3274 vp
->Opcode
= OPCODE_ABS
;
3277 vp
->Opcode
= OPCODE_FLR
;
3280 vp
->Opcode
= OPCODE_FRC
;
3283 vp
->Opcode
= OPCODE_LIT
;
3286 vp
->Opcode
= OPCODE_MOV
;
3290 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3293 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3300 vp
->Opcode
= OPCODE_EX2
;
3303 vp
->Opcode
= OPCODE_EXP
;
3306 vp
->Opcode
= OPCODE_LG2
;
3309 vp
->Opcode
= OPCODE_LOG
;
3312 vp
->Opcode
= OPCODE_RCP
;
3315 vp
->Opcode
= OPCODE_RSQ
;
3318 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3321 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3328 vp
->Opcode
= OPCODE_POW
;
3331 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3334 for (a
= 0; a
< 2; a
++) {
3335 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3343 vp
->Opcode
= OPCODE_ADD
;
3346 vp
->Opcode
= OPCODE_DP3
;
3349 vp
->Opcode
= OPCODE_DP4
;
3352 vp
->Opcode
= OPCODE_DPH
;
3355 vp
->Opcode
= OPCODE_DST
;
3358 vp
->Opcode
= OPCODE_MAX
;
3361 vp
->Opcode
= OPCODE_MIN
;
3364 vp
->Opcode
= OPCODE_MUL
;
3367 vp
->Opcode
= OPCODE_SGE
;
3370 vp
->Opcode
= OPCODE_SLT
;
3373 vp
->Opcode
= OPCODE_SUB
;
3376 vp
->Opcode
= OPCODE_XPD
;
3379 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3382 for (a
= 0; a
< 2; a
++) {
3383 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3391 vp
->Opcode
= OPCODE_MAD
;
3395 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3398 for (a
= 0; a
< 3; a
++) {
3399 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3407 vp
->Opcode
= OPCODE_SWZ
;
3414 gl_register_file file
;
3418 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3421 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
,
3422 &baseSwizzle
, &relAddr
))
3424 parse_extended_swizzle_mask (inst
, swizzle
, &negateMask
);
3425 vp
->SrcReg
[0].File
= file
;
3426 vp
->SrcReg
[0].Index
= index
;
3427 vp
->SrcReg
[0].Negate
= negateMask
;
3428 vp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
3432 vp
->SrcReg
[0].RelAddr
= relAddr
;
3442 debug_variables (GLcontext
* ctx
, struct var_cache
*vc_head
,
3443 struct arb_program
*Program
)
3445 struct var_cache
*vc
;
3448 fprintf (stderr
, "debug_variables, vc_head: %p\n", (void*) vc_head
);
3450 /* First of all, print out the contents of the var_cache */
3453 fprintf (stderr
, "[%p]\n", (void*) vc
);
3456 fprintf (stderr
, "UNDEFINED %s\n", vc
->name
);
3459 fprintf (stderr
, "ATTRIB %s\n", vc
->name
);
3460 fprintf (stderr
, " binding: 0x%x\n", vc
->attrib_binding
);
3463 fprintf (stderr
, "PARAM %s begin: %d len: %d\n", vc
->name
,
3464 vc
->param_binding_begin
, vc
->param_binding_length
);
3465 b
= vc
->param_binding_begin
;
3466 for (a
= 0; a
< vc
->param_binding_length
; a
++) {
3467 fprintf (stderr
, "%s\n",
3468 Program
->Base
.Parameters
->Parameters
[a
+ b
].Name
);
3469 if (Program
->Base
.Parameters
->Parameters
[a
+ b
].Type
== PROGRAM_STATE_VAR
) {
3471 s
= _mesa_program_state_string(Program
->Base
.Parameters
->Parameters
3472 [a
+ b
].StateIndexes
);
3473 fprintf(stderr
, "%s\n", s
);
3477 fprintf (stderr
, "%f %f %f %f\n",
3478 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][0],
3479 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][1],
3480 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][2],
3481 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][3]);
3485 fprintf (stderr
, "TEMP %s\n", vc
->name
);
3486 fprintf (stderr
, " binding: 0x%x\n", vc
->temp_binding
);
3489 fprintf (stderr
, "OUTPUT %s\n", vc
->name
);
3490 fprintf (stderr
, " binding: 0x%x\n", vc
->output_binding
);
3493 fprintf (stderr
, "ALIAS %s\n", vc
->name
);
3494 fprintf (stderr
, " binding: 0x%p (%s)\n",
3495 (void*) vc
->alias_binding
, vc
->alias_binding
->name
);
3505 #endif /* DEBUG_PARSING */
3509 * The main loop for parsing a fragment or vertex program
3511 * \return 1 on error, 0 on success
3514 parse_instructions(GLcontext
* ctx
, const GLubyte
* inst
,
3515 struct var_cache
**vc_head
, struct arb_program
*Program
)
3517 const GLuint maxInst
= (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
3518 ? ctx
->Const
.FragmentProgram
.MaxInstructions
3519 : ctx
->Const
.VertexProgram
.MaxInstructions
;
3522 ASSERT(MAX_PROGRAM_INSTRUCTIONS
>= maxInst
);
3524 Program
->MajorVersion
= (GLuint
) * inst
++;
3525 Program
->MinorVersion
= (GLuint
) * inst
++;
3527 while (*inst
!= END
) {
3532 case ARB_PRECISION_HINT_FASTEST
:
3533 Program
->PrecisionOption
= GL_FASTEST
;
3536 case ARB_PRECISION_HINT_NICEST
:
3537 Program
->PrecisionOption
= GL_NICEST
;
3541 Program
->FogOption
= GL_EXP
;
3545 Program
->FogOption
= GL_EXP2
;
3548 case ARB_FOG_LINEAR
:
3549 Program
->FogOption
= GL_LINEAR
;
3552 case ARB_POSITION_INVARIANT
:
3553 if (Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
3554 Program
->HintPositionInvariant
= GL_TRUE
;
3557 case ARB_FRAGMENT_PROGRAM_SHADOW
:
3558 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3559 /* TODO ARB_fragment_program_shadow code */
3563 case ARB_DRAW_BUFFERS
:
3564 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3565 /* do nothing for now */
3569 case MESA_TEXTURE_ARRAY
:
3570 /* do nothing for now */
3577 if (Program
->Base
.NumInstructions
+ 1 >= maxInst
) {
3578 program_error(ctx
, Program
->Position
,
3579 "Max instruction count exceeded");
3582 Program
->Position
= parse_position (&inst
);
3583 /* parse the current instruction */
3584 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3585 err
= parse_fp_instruction (ctx
, &inst
, vc_head
, Program
,
3586 &Program
->Base
.Instructions
[Program
->Base
.NumInstructions
]);
3589 err
= parse_vp_instruction (ctx
, &inst
, vc_head
, Program
,
3590 &Program
->Base
.Instructions
[Program
->Base
.NumInstructions
]);
3593 /* increment instuction count */
3594 Program
->Base
.NumInstructions
++;
3598 err
= parse_declaration (ctx
, &inst
, vc_head
, Program
);
3609 /* Finally, tag on an OPCODE_END instruction */
3611 const GLuint numInst
= Program
->Base
.NumInstructions
;
3612 _mesa_init_instructions(Program
->Base
.Instructions
+ numInst
, 1);
3613 Program
->Base
.Instructions
[numInst
].Opcode
= OPCODE_END
;
3615 Program
->Base
.NumInstructions
++;
3618 * Initialize native counts to logical counts. The device driver may
3619 * change them if program is translated into a hardware program.
3621 Program
->Base
.NumNativeInstructions
= Program
->Base
.NumInstructions
;
3622 Program
->Base
.NumNativeTemporaries
= Program
->Base
.NumTemporaries
;
3623 Program
->Base
.NumNativeParameters
= Program
->Base
.NumParameters
;
3624 Program
->Base
.NumNativeAttributes
= Program
->Base
.NumAttributes
;
3625 Program
->Base
.NumNativeAddressRegs
= Program
->Base
.NumAddressRegs
;
3632 LONGSTRING
static char core_grammar_text
[] =
3633 #include "shader/grammar/grammar_syn.h"
3638 * Set a grammar parameter.
3639 * \param name the grammar parameter
3640 * \param value the new parameter value
3641 * \return 0 if OK, 1 if error
3644 set_reg8 (GLcontext
*ctx
, grammar id
, const char *name
, GLubyte value
)
3646 char error_msg
[300];
3649 if (grammar_set_reg8 (id
, (const byte
*) name
, value
))
3652 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3653 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3654 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Grammar Register Error");
3660 * Enable support for the given language option in the parser.
3661 * \return 1 if OK, 0 if error
3664 enable_ext(GLcontext
*ctx
, grammar id
, const char *name
)
3666 return !set_reg8(ctx
, id
, name
, 1);
3671 * Enable parser extensions based on which OpenGL extensions are supported
3672 * by this rendering context.
3674 * \return GL_TRUE if OK, GL_FALSE if error.
3677 enable_parser_extensions(GLcontext
*ctx
, grammar id
)
3680 /* These are not supported at this time */
3681 if ((ctx
->Extensions
.ARB_vertex_blend
||
3682 ctx
->Extensions
.EXT_vertex_weighting
)
3683 && !enable_ext(ctx
, id
, "vertex_blend"))
3685 if (ctx
->Extensions
.ARB_matrix_palette
3686 && !enable_ext(ctx
, id
, "matrix_palette"))
3689 if (ctx
->Extensions
.ARB_fragment_program_shadow
3690 && !enable_ext(ctx
, id
, "fragment_program_shadow"))
3692 if (ctx
->Extensions
.EXT_point_parameters
3693 && !enable_ext(ctx
, id
, "point_parameters"))
3695 if (ctx
->Extensions
.EXT_secondary_color
3696 && !enable_ext(ctx
, id
, "secondary_color"))
3698 if (ctx
->Extensions
.EXT_fog_coord
3699 && !enable_ext(ctx
, id
, "fog_coord"))
3701 if (ctx
->Extensions
.NV_texture_rectangle
3702 && !enable_ext(ctx
, id
, "texture_rectangle"))
3704 if (!enable_ext(ctx
, id
, "draw_buffers"))
3706 if (ctx
->Extensions
.MESA_texture_array
3707 && !enable_ext(ctx
, id
, "texture_array"))
3710 /* hack for Warcraft (see bug 8060) */
3711 enable_ext(ctx
, id
, "vertex_blend");
3719 * This kicks everything off.
3721 * \param ctx - The GL Context
3722 * \param str - The program string
3723 * \param len - The program string length
3724 * \param program - The arb_program struct to return all the parsed info in
3725 * \return GL_TRUE on sucess, GL_FALSE on error
3728 _mesa_parse_arb_program(GLcontext
*ctx
, GLenum target
,
3729 const GLubyte
*str
, GLsizei len
,
3730 struct arb_program
*program
)
3732 GLint a
, err
, error_pos
;
3733 char error_msg
[300];
3735 struct var_cache
*vc_head
;
3736 grammar arbprogram_syn_id
;
3737 GLubyte
*parsed
, *inst
;
3738 GLubyte
*strz
= NULL
;
3739 static int arbprogram_syn_is_ok
= 0; /* XXX temporary */
3741 /* set the program target before parsing */
3742 program
->Base
.Target
= target
;
3744 /* Reset error state */
3745 _mesa_set_program_error(ctx
, -1, NULL
);
3747 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
3748 if (!arbprogram_syn_is_ok
) {
3749 /* One-time initialization of parsing system */
3750 grammar grammar_syn_id
;
3753 grammar_syn_id
= grammar_load_from_text ((byte
*) core_grammar_text
);
3754 if (grammar_syn_id
== 0) {
3755 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3756 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3757 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3758 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3759 "glProgramStringARB(Error loading grammar rule set)");
3763 err
= !grammar_check(grammar_syn_id
, (byte
*) arb_grammar_text
,
3764 &parsed
, &parsed_len
);
3766 /* 'parsed' is unused here */
3767 _mesa_free (parsed
);
3770 /* NOTE: we can't destroy grammar_syn_id right here because
3771 * grammar_destroy() can reset the last error
3774 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3775 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3776 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3777 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3778 "glProgramString(Error loading grammar rule set");
3779 grammar_destroy (grammar_syn_id
);
3783 grammar_destroy (grammar_syn_id
);
3785 arbprogram_syn_is_ok
= 1;
3788 /* create the grammar object */
3789 arbprogram_syn_id
= grammar_load_from_text ((byte
*) arb_grammar_text
);
3790 if (arbprogram_syn_id
== 0) {
3791 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3792 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
3793 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3794 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3795 "glProgramString(Error loading grammer rule set)");
3799 /* Set program_target register value */
3800 if (set_reg8 (ctx
, arbprogram_syn_id
, "program_target",
3801 program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
? 0x10 : 0x20)) {
3802 grammar_destroy (arbprogram_syn_id
);
3806 if (!enable_parser_extensions(ctx
, arbprogram_syn_id
)) {
3807 grammar_destroy(arbprogram_syn_id
);
3811 /* check for NULL character occurences */
3814 for (i
= 0; i
< len
; i
++) {
3815 if (str
[i
] == '\0') {
3816 program_error(ctx
, i
, "illegal character");
3817 grammar_destroy (arbprogram_syn_id
);
3823 /* copy the program string to a null-terminated string */
3824 strz
= (GLubyte
*) _mesa_malloc (len
+ 1);
3826 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glProgramStringARB");
3827 grammar_destroy (arbprogram_syn_id
);
3830 _mesa_memcpy (strz
, str
, len
);
3833 /* do a fast check on program string - initial production buffer is 4K */
3834 err
= !grammar_fast_check(arbprogram_syn_id
, strz
,
3835 &parsed
, &parsed_len
, 0x1000);
3837 /* Syntax parse error */
3839 grammar_get_last_error((GLubyte
*) error_msg
, 300, &error_pos
);
3840 program_error(ctx
, error_pos
, error_msg
);
3843 /* useful for debugging */
3847 fprintf(stderr
, "program: %s\n", (char *) strz
);
3848 fprintf(stderr
, "Error Pos: %d\n", ctx
->Program
.ErrorPos
);
3849 s
= (char *) _mesa_find_line_column(strz
, strz
+ctx
->Program
.ErrorPos
,
3851 fprintf(stderr
, "line %d col %d: %s\n", line
, col
, s
);
3858 grammar_destroy (arbprogram_syn_id
);
3862 grammar_destroy (arbprogram_syn_id
);
3865 * Program string is syntactically correct at this point
3866 * Parse the tokenized version of the program now, generating
3867 * vertex/fragment program instructions.
3870 /* Initialize the arb_program struct */
3871 program
->Base
.String
= strz
;
3872 program
->Base
.Instructions
= _mesa_alloc_instructions(MAX_PROGRAM_INSTRUCTIONS
);
3873 program
->Base
.NumInstructions
=
3874 program
->Base
.NumTemporaries
=
3875 program
->Base
.NumParameters
=
3876 program
->Base
.NumAttributes
= program
->Base
.NumAddressRegs
= 0;
3877 program
->Base
.Parameters
= _mesa_new_parameter_list ();
3878 program
->Base
.InputsRead
= 0x0;
3879 program
->Base
.OutputsWritten
= 0x0;
3880 program
->Position
= 0;
3881 program
->MajorVersion
= program
->MinorVersion
= 0;
3882 program
->PrecisionOption
= GL_DONT_CARE
;
3883 program
->FogOption
= GL_NONE
;
3884 program
->HintPositionInvariant
= GL_FALSE
;
3885 for (a
= 0; a
< MAX_TEXTURE_IMAGE_UNITS
; a
++)
3886 program
->TexturesUsed
[a
] = 0x0;
3887 program
->ShadowSamplers
= 0x0;
3888 program
->NumAluInstructions
=
3889 program
->NumTexInstructions
=
3890 program
->NumTexIndirections
= 0;
3891 program
->UsesKill
= 0;
3896 /* Start examining the tokens in the array */
3899 /* Check the grammer rev */
3900 if (*inst
++ != REVISION
) {
3901 program_error (ctx
, 0, "Grammar version mismatch");
3905 /* ignore program target */
3907 err
= parse_instructions(ctx
, inst
, &vc_head
, program
);
3910 /*debug_variables(ctx, vc_head, program); */
3912 /* We're done with the parsed binary array */
3913 var_cache_destroy (&vc_head
);
3915 _mesa_free (parsed
);
3917 /* Reallocate the instruction array from size [MAX_PROGRAM_INSTRUCTIONS]
3918 * to size [ap.Base.NumInstructions].
3920 program
->Base
.Instructions
3921 = _mesa_realloc_instructions(program
->Base
.Instructions
,
3922 MAX_PROGRAM_INSTRUCTIONS
,
3923 program
->Base
.NumInstructions
);
3931 _mesa_parse_arb_fragment_program(GLcontext
* ctx
, GLenum target
,
3932 const GLvoid
*str
, GLsizei len
,
3933 struct gl_fragment_program
*program
)
3935 struct gl_program prog
;
3936 struct asm_parser_state state
;
3939 ASSERT(target
== GL_FRAGMENT_PROGRAM_ARB
);
3941 memset(&prog
, 0, sizeof(prog
));
3942 memset(&state
, 0, sizeof(state
));
3945 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
,
3947 /* Error in the program. Just return. */
3951 /* Copy the relevant contents of the arb_program struct into the
3952 * fragment_program struct.
3954 program
->Base
.String
= prog
.String
;
3955 program
->Base
.NumInstructions
= prog
.NumInstructions
;
3956 program
->Base
.NumTemporaries
= prog
.NumTemporaries
;
3957 program
->Base
.NumParameters
= prog
.NumParameters
;
3958 program
->Base
.NumAttributes
= prog
.NumAttributes
;
3959 program
->Base
.NumAddressRegs
= prog
.NumAddressRegs
;
3960 program
->Base
.NumNativeInstructions
= prog
.NumNativeInstructions
;
3961 program
->Base
.NumNativeTemporaries
= prog
.NumNativeTemporaries
;
3962 program
->Base
.NumNativeParameters
= prog
.NumNativeParameters
;
3963 program
->Base
.NumNativeAttributes
= prog
.NumNativeAttributes
;
3964 program
->Base
.NumNativeAddressRegs
= prog
.NumNativeAddressRegs
;
3965 program
->Base
.NumAluInstructions
= prog
.NumAluInstructions
;
3966 program
->Base
.NumTexInstructions
= prog
.NumTexInstructions
;
3967 program
->Base
.NumTexIndirections
= prog
.NumTexIndirections
;
3968 program
->Base
.NumNativeAluInstructions
= prog
.NumAluInstructions
;
3969 program
->Base
.NumNativeTexInstructions
= prog
.NumTexInstructions
;
3970 program
->Base
.NumNativeTexIndirections
= prog
.NumTexIndirections
;
3971 program
->Base
.InputsRead
= prog
.InputsRead
;
3972 program
->Base
.OutputsWritten
= prog
.OutputsWritten
;
3973 for (i
= 0; i
< MAX_TEXTURE_IMAGE_UNITS
; i
++) {
3974 program
->Base
.TexturesUsed
[i
] = state
.fragment
.TexturesUsed
[i
];
3975 if (state
.fragment
.TexturesUsed
[i
])
3976 program
->Base
.SamplersUsed
|= (1 << i
);
3978 program
->Base
.ShadowSamplers
= state
.fragment
.ShadowSamplers
;
3979 switch (state
.option
.Fog
) {
3980 case OPTION_FOG_EXP
: program
->FogOption
= GL_EXP
; break;
3981 case OPTION_FOG_EXP2
: program
->FogOption
= GL_EXP2
; break;
3982 case OPTION_FOG_LINEAR
: program
->FogOption
= GL_LINEAR
; break;
3983 default: program
->FogOption
= GL_NONE
; break;
3986 program
->UsesKill
= state
.fragment
.UsesKill
;
3988 if (program
->FogOption
)
3989 program
->Base
.InputsRead
|= FRAG_BIT_FOGC
;
3991 /* XXX: assume that ARB fragment programs don't have access to the
3992 * FrontFacing and PointCoord values stuffed into the fog
3993 * coordinate in GLSL shaders.
3995 if (program
->Base
.InputsRead
& FRAG_BIT_FOGC
)
3996 program
->UsesFogFragCoord
= GL_TRUE
;
3998 if (program
->Base
.Instructions
)
3999 _mesa_free(program
->Base
.Instructions
);
4000 program
->Base
.Instructions
= prog
.Instructions
;
4002 if (program
->Base
.Parameters
)
4003 _mesa_free_parameter_list(program
->Base
.Parameters
);
4004 program
->Base
.Parameters
= prog
.Parameters
;
4006 /* Append fog instructions now if the program has "OPTION ARB_fog_exp"
4007 * or similar. We used to leave this up to drivers, but it appears
4008 * there's no hardware that wants to do fog in a discrete stage separate
4009 * from the fragment shader.
4011 if (program
->FogOption
!= GL_NONE
) {
4012 _mesa_append_fog_code(ctx
, program
);
4013 program
->FogOption
= GL_NONE
;
4017 _mesa_printf("____________Fragment program %u ________\n", program
->Base
.Id
);
4018 _mesa_print_program(&program
->Base
);
4025 * Parse the vertex program string. If success, update the given
4026 * vertex_program object with the new program. Else, leave the vertex_program
4030 _mesa_parse_arb_vertex_program(GLcontext
*ctx
, GLenum target
,
4031 const GLvoid
*str
, GLsizei len
,
4032 struct gl_vertex_program
*program
)
4034 struct gl_program prog
;
4035 struct asm_parser_state state
;
4037 ASSERT(target
== GL_VERTEX_PROGRAM_ARB
);
4039 memset(&prog
, 0, sizeof(prog
));
4040 memset(&state
, 0, sizeof(state
));
4043 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
,
4045 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glProgramString(bad program)");
4049 /* Copy the relevant contents of the arb_program struct into the
4050 * vertex_program struct.
4052 program
->Base
.String
= prog
.String
;
4053 program
->Base
.NumInstructions
= prog
.NumInstructions
;
4054 program
->Base
.NumTemporaries
= prog
.NumTemporaries
;
4055 program
->Base
.NumParameters
= prog
.NumParameters
;
4056 program
->Base
.NumAttributes
= prog
.NumAttributes
;
4057 program
->Base
.NumAddressRegs
= prog
.NumAddressRegs
;
4058 program
->Base
.NumNativeInstructions
= prog
.NumNativeInstructions
;
4059 program
->Base
.NumNativeTemporaries
= prog
.NumNativeTemporaries
;
4060 program
->Base
.NumNativeParameters
= prog
.NumNativeParameters
;
4061 program
->Base
.NumNativeAttributes
= prog
.NumNativeAttributes
;
4062 program
->Base
.NumNativeAddressRegs
= prog
.NumNativeAddressRegs
;
4063 program
->Base
.InputsRead
= prog
.InputsRead
;
4064 program
->Base
.OutputsWritten
= prog
.OutputsWritten
;
4065 program
->IsPositionInvariant
= (state
.option
.PositionInvariant
)
4066 ? GL_TRUE
: GL_FALSE
;
4068 if (program
->Base
.Instructions
)
4069 _mesa_free(program
->Base
.Instructions
);
4070 program
->Base
.Instructions
= prog
.Instructions
;
4072 if (program
->Base
.Parameters
)
4073 _mesa_free_parameter_list(program
->Base
.Parameters
);
4074 program
->Base
.Parameters
= prog
.Parameters
;
4077 _mesa_printf("____________Vertex program %u __________\n", program
->Base
.Id
);
4078 _mesa_print_program(&program
->Base
);
4079 _mesa_printf("inputs = 0x%04x, outputs = 0x%04x\n", program
->Base
.InputsRead
,
4080 program
->Base
.OutputsWritten
);