2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #define DEBUG_PARSING 0
28 * \file arbprogparse.c
29 * ARB_*_program parser core
34 Notes on program parameters, etc.
36 The instructions we emit will use six kinds of source registers:
38 PROGRAM_INPUT - input registers
39 PROGRAM_TEMPORARY - temp registers
40 PROGRAM_ADDRESS - address/indirect register
41 PROGRAM_SAMPLER - texture sampler
42 PROGRAM_CONSTANT - indexes into program->Parameters, a known constant/literal
43 PROGRAM_STATE_VAR - indexes into program->Parameters, and may actually be:
44 + a state variable, like "state.fog.color", or
45 + a pointer to a "program.local[k]" parameter, or
46 + a pointer to a "program.env[k]" parameter
48 Basically, all the program.local[] and program.env[] values will get mapped
49 into the unified gl_program->Parameters array. This solves the problem of
50 having three separate program parameter arrays.
54 #include "main/glheader.h"
55 #include "main/imports.h"
56 #include "main/context.h"
57 #include "main/macros.h"
58 #include "main/mtypes.h"
59 #include "shader/grammar/grammar_mesa.h"
60 #include "arbprogparse.h"
62 #include "programopt.h"
63 #include "prog_parameter.h"
64 #include "prog_statevars.h"
65 #include "prog_instruction.h"
68 * This is basically a union of the vertex_program and fragment_program
69 * structs that we can use to parse the program into
71 * XXX we can probably get rid of this entirely someday.
75 struct gl_program Base
;
77 GLuint Position
; /* Just used for error reporting while parsing */
81 /* ARB_vertex_progmra options */
82 GLboolean HintPositionInvariant
;
84 /* ARB_fragment_progmra options */
85 GLenum PrecisionOption
; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
86 GLenum FogOption
; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
88 /* ARB_fragment_program specifics */
89 GLbitfield TexturesUsed
[MAX_TEXTURE_IMAGE_UNITS
];
90 GLbitfield ShadowSamplers
;
91 GLuint NumAluInstructions
;
92 GLuint NumTexInstructions
;
93 GLuint NumTexIndirections
;
101 * Fragment Program Stuff:
102 * -----------------------------------------------------
104 * - things from Michal's email
106 * + not-overflowing floats (don't use parse_integer..)
107 * + can remove range checking in arbparse.c
109 * - check all limits of number of various variables
112 * - test! test! test!
114 * Vertex Program Stuff:
115 * -----------------------------------------------------
116 * - Optimize param array usage and count limits correctly, see spec,
118 * + Record if an array is reference absolutly or relatively (or both)
119 * + For absolute arrays, store a bitmap of accesses
120 * + For single parameters, store an access flag
121 * + After parsing, make a parameter cleanup and merging pass, where
122 * relative arrays are layed out first, followed by abs arrays, and
123 * finally single state.
124 * + Remap offsets for param src and dst registers
125 * + Now we can properly count parameter usage
127 * - Multiple state binding errors in param arrays (see spec, just before
132 * -----------------------------------------------------
133 * - User clipping planes vs. PositionInvariant
134 * - Is it sufficient to just multiply by the mvp to transform in the
135 * PositionInvariant case? Or do we need something more involved?
137 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
138 * - fetch state listed in program_parameters list
139 * + WTF should this go???
140 * + currently in nvvertexec.c and s_nvfragprog.c
142 * - allow for multiple address registers (and fetch address regs properly)
145 * -----------------------------------------------------
146 * - remove any leftover unused grammer.c stuff (dict_ ?)
147 * - fix grammer.c error handling so its not static
148 * - #ifdef around stuff pertaining to extentions
150 * Outstanding Questions:
151 * -----------------------------------------------------
152 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
153 * what gets hacked off because of this:
154 * + VERTEX_ATTRIB_MATRIXINDEX
155 * + VERTEX_ATTRIB_WEIGHT
159 * - When can we fetch env/local params from their own register files, and
160 * when to we have to fetch them into the main state register file?
164 * -----------------------------------------------------
167 /* Changes since moving the file to shader directory
169 2004-III-4 ------------------------------------------------------------
170 - added #include "grammar_mesa.h"
171 - removed grammar specific code part (it resides now in grammar.c)
172 - added GL_ARB_fragment_program_shadow tokens
173 - modified #include "arbparse_syn.h"
174 - major changes inside _mesa_parse_arb_program()
175 - check the program string for '\0' characters
176 - copy the program string to a one-byte-longer location to have
178 - position invariance test (not writing to result.position) moved
182 typedef GLubyte
*production
;
186 * This is the text describing the rules to parse the grammar
188 LONGSTRING
static char arb_grammar_text
[] =
189 #include "arbprogram_syn.h"
193 * These should match up with the values defined in arbprogram.syn
198 - changed and merged V_* and F_* opcode values to OP_*.
199 - added GL_ARB_fragment_program_shadow specific tokens (michal)
201 #define REVISION 0x0a
204 #define FRAGMENT_PROGRAM 0x01
205 #define VERTEX_PROGRAM 0x02
207 /* program section */
209 #define INSTRUCTION 0x02
210 #define DECLARATION 0x03
213 /* GL_ARB_fragment_program option */
214 #define ARB_PRECISION_HINT_FASTEST 0x00
215 #define ARB_PRECISION_HINT_NICEST 0x01
216 #define ARB_FOG_EXP 0x02
217 #define ARB_FOG_EXP2 0x03
218 #define ARB_FOG_LINEAR 0x04
220 /* GL_ARB_vertex_program option */
221 #define ARB_POSITION_INVARIANT 0x05
223 /* GL_ARB_fragment_program_shadow option */
224 #define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
226 /* GL_ARB_draw_buffers option */
227 #define ARB_DRAW_BUFFERS 0x07
229 /* GL_MESA_texture_array option */
230 #define MESA_TEXTURE_ARRAY 0x08
232 /* GL_ARB_fragment_program instruction class */
233 #define OP_ALU_INST 0x00
234 #define OP_TEX_INST 0x01
236 /* GL_ARB_vertex_program instruction class */
239 /* GL_ARB_fragment_program instruction type */
240 #define OP_ALU_VECTOR 0x00
241 #define OP_ALU_SCALAR 0x01
242 #define OP_ALU_BINSC 0x02
243 #define OP_ALU_BIN 0x03
244 #define OP_ALU_TRI 0x04
245 #define OP_ALU_SWZ 0x05
246 #define OP_TEX_SAMPLE 0x06
247 #define OP_TEX_KIL 0x07
249 /* GL_ARB_vertex_program instruction type */
250 #define OP_ALU_ARL 0x08
258 /* GL_ARB_fragment_program instruction code */
260 #define OP_ABS_SAT 0x1B
262 #define OP_FLR_SAT 0x26
264 #define OP_FRC_SAT 0x27
266 #define OP_LIT_SAT 0x2A
268 #define OP_MOV_SAT 0x30
270 #define OP_COS_SAT 0x20
272 #define OP_EX2_SAT 0x25
274 #define OP_LG2_SAT 0x29
276 #define OP_RCP_SAT 0x33
278 #define OP_RSQ_SAT 0x34
280 #define OP_SIN_SAT 0x39
282 #define OP_SCS_SAT 0x36
284 #define OP_POW_SAT 0x32
286 #define OP_ADD_SAT 0x1C
288 #define OP_DP3_SAT 0x21
290 #define OP_DP4_SAT 0x22
292 #define OP_DPH_SAT 0x23
294 #define OP_DST_SAT 0x24
296 #define OP_MAX_SAT 0x2E
298 #define OP_MIN_SAT 0x2F
300 #define OP_MUL_SAT 0x31
302 #define OP_SGE_SAT 0x37
304 #define OP_SLT_SAT 0x3A
306 #define OP_SUB_SAT 0x3B
308 #define OP_XPD_SAT 0x43
310 #define OP_CMP_SAT 0x1E
312 #define OP_LRP_SAT 0x2C
314 #define OP_MAD_SAT 0x2D
316 #define OP_SWZ_SAT 0x3C
318 #define OP_TEX_SAT 0x3E
320 #define OP_TXB_SAT 0x40
322 #define OP_TXP_SAT 0x42
325 /* GL_ARB_vertex_program instruction code */
354 /* fragment attribute binding */
355 #define FRAGMENT_ATTRIB_COLOR 0x01
356 #define FRAGMENT_ATTRIB_TEXCOORD 0x02
357 #define FRAGMENT_ATTRIB_FOGCOORD 0x03
358 #define FRAGMENT_ATTRIB_POSITION 0x04
360 /* vertex attribute binding */
361 #define VERTEX_ATTRIB_POSITION 0x01
362 #define VERTEX_ATTRIB_WEIGHT 0x02
363 #define VERTEX_ATTRIB_NORMAL 0x03
364 #define VERTEX_ATTRIB_COLOR 0x04
365 #define VERTEX_ATTRIB_FOGCOORD 0x05
366 #define VERTEX_ATTRIB_TEXCOORD 0x06
367 #define VERTEX_ATTRIB_MATRIXINDEX 0x07
368 #define VERTEX_ATTRIB_GENERIC 0x08
370 /* fragment result binding */
371 #define FRAGMENT_RESULT_COLOR 0x01
372 #define FRAGMENT_RESULT_DEPTH 0x02
374 /* vertex result binding */
375 #define VERTEX_RESULT_POSITION 0x01
376 #define VERTEX_RESULT_COLOR 0x02
377 #define VERTEX_RESULT_FOGCOORD 0x03
378 #define VERTEX_RESULT_POINTSIZE 0x04
379 #define VERTEX_RESULT_TEXCOORD 0x05
382 #define TEXTARGET_1D 0x01
383 #define TEXTARGET_2D 0x02
384 #define TEXTARGET_3D 0x03
385 #define TEXTARGET_RECT 0x04
386 #define TEXTARGET_CUBE 0x05
387 /* GL_ARB_fragment_program_shadow */
388 #define TEXTARGET_SHADOW1D 0x06
389 #define TEXTARGET_SHADOW2D 0x07
390 #define TEXTARGET_SHADOWRECT 0x08
391 /* GL_MESA_texture_array */
392 #define TEXTARGET_1D_ARRAY 0x09
393 #define TEXTARGET_2D_ARRAY 0x0a
394 #define TEXTARGET_SHADOW1D_ARRAY 0x0b
395 #define TEXTARGET_SHADOW2D_ARRAY 0x0c
398 #define FACE_FRONT 0x00
399 #define FACE_BACK 0x01
402 #define COLOR_PRIMARY 0x00
403 #define COLOR_SECONDARY 0x01
406 #define COMPONENT_X 0x00
407 #define COMPONENT_Y 0x01
408 #define COMPONENT_Z 0x02
409 #define COMPONENT_W 0x03
410 #define COMPONENT_0 0x04
411 #define COMPONENT_1 0x05
413 /* array index type */
414 #define ARRAY_INDEX_ABSOLUTE 0x00
415 #define ARRAY_INDEX_RELATIVE 0x01
418 #define MATRIX_MODELVIEW 0x01
419 #define MATRIX_PROJECTION 0x02
420 #define MATRIX_MVP 0x03
421 #define MATRIX_TEXTURE 0x04
422 #define MATRIX_PALETTE 0x05
423 #define MATRIX_PROGRAM 0x06
425 /* matrix modifier */
426 #define MATRIX_MODIFIER_IDENTITY 0x00
427 #define MATRIX_MODIFIER_INVERSE 0x01
428 #define MATRIX_MODIFIER_TRANSPOSE 0x02
429 #define MATRIX_MODIFIER_INVTRANS 0x03
432 #define CONSTANT_SCALAR 0x01
433 #define CONSTANT_VECTOR 0x02
435 /* program param type */
436 #define PROGRAM_PARAM_ENV 0x01
437 #define PROGRAM_PARAM_LOCAL 0x02
440 #define REGISTER_ATTRIB 0x01
441 #define REGISTER_PARAM 0x02
442 #define REGISTER_RESULT 0x03
443 #define REGISTER_ESTABLISHED_NAME 0x04
446 #define PARAM_NULL 0x00
447 #define PARAM_ARRAY_ELEMENT 0x01
448 #define PARAM_STATE_ELEMENT 0x02
449 #define PARAM_PROGRAM_ELEMENT 0x03
450 #define PARAM_PROGRAM_ELEMENTS 0x04
451 #define PARAM_CONSTANT 0x05
453 /* param state property */
454 #define STATE_MATERIAL_PARSER 0x01
455 #define STATE_LIGHT_PARSER 0x02
456 #define STATE_LIGHT_MODEL 0x03
457 #define STATE_LIGHT_PROD 0x04
458 #define STATE_FOG 0x05
459 #define STATE_MATRIX_ROWS 0x06
460 /* GL_ARB_fragment_program */
461 #define STATE_TEX_ENV 0x07
462 #define STATE_DEPTH 0x08
463 /* GL_ARB_vertex_program */
464 #define STATE_TEX_GEN 0x09
465 #define STATE_CLIP_PLANE 0x0A
466 #define STATE_POINT 0x0B
468 /* state material property */
469 #define MATERIAL_AMBIENT 0x01
470 #define MATERIAL_DIFFUSE 0x02
471 #define MATERIAL_SPECULAR 0x03
472 #define MATERIAL_EMISSION 0x04
473 #define MATERIAL_SHININESS 0x05
475 /* state light property */
476 #define LIGHT_AMBIENT 0x01
477 #define LIGHT_DIFFUSE 0x02
478 #define LIGHT_SPECULAR 0x03
479 #define LIGHT_POSITION 0x04
480 #define LIGHT_ATTENUATION 0x05
481 #define LIGHT_HALF 0x06
482 #define LIGHT_SPOT_DIRECTION 0x07
484 /* state light model property */
485 #define LIGHT_MODEL_AMBIENT 0x01
486 #define LIGHT_MODEL_SCENECOLOR 0x02
488 /* state light product property */
489 #define LIGHT_PROD_AMBIENT 0x01
490 #define LIGHT_PROD_DIFFUSE 0x02
491 #define LIGHT_PROD_SPECULAR 0x03
493 /* state texture environment property */
494 #define TEX_ENV_COLOR 0x01
496 /* state texture generation coord property */
497 #define TEX_GEN_EYE 0x01
498 #define TEX_GEN_OBJECT 0x02
500 /* state fog property */
501 #define FOG_COLOR 0x01
502 #define FOG_PARAMS 0x02
504 /* state depth property */
505 #define DEPTH_RANGE 0x01
507 /* state point parameters property */
508 #define POINT_SIZE 0x01
509 #define POINT_ATTENUATION 0x02
517 /* GL_ARB_vertex_program */
520 /*-----------------------------------------------------------------------
521 * From here on down is the semantic checking portion
526 * Variable Table Handling functions
541 * Setting an explicit field for each of the binding properties is a bit
542 * wasteful of space, but it should be much more clear when reading later on..
546 const GLubyte
*name
; /* don't free() - no need */
548 GLuint address_binding
; /* The index of the address register we should
550 GLuint attrib_binding
; /* For type vt_attrib, see nvfragprog.h for values */
551 GLuint attrib_is_generic
; /* If the attrib was specified through a generic
553 GLuint temp_binding
; /* The index of the temp register we are to use */
554 GLuint output_binding
; /* Output/result register number */
555 struct var_cache
*alias_binding
; /* For type vt_alias, points to the var_cache entry
556 * that this is aliased to */
557 GLuint param_binding_type
; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
558 * PROGRAM_ENV_PARAM} */
559 GLuint param_binding_begin
; /* This is the offset into the program_parameter_list where
560 * the tokens representing our bound state (or constants)
562 GLuint param_binding_length
; /* This is how many entries in the the program_parameter_list
563 * we take up with our state tokens or constants. Note that
564 * this is _not_ the same as the number of param registers
565 * we eventually use */
566 GLuint swizzle
; /**< swizzle to access this variable */
567 struct var_cache
*next
;
571 var_cache_create (struct var_cache
**va
)
573 *va
= (struct var_cache
*) _mesa_malloc (sizeof (struct var_cache
));
576 (**va
).type
= vt_none
;
577 (**va
).attrib_binding
= ~0;
578 (**va
).attrib_is_generic
= 0;
579 (**va
).temp_binding
= ~0;
580 (**va
).output_binding
= ~0;
581 (**va
).param_binding_type
= ~0;
582 (**va
).param_binding_begin
= ~0;
583 (**va
).param_binding_length
= ~0;
584 (**va
).alias_binding
= NULL
;
585 (**va
).swizzle
= SWIZZLE_XYZW
;
591 var_cache_destroy (struct var_cache
**va
)
594 var_cache_destroy (&(**va
).next
);
601 var_cache_append (struct var_cache
**va
, struct var_cache
*nv
)
604 var_cache_append (&(**va
).next
, nv
);
609 static struct var_cache
*
610 var_cache_find (struct var_cache
*va
, const GLubyte
* name
)
612 /*struct var_cache *first = va;*/
615 if (!_mesa_strcmp ( (const char*) name
, (const char*) va
->name
)) {
616 if (va
->type
== vt_alias
)
617 return va
->alias_binding
;
630 * Called when an error is detected while parsing/compiling a program.
631 * Sets the ctx->Program.ErrorString field to descript and records a
632 * GL_INVALID_OPERATION error.
633 * \param position position of error in program string
634 * \param descrip verbose error description
637 program_error(GLcontext
*ctx
, GLint position
, const char *descrip
)
640 const char *prefix
= "glProgramString(", *suffix
= ")";
641 char *str
= (char *) _mesa_malloc(_mesa_strlen(descrip
) +
642 _mesa_strlen(prefix
) +
643 _mesa_strlen(suffix
) + 1);
645 _mesa_sprintf(str
, "%s%s%s", prefix
, descrip
, suffix
);
646 _mesa_error(ctx
, GL_INVALID_OPERATION
, str
);
650 _mesa_set_program_error(ctx
, position
, descrip
);
655 * As above, but with an extra string parameter for more info.
658 program_error2(GLcontext
*ctx
, GLint position
, const char *descrip
,
662 const char *prefix
= "glProgramString(", *suffix
= ")";
663 char *str
= (char *) _mesa_malloc(_mesa_strlen(descrip
) +
666 _mesa_strlen(prefix
) +
667 _mesa_strlen(suffix
) + 1);
669 _mesa_sprintf(str
, "%s%s: %s%s", prefix
, descrip
, var
, suffix
);
670 _mesa_error(ctx
, GL_INVALID_OPERATION
, str
);
675 char *str
= (char *) _mesa_malloc(_mesa_strlen(descrip
) +
677 _mesa_strlen(var
) + 1);
679 _mesa_sprintf(str
, "%s: %s", descrip
, var
);
681 _mesa_set_program_error(ctx
, position
, str
);
691 * constructs an integer from 4 GLubytes in LE format
694 parse_position (const GLubyte
** inst
)
698 value
= (GLuint
) (*(*inst
)++);
699 value
+= (GLuint
) (*(*inst
)++) * 0x100;
700 value
+= (GLuint
) (*(*inst
)++) * 0x10000;
701 value
+= (GLuint
) (*(*inst
)++) * 0x1000000;
707 * This will, given a string, lookup the string as a variable name in the
708 * var cache. If the name is found, the var cache node corresponding to the
709 * var name is returned. If it is not found, a new entry is allocated
711 * \param I Points into the binary array where the string identifier begins
712 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
713 * \return The location on the var_cache corresponding the the string starting at I
715 static struct var_cache
*
716 parse_string (const GLubyte
** inst
, struct var_cache
**vc_head
,
717 struct arb_program
*Program
, GLuint
* found
)
719 const GLubyte
*i
= *inst
;
720 struct var_cache
*va
= NULL
;
723 *inst
+= _mesa_strlen ((char *) i
) + 1;
725 va
= var_cache_find (*vc_head
, i
);
733 var_cache_create (&va
);
734 va
->name
= (const GLubyte
*) i
;
736 var_cache_append (vc_head
, va
);
742 parse_string_without_adding (const GLubyte
** inst
, struct arb_program
*Program
)
744 const GLubyte
*i
= *inst
;
747 *inst
+= _mesa_strlen ((char *) i
) + 1;
753 * \return -1 if we parse '-', return 1 otherwise
756 parse_sign (const GLubyte
** inst
)
758 /*return *(*inst)++ != '+'; */
764 else if (**inst
== '+') {
773 * parses and returns signed integer
776 parse_integer (const GLubyte
** inst
, struct arb_program
*Program
)
781 /* check if *inst points to '+' or '-'
782 * if yes, grab the sign and increment *inst
784 sign
= parse_sign (inst
);
786 /* now check if *inst points to 0
787 * if yes, increment the *inst and return the default value
794 /* parse the integer as you normally would do it */
795 value
= _mesa_atoi (parse_string_without_adding (inst
, Program
));
797 /* now, after terminating 0 there is a position
798 * to parse it - parse_position()
800 Program
->Position
= parse_position (inst
);
806 Accumulate this string of digits, and return them as
807 a large integer represented in floating point (for range).
808 If scale is not NULL, also accumulates a power-of-ten
809 integer scale factor that represents the number of digits
813 parse_float_string(const GLubyte
** inst
, struct arb_program
*Program
, GLdouble
*scale
)
815 GLdouble value
= 0.0;
816 GLdouble oscale
= 1.0;
818 if (**inst
== 0) { /* this string of digits is empty-- do nothing */
821 else { /* nonempty string-- parse out the digits */
822 while (**inst
>= '0' && **inst
<= '9') {
823 GLubyte digit
= *((*inst
)++);
824 value
= value
* 10.0 + (GLint
) (digit
- '0');
827 assert(**inst
== 0); /* integer string should end with 0 */
828 (*inst
)++; /* skip over terminating 0 */
829 Program
->Position
= parse_position(inst
); /* skip position (from integer) */
837 Parse an unsigned floating-point number from this stream of tokenized
838 characters. Example floating-point formats supported:
846 parse_float (const GLubyte
** inst
, struct arb_program
*Program
)
849 GLdouble whole
, fraction
, fracScale
= 1.0;
851 whole
= parse_float_string(inst
, Program
, 0);
852 fraction
= parse_float_string(inst
, Program
, &fracScale
);
854 /* Parse signed exponent */
855 exponent
= parse_integer(inst
, Program
); /* This is the exponent */
857 /* Assemble parts of floating-point number: */
858 return (GLfloat
) ((whole
+ fraction
/ fracScale
) *
859 _mesa_pow(10.0, (GLfloat
) exponent
));
866 parse_signed_float (const GLubyte
** inst
, struct arb_program
*Program
)
868 GLint sign
= parse_sign (inst
);
869 GLfloat value
= parse_float (inst
, Program
);
874 * This picks out a constant value from the parsed array. The constant vector is r
875 * returned in the *values array, which should be of length 4.
877 * \param values - return the vector constant values.
878 * \param size - returns the number elements in valuesOut [1..4]
881 parse_constant(const GLubyte
** inst
, GLfloat
*values
, GLint
*size
,
882 struct arb_program
*Program
,
885 GLuint components
, i
;
887 switch (*(*inst
)++) {
888 case CONSTANT_SCALAR
:
889 if (use
== GL_TRUE
) {
892 values
[2] = values
[3] = parse_float (inst
, Program
);
897 values
[2] = values
[3] = parse_signed_float (inst
, Program
);
901 case CONSTANT_VECTOR
:
902 values
[0] = values
[1] = values
[2] = 0;
904 components
= *(*inst
)++;
905 for (i
= 0; i
< components
; i
++) {
906 values
[i
] = parse_signed_float (inst
, Program
);
911 _mesa_problem(NULL
, "unexpected case in parse_constant()");
918 * \param offset The offset from the address register that we should
921 * \return 0 on sucess, 1 on error
924 parse_relative_offset(GLcontext
*ctx
, const GLubyte
**inst
,
925 struct arb_program
*Program
, GLint
*offset
)
928 *offset
= parse_integer(inst
, Program
);
933 * \param color 0 if color type is primary, 1 if color type is secondary
934 * \return 0 on sucess, 1 on error
937 parse_color_type (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
940 (void) ctx
; (void) Program
;
941 *color
= *(*inst
)++ != COLOR_PRIMARY
;
946 * Get an integer corresponding to a generic vertex attribute.
948 * \return 0 on sucess, 1 on error
951 parse_generic_attrib_num(GLcontext
*ctx
, const GLubyte
** inst
,
952 struct arb_program
*Program
, GLuint
*attrib
)
954 GLint i
= parse_integer(inst
, Program
);
956 if ((i
< 0) || (i
>= MAX_VERTEX_GENERIC_ATTRIBS
))
958 program_error(ctx
, Program
->Position
,
959 "Invalid generic vertex attribute index");
963 *attrib
= (GLuint
) i
;
970 * \param color The index of the color buffer to write into
971 * \return 0 on sucess, 1 on error
974 parse_output_color_num (GLcontext
* ctx
, const GLubyte
** inst
,
975 struct arb_program
*Program
, GLuint
* color
)
977 GLint i
= parse_integer (inst
, Program
);
979 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxDrawBuffers
)) {
981 program_error(ctx
, Program
->Position
, "Invalid draw buffer index");
991 * Validate the index of a texture coordinate
993 * \param coord The texture unit index
994 * \return 0 on sucess, 1 on error
997 parse_texcoord_num (GLcontext
* ctx
, const GLubyte
** inst
,
998 struct arb_program
*Program
, GLuint
* coord
)
1000 GLint i
= parse_integer (inst
, Program
);
1002 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxTextureCoordUnits
)) {
1003 program_error(ctx
, Program
->Position
, "Invalid texture coordinate index");
1007 *coord
= (GLuint
) i
;
1013 * Validate the index of a texture image unit
1015 * \param coord The texture unit index
1016 * \return 0 on sucess, 1 on error
1019 parse_teximage_num (GLcontext
* ctx
, const GLubyte
** inst
,
1020 struct arb_program
*Program
, GLuint
* coord
)
1022 GLint i
= parse_integer (inst
, Program
);
1024 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxTextureImageUnits
)) {
1026 _mesa_snprintf(s
, sizeof(s
), "Invalid texture image index %d (%u is max)",
1027 i
, ctx
->Const
.MaxTextureImageUnits
);
1028 program_error(ctx
, Program
->Position
, s
);
1032 *coord
= (GLuint
) i
;
1038 * \param coord The weight index
1039 * \return 0 on sucess, 1 on error
1042 parse_weight_num (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
1045 *coord
= parse_integer (inst
, Program
);
1047 if ((*coord
< 0) || (*coord
>= 1)) {
1048 program_error(ctx
, Program
->Position
, "Invalid weight index");
1056 * \param coord The clip plane index
1057 * \return 0 on sucess, 1 on error
1060 parse_clipplane_num (GLcontext
* ctx
, const GLubyte
** inst
,
1061 struct arb_program
*Program
, GLint
* coord
)
1063 *coord
= parse_integer (inst
, Program
);
1065 if ((*coord
< 0) || (*coord
>= (GLint
) ctx
->Const
.MaxClipPlanes
)) {
1066 program_error(ctx
, Program
->Position
, "Invalid clip plane index");
1075 * \return 0 on front face, 1 on back face
1078 parse_face_type (const GLubyte
** inst
)
1080 switch (*(*inst
)++) {
1092 * Given a matrix and a modifier token on the binary array, return tokens
1093 * that _mesa_fetch_state() [program.c] can understand.
1095 * \param matrix - the matrix we are talking about
1096 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
1097 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
1098 * \return 0 on sucess, 1 on failure
1101 parse_matrix (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
1102 GLint
* matrix
, GLint
* matrix_idx
, GLint
* matrix_modifier
)
1104 GLubyte mat
= *(*inst
)++;
1109 case MATRIX_MODELVIEW
:
1110 *matrix
= STATE_MODELVIEW_MATRIX
;
1111 *matrix_idx
= parse_integer (inst
, Program
);
1112 if (*matrix_idx
> 0) {
1113 program_error(ctx
, Program
->Position
,
1114 "ARB_vertex_blend not supported");
1119 case MATRIX_PROJECTION
:
1120 *matrix
= STATE_PROJECTION_MATRIX
;
1124 *matrix
= STATE_MVP_MATRIX
;
1127 case MATRIX_TEXTURE
:
1128 *matrix
= STATE_TEXTURE_MATRIX
;
1129 *matrix_idx
= parse_integer (inst
, Program
);
1130 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxTextureUnits
) {
1131 program_error(ctx
, Program
->Position
, "Invalid Texture Unit");
1132 /* bad *matrix_id */
1137 /* This is not currently supported (ARB_matrix_palette) */
1138 case MATRIX_PALETTE
:
1139 *matrix_idx
= parse_integer (inst
, Program
);
1140 program_error(ctx
, Program
->Position
,
1141 "ARB_matrix_palette not supported");
1145 case MATRIX_PROGRAM
:
1146 *matrix
= STATE_PROGRAM_MATRIX
;
1147 *matrix_idx
= parse_integer (inst
, Program
);
1148 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxProgramMatrices
) {
1149 program_error(ctx
, Program
->Position
, "Invalid Program Matrix");
1150 /* bad *matrix_idx */
1156 switch (*(*inst
)++) {
1157 case MATRIX_MODIFIER_IDENTITY
:
1158 *matrix_modifier
= 0;
1160 case MATRIX_MODIFIER_INVERSE
:
1161 *matrix_modifier
= STATE_MATRIX_INVERSE
;
1163 case MATRIX_MODIFIER_TRANSPOSE
:
1164 *matrix_modifier
= STATE_MATRIX_TRANSPOSE
;
1166 case MATRIX_MODIFIER_INVTRANS
:
1167 *matrix_modifier
= STATE_MATRIX_INVTRANS
;
1176 * This parses a state string (rather, the binary version of it) into
1177 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1179 * \param inst - the start in the binary arry to start working from
1180 * \param state_tokens - the storage for the 6-token state description
1181 * \return - 0 on sucess, 1 on error
1184 parse_state_single_item (GLcontext
* ctx
, const GLubyte
** inst
,
1185 struct arb_program
*Program
,
1186 gl_state_index state_tokens
[STATE_LENGTH
])
1188 GLubyte token
= *(*inst
)++;
1191 case STATE_MATERIAL_PARSER
:
1192 state_tokens
[0] = STATE_MATERIAL
;
1193 state_tokens
[1] = parse_face_type (inst
);
1194 switch (*(*inst
)++) {
1195 case MATERIAL_AMBIENT
:
1196 state_tokens
[2] = STATE_AMBIENT
;
1198 case MATERIAL_DIFFUSE
:
1199 state_tokens
[2] = STATE_DIFFUSE
;
1201 case MATERIAL_SPECULAR
:
1202 state_tokens
[2] = STATE_SPECULAR
;
1204 case MATERIAL_EMISSION
:
1205 state_tokens
[2] = STATE_EMISSION
;
1207 case MATERIAL_SHININESS
:
1208 state_tokens
[2] = STATE_SHININESS
;
1213 case STATE_LIGHT_PARSER
:
1214 state_tokens
[0] = STATE_LIGHT
;
1215 state_tokens
[1] = parse_integer (inst
, Program
);
1217 /* Check the value of state_tokens[1] against the # of lights */
1218 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1219 program_error(ctx
, Program
->Position
, "Invalid Light Number");
1220 /* bad state_tokens[1] */
1224 switch (*(*inst
)++) {
1226 state_tokens
[2] = STATE_AMBIENT
;
1229 state_tokens
[2] = STATE_DIFFUSE
;
1231 case LIGHT_SPECULAR
:
1232 state_tokens
[2] = STATE_SPECULAR
;
1234 case LIGHT_POSITION
:
1235 state_tokens
[2] = STATE_POSITION
;
1237 case LIGHT_ATTENUATION
:
1238 state_tokens
[2] = STATE_ATTENUATION
;
1241 state_tokens
[2] = STATE_HALF_VECTOR
;
1243 case LIGHT_SPOT_DIRECTION
:
1244 state_tokens
[2] = STATE_SPOT_DIRECTION
;
1249 case STATE_LIGHT_MODEL
:
1250 switch (*(*inst
)++) {
1251 case LIGHT_MODEL_AMBIENT
:
1252 state_tokens
[0] = STATE_LIGHTMODEL_AMBIENT
;
1254 case LIGHT_MODEL_SCENECOLOR
:
1255 state_tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
1256 state_tokens
[1] = parse_face_type (inst
);
1261 case STATE_LIGHT_PROD
:
1262 state_tokens
[0] = STATE_LIGHTPROD
;
1263 state_tokens
[1] = parse_integer (inst
, Program
);
1265 /* Check the value of state_tokens[1] against the # of lights */
1266 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1267 program_error(ctx
, Program
->Position
, "Invalid Light Number");
1268 /* bad state_tokens[1] */
1272 state_tokens
[2] = parse_face_type (inst
);
1273 switch (*(*inst
)++) {
1274 case LIGHT_PROD_AMBIENT
:
1275 state_tokens
[3] = STATE_AMBIENT
;
1277 case LIGHT_PROD_DIFFUSE
:
1278 state_tokens
[3] = STATE_DIFFUSE
;
1280 case LIGHT_PROD_SPECULAR
:
1281 state_tokens
[3] = STATE_SPECULAR
;
1288 switch (*(*inst
)++) {
1290 state_tokens
[0] = STATE_FOG_COLOR
;
1293 state_tokens
[0] = STATE_FOG_PARAMS
;
1299 state_tokens
[1] = parse_integer (inst
, Program
);
1300 switch (*(*inst
)++) {
1302 state_tokens
[0] = STATE_TEXENV_COLOR
;
1311 state_tokens
[0] = STATE_TEXGEN
;
1312 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1314 if (parse_texcoord_num (ctx
, inst
, Program
, &coord
))
1316 state_tokens
[1] = coord
;
1321 /* 0 - s, 1 - t, 2 - r, 3 - q */
1324 if (type
== TEX_GEN_EYE
) {
1327 state_tokens
[2] = STATE_TEXGEN_EYE_S
;
1330 state_tokens
[2] = STATE_TEXGEN_EYE_T
;
1333 state_tokens
[2] = STATE_TEXGEN_EYE_R
;
1336 state_tokens
[2] = STATE_TEXGEN_EYE_Q
;
1339 _mesa_problem(ctx
, "bad texgen component in "
1340 "parse_state_single_item()");
1346 state_tokens
[2] = STATE_TEXGEN_OBJECT_S
;
1349 state_tokens
[2] = STATE_TEXGEN_OBJECT_T
;
1352 state_tokens
[2] = STATE_TEXGEN_OBJECT_R
;
1355 state_tokens
[2] = STATE_TEXGEN_OBJECT_Q
;
1358 _mesa_problem(ctx
, "bad texgen component in "
1359 "parse_state_single_item()");
1366 switch (*(*inst
)++) {
1368 state_tokens
[0] = STATE_DEPTH_RANGE
;
1373 case STATE_CLIP_PLANE
:
1374 state_tokens
[0] = STATE_CLIPPLANE
;
1375 if (parse_clipplane_num (ctx
, inst
, Program
,
1376 (GLint
*) &state_tokens
[1]))
1381 switch (*(*inst
)++) {
1383 state_tokens
[0] = STATE_POINT_SIZE
;
1386 case POINT_ATTENUATION
:
1387 state_tokens
[0] = STATE_POINT_ATTENUATION
;
1392 /* XXX: I think this is the correct format for a matrix row */
1393 case STATE_MATRIX_ROWS
:
1394 if (parse_matrix(ctx
, inst
, Program
,
1395 (GLint
*) &state_tokens
[0],
1396 (GLint
*) &state_tokens
[1],
1397 (GLint
*) &state_tokens
[4]))
1400 state_tokens
[2] = parse_integer (inst
, Program
); /* The first row to grab */
1402 if ((**inst
) != 0) { /* Either the last row, 0 */
1403 state_tokens
[3] = parse_integer (inst
, Program
);
1404 if (state_tokens
[3] < state_tokens
[2]) {
1405 program_error(ctx
, Program
->Position
,
1406 "Second matrix index less than the first");
1407 /* state_tokens[4] vs. state_tokens[3] */
1412 state_tokens
[3] = state_tokens
[2];
1422 * This parses a state string (rather, the binary version of it) into
1423 * a 6-token similar for the state fetching code in program.c
1425 * One might ask, why fetch these parameters into just like you fetch
1426 * state when they are already stored in other places?
1428 * Because of array offsets -> We can stick env/local parameters in the
1429 * middle of a parameter array and then index someplace into the array
1432 * One optimization might be to only do this for the cases where the
1433 * env/local parameters end up inside of an array, and leave the
1434 * single parameters (or arrays of pure env/local pareameters) in their
1435 * respective register files.
1437 * For ENV parameters, the format is:
1438 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1439 * state_tokens[1] = STATE_ENV
1440 * state_tokens[2] = the parameter index
1442 * for LOCAL parameters, the format is:
1443 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1444 * state_tokens[1] = STATE_LOCAL
1445 * state_tokens[2] = the parameter index
1447 * \param inst - the start in the binary arry to start working from
1448 * \param state_tokens - the storage for the 6-token state description
1449 * \return - 0 on sucess, 1 on failure
1452 parse_program_single_item (GLcontext
* ctx
, const GLubyte
** inst
,
1453 struct arb_program
*Program
,
1454 gl_state_index state_tokens
[STATE_LENGTH
])
1456 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1457 state_tokens
[0] = STATE_FRAGMENT_PROGRAM
;
1459 state_tokens
[0] = STATE_VERTEX_PROGRAM
;
1462 switch (*(*inst
)++) {
1463 case PROGRAM_PARAM_ENV
:
1464 state_tokens
[1] = STATE_ENV
;
1465 state_tokens
[2] = parse_integer (inst
, Program
);
1467 /* Check state_tokens[2] against the number of ENV parameters available */
1468 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1469 (state_tokens
[2] >= (GLint
) ctx
->Const
.FragmentProgram
.MaxEnvParams
))
1471 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1472 (state_tokens
[2] >= (GLint
) ctx
->Const
.VertexProgram
.MaxEnvParams
))) {
1473 program_error(ctx
, Program
->Position
,
1474 "Invalid Program Env Parameter");
1475 /* bad state_tokens[2] */
1481 case PROGRAM_PARAM_LOCAL
:
1482 state_tokens
[1] = STATE_LOCAL
;
1483 state_tokens
[2] = parse_integer (inst
, Program
);
1485 /* Check state_tokens[2] against the number of LOCAL parameters available */
1486 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1487 (state_tokens
[2] >= (GLint
) ctx
->Const
.FragmentProgram
.MaxLocalParams
))
1489 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1490 (state_tokens
[2] >= (GLint
) ctx
->Const
.VertexProgram
.MaxLocalParams
))) {
1491 program_error(ctx
, Program
->Position
,
1492 "Invalid Program Local Parameter");
1493 /* bad state_tokens[2] */
1503 * For ARB_vertex_program, programs are not allowed to use both an explicit
1504 * vertex attribute and a generic vertex attribute corresponding to the same
1505 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1507 * This will walk our var_cache and make sure that nobody does anything fishy.
1509 * \return 0 on sucess, 1 on error
1512 generic_attrib_check(struct var_cache
*vc_head
)
1515 struct var_cache
*curr
;
1516 GLboolean explicitAttrib
[MAX_VERTEX_GENERIC_ATTRIBS
],
1517 genericAttrib
[MAX_VERTEX_GENERIC_ATTRIBS
];
1519 for (a
=0; a
<MAX_VERTEX_GENERIC_ATTRIBS
; a
++) {
1520 explicitAttrib
[a
] = GL_FALSE
;
1521 genericAttrib
[a
] = GL_FALSE
;
1526 if (curr
->type
== vt_attrib
) {
1527 if (curr
->attrib_is_generic
) {
1528 GLuint attr
= (curr
->attrib_binding
== 0)
1529 ? 0 : (curr
->attrib_binding
- VERT_ATTRIB_GENERIC0
);
1530 assert(attr
< MAX_VERTEX_GENERIC_ATTRIBS
);
1531 genericAttrib
[attr
] = GL_TRUE
;
1534 assert(curr
->attrib_binding
< MAX_VERTEX_GENERIC_ATTRIBS
);
1535 explicitAttrib
[ curr
->attrib_binding
] = GL_TRUE
;
1542 for (a
=0; a
<MAX_VERTEX_GENERIC_ATTRIBS
; a
++) {
1543 if ((explicitAttrib
[a
]) && (genericAttrib
[a
]))
1551 * This will handle the binding side of an ATTRIB var declaration
1553 * \param inputReg returns the input register index, one of the
1554 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
1555 * \return returns 0 on success, 1 on error
1558 parse_attrib_binding(GLcontext
* ctx
, const GLubyte
** inst
,
1559 struct arb_program
*Program
,
1560 GLuint
*inputReg
, GLuint
*is_generic
)
1566 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1567 switch (*(*inst
)++) {
1568 case FRAGMENT_ATTRIB_COLOR
:
1571 err
= parse_color_type (ctx
, inst
, Program
, &coord
);
1572 *inputReg
= FRAG_ATTRIB_COL0
+ coord
;
1575 case FRAGMENT_ATTRIB_TEXCOORD
:
1577 GLuint texcoord
= 0;
1578 err
= parse_texcoord_num (ctx
, inst
, Program
, &texcoord
);
1579 *inputReg
= FRAG_ATTRIB_TEX0
+ texcoord
;
1582 case FRAGMENT_ATTRIB_FOGCOORD
:
1583 *inputReg
= FRAG_ATTRIB_FOGC
;
1585 case FRAGMENT_ATTRIB_POSITION
:
1586 *inputReg
= FRAG_ATTRIB_WPOS
;
1594 switch (*(*inst
)++) {
1595 case VERTEX_ATTRIB_POSITION
:
1596 *inputReg
= VERT_ATTRIB_POS
;
1599 case VERTEX_ATTRIB_WEIGHT
:
1602 err
= parse_weight_num (ctx
, inst
, Program
, &weight
);
1603 *inputReg
= VERT_ATTRIB_WEIGHT
;
1605 /* hack for Warcraft (see bug 8060) */
1606 _mesa_warning(ctx
, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
1609 program_error(ctx
, Program
->Position
,
1610 "ARB_vertex_blend not supported");
1615 case VERTEX_ATTRIB_NORMAL
:
1616 *inputReg
= VERT_ATTRIB_NORMAL
;
1619 case VERTEX_ATTRIB_COLOR
:
1622 err
= parse_color_type (ctx
, inst
, Program
, &color
);
1624 *inputReg
= VERT_ATTRIB_COLOR1
;
1627 *inputReg
= VERT_ATTRIB_COLOR0
;
1632 case VERTEX_ATTRIB_FOGCOORD
:
1633 *inputReg
= VERT_ATTRIB_FOG
;
1636 case VERTEX_ATTRIB_TEXCOORD
:
1639 err
= parse_texcoord_num (ctx
, inst
, Program
, &unit
);
1640 *inputReg
= VERT_ATTRIB_TEX0
+ unit
;
1644 case VERTEX_ATTRIB_MATRIXINDEX
:
1645 /* Not supported at this time */
1647 const char *msg
= "ARB_palette_matrix not supported";
1648 parse_integer (inst
, Program
);
1649 program_error(ctx
, Program
->Position
, msg
);
1653 case VERTEX_ATTRIB_GENERIC
:
1656 err
= parse_generic_attrib_num(ctx
, inst
, Program
, &attrib
);
1659 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1660 * attributes do not alias the conventional vertex
1664 *inputReg
= attrib
+ VERT_ATTRIB_GENERIC0
;
1678 program_error(ctx
, Program
->Position
, "Bad attribute binding");
1686 * This translates between a binary token for an output variable type
1687 * and the mesa token for the same thing.
1689 * \param inst The parsed tokens
1690 * \param outputReg Returned index/number of the output register,
1691 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
1694 parse_result_binding(GLcontext
*ctx
, const GLubyte
**inst
,
1695 GLuint
*outputReg
, struct arb_program
*Program
)
1697 const GLubyte token
= *(*inst
)++;
1700 case FRAGMENT_RESULT_COLOR
:
1701 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1704 /* This gets result of the color buffer we're supposed to
1705 * draw into. This pertains to GL_ARB_draw_buffers.
1707 parse_output_color_num(ctx
, inst
, Program
, &out_color
);
1708 ASSERT(out_color
< MAX_DRAW_BUFFERS
);
1709 *outputReg
= FRAG_RESULT_COLOR
;
1712 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1713 *outputReg
= VERT_RESULT_HPOS
;
1717 case FRAGMENT_RESULT_DEPTH
:
1718 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1719 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
1720 *outputReg
= FRAG_RESULT_DEPTH
;
1723 /* for vtx programs, this is VERTEX_RESULT_COLOR */
1725 GLuint face_type
= parse_face_type(inst
);
1726 GLint err
= parse_color_type(ctx
, inst
, Program
, &color_type
);
1733 *outputReg
= VERT_RESULT_BFC1
; /* secondary color */
1736 *outputReg
= VERT_RESULT_BFC0
; /* primary color */
1742 *outputReg
= VERT_RESULT_COL1
; /* secondary color */
1746 *outputReg
= VERT_RESULT_COL0
; /* primary color */
1752 case VERTEX_RESULT_FOGCOORD
:
1753 *outputReg
= VERT_RESULT_FOGC
;
1756 case VERTEX_RESULT_POINTSIZE
:
1757 *outputReg
= VERT_RESULT_PSIZ
;
1760 case VERTEX_RESULT_TEXCOORD
:
1763 if (parse_texcoord_num (ctx
, inst
, Program
, &unit
))
1765 *outputReg
= VERT_RESULT_TEX0
+ unit
;
1770 Program
->Base
.OutputsWritten
|= (1 << *outputReg
);
1777 * This handles the declaration of ATTRIB variables
1780 * parse_vert_attrib_binding(), or something like that
1782 * \return 0 on sucess, 1 on error
1785 parse_attrib (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1786 struct arb_program
*Program
)
1789 struct var_cache
*attrib_var
;
1791 attrib_var
= parse_string (inst
, vc_head
, Program
, &found
);
1792 Program
->Position
= parse_position (inst
);
1794 program_error2(ctx
, Program
->Position
,
1795 "Duplicate variable declaration",
1796 (char *) attrib_var
->name
);
1800 attrib_var
->type
= vt_attrib
;
1802 if (parse_attrib_binding(ctx
, inst
, Program
, &attrib_var
->attrib_binding
,
1803 &attrib_var
->attrib_is_generic
))
1806 if (generic_attrib_check(*vc_head
)) {
1807 program_error(ctx
, Program
->Position
,
1808 "Cannot use both a generic vertex attribute "
1809 "and a specific attribute of the same type");
1813 Program
->Base
.NumAttributes
++;
1818 * \param use -- TRUE if we're called when declaring implicit parameters,
1819 * FALSE if we're declaraing variables. This has to do with
1820 * if we get a signed or unsigned float for scalar constants
1823 parse_param_elements (GLcontext
* ctx
, const GLubyte
** inst
,
1824 struct var_cache
*param_var
,
1825 struct arb_program
*Program
, GLboolean use
)
1829 gl_state_index state_tokens
[STATE_LENGTH
] = {0, 0, 0, 0, 0};
1831 GLubyte token
= *(*inst
)++;
1834 case PARAM_STATE_ELEMENT
:
1835 if (parse_state_single_item (ctx
, inst
, Program
, state_tokens
))
1838 /* If we adding STATE_MATRIX that has multiple rows, we need to
1839 * unroll it and call _mesa_add_state_reference() for each row
1841 if ((state_tokens
[0] == STATE_MODELVIEW_MATRIX
||
1842 state_tokens
[0] == STATE_PROJECTION_MATRIX
||
1843 state_tokens
[0] == STATE_MVP_MATRIX
||
1844 state_tokens
[0] == STATE_TEXTURE_MATRIX
||
1845 state_tokens
[0] == STATE_PROGRAM_MATRIX
)
1846 && (state_tokens
[2] != state_tokens
[3])) {
1848 const GLint first_row
= state_tokens
[2];
1849 const GLint last_row
= state_tokens
[3];
1851 for (row
= first_row
; row
<= last_row
; row
++) {
1852 state_tokens
[2] = state_tokens
[3] = row
;
1854 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1856 if (param_var
->param_binding_begin
== ~0U)
1857 param_var
->param_binding_begin
= idx
;
1858 param_var
->param_binding_length
++;
1862 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1864 if (param_var
->param_binding_begin
== ~0U)
1865 param_var
->param_binding_begin
= idx
;
1866 param_var
->param_binding_length
++;
1870 case PARAM_PROGRAM_ELEMENT
:
1871 if (parse_program_single_item (ctx
, inst
, Program
, state_tokens
))
1873 idx
= _mesa_add_state_reference (Program
->Base
.Parameters
, state_tokens
);
1874 if (param_var
->param_binding_begin
== ~0U)
1875 param_var
->param_binding_begin
= idx
;
1876 param_var
->param_binding_length
++;
1878 /* Check if there is more: 0 -> we're done, else its an integer */
1880 GLuint out_of_range
, new_idx
;
1881 GLuint start_idx
= state_tokens
[2] + 1;
1882 GLuint end_idx
= parse_integer (inst
, Program
);
1885 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1886 if (((state_tokens
[1] == STATE_ENV
)
1887 && (end_idx
>= ctx
->Const
.FragmentProgram
.MaxEnvParams
))
1888 || ((state_tokens
[1] == STATE_LOCAL
)
1890 ctx
->Const
.FragmentProgram
.MaxLocalParams
)))
1894 if (((state_tokens
[1] == STATE_ENV
)
1895 && (end_idx
>= ctx
->Const
.VertexProgram
.MaxEnvParams
))
1896 || ((state_tokens
[1] == STATE_LOCAL
)
1898 ctx
->Const
.VertexProgram
.MaxLocalParams
)))
1902 program_error(ctx
, Program
->Position
,
1903 "Invalid Program Parameter"); /*end_idx*/
1907 for (new_idx
= start_idx
; new_idx
<= end_idx
; new_idx
++) {
1908 state_tokens
[2] = new_idx
;
1909 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1911 param_var
->param_binding_length
++;
1919 case PARAM_CONSTANT
:
1920 /* parsing something like {1.0, 2.0, 3.0, 4.0} */
1922 GLfloat const_values
[4];
1924 parse_constant(inst
, const_values
, &size
, Program
, use
);
1925 if (param_var
->name
[0] == ' ') {
1926 /* this is an unnamed constant */
1927 idx
= _mesa_add_unnamed_constant(Program
->Base
.Parameters
,
1929 ¶m_var
->swizzle
);
1932 /* named parameter/constant */
1933 idx
= _mesa_add_named_constant(Program
->Base
.Parameters
,
1934 (char *) param_var
->name
,
1935 const_values
, size
);
1937 if (param_var
->param_binding_begin
== ~0U)
1938 param_var
->param_binding_begin
= idx
;
1939 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1940 /* Note: when we reference this parameter in an instruction later,
1941 * we'll check if it's really a constant/immediate and set the
1942 * instruction register type appropriately.
1944 param_var
->param_binding_length
++;
1949 program_error(ctx
, Program
->Position
,
1950 "Unexpected token (in parse_param_elements())");
1954 Program
->Base
.NumParameters
= Program
->Base
.Parameters
->NumParameters
;
1956 /* Make sure we haven't blown past our parameter limits */
1957 if (((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1958 (Program
->Base
.NumParameters
>
1959 ctx
->Const
.VertexProgram
.MaxLocalParams
))
1960 || ((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1961 && (Program
->Base
.NumParameters
>
1962 ctx
->Const
.FragmentProgram
.MaxLocalParams
))) {
1963 program_error(ctx
, Program
->Position
, "Too many parameter variables");
1972 * This picks out PARAM program parameter bindings.
1974 * XXX: This needs to be stressed & tested
1976 * \return 0 on sucess, 1 on error
1979 parse_param (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1980 struct arb_program
*Program
)
1983 GLint specified_length
;
1984 struct var_cache
*param_var
;
1987 param_var
= parse_string (inst
, vc_head
, Program
, &found
);
1988 Program
->Position
= parse_position (inst
);
1991 program_error2(ctx
, Program
->Position
,
1992 "Duplicate variable declaration",
1993 (char *) param_var
->name
);
1997 specified_length
= parse_integer (inst
, Program
);
1999 if (specified_length
< 0) {
2000 program_error(ctx
, Program
->Position
, "Negative parameter array length");
2004 param_var
->type
= vt_param
;
2005 param_var
->param_binding_length
= 0;
2007 /* Right now, everything is shoved into the main state register file.
2009 * In the future, it would be nice to leave things ENV/LOCAL params
2010 * in their respective register files, if possible
2012 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
2015 * * - add each guy to the parameter list
2016 * * - increment the param_var->param_binding_len
2017 * * - store the param_var->param_binding_begin for the first one
2018 * * - compare the actual len to the specified len at the end
2020 while (**inst
!= PARAM_NULL
) {
2021 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_FALSE
))
2025 /* Test array length here! */
2026 if (specified_length
) {
2027 if (specified_length
!= (int)param_var
->param_binding_length
) {
2028 program_error(ctx
, Program
->Position
,
2029 "Declared parameter array length does not match parameter list");
2042 parse_param_use (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2043 struct arb_program
*Program
, struct var_cache
**new_var
)
2045 struct var_cache
*param_var
;
2047 /* First, insert a dummy entry into the var_cache */
2048 var_cache_create (¶m_var
);
2049 param_var
->name
= (const GLubyte
*) " ";
2050 param_var
->type
= vt_param
;
2052 param_var
->param_binding_length
= 0;
2053 /* Don't fill in binding_begin; We use the default value of -1
2054 * to tell if its already initialized, elsewhere.
2056 * param_var->param_binding_begin = 0;
2058 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
2060 var_cache_append (vc_head
, param_var
);
2062 /* Then fill it with juicy parameter goodness */
2063 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_TRUE
))
2066 *new_var
= param_var
;
2073 * This handles the declaration of TEMP variables
2075 * \return 0 on sucess, 1 on error
2078 parse_temp (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2079 struct arb_program
*Program
)
2082 struct var_cache
*temp_var
;
2084 while (**inst
!= 0) {
2085 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2086 Program
->Position
= parse_position (inst
);
2088 program_error2(ctx
, Program
->Position
,
2089 "Duplicate variable declaration",
2090 (char *) temp_var
->name
);
2094 temp_var
->type
= vt_temp
;
2096 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
2097 (Program
->Base
.NumTemporaries
>=
2098 ctx
->Const
.FragmentProgram
.MaxTemps
))
2099 || ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
2100 && (Program
->Base
.NumTemporaries
>=
2101 ctx
->Const
.VertexProgram
.MaxTemps
))) {
2102 program_error(ctx
, Program
->Position
,
2103 "Too many TEMP variables declared");
2107 temp_var
->temp_binding
= Program
->Base
.NumTemporaries
;
2108 Program
->Base
.NumTemporaries
++;
2116 * This handles variables of the OUTPUT variety
2118 * \return 0 on sucess, 1 on error
2121 parse_output (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2122 struct arb_program
*Program
)
2125 struct var_cache
*output_var
;
2128 output_var
= parse_string (inst
, vc_head
, Program
, &found
);
2129 Program
->Position
= parse_position (inst
);
2131 program_error2(ctx
, Program
->Position
,
2132 "Duplicate variable declaration",
2133 (char *) output_var
->name
);
2137 output_var
->type
= vt_output
;
2139 err
= parse_result_binding(ctx
, inst
, &output_var
->output_binding
, Program
);
2144 * This handles variables of the ALIAS kind
2146 * \return 0 on sucess, 1 on error
2149 parse_alias (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2150 struct arb_program
*Program
)
2153 struct var_cache
*temp_var
;
2155 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2156 Program
->Position
= parse_position (inst
);
2159 program_error2(ctx
, Program
->Position
,
2160 "Duplicate variable declaration",
2161 (char *) temp_var
->name
);
2165 temp_var
->type
= vt_alias
;
2166 temp_var
->alias_binding
= parse_string (inst
, vc_head
, Program
, &found
);
2167 Program
->Position
= parse_position (inst
);
2171 program_error2(ctx
, Program
->Position
,
2172 "Undefined alias value",
2173 (char *) temp_var
->alias_binding
->name
);
2181 * This handles variables of the ADDRESS kind
2183 * \return 0 on sucess, 1 on error
2186 parse_address (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2187 struct arb_program
*Program
)
2190 struct var_cache
*temp_var
;
2192 while (**inst
!= 0) {
2193 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2194 Program
->Position
= parse_position (inst
);
2196 program_error2(ctx
, Program
->Position
,
2197 "Duplicate variable declaration",
2198 (char *) temp_var
->name
);
2202 temp_var
->type
= vt_address
;
2204 if (Program
->Base
.NumAddressRegs
>=
2205 ctx
->Const
.VertexProgram
.MaxAddressRegs
) {
2206 const char *msg
= "Too many ADDRESS variables declared";
2207 program_error(ctx
, Program
->Position
, msg
);
2211 temp_var
->address_binding
= Program
->Base
.NumAddressRegs
;
2212 Program
->Base
.NumAddressRegs
++;
2220 * Parse a program declaration
2222 * \return 0 on sucess, 1 on error
2225 parse_declaration (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2226 struct arb_program
*Program
)
2230 switch (*(*inst
)++) {
2232 err
= parse_address (ctx
, inst
, vc_head
, Program
);
2236 err
= parse_alias (ctx
, inst
, vc_head
, Program
);
2240 err
= parse_attrib (ctx
, inst
, vc_head
, Program
);
2244 err
= parse_output (ctx
, inst
, vc_head
, Program
);
2248 err
= parse_param (ctx
, inst
, vc_head
, Program
);
2252 err
= parse_temp (ctx
, inst
, vc_head
, Program
);
2260 * Handle the parsing out of a masked destination register, either for a
2261 * vertex or fragment program.
2263 * If we are a vertex program, make sure we don't write to
2264 * result.position if we have specified that the program is
2265 * position invariant
2267 * \param File - The register file we write to
2268 * \param Index - The register index we write to
2269 * \param WriteMask - The mask controlling which components we write (1->write)
2271 * \return 0 on sucess, 1 on error
2274 parse_masked_dst_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2275 struct var_cache
**vc_head
, struct arb_program
*Program
,
2276 gl_register_file
*File
, GLuint
*Index
, GLint
*WriteMask
)
2279 struct var_cache
*dst
;
2281 /* We either have a result register specified, or a
2282 * variable that may or may not be writable
2284 switch (*(*inst
)++) {
2285 case REGISTER_RESULT
:
2286 if (parse_result_binding(ctx
, inst
, Index
, Program
))
2288 *File
= PROGRAM_OUTPUT
;
2291 case REGISTER_ESTABLISHED_NAME
:
2292 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2293 Program
->Position
= parse_position (inst
);
2295 /* If the name has never been added to our symbol table, we're hosed */
2297 program_error(ctx
, Program
->Position
, "0: Undefined variable");
2301 switch (dst
->type
) {
2303 *File
= PROGRAM_OUTPUT
;
2304 *Index
= dst
->output_binding
;
2308 *File
= PROGRAM_TEMPORARY
;
2309 *Index
= dst
->temp_binding
;
2312 /* If the var type is not vt_output or vt_temp, no go */
2314 program_error(ctx
, Program
->Position
,
2315 "Destination register is read only");
2321 program_error(ctx
, Program
->Position
,
2322 "Unexpected opcode in parse_masked_dst_reg()");
2327 /* Position invariance test */
2328 /* This test is done now in syntax portion - when position invariance OPTION
2329 is specified, "result.position" rule is disabled so there is no way
2330 to write the position
2332 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2334 program_error(ctx, Program->Position,
2335 "Vertex program specified position invariance and wrote vertex position");
2338 /* And then the mask.
2344 * ==> Need to reverse the order of bits for this!
2346 tmp
= (GLint
) *(*inst
)++;
2347 *WriteMask
= (((tmp
>>3) & 0x1) |
2357 * Handle the parsing of a address register
2359 * \param Index - The register index we write to
2361 * \return 0 on sucess, 1 on error
2364 parse_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2365 struct var_cache
**vc_head
,
2366 struct arb_program
*Program
, GLint
* Index
)
2368 struct var_cache
*dst
;
2371 *Index
= 0; /* XXX */
2373 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2374 Program
->Position
= parse_position (inst
);
2376 /* If the name has never been added to our symbol table, we're hosed */
2378 program_error(ctx
, Program
->Position
, "Undefined variable");
2382 if (dst
->type
!= vt_address
) {
2383 program_error(ctx
, Program
->Position
, "Variable is not of type ADDRESS");
2392 * Handle the parsing out of a masked address register
2394 * \param Index - The register index we write to
2395 * \param WriteMask - The mask controlling which components we write (1->write)
2397 * \return 0 on sucess, 1 on error
2400 parse_masked_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2401 struct var_cache
**vc_head
,
2402 struct arb_program
*Program
, GLint
* Index
,
2403 GLboolean
* WriteMask
)
2405 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, Index
))
2408 /* This should be 0x8 */
2411 /* Writemask of .x is implied */
2413 WriteMask
[1] = WriteMask
[2] = WriteMask
[3] = 0;
2420 * Parse out a swizzle mask.
2422 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
2424 * The len parameter allows us to grab 4 components for a vector
2425 * swizzle, or just 1 component for a scalar src register selection
2428 parse_swizzle_mask(const GLubyte
** inst
, GLubyte
*swizzle
, GLint len
)
2432 for (i
= 0; i
< 4; i
++)
2435 for (i
= 0; i
< len
; i
++) {
2436 switch (*(*inst
)++) {
2438 swizzle
[i
] = SWIZZLE_X
;
2441 swizzle
[i
] = SWIZZLE_Y
;
2444 swizzle
[i
] = SWIZZLE_Z
;
2447 swizzle
[i
] = SWIZZLE_W
;
2450 _mesa_problem(NULL
, "bad component in parse_swizzle_mask()");
2456 swizzle
[1] = swizzle
[2] = swizzle
[3] = swizzle
[0];
2461 * Parse an extended swizzle mask which is a sequence of
2462 * four x/y/z/w/0/1 tokens.
2463 * \return swizzle four swizzle values
2464 * \return negateMask four element bitfield
2467 parse_extended_swizzle_mask(const GLubyte
**inst
, GLubyte swizzle
[4],
2468 GLubyte
*negateMask
)
2473 for (i
= 0; i
< 4; i
++) {
2475 if (parse_sign(inst
) == -1)
2476 *negateMask
|= (1 << i
);
2482 swizzle
[i
] = SWIZZLE_ZERO
;
2485 swizzle
[i
] = SWIZZLE_ONE
;
2488 swizzle
[i
] = SWIZZLE_X
;
2491 swizzle
[i
] = SWIZZLE_Y
;
2494 swizzle
[i
] = SWIZZLE_Z
;
2497 swizzle
[i
] = SWIZZLE_W
;
2500 _mesa_problem(NULL
, "bad case in parse_extended_swizzle_mask()");
2508 parse_src_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2509 struct var_cache
**vc_head
,
2510 struct arb_program
*Program
,
2511 gl_register_file
* File
, GLint
* Index
, GLuint
*swizzle
,
2512 GLboolean
*IsRelOffset
)
2514 struct var_cache
*src
;
2515 GLuint binding
, is_generic
, found
;
2520 *swizzle
= SWIZZLE_XYZW
; /* default */
2522 /* And the binding for the src */
2523 switch (*(*inst
)++) {
2524 case REGISTER_ATTRIB
:
2525 if (parse_attrib_binding
2526 (ctx
, inst
, Program
, &binding
, &is_generic
))
2528 *File
= PROGRAM_INPUT
;
2531 /* We need to insert a dummy variable into the var_cache so we can
2532 * catch generic vertex attrib aliasing errors
2534 var_cache_create(&src
);
2535 src
->type
= vt_attrib
;
2536 src
->name
= (const GLubyte
*) "Dummy Attrib Variable";
2537 src
->attrib_binding
= binding
;
2538 src
->attrib_is_generic
= is_generic
;
2539 var_cache_append(vc_head
, src
);
2540 if (generic_attrib_check(*vc_head
)) {
2541 program_error(ctx
, Program
->Position
,
2542 "Cannot use both a generic vertex attribute "
2543 "and a specific attribute of the same type");
2548 case REGISTER_PARAM
:
2550 case PARAM_ARRAY_ELEMENT
:
2552 src
= parse_string (inst
, vc_head
, Program
, &found
);
2553 Program
->Position
= parse_position (inst
);
2556 program_error2(ctx
, Program
->Position
,
2557 "Undefined variable",
2558 (char *) src
->name
);
2562 *File
= (gl_register_file
) src
->param_binding_type
;
2564 switch (*(*inst
)++) {
2565 case ARRAY_INDEX_ABSOLUTE
:
2566 offset
= parse_integer (inst
, Program
);
2569 || (offset
>= (int)src
->param_binding_length
)) {
2570 program_error(ctx
, Program
->Position
,
2571 "Index out of range");
2572 /* offset, src->name */
2576 *Index
= src
->param_binding_begin
+ offset
;
2577 *swizzle
= src
->swizzle
;
2580 case ARRAY_INDEX_RELATIVE
:
2582 GLint addr_reg_idx
, rel_off
;
2584 /* First, grab the address regiseter */
2585 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &addr_reg_idx
))
2594 /* Then the relative offset */
2595 if (parse_relative_offset(ctx
, inst
, Program
, &rel_off
)) return 1;
2597 /* And store it properly */
2598 *Index
= src
->param_binding_begin
+ rel_off
;
2600 *swizzle
= src
->swizzle
;
2607 if (parse_param_use (ctx
, inst
, vc_head
, Program
, &src
))
2610 *File
= (gl_register_file
) src
->param_binding_type
;
2611 *Index
= src
->param_binding_begin
;
2612 *swizzle
= src
->swizzle
;
2617 case REGISTER_ESTABLISHED_NAME
:
2618 src
= parse_string (inst
, vc_head
, Program
, &found
);
2619 Program
->Position
= parse_position (inst
);
2621 /* If the name has never been added to our symbol table, we're hosed */
2623 program_error(ctx
, Program
->Position
,
2624 "3: Undefined variable"); /* src->name */
2628 switch (src
->type
) {
2630 *File
= PROGRAM_INPUT
;
2631 *Index
= src
->attrib_binding
;
2634 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2636 *File
= (gl_register_file
) src
->param_binding_type
;
2637 *Index
= src
->param_binding_begin
;
2641 *File
= PROGRAM_TEMPORARY
;
2642 *Index
= src
->temp_binding
;
2645 /* If the var type is vt_output no go */
2647 program_error(ctx
, Program
->Position
,
2648 "destination register is read only");
2655 program_error(ctx
, Program
->Position
,
2656 "Unknown token in parse_src_reg");
2660 if (*File
== PROGRAM_STATE_VAR
) {
2661 gl_register_file file
;
2663 /* If we're referencing the Program->Parameters[] array, check if the
2664 * parameter is really a constant/literal. If so, set File to CONSTANT.
2666 assert(*Index
< (GLint
) Program
->Base
.Parameters
->NumParameters
);
2667 file
= Program
->Base
.Parameters
->Parameters
[*Index
].Type
;
2668 if (file
== PROGRAM_CONSTANT
)
2669 *File
= PROGRAM_CONSTANT
;
2672 /* Add attributes to InputsRead only if they are used the program.
2673 * This avoids the handling of unused ATTRIB declarations in the drivers. */
2674 if (*File
== PROGRAM_INPUT
)
2675 Program
->Base
.InputsRead
|= (1 << *Index
);
2682 swizzle_swizzle(GLuint baseSwizzle
, const GLubyte swizzle
[4])
2684 GLuint i
, swz
, s
[4];
2685 for (i
= 0; i
< 4; i
++) {
2686 GLuint c
= swizzle
[i
];
2688 s
[i
] = GET_SWZ(baseSwizzle
, c
);
2692 swz
= MAKE_SWIZZLE4(s
[0], s
[1], s
[2], s
[3]);
2697 * Parse vertex/fragment program vector source register.
2700 parse_vector_src_reg(GLcontext
*ctx
, const GLubyte
**inst
,
2701 struct var_cache
**vc_head
,
2702 struct arb_program
*program
,
2703 struct prog_src_register
*reg
)
2705 gl_register_file file
;
2709 GLboolean isRelOffset
;
2713 negateMask
= (parse_sign (inst
) == -1) ? NEGATE_XYZW
: NEGATE_NONE
;
2715 /* And the src reg */
2716 if (parse_src_reg(ctx
, inst
, vc_head
, program
, &file
, &index
, &baseSwizzle
,
2720 /* finally, the swizzle */
2721 parse_swizzle_mask(inst
, swizzle
, 4);
2725 reg
->Swizzle
= swizzle_swizzle(baseSwizzle
, swizzle
);
2726 reg
->Negate
= negateMask
;
2727 reg
->RelAddr
= isRelOffset
;
2733 * Parse vertex/fragment program scalar source register.
2736 parse_scalar_src_reg(GLcontext
*ctx
, const GLubyte
**inst
,
2737 struct var_cache
**vc_head
,
2738 struct arb_program
*program
,
2739 struct prog_src_register
*reg
)
2741 gl_register_file file
;
2745 GLboolean isRelOffset
;
2749 negateMask
= (parse_sign (inst
) == -1) ? NEGATE_XYZW
: NEGATE_NONE
;
2751 /* And the src reg */
2752 if (parse_src_reg(ctx
, inst
, vc_head
, program
, &file
, &index
, &baseSwizzle
,
2756 /* finally, the swizzle */
2757 parse_swizzle_mask(inst
, swizzle
, 1);
2761 reg
->Swizzle
= swizzle_swizzle(baseSwizzle
, swizzle
);
2762 reg
->Negate
= negateMask
;
2763 reg
->RelAddr
= isRelOffset
;
2769 * Parse vertex/fragment program destination register.
2770 * \return 1 if error, 0 if no error.
2773 parse_dst_reg(GLcontext
* ctx
, const GLubyte
** inst
,
2774 struct var_cache
**vc_head
, struct arb_program
*program
,
2775 struct prog_dst_register
*reg
)
2779 gl_register_file file
;
2781 if (parse_masked_dst_reg (ctx
, inst
, vc_head
, program
, &file
, &idx
, &mask
))
2786 reg
->WriteMask
= mask
;
2792 * This is a big mother that handles getting opcodes into the instruction
2793 * and handling the src & dst registers for fragment program instructions
2794 * \return 1 if error, 0 if no error
2797 parse_fp_instruction (GLcontext
* ctx
, const GLubyte
** inst
,
2798 struct var_cache
**vc_head
, struct arb_program
*Program
,
2799 struct prog_instruction
*fp
)
2803 GLubyte instClass
, type
, code
;
2805 GLuint shadow_tex
= 0;
2807 _mesa_init_instructions(fp
, 1);
2809 /* OP_ALU_INST or OP_TEX_INST */
2810 instClass
= *(*inst
)++;
2812 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2813 * OP_TEX_{SAMPLE, KIL}
2817 /* The actual opcode name */
2820 /* Increment the correct count */
2821 switch (instClass
) {
2823 Program
->NumAluInstructions
++;
2826 Program
->NumTexInstructions
++;
2834 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2836 fp
->Opcode
= OPCODE_ABS
;
2840 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2842 fp
->Opcode
= OPCODE_FLR
;
2846 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2848 fp
->Opcode
= OPCODE_FRC
;
2852 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2854 fp
->Opcode
= OPCODE_LIT
;
2858 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2860 fp
->Opcode
= OPCODE_MOV
;
2864 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2867 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2874 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2876 fp
->Opcode
= OPCODE_COS
;
2880 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2882 fp
->Opcode
= OPCODE_EX2
;
2886 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2888 fp
->Opcode
= OPCODE_LG2
;
2892 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2894 fp
->Opcode
= OPCODE_RCP
;
2898 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2900 fp
->Opcode
= OPCODE_RSQ
;
2904 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2906 fp
->Opcode
= OPCODE_SIN
;
2910 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2913 fp
->Opcode
= OPCODE_SCS
;
2917 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2920 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2927 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2929 fp
->Opcode
= OPCODE_POW
;
2933 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2936 for (a
= 0; a
< 2; a
++) {
2937 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2946 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2948 fp
->Opcode
= OPCODE_ADD
;
2952 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2954 fp
->Opcode
= OPCODE_DP3
;
2958 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2960 fp
->Opcode
= OPCODE_DP4
;
2964 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2966 fp
->Opcode
= OPCODE_DPH
;
2970 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2972 fp
->Opcode
= OPCODE_DST
;
2976 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2978 fp
->Opcode
= OPCODE_MAX
;
2982 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2984 fp
->Opcode
= OPCODE_MIN
;
2988 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2990 fp
->Opcode
= OPCODE_MUL
;
2994 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2996 fp
->Opcode
= OPCODE_SGE
;
3000 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3002 fp
->Opcode
= OPCODE_SLT
;
3006 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3008 fp
->Opcode
= OPCODE_SUB
;
3012 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3014 fp
->Opcode
= OPCODE_XPD
;
3018 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3020 for (a
= 0; a
< 2; a
++) {
3021 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
3029 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3031 fp
->Opcode
= OPCODE_CMP
;
3035 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3037 fp
->Opcode
= OPCODE_LRP
;
3041 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3043 fp
->Opcode
= OPCODE_MAD
;
3047 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3050 for (a
= 0; a
< 3; a
++) {
3051 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
3059 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3061 fp
->Opcode
= OPCODE_SWZ
;
3064 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3070 gl_register_file file
;
3074 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
,
3075 &baseSwizzle
, &rel
))
3077 parse_extended_swizzle_mask(inst
, swizzle
, &negateMask
);
3078 fp
->SrcReg
[0].File
= file
;
3079 fp
->SrcReg
[0].Index
= index
;
3080 fp
->SrcReg
[0].Negate
= negateMask
;
3081 fp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
3091 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3093 fp
->Opcode
= OPCODE_TEX
;
3097 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3099 fp
->Opcode
= OPCODE_TXP
;
3103 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
3105 fp
->Opcode
= OPCODE_TXB
;
3109 if (parse_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3112 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
3116 if (parse_teximage_num (ctx
, inst
, Program
, &texcoord
))
3118 fp
->TexSrcUnit
= texcoord
;
3121 switch (*(*inst
)++) {
3122 case TEXTARGET_SHADOW1D
:
3123 shadow_tex
= 1 << texcoord
;
3126 fp
->TexSrcTarget
= TEXTURE_1D_INDEX
;
3128 case TEXTARGET_SHADOW2D
:
3129 shadow_tex
= 1 << texcoord
;
3132 fp
->TexSrcTarget
= TEXTURE_2D_INDEX
;
3135 fp
->TexSrcTarget
= TEXTURE_3D_INDEX
;
3137 case TEXTARGET_SHADOWRECT
:
3138 shadow_tex
= 1 << texcoord
;
3140 case TEXTARGET_RECT
:
3141 fp
->TexSrcTarget
= TEXTURE_RECT_INDEX
;
3143 case TEXTARGET_CUBE
:
3144 fp
->TexSrcTarget
= TEXTURE_CUBE_INDEX
;
3146 case TEXTARGET_SHADOW1D_ARRAY
:
3147 shadow_tex
= 1 << texcoord
;
3149 case TEXTARGET_1D_ARRAY
:
3150 fp
->TexSrcTarget
= TEXTURE_1D_ARRAY_INDEX
;
3152 case TEXTARGET_SHADOW2D_ARRAY
:
3153 shadow_tex
= 1 << texcoord
;
3155 case TEXTARGET_2D_ARRAY
:
3156 fp
->TexSrcTarget
= TEXTURE_2D_ARRAY_INDEX
;
3163 /* Don't test the first time a particular sampler is seen. Each time
3164 * after that, make sure the shadow state is the same.
3166 if ((_mesa_bitcount(Program
->TexturesUsed
[texcoord
]) > 0)
3167 && ((Program
->ShadowSamplers
& (1 << texcoord
)) != shadow_tex
)) {
3168 program_error(ctx
, Program
->Position
,
3169 "texture image unit used for shadow sampling and non-shadow sampling");
3173 Program
->TexturesUsed
[texcoord
] |= (1 << fp
->TexSrcTarget
);
3174 /* Check that both "2D" and "CUBE" (for example) aren't both used */
3175 if (_mesa_bitcount(Program
->TexturesUsed
[texcoord
]) > 1) {
3176 program_error(ctx
, Program
->Position
,
3177 "multiple targets used on one texture image unit");
3182 Program
->ShadowSamplers
|= shadow_tex
;
3186 Program
->UsesKill
= 1;
3187 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
3189 fp
->Opcode
= OPCODE_KIL
;
3192 _mesa_problem(ctx
, "bad type 0x%x in parse_fp_instruction()", type
);
3201 * Handle the parsing out of a masked address register
3203 * \param Index - The register index we write to
3204 * \param WriteMask - The mask controlling which components we write (1->write)
3206 * \return 0 on sucess, 1 on error
3209 parse_vp_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
3210 struct var_cache
**vc_head
,
3211 struct arb_program
*Program
,
3212 struct prog_dst_register
*reg
)
3216 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &idx
))
3219 /* This should be 0x8 */
3222 reg
->File
= PROGRAM_ADDRESS
;
3225 /* Writemask of .x is implied */
3226 reg
->WriteMask
= 0x1;
3232 * This is a big mother that handles getting opcodes into the instruction
3233 * and handling the src & dst registers for vertex program instructions
3236 parse_vp_instruction (GLcontext
* ctx
, const GLubyte
** inst
,
3237 struct var_cache
**vc_head
, struct arb_program
*Program
,
3238 struct prog_instruction
*vp
)
3243 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3246 /* The actual opcode name */
3249 _mesa_init_instructions(vp
, 1);
3254 vp
->Opcode
= OPCODE_ARL
;
3256 /* Remember to set SrcReg.RelAddr; */
3258 /* Get the masked address register [dst] */
3259 if (parse_vp_address_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3262 vp
->DstReg
.File
= PROGRAM_ADDRESS
;
3264 /* Get a scalar src register */
3265 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3273 vp
->Opcode
= OPCODE_ABS
;
3276 vp
->Opcode
= OPCODE_FLR
;
3279 vp
->Opcode
= OPCODE_FRC
;
3282 vp
->Opcode
= OPCODE_LIT
;
3285 vp
->Opcode
= OPCODE_MOV
;
3289 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3292 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3299 vp
->Opcode
= OPCODE_EX2
;
3302 vp
->Opcode
= OPCODE_EXP
;
3305 vp
->Opcode
= OPCODE_LG2
;
3308 vp
->Opcode
= OPCODE_LOG
;
3311 vp
->Opcode
= OPCODE_RCP
;
3314 vp
->Opcode
= OPCODE_RSQ
;
3317 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3320 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3327 vp
->Opcode
= OPCODE_POW
;
3330 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3333 for (a
= 0; a
< 2; a
++) {
3334 if (parse_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3342 vp
->Opcode
= OPCODE_ADD
;
3345 vp
->Opcode
= OPCODE_DP3
;
3348 vp
->Opcode
= OPCODE_DP4
;
3351 vp
->Opcode
= OPCODE_DPH
;
3354 vp
->Opcode
= OPCODE_DST
;
3357 vp
->Opcode
= OPCODE_MAX
;
3360 vp
->Opcode
= OPCODE_MIN
;
3363 vp
->Opcode
= OPCODE_MUL
;
3366 vp
->Opcode
= OPCODE_SGE
;
3369 vp
->Opcode
= OPCODE_SLT
;
3372 vp
->Opcode
= OPCODE_SUB
;
3375 vp
->Opcode
= OPCODE_XPD
;
3378 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3381 for (a
= 0; a
< 2; a
++) {
3382 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3390 vp
->Opcode
= OPCODE_MAD
;
3394 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3397 for (a
= 0; a
< 3; a
++) {
3398 if (parse_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3406 vp
->Opcode
= OPCODE_SWZ
;
3413 gl_register_file file
;
3417 if (parse_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3420 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
,
3421 &baseSwizzle
, &relAddr
))
3423 parse_extended_swizzle_mask (inst
, swizzle
, &negateMask
);
3424 vp
->SrcReg
[0].File
= file
;
3425 vp
->SrcReg
[0].Index
= index
;
3426 vp
->SrcReg
[0].Negate
= negateMask
;
3427 vp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
3431 vp
->SrcReg
[0].RelAddr
= relAddr
;
3441 debug_variables (GLcontext
* ctx
, struct var_cache
*vc_head
,
3442 struct arb_program
*Program
)
3444 struct var_cache
*vc
;
3447 fprintf (stderr
, "debug_variables, vc_head: %p\n", (void*) vc_head
);
3449 /* First of all, print out the contents of the var_cache */
3452 fprintf (stderr
, "[%p]\n", (void*) vc
);
3455 fprintf (stderr
, "UNDEFINED %s\n", vc
->name
);
3458 fprintf (stderr
, "ATTRIB %s\n", vc
->name
);
3459 fprintf (stderr
, " binding: 0x%x\n", vc
->attrib_binding
);
3462 fprintf (stderr
, "PARAM %s begin: %d len: %d\n", vc
->name
,
3463 vc
->param_binding_begin
, vc
->param_binding_length
);
3464 b
= vc
->param_binding_begin
;
3465 for (a
= 0; a
< vc
->param_binding_length
; a
++) {
3466 fprintf (stderr
, "%s\n",
3467 Program
->Base
.Parameters
->Parameters
[a
+ b
].Name
);
3468 if (Program
->Base
.Parameters
->Parameters
[a
+ b
].Type
== PROGRAM_STATE_VAR
) {
3470 s
= _mesa_program_state_string(Program
->Base
.Parameters
->Parameters
3471 [a
+ b
].StateIndexes
);
3472 fprintf(stderr
, "%s\n", s
);
3476 fprintf (stderr
, "%f %f %f %f\n",
3477 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][0],
3478 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][1],
3479 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][2],
3480 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][3]);
3484 fprintf (stderr
, "TEMP %s\n", vc
->name
);
3485 fprintf (stderr
, " binding: 0x%x\n", vc
->temp_binding
);
3488 fprintf (stderr
, "OUTPUT %s\n", vc
->name
);
3489 fprintf (stderr
, " binding: 0x%x\n", vc
->output_binding
);
3492 fprintf (stderr
, "ALIAS %s\n", vc
->name
);
3493 fprintf (stderr
, " binding: 0x%p (%s)\n",
3494 (void*) vc
->alias_binding
, vc
->alias_binding
->name
);
3504 #endif /* DEBUG_PARSING */
3508 * The main loop for parsing a fragment or vertex program
3510 * \return 1 on error, 0 on success
3513 parse_instructions(GLcontext
* ctx
, const GLubyte
* inst
,
3514 struct var_cache
**vc_head
, struct arb_program
*Program
)
3516 const GLuint maxInst
= (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
3517 ? ctx
->Const
.FragmentProgram
.MaxInstructions
3518 : ctx
->Const
.VertexProgram
.MaxInstructions
;
3521 ASSERT(MAX_PROGRAM_INSTRUCTIONS
>= maxInst
);
3523 Program
->MajorVersion
= (GLuint
) * inst
++;
3524 Program
->MinorVersion
= (GLuint
) * inst
++;
3526 while (*inst
!= END
) {
3531 case ARB_PRECISION_HINT_FASTEST
:
3532 Program
->PrecisionOption
= GL_FASTEST
;
3535 case ARB_PRECISION_HINT_NICEST
:
3536 Program
->PrecisionOption
= GL_NICEST
;
3540 Program
->FogOption
= GL_EXP
;
3544 Program
->FogOption
= GL_EXP2
;
3547 case ARB_FOG_LINEAR
:
3548 Program
->FogOption
= GL_LINEAR
;
3551 case ARB_POSITION_INVARIANT
:
3552 if (Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
3553 Program
->HintPositionInvariant
= GL_TRUE
;
3556 case ARB_FRAGMENT_PROGRAM_SHADOW
:
3557 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3558 /* TODO ARB_fragment_program_shadow code */
3562 case ARB_DRAW_BUFFERS
:
3563 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3564 /* do nothing for now */
3568 case MESA_TEXTURE_ARRAY
:
3569 /* do nothing for now */
3576 if (Program
->Base
.NumInstructions
+ 1 >= maxInst
) {
3577 program_error(ctx
, Program
->Position
,
3578 "Max instruction count exceeded");
3581 Program
->Position
= parse_position (&inst
);
3582 /* parse the current instruction */
3583 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3584 err
= parse_fp_instruction (ctx
, &inst
, vc_head
, Program
,
3585 &Program
->Base
.Instructions
[Program
->Base
.NumInstructions
]);
3588 err
= parse_vp_instruction (ctx
, &inst
, vc_head
, Program
,
3589 &Program
->Base
.Instructions
[Program
->Base
.NumInstructions
]);
3592 /* increment instuction count */
3593 Program
->Base
.NumInstructions
++;
3597 err
= parse_declaration (ctx
, &inst
, vc_head
, Program
);
3608 /* Finally, tag on an OPCODE_END instruction */
3610 const GLuint numInst
= Program
->Base
.NumInstructions
;
3611 _mesa_init_instructions(Program
->Base
.Instructions
+ numInst
, 1);
3612 Program
->Base
.Instructions
[numInst
].Opcode
= OPCODE_END
;
3614 Program
->Base
.NumInstructions
++;
3617 * Initialize native counts to logical counts. The device driver may
3618 * change them if program is translated into a hardware program.
3620 Program
->Base
.NumNativeInstructions
= Program
->Base
.NumInstructions
;
3621 Program
->Base
.NumNativeTemporaries
= Program
->Base
.NumTemporaries
;
3622 Program
->Base
.NumNativeParameters
= Program
->Base
.NumParameters
;
3623 Program
->Base
.NumNativeAttributes
= Program
->Base
.NumAttributes
;
3624 Program
->Base
.NumNativeAddressRegs
= Program
->Base
.NumAddressRegs
;
3631 LONGSTRING
static char core_grammar_text
[] =
3632 #include "shader/grammar/grammar_syn.h"
3637 * Set a grammar parameter.
3638 * \param name the grammar parameter
3639 * \param value the new parameter value
3640 * \return 0 if OK, 1 if error
3643 set_reg8 (GLcontext
*ctx
, grammar id
, const char *name
, GLubyte value
)
3645 char error_msg
[300];
3648 if (grammar_set_reg8 (id
, (const byte
*) name
, value
))
3651 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3652 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3653 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Grammar Register Error");
3659 * Enable support for the given language option in the parser.
3660 * \return 1 if OK, 0 if error
3663 enable_ext(GLcontext
*ctx
, grammar id
, const char *name
)
3665 return !set_reg8(ctx
, id
, name
, 1);
3670 * Enable parser extensions based on which OpenGL extensions are supported
3671 * by this rendering context.
3673 * \return GL_TRUE if OK, GL_FALSE if error.
3676 enable_parser_extensions(GLcontext
*ctx
, grammar id
)
3679 /* These are not supported at this time */
3680 if ((ctx
->Extensions
.ARB_vertex_blend
||
3681 ctx
->Extensions
.EXT_vertex_weighting
)
3682 && !enable_ext(ctx
, id
, "vertex_blend"))
3684 if (ctx
->Extensions
.ARB_matrix_palette
3685 && !enable_ext(ctx
, id
, "matrix_palette"))
3688 if (ctx
->Extensions
.ARB_fragment_program_shadow
3689 && !enable_ext(ctx
, id
, "fragment_program_shadow"))
3691 if (ctx
->Extensions
.EXT_point_parameters
3692 && !enable_ext(ctx
, id
, "point_parameters"))
3694 if (ctx
->Extensions
.EXT_secondary_color
3695 && !enable_ext(ctx
, id
, "secondary_color"))
3697 if (ctx
->Extensions
.EXT_fog_coord
3698 && !enable_ext(ctx
, id
, "fog_coord"))
3700 if (ctx
->Extensions
.NV_texture_rectangle
3701 && !enable_ext(ctx
, id
, "texture_rectangle"))
3703 if (!enable_ext(ctx
, id
, "draw_buffers"))
3705 if (ctx
->Extensions
.MESA_texture_array
3706 && !enable_ext(ctx
, id
, "texture_array"))
3709 /* hack for Warcraft (see bug 8060) */
3710 enable_ext(ctx
, id
, "vertex_blend");
3718 * This kicks everything off.
3720 * \param ctx - The GL Context
3721 * \param str - The program string
3722 * \param len - The program string length
3723 * \param program - The arb_program struct to return all the parsed info in
3724 * \return GL_TRUE on sucess, GL_FALSE on error
3727 _mesa_parse_arb_program(GLcontext
*ctx
, GLenum target
,
3728 const GLubyte
*str
, GLsizei len
,
3729 struct arb_program
*program
)
3731 GLint a
, err
, error_pos
;
3732 char error_msg
[300];
3734 struct var_cache
*vc_head
;
3735 grammar arbprogram_syn_id
;
3736 GLubyte
*parsed
, *inst
;
3737 GLubyte
*strz
= NULL
;
3738 static int arbprogram_syn_is_ok
= 0; /* XXX temporary */
3740 /* set the program target before parsing */
3741 program
->Base
.Target
= target
;
3743 /* Reset error state */
3744 _mesa_set_program_error(ctx
, -1, NULL
);
3746 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
3747 if (!arbprogram_syn_is_ok
) {
3748 /* One-time initialization of parsing system */
3749 grammar grammar_syn_id
;
3752 grammar_syn_id
= grammar_load_from_text ((byte
*) core_grammar_text
);
3753 if (grammar_syn_id
== 0) {
3754 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3755 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3756 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3757 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3758 "glProgramStringARB(Error loading grammar rule set)");
3762 err
= !grammar_check(grammar_syn_id
, (byte
*) arb_grammar_text
,
3763 &parsed
, &parsed_len
);
3765 /* 'parsed' is unused here */
3766 _mesa_free (parsed
);
3769 /* NOTE: we can't destroy grammar_syn_id right here because
3770 * grammar_destroy() can reset the last error
3773 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3774 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3775 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3776 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3777 "glProgramString(Error loading grammar rule set");
3778 grammar_destroy (grammar_syn_id
);
3782 grammar_destroy (grammar_syn_id
);
3784 arbprogram_syn_is_ok
= 1;
3787 /* create the grammar object */
3788 arbprogram_syn_id
= grammar_load_from_text ((byte
*) arb_grammar_text
);
3789 if (arbprogram_syn_id
== 0) {
3790 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3791 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
3792 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3793 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3794 "glProgramString(Error loading grammer rule set)");
3798 /* Set program_target register value */
3799 if (set_reg8 (ctx
, arbprogram_syn_id
, "program_target",
3800 program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
? 0x10 : 0x20)) {
3801 grammar_destroy (arbprogram_syn_id
);
3805 if (!enable_parser_extensions(ctx
, arbprogram_syn_id
)) {
3806 grammar_destroy(arbprogram_syn_id
);
3810 /* check for NULL character occurences */
3813 for (i
= 0; i
< len
; i
++) {
3814 if (str
[i
] == '\0') {
3815 program_error(ctx
, i
, "illegal character");
3816 grammar_destroy (arbprogram_syn_id
);
3822 /* copy the program string to a null-terminated string */
3823 strz
= (GLubyte
*) _mesa_malloc (len
+ 1);
3825 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glProgramStringARB");
3826 grammar_destroy (arbprogram_syn_id
);
3829 _mesa_memcpy (strz
, str
, len
);
3832 /* do a fast check on program string - initial production buffer is 4K */
3833 err
= !grammar_fast_check(arbprogram_syn_id
, strz
,
3834 &parsed
, &parsed_len
, 0x1000);
3836 /* Syntax parse error */
3838 grammar_get_last_error((GLubyte
*) error_msg
, 300, &error_pos
);
3839 program_error(ctx
, error_pos
, error_msg
);
3842 /* useful for debugging */
3846 fprintf(stderr
, "program: %s\n", (char *) strz
);
3847 fprintf(stderr
, "Error Pos: %d\n", ctx
->Program
.ErrorPos
);
3848 s
= (char *) _mesa_find_line_column(strz
, strz
+ctx
->Program
.ErrorPos
,
3850 fprintf(stderr
, "line %d col %d: %s\n", line
, col
, s
);
3857 grammar_destroy (arbprogram_syn_id
);
3861 grammar_destroy (arbprogram_syn_id
);
3864 * Program string is syntactically correct at this point
3865 * Parse the tokenized version of the program now, generating
3866 * vertex/fragment program instructions.
3869 /* Initialize the arb_program struct */
3870 program
->Base
.String
= strz
;
3871 program
->Base
.Instructions
= _mesa_alloc_instructions(MAX_PROGRAM_INSTRUCTIONS
);
3872 program
->Base
.NumInstructions
=
3873 program
->Base
.NumTemporaries
=
3874 program
->Base
.NumParameters
=
3875 program
->Base
.NumAttributes
= program
->Base
.NumAddressRegs
= 0;
3876 program
->Base
.Parameters
= _mesa_new_parameter_list ();
3877 program
->Base
.InputsRead
= 0x0;
3878 program
->Base
.OutputsWritten
= 0x0;
3879 program
->Position
= 0;
3880 program
->MajorVersion
= program
->MinorVersion
= 0;
3881 program
->PrecisionOption
= GL_DONT_CARE
;
3882 program
->FogOption
= GL_NONE
;
3883 program
->HintPositionInvariant
= GL_FALSE
;
3884 for (a
= 0; a
< MAX_TEXTURE_IMAGE_UNITS
; a
++)
3885 program
->TexturesUsed
[a
] = 0x0;
3886 program
->ShadowSamplers
= 0x0;
3887 program
->NumAluInstructions
=
3888 program
->NumTexInstructions
=
3889 program
->NumTexIndirections
= 0;
3890 program
->UsesKill
= 0;
3895 /* Start examining the tokens in the array */
3898 /* Check the grammer rev */
3899 if (*inst
++ != REVISION
) {
3900 program_error (ctx
, 0, "Grammar version mismatch");
3904 /* ignore program target */
3906 err
= parse_instructions(ctx
, inst
, &vc_head
, program
);
3909 /*debug_variables(ctx, vc_head, program); */
3911 /* We're done with the parsed binary array */
3912 var_cache_destroy (&vc_head
);
3914 _mesa_free (parsed
);
3916 /* Reallocate the instruction array from size [MAX_PROGRAM_INSTRUCTIONS]
3917 * to size [ap.Base.NumInstructions].
3919 program
->Base
.Instructions
3920 = _mesa_realloc_instructions(program
->Base
.Instructions
,
3921 MAX_PROGRAM_INSTRUCTIONS
,
3922 program
->Base
.NumInstructions
);
3930 _mesa_parse_arb_fragment_program(GLcontext
* ctx
, GLenum target
,
3931 const GLvoid
*str
, GLsizei len
,
3932 struct gl_fragment_program
*program
)
3934 struct arb_program ap
;
3937 ASSERT(target
== GL_FRAGMENT_PROGRAM_ARB
);
3938 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
, &ap
)) {
3939 /* Error in the program. Just return. */
3943 /* Copy the relevant contents of the arb_program struct into the
3944 * fragment_program struct.
3946 program
->Base
.String
= ap
.Base
.String
;
3947 program
->Base
.NumInstructions
= ap
.Base
.NumInstructions
;
3948 program
->Base
.NumTemporaries
= ap
.Base
.NumTemporaries
;
3949 program
->Base
.NumParameters
= ap
.Base
.NumParameters
;
3950 program
->Base
.NumAttributes
= ap
.Base
.NumAttributes
;
3951 program
->Base
.NumAddressRegs
= ap
.Base
.NumAddressRegs
;
3952 program
->Base
.NumNativeInstructions
= ap
.Base
.NumNativeInstructions
;
3953 program
->Base
.NumNativeTemporaries
= ap
.Base
.NumNativeTemporaries
;
3954 program
->Base
.NumNativeParameters
= ap
.Base
.NumNativeParameters
;
3955 program
->Base
.NumNativeAttributes
= ap
.Base
.NumNativeAttributes
;
3956 program
->Base
.NumNativeAddressRegs
= ap
.Base
.NumNativeAddressRegs
;
3957 program
->Base
.NumAluInstructions
= ap
.Base
.NumAluInstructions
;
3958 program
->Base
.NumTexInstructions
= ap
.Base
.NumTexInstructions
;
3959 program
->Base
.NumTexIndirections
= ap
.Base
.NumTexIndirections
;
3960 program
->Base
.NumNativeAluInstructions
= ap
.Base
.NumAluInstructions
;
3961 program
->Base
.NumNativeTexInstructions
= ap
.Base
.NumTexInstructions
;
3962 program
->Base
.NumNativeTexIndirections
= ap
.Base
.NumTexIndirections
;
3963 program
->Base
.InputsRead
= ap
.Base
.InputsRead
;
3964 program
->Base
.OutputsWritten
= ap
.Base
.OutputsWritten
;
3965 for (i
= 0; i
< MAX_TEXTURE_IMAGE_UNITS
; i
++) {
3966 program
->Base
.TexturesUsed
[i
] = ap
.TexturesUsed
[i
];
3967 if (ap
.TexturesUsed
[i
])
3968 program
->Base
.SamplersUsed
|= (1 << i
);
3970 program
->Base
.ShadowSamplers
= ap
.ShadowSamplers
;
3971 program
->FogOption
= ap
.FogOption
;
3972 program
->UsesKill
= ap
.UsesKill
;
3974 if (program
->FogOption
)
3975 program
->Base
.InputsRead
|= FRAG_BIT_FOGC
;
3977 if (program
->Base
.Instructions
)
3978 _mesa_free(program
->Base
.Instructions
);
3979 program
->Base
.Instructions
= ap
.Base
.Instructions
;
3981 if (program
->Base
.Parameters
)
3982 _mesa_free_parameter_list(program
->Base
.Parameters
);
3983 program
->Base
.Parameters
= ap
.Base
.Parameters
;
3985 /* Append fog instructions now if the program has "OPTION ARB_fog_exp"
3986 * or similar. We used to leave this up to drivers, but it appears
3987 * there's no hardware that wants to do fog in a discrete stage separate
3988 * from the fragment shader.
3990 if (program
->FogOption
!= GL_NONE
) {
3991 _mesa_append_fog_code(ctx
, program
);
3992 program
->FogOption
= GL_NONE
;
3996 _mesa_printf("____________Fragment program %u ________\n", program
->Base
.Id
);
3997 _mesa_print_program(&program
->Base
);
4004 * Parse the vertex program string. If success, update the given
4005 * vertex_program object with the new program. Else, leave the vertex_program
4009 _mesa_parse_arb_vertex_program(GLcontext
*ctx
, GLenum target
,
4010 const GLvoid
*str
, GLsizei len
,
4011 struct gl_vertex_program
*program
)
4013 struct arb_program ap
;
4015 ASSERT(target
== GL_VERTEX_PROGRAM_ARB
);
4017 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
, &ap
)) {
4018 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glProgramString(bad program)");
4022 /* Copy the relevant contents of the arb_program struct into the
4023 * vertex_program struct.
4025 program
->Base
.String
= ap
.Base
.String
;
4026 program
->Base
.NumInstructions
= ap
.Base
.NumInstructions
;
4027 program
->Base
.NumTemporaries
= ap
.Base
.NumTemporaries
;
4028 program
->Base
.NumParameters
= ap
.Base
.NumParameters
;
4029 program
->Base
.NumAttributes
= ap
.Base
.NumAttributes
;
4030 program
->Base
.NumAddressRegs
= ap
.Base
.NumAddressRegs
;
4031 program
->Base
.NumNativeInstructions
= ap
.Base
.NumNativeInstructions
;
4032 program
->Base
.NumNativeTemporaries
= ap
.Base
.NumNativeTemporaries
;
4033 program
->Base
.NumNativeParameters
= ap
.Base
.NumNativeParameters
;
4034 program
->Base
.NumNativeAttributes
= ap
.Base
.NumNativeAttributes
;
4035 program
->Base
.NumNativeAddressRegs
= ap
.Base
.NumNativeAddressRegs
;
4036 program
->Base
.InputsRead
= ap
.Base
.InputsRead
;
4037 program
->Base
.OutputsWritten
= ap
.Base
.OutputsWritten
;
4038 program
->IsPositionInvariant
= ap
.HintPositionInvariant
;
4040 if (program
->Base
.Instructions
)
4041 _mesa_free(program
->Base
.Instructions
);
4042 program
->Base
.Instructions
= ap
.Base
.Instructions
;
4044 if (program
->Base
.Parameters
)
4045 _mesa_free_parameter_list(program
->Base
.Parameters
);
4046 program
->Base
.Parameters
= ap
.Base
.Parameters
;
4049 _mesa_printf("____________Vertex program %u __________\n", program
->Base
.Id
);
4050 _mesa_print_program(&program
->Base
);