2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #define DEBUG_PARSING 0
28 * \file arbprogparse.c
29 * ARB_*_program parser core
34 Notes on program parameters, etc.
36 The instructions we emit will use six kinds of source registers:
38 PROGRAM_INPUT - input registers
39 PROGRAM_TEMPORARY - temp registers
40 PROGRAM_ADDRESS - address/indirect register
41 PROGRAM_SAMPLER - texture sampler
42 PROGRAM_CONSTANT - indexes into program->Parameters, a known constant/literal
43 PROGRAM_STATE_VAR - indexes into program->Parameters, and may actually be:
44 + a state variable, like "state.fog.color", or
45 + a pointer to a "program.local[k]" parameter, or
46 + a pointer to a "program.env[k]" parameter
48 Basically, all the program.local[] and program.env[] values will get mapped
49 into the unified gl_program->Parameters array. This solves the problem of
50 having three separate program parameter arrays.
54 #include "main/glheader.h"
55 #include "main/imports.h"
56 #include "shader/grammar/grammar_mesa.h"
57 #include "arbprogparse.h"
59 #include "programopt.h"
60 #include "prog_parameter.h"
61 #include "prog_statevars.h"
62 #include "main/context.h"
63 #include "main/macros.h"
64 #include "main/mtypes.h"
65 #include "prog_instruction.h"
68 /* For ARB programs, use the NV instruction limits */
69 #define MAX_INSTRUCTIONS MAX2(MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS, \
70 MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS)
74 * This is basically a union of the vertex_program and fragment_program
75 * structs that we can use to parse the program into
77 * XXX we can probably get rid of this entirely someday.
81 struct gl_program Base
;
83 GLuint Position
; /* Just used for error reporting while parsing */
87 /* ARB_vertex_progmra options */
88 GLboolean HintPositionInvariant
;
90 /* ARB_fragment_progmra options */
91 GLenum PrecisionOption
; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
92 GLenum FogOption
; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
94 /* ARB_fragment_program specifics */
95 GLbitfield TexturesUsed
[MAX_TEXTURE_IMAGE_UNITS
];
96 GLbitfield ShadowSamplers
;
97 GLuint NumAluInstructions
;
98 GLuint NumTexInstructions
;
99 GLuint NumTexIndirections
;
107 * Fragment Program Stuff:
108 * -----------------------------------------------------
110 * - things from Michal's email
112 * + not-overflowing floats (don't use parse_integer..)
113 * + can remove range checking in arbparse.c
115 * - check all limits of number of various variables
118 * - test! test! test!
120 * Vertex Program Stuff:
121 * -----------------------------------------------------
122 * - Optimize param array usage and count limits correctly, see spec,
124 * + Record if an array is reference absolutly or relatively (or both)
125 * + For absolute arrays, store a bitmap of accesses
126 * + For single parameters, store an access flag
127 * + After parsing, make a parameter cleanup and merging pass, where
128 * relative arrays are layed out first, followed by abs arrays, and
129 * finally single state.
130 * + Remap offsets for param src and dst registers
131 * + Now we can properly count parameter usage
133 * - Multiple state binding errors in param arrays (see spec, just before
138 * -----------------------------------------------------
139 * - User clipping planes vs. PositionInvariant
140 * - Is it sufficient to just multiply by the mvp to transform in the
141 * PositionInvariant case? Or do we need something more involved?
143 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
144 * - fetch state listed in program_parameters list
145 * + WTF should this go???
146 * + currently in nvvertexec.c and s_nvfragprog.c
148 * - allow for multiple address registers (and fetch address regs properly)
151 * -----------------------------------------------------
152 * - remove any leftover unused grammer.c stuff (dict_ ?)
153 * - fix grammer.c error handling so its not static
154 * - #ifdef around stuff pertaining to extentions
156 * Outstanding Questions:
157 * -----------------------------------------------------
158 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
159 * what gets hacked off because of this:
160 * + VERTEX_ATTRIB_MATRIXINDEX
161 * + VERTEX_ATTRIB_WEIGHT
165 * - When can we fetch env/local params from their own register files, and
166 * when to we have to fetch them into the main state register file?
170 * -----------------------------------------------------
173 /* Changes since moving the file to shader directory
175 2004-III-4 ------------------------------------------------------------
176 - added #include "grammar_mesa.h"
177 - removed grammar specific code part (it resides now in grammar.c)
178 - added GL_ARB_fragment_program_shadow tokens
179 - modified #include "arbparse_syn.h"
180 - major changes inside _mesa_parse_arb_program()
181 - check the program string for '\0' characters
182 - copy the program string to a one-byte-longer location to have
184 - position invariance test (not writing to result.position) moved
188 typedef GLubyte
*production
;
192 * This is the text describing the rules to parse the grammar
194 LONGSTRING
static char arb_grammar_text
[] =
195 #include "arbprogram_syn.h"
199 * These should match up with the values defined in arbprogram.syn
204 - changed and merged V_* and F_* opcode values to OP_*.
205 - added GL_ARB_fragment_program_shadow specific tokens (michal)
207 #define REVISION 0x0a
210 #define FRAGMENT_PROGRAM 0x01
211 #define VERTEX_PROGRAM 0x02
213 /* program section */
215 #define INSTRUCTION 0x02
216 #define DECLARATION 0x03
219 /* GL_ARB_fragment_program option */
220 #define ARB_PRECISION_HINT_FASTEST 0x00
221 #define ARB_PRECISION_HINT_NICEST 0x01
222 #define ARB_FOG_EXP 0x02
223 #define ARB_FOG_EXP2 0x03
224 #define ARB_FOG_LINEAR 0x04
226 /* GL_ARB_vertex_program option */
227 #define ARB_POSITION_INVARIANT 0x05
229 /* GL_ARB_fragment_program_shadow option */
230 #define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
232 /* GL_ARB_draw_buffers option */
233 #define ARB_DRAW_BUFFERS 0x07
235 /* GL_MESA_texture_array option */
236 #define MESA_TEXTURE_ARRAY 0x08
238 /* GL_ARB_fragment_program instruction class */
239 #define OP_ALU_INST 0x00
240 #define OP_TEX_INST 0x01
242 /* GL_ARB_vertex_program instruction class */
245 /* GL_ARB_fragment_program instruction type */
246 #define OP_ALU_VECTOR 0x00
247 #define OP_ALU_SCALAR 0x01
248 #define OP_ALU_BINSC 0x02
249 #define OP_ALU_BIN 0x03
250 #define OP_ALU_TRI 0x04
251 #define OP_ALU_SWZ 0x05
252 #define OP_TEX_SAMPLE 0x06
253 #define OP_TEX_KIL 0x07
255 /* GL_ARB_vertex_program instruction type */
256 #define OP_ALU_ARL 0x08
264 /* GL_ARB_fragment_program instruction code */
266 #define OP_ABS_SAT 0x1B
268 #define OP_FLR_SAT 0x26
270 #define OP_FRC_SAT 0x27
272 #define OP_LIT_SAT 0x2A
274 #define OP_MOV_SAT 0x30
276 #define OP_COS_SAT 0x20
278 #define OP_EX2_SAT 0x25
280 #define OP_LG2_SAT 0x29
282 #define OP_RCP_SAT 0x33
284 #define OP_RSQ_SAT 0x34
286 #define OP_SIN_SAT 0x39
288 #define OP_SCS_SAT 0x36
290 #define OP_POW_SAT 0x32
292 #define OP_ADD_SAT 0x1C
294 #define OP_DP3_SAT 0x21
296 #define OP_DP4_SAT 0x22
298 #define OP_DPH_SAT 0x23
300 #define OP_DST_SAT 0x24
302 #define OP_MAX_SAT 0x2E
304 #define OP_MIN_SAT 0x2F
306 #define OP_MUL_SAT 0x31
308 #define OP_SGE_SAT 0x37
310 #define OP_SLT_SAT 0x3A
312 #define OP_SUB_SAT 0x3B
314 #define OP_XPD_SAT 0x43
316 #define OP_CMP_SAT 0x1E
318 #define OP_LRP_SAT 0x2C
320 #define OP_MAD_SAT 0x2D
322 #define OP_SWZ_SAT 0x3C
324 #define OP_TEX_SAT 0x3E
326 #define OP_TXB_SAT 0x40
328 #define OP_TXP_SAT 0x42
331 /* GL_ARB_vertex_program instruction code */
360 /* fragment attribute binding */
361 #define FRAGMENT_ATTRIB_COLOR 0x01
362 #define FRAGMENT_ATTRIB_TEXCOORD 0x02
363 #define FRAGMENT_ATTRIB_FOGCOORD 0x03
364 #define FRAGMENT_ATTRIB_POSITION 0x04
366 /* vertex attribute binding */
367 #define VERTEX_ATTRIB_POSITION 0x01
368 #define VERTEX_ATTRIB_WEIGHT 0x02
369 #define VERTEX_ATTRIB_NORMAL 0x03
370 #define VERTEX_ATTRIB_COLOR 0x04
371 #define VERTEX_ATTRIB_FOGCOORD 0x05
372 #define VERTEX_ATTRIB_TEXCOORD 0x06
373 #define VERTEX_ATTRIB_MATRIXINDEX 0x07
374 #define VERTEX_ATTRIB_GENERIC 0x08
376 /* fragment result binding */
377 #define FRAGMENT_RESULT_COLOR 0x01
378 #define FRAGMENT_RESULT_DEPTH 0x02
380 /* vertex result binding */
381 #define VERTEX_RESULT_POSITION 0x01
382 #define VERTEX_RESULT_COLOR 0x02
383 #define VERTEX_RESULT_FOGCOORD 0x03
384 #define VERTEX_RESULT_POINTSIZE 0x04
385 #define VERTEX_RESULT_TEXCOORD 0x05
388 #define TEXTARGET_1D 0x01
389 #define TEXTARGET_2D 0x02
390 #define TEXTARGET_3D 0x03
391 #define TEXTARGET_RECT 0x04
392 #define TEXTARGET_CUBE 0x05
393 /* GL_ARB_fragment_program_shadow */
394 #define TEXTARGET_SHADOW1D 0x06
395 #define TEXTARGET_SHADOW2D 0x07
396 #define TEXTARGET_SHADOWRECT 0x08
397 /* GL_MESA_texture_array */
398 #define TEXTARGET_1D_ARRAY 0x09
399 #define TEXTARGET_2D_ARRAY 0x0a
400 #define TEXTARGET_SHADOW1D_ARRAY 0x0b
401 #define TEXTARGET_SHADOW2D_ARRAY 0x0c
404 #define FACE_FRONT 0x00
405 #define FACE_BACK 0x01
408 #define COLOR_PRIMARY 0x00
409 #define COLOR_SECONDARY 0x01
412 #define COMPONENT_X 0x00
413 #define COMPONENT_Y 0x01
414 #define COMPONENT_Z 0x02
415 #define COMPONENT_W 0x03
416 #define COMPONENT_0 0x04
417 #define COMPONENT_1 0x05
419 /* array index type */
420 #define ARRAY_INDEX_ABSOLUTE 0x00
421 #define ARRAY_INDEX_RELATIVE 0x01
424 #define MATRIX_MODELVIEW 0x01
425 #define MATRIX_PROJECTION 0x02
426 #define MATRIX_MVP 0x03
427 #define MATRIX_TEXTURE 0x04
428 #define MATRIX_PALETTE 0x05
429 #define MATRIX_PROGRAM 0x06
431 /* matrix modifier */
432 #define MATRIX_MODIFIER_IDENTITY 0x00
433 #define MATRIX_MODIFIER_INVERSE 0x01
434 #define MATRIX_MODIFIER_TRANSPOSE 0x02
435 #define MATRIX_MODIFIER_INVTRANS 0x03
438 #define CONSTANT_SCALAR 0x01
439 #define CONSTANT_VECTOR 0x02
441 /* program param type */
442 #define PROGRAM_PARAM_ENV 0x01
443 #define PROGRAM_PARAM_LOCAL 0x02
446 #define REGISTER_ATTRIB 0x01
447 #define REGISTER_PARAM 0x02
448 #define REGISTER_RESULT 0x03
449 #define REGISTER_ESTABLISHED_NAME 0x04
452 #define PARAM_NULL 0x00
453 #define PARAM_ARRAY_ELEMENT 0x01
454 #define PARAM_STATE_ELEMENT 0x02
455 #define PARAM_PROGRAM_ELEMENT 0x03
456 #define PARAM_PROGRAM_ELEMENTS 0x04
457 #define PARAM_CONSTANT 0x05
459 /* param state property */
460 #define STATE_MATERIAL_PARSER 0x01
461 #define STATE_LIGHT_PARSER 0x02
462 #define STATE_LIGHT_MODEL 0x03
463 #define STATE_LIGHT_PROD 0x04
464 #define STATE_FOG 0x05
465 #define STATE_MATRIX_ROWS 0x06
466 /* GL_ARB_fragment_program */
467 #define STATE_TEX_ENV 0x07
468 #define STATE_DEPTH 0x08
469 /* GL_ARB_vertex_program */
470 #define STATE_TEX_GEN 0x09
471 #define STATE_CLIP_PLANE 0x0A
472 #define STATE_POINT 0x0B
474 /* state material property */
475 #define MATERIAL_AMBIENT 0x01
476 #define MATERIAL_DIFFUSE 0x02
477 #define MATERIAL_SPECULAR 0x03
478 #define MATERIAL_EMISSION 0x04
479 #define MATERIAL_SHININESS 0x05
481 /* state light property */
482 #define LIGHT_AMBIENT 0x01
483 #define LIGHT_DIFFUSE 0x02
484 #define LIGHT_SPECULAR 0x03
485 #define LIGHT_POSITION 0x04
486 #define LIGHT_ATTENUATION 0x05
487 #define LIGHT_HALF 0x06
488 #define LIGHT_SPOT_DIRECTION 0x07
490 /* state light model property */
491 #define LIGHT_MODEL_AMBIENT 0x01
492 #define LIGHT_MODEL_SCENECOLOR 0x02
494 /* state light product property */
495 #define LIGHT_PROD_AMBIENT 0x01
496 #define LIGHT_PROD_DIFFUSE 0x02
497 #define LIGHT_PROD_SPECULAR 0x03
499 /* state texture environment property */
500 #define TEX_ENV_COLOR 0x01
502 /* state texture generation coord property */
503 #define TEX_GEN_EYE 0x01
504 #define TEX_GEN_OBJECT 0x02
506 /* state fog property */
507 #define FOG_COLOR 0x01
508 #define FOG_PARAMS 0x02
510 /* state depth property */
511 #define DEPTH_RANGE 0x01
513 /* state point parameters property */
514 #define POINT_SIZE 0x01
515 #define POINT_ATTENUATION 0x02
523 /* GL_ARB_vertex_program */
526 /*-----------------------------------------------------------------------
527 * From here on down is the semantic checking portion
532 * Variable Table Handling functions
547 * Setting an explicit field for each of the binding properties is a bit
548 * wasteful of space, but it should be much more clear when reading later on..
552 const GLubyte
*name
; /* don't free() - no need */
554 GLuint address_binding
; /* The index of the address register we should
556 GLuint attrib_binding
; /* For type vt_attrib, see nvfragprog.h for values */
557 GLuint attrib_is_generic
; /* If the attrib was specified through a generic
559 GLuint temp_binding
; /* The index of the temp register we are to use */
560 GLuint output_binding
; /* Output/result register number */
561 struct var_cache
*alias_binding
; /* For type vt_alias, points to the var_cache entry
562 * that this is aliased to */
563 GLuint param_binding_type
; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
564 * PROGRAM_ENV_PARAM} */
565 GLuint param_binding_begin
; /* This is the offset into the program_parameter_list where
566 * the tokens representing our bound state (or constants)
568 GLuint param_binding_length
; /* This is how many entries in the the program_parameter_list
569 * we take up with our state tokens or constants. Note that
570 * this is _not_ the same as the number of param registers
571 * we eventually use */
572 struct var_cache
*next
;
576 var_cache_create (struct var_cache
**va
)
578 *va
= (struct var_cache
*) _mesa_malloc (sizeof (struct var_cache
));
581 (**va
).type
= vt_none
;
582 (**va
).attrib_binding
= ~0;
583 (**va
).attrib_is_generic
= 0;
584 (**va
).temp_binding
= ~0;
585 (**va
).output_binding
= ~0;
586 (**va
).param_binding_type
= ~0;
587 (**va
).param_binding_begin
= ~0;
588 (**va
).param_binding_length
= ~0;
589 (**va
).alias_binding
= NULL
;
595 var_cache_destroy (struct var_cache
**va
)
598 var_cache_destroy (&(**va
).next
);
605 var_cache_append (struct var_cache
**va
, struct var_cache
*nv
)
608 var_cache_append (&(**va
).next
, nv
);
613 static struct var_cache
*
614 var_cache_find (struct var_cache
*va
, const GLubyte
* name
)
616 /*struct var_cache *first = va;*/
619 if (!_mesa_strcmp ( (const char*) name
, (const char*) va
->name
)) {
620 if (va
->type
== vt_alias
)
621 return va
->alias_binding
;
634 * Called when an error is detected while parsing/compiling a program.
635 * Sets the ctx->Program.ErrorString field to descript and records a
636 * GL_INVALID_OPERATION error.
637 * \param position position of error in program string
638 * \param descrip verbose error description
641 program_error(GLcontext
*ctx
, GLint position
, const char *descrip
)
644 const char *prefix
= "glProgramString(", *suffix
= ")";
645 char *str
= (char *) _mesa_malloc(_mesa_strlen(descrip
) +
646 _mesa_strlen(prefix
) +
647 _mesa_strlen(suffix
) + 1);
649 _mesa_sprintf(str
, "%s%s%s", prefix
, descrip
, suffix
);
650 _mesa_error(ctx
, GL_INVALID_OPERATION
, str
);
654 _mesa_set_program_error(ctx
, position
, descrip
);
659 * As above, but with an extra string parameter for more info.
662 program_error2(GLcontext
*ctx
, GLint position
, const char *descrip
,
666 const char *prefix
= "glProgramString(", *suffix
= ")";
667 char *str
= (char *) _mesa_malloc(_mesa_strlen(descrip
) +
670 _mesa_strlen(prefix
) +
671 _mesa_strlen(suffix
) + 1);
673 _mesa_sprintf(str
, "%s%s: %s%s", prefix
, descrip
, var
, suffix
);
674 _mesa_error(ctx
, GL_INVALID_OPERATION
, str
);
679 char *str
= (char *) _mesa_malloc(_mesa_strlen(descrip
) +
681 _mesa_strlen(var
) + 1);
683 _mesa_sprintf(str
, "%s: %s", descrip
, var
);
685 _mesa_set_program_error(ctx
, position
, str
);
695 * constructs an integer from 4 GLubytes in LE format
698 parse_position (const GLubyte
** inst
)
702 value
= (GLuint
) (*(*inst
)++);
703 value
+= (GLuint
) (*(*inst
)++) * 0x100;
704 value
+= (GLuint
) (*(*inst
)++) * 0x10000;
705 value
+= (GLuint
) (*(*inst
)++) * 0x1000000;
711 * This will, given a string, lookup the string as a variable name in the
712 * var cache. If the name is found, the var cache node corresponding to the
713 * var name is returned. If it is not found, a new entry is allocated
715 * \param I Points into the binary array where the string identifier begins
716 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
717 * \return The location on the var_cache corresponding the the string starting at I
719 static struct var_cache
*
720 parse_string (const GLubyte
** inst
, struct var_cache
**vc_head
,
721 struct arb_program
*Program
, GLuint
* found
)
723 const GLubyte
*i
= *inst
;
724 struct var_cache
*va
= NULL
;
727 *inst
+= _mesa_strlen ((char *) i
) + 1;
729 va
= var_cache_find (*vc_head
, i
);
737 var_cache_create (&va
);
738 va
->name
= (const GLubyte
*) i
;
740 var_cache_append (vc_head
, va
);
746 parse_string_without_adding (const GLubyte
** inst
, struct arb_program
*Program
)
748 const GLubyte
*i
= *inst
;
751 *inst
+= _mesa_strlen ((char *) i
) + 1;
757 * \return -1 if we parse '-', return 1 otherwise
760 parse_sign (const GLubyte
** inst
)
762 /*return *(*inst)++ != '+'; */
768 else if (**inst
== '+') {
777 * parses and returns signed integer
780 parse_integer (const GLubyte
** inst
, struct arb_program
*Program
)
785 /* check if *inst points to '+' or '-'
786 * if yes, grab the sign and increment *inst
788 sign
= parse_sign (inst
);
790 /* now check if *inst points to 0
791 * if yes, increment the *inst and return the default value
798 /* parse the integer as you normally would do it */
799 value
= _mesa_atoi (parse_string_without_adding (inst
, Program
));
801 /* now, after terminating 0 there is a position
802 * to parse it - parse_position()
804 Program
->Position
= parse_position (inst
);
810 Accumulate this string of digits, and return them as
811 a large integer represented in floating point (for range).
812 If scale is not NULL, also accumulates a power-of-ten
813 integer scale factor that represents the number of digits
817 parse_float_string(const GLubyte
** inst
, struct arb_program
*Program
, GLdouble
*scale
)
819 GLdouble value
= 0.0;
820 GLdouble oscale
= 1.0;
822 if (**inst
== 0) { /* this string of digits is empty-- do nothing */
825 else { /* nonempty string-- parse out the digits */
826 while (**inst
>= '0' && **inst
<= '9') {
827 GLubyte digit
= *((*inst
)++);
828 value
= value
* 10.0 + (GLint
) (digit
- '0');
831 assert(**inst
== 0); /* integer string should end with 0 */
832 (*inst
)++; /* skip over terminating 0 */
833 Program
->Position
= parse_position(inst
); /* skip position (from integer) */
841 Parse an unsigned floating-point number from this stream of tokenized
842 characters. Example floating-point formats supported:
850 parse_float (const GLubyte
** inst
, struct arb_program
*Program
)
853 GLdouble whole
, fraction
, fracScale
= 1.0;
855 whole
= parse_float_string(inst
, Program
, 0);
856 fraction
= parse_float_string(inst
, Program
, &fracScale
);
858 /* Parse signed exponent */
859 exponent
= parse_integer(inst
, Program
); /* This is the exponent */
861 /* Assemble parts of floating-point number: */
862 return (GLfloat
) ((whole
+ fraction
/ fracScale
) *
863 _mesa_pow(10.0, (GLfloat
) exponent
));
870 parse_signed_float (const GLubyte
** inst
, struct arb_program
*Program
)
872 GLint sign
= parse_sign (inst
);
873 GLfloat value
= parse_float (inst
, Program
);
878 * This picks out a constant value from the parsed array. The constant vector is r
879 * returned in the *values array, which should be of length 4.
881 * \param values - The 4 component vector with the constant value in it
884 parse_constant (const GLubyte
** inst
, GLfloat
*values
, struct arb_program
*Program
,
887 GLuint components
, i
;
890 switch (*(*inst
)++) {
891 case CONSTANT_SCALAR
:
892 if (use
== GL_TRUE
) {
895 values
[2] = values
[3] = parse_float (inst
, Program
);
900 values
[2] = values
[3] = parse_signed_float (inst
, Program
);
904 case CONSTANT_VECTOR
:
905 values
[0] = values
[1] = values
[2] = 0;
907 components
= *(*inst
)++;
908 for (i
= 0; i
< components
; i
++) {
909 values
[i
] = parse_signed_float (inst
, Program
);
916 * \param offset The offset from the address register that we should
919 * \return 0 on sucess, 1 on error
922 parse_relative_offset(GLcontext
*ctx
, const GLubyte
**inst
,
923 struct arb_program
*Program
, GLint
*offset
)
926 *offset
= parse_integer(inst
, Program
);
931 * \param color 0 if color type is primary, 1 if color type is secondary
932 * \return 0 on sucess, 1 on error
935 parse_color_type (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
938 (void) ctx
; (void) Program
;
939 *color
= *(*inst
)++ != COLOR_PRIMARY
;
944 * Get an integer corresponding to a generic vertex attribute.
946 * \return 0 on sucess, 1 on error
949 parse_generic_attrib_num(GLcontext
*ctx
, const GLubyte
** inst
,
950 struct arb_program
*Program
, GLuint
*attrib
)
952 GLint i
= parse_integer(inst
, Program
);
954 if ((i
< 0) || (i
>= MAX_VERTEX_PROGRAM_ATTRIBS
))
956 program_error(ctx
, Program
->Position
,
957 "Invalid generic vertex attribute index");
961 *attrib
= (GLuint
) i
;
968 * \param color The index of the color buffer to write into
969 * \return 0 on sucess, 1 on error
972 parse_output_color_num (GLcontext
* ctx
, const GLubyte
** inst
,
973 struct arb_program
*Program
, GLuint
* color
)
975 GLint i
= parse_integer (inst
, Program
);
977 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxDrawBuffers
)) {
978 program_error(ctx
, Program
->Position
, "Invalid draw buffer index");
988 * \param coord The texture unit index
989 * \return 0 on sucess, 1 on error
992 parse_texcoord_num (GLcontext
* ctx
, const GLubyte
** inst
,
993 struct arb_program
*Program
, GLuint
* coord
)
995 GLint i
= parse_integer (inst
, Program
);
997 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxTextureUnits
)) {
998 program_error(ctx
, Program
->Position
, "Invalid texture unit index");
1002 *coord
= (GLuint
) i
;
1007 * \param coord The weight index
1008 * \return 0 on sucess, 1 on error
1011 parse_weight_num (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
1014 *coord
= parse_integer (inst
, Program
);
1016 if ((*coord
< 0) || (*coord
>= 1)) {
1017 program_error(ctx
, Program
->Position
, "Invalid weight index");
1025 * \param coord The clip plane index
1026 * \return 0 on sucess, 1 on error
1029 parse_clipplane_num (GLcontext
* ctx
, const GLubyte
** inst
,
1030 struct arb_program
*Program
, GLint
* coord
)
1032 *coord
= parse_integer (inst
, Program
);
1034 if ((*coord
< 0) || (*coord
>= (GLint
) ctx
->Const
.MaxClipPlanes
)) {
1035 program_error(ctx
, Program
->Position
, "Invalid clip plane index");
1044 * \return 0 on front face, 1 on back face
1047 parse_face_type (const GLubyte
** inst
)
1049 switch (*(*inst
)++) {
1061 * Given a matrix and a modifier token on the binary array, return tokens
1062 * that _mesa_fetch_state() [program.c] can understand.
1064 * \param matrix - the matrix we are talking about
1065 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
1066 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
1067 * \return 0 on sucess, 1 on failure
1070 parse_matrix (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
1071 GLint
* matrix
, GLint
* matrix_idx
, GLint
* matrix_modifier
)
1073 GLubyte mat
= *(*inst
)++;
1078 case MATRIX_MODELVIEW
:
1079 *matrix
= STATE_MODELVIEW_MATRIX
;
1080 *matrix_idx
= parse_integer (inst
, Program
);
1081 if (*matrix_idx
> 0) {
1082 program_error(ctx
, Program
->Position
,
1083 "ARB_vertex_blend not supported");
1088 case MATRIX_PROJECTION
:
1089 *matrix
= STATE_PROJECTION_MATRIX
;
1093 *matrix
= STATE_MVP_MATRIX
;
1096 case MATRIX_TEXTURE
:
1097 *matrix
= STATE_TEXTURE_MATRIX
;
1098 *matrix_idx
= parse_integer (inst
, Program
);
1099 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxTextureUnits
) {
1100 program_error(ctx
, Program
->Position
, "Invalid Texture Unit");
1101 /* bad *matrix_id */
1106 /* This is not currently supported (ARB_matrix_palette) */
1107 case MATRIX_PALETTE
:
1108 *matrix_idx
= parse_integer (inst
, Program
);
1109 program_error(ctx
, Program
->Position
,
1110 "ARB_matrix_palette not supported");
1114 case MATRIX_PROGRAM
:
1115 *matrix
= STATE_PROGRAM_MATRIX
;
1116 *matrix_idx
= parse_integer (inst
, Program
);
1117 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxProgramMatrices
) {
1118 program_error(ctx
, Program
->Position
, "Invalid Program Matrix");
1119 /* bad *matrix_idx */
1125 switch (*(*inst
)++) {
1126 case MATRIX_MODIFIER_IDENTITY
:
1127 *matrix_modifier
= 0;
1129 case MATRIX_MODIFIER_INVERSE
:
1130 *matrix_modifier
= STATE_MATRIX_INVERSE
;
1132 case MATRIX_MODIFIER_TRANSPOSE
:
1133 *matrix_modifier
= STATE_MATRIX_TRANSPOSE
;
1135 case MATRIX_MODIFIER_INVTRANS
:
1136 *matrix_modifier
= STATE_MATRIX_INVTRANS
;
1145 * This parses a state string (rather, the binary version of it) into
1146 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1148 * \param inst - the start in the binary arry to start working from
1149 * \param state_tokens - the storage for the 6-token state description
1150 * \return - 0 on sucess, 1 on error
1153 parse_state_single_item (GLcontext
* ctx
, const GLubyte
** inst
,
1154 struct arb_program
*Program
,
1155 gl_state_index state_tokens
[STATE_LENGTH
])
1157 GLubyte token
= *(*inst
)++;
1160 case STATE_MATERIAL_PARSER
:
1161 state_tokens
[0] = STATE_MATERIAL
;
1162 state_tokens
[1] = parse_face_type (inst
);
1163 switch (*(*inst
)++) {
1164 case MATERIAL_AMBIENT
:
1165 state_tokens
[2] = STATE_AMBIENT
;
1167 case MATERIAL_DIFFUSE
:
1168 state_tokens
[2] = STATE_DIFFUSE
;
1170 case MATERIAL_SPECULAR
:
1171 state_tokens
[2] = STATE_SPECULAR
;
1173 case MATERIAL_EMISSION
:
1174 state_tokens
[2] = STATE_EMISSION
;
1176 case MATERIAL_SHININESS
:
1177 state_tokens
[2] = STATE_SHININESS
;
1182 case STATE_LIGHT_PARSER
:
1183 state_tokens
[0] = STATE_LIGHT
;
1184 state_tokens
[1] = parse_integer (inst
, Program
);
1186 /* Check the value of state_tokens[1] against the # of lights */
1187 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1188 program_error(ctx
, Program
->Position
, "Invalid Light Number");
1189 /* bad state_tokens[1] */
1193 switch (*(*inst
)++) {
1195 state_tokens
[2] = STATE_AMBIENT
;
1198 state_tokens
[2] = STATE_DIFFUSE
;
1200 case LIGHT_SPECULAR
:
1201 state_tokens
[2] = STATE_SPECULAR
;
1203 case LIGHT_POSITION
:
1204 state_tokens
[2] = STATE_POSITION
;
1206 case LIGHT_ATTENUATION
:
1207 state_tokens
[2] = STATE_ATTENUATION
;
1210 state_tokens
[2] = STATE_HALF_VECTOR
;
1212 case LIGHT_SPOT_DIRECTION
:
1213 state_tokens
[2] = STATE_SPOT_DIRECTION
;
1218 case STATE_LIGHT_MODEL
:
1219 switch (*(*inst
)++) {
1220 case LIGHT_MODEL_AMBIENT
:
1221 state_tokens
[0] = STATE_LIGHTMODEL_AMBIENT
;
1223 case LIGHT_MODEL_SCENECOLOR
:
1224 state_tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
1225 state_tokens
[1] = parse_face_type (inst
);
1230 case STATE_LIGHT_PROD
:
1231 state_tokens
[0] = STATE_LIGHTPROD
;
1232 state_tokens
[1] = parse_integer (inst
, Program
);
1234 /* Check the value of state_tokens[1] against the # of lights */
1235 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1236 program_error(ctx
, Program
->Position
, "Invalid Light Number");
1237 /* bad state_tokens[1] */
1241 state_tokens
[2] = parse_face_type (inst
);
1242 switch (*(*inst
)++) {
1243 case LIGHT_PROD_AMBIENT
:
1244 state_tokens
[3] = STATE_AMBIENT
;
1246 case LIGHT_PROD_DIFFUSE
:
1247 state_tokens
[3] = STATE_DIFFUSE
;
1249 case LIGHT_PROD_SPECULAR
:
1250 state_tokens
[3] = STATE_SPECULAR
;
1257 switch (*(*inst
)++) {
1259 state_tokens
[0] = STATE_FOG_COLOR
;
1262 state_tokens
[0] = STATE_FOG_PARAMS
;
1268 state_tokens
[1] = parse_integer (inst
, Program
);
1269 switch (*(*inst
)++) {
1271 state_tokens
[0] = STATE_TEXENV_COLOR
;
1280 state_tokens
[0] = STATE_TEXGEN
;
1281 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1283 if (parse_texcoord_num (ctx
, inst
, Program
, &coord
))
1285 state_tokens
[1] = coord
;
1290 /* 0 - s, 1 - t, 2 - r, 3 - q */
1293 if (type
== TEX_GEN_EYE
) {
1296 state_tokens
[2] = STATE_TEXGEN_EYE_S
;
1299 state_tokens
[2] = STATE_TEXGEN_EYE_T
;
1302 state_tokens
[2] = STATE_TEXGEN_EYE_R
;
1305 state_tokens
[2] = STATE_TEXGEN_EYE_Q
;
1308 _mesa_problem(ctx
, "bad texgen component in "
1309 "parse_state_single_item()");
1315 state_tokens
[2] = STATE_TEXGEN_OBJECT_S
;
1318 state_tokens
[2] = STATE_TEXGEN_OBJECT_T
;
1321 state_tokens
[2] = STATE_TEXGEN_OBJECT_R
;
1324 state_tokens
[2] = STATE_TEXGEN_OBJECT_Q
;
1327 _mesa_problem(ctx
, "bad texgen component in "
1328 "parse_state_single_item()");
1335 switch (*(*inst
)++) {
1337 state_tokens
[0] = STATE_DEPTH_RANGE
;
1342 case STATE_CLIP_PLANE
:
1343 state_tokens
[0] = STATE_CLIPPLANE
;
1344 if (parse_clipplane_num (ctx
, inst
, Program
,
1345 (GLint
*) &state_tokens
[1]))
1350 switch (*(*inst
)++) {
1352 state_tokens
[0] = STATE_POINT_SIZE
;
1355 case POINT_ATTENUATION
:
1356 state_tokens
[0] = STATE_POINT_ATTENUATION
;
1361 /* XXX: I think this is the correct format for a matrix row */
1362 case STATE_MATRIX_ROWS
:
1363 if (parse_matrix(ctx
, inst
, Program
,
1364 (GLint
*) &state_tokens
[0],
1365 (GLint
*) &state_tokens
[1],
1366 (GLint
*) &state_tokens
[4]))
1369 state_tokens
[2] = parse_integer (inst
, Program
); /* The first row to grab */
1371 if ((**inst
) != 0) { /* Either the last row, 0 */
1372 state_tokens
[3] = parse_integer (inst
, Program
);
1373 if (state_tokens
[3] < state_tokens
[2]) {
1374 program_error(ctx
, Program
->Position
,
1375 "Second matrix index less than the first");
1376 /* state_tokens[4] vs. state_tokens[3] */
1381 state_tokens
[3] = state_tokens
[2];
1391 * This parses a state string (rather, the binary version of it) into
1392 * a 6-token similar for the state fetching code in program.c
1394 * One might ask, why fetch these parameters into just like you fetch
1395 * state when they are already stored in other places?
1397 * Because of array offsets -> We can stick env/local parameters in the
1398 * middle of a parameter array and then index someplace into the array
1401 * One optimization might be to only do this for the cases where the
1402 * env/local parameters end up inside of an array, and leave the
1403 * single parameters (or arrays of pure env/local pareameters) in their
1404 * respective register files.
1406 * For ENV parameters, the format is:
1407 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1408 * state_tokens[1] = STATE_ENV
1409 * state_tokens[2] = the parameter index
1411 * for LOCAL parameters, the format is:
1412 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1413 * state_tokens[1] = STATE_LOCAL
1414 * state_tokens[2] = the parameter index
1416 * \param inst - the start in the binary arry to start working from
1417 * \param state_tokens - the storage for the 6-token state description
1418 * \return - 0 on sucess, 1 on failure
1421 parse_program_single_item (GLcontext
* ctx
, const GLubyte
** inst
,
1422 struct arb_program
*Program
,
1423 gl_state_index state_tokens
[STATE_LENGTH
])
1425 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1426 state_tokens
[0] = STATE_FRAGMENT_PROGRAM
;
1428 state_tokens
[0] = STATE_VERTEX_PROGRAM
;
1431 switch (*(*inst
)++) {
1432 case PROGRAM_PARAM_ENV
:
1433 state_tokens
[1] = STATE_ENV
;
1434 state_tokens
[2] = parse_integer (inst
, Program
);
1436 /* Check state_tokens[2] against the number of ENV parameters available */
1437 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1438 (state_tokens
[2] >= (GLint
) ctx
->Const
.FragmentProgram
.MaxEnvParams
))
1440 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1441 (state_tokens
[2] >= (GLint
) ctx
->Const
.VertexProgram
.MaxEnvParams
))) {
1442 program_error(ctx
, Program
->Position
,
1443 "Invalid Program Env Parameter");
1444 /* bad state_tokens[2] */
1450 case PROGRAM_PARAM_LOCAL
:
1451 state_tokens
[1] = STATE_LOCAL
;
1452 state_tokens
[2] = parse_integer (inst
, Program
);
1454 /* Check state_tokens[2] against the number of LOCAL parameters available */
1455 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1456 (state_tokens
[2] >= (GLint
) ctx
->Const
.FragmentProgram
.MaxLocalParams
))
1458 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1459 (state_tokens
[2] >= (GLint
) ctx
->Const
.VertexProgram
.MaxLocalParams
))) {
1460 program_error(ctx
, Program
->Position
,
1461 "Invalid Program Local Parameter");
1462 /* bad state_tokens[2] */
1472 * For ARB_vertex_program, programs are not allowed to use both an explicit
1473 * vertex attribute and a generic vertex attribute corresponding to the same
1474 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1476 * This will walk our var_cache and make sure that nobody does anything fishy.
1478 * \return 0 on sucess, 1 on error
1481 generic_attrib_check(struct var_cache
*vc_head
)
1484 struct var_cache
*curr
;
1485 GLboolean explicitAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
],
1486 genericAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
];
1488 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1489 explicitAttrib
[a
] = GL_FALSE
;
1490 genericAttrib
[a
] = GL_FALSE
;
1495 if (curr
->type
== vt_attrib
) {
1496 if (curr
->attrib_is_generic
)
1497 genericAttrib
[ curr
->attrib_binding
] = GL_TRUE
;
1499 explicitAttrib
[ curr
->attrib_binding
] = GL_TRUE
;
1505 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1506 if ((explicitAttrib
[a
]) && (genericAttrib
[a
]))
1514 * This will handle the binding side of an ATTRIB var declaration
1516 * \param inputReg returns the input register index, one of the
1517 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
1518 * \return returns 0 on success, 1 on error
1521 parse_attrib_binding(GLcontext
* ctx
, const GLubyte
** inst
,
1522 struct arb_program
*Program
,
1523 GLuint
*inputReg
, GLuint
*is_generic
)
1529 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1530 switch (*(*inst
)++) {
1531 case FRAGMENT_ATTRIB_COLOR
:
1534 err
= parse_color_type (ctx
, inst
, Program
, &coord
);
1535 *inputReg
= FRAG_ATTRIB_COL0
+ coord
;
1538 case FRAGMENT_ATTRIB_TEXCOORD
:
1540 GLuint texcoord
= 0;
1541 err
= parse_texcoord_num (ctx
, inst
, Program
, &texcoord
);
1542 *inputReg
= FRAG_ATTRIB_TEX0
+ texcoord
;
1545 case FRAGMENT_ATTRIB_FOGCOORD
:
1546 *inputReg
= FRAG_ATTRIB_FOGC
;
1548 case FRAGMENT_ATTRIB_POSITION
:
1549 *inputReg
= FRAG_ATTRIB_WPOS
;
1557 switch (*(*inst
)++) {
1558 case VERTEX_ATTRIB_POSITION
:
1559 *inputReg
= VERT_ATTRIB_POS
;
1562 case VERTEX_ATTRIB_WEIGHT
:
1565 err
= parse_weight_num (ctx
, inst
, Program
, &weight
);
1566 *inputReg
= VERT_ATTRIB_WEIGHT
;
1568 /* hack for Warcraft (see bug 8060) */
1569 _mesa_warning(ctx
, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
1572 program_error(ctx
, Program
->Position
,
1573 "ARB_vertex_blend not supported");
1578 case VERTEX_ATTRIB_NORMAL
:
1579 *inputReg
= VERT_ATTRIB_NORMAL
;
1582 case VERTEX_ATTRIB_COLOR
:
1585 err
= parse_color_type (ctx
, inst
, Program
, &color
);
1587 *inputReg
= VERT_ATTRIB_COLOR1
;
1590 *inputReg
= VERT_ATTRIB_COLOR0
;
1595 case VERTEX_ATTRIB_FOGCOORD
:
1596 *inputReg
= VERT_ATTRIB_FOG
;
1599 case VERTEX_ATTRIB_TEXCOORD
:
1602 err
= parse_texcoord_num (ctx
, inst
, Program
, &unit
);
1603 *inputReg
= VERT_ATTRIB_TEX0
+ unit
;
1607 case VERTEX_ATTRIB_MATRIXINDEX
:
1608 /* Not supported at this time */
1610 const char *msg
= "ARB_palette_matrix not supported";
1611 parse_integer (inst
, Program
);
1612 program_error(ctx
, Program
->Position
, msg
);
1616 case VERTEX_ATTRIB_GENERIC
:
1619 err
= parse_generic_attrib_num(ctx
, inst
, Program
, &attrib
);
1622 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1623 * attributes do not alias the conventional vertex
1627 *inputReg
= attrib
+ VERT_ATTRIB_GENERIC0
;
1641 program_error(ctx
, Program
->Position
, "Bad attribute binding");
1649 * This translates between a binary token for an output variable type
1650 * and the mesa token for the same thing.
1652 * \param inst The parsed tokens
1653 * \param outputReg Returned index/number of the output register,
1654 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
1657 parse_result_binding(GLcontext
*ctx
, const GLubyte
**inst
,
1658 GLuint
*outputReg
, struct arb_program
*Program
)
1660 const GLubyte token
= *(*inst
)++;
1663 case FRAGMENT_RESULT_COLOR
:
1664 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1667 /* This gets result of the color buffer we're supposed to
1668 * draw into. This pertains to GL_ARB_draw_buffers.
1670 parse_output_color_num(ctx
, inst
, Program
, &out_color
);
1671 ASSERT(out_color
< MAX_DRAW_BUFFERS
);
1672 *outputReg
= FRAG_RESULT_COLR
;
1675 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1676 *outputReg
= VERT_RESULT_HPOS
;
1680 case FRAGMENT_RESULT_DEPTH
:
1681 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1682 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
1683 *outputReg
= FRAG_RESULT_DEPR
;
1686 /* for vtx programs, this is VERTEX_RESULT_COLOR */
1688 GLuint face_type
= parse_face_type(inst
);
1689 GLint err
= parse_color_type(ctx
, inst
, Program
, &color_type
);
1696 *outputReg
= VERT_RESULT_BFC1
; /* secondary color */
1699 *outputReg
= VERT_RESULT_BFC0
; /* primary color */
1705 *outputReg
= VERT_RESULT_COL1
; /* secondary color */
1709 *outputReg
= VERT_RESULT_COL0
; /* primary color */
1715 case VERTEX_RESULT_FOGCOORD
:
1716 *outputReg
= VERT_RESULT_FOGC
;
1719 case VERTEX_RESULT_POINTSIZE
:
1720 *outputReg
= VERT_RESULT_PSIZ
;
1723 case VERTEX_RESULT_TEXCOORD
:
1726 if (parse_texcoord_num (ctx
, inst
, Program
, &unit
))
1728 *outputReg
= VERT_RESULT_TEX0
+ unit
;
1733 Program
->Base
.OutputsWritten
|= (1 << *outputReg
);
1740 * This handles the declaration of ATTRIB variables
1743 * parse_vert_attrib_binding(), or something like that
1745 * \return 0 on sucess, 1 on error
1748 parse_attrib (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1749 struct arb_program
*Program
)
1752 struct var_cache
*attrib_var
;
1754 attrib_var
= parse_string (inst
, vc_head
, Program
, &found
);
1755 Program
->Position
= parse_position (inst
);
1757 program_error2(ctx
, Program
->Position
,
1758 "Duplicate variable declaration",
1759 (char *) attrib_var
->name
);
1763 attrib_var
->type
= vt_attrib
;
1765 if (parse_attrib_binding(ctx
, inst
, Program
, &attrib_var
->attrib_binding
,
1766 &attrib_var
->attrib_is_generic
))
1769 if (generic_attrib_check(*vc_head
)) {
1770 program_error(ctx
, Program
->Position
,
1771 "Cannot use both a generic vertex attribute "
1772 "and a specific attribute of the same type");
1776 Program
->Base
.NumAttributes
++;
1781 * \param use -- TRUE if we're called when declaring implicit parameters,
1782 * FALSE if we're declaraing variables. This has to do with
1783 * if we get a signed or unsigned float for scalar constants
1786 parse_param_elements (GLcontext
* ctx
, const GLubyte
** inst
,
1787 struct var_cache
*param_var
,
1788 struct arb_program
*Program
, GLboolean use
)
1792 gl_state_index state_tokens
[STATE_LENGTH
] = {0, 0, 0, 0, 0};
1793 GLfloat const_values
[4];
1795 GLubyte token
= *(*inst
)++;
1798 case PARAM_STATE_ELEMENT
:
1799 if (parse_state_single_item (ctx
, inst
, Program
, state_tokens
))
1802 /* If we adding STATE_MATRIX that has multiple rows, we need to
1803 * unroll it and call _mesa_add_state_reference() for each row
1805 if ((state_tokens
[0] == STATE_MODELVIEW_MATRIX
||
1806 state_tokens
[0] == STATE_PROJECTION_MATRIX
||
1807 state_tokens
[0] == STATE_MVP_MATRIX
||
1808 state_tokens
[0] == STATE_TEXTURE_MATRIX
||
1809 state_tokens
[0] == STATE_PROGRAM_MATRIX
)
1810 && (state_tokens
[2] != state_tokens
[3])) {
1812 const GLint first_row
= state_tokens
[2];
1813 const GLint last_row
= state_tokens
[3];
1815 for (row
= first_row
; row
<= last_row
; row
++) {
1816 state_tokens
[2] = state_tokens
[3] = row
;
1818 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1820 if (param_var
->param_binding_begin
== ~0U)
1821 param_var
->param_binding_begin
= idx
;
1822 param_var
->param_binding_length
++;
1823 Program
->Base
.NumParameters
++;
1827 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1829 if (param_var
->param_binding_begin
== ~0U)
1830 param_var
->param_binding_begin
= idx
;
1831 param_var
->param_binding_length
++;
1832 Program
->Base
.NumParameters
++;
1836 case PARAM_PROGRAM_ELEMENT
:
1837 if (parse_program_single_item (ctx
, inst
, Program
, state_tokens
))
1839 idx
= _mesa_add_state_reference (Program
->Base
.Parameters
, state_tokens
);
1840 if (param_var
->param_binding_begin
== ~0U)
1841 param_var
->param_binding_begin
= idx
;
1842 param_var
->param_binding_length
++;
1843 Program
->Base
.NumParameters
++;
1845 /* Check if there is more: 0 -> we're done, else its an integer */
1847 GLuint out_of_range
, new_idx
;
1848 GLuint start_idx
= state_tokens
[2] + 1;
1849 GLuint end_idx
= parse_integer (inst
, Program
);
1852 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1853 if (((state_tokens
[1] == STATE_ENV
)
1854 && (end_idx
>= ctx
->Const
.FragmentProgram
.MaxEnvParams
))
1855 || ((state_tokens
[1] == STATE_LOCAL
)
1857 ctx
->Const
.FragmentProgram
.MaxLocalParams
)))
1861 if (((state_tokens
[1] == STATE_ENV
)
1862 && (end_idx
>= ctx
->Const
.VertexProgram
.MaxEnvParams
))
1863 || ((state_tokens
[1] == STATE_LOCAL
)
1865 ctx
->Const
.VertexProgram
.MaxLocalParams
)))
1869 program_error(ctx
, Program
->Position
,
1870 "Invalid Program Parameter"); /*end_idx*/
1874 for (new_idx
= start_idx
; new_idx
<= end_idx
; new_idx
++) {
1875 state_tokens
[2] = new_idx
;
1876 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1878 param_var
->param_binding_length
++;
1879 Program
->Base
.NumParameters
++;
1887 case PARAM_CONSTANT
:
1888 /* parsing something like {1.0, 2.0, 3.0, 4.0} */
1889 parse_constant (inst
, const_values
, Program
, use
);
1890 idx
= _mesa_add_named_constant(Program
->Base
.Parameters
,
1891 (char *) param_var
->name
,
1893 if (param_var
->param_binding_begin
== ~0U)
1894 param_var
->param_binding_begin
= idx
;
1895 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1896 /* Note: when we reference this parameter in an instruction later,
1897 * we'll check if it's really a constant/immediate and set the
1898 * instruction register type appropriately.
1900 param_var
->param_binding_length
++;
1901 Program
->Base
.NumParameters
++;
1905 program_error(ctx
, Program
->Position
,
1906 "Unexpected token (in parse_param_elements())");
1910 /* Make sure we haven't blown past our parameter limits */
1911 if (((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1912 (Program
->Base
.NumParameters
>=
1913 ctx
->Const
.VertexProgram
.MaxLocalParams
))
1914 || ((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1915 && (Program
->Base
.NumParameters
>=
1916 ctx
->Const
.FragmentProgram
.MaxLocalParams
))) {
1917 program_error(ctx
, Program
->Position
, "Too many parameter variables");
1926 * This picks out PARAM program parameter bindings.
1928 * XXX: This needs to be stressed & tested
1930 * \return 0 on sucess, 1 on error
1933 parse_param (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1934 struct arb_program
*Program
)
1937 GLint specified_length
;
1938 struct var_cache
*param_var
;
1941 param_var
= parse_string (inst
, vc_head
, Program
, &found
);
1942 Program
->Position
= parse_position (inst
);
1945 program_error2(ctx
, Program
->Position
,
1946 "Duplicate variable declaration",
1947 (char *) param_var
->name
);
1951 specified_length
= parse_integer (inst
, Program
);
1953 if (specified_length
< 0) {
1954 program_error(ctx
, Program
->Position
, "Negative parameter array length");
1958 param_var
->type
= vt_param
;
1959 param_var
->param_binding_length
= 0;
1961 /* Right now, everything is shoved into the main state register file.
1963 * In the future, it would be nice to leave things ENV/LOCAL params
1964 * in their respective register files, if possible
1966 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1969 * * - add each guy to the parameter list
1970 * * - increment the param_var->param_binding_len
1971 * * - store the param_var->param_binding_begin for the first one
1972 * * - compare the actual len to the specified len at the end
1974 while (**inst
!= PARAM_NULL
) {
1975 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_FALSE
))
1979 /* Test array length here! */
1980 if (specified_length
) {
1981 if (specified_length
!= (int)param_var
->param_binding_length
) {
1982 program_error(ctx
, Program
->Position
,
1983 "Declared parameter array length does not match parameter list");
1996 parse_param_use (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1997 struct arb_program
*Program
, struct var_cache
**new_var
)
1999 struct var_cache
*param_var
;
2001 /* First, insert a dummy entry into the var_cache */
2002 var_cache_create (¶m_var
);
2003 param_var
->name
= (const GLubyte
*) " ";
2004 param_var
->type
= vt_param
;
2006 param_var
->param_binding_length
= 0;
2007 /* Don't fill in binding_begin; We use the default value of -1
2008 * to tell if its already initialized, elsewhere.
2010 * param_var->param_binding_begin = 0;
2012 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
2014 var_cache_append (vc_head
, param_var
);
2016 /* Then fill it with juicy parameter goodness */
2017 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_TRUE
))
2020 *new_var
= param_var
;
2027 * This handles the declaration of TEMP variables
2029 * \return 0 on sucess, 1 on error
2032 parse_temp (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2033 struct arb_program
*Program
)
2036 struct var_cache
*temp_var
;
2038 while (**inst
!= 0) {
2039 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2040 Program
->Position
= parse_position (inst
);
2042 program_error2(ctx
, Program
->Position
,
2043 "Duplicate variable declaration",
2044 (char *) temp_var
->name
);
2048 temp_var
->type
= vt_temp
;
2050 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
2051 (Program
->Base
.NumTemporaries
>=
2052 ctx
->Const
.FragmentProgram
.MaxTemps
))
2053 || ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
2054 && (Program
->Base
.NumTemporaries
>=
2055 ctx
->Const
.VertexProgram
.MaxTemps
))) {
2056 program_error(ctx
, Program
->Position
,
2057 "Too many TEMP variables declared");
2061 temp_var
->temp_binding
= Program
->Base
.NumTemporaries
;
2062 Program
->Base
.NumTemporaries
++;
2070 * This handles variables of the OUTPUT variety
2072 * \return 0 on sucess, 1 on error
2075 parse_output (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2076 struct arb_program
*Program
)
2079 struct var_cache
*output_var
;
2082 output_var
= parse_string (inst
, vc_head
, Program
, &found
);
2083 Program
->Position
= parse_position (inst
);
2085 program_error2(ctx
, Program
->Position
,
2086 "Duplicate variable declaration",
2087 (char *) output_var
->name
);
2091 output_var
->type
= vt_output
;
2093 err
= parse_result_binding(ctx
, inst
, &output_var
->output_binding
, Program
);
2098 * This handles variables of the ALIAS kind
2100 * \return 0 on sucess, 1 on error
2103 parse_alias (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2104 struct arb_program
*Program
)
2107 struct var_cache
*temp_var
;
2109 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2110 Program
->Position
= parse_position (inst
);
2113 program_error2(ctx
, Program
->Position
,
2114 "Duplicate variable declaration",
2115 (char *) temp_var
->name
);
2119 temp_var
->type
= vt_alias
;
2120 temp_var
->alias_binding
= parse_string (inst
, vc_head
, Program
, &found
);
2121 Program
->Position
= parse_position (inst
);
2125 program_error2(ctx
, Program
->Position
,
2126 "Undefined alias value",
2127 (char *) temp_var
->alias_binding
->name
);
2135 * This handles variables of the ADDRESS kind
2137 * \return 0 on sucess, 1 on error
2140 parse_address (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2141 struct arb_program
*Program
)
2144 struct var_cache
*temp_var
;
2146 while (**inst
!= 0) {
2147 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2148 Program
->Position
= parse_position (inst
);
2150 program_error2(ctx
, Program
->Position
,
2151 "Duplicate variable declaration",
2152 (char *) temp_var
->name
);
2156 temp_var
->type
= vt_address
;
2158 if (Program
->Base
.NumAddressRegs
>=
2159 ctx
->Const
.VertexProgram
.MaxAddressRegs
) {
2160 const char *msg
= "Too many ADDRESS variables declared";
2161 program_error(ctx
, Program
->Position
, msg
);
2165 temp_var
->address_binding
= Program
->Base
.NumAddressRegs
;
2166 Program
->Base
.NumAddressRegs
++;
2174 * Parse a program declaration
2176 * \return 0 on sucess, 1 on error
2179 parse_declaration (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2180 struct arb_program
*Program
)
2184 switch (*(*inst
)++) {
2186 err
= parse_address (ctx
, inst
, vc_head
, Program
);
2190 err
= parse_alias (ctx
, inst
, vc_head
, Program
);
2194 err
= parse_attrib (ctx
, inst
, vc_head
, Program
);
2198 err
= parse_output (ctx
, inst
, vc_head
, Program
);
2202 err
= parse_param (ctx
, inst
, vc_head
, Program
);
2206 err
= parse_temp (ctx
, inst
, vc_head
, Program
);
2214 * Handle the parsing out of a masked destination register, either for a
2215 * vertex or fragment program.
2217 * If we are a vertex program, make sure we don't write to
2218 * result.position if we have specified that the program is
2219 * position invariant
2221 * \param File - The register file we write to
2222 * \param Index - The register index we write to
2223 * \param WriteMask - The mask controlling which components we write (1->write)
2225 * \return 0 on sucess, 1 on error
2228 parse_masked_dst_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2229 struct var_cache
**vc_head
, struct arb_program
*Program
,
2230 enum register_file
*File
, GLuint
*Index
, GLint
*WriteMask
)
2233 struct var_cache
*dst
;
2235 /* We either have a result register specified, or a
2236 * variable that may or may not be writable
2238 switch (*(*inst
)++) {
2239 case REGISTER_RESULT
:
2240 if (parse_result_binding(ctx
, inst
, Index
, Program
))
2242 *File
= PROGRAM_OUTPUT
;
2245 case REGISTER_ESTABLISHED_NAME
:
2246 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2247 Program
->Position
= parse_position (inst
);
2249 /* If the name has never been added to our symbol table, we're hosed */
2251 program_error(ctx
, Program
->Position
, "0: Undefined variable");
2255 switch (dst
->type
) {
2257 *File
= PROGRAM_OUTPUT
;
2258 *Index
= dst
->output_binding
;
2262 *File
= PROGRAM_TEMPORARY
;
2263 *Index
= dst
->temp_binding
;
2266 /* If the var type is not vt_output or vt_temp, no go */
2268 program_error(ctx
, Program
->Position
,
2269 "Destination register is read only");
2275 program_error(ctx
, Program
->Position
,
2276 "Unexpected opcode in parse_masked_dst_reg()");
2281 /* Position invariance test */
2282 /* This test is done now in syntax portion - when position invariance OPTION
2283 is specified, "result.position" rule is disabled so there is no way
2284 to write the position
2286 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2288 program_error(ctx, Program->Position,
2289 "Vertex program specified position invariance and wrote vertex position");
2292 /* And then the mask.
2298 * ==> Need to reverse the order of bits for this!
2300 tmp
= (GLint
) *(*inst
)++;
2301 *WriteMask
= (((tmp
>>3) & 0x1) |
2311 * Handle the parsing of a address register
2313 * \param Index - The register index we write to
2315 * \return 0 on sucess, 1 on error
2318 parse_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2319 struct var_cache
**vc_head
,
2320 struct arb_program
*Program
, GLint
* Index
)
2322 struct var_cache
*dst
;
2325 *Index
= 0; /* XXX */
2327 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2328 Program
->Position
= parse_position (inst
);
2330 /* If the name has never been added to our symbol table, we're hosed */
2332 program_error(ctx
, Program
->Position
, "Undefined variable");
2336 if (dst
->type
!= vt_address
) {
2337 program_error(ctx
, Program
->Position
, "Variable is not of type ADDRESS");
2346 * Handle the parsing out of a masked address register
2348 * \param Index - The register index we write to
2349 * \param WriteMask - The mask controlling which components we write (1->write)
2351 * \return 0 on sucess, 1 on error
2354 parse_masked_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2355 struct var_cache
**vc_head
,
2356 struct arb_program
*Program
, GLint
* Index
,
2357 GLboolean
* WriteMask
)
2359 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, Index
))
2362 /* This should be 0x8 */
2365 /* Writemask of .x is implied */
2367 WriteMask
[1] = WriteMask
[2] = WriteMask
[3] = 0;
2374 * Parse out a swizzle mask.
2376 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
2378 * The len parameter allows us to grab 4 components for a vector
2379 * swizzle, or just 1 component for a scalar src register selection
2382 parse_swizzle_mask(const GLubyte
** inst
, GLubyte
*swizzle
, GLint len
)
2386 for (i
= 0; i
< 4; i
++)
2389 for (i
= 0; i
< len
; i
++) {
2390 switch (*(*inst
)++) {
2392 swizzle
[i
] = SWIZZLE_X
;
2395 swizzle
[i
] = SWIZZLE_Y
;
2398 swizzle
[i
] = SWIZZLE_Z
;
2401 swizzle
[i
] = SWIZZLE_W
;
2404 _mesa_problem(NULL
, "bad component in parse_swizzle_mask()");
2412 * Parse an extended swizzle mask which is a sequence of
2413 * four x/y/z/w/0/1 tokens.
2414 * \return swizzle four swizzle values
2415 * \return negateMask four element bitfield
2418 parse_extended_swizzle_mask(const GLubyte
**inst
, GLubyte swizzle
[4],
2419 GLubyte
*negateMask
)
2424 for (i
= 0; i
< 4; i
++) {
2426 if (parse_sign(inst
) == -1)
2427 *negateMask
|= (1 << i
);
2433 swizzle
[i
] = SWIZZLE_ZERO
;
2436 swizzle
[i
] = SWIZZLE_ONE
;
2439 swizzle
[i
] = SWIZZLE_X
;
2442 swizzle
[i
] = SWIZZLE_Y
;
2445 swizzle
[i
] = SWIZZLE_Z
;
2448 swizzle
[i
] = SWIZZLE_W
;
2451 _mesa_problem(NULL
, "bad case in parse_extended_swizzle_mask()");
2459 parse_src_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2460 struct var_cache
**vc_head
,
2461 struct arb_program
*Program
,
2462 enum register_file
* File
, GLint
* Index
,
2463 GLboolean
*IsRelOffset
)
2465 struct var_cache
*src
;
2466 GLuint binding
, is_generic
, found
;
2471 /* And the binding for the src */
2472 switch (*(*inst
)++) {
2473 case REGISTER_ATTRIB
:
2474 if (parse_attrib_binding
2475 (ctx
, inst
, Program
, &binding
, &is_generic
))
2477 *File
= PROGRAM_INPUT
;
2480 /* We need to insert a dummy variable into the var_cache so we can
2481 * catch generic vertex attrib aliasing errors
2483 var_cache_create(&src
);
2484 src
->type
= vt_attrib
;
2485 src
->name
= (const GLubyte
*) "Dummy Attrib Variable";
2486 src
->attrib_binding
= binding
;
2487 src
->attrib_is_generic
= is_generic
;
2488 var_cache_append(vc_head
, src
);
2489 if (generic_attrib_check(*vc_head
)) {
2490 program_error(ctx
, Program
->Position
,
2491 "Cannot use both a generic vertex attribute "
2492 "and a specific attribute of the same type");
2497 case REGISTER_PARAM
:
2499 case PARAM_ARRAY_ELEMENT
:
2501 src
= parse_string (inst
, vc_head
, Program
, &found
);
2502 Program
->Position
= parse_position (inst
);
2505 program_error2(ctx
, Program
->Position
,
2506 "Undefined variable",
2507 (char *) src
->name
);
2511 *File
= (enum register_file
) src
->param_binding_type
;
2513 switch (*(*inst
)++) {
2514 case ARRAY_INDEX_ABSOLUTE
:
2515 offset
= parse_integer (inst
, Program
);
2518 || (offset
>= (int)src
->param_binding_length
)) {
2519 program_error(ctx
, Program
->Position
,
2520 "Index out of range");
2521 /* offset, src->name */
2525 *Index
= src
->param_binding_begin
+ offset
;
2528 case ARRAY_INDEX_RELATIVE
:
2530 GLint addr_reg_idx
, rel_off
;
2532 /* First, grab the address regiseter */
2533 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &addr_reg_idx
))
2542 /* Then the relative offset */
2543 if (parse_relative_offset(ctx
, inst
, Program
, &rel_off
)) return 1;
2545 /* And store it properly */
2546 *Index
= src
->param_binding_begin
+ rel_off
;
2554 if (parse_param_use (ctx
, inst
, vc_head
, Program
, &src
))
2557 *File
= (enum register_file
) src
->param_binding_type
;
2558 *Index
= src
->param_binding_begin
;
2563 case REGISTER_ESTABLISHED_NAME
:
2564 src
= parse_string (inst
, vc_head
, Program
, &found
);
2565 Program
->Position
= parse_position (inst
);
2567 /* If the name has never been added to our symbol table, we're hosed */
2569 program_error(ctx
, Program
->Position
,
2570 "3: Undefined variable"); /* src->name */
2574 switch (src
->type
) {
2576 *File
= PROGRAM_INPUT
;
2577 *Index
= src
->attrib_binding
;
2580 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2582 *File
= (enum register_file
) src
->param_binding_type
;
2583 *Index
= src
->param_binding_begin
;
2587 *File
= PROGRAM_TEMPORARY
;
2588 *Index
= src
->temp_binding
;
2591 /* If the var type is vt_output no go */
2593 program_error(ctx
, Program
->Position
,
2594 "destination register is read only");
2601 program_error(ctx
, Program
->Position
,
2602 "Unknown token in parse_src_reg");
2606 if (*File
== PROGRAM_STATE_VAR
) {
2607 enum register_file file
;
2609 /* If we're referencing the Program->Parameters[] array, check if the
2610 * parameter is really a constant/literal. If so, set File to CONSTANT.
2612 assert(*Index
< (GLint
) Program
->Base
.Parameters
->NumParameters
);
2613 file
= Program
->Base
.Parameters
->Parameters
[*Index
].Type
;
2614 if (file
== PROGRAM_CONSTANT
)
2615 *File
= PROGRAM_CONSTANT
;
2618 /* Add attributes to InputsRead only if they are used the program.
2619 * This avoids the handling of unused ATTRIB declarations in the drivers. */
2620 if (*File
== PROGRAM_INPUT
)
2621 Program
->Base
.InputsRead
|= (1 << *Index
);
2628 * Parse vertex/fragment program vector source register.
2631 parse_vector_src_reg(GLcontext
*ctx
, const GLubyte
**inst
,
2632 struct var_cache
**vc_head
,
2633 struct arb_program
*program
,
2634 struct prog_src_register
*reg
)
2636 enum register_file file
;
2640 GLboolean isRelOffset
;
2643 negateMask
= (parse_sign (inst
) == -1) ? NEGATE_XYZW
: NEGATE_NONE
;
2645 /* And the src reg */
2646 if (parse_src_reg(ctx
, inst
, vc_head
, program
, &file
, &index
, &isRelOffset
))
2649 /* finally, the swizzle */
2650 parse_swizzle_mask(inst
, swizzle
, 4);
2654 reg
->Swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
2655 reg
->NegateBase
= negateMask
;
2656 reg
->RelAddr
= isRelOffset
;
2662 * Parse vertex/fragment program scalar source register.
2665 parse_scalar_src_reg(GLcontext
*ctx
, const GLubyte
**inst
,
2666 struct var_cache
**vc_head
,
2667 struct arb_program
*program
,
2668 struct prog_src_register
*reg
)
2670 enum register_file file
;
2674 GLboolean isRelOffset
;
2677 negateMask
= (parse_sign (inst
) == -1) ? NEGATE_XYZW
: NEGATE_NONE
;
2679 /* And the src reg */
2680 if (parse_src_reg(ctx
, inst
, vc_head
, program
, &file
, &index
, &isRelOffset
))
2683 /* finally, the swizzle */
2684 parse_swizzle_mask(inst
, swizzle
, 1);
2688 reg
->Swizzle
= (swizzle
[0] << 0);
2689 reg
->NegateBase
= negateMask
;
2690 reg
->RelAddr
= isRelOffset
;
2696 * Parse vertex/fragment program destination register.
2697 * \return 1 if error, 0 if no error.
2700 parse_dst_reg(GLcontext
* ctx
, const GLubyte
** inst
,
2701 struct var_cache
**vc_head
, struct arb_program
*program
,
2702 struct prog_dst_register
*reg
)
2706 enum register_file file
;
2708 if (parse_masked_dst_reg (ctx
, inst
, vc_head
, program
, &file
, &idx
, &mask
))
2713 reg
->WriteMask
= mask
;
2719 * This is a big mother that handles getting opcodes into the instruction
2720 * and handling the src & dst registers for fragment program instructions
2721 * \return 1 if error, 0 if no error
2724 parse_fp_instruction (GLcontext
* ctx
, const GLubyte
** inst
,
2725 struct var_cache
**vc_head
, struct arb_program
*Program
,
2726 struct prog_instruction
*fp
)
2730 GLubyte instClass
, type
, code
;
2732 GLuint shadow_tex
= 0;
2734 _mesa_init_instructions(fp
, 1);
2736 /* Record the position in the program string for debugging */
2737 fp
->StringPos
= Program
->Position
;
2739 /* OP_ALU_INST or OP_TEX_INST */
2740 instClass
= *(*inst
)++;
2742 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2743 * OP_TEX_{SAMPLE, KIL}
2747 /* The actual opcode name */
2750 /* Increment the correct count */
2751 switch (instClass
) {
2753 Program
->NumAluInstructions
++;
2756 Program
->NumTexInstructions
++;
2764 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2766 fp
->Opcode
= OPCODE_ABS
;
2770 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2772 fp
->Opcode
= OPCODE_FLR
;
2776 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2778 fp
->Opcode
= OPCODE_FRC
;
2782 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2784 fp
->Opcode
= OPCODE_LIT
;
2788 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2790 fp
->Opcode
= OPCODE_MOV
;
2794 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2797 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2804 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2806 fp
->Opcode
= OPCODE_COS
;
2810 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2812 fp
->Opcode
= OPCODE_EX2
;
2816 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2818 fp
->Opcode
= OPCODE_LG2
;
2822 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2824 fp
->Opcode
= OPCODE_RCP
;
2828 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2830 fp
->Opcode
= OPCODE_RSQ
;
2834 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2836 fp
->Opcode
= OPCODE_SIN
;
2840 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2843 fp
->Opcode
= OPCODE_SCS
;
2847 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2850 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2857 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2859 fp
->Opcode
= OPCODE_POW
;
2863 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2866 for (a
= 0; a
< 2; a
++) {
2867 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2876 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2878 fp
->Opcode
= OPCODE_ADD
;
2882 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2884 fp
->Opcode
= OPCODE_DP3
;
2888 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2890 fp
->Opcode
= OPCODE_DP4
;
2894 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2896 fp
->Opcode
= OPCODE_DPH
;
2900 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2902 fp
->Opcode
= OPCODE_DST
;
2906 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2908 fp
->Opcode
= OPCODE_MAX
;
2912 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2914 fp
->Opcode
= OPCODE_MIN
;
2918 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2920 fp
->Opcode
= OPCODE_MUL
;
2924 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2926 fp
->Opcode
= OPCODE_SGE
;
2930 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2932 fp
->Opcode
= OPCODE_SLT
;
2936 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2938 fp
->Opcode
= OPCODE_SUB
;
2942 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2944 fp
->Opcode
= OPCODE_XPD
;
2948 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2950 for (a
= 0; a
< 2; a
++) {
2951 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2959 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2961 fp
->Opcode
= OPCODE_CMP
;
2965 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2967 fp
->Opcode
= OPCODE_LRP
;
2971 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2973 fp
->Opcode
= OPCODE_MAD
;
2977 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2980 for (a
= 0; a
< 3; a
++) {
2981 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2989 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2991 fp
->Opcode
= OPCODE_SWZ
;
2994 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3000 enum register_file file
;
3003 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
, &rel
))
3005 parse_extended_swizzle_mask(inst
, swizzle
, &negateMask
);
3006 fp
->SrcReg
[0].File
= file
;
3007 fp
->SrcReg
[0].Index
= index
;
3008 fp
->SrcReg
[0].NegateBase
= negateMask
;
3009 fp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
3019 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3021 fp
->Opcode
= OPCODE_TEX
;
3025 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3027 fp
->Opcode
= OPCODE_TXP
;
3031 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3033 fp
->Opcode
= OPCODE_TXB
;
3037 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3040 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
3044 if (parse_texcoord_num (ctx
, inst
, Program
, &texcoord
))
3046 fp
->TexSrcUnit
= texcoord
;
3049 switch (*(*inst
)++) {
3050 case TEXTARGET_SHADOW1D
:
3051 shadow_tex
= 1 << texcoord
;
3054 fp
->TexSrcTarget
= TEXTURE_1D_INDEX
;
3056 case TEXTARGET_SHADOW2D
:
3057 shadow_tex
= 1 << texcoord
;
3060 fp
->TexSrcTarget
= TEXTURE_2D_INDEX
;
3063 fp
->TexSrcTarget
= TEXTURE_3D_INDEX
;
3065 case TEXTARGET_SHADOWRECT
:
3066 shadow_tex
= 1 << texcoord
;
3068 case TEXTARGET_RECT
:
3069 fp
->TexSrcTarget
= TEXTURE_RECT_INDEX
;
3071 case TEXTARGET_CUBE
:
3072 fp
->TexSrcTarget
= TEXTURE_CUBE_INDEX
;
3074 case TEXTARGET_SHADOW1D_ARRAY
:
3075 shadow_tex
= 1 << texcoord
;
3077 case TEXTARGET_1D_ARRAY
:
3078 fp
->TexSrcTarget
= TEXTURE_1D_ARRAY_INDEX
;
3080 case TEXTARGET_SHADOW2D_ARRAY
:
3081 shadow_tex
= 1 << texcoord
;
3083 case TEXTARGET_2D_ARRAY
:
3084 fp
->TexSrcTarget
= TEXTURE_2D_ARRAY_INDEX
;
3088 /* Don't test the first time a particular sampler is seen. Each time
3089 * after that, make sure the shadow state is the same.
3091 if ((_mesa_bitcount(Program
->TexturesUsed
[texcoord
]) > 0)
3092 && ((Program
->ShadowSamplers
& (1 << texcoord
)) != shadow_tex
)) {
3093 program_error(ctx
, Program
->Position
,
3094 "texture image unit used for shadow sampling and non-shadow sampling");
3098 Program
->TexturesUsed
[texcoord
] |= (1 << fp
->TexSrcTarget
);
3099 /* Check that both "2D" and "CUBE" (for example) aren't both used */
3100 if (_mesa_bitcount(Program
->TexturesUsed
[texcoord
]) > 1) {
3101 program_error(ctx
, Program
->Position
,
3102 "multiple targets used on one texture image unit");
3107 Program
->ShadowSamplers
|= shadow_tex
;
3111 Program
->UsesKill
= 1;
3112 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
3114 fp
->Opcode
= OPCODE_KIL
;
3117 _mesa_problem(ctx
, "bad type 0x%x in parse_fp_instruction()", type
);
3126 * Handle the parsing out of a masked address register
3128 * \param Index - The register index we write to
3129 * \param WriteMask - The mask controlling which components we write (1->write)
3131 * \return 0 on sucess, 1 on error
3134 parse_vp_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
3135 struct var_cache
**vc_head
,
3136 struct arb_program
*Program
,
3137 struct prog_dst_register
*reg
)
3141 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &idx
))
3144 /* This should be 0x8 */
3147 reg
->File
= PROGRAM_ADDRESS
;
3150 /* Writemask of .x is implied */
3151 reg
->WriteMask
= 0x1;
3157 * This is a big mother that handles getting opcodes into the instruction
3158 * and handling the src & dst registers for vertex program instructions
3161 parse_vp_instruction (GLcontext
* ctx
, const GLubyte
** inst
,
3162 struct var_cache
**vc_head
, struct arb_program
*Program
,
3163 struct prog_instruction
*vp
)
3168 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3171 /* The actual opcode name */
3174 _mesa_init_instructions(vp
, 1);
3175 /* Record the position in the program string for debugging */
3176 vp
->StringPos
= Program
->Position
;
3181 vp
->Opcode
= OPCODE_ARL
;
3183 /* Remember to set SrcReg.RelAddr; */
3185 /* Get the masked address register [dst] */
3186 if (parse_vp_address_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3189 vp
->DstReg
.File
= PROGRAM_ADDRESS
;
3191 /* Get a scalar src register */
3192 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3200 vp
->Opcode
= OPCODE_ABS
;
3203 vp
->Opcode
= OPCODE_FLR
;
3206 vp
->Opcode
= OPCODE_FRC
;
3209 vp
->Opcode
= OPCODE_LIT
;
3212 vp
->Opcode
= OPCODE_MOV
;
3216 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3219 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3226 vp
->Opcode
= OPCODE_EX2
;
3229 vp
->Opcode
= OPCODE_EXP
;
3232 vp
->Opcode
= OPCODE_LG2
;
3235 vp
->Opcode
= OPCODE_LOG
;
3238 vp
->Opcode
= OPCODE_RCP
;
3241 vp
->Opcode
= OPCODE_RSQ
;
3244 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3247 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3254 vp
->Opcode
= OPCODE_POW
;
3257 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3260 for (a
= 0; a
< 2; a
++) {
3261 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3269 vp
->Opcode
= OPCODE_ADD
;
3272 vp
->Opcode
= OPCODE_DP3
;
3275 vp
->Opcode
= OPCODE_DP4
;
3278 vp
->Opcode
= OPCODE_DPH
;
3281 vp
->Opcode
= OPCODE_DST
;
3284 vp
->Opcode
= OPCODE_MAX
;
3287 vp
->Opcode
= OPCODE_MIN
;
3290 vp
->Opcode
= OPCODE_MUL
;
3293 vp
->Opcode
= OPCODE_SGE
;
3296 vp
->Opcode
= OPCODE_SLT
;
3299 vp
->Opcode
= OPCODE_SUB
;
3302 vp
->Opcode
= OPCODE_XPD
;
3305 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3308 for (a
= 0; a
< 2; a
++) {
3309 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3317 vp
->Opcode
= OPCODE_MAD
;
3321 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3324 for (a
= 0; a
< 3; a
++) {
3325 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3333 vp
->Opcode
= OPCODE_SWZ
;
3340 enum register_file file
;
3343 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3346 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
, &relAddr
))
3348 parse_extended_swizzle_mask (inst
, swizzle
, &negateMask
);
3349 vp
->SrcReg
[0].File
= file
;
3350 vp
->SrcReg
[0].Index
= index
;
3351 vp
->SrcReg
[0].NegateBase
= negateMask
;
3352 vp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
3356 vp
->SrcReg
[0].RelAddr
= relAddr
;
3366 debug_variables (GLcontext
* ctx
, struct var_cache
*vc_head
,
3367 struct arb_program
*Program
)
3369 struct var_cache
*vc
;
3372 fprintf (stderr
, "debug_variables, vc_head: %p\n", (void*) vc_head
);
3374 /* First of all, print out the contents of the var_cache */
3377 fprintf (stderr
, "[%p]\n", (void*) vc
);
3380 fprintf (stderr
, "UNDEFINED %s\n", vc
->name
);
3383 fprintf (stderr
, "ATTRIB %s\n", vc
->name
);
3384 fprintf (stderr
, " binding: 0x%x\n", vc
->attrib_binding
);
3387 fprintf (stderr
, "PARAM %s begin: %d len: %d\n", vc
->name
,
3388 vc
->param_binding_begin
, vc
->param_binding_length
);
3389 b
= vc
->param_binding_begin
;
3390 for (a
= 0; a
< vc
->param_binding_length
; a
++) {
3391 fprintf (stderr
, "%s\n",
3392 Program
->Base
.Parameters
->Parameters
[a
+ b
].Name
);
3393 if (Program
->Base
.Parameters
->Parameters
[a
+ b
].Type
== PROGRAM_STATE_VAR
) {
3395 s
= _mesa_program_state_string(Program
->Base
.Parameters
->Parameters
3396 [a
+ b
].StateIndexes
);
3397 fprintf(stderr
, "%s\n", s
);
3401 fprintf (stderr
, "%f %f %f %f\n",
3402 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][0],
3403 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][1],
3404 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][2],
3405 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][3]);
3409 fprintf (stderr
, "TEMP %s\n", vc
->name
);
3410 fprintf (stderr
, " binding: 0x%x\n", vc
->temp_binding
);
3413 fprintf (stderr
, "OUTPUT %s\n", vc
->name
);
3414 fprintf (stderr
, " binding: 0x%x\n", vc
->output_binding
);
3417 fprintf (stderr
, "ALIAS %s\n", vc
->name
);
3418 fprintf (stderr
, " binding: 0x%p (%s)\n",
3419 (void*) vc
->alias_binding
, vc
->alias_binding
->name
);
3429 #endif /* DEBUG_PARSING */
3433 * The main loop for parsing a fragment or vertex program
3435 * \return 1 on error, 0 on success
3438 parse_instructions(GLcontext
* ctx
, const GLubyte
* inst
,
3439 struct var_cache
**vc_head
, struct arb_program
*Program
)
3441 const GLuint maxInst
= (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
3442 ? ctx
->Const
.FragmentProgram
.MaxInstructions
3443 : ctx
->Const
.VertexProgram
.MaxInstructions
;
3446 Program
->MajorVersion
= (GLuint
) * inst
++;
3447 Program
->MinorVersion
= (GLuint
) * inst
++;
3449 while (*inst
!= END
) {
3454 case ARB_PRECISION_HINT_FASTEST
:
3455 Program
->PrecisionOption
= GL_FASTEST
;
3458 case ARB_PRECISION_HINT_NICEST
:
3459 Program
->PrecisionOption
= GL_NICEST
;
3463 Program
->FogOption
= GL_EXP
;
3467 Program
->FogOption
= GL_EXP2
;
3470 case ARB_FOG_LINEAR
:
3471 Program
->FogOption
= GL_LINEAR
;
3474 case ARB_POSITION_INVARIANT
:
3475 if (Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
3476 Program
->HintPositionInvariant
= GL_TRUE
;
3479 case ARB_FRAGMENT_PROGRAM_SHADOW
:
3480 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3481 /* TODO ARB_fragment_program_shadow code */
3485 case ARB_DRAW_BUFFERS
:
3486 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3487 /* do nothing for now */
3491 case MESA_TEXTURE_ARRAY
:
3492 /* do nothing for now */
3499 if (Program
->Base
.NumInstructions
+ 1 >= maxInst
) {
3500 program_error(ctx
, Program
->Position
,
3501 "Max instruction count exceeded");
3504 Program
->Position
= parse_position (&inst
);
3505 /* parse the current instruction */
3506 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3507 err
= parse_fp_instruction (ctx
, &inst
, vc_head
, Program
,
3508 &Program
->Base
.Instructions
[Program
->Base
.NumInstructions
]);
3511 err
= parse_vp_instruction (ctx
, &inst
, vc_head
, Program
,
3512 &Program
->Base
.Instructions
[Program
->Base
.NumInstructions
]);
3515 /* increment instuction count */
3516 Program
->Base
.NumInstructions
++;
3520 err
= parse_declaration (ctx
, &inst
, vc_head
, Program
);
3531 /* Finally, tag on an OPCODE_END instruction */
3533 const GLuint numInst
= Program
->Base
.NumInstructions
;
3534 _mesa_init_instructions(Program
->Base
.Instructions
+ numInst
, 1);
3535 Program
->Base
.Instructions
[numInst
].Opcode
= OPCODE_END
;
3536 /* YYY Wrong Position in program, whatever, at least not random -> crash
3537 Program->Position = parse_position (&inst);
3539 Program
->Base
.Instructions
[numInst
].StringPos
= Program
->Position
;
3541 Program
->Base
.NumInstructions
++;
3544 * Initialize native counts to logical counts. The device driver may
3545 * change them if program is translated into a hardware program.
3547 Program
->Base
.NumNativeInstructions
= Program
->Base
.NumInstructions
;
3548 Program
->Base
.NumNativeTemporaries
= Program
->Base
.NumTemporaries
;
3549 Program
->Base
.NumNativeParameters
= Program
->Base
.NumParameters
;
3550 Program
->Base
.NumNativeAttributes
= Program
->Base
.NumAttributes
;
3551 Program
->Base
.NumNativeAddressRegs
= Program
->Base
.NumAddressRegs
;
3558 LONGSTRING
static char core_grammar_text
[] =
3559 #include "shader/grammar/grammar_syn.h"
3564 * Set a grammar parameter.
3565 * \param name the grammar parameter
3566 * \param value the new parameter value
3567 * \return 0 if OK, 1 if error
3570 set_reg8 (GLcontext
*ctx
, grammar id
, const char *name
, GLubyte value
)
3572 char error_msg
[300];
3575 if (grammar_set_reg8 (id
, (const byte
*) name
, value
))
3578 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3579 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3580 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Grammar Register Error");
3586 * Enable support for the given language option in the parser.
3587 * \return 1 if OK, 0 if error
3590 enable_ext(GLcontext
*ctx
, grammar id
, const char *name
)
3592 return !set_reg8(ctx
, id
, name
, 1);
3597 * Enable parser extensions based on which OpenGL extensions are supported
3598 * by this rendering context.
3600 * \return GL_TRUE if OK, GL_FALSE if error.
3603 enable_parser_extensions(GLcontext
*ctx
, grammar id
)
3606 /* These are not supported at this time */
3607 if ((ctx
->Extensions
.ARB_vertex_blend
||
3608 ctx
->Extensions
.EXT_vertex_weighting
)
3609 && !enable_ext(ctx
, id
, "vertex_blend"))
3611 if (ctx
->Extensions
.ARB_matrix_palette
3612 && !enable_ext(ctx
, id
, "matrix_palette"))
3615 if (ctx
->Extensions
.ARB_fragment_program_shadow
3616 && !enable_ext(ctx
, id
, "fragment_program_shadow"))
3618 if (ctx
->Extensions
.EXT_point_parameters
3619 && !enable_ext(ctx
, id
, "point_parameters"))
3621 if (ctx
->Extensions
.EXT_secondary_color
3622 && !enable_ext(ctx
, id
, "secondary_color"))
3624 if (ctx
->Extensions
.EXT_fog_coord
3625 && !enable_ext(ctx
, id
, "fog_coord"))
3627 if (ctx
->Extensions
.NV_texture_rectangle
3628 && !enable_ext(ctx
, id
, "texture_rectangle"))
3630 if (ctx
->Extensions
.ARB_draw_buffers
3631 && !enable_ext(ctx
, id
, "draw_buffers"))
3633 if (ctx
->Extensions
.MESA_texture_array
3634 && !enable_ext(ctx
, id
, "texture_array"))
3637 /* hack for Warcraft (see bug 8060) */
3638 enable_ext(ctx
, id
, "vertex_blend");
3646 * This kicks everything off.
3648 * \param ctx - The GL Context
3649 * \param str - The program string
3650 * \param len - The program string length
3651 * \param program - The arb_program struct to return all the parsed info in
3652 * \return GL_TRUE on sucess, GL_FALSE on error
3655 _mesa_parse_arb_program(GLcontext
*ctx
, GLenum target
,
3656 const GLubyte
*str
, GLsizei len
,
3657 struct arb_program
*program
)
3659 GLint a
, err
, error_pos
;
3660 char error_msg
[300];
3662 struct var_cache
*vc_head
;
3663 grammar arbprogram_syn_id
;
3664 GLubyte
*parsed
, *inst
;
3665 GLubyte
*strz
= NULL
;
3666 static int arbprogram_syn_is_ok
= 0; /* XXX temporary */
3668 /* set the program target before parsing */
3669 program
->Base
.Target
= target
;
3671 /* Reset error state */
3672 _mesa_set_program_error(ctx
, -1, NULL
);
3674 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
3675 if (!arbprogram_syn_is_ok
) {
3676 /* One-time initialization of parsing system */
3677 grammar grammar_syn_id
;
3680 grammar_syn_id
= grammar_load_from_text ((byte
*) core_grammar_text
);
3681 if (grammar_syn_id
== 0) {
3682 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3683 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3684 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3685 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3686 "glProgramStringARB(Error loading grammar rule set)");
3690 err
= !grammar_check(grammar_syn_id
, (byte
*) arb_grammar_text
,
3691 &parsed
, &parsed_len
);
3693 /* 'parsed' is unused here */
3694 _mesa_free (parsed
);
3697 /* NOTE: we can't destroy grammar_syn_id right here because
3698 * grammar_destroy() can reset the last error
3701 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3702 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3703 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3704 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3705 "glProgramString(Error loading grammar rule set");
3706 grammar_destroy (grammar_syn_id
);
3710 grammar_destroy (grammar_syn_id
);
3712 arbprogram_syn_is_ok
= 1;
3715 /* create the grammar object */
3716 arbprogram_syn_id
= grammar_load_from_text ((byte
*) arb_grammar_text
);
3717 if (arbprogram_syn_id
== 0) {
3718 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3719 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
3720 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3721 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3722 "glProgramString(Error loading grammer rule set)");
3726 /* Set program_target register value */
3727 if (set_reg8 (ctx
, arbprogram_syn_id
, "program_target",
3728 program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
? 0x10 : 0x20)) {
3729 grammar_destroy (arbprogram_syn_id
);
3733 if (!enable_parser_extensions(ctx
, arbprogram_syn_id
)) {
3734 grammar_destroy(arbprogram_syn_id
);
3738 /* check for NULL character occurences */
3741 for (i
= 0; i
< len
; i
++) {
3742 if (str
[i
] == '\0') {
3743 program_error(ctx
, i
, "illegal character");
3744 grammar_destroy (arbprogram_syn_id
);
3750 /* copy the program string to a null-terminated string */
3751 strz
= (GLubyte
*) _mesa_malloc (len
+ 1);
3753 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glProgramStringARB");
3754 grammar_destroy (arbprogram_syn_id
);
3757 _mesa_memcpy (strz
, str
, len
);
3760 /* do a fast check on program string - initial production buffer is 4K */
3761 err
= !grammar_fast_check(arbprogram_syn_id
, strz
,
3762 &parsed
, &parsed_len
, 0x1000);
3764 /* Syntax parse error */
3766 grammar_get_last_error((GLubyte
*) error_msg
, 300, &error_pos
);
3767 program_error(ctx
, error_pos
, error_msg
);
3770 /* useful for debugging */
3774 fprintf(stderr
, "program: %s\n", (char *) strz
);
3775 fprintf(stderr
, "Error Pos: %d\n", ctx
->Program
.ErrorPos
);
3776 s
= (char *) _mesa_find_line_column(strz
, strz
+ctx
->Program
.ErrorPos
,
3778 fprintf(stderr
, "line %d col %d: %s\n", line
, col
, s
);
3785 grammar_destroy (arbprogram_syn_id
);
3789 grammar_destroy (arbprogram_syn_id
);
3792 * Program string is syntactically correct at this point
3793 * Parse the tokenized version of the program now, generating
3794 * vertex/fragment program instructions.
3797 /* Initialize the arb_program struct */
3798 program
->Base
.String
= strz
;
3799 program
->Base
.Instructions
= _mesa_alloc_instructions(MAX_INSTRUCTIONS
);
3800 program
->Base
.NumInstructions
=
3801 program
->Base
.NumTemporaries
=
3802 program
->Base
.NumParameters
=
3803 program
->Base
.NumAttributes
= program
->Base
.NumAddressRegs
= 0;
3804 program
->Base
.Parameters
= _mesa_new_parameter_list ();
3805 program
->Base
.InputsRead
= 0x0;
3806 program
->Base
.OutputsWritten
= 0x0;
3807 program
->Position
= 0;
3808 program
->MajorVersion
= program
->MinorVersion
= 0;
3809 program
->PrecisionOption
= GL_DONT_CARE
;
3810 program
->FogOption
= GL_NONE
;
3811 program
->HintPositionInvariant
= GL_FALSE
;
3812 for (a
= 0; a
< MAX_TEXTURE_IMAGE_UNITS
; a
++)
3813 program
->TexturesUsed
[a
] = 0x0;
3814 program
->ShadowSamplers
= 0x0;
3815 program
->NumAluInstructions
=
3816 program
->NumTexInstructions
=
3817 program
->NumTexIndirections
= 0;
3818 program
->UsesKill
= 0;
3823 /* Start examining the tokens in the array */
3826 /* Check the grammer rev */
3827 if (*inst
++ != REVISION
) {
3828 program_error (ctx
, 0, "Grammar version mismatch");
3832 /* ignore program target */
3834 err
= parse_instructions(ctx
, inst
, &vc_head
, program
);
3837 /*debug_variables(ctx, vc_head, program); */
3839 /* We're done with the parsed binary array */
3840 var_cache_destroy (&vc_head
);
3842 _mesa_free (parsed
);
3844 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
3845 * to size [ap.Base.NumInstructions].
3847 program
->Base
.Instructions
3848 = _mesa_realloc_instructions(program
->Base
.Instructions
,
3850 program
->Base
.NumInstructions
);
3858 _mesa_parse_arb_fragment_program(GLcontext
* ctx
, GLenum target
,
3859 const GLvoid
*str
, GLsizei len
,
3860 struct gl_fragment_program
*program
)
3862 struct arb_program ap
;
3865 ASSERT(target
== GL_FRAGMENT_PROGRAM_ARB
);
3866 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
, &ap
)) {
3867 /* Error in the program. Just return. */
3871 /* Copy the relevant contents of the arb_program struct into the
3872 * fragment_program struct.
3874 program
->Base
.String
= ap
.Base
.String
;
3875 program
->Base
.NumInstructions
= ap
.Base
.NumInstructions
;
3876 program
->Base
.NumTemporaries
= ap
.Base
.NumTemporaries
;
3877 program
->Base
.NumParameters
= ap
.Base
.NumParameters
;
3878 program
->Base
.NumAttributes
= ap
.Base
.NumAttributes
;
3879 program
->Base
.NumAddressRegs
= ap
.Base
.NumAddressRegs
;
3880 program
->Base
.NumNativeInstructions
= ap
.Base
.NumNativeInstructions
;
3881 program
->Base
.NumNativeTemporaries
= ap
.Base
.NumNativeTemporaries
;
3882 program
->Base
.NumNativeParameters
= ap
.Base
.NumNativeParameters
;
3883 program
->Base
.NumNativeAttributes
= ap
.Base
.NumNativeAttributes
;
3884 program
->Base
.NumNativeAddressRegs
= ap
.Base
.NumNativeAddressRegs
;
3885 program
->Base
.NumAluInstructions
= ap
.Base
.NumAluInstructions
;
3886 program
->Base
.NumTexInstructions
= ap
.Base
.NumTexInstructions
;
3887 program
->Base
.NumTexIndirections
= ap
.Base
.NumTexIndirections
;
3888 program
->Base
.NumNativeAluInstructions
= ap
.Base
.NumAluInstructions
;
3889 program
->Base
.NumNativeTexInstructions
= ap
.Base
.NumTexInstructions
;
3890 program
->Base
.NumNativeTexIndirections
= ap
.Base
.NumTexIndirections
;
3891 program
->Base
.InputsRead
= ap
.Base
.InputsRead
;
3892 program
->Base
.OutputsWritten
= ap
.Base
.OutputsWritten
;
3893 for (i
= 0; i
< MAX_TEXTURE_IMAGE_UNITS
; i
++) {
3894 program
->Base
.TexturesUsed
[i
] = ap
.TexturesUsed
[i
];
3895 if (ap
.TexturesUsed
[i
])
3896 program
->Base
.SamplersUsed
|= (1 << i
);
3898 program
->Base
.ShadowSamplers
= ap
.ShadowSamplers
;
3899 program
->FogOption
= ap
.FogOption
;
3900 program
->UsesKill
= ap
.UsesKill
;
3902 if (program
->Base
.Instructions
)
3903 _mesa_free(program
->Base
.Instructions
);
3904 program
->Base
.Instructions
= ap
.Base
.Instructions
;
3906 if (program
->Base
.Parameters
)
3907 _mesa_free_parameter_list(program
->Base
.Parameters
);
3908 program
->Base
.Parameters
= ap
.Base
.Parameters
;
3910 /* Append fog instructions now if the program has "OPTION ARB_fog_exp"
3911 * or similar. We used to leave this up to drivers, but it appears
3912 * there's no hardware that wants to do fog in a discrete stage separate
3913 * from the fragment shader.
3915 if (program
->FogOption
!= GL_NONE
) {
3916 _mesa_append_fog_code(ctx
, program
);
3917 program
->FogOption
= GL_NONE
;
3921 _mesa_printf("____________Fragment program %u ________\n", program
->Base
.Id
);
3922 _mesa_print_program(&program
->Base
);
3929 * Parse the vertex program string. If success, update the given
3930 * vertex_program object with the new program. Else, leave the vertex_program
3934 _mesa_parse_arb_vertex_program(GLcontext
*ctx
, GLenum target
,
3935 const GLvoid
*str
, GLsizei len
,
3936 struct gl_vertex_program
*program
)
3938 struct arb_program ap
;
3940 ASSERT(target
== GL_VERTEX_PROGRAM_ARB
);
3942 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
, &ap
)) {
3943 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glProgramString(bad program)");
3947 /* Copy the relevant contents of the arb_program struct into the
3948 * vertex_program struct.
3950 program
->Base
.String
= ap
.Base
.String
;
3951 program
->Base
.NumInstructions
= ap
.Base
.NumInstructions
;
3952 program
->Base
.NumTemporaries
= ap
.Base
.NumTemporaries
;
3953 program
->Base
.NumParameters
= ap
.Base
.NumParameters
;
3954 program
->Base
.NumAttributes
= ap
.Base
.NumAttributes
;
3955 program
->Base
.NumAddressRegs
= ap
.Base
.NumAddressRegs
;
3956 program
->Base
.NumNativeInstructions
= ap
.Base
.NumNativeInstructions
;
3957 program
->Base
.NumNativeTemporaries
= ap
.Base
.NumNativeTemporaries
;
3958 program
->Base
.NumNativeParameters
= ap
.Base
.NumNativeParameters
;
3959 program
->Base
.NumNativeAttributes
= ap
.Base
.NumNativeAttributes
;
3960 program
->Base
.NumNativeAddressRegs
= ap
.Base
.NumNativeAddressRegs
;
3961 program
->Base
.InputsRead
= ap
.Base
.InputsRead
;
3962 program
->Base
.OutputsWritten
= ap
.Base
.OutputsWritten
;
3963 program
->IsPositionInvariant
= ap
.HintPositionInvariant
;
3965 if (program
->Base
.Instructions
)
3966 _mesa_free(program
->Base
.Instructions
);
3967 program
->Base
.Instructions
= ap
.Base
.Instructions
;
3969 if (program
->Base
.Parameters
)
3970 _mesa_free_parameter_list(program
->Base
.Parameters
);
3971 program
->Base
.Parameters
= ap
.Base
.Parameters
;
3974 _mesa_printf("____________Vertex program %u __________\n", program
->Base
.Id
);
3975 _mesa_print_program(&program
->Base
);