2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #define DEBUG_PARSING 0
28 * \file arbprogparse.c
29 * ARB_*_program parser core
33 #include "main/glheader.h"
34 #include "main/imports.h"
35 #include "shader/grammar/grammar_mesa.h"
36 #include "arbprogparse.h"
38 #include "prog_parameter.h"
39 #include "prog_statevars.h"
43 #include "prog_instruction.h"
46 /* For ARB programs, use the NV instruction limits */
47 #define MAX_INSTRUCTIONS MAX2(MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS, \
48 MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS)
52 * This is basically a union of the vertex_program and fragment_program
53 * structs that we can use to parse the program into
55 * XXX we can probably get rid of this entirely someday.
59 struct gl_program Base
;
61 GLuint Position
; /* Just used for error reporting while parsing */
65 /* ARB_vertex_progmra options */
66 GLboolean HintPositionInvariant
;
68 /* ARB_fragment_progmra options */
69 GLenum PrecisionOption
; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
70 GLenum FogOption
; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
72 /* ARB_fragment_program specifics */
73 GLbitfield TexturesUsed
[MAX_TEXTURE_IMAGE_UNITS
];
74 GLbitfield ShadowSamplers
;
75 GLuint NumAluInstructions
;
76 GLuint NumTexInstructions
;
77 GLuint NumTexIndirections
;
85 * Fragment Program Stuff:
86 * -----------------------------------------------------
88 * - things from Michal's email
90 * + not-overflowing floats (don't use parse_integer..)
91 * + can remove range checking in arbparse.c
93 * - check all limits of number of various variables
98 * Vertex Program Stuff:
99 * -----------------------------------------------------
100 * - Optimize param array usage and count limits correctly, see spec,
102 * + Record if an array is reference absolutly or relatively (or both)
103 * + For absolute arrays, store a bitmap of accesses
104 * + For single parameters, store an access flag
105 * + After parsing, make a parameter cleanup and merging pass, where
106 * relative arrays are layed out first, followed by abs arrays, and
107 * finally single state.
108 * + Remap offsets for param src and dst registers
109 * + Now we can properly count parameter usage
111 * - Multiple state binding errors in param arrays (see spec, just before
116 * -----------------------------------------------------
117 * - User clipping planes vs. PositionInvariant
118 * - Is it sufficient to just multiply by the mvp to transform in the
119 * PositionInvariant case? Or do we need something more involved?
121 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
122 * - fetch state listed in program_parameters list
123 * + WTF should this go???
124 * + currently in nvvertexec.c and s_nvfragprog.c
126 * - allow for multiple address registers (and fetch address regs properly)
129 * -----------------------------------------------------
130 * - remove any leftover unused grammer.c stuff (dict_ ?)
131 * - fix grammer.c error handling so its not static
132 * - #ifdef around stuff pertaining to extentions
134 * Outstanding Questions:
135 * -----------------------------------------------------
136 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
137 * what gets hacked off because of this:
138 * + VERTEX_ATTRIB_MATRIXINDEX
139 * + VERTEX_ATTRIB_WEIGHT
143 * - When can we fetch env/local params from their own register files, and
144 * when to we have to fetch them into the main state register file?
148 * -----------------------------------------------------
151 /* Changes since moving the file to shader directory
153 2004-III-4 ------------------------------------------------------------
154 - added #include "grammar_mesa.h"
155 - removed grammar specific code part (it resides now in grammar.c)
156 - added GL_ARB_fragment_program_shadow tokens
157 - modified #include "arbparse_syn.h"
158 - major changes inside _mesa_parse_arb_program()
159 - check the program string for '\0' characters
160 - copy the program string to a one-byte-longer location to have
162 - position invariance test (not writing to result.position) moved
166 typedef GLubyte
*production
;
170 * This is the text describing the rules to parse the grammar
172 LONGSTRING
static char arb_grammar_text
[] =
173 #include "arbprogram_syn.h"
177 * These should match up with the values defined in arbprogram.syn
182 - changed and merged V_* and F_* opcode values to OP_*.
183 - added GL_ARB_fragment_program_shadow specific tokens (michal)
185 #define REVISION 0x0a
188 #define FRAGMENT_PROGRAM 0x01
189 #define VERTEX_PROGRAM 0x02
191 /* program section */
193 #define INSTRUCTION 0x02
194 #define DECLARATION 0x03
197 /* GL_ARB_fragment_program option */
198 #define ARB_PRECISION_HINT_FASTEST 0x00
199 #define ARB_PRECISION_HINT_NICEST 0x01
200 #define ARB_FOG_EXP 0x02
201 #define ARB_FOG_EXP2 0x03
202 #define ARB_FOG_LINEAR 0x04
204 /* GL_ARB_vertex_program option */
205 #define ARB_POSITION_INVARIANT 0x05
207 /* GL_ARB_fragment_program_shadow option */
208 #define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
210 /* GL_ARB_draw_buffers option */
211 #define ARB_DRAW_BUFFERS 0x07
213 /* GL_MESA_texture_array option */
214 #define MESA_TEXTURE_ARRAY 0x08
216 /* GL_ARB_fragment_program instruction class */
217 #define OP_ALU_INST 0x00
218 #define OP_TEX_INST 0x01
220 /* GL_ARB_vertex_program instruction class */
223 /* GL_ARB_fragment_program instruction type */
224 #define OP_ALU_VECTOR 0x00
225 #define OP_ALU_SCALAR 0x01
226 #define OP_ALU_BINSC 0x02
227 #define OP_ALU_BIN 0x03
228 #define OP_ALU_TRI 0x04
229 #define OP_ALU_SWZ 0x05
230 #define OP_TEX_SAMPLE 0x06
231 #define OP_TEX_KIL 0x07
233 /* GL_ARB_vertex_program instruction type */
234 #define OP_ALU_ARL 0x08
242 /* GL_ARB_fragment_program instruction code */
244 #define OP_ABS_SAT 0x1B
246 #define OP_FLR_SAT 0x26
248 #define OP_FRC_SAT 0x27
250 #define OP_LIT_SAT 0x2A
252 #define OP_MOV_SAT 0x30
254 #define OP_COS_SAT 0x20
256 #define OP_EX2_SAT 0x25
258 #define OP_LG2_SAT 0x29
260 #define OP_RCP_SAT 0x33
262 #define OP_RSQ_SAT 0x34
264 #define OP_SIN_SAT 0x39
266 #define OP_SCS_SAT 0x36
268 #define OP_POW_SAT 0x32
270 #define OP_ADD_SAT 0x1C
272 #define OP_DP3_SAT 0x21
274 #define OP_DP4_SAT 0x22
276 #define OP_DPH_SAT 0x23
278 #define OP_DST_SAT 0x24
280 #define OP_MAX_SAT 0x2E
282 #define OP_MIN_SAT 0x2F
284 #define OP_MUL_SAT 0x31
286 #define OP_SGE_SAT 0x37
288 #define OP_SLT_SAT 0x3A
290 #define OP_SUB_SAT 0x3B
292 #define OP_XPD_SAT 0x43
294 #define OP_CMP_SAT 0x1E
296 #define OP_LRP_SAT 0x2C
298 #define OP_MAD_SAT 0x2D
300 #define OP_SWZ_SAT 0x3C
302 #define OP_TEX_SAT 0x3E
304 #define OP_TXB_SAT 0x40
306 #define OP_TXP_SAT 0x42
309 /* GL_ARB_vertex_program instruction code */
338 /* fragment attribute binding */
339 #define FRAGMENT_ATTRIB_COLOR 0x01
340 #define FRAGMENT_ATTRIB_TEXCOORD 0x02
341 #define FRAGMENT_ATTRIB_FOGCOORD 0x03
342 #define FRAGMENT_ATTRIB_POSITION 0x04
344 /* vertex attribute binding */
345 #define VERTEX_ATTRIB_POSITION 0x01
346 #define VERTEX_ATTRIB_WEIGHT 0x02
347 #define VERTEX_ATTRIB_NORMAL 0x03
348 #define VERTEX_ATTRIB_COLOR 0x04
349 #define VERTEX_ATTRIB_FOGCOORD 0x05
350 #define VERTEX_ATTRIB_TEXCOORD 0x06
351 #define VERTEX_ATTRIB_MATRIXINDEX 0x07
352 #define VERTEX_ATTRIB_GENERIC 0x08
354 /* fragment result binding */
355 #define FRAGMENT_RESULT_COLOR 0x01
356 #define FRAGMENT_RESULT_DEPTH 0x02
358 /* vertex result binding */
359 #define VERTEX_RESULT_POSITION 0x01
360 #define VERTEX_RESULT_COLOR 0x02
361 #define VERTEX_RESULT_FOGCOORD 0x03
362 #define VERTEX_RESULT_POINTSIZE 0x04
363 #define VERTEX_RESULT_TEXCOORD 0x05
366 #define TEXTARGET_1D 0x01
367 #define TEXTARGET_2D 0x02
368 #define TEXTARGET_3D 0x03
369 #define TEXTARGET_RECT 0x04
370 #define TEXTARGET_CUBE 0x05
371 /* GL_ARB_fragment_program_shadow */
372 #define TEXTARGET_SHADOW1D 0x06
373 #define TEXTARGET_SHADOW2D 0x07
374 #define TEXTARGET_SHADOWRECT 0x08
375 /* GL_MESA_texture_array */
376 #define TEXTARGET_1D_ARRAY 0x09
377 #define TEXTARGET_2D_ARRAY 0x0a
378 #define TEXTARGET_SHADOW1D_ARRAY 0x0b
379 #define TEXTARGET_SHADOW2D_ARRAY 0x0c
382 #define FACE_FRONT 0x00
383 #define FACE_BACK 0x01
386 #define COLOR_PRIMARY 0x00
387 #define COLOR_SECONDARY 0x01
390 #define COMPONENT_X 0x00
391 #define COMPONENT_Y 0x01
392 #define COMPONENT_Z 0x02
393 #define COMPONENT_W 0x03
394 #define COMPONENT_0 0x04
395 #define COMPONENT_1 0x05
397 /* array index type */
398 #define ARRAY_INDEX_ABSOLUTE 0x00
399 #define ARRAY_INDEX_RELATIVE 0x01
402 #define MATRIX_MODELVIEW 0x01
403 #define MATRIX_PROJECTION 0x02
404 #define MATRIX_MVP 0x03
405 #define MATRIX_TEXTURE 0x04
406 #define MATRIX_PALETTE 0x05
407 #define MATRIX_PROGRAM 0x06
409 /* matrix modifier */
410 #define MATRIX_MODIFIER_IDENTITY 0x00
411 #define MATRIX_MODIFIER_INVERSE 0x01
412 #define MATRIX_MODIFIER_TRANSPOSE 0x02
413 #define MATRIX_MODIFIER_INVTRANS 0x03
416 #define CONSTANT_SCALAR 0x01
417 #define CONSTANT_VECTOR 0x02
419 /* program param type */
420 #define PROGRAM_PARAM_ENV 0x01
421 #define PROGRAM_PARAM_LOCAL 0x02
424 #define REGISTER_ATTRIB 0x01
425 #define REGISTER_PARAM 0x02
426 #define REGISTER_RESULT 0x03
427 #define REGISTER_ESTABLISHED_NAME 0x04
430 #define PARAM_NULL 0x00
431 #define PARAM_ARRAY_ELEMENT 0x01
432 #define PARAM_STATE_ELEMENT 0x02
433 #define PARAM_PROGRAM_ELEMENT 0x03
434 #define PARAM_PROGRAM_ELEMENTS 0x04
435 #define PARAM_CONSTANT 0x05
437 /* param state property */
438 #define STATE_MATERIAL_PARSER 0x01
439 #define STATE_LIGHT_PARSER 0x02
440 #define STATE_LIGHT_MODEL 0x03
441 #define STATE_LIGHT_PROD 0x04
442 #define STATE_FOG 0x05
443 #define STATE_MATRIX_ROWS 0x06
444 /* GL_ARB_fragment_program */
445 #define STATE_TEX_ENV 0x07
446 #define STATE_DEPTH 0x08
447 /* GL_ARB_vertex_program */
448 #define STATE_TEX_GEN 0x09
449 #define STATE_CLIP_PLANE 0x0A
450 #define STATE_POINT 0x0B
452 /* state material property */
453 #define MATERIAL_AMBIENT 0x01
454 #define MATERIAL_DIFFUSE 0x02
455 #define MATERIAL_SPECULAR 0x03
456 #define MATERIAL_EMISSION 0x04
457 #define MATERIAL_SHININESS 0x05
459 /* state light property */
460 #define LIGHT_AMBIENT 0x01
461 #define LIGHT_DIFFUSE 0x02
462 #define LIGHT_SPECULAR 0x03
463 #define LIGHT_POSITION 0x04
464 #define LIGHT_ATTENUATION 0x05
465 #define LIGHT_HALF 0x06
466 #define LIGHT_SPOT_DIRECTION 0x07
468 /* state light model property */
469 #define LIGHT_MODEL_AMBIENT 0x01
470 #define LIGHT_MODEL_SCENECOLOR 0x02
472 /* state light product property */
473 #define LIGHT_PROD_AMBIENT 0x01
474 #define LIGHT_PROD_DIFFUSE 0x02
475 #define LIGHT_PROD_SPECULAR 0x03
477 /* state texture environment property */
478 #define TEX_ENV_COLOR 0x01
480 /* state texture generation coord property */
481 #define TEX_GEN_EYE 0x01
482 #define TEX_GEN_OBJECT 0x02
484 /* state fog property */
485 #define FOG_COLOR 0x01
486 #define FOG_PARAMS 0x02
488 /* state depth property */
489 #define DEPTH_RANGE 0x01
491 /* state point parameters property */
492 #define POINT_SIZE 0x01
493 #define POINT_ATTENUATION 0x02
501 /* GL_ARB_vertex_program */
504 /*-----------------------------------------------------------------------
505 * From here on down is the semantic checking portion
510 * Variable Table Handling functions
525 * Setting an explicit field for each of the binding properties is a bit
526 * wasteful of space, but it should be much more clear when reading later on..
530 const GLubyte
*name
; /* don't free() - no need */
532 GLuint address_binding
; /* The index of the address register we should
534 GLuint attrib_binding
; /* For type vt_attrib, see nvfragprog.h for values */
535 GLuint attrib_is_generic
; /* If the attrib was specified through a generic
537 GLuint temp_binding
; /* The index of the temp register we are to use */
538 GLuint output_binding
; /* Output/result register number */
539 struct var_cache
*alias_binding
; /* For type vt_alias, points to the var_cache entry
540 * that this is aliased to */
541 GLuint param_binding_type
; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
542 * PROGRAM_ENV_PARAM} */
543 GLuint param_binding_begin
; /* This is the offset into the program_parameter_list where
544 * the tokens representing our bound state (or constants)
546 GLuint param_binding_length
; /* This is how many entries in the the program_parameter_list
547 * we take up with our state tokens or constants. Note that
548 * this is _not_ the same as the number of param registers
549 * we eventually use */
550 struct var_cache
*next
;
554 var_cache_create (struct var_cache
**va
)
556 *va
= (struct var_cache
*) _mesa_malloc (sizeof (struct var_cache
));
559 (**va
).type
= vt_none
;
560 (**va
).attrib_binding
= ~0;
561 (**va
).attrib_is_generic
= 0;
562 (**va
).temp_binding
= ~0;
563 (**va
).output_binding
= ~0;
564 (**va
).param_binding_type
= ~0;
565 (**va
).param_binding_begin
= ~0;
566 (**va
).param_binding_length
= ~0;
567 (**va
).alias_binding
= NULL
;
573 var_cache_destroy (struct var_cache
**va
)
576 var_cache_destroy (&(**va
).next
);
583 var_cache_append (struct var_cache
**va
, struct var_cache
*nv
)
586 var_cache_append (&(**va
).next
, nv
);
591 static struct var_cache
*
592 var_cache_find (struct var_cache
*va
, const GLubyte
* name
)
594 /*struct var_cache *first = va;*/
597 if (!_mesa_strcmp ( (const char*) name
, (const char*) va
->name
)) {
598 if (va
->type
== vt_alias
)
599 return va
->alias_binding
;
612 * Called when an error is detected while parsing/compiling a program.
613 * Sets the ctx->Program.ErrorString field to descript and records a
614 * GL_INVALID_OPERATION error.
615 * \param position position of error in program string
616 * \param descrip verbose error description
619 program_error(GLcontext
*ctx
, GLint position
, const char *descrip
)
622 const char *prefix
= "glProgramString(", *suffix
= ")";
623 char *str
= (char *) _mesa_malloc(_mesa_strlen(descrip
) +
624 _mesa_strlen(prefix
) +
625 _mesa_strlen(suffix
) + 1);
627 _mesa_sprintf(str
, "%s%s%s", prefix
, descrip
, suffix
);
628 _mesa_error(ctx
, GL_INVALID_OPERATION
, str
);
632 _mesa_set_program_error(ctx
, position
, descrip
);
638 * constructs an integer from 4 GLubytes in LE format
641 parse_position (const GLubyte
** inst
)
645 value
= (GLuint
) (*(*inst
)++);
646 value
+= (GLuint
) (*(*inst
)++) * 0x100;
647 value
+= (GLuint
) (*(*inst
)++) * 0x10000;
648 value
+= (GLuint
) (*(*inst
)++) * 0x1000000;
654 * This will, given a string, lookup the string as a variable name in the
655 * var cache. If the name is found, the var cache node corresponding to the
656 * var name is returned. If it is not found, a new entry is allocated
658 * \param I Points into the binary array where the string identifier begins
659 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
660 * \return The location on the var_cache corresponding the the string starting at I
662 static struct var_cache
*
663 parse_string (const GLubyte
** inst
, struct var_cache
**vc_head
,
664 struct arb_program
*Program
, GLuint
* found
)
666 const GLubyte
*i
= *inst
;
667 struct var_cache
*va
= NULL
;
670 *inst
+= _mesa_strlen ((char *) i
) + 1;
672 va
= var_cache_find (*vc_head
, i
);
680 var_cache_create (&va
);
681 va
->name
= (const GLubyte
*) i
;
683 var_cache_append (vc_head
, va
);
689 parse_string_without_adding (const GLubyte
** inst
, struct arb_program
*Program
)
691 const GLubyte
*i
= *inst
;
694 *inst
+= _mesa_strlen ((char *) i
) + 1;
700 * \return -1 if we parse '-', return 1 otherwise
703 parse_sign (const GLubyte
** inst
)
705 /*return *(*inst)++ != '+'; */
711 else if (**inst
== '+') {
720 * parses and returns signed integer
723 parse_integer (const GLubyte
** inst
, struct arb_program
*Program
)
728 /* check if *inst points to '+' or '-'
729 * if yes, grab the sign and increment *inst
731 sign
= parse_sign (inst
);
733 /* now check if *inst points to 0
734 * if yes, increment the *inst and return the default value
741 /* parse the integer as you normally would do it */
742 value
= _mesa_atoi (parse_string_without_adding (inst
, Program
));
744 /* now, after terminating 0 there is a position
745 * to parse it - parse_position()
747 Program
->Position
= parse_position (inst
);
753 Accumulate this string of digits, and return them as
754 a large integer represented in floating point (for range).
755 If scale is not NULL, also accumulates a power-of-ten
756 integer scale factor that represents the number of digits
760 parse_float_string(const GLubyte
** inst
, struct arb_program
*Program
, GLdouble
*scale
)
762 GLdouble value
= 0.0;
763 GLdouble oscale
= 1.0;
765 if (**inst
== 0) { /* this string of digits is empty-- do nothing */
768 else { /* nonempty string-- parse out the digits */
769 while (**inst
>= '0' && **inst
<= '9') {
770 GLubyte digit
= *((*inst
)++);
771 value
= value
* 10.0 + (GLint
) (digit
- '0');
774 assert(**inst
== 0); /* integer string should end with 0 */
775 (*inst
)++; /* skip over terminating 0 */
776 Program
->Position
= parse_position(inst
); /* skip position (from integer) */
784 Parse an unsigned floating-point number from this stream of tokenized
785 characters. Example floating-point formats supported:
793 parse_float (const GLubyte
** inst
, struct arb_program
*Program
)
796 GLdouble whole
, fraction
, fracScale
= 1.0;
798 whole
= parse_float_string(inst
, Program
, 0);
799 fraction
= parse_float_string(inst
, Program
, &fracScale
);
801 /* Parse signed exponent */
802 exponent
= parse_integer(inst
, Program
); /* This is the exponent */
804 /* Assemble parts of floating-point number: */
805 return (GLfloat
) ((whole
+ fraction
/ fracScale
) *
806 _mesa_pow(10.0, (GLfloat
) exponent
));
813 parse_signed_float (const GLubyte
** inst
, struct arb_program
*Program
)
815 GLint sign
= parse_sign (inst
);
816 GLfloat value
= parse_float (inst
, Program
);
821 * This picks out a constant value from the parsed array. The constant vector is r
822 * returned in the *values array, which should be of length 4.
824 * \param values - The 4 component vector with the constant value in it
827 parse_constant (const GLubyte
** inst
, GLfloat
*values
, struct arb_program
*Program
,
830 GLuint components
, i
;
833 switch (*(*inst
)++) {
834 case CONSTANT_SCALAR
:
835 if (use
== GL_TRUE
) {
838 values
[2] = values
[3] = parse_float (inst
, Program
);
843 values
[2] = values
[3] = parse_signed_float (inst
, Program
);
847 case CONSTANT_VECTOR
:
848 values
[0] = values
[1] = values
[2] = 0;
850 components
= *(*inst
)++;
851 for (i
= 0; i
< components
; i
++) {
852 values
[i
] = parse_signed_float (inst
, Program
);
859 * \param offset The offset from the address register that we should
862 * \return 0 on sucess, 1 on error
865 parse_relative_offset(GLcontext
*ctx
, const GLubyte
**inst
,
866 struct arb_program
*Program
, GLint
*offset
)
869 *offset
= parse_integer(inst
, Program
);
874 * \param color 0 if color type is primary, 1 if color type is secondary
875 * \return 0 on sucess, 1 on error
878 parse_color_type (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
881 (void) ctx
; (void) Program
;
882 *color
= *(*inst
)++ != COLOR_PRIMARY
;
887 * Get an integer corresponding to a generic vertex attribute.
889 * \return 0 on sucess, 1 on error
892 parse_generic_attrib_num(GLcontext
*ctx
, const GLubyte
** inst
,
893 struct arb_program
*Program
, GLuint
*attrib
)
895 GLint i
= parse_integer(inst
, Program
);
897 if ((i
< 0) || (i
>= MAX_VERTEX_PROGRAM_ATTRIBS
))
899 program_error(ctx
, Program
->Position
,
900 "Invalid generic vertex attribute index");
904 *attrib
= (GLuint
) i
;
911 * \param color The index of the color buffer to write into
912 * \return 0 on sucess, 1 on error
915 parse_output_color_num (GLcontext
* ctx
, const GLubyte
** inst
,
916 struct arb_program
*Program
, GLuint
* color
)
918 GLint i
= parse_integer (inst
, Program
);
920 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxDrawBuffers
)) {
921 program_error(ctx
, Program
->Position
, "Invalid draw buffer index");
931 * \param coord The texture unit index
932 * \return 0 on sucess, 1 on error
935 parse_texcoord_num (GLcontext
* ctx
, const GLubyte
** inst
,
936 struct arb_program
*Program
, GLuint
* coord
)
938 GLint i
= parse_integer (inst
, Program
);
940 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxTextureUnits
)) {
941 program_error(ctx
, Program
->Position
, "Invalid texture unit index");
950 * \param coord The weight index
951 * \return 0 on sucess, 1 on error
954 parse_weight_num (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
957 *coord
= parse_integer (inst
, Program
);
959 if ((*coord
< 0) || (*coord
>= 1)) {
960 program_error(ctx
, Program
->Position
, "Invalid weight index");
968 * \param coord The clip plane index
969 * \return 0 on sucess, 1 on error
972 parse_clipplane_num (GLcontext
* ctx
, const GLubyte
** inst
,
973 struct arb_program
*Program
, GLint
* coord
)
975 *coord
= parse_integer (inst
, Program
);
977 if ((*coord
< 0) || (*coord
>= (GLint
) ctx
->Const
.MaxClipPlanes
)) {
978 program_error(ctx
, Program
->Position
, "Invalid clip plane index");
987 * \return 0 on front face, 1 on back face
990 parse_face_type (const GLubyte
** inst
)
992 switch (*(*inst
)++) {
1004 * Given a matrix and a modifier token on the binary array, return tokens
1005 * that _mesa_fetch_state() [program.c] can understand.
1007 * \param matrix - the matrix we are talking about
1008 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
1009 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
1010 * \return 0 on sucess, 1 on failure
1013 parse_matrix (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
1014 GLint
* matrix
, GLint
* matrix_idx
, GLint
* matrix_modifier
)
1016 GLubyte mat
= *(*inst
)++;
1021 case MATRIX_MODELVIEW
:
1022 *matrix
= STATE_MODELVIEW_MATRIX
;
1023 *matrix_idx
= parse_integer (inst
, Program
);
1024 if (*matrix_idx
> 0) {
1025 program_error(ctx
, Program
->Position
,
1026 "ARB_vertex_blend not supported");
1031 case MATRIX_PROJECTION
:
1032 *matrix
= STATE_PROJECTION_MATRIX
;
1036 *matrix
= STATE_MVP_MATRIX
;
1039 case MATRIX_TEXTURE
:
1040 *matrix
= STATE_TEXTURE_MATRIX
;
1041 *matrix_idx
= parse_integer (inst
, Program
);
1042 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxTextureUnits
) {
1043 program_error(ctx
, Program
->Position
, "Invalid Texture Unit");
1044 /* bad *matrix_id */
1049 /* This is not currently supported (ARB_matrix_palette) */
1050 case MATRIX_PALETTE
:
1051 *matrix_idx
= parse_integer (inst
, Program
);
1052 program_error(ctx
, Program
->Position
,
1053 "ARB_matrix_palette not supported");
1057 case MATRIX_PROGRAM
:
1058 *matrix
= STATE_PROGRAM_MATRIX
;
1059 *matrix_idx
= parse_integer (inst
, Program
);
1060 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxProgramMatrices
) {
1061 program_error(ctx
, Program
->Position
, "Invalid Program Matrix");
1062 /* bad *matrix_idx */
1068 switch (*(*inst
)++) {
1069 case MATRIX_MODIFIER_IDENTITY
:
1070 *matrix_modifier
= 0;
1072 case MATRIX_MODIFIER_INVERSE
:
1073 *matrix_modifier
= STATE_MATRIX_INVERSE
;
1075 case MATRIX_MODIFIER_TRANSPOSE
:
1076 *matrix_modifier
= STATE_MATRIX_TRANSPOSE
;
1078 case MATRIX_MODIFIER_INVTRANS
:
1079 *matrix_modifier
= STATE_MATRIX_INVTRANS
;
1088 * This parses a state string (rather, the binary version of it) into
1089 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1091 * \param inst - the start in the binary arry to start working from
1092 * \param state_tokens - the storage for the 6-token state description
1093 * \return - 0 on sucess, 1 on error
1096 parse_state_single_item (GLcontext
* ctx
, const GLubyte
** inst
,
1097 struct arb_program
*Program
,
1098 gl_state_index state_tokens
[STATE_LENGTH
])
1100 switch (*(*inst
)++) {
1101 case STATE_MATERIAL_PARSER
:
1102 state_tokens
[0] = STATE_MATERIAL
;
1103 state_tokens
[1] = parse_face_type (inst
);
1104 switch (*(*inst
)++) {
1105 case MATERIAL_AMBIENT
:
1106 state_tokens
[2] = STATE_AMBIENT
;
1108 case MATERIAL_DIFFUSE
:
1109 state_tokens
[2] = STATE_DIFFUSE
;
1111 case MATERIAL_SPECULAR
:
1112 state_tokens
[2] = STATE_SPECULAR
;
1114 case MATERIAL_EMISSION
:
1115 state_tokens
[2] = STATE_EMISSION
;
1117 case MATERIAL_SHININESS
:
1118 state_tokens
[2] = STATE_SHININESS
;
1123 case STATE_LIGHT_PARSER
:
1124 state_tokens
[0] = STATE_LIGHT
;
1125 state_tokens
[1] = parse_integer (inst
, Program
);
1127 /* Check the value of state_tokens[1] against the # of lights */
1128 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1129 program_error(ctx
, Program
->Position
, "Invalid Light Number");
1130 /* bad state_tokens[1] */
1134 switch (*(*inst
)++) {
1136 state_tokens
[2] = STATE_AMBIENT
;
1139 state_tokens
[2] = STATE_DIFFUSE
;
1141 case LIGHT_SPECULAR
:
1142 state_tokens
[2] = STATE_SPECULAR
;
1144 case LIGHT_POSITION
:
1145 state_tokens
[2] = STATE_POSITION
;
1147 case LIGHT_ATTENUATION
:
1148 state_tokens
[2] = STATE_ATTENUATION
;
1151 state_tokens
[2] = STATE_HALF_VECTOR
;
1153 case LIGHT_SPOT_DIRECTION
:
1154 state_tokens
[2] = STATE_SPOT_DIRECTION
;
1159 case STATE_LIGHT_MODEL
:
1160 switch (*(*inst
)++) {
1161 case LIGHT_MODEL_AMBIENT
:
1162 state_tokens
[0] = STATE_LIGHTMODEL_AMBIENT
;
1164 case LIGHT_MODEL_SCENECOLOR
:
1165 state_tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
1166 state_tokens
[1] = parse_face_type (inst
);
1171 case STATE_LIGHT_PROD
:
1172 state_tokens
[0] = STATE_LIGHTPROD
;
1173 state_tokens
[1] = parse_integer (inst
, Program
);
1175 /* Check the value of state_tokens[1] against the # of lights */
1176 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1177 program_error(ctx
, Program
->Position
, "Invalid Light Number");
1178 /* bad state_tokens[1] */
1182 state_tokens
[2] = parse_face_type (inst
);
1183 switch (*(*inst
)++) {
1184 case LIGHT_PROD_AMBIENT
:
1185 state_tokens
[3] = STATE_AMBIENT
;
1187 case LIGHT_PROD_DIFFUSE
:
1188 state_tokens
[3] = STATE_DIFFUSE
;
1190 case LIGHT_PROD_SPECULAR
:
1191 state_tokens
[3] = STATE_SPECULAR
;
1198 switch (*(*inst
)++) {
1200 state_tokens
[0] = STATE_FOG_COLOR
;
1203 state_tokens
[0] = STATE_FOG_PARAMS
;
1209 state_tokens
[1] = parse_integer (inst
, Program
);
1210 switch (*(*inst
)++) {
1212 state_tokens
[0] = STATE_TEXENV_COLOR
;
1221 state_tokens
[0] = STATE_TEXGEN
;
1222 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1224 if (parse_texcoord_num (ctx
, inst
, Program
, &coord
))
1226 state_tokens
[1] = coord
;
1231 /* 0 - s, 1 - t, 2 - r, 3 - q */
1234 if (type
== TEX_GEN_EYE
) {
1237 state_tokens
[2] = STATE_TEXGEN_EYE_S
;
1240 state_tokens
[2] = STATE_TEXGEN_EYE_T
;
1243 state_tokens
[2] = STATE_TEXGEN_EYE_R
;
1246 state_tokens
[2] = STATE_TEXGEN_EYE_Q
;
1253 state_tokens
[2] = STATE_TEXGEN_OBJECT_S
;
1256 state_tokens
[2] = STATE_TEXGEN_OBJECT_T
;
1259 state_tokens
[2] = STATE_TEXGEN_OBJECT_R
;
1262 state_tokens
[2] = STATE_TEXGEN_OBJECT_Q
;
1270 switch (*(*inst
)++) {
1272 state_tokens
[0] = STATE_DEPTH_RANGE
;
1277 case STATE_CLIP_PLANE
:
1278 state_tokens
[0] = STATE_CLIPPLANE
;
1279 state_tokens
[1] = parse_integer (inst
, Program
);
1280 if (parse_clipplane_num (ctx
, inst
, Program
,
1281 (GLint
*) &state_tokens
[1]))
1286 switch (*(*inst
++)) {
1288 state_tokens
[0] = STATE_POINT_SIZE
;
1291 case POINT_ATTENUATION
:
1292 state_tokens
[0] = STATE_POINT_ATTENUATION
;
1297 /* XXX: I think this is the correct format for a matrix row */
1298 case STATE_MATRIX_ROWS
:
1299 if (parse_matrix(ctx
, inst
, Program
,
1300 (GLint
*) &state_tokens
[0],
1301 (GLint
*) &state_tokens
[1],
1302 (GLint
*) &state_tokens
[4]))
1305 state_tokens
[2] = parse_integer (inst
, Program
); /* The first row to grab */
1307 if ((**inst
) != 0) { /* Either the last row, 0 */
1308 state_tokens
[3] = parse_integer (inst
, Program
);
1309 if (state_tokens
[3] < state_tokens
[2]) {
1310 program_error(ctx
, Program
->Position
,
1311 "Second matrix index less than the first");
1312 /* state_tokens[4] vs. state_tokens[3] */
1317 state_tokens
[3] = state_tokens
[2];
1327 * This parses a state string (rather, the binary version of it) into
1328 * a 6-token similar for the state fetching code in program.c
1330 * One might ask, why fetch these parameters into just like you fetch
1331 * state when they are already stored in other places?
1333 * Because of array offsets -> We can stick env/local parameters in the
1334 * middle of a parameter array and then index someplace into the array
1337 * One optimization might be to only do this for the cases where the
1338 * env/local parameters end up inside of an array, and leave the
1339 * single parameters (or arrays of pure env/local pareameters) in their
1340 * respective register files.
1342 * For ENV parameters, the format is:
1343 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1344 * state_tokens[1] = STATE_ENV
1345 * state_tokens[2] = the parameter index
1347 * for LOCAL parameters, the format is:
1348 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1349 * state_tokens[1] = STATE_LOCAL
1350 * state_tokens[2] = the parameter index
1352 * \param inst - the start in the binary arry to start working from
1353 * \param state_tokens - the storage for the 6-token state description
1354 * \return - 0 on sucess, 1 on failure
1357 parse_program_single_item (GLcontext
* ctx
, const GLubyte
** inst
,
1358 struct arb_program
*Program
,
1359 gl_state_index state_tokens
[STATE_LENGTH
])
1361 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1362 state_tokens
[0] = STATE_FRAGMENT_PROGRAM
;
1364 state_tokens
[0] = STATE_VERTEX_PROGRAM
;
1367 switch (*(*inst
)++) {
1368 case PROGRAM_PARAM_ENV
:
1369 state_tokens
[1] = STATE_ENV
;
1370 state_tokens
[2] = parse_integer (inst
, Program
);
1372 /* Check state_tokens[2] against the number of ENV parameters available */
1373 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1374 (state_tokens
[2] >= (GLint
) ctx
->Const
.FragmentProgram
.MaxEnvParams
))
1376 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1377 (state_tokens
[2] >= (GLint
) ctx
->Const
.VertexProgram
.MaxEnvParams
))) {
1378 program_error(ctx
, Program
->Position
,
1379 "Invalid Program Env Parameter");
1380 /* bad state_tokens[2] */
1386 case PROGRAM_PARAM_LOCAL
:
1387 state_tokens
[1] = STATE_LOCAL
;
1388 state_tokens
[2] = parse_integer (inst
, Program
);
1390 /* Check state_tokens[2] against the number of LOCAL parameters available */
1391 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1392 (state_tokens
[2] >= (GLint
) ctx
->Const
.FragmentProgram
.MaxLocalParams
))
1394 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1395 (state_tokens
[2] >= (GLint
) ctx
->Const
.VertexProgram
.MaxLocalParams
))) {
1396 program_error(ctx
, Program
->Position
,
1397 "Invalid Program Local Parameter");
1398 /* bad state_tokens[2] */
1408 * For ARB_vertex_program, programs are not allowed to use both an explicit
1409 * vertex attribute and a generic vertex attribute corresponding to the same
1410 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1412 * This will walk our var_cache and make sure that nobody does anything fishy.
1414 * \return 0 on sucess, 1 on error
1417 generic_attrib_check(struct var_cache
*vc_head
)
1420 struct var_cache
*curr
;
1421 GLboolean explicitAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
],
1422 genericAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
];
1424 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1425 explicitAttrib
[a
] = GL_FALSE
;
1426 genericAttrib
[a
] = GL_FALSE
;
1431 if (curr
->type
== vt_attrib
) {
1432 if (curr
->attrib_is_generic
)
1433 genericAttrib
[ curr
->attrib_binding
] = GL_TRUE
;
1435 explicitAttrib
[ curr
->attrib_binding
] = GL_TRUE
;
1441 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1442 if ((explicitAttrib
[a
]) && (genericAttrib
[a
]))
1450 * This will handle the binding side of an ATTRIB var declaration
1452 * \param inputReg returns the input register index, one of the
1453 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
1454 * \return returns 0 on success, 1 on error
1457 parse_attrib_binding(GLcontext
* ctx
, const GLubyte
** inst
,
1458 struct arb_program
*Program
,
1459 GLuint
*inputReg
, GLuint
*is_generic
)
1465 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1466 switch (*(*inst
)++) {
1467 case FRAGMENT_ATTRIB_COLOR
:
1470 err
= parse_color_type (ctx
, inst
, Program
, &coord
);
1471 *inputReg
= FRAG_ATTRIB_COL0
+ coord
;
1474 case FRAGMENT_ATTRIB_TEXCOORD
:
1476 GLuint texcoord
= 0;
1477 err
= parse_texcoord_num (ctx
, inst
, Program
, &texcoord
);
1478 *inputReg
= FRAG_ATTRIB_TEX0
+ texcoord
;
1481 case FRAGMENT_ATTRIB_FOGCOORD
:
1482 *inputReg
= FRAG_ATTRIB_FOGC
;
1484 case FRAGMENT_ATTRIB_POSITION
:
1485 *inputReg
= FRAG_ATTRIB_WPOS
;
1493 switch (*(*inst
)++) {
1494 case VERTEX_ATTRIB_POSITION
:
1495 *inputReg
= VERT_ATTRIB_POS
;
1498 case VERTEX_ATTRIB_WEIGHT
:
1501 err
= parse_weight_num (ctx
, inst
, Program
, &weight
);
1502 *inputReg
= VERT_ATTRIB_WEIGHT
;
1504 /* hack for Warcraft (see bug 8060) */
1505 _mesa_warning(ctx
, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
1508 program_error(ctx
, Program
->Position
,
1509 "ARB_vertex_blend not supported");
1514 case VERTEX_ATTRIB_NORMAL
:
1515 *inputReg
= VERT_ATTRIB_NORMAL
;
1518 case VERTEX_ATTRIB_COLOR
:
1521 err
= parse_color_type (ctx
, inst
, Program
, &color
);
1523 *inputReg
= VERT_ATTRIB_COLOR1
;
1526 *inputReg
= VERT_ATTRIB_COLOR0
;
1531 case VERTEX_ATTRIB_FOGCOORD
:
1532 *inputReg
= VERT_ATTRIB_FOG
;
1535 case VERTEX_ATTRIB_TEXCOORD
:
1538 err
= parse_texcoord_num (ctx
, inst
, Program
, &unit
);
1539 *inputReg
= VERT_ATTRIB_TEX0
+ unit
;
1543 case VERTEX_ATTRIB_MATRIXINDEX
:
1544 /* Not supported at this time */
1546 const char *msg
= "ARB_palette_matrix not supported";
1547 parse_integer (inst
, Program
);
1548 program_error(ctx
, Program
->Position
, msg
);
1552 case VERTEX_ATTRIB_GENERIC
:
1555 err
= parse_generic_attrib_num(ctx
, inst
, Program
, &attrib
);
1558 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1559 * attributes do not alias the conventional vertex
1563 *inputReg
= attrib
+ VERT_ATTRIB_GENERIC0
;
1577 program_error(ctx
, Program
->Position
, "Bad attribute binding");
1580 Program
->Base
.InputsRead
|= (1 << *inputReg
);
1587 * This translates between a binary token for an output variable type
1588 * and the mesa token for the same thing.
1590 * \param inst The parsed tokens
1591 * \param outputReg Returned index/number of the output register,
1592 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
1595 parse_result_binding(GLcontext
*ctx
, const GLubyte
**inst
,
1596 GLuint
*outputReg
, struct arb_program
*Program
)
1598 const GLubyte token
= *(*inst
)++;
1601 case FRAGMENT_RESULT_COLOR
:
1602 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1605 /* This gets result of the color buffer we're supposed to
1606 * draw into. This pertains to GL_ARB_draw_buffers.
1608 parse_output_color_num(ctx
, inst
, Program
, &out_color
);
1609 ASSERT(out_color
< MAX_DRAW_BUFFERS
);
1610 *outputReg
= FRAG_RESULT_COLR
;
1613 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1614 *outputReg
= VERT_RESULT_HPOS
;
1618 case FRAGMENT_RESULT_DEPTH
:
1619 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1620 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
1621 *outputReg
= FRAG_RESULT_DEPR
;
1624 /* for vtx programs, this is VERTEX_RESULT_COLOR */
1626 GLuint face_type
= parse_face_type(inst
);
1627 GLint err
= parse_color_type(ctx
, inst
, Program
, &color_type
);
1634 *outputReg
= VERT_RESULT_BFC1
; /* secondary color */
1637 *outputReg
= VERT_RESULT_BFC0
; /* primary color */
1643 *outputReg
= VERT_RESULT_COL1
; /* secondary color */
1647 *outputReg
= VERT_RESULT_COL0
; /* primary color */
1653 case VERTEX_RESULT_FOGCOORD
:
1654 *outputReg
= VERT_RESULT_FOGC
;
1657 case VERTEX_RESULT_POINTSIZE
:
1658 *outputReg
= VERT_RESULT_PSIZ
;
1661 case VERTEX_RESULT_TEXCOORD
:
1664 if (parse_texcoord_num (ctx
, inst
, Program
, &unit
))
1666 *outputReg
= VERT_RESULT_TEX0
+ unit
;
1671 Program
->Base
.OutputsWritten
|= (1 << *outputReg
);
1678 * This handles the declaration of ATTRIB variables
1681 * parse_vert_attrib_binding(), or something like that
1683 * \return 0 on sucess, 1 on error
1686 parse_attrib (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1687 struct arb_program
*Program
)
1691 struct var_cache
*attrib_var
;
1693 attrib_var
= parse_string (inst
, vc_head
, Program
, &found
);
1694 Program
->Position
= parse_position (inst
);
1696 error_msg
= (char *)
1697 _mesa_malloc (_mesa_strlen ((char *) attrib_var
->name
) + 40);
1698 _mesa_sprintf (error_msg
, "Duplicate Variable Declaration: %s",
1700 program_error(ctx
, Program
->Position
, error_msg
);
1701 _mesa_free (error_msg
);
1705 attrib_var
->type
= vt_attrib
;
1707 if (parse_attrib_binding(ctx
, inst
, Program
, &attrib_var
->attrib_binding
,
1708 &attrib_var
->attrib_is_generic
))
1711 if (generic_attrib_check(*vc_head
)) {
1712 program_error(ctx
, Program
->Position
,
1713 "Cannot use both a generic vertex attribute "
1714 "and a specific attribute of the same type");
1718 Program
->Base
.NumAttributes
++;
1723 * \param use -- TRUE if we're called when declaring implicit parameters,
1724 * FALSE if we're declaraing variables. This has to do with
1725 * if we get a signed or unsigned float for scalar constants
1728 parse_param_elements (GLcontext
* ctx
, const GLubyte
** inst
,
1729 struct var_cache
*param_var
,
1730 struct arb_program
*Program
, GLboolean use
)
1734 gl_state_index state_tokens
[STATE_LENGTH
];
1735 GLfloat const_values
[4];
1737 switch (*(*inst
)++) {
1738 case PARAM_STATE_ELEMENT
:
1739 if (parse_state_single_item (ctx
, inst
, Program
, state_tokens
))
1742 /* If we adding STATE_MATRIX that has multiple rows, we need to
1743 * unroll it and call _mesa_add_state_reference() for each row
1745 if ((state_tokens
[0] == STATE_MODELVIEW_MATRIX
||
1746 state_tokens
[0] == STATE_PROJECTION_MATRIX
||
1747 state_tokens
[0] == STATE_MVP_MATRIX
||
1748 state_tokens
[0] == STATE_TEXTURE_MATRIX
||
1749 state_tokens
[0] == STATE_PROGRAM_MATRIX
)
1750 && (state_tokens
[2] != state_tokens
[3])) {
1752 const GLint first_row
= state_tokens
[2];
1753 const GLint last_row
= state_tokens
[3];
1755 for (row
= first_row
; row
<= last_row
; row
++) {
1756 state_tokens
[2] = state_tokens
[3] = row
;
1758 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1760 if (param_var
->param_binding_begin
== ~0U)
1761 param_var
->param_binding_begin
= idx
;
1762 param_var
->param_binding_length
++;
1763 Program
->Base
.NumParameters
++;
1767 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1769 if (param_var
->param_binding_begin
== ~0U)
1770 param_var
->param_binding_begin
= idx
;
1771 param_var
->param_binding_length
++;
1772 Program
->Base
.NumParameters
++;
1776 case PARAM_PROGRAM_ELEMENT
:
1777 if (parse_program_single_item (ctx
, inst
, Program
, state_tokens
))
1779 idx
= _mesa_add_state_reference (Program
->Base
.Parameters
, state_tokens
);
1780 if (param_var
->param_binding_begin
== ~0U)
1781 param_var
->param_binding_begin
= idx
;
1782 param_var
->param_binding_length
++;
1783 Program
->Base
.NumParameters
++;
1785 /* Check if there is more: 0 -> we're done, else its an integer */
1787 GLuint out_of_range
, new_idx
;
1788 GLuint start_idx
= state_tokens
[2] + 1;
1789 GLuint end_idx
= parse_integer (inst
, Program
);
1792 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1793 if (((state_tokens
[1] == STATE_ENV
)
1794 && (end_idx
>= ctx
->Const
.FragmentProgram
.MaxEnvParams
))
1795 || ((state_tokens
[1] == STATE_LOCAL
)
1797 ctx
->Const
.FragmentProgram
.MaxLocalParams
)))
1801 if (((state_tokens
[1] == STATE_ENV
)
1802 && (end_idx
>= ctx
->Const
.VertexProgram
.MaxEnvParams
))
1803 || ((state_tokens
[1] == STATE_LOCAL
)
1805 ctx
->Const
.VertexProgram
.MaxLocalParams
)))
1809 program_error(ctx
, Program
->Position
,
1810 "Invalid Program Parameter"); /*end_idx*/
1814 for (new_idx
= start_idx
; new_idx
<= end_idx
; new_idx
++) {
1815 state_tokens
[2] = new_idx
;
1816 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1818 param_var
->param_binding_length
++;
1819 Program
->Base
.NumParameters
++;
1827 case PARAM_CONSTANT
:
1828 /* parsing something like {1.0, 2.0, 3.0, 4.0} */
1829 parse_constant (inst
, const_values
, Program
, use
);
1830 idx
= _mesa_add_named_constant(Program
->Base
.Parameters
,
1831 (char *) param_var
->name
,
1833 if (param_var
->param_binding_begin
== ~0U)
1834 param_var
->param_binding_begin
= idx
;
1835 param_var
->param_binding_type
= PROGRAM_CONSTANT
;
1836 param_var
->param_binding_length
++;
1837 Program
->Base
.NumParameters
++;
1841 program_error(ctx
, Program
->Position
,
1842 "Unexpected token (in parse_param_elements())");
1846 /* Make sure we haven't blown past our parameter limits */
1847 if (((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1848 (Program
->Base
.NumParameters
>=
1849 ctx
->Const
.VertexProgram
.MaxLocalParams
))
1850 || ((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1851 && (Program
->Base
.NumParameters
>=
1852 ctx
->Const
.FragmentProgram
.MaxLocalParams
))) {
1853 program_error(ctx
, Program
->Position
, "Too many parameter variables");
1862 * This picks out PARAM program parameter bindings.
1864 * XXX: This needs to be stressed & tested
1866 * \return 0 on sucess, 1 on error
1869 parse_param (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1870 struct arb_program
*Program
)
1873 GLint specified_length
;
1874 struct var_cache
*param_var
;
1877 param_var
= parse_string (inst
, vc_head
, Program
, &found
);
1878 Program
->Position
= parse_position (inst
);
1881 char *error_msg
= (char *)
1882 _mesa_malloc (_mesa_strlen ((char *) param_var
->name
) + 40);
1883 _mesa_sprintf (error_msg
, "Duplicate Variable Declaration: %s",
1885 program_error (ctx
, Program
->Position
, error_msg
);
1886 _mesa_free (error_msg
);
1890 specified_length
= parse_integer (inst
, Program
);
1892 if (specified_length
< 0) {
1893 program_error(ctx
, Program
->Position
, "Negative parameter array length");
1897 param_var
->type
= vt_param
;
1898 param_var
->param_binding_length
= 0;
1900 /* Right now, everything is shoved into the main state register file.
1902 * In the future, it would be nice to leave things ENV/LOCAL params
1903 * in their respective register files, if possible
1905 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1908 * * - add each guy to the parameter list
1909 * * - increment the param_var->param_binding_len
1910 * * - store the param_var->param_binding_begin for the first one
1911 * * - compare the actual len to the specified len at the end
1913 while (**inst
!= PARAM_NULL
) {
1914 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_FALSE
))
1918 /* Test array length here! */
1919 if (specified_length
) {
1920 if (specified_length
!= (int)param_var
->param_binding_length
) {
1921 program_error(ctx
, Program
->Position
,
1922 "Declared parameter array length does not match parameter list");
1935 parse_param_use (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1936 struct arb_program
*Program
, struct var_cache
**new_var
)
1938 struct var_cache
*param_var
;
1940 /* First, insert a dummy entry into the var_cache */
1941 var_cache_create (¶m_var
);
1942 param_var
->name
= (const GLubyte
*) " ";
1943 param_var
->type
= vt_param
;
1945 param_var
->param_binding_length
= 0;
1946 /* Don't fill in binding_begin; We use the default value of -1
1947 * to tell if its already initialized, elsewhere.
1949 * param_var->param_binding_begin = 0;
1951 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1953 var_cache_append (vc_head
, param_var
);
1955 /* Then fill it with juicy parameter goodness */
1956 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_TRUE
))
1959 *new_var
= param_var
;
1966 * This handles the declaration of TEMP variables
1968 * \return 0 on sucess, 1 on error
1971 parse_temp (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1972 struct arb_program
*Program
)
1975 struct var_cache
*temp_var
;
1977 while (**inst
!= 0) {
1978 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
1979 Program
->Position
= parse_position (inst
);
1981 char *error_msg
= (char *)
1982 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
1983 _mesa_sprintf (error_msg
, "Duplicate Variable Declaration: %s",
1985 program_error(ctx
, Program
->Position
, error_msg
);
1986 _mesa_free (error_msg
);
1990 temp_var
->type
= vt_temp
;
1992 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1993 (Program
->Base
.NumTemporaries
>=
1994 ctx
->Const
.FragmentProgram
.MaxTemps
))
1995 || ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
1996 && (Program
->Base
.NumTemporaries
>=
1997 ctx
->Const
.VertexProgram
.MaxTemps
))) {
1998 program_error(ctx
, Program
->Position
,
1999 "Too many TEMP variables declared");
2003 temp_var
->temp_binding
= Program
->Base
.NumTemporaries
;
2004 Program
->Base
.NumTemporaries
++;
2012 * This handles variables of the OUTPUT variety
2014 * \return 0 on sucess, 1 on error
2017 parse_output (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2018 struct arb_program
*Program
)
2021 struct var_cache
*output_var
;
2024 output_var
= parse_string (inst
, vc_head
, Program
, &found
);
2025 Program
->Position
= parse_position (inst
);
2027 char *error_msg
= (char *)
2028 _mesa_malloc (_mesa_strlen ((char *) output_var
->name
) + 40);
2029 _mesa_sprintf (error_msg
, "Duplicate Variable Declaration: %s",
2031 program_error (ctx
, Program
->Position
, error_msg
);
2032 _mesa_free (error_msg
);
2036 output_var
->type
= vt_output
;
2038 err
= parse_result_binding(ctx
, inst
, &output_var
->output_binding
, Program
);
2043 * This handles variables of the ALIAS kind
2045 * \return 0 on sucess, 1 on error
2048 parse_alias (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2049 struct arb_program
*Program
)
2052 struct var_cache
*temp_var
;
2054 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2055 Program
->Position
= parse_position (inst
);
2058 char *error_msg
= (char *)
2059 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2060 _mesa_sprintf (error_msg
, "Duplicate Variable Declaration: %s",
2062 program_error(ctx
, Program
->Position
, error_msg
);
2063 _mesa_free (error_msg
);
2067 temp_var
->type
= vt_alias
;
2068 temp_var
->alias_binding
= parse_string (inst
, vc_head
, Program
, &found
);
2069 Program
->Position
= parse_position (inst
);
2073 char *error_msg
= (char *)
2074 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2075 _mesa_sprintf (error_msg
, "Alias value %s is not defined",
2076 temp_var
->alias_binding
->name
);
2077 program_error (ctx
, Program
->Position
, error_msg
);
2078 _mesa_free (error_msg
);
2086 * This handles variables of the ADDRESS kind
2088 * \return 0 on sucess, 1 on error
2091 parse_address (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2092 struct arb_program
*Program
)
2095 struct var_cache
*temp_var
;
2097 while (**inst
!= 0) {
2098 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2099 Program
->Position
= parse_position (inst
);
2101 char *error_msg
= (char *)
2102 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2103 _mesa_sprintf (error_msg
, "Duplicate Variable Declaration: %s",
2105 program_error (ctx
, Program
->Position
, error_msg
);
2106 _mesa_free (error_msg
);
2110 temp_var
->type
= vt_address
;
2112 if (Program
->Base
.NumAddressRegs
>=
2113 ctx
->Const
.VertexProgram
.MaxAddressRegs
) {
2114 const char *msg
= "Too many ADDRESS variables declared";
2115 program_error(ctx
, Program
->Position
, msg
);
2119 temp_var
->address_binding
= Program
->Base
.NumAddressRegs
;
2120 Program
->Base
.NumAddressRegs
++;
2128 * Parse a program declaration
2130 * \return 0 on sucess, 1 on error
2133 parse_declaration (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2134 struct arb_program
*Program
)
2138 switch (*(*inst
)++) {
2140 err
= parse_address (ctx
, inst
, vc_head
, Program
);
2144 err
= parse_alias (ctx
, inst
, vc_head
, Program
);
2148 err
= parse_attrib (ctx
, inst
, vc_head
, Program
);
2152 err
= parse_output (ctx
, inst
, vc_head
, Program
);
2156 err
= parse_param (ctx
, inst
, vc_head
, Program
);
2160 err
= parse_temp (ctx
, inst
, vc_head
, Program
);
2168 * Handle the parsing out of a masked destination register, either for a
2169 * vertex or fragment program.
2171 * If we are a vertex program, make sure we don't write to
2172 * result.position if we have specified that the program is
2173 * position invariant
2175 * \param File - The register file we write to
2176 * \param Index - The register index we write to
2177 * \param WriteMask - The mask controlling which components we write (1->write)
2179 * \return 0 on sucess, 1 on error
2182 parse_masked_dst_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2183 struct var_cache
**vc_head
, struct arb_program
*Program
,
2184 enum register_file
*File
, GLuint
*Index
, GLint
*WriteMask
)
2187 struct var_cache
*dst
;
2189 /* We either have a result register specified, or a
2190 * variable that may or may not be writable
2192 switch (*(*inst
)++) {
2193 case REGISTER_RESULT
:
2194 if (parse_result_binding(ctx
, inst
, Index
, Program
))
2196 *File
= PROGRAM_OUTPUT
;
2199 case REGISTER_ESTABLISHED_NAME
:
2200 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2201 Program
->Position
= parse_position (inst
);
2203 /* If the name has never been added to our symbol table, we're hosed */
2205 program_error(ctx
, Program
->Position
, "0: Undefined variable");
2209 switch (dst
->type
) {
2211 *File
= PROGRAM_OUTPUT
;
2212 *Index
= dst
->output_binding
;
2216 *File
= PROGRAM_TEMPORARY
;
2217 *Index
= dst
->temp_binding
;
2220 /* If the var type is not vt_output or vt_temp, no go */
2222 program_error(ctx
, Program
->Position
,
2223 "Destination register is read only");
2229 program_error(ctx
, Program
->Position
,
2230 "Unexpected opcode in parse_masked_dst_reg()");
2235 /* Position invariance test */
2236 /* This test is done now in syntax portion - when position invariance OPTION
2237 is specified, "result.position" rule is disabled so there is no way
2238 to write the position
2240 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2242 program_error(ctx, Program->Position,
2243 "Vertex program specified position invariance and wrote vertex position");
2246 /* And then the mask.
2252 * ==> Need to reverse the order of bits for this!
2254 tmp
= (GLint
) *(*inst
)++;
2255 *WriteMask
= (((tmp
>>3) & 0x1) |
2265 * Handle the parsing of a address register
2267 * \param Index - The register index we write to
2269 * \return 0 on sucess, 1 on error
2272 parse_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2273 struct var_cache
**vc_head
,
2274 struct arb_program
*Program
, GLint
* Index
)
2276 struct var_cache
*dst
;
2279 *Index
= 0; /* XXX */
2281 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2282 Program
->Position
= parse_position (inst
);
2284 /* If the name has never been added to our symbol table, we're hosed */
2286 program_error(ctx
, Program
->Position
, "Undefined variable");
2290 if (dst
->type
!= vt_address
) {
2291 program_error(ctx
, Program
->Position
, "Variable is not of type ADDRESS");
2300 * Handle the parsing out of a masked address register
2302 * \param Index - The register index we write to
2303 * \param WriteMask - The mask controlling which components we write (1->write)
2305 * \return 0 on sucess, 1 on error
2308 parse_masked_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2309 struct var_cache
**vc_head
,
2310 struct arb_program
*Program
, GLint
* Index
,
2311 GLboolean
* WriteMask
)
2313 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, Index
))
2316 /* This should be 0x8 */
2319 /* Writemask of .x is implied */
2321 WriteMask
[1] = WriteMask
[2] = WriteMask
[3] = 0;
2328 * Parse out a swizzle mask.
2330 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
2332 * The len parameter allows us to grab 4 components for a vector
2333 * swizzle, or just 1 component for a scalar src register selection
2336 parse_swizzle_mask(const GLubyte
** inst
, GLubyte
*swizzle
, GLint len
)
2340 for (i
= 0; i
< 4; i
++)
2343 for (i
= 0; i
< len
; i
++) {
2344 switch (*(*inst
)++) {
2346 swizzle
[i
] = SWIZZLE_X
;
2349 swizzle
[i
] = SWIZZLE_Y
;
2352 swizzle
[i
] = SWIZZLE_Z
;
2355 swizzle
[i
] = SWIZZLE_W
;
2358 _mesa_problem(NULL
, "bad component in parse_swizzle_mask()");
2366 * Parse an extended swizzle mask which is a sequence of
2367 * four x/y/z/w/0/1 tokens.
2368 * \return swizzle four swizzle values
2369 * \return negateMask four element bitfield
2372 parse_extended_swizzle_mask(const GLubyte
**inst
, GLubyte swizzle
[4],
2373 GLubyte
*negateMask
)
2378 for (i
= 0; i
< 4; i
++) {
2380 if (parse_sign(inst
) == -1)
2381 *negateMask
|= (1 << i
);
2387 swizzle
[i
] = SWIZZLE_ZERO
;
2390 swizzle
[i
] = SWIZZLE_ONE
;
2393 swizzle
[i
] = SWIZZLE_X
;
2396 swizzle
[i
] = SWIZZLE_Y
;
2399 swizzle
[i
] = SWIZZLE_Z
;
2402 swizzle
[i
] = SWIZZLE_W
;
2405 _mesa_problem(NULL
, "bad case in parse_extended_swizzle_mask()");
2413 parse_src_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2414 struct var_cache
**vc_head
,
2415 struct arb_program
*Program
,
2416 enum register_file
* File
, GLint
* Index
,
2417 GLboolean
*IsRelOffset
)
2419 struct var_cache
*src
;
2420 GLuint binding
, is_generic
, found
;
2425 /* And the binding for the src */
2426 switch (*(*inst
)++) {
2427 case REGISTER_ATTRIB
:
2428 if (parse_attrib_binding
2429 (ctx
, inst
, Program
, &binding
, &is_generic
))
2431 *File
= PROGRAM_INPUT
;
2434 /* We need to insert a dummy variable into the var_cache so we can
2435 * catch generic vertex attrib aliasing errors
2437 var_cache_create(&src
);
2438 src
->type
= vt_attrib
;
2439 src
->name
= (const GLubyte
*) "Dummy Attrib Variable";
2440 src
->attrib_binding
= binding
;
2441 src
->attrib_is_generic
= is_generic
;
2442 var_cache_append(vc_head
, src
);
2443 if (generic_attrib_check(*vc_head
)) {
2444 program_error(ctx
, Program
->Position
,
2445 "Cannot use both a generic vertex attribute "
2446 "and a specific attribute of the same type");
2451 case REGISTER_PARAM
:
2453 case PARAM_ARRAY_ELEMENT
:
2455 src
= parse_string (inst
, vc_head
, Program
, &found
);
2456 Program
->Position
= parse_position (inst
);
2459 program_error(ctx
, Program
->Position
,
2460 "2: Undefined variable"); /* src->name */
2464 *File
= (enum register_file
) src
->param_binding_type
;
2466 switch (*(*inst
)++) {
2467 case ARRAY_INDEX_ABSOLUTE
:
2468 offset
= parse_integer (inst
, Program
);
2471 || (offset
>= (int)src
->param_binding_length
)) {
2472 program_error(ctx
, Program
->Position
,
2473 "Index out of range");
2474 /* offset, src->name */
2478 *Index
= src
->param_binding_begin
+ offset
;
2481 case ARRAY_INDEX_RELATIVE
:
2483 GLint addr_reg_idx
, rel_off
;
2485 /* First, grab the address regiseter */
2486 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &addr_reg_idx
))
2495 /* Then the relative offset */
2496 if (parse_relative_offset(ctx
, inst
, Program
, &rel_off
)) return 1;
2498 /* And store it properly */
2499 *Index
= src
->param_binding_begin
+ rel_off
;
2507 if (parse_param_use (ctx
, inst
, vc_head
, Program
, &src
))
2510 *File
= (enum register_file
) src
->param_binding_type
;
2511 *Index
= src
->param_binding_begin
;
2516 case REGISTER_ESTABLISHED_NAME
:
2517 src
= parse_string (inst
, vc_head
, Program
, &found
);
2518 Program
->Position
= parse_position (inst
);
2520 /* If the name has never been added to our symbol table, we're hosed */
2522 program_error(ctx
, Program
->Position
,
2523 "3: Undefined variable"); /* src->name */
2527 switch (src
->type
) {
2529 *File
= PROGRAM_INPUT
;
2530 *Index
= src
->attrib_binding
;
2533 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2535 *File
= (enum register_file
) src
->param_binding_type
;
2536 *Index
= src
->param_binding_begin
;
2540 *File
= PROGRAM_TEMPORARY
;
2541 *Index
= src
->temp_binding
;
2544 /* If the var type is vt_output no go */
2546 program_error(ctx
, Program
->Position
,
2547 "destination register is read only");
2554 program_error(ctx
, Program
->Position
,
2555 "Unknown token in parse_src_reg");
2563 * Parse fragment program vector source register.
2566 parse_fp_vector_src_reg(GLcontext
* ctx
, const GLubyte
** inst
,
2567 struct var_cache
**vc_head
,
2568 struct arb_program
*program
,
2569 struct prog_src_register
*reg
)
2571 enum register_file file
;
2575 GLboolean isRelOffset
;
2578 negate
= (parse_sign (inst
) == -1) ? 0xf : 0x0;
2580 /* And the src reg */
2581 if (parse_src_reg(ctx
, inst
, vc_head
, program
, &file
, &index
, &isRelOffset
))
2584 /* finally, the swizzle */
2585 parse_swizzle_mask(inst
, swizzle
, 4);
2589 reg
->NegateBase
= negate
;
2590 reg
->Swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
2596 * Parse fragment program destination register.
2597 * \return 1 if error, 0 if no error.
2600 parse_fp_dst_reg(GLcontext
* ctx
, const GLubyte
** inst
,
2601 struct var_cache
**vc_head
, struct arb_program
*Program
,
2602 struct prog_dst_register
*reg
)
2606 enum register_file file
;
2608 if (parse_masked_dst_reg (ctx
, inst
, vc_head
, Program
, &file
, &idx
, &mask
))
2613 reg
->WriteMask
= mask
;
2619 * Parse fragment program scalar src register.
2620 * \return 1 if error, 0 if no error.
2623 parse_fp_scalar_src_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2624 struct var_cache
**vc_head
,
2625 struct arb_program
*Program
,
2626 struct prog_src_register
*reg
)
2628 enum register_file File
;
2632 GLboolean IsRelOffset
;
2635 Negate
= (parse_sign (inst
) == -1) ? 0x1 : 0x0;
2637 /* And the src reg */
2638 if (parse_src_reg (ctx
, inst
, vc_head
, Program
, &File
, &Index
, &IsRelOffset
))
2641 /* finally, the swizzle */
2642 parse_swizzle_mask(inst
, Swizzle
, 1);
2646 reg
->NegateBase
= Negate
;
2647 reg
->Swizzle
= (Swizzle
[0] << 0);
2654 * This is a big mother that handles getting opcodes into the instruction
2655 * and handling the src & dst registers for fragment program instructions
2656 * \return 1 if error, 0 if no error
2659 parse_fp_instruction (GLcontext
* ctx
, const GLubyte
** inst
,
2660 struct var_cache
**vc_head
, struct arb_program
*Program
,
2661 struct prog_instruction
*fp
)
2665 GLubyte instClass
, type
, code
;
2667 GLuint shadow_tex
= 0;
2669 _mesa_init_instructions(fp
, 1);
2671 /* Record the position in the program string for debugging */
2672 fp
->StringPos
= Program
->Position
;
2674 /* OP_ALU_INST or OP_TEX_INST */
2675 instClass
= *(*inst
)++;
2677 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2678 * OP_TEX_{SAMPLE, KIL}
2682 /* The actual opcode name */
2685 /* Increment the correct count */
2686 switch (instClass
) {
2688 Program
->NumAluInstructions
++;
2691 Program
->NumTexInstructions
++;
2699 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2701 fp
->Opcode
= OPCODE_ABS
;
2705 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2707 fp
->Opcode
= OPCODE_FLR
;
2711 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2713 fp
->Opcode
= OPCODE_FRC
;
2717 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2719 fp
->Opcode
= OPCODE_LIT
;
2723 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2725 fp
->Opcode
= OPCODE_MOV
;
2729 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2732 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2739 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2741 fp
->Opcode
= OPCODE_COS
;
2745 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2747 fp
->Opcode
= OPCODE_EX2
;
2751 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2753 fp
->Opcode
= OPCODE_LG2
;
2757 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2759 fp
->Opcode
= OPCODE_RCP
;
2763 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2765 fp
->Opcode
= OPCODE_RSQ
;
2769 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2771 fp
->Opcode
= OPCODE_SIN
;
2775 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2778 fp
->Opcode
= OPCODE_SCS
;
2782 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2785 if (parse_fp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2792 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2794 fp
->Opcode
= OPCODE_POW
;
2798 if (parse_fp_dst_reg(ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2801 for (a
= 0; a
< 2; a
++) {
2802 if (parse_fp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2811 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2813 fp
->Opcode
= OPCODE_ADD
;
2817 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2819 fp
->Opcode
= OPCODE_DP3
;
2823 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2825 fp
->Opcode
= OPCODE_DP4
;
2829 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2831 fp
->Opcode
= OPCODE_DPH
;
2835 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2837 fp
->Opcode
= OPCODE_DST
;
2841 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2843 fp
->Opcode
= OPCODE_MAX
;
2847 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2849 fp
->Opcode
= OPCODE_MIN
;
2853 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2855 fp
->Opcode
= OPCODE_MUL
;
2859 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2861 fp
->Opcode
= OPCODE_SGE
;
2865 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2867 fp
->Opcode
= OPCODE_SLT
;
2871 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2873 fp
->Opcode
= OPCODE_SUB
;
2877 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2879 fp
->Opcode
= OPCODE_XPD
;
2883 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2885 for (a
= 0; a
< 2; a
++) {
2886 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2894 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2896 fp
->Opcode
= OPCODE_CMP
;
2900 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2902 fp
->Opcode
= OPCODE_LRP
;
2906 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2908 fp
->Opcode
= OPCODE_MAD
;
2912 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2915 for (a
= 0; a
< 3; a
++) {
2916 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2924 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2926 fp
->Opcode
= OPCODE_SWZ
;
2929 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2935 enum register_file file
;
2938 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
, &rel
))
2940 parse_extended_swizzle_mask(inst
, swizzle
, &negateMask
);
2941 fp
->SrcReg
[0].File
= file
;
2942 fp
->SrcReg
[0].Index
= index
;
2943 fp
->SrcReg
[0].NegateBase
= negateMask
;
2944 fp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
2954 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2956 fp
->Opcode
= OPCODE_TEX
;
2960 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2962 fp
->Opcode
= OPCODE_TXP
;
2966 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2968 fp
->Opcode
= OPCODE_TXB
;
2972 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2975 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2979 if (parse_texcoord_num (ctx
, inst
, Program
, &texcoord
))
2981 fp
->TexSrcUnit
= texcoord
;
2984 switch (*(*inst
)++) {
2985 case TEXTARGET_SHADOW1D
:
2986 shadow_tex
= 1 << texcoord
;
2989 fp
->TexSrcTarget
= TEXTURE_1D_INDEX
;
2991 case TEXTARGET_SHADOW2D
:
2992 shadow_tex
= 1 << texcoord
;
2995 fp
->TexSrcTarget
= TEXTURE_2D_INDEX
;
2998 fp
->TexSrcTarget
= TEXTURE_3D_INDEX
;
3000 case TEXTARGET_SHADOWRECT
:
3001 shadow_tex
= 1 << texcoord
;
3003 case TEXTARGET_RECT
:
3004 fp
->TexSrcTarget
= TEXTURE_RECT_INDEX
;
3006 case TEXTARGET_CUBE
:
3007 fp
->TexSrcTarget
= TEXTURE_CUBE_INDEX
;
3009 case TEXTARGET_SHADOW1D_ARRAY
:
3010 shadow_tex
= 1 << texcoord
;
3012 case TEXTARGET_1D_ARRAY
:
3013 fp
->TexSrcTarget
= TEXTURE_1D_ARRAY_INDEX
;
3015 case TEXTARGET_SHADOW2D_ARRAY
:
3016 shadow_tex
= 1 << texcoord
;
3018 case TEXTARGET_2D_ARRAY
:
3019 fp
->TexSrcTarget
= TEXTURE_2D_ARRAY_INDEX
;
3023 /* Don't test the first time a particular sampler is seen. Each time
3024 * after that, make sure the shadow state is the same.
3026 if ((_mesa_bitcount(Program
->TexturesUsed
[texcoord
]) > 0)
3027 && ((Program
->ShadowSamplers
& (1 << texcoord
)) != shadow_tex
)) {
3028 program_error(ctx
, Program
->Position
,
3029 "texture image unit used for shadow sampling and non-shadow sampling");
3033 Program
->TexturesUsed
[texcoord
] |= (1 << fp
->TexSrcTarget
);
3034 /* Check that both "2D" and "CUBE" (for example) aren't both used */
3035 if (_mesa_bitcount(Program
->TexturesUsed
[texcoord
]) > 1) {
3036 program_error(ctx
, Program
->Position
,
3037 "multiple targets used on one texture image unit");
3042 Program
->ShadowSamplers
|= shadow_tex
;
3046 Program
->UsesKill
= 1;
3047 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
3049 fp
->Opcode
= OPCODE_KIL
;
3052 _mesa_problem(ctx
, "bad type 0x%x in parse_fp_instruction()", type
);
3060 parse_vp_dst_reg(GLcontext
* ctx
, const GLubyte
** inst
,
3061 struct var_cache
**vc_head
, struct arb_program
*Program
,
3062 struct prog_dst_register
*reg
)
3066 enum register_file file
;
3068 if (parse_masked_dst_reg(ctx
, inst
, vc_head
, Program
, &file
, &idx
, &mask
))
3073 reg
->WriteMask
= mask
;
3078 * Handle the parsing out of a masked address register
3080 * \param Index - The register index we write to
3081 * \param WriteMask - The mask controlling which components we write (1->write)
3083 * \return 0 on sucess, 1 on error
3086 parse_vp_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
3087 struct var_cache
**vc_head
,
3088 struct arb_program
*Program
,
3089 struct prog_dst_register
*reg
)
3093 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &idx
))
3096 /* This should be 0x8 */
3099 reg
->File
= PROGRAM_ADDRESS
;
3102 /* Writemask of .x is implied */
3103 reg
->WriteMask
= 0x1;
3108 * Parse vertex program vector source register.
3111 parse_vp_vector_src_reg(GLcontext
* ctx
, const GLubyte
** inst
,
3112 struct var_cache
**vc_head
,
3113 struct arb_program
*program
,
3114 struct prog_src_register
*reg
)
3116 enum register_file file
;
3120 GLboolean isRelOffset
;
3123 negateMask
= (parse_sign (inst
) == -1) ? 0xf : 0x0;
3125 /* And the src reg */
3126 if (parse_src_reg (ctx
, inst
, vc_head
, program
, &file
, &index
, &isRelOffset
))
3129 /* finally, the swizzle */
3130 parse_swizzle_mask(inst
, swizzle
, 4);
3134 reg
->Swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1],
3135 swizzle
[2], swizzle
[3]);
3136 reg
->NegateBase
= negateMask
;
3137 reg
->RelAddr
= isRelOffset
;
3143 parse_vp_scalar_src_reg (GLcontext
* ctx
, const GLubyte
** inst
,
3144 struct var_cache
**vc_head
,
3145 struct arb_program
*Program
,
3146 struct prog_src_register
*reg
)
3148 enum register_file File
;
3152 GLboolean IsRelOffset
;
3155 Negate
= (parse_sign (inst
) == -1) ? 0x1 : 0x0;
3157 /* And the src reg */
3158 if (parse_src_reg (ctx
, inst
, vc_head
, Program
, &File
, &Index
, &IsRelOffset
))
3161 /* finally, the swizzle */
3162 parse_swizzle_mask(inst
, Swizzle
, 1);
3166 reg
->Swizzle
= (Swizzle
[0] << 0);
3167 reg
->NegateBase
= Negate
;
3168 reg
->RelAddr
= IsRelOffset
;
3174 * This is a big mother that handles getting opcodes into the instruction
3175 * and handling the src & dst registers for vertex program instructions
3178 parse_vp_instruction (GLcontext
* ctx
, const GLubyte
** inst
,
3179 struct var_cache
**vc_head
, struct arb_program
*Program
,
3180 struct prog_instruction
*vp
)
3185 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3188 /* The actual opcode name */
3191 _mesa_init_instructions(vp
, 1);
3192 /* Record the position in the program string for debugging */
3193 vp
->StringPos
= Program
->Position
;
3198 vp
->Opcode
= OPCODE_ARL
;
3200 /* Remember to set SrcReg.RelAddr; */
3202 /* Get the masked address register [dst] */
3203 if (parse_vp_address_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3206 vp
->DstReg
.File
= PROGRAM_ADDRESS
;
3208 /* Get a scalar src register */
3209 if (parse_vp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3217 vp
->Opcode
= OPCODE_ABS
;
3220 vp
->Opcode
= OPCODE_FLR
;
3223 vp
->Opcode
= OPCODE_FRC
;
3226 vp
->Opcode
= OPCODE_LIT
;
3229 vp
->Opcode
= OPCODE_MOV
;
3233 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3236 if (parse_vp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3243 vp
->Opcode
= OPCODE_EX2
;
3246 vp
->Opcode
= OPCODE_EXP
;
3249 vp
->Opcode
= OPCODE_LG2
;
3252 vp
->Opcode
= OPCODE_LOG
;
3255 vp
->Opcode
= OPCODE_RCP
;
3258 vp
->Opcode
= OPCODE_RSQ
;
3261 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3264 if (parse_vp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3271 vp
->Opcode
= OPCODE_POW
;
3274 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3277 for (a
= 0; a
< 2; a
++) {
3278 if (parse_vp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3286 vp
->Opcode
= OPCODE_ADD
;
3289 vp
->Opcode
= OPCODE_DP3
;
3292 vp
->Opcode
= OPCODE_DP4
;
3295 vp
->Opcode
= OPCODE_DPH
;
3298 vp
->Opcode
= OPCODE_DST
;
3301 vp
->Opcode
= OPCODE_MAX
;
3304 vp
->Opcode
= OPCODE_MIN
;
3307 vp
->Opcode
= OPCODE_MUL
;
3310 vp
->Opcode
= OPCODE_SGE
;
3313 vp
->Opcode
= OPCODE_SLT
;
3316 vp
->Opcode
= OPCODE_SUB
;
3319 vp
->Opcode
= OPCODE_XPD
;
3322 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3325 for (a
= 0; a
< 2; a
++) {
3326 if (parse_vp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3334 vp
->Opcode
= OPCODE_MAD
;
3338 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3341 for (a
= 0; a
< 3; a
++) {
3342 if (parse_vp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3350 vp
->Opcode
= OPCODE_SWZ
;
3357 enum register_file file
;
3360 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3363 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
, &relAddr
))
3365 parse_extended_swizzle_mask (inst
, swizzle
, &negateMask
);
3366 vp
->SrcReg
[0].File
= file
;
3367 vp
->SrcReg
[0].Index
= index
;
3368 vp
->SrcReg
[0].NegateBase
= negateMask
;
3369 vp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
3373 vp
->SrcReg
[0].RelAddr
= relAddr
;
3383 debug_variables (GLcontext
* ctx
, struct var_cache
*vc_head
,
3384 struct arb_program
*Program
)
3386 struct var_cache
*vc
;
3389 fprintf (stderr
, "debug_variables, vc_head: %p\n", (void*) vc_head
);
3391 /* First of all, print out the contents of the var_cache */
3394 fprintf (stderr
, "[%p]\n", (void*) vc
);
3397 fprintf (stderr
, "UNDEFINED %s\n", vc
->name
);
3400 fprintf (stderr
, "ATTRIB %s\n", vc
->name
);
3401 fprintf (stderr
, " binding: 0x%x\n", vc
->attrib_binding
);
3404 fprintf (stderr
, "PARAM %s begin: %d len: %d\n", vc
->name
,
3405 vc
->param_binding_begin
, vc
->param_binding_length
);
3406 b
= vc
->param_binding_begin
;
3407 for (a
= 0; a
< vc
->param_binding_length
; a
++) {
3408 fprintf (stderr
, "%s\n",
3409 Program
->Base
.Parameters
->Parameters
[a
+ b
].Name
);
3410 if (Program
->Base
.Parameters
->Parameters
[a
+ b
].Type
== PROGRAM_STATE_VAR
) {
3412 s
= _mesa_program_state_string(Program
->Base
.Parameters
->Parameters
3413 [a
+ b
].StateIndexes
);
3414 fprintf(stderr
, "%s\n", s
);
3415 _mesa_free((char *) s
);
3418 fprintf (stderr
, "%f %f %f %f\n",
3419 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][0],
3420 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][1],
3421 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][2],
3422 Program
->Base
.Parameters
->ParameterValues
[a
+ b
][3]);
3426 fprintf (stderr
, "TEMP %s\n", vc
->name
);
3427 fprintf (stderr
, " binding: 0x%x\n", vc
->temp_binding
);
3430 fprintf (stderr
, "OUTPUT %s\n", vc
->name
);
3431 fprintf (stderr
, " binding: 0x%x\n", vc
->output_binding
);
3434 fprintf (stderr
, "ALIAS %s\n", vc
->name
);
3435 fprintf (stderr
, " binding: 0x%p (%s)\n",
3436 (void*) vc
->alias_binding
, vc
->alias_binding
->name
);
3446 #endif /* DEBUG_PARSING */
3450 * The main loop for parsing a fragment or vertex program
3452 * \return 1 on error, 0 on success
3455 parse_instructions(GLcontext
* ctx
, const GLubyte
* inst
,
3456 struct var_cache
**vc_head
, struct arb_program
*Program
)
3458 const GLuint maxInst
= (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
3459 ? ctx
->Const
.FragmentProgram
.MaxInstructions
3460 : ctx
->Const
.VertexProgram
.MaxInstructions
;
3463 ASSERT(MAX_INSTRUCTIONS
>= maxInst
);
3465 Program
->MajorVersion
= (GLuint
) * inst
++;
3466 Program
->MinorVersion
= (GLuint
) * inst
++;
3468 while (*inst
!= END
) {
3473 case ARB_PRECISION_HINT_FASTEST
:
3474 Program
->PrecisionOption
= GL_FASTEST
;
3477 case ARB_PRECISION_HINT_NICEST
:
3478 Program
->PrecisionOption
= GL_NICEST
;
3482 Program
->FogOption
= GL_EXP
;
3486 Program
->FogOption
= GL_EXP2
;
3489 case ARB_FOG_LINEAR
:
3490 Program
->FogOption
= GL_LINEAR
;
3493 case ARB_POSITION_INVARIANT
:
3494 if (Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
3495 Program
->HintPositionInvariant
= GL_TRUE
;
3498 case ARB_FRAGMENT_PROGRAM_SHADOW
:
3499 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3500 /* TODO ARB_fragment_program_shadow code */
3504 case ARB_DRAW_BUFFERS
:
3505 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3506 /* do nothing for now */
3510 case MESA_TEXTURE_ARRAY
:
3511 /* do nothing for now */
3518 if (Program
->Base
.NumInstructions
+ 1 >= maxInst
) {
3519 program_error(ctx
, Program
->Position
,
3520 "Max instruction count exceeded");
3523 Program
->Position
= parse_position (&inst
);
3524 /* parse the current instruction */
3525 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3526 err
= parse_fp_instruction (ctx
, &inst
, vc_head
, Program
,
3527 &Program
->Base
.Instructions
[Program
->Base
.NumInstructions
]);
3530 err
= parse_vp_instruction (ctx
, &inst
, vc_head
, Program
,
3531 &Program
->Base
.Instructions
[Program
->Base
.NumInstructions
]);
3534 /* increment instuction count */
3535 Program
->Base
.NumInstructions
++;
3539 err
= parse_declaration (ctx
, &inst
, vc_head
, Program
);
3550 /* Finally, tag on an OPCODE_END instruction */
3552 const GLuint numInst
= Program
->Base
.NumInstructions
;
3553 _mesa_init_instructions(Program
->Base
.Instructions
+ numInst
, 1);
3554 Program
->Base
.Instructions
[numInst
].Opcode
= OPCODE_END
;
3555 /* YYY Wrong Position in program, whatever, at least not random -> crash
3556 Program->Position = parse_position (&inst);
3558 Program
->Base
.Instructions
[numInst
].StringPos
= Program
->Position
;
3560 Program
->Base
.NumInstructions
++;
3563 * Initialize native counts to logical counts. The device driver may
3564 * change them if program is translated into a hardware program.
3566 Program
->Base
.NumNativeInstructions
= Program
->Base
.NumInstructions
;
3567 Program
->Base
.NumNativeTemporaries
= Program
->Base
.NumTemporaries
;
3568 Program
->Base
.NumNativeParameters
= Program
->Base
.NumParameters
;
3569 Program
->Base
.NumNativeAttributes
= Program
->Base
.NumAttributes
;
3570 Program
->Base
.NumNativeAddressRegs
= Program
->Base
.NumAddressRegs
;
3577 LONGSTRING
static char core_grammar_text
[] =
3578 #include "shader/grammar/grammar_syn.h"
3583 * Set a grammar parameter.
3584 * \param name the grammar parameter
3585 * \param value the new parameter value
3586 * \return 0 if OK, 1 if error
3589 set_reg8 (GLcontext
*ctx
, grammar id
, const char *name
, GLubyte value
)
3591 char error_msg
[300];
3594 if (grammar_set_reg8 (id
, (const byte
*) name
, value
))
3597 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3598 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3599 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Grammar Register Error");
3605 * Enable support for the given language option in the parser.
3606 * \return 1 if OK, 0 if error
3609 enable_ext(GLcontext
*ctx
, grammar id
, const char *name
)
3611 return !set_reg8(ctx
, id
, name
, 1);
3616 * Enable parser extensions based on which OpenGL extensions are supported
3617 * by this rendering context.
3619 * \return GL_TRUE if OK, GL_FALSE if error.
3622 enable_parser_extensions(GLcontext
*ctx
, grammar id
)
3625 /* These are not supported at this time */
3626 if ((ctx
->Extensions
.ARB_vertex_blend
||
3627 ctx
->Extensions
.EXT_vertex_weighting
)
3628 && !enable_ext(ctx
, id
, "vertex_blend"))
3630 if (ctx
->Extensions
.ARB_matrix_palette
3631 && !enable_ext(ctx
, id
, "matrix_palette"))
3634 if (ctx
->Extensions
.ARB_fragment_program_shadow
3635 && !enable_ext(ctx
, id
, "fragment_program_shadow"))
3637 if (ctx
->Extensions
.EXT_point_parameters
3638 && !enable_ext(ctx
, id
, "point_parameters"))
3640 if (ctx
->Extensions
.EXT_secondary_color
3641 && !enable_ext(ctx
, id
, "secondary_color"))
3643 if (ctx
->Extensions
.EXT_fog_coord
3644 && !enable_ext(ctx
, id
, "fog_coord"))
3646 if (ctx
->Extensions
.NV_texture_rectangle
3647 && !enable_ext(ctx
, id
, "texture_rectangle"))
3649 if (ctx
->Extensions
.ARB_draw_buffers
3650 && !enable_ext(ctx
, id
, "draw_buffers"))
3652 if (ctx
->Extensions
.MESA_texture_array
3653 && !enable_ext(ctx
, id
, "texture_array"))
3656 /* hack for Warcraft (see bug 8060) */
3657 enable_ext(ctx
, id
, "vertex_blend");
3665 * This kicks everything off.
3667 * \param ctx - The GL Context
3668 * \param str - The program string
3669 * \param len - The program string length
3670 * \param program - The arb_program struct to return all the parsed info in
3671 * \return GL_TRUE on sucess, GL_FALSE on error
3674 _mesa_parse_arb_program(GLcontext
*ctx
, GLenum target
,
3675 const GLubyte
*str
, GLsizei len
,
3676 struct arb_program
*program
)
3678 GLint a
, err
, error_pos
;
3679 char error_msg
[300];
3681 struct var_cache
*vc_head
;
3682 grammar arbprogram_syn_id
;
3683 GLubyte
*parsed
, *inst
;
3684 GLubyte
*strz
= NULL
;
3685 static int arbprogram_syn_is_ok
= 0; /* XXX temporary */
3687 /* set the program target before parsing */
3688 program
->Base
.Target
= target
;
3690 /* Reset error state */
3691 _mesa_set_program_error(ctx
, -1, NULL
);
3693 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
3694 if (!arbprogram_syn_is_ok
) {
3695 /* One-time initialization of parsing system */
3696 grammar grammar_syn_id
;
3699 grammar_syn_id
= grammar_load_from_text ((byte
*) core_grammar_text
);
3700 if (grammar_syn_id
== 0) {
3701 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3702 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3703 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3704 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3705 "glProgramStringARB(Error loading grammar rule set)");
3709 err
= !grammar_check(grammar_syn_id
, (byte
*) arb_grammar_text
,
3710 &parsed
, &parsed_len
);
3712 /* 'parsed' is unused here */
3713 _mesa_free (parsed
);
3716 /* NOTE: we can't destroy grammar_syn_id right here because
3717 * grammar_destroy() can reset the last error
3720 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3721 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3722 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3723 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3724 "glProgramString(Error loading grammar rule set");
3725 grammar_destroy (grammar_syn_id
);
3729 grammar_destroy (grammar_syn_id
);
3731 arbprogram_syn_is_ok
= 1;
3734 /* create the grammar object */
3735 arbprogram_syn_id
= grammar_load_from_text ((byte
*) arb_grammar_text
);
3736 if (arbprogram_syn_id
== 0) {
3737 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3738 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
3739 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3740 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3741 "glProgramString(Error loading grammer rule set)");
3745 /* Set program_target register value */
3746 if (set_reg8 (ctx
, arbprogram_syn_id
, "program_target",
3747 program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
? 0x10 : 0x20)) {
3748 grammar_destroy (arbprogram_syn_id
);
3752 if (!enable_parser_extensions(ctx
, arbprogram_syn_id
)) {
3753 grammar_destroy(arbprogram_syn_id
);
3757 /* check for NULL character occurences */
3760 for (i
= 0; i
< len
; i
++) {
3761 if (str
[i
] == '\0') {
3762 program_error(ctx
, i
, "illegal character");
3763 grammar_destroy (arbprogram_syn_id
);
3769 /* copy the program string to a null-terminated string */
3770 strz
= (GLubyte
*) _mesa_malloc (len
+ 1);
3772 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glProgramStringARB");
3773 grammar_destroy (arbprogram_syn_id
);
3776 _mesa_memcpy (strz
, str
, len
);
3779 /* do a fast check on program string - initial production buffer is 4K */
3780 err
= !grammar_fast_check(arbprogram_syn_id
, strz
,
3781 &parsed
, &parsed_len
, 0x1000);
3783 /* Syntax parse error */
3785 grammar_get_last_error((GLubyte
*) error_msg
, 300, &error_pos
);
3786 program_error(ctx
, error_pos
, error_msg
);
3789 /* useful for debugging */
3793 fprintf(stderr
, "program: %s\n", (char *) strz
);
3794 fprintf(stderr
, "Error Pos: %d\n", ctx
->program
.ErrorPos
);
3795 s
= (char *) _mesa_find_line_column(strz
, strz
+ctx
->program
.ErrorPos
,
3797 fprintf(stderr
, "line %d col %d: %s\n", line
, col
, s
);
3804 grammar_destroy (arbprogram_syn_id
);
3808 grammar_destroy (arbprogram_syn_id
);
3811 * Program string is syntactically correct at this point
3812 * Parse the tokenized version of the program now, generating
3813 * vertex/fragment program instructions.
3816 /* Initialize the arb_program struct */
3817 program
->Base
.String
= strz
;
3818 program
->Base
.Instructions
= _mesa_alloc_instructions(MAX_INSTRUCTIONS
);
3819 program
->Base
.NumInstructions
=
3820 program
->Base
.NumTemporaries
=
3821 program
->Base
.NumParameters
=
3822 program
->Base
.NumAttributes
= program
->Base
.NumAddressRegs
= 0;
3823 program
->Base
.Parameters
= _mesa_new_parameter_list ();
3824 program
->Base
.InputsRead
= 0x0;
3825 program
->Base
.OutputsWritten
= 0x0;
3826 program
->Position
= 0;
3827 program
->MajorVersion
= program
->MinorVersion
= 0;
3828 program
->PrecisionOption
= GL_DONT_CARE
;
3829 program
->FogOption
= GL_NONE
;
3830 program
->HintPositionInvariant
= GL_FALSE
;
3831 for (a
= 0; a
< MAX_TEXTURE_IMAGE_UNITS
; a
++)
3832 program
->TexturesUsed
[a
] = 0x0;
3833 program
->ShadowSamplers
= 0x0;
3834 program
->NumAluInstructions
=
3835 program
->NumTexInstructions
=
3836 program
->NumTexIndirections
= 0;
3837 program
->UsesKill
= 0;
3842 /* Start examining the tokens in the array */
3845 /* Check the grammer rev */
3846 if (*inst
++ != REVISION
) {
3847 program_error (ctx
, 0, "Grammar version mismatch");
3851 /* ignore program target */
3853 err
= parse_instructions(ctx
, inst
, &vc_head
, program
);
3856 /*debug_variables(ctx, vc_head, program); */
3858 /* We're done with the parsed binary array */
3859 var_cache_destroy (&vc_head
);
3861 _mesa_free (parsed
);
3863 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
3864 * to size [ap.Base.NumInstructions].
3866 program
->Base
.Instructions
3867 = _mesa_realloc_instructions(program
->Base
.Instructions
,
3869 program
->Base
.NumInstructions
);
3877 _mesa_parse_arb_fragment_program(GLcontext
* ctx
, GLenum target
,
3878 const GLvoid
*str
, GLsizei len
,
3879 struct gl_fragment_program
*program
)
3881 struct arb_program ap
;
3884 ASSERT(target
== GL_FRAGMENT_PROGRAM_ARB
);
3885 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
, &ap
)) {
3886 /* Error in the program. Just return. */
3890 /* Copy the relevant contents of the arb_program struct into the
3891 * fragment_program struct.
3893 program
->Base
.String
= ap
.Base
.String
;
3894 program
->Base
.NumInstructions
= ap
.Base
.NumInstructions
;
3895 program
->Base
.NumTemporaries
= ap
.Base
.NumTemporaries
;
3896 program
->Base
.NumParameters
= ap
.Base
.NumParameters
;
3897 program
->Base
.NumAttributes
= ap
.Base
.NumAttributes
;
3898 program
->Base
.NumAddressRegs
= ap
.Base
.NumAddressRegs
;
3899 program
->Base
.NumNativeInstructions
= ap
.Base
.NumNativeInstructions
;
3900 program
->Base
.NumNativeTemporaries
= ap
.Base
.NumNativeTemporaries
;
3901 program
->Base
.NumNativeParameters
= ap
.Base
.NumNativeParameters
;
3902 program
->Base
.NumNativeAttributes
= ap
.Base
.NumNativeAttributes
;
3903 program
->Base
.NumNativeAddressRegs
= ap
.Base
.NumNativeAddressRegs
;
3904 program
->Base
.NumAluInstructions
= ap
.Base
.NumAluInstructions
;
3905 program
->Base
.NumTexInstructions
= ap
.Base
.NumTexInstructions
;
3906 program
->Base
.NumTexIndirections
= ap
.Base
.NumTexIndirections
;
3907 program
->Base
.NumNativeAluInstructions
= ap
.Base
.NumAluInstructions
;
3908 program
->Base
.NumNativeTexInstructions
= ap
.Base
.NumTexInstructions
;
3909 program
->Base
.NumNativeTexIndirections
= ap
.Base
.NumTexIndirections
;
3910 program
->Base
.InputsRead
= ap
.Base
.InputsRead
;
3911 program
->Base
.OutputsWritten
= ap
.Base
.OutputsWritten
;
3912 for (i
= 0; i
< MAX_TEXTURE_IMAGE_UNITS
; i
++)
3913 program
->Base
.TexturesUsed
[i
] = ap
.TexturesUsed
[i
];
3914 program
->Base
.ShadowSamplers
= ap
.ShadowSamplers
;
3915 program
->FogOption
= ap
.FogOption
;
3916 program
->UsesKill
= ap
.UsesKill
;
3918 if (program
->Base
.Instructions
)
3919 _mesa_free(program
->Base
.Instructions
);
3920 program
->Base
.Instructions
= ap
.Base
.Instructions
;
3922 if (program
->Base
.Parameters
)
3923 _mesa_free_parameter_list(program
->Base
.Parameters
);
3924 program
->Base
.Parameters
= ap
.Base
.Parameters
;
3927 _mesa_printf("____________Fragment program %u ________\n", program
->Base
.ID
);
3928 _mesa_print_program(&program
->Base
);
3935 * Parse the vertex program string. If success, update the given
3936 * vertex_program object with the new program. Else, leave the vertex_program
3940 _mesa_parse_arb_vertex_program(GLcontext
*ctx
, GLenum target
,
3941 const GLvoid
*str
, GLsizei len
,
3942 struct gl_vertex_program
*program
)
3944 struct arb_program ap
;
3946 ASSERT(target
== GL_VERTEX_PROGRAM_ARB
);
3948 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
, &ap
)) {
3949 /* Error in the program. Just return. */
3953 /* Copy the relevant contents of the arb_program struct into the
3954 * vertex_program struct.
3956 program
->Base
.String
= ap
.Base
.String
;
3957 program
->Base
.NumInstructions
= ap
.Base
.NumInstructions
;
3958 program
->Base
.NumTemporaries
= ap
.Base
.NumTemporaries
;
3959 program
->Base
.NumParameters
= ap
.Base
.NumParameters
;
3960 program
->Base
.NumAttributes
= ap
.Base
.NumAttributes
;
3961 program
->Base
.NumAddressRegs
= ap
.Base
.NumAddressRegs
;
3962 program
->Base
.NumNativeInstructions
= ap
.Base
.NumNativeInstructions
;
3963 program
->Base
.NumNativeTemporaries
= ap
.Base
.NumNativeTemporaries
;
3964 program
->Base
.NumNativeParameters
= ap
.Base
.NumNativeParameters
;
3965 program
->Base
.NumNativeAttributes
= ap
.Base
.NumNativeAttributes
;
3966 program
->Base
.NumNativeAddressRegs
= ap
.Base
.NumNativeAddressRegs
;
3967 program
->Base
.InputsRead
= ap
.Base
.InputsRead
;
3968 program
->Base
.OutputsWritten
= ap
.Base
.OutputsWritten
;
3969 program
->IsPositionInvariant
= ap
.HintPositionInvariant
;
3971 if (program
->Base
.Instructions
)
3972 _mesa_free(program
->Base
.Instructions
);
3973 program
->Base
.Instructions
= ap
.Base
.Instructions
;
3975 if (program
->Base
.Parameters
)
3976 _mesa_free_parameter_list(program
->Base
.Parameters
);
3977 program
->Base
.Parameters
= ap
.Base
.Parameters
;
3980 _mesa_printf("____________Vertex program %u __________\n", program
->Base
.Id
);
3981 _mesa_print_program(&program
->Base
);