2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #define DEBUG_PARSING 0
28 * \file arbprogparse.c
29 * ARB_*_program parser core
33 #include "main/glheader.h"
34 #include "main/imports.h"
35 #include "shader/grammar/grammar_mesa.h"
36 #include "arbprogparse.h"
38 #include "programopt.h"
39 #include "prog_parameter.h"
40 #include "prog_statevars.h"
44 #include "prog_instruction.h"
47 /* For ARB programs, use the NV instruction limits */
48 #define MAX_INSTRUCTIONS MAX2(MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS, \
49 MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS)
53 * This is basically a union of the vertex_program and fragment_program
54 * structs that we can use to parse the program into
56 * XXX we can probably get rid of this entirely someday.
60 struct gl_program Base
;
62 GLuint Position
; /* Just used for error reporting while parsing */
66 /* ARB_vertex_progmra options */
67 GLboolean HintPositionInvariant
;
69 /* ARB_fragment_progmra options */
70 GLenum PrecisionOption
; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
71 GLenum FogOption
; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
73 /* ARB_fragment_program specifics */
74 GLbitfield TexturesUsed
[MAX_TEXTURE_IMAGE_UNITS
];
75 GLbitfield ShadowSamplers
;
76 GLuint NumAluInstructions
;
77 GLuint NumTexInstructions
;
78 GLuint NumTexIndirections
;
86 * Fragment Program Stuff:
87 * -----------------------------------------------------
89 * - things from Michal's email
91 * + not-overflowing floats (don't use parse_integer..)
92 * + can remove range checking in arbparse.c
94 * - check all limits of number of various variables
99 * Vertex Program Stuff:
100 * -----------------------------------------------------
101 * - Optimize param array usage and count limits correctly, see spec,
103 * + Record if an array is reference absolutly or relatively (or both)
104 * + For absolute arrays, store a bitmap of accesses
105 * + For single parameters, store an access flag
106 * + After parsing, make a parameter cleanup and merging pass, where
107 * relative arrays are layed out first, followed by abs arrays, and
108 * finally single state.
109 * + Remap offsets for param src and dst registers
110 * + Now we can properly count parameter usage
112 * - Multiple state binding errors in param arrays (see spec, just before
117 * -----------------------------------------------------
118 * - User clipping planes vs. PositionInvariant
119 * - Is it sufficient to just multiply by the mvp to transform in the
120 * PositionInvariant case? Or do we need something more involved?
122 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
123 * - fetch state listed in program_parameters list
124 * + WTF should this go???
125 * + currently in nvvertexec.c and s_nvfragprog.c
127 * - allow for multiple address registers (and fetch address regs properly)
130 * -----------------------------------------------------
131 * - remove any leftover unused grammer.c stuff (dict_ ?)
132 * - fix grammer.c error handling so its not static
133 * - #ifdef around stuff pertaining to extentions
135 * Outstanding Questions:
136 * -----------------------------------------------------
137 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
138 * what gets hacked off because of this:
139 * + VERTEX_ATTRIB_MATRIXINDEX
140 * + VERTEX_ATTRIB_WEIGHT
144 * - When can we fetch env/local params from their own register files, and
145 * when to we have to fetch them into the main state register file?
149 * -----------------------------------------------------
152 /* Changes since moving the file to shader directory
154 2004-III-4 ------------------------------------------------------------
155 - added #include "grammar_mesa.h"
156 - removed grammar specific code part (it resides now in grammar.c)
157 - added GL_ARB_fragment_program_shadow tokens
158 - modified #include "arbparse_syn.h"
159 - major changes inside _mesa_parse_arb_program()
160 - check the program string for '\0' characters
161 - copy the program string to a one-byte-longer location to have
163 - position invariance test (not writing to result.position) moved
167 typedef GLubyte
*production
;
171 * This is the text describing the rules to parse the grammar
173 LONGSTRING
static char arb_grammar_text
[] =
174 #include "arbprogram_syn.h"
178 * These should match up with the values defined in arbprogram.syn
183 - changed and merged V_* and F_* opcode values to OP_*.
184 - added GL_ARB_fragment_program_shadow specific tokens (michal)
186 #define REVISION 0x0a
189 #define FRAGMENT_PROGRAM 0x01
190 #define VERTEX_PROGRAM 0x02
192 /* program section */
194 #define INSTRUCTION 0x02
195 #define DECLARATION 0x03
198 /* GL_ARB_fragment_program option */
199 #define ARB_PRECISION_HINT_FASTEST 0x00
200 #define ARB_PRECISION_HINT_NICEST 0x01
201 #define ARB_FOG_EXP 0x02
202 #define ARB_FOG_EXP2 0x03
203 #define ARB_FOG_LINEAR 0x04
205 /* GL_ARB_vertex_program option */
206 #define ARB_POSITION_INVARIANT 0x05
208 /* GL_ARB_fragment_program_shadow option */
209 #define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
211 /* GL_ARB_draw_buffers option */
212 #define ARB_DRAW_BUFFERS 0x07
214 /* GL_MESA_texture_array option */
215 #define MESA_TEXTURE_ARRAY 0x08
217 /* GL_ARB_fragment_program instruction class */
218 #define OP_ALU_INST 0x00
219 #define OP_TEX_INST 0x01
221 /* GL_ARB_vertex_program instruction class */
224 /* GL_ARB_fragment_program instruction type */
225 #define OP_ALU_VECTOR 0x00
226 #define OP_ALU_SCALAR 0x01
227 #define OP_ALU_BINSC 0x02
228 #define OP_ALU_BIN 0x03
229 #define OP_ALU_TRI 0x04
230 #define OP_ALU_SWZ 0x05
231 #define OP_TEX_SAMPLE 0x06
232 #define OP_TEX_KIL 0x07
234 /* GL_ARB_vertex_program instruction type */
235 #define OP_ALU_ARL 0x08
243 /* GL_ARB_fragment_program instruction code */
245 #define OP_ABS_SAT 0x1B
247 #define OP_FLR_SAT 0x26
249 #define OP_FRC_SAT 0x27
251 #define OP_LIT_SAT 0x2A
253 #define OP_MOV_SAT 0x30
255 #define OP_COS_SAT 0x20
257 #define OP_EX2_SAT 0x25
259 #define OP_LG2_SAT 0x29
261 #define OP_RCP_SAT 0x33
263 #define OP_RSQ_SAT 0x34
265 #define OP_SIN_SAT 0x39
267 #define OP_SCS_SAT 0x36
269 #define OP_POW_SAT 0x32
271 #define OP_ADD_SAT 0x1C
273 #define OP_DP3_SAT 0x21
275 #define OP_DP4_SAT 0x22
277 #define OP_DPH_SAT 0x23
279 #define OP_DST_SAT 0x24
281 #define OP_MAX_SAT 0x2E
283 #define OP_MIN_SAT 0x2F
285 #define OP_MUL_SAT 0x31
287 #define OP_SGE_SAT 0x37
289 #define OP_SLT_SAT 0x3A
291 #define OP_SUB_SAT 0x3B
293 #define OP_XPD_SAT 0x43
295 #define OP_CMP_SAT 0x1E
297 #define OP_LRP_SAT 0x2C
299 #define OP_MAD_SAT 0x2D
301 #define OP_SWZ_SAT 0x3C
303 #define OP_TEX_SAT 0x3E
305 #define OP_TXB_SAT 0x40
307 #define OP_TXP_SAT 0x42
310 /* GL_ARB_vertex_program instruction code */
339 /* fragment attribute binding */
340 #define FRAGMENT_ATTRIB_COLOR 0x01
341 #define FRAGMENT_ATTRIB_TEXCOORD 0x02
342 #define FRAGMENT_ATTRIB_FOGCOORD 0x03
343 #define FRAGMENT_ATTRIB_POSITION 0x04
345 /* vertex attribute binding */
346 #define VERTEX_ATTRIB_POSITION 0x01
347 #define VERTEX_ATTRIB_WEIGHT 0x02
348 #define VERTEX_ATTRIB_NORMAL 0x03
349 #define VERTEX_ATTRIB_COLOR 0x04
350 #define VERTEX_ATTRIB_FOGCOORD 0x05
351 #define VERTEX_ATTRIB_TEXCOORD 0x06
352 #define VERTEX_ATTRIB_MATRIXINDEX 0x07
353 #define VERTEX_ATTRIB_GENERIC 0x08
355 /* fragment result binding */
356 #define FRAGMENT_RESULT_COLOR 0x01
357 #define FRAGMENT_RESULT_DEPTH 0x02
359 /* vertex result binding */
360 #define VERTEX_RESULT_POSITION 0x01
361 #define VERTEX_RESULT_COLOR 0x02
362 #define VERTEX_RESULT_FOGCOORD 0x03
363 #define VERTEX_RESULT_POINTSIZE 0x04
364 #define VERTEX_RESULT_TEXCOORD 0x05
367 #define TEXTARGET_1D 0x01
368 #define TEXTARGET_2D 0x02
369 #define TEXTARGET_3D 0x03
370 #define TEXTARGET_RECT 0x04
371 #define TEXTARGET_CUBE 0x05
372 /* GL_ARB_fragment_program_shadow */
373 #define TEXTARGET_SHADOW1D 0x06
374 #define TEXTARGET_SHADOW2D 0x07
375 #define TEXTARGET_SHADOWRECT 0x08
376 /* GL_MESA_texture_array */
377 #define TEXTARGET_1D_ARRAY 0x09
378 #define TEXTARGET_2D_ARRAY 0x0a
379 #define TEXTARGET_SHADOW1D_ARRAY 0x0b
380 #define TEXTARGET_SHADOW2D_ARRAY 0x0c
383 #define FACE_FRONT 0x00
384 #define FACE_BACK 0x01
387 #define COLOR_PRIMARY 0x00
388 #define COLOR_SECONDARY 0x01
391 #define COMPONENT_X 0x00
392 #define COMPONENT_Y 0x01
393 #define COMPONENT_Z 0x02
394 #define COMPONENT_W 0x03
395 #define COMPONENT_0 0x04
396 #define COMPONENT_1 0x05
398 /* array index type */
399 #define ARRAY_INDEX_ABSOLUTE 0x00
400 #define ARRAY_INDEX_RELATIVE 0x01
403 #define MATRIX_MODELVIEW 0x01
404 #define MATRIX_PROJECTION 0x02
405 #define MATRIX_MVP 0x03
406 #define MATRIX_TEXTURE 0x04
407 #define MATRIX_PALETTE 0x05
408 #define MATRIX_PROGRAM 0x06
410 /* matrix modifier */
411 #define MATRIX_MODIFIER_IDENTITY 0x00
412 #define MATRIX_MODIFIER_INVERSE 0x01
413 #define MATRIX_MODIFIER_TRANSPOSE 0x02
414 #define MATRIX_MODIFIER_INVTRANS 0x03
417 #define CONSTANT_SCALAR 0x01
418 #define CONSTANT_VECTOR 0x02
420 /* program param type */
421 #define PROGRAM_PARAM_ENV 0x01
422 #define PROGRAM_PARAM_LOCAL 0x02
425 #define REGISTER_ATTRIB 0x01
426 #define REGISTER_PARAM 0x02
427 #define REGISTER_RESULT 0x03
428 #define REGISTER_ESTABLISHED_NAME 0x04
431 #define PARAM_NULL 0x00
432 #define PARAM_ARRAY_ELEMENT 0x01
433 #define PARAM_STATE_ELEMENT 0x02
434 #define PARAM_PROGRAM_ELEMENT 0x03
435 #define PARAM_PROGRAM_ELEMENTS 0x04
436 #define PARAM_CONSTANT 0x05
438 /* param state property */
439 #define STATE_MATERIAL_PARSER 0x01
440 #define STATE_LIGHT_PARSER 0x02
441 #define STATE_LIGHT_MODEL 0x03
442 #define STATE_LIGHT_PROD 0x04
443 #define STATE_FOG 0x05
444 #define STATE_MATRIX_ROWS 0x06
445 /* GL_ARB_fragment_program */
446 #define STATE_TEX_ENV 0x07
447 #define STATE_DEPTH 0x08
448 /* GL_ARB_vertex_program */
449 #define STATE_TEX_GEN 0x09
450 #define STATE_CLIP_PLANE 0x0A
451 #define STATE_POINT 0x0B
453 /* state material property */
454 #define MATERIAL_AMBIENT 0x01
455 #define MATERIAL_DIFFUSE 0x02
456 #define MATERIAL_SPECULAR 0x03
457 #define MATERIAL_EMISSION 0x04
458 #define MATERIAL_SHININESS 0x05
460 /* state light property */
461 #define LIGHT_AMBIENT 0x01
462 #define LIGHT_DIFFUSE 0x02
463 #define LIGHT_SPECULAR 0x03
464 #define LIGHT_POSITION 0x04
465 #define LIGHT_ATTENUATION 0x05
466 #define LIGHT_HALF 0x06
467 #define LIGHT_SPOT_DIRECTION 0x07
469 /* state light model property */
470 #define LIGHT_MODEL_AMBIENT 0x01
471 #define LIGHT_MODEL_SCENECOLOR 0x02
473 /* state light product property */
474 #define LIGHT_PROD_AMBIENT 0x01
475 #define LIGHT_PROD_DIFFUSE 0x02
476 #define LIGHT_PROD_SPECULAR 0x03
478 /* state texture environment property */
479 #define TEX_ENV_COLOR 0x01
481 /* state texture generation coord property */
482 #define TEX_GEN_EYE 0x01
483 #define TEX_GEN_OBJECT 0x02
485 /* state fog property */
486 #define FOG_COLOR 0x01
487 #define FOG_PARAMS 0x02
489 /* state depth property */
490 #define DEPTH_RANGE 0x01
492 /* state point parameters property */
493 #define POINT_SIZE 0x01
494 #define POINT_ATTENUATION 0x02
502 /* GL_ARB_vertex_program */
505 /*-----------------------------------------------------------------------
506 * From here on down is the semantic checking portion
511 * Variable Table Handling functions
526 * Setting an explicit field for each of the binding properties is a bit
527 * wasteful of space, but it should be much more clear when reading later on..
531 const GLubyte
*name
; /* don't free() - no need */
533 GLuint address_binding
; /* The index of the address register we should
535 GLuint attrib_binding
; /* For type vt_attrib, see nvfragprog.h for values */
536 GLuint attrib_is_generic
; /* If the attrib was specified through a generic
538 GLuint temp_binding
; /* The index of the temp register we are to use */
539 GLuint output_binding
; /* Output/result register number */
540 struct var_cache
*alias_binding
; /* For type vt_alias, points to the var_cache entry
541 * that this is aliased to */
542 GLuint param_binding_type
; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
543 * PROGRAM_ENV_PARAM} */
544 GLuint param_binding_begin
; /* This is the offset into the program_parameter_list where
545 * the tokens representing our bound state (or constants)
547 GLuint param_binding_length
; /* This is how many entries in the the program_parameter_list
548 * we take up with our state tokens or constants. Note that
549 * this is _not_ the same as the number of param registers
550 * we eventually use */
551 struct var_cache
*next
;
555 var_cache_create (struct var_cache
**va
)
557 *va
= (struct var_cache
*) _mesa_malloc (sizeof (struct var_cache
));
560 (**va
).type
= vt_none
;
561 (**va
).attrib_binding
= ~0;
562 (**va
).attrib_is_generic
= 0;
563 (**va
).temp_binding
= ~0;
564 (**va
).output_binding
= ~0;
565 (**va
).param_binding_type
= ~0;
566 (**va
).param_binding_begin
= ~0;
567 (**va
).param_binding_length
= ~0;
568 (**va
).alias_binding
= NULL
;
574 var_cache_destroy (struct var_cache
**va
)
577 var_cache_destroy (&(**va
).next
);
584 var_cache_append (struct var_cache
**va
, struct var_cache
*nv
)
587 var_cache_append (&(**va
).next
, nv
);
592 static struct var_cache
*
593 var_cache_find (struct var_cache
*va
, const GLubyte
* name
)
595 /*struct var_cache *first = va;*/
598 if (!_mesa_strcmp ( (const char*) name
, (const char*) va
->name
)) {
599 if (va
->type
== vt_alias
)
600 return va
->alias_binding
;
613 * Called when an error is detected while parsing/compiling a program.
614 * Sets the ctx->Program.ErrorString field to descript and records a
615 * GL_INVALID_OPERATION error.
616 * \param position position of error in program string
617 * \param descrip verbose error description
620 program_error(GLcontext
*ctx
, GLint position
, const char *descrip
)
623 const char *prefix
= "glProgramString(", *suffix
= ")";
624 char *str
= (char *) _mesa_malloc(_mesa_strlen(descrip
) +
625 _mesa_strlen(prefix
) +
626 _mesa_strlen(suffix
) + 1);
628 _mesa_sprintf(str
, "%s%s%s", prefix
, descrip
, suffix
);
629 _mesa_error(ctx
, GL_INVALID_OPERATION
, str
);
633 _mesa_set_program_error(ctx
, position
, descrip
);
638 * As above, but with an extra string parameter for more info.
641 program_error2(GLcontext
*ctx
, GLint position
, const char *descrip
,
645 const char *prefix
= "glProgramString(", *suffix
= ")";
646 char *str
= (char *) _mesa_malloc(_mesa_strlen(descrip
) +
649 _mesa_strlen(prefix
) +
650 _mesa_strlen(suffix
) + 1);
652 _mesa_sprintf(str
, "%s%s: %s%s", prefix
, descrip
, var
, suffix
);
653 _mesa_error(ctx
, GL_INVALID_OPERATION
, str
);
658 char *str
= (char *) _mesa_malloc(_mesa_strlen(descrip
) +
660 _mesa_strlen(var
) + 1);
662 _mesa_sprintf(str
, "%s: %s", descrip
, var
);
664 _mesa_set_program_error(ctx
, position
, str
);
674 * constructs an integer from 4 GLubytes in LE format
677 parse_position (const GLubyte
** inst
)
681 value
= (GLuint
) (*(*inst
)++);
682 value
+= (GLuint
) (*(*inst
)++) * 0x100;
683 value
+= (GLuint
) (*(*inst
)++) * 0x10000;
684 value
+= (GLuint
) (*(*inst
)++) * 0x1000000;
690 * This will, given a string, lookup the string as a variable name in the
691 * var cache. If the name is found, the var cache node corresponding to the
692 * var name is returned. If it is not found, a new entry is allocated
694 * \param I Points into the binary array where the string identifier begins
695 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
696 * \return The location on the var_cache corresponding the the string starting at I
698 static struct var_cache
*
699 parse_string (const GLubyte
** inst
, struct var_cache
**vc_head
,
700 struct arb_program
*Program
, GLuint
* found
)
702 const GLubyte
*i
= *inst
;
703 struct var_cache
*va
= NULL
;
706 *inst
+= _mesa_strlen ((char *) i
) + 1;
708 va
= var_cache_find (*vc_head
, i
);
716 var_cache_create (&va
);
717 va
->name
= (const GLubyte
*) i
;
719 var_cache_append (vc_head
, va
);
725 parse_string_without_adding (const GLubyte
** inst
, struct arb_program
*Program
)
727 const GLubyte
*i
= *inst
;
730 *inst
+= _mesa_strlen ((char *) i
) + 1;
736 * \return -1 if we parse '-', return 1 otherwise
739 parse_sign (const GLubyte
** inst
)
741 /*return *(*inst)++ != '+'; */
747 else if (**inst
== '+') {
756 * parses and returns signed integer
759 parse_integer (const GLubyte
** inst
, struct arb_program
*Program
)
764 /* check if *inst points to '+' or '-'
765 * if yes, grab the sign and increment *inst
767 sign
= parse_sign (inst
);
769 /* now check if *inst points to 0
770 * if yes, increment the *inst and return the default value
777 /* parse the integer as you normally would do it */
778 value
= _mesa_atoi (parse_string_without_adding (inst
, Program
));
780 /* now, after terminating 0 there is a position
781 * to parse it - parse_position()
783 Program
->Position
= parse_position (inst
);
789 Accumulate this string of digits, and return them as
790 a large integer represented in floating point (for range).
791 If scale is not NULL, also accumulates a power-of-ten
792 integer scale factor that represents the number of digits
796 parse_float_string(const GLubyte
** inst
, struct arb_program
*Program
, GLdouble
*scale
)
798 GLdouble value
= 0.0;
799 GLdouble oscale
= 1.0;
801 if (**inst
== 0) { /* this string of digits is empty-- do nothing */
804 else { /* nonempty string-- parse out the digits */
805 while (**inst
>= '0' && **inst
<= '9') {
806 GLubyte digit
= *((*inst
)++);
807 value
= value
* 10.0 + (GLint
) (digit
- '0');
810 assert(**inst
== 0); /* integer string should end with 0 */
811 (*inst
)++; /* skip over terminating 0 */
812 Program
->Position
= parse_position(inst
); /* skip position (from integer) */
820 Parse an unsigned floating-point number from this stream of tokenized
821 characters. Example floating-point formats supported:
829 parse_float (const GLubyte
** inst
, struct arb_program
*Program
)
832 GLdouble whole
, fraction
, fracScale
= 1.0;
834 whole
= parse_float_string(inst
, Program
, 0);
835 fraction
= parse_float_string(inst
, Program
, &fracScale
);
837 /* Parse signed exponent */
838 exponent
= parse_integer(inst
, Program
); /* This is the exponent */
840 /* Assemble parts of floating-point number: */
841 return (GLfloat
) ((whole
+ fraction
/ fracScale
) *
842 _mesa_pow(10.0, (GLfloat
) exponent
));
849 parse_signed_float (const GLubyte
** inst
, struct arb_program
*Program
)
851 GLint sign
= parse_sign (inst
);
852 GLfloat value
= parse_float (inst
, Program
);
857 * This picks out a constant value from the parsed array. The constant vector is r
858 * returned in the *values array, which should be of length 4.
860 * \param values - The 4 component vector with the constant value in it
863 parse_constant (const GLubyte
** inst
, GLfloat
*values
, struct arb_program
*Program
,
866 GLuint components
, i
;
869 switch (*(*inst
)++) {
870 case CONSTANT_SCALAR
:
871 if (use
== GL_TRUE
) {
874 values
[2] = values
[3] = parse_float (inst
, Program
);
879 values
[2] = values
[3] = parse_signed_float (inst
, Program
);
883 case CONSTANT_VECTOR
:
884 values
[0] = values
[1] = values
[2] = 0;
886 components
= *(*inst
)++;
887 for (i
= 0; i
< components
; i
++) {
888 values
[i
] = parse_signed_float (inst
, Program
);
895 * \param offset The offset from the address register that we should
898 * \return 0 on sucess, 1 on error
901 parse_relative_offset(GLcontext
*ctx
, const GLubyte
**inst
,
902 struct arb_program
*Program
, GLint
*offset
)
905 *offset
= parse_integer(inst
, Program
);
910 * \param color 0 if color type is primary, 1 if color type is secondary
911 * \return 0 on sucess, 1 on error
914 parse_color_type (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
917 (void) ctx
; (void) Program
;
918 *color
= *(*inst
)++ != COLOR_PRIMARY
;
923 * Get an integer corresponding to a generic vertex attribute.
925 * \return 0 on sucess, 1 on error
928 parse_generic_attrib_num(GLcontext
*ctx
, const GLubyte
** inst
,
929 struct arb_program
*Program
, GLuint
*attrib
)
931 GLint i
= parse_integer(inst
, Program
);
933 if ((i
< 0) || (i
>= MAX_VERTEX_PROGRAM_ATTRIBS
))
935 program_error(ctx
, Program
->Position
,
936 "Invalid generic vertex attribute index");
940 *attrib
= (GLuint
) i
;
947 * \param color The index of the color buffer to write into
948 * \return 0 on sucess, 1 on error
951 parse_output_color_num (GLcontext
* ctx
, const GLubyte
** inst
,
952 struct arb_program
*Program
, GLuint
* color
)
954 GLint i
= parse_integer (inst
, Program
);
956 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxDrawBuffers
)) {
957 program_error(ctx
, Program
->Position
, "Invalid draw buffer index");
967 * \param coord The texture unit index
968 * \return 0 on sucess, 1 on error
971 parse_texcoord_num (GLcontext
* ctx
, const GLubyte
** inst
,
972 struct arb_program
*Program
, GLuint
* coord
)
974 GLint i
= parse_integer (inst
, Program
);
976 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxTextureUnits
)) {
977 program_error(ctx
, Program
->Position
, "Invalid texture unit index");
986 * \param coord The weight index
987 * \return 0 on sucess, 1 on error
990 parse_weight_num (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
993 *coord
= parse_integer (inst
, Program
);
995 if ((*coord
< 0) || (*coord
>= 1)) {
996 program_error(ctx
, Program
->Position
, "Invalid weight index");
1004 * \param coord The clip plane index
1005 * \return 0 on sucess, 1 on error
1008 parse_clipplane_num (GLcontext
* ctx
, const GLubyte
** inst
,
1009 struct arb_program
*Program
, GLint
* coord
)
1011 *coord
= parse_integer (inst
, Program
);
1013 if ((*coord
< 0) || (*coord
>= (GLint
) ctx
->Const
.MaxClipPlanes
)) {
1014 program_error(ctx
, Program
->Position
, "Invalid clip plane index");
1023 * \return 0 on front face, 1 on back face
1026 parse_face_type (const GLubyte
** inst
)
1028 switch (*(*inst
)++) {
1040 * Given a matrix and a modifier token on the binary array, return tokens
1041 * that _mesa_fetch_state() [program.c] can understand.
1043 * \param matrix - the matrix we are talking about
1044 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
1045 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
1046 * \return 0 on sucess, 1 on failure
1049 parse_matrix (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
1050 GLint
* matrix
, GLint
* matrix_idx
, GLint
* matrix_modifier
)
1052 GLubyte mat
= *(*inst
)++;
1057 case MATRIX_MODELVIEW
:
1058 *matrix
= STATE_MODELVIEW_MATRIX
;
1059 *matrix_idx
= parse_integer (inst
, Program
);
1060 if (*matrix_idx
> 0) {
1061 program_error(ctx
, Program
->Position
,
1062 "ARB_vertex_blend not supported");
1067 case MATRIX_PROJECTION
:
1068 *matrix
= STATE_PROJECTION_MATRIX
;
1072 *matrix
= STATE_MVP_MATRIX
;
1075 case MATRIX_TEXTURE
:
1076 *matrix
= STATE_TEXTURE_MATRIX
;
1077 *matrix_idx
= parse_integer (inst
, Program
);
1078 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxTextureUnits
) {
1079 program_error(ctx
, Program
->Position
, "Invalid Texture Unit");
1080 /* bad *matrix_id */
1085 /* This is not currently supported (ARB_matrix_palette) */
1086 case MATRIX_PALETTE
:
1087 *matrix_idx
= parse_integer (inst
, Program
);
1088 program_error(ctx
, Program
->Position
,
1089 "ARB_matrix_palette not supported");
1093 case MATRIX_PROGRAM
:
1094 *matrix
= STATE_PROGRAM_MATRIX
;
1095 *matrix_idx
= parse_integer (inst
, Program
);
1096 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxProgramMatrices
) {
1097 program_error(ctx
, Program
->Position
, "Invalid Program Matrix");
1098 /* bad *matrix_idx */
1104 switch (*(*inst
)++) {
1105 case MATRIX_MODIFIER_IDENTITY
:
1106 *matrix_modifier
= 0;
1108 case MATRIX_MODIFIER_INVERSE
:
1109 *matrix_modifier
= STATE_MATRIX_INVERSE
;
1111 case MATRIX_MODIFIER_TRANSPOSE
:
1112 *matrix_modifier
= STATE_MATRIX_TRANSPOSE
;
1114 case MATRIX_MODIFIER_INVTRANS
:
1115 *matrix_modifier
= STATE_MATRIX_INVTRANS
;
1124 * This parses a state string (rather, the binary version of it) into
1125 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1127 * \param inst - the start in the binary arry to start working from
1128 * \param state_tokens - the storage for the 6-token state description
1129 * \return - 0 on sucess, 1 on error
1132 parse_state_single_item (GLcontext
* ctx
, const GLubyte
** inst
,
1133 struct arb_program
*Program
,
1134 gl_state_index state_tokens
[STATE_LENGTH
])
1136 switch (*(*inst
)++) {
1137 case STATE_MATERIAL_PARSER
:
1138 state_tokens
[0] = STATE_MATERIAL
;
1139 state_tokens
[1] = parse_face_type (inst
);
1140 switch (*(*inst
)++) {
1141 case MATERIAL_AMBIENT
:
1142 state_tokens
[2] = STATE_AMBIENT
;
1144 case MATERIAL_DIFFUSE
:
1145 state_tokens
[2] = STATE_DIFFUSE
;
1147 case MATERIAL_SPECULAR
:
1148 state_tokens
[2] = STATE_SPECULAR
;
1150 case MATERIAL_EMISSION
:
1151 state_tokens
[2] = STATE_EMISSION
;
1153 case MATERIAL_SHININESS
:
1154 state_tokens
[2] = STATE_SHININESS
;
1159 case STATE_LIGHT_PARSER
:
1160 state_tokens
[0] = STATE_LIGHT
;
1161 state_tokens
[1] = parse_integer (inst
, Program
);
1163 /* Check the value of state_tokens[1] against the # of lights */
1164 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1165 program_error(ctx
, Program
->Position
, "Invalid Light Number");
1166 /* bad state_tokens[1] */
1170 switch (*(*inst
)++) {
1172 state_tokens
[2] = STATE_AMBIENT
;
1175 state_tokens
[2] = STATE_DIFFUSE
;
1177 case LIGHT_SPECULAR
:
1178 state_tokens
[2] = STATE_SPECULAR
;
1180 case LIGHT_POSITION
:
1181 state_tokens
[2] = STATE_POSITION
;
1183 case LIGHT_ATTENUATION
:
1184 state_tokens
[2] = STATE_ATTENUATION
;
1187 state_tokens
[2] = STATE_HALF_VECTOR
;
1189 case LIGHT_SPOT_DIRECTION
:
1190 state_tokens
[2] = STATE_SPOT_DIRECTION
;
1195 case STATE_LIGHT_MODEL
:
1196 switch (*(*inst
)++) {
1197 case LIGHT_MODEL_AMBIENT
:
1198 state_tokens
[0] = STATE_LIGHTMODEL_AMBIENT
;
1200 case LIGHT_MODEL_SCENECOLOR
:
1201 state_tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
1202 state_tokens
[1] = parse_face_type (inst
);
1207 case STATE_LIGHT_PROD
:
1208 state_tokens
[0] = STATE_LIGHTPROD
;
1209 state_tokens
[1] = parse_integer (inst
, Program
);
1211 /* Check the value of state_tokens[1] against the # of lights */
1212 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1213 program_error(ctx
, Program
->Position
, "Invalid Light Number");
1214 /* bad state_tokens[1] */
1218 state_tokens
[2] = parse_face_type (inst
);
1219 switch (*(*inst
)++) {
1220 case LIGHT_PROD_AMBIENT
:
1221 state_tokens
[3] = STATE_AMBIENT
;
1223 case LIGHT_PROD_DIFFUSE
:
1224 state_tokens
[3] = STATE_DIFFUSE
;
1226 case LIGHT_PROD_SPECULAR
:
1227 state_tokens
[3] = STATE_SPECULAR
;
1234 switch (*(*inst
)++) {
1236 state_tokens
[0] = STATE_FOG_COLOR
;
1239 state_tokens
[0] = STATE_FOG_PARAMS
;
1245 state_tokens
[1] = parse_integer (inst
, Program
);
1246 switch (*(*inst
)++) {
1248 state_tokens
[0] = STATE_TEXENV_COLOR
;
1257 state_tokens
[0] = STATE_TEXGEN
;
1258 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1260 if (parse_texcoord_num (ctx
, inst
, Program
, &coord
))
1262 state_tokens
[1] = coord
;
1267 /* 0 - s, 1 - t, 2 - r, 3 - q */
1270 if (type
== TEX_GEN_EYE
) {
1273 state_tokens
[2] = STATE_TEXGEN_EYE_S
;
1276 state_tokens
[2] = STATE_TEXGEN_EYE_T
;
1279 state_tokens
[2] = STATE_TEXGEN_EYE_R
;
1282 state_tokens
[2] = STATE_TEXGEN_EYE_Q
;
1285 _mesa_problem(ctx
, "bad texgen component in "
1286 "parse_state_single_item()");
1292 state_tokens
[2] = STATE_TEXGEN_OBJECT_S
;
1295 state_tokens
[2] = STATE_TEXGEN_OBJECT_T
;
1298 state_tokens
[2] = STATE_TEXGEN_OBJECT_R
;
1301 state_tokens
[2] = STATE_TEXGEN_OBJECT_Q
;
1304 _mesa_problem(ctx
, "bad texgen component in "
1305 "parse_state_single_item()");
1312 switch (*(*inst
)++) {
1314 state_tokens
[0] = STATE_DEPTH_RANGE
;
1319 case STATE_CLIP_PLANE
:
1320 state_tokens
[0] = STATE_CLIPPLANE
;
1321 state_tokens
[1] = parse_integer (inst
, Program
);
1322 if (parse_clipplane_num (ctx
, inst
, Program
,
1323 (GLint
*) &state_tokens
[1]))
1328 switch (*(*inst
)++) {
1330 state_tokens
[0] = STATE_POINT_SIZE
;
1333 case POINT_ATTENUATION
:
1334 state_tokens
[0] = STATE_POINT_ATTENUATION
;
1339 /* XXX: I think this is the correct format for a matrix row */
1340 case STATE_MATRIX_ROWS
:
1341 if (parse_matrix(ctx
, inst
, Program
,
1342 (GLint
*) &state_tokens
[0],
1343 (GLint
*) &state_tokens
[1],
1344 (GLint
*) &state_tokens
[4]))
1347 state_tokens
[2] = parse_integer (inst
, Program
); /* The first row to grab */
1349 if ((**inst
) != 0) { /* Either the last row, 0 */
1350 state_tokens
[3] = parse_integer (inst
, Program
);
1351 if (state_tokens
[3] < state_tokens
[2]) {
1352 program_error(ctx
, Program
->Position
,
1353 "Second matrix index less than the first");
1354 /* state_tokens[4] vs. state_tokens[3] */
1359 state_tokens
[3] = state_tokens
[2];
1369 * This parses a state string (rather, the binary version of it) into
1370 * a 6-token similar for the state fetching code in program.c
1372 * One might ask, why fetch these parameters into just like you fetch
1373 * state when they are already stored in other places?
1375 * Because of array offsets -> We can stick env/local parameters in the
1376 * middle of a parameter array and then index someplace into the array
1379 * One optimization might be to only do this for the cases where the
1380 * env/local parameters end up inside of an array, and leave the
1381 * single parameters (or arrays of pure env/local pareameters) in their
1382 * respective register files.
1384 * For ENV parameters, the format is:
1385 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1386 * state_tokens[1] = STATE_ENV
1387 * state_tokens[2] = the parameter index
1389 * for LOCAL parameters, the format is:
1390 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1391 * state_tokens[1] = STATE_LOCAL
1392 * state_tokens[2] = the parameter index
1394 * \param inst - the start in the binary arry to start working from
1395 * \param state_tokens - the storage for the 6-token state description
1396 * \return - 0 on sucess, 1 on failure
1399 parse_program_single_item (GLcontext
* ctx
, const GLubyte
** inst
,
1400 struct arb_program
*Program
,
1401 gl_state_index state_tokens
[STATE_LENGTH
])
1403 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1404 state_tokens
[0] = STATE_FRAGMENT_PROGRAM
;
1406 state_tokens
[0] = STATE_VERTEX_PROGRAM
;
1409 switch (*(*inst
)++) {
1410 case PROGRAM_PARAM_ENV
:
1411 state_tokens
[1] = STATE_ENV
;
1412 state_tokens
[2] = parse_integer (inst
, Program
);
1414 /* Check state_tokens[2] against the number of ENV parameters available */
1415 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1416 (state_tokens
[2] >= (GLint
) ctx
->Const
.FragmentProgram
.MaxEnvParams
))
1418 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1419 (state_tokens
[2] >= (GLint
) ctx
->Const
.VertexProgram
.MaxEnvParams
))) {
1420 program_error(ctx
, Program
->Position
,
1421 "Invalid Program Env Parameter");
1422 /* bad state_tokens[2] */
1428 case PROGRAM_PARAM_LOCAL
:
1429 state_tokens
[1] = STATE_LOCAL
;
1430 state_tokens
[2] = parse_integer (inst
, Program
);
1432 /* Check state_tokens[2] against the number of LOCAL parameters available */
1433 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1434 (state_tokens
[2] >= (GLint
) ctx
->Const
.FragmentProgram
.MaxLocalParams
))
1436 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1437 (state_tokens
[2] >= (GLint
) ctx
->Const
.VertexProgram
.MaxLocalParams
))) {
1438 program_error(ctx
, Program
->Position
,
1439 "Invalid Program Local Parameter");
1440 /* bad state_tokens[2] */
1450 * For ARB_vertex_program, programs are not allowed to use both an explicit
1451 * vertex attribute and a generic vertex attribute corresponding to the same
1452 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1454 * This will walk our var_cache and make sure that nobody does anything fishy.
1456 * \return 0 on sucess, 1 on error
1459 generic_attrib_check(struct var_cache
*vc_head
)
1462 struct var_cache
*curr
;
1463 GLboolean explicitAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
],
1464 genericAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
];
1466 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1467 explicitAttrib
[a
] = GL_FALSE
;
1468 genericAttrib
[a
] = GL_FALSE
;
1473 if (curr
->type
== vt_attrib
) {
1474 if (curr
->attrib_is_generic
)
1475 genericAttrib
[ curr
->attrib_binding
] = GL_TRUE
;
1477 explicitAttrib
[ curr
->attrib_binding
] = GL_TRUE
;
1483 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1484 if ((explicitAttrib
[a
]) && (genericAttrib
[a
]))
1492 * This will handle the binding side of an ATTRIB var declaration
1494 * \param inputReg returns the input register index, one of the
1495 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
1496 * \return returns 0 on success, 1 on error
1499 parse_attrib_binding(GLcontext
* ctx
, const GLubyte
** inst
,
1500 struct arb_program
*Program
,
1501 GLuint
*inputReg
, GLuint
*is_generic
)
1507 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1508 switch (*(*inst
)++) {
1509 case FRAGMENT_ATTRIB_COLOR
:
1512 err
= parse_color_type (ctx
, inst
, Program
, &coord
);
1513 *inputReg
= FRAG_ATTRIB_COL0
+ coord
;
1516 case FRAGMENT_ATTRIB_TEXCOORD
:
1518 GLuint texcoord
= 0;
1519 err
= parse_texcoord_num (ctx
, inst
, Program
, &texcoord
);
1520 *inputReg
= FRAG_ATTRIB_TEX0
+ texcoord
;
1523 case FRAGMENT_ATTRIB_FOGCOORD
:
1524 *inputReg
= FRAG_ATTRIB_FOGC
;
1526 case FRAGMENT_ATTRIB_POSITION
:
1527 *inputReg
= FRAG_ATTRIB_WPOS
;
1535 switch (*(*inst
)++) {
1536 case VERTEX_ATTRIB_POSITION
:
1537 *inputReg
= VERT_ATTRIB_POS
;
1540 case VERTEX_ATTRIB_WEIGHT
:
1543 err
= parse_weight_num (ctx
, inst
, Program
, &weight
);
1544 *inputReg
= VERT_ATTRIB_WEIGHT
;
1546 /* hack for Warcraft (see bug 8060) */
1547 _mesa_warning(ctx
, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
1550 program_error(ctx
, Program
->Position
,
1551 "ARB_vertex_blend not supported");
1556 case VERTEX_ATTRIB_NORMAL
:
1557 *inputReg
= VERT_ATTRIB_NORMAL
;
1560 case VERTEX_ATTRIB_COLOR
:
1563 err
= parse_color_type (ctx
, inst
, Program
, &color
);
1565 *inputReg
= VERT_ATTRIB_COLOR1
;
1568 *inputReg
= VERT_ATTRIB_COLOR0
;
1573 case VERTEX_ATTRIB_FOGCOORD
:
1574 *inputReg
= VERT_ATTRIB_FOG
;
1577 case VERTEX_ATTRIB_TEXCOORD
:
1580 err
= parse_texcoord_num (ctx
, inst
, Program
, &unit
);
1581 *inputReg
= VERT_ATTRIB_TEX0
+ unit
;
1585 case VERTEX_ATTRIB_MATRIXINDEX
:
1586 /* Not supported at this time */
1588 const char *msg
= "ARB_palette_matrix not supported";
1589 parse_integer (inst
, Program
);
1590 program_error(ctx
, Program
->Position
, msg
);
1594 case VERTEX_ATTRIB_GENERIC
:
1597 err
= parse_generic_attrib_num(ctx
, inst
, Program
, &attrib
);
1600 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1601 * attributes do not alias the conventional vertex
1605 *inputReg
= attrib
+ VERT_ATTRIB_GENERIC0
;
1619 program_error(ctx
, Program
->Position
, "Bad attribute binding");
1627 * This translates between a binary token for an output variable type
1628 * and the mesa token for the same thing.
1630 * \param inst The parsed tokens
1631 * \param outputReg Returned index/number of the output register,
1632 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
1635 parse_result_binding(GLcontext
*ctx
, const GLubyte
**inst
,
1636 GLuint
*outputReg
, struct arb_program
*Program
)
1638 const GLubyte token
= *(*inst
)++;
1641 case FRAGMENT_RESULT_COLOR
:
1642 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1645 /* This gets result of the color buffer we're supposed to
1646 * draw into. This pertains to GL_ARB_draw_buffers.
1648 parse_output_color_num(ctx
, inst
, Program
, &out_color
);
1649 ASSERT(out_color
< MAX_DRAW_BUFFERS
);
1650 *outputReg
= FRAG_RESULT_COLR
;
1653 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1654 *outputReg
= VERT_RESULT_HPOS
;
1658 case FRAGMENT_RESULT_DEPTH
:
1659 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1660 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
1661 *outputReg
= FRAG_RESULT_DEPR
;
1664 /* for vtx programs, this is VERTEX_RESULT_COLOR */
1666 GLuint face_type
= parse_face_type(inst
);
1667 GLint err
= parse_color_type(ctx
, inst
, Program
, &color_type
);
1674 *outputReg
= VERT_RESULT_BFC1
; /* secondary color */
1677 *outputReg
= VERT_RESULT_BFC0
; /* primary color */
1683 *outputReg
= VERT_RESULT_COL1
; /* secondary color */
1687 *outputReg
= VERT_RESULT_COL0
; /* primary color */
1693 case VERTEX_RESULT_FOGCOORD
:
1694 *outputReg
= VERT_RESULT_FOGC
;
1697 case VERTEX_RESULT_POINTSIZE
:
1698 *outputReg
= VERT_RESULT_PSIZ
;
1701 case VERTEX_RESULT_TEXCOORD
:
1704 if (parse_texcoord_num (ctx
, inst
, Program
, &unit
))
1706 *outputReg
= VERT_RESULT_TEX0
+ unit
;
1711 Program
->Base
.OutputsWritten
|= (1 << *outputReg
);
1718 * This handles the declaration of ATTRIB variables
1721 * parse_vert_attrib_binding(), or something like that
1723 * \return 0 on sucess, 1 on error
1726 parse_attrib (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1727 struct arb_program
*Program
)
1730 struct var_cache
*attrib_var
;
1732 attrib_var
= parse_string (inst
, vc_head
, Program
, &found
);
1733 Program
->Position
= parse_position (inst
);
1735 program_error2(ctx
, Program
->Position
,
1736 "Duplicate variable declaration",
1737 (char *) attrib_var
->name
);
1741 attrib_var
->type
= vt_attrib
;
1743 if (parse_attrib_binding(ctx
, inst
, Program
, &attrib_var
->attrib_binding
,
1744 &attrib_var
->attrib_is_generic
))
1747 if (generic_attrib_check(*vc_head
)) {
1748 program_error(ctx
, Program
->Position
,
1749 "Cannot use both a generic vertex attribute "
1750 "and a specific attribute of the same type");
1754 Program
->Base
.NumAttributes
++;
1759 * \param use -- TRUE if we're called when declaring implicit parameters,
1760 * FALSE if we're declaraing variables. This has to do with
1761 * if we get a signed or unsigned float for scalar constants
1764 parse_param_elements (GLcontext
* ctx
, const GLubyte
** inst
,
1765 struct var_cache
*param_var
,
1766 struct arb_program
*Program
, GLboolean use
)
1770 gl_state_index state_tokens
[STATE_LENGTH
] = {0, 0, 0, 0, 0};
1771 GLfloat const_values
[4];
1773 switch (*(*inst
)++) {
1774 case PARAM_STATE_ELEMENT
:
1775 if (parse_state_single_item (ctx
, inst
, Program
, state_tokens
))
1778 /* If we adding STATE_MATRIX that has multiple rows, we need to
1779 * unroll it and call _mesa_add_state_reference() for each row
1781 if ((state_tokens
[0] == STATE_MODELVIEW_MATRIX
||
1782 state_tokens
[0] == STATE_PROJECTION_MATRIX
||
1783 state_tokens
[0] == STATE_MVP_MATRIX
||
1784 state_tokens
[0] == STATE_TEXTURE_MATRIX
||
1785 state_tokens
[0] == STATE_PROGRAM_MATRIX
)
1786 && (state_tokens
[2] != state_tokens
[3])) {
1788 const GLint first_row
= state_tokens
[2];
1789 const GLint last_row
= state_tokens
[3];
1791 for (row
= first_row
; row
<= last_row
; row
++) {
1792 state_tokens
[2] = state_tokens
[3] = row
;
1794 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1796 if (param_var
->param_binding_begin
== ~0U)
1797 param_var
->param_binding_begin
= idx
;
1798 param_var
->param_binding_length
++;
1799 Program
->Base
.NumParameters
++;
1803 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1805 if (param_var
->param_binding_begin
== ~0U)
1806 param_var
->param_binding_begin
= idx
;
1807 param_var
->param_binding_length
++;
1808 Program
->Base
.NumParameters
++;
1812 case PARAM_PROGRAM_ELEMENT
:
1813 if (parse_program_single_item (ctx
, inst
, Program
, state_tokens
))
1815 idx
= _mesa_add_state_reference (Program
->Base
.Parameters
, state_tokens
);
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
++;
1821 /* Check if there is more: 0 -> we're done, else its an integer */
1823 GLuint out_of_range
, new_idx
;
1824 GLuint start_idx
= state_tokens
[2] + 1;
1825 GLuint end_idx
= parse_integer (inst
, Program
);
1828 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1829 if (((state_tokens
[1] == STATE_ENV
)
1830 && (end_idx
>= ctx
->Const
.FragmentProgram
.MaxEnvParams
))
1831 || ((state_tokens
[1] == STATE_LOCAL
)
1833 ctx
->Const
.FragmentProgram
.MaxLocalParams
)))
1837 if (((state_tokens
[1] == STATE_ENV
)
1838 && (end_idx
>= ctx
->Const
.VertexProgram
.MaxEnvParams
))
1839 || ((state_tokens
[1] == STATE_LOCAL
)
1841 ctx
->Const
.VertexProgram
.MaxLocalParams
)))
1845 program_error(ctx
, Program
->Position
,
1846 "Invalid Program Parameter"); /*end_idx*/
1850 for (new_idx
= start_idx
; new_idx
<= end_idx
; new_idx
++) {
1851 state_tokens
[2] = new_idx
;
1852 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1854 param_var
->param_binding_length
++;
1855 Program
->Base
.NumParameters
++;
1863 case PARAM_CONSTANT
:
1864 /* parsing something like {1.0, 2.0, 3.0, 4.0} */
1865 parse_constant (inst
, const_values
, Program
, use
);
1866 idx
= _mesa_add_named_constant(Program
->Base
.Parameters
,
1867 (char *) param_var
->name
,
1869 if (param_var
->param_binding_begin
== ~0U)
1870 param_var
->param_binding_begin
= idx
;
1871 param_var
->param_binding_type
= PROGRAM_CONSTANT
;
1872 param_var
->param_binding_length
++;
1873 Program
->Base
.NumParameters
++;
1877 program_error(ctx
, Program
->Position
,
1878 "Unexpected token (in parse_param_elements())");
1882 /* Make sure we haven't blown past our parameter limits */
1883 if (((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1884 (Program
->Base
.NumParameters
>=
1885 ctx
->Const
.VertexProgram
.MaxLocalParams
))
1886 || ((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1887 && (Program
->Base
.NumParameters
>=
1888 ctx
->Const
.FragmentProgram
.MaxLocalParams
))) {
1889 program_error(ctx
, Program
->Position
, "Too many parameter variables");
1898 * This picks out PARAM program parameter bindings.
1900 * XXX: This needs to be stressed & tested
1902 * \return 0 on sucess, 1 on error
1905 parse_param (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1906 struct arb_program
*Program
)
1909 GLint specified_length
;
1910 struct var_cache
*param_var
;
1913 param_var
= parse_string (inst
, vc_head
, Program
, &found
);
1914 Program
->Position
= parse_position (inst
);
1917 program_error2(ctx
, Program
->Position
,
1918 "Duplicate variable declaration",
1919 (char *) param_var
->name
);
1923 specified_length
= parse_integer (inst
, Program
);
1925 if (specified_length
< 0) {
1926 program_error(ctx
, Program
->Position
, "Negative parameter array length");
1930 param_var
->type
= vt_param
;
1931 param_var
->param_binding_length
= 0;
1933 /* Right now, everything is shoved into the main state register file.
1935 * In the future, it would be nice to leave things ENV/LOCAL params
1936 * in their respective register files, if possible
1938 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1941 * * - add each guy to the parameter list
1942 * * - increment the param_var->param_binding_len
1943 * * - store the param_var->param_binding_begin for the first one
1944 * * - compare the actual len to the specified len at the end
1946 while (**inst
!= PARAM_NULL
) {
1947 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_FALSE
))
1951 /* Test array length here! */
1952 if (specified_length
) {
1953 if (specified_length
!= (int)param_var
->param_binding_length
) {
1954 program_error(ctx
, Program
->Position
,
1955 "Declared parameter array length does not match parameter list");
1968 parse_param_use (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1969 struct arb_program
*Program
, struct var_cache
**new_var
)
1971 struct var_cache
*param_var
;
1973 /* First, insert a dummy entry into the var_cache */
1974 var_cache_create (¶m_var
);
1975 param_var
->name
= (const GLubyte
*) " ";
1976 param_var
->type
= vt_param
;
1978 param_var
->param_binding_length
= 0;
1979 /* Don't fill in binding_begin; We use the default value of -1
1980 * to tell if its already initialized, elsewhere.
1982 * param_var->param_binding_begin = 0;
1984 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1986 var_cache_append (vc_head
, param_var
);
1988 /* Then fill it with juicy parameter goodness */
1989 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_TRUE
))
1992 *new_var
= param_var
;
1999 * This handles the declaration of TEMP variables
2001 * \return 0 on sucess, 1 on error
2004 parse_temp (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2005 struct arb_program
*Program
)
2008 struct var_cache
*temp_var
;
2010 while (**inst
!= 0) {
2011 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2012 Program
->Position
= parse_position (inst
);
2014 program_error2(ctx
, Program
->Position
,
2015 "Duplicate variable declaration",
2016 (char *) temp_var
->name
);
2020 temp_var
->type
= vt_temp
;
2022 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
2023 (Program
->Base
.NumTemporaries
>=
2024 ctx
->Const
.FragmentProgram
.MaxTemps
))
2025 || ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
2026 && (Program
->Base
.NumTemporaries
>=
2027 ctx
->Const
.VertexProgram
.MaxTemps
))) {
2028 program_error(ctx
, Program
->Position
,
2029 "Too many TEMP variables declared");
2033 temp_var
->temp_binding
= Program
->Base
.NumTemporaries
;
2034 Program
->Base
.NumTemporaries
++;
2042 * This handles variables of the OUTPUT variety
2044 * \return 0 on sucess, 1 on error
2047 parse_output (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2048 struct arb_program
*Program
)
2051 struct var_cache
*output_var
;
2054 output_var
= parse_string (inst
, vc_head
, Program
, &found
);
2055 Program
->Position
= parse_position (inst
);
2057 program_error2(ctx
, Program
->Position
,
2058 "Duplicate variable declaration",
2059 (char *) output_var
->name
);
2063 output_var
->type
= vt_output
;
2065 err
= parse_result_binding(ctx
, inst
, &output_var
->output_binding
, Program
);
2070 * This handles variables of the ALIAS kind
2072 * \return 0 on sucess, 1 on error
2075 parse_alias (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2076 struct arb_program
*Program
)
2079 struct var_cache
*temp_var
;
2081 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2082 Program
->Position
= parse_position (inst
);
2085 program_error2(ctx
, Program
->Position
,
2086 "Duplicate variable declaration",
2087 (char *) temp_var
->name
);
2091 temp_var
->type
= vt_alias
;
2092 temp_var
->alias_binding
= parse_string (inst
, vc_head
, Program
, &found
);
2093 Program
->Position
= parse_position (inst
);
2097 program_error2(ctx
, Program
->Position
,
2098 "Undefined alias value",
2099 (char *) temp_var
->alias_binding
->name
);
2107 * This handles variables of the ADDRESS kind
2109 * \return 0 on sucess, 1 on error
2112 parse_address (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2113 struct arb_program
*Program
)
2116 struct var_cache
*temp_var
;
2118 while (**inst
!= 0) {
2119 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2120 Program
->Position
= parse_position (inst
);
2122 program_error2(ctx
, Program
->Position
,
2123 "Duplicate variable declaration",
2124 (char *) temp_var
->name
);
2128 temp_var
->type
= vt_address
;
2130 if (Program
->Base
.NumAddressRegs
>=
2131 ctx
->Const
.VertexProgram
.MaxAddressRegs
) {
2132 const char *msg
= "Too many ADDRESS variables declared";
2133 program_error(ctx
, Program
->Position
, msg
);
2137 temp_var
->address_binding
= Program
->Base
.NumAddressRegs
;
2138 Program
->Base
.NumAddressRegs
++;
2146 * Parse a program declaration
2148 * \return 0 on sucess, 1 on error
2151 parse_declaration (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2152 struct arb_program
*Program
)
2156 switch (*(*inst
)++) {
2158 err
= parse_address (ctx
, inst
, vc_head
, Program
);
2162 err
= parse_alias (ctx
, inst
, vc_head
, Program
);
2166 err
= parse_attrib (ctx
, inst
, vc_head
, Program
);
2170 err
= parse_output (ctx
, inst
, vc_head
, Program
);
2174 err
= parse_param (ctx
, inst
, vc_head
, Program
);
2178 err
= parse_temp (ctx
, inst
, vc_head
, Program
);
2186 * Handle the parsing out of a masked destination register, either for a
2187 * vertex or fragment program.
2189 * If we are a vertex program, make sure we don't write to
2190 * result.position if we have specified that the program is
2191 * position invariant
2193 * \param File - The register file we write to
2194 * \param Index - The register index we write to
2195 * \param WriteMask - The mask controlling which components we write (1->write)
2197 * \return 0 on sucess, 1 on error
2200 parse_masked_dst_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2201 struct var_cache
**vc_head
, struct arb_program
*Program
,
2202 enum register_file
*File
, GLuint
*Index
, GLint
*WriteMask
)
2205 struct var_cache
*dst
;
2207 /* We either have a result register specified, or a
2208 * variable that may or may not be writable
2210 switch (*(*inst
)++) {
2211 case REGISTER_RESULT
:
2212 if (parse_result_binding(ctx
, inst
, Index
, Program
))
2214 *File
= PROGRAM_OUTPUT
;
2217 case REGISTER_ESTABLISHED_NAME
:
2218 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2219 Program
->Position
= parse_position (inst
);
2221 /* If the name has never been added to our symbol table, we're hosed */
2223 program_error(ctx
, Program
->Position
, "0: Undefined variable");
2227 switch (dst
->type
) {
2229 *File
= PROGRAM_OUTPUT
;
2230 *Index
= dst
->output_binding
;
2234 *File
= PROGRAM_TEMPORARY
;
2235 *Index
= dst
->temp_binding
;
2238 /* If the var type is not vt_output or vt_temp, no go */
2240 program_error(ctx
, Program
->Position
,
2241 "Destination register is read only");
2247 program_error(ctx
, Program
->Position
,
2248 "Unexpected opcode in parse_masked_dst_reg()");
2253 /* Position invariance test */
2254 /* This test is done now in syntax portion - when position invariance OPTION
2255 is specified, "result.position" rule is disabled so there is no way
2256 to write the position
2258 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2260 program_error(ctx, Program->Position,
2261 "Vertex program specified position invariance and wrote vertex position");
2264 /* And then the mask.
2270 * ==> Need to reverse the order of bits for this!
2272 tmp
= (GLint
) *(*inst
)++;
2273 *WriteMask
= (((tmp
>>3) & 0x1) |
2283 * Handle the parsing of a address register
2285 * \param Index - The register index we write to
2287 * \return 0 on sucess, 1 on error
2290 parse_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2291 struct var_cache
**vc_head
,
2292 struct arb_program
*Program
, GLint
* Index
)
2294 struct var_cache
*dst
;
2297 *Index
= 0; /* XXX */
2299 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2300 Program
->Position
= parse_position (inst
);
2302 /* If the name has never been added to our symbol table, we're hosed */
2304 program_error(ctx
, Program
->Position
, "Undefined variable");
2308 if (dst
->type
!= vt_address
) {
2309 program_error(ctx
, Program
->Position
, "Variable is not of type ADDRESS");
2318 * Handle the parsing out of a masked address register
2320 * \param Index - The register index we write to
2321 * \param WriteMask - The mask controlling which components we write (1->write)
2323 * \return 0 on sucess, 1 on error
2326 parse_masked_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2327 struct var_cache
**vc_head
,
2328 struct arb_program
*Program
, GLint
* Index
,
2329 GLboolean
* WriteMask
)
2331 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, Index
))
2334 /* This should be 0x8 */
2337 /* Writemask of .x is implied */
2339 WriteMask
[1] = WriteMask
[2] = WriteMask
[3] = 0;
2346 * Parse out a swizzle mask.
2348 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
2350 * The len parameter allows us to grab 4 components for a vector
2351 * swizzle, or just 1 component for a scalar src register selection
2354 parse_swizzle_mask(const GLubyte
** inst
, GLubyte
*swizzle
, GLint len
)
2358 for (i
= 0; i
< 4; i
++)
2361 for (i
= 0; i
< len
; i
++) {
2362 switch (*(*inst
)++) {
2364 swizzle
[i
] = SWIZZLE_X
;
2367 swizzle
[i
] = SWIZZLE_Y
;
2370 swizzle
[i
] = SWIZZLE_Z
;
2373 swizzle
[i
] = SWIZZLE_W
;
2376 _mesa_problem(NULL
, "bad component in parse_swizzle_mask()");
2384 * Parse an extended swizzle mask which is a sequence of
2385 * four x/y/z/w/0/1 tokens.
2386 * \return swizzle four swizzle values
2387 * \return negateMask four element bitfield
2390 parse_extended_swizzle_mask(const GLubyte
**inst
, GLubyte swizzle
[4],
2391 GLubyte
*negateMask
)
2396 for (i
= 0; i
< 4; i
++) {
2398 if (parse_sign(inst
) == -1)
2399 *negateMask
|= (1 << i
);
2405 swizzle
[i
] = SWIZZLE_ZERO
;
2408 swizzle
[i
] = SWIZZLE_ONE
;
2411 swizzle
[i
] = SWIZZLE_X
;
2414 swizzle
[i
] = SWIZZLE_Y
;
2417 swizzle
[i
] = SWIZZLE_Z
;
2420 swizzle
[i
] = SWIZZLE_W
;
2423 _mesa_problem(NULL
, "bad case in parse_extended_swizzle_mask()");
2431 parse_src_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2432 struct var_cache
**vc_head
,
2433 struct arb_program
*Program
,
2434 enum register_file
* File
, GLint
* Index
,
2435 GLboolean
*IsRelOffset
)
2437 struct var_cache
*src
;
2438 GLuint binding
, is_generic
, found
;
2443 /* And the binding for the src */
2444 switch (*(*inst
)++) {
2445 case REGISTER_ATTRIB
:
2446 if (parse_attrib_binding
2447 (ctx
, inst
, Program
, &binding
, &is_generic
))
2449 *File
= PROGRAM_INPUT
;
2452 /* We need to insert a dummy variable into the var_cache so we can
2453 * catch generic vertex attrib aliasing errors
2455 var_cache_create(&src
);
2456 src
->type
= vt_attrib
;
2457 src
->name
= (const GLubyte
*) "Dummy Attrib Variable";
2458 src
->attrib_binding
= binding
;
2459 src
->attrib_is_generic
= is_generic
;
2460 var_cache_append(vc_head
, src
);
2461 if (generic_attrib_check(*vc_head
)) {
2462 program_error(ctx
, Program
->Position
,
2463 "Cannot use both a generic vertex attribute "
2464 "and a specific attribute of the same type");
2469 case REGISTER_PARAM
:
2471 case PARAM_ARRAY_ELEMENT
:
2473 src
= parse_string (inst
, vc_head
, Program
, &found
);
2474 Program
->Position
= parse_position (inst
);
2477 program_error2(ctx
, Program
->Position
,
2478 "Undefined variable",
2479 (char *) src
->name
);
2483 *File
= (enum register_file
) src
->param_binding_type
;
2485 switch (*(*inst
)++) {
2486 case ARRAY_INDEX_ABSOLUTE
:
2487 offset
= parse_integer (inst
, Program
);
2490 || (offset
>= (int)src
->param_binding_length
)) {
2491 program_error(ctx
, Program
->Position
,
2492 "Index out of range");
2493 /* offset, src->name */
2497 *Index
= src
->param_binding_begin
+ offset
;
2500 case ARRAY_INDEX_RELATIVE
:
2502 GLint addr_reg_idx
, rel_off
;
2504 /* First, grab the address regiseter */
2505 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &addr_reg_idx
))
2514 /* Then the relative offset */
2515 if (parse_relative_offset(ctx
, inst
, Program
, &rel_off
)) return 1;
2517 /* And store it properly */
2518 *Index
= src
->param_binding_begin
+ rel_off
;
2526 if (parse_param_use (ctx
, inst
, vc_head
, Program
, &src
))
2529 *File
= (enum register_file
) src
->param_binding_type
;
2530 *Index
= src
->param_binding_begin
;
2535 case REGISTER_ESTABLISHED_NAME
:
2536 src
= parse_string (inst
, vc_head
, Program
, &found
);
2537 Program
->Position
= parse_position (inst
);
2539 /* If the name has never been added to our symbol table, we're hosed */
2541 program_error(ctx
, Program
->Position
,
2542 "3: Undefined variable"); /* src->name */
2546 switch (src
->type
) {
2548 *File
= PROGRAM_INPUT
;
2549 *Index
= src
->attrib_binding
;
2552 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2554 *File
= (enum register_file
) src
->param_binding_type
;
2555 *Index
= src
->param_binding_begin
;
2559 *File
= PROGRAM_TEMPORARY
;
2560 *Index
= src
->temp_binding
;
2563 /* If the var type is vt_output no go */
2565 program_error(ctx
, Program
->Position
,
2566 "destination register is read only");
2573 program_error(ctx
, Program
->Position
,
2574 "Unknown token in parse_src_reg");
2578 /* Add attributes to InputsRead only if they are used the program.
2579 * This avoids the handling of unused ATTRIB declarations in the drivers. */
2580 if (*File
== PROGRAM_INPUT
)
2581 Program
->Base
.InputsRead
|= (1 << *Index
);
2588 * Parse vertex/fragment program vector source register.
2591 parse_vector_src_reg(GLcontext
*ctx
, const GLubyte
**inst
,
2592 struct var_cache
**vc_head
,
2593 struct arb_program
*program
,
2594 struct prog_src_register
*reg
)
2596 enum register_file file
;
2600 GLboolean isRelOffset
;
2603 negateMask
= (parse_sign (inst
) == -1) ? NEGATE_XYZW
: NEGATE_NONE
;
2605 /* And the src reg */
2606 if (parse_src_reg(ctx
, inst
, vc_head
, program
, &file
, &index
, &isRelOffset
))
2609 /* finally, the swizzle */
2610 parse_swizzle_mask(inst
, swizzle
, 4);
2614 reg
->Swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
2615 reg
->NegateBase
= negateMask
;
2616 reg
->RelAddr
= isRelOffset
;
2622 * Parse vertex/fragment program scalar source register.
2625 parse_scalar_src_reg(GLcontext
*ctx
, const GLubyte
**inst
,
2626 struct var_cache
**vc_head
,
2627 struct arb_program
*program
,
2628 struct prog_src_register
*reg
)
2630 enum register_file file
;
2634 GLboolean isRelOffset
;
2637 negateMask
= (parse_sign (inst
) == -1) ? NEGATE_XYZW
: NEGATE_NONE
;
2639 /* And the src reg */
2640 if (parse_src_reg(ctx
, inst
, vc_head
, program
, &file
, &index
, &isRelOffset
))
2643 /* finally, the swizzle */
2644 parse_swizzle_mask(inst
, swizzle
, 1);
2648 reg
->Swizzle
= (swizzle
[0] << 0);
2649 reg
->NegateBase
= negateMask
;
2650 reg
->RelAddr
= isRelOffset
;
2656 * Parse vertex/fragment program destination register.
2657 * \return 1 if error, 0 if no error.
2660 parse_dst_reg(GLcontext
* ctx
, const GLubyte
** inst
,
2661 struct var_cache
**vc_head
, struct arb_program
*program
,
2662 struct prog_dst_register
*reg
)
2666 enum register_file file
;
2668 if (parse_masked_dst_reg (ctx
, inst
, vc_head
, program
, &file
, &idx
, &mask
))
2673 reg
->WriteMask
= mask
;
2679 * This is a big mother that handles getting opcodes into the instruction
2680 * and handling the src & dst registers for fragment program instructions
2681 * \return 1 if error, 0 if no error
2684 parse_fp_instruction (GLcontext
* ctx
, const GLubyte
** inst
,
2685 struct var_cache
**vc_head
, struct arb_program
*Program
,
2686 struct prog_instruction
*fp
)
2690 GLubyte instClass
, type
, code
;
2692 GLuint shadow_tex
= 0;
2694 _mesa_init_instructions(fp
, 1);
2696 /* Record the position in the program string for debugging */
2697 fp
->StringPos
= Program
->Position
;
2699 /* OP_ALU_INST or OP_TEX_INST */
2700 instClass
= *(*inst
)++;
2702 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2703 * OP_TEX_{SAMPLE, KIL}
2707 /* The actual opcode name */
2710 /* Increment the correct count */
2711 switch (instClass
) {
2713 Program
->NumAluInstructions
++;
2716 Program
->NumTexInstructions
++;
2724 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2726 fp
->Opcode
= OPCODE_ABS
;
2730 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2732 fp
->Opcode
= OPCODE_FLR
;
2736 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2738 fp
->Opcode
= OPCODE_FRC
;
2742 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2744 fp
->Opcode
= OPCODE_LIT
;
2748 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2750 fp
->Opcode
= OPCODE_MOV
;
2754 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2757 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2764 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2766 fp
->Opcode
= OPCODE_COS
;
2770 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2772 fp
->Opcode
= OPCODE_EX2
;
2776 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2778 fp
->Opcode
= OPCODE_LG2
;
2782 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2784 fp
->Opcode
= OPCODE_RCP
;
2788 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2790 fp
->Opcode
= OPCODE_RSQ
;
2794 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2796 fp
->Opcode
= OPCODE_SIN
;
2800 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2803 fp
->Opcode
= OPCODE_SCS
;
2807 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2810 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2817 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2819 fp
->Opcode
= OPCODE_POW
;
2823 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2826 for (a
= 0; a
< 2; a
++) {
2827 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2836 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2838 fp
->Opcode
= OPCODE_ADD
;
2842 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2844 fp
->Opcode
= OPCODE_DP3
;
2848 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2850 fp
->Opcode
= OPCODE_DP4
;
2854 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2856 fp
->Opcode
= OPCODE_DPH
;
2860 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2862 fp
->Opcode
= OPCODE_DST
;
2866 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2868 fp
->Opcode
= OPCODE_MAX
;
2872 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2874 fp
->Opcode
= OPCODE_MIN
;
2878 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2880 fp
->Opcode
= OPCODE_MUL
;
2884 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2886 fp
->Opcode
= OPCODE_SGE
;
2890 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2892 fp
->Opcode
= OPCODE_SLT
;
2896 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2898 fp
->Opcode
= OPCODE_SUB
;
2902 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2904 fp
->Opcode
= OPCODE_XPD
;
2908 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2910 for (a
= 0; a
< 2; a
++) {
2911 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2919 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2921 fp
->Opcode
= OPCODE_CMP
;
2925 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2927 fp
->Opcode
= OPCODE_LRP
;
2931 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2933 fp
->Opcode
= OPCODE_MAD
;
2937 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2940 for (a
= 0; a
< 3; a
++) {
2941 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2949 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2951 fp
->Opcode
= OPCODE_SWZ
;
2954 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2960 enum register_file file
;
2963 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
, &rel
))
2965 parse_extended_swizzle_mask(inst
, swizzle
, &negateMask
);
2966 fp
->SrcReg
[0].File
= file
;
2967 fp
->SrcReg
[0].Index
= index
;
2968 fp
->SrcReg
[0].NegateBase
= negateMask
;
2969 fp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
2979 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2981 fp
->Opcode
= OPCODE_TEX
;
2985 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2987 fp
->Opcode
= OPCODE_TXP
;
2991 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2993 fp
->Opcode
= OPCODE_TXB
;
2997 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3000 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
3004 if (parse_texcoord_num (ctx
, inst
, Program
, &texcoord
))
3006 fp
->TexSrcUnit
= texcoord
;
3009 switch (*(*inst
)++) {
3010 case TEXTARGET_SHADOW1D
:
3011 shadow_tex
= 1 << texcoord
;
3014 fp
->TexSrcTarget
= TEXTURE_1D_INDEX
;
3016 case TEXTARGET_SHADOW2D
:
3017 shadow_tex
= 1 << texcoord
;
3020 fp
->TexSrcTarget
= TEXTURE_2D_INDEX
;
3023 fp
->TexSrcTarget
= TEXTURE_3D_INDEX
;
3025 case TEXTARGET_SHADOWRECT
:
3026 shadow_tex
= 1 << texcoord
;
3028 case TEXTARGET_RECT
:
3029 fp
->TexSrcTarget
= TEXTURE_RECT_INDEX
;
3031 case TEXTARGET_CUBE
:
3032 fp
->TexSrcTarget
= TEXTURE_CUBE_INDEX
;
3034 case TEXTARGET_SHADOW1D_ARRAY
:
3035 shadow_tex
= 1 << texcoord
;
3037 case TEXTARGET_1D_ARRAY
:
3038 fp
->TexSrcTarget
= TEXTURE_1D_ARRAY_INDEX
;
3040 case TEXTARGET_SHADOW2D_ARRAY
:
3041 shadow_tex
= 1 << texcoord
;
3043 case TEXTARGET_2D_ARRAY
:
3044 fp
->TexSrcTarget
= TEXTURE_2D_ARRAY_INDEX
;
3048 /* Don't test the first time a particular sampler is seen. Each time
3049 * after that, make sure the shadow state is the same.
3051 if ((_mesa_bitcount(Program
->TexturesUsed
[texcoord
]) > 0)
3052 && ((Program
->ShadowSamplers
& (1 << texcoord
)) != shadow_tex
)) {
3053 program_error(ctx
, Program
->Position
,
3054 "texture image unit used for shadow sampling and non-shadow sampling");
3058 Program
->TexturesUsed
[texcoord
] |= (1 << fp
->TexSrcTarget
);
3059 /* Check that both "2D" and "CUBE" (for example) aren't both used */
3060 if (_mesa_bitcount(Program
->TexturesUsed
[texcoord
]) > 1) {
3061 program_error(ctx
, Program
->Position
,
3062 "multiple targets used on one texture image unit");
3067 Program
->ShadowSamplers
|= shadow_tex
;
3071 Program
->UsesKill
= 1;
3072 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
3074 fp
->Opcode
= OPCODE_KIL
;
3077 _mesa_problem(ctx
, "bad type 0x%x in parse_fp_instruction()", type
);
3086 * Handle the parsing out of a masked address register
3088 * \param Index - The register index we write to
3089 * \param WriteMask - The mask controlling which components we write (1->write)
3091 * \return 0 on sucess, 1 on error
3094 parse_vp_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
3095 struct var_cache
**vc_head
,
3096 struct arb_program
*Program
,
3097 struct prog_dst_register
*reg
)
3101 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &idx
))
3104 /* This should be 0x8 */
3107 reg
->File
= PROGRAM_ADDRESS
;
3110 /* Writemask of .x is implied */
3111 reg
->WriteMask
= 0x1;
3117 * This is a big mother that handles getting opcodes into the instruction
3118 * and handling the src & dst registers for vertex program instructions
3121 parse_vp_instruction (GLcontext
* ctx
, const GLubyte
** inst
,
3122 struct var_cache
**vc_head
, struct arb_program
*Program
,
3123 struct prog_instruction
*vp
)
3128 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3131 /* The actual opcode name */
3134 _mesa_init_instructions(vp
, 1);
3135 /* Record the position in the program string for debugging */
3136 vp
->StringPos
= Program
->Position
;
3141 vp
->Opcode
= OPCODE_ARL
;
3143 /* Remember to set SrcReg.RelAddr; */
3145 /* Get the masked address register [dst] */
3146 if (parse_vp_address_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3149 vp
->DstReg
.File
= PROGRAM_ADDRESS
;
3151 /* Get a scalar src register */
3152 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3160 vp
->Opcode
= OPCODE_ABS
;
3163 vp
->Opcode
= OPCODE_FLR
;
3166 vp
->Opcode
= OPCODE_FRC
;
3169 vp
->Opcode
= OPCODE_LIT
;
3172 vp
->Opcode
= OPCODE_MOV
;
3176 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3179 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3186 vp
->Opcode
= OPCODE_EX2
;
3189 vp
->Opcode
= OPCODE_EXP
;
3192 vp
->Opcode
= OPCODE_LG2
;
3195 vp
->Opcode
= OPCODE_LOG
;
3198 vp
->Opcode
= OPCODE_RCP
;
3201 vp
->Opcode
= OPCODE_RSQ
;
3204 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3207 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3214 vp
->Opcode
= OPCODE_POW
;
3217 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3220 for (a
= 0; a
< 2; a
++) {
3221 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3229 vp
->Opcode
= OPCODE_ADD
;
3232 vp
->Opcode
= OPCODE_DP3
;
3235 vp
->Opcode
= OPCODE_DP4
;
3238 vp
->Opcode
= OPCODE_DPH
;
3241 vp
->Opcode
= OPCODE_DST
;
3244 vp
->Opcode
= OPCODE_MAX
;
3247 vp
->Opcode
= OPCODE_MIN
;
3250 vp
->Opcode
= OPCODE_MUL
;
3253 vp
->Opcode
= OPCODE_SGE
;
3256 vp
->Opcode
= OPCODE_SLT
;
3259 vp
->Opcode
= OPCODE_SUB
;
3262 vp
->Opcode
= OPCODE_XPD
;
3265 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3268 for (a
= 0; a
< 2; a
++) {
3269 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3277 vp
->Opcode
= OPCODE_MAD
;
3281 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3284 for (a
= 0; a
< 3; a
++) {
3285 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3293 vp
->Opcode
= OPCODE_SWZ
;
3300 enum register_file file
;
3303 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3306 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
, &relAddr
))
3308 parse_extended_swizzle_mask (inst
, swizzle
, &negateMask
);
3309 vp
->SrcReg
[0].File
= file
;
3310 vp
->SrcReg
[0].Index
= index
;
3311 vp
->SrcReg
[0].NegateBase
= negateMask
;
3312 vp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
3316 vp
->SrcReg
[0].RelAddr
= relAddr
;
3326 debug_variables (GLcontext
* ctx
, struct var_cache
*vc_head
,
3327 struct arb_program
*Program
)
3329 struct var_cache
*vc
;
3332 fprintf (stderr
, "debug_variables, vc_head: %p\n", (void*) vc_head
);
3334 /* First of all, print out the contents of the var_cache */
3337 fprintf (stderr
, "[%p]\n", (void*) vc
);
3340 fprintf (stderr
, "UNDEFINED %s\n", vc
->name
);
3343 fprintf (stderr
, "ATTRIB %s\n", vc
->name
);
3344 fprintf (stderr
, " binding: 0x%x\n", vc
->attrib_binding
);
3347 fprintf (stderr
, "PARAM %s begin: %d len: %d\n", vc
->name
,
3348 vc
->param_binding_begin
, vc
->param_binding_length
);
3349 b
= vc
->param_binding_begin
;
3350 for (a
= 0; a
< vc
->param_binding_length
; a
++) {
3351 fprintf (stderr
, "%s\n",
3352 Program
->Base
.Parameters
->Parameters
[a
+ b
].Name
);
3353 if (Program
->Base
.Parameters
->Parameters
[a
+ b
].Type
== PROGRAM_STATE_VAR
) {
3355 s
= _mesa_program_state_string(Program
->Base
.Parameters
->Parameters
3356 [a
+ b
].StateIndexes
);
3357 fprintf(stderr
, "%s\n", s
);
3358 _mesa_free((char *) s
);
3361 fprintf (stderr
, "%f %f %f %f\n",
3362 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][0],
3363 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][1],
3364 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][2],
3365 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][3]);
3369 fprintf (stderr
, "TEMP %s\n", vc
->name
);
3370 fprintf (stderr
, " binding: 0x%x\n", vc
->temp_binding
);
3373 fprintf (stderr
, "OUTPUT %s\n", vc
->name
);
3374 fprintf (stderr
, " binding: 0x%x\n", vc
->output_binding
);
3377 fprintf (stderr
, "ALIAS %s\n", vc
->name
);
3378 fprintf (stderr
, " binding: 0x%p (%s)\n",
3379 (void*) vc
->alias_binding
, vc
->alias_binding
->name
);
3389 #endif /* DEBUG_PARSING */
3393 * The main loop for parsing a fragment or vertex program
3395 * \return 1 on error, 0 on success
3398 parse_instructions(GLcontext
* ctx
, const GLubyte
* inst
,
3399 struct var_cache
**vc_head
, struct arb_program
*Program
)
3401 const GLuint maxInst
= (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
3402 ? ctx
->Const
.FragmentProgram
.MaxInstructions
3403 : ctx
->Const
.VertexProgram
.MaxInstructions
;
3406 ASSERT(MAX_INSTRUCTIONS
>= maxInst
);
3408 Program
->MajorVersion
= (GLuint
) * inst
++;
3409 Program
->MinorVersion
= (GLuint
) * inst
++;
3411 while (*inst
!= END
) {
3416 case ARB_PRECISION_HINT_FASTEST
:
3417 Program
->PrecisionOption
= GL_FASTEST
;
3420 case ARB_PRECISION_HINT_NICEST
:
3421 Program
->PrecisionOption
= GL_NICEST
;
3425 Program
->FogOption
= GL_EXP
;
3429 Program
->FogOption
= GL_EXP2
;
3432 case ARB_FOG_LINEAR
:
3433 Program
->FogOption
= GL_LINEAR
;
3436 case ARB_POSITION_INVARIANT
:
3437 if (Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
3438 Program
->HintPositionInvariant
= GL_TRUE
;
3441 case ARB_FRAGMENT_PROGRAM_SHADOW
:
3442 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3443 /* TODO ARB_fragment_program_shadow code */
3447 case ARB_DRAW_BUFFERS
:
3448 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3449 /* do nothing for now */
3453 case MESA_TEXTURE_ARRAY
:
3454 /* do nothing for now */
3461 if (Program
->Base
.NumInstructions
+ 1 >= maxInst
) {
3462 program_error(ctx
, Program
->Position
,
3463 "Max instruction count exceeded");
3466 Program
->Position
= parse_position (&inst
);
3467 /* parse the current instruction */
3468 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3469 err
= parse_fp_instruction (ctx
, &inst
, vc_head
, Program
,
3470 &Program
->Base
.Instructions
[Program
->Base
.NumInstructions
]);
3473 err
= parse_vp_instruction (ctx
, &inst
, vc_head
, Program
,
3474 &Program
->Base
.Instructions
[Program
->Base
.NumInstructions
]);
3477 /* increment instuction count */
3478 Program
->Base
.NumInstructions
++;
3482 err
= parse_declaration (ctx
, &inst
, vc_head
, Program
);
3493 /* Finally, tag on an OPCODE_END instruction */
3495 const GLuint numInst
= Program
->Base
.NumInstructions
;
3496 _mesa_init_instructions(Program
->Base
.Instructions
+ numInst
, 1);
3497 Program
->Base
.Instructions
[numInst
].Opcode
= OPCODE_END
;
3498 /* YYY Wrong Position in program, whatever, at least not random -> crash
3499 Program->Position = parse_position (&inst);
3501 Program
->Base
.Instructions
[numInst
].StringPos
= Program
->Position
;
3503 Program
->Base
.NumInstructions
++;
3506 * Initialize native counts to logical counts. The device driver may
3507 * change them if program is translated into a hardware program.
3509 Program
->Base
.NumNativeInstructions
= Program
->Base
.NumInstructions
;
3510 Program
->Base
.NumNativeTemporaries
= Program
->Base
.NumTemporaries
;
3511 Program
->Base
.NumNativeParameters
= Program
->Base
.NumParameters
;
3512 Program
->Base
.NumNativeAttributes
= Program
->Base
.NumAttributes
;
3513 Program
->Base
.NumNativeAddressRegs
= Program
->Base
.NumAddressRegs
;
3520 LONGSTRING
static char core_grammar_text
[] =
3521 #include "shader/grammar/grammar_syn.h"
3526 * Set a grammar parameter.
3527 * \param name the grammar parameter
3528 * \param value the new parameter value
3529 * \return 0 if OK, 1 if error
3532 set_reg8 (GLcontext
*ctx
, grammar id
, const char *name
, GLubyte value
)
3534 char error_msg
[300];
3537 if (grammar_set_reg8 (id
, (const byte
*) name
, value
))
3540 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3541 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3542 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Grammar Register Error");
3548 * Enable support for the given language option in the parser.
3549 * \return 1 if OK, 0 if error
3552 enable_ext(GLcontext
*ctx
, grammar id
, const char *name
)
3554 return !set_reg8(ctx
, id
, name
, 1);
3559 * Enable parser extensions based on which OpenGL extensions are supported
3560 * by this rendering context.
3562 * \return GL_TRUE if OK, GL_FALSE if error.
3565 enable_parser_extensions(GLcontext
*ctx
, grammar id
)
3568 /* These are not supported at this time */
3569 if ((ctx
->Extensions
.ARB_vertex_blend
||
3570 ctx
->Extensions
.EXT_vertex_weighting
)
3571 && !enable_ext(ctx
, id
, "vertex_blend"))
3573 if (ctx
->Extensions
.ARB_matrix_palette
3574 && !enable_ext(ctx
, id
, "matrix_palette"))
3577 if (ctx
->Extensions
.ARB_fragment_program_shadow
3578 && !enable_ext(ctx
, id
, "fragment_program_shadow"))
3580 if (ctx
->Extensions
.EXT_point_parameters
3581 && !enable_ext(ctx
, id
, "point_parameters"))
3583 if (ctx
->Extensions
.EXT_secondary_color
3584 && !enable_ext(ctx
, id
, "secondary_color"))
3586 if (ctx
->Extensions
.EXT_fog_coord
3587 && !enable_ext(ctx
, id
, "fog_coord"))
3589 if (ctx
->Extensions
.NV_texture_rectangle
3590 && !enable_ext(ctx
, id
, "texture_rectangle"))
3592 if (ctx
->Extensions
.ARB_draw_buffers
3593 && !enable_ext(ctx
, id
, "draw_buffers"))
3595 if (ctx
->Extensions
.MESA_texture_array
3596 && !enable_ext(ctx
, id
, "texture_array"))
3599 /* hack for Warcraft (see bug 8060) */
3600 enable_ext(ctx
, id
, "vertex_blend");
3608 * This kicks everything off.
3610 * \param ctx - The GL Context
3611 * \param str - The program string
3612 * \param len - The program string length
3613 * \param program - The arb_program struct to return all the parsed info in
3614 * \return GL_TRUE on sucess, GL_FALSE on error
3617 _mesa_parse_arb_program(GLcontext
*ctx
, GLenum target
,
3618 const GLubyte
*str
, GLsizei len
,
3619 struct arb_program
*program
)
3621 GLint a
, err
, error_pos
;
3622 char error_msg
[300];
3624 struct var_cache
*vc_head
;
3625 grammar arbprogram_syn_id
;
3626 GLubyte
*parsed
, *inst
;
3627 GLubyte
*strz
= NULL
;
3628 static int arbprogram_syn_is_ok
= 0; /* XXX temporary */
3630 /* set the program target before parsing */
3631 program
->Base
.Target
= target
;
3633 /* Reset error state */
3634 _mesa_set_program_error(ctx
, -1, NULL
);
3636 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
3637 if (!arbprogram_syn_is_ok
) {
3638 /* One-time initialization of parsing system */
3639 grammar grammar_syn_id
;
3642 grammar_syn_id
= grammar_load_from_text ((byte
*) core_grammar_text
);
3643 if (grammar_syn_id
== 0) {
3644 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3645 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3646 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3647 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3648 "glProgramStringARB(Error loading grammar rule set)");
3652 err
= !grammar_check(grammar_syn_id
, (byte
*) arb_grammar_text
,
3653 &parsed
, &parsed_len
);
3655 /* 'parsed' is unused here */
3656 _mesa_free (parsed
);
3659 /* NOTE: we can't destroy grammar_syn_id right here because
3660 * grammar_destroy() can reset the last error
3663 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3664 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3665 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3666 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3667 "glProgramString(Error loading grammar rule set");
3668 grammar_destroy (grammar_syn_id
);
3672 grammar_destroy (grammar_syn_id
);
3674 arbprogram_syn_is_ok
= 1;
3677 /* create the grammar object */
3678 arbprogram_syn_id
= grammar_load_from_text ((byte
*) arb_grammar_text
);
3679 if (arbprogram_syn_id
== 0) {
3680 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3681 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
3682 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3683 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3684 "glProgramString(Error loading grammer rule set)");
3688 /* Set program_target register value */
3689 if (set_reg8 (ctx
, arbprogram_syn_id
, "program_target",
3690 program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
? 0x10 : 0x20)) {
3691 grammar_destroy (arbprogram_syn_id
);
3695 if (!enable_parser_extensions(ctx
, arbprogram_syn_id
)) {
3696 grammar_destroy(arbprogram_syn_id
);
3700 /* check for NULL character occurences */
3703 for (i
= 0; i
< len
; i
++) {
3704 if (str
[i
] == '\0') {
3705 program_error(ctx
, i
, "illegal character");
3706 grammar_destroy (arbprogram_syn_id
);
3712 /* copy the program string to a null-terminated string */
3713 strz
= (GLubyte
*) _mesa_malloc (len
+ 1);
3715 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glProgramStringARB");
3716 grammar_destroy (arbprogram_syn_id
);
3719 _mesa_memcpy (strz
, str
, len
);
3722 /* do a fast check on program string - initial production buffer is 4K */
3723 err
= !grammar_fast_check(arbprogram_syn_id
, strz
,
3724 &parsed
, &parsed_len
, 0x1000);
3726 /* Syntax parse error */
3728 grammar_get_last_error((GLubyte
*) error_msg
, 300, &error_pos
);
3729 program_error(ctx
, error_pos
, error_msg
);
3732 /* useful for debugging */
3736 fprintf(stderr
, "program: %s\n", (char *) strz
);
3737 fprintf(stderr
, "Error Pos: %d\n", ctx
->Program
.ErrorPos
);
3738 s
= (char *) _mesa_find_line_column(strz
, strz
+ctx
->Program
.ErrorPos
,
3740 fprintf(stderr
, "line %d col %d: %s\n", line
, col
, s
);
3747 grammar_destroy (arbprogram_syn_id
);
3751 grammar_destroy (arbprogram_syn_id
);
3754 * Program string is syntactically correct at this point
3755 * Parse the tokenized version of the program now, generating
3756 * vertex/fragment program instructions.
3759 /* Initialize the arb_program struct */
3760 program
->Base
.String
= strz
;
3761 program
->Base
.Instructions
= _mesa_alloc_instructions(MAX_INSTRUCTIONS
);
3762 program
->Base
.NumInstructions
=
3763 program
->Base
.NumTemporaries
=
3764 program
->Base
.NumParameters
=
3765 program
->Base
.NumAttributes
= program
->Base
.NumAddressRegs
= 0;
3766 program
->Base
.Parameters
= _mesa_new_parameter_list ();
3767 program
->Base
.InputsRead
= 0x0;
3768 program
->Base
.OutputsWritten
= 0x0;
3769 program
->Position
= 0;
3770 program
->MajorVersion
= program
->MinorVersion
= 0;
3771 program
->PrecisionOption
= GL_DONT_CARE
;
3772 program
->FogOption
= GL_NONE
;
3773 program
->HintPositionInvariant
= GL_FALSE
;
3774 for (a
= 0; a
< MAX_TEXTURE_IMAGE_UNITS
; a
++)
3775 program
->TexturesUsed
[a
] = 0x0;
3776 program
->ShadowSamplers
= 0x0;
3777 program
->NumAluInstructions
=
3778 program
->NumTexInstructions
=
3779 program
->NumTexIndirections
= 0;
3780 program
->UsesKill
= 0;
3785 /* Start examining the tokens in the array */
3788 /* Check the grammer rev */
3789 if (*inst
++ != REVISION
) {
3790 program_error (ctx
, 0, "Grammar version mismatch");
3794 /* ignore program target */
3796 err
= parse_instructions(ctx
, inst
, &vc_head
, program
);
3799 /*debug_variables(ctx, vc_head, program); */
3801 /* We're done with the parsed binary array */
3802 var_cache_destroy (&vc_head
);
3804 _mesa_free (parsed
);
3806 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
3807 * to size [ap.Base.NumInstructions].
3809 program
->Base
.Instructions
3810 = _mesa_realloc_instructions(program
->Base
.Instructions
,
3812 program
->Base
.NumInstructions
);
3820 _mesa_parse_arb_fragment_program(GLcontext
* ctx
, GLenum target
,
3821 const GLvoid
*str
, GLsizei len
,
3822 struct gl_fragment_program
*program
)
3824 struct arb_program ap
;
3827 ASSERT(target
== GL_FRAGMENT_PROGRAM_ARB
);
3828 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
, &ap
)) {
3829 /* Error in the program. Just return. */
3833 /* Copy the relevant contents of the arb_program struct into the
3834 * fragment_program struct.
3836 program
->Base
.String
= ap
.Base
.String
;
3837 program
->Base
.NumInstructions
= ap
.Base
.NumInstructions
;
3838 program
->Base
.NumTemporaries
= ap
.Base
.NumTemporaries
;
3839 program
->Base
.NumParameters
= ap
.Base
.NumParameters
;
3840 program
->Base
.NumAttributes
= ap
.Base
.NumAttributes
;
3841 program
->Base
.NumAddressRegs
= ap
.Base
.NumAddressRegs
;
3842 program
->Base
.NumNativeInstructions
= ap
.Base
.NumNativeInstructions
;
3843 program
->Base
.NumNativeTemporaries
= ap
.Base
.NumNativeTemporaries
;
3844 program
->Base
.NumNativeParameters
= ap
.Base
.NumNativeParameters
;
3845 program
->Base
.NumNativeAttributes
= ap
.Base
.NumNativeAttributes
;
3846 program
->Base
.NumNativeAddressRegs
= ap
.Base
.NumNativeAddressRegs
;
3847 program
->Base
.NumAluInstructions
= ap
.Base
.NumAluInstructions
;
3848 program
->Base
.NumTexInstructions
= ap
.Base
.NumTexInstructions
;
3849 program
->Base
.NumTexIndirections
= ap
.Base
.NumTexIndirections
;
3850 program
->Base
.NumNativeAluInstructions
= ap
.Base
.NumAluInstructions
;
3851 program
->Base
.NumNativeTexInstructions
= ap
.Base
.NumTexInstructions
;
3852 program
->Base
.NumNativeTexIndirections
= ap
.Base
.NumTexIndirections
;
3853 program
->Base
.InputsRead
= ap
.Base
.InputsRead
;
3854 program
->Base
.OutputsWritten
= ap
.Base
.OutputsWritten
;
3855 for (i
= 0; i
< MAX_TEXTURE_IMAGE_UNITS
; i
++) {
3856 program
->Base
.TexturesUsed
[i
] = ap
.TexturesUsed
[i
];
3857 if (ap
.TexturesUsed
[i
])
3858 program
->Base
.SamplersUsed
|= (1 << i
);
3860 program
->Base
.ShadowSamplers
= ap
.ShadowSamplers
;
3861 program
->FogOption
= ap
.FogOption
;
3862 program
->UsesKill
= ap
.UsesKill
;
3864 if (program
->Base
.Instructions
)
3865 _mesa_free(program
->Base
.Instructions
);
3866 program
->Base
.Instructions
= ap
.Base
.Instructions
;
3868 if (program
->Base
.Parameters
)
3869 _mesa_free_parameter_list(program
->Base
.Parameters
);
3870 program
->Base
.Parameters
= ap
.Base
.Parameters
;
3872 /* Append fog instructions now if the program has "OPTION ARB_fog_exp"
3873 * or similar. We used to leave this up to drivers, but it appears
3874 * there's no hardware that wants to do fog in a discrete stage separate
3875 * from the fragment shader.
3877 if (program
->FogOption
!= GL_NONE
) {
3878 _mesa_append_fog_code(ctx
, program
);
3879 program
->FogOption
= GL_NONE
;
3883 _mesa_printf("____________Fragment program %u ________\n", program
->Base
.Id
);
3884 _mesa_print_program(&program
->Base
);
3891 * Parse the vertex program string. If success, update the given
3892 * vertex_program object with the new program. Else, leave the vertex_program
3896 _mesa_parse_arb_vertex_program(GLcontext
*ctx
, GLenum target
,
3897 const GLvoid
*str
, GLsizei len
,
3898 struct gl_vertex_program
*program
)
3900 struct arb_program ap
;
3902 ASSERT(target
== GL_VERTEX_PROGRAM_ARB
);
3904 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
, &ap
)) {
3905 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glProgramString(bad program)");
3909 /* Copy the relevant contents of the arb_program struct into the
3910 * vertex_program struct.
3912 program
->Base
.String
= ap
.Base
.String
;
3913 program
->Base
.NumInstructions
= ap
.Base
.NumInstructions
;
3914 program
->Base
.NumTemporaries
= ap
.Base
.NumTemporaries
;
3915 program
->Base
.NumParameters
= ap
.Base
.NumParameters
;
3916 program
->Base
.NumAttributes
= ap
.Base
.NumAttributes
;
3917 program
->Base
.NumAddressRegs
= ap
.Base
.NumAddressRegs
;
3918 program
->Base
.NumNativeInstructions
= ap
.Base
.NumNativeInstructions
;
3919 program
->Base
.NumNativeTemporaries
= ap
.Base
.NumNativeTemporaries
;
3920 program
->Base
.NumNativeParameters
= ap
.Base
.NumNativeParameters
;
3921 program
->Base
.NumNativeAttributes
= ap
.Base
.NumNativeAttributes
;
3922 program
->Base
.NumNativeAddressRegs
= ap
.Base
.NumNativeAddressRegs
;
3923 program
->Base
.InputsRead
= ap
.Base
.InputsRead
;
3924 program
->Base
.OutputsWritten
= ap
.Base
.OutputsWritten
;
3925 program
->IsPositionInvariant
= ap
.HintPositionInvariant
;
3927 if (program
->Base
.Instructions
)
3928 _mesa_free(program
->Base
.Instructions
);
3929 program
->Base
.Instructions
= ap
.Base
.Instructions
;
3931 if (program
->Base
.Parameters
)
3932 _mesa_free_parameter_list(program
->Base
.Parameters
);
3933 program
->Base
.Parameters
= ap
.Base
.Parameters
;
3936 _mesa_printf("____________Vertex program %u __________\n", program
->Base
.Id
);
3937 _mesa_print_program(&program
->Base
);