2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 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
35 #include "arbprogparse.h"
36 #include "grammar_mesa.h"
40 #include "program_instruction.h"
43 #define MAX_INSTRUCTIONS 256
47 * This is basically a union of the vertex_program and fragment_program
48 * structs that we can use to parse the program into
50 * XXX we can probably get rid of this entirely someday.
56 GLuint Position
; /* Just used for error reporting while parsing */
60 /* ARB_vertex_progmra options */
61 GLboolean HintPositionInvariant
;
63 /* ARB_fragment_progmra options */
64 GLenum PrecisionOption
; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
65 GLenum FogOption
; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
67 /* ARB_fragment_program specifics */
68 GLbitfield TexturesUsed
[MAX_TEXTURE_IMAGE_UNITS
];
69 GLuint NumAluInstructions
;
70 GLuint NumTexInstructions
;
71 GLuint NumTexIndirections
;
78 #if !defined(__GNUC__) || (__GNUC__ < 2) || \
79 ((__GNUC__ == 2) && (__GNUC_MINOR__ <= 7))
80 # define __extension__
85 * Fragment Program Stuff:
86 * -----------------------------------------------------
88 * - things from Michal's email
90 * + not-overflowing floats (don't use parse_integer..)
91 * + can remove range checking in arbparse.c
93 * - check all limits of number of various variables
98 * Vertex Program Stuff:
99 * -----------------------------------------------------
100 * - Optimize param array usage and count limits correctly, see spec,
102 * + Record if an array is reference absolutly or relatively (or both)
103 * + For absolute arrays, store a bitmap of accesses
104 * + For single parameters, store an access flag
105 * + After parsing, make a parameter cleanup and merging pass, where
106 * relative arrays are layed out first, followed by abs arrays, and
107 * finally single state.
108 * + Remap offsets for param src and dst registers
109 * + Now we can properly count parameter usage
111 * - Multiple state binding errors in param arrays (see spec, just before
116 * -----------------------------------------------------
117 * - User clipping planes vs. PositionInvariant
118 * - Is it sufficient to just multiply by the mvp to transform in the
119 * PositionInvariant case? Or do we need something more involved?
121 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
122 * - fetch state listed in program_parameters list
123 * + WTF should this go???
124 * + currently in nvvertexec.c and s_nvfragprog.c
126 * - allow for multiple address registers (and fetch address regs properly)
129 * -----------------------------------------------------
130 * - remove any leftover unused grammer.c stuff (dict_ ?)
131 * - fix grammer.c error handling so its not static
132 * - #ifdef around stuff pertaining to extentions
134 * Outstanding Questions:
135 * -----------------------------------------------------
136 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
137 * what gets hacked off because of this:
138 * + VERTEX_ATTRIB_MATRIXINDEX
139 * + VERTEX_ATTRIB_WEIGHT
143 * - When can we fetch env/local params from their own register files, and
144 * when to we have to fetch them into the main state register file?
148 * -----------------------------------------------------
151 /* Changes since moving the file to shader directory
153 2004-III-4 ------------------------------------------------------------
154 - added #include "grammar_mesa.h"
155 - removed grammar specific code part (it resides now in grammar.c)
156 - added GL_ARB_fragment_program_shadow tokens
157 - modified #include "arbparse_syn.h"
158 - major changes inside _mesa_parse_arb_program()
159 - check the program string for '\0' characters
160 - copy the program string to a one-byte-longer location to have
162 - position invariance test (not writing to result.position) moved
166 typedef GLubyte
*production
;
169 * This is the text describing the rules to parse the grammar
171 __extension__
static char arb_grammar_text
[] =
172 #include "arbprogram_syn.h"
176 * These should match up with the values defined in arbprogram.syn
181 - changed and merged V_* and F_* opcode values to OP_*.
182 - added GL_ARB_fragment_program_shadow specific tokens (michal)
184 #define REVISION 0x09
187 #define FRAGMENT_PROGRAM 0x01
188 #define VERTEX_PROGRAM 0x02
190 /* program section */
192 #define INSTRUCTION 0x02
193 #define DECLARATION 0x03
196 /* GL_ARB_fragment_program option */
197 #define ARB_PRECISION_HINT_FASTEST 0x00
198 #define ARB_PRECISION_HINT_NICEST 0x01
199 #define ARB_FOG_EXP 0x02
200 #define ARB_FOG_EXP2 0x03
201 #define ARB_FOG_LINEAR 0x04
203 /* GL_ARB_vertex_program option */
204 #define ARB_POSITION_INVARIANT 0x05
206 /* GL_ARB_fragment_program_shadow option */
207 #define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
209 /* GL_ARB_draw_buffers option */
210 #define ARB_DRAW_BUFFERS 0x07
212 /* GL_ARB_fragment_program instruction class */
213 #define OP_ALU_INST 0x00
214 #define OP_TEX_INST 0x01
216 /* GL_ARB_vertex_program instruction class */
219 /* GL_ARB_fragment_program instruction type */
220 #define OP_ALU_VECTOR 0x00
221 #define OP_ALU_SCALAR 0x01
222 #define OP_ALU_BINSC 0x02
223 #define OP_ALU_BIN 0x03
224 #define OP_ALU_TRI 0x04
225 #define OP_ALU_SWZ 0x05
226 #define OP_TEX_SAMPLE 0x06
227 #define OP_TEX_KIL 0x07
229 /* GL_ARB_vertex_program instruction type */
230 #define OP_ALU_ARL 0x08
238 /* GL_ARB_fragment_program instruction code */
240 #define OP_ABS_SAT 0x1B
242 #define OP_FLR_SAT 0x26
244 #define OP_FRC_SAT 0x27
246 #define OP_LIT_SAT 0x2A
248 #define OP_MOV_SAT 0x30
250 #define OP_COS_SAT 0x20
252 #define OP_EX2_SAT 0x25
254 #define OP_LG2_SAT 0x29
256 #define OP_RCP_SAT 0x33
258 #define OP_RSQ_SAT 0x34
260 #define OP_SIN_SAT 0x39
262 #define OP_SCS_SAT 0x36
264 #define OP_POW_SAT 0x32
266 #define OP_ADD_SAT 0x1C
268 #define OP_DP3_SAT 0x21
270 #define OP_DP4_SAT 0x22
272 #define OP_DPH_SAT 0x23
274 #define OP_DST_SAT 0x24
276 #define OP_MAX_SAT 0x2E
278 #define OP_MIN_SAT 0x2F
280 #define OP_MUL_SAT 0x31
282 #define OP_SGE_SAT 0x37
284 #define OP_SLT_SAT 0x3A
286 #define OP_SUB_SAT 0x3B
288 #define OP_XPD_SAT 0x43
290 #define OP_CMP_SAT 0x1E
292 #define OP_LRP_SAT 0x2C
294 #define OP_MAD_SAT 0x2D
296 #define OP_SWZ_SAT 0x3C
298 #define OP_TEX_SAT 0x3E
300 #define OP_TXB_SAT 0x40
302 #define OP_TXP_SAT 0x42
305 /* GL_ARB_vertex_program instruction code */
334 /* fragment attribute binding */
335 #define FRAGMENT_ATTRIB_COLOR 0x01
336 #define FRAGMENT_ATTRIB_TEXCOORD 0x02
337 #define FRAGMENT_ATTRIB_FOGCOORD 0x03
338 #define FRAGMENT_ATTRIB_POSITION 0x04
340 /* vertex attribute binding */
341 #define VERTEX_ATTRIB_POSITION 0x01
342 #define VERTEX_ATTRIB_WEIGHT 0x02
343 #define VERTEX_ATTRIB_NORMAL 0x03
344 #define VERTEX_ATTRIB_COLOR 0x04
345 #define VERTEX_ATTRIB_FOGCOORD 0x05
346 #define VERTEX_ATTRIB_TEXCOORD 0x06
347 #define VERTEX_ATTRIB_MATRIXINDEX 0x07
348 #define VERTEX_ATTRIB_GENERIC 0x08
350 /* fragment result binding */
351 #define FRAGMENT_RESULT_COLOR 0x01
352 #define FRAGMENT_RESULT_DEPTH 0x02
354 /* vertex result binding */
355 #define VERTEX_RESULT_POSITION 0x01
356 #define VERTEX_RESULT_COLOR 0x02
357 #define VERTEX_RESULT_FOGCOORD 0x03
358 #define VERTEX_RESULT_POINTSIZE 0x04
359 #define VERTEX_RESULT_TEXCOORD 0x05
362 #define TEXTARGET_1D 0x01
363 #define TEXTARGET_2D 0x02
364 #define TEXTARGET_3D 0x03
365 #define TEXTARGET_RECT 0x04
366 #define TEXTARGET_CUBE 0x05
367 /* GL_ARB_fragment_program_shadow */
368 #define TEXTARGET_SHADOW1D 0x06
369 #define TEXTARGET_SHADOW2D 0x07
370 #define TEXTARGET_SHADOWRECT 0x08
373 #define FACE_FRONT 0x00
374 #define FACE_BACK 0x01
377 #define COLOR_PRIMARY 0x00
378 #define COLOR_SECONDARY 0x01
381 #define COMPONENT_X 0x00
382 #define COMPONENT_Y 0x01
383 #define COMPONENT_Z 0x02
384 #define COMPONENT_W 0x03
385 #define COMPONENT_0 0x04
386 #define COMPONENT_1 0x05
388 /* array index type */
389 #define ARRAY_INDEX_ABSOLUTE 0x00
390 #define ARRAY_INDEX_RELATIVE 0x01
393 #define MATRIX_MODELVIEW 0x01
394 #define MATRIX_PROJECTION 0x02
395 #define MATRIX_MVP 0x03
396 #define MATRIX_TEXTURE 0x04
397 #define MATRIX_PALETTE 0x05
398 #define MATRIX_PROGRAM 0x06
400 /* matrix modifier */
401 #define MATRIX_MODIFIER_IDENTITY 0x00
402 #define MATRIX_MODIFIER_INVERSE 0x01
403 #define MATRIX_MODIFIER_TRANSPOSE 0x02
404 #define MATRIX_MODIFIER_INVTRANS 0x03
407 #define CONSTANT_SCALAR 0x01
408 #define CONSTANT_VECTOR 0x02
410 /* program param type */
411 #define PROGRAM_PARAM_ENV 0x01
412 #define PROGRAM_PARAM_LOCAL 0x02
415 #define REGISTER_ATTRIB 0x01
416 #define REGISTER_PARAM 0x02
417 #define REGISTER_RESULT 0x03
418 #define REGISTER_ESTABLISHED_NAME 0x04
421 #define PARAM_NULL 0x00
422 #define PARAM_ARRAY_ELEMENT 0x01
423 #define PARAM_STATE_ELEMENT 0x02
424 #define PARAM_PROGRAM_ELEMENT 0x03
425 #define PARAM_PROGRAM_ELEMENTS 0x04
426 #define PARAM_CONSTANT 0x05
428 /* param state property */
429 #define STATE_MATERIAL_PARSER 0x01
430 #define STATE_LIGHT_PARSER 0x02
431 #define STATE_LIGHT_MODEL 0x03
432 #define STATE_LIGHT_PROD 0x04
433 #define STATE_FOG 0x05
434 #define STATE_MATRIX_ROWS 0x06
435 /* GL_ARB_fragment_program */
436 #define STATE_TEX_ENV 0x07
437 #define STATE_DEPTH 0x08
438 /* GL_ARB_vertex_program */
439 #define STATE_TEX_GEN 0x09
440 #define STATE_CLIP_PLANE 0x0A
441 #define STATE_POINT 0x0B
443 /* state material property */
444 #define MATERIAL_AMBIENT 0x01
445 #define MATERIAL_DIFFUSE 0x02
446 #define MATERIAL_SPECULAR 0x03
447 #define MATERIAL_EMISSION 0x04
448 #define MATERIAL_SHININESS 0x05
450 /* state light property */
451 #define LIGHT_AMBIENT 0x01
452 #define LIGHT_DIFFUSE 0x02
453 #define LIGHT_SPECULAR 0x03
454 #define LIGHT_POSITION 0x04
455 #define LIGHT_ATTENUATION 0x05
456 #define LIGHT_HALF 0x06
457 #define LIGHT_SPOT_DIRECTION 0x07
459 /* state light model property */
460 #define LIGHT_MODEL_AMBIENT 0x01
461 #define LIGHT_MODEL_SCENECOLOR 0x02
463 /* state light product property */
464 #define LIGHT_PROD_AMBIENT 0x01
465 #define LIGHT_PROD_DIFFUSE 0x02
466 #define LIGHT_PROD_SPECULAR 0x03
468 /* state texture environment property */
469 #define TEX_ENV_COLOR 0x01
471 /* state texture generation coord property */
472 #define TEX_GEN_EYE 0x01
473 #define TEX_GEN_OBJECT 0x02
475 /* state fog property */
476 #define FOG_COLOR 0x01
477 #define FOG_PARAMS 0x02
479 /* state depth property */
480 #define DEPTH_RANGE 0x01
482 /* state point parameters property */
483 #define POINT_SIZE 0x01
484 #define POINT_ATTENUATION 0x02
492 /* GL_ARB_vertex_program */
495 /*-----------------------------------------------------------------------
496 * From here on down is the semantic checking portion
501 * Variable Table Handling functions
516 * Setting an explicit field for each of the binding properties is a bit
517 * wasteful of space, but it should be much more clear when reading later on..
523 GLuint address_binding
; /* The index of the address register we should
525 GLuint attrib_binding
; /* For type vt_attrib, see nvfragprog.h for values */
526 GLuint attrib_is_generic
; /* If the attrib was specified through a generic
528 GLuint temp_binding
; /* The index of the temp register we are to use */
529 GLuint output_binding
; /* Output/result register number */
530 struct var_cache
*alias_binding
; /* For type vt_alias, points to the var_cache entry
531 * that this is aliased to */
532 GLuint param_binding_type
; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
533 * PROGRAM_ENV_PARAM} */
534 GLuint param_binding_begin
; /* This is the offset into the program_parameter_list where
535 * the tokens representing our bound state (or constants)
537 GLuint param_binding_length
; /* This is how many entries in the the program_parameter_list
538 * we take up with our state tokens or constants. Note that
539 * this is _not_ the same as the number of param registers
540 * we eventually use */
541 struct var_cache
*next
;
545 var_cache_create (struct var_cache
**va
)
547 *va
= (struct var_cache
*) _mesa_malloc (sizeof (struct var_cache
));
550 (**va
).type
= vt_none
;
551 (**va
).attrib_binding
= ~0;
552 (**va
).attrib_is_generic
= 0;
553 (**va
).temp_binding
= ~0;
554 (**va
).output_binding
= ~0;
555 (**va
).param_binding_type
= ~0;
556 (**va
).param_binding_begin
= ~0;
557 (**va
).param_binding_length
= ~0;
558 (**va
).alias_binding
= NULL
;
564 var_cache_destroy (struct var_cache
**va
)
567 var_cache_destroy (&(**va
).next
);
574 var_cache_append (struct var_cache
**va
, struct var_cache
*nv
)
577 var_cache_append (&(**va
).next
, nv
);
582 static struct var_cache
*
583 var_cache_find (struct var_cache
*va
, GLubyte
* name
)
585 /*struct var_cache *first = va;*/
588 if (!_mesa_strcmp ( (const char*) name
, (const char*) va
->name
)) {
589 if (va
->type
== vt_alias
)
590 return va
->alias_binding
;
601 * constructs an integer from 4 GLubytes in LE format
604 parse_position (GLubyte
** inst
)
608 value
= (GLuint
) (*(*inst
)++);
609 value
+= (GLuint
) (*(*inst
)++) * 0x100;
610 value
+= (GLuint
) (*(*inst
)++) * 0x10000;
611 value
+= (GLuint
) (*(*inst
)++) * 0x1000000;
617 * This will, given a string, lookup the string as a variable name in the
618 * var cache. If the name is found, the var cache node corresponding to the
619 * var name is returned. If it is not found, a new entry is allocated
621 * \param I Points into the binary array where the string identifier begins
622 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
623 * \return The location on the var_cache corresponding the the string starting at I
625 static struct var_cache
*
626 parse_string (GLubyte
** inst
, struct var_cache
**vc_head
,
627 struct arb_program
*Program
, GLuint
* found
)
630 struct var_cache
*va
= NULL
;
633 *inst
+= _mesa_strlen ((char *) i
) + 1;
635 va
= var_cache_find (*vc_head
, i
);
643 var_cache_create (&va
);
646 var_cache_append (vc_head
, va
);
652 parse_string_without_adding (GLubyte
** inst
, struct arb_program
*Program
)
657 *inst
+= _mesa_strlen ((char *) i
) + 1;
663 * \return -1 if we parse '-', return 1 otherwise
666 parse_sign (GLubyte
** inst
)
668 /*return *(*inst)++ != '+'; */
674 else if (**inst
== '+') {
683 * parses and returns signed integer
686 parse_integer (GLubyte
** inst
, struct arb_program
*Program
)
691 /* check if *inst points to '+' or '-'
692 * if yes, grab the sign and increment *inst
694 sign
= parse_sign (inst
);
696 /* now check if *inst points to 0
697 * if yes, increment the *inst and return the default value
704 /* parse the integer as you normally would do it */
705 value
= _mesa_atoi (parse_string_without_adding (inst
, Program
));
707 /* now, after terminating 0 there is a position
708 * to parse it - parse_position()
710 Program
->Position
= parse_position (inst
);
716 Accumulate this string of digits, and return them as
717 a large integer represented in floating point (for range).
718 If scale is not NULL, also accumulates a power-of-ten
719 integer scale factor that represents the number of digits
723 parse_float_string(GLubyte
** inst
, struct arb_program
*Program
, GLdouble
*scale
)
725 GLdouble value
= 0.0;
726 GLdouble oscale
= 1.0;
728 if (**inst
== 0) { /* this string of digits is empty-- do nothing */
731 else { /* nonempty string-- parse out the digits */
732 while (**inst
>= '0' && **inst
<= '9') {
733 GLubyte digit
= *((*inst
)++);
734 value
= value
* 10.0 + (GLint
) (digit
- '0');
737 assert(**inst
== 0); /* integer string should end with 0 */
738 (*inst
)++; /* skip over terminating 0 */
739 Program
->Position
= parse_position(inst
); /* skip position (from integer) */
747 Parse an unsigned floating-point number from this stream of tokenized
748 characters. Example floating-point formats supported:
756 parse_float (GLubyte
** inst
, struct arb_program
*Program
)
759 GLdouble whole
, fraction
, fracScale
= 1.0;
761 whole
= parse_float_string(inst
, Program
, 0);
762 fraction
= parse_float_string(inst
, Program
, &fracScale
);
764 /* Parse signed exponent */
765 exponent
= parse_integer(inst
, Program
); /* This is the exponent */
767 /* Assemble parts of floating-point number: */
768 return (GLfloat
) ((whole
+ fraction
/ fracScale
) *
769 _mesa_pow(10.0, (GLfloat
) exponent
));
776 parse_signed_float (GLubyte
** inst
, struct arb_program
*Program
)
778 GLint sign
= parse_sign (inst
);
779 GLfloat value
= parse_float (inst
, Program
);
784 * This picks out a constant value from the parsed array. The constant vector is r
785 * returned in the *values array, which should be of length 4.
787 * \param values - The 4 component vector with the constant value in it
790 parse_constant (GLubyte
** inst
, GLfloat
*values
, struct arb_program
*Program
,
793 GLuint components
, i
;
796 switch (*(*inst
)++) {
797 case CONSTANT_SCALAR
:
798 if (use
== GL_TRUE
) {
801 values
[2] = values
[3] = parse_float (inst
, Program
);
806 values
[2] = values
[3] = parse_signed_float (inst
, Program
);
810 case CONSTANT_VECTOR
:
811 values
[0] = values
[1] = values
[2] = 0;
813 components
= *(*inst
)++;
814 for (i
= 0; i
< components
; i
++) {
815 values
[i
] = parse_signed_float (inst
, Program
);
822 * \param offset The offset from the address register that we should
825 * \return 0 on sucess, 1 on error
828 parse_relative_offset (GLcontext
*ctx
, GLubyte
**inst
, struct arb_program
*Program
,
831 *offset
= parse_integer(inst
, Program
);
836 * \param color 0 if color type is primary, 1 if color type is secondary
837 * \return 0 on sucess, 1 on error
840 parse_color_type (GLcontext
* ctx
, GLubyte
** inst
, struct arb_program
*Program
,
843 (void) ctx
; (void) Program
;
844 *color
= *(*inst
)++ != COLOR_PRIMARY
;
849 * Get an integer corresponding to a generic vertex attribute.
851 * \return 0 on sucess, 1 on error
854 parse_generic_attrib_num(GLcontext
*ctx
, GLubyte
** inst
,
855 struct arb_program
*Program
, GLuint
*attrib
)
857 GLint i
= parse_integer(inst
, Program
);
859 if ((i
< 0) || (i
>= MAX_VERTEX_PROGRAM_ATTRIBS
))
861 _mesa_set_program_error (ctx
, Program
->Position
,
862 "Invalid generic vertex attribute index");
863 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid generic vertex attribute index");
868 *attrib
= (GLuint
) i
;
875 * \param color The index of the color buffer to write into
876 * \return 0 on sucess, 1 on error
879 parse_output_color_num (GLcontext
* ctx
, GLubyte
** inst
,
880 struct arb_program
*Program
, GLuint
* color
)
882 GLint i
= parse_integer (inst
, Program
);
884 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxDrawBuffers
)) {
885 _mesa_set_program_error (ctx
, Program
->Position
,
886 "Invalid draw buffer index");
887 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid draw buffer index");
897 * \param coord The texture unit index
898 * \return 0 on sucess, 1 on error
901 parse_texcoord_num (GLcontext
* ctx
, GLubyte
** inst
,
902 struct arb_program
*Program
, GLuint
* coord
)
904 GLint i
= parse_integer (inst
, Program
);
906 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxTextureUnits
)) {
907 _mesa_set_program_error (ctx
, Program
->Position
,
908 "Invalid texture unit index");
909 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid texture unit index");
918 * \param coord The weight index
919 * \return 0 on sucess, 1 on error
922 parse_weight_num (GLcontext
* ctx
, GLubyte
** inst
, struct arb_program
*Program
,
925 *coord
= parse_integer (inst
, Program
);
927 if ((*coord
< 0) || (*coord
>= 1)) {
928 _mesa_set_program_error (ctx
, Program
->Position
,
929 "Invalid weight index");
930 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid weight index");
938 * \param coord The clip plane index
939 * \return 0 on sucess, 1 on error
942 parse_clipplane_num (GLcontext
* ctx
, GLubyte
** inst
,
943 struct arb_program
*Program
, GLint
* coord
)
945 *coord
= parse_integer (inst
, Program
);
947 if ((*coord
< 0) || (*coord
>= (GLint
) ctx
->Const
.MaxClipPlanes
)) {
948 _mesa_set_program_error (ctx
, Program
->Position
,
949 "Invalid clip plane index");
950 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid clip plane index");
959 * \return 0 on front face, 1 on back face
962 parse_face_type (GLubyte
** inst
)
964 switch (*(*inst
)++) {
976 * Given a matrix and a modifier token on the binary array, return tokens
977 * that _mesa_fetch_state() [program.c] can understand.
979 * \param matrix - the matrix we are talking about
980 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
981 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
982 * \return 0 on sucess, 1 on failure
985 parse_matrix (GLcontext
* ctx
, GLubyte
** inst
, struct arb_program
*Program
,
986 GLint
* matrix
, GLint
* matrix_idx
, GLint
* matrix_modifier
)
988 GLubyte mat
= *(*inst
)++;
993 case MATRIX_MODELVIEW
:
994 *matrix
= STATE_MODELVIEW
;
995 *matrix_idx
= parse_integer (inst
, Program
);
996 if (*matrix_idx
> 0) {
997 _mesa_set_program_error (ctx
, Program
->Position
,
998 "ARB_vertex_blend not supported\n");
999 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1000 "ARB_vertex_blend not supported\n");
1005 case MATRIX_PROJECTION
:
1006 *matrix
= STATE_PROJECTION
;
1010 *matrix
= STATE_MVP
;
1013 case MATRIX_TEXTURE
:
1014 *matrix
= STATE_TEXTURE
;
1015 *matrix_idx
= parse_integer (inst
, Program
);
1016 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxTextureUnits
) {
1017 _mesa_set_program_error (ctx
, Program
->Position
,
1018 "Invalid Texture Unit");
1019 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1020 "Invalid Texture Unit: %d", *matrix_idx
);
1025 /* This is not currently supported (ARB_matrix_palette) */
1026 case MATRIX_PALETTE
:
1027 *matrix_idx
= parse_integer (inst
, Program
);
1028 _mesa_set_program_error (ctx
, Program
->Position
,
1029 "ARB_matrix_palette not supported\n");
1030 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1031 "ARB_matrix_palette not supported\n");
1035 case MATRIX_PROGRAM
:
1036 *matrix
= STATE_PROGRAM
;
1037 *matrix_idx
= parse_integer (inst
, Program
);
1038 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxProgramMatrices
) {
1039 _mesa_set_program_error (ctx
, Program
->Position
,
1040 "Invalid Program Matrix");
1041 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1042 "Invalid Program Matrix: %d", *matrix_idx
);
1048 switch (*(*inst
)++) {
1049 case MATRIX_MODIFIER_IDENTITY
:
1050 *matrix_modifier
= 0;
1052 case MATRIX_MODIFIER_INVERSE
:
1053 *matrix_modifier
= STATE_MATRIX_INVERSE
;
1055 case MATRIX_MODIFIER_TRANSPOSE
:
1056 *matrix_modifier
= STATE_MATRIX_TRANSPOSE
;
1058 case MATRIX_MODIFIER_INVTRANS
:
1059 *matrix_modifier
= STATE_MATRIX_INVTRANS
;
1068 * This parses a state string (rather, the binary version of it) into
1069 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1071 * \param inst - the start in the binary arry to start working from
1072 * \param state_tokens - the storage for the 6-token state description
1073 * \return - 0 on sucess, 1 on error
1076 parse_state_single_item (GLcontext
* ctx
, GLubyte
** inst
,
1077 struct arb_program
*Program
, GLint
* state_tokens
)
1079 switch (*(*inst
)++) {
1080 case STATE_MATERIAL_PARSER
:
1081 state_tokens
[0] = STATE_MATERIAL
;
1082 state_tokens
[1] = parse_face_type (inst
);
1083 switch (*(*inst
)++) {
1084 case MATERIAL_AMBIENT
:
1085 state_tokens
[2] = STATE_AMBIENT
;
1087 case MATERIAL_DIFFUSE
:
1088 state_tokens
[2] = STATE_DIFFUSE
;
1090 case MATERIAL_SPECULAR
:
1091 state_tokens
[2] = STATE_SPECULAR
;
1093 case MATERIAL_EMISSION
:
1094 state_tokens
[2] = STATE_EMISSION
;
1096 case MATERIAL_SHININESS
:
1097 state_tokens
[2] = STATE_SHININESS
;
1102 case STATE_LIGHT_PARSER
:
1103 state_tokens
[0] = STATE_LIGHT
;
1104 state_tokens
[1] = parse_integer (inst
, Program
);
1106 /* Check the value of state_tokens[1] against the # of lights */
1107 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1108 _mesa_set_program_error (ctx
, Program
->Position
,
1109 "Invalid Light Number");
1110 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1111 "Invalid Light Number: %d", state_tokens
[1]);
1115 switch (*(*inst
)++) {
1117 state_tokens
[2] = STATE_AMBIENT
;
1120 state_tokens
[2] = STATE_DIFFUSE
;
1122 case LIGHT_SPECULAR
:
1123 state_tokens
[2] = STATE_SPECULAR
;
1125 case LIGHT_POSITION
:
1126 state_tokens
[2] = STATE_POSITION
;
1128 case LIGHT_ATTENUATION
:
1129 state_tokens
[2] = STATE_ATTENUATION
;
1132 state_tokens
[2] = STATE_HALF
;
1134 case LIGHT_SPOT_DIRECTION
:
1135 state_tokens
[2] = STATE_SPOT_DIRECTION
;
1140 case STATE_LIGHT_MODEL
:
1141 switch (*(*inst
)++) {
1142 case LIGHT_MODEL_AMBIENT
:
1143 state_tokens
[0] = STATE_LIGHTMODEL_AMBIENT
;
1145 case LIGHT_MODEL_SCENECOLOR
:
1146 state_tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
1147 state_tokens
[1] = parse_face_type (inst
);
1152 case STATE_LIGHT_PROD
:
1153 state_tokens
[0] = STATE_LIGHTPROD
;
1154 state_tokens
[1] = parse_integer (inst
, Program
);
1156 /* Check the value of state_tokens[1] against the # of lights */
1157 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1158 _mesa_set_program_error (ctx
, Program
->Position
,
1159 "Invalid Light Number");
1160 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1161 "Invalid Light Number: %d", state_tokens
[1]);
1165 state_tokens
[2] = parse_face_type (inst
);
1166 switch (*(*inst
)++) {
1167 case LIGHT_PROD_AMBIENT
:
1168 state_tokens
[3] = STATE_AMBIENT
;
1170 case LIGHT_PROD_DIFFUSE
:
1171 state_tokens
[3] = STATE_DIFFUSE
;
1173 case LIGHT_PROD_SPECULAR
:
1174 state_tokens
[3] = STATE_SPECULAR
;
1181 switch (*(*inst
)++) {
1183 state_tokens
[0] = STATE_FOG_COLOR
;
1186 state_tokens
[0] = STATE_FOG_PARAMS
;
1192 state_tokens
[1] = parse_integer (inst
, Program
);
1193 switch (*(*inst
)++) {
1195 state_tokens
[0] = STATE_TEXENV_COLOR
;
1204 state_tokens
[0] = STATE_TEXGEN
;
1205 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1207 if (parse_texcoord_num (ctx
, inst
, Program
, &coord
))
1209 state_tokens
[1] = coord
;
1214 /* 0 - s, 1 - t, 2 - r, 3 - q */
1217 if (type
== TEX_GEN_EYE
) {
1220 state_tokens
[2] = STATE_TEXGEN_EYE_S
;
1223 state_tokens
[2] = STATE_TEXGEN_EYE_T
;
1226 state_tokens
[2] = STATE_TEXGEN_EYE_R
;
1229 state_tokens
[2] = STATE_TEXGEN_EYE_Q
;
1236 state_tokens
[2] = STATE_TEXGEN_OBJECT_S
;
1239 state_tokens
[2] = STATE_TEXGEN_OBJECT_T
;
1242 state_tokens
[2] = STATE_TEXGEN_OBJECT_R
;
1245 state_tokens
[2] = STATE_TEXGEN_OBJECT_Q
;
1253 switch (*(*inst
)++) {
1255 state_tokens
[0] = STATE_DEPTH_RANGE
;
1260 case STATE_CLIP_PLANE
:
1261 state_tokens
[0] = STATE_CLIPPLANE
;
1262 state_tokens
[1] = parse_integer (inst
, Program
);
1263 if (parse_clipplane_num (ctx
, inst
, Program
, &state_tokens
[1]))
1268 switch (*(*inst
++)) {
1270 state_tokens
[0] = STATE_POINT_SIZE
;
1273 case POINT_ATTENUATION
:
1274 state_tokens
[0] = STATE_POINT_ATTENUATION
;
1279 /* XXX: I think this is the correct format for a matrix row */
1280 case STATE_MATRIX_ROWS
:
1281 state_tokens
[0] = STATE_MATRIX
;
1283 (ctx
, inst
, Program
, &state_tokens
[1], &state_tokens
[2],
1287 state_tokens
[3] = parse_integer (inst
, Program
); /* The first row to grab */
1289 if ((**inst
) != 0) { /* Either the last row, 0 */
1290 state_tokens
[4] = parse_integer (inst
, Program
);
1291 if (state_tokens
[4] < state_tokens
[3]) {
1292 _mesa_set_program_error (ctx
, Program
->Position
,
1293 "Second matrix index less than the first");
1294 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1295 "Second matrix index (%d) less than the first (%d)",
1296 state_tokens
[4], state_tokens
[3]);
1301 state_tokens
[4] = state_tokens
[3];
1311 * This parses a state string (rather, the binary version of it) into
1312 * a 6-token similar for the state fetching code in program.c
1314 * One might ask, why fetch these parameters into just like you fetch
1315 * state when they are already stored in other places?
1317 * Because of array offsets -> We can stick env/local parameters in the
1318 * middle of a parameter array and then index someplace into the array
1321 * One optimization might be to only do this for the cases where the
1322 * env/local parameters end up inside of an array, and leave the
1323 * single parameters (or arrays of pure env/local pareameters) in their
1324 * respective register files.
1326 * For ENV parameters, the format is:
1327 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1328 * state_tokens[1] = STATE_ENV
1329 * state_tokens[2] = the parameter index
1331 * for LOCAL parameters, the format is:
1332 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1333 * state_tokens[1] = STATE_LOCAL
1334 * state_tokens[2] = the parameter index
1336 * \param inst - the start in the binary arry to start working from
1337 * \param state_tokens - the storage for the 6-token state description
1338 * \return - 0 on sucess, 1 on failure
1341 parse_program_single_item (GLcontext
* ctx
, GLubyte
** inst
,
1342 struct arb_program
*Program
, GLint
* state_tokens
)
1344 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1345 state_tokens
[0] = STATE_FRAGMENT_PROGRAM
;
1347 state_tokens
[0] = STATE_VERTEX_PROGRAM
;
1350 switch (*(*inst
)++) {
1351 case PROGRAM_PARAM_ENV
:
1352 state_tokens
[1] = STATE_ENV
;
1353 state_tokens
[2] = parse_integer (inst
, Program
);
1355 /* Check state_tokens[2] against the number of ENV parameters available */
1356 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1357 (state_tokens
[2] >= (GLint
) ctx
->Const
.FragmentProgram
.MaxEnvParams
))
1359 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1360 (state_tokens
[2] >= (GLint
) ctx
->Const
.VertexProgram
.MaxEnvParams
))) {
1361 _mesa_set_program_error (ctx
, Program
->Position
,
1362 "Invalid Program Env Parameter");
1363 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1364 "Invalid Program Env Parameter: %d",
1371 case PROGRAM_PARAM_LOCAL
:
1372 state_tokens
[1] = STATE_LOCAL
;
1373 state_tokens
[2] = parse_integer (inst
, Program
);
1375 /* Check state_tokens[2] against the number of LOCAL parameters available */
1376 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1377 (state_tokens
[2] >= (GLint
) ctx
->Const
.FragmentProgram
.MaxLocalParams
))
1379 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1380 (state_tokens
[2] >= (GLint
) ctx
->Const
.VertexProgram
.MaxLocalParams
))) {
1381 _mesa_set_program_error (ctx
, Program
->Position
,
1382 "Invalid Program Local Parameter");
1383 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1384 "Invalid Program Local Parameter: %d",
1395 * For ARB_vertex_program, programs are not allowed to use both an explicit
1396 * vertex attribute and a generic vertex attribute corresponding to the same
1397 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1399 * This will walk our var_cache and make sure that nobody does anything fishy.
1401 * \return 0 on sucess, 1 on error
1404 generic_attrib_check(struct var_cache
*vc_head
)
1407 struct var_cache
*curr
;
1408 GLboolean explicitAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
],
1409 genericAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
];
1411 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1412 explicitAttrib
[a
] = GL_FALSE
;
1413 genericAttrib
[a
] = GL_FALSE
;
1418 if (curr
->type
== vt_attrib
) {
1419 if (curr
->attrib_is_generic
)
1420 genericAttrib
[ curr
->attrib_binding
] = GL_TRUE
;
1422 explicitAttrib
[ curr
->attrib_binding
] = GL_TRUE
;
1428 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1429 if ((explicitAttrib
[a
]) && (genericAttrib
[a
]))
1437 * This will handle the binding side of an ATTRIB var declaration
1439 * \param inputReg returns the input register index, one of the
1440 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
1441 * \return returns 0 on sucess, 1 on error
1444 parse_attrib_binding(GLcontext
* ctx
, GLubyte
** inst
,
1445 struct arb_program
*Program
,
1446 GLuint
*inputReg
, GLuint
*is_generic
)
1452 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1453 switch (*(*inst
)++) {
1454 case FRAGMENT_ATTRIB_COLOR
:
1457 err
= parse_color_type (ctx
, inst
, Program
, &coord
);
1458 *inputReg
= FRAG_ATTRIB_COL0
+ coord
;
1461 case FRAGMENT_ATTRIB_TEXCOORD
:
1464 err
= parse_texcoord_num (ctx
, inst
, Program
, &texcoord
);
1465 *inputReg
= FRAG_ATTRIB_TEX0
+ texcoord
;
1468 case FRAGMENT_ATTRIB_FOGCOORD
:
1469 *inputReg
= FRAG_ATTRIB_FOGC
;
1471 case FRAGMENT_ATTRIB_POSITION
:
1472 *inputReg
= FRAG_ATTRIB_WPOS
;
1480 switch (*(*inst
)++) {
1481 case VERTEX_ATTRIB_POSITION
:
1482 *inputReg
= VERT_ATTRIB_POS
;
1485 case VERTEX_ATTRIB_WEIGHT
:
1487 const char *msg
= "ARB_vertex_blend not supported";
1489 err
= parse_weight_num (ctx
, inst
, Program
, &weight
);
1490 *inputReg
= VERT_ATTRIB_WEIGHT
;
1491 _mesa_set_program_error(ctx
, Program
->Position
, msg
);
1492 _mesa_error(ctx
, GL_INVALID_OPERATION
, msg
);
1496 case VERTEX_ATTRIB_NORMAL
:
1497 *inputReg
= VERT_ATTRIB_NORMAL
;
1500 case VERTEX_ATTRIB_COLOR
:
1503 err
= parse_color_type (ctx
, inst
, Program
, &color
);
1505 *inputReg
= VERT_ATTRIB_COLOR1
;
1508 *inputReg
= VERT_ATTRIB_COLOR0
;
1513 case VERTEX_ATTRIB_FOGCOORD
:
1514 *inputReg
= VERT_ATTRIB_FOG
;
1517 case VERTEX_ATTRIB_TEXCOORD
:
1520 err
= parse_texcoord_num (ctx
, inst
, Program
, &unit
);
1521 *inputReg
= VERT_ATTRIB_TEX0
+ unit
;
1525 case VERTEX_ATTRIB_MATRIXINDEX
:
1526 /* Not supported at this time */
1528 const char *msg
= "ARB_palette_matrix not supported";
1529 parse_integer (inst
, Program
);
1530 _mesa_set_program_error (ctx
, Program
->Position
, msg
);
1531 _mesa_error (ctx
, GL_INVALID_OPERATION
, msg
);
1535 case VERTEX_ATTRIB_GENERIC
:
1538 if (!parse_generic_attrib_num(ctx
, inst
, Program
, &attrib
)) {
1551 /* Can this even happen? */
1553 const char *msg
= "Bad attribute binding";
1554 _mesa_set_program_error(ctx
, Program
->Position
, msg
);
1555 _mesa_error(ctx
, GL_INVALID_OPERATION
, msg
);
1558 Program
->Base
.InputsRead
|= (1 << *inputReg
);
1565 * This translates between a binary token for an output variable type
1566 * and the mesa token for the same thing.
1568 * \param inst The parsed tokens
1569 * \param outputReg Returned index/number of the output register,
1570 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
1573 parse_result_binding(GLcontext
*ctx
, GLubyte
**inst
,
1574 GLuint
*outputReg
, struct arb_program
*Program
)
1576 const GLubyte token
= *(*inst
)++;
1579 case FRAGMENT_RESULT_COLOR
:
1580 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1583 /* This gets result of the color buffer we're supposed to
1584 * draw into. This pertains to GL_ARB_draw_buffers.
1586 parse_output_color_num(ctx
, inst
, Program
, &out_color
);
1587 ASSERT(out_color
< MAX_DRAW_BUFFERS
);
1588 *outputReg
= FRAG_RESULT_COLR
;
1591 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1592 *outputReg
= VERT_RESULT_HPOS
;
1596 case FRAGMENT_RESULT_DEPTH
:
1597 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1598 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
1599 *outputReg
= FRAG_RESULT_DEPR
;
1602 /* for vtx programs, this is VERTEX_RESULT_COLOR */
1604 GLuint face_type
= parse_face_type(inst
);
1605 GLint err
= parse_color_type(ctx
, inst
, Program
, &color_type
);
1612 *outputReg
= VERT_RESULT_BFC1
; /* secondary color */
1615 *outputReg
= VERT_RESULT_BFC0
; /* primary color */
1621 *outputReg
= VERT_RESULT_COL1
; /* secondary color */
1625 *outputReg
= VERT_RESULT_COL0
; /* primary color */
1631 case VERTEX_RESULT_FOGCOORD
:
1632 *outputReg
= VERT_RESULT_FOGC
;
1635 case VERTEX_RESULT_POINTSIZE
:
1636 *outputReg
= VERT_RESULT_PSIZ
;
1639 case VERTEX_RESULT_TEXCOORD
:
1642 if (parse_texcoord_num (ctx
, inst
, Program
, &unit
))
1644 *outputReg
= VERT_RESULT_TEX0
+ unit
;
1649 Program
->Base
.OutputsWritten
|= (1 << *outputReg
);
1656 * This handles the declaration of ATTRIB variables
1659 * parse_vert_attrib_binding(), or something like that
1661 * \return 0 on sucess, 1 on error
1664 parse_attrib (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
1665 struct arb_program
*Program
)
1669 struct var_cache
*attrib_var
;
1671 attrib_var
= parse_string (inst
, vc_head
, Program
, &found
);
1672 Program
->Position
= parse_position (inst
);
1674 error_msg
= (char *)
1675 _mesa_malloc (_mesa_strlen ((char *) attrib_var
->name
) + 40);
1676 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
1679 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
1680 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
1682 _mesa_free (error_msg
);
1686 attrib_var
->type
= vt_attrib
;
1688 if (parse_attrib_binding(ctx
, inst
, Program
, &attrib_var
->attrib_binding
,
1689 &attrib_var
->attrib_is_generic
))
1692 if (generic_attrib_check(*vc_head
)) {
1693 _mesa_set_program_error(ctx
, Program
->Position
,
1694 "Cannot use both a generic vertex attribute "
1695 "and a specific attribute of the same type");
1696 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1697 "Cannot use both a generic vertex attribute and a specific "
1698 "attribute of the same type");
1702 Program
->Base
.NumAttributes
++;
1707 * \param use -- TRUE if we're called when declaring implicit parameters,
1708 * FALSE if we're declaraing variables. This has to do with
1709 * if we get a signed or unsigned float for scalar constants
1712 parse_param_elements (GLcontext
* ctx
, GLubyte
** inst
,
1713 struct var_cache
*param_var
,
1714 struct arb_program
*Program
, GLboolean use
)
1718 GLint state_tokens
[6];
1719 GLfloat const_values
[4];
1721 switch (*(*inst
)++) {
1722 case PARAM_STATE_ELEMENT
:
1723 if (parse_state_single_item (ctx
, inst
, Program
, state_tokens
))
1726 /* If we adding STATE_MATRIX that has multiple rows, we need to
1727 * unroll it and call _mesa_add_state_reference() for each row
1729 if ((state_tokens
[0] == STATE_MATRIX
)
1730 && (state_tokens
[3] != state_tokens
[4])) {
1732 GLint first_row
= state_tokens
[3];
1733 GLint last_row
= state_tokens
[4];
1735 for (row
= first_row
; row
<= last_row
; row
++) {
1736 state_tokens
[3] = state_tokens
[4] = row
;
1738 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1740 if (param_var
->param_binding_begin
== ~0U)
1741 param_var
->param_binding_begin
= idx
;
1742 param_var
->param_binding_length
++;
1743 Program
->Base
.NumParameters
++;
1747 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1749 if (param_var
->param_binding_begin
== ~0U)
1750 param_var
->param_binding_begin
= idx
;
1751 param_var
->param_binding_length
++;
1752 Program
->Base
.NumParameters
++;
1756 case PARAM_PROGRAM_ELEMENT
:
1757 if (parse_program_single_item (ctx
, inst
, Program
, state_tokens
))
1759 idx
= _mesa_add_state_reference (Program
->Base
.Parameters
, state_tokens
);
1760 if (param_var
->param_binding_begin
== ~0U)
1761 param_var
->param_binding_begin
= idx
;
1762 param_var
->param_binding_length
++;
1763 Program
->Base
.NumParameters
++;
1765 /* Check if there is more: 0 -> we're done, else its an integer */
1767 GLuint out_of_range
, new_idx
;
1768 GLuint start_idx
= state_tokens
[2] + 1;
1769 GLuint end_idx
= parse_integer (inst
, Program
);
1772 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1773 if (((state_tokens
[1] == STATE_ENV
)
1774 && (end_idx
>= ctx
->Const
.FragmentProgram
.MaxEnvParams
))
1775 || ((state_tokens
[1] == STATE_LOCAL
)
1777 ctx
->Const
.FragmentProgram
.MaxLocalParams
)))
1781 if (((state_tokens
[1] == STATE_ENV
)
1782 && (end_idx
>= ctx
->Const
.VertexProgram
.MaxEnvParams
))
1783 || ((state_tokens
[1] == STATE_LOCAL
)
1785 ctx
->Const
.VertexProgram
.MaxLocalParams
)))
1789 _mesa_set_program_error (ctx
, Program
->Position
,
1790 "Invalid Program Parameter");
1791 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1792 "Invalid Program Parameter: %d", end_idx
);
1796 for (new_idx
= start_idx
; new_idx
<= end_idx
; new_idx
++) {
1797 state_tokens
[2] = new_idx
;
1798 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1800 param_var
->param_binding_length
++;
1801 Program
->Base
.NumParameters
++;
1809 case PARAM_CONSTANT
:
1810 parse_constant (inst
, const_values
, Program
, use
);
1811 idx
= _mesa_add_named_constant(Program
->Base
.Parameters
,
1812 (char *) param_var
->name
,
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 _mesa_set_program_error(ctx
, Program
->Position
,
1822 "Unexpected token in parse_param_elements()");
1823 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1824 "Unexpected token in parse_param_elements()");
1828 /* Make sure we haven't blown past our parameter limits */
1829 if (((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1830 (Program
->Base
.NumParameters
>=
1831 ctx
->Const
.VertexProgram
.MaxLocalParams
))
1832 || ((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1833 && (Program
->Base
.NumParameters
>=
1834 ctx
->Const
.FragmentProgram
.MaxLocalParams
))) {
1835 _mesa_set_program_error (ctx
, Program
->Position
,
1836 "Too many parameter variables");
1837 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Too many parameter variables");
1846 * This picks out PARAM program parameter bindings.
1848 * XXX: This needs to be stressed & tested
1850 * \return 0 on sucess, 1 on error
1853 parse_param (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
1854 struct arb_program
*Program
)
1857 GLint specified_length
;
1858 struct var_cache
*param_var
;
1861 param_var
= parse_string (inst
, vc_head
, Program
, &found
);
1862 Program
->Position
= parse_position (inst
);
1865 char *error_msg
= (char *)
1866 _mesa_malloc (_mesa_strlen ((char *) param_var
->name
) + 40);
1867 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
1870 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
1871 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
1873 _mesa_free (error_msg
);
1877 specified_length
= parse_integer (inst
, Program
);
1879 if (specified_length
< 0) {
1880 _mesa_set_program_error (ctx
, Program
->Position
,
1881 "Negative parameter array length");
1882 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1883 "Negative parameter array length: %d", specified_length
);
1887 param_var
->type
= vt_param
;
1888 param_var
->param_binding_length
= 0;
1890 /* Right now, everything is shoved into the main state register file.
1892 * In the future, it would be nice to leave things ENV/LOCAL params
1893 * in their respective register files, if possible
1895 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1898 * * - add each guy to the parameter list
1899 * * - increment the param_var->param_binding_len
1900 * * - store the param_var->param_binding_begin for the first one
1901 * * - compare the actual len to the specified len at the end
1903 while (**inst
!= PARAM_NULL
) {
1904 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_FALSE
))
1908 /* Test array length here! */
1909 if (specified_length
) {
1910 if (specified_length
!= (int)param_var
->param_binding_length
) {
1912 = "Declared parameter array length does not match parameter list";
1913 _mesa_set_program_error(ctx
, Program
->Position
, msg
);
1914 _mesa_error(ctx
, GL_INVALID_OPERATION
, msg
);
1927 parse_param_use (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
1928 struct arb_program
*Program
, struct var_cache
**new_var
)
1930 struct var_cache
*param_var
;
1932 /* First, insert a dummy entry into the var_cache */
1933 var_cache_create (¶m_var
);
1934 param_var
->name
= (GLubyte
*) _mesa_strdup (" ");
1935 param_var
->type
= vt_param
;
1937 param_var
->param_binding_length
= 0;
1938 /* Don't fill in binding_begin; We use the default value of -1
1939 * to tell if its already initialized, elsewhere.
1941 * param_var->param_binding_begin = 0;
1943 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1945 var_cache_append (vc_head
, param_var
);
1947 /* Then fill it with juicy parameter goodness */
1948 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_TRUE
))
1951 *new_var
= param_var
;
1958 * This handles the declaration of TEMP variables
1960 * \return 0 on sucess, 1 on error
1963 parse_temp (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
1964 struct arb_program
*Program
)
1967 struct var_cache
*temp_var
;
1969 while (**inst
!= 0) {
1970 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
1971 Program
->Position
= parse_position (inst
);
1973 char *error_msg
= (char *)
1974 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
1975 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
1978 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
1979 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
1981 _mesa_free (error_msg
);
1985 temp_var
->type
= vt_temp
;
1987 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1988 (Program
->Base
.NumTemporaries
>=
1989 ctx
->Const
.FragmentProgram
.MaxTemps
))
1990 || ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
1991 && (Program
->Base
.NumTemporaries
>=
1992 ctx
->Const
.VertexProgram
.MaxTemps
))) {
1993 _mesa_set_program_error (ctx
, Program
->Position
,
1994 "Too many TEMP variables declared");
1995 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1996 "Too many TEMP variables declared");
2000 temp_var
->temp_binding
= Program
->Base
.NumTemporaries
;
2001 Program
->Base
.NumTemporaries
++;
2009 * This handles variables of the OUTPUT variety
2011 * \return 0 on sucess, 1 on error
2014 parse_output (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2015 struct arb_program
*Program
)
2018 struct var_cache
*output_var
;
2021 output_var
= parse_string (inst
, vc_head
, Program
, &found
);
2022 Program
->Position
= parse_position (inst
);
2024 char *error_msg
= (char *)
2025 _mesa_malloc (_mesa_strlen ((char *) output_var
->name
) + 40);
2026 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
2029 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
2030 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
2032 _mesa_free (error_msg
);
2036 output_var
->type
= vt_output
;
2038 err
= parse_result_binding(ctx
, inst
, &output_var
->output_binding
, Program
);
2043 * This handles variables of the ALIAS kind
2045 * \return 0 on sucess, 1 on error
2048 parse_alias (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2049 struct arb_program
*Program
)
2052 struct var_cache
*temp_var
;
2054 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2055 Program
->Position
= parse_position (inst
);
2058 char *error_msg
= (char *)
2059 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2060 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
2063 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
2064 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
2066 _mesa_free (error_msg
);
2070 temp_var
->type
= vt_alias
;
2071 temp_var
->alias_binding
= parse_string (inst
, vc_head
, Program
, &found
);
2072 Program
->Position
= parse_position (inst
);
2076 char *error_msg
= (char *)
2077 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2078 _mesa_sprintf (error_msg
, "Alias value %s is not defined",
2079 temp_var
->alias_binding
->name
);
2081 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
2082 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
2084 _mesa_free (error_msg
);
2092 * This handles variables of the ADDRESS kind
2094 * \return 0 on sucess, 1 on error
2097 parse_address (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2098 struct arb_program
*Program
)
2101 struct var_cache
*temp_var
;
2103 while (**inst
!= 0) {
2104 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2105 Program
->Position
= parse_position (inst
);
2107 char *error_msg
= (char *)
2108 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2109 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
2112 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
2113 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
2115 _mesa_free (error_msg
);
2119 temp_var
->type
= vt_address
;
2121 if (Program
->Base
.NumAddressRegs
>=
2122 ctx
->Const
.VertexProgram
.MaxAddressRegs
) {
2123 const char *msg
= "Too many ADDRESS variables declared";
2124 _mesa_set_program_error(ctx
, Program
->Position
, msg
);
2126 _mesa_error(ctx
, GL_INVALID_OPERATION
, msg
);
2130 temp_var
->address_binding
= Program
->Base
.NumAddressRegs
;
2131 Program
->Base
.NumAddressRegs
++;
2139 * Parse a program declaration
2141 * \return 0 on sucess, 1 on error
2144 parse_declaration (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2145 struct arb_program
*Program
)
2149 switch (*(*inst
)++) {
2151 err
= parse_address (ctx
, inst
, vc_head
, Program
);
2155 err
= parse_alias (ctx
, inst
, vc_head
, Program
);
2159 err
= parse_attrib (ctx
, inst
, vc_head
, Program
);
2163 err
= parse_output (ctx
, inst
, vc_head
, Program
);
2167 err
= parse_param (ctx
, inst
, vc_head
, Program
);
2171 err
= parse_temp (ctx
, inst
, vc_head
, Program
);
2179 * Handle the parsing out of a masked destination register, either for a
2180 * vertex or fragment program.
2182 * If we are a vertex program, make sure we don't write to
2183 * result.position if we have specified that the program is
2184 * position invariant
2186 * \param File - The register file we write to
2187 * \param Index - The register index we write to
2188 * \param WriteMask - The mask controlling which components we write (1->write)
2190 * \return 0 on sucess, 1 on error
2193 parse_masked_dst_reg (GLcontext
* ctx
, GLubyte
** inst
,
2194 struct var_cache
**vc_head
, struct arb_program
*Program
,
2195 enum register_file
*File
, GLuint
*Index
, GLint
*WriteMask
)
2198 struct var_cache
*dst
;
2200 /* We either have a result register specified, or a
2201 * variable that may or may not be writable
2203 switch (*(*inst
)++) {
2204 case REGISTER_RESULT
:
2205 if (parse_result_binding(ctx
, inst
, Index
, Program
))
2207 *File
= PROGRAM_OUTPUT
;
2210 case REGISTER_ESTABLISHED_NAME
:
2211 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2212 Program
->Position
= parse_position (inst
);
2214 /* If the name has never been added to our symbol table, we're hosed */
2216 _mesa_set_program_error (ctx
, Program
->Position
,
2217 "0: Undefined variable");
2218 _mesa_error (ctx
, GL_INVALID_OPERATION
, "0: Undefined variable: %s",
2223 switch (dst
->type
) {
2225 *File
= PROGRAM_OUTPUT
;
2226 *Index
= dst
->output_binding
;
2230 *File
= PROGRAM_TEMPORARY
;
2231 *Index
= dst
->temp_binding
;
2234 /* If the var type is not vt_output or vt_temp, no go */
2236 _mesa_set_program_error (ctx
, Program
->Position
,
2237 "Destination register is read only");
2238 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2239 "Destination register is read only: %s",
2246 _mesa_set_program_error (ctx
, Program
->Position
,
2247 "Unexpected opcode in parse_masked_dst_reg()");
2248 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2249 "Unexpected opcode in parse_masked_dst_reg()");
2254 /* Position invariance test */
2255 /* This test is done now in syntax portion - when position invariance OPTION
2256 is specified, "result.position" rule is disabled so there is no way
2257 to write the position
2259 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2261 _mesa_set_program_error (ctx, Program->Position,
2262 "Vertex program specified position invariance and wrote vertex position");
2263 _mesa_error (ctx, GL_INVALID_OPERATION,
2264 "Vertex program specified position invariance and wrote vertex position");
2267 /* And then the mask.
2273 * ==> Need to reverse the order of bits for this!
2275 tmp
= (GLint
) *(*inst
)++;
2276 *WriteMask
= (((tmp
>>3) & 0x1) |
2286 * Handle the parsing of a address register
2288 * \param Index - The register index we write to
2290 * \return 0 on sucess, 1 on error
2293 parse_address_reg (GLcontext
* ctx
, GLubyte
** inst
,
2294 struct var_cache
**vc_head
,
2295 struct arb_program
*Program
, GLint
* Index
)
2297 struct var_cache
*dst
;
2300 *Index
= 0; /* XXX */
2302 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2303 Program
->Position
= parse_position (inst
);
2305 /* If the name has never been added to our symbol table, we're hosed */
2307 _mesa_set_program_error (ctx
, Program
->Position
, "Undefined variable");
2308 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Undefined variable: %s",
2313 if (dst
->type
!= vt_address
) {
2314 _mesa_set_program_error (ctx
, Program
->Position
,
2315 "Variable is not of type ADDRESS");
2316 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2317 "Variable: %s is not of type ADDRESS", dst
->name
);
2326 * Handle the parsing out of a masked address register
2328 * \param Index - The register index we write to
2329 * \param WriteMask - The mask controlling which components we write (1->write)
2331 * \return 0 on sucess, 1 on error
2334 parse_masked_address_reg (GLcontext
* ctx
, GLubyte
** inst
,
2335 struct var_cache
**vc_head
,
2336 struct arb_program
*Program
, GLint
* Index
,
2337 GLboolean
* WriteMask
)
2339 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, Index
))
2342 /* This should be 0x8 */
2345 /* Writemask of .x is implied */
2347 WriteMask
[1] = WriteMask
[2] = WriteMask
[3] = 0;
2354 * Parse out a swizzle mask.
2356 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
2358 * The len parameter allows us to grab 4 components for a vector
2359 * swizzle, or just 1 component for a scalar src register selection
2362 parse_swizzle_mask(GLubyte
** inst
, GLubyte
*swizzle
, GLint len
)
2366 for (i
= 0; i
< 4; i
++)
2369 for (i
= 0; i
< len
; i
++) {
2370 switch (*(*inst
)++) {
2372 swizzle
[i
] = SWIZZLE_X
;
2375 swizzle
[i
] = SWIZZLE_Y
;
2378 swizzle
[i
] = SWIZZLE_Z
;
2381 swizzle
[i
] = SWIZZLE_W
;
2384 _mesa_problem(NULL
, "bad component in parse_swizzle_mask()");
2392 * Parse an extended swizzle mask which is a sequence of
2393 * four x/y/z/w/0/1 tokens.
2394 * \return swizzle four swizzle values
2395 * \return negateMask four element bitfield
2398 parse_extended_swizzle_mask(GLubyte
**inst
, GLubyte swizzle
[4],
2399 GLubyte
*negateMask
)
2404 for (i
= 0; i
< 4; i
++) {
2406 if (parse_sign(inst
) == -1)
2407 *negateMask
|= (1 << i
);
2413 swizzle
[i
] = SWIZZLE_ZERO
;
2416 swizzle
[i
] = SWIZZLE_ONE
;
2419 swizzle
[i
] = SWIZZLE_X
;
2422 swizzle
[i
] = SWIZZLE_Y
;
2425 swizzle
[i
] = SWIZZLE_Z
;
2428 swizzle
[i
] = SWIZZLE_W
;
2431 _mesa_problem(NULL
, "bad case in parse_extended_swizzle_mask()");
2439 parse_src_reg (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2440 struct arb_program
*Program
,
2441 enum register_file
* File
, GLint
* Index
,
2442 GLboolean
*IsRelOffset
)
2444 struct var_cache
*src
;
2445 GLuint binding
, is_generic
, found
;
2450 /* And the binding for the src */
2451 switch (*(*inst
)++) {
2452 case REGISTER_ATTRIB
:
2453 if (parse_attrib_binding
2454 (ctx
, inst
, Program
, &binding
, &is_generic
))
2456 *File
= PROGRAM_INPUT
;
2459 /* We need to insert a dummy variable into the var_cache so we can
2460 * catch generic vertex attrib aliasing errors
2462 var_cache_create(&src
);
2463 src
->type
= vt_attrib
;
2464 src
->name
= (GLubyte
*)_mesa_strdup("Dummy Attrib Variable");
2465 src
->attrib_binding
= binding
;
2466 src
->attrib_is_generic
= is_generic
;
2467 var_cache_append(vc_head
, src
);
2468 if (generic_attrib_check(*vc_head
)) {
2469 const char *msg
= "Cannot use both a generic vertex attribute "
2470 "and a specific attribute of the same type";
2471 _mesa_set_program_error (ctx
, Program
->Position
, msg
);
2472 _mesa_error (ctx
, GL_INVALID_OPERATION
, msg
);
2477 case REGISTER_PARAM
:
2479 case PARAM_ARRAY_ELEMENT
:
2481 src
= parse_string (inst
, vc_head
, Program
, &found
);
2482 Program
->Position
= parse_position (inst
);
2485 _mesa_set_program_error (ctx
, Program
->Position
,
2486 "2: Undefined variable");
2487 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2488 "2: Undefined variable: %s", src
->name
);
2492 *File
= (enum register_file
) src
->param_binding_type
;
2494 switch (*(*inst
)++) {
2495 case ARRAY_INDEX_ABSOLUTE
:
2496 offset
= parse_integer (inst
, Program
);
2499 || (offset
>= (int)src
->param_binding_length
)) {
2500 _mesa_set_program_error (ctx
, Program
->Position
,
2501 "Index out of range");
2502 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2503 "Index %d out of range for %s", offset
,
2508 *Index
= src
->param_binding_begin
+ offset
;
2511 case ARRAY_INDEX_RELATIVE
:
2513 GLint addr_reg_idx
, rel_off
;
2515 /* First, grab the address regiseter */
2516 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &addr_reg_idx
))
2525 /* Then the relative offset */
2526 if (parse_relative_offset(ctx
, inst
, Program
, &rel_off
)) return 1;
2528 /* And store it properly */
2529 *Index
= src
->param_binding_begin
+ rel_off
;
2537 if (parse_param_use (ctx
, inst
, vc_head
, Program
, &src
))
2540 *File
= (enum register_file
) src
->param_binding_type
;
2541 *Index
= src
->param_binding_begin
;
2546 case REGISTER_ESTABLISHED_NAME
:
2547 src
= parse_string (inst
, vc_head
, Program
, &found
);
2548 Program
->Position
= parse_position (inst
);
2550 /* If the name has never been added to our symbol table, we're hosed */
2552 _mesa_set_program_error (ctx
, Program
->Position
,
2553 "3: Undefined variable");
2554 _mesa_error (ctx
, GL_INVALID_OPERATION
, "3: Undefined variable: %s",
2559 switch (src
->type
) {
2561 *File
= PROGRAM_INPUT
;
2562 *Index
= src
->attrib_binding
;
2565 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2567 *File
= (enum register_file
) src
->param_binding_type
;
2568 *Index
= src
->param_binding_begin
;
2572 *File
= PROGRAM_TEMPORARY
;
2573 *Index
= src
->temp_binding
;
2576 /* If the var type is vt_output no go */
2578 _mesa_set_program_error (ctx
, Program
->Position
,
2579 "destination register is read only");
2580 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2581 "destination register is read only: %s",
2588 _mesa_set_program_error (ctx
, Program
->Position
,
2589 "Unknown token in parse_src_reg");
2590 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2591 "Unknown token in parse_src_reg");
2599 * Parse fragment program vector source register.
2602 parse_fp_vector_src_reg(GLcontext
* ctx
, GLubyte
** inst
,
2603 struct var_cache
**vc_head
,
2604 struct arb_program
*program
,
2605 struct prog_src_register
*reg
)
2607 enum register_file file
;
2611 GLboolean isRelOffset
;
2614 negate
= (parse_sign (inst
) == -1) ? 0xf : 0x0;
2616 /* And the src reg */
2617 if (parse_src_reg(ctx
, inst
, vc_head
, program
, &file
, &index
, &isRelOffset
))
2620 /* finally, the swizzle */
2621 parse_swizzle_mask(inst
, swizzle
, 4);
2625 reg
->Abs
= 0; /* NV only */
2626 reg
->NegateAbs
= 0; /* NV only */
2627 reg
->NegateBase
= negate
;
2628 reg
->Swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
2634 parse_fp_dst_reg(GLcontext
* ctx
, GLubyte
** inst
,
2635 struct var_cache
**vc_head
, struct arb_program
*Program
,
2636 struct prog_dst_register
*reg
)
2640 enum register_file file
;
2642 if (parse_masked_dst_reg (ctx
, inst
, vc_head
, Program
, &file
, &idx
, &mask
))
2645 reg
->CondMask
= 0; /* NV only */
2646 reg
->CondSwizzle
= 0; /* NV only */
2649 reg
->WriteMask
= mask
;
2655 * Parse fragment program scalar src register.
2658 parse_fp_scalar_src_reg (GLcontext
* ctx
, GLubyte
** inst
,
2659 struct var_cache
**vc_head
,
2660 struct arb_program
*Program
,
2661 struct prog_src_register
*reg
)
2663 enum register_file File
;
2667 GLboolean IsRelOffset
;
2670 Negate
= (parse_sign (inst
) == -1) ? 0x1 : 0x0;
2672 /* And the src reg */
2673 if (parse_src_reg (ctx
, inst
, vc_head
, Program
, &File
, &Index
, &IsRelOffset
))
2676 /* finally, the swizzle */
2677 parse_swizzle_mask(inst
, Swizzle
, 1);
2681 reg
->Abs
= 0; /* NV only */
2682 reg
->NegateAbs
= 0; /* NV only */
2683 reg
->NegateBase
= Negate
;
2684 reg
->Swizzle
= (Swizzle
[0] << 0);
2691 * This is a big mother that handles getting opcodes into the instruction
2692 * and handling the src & dst registers for fragment program instructions
2695 parse_fp_instruction (GLcontext
* ctx
, GLubyte
** inst
,
2696 struct var_cache
**vc_head
, struct arb_program
*Program
,
2697 struct prog_instruction
*fp
)
2701 GLubyte instClass
, type
, code
;
2704 _mesa_init_instruction(fp
);
2706 /* Record the position in the program string for debugging */
2707 fp
->StringPos
= Program
->Position
;
2709 /* OP_ALU_INST or OP_TEX_INST */
2710 instClass
= *(*inst
)++;
2712 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2713 * OP_TEX_{SAMPLE, KIL}
2717 /* The actual opcode name */
2720 /* Increment the correct count */
2721 switch (instClass
) {
2723 Program
->NumAluInstructions
++;
2726 Program
->NumTexInstructions
++;
2734 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2736 fp
->Opcode
= OPCODE_ABS
;
2740 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2742 fp
->Opcode
= OPCODE_FLR
;
2746 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2748 fp
->Opcode
= OPCODE_FRC
;
2752 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2754 fp
->Opcode
= OPCODE_LIT
;
2758 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2760 fp
->Opcode
= OPCODE_MOV
;
2764 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2767 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2774 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2776 fp
->Opcode
= OPCODE_COS
;
2780 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2782 fp
->Opcode
= OPCODE_EX2
;
2786 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2788 fp
->Opcode
= OPCODE_LG2
;
2792 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2794 fp
->Opcode
= OPCODE_RCP
;
2798 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2800 fp
->Opcode
= OPCODE_RSQ
;
2804 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2806 fp
->Opcode
= OPCODE_SIN
;
2810 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2813 fp
->Opcode
= OPCODE_SCS
;
2817 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2820 if (parse_fp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2827 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2829 fp
->Opcode
= OPCODE_POW
;
2833 if (parse_fp_dst_reg(ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2836 for (a
= 0; a
< 2; a
++) {
2837 if (parse_fp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2846 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2848 fp
->Opcode
= OPCODE_ADD
;
2852 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2854 fp
->Opcode
= OPCODE_DP3
;
2858 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2860 fp
->Opcode
= OPCODE_DP4
;
2864 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2866 fp
->Opcode
= OPCODE_DPH
;
2870 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2872 fp
->Opcode
= OPCODE_DST
;
2876 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2878 fp
->Opcode
= OPCODE_MAX
;
2882 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2884 fp
->Opcode
= OPCODE_MIN
;
2888 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2890 fp
->Opcode
= OPCODE_MUL
;
2894 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2896 fp
->Opcode
= OPCODE_SGE
;
2900 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2902 fp
->Opcode
= OPCODE_SLT
;
2906 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2908 fp
->Opcode
= OPCODE_SUB
;
2912 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2914 fp
->Opcode
= OPCODE_XPD
;
2918 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2920 for (a
= 0; a
< 2; a
++) {
2921 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2929 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2931 fp
->Opcode
= OPCODE_CMP
;
2935 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2937 fp
->Opcode
= OPCODE_LRP
;
2941 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2943 fp
->Opcode
= OPCODE_MAD
;
2947 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2950 for (a
= 0; a
< 3; a
++) {
2951 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2959 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2961 fp
->Opcode
= OPCODE_SWZ
;
2964 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2970 enum register_file file
;
2973 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
, &rel
))
2975 parse_extended_swizzle_mask(inst
, swizzle
, &negateMask
);
2976 fp
->SrcReg
[0].File
= file
;
2977 fp
->SrcReg
[0].Index
= index
;
2978 fp
->SrcReg
[0].NegateBase
= negateMask
;
2979 fp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
2989 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2991 fp
->Opcode
= OPCODE_TEX
;
2995 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2997 fp
->Opcode
= OPCODE_TXP
;
3001 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3003 fp
->Opcode
= OPCODE_TXB
;
3007 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3010 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
3014 if (parse_texcoord_num (ctx
, inst
, Program
, &texcoord
))
3016 fp
->TexSrcUnit
= texcoord
;
3019 switch (*(*inst
)++) {
3021 fp
->TexSrcTarget
= TEXTURE_1D_INDEX
;
3024 fp
->TexSrcTarget
= TEXTURE_2D_INDEX
;
3027 fp
->TexSrcTarget
= TEXTURE_3D_INDEX
;
3029 case TEXTARGET_RECT
:
3030 fp
->TexSrcTarget
= TEXTURE_RECT_INDEX
;
3032 case TEXTARGET_CUBE
:
3033 fp
->TexSrcTarget
= TEXTURE_CUBE_INDEX
;
3035 case TEXTARGET_SHADOW1D
:
3036 case TEXTARGET_SHADOW2D
:
3037 case TEXTARGET_SHADOWRECT
:
3038 /* TODO ARB_fragment_program_shadow code */
3041 Program
->TexturesUsed
[texcoord
] |= (1<<fp
->TexSrcTarget
);
3045 Program
->UsesKill
= 1;
3046 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
3048 fp
->Opcode
= OPCODE_KIL
;
3056 parse_vp_dst_reg(GLcontext
* ctx
, GLubyte
** inst
,
3057 struct var_cache
**vc_head
, struct arb_program
*Program
,
3058 struct prog_dst_register
*reg
)
3062 enum register_file file
;
3064 if (parse_masked_dst_reg(ctx
, inst
, vc_head
, Program
, &file
, &idx
, &mask
))
3069 reg
->WriteMask
= mask
;
3074 * Handle the parsing out of a masked address register
3076 * \param Index - The register index we write to
3077 * \param WriteMask - The mask controlling which components we write (1->write)
3079 * \return 0 on sucess, 1 on error
3082 parse_vp_address_reg (GLcontext
* ctx
, GLubyte
** inst
,
3083 struct var_cache
**vc_head
,
3084 struct arb_program
*Program
,
3085 struct prog_dst_register
*reg
)
3089 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &idx
))
3092 /* This should be 0x8 */
3095 reg
->File
= PROGRAM_ADDRESS
;
3098 /* Writemask of .x is implied */
3099 reg
->WriteMask
= 0x1;
3104 * Parse vertex program vector source register.
3107 parse_vp_vector_src_reg(GLcontext
* ctx
, GLubyte
** inst
,
3108 struct var_cache
**vc_head
,
3109 struct arb_program
*program
,
3110 struct prog_src_register
*reg
)
3112 enum register_file file
;
3116 GLboolean isRelOffset
;
3119 negateMask
= (parse_sign (inst
) == -1) ? 0xf : 0x0;
3121 /* And the src reg */
3122 if (parse_src_reg (ctx
, inst
, vc_head
, program
, &file
, &index
, &isRelOffset
))
3125 /* finally, the swizzle */
3126 parse_swizzle_mask(inst
, swizzle
, 4);
3130 reg
->Swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1],
3131 swizzle
[2], swizzle
[3]);
3132 reg
->NegateBase
= negateMask
;
3133 reg
->RelAddr
= isRelOffset
;
3139 parse_vp_scalar_src_reg (GLcontext
* ctx
, GLubyte
** inst
,
3140 struct var_cache
**vc_head
,
3141 struct arb_program
*Program
,
3142 struct prog_src_register
*reg
)
3144 enum register_file File
;
3148 GLboolean IsRelOffset
;
3151 Negate
= (parse_sign (inst
) == -1) ? 0x1 : 0x0;
3153 /* And the src reg */
3154 if (parse_src_reg (ctx
, inst
, vc_head
, Program
, &File
, &Index
, &IsRelOffset
))
3157 /* finally, the swizzle */
3158 parse_swizzle_mask(inst
, Swizzle
, 1);
3162 reg
->Swizzle
= (Swizzle
[0] << 0);
3163 reg
->NegateBase
= Negate
;
3164 reg
->RelAddr
= IsRelOffset
;
3170 * This is a big mother that handles getting opcodes into the instruction
3171 * and handling the src & dst registers for vertex program instructions
3174 parse_vp_instruction (GLcontext
* ctx
, GLubyte
** inst
,
3175 struct var_cache
**vc_head
, struct arb_program
*Program
,
3176 struct prog_instruction
*vp
)
3181 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3184 /* The actual opcode name */
3187 _mesa_init_instruction(vp
);
3188 /* Record the position in the program string for debugging */
3189 vp
->StringPos
= Program
->Position
;
3194 vp
->Opcode
= OPCODE_ARL
;
3196 /* Remember to set SrcReg.RelAddr; */
3198 /* Get the masked address register [dst] */
3199 if (parse_vp_address_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3202 vp
->DstReg
.File
= PROGRAM_ADDRESS
;
3204 /* Get a scalar src register */
3205 if (parse_vp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3213 vp
->Opcode
= OPCODE_ABS
;
3216 vp
->Opcode
= OPCODE_FLR
;
3219 vp
->Opcode
= OPCODE_FRC
;
3222 vp
->Opcode
= OPCODE_LIT
;
3225 vp
->Opcode
= OPCODE_MOV
;
3229 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3232 if (parse_vp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3239 vp
->Opcode
= OPCODE_EX2
;
3242 vp
->Opcode
= OPCODE_EXP
;
3245 vp
->Opcode
= OPCODE_LG2
;
3248 vp
->Opcode
= OPCODE_LOG
;
3251 vp
->Opcode
= OPCODE_RCP
;
3254 vp
->Opcode
= OPCODE_RSQ
;
3257 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3260 if (parse_vp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3267 vp
->Opcode
= OPCODE_POW
;
3270 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3273 for (a
= 0; a
< 2; a
++) {
3274 if (parse_vp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3282 vp
->Opcode
= OPCODE_ADD
;
3285 vp
->Opcode
= OPCODE_DP3
;
3288 vp
->Opcode
= OPCODE_DP4
;
3291 vp
->Opcode
= OPCODE_DPH
;
3294 vp
->Opcode
= OPCODE_DST
;
3297 vp
->Opcode
= OPCODE_MAX
;
3300 vp
->Opcode
= OPCODE_MIN
;
3303 vp
->Opcode
= OPCODE_MUL
;
3306 vp
->Opcode
= OPCODE_SGE
;
3309 vp
->Opcode
= OPCODE_SLT
;
3312 vp
->Opcode
= OPCODE_SUB
;
3315 vp
->Opcode
= OPCODE_XPD
;
3318 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3321 for (a
= 0; a
< 2; a
++) {
3322 if (parse_vp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3330 vp
->Opcode
= OPCODE_MAD
;
3334 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3337 for (a
= 0; a
< 3; a
++) {
3338 if (parse_vp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3346 vp
->Opcode
= OPCODE_SWZ
;
3353 enum register_file file
;
3356 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3359 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
, &relAddr
))
3361 parse_extended_swizzle_mask (inst
, swizzle
, &negateMask
);
3362 vp
->SrcReg
[0].File
= file
;
3363 vp
->SrcReg
[0].Index
= index
;
3364 vp
->SrcReg
[0].NegateBase
= negateMask
;
3365 vp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
3369 vp
->SrcReg
[0].RelAddr
= relAddr
;
3379 print_state_token (GLint token
)
3382 case STATE_MATERIAL
:
3383 fprintf (stderr
, "STATE_MATERIAL ");
3386 fprintf (stderr
, "STATE_LIGHT ");
3389 case STATE_LIGHTMODEL_AMBIENT
:
3390 fprintf (stderr
, "STATE_AMBIENT ");
3393 case STATE_LIGHTMODEL_SCENECOLOR
:
3394 fprintf (stderr
, "STATE_SCENECOLOR ");
3397 case STATE_LIGHTPROD
:
3398 fprintf (stderr
, "STATE_LIGHTPROD ");
3402 fprintf (stderr
, "STATE_TEXGEN ");
3405 case STATE_FOG_COLOR
:
3406 fprintf (stderr
, "STATE_FOG_COLOR ");
3409 case STATE_FOG_PARAMS
:
3410 fprintf (stderr
, "STATE_FOG_PARAMS ");
3413 case STATE_CLIPPLANE
:
3414 fprintf (stderr
, "STATE_CLIPPLANE ");
3417 case STATE_POINT_SIZE
:
3418 fprintf (stderr
, "STATE_POINT_SIZE ");
3421 case STATE_POINT_ATTENUATION
:
3422 fprintf (stderr
, "STATE_ATTENUATION ");
3426 fprintf (stderr
, "STATE_MATRIX ");
3429 case STATE_MODELVIEW
:
3430 fprintf (stderr
, "STATE_MODELVIEW ");
3433 case STATE_PROJECTION
:
3434 fprintf (stderr
, "STATE_PROJECTION ");
3438 fprintf (stderr
, "STATE_MVP ");
3442 fprintf (stderr
, "STATE_TEXTURE ");
3446 fprintf (stderr
, "STATE_PROGRAM ");
3449 case STATE_MATRIX_INVERSE
:
3450 fprintf (stderr
, "STATE_INVERSE ");
3453 case STATE_MATRIX_TRANSPOSE
:
3454 fprintf (stderr
, "STATE_TRANSPOSE ");
3457 case STATE_MATRIX_INVTRANS
:
3458 fprintf (stderr
, "STATE_INVTRANS ");
3462 fprintf (stderr
, "STATE_AMBIENT ");
3466 fprintf (stderr
, "STATE_DIFFUSE ");
3469 case STATE_SPECULAR
:
3470 fprintf (stderr
, "STATE_SPECULAR ");
3473 case STATE_EMISSION
:
3474 fprintf (stderr
, "STATE_EMISSION ");
3477 case STATE_SHININESS
:
3478 fprintf (stderr
, "STATE_SHININESS ");
3482 fprintf (stderr
, "STATE_HALF ");
3485 case STATE_POSITION
:
3486 fprintf (stderr
, "STATE_POSITION ");
3489 case STATE_ATTENUATION
:
3490 fprintf (stderr
, "STATE_ATTENUATION ");
3493 case STATE_SPOT_DIRECTION
:
3494 fprintf (stderr
, "STATE_DIRECTION ");
3497 case STATE_TEXGEN_EYE_S
:
3498 fprintf (stderr
, "STATE_TEXGEN_EYE_S ");
3501 case STATE_TEXGEN_EYE_T
:
3502 fprintf (stderr
, "STATE_TEXGEN_EYE_T ");
3505 case STATE_TEXGEN_EYE_R
:
3506 fprintf (stderr
, "STATE_TEXGEN_EYE_R ");
3509 case STATE_TEXGEN_EYE_Q
:
3510 fprintf (stderr
, "STATE_TEXGEN_EYE_Q ");
3513 case STATE_TEXGEN_OBJECT_S
:
3514 fprintf (stderr
, "STATE_TEXGEN_EYE_S ");
3517 case STATE_TEXGEN_OBJECT_T
:
3518 fprintf (stderr
, "STATE_TEXGEN_OBJECT_T ");
3521 case STATE_TEXGEN_OBJECT_R
:
3522 fprintf (stderr
, "STATE_TEXGEN_OBJECT_R ");
3525 case STATE_TEXGEN_OBJECT_Q
:
3526 fprintf (stderr
, "STATE_TEXGEN_OBJECT_Q ");
3529 case STATE_TEXENV_COLOR
:
3530 fprintf (stderr
, "STATE_TEXENV_COLOR ");
3533 case STATE_DEPTH_RANGE
:
3534 fprintf (stderr
, "STATE_DEPTH_RANGE ");
3537 case STATE_VERTEX_PROGRAM
:
3538 fprintf (stderr
, "STATE_VERTEX_PROGRAM ");
3541 case STATE_FRAGMENT_PROGRAM
:
3542 fprintf (stderr
, "STATE_FRAGMENT_PROGRAM ");
3546 fprintf (stderr
, "STATE_ENV ");
3550 fprintf (stderr
, "STATE_LOCAL ");
3554 fprintf (stderr
, "[%d] ", token
);
3559 debug_variables (GLcontext
* ctx
, struct var_cache
*vc_head
,
3560 struct arb_program
*Program
)
3562 struct var_cache
*vc
;
3565 fprintf (stderr
, "debug_variables, vc_head: %x\n", vc_head
);
3567 /* First of all, print out the contents of the var_cache */
3570 fprintf (stderr
, "[%x]\n", vc
);
3573 fprintf (stderr
, "UNDEFINED %s\n", vc
->name
);
3576 fprintf (stderr
, "ATTRIB %s\n", vc
->name
);
3577 fprintf (stderr
, " binding: 0x%x\n", vc
->attrib_binding
);
3580 fprintf (stderr
, "PARAM %s begin: %d len: %d\n", vc
->name
,
3581 vc
->param_binding_begin
, vc
->param_binding_length
);
3582 b
= vc
->param_binding_begin
;
3583 for (a
= 0; a
< vc
->param_binding_length
; a
++) {
3584 fprintf (stderr
, "%s\n",
3585 Program
->Parameters
->Parameters
[a
+ b
].Name
);
3586 if (Program
->Parameters
->Parameters
[a
+ b
].Type
== STATE
) {
3587 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3589 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3591 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3593 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3595 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3597 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3601 fprintf (stderr
, "%f %f %f %f\n",
3602 Program
->Parameters
->Parameters
[a
+ b
].Values
[0],
3603 Program
->Parameters
->Parameters
[a
+ b
].Values
[1],
3604 Program
->Parameters
->Parameters
[a
+ b
].Values
[2],
3605 Program
->Parameters
->Parameters
[a
+ b
].Values
[3]);
3609 fprintf (stderr
, "TEMP %s\n", vc
->name
);
3610 fprintf (stderr
, " binding: 0x%x\n", vc
->temp_binding
);
3613 fprintf (stderr
, "OUTPUT %s\n", vc
->name
);
3614 fprintf (stderr
, " binding: 0x%x\n", vc
->output_binding
);
3617 fprintf (stderr
, "ALIAS %s\n", vc
->name
);
3618 fprintf (stderr
, " binding: 0x%x (%s)\n",
3619 vc
->alias_binding
, vc
->alias_binding
->name
);
3626 #endif /* DEBUG_PARSING */
3630 * The main loop for parsing a fragment or vertex program
3632 * \return 1 on error, 0 on success
3635 parse_instructions(GLcontext
* ctx
, GLubyte
* inst
, struct var_cache
**vc_head
,
3636 struct arb_program
*Program
)
3638 const GLuint maxInst
= (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
3639 ? ctx
->Const
.FragmentProgram
.MaxInstructions
3640 : ctx
->Const
.VertexProgram
.MaxInstructions
;
3643 ASSERT(MAX_INSTRUCTIONS
>= maxInst
);
3645 Program
->MajorVersion
= (GLuint
) * inst
++;
3646 Program
->MinorVersion
= (GLuint
) * inst
++;
3648 while (*inst
!= END
) {
3653 case ARB_PRECISION_HINT_FASTEST
:
3654 Program
->PrecisionOption
= GL_FASTEST
;
3657 case ARB_PRECISION_HINT_NICEST
:
3658 Program
->PrecisionOption
= GL_NICEST
;
3662 Program
->FogOption
= GL_EXP
;
3666 Program
->FogOption
= GL_EXP2
;
3669 case ARB_FOG_LINEAR
:
3670 Program
->FogOption
= GL_LINEAR
;
3673 case ARB_POSITION_INVARIANT
:
3674 if (Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
3675 Program
->HintPositionInvariant
= GL_TRUE
;
3678 case ARB_FRAGMENT_PROGRAM_SHADOW
:
3679 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3680 /* TODO ARB_fragment_program_shadow code */
3684 case ARB_DRAW_BUFFERS
:
3685 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3686 /* do nothing for now */
3694 if (Program
->Base
.NumInstructions
+ 1 >= maxInst
) {
3695 const char *msg
= "Max instruction count exceeded";
3696 _mesa_set_program_error(ctx
, Program
->Position
, msg
);
3697 _mesa_error(ctx
, GL_INVALID_OPERATION
, msg
);
3700 Program
->Position
= parse_position (&inst
);
3701 /* parse the current instruction */
3702 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3703 err
= parse_fp_instruction (ctx
, &inst
, vc_head
, Program
,
3704 &Program
->Base
.Instructions
[Program
->Base
.NumInstructions
]);
3707 err
= parse_vp_instruction (ctx
, &inst
, vc_head
, Program
,
3708 &Program
->Base
.Instructions
[Program
->Base
.NumInstructions
]);
3711 /* increment instuction count */
3712 Program
->Base
.NumInstructions
++;
3716 err
= parse_declaration (ctx
, &inst
, vc_head
, Program
);
3727 /* Finally, tag on an OPCODE_END instruction */
3729 const GLuint numInst
= Program
->Base
.NumInstructions
;
3730 _mesa_init_instruction(Program
->Base
.Instructions
+ numInst
);
3731 Program
->Base
.Instructions
[numInst
].Opcode
= OPCODE_END
;
3732 /* YYY Wrong Position in program, whatever, at least not random -> crash
3733 Program->Position = parse_position (&inst);
3735 Program
->Base
.Instructions
[numInst
].StringPos
= Program
->Position
;
3737 Program
->Base
.NumInstructions
++;
3740 * Initialize native counts to logical counts. The device driver may
3741 * change them if program is translated into a hardware program.
3743 Program
->Base
.NumNativeInstructions
= Program
->Base
.NumInstructions
;
3744 Program
->Base
.NumNativeTemporaries
= Program
->Base
.NumTemporaries
;
3745 Program
->Base
.NumNativeParameters
= Program
->Base
.NumParameters
;
3746 Program
->Base
.NumNativeAttributes
= Program
->Base
.NumAttributes
;
3747 Program
->Base
.NumNativeAddressRegs
= Program
->Base
.NumAddressRegs
;
3754 __extension__
static char core_grammar_text
[] =
3755 #include "grammar_syn.h"
3760 * Set a grammar parameter.
3761 * \param name the grammar parameter
3762 * \param value the new parameter value
3763 * \return 0 if OK, 1 if error
3766 set_reg8 (GLcontext
*ctx
, grammar id
, const char *name
, GLubyte value
)
3768 char error_msg
[300];
3771 if (grammar_set_reg8 (id
, (const byte
*) name
, value
))
3774 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3775 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3776 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Grammar Register Error");
3782 * Enable support for the given language option in the parser.
3783 * \return 1 if OK, 0 if error
3786 enable_ext(GLcontext
*ctx
, grammar id
, const char *name
)
3788 return !set_reg8(ctx
, id
, name
, 1);
3793 * Enable parser extensions based on which OpenGL extensions are supported
3794 * by this rendering context.
3796 * \return GL_TRUE if OK, GL_FALSE if error.
3799 enable_parser_extensions(GLcontext
*ctx
, grammar id
)
3802 /* These are not supported at this time */
3803 if ((ctx
->Extensions
.ARB_vertex_blend
||
3804 ctx
->Extensions
.EXT_vertex_weighting
)
3805 && !enable_ext(ctx
, id
, "point_parameters"))
3807 if (ctx
->Extensions
.ARB_matrix_palette
3808 && !enable_ext(ctx
, id
, "matrix_palette"))
3810 if (ctx
->Extensions
.ARB_fragment_program_shadow
3811 && !enable_ext(ctx
, id
, "fragment_program_shadow"))
3814 if (ctx
->Extensions
.EXT_point_parameters
3815 && !enable_ext(ctx
, id
, "point_parameters"))
3817 if (ctx
->Extensions
.EXT_secondary_color
3818 && !enable_ext(ctx
, id
, "secondary_color"))
3820 if (ctx
->Extensions
.EXT_fog_coord
3821 && !enable_ext(ctx
, id
, "fog_coord"))
3823 if (ctx
->Extensions
.NV_texture_rectangle
3824 && !enable_ext(ctx
, id
, "texture_rectangle"))
3826 if (ctx
->Extensions
.ARB_draw_buffers
3827 && !enable_ext(ctx
, id
, "draw_buffers"))
3835 * This kicks everything off.
3837 * \param ctx - The GL Context
3838 * \param str - The program string
3839 * \param len - The program string length
3840 * \param program - The arb_program struct to return all the parsed info in
3841 * \return GL_TRUE on sucess, GL_FALSE on error
3844 _mesa_parse_arb_program(GLcontext
*ctx
, GLenum target
,
3845 const GLubyte
*str
, GLsizei len
,
3846 struct arb_program
*program
)
3848 GLint a
, err
, error_pos
;
3849 char error_msg
[300];
3851 struct var_cache
*vc_head
;
3852 grammar arbprogram_syn_id
;
3853 GLubyte
*parsed
, *inst
;
3854 GLubyte
*strz
= NULL
;
3855 static int arbprogram_syn_is_ok
= 0; /* XXX temporary */
3857 /* set the program target before parsing */
3858 program
->Base
.Target
= target
;
3860 /* Reset error state */
3861 _mesa_set_program_error(ctx
, -1, NULL
);
3863 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
3864 if (!arbprogram_syn_is_ok
) {
3865 /* One-time initialization of parsing system */
3866 grammar grammar_syn_id
;
3869 grammar_syn_id
= grammar_load_from_text ((byte
*) core_grammar_text
);
3870 if (grammar_syn_id
== 0) {
3871 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3872 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3873 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3874 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3875 "glProgramStringARB(Error loading grammar rule set)");
3879 err
= !grammar_check(grammar_syn_id
, (byte
*) arb_grammar_text
,
3880 &parsed
, &parsed_len
);
3882 /* NOTE: we can't destroy grammar_syn_id right here because
3883 * grammar_destroy() can reset the last error
3886 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3887 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3888 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3889 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3890 "glProgramString(Error loading grammar rule set");
3891 grammar_destroy (grammar_syn_id
);
3895 grammar_destroy (grammar_syn_id
);
3897 arbprogram_syn_is_ok
= 1;
3900 /* create the grammar object */
3901 arbprogram_syn_id
= grammar_load_from_text ((byte
*) arb_grammar_text
);
3902 if (arbprogram_syn_id
== 0) {
3903 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3904 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
3905 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3906 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3907 "glProgramString(Error loading grammer rule set)");
3911 /* Set program_target register value */
3912 if (set_reg8 (ctx
, arbprogram_syn_id
, "program_target",
3913 program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
? 0x10 : 0x20)) {
3914 grammar_destroy (arbprogram_syn_id
);
3918 if (!enable_parser_extensions(ctx
, arbprogram_syn_id
)) {
3919 grammar_destroy(arbprogram_syn_id
);
3923 /* check for NULL character occurences */
3926 for (i
= 0; i
< len
; i
++) {
3927 if (str
[i
] == '\0') {
3928 _mesa_set_program_error (ctx
, i
, "invalid character");
3929 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3930 "glProgramStringARB(illegal character)");
3931 grammar_destroy (arbprogram_syn_id
);
3937 /* copy the program string to a null-terminated string */
3938 strz
= (GLubyte
*) _mesa_malloc (len
+ 1);
3940 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glProgramStringARB");
3941 grammar_destroy (arbprogram_syn_id
);
3944 _mesa_memcpy (strz
, str
, len
);
3947 /* do a fast check on program string - initial production buffer is 4K */
3948 err
= !grammar_fast_check(arbprogram_syn_id
, strz
,
3949 &parsed
, &parsed_len
, 0x1000);
3951 /* Syntax parse error */
3954 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
3955 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3956 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3957 "glProgramStringARB(syntax error)");
3959 /* useful for debugging */
3964 fprintf(stderr
, "program: %s\n", (char *) strz
);
3965 fprintf(stderr
, "Error Pos: %d\n", ctx
->program
.ErrorPos
);
3966 s
= (char *) _mesa_find_line_column(strz
, strz
+ctx
->program
.ErrorPos
, &line
, &col
);
3967 fprintf(stderr
, "line %d col %d: %s\n", line
, col
, s
);
3971 grammar_destroy (arbprogram_syn_id
);
3975 grammar_destroy (arbprogram_syn_id
);
3978 * Program string is syntactically correct at this point
3979 * Parse the tokenized version of the program now, generating
3980 * vertex/fragment program instructions.
3983 /* Initialize the arb_program struct */
3984 program
->Base
.String
= strz
;
3985 program
->Base
.Instructions
= (struct prog_instruction
*)
3986 _mesa_malloc(MAX_INSTRUCTIONS
* sizeof(struct prog_instruction
));
3987 program
->Base
.NumInstructions
=
3988 program
->Base
.NumTemporaries
=
3989 program
->Base
.NumParameters
=
3990 program
->Base
.NumAttributes
= program
->Base
.NumAddressRegs
= 0;
3991 program
->Base
.Parameters
= _mesa_new_parameter_list ();
3992 program
->Base
.InputsRead
= 0x0;
3993 program
->Base
.OutputsWritten
= 0x0;
3994 program
->Position
= 0;
3995 program
->MajorVersion
= program
->MinorVersion
= 0;
3996 program
->PrecisionOption
= GL_DONT_CARE
;
3997 program
->FogOption
= GL_NONE
;
3998 program
->HintPositionInvariant
= GL_FALSE
;
3999 for (a
= 0; a
< MAX_TEXTURE_IMAGE_UNITS
; a
++)
4000 program
->TexturesUsed
[a
] = 0x0;
4001 program
->NumAluInstructions
=
4002 program
->NumTexInstructions
=
4003 program
->NumTexIndirections
= 0;
4004 program
->UsesKill
= 0;
4009 /* Start examining the tokens in the array */
4012 /* Check the grammer rev */
4013 if (*inst
++ != REVISION
) {
4014 _mesa_set_program_error (ctx
, 0, "Grammar version mismatch");
4015 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4016 "glProgramStringARB(Grammar version mismatch)");
4020 /* ignore program target */
4022 err
= parse_instructions(ctx
, inst
, &vc_head
, program
);
4025 /*debug_variables(ctx, vc_head, program); */
4027 /* We're done with the parsed binary array */
4028 var_cache_destroy (&vc_head
);
4030 _mesa_free (parsed
);
4032 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
4033 * to size [ap.Base.NumInstructions].
4035 program
->Base
.Instructions
= (struct prog_instruction
*)
4036 _mesa_realloc(program
->Base
.Instructions
,
4037 MAX_INSTRUCTIONS
* sizeof(struct prog_instruction
),/*orig*/
4038 program
->Base
.NumInstructions
* sizeof(struct prog_instruction
));
4046 _mesa_parse_arb_fragment_program(GLcontext
* ctx
, GLenum target
,
4047 const GLvoid
*str
, GLsizei len
,
4048 struct fragment_program
*program
)
4050 struct arb_program ap
;
4053 ASSERT(target
== GL_FRAGMENT_PROGRAM_ARB
);
4054 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
, &ap
)) {
4055 /* Error in the program. Just return. */
4059 /* Copy the relevant contents of the arb_program struct into the
4060 * fragment_program struct.
4062 program
->Base
.String
= ap
.Base
.String
;
4063 program
->Base
.NumInstructions
= ap
.Base
.NumInstructions
;
4064 program
->Base
.NumTemporaries
= ap
.Base
.NumTemporaries
;
4065 program
->Base
.NumParameters
= ap
.Base
.NumParameters
;
4066 program
->Base
.NumAttributes
= ap
.Base
.NumAttributes
;
4067 program
->Base
.NumAddressRegs
= ap
.Base
.NumAddressRegs
;
4068 program
->NumAluInstructions
= ap
.NumAluInstructions
;
4069 program
->NumTexInstructions
= ap
.NumTexInstructions
;
4070 program
->NumTexIndirections
= ap
.NumTexIndirections
;
4071 program
->NumNativeAluInstructions
= ap
.NumAluInstructions
;
4072 program
->NumNativeTexInstructions
= ap
.NumTexInstructions
;
4073 program
->NumNativeTexIndirections
= ap
.NumTexIndirections
;
4074 program
->Base
.InputsRead
= ap
.Base
.InputsRead
;
4075 program
->Base
.OutputsWritten
= ap
.Base
.OutputsWritten
;
4076 for (i
= 0; i
< MAX_TEXTURE_IMAGE_UNITS
; i
++)
4077 program
->TexturesUsed
[i
] = ap
.TexturesUsed
[i
];
4078 program
->FogOption
= ap
.FogOption
;
4080 if (program
->Base
.Instructions
)
4081 _mesa_free(program
->Base
.Instructions
);
4082 program
->Base
.Instructions
= ap
.Base
.Instructions
;
4084 if (program
->Base
.Parameters
)
4085 _mesa_free_parameter_list(program
->Base
.Parameters
);
4086 program
->Base
.Parameters
= ap
.Base
.Parameters
;
4089 _mesa_print_program(&program
.Base
);
4096 * Parse the vertex program string. If success, update the given
4097 * vertex_program object with the new program. Else, leave the vertex_program
4101 _mesa_parse_arb_vertex_program(GLcontext
*ctx
, GLenum target
,
4102 const GLvoid
*str
, GLsizei len
,
4103 struct vertex_program
*program
)
4105 struct arb_program ap
;
4107 ASSERT(target
== GL_VERTEX_PROGRAM_ARB
);
4109 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
, &ap
)) {
4110 /* Error in the program. Just return. */
4114 /* Copy the relevant contents of the arb_program struct into the
4115 * vertex_program struct.
4117 program
->Base
.String
= ap
.Base
.String
;
4118 program
->Base
.NumInstructions
= ap
.Base
.NumInstructions
;
4119 program
->Base
.NumTemporaries
= ap
.Base
.NumTemporaries
;
4120 program
->Base
.NumParameters
= ap
.Base
.NumParameters
;
4121 program
->Base
.NumAttributes
= ap
.Base
.NumAttributes
;
4122 program
->Base
.NumAddressRegs
= ap
.Base
.NumAddressRegs
;
4123 program
->Base
.InputsRead
= ap
.Base
.InputsRead
;
4124 program
->Base
.OutputsWritten
= ap
.Base
.OutputsWritten
;
4125 program
->IsPositionInvariant
= ap
.HintPositionInvariant
;
4127 if (program
->Base
.Instructions
)
4128 _mesa_free(program
->Base
.Instructions
);
4129 program
->Base
.Instructions
= ap
.Base
.Instructions
;
4131 if (program
->Base
.Parameters
)
4132 _mesa_free_parameter_list(program
->Base
.Parameters
);
4133 program
->Base
.Parameters
= ap
.Base
.Parameters
;
4136 _mesa_print_program(&program
->Base
);