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"
68 * This is basically a union of the vertex_program and fragment_program
69 * structs that we can use to parse the program into
71 * XXX we can probably get rid of this entirely someday.
75 struct gl_program Base
;
77 GLuint Position
; /* Just used for error reporting while parsing */
81 /* ARB_vertex_progmra options */
82 GLboolean HintPositionInvariant
;
84 /* ARB_fragment_progmra options */
85 GLenum PrecisionOption
; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
86 GLenum FogOption
; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
88 /* ARB_fragment_program specifics */
89 GLbitfield TexturesUsed
[MAX_TEXTURE_IMAGE_UNITS
];
90 GLbitfield ShadowSamplers
;
91 GLuint NumAluInstructions
;
92 GLuint NumTexInstructions
;
93 GLuint NumTexIndirections
;
101 * Fragment Program Stuff:
102 * -----------------------------------------------------
104 * - things from Michal's email
106 * + not-overflowing floats (don't use parse_integer..)
107 * + can remove range checking in arbparse.c
109 * - check all limits of number of various variables
112 * - test! test! test!
114 * Vertex Program Stuff:
115 * -----------------------------------------------------
116 * - Optimize param array usage and count limits correctly, see spec,
118 * + Record if an array is reference absolutly or relatively (or both)
119 * + For absolute arrays, store a bitmap of accesses
120 * + For single parameters, store an access flag
121 * + After parsing, make a parameter cleanup and merging pass, where
122 * relative arrays are layed out first, followed by abs arrays, and
123 * finally single state.
124 * + Remap offsets for param src and dst registers
125 * + Now we can properly count parameter usage
127 * - Multiple state binding errors in param arrays (see spec, just before
132 * -----------------------------------------------------
133 * - User clipping planes vs. PositionInvariant
134 * - Is it sufficient to just multiply by the mvp to transform in the
135 * PositionInvariant case? Or do we need something more involved?
137 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
138 * - fetch state listed in program_parameters list
139 * + WTF should this go???
140 * + currently in nvvertexec.c and s_nvfragprog.c
142 * - allow for multiple address registers (and fetch address regs properly)
145 * -----------------------------------------------------
146 * - remove any leftover unused grammer.c stuff (dict_ ?)
147 * - fix grammer.c error handling so its not static
148 * - #ifdef around stuff pertaining to extentions
150 * Outstanding Questions:
151 * -----------------------------------------------------
152 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
153 * what gets hacked off because of this:
154 * + VERTEX_ATTRIB_MATRIXINDEX
155 * + VERTEX_ATTRIB_WEIGHT
159 * - When can we fetch env/local params from their own register files, and
160 * when to we have to fetch them into the main state register file?
164 * -----------------------------------------------------
167 /* Changes since moving the file to shader directory
169 2004-III-4 ------------------------------------------------------------
170 - added #include "grammar_mesa.h"
171 - removed grammar specific code part (it resides now in grammar.c)
172 - added GL_ARB_fragment_program_shadow tokens
173 - modified #include "arbparse_syn.h"
174 - major changes inside _mesa_parse_arb_program()
175 - check the program string for '\0' characters
176 - copy the program string to a one-byte-longer location to have
178 - position invariance test (not writing to result.position) moved
182 typedef GLubyte
*production
;
186 * This is the text describing the rules to parse the grammar
188 LONGSTRING
static char arb_grammar_text
[] =
189 #include "arbprogram_syn.h"
193 * These should match up with the values defined in arbprogram.syn
198 - changed and merged V_* and F_* opcode values to OP_*.
199 - added GL_ARB_fragment_program_shadow specific tokens (michal)
201 #define REVISION 0x0a
204 #define FRAGMENT_PROGRAM 0x01
205 #define VERTEX_PROGRAM 0x02
207 /* program section */
209 #define INSTRUCTION 0x02
210 #define DECLARATION 0x03
213 /* GL_ARB_fragment_program option */
214 #define ARB_PRECISION_HINT_FASTEST 0x00
215 #define ARB_PRECISION_HINT_NICEST 0x01
216 #define ARB_FOG_EXP 0x02
217 #define ARB_FOG_EXP2 0x03
218 #define ARB_FOG_LINEAR 0x04
220 /* GL_ARB_vertex_program option */
221 #define ARB_POSITION_INVARIANT 0x05
223 /* GL_ARB_fragment_program_shadow option */
224 #define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
226 /* GL_ARB_draw_buffers option */
227 #define ARB_DRAW_BUFFERS 0x07
229 /* GL_MESA_texture_array option */
230 #define MESA_TEXTURE_ARRAY 0x08
232 /* GL_ARB_fragment_program instruction class */
233 #define OP_ALU_INST 0x00
234 #define OP_TEX_INST 0x01
236 /* GL_ARB_vertex_program instruction class */
239 /* GL_ARB_fragment_program instruction type */
240 #define OP_ALU_VECTOR 0x00
241 #define OP_ALU_SCALAR 0x01
242 #define OP_ALU_BINSC 0x02
243 #define OP_ALU_BIN 0x03
244 #define OP_ALU_TRI 0x04
245 #define OP_ALU_SWZ 0x05
246 #define OP_TEX_SAMPLE 0x06
247 #define OP_TEX_KIL 0x07
249 /* GL_ARB_vertex_program instruction type */
250 #define OP_ALU_ARL 0x08
258 /* GL_ARB_fragment_program instruction code */
260 #define OP_ABS_SAT 0x1B
262 #define OP_FLR_SAT 0x26
264 #define OP_FRC_SAT 0x27
266 #define OP_LIT_SAT 0x2A
268 #define OP_MOV_SAT 0x30
270 #define OP_COS_SAT 0x20
272 #define OP_EX2_SAT 0x25
274 #define OP_LG2_SAT 0x29
276 #define OP_RCP_SAT 0x33
278 #define OP_RSQ_SAT 0x34
280 #define OP_SIN_SAT 0x39
282 #define OP_SCS_SAT 0x36
284 #define OP_POW_SAT 0x32
286 #define OP_ADD_SAT 0x1C
288 #define OP_DP3_SAT 0x21
290 #define OP_DP4_SAT 0x22
292 #define OP_DPH_SAT 0x23
294 #define OP_DST_SAT 0x24
296 #define OP_MAX_SAT 0x2E
298 #define OP_MIN_SAT 0x2F
300 #define OP_MUL_SAT 0x31
302 #define OP_SGE_SAT 0x37
304 #define OP_SLT_SAT 0x3A
306 #define OP_SUB_SAT 0x3B
308 #define OP_XPD_SAT 0x43
310 #define OP_CMP_SAT 0x1E
312 #define OP_LRP_SAT 0x2C
314 #define OP_MAD_SAT 0x2D
316 #define OP_SWZ_SAT 0x3C
318 #define OP_TEX_SAT 0x3E
320 #define OP_TXB_SAT 0x40
322 #define OP_TXP_SAT 0x42
325 /* GL_ARB_vertex_program instruction code */
354 /* fragment attribute binding */
355 #define FRAGMENT_ATTRIB_COLOR 0x01
356 #define FRAGMENT_ATTRIB_TEXCOORD 0x02
357 #define FRAGMENT_ATTRIB_FOGCOORD 0x03
358 #define FRAGMENT_ATTRIB_POSITION 0x04
360 /* vertex attribute binding */
361 #define VERTEX_ATTRIB_POSITION 0x01
362 #define VERTEX_ATTRIB_WEIGHT 0x02
363 #define VERTEX_ATTRIB_NORMAL 0x03
364 #define VERTEX_ATTRIB_COLOR 0x04
365 #define VERTEX_ATTRIB_FOGCOORD 0x05
366 #define VERTEX_ATTRIB_TEXCOORD 0x06
367 #define VERTEX_ATTRIB_MATRIXINDEX 0x07
368 #define VERTEX_ATTRIB_GENERIC 0x08
370 /* fragment result binding */
371 #define FRAGMENT_RESULT_COLOR 0x01
372 #define FRAGMENT_RESULT_DEPTH 0x02
374 /* vertex result binding */
375 #define VERTEX_RESULT_POSITION 0x01
376 #define VERTEX_RESULT_COLOR 0x02
377 #define VERTEX_RESULT_FOGCOORD 0x03
378 #define VERTEX_RESULT_POINTSIZE 0x04
379 #define VERTEX_RESULT_TEXCOORD 0x05
382 #define TEXTARGET_1D 0x01
383 #define TEXTARGET_2D 0x02
384 #define TEXTARGET_3D 0x03
385 #define TEXTARGET_RECT 0x04
386 #define TEXTARGET_CUBE 0x05
387 /* GL_ARB_fragment_program_shadow */
388 #define TEXTARGET_SHADOW1D 0x06
389 #define TEXTARGET_SHADOW2D 0x07
390 #define TEXTARGET_SHADOWRECT 0x08
391 /* GL_MESA_texture_array */
392 #define TEXTARGET_1D_ARRAY 0x09
393 #define TEXTARGET_2D_ARRAY 0x0a
394 #define TEXTARGET_SHADOW1D_ARRAY 0x0b
395 #define TEXTARGET_SHADOW2D_ARRAY 0x0c
398 #define FACE_FRONT 0x00
399 #define FACE_BACK 0x01
402 #define COLOR_PRIMARY 0x00
403 #define COLOR_SECONDARY 0x01
406 #define COMPONENT_X 0x00
407 #define COMPONENT_Y 0x01
408 #define COMPONENT_Z 0x02
409 #define COMPONENT_W 0x03
410 #define COMPONENT_0 0x04
411 #define COMPONENT_1 0x05
413 /* array index type */
414 #define ARRAY_INDEX_ABSOLUTE 0x00
415 #define ARRAY_INDEX_RELATIVE 0x01
418 #define MATRIX_MODELVIEW 0x01
419 #define MATRIX_PROJECTION 0x02
420 #define MATRIX_MVP 0x03
421 #define MATRIX_TEXTURE 0x04
422 #define MATRIX_PALETTE 0x05
423 #define MATRIX_PROGRAM 0x06
425 /* matrix modifier */
426 #define MATRIX_MODIFIER_IDENTITY 0x00
427 #define MATRIX_MODIFIER_INVERSE 0x01
428 #define MATRIX_MODIFIER_TRANSPOSE 0x02
429 #define MATRIX_MODIFIER_INVTRANS 0x03
432 #define CONSTANT_SCALAR 0x01
433 #define CONSTANT_VECTOR 0x02
435 /* program param type */
436 #define PROGRAM_PARAM_ENV 0x01
437 #define PROGRAM_PARAM_LOCAL 0x02
440 #define REGISTER_ATTRIB 0x01
441 #define REGISTER_PARAM 0x02
442 #define REGISTER_RESULT 0x03
443 #define REGISTER_ESTABLISHED_NAME 0x04
446 #define PARAM_NULL 0x00
447 #define PARAM_ARRAY_ELEMENT 0x01
448 #define PARAM_STATE_ELEMENT 0x02
449 #define PARAM_PROGRAM_ELEMENT 0x03
450 #define PARAM_PROGRAM_ELEMENTS 0x04
451 #define PARAM_CONSTANT 0x05
453 /* param state property */
454 #define STATE_MATERIAL_PARSER 0x01
455 #define STATE_LIGHT_PARSER 0x02
456 #define STATE_LIGHT_MODEL 0x03
457 #define STATE_LIGHT_PROD 0x04
458 #define STATE_FOG 0x05
459 #define STATE_MATRIX_ROWS 0x06
460 /* GL_ARB_fragment_program */
461 #define STATE_TEX_ENV 0x07
462 #define STATE_DEPTH 0x08
463 /* GL_ARB_vertex_program */
464 #define STATE_TEX_GEN 0x09
465 #define STATE_CLIP_PLANE 0x0A
466 #define STATE_POINT 0x0B
468 /* state material property */
469 #define MATERIAL_AMBIENT 0x01
470 #define MATERIAL_DIFFUSE 0x02
471 #define MATERIAL_SPECULAR 0x03
472 #define MATERIAL_EMISSION 0x04
473 #define MATERIAL_SHININESS 0x05
475 /* state light property */
476 #define LIGHT_AMBIENT 0x01
477 #define LIGHT_DIFFUSE 0x02
478 #define LIGHT_SPECULAR 0x03
479 #define LIGHT_POSITION 0x04
480 #define LIGHT_ATTENUATION 0x05
481 #define LIGHT_HALF 0x06
482 #define LIGHT_SPOT_DIRECTION 0x07
484 /* state light model property */
485 #define LIGHT_MODEL_AMBIENT 0x01
486 #define LIGHT_MODEL_SCENECOLOR 0x02
488 /* state light product property */
489 #define LIGHT_PROD_AMBIENT 0x01
490 #define LIGHT_PROD_DIFFUSE 0x02
491 #define LIGHT_PROD_SPECULAR 0x03
493 /* state texture environment property */
494 #define TEX_ENV_COLOR 0x01
496 /* state texture generation coord property */
497 #define TEX_GEN_EYE 0x01
498 #define TEX_GEN_OBJECT 0x02
500 /* state fog property */
501 #define FOG_COLOR 0x01
502 #define FOG_PARAMS 0x02
504 /* state depth property */
505 #define DEPTH_RANGE 0x01
507 /* state point parameters property */
508 #define POINT_SIZE 0x01
509 #define POINT_ATTENUATION 0x02
517 /* GL_ARB_vertex_program */
520 /*-----------------------------------------------------------------------
521 * From here on down is the semantic checking portion
526 * Variable Table Handling functions
541 * Setting an explicit field for each of the binding properties is a bit
542 * wasteful of space, but it should be much more clear when reading later on..
546 const GLubyte
*name
; /* don't free() - no need */
548 GLuint address_binding
; /* The index of the address register we should
550 GLuint attrib_binding
; /* For type vt_attrib, see nvfragprog.h for values */
551 GLuint attrib_is_generic
; /* If the attrib was specified through a generic
553 GLuint temp_binding
; /* The index of the temp register we are to use */
554 GLuint output_binding
; /* Output/result register number */
555 struct var_cache
*alias_binding
; /* For type vt_alias, points to the var_cache entry
556 * that this is aliased to */
557 GLuint param_binding_type
; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
558 * PROGRAM_ENV_PARAM} */
559 GLuint param_binding_begin
; /* This is the offset into the program_parameter_list where
560 * the tokens representing our bound state (or constants)
562 GLuint param_binding_length
; /* This is how many entries in the the program_parameter_list
563 * we take up with our state tokens or constants. Note that
564 * this is _not_ the same as the number of param registers
565 * we eventually use */
566 struct var_cache
*next
;
570 var_cache_create (struct var_cache
**va
)
572 *va
= (struct var_cache
*) _mesa_malloc (sizeof (struct var_cache
));
575 (**va
).type
= vt_none
;
576 (**va
).attrib_binding
= ~0;
577 (**va
).attrib_is_generic
= 0;
578 (**va
).temp_binding
= ~0;
579 (**va
).output_binding
= ~0;
580 (**va
).param_binding_type
= ~0;
581 (**va
).param_binding_begin
= ~0;
582 (**va
).param_binding_length
= ~0;
583 (**va
).alias_binding
= NULL
;
589 var_cache_destroy (struct var_cache
**va
)
592 var_cache_destroy (&(**va
).next
);
599 var_cache_append (struct var_cache
**va
, struct var_cache
*nv
)
602 var_cache_append (&(**va
).next
, nv
);
607 static struct var_cache
*
608 var_cache_find (struct var_cache
*va
, const GLubyte
* name
)
610 /*struct var_cache *first = va;*/
613 if (!_mesa_strcmp ( (const char*) name
, (const char*) va
->name
)) {
614 if (va
->type
== vt_alias
)
615 return va
->alias_binding
;
628 * Called when an error is detected while parsing/compiling a program.
629 * Sets the ctx->Program.ErrorString field to descript and records a
630 * GL_INVALID_OPERATION error.
631 * \param position position of error in program string
632 * \param descrip verbose error description
635 program_error(GLcontext
*ctx
, GLint position
, const char *descrip
)
638 const char *prefix
= "glProgramString(", *suffix
= ")";
639 char *str
= (char *) _mesa_malloc(_mesa_strlen(descrip
) +
640 _mesa_strlen(prefix
) +
641 _mesa_strlen(suffix
) + 1);
643 _mesa_sprintf(str
, "%s%s%s", prefix
, descrip
, suffix
);
644 _mesa_error(ctx
, GL_INVALID_OPERATION
, str
);
648 _mesa_set_program_error(ctx
, position
, descrip
);
653 * As above, but with an extra string parameter for more info.
656 program_error2(GLcontext
*ctx
, GLint position
, const char *descrip
,
660 const char *prefix
= "glProgramString(", *suffix
= ")";
661 char *str
= (char *) _mesa_malloc(_mesa_strlen(descrip
) +
664 _mesa_strlen(prefix
) +
665 _mesa_strlen(suffix
) + 1);
667 _mesa_sprintf(str
, "%s%s: %s%s", prefix
, descrip
, var
, suffix
);
668 _mesa_error(ctx
, GL_INVALID_OPERATION
, str
);
673 char *str
= (char *) _mesa_malloc(_mesa_strlen(descrip
) +
675 _mesa_strlen(var
) + 1);
677 _mesa_sprintf(str
, "%s: %s", descrip
, var
);
679 _mesa_set_program_error(ctx
, position
, str
);
689 * constructs an integer from 4 GLubytes in LE format
692 parse_position (const GLubyte
** inst
)
696 value
= (GLuint
) (*(*inst
)++);
697 value
+= (GLuint
) (*(*inst
)++) * 0x100;
698 value
+= (GLuint
) (*(*inst
)++) * 0x10000;
699 value
+= (GLuint
) (*(*inst
)++) * 0x1000000;
705 * This will, given a string, lookup the string as a variable name in the
706 * var cache. If the name is found, the var cache node corresponding to the
707 * var name is returned. If it is not found, a new entry is allocated
709 * \param I Points into the binary array where the string identifier begins
710 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
711 * \return The location on the var_cache corresponding the the string starting at I
713 static struct var_cache
*
714 parse_string (const GLubyte
** inst
, struct var_cache
**vc_head
,
715 struct arb_program
*Program
, GLuint
* found
)
717 const GLubyte
*i
= *inst
;
718 struct var_cache
*va
= NULL
;
721 *inst
+= _mesa_strlen ((char *) i
) + 1;
723 va
= var_cache_find (*vc_head
, i
);
731 var_cache_create (&va
);
732 va
->name
= (const GLubyte
*) i
;
734 var_cache_append (vc_head
, va
);
740 parse_string_without_adding (const GLubyte
** inst
, struct arb_program
*Program
)
742 const GLubyte
*i
= *inst
;
745 *inst
+= _mesa_strlen ((char *) i
) + 1;
751 * \return -1 if we parse '-', return 1 otherwise
754 parse_sign (const GLubyte
** inst
)
756 /*return *(*inst)++ != '+'; */
762 else if (**inst
== '+') {
771 * parses and returns signed integer
774 parse_integer (const GLubyte
** inst
, struct arb_program
*Program
)
779 /* check if *inst points to '+' or '-'
780 * if yes, grab the sign and increment *inst
782 sign
= parse_sign (inst
);
784 /* now check if *inst points to 0
785 * if yes, increment the *inst and return the default value
792 /* parse the integer as you normally would do it */
793 value
= _mesa_atoi (parse_string_without_adding (inst
, Program
));
795 /* now, after terminating 0 there is a position
796 * to parse it - parse_position()
798 Program
->Position
= parse_position (inst
);
804 Accumulate this string of digits, and return them as
805 a large integer represented in floating point (for range).
806 If scale is not NULL, also accumulates a power-of-ten
807 integer scale factor that represents the number of digits
811 parse_float_string(const GLubyte
** inst
, struct arb_program
*Program
, GLdouble
*scale
)
813 GLdouble value
= 0.0;
814 GLdouble oscale
= 1.0;
816 if (**inst
== 0) { /* this string of digits is empty-- do nothing */
819 else { /* nonempty string-- parse out the digits */
820 while (**inst
>= '0' && **inst
<= '9') {
821 GLubyte digit
= *((*inst
)++);
822 value
= value
* 10.0 + (GLint
) (digit
- '0');
825 assert(**inst
== 0); /* integer string should end with 0 */
826 (*inst
)++; /* skip over terminating 0 */
827 Program
->Position
= parse_position(inst
); /* skip position (from integer) */
835 Parse an unsigned floating-point number from this stream of tokenized
836 characters. Example floating-point formats supported:
844 parse_float (const GLubyte
** inst
, struct arb_program
*Program
)
847 GLdouble whole
, fraction
, fracScale
= 1.0;
849 whole
= parse_float_string(inst
, Program
, 0);
850 fraction
= parse_float_string(inst
, Program
, &fracScale
);
852 /* Parse signed exponent */
853 exponent
= parse_integer(inst
, Program
); /* This is the exponent */
855 /* Assemble parts of floating-point number: */
856 return (GLfloat
) ((whole
+ fraction
/ fracScale
) *
857 _mesa_pow(10.0, (GLfloat
) exponent
));
864 parse_signed_float (const GLubyte
** inst
, struct arb_program
*Program
)
866 GLint sign
= parse_sign (inst
);
867 GLfloat value
= parse_float (inst
, Program
);
872 * This picks out a constant value from the parsed array. The constant vector is r
873 * returned in the *values array, which should be of length 4.
875 * \param values - The 4 component vector with the constant value in it
878 parse_constant (const GLubyte
** inst
, GLfloat
*values
, struct arb_program
*Program
,
881 GLuint components
, i
;
884 switch (*(*inst
)++) {
885 case CONSTANT_SCALAR
:
886 if (use
== GL_TRUE
) {
889 values
[2] = values
[3] = parse_float (inst
, Program
);
894 values
[2] = values
[3] = parse_signed_float (inst
, Program
);
898 case CONSTANT_VECTOR
:
899 values
[0] = values
[1] = values
[2] = 0;
901 components
= *(*inst
)++;
902 for (i
= 0; i
< components
; i
++) {
903 values
[i
] = parse_signed_float (inst
, Program
);
910 * \param offset The offset from the address register that we should
913 * \return 0 on sucess, 1 on error
916 parse_relative_offset(GLcontext
*ctx
, const GLubyte
**inst
,
917 struct arb_program
*Program
, GLint
*offset
)
920 *offset
= parse_integer(inst
, Program
);
925 * \param color 0 if color type is primary, 1 if color type is secondary
926 * \return 0 on sucess, 1 on error
929 parse_color_type (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
932 (void) ctx
; (void) Program
;
933 *color
= *(*inst
)++ != COLOR_PRIMARY
;
938 * Get an integer corresponding to a generic vertex attribute.
940 * \return 0 on sucess, 1 on error
943 parse_generic_attrib_num(GLcontext
*ctx
, const GLubyte
** inst
,
944 struct arb_program
*Program
, GLuint
*attrib
)
946 GLint i
= parse_integer(inst
, Program
);
948 if ((i
< 0) || (i
>= MAX_VERTEX_PROGRAM_ATTRIBS
))
950 program_error(ctx
, Program
->Position
,
951 "Invalid generic vertex attribute index");
955 *attrib
= (GLuint
) i
;
962 * \param color The index of the color buffer to write into
963 * \return 0 on sucess, 1 on error
966 parse_output_color_num (GLcontext
* ctx
, const GLubyte
** inst
,
967 struct arb_program
*Program
, GLuint
* color
)
969 GLint i
= parse_integer (inst
, Program
);
971 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxDrawBuffers
)) {
973 program_error(ctx
, Program
->Position
, "Invalid draw buffer index");
983 * Validate the index of a texture coordinate
985 * \param coord The texture unit index
986 * \return 0 on sucess, 1 on error
989 parse_texcoord_num (GLcontext
* ctx
, const GLubyte
** inst
,
990 struct arb_program
*Program
, GLuint
* coord
)
992 GLint i
= parse_integer (inst
, Program
);
994 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxTextureCoordUnits
)) {
995 program_error(ctx
, Program
->Position
, "Invalid texture coordinate index");
1005 * Validate the index of a texture image unit
1007 * \param coord The texture unit index
1008 * \return 0 on sucess, 1 on error
1011 parse_teximage_num (GLcontext
* ctx
, const GLubyte
** inst
,
1012 struct arb_program
*Program
, GLuint
* coord
)
1014 GLint i
= parse_integer (inst
, Program
);
1016 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxTextureImageUnits
)) {
1017 program_error(ctx
, Program
->Position
, "Invalid texture image index");
1021 *coord
= (GLuint
) i
;
1027 * \param coord The weight index
1028 * \return 0 on sucess, 1 on error
1031 parse_weight_num (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
1034 *coord
= parse_integer (inst
, Program
);
1036 if ((*coord
< 0) || (*coord
>= 1)) {
1037 program_error(ctx
, Program
->Position
, "Invalid weight index");
1045 * \param coord The clip plane index
1046 * \return 0 on sucess, 1 on error
1049 parse_clipplane_num (GLcontext
* ctx
, const GLubyte
** inst
,
1050 struct arb_program
*Program
, GLint
* coord
)
1052 *coord
= parse_integer (inst
, Program
);
1054 if ((*coord
< 0) || (*coord
>= (GLint
) ctx
->Const
.MaxClipPlanes
)) {
1055 program_error(ctx
, Program
->Position
, "Invalid clip plane index");
1064 * \return 0 on front face, 1 on back face
1067 parse_face_type (const GLubyte
** inst
)
1069 switch (*(*inst
)++) {
1081 * Given a matrix and a modifier token on the binary array, return tokens
1082 * that _mesa_fetch_state() [program.c] can understand.
1084 * \param matrix - the matrix we are talking about
1085 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
1086 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
1087 * \return 0 on sucess, 1 on failure
1090 parse_matrix (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
1091 GLint
* matrix
, GLint
* matrix_idx
, GLint
* matrix_modifier
)
1093 GLubyte mat
= *(*inst
)++;
1098 case MATRIX_MODELVIEW
:
1099 *matrix
= STATE_MODELVIEW_MATRIX
;
1100 *matrix_idx
= parse_integer (inst
, Program
);
1101 if (*matrix_idx
> 0) {
1102 program_error(ctx
, Program
->Position
,
1103 "ARB_vertex_blend not supported");
1108 case MATRIX_PROJECTION
:
1109 *matrix
= STATE_PROJECTION_MATRIX
;
1113 *matrix
= STATE_MVP_MATRIX
;
1116 case MATRIX_TEXTURE
:
1117 *matrix
= STATE_TEXTURE_MATRIX
;
1118 *matrix_idx
= parse_integer (inst
, Program
);
1119 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxTextureUnits
) {
1120 program_error(ctx
, Program
->Position
, "Invalid Texture Unit");
1121 /* bad *matrix_id */
1126 /* This is not currently supported (ARB_matrix_palette) */
1127 case MATRIX_PALETTE
:
1128 *matrix_idx
= parse_integer (inst
, Program
);
1129 program_error(ctx
, Program
->Position
,
1130 "ARB_matrix_palette not supported");
1134 case MATRIX_PROGRAM
:
1135 *matrix
= STATE_PROGRAM_MATRIX
;
1136 *matrix_idx
= parse_integer (inst
, Program
);
1137 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxProgramMatrices
) {
1138 program_error(ctx
, Program
->Position
, "Invalid Program Matrix");
1139 /* bad *matrix_idx */
1145 switch (*(*inst
)++) {
1146 case MATRIX_MODIFIER_IDENTITY
:
1147 *matrix_modifier
= 0;
1149 case MATRIX_MODIFIER_INVERSE
:
1150 *matrix_modifier
= STATE_MATRIX_INVERSE
;
1152 case MATRIX_MODIFIER_TRANSPOSE
:
1153 *matrix_modifier
= STATE_MATRIX_TRANSPOSE
;
1155 case MATRIX_MODIFIER_INVTRANS
:
1156 *matrix_modifier
= STATE_MATRIX_INVTRANS
;
1165 * This parses a state string (rather, the binary version of it) into
1166 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1168 * \param inst - the start in the binary arry to start working from
1169 * \param state_tokens - the storage for the 6-token state description
1170 * \return - 0 on sucess, 1 on error
1173 parse_state_single_item (GLcontext
* ctx
, const GLubyte
** inst
,
1174 struct arb_program
*Program
,
1175 gl_state_index state_tokens
[STATE_LENGTH
])
1177 GLubyte token
= *(*inst
)++;
1180 case STATE_MATERIAL_PARSER
:
1181 state_tokens
[0] = STATE_MATERIAL
;
1182 state_tokens
[1] = parse_face_type (inst
);
1183 switch (*(*inst
)++) {
1184 case MATERIAL_AMBIENT
:
1185 state_tokens
[2] = STATE_AMBIENT
;
1187 case MATERIAL_DIFFUSE
:
1188 state_tokens
[2] = STATE_DIFFUSE
;
1190 case MATERIAL_SPECULAR
:
1191 state_tokens
[2] = STATE_SPECULAR
;
1193 case MATERIAL_EMISSION
:
1194 state_tokens
[2] = STATE_EMISSION
;
1196 case MATERIAL_SHININESS
:
1197 state_tokens
[2] = STATE_SHININESS
;
1202 case STATE_LIGHT_PARSER
:
1203 state_tokens
[0] = STATE_LIGHT
;
1204 state_tokens
[1] = parse_integer (inst
, Program
);
1206 /* Check the value of state_tokens[1] against the # of lights */
1207 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1208 program_error(ctx
, Program
->Position
, "Invalid Light Number");
1209 /* bad state_tokens[1] */
1213 switch (*(*inst
)++) {
1215 state_tokens
[2] = STATE_AMBIENT
;
1218 state_tokens
[2] = STATE_DIFFUSE
;
1220 case LIGHT_SPECULAR
:
1221 state_tokens
[2] = STATE_SPECULAR
;
1223 case LIGHT_POSITION
:
1224 state_tokens
[2] = STATE_POSITION
;
1226 case LIGHT_ATTENUATION
:
1227 state_tokens
[2] = STATE_ATTENUATION
;
1230 state_tokens
[2] = STATE_HALF_VECTOR
;
1232 case LIGHT_SPOT_DIRECTION
:
1233 state_tokens
[2] = STATE_SPOT_DIRECTION
;
1238 case STATE_LIGHT_MODEL
:
1239 switch (*(*inst
)++) {
1240 case LIGHT_MODEL_AMBIENT
:
1241 state_tokens
[0] = STATE_LIGHTMODEL_AMBIENT
;
1243 case LIGHT_MODEL_SCENECOLOR
:
1244 state_tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
1245 state_tokens
[1] = parse_face_type (inst
);
1250 case STATE_LIGHT_PROD
:
1251 state_tokens
[0] = STATE_LIGHTPROD
;
1252 state_tokens
[1] = parse_integer (inst
, Program
);
1254 /* Check the value of state_tokens[1] against the # of lights */
1255 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1256 program_error(ctx
, Program
->Position
, "Invalid Light Number");
1257 /* bad state_tokens[1] */
1261 state_tokens
[2] = parse_face_type (inst
);
1262 switch (*(*inst
)++) {
1263 case LIGHT_PROD_AMBIENT
:
1264 state_tokens
[3] = STATE_AMBIENT
;
1266 case LIGHT_PROD_DIFFUSE
:
1267 state_tokens
[3] = STATE_DIFFUSE
;
1269 case LIGHT_PROD_SPECULAR
:
1270 state_tokens
[3] = STATE_SPECULAR
;
1277 switch (*(*inst
)++) {
1279 state_tokens
[0] = STATE_FOG_COLOR
;
1282 state_tokens
[0] = STATE_FOG_PARAMS
;
1288 state_tokens
[1] = parse_integer (inst
, Program
);
1289 switch (*(*inst
)++) {
1291 state_tokens
[0] = STATE_TEXENV_COLOR
;
1300 state_tokens
[0] = STATE_TEXGEN
;
1301 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1303 if (parse_texcoord_num (ctx
, inst
, Program
, &coord
))
1305 state_tokens
[1] = coord
;
1310 /* 0 - s, 1 - t, 2 - r, 3 - q */
1313 if (type
== TEX_GEN_EYE
) {
1316 state_tokens
[2] = STATE_TEXGEN_EYE_S
;
1319 state_tokens
[2] = STATE_TEXGEN_EYE_T
;
1322 state_tokens
[2] = STATE_TEXGEN_EYE_R
;
1325 state_tokens
[2] = STATE_TEXGEN_EYE_Q
;
1328 _mesa_problem(ctx
, "bad texgen component in "
1329 "parse_state_single_item()");
1335 state_tokens
[2] = STATE_TEXGEN_OBJECT_S
;
1338 state_tokens
[2] = STATE_TEXGEN_OBJECT_T
;
1341 state_tokens
[2] = STATE_TEXGEN_OBJECT_R
;
1344 state_tokens
[2] = STATE_TEXGEN_OBJECT_Q
;
1347 _mesa_problem(ctx
, "bad texgen component in "
1348 "parse_state_single_item()");
1355 switch (*(*inst
)++) {
1357 state_tokens
[0] = STATE_DEPTH_RANGE
;
1362 case STATE_CLIP_PLANE
:
1363 state_tokens
[0] = STATE_CLIPPLANE
;
1364 if (parse_clipplane_num (ctx
, inst
, Program
,
1365 (GLint
*) &state_tokens
[1]))
1370 switch (*(*inst
)++) {
1372 state_tokens
[0] = STATE_POINT_SIZE
;
1375 case POINT_ATTENUATION
:
1376 state_tokens
[0] = STATE_POINT_ATTENUATION
;
1381 /* XXX: I think this is the correct format for a matrix row */
1382 case STATE_MATRIX_ROWS
:
1383 if (parse_matrix(ctx
, inst
, Program
,
1384 (GLint
*) &state_tokens
[0],
1385 (GLint
*) &state_tokens
[1],
1386 (GLint
*) &state_tokens
[4]))
1389 state_tokens
[2] = parse_integer (inst
, Program
); /* The first row to grab */
1391 if ((**inst
) != 0) { /* Either the last row, 0 */
1392 state_tokens
[3] = parse_integer (inst
, Program
);
1393 if (state_tokens
[3] < state_tokens
[2]) {
1394 program_error(ctx
, Program
->Position
,
1395 "Second matrix index less than the first");
1396 /* state_tokens[4] vs. state_tokens[3] */
1401 state_tokens
[3] = state_tokens
[2];
1411 * This parses a state string (rather, the binary version of it) into
1412 * a 6-token similar for the state fetching code in program.c
1414 * One might ask, why fetch these parameters into just like you fetch
1415 * state when they are already stored in other places?
1417 * Because of array offsets -> We can stick env/local parameters in the
1418 * middle of a parameter array and then index someplace into the array
1421 * One optimization might be to only do this for the cases where the
1422 * env/local parameters end up inside of an array, and leave the
1423 * single parameters (or arrays of pure env/local pareameters) in their
1424 * respective register files.
1426 * For ENV parameters, the format is:
1427 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1428 * state_tokens[1] = STATE_ENV
1429 * state_tokens[2] = the parameter index
1431 * for LOCAL parameters, the format is:
1432 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1433 * state_tokens[1] = STATE_LOCAL
1434 * state_tokens[2] = the parameter index
1436 * \param inst - the start in the binary arry to start working from
1437 * \param state_tokens - the storage for the 6-token state description
1438 * \return - 0 on sucess, 1 on failure
1441 parse_program_single_item (GLcontext
* ctx
, const GLubyte
** inst
,
1442 struct arb_program
*Program
,
1443 gl_state_index state_tokens
[STATE_LENGTH
])
1445 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1446 state_tokens
[0] = STATE_FRAGMENT_PROGRAM
;
1448 state_tokens
[0] = STATE_VERTEX_PROGRAM
;
1451 switch (*(*inst
)++) {
1452 case PROGRAM_PARAM_ENV
:
1453 state_tokens
[1] = STATE_ENV
;
1454 state_tokens
[2] = parse_integer (inst
, Program
);
1456 /* Check state_tokens[2] against the number of ENV parameters available */
1457 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1458 (state_tokens
[2] >= (GLint
) ctx
->Const
.FragmentProgram
.MaxEnvParams
))
1460 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1461 (state_tokens
[2] >= (GLint
) ctx
->Const
.VertexProgram
.MaxEnvParams
))) {
1462 program_error(ctx
, Program
->Position
,
1463 "Invalid Program Env Parameter");
1464 /* bad state_tokens[2] */
1470 case PROGRAM_PARAM_LOCAL
:
1471 state_tokens
[1] = STATE_LOCAL
;
1472 state_tokens
[2] = parse_integer (inst
, Program
);
1474 /* Check state_tokens[2] against the number of LOCAL parameters available */
1475 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1476 (state_tokens
[2] >= (GLint
) ctx
->Const
.FragmentProgram
.MaxLocalParams
))
1478 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1479 (state_tokens
[2] >= (GLint
) ctx
->Const
.VertexProgram
.MaxLocalParams
))) {
1480 program_error(ctx
, Program
->Position
,
1481 "Invalid Program Local Parameter");
1482 /* bad state_tokens[2] */
1492 * For ARB_vertex_program, programs are not allowed to use both an explicit
1493 * vertex attribute and a generic vertex attribute corresponding to the same
1494 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1496 * This will walk our var_cache and make sure that nobody does anything fishy.
1498 * \return 0 on sucess, 1 on error
1501 generic_attrib_check(struct var_cache
*vc_head
)
1504 struct var_cache
*curr
;
1505 GLboolean explicitAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
],
1506 genericAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
];
1508 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1509 explicitAttrib
[a
] = GL_FALSE
;
1510 genericAttrib
[a
] = GL_FALSE
;
1515 if (curr
->type
== vt_attrib
) {
1516 if (curr
->attrib_is_generic
)
1517 genericAttrib
[ curr
->attrib_binding
] = GL_TRUE
;
1519 explicitAttrib
[ curr
->attrib_binding
] = GL_TRUE
;
1525 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1526 if ((explicitAttrib
[a
]) && (genericAttrib
[a
]))
1534 * This will handle the binding side of an ATTRIB var declaration
1536 * \param inputReg returns the input register index, one of the
1537 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
1538 * \return returns 0 on success, 1 on error
1541 parse_attrib_binding(GLcontext
* ctx
, const GLubyte
** inst
,
1542 struct arb_program
*Program
,
1543 GLuint
*inputReg
, GLuint
*is_generic
)
1549 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1550 switch (*(*inst
)++) {
1551 case FRAGMENT_ATTRIB_COLOR
:
1554 err
= parse_color_type (ctx
, inst
, Program
, &coord
);
1555 *inputReg
= FRAG_ATTRIB_COL0
+ coord
;
1558 case FRAGMENT_ATTRIB_TEXCOORD
:
1560 GLuint texcoord
= 0;
1561 err
= parse_texcoord_num (ctx
, inst
, Program
, &texcoord
);
1562 *inputReg
= FRAG_ATTRIB_TEX0
+ texcoord
;
1565 case FRAGMENT_ATTRIB_FOGCOORD
:
1566 *inputReg
= FRAG_ATTRIB_FOGC
;
1568 case FRAGMENT_ATTRIB_POSITION
:
1569 *inputReg
= FRAG_ATTRIB_WPOS
;
1577 switch (*(*inst
)++) {
1578 case VERTEX_ATTRIB_POSITION
:
1579 *inputReg
= VERT_ATTRIB_POS
;
1582 case VERTEX_ATTRIB_WEIGHT
:
1585 err
= parse_weight_num (ctx
, inst
, Program
, &weight
);
1586 *inputReg
= VERT_ATTRIB_WEIGHT
;
1588 /* hack for Warcraft (see bug 8060) */
1589 _mesa_warning(ctx
, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
1592 program_error(ctx
, Program
->Position
,
1593 "ARB_vertex_blend not supported");
1598 case VERTEX_ATTRIB_NORMAL
:
1599 *inputReg
= VERT_ATTRIB_NORMAL
;
1602 case VERTEX_ATTRIB_COLOR
:
1605 err
= parse_color_type (ctx
, inst
, Program
, &color
);
1607 *inputReg
= VERT_ATTRIB_COLOR1
;
1610 *inputReg
= VERT_ATTRIB_COLOR0
;
1615 case VERTEX_ATTRIB_FOGCOORD
:
1616 *inputReg
= VERT_ATTRIB_FOG
;
1619 case VERTEX_ATTRIB_TEXCOORD
:
1622 err
= parse_texcoord_num (ctx
, inst
, Program
, &unit
);
1623 *inputReg
= VERT_ATTRIB_TEX0
+ unit
;
1627 case VERTEX_ATTRIB_MATRIXINDEX
:
1628 /* Not supported at this time */
1630 const char *msg
= "ARB_palette_matrix not supported";
1631 parse_integer (inst
, Program
);
1632 program_error(ctx
, Program
->Position
, msg
);
1636 case VERTEX_ATTRIB_GENERIC
:
1639 err
= parse_generic_attrib_num(ctx
, inst
, Program
, &attrib
);
1642 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1643 * attributes do not alias the conventional vertex
1647 *inputReg
= attrib
+ VERT_ATTRIB_GENERIC0
;
1661 program_error(ctx
, Program
->Position
, "Bad attribute binding");
1669 * This translates between a binary token for an output variable type
1670 * and the mesa token for the same thing.
1672 * \param inst The parsed tokens
1673 * \param outputReg Returned index/number of the output register,
1674 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
1677 parse_result_binding(GLcontext
*ctx
, const GLubyte
**inst
,
1678 GLuint
*outputReg
, struct arb_program
*Program
)
1680 const GLubyte token
= *(*inst
)++;
1683 case FRAGMENT_RESULT_COLOR
:
1684 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1687 /* This gets result of the color buffer we're supposed to
1688 * draw into. This pertains to GL_ARB_draw_buffers.
1690 parse_output_color_num(ctx
, inst
, Program
, &out_color
);
1691 ASSERT(out_color
< MAX_DRAW_BUFFERS
);
1692 *outputReg
= FRAG_RESULT_COLOR
;
1695 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1696 *outputReg
= VERT_RESULT_HPOS
;
1700 case FRAGMENT_RESULT_DEPTH
:
1701 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1702 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
1703 *outputReg
= FRAG_RESULT_DEPTH
;
1706 /* for vtx programs, this is VERTEX_RESULT_COLOR */
1708 GLuint face_type
= parse_face_type(inst
);
1709 GLint err
= parse_color_type(ctx
, inst
, Program
, &color_type
);
1716 *outputReg
= VERT_RESULT_BFC1
; /* secondary color */
1719 *outputReg
= VERT_RESULT_BFC0
; /* primary color */
1725 *outputReg
= VERT_RESULT_COL1
; /* secondary color */
1729 *outputReg
= VERT_RESULT_COL0
; /* primary color */
1735 case VERTEX_RESULT_FOGCOORD
:
1736 *outputReg
= VERT_RESULT_FOGC
;
1739 case VERTEX_RESULT_POINTSIZE
:
1740 *outputReg
= VERT_RESULT_PSIZ
;
1743 case VERTEX_RESULT_TEXCOORD
:
1746 if (parse_texcoord_num (ctx
, inst
, Program
, &unit
))
1748 *outputReg
= VERT_RESULT_TEX0
+ unit
;
1753 Program
->Base
.OutputsWritten
|= (1 << *outputReg
);
1760 * This handles the declaration of ATTRIB variables
1763 * parse_vert_attrib_binding(), or something like that
1765 * \return 0 on sucess, 1 on error
1768 parse_attrib (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1769 struct arb_program
*Program
)
1772 struct var_cache
*attrib_var
;
1774 attrib_var
= parse_string (inst
, vc_head
, Program
, &found
);
1775 Program
->Position
= parse_position (inst
);
1777 program_error2(ctx
, Program
->Position
,
1778 "Duplicate variable declaration",
1779 (char *) attrib_var
->name
);
1783 attrib_var
->type
= vt_attrib
;
1785 if (parse_attrib_binding(ctx
, inst
, Program
, &attrib_var
->attrib_binding
,
1786 &attrib_var
->attrib_is_generic
))
1789 if (generic_attrib_check(*vc_head
)) {
1790 program_error(ctx
, Program
->Position
,
1791 "Cannot use both a generic vertex attribute "
1792 "and a specific attribute of the same type");
1796 Program
->Base
.NumAttributes
++;
1801 * \param use -- TRUE if we're called when declaring implicit parameters,
1802 * FALSE if we're declaraing variables. This has to do with
1803 * if we get a signed or unsigned float for scalar constants
1806 parse_param_elements (GLcontext
* ctx
, const GLubyte
** inst
,
1807 struct var_cache
*param_var
,
1808 struct arb_program
*Program
, GLboolean use
)
1812 gl_state_index state_tokens
[STATE_LENGTH
] = {0, 0, 0, 0, 0};
1813 GLfloat const_values
[4];
1815 GLubyte token
= *(*inst
)++;
1818 case PARAM_STATE_ELEMENT
:
1819 if (parse_state_single_item (ctx
, inst
, Program
, state_tokens
))
1822 /* If we adding STATE_MATRIX that has multiple rows, we need to
1823 * unroll it and call _mesa_add_state_reference() for each row
1825 if ((state_tokens
[0] == STATE_MODELVIEW_MATRIX
||
1826 state_tokens
[0] == STATE_PROJECTION_MATRIX
||
1827 state_tokens
[0] == STATE_MVP_MATRIX
||
1828 state_tokens
[0] == STATE_TEXTURE_MATRIX
||
1829 state_tokens
[0] == STATE_PROGRAM_MATRIX
)
1830 && (state_tokens
[2] != state_tokens
[3])) {
1832 const GLint first_row
= state_tokens
[2];
1833 const GLint last_row
= state_tokens
[3];
1835 for (row
= first_row
; row
<= last_row
; row
++) {
1836 state_tokens
[2] = state_tokens
[3] = row
;
1838 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1840 if (param_var
->param_binding_begin
== ~0U)
1841 param_var
->param_binding_begin
= idx
;
1842 param_var
->param_binding_length
++;
1843 Program
->Base
.NumParameters
++;
1847 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1849 if (param_var
->param_binding_begin
== ~0U)
1850 param_var
->param_binding_begin
= idx
;
1851 param_var
->param_binding_length
++;
1852 Program
->Base
.NumParameters
++;
1856 case PARAM_PROGRAM_ELEMENT
:
1857 if (parse_program_single_item (ctx
, inst
, Program
, state_tokens
))
1859 idx
= _mesa_add_state_reference (Program
->Base
.Parameters
, state_tokens
);
1860 if (param_var
->param_binding_begin
== ~0U)
1861 param_var
->param_binding_begin
= idx
;
1862 param_var
->param_binding_length
++;
1863 Program
->Base
.NumParameters
++;
1865 /* Check if there is more: 0 -> we're done, else its an integer */
1867 GLuint out_of_range
, new_idx
;
1868 GLuint start_idx
= state_tokens
[2] + 1;
1869 GLuint end_idx
= parse_integer (inst
, Program
);
1872 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1873 if (((state_tokens
[1] == STATE_ENV
)
1874 && (end_idx
>= ctx
->Const
.FragmentProgram
.MaxEnvParams
))
1875 || ((state_tokens
[1] == STATE_LOCAL
)
1877 ctx
->Const
.FragmentProgram
.MaxLocalParams
)))
1881 if (((state_tokens
[1] == STATE_ENV
)
1882 && (end_idx
>= ctx
->Const
.VertexProgram
.MaxEnvParams
))
1883 || ((state_tokens
[1] == STATE_LOCAL
)
1885 ctx
->Const
.VertexProgram
.MaxLocalParams
)))
1889 program_error(ctx
, Program
->Position
,
1890 "Invalid Program Parameter"); /*end_idx*/
1894 for (new_idx
= start_idx
; new_idx
<= end_idx
; new_idx
++) {
1895 state_tokens
[2] = new_idx
;
1896 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1898 param_var
->param_binding_length
++;
1899 Program
->Base
.NumParameters
++;
1907 case PARAM_CONSTANT
:
1908 /* parsing something like {1.0, 2.0, 3.0, 4.0} */
1909 parse_constant (inst
, const_values
, Program
, use
);
1910 idx
= _mesa_add_named_constant(Program
->Base
.Parameters
,
1911 (char *) param_var
->name
,
1913 if (param_var
->param_binding_begin
== ~0U)
1914 param_var
->param_binding_begin
= idx
;
1915 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1916 /* Note: when we reference this parameter in an instruction later,
1917 * we'll check if it's really a constant/immediate and set the
1918 * instruction register type appropriately.
1920 param_var
->param_binding_length
++;
1921 Program
->Base
.NumParameters
++;
1925 program_error(ctx
, Program
->Position
,
1926 "Unexpected token (in parse_param_elements())");
1930 /* Make sure we haven't blown past our parameter limits */
1931 if (((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1932 (Program
->Base
.NumParameters
>=
1933 ctx
->Const
.VertexProgram
.MaxLocalParams
))
1934 || ((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1935 && (Program
->Base
.NumParameters
>=
1936 ctx
->Const
.FragmentProgram
.MaxLocalParams
))) {
1937 program_error(ctx
, Program
->Position
, "Too many parameter variables");
1946 * This picks out PARAM program parameter bindings.
1948 * XXX: This needs to be stressed & tested
1950 * \return 0 on sucess, 1 on error
1953 parse_param (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1954 struct arb_program
*Program
)
1957 GLint specified_length
;
1958 struct var_cache
*param_var
;
1961 param_var
= parse_string (inst
, vc_head
, Program
, &found
);
1962 Program
->Position
= parse_position (inst
);
1965 program_error2(ctx
, Program
->Position
,
1966 "Duplicate variable declaration",
1967 (char *) param_var
->name
);
1971 specified_length
= parse_integer (inst
, Program
);
1973 if (specified_length
< 0) {
1974 program_error(ctx
, Program
->Position
, "Negative parameter array length");
1978 param_var
->type
= vt_param
;
1979 param_var
->param_binding_length
= 0;
1981 /* Right now, everything is shoved into the main state register file.
1983 * In the future, it would be nice to leave things ENV/LOCAL params
1984 * in their respective register files, if possible
1986 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1989 * * - add each guy to the parameter list
1990 * * - increment the param_var->param_binding_len
1991 * * - store the param_var->param_binding_begin for the first one
1992 * * - compare the actual len to the specified len at the end
1994 while (**inst
!= PARAM_NULL
) {
1995 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_FALSE
))
1999 /* Test array length here! */
2000 if (specified_length
) {
2001 if (specified_length
!= (int)param_var
->param_binding_length
) {
2002 program_error(ctx
, Program
->Position
,
2003 "Declared parameter array length does not match parameter list");
2016 parse_param_use (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2017 struct arb_program
*Program
, struct var_cache
**new_var
)
2019 struct var_cache
*param_var
;
2021 /* First, insert a dummy entry into the var_cache */
2022 var_cache_create (¶m_var
);
2023 param_var
->name
= (const GLubyte
*) " ";
2024 param_var
->type
= vt_param
;
2026 param_var
->param_binding_length
= 0;
2027 /* Don't fill in binding_begin; We use the default value of -1
2028 * to tell if its already initialized, elsewhere.
2030 * param_var->param_binding_begin = 0;
2032 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
2034 var_cache_append (vc_head
, param_var
);
2036 /* Then fill it with juicy parameter goodness */
2037 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_TRUE
))
2040 *new_var
= param_var
;
2047 * This handles the declaration of TEMP variables
2049 * \return 0 on sucess, 1 on error
2052 parse_temp (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2053 struct arb_program
*Program
)
2056 struct var_cache
*temp_var
;
2058 while (**inst
!= 0) {
2059 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2060 Program
->Position
= parse_position (inst
);
2062 program_error2(ctx
, Program
->Position
,
2063 "Duplicate variable declaration",
2064 (char *) temp_var
->name
);
2068 temp_var
->type
= vt_temp
;
2070 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
2071 (Program
->Base
.NumTemporaries
>=
2072 ctx
->Const
.FragmentProgram
.MaxTemps
))
2073 || ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
2074 && (Program
->Base
.NumTemporaries
>=
2075 ctx
->Const
.VertexProgram
.MaxTemps
))) {
2076 program_error(ctx
, Program
->Position
,
2077 "Too many TEMP variables declared");
2081 temp_var
->temp_binding
= Program
->Base
.NumTemporaries
;
2082 Program
->Base
.NumTemporaries
++;
2090 * This handles variables of the OUTPUT variety
2092 * \return 0 on sucess, 1 on error
2095 parse_output (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2096 struct arb_program
*Program
)
2099 struct var_cache
*output_var
;
2102 output_var
= parse_string (inst
, vc_head
, Program
, &found
);
2103 Program
->Position
= parse_position (inst
);
2105 program_error2(ctx
, Program
->Position
,
2106 "Duplicate variable declaration",
2107 (char *) output_var
->name
);
2111 output_var
->type
= vt_output
;
2113 err
= parse_result_binding(ctx
, inst
, &output_var
->output_binding
, Program
);
2118 * This handles variables of the ALIAS kind
2120 * \return 0 on sucess, 1 on error
2123 parse_alias (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2124 struct arb_program
*Program
)
2127 struct var_cache
*temp_var
;
2129 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2130 Program
->Position
= parse_position (inst
);
2133 program_error2(ctx
, Program
->Position
,
2134 "Duplicate variable declaration",
2135 (char *) temp_var
->name
);
2139 temp_var
->type
= vt_alias
;
2140 temp_var
->alias_binding
= parse_string (inst
, vc_head
, Program
, &found
);
2141 Program
->Position
= parse_position (inst
);
2145 program_error2(ctx
, Program
->Position
,
2146 "Undefined alias value",
2147 (char *) temp_var
->alias_binding
->name
);
2155 * This handles variables of the ADDRESS kind
2157 * \return 0 on sucess, 1 on error
2160 parse_address (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2161 struct arb_program
*Program
)
2164 struct var_cache
*temp_var
;
2166 while (**inst
!= 0) {
2167 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2168 Program
->Position
= parse_position (inst
);
2170 program_error2(ctx
, Program
->Position
,
2171 "Duplicate variable declaration",
2172 (char *) temp_var
->name
);
2176 temp_var
->type
= vt_address
;
2178 if (Program
->Base
.NumAddressRegs
>=
2179 ctx
->Const
.VertexProgram
.MaxAddressRegs
) {
2180 const char *msg
= "Too many ADDRESS variables declared";
2181 program_error(ctx
, Program
->Position
, msg
);
2185 temp_var
->address_binding
= Program
->Base
.NumAddressRegs
;
2186 Program
->Base
.NumAddressRegs
++;
2194 * Parse a program declaration
2196 * \return 0 on sucess, 1 on error
2199 parse_declaration (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2200 struct arb_program
*Program
)
2204 switch (*(*inst
)++) {
2206 err
= parse_address (ctx
, inst
, vc_head
, Program
);
2210 err
= parse_alias (ctx
, inst
, vc_head
, Program
);
2214 err
= parse_attrib (ctx
, inst
, vc_head
, Program
);
2218 err
= parse_output (ctx
, inst
, vc_head
, Program
);
2222 err
= parse_param (ctx
, inst
, vc_head
, Program
);
2226 err
= parse_temp (ctx
, inst
, vc_head
, Program
);
2234 * Handle the parsing out of a masked destination register, either for a
2235 * vertex or fragment program.
2237 * If we are a vertex program, make sure we don't write to
2238 * result.position if we have specified that the program is
2239 * position invariant
2241 * \param File - The register file we write to
2242 * \param Index - The register index we write to
2243 * \param WriteMask - The mask controlling which components we write (1->write)
2245 * \return 0 on sucess, 1 on error
2248 parse_masked_dst_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2249 struct var_cache
**vc_head
, struct arb_program
*Program
,
2250 gl_register_file
*File
, GLuint
*Index
, GLint
*WriteMask
)
2253 struct var_cache
*dst
;
2255 /* We either have a result register specified, or a
2256 * variable that may or may not be writable
2258 switch (*(*inst
)++) {
2259 case REGISTER_RESULT
:
2260 if (parse_result_binding(ctx
, inst
, Index
, Program
))
2262 *File
= PROGRAM_OUTPUT
;
2265 case REGISTER_ESTABLISHED_NAME
:
2266 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2267 Program
->Position
= parse_position (inst
);
2269 /* If the name has never been added to our symbol table, we're hosed */
2271 program_error(ctx
, Program
->Position
, "0: Undefined variable");
2275 switch (dst
->type
) {
2277 *File
= PROGRAM_OUTPUT
;
2278 *Index
= dst
->output_binding
;
2282 *File
= PROGRAM_TEMPORARY
;
2283 *Index
= dst
->temp_binding
;
2286 /* If the var type is not vt_output or vt_temp, no go */
2288 program_error(ctx
, Program
->Position
,
2289 "Destination register is read only");
2295 program_error(ctx
, Program
->Position
,
2296 "Unexpected opcode in parse_masked_dst_reg()");
2301 /* Position invariance test */
2302 /* This test is done now in syntax portion - when position invariance OPTION
2303 is specified, "result.position" rule is disabled so there is no way
2304 to write the position
2306 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2308 program_error(ctx, Program->Position,
2309 "Vertex program specified position invariance and wrote vertex position");
2312 /* And then the mask.
2318 * ==> Need to reverse the order of bits for this!
2320 tmp
= (GLint
) *(*inst
)++;
2321 *WriteMask
= (((tmp
>>3) & 0x1) |
2331 * Handle the parsing of a address register
2333 * \param Index - The register index we write to
2335 * \return 0 on sucess, 1 on error
2338 parse_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2339 struct var_cache
**vc_head
,
2340 struct arb_program
*Program
, GLint
* Index
)
2342 struct var_cache
*dst
;
2345 *Index
= 0; /* XXX */
2347 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2348 Program
->Position
= parse_position (inst
);
2350 /* If the name has never been added to our symbol table, we're hosed */
2352 program_error(ctx
, Program
->Position
, "Undefined variable");
2356 if (dst
->type
!= vt_address
) {
2357 program_error(ctx
, Program
->Position
, "Variable is not of type ADDRESS");
2366 * Handle the parsing out of a masked address register
2368 * \param Index - The register index we write to
2369 * \param WriteMask - The mask controlling which components we write (1->write)
2371 * \return 0 on sucess, 1 on error
2374 parse_masked_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2375 struct var_cache
**vc_head
,
2376 struct arb_program
*Program
, GLint
* Index
,
2377 GLboolean
* WriteMask
)
2379 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, Index
))
2382 /* This should be 0x8 */
2385 /* Writemask of .x is implied */
2387 WriteMask
[1] = WriteMask
[2] = WriteMask
[3] = 0;
2394 * Parse out a swizzle mask.
2396 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
2398 * The len parameter allows us to grab 4 components for a vector
2399 * swizzle, or just 1 component for a scalar src register selection
2402 parse_swizzle_mask(const GLubyte
** inst
, GLubyte
*swizzle
, GLint len
)
2406 for (i
= 0; i
< 4; i
++)
2409 for (i
= 0; i
< len
; i
++) {
2410 switch (*(*inst
)++) {
2412 swizzle
[i
] = SWIZZLE_X
;
2415 swizzle
[i
] = SWIZZLE_Y
;
2418 swizzle
[i
] = SWIZZLE_Z
;
2421 swizzle
[i
] = SWIZZLE_W
;
2424 _mesa_problem(NULL
, "bad component in parse_swizzle_mask()");
2432 * Parse an extended swizzle mask which is a sequence of
2433 * four x/y/z/w/0/1 tokens.
2434 * \return swizzle four swizzle values
2435 * \return negateMask four element bitfield
2438 parse_extended_swizzle_mask(const GLubyte
**inst
, GLubyte swizzle
[4],
2439 GLubyte
*negateMask
)
2444 for (i
= 0; i
< 4; i
++) {
2446 if (parse_sign(inst
) == -1)
2447 *negateMask
|= (1 << i
);
2453 swizzle
[i
] = SWIZZLE_ZERO
;
2456 swizzle
[i
] = SWIZZLE_ONE
;
2459 swizzle
[i
] = SWIZZLE_X
;
2462 swizzle
[i
] = SWIZZLE_Y
;
2465 swizzle
[i
] = SWIZZLE_Z
;
2468 swizzle
[i
] = SWIZZLE_W
;
2471 _mesa_problem(NULL
, "bad case in parse_extended_swizzle_mask()");
2479 parse_src_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2480 struct var_cache
**vc_head
,
2481 struct arb_program
*Program
,
2482 gl_register_file
* File
, GLint
* Index
,
2483 GLboolean
*IsRelOffset
)
2485 struct var_cache
*src
;
2486 GLuint binding
, is_generic
, found
;
2491 /* And the binding for the src */
2492 switch (*(*inst
)++) {
2493 case REGISTER_ATTRIB
:
2494 if (parse_attrib_binding
2495 (ctx
, inst
, Program
, &binding
, &is_generic
))
2497 *File
= PROGRAM_INPUT
;
2500 /* We need to insert a dummy variable into the var_cache so we can
2501 * catch generic vertex attrib aliasing errors
2503 var_cache_create(&src
);
2504 src
->type
= vt_attrib
;
2505 src
->name
= (const GLubyte
*) "Dummy Attrib Variable";
2506 src
->attrib_binding
= binding
;
2507 src
->attrib_is_generic
= is_generic
;
2508 var_cache_append(vc_head
, src
);
2509 if (generic_attrib_check(*vc_head
)) {
2510 program_error(ctx
, Program
->Position
,
2511 "Cannot use both a generic vertex attribute "
2512 "and a specific attribute of the same type");
2517 case REGISTER_PARAM
:
2519 case PARAM_ARRAY_ELEMENT
:
2521 src
= parse_string (inst
, vc_head
, Program
, &found
);
2522 Program
->Position
= parse_position (inst
);
2525 program_error2(ctx
, Program
->Position
,
2526 "Undefined variable",
2527 (char *) src
->name
);
2531 *File
= (gl_register_file
) src
->param_binding_type
;
2533 switch (*(*inst
)++) {
2534 case ARRAY_INDEX_ABSOLUTE
:
2535 offset
= parse_integer (inst
, Program
);
2538 || (offset
>= (int)src
->param_binding_length
)) {
2539 program_error(ctx
, Program
->Position
,
2540 "Index out of range");
2541 /* offset, src->name */
2545 *Index
= src
->param_binding_begin
+ offset
;
2548 case ARRAY_INDEX_RELATIVE
:
2550 GLint addr_reg_idx
, rel_off
;
2552 /* First, grab the address regiseter */
2553 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &addr_reg_idx
))
2562 /* Then the relative offset */
2563 if (parse_relative_offset(ctx
, inst
, Program
, &rel_off
)) return 1;
2565 /* And store it properly */
2566 *Index
= src
->param_binding_begin
+ rel_off
;
2574 if (parse_param_use (ctx
, inst
, vc_head
, Program
, &src
))
2577 *File
= (gl_register_file
) src
->param_binding_type
;
2578 *Index
= src
->param_binding_begin
;
2583 case REGISTER_ESTABLISHED_NAME
:
2584 src
= parse_string (inst
, vc_head
, Program
, &found
);
2585 Program
->Position
= parse_position (inst
);
2587 /* If the name has never been added to our symbol table, we're hosed */
2589 program_error(ctx
, Program
->Position
,
2590 "3: Undefined variable"); /* src->name */
2594 switch (src
->type
) {
2596 *File
= PROGRAM_INPUT
;
2597 *Index
= src
->attrib_binding
;
2600 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2602 *File
= (gl_register_file
) src
->param_binding_type
;
2603 *Index
= src
->param_binding_begin
;
2607 *File
= PROGRAM_TEMPORARY
;
2608 *Index
= src
->temp_binding
;
2611 /* If the var type is vt_output no go */
2613 program_error(ctx
, Program
->Position
,
2614 "destination register is read only");
2621 program_error(ctx
, Program
->Position
,
2622 "Unknown token in parse_src_reg");
2626 if (*File
== PROGRAM_STATE_VAR
) {
2627 gl_register_file file
;
2629 /* If we're referencing the Program->Parameters[] array, check if the
2630 * parameter is really a constant/literal. If so, set File to CONSTANT.
2632 assert(*Index
< (GLint
) Program
->Base
.Parameters
->NumParameters
);
2633 file
= Program
->Base
.Parameters
->Parameters
[*Index
].Type
;
2634 if (file
== PROGRAM_CONSTANT
)
2635 *File
= PROGRAM_CONSTANT
;
2638 /* Add attributes to InputsRead only if they are used the program.
2639 * This avoids the handling of unused ATTRIB declarations in the drivers. */
2640 if (*File
== PROGRAM_INPUT
)
2641 Program
->Base
.InputsRead
|= (1 << *Index
);
2648 * Parse vertex/fragment program vector source register.
2651 parse_vector_src_reg(GLcontext
*ctx
, const GLubyte
**inst
,
2652 struct var_cache
**vc_head
,
2653 struct arb_program
*program
,
2654 struct prog_src_register
*reg
)
2656 gl_register_file file
;
2660 GLboolean isRelOffset
;
2663 negateMask
= (parse_sign (inst
) == -1) ? NEGATE_XYZW
: NEGATE_NONE
;
2665 /* And the src reg */
2666 if (parse_src_reg(ctx
, inst
, vc_head
, program
, &file
, &index
, &isRelOffset
))
2669 /* finally, the swizzle */
2670 parse_swizzle_mask(inst
, swizzle
, 4);
2674 reg
->Swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
2675 reg
->NegateBase
= negateMask
;
2676 reg
->RelAddr
= isRelOffset
;
2682 * Parse vertex/fragment program scalar source register.
2685 parse_scalar_src_reg(GLcontext
*ctx
, const GLubyte
**inst
,
2686 struct var_cache
**vc_head
,
2687 struct arb_program
*program
,
2688 struct prog_src_register
*reg
)
2690 gl_register_file file
;
2694 GLboolean isRelOffset
;
2697 negateMask
= (parse_sign (inst
) == -1) ? NEGATE_XYZW
: NEGATE_NONE
;
2699 /* And the src reg */
2700 if (parse_src_reg(ctx
, inst
, vc_head
, program
, &file
, &index
, &isRelOffset
))
2703 /* finally, the swizzle */
2704 parse_swizzle_mask(inst
, swizzle
, 1);
2708 reg
->Swizzle
= (swizzle
[0] << 0);
2709 reg
->NegateBase
= negateMask
;
2710 reg
->RelAddr
= isRelOffset
;
2716 * Parse vertex/fragment program destination register.
2717 * \return 1 if error, 0 if no error.
2720 parse_dst_reg(GLcontext
* ctx
, const GLubyte
** inst
,
2721 struct var_cache
**vc_head
, struct arb_program
*program
,
2722 struct prog_dst_register
*reg
)
2726 gl_register_file file
;
2728 if (parse_masked_dst_reg (ctx
, inst
, vc_head
, program
, &file
, &idx
, &mask
))
2733 reg
->WriteMask
= mask
;
2739 * This is a big mother that handles getting opcodes into the instruction
2740 * and handling the src & dst registers for fragment program instructions
2741 * \return 1 if error, 0 if no error
2744 parse_fp_instruction (GLcontext
* ctx
, const GLubyte
** inst
,
2745 struct var_cache
**vc_head
, struct arb_program
*Program
,
2746 struct prog_instruction
*fp
)
2750 GLubyte instClass
, type
, code
;
2752 GLuint shadow_tex
= 0;
2754 _mesa_init_instructions(fp
, 1);
2756 /* OP_ALU_INST or OP_TEX_INST */
2757 instClass
= *(*inst
)++;
2759 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2760 * OP_TEX_{SAMPLE, KIL}
2764 /* The actual opcode name */
2767 /* Increment the correct count */
2768 switch (instClass
) {
2770 Program
->NumAluInstructions
++;
2773 Program
->NumTexInstructions
++;
2781 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2783 fp
->Opcode
= OPCODE_ABS
;
2787 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2789 fp
->Opcode
= OPCODE_FLR
;
2793 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2795 fp
->Opcode
= OPCODE_FRC
;
2799 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2801 fp
->Opcode
= OPCODE_LIT
;
2805 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2807 fp
->Opcode
= OPCODE_MOV
;
2811 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2814 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2821 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2823 fp
->Opcode
= OPCODE_COS
;
2827 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2829 fp
->Opcode
= OPCODE_EX2
;
2833 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2835 fp
->Opcode
= OPCODE_LG2
;
2839 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2841 fp
->Opcode
= OPCODE_RCP
;
2845 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2847 fp
->Opcode
= OPCODE_RSQ
;
2851 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2853 fp
->Opcode
= OPCODE_SIN
;
2857 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2860 fp
->Opcode
= OPCODE_SCS
;
2864 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2867 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2874 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2876 fp
->Opcode
= OPCODE_POW
;
2880 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2883 for (a
= 0; a
< 2; a
++) {
2884 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2893 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2895 fp
->Opcode
= OPCODE_ADD
;
2899 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2901 fp
->Opcode
= OPCODE_DP3
;
2905 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2907 fp
->Opcode
= OPCODE_DP4
;
2911 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2913 fp
->Opcode
= OPCODE_DPH
;
2917 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2919 fp
->Opcode
= OPCODE_DST
;
2923 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2925 fp
->Opcode
= OPCODE_MAX
;
2929 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2931 fp
->Opcode
= OPCODE_MIN
;
2935 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2937 fp
->Opcode
= OPCODE_MUL
;
2941 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2943 fp
->Opcode
= OPCODE_SGE
;
2947 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2949 fp
->Opcode
= OPCODE_SLT
;
2953 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2955 fp
->Opcode
= OPCODE_SUB
;
2959 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2961 fp
->Opcode
= OPCODE_XPD
;
2965 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2967 for (a
= 0; a
< 2; a
++) {
2968 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2976 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2978 fp
->Opcode
= OPCODE_CMP
;
2982 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2984 fp
->Opcode
= OPCODE_LRP
;
2988 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2990 fp
->Opcode
= OPCODE_MAD
;
2994 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2997 for (a
= 0; a
< 3; a
++) {
2998 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
3006 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3008 fp
->Opcode
= OPCODE_SWZ
;
3011 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3017 gl_register_file file
;
3020 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
, &rel
))
3022 parse_extended_swizzle_mask(inst
, swizzle
, &negateMask
);
3023 fp
->SrcReg
[0].File
= file
;
3024 fp
->SrcReg
[0].Index
= index
;
3025 fp
->SrcReg
[0].NegateBase
= negateMask
;
3026 fp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
3036 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3038 fp
->Opcode
= OPCODE_TEX
;
3042 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3044 fp
->Opcode
= OPCODE_TXP
;
3048 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3050 fp
->Opcode
= OPCODE_TXB
;
3054 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3057 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
3061 if (parse_teximage_num (ctx
, inst
, Program
, &texcoord
))
3063 fp
->TexSrcUnit
= texcoord
;
3066 switch (*(*inst
)++) {
3067 case TEXTARGET_SHADOW1D
:
3068 shadow_tex
= 1 << texcoord
;
3071 fp
->TexSrcTarget
= TEXTURE_1D_INDEX
;
3073 case TEXTARGET_SHADOW2D
:
3074 shadow_tex
= 1 << texcoord
;
3077 fp
->TexSrcTarget
= TEXTURE_2D_INDEX
;
3080 fp
->TexSrcTarget
= TEXTURE_3D_INDEX
;
3082 case TEXTARGET_SHADOWRECT
:
3083 shadow_tex
= 1 << texcoord
;
3085 case TEXTARGET_RECT
:
3086 fp
->TexSrcTarget
= TEXTURE_RECT_INDEX
;
3088 case TEXTARGET_CUBE
:
3089 fp
->TexSrcTarget
= TEXTURE_CUBE_INDEX
;
3091 case TEXTARGET_SHADOW1D_ARRAY
:
3092 shadow_tex
= 1 << texcoord
;
3094 case TEXTARGET_1D_ARRAY
:
3095 fp
->TexSrcTarget
= TEXTURE_1D_ARRAY_INDEX
;
3097 case TEXTARGET_SHADOW2D_ARRAY
:
3098 shadow_tex
= 1 << texcoord
;
3100 case TEXTARGET_2D_ARRAY
:
3101 fp
->TexSrcTarget
= TEXTURE_2D_ARRAY_INDEX
;
3108 /* Don't test the first time a particular sampler is seen. Each time
3109 * after that, make sure the shadow state is the same.
3111 if ((_mesa_bitcount(Program
->TexturesUsed
[texcoord
]) > 0)
3112 && ((Program
->ShadowSamplers
& (1 << texcoord
)) != shadow_tex
)) {
3113 program_error(ctx
, Program
->Position
,
3114 "texture image unit used for shadow sampling and non-shadow sampling");
3118 Program
->TexturesUsed
[texcoord
] |= (1 << fp
->TexSrcTarget
);
3119 /* Check that both "2D" and "CUBE" (for example) aren't both used */
3120 if (_mesa_bitcount(Program
->TexturesUsed
[texcoord
]) > 1) {
3121 program_error(ctx
, Program
->Position
,
3122 "multiple targets used on one texture image unit");
3127 Program
->ShadowSamplers
|= shadow_tex
;
3131 Program
->UsesKill
= 1;
3132 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
3134 fp
->Opcode
= OPCODE_KIL
;
3137 _mesa_problem(ctx
, "bad type 0x%x in parse_fp_instruction()", type
);
3146 * Handle the parsing out of a masked address register
3148 * \param Index - The register index we write to
3149 * \param WriteMask - The mask controlling which components we write (1->write)
3151 * \return 0 on sucess, 1 on error
3154 parse_vp_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
3155 struct var_cache
**vc_head
,
3156 struct arb_program
*Program
,
3157 struct prog_dst_register
*reg
)
3161 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &idx
))
3164 /* This should be 0x8 */
3167 reg
->File
= PROGRAM_ADDRESS
;
3170 /* Writemask of .x is implied */
3171 reg
->WriteMask
= 0x1;
3177 * This is a big mother that handles getting opcodes into the instruction
3178 * and handling the src & dst registers for vertex program instructions
3181 parse_vp_instruction (GLcontext
* ctx
, const GLubyte
** inst
,
3182 struct var_cache
**vc_head
, struct arb_program
*Program
,
3183 struct prog_instruction
*vp
)
3188 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3191 /* The actual opcode name */
3194 _mesa_init_instructions(vp
, 1);
3199 vp
->Opcode
= OPCODE_ARL
;
3201 /* Remember to set SrcReg.RelAddr; */
3203 /* Get the masked address register [dst] */
3204 if (parse_vp_address_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3207 vp
->DstReg
.File
= PROGRAM_ADDRESS
;
3209 /* Get a scalar src register */
3210 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3218 vp
->Opcode
= OPCODE_ABS
;
3221 vp
->Opcode
= OPCODE_FLR
;
3224 vp
->Opcode
= OPCODE_FRC
;
3227 vp
->Opcode
= OPCODE_LIT
;
3230 vp
->Opcode
= OPCODE_MOV
;
3234 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3237 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3244 vp
->Opcode
= OPCODE_EX2
;
3247 vp
->Opcode
= OPCODE_EXP
;
3250 vp
->Opcode
= OPCODE_LG2
;
3253 vp
->Opcode
= OPCODE_LOG
;
3256 vp
->Opcode
= OPCODE_RCP
;
3259 vp
->Opcode
= OPCODE_RSQ
;
3262 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3265 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3272 vp
->Opcode
= OPCODE_POW
;
3275 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3278 for (a
= 0; a
< 2; a
++) {
3279 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3287 vp
->Opcode
= OPCODE_ADD
;
3290 vp
->Opcode
= OPCODE_DP3
;
3293 vp
->Opcode
= OPCODE_DP4
;
3296 vp
->Opcode
= OPCODE_DPH
;
3299 vp
->Opcode
= OPCODE_DST
;
3302 vp
->Opcode
= OPCODE_MAX
;
3305 vp
->Opcode
= OPCODE_MIN
;
3308 vp
->Opcode
= OPCODE_MUL
;
3311 vp
->Opcode
= OPCODE_SGE
;
3314 vp
->Opcode
= OPCODE_SLT
;
3317 vp
->Opcode
= OPCODE_SUB
;
3320 vp
->Opcode
= OPCODE_XPD
;
3323 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3326 for (a
= 0; a
< 2; a
++) {
3327 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3335 vp
->Opcode
= OPCODE_MAD
;
3339 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3342 for (a
= 0; a
< 3; a
++) {
3343 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3351 vp
->Opcode
= OPCODE_SWZ
;
3358 gl_register_file file
;
3361 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3364 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
, &relAddr
))
3366 parse_extended_swizzle_mask (inst
, swizzle
, &negateMask
);
3367 vp
->SrcReg
[0].File
= file
;
3368 vp
->SrcReg
[0].Index
= index
;
3369 vp
->SrcReg
[0].NegateBase
= negateMask
;
3370 vp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
3374 vp
->SrcReg
[0].RelAddr
= relAddr
;
3384 debug_variables (GLcontext
* ctx
, struct var_cache
*vc_head
,
3385 struct arb_program
*Program
)
3387 struct var_cache
*vc
;
3390 fprintf (stderr
, "debug_variables, vc_head: %p\n", (void*) vc_head
);
3392 /* First of all, print out the contents of the var_cache */
3395 fprintf (stderr
, "[%p]\n", (void*) vc
);
3398 fprintf (stderr
, "UNDEFINED %s\n", vc
->name
);
3401 fprintf (stderr
, "ATTRIB %s\n", vc
->name
);
3402 fprintf (stderr
, " binding: 0x%x\n", vc
->attrib_binding
);
3405 fprintf (stderr
, "PARAM %s begin: %d len: %d\n", vc
->name
,
3406 vc
->param_binding_begin
, vc
->param_binding_length
);
3407 b
= vc
->param_binding_begin
;
3408 for (a
= 0; a
< vc
->param_binding_length
; a
++) {
3409 fprintf (stderr
, "%s\n",
3410 Program
->Base
.Parameters
->Parameters
[a
+ b
].Name
);
3411 if (Program
->Base
.Parameters
->Parameters
[a
+ b
].Type
== PROGRAM_STATE_VAR
) {
3413 s
= _mesa_program_state_string(Program
->Base
.Parameters
->Parameters
3414 [a
+ b
].StateIndexes
);
3415 fprintf(stderr
, "%s\n", s
);
3419 fprintf (stderr
, "%f %f %f %f\n",
3420 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][0],
3421 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][1],
3422 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][2],
3423 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][3]);
3427 fprintf (stderr
, "TEMP %s\n", vc
->name
);
3428 fprintf (stderr
, " binding: 0x%x\n", vc
->temp_binding
);
3431 fprintf (stderr
, "OUTPUT %s\n", vc
->name
);
3432 fprintf (stderr
, " binding: 0x%x\n", vc
->output_binding
);
3435 fprintf (stderr
, "ALIAS %s\n", vc
->name
);
3436 fprintf (stderr
, " binding: 0x%p (%s)\n",
3437 (void*) vc
->alias_binding
, vc
->alias_binding
->name
);
3447 #endif /* DEBUG_PARSING */
3451 * The main loop for parsing a fragment or vertex program
3453 * \return 1 on error, 0 on success
3456 parse_instructions(GLcontext
* ctx
, const GLubyte
* inst
,
3457 struct var_cache
**vc_head
, struct arb_program
*Program
)
3459 const GLuint maxInst
= (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
3460 ? ctx
->Const
.FragmentProgram
.MaxInstructions
3461 : ctx
->Const
.VertexProgram
.MaxInstructions
;
3464 ASSERT(MAX_PROGRAM_INSTRUCTIONS
>= maxInst
);
3466 Program
->MajorVersion
= (GLuint
) * inst
++;
3467 Program
->MinorVersion
= (GLuint
) * inst
++;
3469 while (*inst
!= END
) {
3474 case ARB_PRECISION_HINT_FASTEST
:
3475 Program
->PrecisionOption
= GL_FASTEST
;
3478 case ARB_PRECISION_HINT_NICEST
:
3479 Program
->PrecisionOption
= GL_NICEST
;
3483 Program
->FogOption
= GL_EXP
;
3487 Program
->FogOption
= GL_EXP2
;
3490 case ARB_FOG_LINEAR
:
3491 Program
->FogOption
= GL_LINEAR
;
3494 case ARB_POSITION_INVARIANT
:
3495 if (Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
3496 Program
->HintPositionInvariant
= GL_TRUE
;
3499 case ARB_FRAGMENT_PROGRAM_SHADOW
:
3500 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3501 /* TODO ARB_fragment_program_shadow code */
3505 case ARB_DRAW_BUFFERS
:
3506 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3507 /* do nothing for now */
3511 case MESA_TEXTURE_ARRAY
:
3512 /* do nothing for now */
3519 if (Program
->Base
.NumInstructions
+ 1 >= maxInst
) {
3520 program_error(ctx
, Program
->Position
,
3521 "Max instruction count exceeded");
3524 Program
->Position
= parse_position (&inst
);
3525 /* parse the current instruction */
3526 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3527 err
= parse_fp_instruction (ctx
, &inst
, vc_head
, Program
,
3528 &Program
->Base
.Instructions
[Program
->Base
.NumInstructions
]);
3531 err
= parse_vp_instruction (ctx
, &inst
, vc_head
, Program
,
3532 &Program
->Base
.Instructions
[Program
->Base
.NumInstructions
]);
3535 /* increment instuction count */
3536 Program
->Base
.NumInstructions
++;
3540 err
= parse_declaration (ctx
, &inst
, vc_head
, Program
);
3551 /* Finally, tag on an OPCODE_END instruction */
3553 const GLuint numInst
= Program
->Base
.NumInstructions
;
3554 _mesa_init_instructions(Program
->Base
.Instructions
+ numInst
, 1);
3555 Program
->Base
.Instructions
[numInst
].Opcode
= OPCODE_END
;
3557 Program
->Base
.NumInstructions
++;
3560 * Initialize native counts to logical counts. The device driver may
3561 * change them if program is translated into a hardware program.
3563 Program
->Base
.NumNativeInstructions
= Program
->Base
.NumInstructions
;
3564 Program
->Base
.NumNativeTemporaries
= Program
->Base
.NumTemporaries
;
3565 Program
->Base
.NumNativeParameters
= Program
->Base
.NumParameters
;
3566 Program
->Base
.NumNativeAttributes
= Program
->Base
.NumAttributes
;
3567 Program
->Base
.NumNativeAddressRegs
= Program
->Base
.NumAddressRegs
;
3574 LONGSTRING
static char core_grammar_text
[] =
3575 #include "shader/grammar/grammar_syn.h"
3580 * Set a grammar parameter.
3581 * \param name the grammar parameter
3582 * \param value the new parameter value
3583 * \return 0 if OK, 1 if error
3586 set_reg8 (GLcontext
*ctx
, grammar id
, const char *name
, GLubyte value
)
3588 char error_msg
[300];
3591 if (grammar_set_reg8 (id
, (const byte
*) name
, value
))
3594 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3595 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3596 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Grammar Register Error");
3602 * Enable support for the given language option in the parser.
3603 * \return 1 if OK, 0 if error
3606 enable_ext(GLcontext
*ctx
, grammar id
, const char *name
)
3608 return !set_reg8(ctx
, id
, name
, 1);
3613 * Enable parser extensions based on which OpenGL extensions are supported
3614 * by this rendering context.
3616 * \return GL_TRUE if OK, GL_FALSE if error.
3619 enable_parser_extensions(GLcontext
*ctx
, grammar id
)
3622 /* These are not supported at this time */
3623 if ((ctx
->Extensions
.ARB_vertex_blend
||
3624 ctx
->Extensions
.EXT_vertex_weighting
)
3625 && !enable_ext(ctx
, id
, "vertex_blend"))
3627 if (ctx
->Extensions
.ARB_matrix_palette
3628 && !enable_ext(ctx
, id
, "matrix_palette"))
3631 if (ctx
->Extensions
.ARB_fragment_program_shadow
3632 && !enable_ext(ctx
, id
, "fragment_program_shadow"))
3634 if (ctx
->Extensions
.EXT_point_parameters
3635 && !enable_ext(ctx
, id
, "point_parameters"))
3637 if (ctx
->Extensions
.EXT_secondary_color
3638 && !enable_ext(ctx
, id
, "secondary_color"))
3640 if (ctx
->Extensions
.EXT_fog_coord
3641 && !enable_ext(ctx
, id
, "fog_coord"))
3643 if (ctx
->Extensions
.NV_texture_rectangle
3644 && !enable_ext(ctx
, id
, "texture_rectangle"))
3646 if (!enable_ext(ctx
, id
, "draw_buffers"))
3648 if (ctx
->Extensions
.MESA_texture_array
3649 && !enable_ext(ctx
, id
, "texture_array"))
3652 /* hack for Warcraft (see bug 8060) */
3653 enable_ext(ctx
, id
, "vertex_blend");
3661 * This kicks everything off.
3663 * \param ctx - The GL Context
3664 * \param str - The program string
3665 * \param len - The program string length
3666 * \param program - The arb_program struct to return all the parsed info in
3667 * \return GL_TRUE on sucess, GL_FALSE on error
3670 _mesa_parse_arb_program(GLcontext
*ctx
, GLenum target
,
3671 const GLubyte
*str
, GLsizei len
,
3672 struct arb_program
*program
)
3674 GLint a
, err
, error_pos
;
3675 char error_msg
[300];
3677 struct var_cache
*vc_head
;
3678 grammar arbprogram_syn_id
;
3679 GLubyte
*parsed
, *inst
;
3680 GLubyte
*strz
= NULL
;
3681 static int arbprogram_syn_is_ok
= 0; /* XXX temporary */
3683 /* set the program target before parsing */
3684 program
->Base
.Target
= target
;
3686 /* Reset error state */
3687 _mesa_set_program_error(ctx
, -1, NULL
);
3689 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
3690 if (!arbprogram_syn_is_ok
) {
3691 /* One-time initialization of parsing system */
3692 grammar grammar_syn_id
;
3695 grammar_syn_id
= grammar_load_from_text ((byte
*) core_grammar_text
);
3696 if (grammar_syn_id
== 0) {
3697 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3698 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3699 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3700 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3701 "glProgramStringARB(Error loading grammar rule set)");
3705 err
= !grammar_check(grammar_syn_id
, (byte
*) arb_grammar_text
,
3706 &parsed
, &parsed_len
);
3708 /* 'parsed' is unused here */
3709 _mesa_free (parsed
);
3712 /* NOTE: we can't destroy grammar_syn_id right here because
3713 * grammar_destroy() can reset the last error
3716 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3717 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3718 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3719 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3720 "glProgramString(Error loading grammar rule set");
3721 grammar_destroy (grammar_syn_id
);
3725 grammar_destroy (grammar_syn_id
);
3727 arbprogram_syn_is_ok
= 1;
3730 /* create the grammar object */
3731 arbprogram_syn_id
= grammar_load_from_text ((byte
*) arb_grammar_text
);
3732 if (arbprogram_syn_id
== 0) {
3733 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3734 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
3735 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3736 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3737 "glProgramString(Error loading grammer rule set)");
3741 /* Set program_target register value */
3742 if (set_reg8 (ctx
, arbprogram_syn_id
, "program_target",
3743 program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
? 0x10 : 0x20)) {
3744 grammar_destroy (arbprogram_syn_id
);
3748 if (!enable_parser_extensions(ctx
, arbprogram_syn_id
)) {
3749 grammar_destroy(arbprogram_syn_id
);
3753 /* check for NULL character occurences */
3756 for (i
= 0; i
< len
; i
++) {
3757 if (str
[i
] == '\0') {
3758 program_error(ctx
, i
, "illegal character");
3759 grammar_destroy (arbprogram_syn_id
);
3765 /* copy the program string to a null-terminated string */
3766 strz
= (GLubyte
*) _mesa_malloc (len
+ 1);
3768 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glProgramStringARB");
3769 grammar_destroy (arbprogram_syn_id
);
3772 _mesa_memcpy (strz
, str
, len
);
3775 /* do a fast check on program string - initial production buffer is 4K */
3776 err
= !grammar_fast_check(arbprogram_syn_id
, strz
,
3777 &parsed
, &parsed_len
, 0x1000);
3779 /* Syntax parse error */
3781 grammar_get_last_error((GLubyte
*) error_msg
, 300, &error_pos
);
3782 program_error(ctx
, error_pos
, error_msg
);
3785 /* useful for debugging */
3789 fprintf(stderr
, "program: %s\n", (char *) strz
);
3790 fprintf(stderr
, "Error Pos: %d\n", ctx
->Program
.ErrorPos
);
3791 s
= (char *) _mesa_find_line_column(strz
, strz
+ctx
->Program
.ErrorPos
,
3793 fprintf(stderr
, "line %d col %d: %s\n", line
, col
, s
);
3800 grammar_destroy (arbprogram_syn_id
);
3804 grammar_destroy (arbprogram_syn_id
);
3807 * Program string is syntactically correct at this point
3808 * Parse the tokenized version of the program now, generating
3809 * vertex/fragment program instructions.
3812 /* Initialize the arb_program struct */
3813 program
->Base
.String
= strz
;
3814 program
->Base
.Instructions
= _mesa_alloc_instructions(MAX_PROGRAM_INSTRUCTIONS
);
3815 program
->Base
.NumInstructions
=
3816 program
->Base
.NumTemporaries
=
3817 program
->Base
.NumParameters
=
3818 program
->Base
.NumAttributes
= program
->Base
.NumAddressRegs
= 0;
3819 program
->Base
.Parameters
= _mesa_new_parameter_list ();
3820 program
->Base
.InputsRead
= 0x0;
3821 program
->Base
.OutputsWritten
= 0x0;
3822 program
->Position
= 0;
3823 program
->MajorVersion
= program
->MinorVersion
= 0;
3824 program
->PrecisionOption
= GL_DONT_CARE
;
3825 program
->FogOption
= GL_NONE
;
3826 program
->HintPositionInvariant
= GL_FALSE
;
3827 for (a
= 0; a
< MAX_TEXTURE_IMAGE_UNITS
; a
++)
3828 program
->TexturesUsed
[a
] = 0x0;
3829 program
->ShadowSamplers
= 0x0;
3830 program
->NumAluInstructions
=
3831 program
->NumTexInstructions
=
3832 program
->NumTexIndirections
= 0;
3833 program
->UsesKill
= 0;
3838 /* Start examining the tokens in the array */
3841 /* Check the grammer rev */
3842 if (*inst
++ != REVISION
) {
3843 program_error (ctx
, 0, "Grammar version mismatch");
3847 /* ignore program target */
3849 err
= parse_instructions(ctx
, inst
, &vc_head
, program
);
3852 /*debug_variables(ctx, vc_head, program); */
3854 /* We're done with the parsed binary array */
3855 var_cache_destroy (&vc_head
);
3857 _mesa_free (parsed
);
3859 /* Reallocate the instruction array from size [MAX_PROGRAM_INSTRUCTIONS]
3860 * to size [ap.Base.NumInstructions].
3862 program
->Base
.Instructions
3863 = _mesa_realloc_instructions(program
->Base
.Instructions
,
3864 MAX_PROGRAM_INSTRUCTIONS
,
3865 program
->Base
.NumInstructions
);
3873 _mesa_parse_arb_fragment_program(GLcontext
* ctx
, GLenum target
,
3874 const GLvoid
*str
, GLsizei len
,
3875 struct gl_fragment_program
*program
)
3877 struct arb_program ap
;
3880 ASSERT(target
== GL_FRAGMENT_PROGRAM_ARB
);
3881 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
, &ap
)) {
3882 /* Error in the program. Just return. */
3886 /* Copy the relevant contents of the arb_program struct into the
3887 * fragment_program struct.
3889 program
->Base
.String
= ap
.Base
.String
;
3890 program
->Base
.NumInstructions
= ap
.Base
.NumInstructions
;
3891 program
->Base
.NumTemporaries
= ap
.Base
.NumTemporaries
;
3892 program
->Base
.NumParameters
= ap
.Base
.NumParameters
;
3893 program
->Base
.NumAttributes
= ap
.Base
.NumAttributes
;
3894 program
->Base
.NumAddressRegs
= ap
.Base
.NumAddressRegs
;
3895 program
->Base
.NumNativeInstructions
= ap
.Base
.NumNativeInstructions
;
3896 program
->Base
.NumNativeTemporaries
= ap
.Base
.NumNativeTemporaries
;
3897 program
->Base
.NumNativeParameters
= ap
.Base
.NumNativeParameters
;
3898 program
->Base
.NumNativeAttributes
= ap
.Base
.NumNativeAttributes
;
3899 program
->Base
.NumNativeAddressRegs
= ap
.Base
.NumNativeAddressRegs
;
3900 program
->Base
.NumAluInstructions
= ap
.Base
.NumAluInstructions
;
3901 program
->Base
.NumTexInstructions
= ap
.Base
.NumTexInstructions
;
3902 program
->Base
.NumTexIndirections
= ap
.Base
.NumTexIndirections
;
3903 program
->Base
.NumNativeAluInstructions
= ap
.Base
.NumAluInstructions
;
3904 program
->Base
.NumNativeTexInstructions
= ap
.Base
.NumTexInstructions
;
3905 program
->Base
.NumNativeTexIndirections
= ap
.Base
.NumTexIndirections
;
3906 program
->Base
.InputsRead
= ap
.Base
.InputsRead
;
3907 program
->Base
.OutputsWritten
= ap
.Base
.OutputsWritten
;
3908 for (i
= 0; i
< MAX_TEXTURE_IMAGE_UNITS
; i
++) {
3909 program
->Base
.TexturesUsed
[i
] = ap
.TexturesUsed
[i
];
3910 if (ap
.TexturesUsed
[i
])
3911 program
->Base
.SamplersUsed
|= (1 << i
);
3913 program
->Base
.ShadowSamplers
= ap
.ShadowSamplers
;
3914 program
->FogOption
= ap
.FogOption
;
3915 program
->UsesKill
= ap
.UsesKill
;
3917 if (program
->FogOption
)
3918 program
->Base
.InputsRead
|= FRAG_BIT_FOGC
;
3920 if (program
->Base
.Instructions
)
3921 _mesa_free(program
->Base
.Instructions
);
3922 program
->Base
.Instructions
= ap
.Base
.Instructions
;
3924 if (program
->Base
.Parameters
)
3925 _mesa_free_parameter_list(program
->Base
.Parameters
);
3926 program
->Base
.Parameters
= ap
.Base
.Parameters
;
3928 /* Append fog instructions now if the program has "OPTION ARB_fog_exp"
3929 * or similar. We used to leave this up to drivers, but it appears
3930 * there's no hardware that wants to do fog in a discrete stage separate
3931 * from the fragment shader.
3933 if (program
->FogOption
!= GL_NONE
) {
3934 _mesa_append_fog_code(ctx
, program
);
3935 program
->FogOption
= GL_NONE
;
3939 _mesa_printf("____________Fragment program %u ________\n", program
->Base
.Id
);
3940 _mesa_print_program(&program
->Base
);
3947 * Parse the vertex program string. If success, update the given
3948 * vertex_program object with the new program. Else, leave the vertex_program
3952 _mesa_parse_arb_vertex_program(GLcontext
*ctx
, GLenum target
,
3953 const GLvoid
*str
, GLsizei len
,
3954 struct gl_vertex_program
*program
)
3956 struct arb_program ap
;
3958 ASSERT(target
== GL_VERTEX_PROGRAM_ARB
);
3960 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
, &ap
)) {
3961 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glProgramString(bad program)");
3965 /* Copy the relevant contents of the arb_program struct into the
3966 * vertex_program struct.
3968 program
->Base
.String
= ap
.Base
.String
;
3969 program
->Base
.NumInstructions
= ap
.Base
.NumInstructions
;
3970 program
->Base
.NumTemporaries
= ap
.Base
.NumTemporaries
;
3971 program
->Base
.NumParameters
= ap
.Base
.NumParameters
;
3972 program
->Base
.NumAttributes
= ap
.Base
.NumAttributes
;
3973 program
->Base
.NumAddressRegs
= ap
.Base
.NumAddressRegs
;
3974 program
->Base
.NumNativeInstructions
= ap
.Base
.NumNativeInstructions
;
3975 program
->Base
.NumNativeTemporaries
= ap
.Base
.NumNativeTemporaries
;
3976 program
->Base
.NumNativeParameters
= ap
.Base
.NumNativeParameters
;
3977 program
->Base
.NumNativeAttributes
= ap
.Base
.NumNativeAttributes
;
3978 program
->Base
.NumNativeAddressRegs
= ap
.Base
.NumNativeAddressRegs
;
3979 program
->Base
.InputsRead
= ap
.Base
.InputsRead
;
3980 program
->Base
.OutputsWritten
= ap
.Base
.OutputsWritten
;
3981 program
->IsPositionInvariant
= ap
.HintPositionInvariant
;
3983 if (program
->Base
.Instructions
)
3984 _mesa_free(program
->Base
.Instructions
);
3985 program
->Base
.Instructions
= ap
.Base
.Instructions
;
3987 if (program
->Base
.Parameters
)
3988 _mesa_free_parameter_list(program
->Base
.Parameters
);
3989 program
->Base
.Parameters
= ap
.Base
.Parameters
;
3992 _mesa_printf("____________Vertex program %u __________\n", program
->Base
.Id
);
3993 _mesa_print_program(&program
->Base
);