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
)) {
972 program_error(ctx
, Program
->Position
, "Invalid draw buffer index");
982 * \param coord The texture unit index
983 * \return 0 on sucess, 1 on error
986 parse_texcoord_num (GLcontext
* ctx
, const GLubyte
** inst
,
987 struct arb_program
*Program
, GLuint
* coord
)
989 GLint i
= parse_integer (inst
, Program
);
991 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxTextureUnits
)) {
992 program_error(ctx
, Program
->Position
, "Invalid texture unit index");
1001 * \param coord The weight index
1002 * \return 0 on sucess, 1 on error
1005 parse_weight_num (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
1008 *coord
= parse_integer (inst
, Program
);
1010 if ((*coord
< 0) || (*coord
>= 1)) {
1011 program_error(ctx
, Program
->Position
, "Invalid weight index");
1019 * \param coord The clip plane index
1020 * \return 0 on sucess, 1 on error
1023 parse_clipplane_num (GLcontext
* ctx
, const GLubyte
** inst
,
1024 struct arb_program
*Program
, GLint
* coord
)
1026 *coord
= parse_integer (inst
, Program
);
1028 if ((*coord
< 0) || (*coord
>= (GLint
) ctx
->Const
.MaxClipPlanes
)) {
1029 program_error(ctx
, Program
->Position
, "Invalid clip plane index");
1038 * \return 0 on front face, 1 on back face
1041 parse_face_type (const GLubyte
** inst
)
1043 switch (*(*inst
)++) {
1055 * Given a matrix and a modifier token on the binary array, return tokens
1056 * that _mesa_fetch_state() [program.c] can understand.
1058 * \param matrix - the matrix we are talking about
1059 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
1060 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
1061 * \return 0 on sucess, 1 on failure
1064 parse_matrix (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
1065 GLint
* matrix
, GLint
* matrix_idx
, GLint
* matrix_modifier
)
1067 GLubyte mat
= *(*inst
)++;
1072 case MATRIX_MODELVIEW
:
1073 *matrix
= STATE_MODELVIEW_MATRIX
;
1074 *matrix_idx
= parse_integer (inst
, Program
);
1075 if (*matrix_idx
> 0) {
1076 program_error(ctx
, Program
->Position
,
1077 "ARB_vertex_blend not supported");
1082 case MATRIX_PROJECTION
:
1083 *matrix
= STATE_PROJECTION_MATRIX
;
1087 *matrix
= STATE_MVP_MATRIX
;
1090 case MATRIX_TEXTURE
:
1091 *matrix
= STATE_TEXTURE_MATRIX
;
1092 *matrix_idx
= parse_integer (inst
, Program
);
1093 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxTextureUnits
) {
1094 program_error(ctx
, Program
->Position
, "Invalid Texture Unit");
1095 /* bad *matrix_id */
1100 /* This is not currently supported (ARB_matrix_palette) */
1101 case MATRIX_PALETTE
:
1102 *matrix_idx
= parse_integer (inst
, Program
);
1103 program_error(ctx
, Program
->Position
,
1104 "ARB_matrix_palette not supported");
1108 case MATRIX_PROGRAM
:
1109 *matrix
= STATE_PROGRAM_MATRIX
;
1110 *matrix_idx
= parse_integer (inst
, Program
);
1111 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxProgramMatrices
) {
1112 program_error(ctx
, Program
->Position
, "Invalid Program Matrix");
1113 /* bad *matrix_idx */
1119 switch (*(*inst
)++) {
1120 case MATRIX_MODIFIER_IDENTITY
:
1121 *matrix_modifier
= 0;
1123 case MATRIX_MODIFIER_INVERSE
:
1124 *matrix_modifier
= STATE_MATRIX_INVERSE
;
1126 case MATRIX_MODIFIER_TRANSPOSE
:
1127 *matrix_modifier
= STATE_MATRIX_TRANSPOSE
;
1129 case MATRIX_MODIFIER_INVTRANS
:
1130 *matrix_modifier
= STATE_MATRIX_INVTRANS
;
1139 * This parses a state string (rather, the binary version of it) into
1140 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1142 * \param inst - the start in the binary arry to start working from
1143 * \param state_tokens - the storage for the 6-token state description
1144 * \return - 0 on sucess, 1 on error
1147 parse_state_single_item (GLcontext
* ctx
, const GLubyte
** inst
,
1148 struct arb_program
*Program
,
1149 gl_state_index state_tokens
[STATE_LENGTH
])
1151 GLubyte token
= *(*inst
)++;
1154 case STATE_MATERIAL_PARSER
:
1155 state_tokens
[0] = STATE_MATERIAL
;
1156 state_tokens
[1] = parse_face_type (inst
);
1157 switch (*(*inst
)++) {
1158 case MATERIAL_AMBIENT
:
1159 state_tokens
[2] = STATE_AMBIENT
;
1161 case MATERIAL_DIFFUSE
:
1162 state_tokens
[2] = STATE_DIFFUSE
;
1164 case MATERIAL_SPECULAR
:
1165 state_tokens
[2] = STATE_SPECULAR
;
1167 case MATERIAL_EMISSION
:
1168 state_tokens
[2] = STATE_EMISSION
;
1170 case MATERIAL_SHININESS
:
1171 state_tokens
[2] = STATE_SHININESS
;
1176 case STATE_LIGHT_PARSER
:
1177 state_tokens
[0] = STATE_LIGHT
;
1178 state_tokens
[1] = parse_integer (inst
, Program
);
1180 /* Check the value of state_tokens[1] against the # of lights */
1181 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1182 program_error(ctx
, Program
->Position
, "Invalid Light Number");
1183 /* bad state_tokens[1] */
1187 switch (*(*inst
)++) {
1189 state_tokens
[2] = STATE_AMBIENT
;
1192 state_tokens
[2] = STATE_DIFFUSE
;
1194 case LIGHT_SPECULAR
:
1195 state_tokens
[2] = STATE_SPECULAR
;
1197 case LIGHT_POSITION
:
1198 state_tokens
[2] = STATE_POSITION
;
1200 case LIGHT_ATTENUATION
:
1201 state_tokens
[2] = STATE_ATTENUATION
;
1204 state_tokens
[2] = STATE_HALF_VECTOR
;
1206 case LIGHT_SPOT_DIRECTION
:
1207 state_tokens
[2] = STATE_SPOT_DIRECTION
;
1212 case STATE_LIGHT_MODEL
:
1213 switch (*(*inst
)++) {
1214 case LIGHT_MODEL_AMBIENT
:
1215 state_tokens
[0] = STATE_LIGHTMODEL_AMBIENT
;
1217 case LIGHT_MODEL_SCENECOLOR
:
1218 state_tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
1219 state_tokens
[1] = parse_face_type (inst
);
1224 case STATE_LIGHT_PROD
:
1225 state_tokens
[0] = STATE_LIGHTPROD
;
1226 state_tokens
[1] = parse_integer (inst
, Program
);
1228 /* Check the value of state_tokens[1] against the # of lights */
1229 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1230 program_error(ctx
, Program
->Position
, "Invalid Light Number");
1231 /* bad state_tokens[1] */
1235 state_tokens
[2] = parse_face_type (inst
);
1236 switch (*(*inst
)++) {
1237 case LIGHT_PROD_AMBIENT
:
1238 state_tokens
[3] = STATE_AMBIENT
;
1240 case LIGHT_PROD_DIFFUSE
:
1241 state_tokens
[3] = STATE_DIFFUSE
;
1243 case LIGHT_PROD_SPECULAR
:
1244 state_tokens
[3] = STATE_SPECULAR
;
1251 switch (*(*inst
)++) {
1253 state_tokens
[0] = STATE_FOG_COLOR
;
1256 state_tokens
[0] = STATE_FOG_PARAMS
;
1262 state_tokens
[1] = parse_integer (inst
, Program
);
1263 switch (*(*inst
)++) {
1265 state_tokens
[0] = STATE_TEXENV_COLOR
;
1274 state_tokens
[0] = STATE_TEXGEN
;
1275 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1277 if (parse_texcoord_num (ctx
, inst
, Program
, &coord
))
1279 state_tokens
[1] = coord
;
1284 /* 0 - s, 1 - t, 2 - r, 3 - q */
1287 if (type
== TEX_GEN_EYE
) {
1290 state_tokens
[2] = STATE_TEXGEN_EYE_S
;
1293 state_tokens
[2] = STATE_TEXGEN_EYE_T
;
1296 state_tokens
[2] = STATE_TEXGEN_EYE_R
;
1299 state_tokens
[2] = STATE_TEXGEN_EYE_Q
;
1302 _mesa_problem(ctx
, "bad texgen component in "
1303 "parse_state_single_item()");
1309 state_tokens
[2] = STATE_TEXGEN_OBJECT_S
;
1312 state_tokens
[2] = STATE_TEXGEN_OBJECT_T
;
1315 state_tokens
[2] = STATE_TEXGEN_OBJECT_R
;
1318 state_tokens
[2] = STATE_TEXGEN_OBJECT_Q
;
1321 _mesa_problem(ctx
, "bad texgen component in "
1322 "parse_state_single_item()");
1329 switch (*(*inst
)++) {
1331 state_tokens
[0] = STATE_DEPTH_RANGE
;
1336 case STATE_CLIP_PLANE
:
1337 state_tokens
[0] = STATE_CLIPPLANE
;
1338 if (parse_clipplane_num (ctx
, inst
, Program
,
1339 (GLint
*) &state_tokens
[1]))
1344 switch (*(*inst
)++) {
1346 state_tokens
[0] = STATE_POINT_SIZE
;
1349 case POINT_ATTENUATION
:
1350 state_tokens
[0] = STATE_POINT_ATTENUATION
;
1355 /* XXX: I think this is the correct format for a matrix row */
1356 case STATE_MATRIX_ROWS
:
1357 if (parse_matrix(ctx
, inst
, Program
,
1358 (GLint
*) &state_tokens
[0],
1359 (GLint
*) &state_tokens
[1],
1360 (GLint
*) &state_tokens
[4]))
1363 state_tokens
[2] = parse_integer (inst
, Program
); /* The first row to grab */
1365 if ((**inst
) != 0) { /* Either the last row, 0 */
1366 state_tokens
[3] = parse_integer (inst
, Program
);
1367 if (state_tokens
[3] < state_tokens
[2]) {
1368 program_error(ctx
, Program
->Position
,
1369 "Second matrix index less than the first");
1370 /* state_tokens[4] vs. state_tokens[3] */
1375 state_tokens
[3] = state_tokens
[2];
1385 * This parses a state string (rather, the binary version of it) into
1386 * a 6-token similar for the state fetching code in program.c
1388 * One might ask, why fetch these parameters into just like you fetch
1389 * state when they are already stored in other places?
1391 * Because of array offsets -> We can stick env/local parameters in the
1392 * middle of a parameter array and then index someplace into the array
1395 * One optimization might be to only do this for the cases where the
1396 * env/local parameters end up inside of an array, and leave the
1397 * single parameters (or arrays of pure env/local pareameters) in their
1398 * respective register files.
1400 * For ENV parameters, the format is:
1401 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1402 * state_tokens[1] = STATE_ENV
1403 * state_tokens[2] = the parameter index
1405 * for LOCAL parameters, the format is:
1406 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1407 * state_tokens[1] = STATE_LOCAL
1408 * state_tokens[2] = the parameter index
1410 * \param inst - the start in the binary arry to start working from
1411 * \param state_tokens - the storage for the 6-token state description
1412 * \return - 0 on sucess, 1 on failure
1415 parse_program_single_item (GLcontext
* ctx
, const GLubyte
** inst
,
1416 struct arb_program
*Program
,
1417 gl_state_index state_tokens
[STATE_LENGTH
])
1419 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1420 state_tokens
[0] = STATE_FRAGMENT_PROGRAM
;
1422 state_tokens
[0] = STATE_VERTEX_PROGRAM
;
1425 switch (*(*inst
)++) {
1426 case PROGRAM_PARAM_ENV
:
1427 state_tokens
[1] = STATE_ENV
;
1428 state_tokens
[2] = parse_integer (inst
, Program
);
1430 /* Check state_tokens[2] against the number of ENV parameters available */
1431 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1432 (state_tokens
[2] >= (GLint
) ctx
->Const
.FragmentProgram
.MaxEnvParams
))
1434 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1435 (state_tokens
[2] >= (GLint
) ctx
->Const
.VertexProgram
.MaxEnvParams
))) {
1436 program_error(ctx
, Program
->Position
,
1437 "Invalid Program Env Parameter");
1438 /* bad state_tokens[2] */
1444 case PROGRAM_PARAM_LOCAL
:
1445 state_tokens
[1] = STATE_LOCAL
;
1446 state_tokens
[2] = parse_integer (inst
, Program
);
1448 /* Check state_tokens[2] against the number of LOCAL parameters available */
1449 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1450 (state_tokens
[2] >= (GLint
) ctx
->Const
.FragmentProgram
.MaxLocalParams
))
1452 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1453 (state_tokens
[2] >= (GLint
) ctx
->Const
.VertexProgram
.MaxLocalParams
))) {
1454 program_error(ctx
, Program
->Position
,
1455 "Invalid Program Local Parameter");
1456 /* bad state_tokens[2] */
1466 * For ARB_vertex_program, programs are not allowed to use both an explicit
1467 * vertex attribute and a generic vertex attribute corresponding to the same
1468 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1470 * This will walk our var_cache and make sure that nobody does anything fishy.
1472 * \return 0 on sucess, 1 on error
1475 generic_attrib_check(struct var_cache
*vc_head
)
1478 struct var_cache
*curr
;
1479 GLboolean explicitAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
],
1480 genericAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
];
1482 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1483 explicitAttrib
[a
] = GL_FALSE
;
1484 genericAttrib
[a
] = GL_FALSE
;
1489 if (curr
->type
== vt_attrib
) {
1490 if (curr
->attrib_is_generic
)
1491 genericAttrib
[ curr
->attrib_binding
] = GL_TRUE
;
1493 explicitAttrib
[ curr
->attrib_binding
] = GL_TRUE
;
1499 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1500 if ((explicitAttrib
[a
]) && (genericAttrib
[a
]))
1508 * This will handle the binding side of an ATTRIB var declaration
1510 * \param inputReg returns the input register index, one of the
1511 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
1512 * \return returns 0 on success, 1 on error
1515 parse_attrib_binding(GLcontext
* ctx
, const GLubyte
** inst
,
1516 struct arb_program
*Program
,
1517 GLuint
*inputReg
, GLuint
*is_generic
)
1523 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1524 switch (*(*inst
)++) {
1525 case FRAGMENT_ATTRIB_COLOR
:
1528 err
= parse_color_type (ctx
, inst
, Program
, &coord
);
1529 *inputReg
= FRAG_ATTRIB_COL0
+ coord
;
1532 case FRAGMENT_ATTRIB_TEXCOORD
:
1534 GLuint texcoord
= 0;
1535 err
= parse_texcoord_num (ctx
, inst
, Program
, &texcoord
);
1536 *inputReg
= FRAG_ATTRIB_TEX0
+ texcoord
;
1539 case FRAGMENT_ATTRIB_FOGCOORD
:
1540 *inputReg
= FRAG_ATTRIB_FOGC
;
1542 case FRAGMENT_ATTRIB_POSITION
:
1543 *inputReg
= FRAG_ATTRIB_WPOS
;
1551 switch (*(*inst
)++) {
1552 case VERTEX_ATTRIB_POSITION
:
1553 *inputReg
= VERT_ATTRIB_POS
;
1556 case VERTEX_ATTRIB_WEIGHT
:
1559 err
= parse_weight_num (ctx
, inst
, Program
, &weight
);
1560 *inputReg
= VERT_ATTRIB_WEIGHT
;
1562 /* hack for Warcraft (see bug 8060) */
1563 _mesa_warning(ctx
, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
1566 program_error(ctx
, Program
->Position
,
1567 "ARB_vertex_blend not supported");
1572 case VERTEX_ATTRIB_NORMAL
:
1573 *inputReg
= VERT_ATTRIB_NORMAL
;
1576 case VERTEX_ATTRIB_COLOR
:
1579 err
= parse_color_type (ctx
, inst
, Program
, &color
);
1581 *inputReg
= VERT_ATTRIB_COLOR1
;
1584 *inputReg
= VERT_ATTRIB_COLOR0
;
1589 case VERTEX_ATTRIB_FOGCOORD
:
1590 *inputReg
= VERT_ATTRIB_FOG
;
1593 case VERTEX_ATTRIB_TEXCOORD
:
1596 err
= parse_texcoord_num (ctx
, inst
, Program
, &unit
);
1597 *inputReg
= VERT_ATTRIB_TEX0
+ unit
;
1601 case VERTEX_ATTRIB_MATRIXINDEX
:
1602 /* Not supported at this time */
1604 const char *msg
= "ARB_palette_matrix not supported";
1605 parse_integer (inst
, Program
);
1606 program_error(ctx
, Program
->Position
, msg
);
1610 case VERTEX_ATTRIB_GENERIC
:
1613 err
= parse_generic_attrib_num(ctx
, inst
, Program
, &attrib
);
1616 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1617 * attributes do not alias the conventional vertex
1621 *inputReg
= attrib
+ VERT_ATTRIB_GENERIC0
;
1635 program_error(ctx
, Program
->Position
, "Bad attribute binding");
1643 * This translates between a binary token for an output variable type
1644 * and the mesa token for the same thing.
1646 * \param inst The parsed tokens
1647 * \param outputReg Returned index/number of the output register,
1648 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
1651 parse_result_binding(GLcontext
*ctx
, const GLubyte
**inst
,
1652 GLuint
*outputReg
, struct arb_program
*Program
)
1654 const GLubyte token
= *(*inst
)++;
1657 case FRAGMENT_RESULT_COLOR
:
1658 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1661 /* This gets result of the color buffer we're supposed to
1662 * draw into. This pertains to GL_ARB_draw_buffers.
1664 parse_output_color_num(ctx
, inst
, Program
, &out_color
);
1665 ASSERT(out_color
< MAX_DRAW_BUFFERS
);
1666 *outputReg
= FRAG_RESULT_COLR
;
1669 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1670 *outputReg
= VERT_RESULT_HPOS
;
1674 case FRAGMENT_RESULT_DEPTH
:
1675 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1676 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
1677 *outputReg
= FRAG_RESULT_DEPR
;
1680 /* for vtx programs, this is VERTEX_RESULT_COLOR */
1682 GLuint face_type
= parse_face_type(inst
);
1683 GLint err
= parse_color_type(ctx
, inst
, Program
, &color_type
);
1690 *outputReg
= VERT_RESULT_BFC1
; /* secondary color */
1693 *outputReg
= VERT_RESULT_BFC0
; /* primary color */
1699 *outputReg
= VERT_RESULT_COL1
; /* secondary color */
1703 *outputReg
= VERT_RESULT_COL0
; /* primary color */
1709 case VERTEX_RESULT_FOGCOORD
:
1710 *outputReg
= VERT_RESULT_FOGC
;
1713 case VERTEX_RESULT_POINTSIZE
:
1714 *outputReg
= VERT_RESULT_PSIZ
;
1717 case VERTEX_RESULT_TEXCOORD
:
1720 if (parse_texcoord_num (ctx
, inst
, Program
, &unit
))
1722 *outputReg
= VERT_RESULT_TEX0
+ unit
;
1727 Program
->Base
.OutputsWritten
|= (1 << *outputReg
);
1734 * This handles the declaration of ATTRIB variables
1737 * parse_vert_attrib_binding(), or something like that
1739 * \return 0 on sucess, 1 on error
1742 parse_attrib (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1743 struct arb_program
*Program
)
1746 struct var_cache
*attrib_var
;
1748 attrib_var
= parse_string (inst
, vc_head
, Program
, &found
);
1749 Program
->Position
= parse_position (inst
);
1751 program_error2(ctx
, Program
->Position
,
1752 "Duplicate variable declaration",
1753 (char *) attrib_var
->name
);
1757 attrib_var
->type
= vt_attrib
;
1759 if (parse_attrib_binding(ctx
, inst
, Program
, &attrib_var
->attrib_binding
,
1760 &attrib_var
->attrib_is_generic
))
1763 if (generic_attrib_check(*vc_head
)) {
1764 program_error(ctx
, Program
->Position
,
1765 "Cannot use both a generic vertex attribute "
1766 "and a specific attribute of the same type");
1770 Program
->Base
.NumAttributes
++;
1775 * \param use -- TRUE if we're called when declaring implicit parameters,
1776 * FALSE if we're declaraing variables. This has to do with
1777 * if we get a signed or unsigned float for scalar constants
1780 parse_param_elements (GLcontext
* ctx
, const GLubyte
** inst
,
1781 struct var_cache
*param_var
,
1782 struct arb_program
*Program
, GLboolean use
)
1786 gl_state_index state_tokens
[STATE_LENGTH
] = {0, 0, 0, 0, 0};
1787 GLfloat const_values
[4];
1789 GLubyte token
= *(*inst
)++;
1792 case PARAM_STATE_ELEMENT
:
1793 if (parse_state_single_item (ctx
, inst
, Program
, state_tokens
))
1796 /* If we adding STATE_MATRIX that has multiple rows, we need to
1797 * unroll it and call _mesa_add_state_reference() for each row
1799 if ((state_tokens
[0] == STATE_MODELVIEW_MATRIX
||
1800 state_tokens
[0] == STATE_PROJECTION_MATRIX
||
1801 state_tokens
[0] == STATE_MVP_MATRIX
||
1802 state_tokens
[0] == STATE_TEXTURE_MATRIX
||
1803 state_tokens
[0] == STATE_PROGRAM_MATRIX
)
1804 && (state_tokens
[2] != state_tokens
[3])) {
1806 const GLint first_row
= state_tokens
[2];
1807 const GLint last_row
= state_tokens
[3];
1809 for (row
= first_row
; row
<= last_row
; row
++) {
1810 state_tokens
[2] = state_tokens
[3] = row
;
1812 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1814 if (param_var
->param_binding_begin
== ~0U)
1815 param_var
->param_binding_begin
= idx
;
1816 param_var
->param_binding_length
++;
1817 Program
->Base
.NumParameters
++;
1821 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1823 if (param_var
->param_binding_begin
== ~0U)
1824 param_var
->param_binding_begin
= idx
;
1825 param_var
->param_binding_length
++;
1826 Program
->Base
.NumParameters
++;
1830 case PARAM_PROGRAM_ELEMENT
:
1831 if (parse_program_single_item (ctx
, inst
, Program
, state_tokens
))
1833 idx
= _mesa_add_state_reference (Program
->Base
.Parameters
, state_tokens
);
1834 if (param_var
->param_binding_begin
== ~0U)
1835 param_var
->param_binding_begin
= idx
;
1836 param_var
->param_binding_length
++;
1837 Program
->Base
.NumParameters
++;
1839 /* Check if there is more: 0 -> we're done, else its an integer */
1841 GLuint out_of_range
, new_idx
;
1842 GLuint start_idx
= state_tokens
[2] + 1;
1843 GLuint end_idx
= parse_integer (inst
, Program
);
1846 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1847 if (((state_tokens
[1] == STATE_ENV
)
1848 && (end_idx
>= ctx
->Const
.FragmentProgram
.MaxEnvParams
))
1849 || ((state_tokens
[1] == STATE_LOCAL
)
1851 ctx
->Const
.FragmentProgram
.MaxLocalParams
)))
1855 if (((state_tokens
[1] == STATE_ENV
)
1856 && (end_idx
>= ctx
->Const
.VertexProgram
.MaxEnvParams
))
1857 || ((state_tokens
[1] == STATE_LOCAL
)
1859 ctx
->Const
.VertexProgram
.MaxLocalParams
)))
1863 program_error(ctx
, Program
->Position
,
1864 "Invalid Program Parameter"); /*end_idx*/
1868 for (new_idx
= start_idx
; new_idx
<= end_idx
; new_idx
++) {
1869 state_tokens
[2] = new_idx
;
1870 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1872 param_var
->param_binding_length
++;
1873 Program
->Base
.NumParameters
++;
1881 case PARAM_CONSTANT
:
1882 /* parsing something like {1.0, 2.0, 3.0, 4.0} */
1883 parse_constant (inst
, const_values
, Program
, use
);
1884 idx
= _mesa_add_named_constant(Program
->Base
.Parameters
,
1885 (char *) param_var
->name
,
1887 if (param_var
->param_binding_begin
== ~0U)
1888 param_var
->param_binding_begin
= idx
;
1889 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1890 /* Note: when we reference this parameter in an instruction later,
1891 * we'll check if it's really a constant/immediate and set the
1892 * instruction register type appropriately.
1894 param_var
->param_binding_length
++;
1895 Program
->Base
.NumParameters
++;
1899 program_error(ctx
, Program
->Position
,
1900 "Unexpected token (in parse_param_elements())");
1904 /* Make sure we haven't blown past our parameter limits */
1905 if (((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1906 (Program
->Base
.NumParameters
>=
1907 ctx
->Const
.VertexProgram
.MaxLocalParams
))
1908 || ((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1909 && (Program
->Base
.NumParameters
>=
1910 ctx
->Const
.FragmentProgram
.MaxLocalParams
))) {
1911 program_error(ctx
, Program
->Position
, "Too many parameter variables");
1920 * This picks out PARAM program parameter bindings.
1922 * XXX: This needs to be stressed & tested
1924 * \return 0 on sucess, 1 on error
1927 parse_param (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1928 struct arb_program
*Program
)
1931 GLint specified_length
;
1932 struct var_cache
*param_var
;
1935 param_var
= parse_string (inst
, vc_head
, Program
, &found
);
1936 Program
->Position
= parse_position (inst
);
1939 program_error2(ctx
, Program
->Position
,
1940 "Duplicate variable declaration",
1941 (char *) param_var
->name
);
1945 specified_length
= parse_integer (inst
, Program
);
1947 if (specified_length
< 0) {
1948 program_error(ctx
, Program
->Position
, "Negative parameter array length");
1952 param_var
->type
= vt_param
;
1953 param_var
->param_binding_length
= 0;
1955 /* Right now, everything is shoved into the main state register file.
1957 * In the future, it would be nice to leave things ENV/LOCAL params
1958 * in their respective register files, if possible
1960 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1963 * * - add each guy to the parameter list
1964 * * - increment the param_var->param_binding_len
1965 * * - store the param_var->param_binding_begin for the first one
1966 * * - compare the actual len to the specified len at the end
1968 while (**inst
!= PARAM_NULL
) {
1969 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_FALSE
))
1973 /* Test array length here! */
1974 if (specified_length
) {
1975 if (specified_length
!= (int)param_var
->param_binding_length
) {
1976 program_error(ctx
, Program
->Position
,
1977 "Declared parameter array length does not match parameter list");
1990 parse_param_use (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1991 struct arb_program
*Program
, struct var_cache
**new_var
)
1993 struct var_cache
*param_var
;
1995 /* First, insert a dummy entry into the var_cache */
1996 var_cache_create (¶m_var
);
1997 param_var
->name
= (const GLubyte
*) " ";
1998 param_var
->type
= vt_param
;
2000 param_var
->param_binding_length
= 0;
2001 /* Don't fill in binding_begin; We use the default value of -1
2002 * to tell if its already initialized, elsewhere.
2004 * param_var->param_binding_begin = 0;
2006 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
2008 var_cache_append (vc_head
, param_var
);
2010 /* Then fill it with juicy parameter goodness */
2011 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_TRUE
))
2014 *new_var
= param_var
;
2021 * This handles the declaration of TEMP variables
2023 * \return 0 on sucess, 1 on error
2026 parse_temp (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2027 struct arb_program
*Program
)
2030 struct var_cache
*temp_var
;
2032 while (**inst
!= 0) {
2033 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2034 Program
->Position
= parse_position (inst
);
2036 program_error2(ctx
, Program
->Position
,
2037 "Duplicate variable declaration",
2038 (char *) temp_var
->name
);
2042 temp_var
->type
= vt_temp
;
2044 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
2045 (Program
->Base
.NumTemporaries
>=
2046 ctx
->Const
.FragmentProgram
.MaxTemps
))
2047 || ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
2048 && (Program
->Base
.NumTemporaries
>=
2049 ctx
->Const
.VertexProgram
.MaxTemps
))) {
2050 program_error(ctx
, Program
->Position
,
2051 "Too many TEMP variables declared");
2055 temp_var
->temp_binding
= Program
->Base
.NumTemporaries
;
2056 Program
->Base
.NumTemporaries
++;
2064 * This handles variables of the OUTPUT variety
2066 * \return 0 on sucess, 1 on error
2069 parse_output (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2070 struct arb_program
*Program
)
2073 struct var_cache
*output_var
;
2076 output_var
= parse_string (inst
, vc_head
, Program
, &found
);
2077 Program
->Position
= parse_position (inst
);
2079 program_error2(ctx
, Program
->Position
,
2080 "Duplicate variable declaration",
2081 (char *) output_var
->name
);
2085 output_var
->type
= vt_output
;
2087 err
= parse_result_binding(ctx
, inst
, &output_var
->output_binding
, Program
);
2092 * This handles variables of the ALIAS kind
2094 * \return 0 on sucess, 1 on error
2097 parse_alias (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2098 struct arb_program
*Program
)
2101 struct var_cache
*temp_var
;
2103 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2104 Program
->Position
= parse_position (inst
);
2107 program_error2(ctx
, Program
->Position
,
2108 "Duplicate variable declaration",
2109 (char *) temp_var
->name
);
2113 temp_var
->type
= vt_alias
;
2114 temp_var
->alias_binding
= parse_string (inst
, vc_head
, Program
, &found
);
2115 Program
->Position
= parse_position (inst
);
2119 program_error2(ctx
, Program
->Position
,
2120 "Undefined alias value",
2121 (char *) temp_var
->alias_binding
->name
);
2129 * This handles variables of the ADDRESS kind
2131 * \return 0 on sucess, 1 on error
2134 parse_address (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2135 struct arb_program
*Program
)
2138 struct var_cache
*temp_var
;
2140 while (**inst
!= 0) {
2141 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2142 Program
->Position
= parse_position (inst
);
2144 program_error2(ctx
, Program
->Position
,
2145 "Duplicate variable declaration",
2146 (char *) temp_var
->name
);
2150 temp_var
->type
= vt_address
;
2152 if (Program
->Base
.NumAddressRegs
>=
2153 ctx
->Const
.VertexProgram
.MaxAddressRegs
) {
2154 const char *msg
= "Too many ADDRESS variables declared";
2155 program_error(ctx
, Program
->Position
, msg
);
2159 temp_var
->address_binding
= Program
->Base
.NumAddressRegs
;
2160 Program
->Base
.NumAddressRegs
++;
2168 * Parse a program declaration
2170 * \return 0 on sucess, 1 on error
2173 parse_declaration (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2174 struct arb_program
*Program
)
2178 switch (*(*inst
)++) {
2180 err
= parse_address (ctx
, inst
, vc_head
, Program
);
2184 err
= parse_alias (ctx
, inst
, vc_head
, Program
);
2188 err
= parse_attrib (ctx
, inst
, vc_head
, Program
);
2192 err
= parse_output (ctx
, inst
, vc_head
, Program
);
2196 err
= parse_param (ctx
, inst
, vc_head
, Program
);
2200 err
= parse_temp (ctx
, inst
, vc_head
, Program
);
2208 * Handle the parsing out of a masked destination register, either for a
2209 * vertex or fragment program.
2211 * If we are a vertex program, make sure we don't write to
2212 * result.position if we have specified that the program is
2213 * position invariant
2215 * \param File - The register file we write to
2216 * \param Index - The register index we write to
2217 * \param WriteMask - The mask controlling which components we write (1->write)
2219 * \return 0 on sucess, 1 on error
2222 parse_masked_dst_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2223 struct var_cache
**vc_head
, struct arb_program
*Program
,
2224 enum register_file
*File
, GLuint
*Index
, GLint
*WriteMask
)
2227 struct var_cache
*dst
;
2229 /* We either have a result register specified, or a
2230 * variable that may or may not be writable
2232 switch (*(*inst
)++) {
2233 case REGISTER_RESULT
:
2234 if (parse_result_binding(ctx
, inst
, Index
, Program
))
2236 *File
= PROGRAM_OUTPUT
;
2239 case REGISTER_ESTABLISHED_NAME
:
2240 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2241 Program
->Position
= parse_position (inst
);
2243 /* If the name has never been added to our symbol table, we're hosed */
2245 program_error(ctx
, Program
->Position
, "0: Undefined variable");
2249 switch (dst
->type
) {
2251 *File
= PROGRAM_OUTPUT
;
2252 *Index
= dst
->output_binding
;
2256 *File
= PROGRAM_TEMPORARY
;
2257 *Index
= dst
->temp_binding
;
2260 /* If the var type is not vt_output or vt_temp, no go */
2262 program_error(ctx
, Program
->Position
,
2263 "Destination register is read only");
2269 program_error(ctx
, Program
->Position
,
2270 "Unexpected opcode in parse_masked_dst_reg()");
2275 /* Position invariance test */
2276 /* This test is done now in syntax portion - when position invariance OPTION
2277 is specified, "result.position" rule is disabled so there is no way
2278 to write the position
2280 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2282 program_error(ctx, Program->Position,
2283 "Vertex program specified position invariance and wrote vertex position");
2286 /* And then the mask.
2292 * ==> Need to reverse the order of bits for this!
2294 tmp
= (GLint
) *(*inst
)++;
2295 *WriteMask
= (((tmp
>>3) & 0x1) |
2305 * Handle the parsing of a address register
2307 * \param Index - The register index we write to
2309 * \return 0 on sucess, 1 on error
2312 parse_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2313 struct var_cache
**vc_head
,
2314 struct arb_program
*Program
, GLint
* Index
)
2316 struct var_cache
*dst
;
2319 *Index
= 0; /* XXX */
2321 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2322 Program
->Position
= parse_position (inst
);
2324 /* If the name has never been added to our symbol table, we're hosed */
2326 program_error(ctx
, Program
->Position
, "Undefined variable");
2330 if (dst
->type
!= vt_address
) {
2331 program_error(ctx
, Program
->Position
, "Variable is not of type ADDRESS");
2340 * Handle the parsing out of a masked address register
2342 * \param Index - The register index we write to
2343 * \param WriteMask - The mask controlling which components we write (1->write)
2345 * \return 0 on sucess, 1 on error
2348 parse_masked_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2349 struct var_cache
**vc_head
,
2350 struct arb_program
*Program
, GLint
* Index
,
2351 GLboolean
* WriteMask
)
2353 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, Index
))
2356 /* This should be 0x8 */
2359 /* Writemask of .x is implied */
2361 WriteMask
[1] = WriteMask
[2] = WriteMask
[3] = 0;
2368 * Parse out a swizzle mask.
2370 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
2372 * The len parameter allows us to grab 4 components for a vector
2373 * swizzle, or just 1 component for a scalar src register selection
2376 parse_swizzle_mask(const GLubyte
** inst
, GLubyte
*swizzle
, GLint len
)
2380 for (i
= 0; i
< 4; i
++)
2383 for (i
= 0; i
< len
; i
++) {
2384 switch (*(*inst
)++) {
2386 swizzle
[i
] = SWIZZLE_X
;
2389 swizzle
[i
] = SWIZZLE_Y
;
2392 swizzle
[i
] = SWIZZLE_Z
;
2395 swizzle
[i
] = SWIZZLE_W
;
2398 _mesa_problem(NULL
, "bad component in parse_swizzle_mask()");
2406 * Parse an extended swizzle mask which is a sequence of
2407 * four x/y/z/w/0/1 tokens.
2408 * \return swizzle four swizzle values
2409 * \return negateMask four element bitfield
2412 parse_extended_swizzle_mask(const GLubyte
**inst
, GLubyte swizzle
[4],
2413 GLubyte
*negateMask
)
2418 for (i
= 0; i
< 4; i
++) {
2420 if (parse_sign(inst
) == -1)
2421 *negateMask
|= (1 << i
);
2427 swizzle
[i
] = SWIZZLE_ZERO
;
2430 swizzle
[i
] = SWIZZLE_ONE
;
2433 swizzle
[i
] = SWIZZLE_X
;
2436 swizzle
[i
] = SWIZZLE_Y
;
2439 swizzle
[i
] = SWIZZLE_Z
;
2442 swizzle
[i
] = SWIZZLE_W
;
2445 _mesa_problem(NULL
, "bad case in parse_extended_swizzle_mask()");
2453 parse_src_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2454 struct var_cache
**vc_head
,
2455 struct arb_program
*Program
,
2456 enum register_file
* File
, GLint
* Index
,
2457 GLboolean
*IsRelOffset
)
2459 struct var_cache
*src
;
2460 GLuint binding
, is_generic
, found
;
2465 /* And the binding for the src */
2466 switch (*(*inst
)++) {
2467 case REGISTER_ATTRIB
:
2468 if (parse_attrib_binding
2469 (ctx
, inst
, Program
, &binding
, &is_generic
))
2471 *File
= PROGRAM_INPUT
;
2474 /* We need to insert a dummy variable into the var_cache so we can
2475 * catch generic vertex attrib aliasing errors
2477 var_cache_create(&src
);
2478 src
->type
= vt_attrib
;
2479 src
->name
= (const GLubyte
*) "Dummy Attrib Variable";
2480 src
->attrib_binding
= binding
;
2481 src
->attrib_is_generic
= is_generic
;
2482 var_cache_append(vc_head
, src
);
2483 if (generic_attrib_check(*vc_head
)) {
2484 program_error(ctx
, Program
->Position
,
2485 "Cannot use both a generic vertex attribute "
2486 "and a specific attribute of the same type");
2491 case REGISTER_PARAM
:
2493 case PARAM_ARRAY_ELEMENT
:
2495 src
= parse_string (inst
, vc_head
, Program
, &found
);
2496 Program
->Position
= parse_position (inst
);
2499 program_error2(ctx
, Program
->Position
,
2500 "Undefined variable",
2501 (char *) src
->name
);
2505 *File
= (enum register_file
) src
->param_binding_type
;
2507 switch (*(*inst
)++) {
2508 case ARRAY_INDEX_ABSOLUTE
:
2509 offset
= parse_integer (inst
, Program
);
2512 || (offset
>= (int)src
->param_binding_length
)) {
2513 program_error(ctx
, Program
->Position
,
2514 "Index out of range");
2515 /* offset, src->name */
2519 *Index
= src
->param_binding_begin
+ offset
;
2522 case ARRAY_INDEX_RELATIVE
:
2524 GLint addr_reg_idx
, rel_off
;
2526 /* First, grab the address regiseter */
2527 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &addr_reg_idx
))
2536 /* Then the relative offset */
2537 if (parse_relative_offset(ctx
, inst
, Program
, &rel_off
)) return 1;
2539 /* And store it properly */
2540 *Index
= src
->param_binding_begin
+ rel_off
;
2548 if (parse_param_use (ctx
, inst
, vc_head
, Program
, &src
))
2551 *File
= (enum register_file
) src
->param_binding_type
;
2552 *Index
= src
->param_binding_begin
;
2557 case REGISTER_ESTABLISHED_NAME
:
2558 src
= parse_string (inst
, vc_head
, Program
, &found
);
2559 Program
->Position
= parse_position (inst
);
2561 /* If the name has never been added to our symbol table, we're hosed */
2563 program_error(ctx
, Program
->Position
,
2564 "3: Undefined variable"); /* src->name */
2568 switch (src
->type
) {
2570 *File
= PROGRAM_INPUT
;
2571 *Index
= src
->attrib_binding
;
2574 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2576 *File
= (enum register_file
) src
->param_binding_type
;
2577 *Index
= src
->param_binding_begin
;
2581 *File
= PROGRAM_TEMPORARY
;
2582 *Index
= src
->temp_binding
;
2585 /* If the var type is vt_output no go */
2587 program_error(ctx
, Program
->Position
,
2588 "destination register is read only");
2595 program_error(ctx
, Program
->Position
,
2596 "Unknown token in parse_src_reg");
2600 if (*File
== PROGRAM_STATE_VAR
) {
2601 enum register_file file
;
2603 /* If we're referencing the Program->Parameters[] array, check if the
2604 * parameter is really a constant/literal. If so, set File to CONSTANT.
2606 assert(*Index
< (GLint
) Program
->Base
.Parameters
->NumParameters
);
2607 file
= Program
->Base
.Parameters
->Parameters
[*Index
].Type
;
2608 if (file
== PROGRAM_CONSTANT
)
2609 *File
= PROGRAM_CONSTANT
;
2612 /* Add attributes to InputsRead only if they are used the program.
2613 * This avoids the handling of unused ATTRIB declarations in the drivers. */
2614 if (*File
== PROGRAM_INPUT
)
2615 Program
->Base
.InputsRead
|= (1 << *Index
);
2622 * Parse vertex/fragment program vector source register.
2625 parse_vector_src_reg(GLcontext
*ctx
, const GLubyte
**inst
,
2626 struct var_cache
**vc_head
,
2627 struct arb_program
*program
,
2628 struct prog_src_register
*reg
)
2630 enum register_file file
;
2634 GLboolean isRelOffset
;
2637 negateMask
= (parse_sign (inst
) == -1) ? NEGATE_XYZW
: NEGATE_NONE
;
2639 /* And the src reg */
2640 if (parse_src_reg(ctx
, inst
, vc_head
, program
, &file
, &index
, &isRelOffset
))
2643 /* finally, the swizzle */
2644 parse_swizzle_mask(inst
, swizzle
, 4);
2648 reg
->Swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
2649 reg
->NegateBase
= negateMask
;
2650 reg
->RelAddr
= isRelOffset
;
2656 * Parse vertex/fragment program scalar source register.
2659 parse_scalar_src_reg(GLcontext
*ctx
, const GLubyte
**inst
,
2660 struct var_cache
**vc_head
,
2661 struct arb_program
*program
,
2662 struct prog_src_register
*reg
)
2664 enum register_file file
;
2668 GLboolean isRelOffset
;
2671 negateMask
= (parse_sign (inst
) == -1) ? NEGATE_XYZW
: NEGATE_NONE
;
2673 /* And the src reg */
2674 if (parse_src_reg(ctx
, inst
, vc_head
, program
, &file
, &index
, &isRelOffset
))
2677 /* finally, the swizzle */
2678 parse_swizzle_mask(inst
, swizzle
, 1);
2682 reg
->Swizzle
= (swizzle
[0] << 0);
2683 reg
->NegateBase
= negateMask
;
2684 reg
->RelAddr
= isRelOffset
;
2690 * Parse vertex/fragment program destination register.
2691 * \return 1 if error, 0 if no error.
2694 parse_dst_reg(GLcontext
* ctx
, const GLubyte
** inst
,
2695 struct var_cache
**vc_head
, struct arb_program
*program
,
2696 struct prog_dst_register
*reg
)
2700 enum register_file file
;
2702 if (parse_masked_dst_reg (ctx
, inst
, vc_head
, program
, &file
, &idx
, &mask
))
2707 reg
->WriteMask
= mask
;
2713 * This is a big mother that handles getting opcodes into the instruction
2714 * and handling the src & dst registers for fragment program instructions
2715 * \return 1 if error, 0 if no error
2718 parse_fp_instruction (GLcontext
* ctx
, const GLubyte
** inst
,
2719 struct var_cache
**vc_head
, struct arb_program
*Program
,
2720 struct prog_instruction
*fp
)
2724 GLubyte instClass
, type
, code
;
2726 GLuint shadow_tex
= 0;
2728 _mesa_init_instructions(fp
, 1);
2730 /* Record the position in the program string for debugging */
2731 fp
->StringPos
= Program
->Position
;
2733 /* OP_ALU_INST or OP_TEX_INST */
2734 instClass
= *(*inst
)++;
2736 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2737 * OP_TEX_{SAMPLE, KIL}
2741 /* The actual opcode name */
2744 /* Increment the correct count */
2745 switch (instClass
) {
2747 Program
->NumAluInstructions
++;
2750 Program
->NumTexInstructions
++;
2758 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2760 fp
->Opcode
= OPCODE_ABS
;
2764 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2766 fp
->Opcode
= OPCODE_FLR
;
2770 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2772 fp
->Opcode
= OPCODE_FRC
;
2776 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2778 fp
->Opcode
= OPCODE_LIT
;
2782 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2784 fp
->Opcode
= OPCODE_MOV
;
2788 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2791 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2798 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2800 fp
->Opcode
= OPCODE_COS
;
2804 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2806 fp
->Opcode
= OPCODE_EX2
;
2810 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2812 fp
->Opcode
= OPCODE_LG2
;
2816 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2818 fp
->Opcode
= OPCODE_RCP
;
2822 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2824 fp
->Opcode
= OPCODE_RSQ
;
2828 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2830 fp
->Opcode
= OPCODE_SIN
;
2834 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2837 fp
->Opcode
= OPCODE_SCS
;
2841 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2844 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2851 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2853 fp
->Opcode
= OPCODE_POW
;
2857 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2860 for (a
= 0; a
< 2; a
++) {
2861 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2870 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2872 fp
->Opcode
= OPCODE_ADD
;
2876 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2878 fp
->Opcode
= OPCODE_DP3
;
2882 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2884 fp
->Opcode
= OPCODE_DP4
;
2888 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2890 fp
->Opcode
= OPCODE_DPH
;
2894 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2896 fp
->Opcode
= OPCODE_DST
;
2900 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2902 fp
->Opcode
= OPCODE_MAX
;
2906 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2908 fp
->Opcode
= OPCODE_MIN
;
2912 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2914 fp
->Opcode
= OPCODE_MUL
;
2918 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2920 fp
->Opcode
= OPCODE_SGE
;
2924 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2926 fp
->Opcode
= OPCODE_SLT
;
2930 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2932 fp
->Opcode
= OPCODE_SUB
;
2936 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2938 fp
->Opcode
= OPCODE_XPD
;
2942 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2944 for (a
= 0; a
< 2; a
++) {
2945 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2953 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2955 fp
->Opcode
= OPCODE_CMP
;
2959 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2961 fp
->Opcode
= OPCODE_LRP
;
2965 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2967 fp
->Opcode
= OPCODE_MAD
;
2971 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2974 for (a
= 0; a
< 3; a
++) {
2975 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2983 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2985 fp
->Opcode
= OPCODE_SWZ
;
2988 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2994 enum register_file file
;
2997 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
, &rel
))
2999 parse_extended_swizzle_mask(inst
, swizzle
, &negateMask
);
3000 fp
->SrcReg
[0].File
= file
;
3001 fp
->SrcReg
[0].Index
= index
;
3002 fp
->SrcReg
[0].NegateBase
= negateMask
;
3003 fp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
3013 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3015 fp
->Opcode
= OPCODE_TEX
;
3019 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3021 fp
->Opcode
= OPCODE_TXP
;
3025 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3027 fp
->Opcode
= OPCODE_TXB
;
3031 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3034 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
3038 if (parse_texcoord_num (ctx
, inst
, Program
, &texcoord
))
3040 fp
->TexSrcUnit
= texcoord
;
3043 switch (*(*inst
)++) {
3044 case TEXTARGET_SHADOW1D
:
3045 shadow_tex
= 1 << texcoord
;
3048 fp
->TexSrcTarget
= TEXTURE_1D_INDEX
;
3050 case TEXTARGET_SHADOW2D
:
3051 shadow_tex
= 1 << texcoord
;
3054 fp
->TexSrcTarget
= TEXTURE_2D_INDEX
;
3057 fp
->TexSrcTarget
= TEXTURE_3D_INDEX
;
3059 case TEXTARGET_SHADOWRECT
:
3060 shadow_tex
= 1 << texcoord
;
3062 case TEXTARGET_RECT
:
3063 fp
->TexSrcTarget
= TEXTURE_RECT_INDEX
;
3065 case TEXTARGET_CUBE
:
3066 fp
->TexSrcTarget
= TEXTURE_CUBE_INDEX
;
3068 case TEXTARGET_SHADOW1D_ARRAY
:
3069 shadow_tex
= 1 << texcoord
;
3071 case TEXTARGET_1D_ARRAY
:
3072 fp
->TexSrcTarget
= TEXTURE_1D_ARRAY_INDEX
;
3074 case TEXTARGET_SHADOW2D_ARRAY
:
3075 shadow_tex
= 1 << texcoord
;
3077 case TEXTARGET_2D_ARRAY
:
3078 fp
->TexSrcTarget
= TEXTURE_2D_ARRAY_INDEX
;
3082 /* Don't test the first time a particular sampler is seen. Each time
3083 * after that, make sure the shadow state is the same.
3085 if ((_mesa_bitcount(Program
->TexturesUsed
[texcoord
]) > 0)
3086 && ((Program
->ShadowSamplers
& (1 << texcoord
)) != shadow_tex
)) {
3087 program_error(ctx
, Program
->Position
,
3088 "texture image unit used for shadow sampling and non-shadow sampling");
3092 Program
->TexturesUsed
[texcoord
] |= (1 << fp
->TexSrcTarget
);
3093 /* Check that both "2D" and "CUBE" (for example) aren't both used */
3094 if (_mesa_bitcount(Program
->TexturesUsed
[texcoord
]) > 1) {
3095 program_error(ctx
, Program
->Position
,
3096 "multiple targets used on one texture image unit");
3101 Program
->ShadowSamplers
|= shadow_tex
;
3105 Program
->UsesKill
= 1;
3106 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
3108 fp
->Opcode
= OPCODE_KIL
;
3111 _mesa_problem(ctx
, "bad type 0x%x in parse_fp_instruction()", type
);
3120 * Handle the parsing out of a masked address register
3122 * \param Index - The register index we write to
3123 * \param WriteMask - The mask controlling which components we write (1->write)
3125 * \return 0 on sucess, 1 on error
3128 parse_vp_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
3129 struct var_cache
**vc_head
,
3130 struct arb_program
*Program
,
3131 struct prog_dst_register
*reg
)
3135 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &idx
))
3138 /* This should be 0x8 */
3141 reg
->File
= PROGRAM_ADDRESS
;
3144 /* Writemask of .x is implied */
3145 reg
->WriteMask
= 0x1;
3151 * This is a big mother that handles getting opcodes into the instruction
3152 * and handling the src & dst registers for vertex program instructions
3155 parse_vp_instruction (GLcontext
* ctx
, const GLubyte
** inst
,
3156 struct var_cache
**vc_head
, struct arb_program
*Program
,
3157 struct prog_instruction
*vp
)
3162 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3165 /* The actual opcode name */
3168 _mesa_init_instructions(vp
, 1);
3169 /* Record the position in the program string for debugging */
3170 vp
->StringPos
= Program
->Position
;
3175 vp
->Opcode
= OPCODE_ARL
;
3177 /* Remember to set SrcReg.RelAddr; */
3179 /* Get the masked address register [dst] */
3180 if (parse_vp_address_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3183 vp
->DstReg
.File
= PROGRAM_ADDRESS
;
3185 /* Get a scalar src register */
3186 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3194 vp
->Opcode
= OPCODE_ABS
;
3197 vp
->Opcode
= OPCODE_FLR
;
3200 vp
->Opcode
= OPCODE_FRC
;
3203 vp
->Opcode
= OPCODE_LIT
;
3206 vp
->Opcode
= OPCODE_MOV
;
3210 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3213 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3220 vp
->Opcode
= OPCODE_EX2
;
3223 vp
->Opcode
= OPCODE_EXP
;
3226 vp
->Opcode
= OPCODE_LG2
;
3229 vp
->Opcode
= OPCODE_LOG
;
3232 vp
->Opcode
= OPCODE_RCP
;
3235 vp
->Opcode
= OPCODE_RSQ
;
3238 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3241 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3248 vp
->Opcode
= OPCODE_POW
;
3251 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3254 for (a
= 0; a
< 2; a
++) {
3255 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3263 vp
->Opcode
= OPCODE_ADD
;
3266 vp
->Opcode
= OPCODE_DP3
;
3269 vp
->Opcode
= OPCODE_DP4
;
3272 vp
->Opcode
= OPCODE_DPH
;
3275 vp
->Opcode
= OPCODE_DST
;
3278 vp
->Opcode
= OPCODE_MAX
;
3281 vp
->Opcode
= OPCODE_MIN
;
3284 vp
->Opcode
= OPCODE_MUL
;
3287 vp
->Opcode
= OPCODE_SGE
;
3290 vp
->Opcode
= OPCODE_SLT
;
3293 vp
->Opcode
= OPCODE_SUB
;
3296 vp
->Opcode
= OPCODE_XPD
;
3299 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3302 for (a
= 0; a
< 2; a
++) {
3303 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3311 vp
->Opcode
= OPCODE_MAD
;
3315 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3318 for (a
= 0; a
< 3; a
++) {
3319 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3327 vp
->Opcode
= OPCODE_SWZ
;
3334 enum register_file file
;
3337 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3340 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
, &relAddr
))
3342 parse_extended_swizzle_mask (inst
, swizzle
, &negateMask
);
3343 vp
->SrcReg
[0].File
= file
;
3344 vp
->SrcReg
[0].Index
= index
;
3345 vp
->SrcReg
[0].NegateBase
= negateMask
;
3346 vp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
3350 vp
->SrcReg
[0].RelAddr
= relAddr
;
3360 debug_variables (GLcontext
* ctx
, struct var_cache
*vc_head
,
3361 struct arb_program
*Program
)
3363 struct var_cache
*vc
;
3366 fprintf (stderr
, "debug_variables, vc_head: %p\n", (void*) vc_head
);
3368 /* First of all, print out the contents of the var_cache */
3371 fprintf (stderr
, "[%p]\n", (void*) vc
);
3374 fprintf (stderr
, "UNDEFINED %s\n", vc
->name
);
3377 fprintf (stderr
, "ATTRIB %s\n", vc
->name
);
3378 fprintf (stderr
, " binding: 0x%x\n", vc
->attrib_binding
);
3381 fprintf (stderr
, "PARAM %s begin: %d len: %d\n", vc
->name
,
3382 vc
->param_binding_begin
, vc
->param_binding_length
);
3383 b
= vc
->param_binding_begin
;
3384 for (a
= 0; a
< vc
->param_binding_length
; a
++) {
3385 fprintf (stderr
, "%s\n",
3386 Program
->Base
.Parameters
->Parameters
[a
+ b
].Name
);
3387 if (Program
->Base
.Parameters
->Parameters
[a
+ b
].Type
== PROGRAM_STATE_VAR
) {
3389 s
= _mesa_program_state_string(Program
->Base
.Parameters
->Parameters
3390 [a
+ b
].StateIndexes
);
3391 fprintf(stderr
, "%s\n", s
);
3395 fprintf (stderr
, "%f %f %f %f\n",
3396 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][0],
3397 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][1],
3398 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][2],
3399 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][3]);
3403 fprintf (stderr
, "TEMP %s\n", vc
->name
);
3404 fprintf (stderr
, " binding: 0x%x\n", vc
->temp_binding
);
3407 fprintf (stderr
, "OUTPUT %s\n", vc
->name
);
3408 fprintf (stderr
, " binding: 0x%x\n", vc
->output_binding
);
3411 fprintf (stderr
, "ALIAS %s\n", vc
->name
);
3412 fprintf (stderr
, " binding: 0x%p (%s)\n",
3413 (void*) vc
->alias_binding
, vc
->alias_binding
->name
);
3423 #endif /* DEBUG_PARSING */
3427 * The main loop for parsing a fragment or vertex program
3429 * \return 1 on error, 0 on success
3432 parse_instructions(GLcontext
* ctx
, const GLubyte
* inst
,
3433 struct var_cache
**vc_head
, struct arb_program
*Program
)
3435 const GLuint maxInst
= (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
3436 ? ctx
->Const
.FragmentProgram
.MaxInstructions
3437 : ctx
->Const
.VertexProgram
.MaxInstructions
;
3440 ASSERT(MAX_PROGRAM_INSTRUCTIONS
>= maxInst
);
3442 Program
->MajorVersion
= (GLuint
) * inst
++;
3443 Program
->MinorVersion
= (GLuint
) * inst
++;
3445 while (*inst
!= END
) {
3450 case ARB_PRECISION_HINT_FASTEST
:
3451 Program
->PrecisionOption
= GL_FASTEST
;
3454 case ARB_PRECISION_HINT_NICEST
:
3455 Program
->PrecisionOption
= GL_NICEST
;
3459 Program
->FogOption
= GL_EXP
;
3463 Program
->FogOption
= GL_EXP2
;
3466 case ARB_FOG_LINEAR
:
3467 Program
->FogOption
= GL_LINEAR
;
3470 case ARB_POSITION_INVARIANT
:
3471 if (Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
3472 Program
->HintPositionInvariant
= GL_TRUE
;
3475 case ARB_FRAGMENT_PROGRAM_SHADOW
:
3476 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3477 /* TODO ARB_fragment_program_shadow code */
3481 case ARB_DRAW_BUFFERS
:
3482 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3483 /* do nothing for now */
3487 case MESA_TEXTURE_ARRAY
:
3488 /* do nothing for now */
3495 if (Program
->Base
.NumInstructions
+ 1 >= maxInst
) {
3496 program_error(ctx
, Program
->Position
,
3497 "Max instruction count exceeded");
3500 Program
->Position
= parse_position (&inst
);
3501 /* parse the current instruction */
3502 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3503 err
= parse_fp_instruction (ctx
, &inst
, vc_head
, Program
,
3504 &Program
->Base
.Instructions
[Program
->Base
.NumInstructions
]);
3507 err
= parse_vp_instruction (ctx
, &inst
, vc_head
, Program
,
3508 &Program
->Base
.Instructions
[Program
->Base
.NumInstructions
]);
3511 /* increment instuction count */
3512 Program
->Base
.NumInstructions
++;
3516 err
= parse_declaration (ctx
, &inst
, vc_head
, Program
);
3527 /* Finally, tag on an OPCODE_END instruction */
3529 const GLuint numInst
= Program
->Base
.NumInstructions
;
3530 _mesa_init_instructions(Program
->Base
.Instructions
+ numInst
, 1);
3531 Program
->Base
.Instructions
[numInst
].Opcode
= OPCODE_END
;
3532 /* YYY Wrong Position in program, whatever, at least not random -> crash
3533 Program->Position = parse_position (&inst);
3535 Program
->Base
.Instructions
[numInst
].StringPos
= Program
->Position
;
3537 Program
->Base
.NumInstructions
++;
3540 * Initialize native counts to logical counts. The device driver may
3541 * change them if program is translated into a hardware program.
3543 Program
->Base
.NumNativeInstructions
= Program
->Base
.NumInstructions
;
3544 Program
->Base
.NumNativeTemporaries
= Program
->Base
.NumTemporaries
;
3545 Program
->Base
.NumNativeParameters
= Program
->Base
.NumParameters
;
3546 Program
->Base
.NumNativeAttributes
= Program
->Base
.NumAttributes
;
3547 Program
->Base
.NumNativeAddressRegs
= Program
->Base
.NumAddressRegs
;
3554 LONGSTRING
static char core_grammar_text
[] =
3555 #include "shader/grammar/grammar_syn.h"
3560 * Set a grammar parameter.
3561 * \param name the grammar parameter
3562 * \param value the new parameter value
3563 * \return 0 if OK, 1 if error
3566 set_reg8 (GLcontext
*ctx
, grammar id
, const char *name
, GLubyte value
)
3568 char error_msg
[300];
3571 if (grammar_set_reg8 (id
, (const byte
*) name
, value
))
3574 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3575 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3576 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Grammar Register Error");
3582 * Enable support for the given language option in the parser.
3583 * \return 1 if OK, 0 if error
3586 enable_ext(GLcontext
*ctx
, grammar id
, const char *name
)
3588 return !set_reg8(ctx
, id
, name
, 1);
3593 * Enable parser extensions based on which OpenGL extensions are supported
3594 * by this rendering context.
3596 * \return GL_TRUE if OK, GL_FALSE if error.
3599 enable_parser_extensions(GLcontext
*ctx
, grammar id
)
3602 /* These are not supported at this time */
3603 if ((ctx
->Extensions
.ARB_vertex_blend
||
3604 ctx
->Extensions
.EXT_vertex_weighting
)
3605 && !enable_ext(ctx
, id
, "vertex_blend"))
3607 if (ctx
->Extensions
.ARB_matrix_palette
3608 && !enable_ext(ctx
, id
, "matrix_palette"))
3611 if (ctx
->Extensions
.ARB_fragment_program_shadow
3612 && !enable_ext(ctx
, id
, "fragment_program_shadow"))
3614 if (ctx
->Extensions
.EXT_point_parameters
3615 && !enable_ext(ctx
, id
, "point_parameters"))
3617 if (ctx
->Extensions
.EXT_secondary_color
3618 && !enable_ext(ctx
, id
, "secondary_color"))
3620 if (ctx
->Extensions
.EXT_fog_coord
3621 && !enable_ext(ctx
, id
, "fog_coord"))
3623 if (ctx
->Extensions
.NV_texture_rectangle
3624 && !enable_ext(ctx
, id
, "texture_rectangle"))
3626 if (ctx
->Extensions
.ARB_draw_buffers
3627 && !enable_ext(ctx
, id
, "draw_buffers"))
3629 if (ctx
->Extensions
.MESA_texture_array
3630 && !enable_ext(ctx
, id
, "texture_array"))
3633 /* hack for Warcraft (see bug 8060) */
3634 enable_ext(ctx
, id
, "vertex_blend");
3642 * This kicks everything off.
3644 * \param ctx - The GL Context
3645 * \param str - The program string
3646 * \param len - The program string length
3647 * \param program - The arb_program struct to return all the parsed info in
3648 * \return GL_TRUE on sucess, GL_FALSE on error
3651 _mesa_parse_arb_program(GLcontext
*ctx
, GLenum target
,
3652 const GLubyte
*str
, GLsizei len
,
3653 struct arb_program
*program
)
3655 GLint a
, err
, error_pos
;
3656 char error_msg
[300];
3658 struct var_cache
*vc_head
;
3659 grammar arbprogram_syn_id
;
3660 GLubyte
*parsed
, *inst
;
3661 GLubyte
*strz
= NULL
;
3662 static int arbprogram_syn_is_ok
= 0; /* XXX temporary */
3664 /* set the program target before parsing */
3665 program
->Base
.Target
= target
;
3667 /* Reset error state */
3668 _mesa_set_program_error(ctx
, -1, NULL
);
3670 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
3671 if (!arbprogram_syn_is_ok
) {
3672 /* One-time initialization of parsing system */
3673 grammar grammar_syn_id
;
3676 grammar_syn_id
= grammar_load_from_text ((byte
*) core_grammar_text
);
3677 if (grammar_syn_id
== 0) {
3678 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3679 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3680 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3681 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3682 "glProgramStringARB(Error loading grammar rule set)");
3686 err
= !grammar_check(grammar_syn_id
, (byte
*) arb_grammar_text
,
3687 &parsed
, &parsed_len
);
3689 /* 'parsed' is unused here */
3690 _mesa_free (parsed
);
3693 /* NOTE: we can't destroy grammar_syn_id right here because
3694 * grammar_destroy() can reset the last error
3697 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3698 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3699 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3700 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3701 "glProgramString(Error loading grammar rule set");
3702 grammar_destroy (grammar_syn_id
);
3706 grammar_destroy (grammar_syn_id
);
3708 arbprogram_syn_is_ok
= 1;
3711 /* create the grammar object */
3712 arbprogram_syn_id
= grammar_load_from_text ((byte
*) arb_grammar_text
);
3713 if (arbprogram_syn_id
== 0) {
3714 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3715 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
3716 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3717 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3718 "glProgramString(Error loading grammer rule set)");
3722 /* Set program_target register value */
3723 if (set_reg8 (ctx
, arbprogram_syn_id
, "program_target",
3724 program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
? 0x10 : 0x20)) {
3725 grammar_destroy (arbprogram_syn_id
);
3729 if (!enable_parser_extensions(ctx
, arbprogram_syn_id
)) {
3730 grammar_destroy(arbprogram_syn_id
);
3734 /* check for NULL character occurences */
3737 for (i
= 0; i
< len
; i
++) {
3738 if (str
[i
] == '\0') {
3739 program_error(ctx
, i
, "illegal character");
3740 grammar_destroy (arbprogram_syn_id
);
3746 /* copy the program string to a null-terminated string */
3747 strz
= (GLubyte
*) _mesa_malloc (len
+ 1);
3749 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glProgramStringARB");
3750 grammar_destroy (arbprogram_syn_id
);
3753 _mesa_memcpy (strz
, str
, len
);
3756 /* do a fast check on program string - initial production buffer is 4K */
3757 err
= !grammar_fast_check(arbprogram_syn_id
, strz
,
3758 &parsed
, &parsed_len
, 0x1000);
3760 /* Syntax parse error */
3762 grammar_get_last_error((GLubyte
*) error_msg
, 300, &error_pos
);
3763 program_error(ctx
, error_pos
, error_msg
);
3766 /* useful for debugging */
3770 fprintf(stderr
, "program: %s\n", (char *) strz
);
3771 fprintf(stderr
, "Error Pos: %d\n", ctx
->Program
.ErrorPos
);
3772 s
= (char *) _mesa_find_line_column(strz
, strz
+ctx
->Program
.ErrorPos
,
3774 fprintf(stderr
, "line %d col %d: %s\n", line
, col
, s
);
3781 grammar_destroy (arbprogram_syn_id
);
3785 grammar_destroy (arbprogram_syn_id
);
3788 * Program string is syntactically correct at this point
3789 * Parse the tokenized version of the program now, generating
3790 * vertex/fragment program instructions.
3793 /* Initialize the arb_program struct */
3794 program
->Base
.String
= strz
;
3795 program
->Base
.Instructions
= _mesa_alloc_instructions(MAX_PROGRAM_INSTRUCTIONS
);
3796 program
->Base
.NumInstructions
=
3797 program
->Base
.NumTemporaries
=
3798 program
->Base
.NumParameters
=
3799 program
->Base
.NumAttributes
= program
->Base
.NumAddressRegs
= 0;
3800 program
->Base
.Parameters
= _mesa_new_parameter_list ();
3801 program
->Base
.InputsRead
= 0x0;
3802 program
->Base
.OutputsWritten
= 0x0;
3803 program
->Position
= 0;
3804 program
->MajorVersion
= program
->MinorVersion
= 0;
3805 program
->PrecisionOption
= GL_DONT_CARE
;
3806 program
->FogOption
= GL_NONE
;
3807 program
->HintPositionInvariant
= GL_FALSE
;
3808 for (a
= 0; a
< MAX_TEXTURE_IMAGE_UNITS
; a
++)
3809 program
->TexturesUsed
[a
] = 0x0;
3810 program
->ShadowSamplers
= 0x0;
3811 program
->NumAluInstructions
=
3812 program
->NumTexInstructions
=
3813 program
->NumTexIndirections
= 0;
3814 program
->UsesKill
= 0;
3819 /* Start examining the tokens in the array */
3822 /* Check the grammer rev */
3823 if (*inst
++ != REVISION
) {
3824 program_error (ctx
, 0, "Grammar version mismatch");
3828 /* ignore program target */
3830 err
= parse_instructions(ctx
, inst
, &vc_head
, program
);
3833 /*debug_variables(ctx, vc_head, program); */
3835 /* We're done with the parsed binary array */
3836 var_cache_destroy (&vc_head
);
3838 _mesa_free (parsed
);
3840 /* Reallocate the instruction array from size [MAX_PROGRAM_INSTRUCTIONS]
3841 * to size [ap.Base.NumInstructions].
3843 program
->Base
.Instructions
3844 = _mesa_realloc_instructions(program
->Base
.Instructions
,
3845 MAX_PROGRAM_INSTRUCTIONS
,
3846 program
->Base
.NumInstructions
);
3854 _mesa_parse_arb_fragment_program(GLcontext
* ctx
, GLenum target
,
3855 const GLvoid
*str
, GLsizei len
,
3856 struct gl_fragment_program
*program
)
3858 struct arb_program ap
;
3861 ASSERT(target
== GL_FRAGMENT_PROGRAM_ARB
);
3862 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
, &ap
)) {
3863 /* Error in the program. Just return. */
3867 /* Copy the relevant contents of the arb_program struct into the
3868 * fragment_program struct.
3870 program
->Base
.String
= ap
.Base
.String
;
3871 program
->Base
.NumInstructions
= ap
.Base
.NumInstructions
;
3872 program
->Base
.NumTemporaries
= ap
.Base
.NumTemporaries
;
3873 program
->Base
.NumParameters
= ap
.Base
.NumParameters
;
3874 program
->Base
.NumAttributes
= ap
.Base
.NumAttributes
;
3875 program
->Base
.NumAddressRegs
= ap
.Base
.NumAddressRegs
;
3876 program
->Base
.NumNativeInstructions
= ap
.Base
.NumNativeInstructions
;
3877 program
->Base
.NumNativeTemporaries
= ap
.Base
.NumNativeTemporaries
;
3878 program
->Base
.NumNativeParameters
= ap
.Base
.NumNativeParameters
;
3879 program
->Base
.NumNativeAttributes
= ap
.Base
.NumNativeAttributes
;
3880 program
->Base
.NumNativeAddressRegs
= ap
.Base
.NumNativeAddressRegs
;
3881 program
->Base
.NumAluInstructions
= ap
.Base
.NumAluInstructions
;
3882 program
->Base
.NumTexInstructions
= ap
.Base
.NumTexInstructions
;
3883 program
->Base
.NumTexIndirections
= ap
.Base
.NumTexIndirections
;
3884 program
->Base
.NumNativeAluInstructions
= ap
.Base
.NumAluInstructions
;
3885 program
->Base
.NumNativeTexInstructions
= ap
.Base
.NumTexInstructions
;
3886 program
->Base
.NumNativeTexIndirections
= ap
.Base
.NumTexIndirections
;
3887 program
->Base
.InputsRead
= ap
.Base
.InputsRead
;
3888 program
->Base
.OutputsWritten
= ap
.Base
.OutputsWritten
;
3889 for (i
= 0; i
< MAX_TEXTURE_IMAGE_UNITS
; i
++) {
3890 program
->Base
.TexturesUsed
[i
] = ap
.TexturesUsed
[i
];
3891 if (ap
.TexturesUsed
[i
])
3892 program
->Base
.SamplersUsed
|= (1 << i
);
3894 program
->Base
.ShadowSamplers
= ap
.ShadowSamplers
;
3895 program
->FogOption
= ap
.FogOption
;
3896 program
->UsesKill
= ap
.UsesKill
;
3898 if (program
->FogOption
)
3899 program
->Base
.InputsRead
|= FRAG_BIT_FOGC
;
3901 if (program
->Base
.Instructions
)
3902 _mesa_free(program
->Base
.Instructions
);
3903 program
->Base
.Instructions
= ap
.Base
.Instructions
;
3905 if (program
->Base
.Parameters
)
3906 _mesa_free_parameter_list(program
->Base
.Parameters
);
3907 program
->Base
.Parameters
= ap
.Base
.Parameters
;
3909 /* Append fog instructions now if the program has "OPTION ARB_fog_exp"
3910 * or similar. We used to leave this up to drivers, but it appears
3911 * there's no hardware that wants to do fog in a discrete stage separate
3912 * from the fragment shader.
3914 if (program
->FogOption
!= GL_NONE
) {
3915 _mesa_append_fog_code(ctx
, program
);
3916 program
->FogOption
= GL_NONE
;
3920 _mesa_printf("____________Fragment program %u ________\n", program
->Base
.Id
);
3921 _mesa_print_program(&program
->Base
);
3928 * Parse the vertex program string. If success, update the given
3929 * vertex_program object with the new program. Else, leave the vertex_program
3933 _mesa_parse_arb_vertex_program(GLcontext
*ctx
, GLenum target
,
3934 const GLvoid
*str
, GLsizei len
,
3935 struct gl_vertex_program
*program
)
3937 struct arb_program ap
;
3939 ASSERT(target
== GL_VERTEX_PROGRAM_ARB
);
3941 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
, &ap
)) {
3942 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glProgramString(bad program)");
3946 /* Copy the relevant contents of the arb_program struct into the
3947 * vertex_program struct.
3949 program
->Base
.String
= ap
.Base
.String
;
3950 program
->Base
.NumInstructions
= ap
.Base
.NumInstructions
;
3951 program
->Base
.NumTemporaries
= ap
.Base
.NumTemporaries
;
3952 program
->Base
.NumParameters
= ap
.Base
.NumParameters
;
3953 program
->Base
.NumAttributes
= ap
.Base
.NumAttributes
;
3954 program
->Base
.NumAddressRegs
= ap
.Base
.NumAddressRegs
;
3955 program
->Base
.NumNativeInstructions
= ap
.Base
.NumNativeInstructions
;
3956 program
->Base
.NumNativeTemporaries
= ap
.Base
.NumNativeTemporaries
;
3957 program
->Base
.NumNativeParameters
= ap
.Base
.NumNativeParameters
;
3958 program
->Base
.NumNativeAttributes
= ap
.Base
.NumNativeAttributes
;
3959 program
->Base
.NumNativeAddressRegs
= ap
.Base
.NumNativeAddressRegs
;
3960 program
->Base
.InputsRead
= ap
.Base
.InputsRead
;
3961 program
->Base
.OutputsWritten
= ap
.Base
.OutputsWritten
;
3962 program
->IsPositionInvariant
= ap
.HintPositionInvariant
;
3964 if (program
->Base
.Instructions
)
3965 _mesa_free(program
->Base
.Instructions
);
3966 program
->Base
.Instructions
= ap
.Base
.Instructions
;
3968 if (program
->Base
.Parameters
)
3969 _mesa_free_parameter_list(program
->Base
.Parameters
);
3970 program
->Base
.Parameters
= ap
.Base
.Parameters
;
3973 _mesa_printf("____________Vertex program %u __________\n", program
->Base
.Id
);
3974 _mesa_print_program(&program
->Base
);