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"
38 #include "prog_parameter.h"
39 #include "prog_statevars.h"
43 #include "prog_instruction.h"
46 /* For ARB programs, use the NV instruction limits */
47 #define MAX_INSTRUCTIONS MAX2(MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS, \
48 MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS)
52 * This is basically a union of the vertex_program and fragment_program
53 * structs that we can use to parse the program into
55 * XXX we can probably get rid of this entirely someday.
59 struct gl_program Base
;
61 GLuint Position
; /* Just used for error reporting while parsing */
65 /* ARB_vertex_progmra options */
66 GLboolean HintPositionInvariant
;
68 /* ARB_fragment_progmra options */
69 GLenum PrecisionOption
; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
70 GLenum FogOption
; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
72 /* ARB_fragment_program specifics */
73 GLbitfield TexturesUsed
[MAX_TEXTURE_IMAGE_UNITS
];
74 GLuint NumAluInstructions
;
75 GLuint NumTexInstructions
;
76 GLuint NumTexIndirections
;
84 * Fragment Program Stuff:
85 * -----------------------------------------------------
87 * - things from Michal's email
89 * + not-overflowing floats (don't use parse_integer..)
90 * + can remove range checking in arbparse.c
92 * - check all limits of number of various variables
97 * Vertex Program Stuff:
98 * -----------------------------------------------------
99 * - Optimize param array usage and count limits correctly, see spec,
101 * + Record if an array is reference absolutly or relatively (or both)
102 * + For absolute arrays, store a bitmap of accesses
103 * + For single parameters, store an access flag
104 * + After parsing, make a parameter cleanup and merging pass, where
105 * relative arrays are layed out first, followed by abs arrays, and
106 * finally single state.
107 * + Remap offsets for param src and dst registers
108 * + Now we can properly count parameter usage
110 * - Multiple state binding errors in param arrays (see spec, just before
115 * -----------------------------------------------------
116 * - User clipping planes vs. PositionInvariant
117 * - Is it sufficient to just multiply by the mvp to transform in the
118 * PositionInvariant case? Or do we need something more involved?
120 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
121 * - fetch state listed in program_parameters list
122 * + WTF should this go???
123 * + currently in nvvertexec.c and s_nvfragprog.c
125 * - allow for multiple address registers (and fetch address regs properly)
128 * -----------------------------------------------------
129 * - remove any leftover unused grammer.c stuff (dict_ ?)
130 * - fix grammer.c error handling so its not static
131 * - #ifdef around stuff pertaining to extentions
133 * Outstanding Questions:
134 * -----------------------------------------------------
135 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
136 * what gets hacked off because of this:
137 * + VERTEX_ATTRIB_MATRIXINDEX
138 * + VERTEX_ATTRIB_WEIGHT
142 * - When can we fetch env/local params from their own register files, and
143 * when to we have to fetch them into the main state register file?
147 * -----------------------------------------------------
150 /* Changes since moving the file to shader directory
152 2004-III-4 ------------------------------------------------------------
153 - added #include "grammar_mesa.h"
154 - removed grammar specific code part (it resides now in grammar.c)
155 - added GL_ARB_fragment_program_shadow tokens
156 - modified #include "arbparse_syn.h"
157 - major changes inside _mesa_parse_arb_program()
158 - check the program string for '\0' characters
159 - copy the program string to a one-byte-longer location to have
161 - position invariance test (not writing to result.position) moved
165 typedef GLubyte
*production
;
169 * This is the text describing the rules to parse the grammar
171 LONGSTRING
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..
521 const GLubyte
*name
; /* don't free() - no need */
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
, const 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
;
603 * Called when an error is detected while parsing/compiling a program.
604 * Sets the ctx->Program.ErrorString field to descript and records a
605 * GL_INVALID_OPERATION error.
606 * \param position position of error in program string
607 * \param descrip verbose error description
610 program_error(GLcontext
*ctx
, GLint position
, const char *descrip
)
613 const char *prefix
= "glProgramString(", *suffix
= ")";
614 char *str
= (char *) _mesa_malloc(_mesa_strlen(descrip
) +
615 _mesa_strlen(prefix
) +
616 _mesa_strlen(suffix
) + 1);
618 _mesa_sprintf(str
, "%s%s%s", prefix
, descrip
, suffix
);
619 _mesa_error(ctx
, GL_INVALID_OPERATION
, str
);
623 _mesa_set_program_error(ctx
, position
, descrip
);
629 * constructs an integer from 4 GLubytes in LE format
632 parse_position (const GLubyte
** inst
)
636 value
= (GLuint
) (*(*inst
)++);
637 value
+= (GLuint
) (*(*inst
)++) * 0x100;
638 value
+= (GLuint
) (*(*inst
)++) * 0x10000;
639 value
+= (GLuint
) (*(*inst
)++) * 0x1000000;
645 * This will, given a string, lookup the string as a variable name in the
646 * var cache. If the name is found, the var cache node corresponding to the
647 * var name is returned. If it is not found, a new entry is allocated
649 * \param I Points into the binary array where the string identifier begins
650 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
651 * \return The location on the var_cache corresponding the the string starting at I
653 static struct var_cache
*
654 parse_string (const GLubyte
** inst
, struct var_cache
**vc_head
,
655 struct arb_program
*Program
, GLuint
* found
)
657 const GLubyte
*i
= *inst
;
658 struct var_cache
*va
= NULL
;
661 *inst
+= _mesa_strlen ((char *) i
) + 1;
663 va
= var_cache_find (*vc_head
, i
);
671 var_cache_create (&va
);
672 va
->name
= (const GLubyte
*) i
;
674 var_cache_append (vc_head
, va
);
680 parse_string_without_adding (const GLubyte
** inst
, struct arb_program
*Program
)
682 const GLubyte
*i
= *inst
;
685 *inst
+= _mesa_strlen ((char *) i
) + 1;
691 * \return -1 if we parse '-', return 1 otherwise
694 parse_sign (const GLubyte
** inst
)
696 /*return *(*inst)++ != '+'; */
702 else if (**inst
== '+') {
711 * parses and returns signed integer
714 parse_integer (const GLubyte
** inst
, struct arb_program
*Program
)
719 /* check if *inst points to '+' or '-'
720 * if yes, grab the sign and increment *inst
722 sign
= parse_sign (inst
);
724 /* now check if *inst points to 0
725 * if yes, increment the *inst and return the default value
732 /* parse the integer as you normally would do it */
733 value
= _mesa_atoi (parse_string_without_adding (inst
, Program
));
735 /* now, after terminating 0 there is a position
736 * to parse it - parse_position()
738 Program
->Position
= parse_position (inst
);
744 Accumulate this string of digits, and return them as
745 a large integer represented in floating point (for range).
746 If scale is not NULL, also accumulates a power-of-ten
747 integer scale factor that represents the number of digits
751 parse_float_string(const GLubyte
** inst
, struct arb_program
*Program
, GLdouble
*scale
)
753 GLdouble value
= 0.0;
754 GLdouble oscale
= 1.0;
756 if (**inst
== 0) { /* this string of digits is empty-- do nothing */
759 else { /* nonempty string-- parse out the digits */
760 while (**inst
>= '0' && **inst
<= '9') {
761 GLubyte digit
= *((*inst
)++);
762 value
= value
* 10.0 + (GLint
) (digit
- '0');
765 assert(**inst
== 0); /* integer string should end with 0 */
766 (*inst
)++; /* skip over terminating 0 */
767 Program
->Position
= parse_position(inst
); /* skip position (from integer) */
775 Parse an unsigned floating-point number from this stream of tokenized
776 characters. Example floating-point formats supported:
784 parse_float (const GLubyte
** inst
, struct arb_program
*Program
)
787 GLdouble whole
, fraction
, fracScale
= 1.0;
789 whole
= parse_float_string(inst
, Program
, 0);
790 fraction
= parse_float_string(inst
, Program
, &fracScale
);
792 /* Parse signed exponent */
793 exponent
= parse_integer(inst
, Program
); /* This is the exponent */
795 /* Assemble parts of floating-point number: */
796 return (GLfloat
) ((whole
+ fraction
/ fracScale
) *
797 _mesa_pow(10.0, (GLfloat
) exponent
));
804 parse_signed_float (const GLubyte
** inst
, struct arb_program
*Program
)
806 GLint sign
= parse_sign (inst
);
807 GLfloat value
= parse_float (inst
, Program
);
812 * This picks out a constant value from the parsed array. The constant vector is r
813 * returned in the *values array, which should be of length 4.
815 * \param values - The 4 component vector with the constant value in it
818 parse_constant (const GLubyte
** inst
, GLfloat
*values
, struct arb_program
*Program
,
821 GLuint components
, i
;
824 switch (*(*inst
)++) {
825 case CONSTANT_SCALAR
:
826 if (use
== GL_TRUE
) {
829 values
[2] = values
[3] = parse_float (inst
, Program
);
834 values
[2] = values
[3] = parse_signed_float (inst
, Program
);
838 case CONSTANT_VECTOR
:
839 values
[0] = values
[1] = values
[2] = 0;
841 components
= *(*inst
)++;
842 for (i
= 0; i
< components
; i
++) {
843 values
[i
] = parse_signed_float (inst
, Program
);
850 * \param offset The offset from the address register that we should
853 * \return 0 on sucess, 1 on error
856 parse_relative_offset(GLcontext
*ctx
, const GLubyte
**inst
,
857 struct arb_program
*Program
, GLint
*offset
)
860 *offset
= parse_integer(inst
, Program
);
865 * \param color 0 if color type is primary, 1 if color type is secondary
866 * \return 0 on sucess, 1 on error
869 parse_color_type (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
872 (void) ctx
; (void) Program
;
873 *color
= *(*inst
)++ != COLOR_PRIMARY
;
878 * Get an integer corresponding to a generic vertex attribute.
880 * \return 0 on sucess, 1 on error
883 parse_generic_attrib_num(GLcontext
*ctx
, const GLubyte
** inst
,
884 struct arb_program
*Program
, GLuint
*attrib
)
886 GLint i
= parse_integer(inst
, Program
);
888 if ((i
< 0) || (i
>= MAX_VERTEX_PROGRAM_ATTRIBS
))
890 program_error(ctx
, Program
->Position
,
891 "Invalid generic vertex attribute index");
895 *attrib
= (GLuint
) i
;
902 * \param color The index of the color buffer to write into
903 * \return 0 on sucess, 1 on error
906 parse_output_color_num (GLcontext
* ctx
, const GLubyte
** inst
,
907 struct arb_program
*Program
, GLuint
* color
)
909 GLint i
= parse_integer (inst
, Program
);
911 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxDrawBuffers
)) {
912 program_error(ctx
, Program
->Position
, "Invalid draw buffer index");
922 * \param coord The texture unit index
923 * \return 0 on sucess, 1 on error
926 parse_texcoord_num (GLcontext
* ctx
, const GLubyte
** inst
,
927 struct arb_program
*Program
, GLuint
* coord
)
929 GLint i
= parse_integer (inst
, Program
);
931 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxTextureUnits
)) {
932 program_error(ctx
, Program
->Position
, "Invalid texture unit index");
941 * \param coord The weight index
942 * \return 0 on sucess, 1 on error
945 parse_weight_num (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
948 *coord
= parse_integer (inst
, Program
);
950 if ((*coord
< 0) || (*coord
>= 1)) {
951 program_error(ctx
, Program
->Position
, "Invalid weight index");
959 * \param coord The clip plane index
960 * \return 0 on sucess, 1 on error
963 parse_clipplane_num (GLcontext
* ctx
, const GLubyte
** inst
,
964 struct arb_program
*Program
, GLint
* coord
)
966 *coord
= parse_integer (inst
, Program
);
968 if ((*coord
< 0) || (*coord
>= (GLint
) ctx
->Const
.MaxClipPlanes
)) {
969 program_error(ctx
, Program
->Position
, "Invalid clip plane index");
978 * \return 0 on front face, 1 on back face
981 parse_face_type (const GLubyte
** inst
)
983 switch (*(*inst
)++) {
995 * Given a matrix and a modifier token on the binary array, return tokens
996 * that _mesa_fetch_state() [program.c] can understand.
998 * \param matrix - the matrix we are talking about
999 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
1000 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
1001 * \return 0 on sucess, 1 on failure
1004 parse_matrix (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
1005 GLint
* matrix
, GLint
* matrix_idx
, GLint
* matrix_modifier
)
1007 GLubyte mat
= *(*inst
)++;
1012 case MATRIX_MODELVIEW
:
1013 *matrix
= STATE_MODELVIEW
;
1014 *matrix_idx
= parse_integer (inst
, Program
);
1015 if (*matrix_idx
> 0) {
1016 program_error(ctx
, Program
->Position
,
1017 "ARB_vertex_blend not supported");
1022 case MATRIX_PROJECTION
:
1023 *matrix
= STATE_PROJECTION
;
1027 *matrix
= STATE_MVP
;
1030 case MATRIX_TEXTURE
:
1031 *matrix
= STATE_TEXTURE
;
1032 *matrix_idx
= parse_integer (inst
, Program
);
1033 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxTextureUnits
) {
1034 program_error(ctx
, Program
->Position
, "Invalid Texture Unit");
1035 /* bad *matrix_id */
1040 /* This is not currently supported (ARB_matrix_palette) */
1041 case MATRIX_PALETTE
:
1042 *matrix_idx
= parse_integer (inst
, Program
);
1043 program_error(ctx
, Program
->Position
,
1044 "ARB_matrix_palette not supported");
1048 case MATRIX_PROGRAM
:
1049 *matrix
= STATE_PROGRAM
;
1050 *matrix_idx
= parse_integer (inst
, Program
);
1051 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxProgramMatrices
) {
1052 program_error(ctx
, Program
->Position
, "Invalid Program Matrix");
1053 /* bad *matrix_idx */
1059 switch (*(*inst
)++) {
1060 case MATRIX_MODIFIER_IDENTITY
:
1061 *matrix_modifier
= 0;
1063 case MATRIX_MODIFIER_INVERSE
:
1064 *matrix_modifier
= STATE_MATRIX_INVERSE
;
1066 case MATRIX_MODIFIER_TRANSPOSE
:
1067 *matrix_modifier
= STATE_MATRIX_TRANSPOSE
;
1069 case MATRIX_MODIFIER_INVTRANS
:
1070 *matrix_modifier
= STATE_MATRIX_INVTRANS
;
1079 * This parses a state string (rather, the binary version of it) into
1080 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1082 * \param inst - the start in the binary arry to start working from
1083 * \param state_tokens - the storage for the 6-token state description
1084 * \return - 0 on sucess, 1 on error
1087 parse_state_single_item (GLcontext
* ctx
, const GLubyte
** inst
,
1088 struct arb_program
*Program
, GLint
* state_tokens
)
1090 switch (*(*inst
)++) {
1091 case STATE_MATERIAL_PARSER
:
1092 state_tokens
[0] = STATE_MATERIAL
;
1093 state_tokens
[1] = parse_face_type (inst
);
1094 switch (*(*inst
)++) {
1095 case MATERIAL_AMBIENT
:
1096 state_tokens
[2] = STATE_AMBIENT
;
1098 case MATERIAL_DIFFUSE
:
1099 state_tokens
[2] = STATE_DIFFUSE
;
1101 case MATERIAL_SPECULAR
:
1102 state_tokens
[2] = STATE_SPECULAR
;
1104 case MATERIAL_EMISSION
:
1105 state_tokens
[2] = STATE_EMISSION
;
1107 case MATERIAL_SHININESS
:
1108 state_tokens
[2] = STATE_SHININESS
;
1113 case STATE_LIGHT_PARSER
:
1114 state_tokens
[0] = STATE_LIGHT
;
1115 state_tokens
[1] = parse_integer (inst
, Program
);
1117 /* Check the value of state_tokens[1] against the # of lights */
1118 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1119 program_error(ctx
, Program
->Position
, "Invalid Light Number");
1120 /* bad state_tokens[1] */
1124 switch (*(*inst
)++) {
1126 state_tokens
[2] = STATE_AMBIENT
;
1129 state_tokens
[2] = STATE_DIFFUSE
;
1131 case LIGHT_SPECULAR
:
1132 state_tokens
[2] = STATE_SPECULAR
;
1134 case LIGHT_POSITION
:
1135 state_tokens
[2] = STATE_POSITION
;
1137 case LIGHT_ATTENUATION
:
1138 state_tokens
[2] = STATE_ATTENUATION
;
1141 state_tokens
[2] = STATE_HALF
;
1143 case LIGHT_SPOT_DIRECTION
:
1144 state_tokens
[2] = STATE_SPOT_DIRECTION
;
1149 case STATE_LIGHT_MODEL
:
1150 switch (*(*inst
)++) {
1151 case LIGHT_MODEL_AMBIENT
:
1152 state_tokens
[0] = STATE_LIGHTMODEL_AMBIENT
;
1154 case LIGHT_MODEL_SCENECOLOR
:
1155 state_tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
1156 state_tokens
[1] = parse_face_type (inst
);
1161 case STATE_LIGHT_PROD
:
1162 state_tokens
[0] = STATE_LIGHTPROD
;
1163 state_tokens
[1] = parse_integer (inst
, Program
);
1165 /* Check the value of state_tokens[1] against the # of lights */
1166 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1167 program_error(ctx
, Program
->Position
, "Invalid Light Number");
1168 /* bad state_tokens[1] */
1172 state_tokens
[2] = parse_face_type (inst
);
1173 switch (*(*inst
)++) {
1174 case LIGHT_PROD_AMBIENT
:
1175 state_tokens
[3] = STATE_AMBIENT
;
1177 case LIGHT_PROD_DIFFUSE
:
1178 state_tokens
[3] = STATE_DIFFUSE
;
1180 case LIGHT_PROD_SPECULAR
:
1181 state_tokens
[3] = STATE_SPECULAR
;
1188 switch (*(*inst
)++) {
1190 state_tokens
[0] = STATE_FOG_COLOR
;
1193 state_tokens
[0] = STATE_FOG_PARAMS
;
1199 state_tokens
[1] = parse_integer (inst
, Program
);
1200 switch (*(*inst
)++) {
1202 state_tokens
[0] = STATE_TEXENV_COLOR
;
1211 state_tokens
[0] = STATE_TEXGEN
;
1212 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1214 if (parse_texcoord_num (ctx
, inst
, Program
, &coord
))
1216 state_tokens
[1] = coord
;
1221 /* 0 - s, 1 - t, 2 - r, 3 - q */
1224 if (type
== TEX_GEN_EYE
) {
1227 state_tokens
[2] = STATE_TEXGEN_EYE_S
;
1230 state_tokens
[2] = STATE_TEXGEN_EYE_T
;
1233 state_tokens
[2] = STATE_TEXGEN_EYE_R
;
1236 state_tokens
[2] = STATE_TEXGEN_EYE_Q
;
1243 state_tokens
[2] = STATE_TEXGEN_OBJECT_S
;
1246 state_tokens
[2] = STATE_TEXGEN_OBJECT_T
;
1249 state_tokens
[2] = STATE_TEXGEN_OBJECT_R
;
1252 state_tokens
[2] = STATE_TEXGEN_OBJECT_Q
;
1260 switch (*(*inst
)++) {
1262 state_tokens
[0] = STATE_DEPTH_RANGE
;
1267 case STATE_CLIP_PLANE
:
1268 state_tokens
[0] = STATE_CLIPPLANE
;
1269 state_tokens
[1] = parse_integer (inst
, Program
);
1270 if (parse_clipplane_num (ctx
, inst
, Program
, &state_tokens
[1]))
1275 switch (*(*inst
++)) {
1277 state_tokens
[0] = STATE_POINT_SIZE
;
1280 case POINT_ATTENUATION
:
1281 state_tokens
[0] = STATE_POINT_ATTENUATION
;
1286 /* XXX: I think this is the correct format for a matrix row */
1287 case STATE_MATRIX_ROWS
:
1288 state_tokens
[0] = STATE_MATRIX
;
1290 (ctx
, inst
, Program
, &state_tokens
[1], &state_tokens
[2],
1294 state_tokens
[3] = parse_integer (inst
, Program
); /* The first row to grab */
1296 if ((**inst
) != 0) { /* Either the last row, 0 */
1297 state_tokens
[4] = parse_integer (inst
, Program
);
1298 if (state_tokens
[4] < state_tokens
[3]) {
1299 program_error(ctx
, Program
->Position
,
1300 "Second matrix index less than the first");
1301 /* state_tokens[4] vs. state_tokens[3] */
1306 state_tokens
[4] = state_tokens
[3];
1316 * This parses a state string (rather, the binary version of it) into
1317 * a 6-token similar for the state fetching code in program.c
1319 * One might ask, why fetch these parameters into just like you fetch
1320 * state when they are already stored in other places?
1322 * Because of array offsets -> We can stick env/local parameters in the
1323 * middle of a parameter array and then index someplace into the array
1326 * One optimization might be to only do this for the cases where the
1327 * env/local parameters end up inside of an array, and leave the
1328 * single parameters (or arrays of pure env/local pareameters) in their
1329 * respective register files.
1331 * For ENV parameters, the format is:
1332 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1333 * state_tokens[1] = STATE_ENV
1334 * state_tokens[2] = the parameter index
1336 * for LOCAL parameters, the format is:
1337 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1338 * state_tokens[1] = STATE_LOCAL
1339 * state_tokens[2] = the parameter index
1341 * \param inst - the start in the binary arry to start working from
1342 * \param state_tokens - the storage for the 6-token state description
1343 * \return - 0 on sucess, 1 on failure
1346 parse_program_single_item (GLcontext
* ctx
, const GLubyte
** inst
,
1347 struct arb_program
*Program
, GLint
* state_tokens
)
1349 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1350 state_tokens
[0] = STATE_FRAGMENT_PROGRAM
;
1352 state_tokens
[0] = STATE_VERTEX_PROGRAM
;
1355 switch (*(*inst
)++) {
1356 case PROGRAM_PARAM_ENV
:
1357 state_tokens
[1] = STATE_ENV
;
1358 state_tokens
[2] = parse_integer (inst
, Program
);
1360 /* Check state_tokens[2] against the number of ENV parameters available */
1361 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1362 (state_tokens
[2] >= (GLint
) ctx
->Const
.FragmentProgram
.MaxEnvParams
))
1364 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1365 (state_tokens
[2] >= (GLint
) ctx
->Const
.VertexProgram
.MaxEnvParams
))) {
1366 program_error(ctx
, Program
->Position
,
1367 "Invalid Program Env Parameter");
1368 /* bad state_tokens[2] */
1374 case PROGRAM_PARAM_LOCAL
:
1375 state_tokens
[1] = STATE_LOCAL
;
1376 state_tokens
[2] = parse_integer (inst
, Program
);
1378 /* Check state_tokens[2] against the number of LOCAL parameters available */
1379 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1380 (state_tokens
[2] >= (GLint
) ctx
->Const
.FragmentProgram
.MaxLocalParams
))
1382 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1383 (state_tokens
[2] >= (GLint
) ctx
->Const
.VertexProgram
.MaxLocalParams
))) {
1384 program_error(ctx
, Program
->Position
,
1385 "Invalid Program Local Parameter");
1386 /* bad state_tokens[2] */
1396 * For ARB_vertex_program, programs are not allowed to use both an explicit
1397 * vertex attribute and a generic vertex attribute corresponding to the same
1398 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1400 * This will walk our var_cache and make sure that nobody does anything fishy.
1402 * \return 0 on sucess, 1 on error
1405 generic_attrib_check(struct var_cache
*vc_head
)
1408 struct var_cache
*curr
;
1409 GLboolean explicitAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
],
1410 genericAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
];
1412 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1413 explicitAttrib
[a
] = GL_FALSE
;
1414 genericAttrib
[a
] = GL_FALSE
;
1419 if (curr
->type
== vt_attrib
) {
1420 if (curr
->attrib_is_generic
)
1421 genericAttrib
[ curr
->attrib_binding
] = GL_TRUE
;
1423 explicitAttrib
[ curr
->attrib_binding
] = GL_TRUE
;
1429 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1430 if ((explicitAttrib
[a
]) && (genericAttrib
[a
]))
1438 * This will handle the binding side of an ATTRIB var declaration
1440 * \param inputReg returns the input register index, one of the
1441 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
1442 * \return returns 0 on success, 1 on error
1445 parse_attrib_binding(GLcontext
* ctx
, const GLubyte
** inst
,
1446 struct arb_program
*Program
,
1447 GLuint
*inputReg
, GLuint
*is_generic
)
1453 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1454 switch (*(*inst
)++) {
1455 case FRAGMENT_ATTRIB_COLOR
:
1458 err
= parse_color_type (ctx
, inst
, Program
, &coord
);
1459 *inputReg
= FRAG_ATTRIB_COL0
+ coord
;
1462 case FRAGMENT_ATTRIB_TEXCOORD
:
1465 err
= parse_texcoord_num (ctx
, inst
, Program
, &texcoord
);
1466 *inputReg
= FRAG_ATTRIB_TEX0
+ texcoord
;
1469 case FRAGMENT_ATTRIB_FOGCOORD
:
1470 *inputReg
= FRAG_ATTRIB_FOGC
;
1472 case FRAGMENT_ATTRIB_POSITION
:
1473 *inputReg
= FRAG_ATTRIB_WPOS
;
1481 switch (*(*inst
)++) {
1482 case VERTEX_ATTRIB_POSITION
:
1483 *inputReg
= VERT_ATTRIB_POS
;
1486 case VERTEX_ATTRIB_WEIGHT
:
1489 err
= parse_weight_num (ctx
, inst
, Program
, &weight
);
1490 *inputReg
= VERT_ATTRIB_WEIGHT
;
1492 /* hack for Warcraft (see bug 8060) */
1493 _mesa_warning(ctx
, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
1496 program_error(ctx
, Program
->Position
,
1497 "ARB_vertex_blend not supported");
1502 case VERTEX_ATTRIB_NORMAL
:
1503 *inputReg
= VERT_ATTRIB_NORMAL
;
1506 case VERTEX_ATTRIB_COLOR
:
1509 err
= parse_color_type (ctx
, inst
, Program
, &color
);
1511 *inputReg
= VERT_ATTRIB_COLOR1
;
1514 *inputReg
= VERT_ATTRIB_COLOR0
;
1519 case VERTEX_ATTRIB_FOGCOORD
:
1520 *inputReg
= VERT_ATTRIB_FOG
;
1523 case VERTEX_ATTRIB_TEXCOORD
:
1526 err
= parse_texcoord_num (ctx
, inst
, Program
, &unit
);
1527 *inputReg
= VERT_ATTRIB_TEX0
+ unit
;
1531 case VERTEX_ATTRIB_MATRIXINDEX
:
1532 /* Not supported at this time */
1534 const char *msg
= "ARB_palette_matrix not supported";
1535 parse_integer (inst
, Program
);
1536 program_error(ctx
, Program
->Position
, msg
);
1540 case VERTEX_ATTRIB_GENERIC
:
1543 err
= parse_generic_attrib_num(ctx
, inst
, Program
, &attrib
);
1546 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1547 * attributes do not alias the conventional vertex
1551 *inputReg
= attrib
+ VERT_ATTRIB_GENERIC0
;
1565 program_error(ctx
, Program
->Position
, "Bad attribute binding");
1568 Program
->Base
.InputsRead
|= (1 << *inputReg
);
1575 * This translates between a binary token for an output variable type
1576 * and the mesa token for the same thing.
1578 * \param inst The parsed tokens
1579 * \param outputReg Returned index/number of the output register,
1580 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
1583 parse_result_binding(GLcontext
*ctx
, const GLubyte
**inst
,
1584 GLuint
*outputReg
, struct arb_program
*Program
)
1586 const GLubyte token
= *(*inst
)++;
1589 case FRAGMENT_RESULT_COLOR
:
1590 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1593 /* This gets result of the color buffer we're supposed to
1594 * draw into. This pertains to GL_ARB_draw_buffers.
1596 parse_output_color_num(ctx
, inst
, Program
, &out_color
);
1597 ASSERT(out_color
< MAX_DRAW_BUFFERS
);
1598 *outputReg
= FRAG_RESULT_COLR
;
1601 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1602 *outputReg
= VERT_RESULT_HPOS
;
1606 case FRAGMENT_RESULT_DEPTH
:
1607 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1608 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
1609 *outputReg
= FRAG_RESULT_DEPR
;
1612 /* for vtx programs, this is VERTEX_RESULT_COLOR */
1614 GLuint face_type
= parse_face_type(inst
);
1615 GLint err
= parse_color_type(ctx
, inst
, Program
, &color_type
);
1622 *outputReg
= VERT_RESULT_BFC1
; /* secondary color */
1625 *outputReg
= VERT_RESULT_BFC0
; /* primary color */
1631 *outputReg
= VERT_RESULT_COL1
; /* secondary color */
1635 *outputReg
= VERT_RESULT_COL0
; /* primary color */
1641 case VERTEX_RESULT_FOGCOORD
:
1642 *outputReg
= VERT_RESULT_FOGC
;
1645 case VERTEX_RESULT_POINTSIZE
:
1646 *outputReg
= VERT_RESULT_PSIZ
;
1649 case VERTEX_RESULT_TEXCOORD
:
1652 if (parse_texcoord_num (ctx
, inst
, Program
, &unit
))
1654 *outputReg
= VERT_RESULT_TEX0
+ unit
;
1659 Program
->Base
.OutputsWritten
|= (1 << *outputReg
);
1666 * This handles the declaration of ATTRIB variables
1669 * parse_vert_attrib_binding(), or something like that
1671 * \return 0 on sucess, 1 on error
1674 parse_attrib (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1675 struct arb_program
*Program
)
1679 struct var_cache
*attrib_var
;
1681 attrib_var
= parse_string (inst
, vc_head
, Program
, &found
);
1682 Program
->Position
= parse_position (inst
);
1684 error_msg
= (char *)
1685 _mesa_malloc (_mesa_strlen ((char *) attrib_var
->name
) + 40);
1686 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
1688 program_error(ctx
, Program
->Position
, error_msg
);
1689 _mesa_free (error_msg
);
1693 attrib_var
->type
= vt_attrib
;
1695 if (parse_attrib_binding(ctx
, inst
, Program
, &attrib_var
->attrib_binding
,
1696 &attrib_var
->attrib_is_generic
))
1699 if (generic_attrib_check(*vc_head
)) {
1700 program_error(ctx
, Program
->Position
,
1701 "Cannot use both a generic vertex attribute "
1702 "and a specific attribute of the same type");
1706 Program
->Base
.NumAttributes
++;
1711 * \param use -- TRUE if we're called when declaring implicit parameters,
1712 * FALSE if we're declaraing variables. This has to do with
1713 * if we get a signed or unsigned float for scalar constants
1716 parse_param_elements (GLcontext
* ctx
, const GLubyte
** inst
,
1717 struct var_cache
*param_var
,
1718 struct arb_program
*Program
, GLboolean use
)
1722 GLint state_tokens
[6];
1723 GLfloat const_values
[4];
1725 switch (*(*inst
)++) {
1726 case PARAM_STATE_ELEMENT
:
1727 if (parse_state_single_item (ctx
, inst
, Program
, state_tokens
))
1730 /* If we adding STATE_MATRIX that has multiple rows, we need to
1731 * unroll it and call _mesa_add_state_reference() for each row
1733 if ((state_tokens
[0] == STATE_MATRIX
)
1734 && (state_tokens
[3] != state_tokens
[4])) {
1736 GLint first_row
= state_tokens
[3];
1737 GLint last_row
= state_tokens
[4];
1739 for (row
= first_row
; row
<= last_row
; row
++) {
1740 state_tokens
[3] = state_tokens
[4] = row
;
1742 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1744 if (param_var
->param_binding_begin
== ~0U)
1745 param_var
->param_binding_begin
= idx
;
1746 param_var
->param_binding_length
++;
1747 Program
->Base
.NumParameters
++;
1751 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1753 if (param_var
->param_binding_begin
== ~0U)
1754 param_var
->param_binding_begin
= idx
;
1755 param_var
->param_binding_length
++;
1756 Program
->Base
.NumParameters
++;
1760 case PARAM_PROGRAM_ELEMENT
:
1761 if (parse_program_single_item (ctx
, inst
, Program
, state_tokens
))
1763 idx
= _mesa_add_state_reference (Program
->Base
.Parameters
, state_tokens
);
1764 if (param_var
->param_binding_begin
== ~0U)
1765 param_var
->param_binding_begin
= idx
;
1766 param_var
->param_binding_length
++;
1767 Program
->Base
.NumParameters
++;
1769 /* Check if there is more: 0 -> we're done, else its an integer */
1771 GLuint out_of_range
, new_idx
;
1772 GLuint start_idx
= state_tokens
[2] + 1;
1773 GLuint end_idx
= parse_integer (inst
, Program
);
1776 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1777 if (((state_tokens
[1] == STATE_ENV
)
1778 && (end_idx
>= ctx
->Const
.FragmentProgram
.MaxEnvParams
))
1779 || ((state_tokens
[1] == STATE_LOCAL
)
1781 ctx
->Const
.FragmentProgram
.MaxLocalParams
)))
1785 if (((state_tokens
[1] == STATE_ENV
)
1786 && (end_idx
>= ctx
->Const
.VertexProgram
.MaxEnvParams
))
1787 || ((state_tokens
[1] == STATE_LOCAL
)
1789 ctx
->Const
.VertexProgram
.MaxLocalParams
)))
1793 program_error(ctx
, Program
->Position
,
1794 "Invalid Program Parameter"); /*end_idx*/
1798 for (new_idx
= start_idx
; new_idx
<= end_idx
; new_idx
++) {
1799 state_tokens
[2] = new_idx
;
1800 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1802 param_var
->param_binding_length
++;
1803 Program
->Base
.NumParameters
++;
1811 case PARAM_CONSTANT
:
1812 parse_constant (inst
, const_values
, Program
, use
);
1813 idx
= _mesa_add_named_constant(Program
->Base
.Parameters
,
1814 (char *) param_var
->name
,
1816 if (param_var
->param_binding_begin
== ~0U)
1817 param_var
->param_binding_begin
= idx
;
1818 param_var
->param_binding_length
++;
1819 Program
->Base
.NumParameters
++;
1823 program_error(ctx
, Program
->Position
,
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 program_error(ctx
, Program
->Position
, "Too many parameter variables");
1844 * This picks out PARAM program parameter bindings.
1846 * XXX: This needs to be stressed & tested
1848 * \return 0 on sucess, 1 on error
1851 parse_param (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1852 struct arb_program
*Program
)
1855 GLint specified_length
;
1856 struct var_cache
*param_var
;
1859 param_var
= parse_string (inst
, vc_head
, Program
, &found
);
1860 Program
->Position
= parse_position (inst
);
1863 char *error_msg
= (char *)
1864 _mesa_malloc (_mesa_strlen ((char *) param_var
->name
) + 40);
1865 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
1867 program_error (ctx
, Program
->Position
, error_msg
);
1868 _mesa_free (error_msg
);
1872 specified_length
= parse_integer (inst
, Program
);
1874 if (specified_length
< 0) {
1875 program_error(ctx
, Program
->Position
, "Negative parameter array length");
1879 param_var
->type
= vt_param
;
1880 param_var
->param_binding_length
= 0;
1882 /* Right now, everything is shoved into the main state register file.
1884 * In the future, it would be nice to leave things ENV/LOCAL params
1885 * in their respective register files, if possible
1887 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1890 * * - add each guy to the parameter list
1891 * * - increment the param_var->param_binding_len
1892 * * - store the param_var->param_binding_begin for the first one
1893 * * - compare the actual len to the specified len at the end
1895 while (**inst
!= PARAM_NULL
) {
1896 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_FALSE
))
1900 /* Test array length here! */
1901 if (specified_length
) {
1902 if (specified_length
!= (int)param_var
->param_binding_length
) {
1903 program_error(ctx
, Program
->Position
,
1904 "Declared parameter array length does not match parameter list");
1917 parse_param_use (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1918 struct arb_program
*Program
, struct var_cache
**new_var
)
1920 struct var_cache
*param_var
;
1922 /* First, insert a dummy entry into the var_cache */
1923 var_cache_create (¶m_var
);
1924 param_var
->name
= (const GLubyte
*) " ";
1925 param_var
->type
= vt_param
;
1927 param_var
->param_binding_length
= 0;
1928 /* Don't fill in binding_begin; We use the default value of -1
1929 * to tell if its already initialized, elsewhere.
1931 * param_var->param_binding_begin = 0;
1933 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1935 var_cache_append (vc_head
, param_var
);
1937 /* Then fill it with juicy parameter goodness */
1938 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_TRUE
))
1941 *new_var
= param_var
;
1948 * This handles the declaration of TEMP variables
1950 * \return 0 on sucess, 1 on error
1953 parse_temp (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1954 struct arb_program
*Program
)
1957 struct var_cache
*temp_var
;
1959 while (**inst
!= 0) {
1960 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
1961 Program
->Position
= parse_position (inst
);
1963 char *error_msg
= (char *)
1964 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
1965 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
1967 program_error(ctx
, Program
->Position
, error_msg
);
1968 _mesa_free (error_msg
);
1972 temp_var
->type
= vt_temp
;
1974 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1975 (Program
->Base
.NumTemporaries
>=
1976 ctx
->Const
.FragmentProgram
.MaxTemps
))
1977 || ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
1978 && (Program
->Base
.NumTemporaries
>=
1979 ctx
->Const
.VertexProgram
.MaxTemps
))) {
1980 program_error(ctx
, Program
->Position
,
1981 "Too many TEMP variables declared");
1985 temp_var
->temp_binding
= Program
->Base
.NumTemporaries
;
1986 Program
->Base
.NumTemporaries
++;
1994 * This handles variables of the OUTPUT variety
1996 * \return 0 on sucess, 1 on error
1999 parse_output (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2000 struct arb_program
*Program
)
2003 struct var_cache
*output_var
;
2006 output_var
= parse_string (inst
, vc_head
, Program
, &found
);
2007 Program
->Position
= parse_position (inst
);
2009 char *error_msg
= (char *)
2010 _mesa_malloc (_mesa_strlen ((char *) output_var
->name
) + 40);
2011 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
2013 program_error (ctx
, Program
->Position
, error_msg
);
2014 _mesa_free (error_msg
);
2018 output_var
->type
= vt_output
;
2020 err
= parse_result_binding(ctx
, inst
, &output_var
->output_binding
, Program
);
2025 * This handles variables of the ALIAS kind
2027 * \return 0 on sucess, 1 on error
2030 parse_alias (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2031 struct arb_program
*Program
)
2034 struct var_cache
*temp_var
;
2036 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2037 Program
->Position
= parse_position (inst
);
2040 char *error_msg
= (char *)
2041 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2042 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
2044 program_error(ctx
, Program
->Position
, error_msg
);
2045 _mesa_free (error_msg
);
2049 temp_var
->type
= vt_alias
;
2050 temp_var
->alias_binding
= parse_string (inst
, vc_head
, Program
, &found
);
2051 Program
->Position
= parse_position (inst
);
2055 char *error_msg
= (char *)
2056 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2057 _mesa_sprintf (error_msg
, "Alias value %s is not defined",
2058 temp_var
->alias_binding
->name
);
2059 program_error (ctx
, Program
->Position
, error_msg
);
2060 _mesa_free (error_msg
);
2068 * This handles variables of the ADDRESS kind
2070 * \return 0 on sucess, 1 on error
2073 parse_address (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2074 struct arb_program
*Program
)
2077 struct var_cache
*temp_var
;
2079 while (**inst
!= 0) {
2080 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2081 Program
->Position
= parse_position (inst
);
2083 char *error_msg
= (char *)
2084 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2085 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
2087 program_error (ctx
, Program
->Position
, error_msg
);
2088 _mesa_free (error_msg
);
2092 temp_var
->type
= vt_address
;
2094 if (Program
->Base
.NumAddressRegs
>=
2095 ctx
->Const
.VertexProgram
.MaxAddressRegs
) {
2096 const char *msg
= "Too many ADDRESS variables declared";
2097 program_error(ctx
, Program
->Position
, msg
);
2101 temp_var
->address_binding
= Program
->Base
.NumAddressRegs
;
2102 Program
->Base
.NumAddressRegs
++;
2110 * Parse a program declaration
2112 * \return 0 on sucess, 1 on error
2115 parse_declaration (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2116 struct arb_program
*Program
)
2120 switch (*(*inst
)++) {
2122 err
= parse_address (ctx
, inst
, vc_head
, Program
);
2126 err
= parse_alias (ctx
, inst
, vc_head
, Program
);
2130 err
= parse_attrib (ctx
, inst
, vc_head
, Program
);
2134 err
= parse_output (ctx
, inst
, vc_head
, Program
);
2138 err
= parse_param (ctx
, inst
, vc_head
, Program
);
2142 err
= parse_temp (ctx
, inst
, vc_head
, Program
);
2150 * Handle the parsing out of a masked destination register, either for a
2151 * vertex or fragment program.
2153 * If we are a vertex program, make sure we don't write to
2154 * result.position if we have specified that the program is
2155 * position invariant
2157 * \param File - The register file we write to
2158 * \param Index - The register index we write to
2159 * \param WriteMask - The mask controlling which components we write (1->write)
2161 * \return 0 on sucess, 1 on error
2164 parse_masked_dst_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2165 struct var_cache
**vc_head
, struct arb_program
*Program
,
2166 enum register_file
*File
, GLuint
*Index
, GLint
*WriteMask
)
2169 struct var_cache
*dst
;
2171 /* We either have a result register specified, or a
2172 * variable that may or may not be writable
2174 switch (*(*inst
)++) {
2175 case REGISTER_RESULT
:
2176 if (parse_result_binding(ctx
, inst
, Index
, Program
))
2178 *File
= PROGRAM_OUTPUT
;
2181 case REGISTER_ESTABLISHED_NAME
:
2182 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2183 Program
->Position
= parse_position (inst
);
2185 /* If the name has never been added to our symbol table, we're hosed */
2187 program_error(ctx
, Program
->Position
, "0: Undefined variable");
2191 switch (dst
->type
) {
2193 *File
= PROGRAM_OUTPUT
;
2194 *Index
= dst
->output_binding
;
2198 *File
= PROGRAM_TEMPORARY
;
2199 *Index
= dst
->temp_binding
;
2202 /* If the var type is not vt_output or vt_temp, no go */
2204 program_error(ctx
, Program
->Position
,
2205 "Destination register is read only");
2211 program_error(ctx
, Program
->Position
,
2212 "Unexpected opcode in parse_masked_dst_reg()");
2217 /* Position invariance test */
2218 /* This test is done now in syntax portion - when position invariance OPTION
2219 is specified, "result.position" rule is disabled so there is no way
2220 to write the position
2222 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2224 program_error(ctx, Program->Position,
2225 "Vertex program specified position invariance and wrote vertex position");
2228 /* And then the mask.
2234 * ==> Need to reverse the order of bits for this!
2236 tmp
= (GLint
) *(*inst
)++;
2237 *WriteMask
= (((tmp
>>3) & 0x1) |
2247 * Handle the parsing of a address register
2249 * \param Index - The register index we write to
2251 * \return 0 on sucess, 1 on error
2254 parse_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2255 struct var_cache
**vc_head
,
2256 struct arb_program
*Program
, GLint
* Index
)
2258 struct var_cache
*dst
;
2261 *Index
= 0; /* XXX */
2263 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2264 Program
->Position
= parse_position (inst
);
2266 /* If the name has never been added to our symbol table, we're hosed */
2268 program_error(ctx
, Program
->Position
, "Undefined variable");
2272 if (dst
->type
!= vt_address
) {
2273 program_error(ctx
, Program
->Position
, "Variable is not of type ADDRESS");
2282 * Handle the parsing out of a masked address register
2284 * \param Index - The register index we write to
2285 * \param WriteMask - The mask controlling which components we write (1->write)
2287 * \return 0 on sucess, 1 on error
2290 parse_masked_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2291 struct var_cache
**vc_head
,
2292 struct arb_program
*Program
, GLint
* Index
,
2293 GLboolean
* WriteMask
)
2295 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, Index
))
2298 /* This should be 0x8 */
2301 /* Writemask of .x is implied */
2303 WriteMask
[1] = WriteMask
[2] = WriteMask
[3] = 0;
2310 * Parse out a swizzle mask.
2312 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
2314 * The len parameter allows us to grab 4 components for a vector
2315 * swizzle, or just 1 component for a scalar src register selection
2318 parse_swizzle_mask(const GLubyte
** inst
, GLubyte
*swizzle
, GLint len
)
2322 for (i
= 0; i
< 4; i
++)
2325 for (i
= 0; i
< len
; i
++) {
2326 switch (*(*inst
)++) {
2328 swizzle
[i
] = SWIZZLE_X
;
2331 swizzle
[i
] = SWIZZLE_Y
;
2334 swizzle
[i
] = SWIZZLE_Z
;
2337 swizzle
[i
] = SWIZZLE_W
;
2340 _mesa_problem(NULL
, "bad component in parse_swizzle_mask()");
2348 * Parse an extended swizzle mask which is a sequence of
2349 * four x/y/z/w/0/1 tokens.
2350 * \return swizzle four swizzle values
2351 * \return negateMask four element bitfield
2354 parse_extended_swizzle_mask(const GLubyte
**inst
, GLubyte swizzle
[4],
2355 GLubyte
*negateMask
)
2360 for (i
= 0; i
< 4; i
++) {
2362 if (parse_sign(inst
) == -1)
2363 *negateMask
|= (1 << i
);
2369 swizzle
[i
] = SWIZZLE_ZERO
;
2372 swizzle
[i
] = SWIZZLE_ONE
;
2375 swizzle
[i
] = SWIZZLE_X
;
2378 swizzle
[i
] = SWIZZLE_Y
;
2381 swizzle
[i
] = SWIZZLE_Z
;
2384 swizzle
[i
] = SWIZZLE_W
;
2387 _mesa_problem(NULL
, "bad case in parse_extended_swizzle_mask()");
2395 parse_src_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2396 struct var_cache
**vc_head
,
2397 struct arb_program
*Program
,
2398 enum register_file
* File
, GLint
* Index
,
2399 GLboolean
*IsRelOffset
)
2401 struct var_cache
*src
;
2402 GLuint binding
, is_generic
, found
;
2407 /* And the binding for the src */
2408 switch (*(*inst
)++) {
2409 case REGISTER_ATTRIB
:
2410 if (parse_attrib_binding
2411 (ctx
, inst
, Program
, &binding
, &is_generic
))
2413 *File
= PROGRAM_INPUT
;
2416 /* We need to insert a dummy variable into the var_cache so we can
2417 * catch generic vertex attrib aliasing errors
2419 var_cache_create(&src
);
2420 src
->type
= vt_attrib
;
2421 src
->name
= (const GLubyte
*) "Dummy Attrib Variable";
2422 src
->attrib_binding
= binding
;
2423 src
->attrib_is_generic
= is_generic
;
2424 var_cache_append(vc_head
, src
);
2425 if (generic_attrib_check(*vc_head
)) {
2426 program_error(ctx
, Program
->Position
,
2427 "Cannot use both a generic vertex attribute "
2428 "and a specific attribute of the same type");
2433 case REGISTER_PARAM
:
2435 case PARAM_ARRAY_ELEMENT
:
2437 src
= parse_string (inst
, vc_head
, Program
, &found
);
2438 Program
->Position
= parse_position (inst
);
2441 program_error(ctx
, Program
->Position
,
2442 "2: Undefined variable"); /* src->name */
2446 *File
= (enum register_file
) src
->param_binding_type
;
2448 switch (*(*inst
)++) {
2449 case ARRAY_INDEX_ABSOLUTE
:
2450 offset
= parse_integer (inst
, Program
);
2453 || (offset
>= (int)src
->param_binding_length
)) {
2454 program_error(ctx
, Program
->Position
,
2455 "Index out of range");
2456 /* offset, src->name */
2460 *Index
= src
->param_binding_begin
+ offset
;
2463 case ARRAY_INDEX_RELATIVE
:
2465 GLint addr_reg_idx
, rel_off
;
2467 /* First, grab the address regiseter */
2468 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &addr_reg_idx
))
2477 /* Then the relative offset */
2478 if (parse_relative_offset(ctx
, inst
, Program
, &rel_off
)) return 1;
2480 /* And store it properly */
2481 *Index
= src
->param_binding_begin
+ rel_off
;
2489 if (parse_param_use (ctx
, inst
, vc_head
, Program
, &src
))
2492 *File
= (enum register_file
) src
->param_binding_type
;
2493 *Index
= src
->param_binding_begin
;
2498 case REGISTER_ESTABLISHED_NAME
:
2499 src
= parse_string (inst
, vc_head
, Program
, &found
);
2500 Program
->Position
= parse_position (inst
);
2502 /* If the name has never been added to our symbol table, we're hosed */
2504 program_error(ctx
, Program
->Position
,
2505 "3: Undefined variable"); /* src->name */
2509 switch (src
->type
) {
2511 *File
= PROGRAM_INPUT
;
2512 *Index
= src
->attrib_binding
;
2515 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2517 *File
= (enum register_file
) src
->param_binding_type
;
2518 *Index
= src
->param_binding_begin
;
2522 *File
= PROGRAM_TEMPORARY
;
2523 *Index
= src
->temp_binding
;
2526 /* If the var type is vt_output no go */
2528 program_error(ctx
, Program
->Position
,
2529 "destination register is read only");
2536 program_error(ctx
, Program
->Position
,
2537 "Unknown token in parse_src_reg");
2545 * Parse fragment program vector source register.
2548 parse_fp_vector_src_reg(GLcontext
* ctx
, const GLubyte
** inst
,
2549 struct var_cache
**vc_head
,
2550 struct arb_program
*program
,
2551 struct prog_src_register
*reg
)
2553 enum register_file file
;
2557 GLboolean isRelOffset
;
2560 negate
= (parse_sign (inst
) == -1) ? 0xf : 0x0;
2562 /* And the src reg */
2563 if (parse_src_reg(ctx
, inst
, vc_head
, program
, &file
, &index
, &isRelOffset
))
2566 /* finally, the swizzle */
2567 parse_swizzle_mask(inst
, swizzle
, 4);
2571 reg
->NegateBase
= negate
;
2572 reg
->Swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
2578 * Parse fragment program destination register.
2579 * \return 1 if error, 0 if no error.
2582 parse_fp_dst_reg(GLcontext
* ctx
, const GLubyte
** inst
,
2583 struct var_cache
**vc_head
, struct arb_program
*Program
,
2584 struct prog_dst_register
*reg
)
2588 enum register_file file
;
2590 if (parse_masked_dst_reg (ctx
, inst
, vc_head
, Program
, &file
, &idx
, &mask
))
2595 reg
->WriteMask
= mask
;
2601 * Parse fragment program scalar src register.
2602 * \return 1 if error, 0 if no error.
2605 parse_fp_scalar_src_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2606 struct var_cache
**vc_head
,
2607 struct arb_program
*Program
,
2608 struct prog_src_register
*reg
)
2610 enum register_file File
;
2614 GLboolean IsRelOffset
;
2617 Negate
= (parse_sign (inst
) == -1) ? 0x1 : 0x0;
2619 /* And the src reg */
2620 if (parse_src_reg (ctx
, inst
, vc_head
, Program
, &File
, &Index
, &IsRelOffset
))
2623 /* finally, the swizzle */
2624 parse_swizzle_mask(inst
, Swizzle
, 1);
2628 reg
->NegateBase
= Negate
;
2629 reg
->Swizzle
= (Swizzle
[0] << 0);
2636 * This is a big mother that handles getting opcodes into the instruction
2637 * and handling the src & dst registers for fragment program instructions
2638 * \return 1 if error, 0 if no error
2641 parse_fp_instruction (GLcontext
* ctx
, const GLubyte
** inst
,
2642 struct var_cache
**vc_head
, struct arb_program
*Program
,
2643 struct prog_instruction
*fp
)
2647 GLubyte instClass
, type
, code
;
2650 _mesa_init_instructions(fp
, 1);
2652 /* Record the position in the program string for debugging */
2653 fp
->StringPos
= Program
->Position
;
2655 /* OP_ALU_INST or OP_TEX_INST */
2656 instClass
= *(*inst
)++;
2658 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2659 * OP_TEX_{SAMPLE, KIL}
2663 /* The actual opcode name */
2666 /* Increment the correct count */
2667 switch (instClass
) {
2669 Program
->NumAluInstructions
++;
2672 Program
->NumTexInstructions
++;
2680 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2682 fp
->Opcode
= OPCODE_ABS
;
2686 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2688 fp
->Opcode
= OPCODE_FLR
;
2692 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2694 fp
->Opcode
= OPCODE_FRC
;
2698 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2700 fp
->Opcode
= OPCODE_LIT
;
2704 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2706 fp
->Opcode
= OPCODE_MOV
;
2710 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2713 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2720 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2722 fp
->Opcode
= OPCODE_COS
;
2726 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2728 fp
->Opcode
= OPCODE_EX2
;
2732 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2734 fp
->Opcode
= OPCODE_LG2
;
2738 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2740 fp
->Opcode
= OPCODE_RCP
;
2744 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2746 fp
->Opcode
= OPCODE_RSQ
;
2750 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2752 fp
->Opcode
= OPCODE_SIN
;
2756 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2759 fp
->Opcode
= OPCODE_SCS
;
2763 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2766 if (parse_fp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2773 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2775 fp
->Opcode
= OPCODE_POW
;
2779 if (parse_fp_dst_reg(ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2782 for (a
= 0; a
< 2; a
++) {
2783 if (parse_fp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2792 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2794 fp
->Opcode
= OPCODE_ADD
;
2798 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2800 fp
->Opcode
= OPCODE_DP3
;
2804 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2806 fp
->Opcode
= OPCODE_DP4
;
2810 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2812 fp
->Opcode
= OPCODE_DPH
;
2816 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2818 fp
->Opcode
= OPCODE_DST
;
2822 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2824 fp
->Opcode
= OPCODE_MAX
;
2828 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2830 fp
->Opcode
= OPCODE_MIN
;
2834 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2836 fp
->Opcode
= OPCODE_MUL
;
2840 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2842 fp
->Opcode
= OPCODE_SGE
;
2846 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2848 fp
->Opcode
= OPCODE_SLT
;
2852 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2854 fp
->Opcode
= OPCODE_SUB
;
2858 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2860 fp
->Opcode
= OPCODE_XPD
;
2864 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2866 for (a
= 0; a
< 2; a
++) {
2867 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2875 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2877 fp
->Opcode
= OPCODE_CMP
;
2881 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2883 fp
->Opcode
= OPCODE_LRP
;
2887 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2889 fp
->Opcode
= OPCODE_MAD
;
2893 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2896 for (a
= 0; a
< 3; a
++) {
2897 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2905 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2907 fp
->Opcode
= OPCODE_SWZ
;
2910 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2916 enum register_file file
;
2919 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
, &rel
))
2921 parse_extended_swizzle_mask(inst
, swizzle
, &negateMask
);
2922 fp
->SrcReg
[0].File
= file
;
2923 fp
->SrcReg
[0].Index
= index
;
2924 fp
->SrcReg
[0].NegateBase
= negateMask
;
2925 fp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
2935 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2937 fp
->Opcode
= OPCODE_TEX
;
2941 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2943 fp
->Opcode
= OPCODE_TXP
;
2947 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2949 fp
->Opcode
= OPCODE_TXB
;
2953 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2956 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2960 if (parse_texcoord_num (ctx
, inst
, Program
, &texcoord
))
2962 fp
->TexSrcUnit
= texcoord
;
2965 switch (*(*inst
)++) {
2967 fp
->TexSrcTarget
= TEXTURE_1D_INDEX
;
2970 fp
->TexSrcTarget
= TEXTURE_2D_INDEX
;
2973 fp
->TexSrcTarget
= TEXTURE_3D_INDEX
;
2975 case TEXTARGET_RECT
:
2976 fp
->TexSrcTarget
= TEXTURE_RECT_INDEX
;
2978 case TEXTARGET_CUBE
:
2979 fp
->TexSrcTarget
= TEXTURE_CUBE_INDEX
;
2981 case TEXTARGET_SHADOW1D
:
2982 case TEXTARGET_SHADOW2D
:
2983 case TEXTARGET_SHADOWRECT
:
2984 /* TODO ARB_fragment_program_shadow code */
2987 Program
->TexturesUsed
[texcoord
] |= (1 << fp
->TexSrcTarget
);
2988 /* Check that both "2D" and "CUBE" (for example) aren't both used */
2989 if (_mesa_bitcount(Program
->TexturesUsed
[texcoord
]) > 1) {
2990 program_error(ctx
, Program
->Position
,
2991 "multiple targets used on one texture image unit");
2997 Program
->UsesKill
= 1;
2998 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
3000 fp
->Opcode
= OPCODE_KIL
;
3003 _mesa_problem(ctx
, "bad type 0x%x in parse_fp_instruction()", type
);
3011 parse_vp_dst_reg(GLcontext
* ctx
, const GLubyte
** inst
,
3012 struct var_cache
**vc_head
, struct arb_program
*Program
,
3013 struct prog_dst_register
*reg
)
3017 enum register_file file
;
3019 if (parse_masked_dst_reg(ctx
, inst
, vc_head
, Program
, &file
, &idx
, &mask
))
3024 reg
->WriteMask
= mask
;
3029 * Handle the parsing out of a masked address register
3031 * \param Index - The register index we write to
3032 * \param WriteMask - The mask controlling which components we write (1->write)
3034 * \return 0 on sucess, 1 on error
3037 parse_vp_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
3038 struct var_cache
**vc_head
,
3039 struct arb_program
*Program
,
3040 struct prog_dst_register
*reg
)
3044 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &idx
))
3047 /* This should be 0x8 */
3050 reg
->File
= PROGRAM_ADDRESS
;
3053 /* Writemask of .x is implied */
3054 reg
->WriteMask
= 0x1;
3059 * Parse vertex program vector source register.
3062 parse_vp_vector_src_reg(GLcontext
* ctx
, const GLubyte
** inst
,
3063 struct var_cache
**vc_head
,
3064 struct arb_program
*program
,
3065 struct prog_src_register
*reg
)
3067 enum register_file file
;
3071 GLboolean isRelOffset
;
3074 negateMask
= (parse_sign (inst
) == -1) ? 0xf : 0x0;
3076 /* And the src reg */
3077 if (parse_src_reg (ctx
, inst
, vc_head
, program
, &file
, &index
, &isRelOffset
))
3080 /* finally, the swizzle */
3081 parse_swizzle_mask(inst
, swizzle
, 4);
3085 reg
->Swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1],
3086 swizzle
[2], swizzle
[3]);
3087 reg
->NegateBase
= negateMask
;
3088 reg
->RelAddr
= isRelOffset
;
3094 parse_vp_scalar_src_reg (GLcontext
* ctx
, const GLubyte
** inst
,
3095 struct var_cache
**vc_head
,
3096 struct arb_program
*Program
,
3097 struct prog_src_register
*reg
)
3099 enum register_file File
;
3103 GLboolean IsRelOffset
;
3106 Negate
= (parse_sign (inst
) == -1) ? 0x1 : 0x0;
3108 /* And the src reg */
3109 if (parse_src_reg (ctx
, inst
, vc_head
, Program
, &File
, &Index
, &IsRelOffset
))
3112 /* finally, the swizzle */
3113 parse_swizzle_mask(inst
, Swizzle
, 1);
3117 reg
->Swizzle
= (Swizzle
[0] << 0);
3118 reg
->NegateBase
= Negate
;
3119 reg
->RelAddr
= IsRelOffset
;
3125 * This is a big mother that handles getting opcodes into the instruction
3126 * and handling the src & dst registers for vertex program instructions
3129 parse_vp_instruction (GLcontext
* ctx
, const GLubyte
** inst
,
3130 struct var_cache
**vc_head
, struct arb_program
*Program
,
3131 struct prog_instruction
*vp
)
3136 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3139 /* The actual opcode name */
3142 _mesa_init_instructions(vp
, 1);
3143 /* Record the position in the program string for debugging */
3144 vp
->StringPos
= Program
->Position
;
3149 vp
->Opcode
= OPCODE_ARL
;
3151 /* Remember to set SrcReg.RelAddr; */
3153 /* Get the masked address register [dst] */
3154 if (parse_vp_address_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3157 vp
->DstReg
.File
= PROGRAM_ADDRESS
;
3159 /* Get a scalar src register */
3160 if (parse_vp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3168 vp
->Opcode
= OPCODE_ABS
;
3171 vp
->Opcode
= OPCODE_FLR
;
3174 vp
->Opcode
= OPCODE_FRC
;
3177 vp
->Opcode
= OPCODE_LIT
;
3180 vp
->Opcode
= OPCODE_MOV
;
3184 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3187 if (parse_vp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3194 vp
->Opcode
= OPCODE_EX2
;
3197 vp
->Opcode
= OPCODE_EXP
;
3200 vp
->Opcode
= OPCODE_LG2
;
3203 vp
->Opcode
= OPCODE_LOG
;
3206 vp
->Opcode
= OPCODE_RCP
;
3209 vp
->Opcode
= OPCODE_RSQ
;
3212 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3215 if (parse_vp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3222 vp
->Opcode
= OPCODE_POW
;
3225 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3228 for (a
= 0; a
< 2; a
++) {
3229 if (parse_vp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3237 vp
->Opcode
= OPCODE_ADD
;
3240 vp
->Opcode
= OPCODE_DP3
;
3243 vp
->Opcode
= OPCODE_DP4
;
3246 vp
->Opcode
= OPCODE_DPH
;
3249 vp
->Opcode
= OPCODE_DST
;
3252 vp
->Opcode
= OPCODE_MAX
;
3255 vp
->Opcode
= OPCODE_MIN
;
3258 vp
->Opcode
= OPCODE_MUL
;
3261 vp
->Opcode
= OPCODE_SGE
;
3264 vp
->Opcode
= OPCODE_SLT
;
3267 vp
->Opcode
= OPCODE_SUB
;
3270 vp
->Opcode
= OPCODE_XPD
;
3273 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3276 for (a
= 0; a
< 2; a
++) {
3277 if (parse_vp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3285 vp
->Opcode
= OPCODE_MAD
;
3289 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3292 for (a
= 0; a
< 3; a
++) {
3293 if (parse_vp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3301 vp
->Opcode
= OPCODE_SWZ
;
3308 enum register_file file
;
3311 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3314 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
, &relAddr
))
3316 parse_extended_swizzle_mask (inst
, swizzle
, &negateMask
);
3317 vp
->SrcReg
[0].File
= file
;
3318 vp
->SrcReg
[0].Index
= index
;
3319 vp
->SrcReg
[0].NegateBase
= negateMask
;
3320 vp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
3324 vp
->SrcReg
[0].RelAddr
= relAddr
;
3334 print_state_token (GLint token
)
3337 case STATE_MATERIAL
:
3338 fprintf (stderr
, "STATE_MATERIAL ");
3341 fprintf (stderr
, "STATE_LIGHT ");
3344 case STATE_LIGHTMODEL_AMBIENT
:
3345 fprintf (stderr
, "STATE_AMBIENT ");
3348 case STATE_LIGHTMODEL_SCENECOLOR
:
3349 fprintf (stderr
, "STATE_SCENECOLOR ");
3352 case STATE_LIGHTPROD
:
3353 fprintf (stderr
, "STATE_LIGHTPROD ");
3357 fprintf (stderr
, "STATE_TEXGEN ");
3360 case STATE_FOG_COLOR
:
3361 fprintf (stderr
, "STATE_FOG_COLOR ");
3364 case STATE_FOG_PARAMS
:
3365 fprintf (stderr
, "STATE_FOG_PARAMS ");
3368 case STATE_CLIPPLANE
:
3369 fprintf (stderr
, "STATE_CLIPPLANE ");
3372 case STATE_POINT_SIZE
:
3373 fprintf (stderr
, "STATE_POINT_SIZE ");
3376 case STATE_POINT_ATTENUATION
:
3377 fprintf (stderr
, "STATE_ATTENUATION ");
3381 fprintf (stderr
, "STATE_MATRIX ");
3384 case STATE_MODELVIEW
:
3385 fprintf (stderr
, "STATE_MODELVIEW ");
3388 case STATE_PROJECTION
:
3389 fprintf (stderr
, "STATE_PROJECTION ");
3393 fprintf (stderr
, "STATE_MVP ");
3397 fprintf (stderr
, "STATE_TEXTURE ");
3401 fprintf (stderr
, "STATE_PROGRAM ");
3404 case STATE_MATRIX_INVERSE
:
3405 fprintf (stderr
, "STATE_INVERSE ");
3408 case STATE_MATRIX_TRANSPOSE
:
3409 fprintf (stderr
, "STATE_TRANSPOSE ");
3412 case STATE_MATRIX_INVTRANS
:
3413 fprintf (stderr
, "STATE_INVTRANS ");
3417 fprintf (stderr
, "STATE_AMBIENT ");
3421 fprintf (stderr
, "STATE_DIFFUSE ");
3424 case STATE_SPECULAR
:
3425 fprintf (stderr
, "STATE_SPECULAR ");
3428 case STATE_EMISSION
:
3429 fprintf (stderr
, "STATE_EMISSION ");
3432 case STATE_SHININESS
:
3433 fprintf (stderr
, "STATE_SHININESS ");
3437 fprintf (stderr
, "STATE_HALF ");
3440 case STATE_POSITION
:
3441 fprintf (stderr
, "STATE_POSITION ");
3444 case STATE_ATTENUATION
:
3445 fprintf (stderr
, "STATE_ATTENUATION ");
3448 case STATE_SPOT_DIRECTION
:
3449 fprintf (stderr
, "STATE_DIRECTION ");
3452 case STATE_TEXGEN_EYE_S
:
3453 fprintf (stderr
, "STATE_TEXGEN_EYE_S ");
3456 case STATE_TEXGEN_EYE_T
:
3457 fprintf (stderr
, "STATE_TEXGEN_EYE_T ");
3460 case STATE_TEXGEN_EYE_R
:
3461 fprintf (stderr
, "STATE_TEXGEN_EYE_R ");
3464 case STATE_TEXGEN_EYE_Q
:
3465 fprintf (stderr
, "STATE_TEXGEN_EYE_Q ");
3468 case STATE_TEXGEN_OBJECT_S
:
3469 fprintf (stderr
, "STATE_TEXGEN_EYE_S ");
3472 case STATE_TEXGEN_OBJECT_T
:
3473 fprintf (stderr
, "STATE_TEXGEN_OBJECT_T ");
3476 case STATE_TEXGEN_OBJECT_R
:
3477 fprintf (stderr
, "STATE_TEXGEN_OBJECT_R ");
3480 case STATE_TEXGEN_OBJECT_Q
:
3481 fprintf (stderr
, "STATE_TEXGEN_OBJECT_Q ");
3484 case STATE_TEXENV_COLOR
:
3485 fprintf (stderr
, "STATE_TEXENV_COLOR ");
3488 case STATE_DEPTH_RANGE
:
3489 fprintf (stderr
, "STATE_DEPTH_RANGE ");
3492 case STATE_VERTEX_PROGRAM
:
3493 fprintf (stderr
, "STATE_VERTEX_PROGRAM ");
3496 case STATE_FRAGMENT_PROGRAM
:
3497 fprintf (stderr
, "STATE_FRAGMENT_PROGRAM ");
3501 fprintf (stderr
, "STATE_ENV ");
3505 fprintf (stderr
, "STATE_LOCAL ");
3509 fprintf (stderr
, "[%d] ", token
);
3514 debug_variables (GLcontext
* ctx
, struct var_cache
*vc_head
,
3515 struct arb_program
*Program
)
3517 struct var_cache
*vc
;
3520 fprintf (stderr
, "debug_variables, vc_head: %x\n", vc_head
);
3522 /* First of all, print out the contents of the var_cache */
3525 fprintf (stderr
, "[%x]\n", vc
);
3528 fprintf (stderr
, "UNDEFINED %s\n", vc
->name
);
3531 fprintf (stderr
, "ATTRIB %s\n", vc
->name
);
3532 fprintf (stderr
, " binding: 0x%x\n", vc
->attrib_binding
);
3535 fprintf (stderr
, "PARAM %s begin: %d len: %d\n", vc
->name
,
3536 vc
->param_binding_begin
, vc
->param_binding_length
);
3537 b
= vc
->param_binding_begin
;
3538 for (a
= 0; a
< vc
->param_binding_length
; a
++) {
3539 fprintf (stderr
, "%s\n",
3540 Program
->Parameters
->Parameters
[a
+ b
].Name
);
3541 if (Program
->Parameters
->Parameters
[a
+ b
].Type
== STATE
) {
3542 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3544 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3546 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3548 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3550 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3552 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3556 fprintf (stderr
, "%f %f %f %f\n",
3557 Program
->Parameters
->Parameters
[a
+ b
].Values
[0],
3558 Program
->Parameters
->Parameters
[a
+ b
].Values
[1],
3559 Program
->Parameters
->Parameters
[a
+ b
].Values
[2],
3560 Program
->Parameters
->Parameters
[a
+ b
].Values
[3]);
3564 fprintf (stderr
, "TEMP %s\n", vc
->name
);
3565 fprintf (stderr
, " binding: 0x%x\n", vc
->temp_binding
);
3568 fprintf (stderr
, "OUTPUT %s\n", vc
->name
);
3569 fprintf (stderr
, " binding: 0x%x\n", vc
->output_binding
);
3572 fprintf (stderr
, "ALIAS %s\n", vc
->name
);
3573 fprintf (stderr
, " binding: 0x%x (%s)\n",
3574 vc
->alias_binding
, vc
->alias_binding
->name
);
3581 #endif /* DEBUG_PARSING */
3585 * The main loop for parsing a fragment or vertex program
3587 * \return 1 on error, 0 on success
3590 parse_instructions(GLcontext
* ctx
, const GLubyte
* inst
,
3591 struct var_cache
**vc_head
, struct arb_program
*Program
)
3593 const GLuint maxInst
= (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
3594 ? ctx
->Const
.FragmentProgram
.MaxInstructions
3595 : ctx
->Const
.VertexProgram
.MaxInstructions
;
3598 ASSERT(MAX_INSTRUCTIONS
>= maxInst
);
3600 Program
->MajorVersion
= (GLuint
) * inst
++;
3601 Program
->MinorVersion
= (GLuint
) * inst
++;
3603 while (*inst
!= END
) {
3608 case ARB_PRECISION_HINT_FASTEST
:
3609 Program
->PrecisionOption
= GL_FASTEST
;
3612 case ARB_PRECISION_HINT_NICEST
:
3613 Program
->PrecisionOption
= GL_NICEST
;
3617 Program
->FogOption
= GL_EXP
;
3621 Program
->FogOption
= GL_EXP2
;
3624 case ARB_FOG_LINEAR
:
3625 Program
->FogOption
= GL_LINEAR
;
3628 case ARB_POSITION_INVARIANT
:
3629 if (Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
3630 Program
->HintPositionInvariant
= GL_TRUE
;
3633 case ARB_FRAGMENT_PROGRAM_SHADOW
:
3634 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3635 /* TODO ARB_fragment_program_shadow code */
3639 case ARB_DRAW_BUFFERS
:
3640 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3641 /* do nothing for now */
3649 if (Program
->Base
.NumInstructions
+ 1 >= maxInst
) {
3650 program_error(ctx
, Program
->Position
,
3651 "Max instruction count exceeded");
3654 Program
->Position
= parse_position (&inst
);
3655 /* parse the current instruction */
3656 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3657 err
= parse_fp_instruction (ctx
, &inst
, vc_head
, Program
,
3658 &Program
->Base
.Instructions
[Program
->Base
.NumInstructions
]);
3661 err
= parse_vp_instruction (ctx
, &inst
, vc_head
, Program
,
3662 &Program
->Base
.Instructions
[Program
->Base
.NumInstructions
]);
3665 /* increment instuction count */
3666 Program
->Base
.NumInstructions
++;
3670 err
= parse_declaration (ctx
, &inst
, vc_head
, Program
);
3681 /* Finally, tag on an OPCODE_END instruction */
3683 const GLuint numInst
= Program
->Base
.NumInstructions
;
3684 _mesa_init_instructions(Program
->Base
.Instructions
+ numInst
, 1);
3685 Program
->Base
.Instructions
[numInst
].Opcode
= OPCODE_END
;
3686 /* YYY Wrong Position in program, whatever, at least not random -> crash
3687 Program->Position = parse_position (&inst);
3689 Program
->Base
.Instructions
[numInst
].StringPos
= Program
->Position
;
3691 Program
->Base
.NumInstructions
++;
3694 * Initialize native counts to logical counts. The device driver may
3695 * change them if program is translated into a hardware program.
3697 Program
->Base
.NumNativeInstructions
= Program
->Base
.NumInstructions
;
3698 Program
->Base
.NumNativeTemporaries
= Program
->Base
.NumTemporaries
;
3699 Program
->Base
.NumNativeParameters
= Program
->Base
.NumParameters
;
3700 Program
->Base
.NumNativeAttributes
= Program
->Base
.NumAttributes
;
3701 Program
->Base
.NumNativeAddressRegs
= Program
->Base
.NumAddressRegs
;
3708 __extension__
static char core_grammar_text
[] =
3709 #include "grammar_syn.h"
3714 * Set a grammar parameter.
3715 * \param name the grammar parameter
3716 * \param value the new parameter value
3717 * \return 0 if OK, 1 if error
3720 set_reg8 (GLcontext
*ctx
, grammar id
, const char *name
, GLubyte value
)
3722 char error_msg
[300];
3725 if (grammar_set_reg8 (id
, (const byte
*) name
, value
))
3728 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3729 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3730 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Grammar Register Error");
3736 * Enable support for the given language option in the parser.
3737 * \return 1 if OK, 0 if error
3740 enable_ext(GLcontext
*ctx
, grammar id
, const char *name
)
3742 return !set_reg8(ctx
, id
, name
, 1);
3747 * Enable parser extensions based on which OpenGL extensions are supported
3748 * by this rendering context.
3750 * \return GL_TRUE if OK, GL_FALSE if error.
3753 enable_parser_extensions(GLcontext
*ctx
, grammar id
)
3756 /* These are not supported at this time */
3757 if ((ctx
->Extensions
.ARB_vertex_blend
||
3758 ctx
->Extensions
.EXT_vertex_weighting
)
3759 && !enable_ext(ctx
, id
, "vertex_blend"))
3761 if (ctx
->Extensions
.ARB_matrix_palette
3762 && !enable_ext(ctx
, id
, "matrix_palette"))
3764 if (ctx
->Extensions
.ARB_fragment_program_shadow
3765 && !enable_ext(ctx
, id
, "fragment_program_shadow"))
3768 if (ctx
->Extensions
.EXT_point_parameters
3769 && !enable_ext(ctx
, id
, "point_parameters"))
3771 if (ctx
->Extensions
.EXT_secondary_color
3772 && !enable_ext(ctx
, id
, "secondary_color"))
3774 if (ctx
->Extensions
.EXT_fog_coord
3775 && !enable_ext(ctx
, id
, "fog_coord"))
3777 if (ctx
->Extensions
.NV_texture_rectangle
3778 && !enable_ext(ctx
, id
, "texture_rectangle"))
3780 if (ctx
->Extensions
.ARB_draw_buffers
3781 && !enable_ext(ctx
, id
, "draw_buffers"))
3785 /* hack for Warcraft (see bug 8060) */
3786 enable_ext(ctx
, id
, "vertex_blend");
3794 * This kicks everything off.
3796 * \param ctx - The GL Context
3797 * \param str - The program string
3798 * \param len - The program string length
3799 * \param program - The arb_program struct to return all the parsed info in
3800 * \return GL_TRUE on sucess, GL_FALSE on error
3803 _mesa_parse_arb_program(GLcontext
*ctx
, GLenum target
,
3804 const GLubyte
*str
, GLsizei len
,
3805 struct arb_program
*program
)
3807 GLint a
, err
, error_pos
;
3808 char error_msg
[300];
3810 struct var_cache
*vc_head
;
3811 grammar arbprogram_syn_id
;
3812 GLubyte
*parsed
, *inst
;
3813 GLubyte
*strz
= NULL
;
3814 static int arbprogram_syn_is_ok
= 0; /* XXX temporary */
3816 /* set the program target before parsing */
3817 program
->Base
.Target
= target
;
3819 /* Reset error state */
3820 _mesa_set_program_error(ctx
, -1, NULL
);
3822 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
3823 if (!arbprogram_syn_is_ok
) {
3824 /* One-time initialization of parsing system */
3825 grammar grammar_syn_id
;
3828 grammar_syn_id
= grammar_load_from_text ((byte
*) core_grammar_text
);
3829 if (grammar_syn_id
== 0) {
3830 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3831 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3832 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3833 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3834 "glProgramStringARB(Error loading grammar rule set)");
3838 err
= !grammar_check(grammar_syn_id
, (byte
*) arb_grammar_text
,
3839 &parsed
, &parsed_len
);
3841 /* 'parsed' is unused here */
3842 _mesa_free (parsed
);
3845 /* NOTE: we can't destroy grammar_syn_id right here because
3846 * grammar_destroy() can reset the last error
3849 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3850 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3851 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3852 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3853 "glProgramString(Error loading grammar rule set");
3854 grammar_destroy (grammar_syn_id
);
3858 grammar_destroy (grammar_syn_id
);
3860 arbprogram_syn_is_ok
= 1;
3863 /* create the grammar object */
3864 arbprogram_syn_id
= grammar_load_from_text ((byte
*) arb_grammar_text
);
3865 if (arbprogram_syn_id
== 0) {
3866 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3867 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
3868 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3869 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3870 "glProgramString(Error loading grammer rule set)");
3874 /* Set program_target register value */
3875 if (set_reg8 (ctx
, arbprogram_syn_id
, "program_target",
3876 program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
? 0x10 : 0x20)) {
3877 grammar_destroy (arbprogram_syn_id
);
3881 if (!enable_parser_extensions(ctx
, arbprogram_syn_id
)) {
3882 grammar_destroy(arbprogram_syn_id
);
3886 /* check for NULL character occurences */
3889 for (i
= 0; i
< len
; i
++) {
3890 if (str
[i
] == '\0') {
3891 program_error(ctx
, i
, "illegal character");
3892 grammar_destroy (arbprogram_syn_id
);
3898 /* copy the program string to a null-terminated string */
3899 strz
= (GLubyte
*) _mesa_malloc (len
+ 1);
3901 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glProgramStringARB");
3902 grammar_destroy (arbprogram_syn_id
);
3905 _mesa_memcpy (strz
, str
, len
);
3908 /* do a fast check on program string - initial production buffer is 4K */
3909 err
= !grammar_fast_check(arbprogram_syn_id
, strz
,
3910 &parsed
, &parsed_len
, 0x1000);
3912 /* Syntax parse error */
3914 grammar_get_last_error((GLubyte
*) error_msg
, 300, &error_pos
);
3915 program_error(ctx
, error_pos
, error_msg
);
3918 /* useful for debugging */
3922 fprintf(stderr
, "program: %s\n", (char *) strz
);
3923 fprintf(stderr
, "Error Pos: %d\n", ctx
->program
.ErrorPos
);
3924 s
= (char *) _mesa_find_line_column(strz
, strz
+ctx
->program
.ErrorPos
,
3926 fprintf(stderr
, "line %d col %d: %s\n", line
, col
, s
);
3933 grammar_destroy (arbprogram_syn_id
);
3937 grammar_destroy (arbprogram_syn_id
);
3940 * Program string is syntactically correct at this point
3941 * Parse the tokenized version of the program now, generating
3942 * vertex/fragment program instructions.
3945 /* Initialize the arb_program struct */
3946 program
->Base
.String
= strz
;
3947 program
->Base
.Instructions
= _mesa_alloc_instructions(MAX_INSTRUCTIONS
);
3948 program
->Base
.NumInstructions
=
3949 program
->Base
.NumTemporaries
=
3950 program
->Base
.NumParameters
=
3951 program
->Base
.NumAttributes
= program
->Base
.NumAddressRegs
= 0;
3952 program
->Base
.Parameters
= _mesa_new_parameter_list ();
3953 program
->Base
.InputsRead
= 0x0;
3954 program
->Base
.OutputsWritten
= 0x0;
3955 program
->Position
= 0;
3956 program
->MajorVersion
= program
->MinorVersion
= 0;
3957 program
->PrecisionOption
= GL_DONT_CARE
;
3958 program
->FogOption
= GL_NONE
;
3959 program
->HintPositionInvariant
= GL_FALSE
;
3960 for (a
= 0; a
< MAX_TEXTURE_IMAGE_UNITS
; a
++)
3961 program
->TexturesUsed
[a
] = 0x0;
3962 program
->NumAluInstructions
=
3963 program
->NumTexInstructions
=
3964 program
->NumTexIndirections
= 0;
3965 program
->UsesKill
= 0;
3970 /* Start examining the tokens in the array */
3973 /* Check the grammer rev */
3974 if (*inst
++ != REVISION
) {
3975 program_error (ctx
, 0, "Grammar version mismatch");
3979 /* ignore program target */
3981 err
= parse_instructions(ctx
, inst
, &vc_head
, program
);
3984 /*debug_variables(ctx, vc_head, program); */
3986 /* We're done with the parsed binary array */
3987 var_cache_destroy (&vc_head
);
3989 _mesa_free (parsed
);
3991 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
3992 * to size [ap.Base.NumInstructions].
3994 program
->Base
.Instructions
3995 = _mesa_realloc_instructions(program
->Base
.Instructions
,
3997 program
->Base
.NumInstructions
);
4005 _mesa_parse_arb_fragment_program(GLcontext
* ctx
, GLenum target
,
4006 const GLvoid
*str
, GLsizei len
,
4007 struct gl_fragment_program
*program
)
4009 struct arb_program ap
;
4012 ASSERT(target
== GL_FRAGMENT_PROGRAM_ARB
);
4013 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
, &ap
)) {
4014 /* Error in the program. Just return. */
4018 /* Copy the relevant contents of the arb_program struct into the
4019 * fragment_program struct.
4021 program
->Base
.String
= ap
.Base
.String
;
4022 program
->Base
.NumInstructions
= ap
.Base
.NumInstructions
;
4023 program
->Base
.NumTemporaries
= ap
.Base
.NumTemporaries
;
4024 program
->Base
.NumParameters
= ap
.Base
.NumParameters
;
4025 program
->Base
.NumAttributes
= ap
.Base
.NumAttributes
;
4026 program
->Base
.NumAddressRegs
= ap
.Base
.NumAddressRegs
;
4027 program
->Base
.NumNativeInstructions
= ap
.Base
.NumNativeInstructions
;
4028 program
->Base
.NumNativeTemporaries
= ap
.Base
.NumNativeTemporaries
;
4029 program
->Base
.NumNativeParameters
= ap
.Base
.NumNativeParameters
;
4030 program
->Base
.NumNativeAttributes
= ap
.Base
.NumNativeAttributes
;
4031 program
->Base
.NumNativeAddressRegs
= ap
.Base
.NumNativeAddressRegs
;
4032 program
->NumAluInstructions
= ap
.NumAluInstructions
;
4033 program
->NumTexInstructions
= ap
.NumTexInstructions
;
4034 program
->NumTexIndirections
= ap
.NumTexIndirections
;
4035 program
->NumNativeAluInstructions
= ap
.NumAluInstructions
;
4036 program
->NumNativeTexInstructions
= ap
.NumTexInstructions
;
4037 program
->NumNativeTexIndirections
= ap
.NumTexIndirections
;
4038 program
->Base
.InputsRead
= ap
.Base
.InputsRead
;
4039 program
->Base
.OutputsWritten
= ap
.Base
.OutputsWritten
;
4040 for (i
= 0; i
< MAX_TEXTURE_IMAGE_UNITS
; i
++)
4041 program
->TexturesUsed
[i
] = ap
.TexturesUsed
[i
];
4042 program
->FogOption
= ap
.FogOption
;
4044 if (program
->Base
.Instructions
)
4045 _mesa_free(program
->Base
.Instructions
);
4046 program
->Base
.Instructions
= ap
.Base
.Instructions
;
4048 if (program
->Base
.Parameters
)
4049 _mesa_free_parameter_list(program
->Base
.Parameters
);
4050 program
->Base
.Parameters
= ap
.Base
.Parameters
;
4053 _mesa_printf("____________Fragment program %u ________\n", program
->Base
.ID
);
4054 _mesa_print_program(&program
->Base
);
4061 * Parse the vertex program string. If success, update the given
4062 * vertex_program object with the new program. Else, leave the vertex_program
4066 _mesa_parse_arb_vertex_program(GLcontext
*ctx
, GLenum target
,
4067 const GLvoid
*str
, GLsizei len
,
4068 struct gl_vertex_program
*program
)
4070 struct arb_program ap
;
4072 ASSERT(target
== GL_VERTEX_PROGRAM_ARB
);
4074 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
, &ap
)) {
4075 /* Error in the program. Just return. */
4079 /* Copy the relevant contents of the arb_program struct into the
4080 * vertex_program struct.
4082 program
->Base
.String
= ap
.Base
.String
;
4083 program
->Base
.NumInstructions
= ap
.Base
.NumInstructions
;
4084 program
->Base
.NumTemporaries
= ap
.Base
.NumTemporaries
;
4085 program
->Base
.NumParameters
= ap
.Base
.NumParameters
;
4086 program
->Base
.NumAttributes
= ap
.Base
.NumAttributes
;
4087 program
->Base
.NumAddressRegs
= ap
.Base
.NumAddressRegs
;
4088 program
->Base
.NumNativeInstructions
= ap
.Base
.NumNativeInstructions
;
4089 program
->Base
.NumNativeTemporaries
= ap
.Base
.NumNativeTemporaries
;
4090 program
->Base
.NumNativeParameters
= ap
.Base
.NumNativeParameters
;
4091 program
->Base
.NumNativeAttributes
= ap
.Base
.NumNativeAttributes
;
4092 program
->Base
.NumNativeAddressRegs
= ap
.Base
.NumNativeAddressRegs
;
4093 program
->Base
.InputsRead
= ap
.Base
.InputsRead
;
4094 program
->Base
.OutputsWritten
= ap
.Base
.OutputsWritten
;
4095 program
->IsPositionInvariant
= ap
.HintPositionInvariant
;
4097 if (program
->Base
.Instructions
)
4098 _mesa_free(program
->Base
.Instructions
);
4099 program
->Base
.Instructions
= ap
.Base
.Instructions
;
4101 if (program
->Base
.Parameters
)
4102 _mesa_free_parameter_list(program
->Base
.Parameters
);
4103 program
->Base
.Parameters
= ap
.Base
.Parameters
;
4106 _mesa_printf("____________Vertex program %u __________\n", program
->Base
.ID
);
4107 _mesa_print_program(&program
->Base
);