2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #define DEBUG_PARSING 0
28 * \file arbprogparse.c
29 * ARB_*_program parser core
35 #include "arbprogparse.h"
36 #include "grammar_mesa.h"
41 #include "program_instruction.h"
44 /* For ARB programs, use the NV instruction limits */
45 #define MAX_INSTRUCTIONS MAX2(MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS, \
46 MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS)
50 * This is basically a union of the vertex_program and fragment_program
51 * structs that we can use to parse the program into
53 * XXX we can probably get rid of this entirely someday.
57 struct gl_program Base
;
59 GLuint Position
; /* Just used for error reporting while parsing */
63 /* ARB_vertex_progmra options */
64 GLboolean HintPositionInvariant
;
66 /* ARB_fragment_progmra options */
67 GLenum PrecisionOption
; /* GL_DONT_CARE, GL_NICEST or GL_FASTEST */
68 GLenum FogOption
; /* GL_NONE, GL_LINEAR, GL_EXP or GL_EXP2 */
70 /* ARB_fragment_program specifics */
71 GLbitfield TexturesUsed
[MAX_TEXTURE_IMAGE_UNITS
];
72 GLuint NumAluInstructions
;
73 GLuint NumTexInstructions
;
74 GLuint NumTexIndirections
;
82 * Fragment Program Stuff:
83 * -----------------------------------------------------
85 * - things from Michal's email
87 * + not-overflowing floats (don't use parse_integer..)
88 * + can remove range checking in arbparse.c
90 * - check all limits of number of various variables
95 * Vertex Program Stuff:
96 * -----------------------------------------------------
97 * - Optimize param array usage and count limits correctly, see spec,
99 * + Record if an array is reference absolutly or relatively (or both)
100 * + For absolute arrays, store a bitmap of accesses
101 * + For single parameters, store an access flag
102 * + After parsing, make a parameter cleanup and merging pass, where
103 * relative arrays are layed out first, followed by abs arrays, and
104 * finally single state.
105 * + Remap offsets for param src and dst registers
106 * + Now we can properly count parameter usage
108 * - Multiple state binding errors in param arrays (see spec, just before
113 * -----------------------------------------------------
114 * - User clipping planes vs. PositionInvariant
115 * - Is it sufficient to just multiply by the mvp to transform in the
116 * PositionInvariant case? Or do we need something more involved?
118 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
119 * - fetch state listed in program_parameters list
120 * + WTF should this go???
121 * + currently in nvvertexec.c and s_nvfragprog.c
123 * - allow for multiple address registers (and fetch address regs properly)
126 * -----------------------------------------------------
127 * - remove any leftover unused grammer.c stuff (dict_ ?)
128 * - fix grammer.c error handling so its not static
129 * - #ifdef around stuff pertaining to extentions
131 * Outstanding Questions:
132 * -----------------------------------------------------
133 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
134 * what gets hacked off because of this:
135 * + VERTEX_ATTRIB_MATRIXINDEX
136 * + VERTEX_ATTRIB_WEIGHT
140 * - When can we fetch env/local params from their own register files, and
141 * when to we have to fetch them into the main state register file?
145 * -----------------------------------------------------
148 /* Changes since moving the file to shader directory
150 2004-III-4 ------------------------------------------------------------
151 - added #include "grammar_mesa.h"
152 - removed grammar specific code part (it resides now in grammar.c)
153 - added GL_ARB_fragment_program_shadow tokens
154 - modified #include "arbparse_syn.h"
155 - major changes inside _mesa_parse_arb_program()
156 - check the program string for '\0' characters
157 - copy the program string to a one-byte-longer location to have
159 - position invariance test (not writing to result.position) moved
163 typedef GLubyte
*production
;
167 * This is the text describing the rules to parse the grammar
169 LONGSTRING
static char arb_grammar_text
[] =
170 #include "arbprogram_syn.h"
174 * These should match up with the values defined in arbprogram.syn
179 - changed and merged V_* and F_* opcode values to OP_*.
180 - added GL_ARB_fragment_program_shadow specific tokens (michal)
182 #define REVISION 0x09
185 #define FRAGMENT_PROGRAM 0x01
186 #define VERTEX_PROGRAM 0x02
188 /* program section */
190 #define INSTRUCTION 0x02
191 #define DECLARATION 0x03
194 /* GL_ARB_fragment_program option */
195 #define ARB_PRECISION_HINT_FASTEST 0x00
196 #define ARB_PRECISION_HINT_NICEST 0x01
197 #define ARB_FOG_EXP 0x02
198 #define ARB_FOG_EXP2 0x03
199 #define ARB_FOG_LINEAR 0x04
201 /* GL_ARB_vertex_program option */
202 #define ARB_POSITION_INVARIANT 0x05
204 /* GL_ARB_fragment_program_shadow option */
205 #define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
207 /* GL_ARB_draw_buffers option */
208 #define ARB_DRAW_BUFFERS 0x07
210 /* GL_ARB_fragment_program instruction class */
211 #define OP_ALU_INST 0x00
212 #define OP_TEX_INST 0x01
214 /* GL_ARB_vertex_program instruction class */
217 /* GL_ARB_fragment_program instruction type */
218 #define OP_ALU_VECTOR 0x00
219 #define OP_ALU_SCALAR 0x01
220 #define OP_ALU_BINSC 0x02
221 #define OP_ALU_BIN 0x03
222 #define OP_ALU_TRI 0x04
223 #define OP_ALU_SWZ 0x05
224 #define OP_TEX_SAMPLE 0x06
225 #define OP_TEX_KIL 0x07
227 /* GL_ARB_vertex_program instruction type */
228 #define OP_ALU_ARL 0x08
236 /* GL_ARB_fragment_program instruction code */
238 #define OP_ABS_SAT 0x1B
240 #define OP_FLR_SAT 0x26
242 #define OP_FRC_SAT 0x27
244 #define OP_LIT_SAT 0x2A
246 #define OP_MOV_SAT 0x30
248 #define OP_COS_SAT 0x20
250 #define OP_EX2_SAT 0x25
252 #define OP_LG2_SAT 0x29
254 #define OP_RCP_SAT 0x33
256 #define OP_RSQ_SAT 0x34
258 #define OP_SIN_SAT 0x39
260 #define OP_SCS_SAT 0x36
262 #define OP_POW_SAT 0x32
264 #define OP_ADD_SAT 0x1C
266 #define OP_DP3_SAT 0x21
268 #define OP_DP4_SAT 0x22
270 #define OP_DPH_SAT 0x23
272 #define OP_DST_SAT 0x24
274 #define OP_MAX_SAT 0x2E
276 #define OP_MIN_SAT 0x2F
278 #define OP_MUL_SAT 0x31
280 #define OP_SGE_SAT 0x37
282 #define OP_SLT_SAT 0x3A
284 #define OP_SUB_SAT 0x3B
286 #define OP_XPD_SAT 0x43
288 #define OP_CMP_SAT 0x1E
290 #define OP_LRP_SAT 0x2C
292 #define OP_MAD_SAT 0x2D
294 #define OP_SWZ_SAT 0x3C
296 #define OP_TEX_SAT 0x3E
298 #define OP_TXB_SAT 0x40
300 #define OP_TXP_SAT 0x42
303 /* GL_ARB_vertex_program instruction code */
332 /* fragment attribute binding */
333 #define FRAGMENT_ATTRIB_COLOR 0x01
334 #define FRAGMENT_ATTRIB_TEXCOORD 0x02
335 #define FRAGMENT_ATTRIB_FOGCOORD 0x03
336 #define FRAGMENT_ATTRIB_POSITION 0x04
338 /* vertex attribute binding */
339 #define VERTEX_ATTRIB_POSITION 0x01
340 #define VERTEX_ATTRIB_WEIGHT 0x02
341 #define VERTEX_ATTRIB_NORMAL 0x03
342 #define VERTEX_ATTRIB_COLOR 0x04
343 #define VERTEX_ATTRIB_FOGCOORD 0x05
344 #define VERTEX_ATTRIB_TEXCOORD 0x06
345 #define VERTEX_ATTRIB_MATRIXINDEX 0x07
346 #define VERTEX_ATTRIB_GENERIC 0x08
348 /* fragment result binding */
349 #define FRAGMENT_RESULT_COLOR 0x01
350 #define FRAGMENT_RESULT_DEPTH 0x02
352 /* vertex result binding */
353 #define VERTEX_RESULT_POSITION 0x01
354 #define VERTEX_RESULT_COLOR 0x02
355 #define VERTEX_RESULT_FOGCOORD 0x03
356 #define VERTEX_RESULT_POINTSIZE 0x04
357 #define VERTEX_RESULT_TEXCOORD 0x05
360 #define TEXTARGET_1D 0x01
361 #define TEXTARGET_2D 0x02
362 #define TEXTARGET_3D 0x03
363 #define TEXTARGET_RECT 0x04
364 #define TEXTARGET_CUBE 0x05
365 /* GL_ARB_fragment_program_shadow */
366 #define TEXTARGET_SHADOW1D 0x06
367 #define TEXTARGET_SHADOW2D 0x07
368 #define TEXTARGET_SHADOWRECT 0x08
371 #define FACE_FRONT 0x00
372 #define FACE_BACK 0x01
375 #define COLOR_PRIMARY 0x00
376 #define COLOR_SECONDARY 0x01
379 #define COMPONENT_X 0x00
380 #define COMPONENT_Y 0x01
381 #define COMPONENT_Z 0x02
382 #define COMPONENT_W 0x03
383 #define COMPONENT_0 0x04
384 #define COMPONENT_1 0x05
386 /* array index type */
387 #define ARRAY_INDEX_ABSOLUTE 0x00
388 #define ARRAY_INDEX_RELATIVE 0x01
391 #define MATRIX_MODELVIEW 0x01
392 #define MATRIX_PROJECTION 0x02
393 #define MATRIX_MVP 0x03
394 #define MATRIX_TEXTURE 0x04
395 #define MATRIX_PALETTE 0x05
396 #define MATRIX_PROGRAM 0x06
398 /* matrix modifier */
399 #define MATRIX_MODIFIER_IDENTITY 0x00
400 #define MATRIX_MODIFIER_INVERSE 0x01
401 #define MATRIX_MODIFIER_TRANSPOSE 0x02
402 #define MATRIX_MODIFIER_INVTRANS 0x03
405 #define CONSTANT_SCALAR 0x01
406 #define CONSTANT_VECTOR 0x02
408 /* program param type */
409 #define PROGRAM_PARAM_ENV 0x01
410 #define PROGRAM_PARAM_LOCAL 0x02
413 #define REGISTER_ATTRIB 0x01
414 #define REGISTER_PARAM 0x02
415 #define REGISTER_RESULT 0x03
416 #define REGISTER_ESTABLISHED_NAME 0x04
419 #define PARAM_NULL 0x00
420 #define PARAM_ARRAY_ELEMENT 0x01
421 #define PARAM_STATE_ELEMENT 0x02
422 #define PARAM_PROGRAM_ELEMENT 0x03
423 #define PARAM_PROGRAM_ELEMENTS 0x04
424 #define PARAM_CONSTANT 0x05
426 /* param state property */
427 #define STATE_MATERIAL_PARSER 0x01
428 #define STATE_LIGHT_PARSER 0x02
429 #define STATE_LIGHT_MODEL 0x03
430 #define STATE_LIGHT_PROD 0x04
431 #define STATE_FOG 0x05
432 #define STATE_MATRIX_ROWS 0x06
433 /* GL_ARB_fragment_program */
434 #define STATE_TEX_ENV 0x07
435 #define STATE_DEPTH 0x08
436 /* GL_ARB_vertex_program */
437 #define STATE_TEX_GEN 0x09
438 #define STATE_CLIP_PLANE 0x0A
439 #define STATE_POINT 0x0B
441 /* state material property */
442 #define MATERIAL_AMBIENT 0x01
443 #define MATERIAL_DIFFUSE 0x02
444 #define MATERIAL_SPECULAR 0x03
445 #define MATERIAL_EMISSION 0x04
446 #define MATERIAL_SHININESS 0x05
448 /* state light property */
449 #define LIGHT_AMBIENT 0x01
450 #define LIGHT_DIFFUSE 0x02
451 #define LIGHT_SPECULAR 0x03
452 #define LIGHT_POSITION 0x04
453 #define LIGHT_ATTENUATION 0x05
454 #define LIGHT_HALF 0x06
455 #define LIGHT_SPOT_DIRECTION 0x07
457 /* state light model property */
458 #define LIGHT_MODEL_AMBIENT 0x01
459 #define LIGHT_MODEL_SCENECOLOR 0x02
461 /* state light product property */
462 #define LIGHT_PROD_AMBIENT 0x01
463 #define LIGHT_PROD_DIFFUSE 0x02
464 #define LIGHT_PROD_SPECULAR 0x03
466 /* state texture environment property */
467 #define TEX_ENV_COLOR 0x01
469 /* state texture generation coord property */
470 #define TEX_GEN_EYE 0x01
471 #define TEX_GEN_OBJECT 0x02
473 /* state fog property */
474 #define FOG_COLOR 0x01
475 #define FOG_PARAMS 0x02
477 /* state depth property */
478 #define DEPTH_RANGE 0x01
480 /* state point parameters property */
481 #define POINT_SIZE 0x01
482 #define POINT_ATTENUATION 0x02
490 /* GL_ARB_vertex_program */
493 /*-----------------------------------------------------------------------
494 * From here on down is the semantic checking portion
499 * Variable Table Handling functions
514 * Setting an explicit field for each of the binding properties is a bit
515 * wasteful of space, but it should be much more clear when reading later on..
519 const GLubyte
*name
; /* don't free() - no need */
521 GLuint address_binding
; /* The index of the address register we should
523 GLuint attrib_binding
; /* For type vt_attrib, see nvfragprog.h for values */
524 GLuint attrib_is_generic
; /* If the attrib was specified through a generic
526 GLuint temp_binding
; /* The index of the temp register we are to use */
527 GLuint output_binding
; /* Output/result register number */
528 struct var_cache
*alias_binding
; /* For type vt_alias, points to the var_cache entry
529 * that this is aliased to */
530 GLuint param_binding_type
; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
531 * PROGRAM_ENV_PARAM} */
532 GLuint param_binding_begin
; /* This is the offset into the program_parameter_list where
533 * the tokens representing our bound state (or constants)
535 GLuint param_binding_length
; /* This is how many entries in the the program_parameter_list
536 * we take up with our state tokens or constants. Note that
537 * this is _not_ the same as the number of param registers
538 * we eventually use */
539 struct var_cache
*next
;
543 var_cache_create (struct var_cache
**va
)
545 *va
= (struct var_cache
*) _mesa_malloc (sizeof (struct var_cache
));
548 (**va
).type
= vt_none
;
549 (**va
).attrib_binding
= ~0;
550 (**va
).attrib_is_generic
= 0;
551 (**va
).temp_binding
= ~0;
552 (**va
).output_binding
= ~0;
553 (**va
).param_binding_type
= ~0;
554 (**va
).param_binding_begin
= ~0;
555 (**va
).param_binding_length
= ~0;
556 (**va
).alias_binding
= NULL
;
562 var_cache_destroy (struct var_cache
**va
)
565 var_cache_destroy (&(**va
).next
);
572 var_cache_append (struct var_cache
**va
, struct var_cache
*nv
)
575 var_cache_append (&(**va
).next
, nv
);
580 static struct var_cache
*
581 var_cache_find (struct var_cache
*va
, const GLubyte
* name
)
583 /*struct var_cache *first = va;*/
586 if (!_mesa_strcmp ( (const char*) name
, (const char*) va
->name
)) {
587 if (va
->type
== vt_alias
)
588 return va
->alias_binding
;
601 * Called when an error is detected while parsing/compiling a program.
602 * Sets the ctx->Program.ErrorString field to descript and records a
603 * GL_INVALID_OPERATION error.
604 * \param position position of error in program string
605 * \param descrip verbose error description
608 program_error(GLcontext
*ctx
, GLint position
, const char *descrip
)
611 const char *prefix
= "glProgramString(", *suffix
= ")";
612 char *str
= (char *) _mesa_malloc(_mesa_strlen(descrip
) +
613 _mesa_strlen(prefix
) +
614 _mesa_strlen(suffix
) + 1);
616 _mesa_sprintf(str
, "%s%s%s", prefix
, descrip
, suffix
);
617 _mesa_error(ctx
, GL_INVALID_OPERATION
, str
);
621 _mesa_set_program_error(ctx
, position
, descrip
);
627 * constructs an integer from 4 GLubytes in LE format
630 parse_position (const GLubyte
** inst
)
634 value
= (GLuint
) (*(*inst
)++);
635 value
+= (GLuint
) (*(*inst
)++) * 0x100;
636 value
+= (GLuint
) (*(*inst
)++) * 0x10000;
637 value
+= (GLuint
) (*(*inst
)++) * 0x1000000;
643 * This will, given a string, lookup the string as a variable name in the
644 * var cache. If the name is found, the var cache node corresponding to the
645 * var name is returned. If it is not found, a new entry is allocated
647 * \param I Points into the binary array where the string identifier begins
648 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
649 * \return The location on the var_cache corresponding the the string starting at I
651 static struct var_cache
*
652 parse_string (const GLubyte
** inst
, struct var_cache
**vc_head
,
653 struct arb_program
*Program
, GLuint
* found
)
655 const GLubyte
*i
= *inst
;
656 struct var_cache
*va
= NULL
;
659 *inst
+= _mesa_strlen ((char *) i
) + 1;
661 va
= var_cache_find (*vc_head
, i
);
669 var_cache_create (&va
);
670 va
->name
= (const GLubyte
*) i
;
672 var_cache_append (vc_head
, va
);
678 parse_string_without_adding (const GLubyte
** inst
, struct arb_program
*Program
)
680 const GLubyte
*i
= *inst
;
683 *inst
+= _mesa_strlen ((char *) i
) + 1;
689 * \return -1 if we parse '-', return 1 otherwise
692 parse_sign (const GLubyte
** inst
)
694 /*return *(*inst)++ != '+'; */
700 else if (**inst
== '+') {
709 * parses and returns signed integer
712 parse_integer (const GLubyte
** inst
, struct arb_program
*Program
)
717 /* check if *inst points to '+' or '-'
718 * if yes, grab the sign and increment *inst
720 sign
= parse_sign (inst
);
722 /* now check if *inst points to 0
723 * if yes, increment the *inst and return the default value
730 /* parse the integer as you normally would do it */
731 value
= _mesa_atoi (parse_string_without_adding (inst
, Program
));
733 /* now, after terminating 0 there is a position
734 * to parse it - parse_position()
736 Program
->Position
= parse_position (inst
);
742 Accumulate this string of digits, and return them as
743 a large integer represented in floating point (for range).
744 If scale is not NULL, also accumulates a power-of-ten
745 integer scale factor that represents the number of digits
749 parse_float_string(const GLubyte
** inst
, struct arb_program
*Program
, GLdouble
*scale
)
751 GLdouble value
= 0.0;
752 GLdouble oscale
= 1.0;
754 if (**inst
== 0) { /* this string of digits is empty-- do nothing */
757 else { /* nonempty string-- parse out the digits */
758 while (**inst
>= '0' && **inst
<= '9') {
759 GLubyte digit
= *((*inst
)++);
760 value
= value
* 10.0 + (GLint
) (digit
- '0');
763 assert(**inst
== 0); /* integer string should end with 0 */
764 (*inst
)++; /* skip over terminating 0 */
765 Program
->Position
= parse_position(inst
); /* skip position (from integer) */
773 Parse an unsigned floating-point number from this stream of tokenized
774 characters. Example floating-point formats supported:
782 parse_float (const GLubyte
** inst
, struct arb_program
*Program
)
785 GLdouble whole
, fraction
, fracScale
= 1.0;
787 whole
= parse_float_string(inst
, Program
, 0);
788 fraction
= parse_float_string(inst
, Program
, &fracScale
);
790 /* Parse signed exponent */
791 exponent
= parse_integer(inst
, Program
); /* This is the exponent */
793 /* Assemble parts of floating-point number: */
794 return (GLfloat
) ((whole
+ fraction
/ fracScale
) *
795 _mesa_pow(10.0, (GLfloat
) exponent
));
802 parse_signed_float (const GLubyte
** inst
, struct arb_program
*Program
)
804 GLint sign
= parse_sign (inst
);
805 GLfloat value
= parse_float (inst
, Program
);
810 * This picks out a constant value from the parsed array. The constant vector is r
811 * returned in the *values array, which should be of length 4.
813 * \param values - The 4 component vector with the constant value in it
816 parse_constant (const GLubyte
** inst
, GLfloat
*values
, struct arb_program
*Program
,
819 GLuint components
, i
;
822 switch (*(*inst
)++) {
823 case CONSTANT_SCALAR
:
824 if (use
== GL_TRUE
) {
827 values
[2] = values
[3] = parse_float (inst
, Program
);
832 values
[2] = values
[3] = parse_signed_float (inst
, Program
);
836 case CONSTANT_VECTOR
:
837 values
[0] = values
[1] = values
[2] = 0;
839 components
= *(*inst
)++;
840 for (i
= 0; i
< components
; i
++) {
841 values
[i
] = parse_signed_float (inst
, Program
);
848 * \param offset The offset from the address register that we should
851 * \return 0 on sucess, 1 on error
854 parse_relative_offset(GLcontext
*ctx
, const GLubyte
**inst
,
855 struct arb_program
*Program
, GLint
*offset
)
858 *offset
= parse_integer(inst
, Program
);
863 * \param color 0 if color type is primary, 1 if color type is secondary
864 * \return 0 on sucess, 1 on error
867 parse_color_type (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
870 (void) ctx
; (void) Program
;
871 *color
= *(*inst
)++ != COLOR_PRIMARY
;
876 * Get an integer corresponding to a generic vertex attribute.
878 * \return 0 on sucess, 1 on error
881 parse_generic_attrib_num(GLcontext
*ctx
, const GLubyte
** inst
,
882 struct arb_program
*Program
, GLuint
*attrib
)
884 GLint i
= parse_integer(inst
, Program
);
886 if ((i
< 0) || (i
>= MAX_VERTEX_PROGRAM_ATTRIBS
))
888 program_error(ctx
, Program
->Position
,
889 "Invalid generic vertex attribute index");
893 *attrib
= (GLuint
) i
;
900 * \param color The index of the color buffer to write into
901 * \return 0 on sucess, 1 on error
904 parse_output_color_num (GLcontext
* ctx
, const GLubyte
** inst
,
905 struct arb_program
*Program
, GLuint
* color
)
907 GLint i
= parse_integer (inst
, Program
);
909 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxDrawBuffers
)) {
910 program_error(ctx
, Program
->Position
, "Invalid draw buffer index");
920 * \param coord The texture unit index
921 * \return 0 on sucess, 1 on error
924 parse_texcoord_num (GLcontext
* ctx
, const GLubyte
** inst
,
925 struct arb_program
*Program
, GLuint
* coord
)
927 GLint i
= parse_integer (inst
, Program
);
929 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxTextureUnits
)) {
930 program_error(ctx
, Program
->Position
, "Invalid texture unit index");
939 * \param coord The weight index
940 * \return 0 on sucess, 1 on error
943 parse_weight_num (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
946 *coord
= parse_integer (inst
, Program
);
948 if ((*coord
< 0) || (*coord
>= 1)) {
949 program_error(ctx
, Program
->Position
, "Invalid weight index");
957 * \param coord The clip plane index
958 * \return 0 on sucess, 1 on error
961 parse_clipplane_num (GLcontext
* ctx
, const GLubyte
** inst
,
962 struct arb_program
*Program
, GLint
* coord
)
964 *coord
= parse_integer (inst
, Program
);
966 if ((*coord
< 0) || (*coord
>= (GLint
) ctx
->Const
.MaxClipPlanes
)) {
967 program_error(ctx
, Program
->Position
, "Invalid clip plane index");
976 * \return 0 on front face, 1 on back face
979 parse_face_type (const GLubyte
** inst
)
981 switch (*(*inst
)++) {
993 * Given a matrix and a modifier token on the binary array, return tokens
994 * that _mesa_fetch_state() [program.c] can understand.
996 * \param matrix - the matrix we are talking about
997 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
998 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
999 * \return 0 on sucess, 1 on failure
1002 parse_matrix (GLcontext
* ctx
, const GLubyte
** inst
, struct arb_program
*Program
,
1003 GLint
* matrix
, GLint
* matrix_idx
, GLint
* matrix_modifier
)
1005 GLubyte mat
= *(*inst
)++;
1010 case MATRIX_MODELVIEW
:
1011 *matrix
= STATE_MODELVIEW
;
1012 *matrix_idx
= parse_integer (inst
, Program
);
1013 if (*matrix_idx
> 0) {
1014 program_error(ctx
, Program
->Position
,
1015 "ARB_vertex_blend not supported");
1020 case MATRIX_PROJECTION
:
1021 *matrix
= STATE_PROJECTION
;
1025 *matrix
= STATE_MVP
;
1028 case MATRIX_TEXTURE
:
1029 *matrix
= STATE_TEXTURE
;
1030 *matrix_idx
= parse_integer (inst
, Program
);
1031 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxTextureUnits
) {
1032 program_error(ctx
, Program
->Position
, "Invalid Texture Unit");
1033 /* bad *matrix_id */
1038 /* This is not currently supported (ARB_matrix_palette) */
1039 case MATRIX_PALETTE
:
1040 *matrix_idx
= parse_integer (inst
, Program
);
1041 program_error(ctx
, Program
->Position
,
1042 "ARB_matrix_palette not supported");
1046 case MATRIX_PROGRAM
:
1047 *matrix
= STATE_PROGRAM
;
1048 *matrix_idx
= parse_integer (inst
, Program
);
1049 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxProgramMatrices
) {
1050 program_error(ctx
, Program
->Position
, "Invalid Program Matrix");
1051 /* bad *matrix_idx */
1057 switch (*(*inst
)++) {
1058 case MATRIX_MODIFIER_IDENTITY
:
1059 *matrix_modifier
= 0;
1061 case MATRIX_MODIFIER_INVERSE
:
1062 *matrix_modifier
= STATE_MATRIX_INVERSE
;
1064 case MATRIX_MODIFIER_TRANSPOSE
:
1065 *matrix_modifier
= STATE_MATRIX_TRANSPOSE
;
1067 case MATRIX_MODIFIER_INVTRANS
:
1068 *matrix_modifier
= STATE_MATRIX_INVTRANS
;
1077 * This parses a state string (rather, the binary version of it) into
1078 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1080 * \param inst - the start in the binary arry to start working from
1081 * \param state_tokens - the storage for the 6-token state description
1082 * \return - 0 on sucess, 1 on error
1085 parse_state_single_item (GLcontext
* ctx
, const GLubyte
** inst
,
1086 struct arb_program
*Program
, GLint
* state_tokens
)
1088 switch (*(*inst
)++) {
1089 case STATE_MATERIAL_PARSER
:
1090 state_tokens
[0] = STATE_MATERIAL
;
1091 state_tokens
[1] = parse_face_type (inst
);
1092 switch (*(*inst
)++) {
1093 case MATERIAL_AMBIENT
:
1094 state_tokens
[2] = STATE_AMBIENT
;
1096 case MATERIAL_DIFFUSE
:
1097 state_tokens
[2] = STATE_DIFFUSE
;
1099 case MATERIAL_SPECULAR
:
1100 state_tokens
[2] = STATE_SPECULAR
;
1102 case MATERIAL_EMISSION
:
1103 state_tokens
[2] = STATE_EMISSION
;
1105 case MATERIAL_SHININESS
:
1106 state_tokens
[2] = STATE_SHININESS
;
1111 case STATE_LIGHT_PARSER
:
1112 state_tokens
[0] = STATE_LIGHT
;
1113 state_tokens
[1] = parse_integer (inst
, Program
);
1115 /* Check the value of state_tokens[1] against the # of lights */
1116 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1117 program_error(ctx
, Program
->Position
, "Invalid Light Number");
1118 /* bad state_tokens[1] */
1122 switch (*(*inst
)++) {
1124 state_tokens
[2] = STATE_AMBIENT
;
1127 state_tokens
[2] = STATE_DIFFUSE
;
1129 case LIGHT_SPECULAR
:
1130 state_tokens
[2] = STATE_SPECULAR
;
1132 case LIGHT_POSITION
:
1133 state_tokens
[2] = STATE_POSITION
;
1135 case LIGHT_ATTENUATION
:
1136 state_tokens
[2] = STATE_ATTENUATION
;
1139 state_tokens
[2] = STATE_HALF
;
1141 case LIGHT_SPOT_DIRECTION
:
1142 state_tokens
[2] = STATE_SPOT_DIRECTION
;
1147 case STATE_LIGHT_MODEL
:
1148 switch (*(*inst
)++) {
1149 case LIGHT_MODEL_AMBIENT
:
1150 state_tokens
[0] = STATE_LIGHTMODEL_AMBIENT
;
1152 case LIGHT_MODEL_SCENECOLOR
:
1153 state_tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
1154 state_tokens
[1] = parse_face_type (inst
);
1159 case STATE_LIGHT_PROD
:
1160 state_tokens
[0] = STATE_LIGHTPROD
;
1161 state_tokens
[1] = parse_integer (inst
, Program
);
1163 /* Check the value of state_tokens[1] against the # of lights */
1164 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1165 program_error(ctx
, Program
->Position
, "Invalid Light Number");
1166 /* bad state_tokens[1] */
1170 state_tokens
[2] = parse_face_type (inst
);
1171 switch (*(*inst
)++) {
1172 case LIGHT_PROD_AMBIENT
:
1173 state_tokens
[3] = STATE_AMBIENT
;
1175 case LIGHT_PROD_DIFFUSE
:
1176 state_tokens
[3] = STATE_DIFFUSE
;
1178 case LIGHT_PROD_SPECULAR
:
1179 state_tokens
[3] = STATE_SPECULAR
;
1186 switch (*(*inst
)++) {
1188 state_tokens
[0] = STATE_FOG_COLOR
;
1191 state_tokens
[0] = STATE_FOG_PARAMS
;
1197 state_tokens
[1] = parse_integer (inst
, Program
);
1198 switch (*(*inst
)++) {
1200 state_tokens
[0] = STATE_TEXENV_COLOR
;
1209 state_tokens
[0] = STATE_TEXGEN
;
1210 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1212 if (parse_texcoord_num (ctx
, inst
, Program
, &coord
))
1214 state_tokens
[1] = coord
;
1219 /* 0 - s, 1 - t, 2 - r, 3 - q */
1222 if (type
== TEX_GEN_EYE
) {
1225 state_tokens
[2] = STATE_TEXGEN_EYE_S
;
1228 state_tokens
[2] = STATE_TEXGEN_EYE_T
;
1231 state_tokens
[2] = STATE_TEXGEN_EYE_R
;
1234 state_tokens
[2] = STATE_TEXGEN_EYE_Q
;
1241 state_tokens
[2] = STATE_TEXGEN_OBJECT_S
;
1244 state_tokens
[2] = STATE_TEXGEN_OBJECT_T
;
1247 state_tokens
[2] = STATE_TEXGEN_OBJECT_R
;
1250 state_tokens
[2] = STATE_TEXGEN_OBJECT_Q
;
1258 switch (*(*inst
)++) {
1260 state_tokens
[0] = STATE_DEPTH_RANGE
;
1265 case STATE_CLIP_PLANE
:
1266 state_tokens
[0] = STATE_CLIPPLANE
;
1267 state_tokens
[1] = parse_integer (inst
, Program
);
1268 if (parse_clipplane_num (ctx
, inst
, Program
, &state_tokens
[1]))
1273 switch (*(*inst
++)) {
1275 state_tokens
[0] = STATE_POINT_SIZE
;
1278 case POINT_ATTENUATION
:
1279 state_tokens
[0] = STATE_POINT_ATTENUATION
;
1284 /* XXX: I think this is the correct format for a matrix row */
1285 case STATE_MATRIX_ROWS
:
1286 state_tokens
[0] = STATE_MATRIX
;
1288 (ctx
, inst
, Program
, &state_tokens
[1], &state_tokens
[2],
1292 state_tokens
[3] = parse_integer (inst
, Program
); /* The first row to grab */
1294 if ((**inst
) != 0) { /* Either the last row, 0 */
1295 state_tokens
[4] = parse_integer (inst
, Program
);
1296 if (state_tokens
[4] < state_tokens
[3]) {
1297 program_error(ctx
, Program
->Position
,
1298 "Second matrix index less than the first");
1299 /* state_tokens[4] vs. state_tokens[3] */
1304 state_tokens
[4] = state_tokens
[3];
1314 * This parses a state string (rather, the binary version of it) into
1315 * a 6-token similar for the state fetching code in program.c
1317 * One might ask, why fetch these parameters into just like you fetch
1318 * state when they are already stored in other places?
1320 * Because of array offsets -> We can stick env/local parameters in the
1321 * middle of a parameter array and then index someplace into the array
1324 * One optimization might be to only do this for the cases where the
1325 * env/local parameters end up inside of an array, and leave the
1326 * single parameters (or arrays of pure env/local pareameters) in their
1327 * respective register files.
1329 * For ENV parameters, the format is:
1330 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1331 * state_tokens[1] = STATE_ENV
1332 * state_tokens[2] = the parameter index
1334 * for LOCAL parameters, the format is:
1335 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1336 * state_tokens[1] = STATE_LOCAL
1337 * state_tokens[2] = the parameter index
1339 * \param inst - the start in the binary arry to start working from
1340 * \param state_tokens - the storage for the 6-token state description
1341 * \return - 0 on sucess, 1 on failure
1344 parse_program_single_item (GLcontext
* ctx
, const GLubyte
** inst
,
1345 struct arb_program
*Program
, GLint
* state_tokens
)
1347 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1348 state_tokens
[0] = STATE_FRAGMENT_PROGRAM
;
1350 state_tokens
[0] = STATE_VERTEX_PROGRAM
;
1353 switch (*(*inst
)++) {
1354 case PROGRAM_PARAM_ENV
:
1355 state_tokens
[1] = STATE_ENV
;
1356 state_tokens
[2] = parse_integer (inst
, Program
);
1358 /* Check state_tokens[2] against the number of ENV parameters available */
1359 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1360 (state_tokens
[2] >= (GLint
) ctx
->Const
.FragmentProgram
.MaxEnvParams
))
1362 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1363 (state_tokens
[2] >= (GLint
) ctx
->Const
.VertexProgram
.MaxEnvParams
))) {
1364 program_error(ctx
, Program
->Position
,
1365 "Invalid Program Env Parameter");
1366 /* bad state_tokens[2] */
1372 case PROGRAM_PARAM_LOCAL
:
1373 state_tokens
[1] = STATE_LOCAL
;
1374 state_tokens
[2] = parse_integer (inst
, Program
);
1376 /* Check state_tokens[2] against the number of LOCAL parameters available */
1377 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1378 (state_tokens
[2] >= (GLint
) ctx
->Const
.FragmentProgram
.MaxLocalParams
))
1380 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1381 (state_tokens
[2] >= (GLint
) ctx
->Const
.VertexProgram
.MaxLocalParams
))) {
1382 program_error(ctx
, Program
->Position
,
1383 "Invalid Program Local Parameter");
1384 /* bad state_tokens[2] */
1394 * For ARB_vertex_program, programs are not allowed to use both an explicit
1395 * vertex attribute and a generic vertex attribute corresponding to the same
1396 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1398 * This will walk our var_cache and make sure that nobody does anything fishy.
1400 * \return 0 on sucess, 1 on error
1403 generic_attrib_check(struct var_cache
*vc_head
)
1406 struct var_cache
*curr
;
1407 GLboolean explicitAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
],
1408 genericAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
];
1410 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1411 explicitAttrib
[a
] = GL_FALSE
;
1412 genericAttrib
[a
] = GL_FALSE
;
1417 if (curr
->type
== vt_attrib
) {
1418 if (curr
->attrib_is_generic
)
1419 genericAttrib
[ curr
->attrib_binding
] = GL_TRUE
;
1421 explicitAttrib
[ curr
->attrib_binding
] = GL_TRUE
;
1427 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1428 if ((explicitAttrib
[a
]) && (genericAttrib
[a
]))
1436 * This will handle the binding side of an ATTRIB var declaration
1438 * \param inputReg returns the input register index, one of the
1439 * VERT_ATTRIB_* or FRAG_ATTRIB_* values.
1440 * \return returns 0 on success, 1 on error
1443 parse_attrib_binding(GLcontext
* ctx
, const GLubyte
** inst
,
1444 struct arb_program
*Program
,
1445 GLuint
*inputReg
, GLuint
*is_generic
)
1451 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1452 switch (*(*inst
)++) {
1453 case FRAGMENT_ATTRIB_COLOR
:
1456 err
= parse_color_type (ctx
, inst
, Program
, &coord
);
1457 *inputReg
= FRAG_ATTRIB_COL0
+ coord
;
1460 case FRAGMENT_ATTRIB_TEXCOORD
:
1463 err
= parse_texcoord_num (ctx
, inst
, Program
, &texcoord
);
1464 *inputReg
= FRAG_ATTRIB_TEX0
+ texcoord
;
1467 case FRAGMENT_ATTRIB_FOGCOORD
:
1468 *inputReg
= FRAG_ATTRIB_FOGC
;
1470 case FRAGMENT_ATTRIB_POSITION
:
1471 *inputReg
= FRAG_ATTRIB_WPOS
;
1479 switch (*(*inst
)++) {
1480 case VERTEX_ATTRIB_POSITION
:
1481 *inputReg
= VERT_ATTRIB_POS
;
1484 case VERTEX_ATTRIB_WEIGHT
:
1487 err
= parse_weight_num (ctx
, inst
, Program
, &weight
);
1488 *inputReg
= VERT_ATTRIB_WEIGHT
;
1490 /* hack for Warcraft (see bug 8060) */
1491 _mesa_warning(ctx
, "Application error: vertex program uses 'vertex.weight' but GL_ARB_vertex_blend not supported.");
1494 program_error(ctx
, Program
->Position
,
1495 "ARB_vertex_blend not supported");
1500 case VERTEX_ATTRIB_NORMAL
:
1501 *inputReg
= VERT_ATTRIB_NORMAL
;
1504 case VERTEX_ATTRIB_COLOR
:
1507 err
= parse_color_type (ctx
, inst
, Program
, &color
);
1509 *inputReg
= VERT_ATTRIB_COLOR1
;
1512 *inputReg
= VERT_ATTRIB_COLOR0
;
1517 case VERTEX_ATTRIB_FOGCOORD
:
1518 *inputReg
= VERT_ATTRIB_FOG
;
1521 case VERTEX_ATTRIB_TEXCOORD
:
1524 err
= parse_texcoord_num (ctx
, inst
, Program
, &unit
);
1525 *inputReg
= VERT_ATTRIB_TEX0
+ unit
;
1529 case VERTEX_ATTRIB_MATRIXINDEX
:
1530 /* Not supported at this time */
1532 const char *msg
= "ARB_palette_matrix not supported";
1533 parse_integer (inst
, Program
);
1534 program_error(ctx
, Program
->Position
, msg
);
1538 case VERTEX_ATTRIB_GENERIC
:
1541 err
= parse_generic_attrib_num(ctx
, inst
, Program
, &attrib
);
1544 /* Add VERT_ATTRIB_GENERIC0 here because ARB_vertex_program's
1545 * attributes do not alias the conventional vertex
1549 *inputReg
= attrib
+ VERT_ATTRIB_GENERIC0
;
1563 program_error(ctx
, Program
->Position
, "Bad attribute binding");
1566 Program
->Base
.InputsRead
|= (1 << *inputReg
);
1573 * This translates between a binary token for an output variable type
1574 * and the mesa token for the same thing.
1576 * \param inst The parsed tokens
1577 * \param outputReg Returned index/number of the output register,
1578 * one of the VERT_RESULT_* or FRAG_RESULT_* values.
1581 parse_result_binding(GLcontext
*ctx
, const GLubyte
**inst
,
1582 GLuint
*outputReg
, struct arb_program
*Program
)
1584 const GLubyte token
= *(*inst
)++;
1587 case FRAGMENT_RESULT_COLOR
:
1588 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1591 /* This gets result of the color buffer we're supposed to
1592 * draw into. This pertains to GL_ARB_draw_buffers.
1594 parse_output_color_num(ctx
, inst
, Program
, &out_color
);
1595 ASSERT(out_color
< MAX_DRAW_BUFFERS
);
1596 *outputReg
= FRAG_RESULT_COLR
;
1599 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1600 *outputReg
= VERT_RESULT_HPOS
;
1604 case FRAGMENT_RESULT_DEPTH
:
1605 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1606 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
1607 *outputReg
= FRAG_RESULT_DEPR
;
1610 /* for vtx programs, this is VERTEX_RESULT_COLOR */
1612 GLuint face_type
= parse_face_type(inst
);
1613 GLint err
= parse_color_type(ctx
, inst
, Program
, &color_type
);
1620 *outputReg
= VERT_RESULT_BFC1
; /* secondary color */
1623 *outputReg
= VERT_RESULT_BFC0
; /* primary color */
1629 *outputReg
= VERT_RESULT_COL1
; /* secondary color */
1633 *outputReg
= VERT_RESULT_COL0
; /* primary color */
1639 case VERTEX_RESULT_FOGCOORD
:
1640 *outputReg
= VERT_RESULT_FOGC
;
1643 case VERTEX_RESULT_POINTSIZE
:
1644 *outputReg
= VERT_RESULT_PSIZ
;
1647 case VERTEX_RESULT_TEXCOORD
:
1650 if (parse_texcoord_num (ctx
, inst
, Program
, &unit
))
1652 *outputReg
= VERT_RESULT_TEX0
+ unit
;
1657 Program
->Base
.OutputsWritten
|= (1 << *outputReg
);
1664 * This handles the declaration of ATTRIB variables
1667 * parse_vert_attrib_binding(), or something like that
1669 * \return 0 on sucess, 1 on error
1672 parse_attrib (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1673 struct arb_program
*Program
)
1677 struct var_cache
*attrib_var
;
1679 attrib_var
= parse_string (inst
, vc_head
, Program
, &found
);
1680 Program
->Position
= parse_position (inst
);
1682 error_msg
= (char *)
1683 _mesa_malloc (_mesa_strlen ((char *) attrib_var
->name
) + 40);
1684 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
1686 program_error(ctx
, Program
->Position
, error_msg
);
1687 _mesa_free (error_msg
);
1691 attrib_var
->type
= vt_attrib
;
1693 if (parse_attrib_binding(ctx
, inst
, Program
, &attrib_var
->attrib_binding
,
1694 &attrib_var
->attrib_is_generic
))
1697 if (generic_attrib_check(*vc_head
)) {
1698 program_error(ctx
, Program
->Position
,
1699 "Cannot use both a generic vertex attribute "
1700 "and a specific attribute of the same type");
1704 Program
->Base
.NumAttributes
++;
1709 * \param use -- TRUE if we're called when declaring implicit parameters,
1710 * FALSE if we're declaraing variables. This has to do with
1711 * if we get a signed or unsigned float for scalar constants
1714 parse_param_elements (GLcontext
* ctx
, const GLubyte
** inst
,
1715 struct var_cache
*param_var
,
1716 struct arb_program
*Program
, GLboolean use
)
1720 GLint state_tokens
[6];
1721 GLfloat const_values
[4];
1723 switch (*(*inst
)++) {
1724 case PARAM_STATE_ELEMENT
:
1725 if (parse_state_single_item (ctx
, inst
, Program
, state_tokens
))
1728 /* If we adding STATE_MATRIX that has multiple rows, we need to
1729 * unroll it and call _mesa_add_state_reference() for each row
1731 if ((state_tokens
[0] == STATE_MATRIX
)
1732 && (state_tokens
[3] != state_tokens
[4])) {
1734 GLint first_row
= state_tokens
[3];
1735 GLint last_row
= state_tokens
[4];
1737 for (row
= first_row
; row
<= last_row
; row
++) {
1738 state_tokens
[3] = state_tokens
[4] = row
;
1740 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1742 if (param_var
->param_binding_begin
== ~0U)
1743 param_var
->param_binding_begin
= idx
;
1744 param_var
->param_binding_length
++;
1745 Program
->Base
.NumParameters
++;
1749 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1751 if (param_var
->param_binding_begin
== ~0U)
1752 param_var
->param_binding_begin
= idx
;
1753 param_var
->param_binding_length
++;
1754 Program
->Base
.NumParameters
++;
1758 case PARAM_PROGRAM_ELEMENT
:
1759 if (parse_program_single_item (ctx
, inst
, Program
, state_tokens
))
1761 idx
= _mesa_add_state_reference (Program
->Base
.Parameters
, state_tokens
);
1762 if (param_var
->param_binding_begin
== ~0U)
1763 param_var
->param_binding_begin
= idx
;
1764 param_var
->param_binding_length
++;
1765 Program
->Base
.NumParameters
++;
1767 /* Check if there is more: 0 -> we're done, else its an integer */
1769 GLuint out_of_range
, new_idx
;
1770 GLuint start_idx
= state_tokens
[2] + 1;
1771 GLuint end_idx
= parse_integer (inst
, Program
);
1774 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1775 if (((state_tokens
[1] == STATE_ENV
)
1776 && (end_idx
>= ctx
->Const
.FragmentProgram
.MaxEnvParams
))
1777 || ((state_tokens
[1] == STATE_LOCAL
)
1779 ctx
->Const
.FragmentProgram
.MaxLocalParams
)))
1783 if (((state_tokens
[1] == STATE_ENV
)
1784 && (end_idx
>= ctx
->Const
.VertexProgram
.MaxEnvParams
))
1785 || ((state_tokens
[1] == STATE_LOCAL
)
1787 ctx
->Const
.VertexProgram
.MaxLocalParams
)))
1791 program_error(ctx
, Program
->Position
,
1792 "Invalid Program Parameter"); /*end_idx*/
1796 for (new_idx
= start_idx
; new_idx
<= end_idx
; new_idx
++) {
1797 state_tokens
[2] = new_idx
;
1798 idx
= _mesa_add_state_reference(Program
->Base
.Parameters
,
1800 param_var
->param_binding_length
++;
1801 Program
->Base
.NumParameters
++;
1809 case PARAM_CONSTANT
:
1810 parse_constant (inst
, const_values
, Program
, use
);
1811 idx
= _mesa_add_named_constant(Program
->Base
.Parameters
,
1812 (char *) param_var
->name
,
1814 if (param_var
->param_binding_begin
== ~0U)
1815 param_var
->param_binding_begin
= idx
;
1816 param_var
->param_binding_length
++;
1817 Program
->Base
.NumParameters
++;
1821 program_error(ctx
, Program
->Position
,
1822 "Unexpected token (in parse_param_elements())");
1826 /* Make sure we haven't blown past our parameter limits */
1827 if (((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1828 (Program
->Base
.NumParameters
>=
1829 ctx
->Const
.VertexProgram
.MaxLocalParams
))
1830 || ((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1831 && (Program
->Base
.NumParameters
>=
1832 ctx
->Const
.FragmentProgram
.MaxLocalParams
))) {
1833 program_error(ctx
, Program
->Position
, "Too many parameter variables");
1842 * This picks out PARAM program parameter bindings.
1844 * XXX: This needs to be stressed & tested
1846 * \return 0 on sucess, 1 on error
1849 parse_param (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1850 struct arb_program
*Program
)
1853 GLint specified_length
;
1854 struct var_cache
*param_var
;
1857 param_var
= parse_string (inst
, vc_head
, Program
, &found
);
1858 Program
->Position
= parse_position (inst
);
1861 char *error_msg
= (char *)
1862 _mesa_malloc (_mesa_strlen ((char *) param_var
->name
) + 40);
1863 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
1865 program_error (ctx
, Program
->Position
, error_msg
);
1866 _mesa_free (error_msg
);
1870 specified_length
= parse_integer (inst
, Program
);
1872 if (specified_length
< 0) {
1873 program_error(ctx
, Program
->Position
, "Negative parameter array length");
1877 param_var
->type
= vt_param
;
1878 param_var
->param_binding_length
= 0;
1880 /* Right now, everything is shoved into the main state register file.
1882 * In the future, it would be nice to leave things ENV/LOCAL params
1883 * in their respective register files, if possible
1885 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1888 * * - add each guy to the parameter list
1889 * * - increment the param_var->param_binding_len
1890 * * - store the param_var->param_binding_begin for the first one
1891 * * - compare the actual len to the specified len at the end
1893 while (**inst
!= PARAM_NULL
) {
1894 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_FALSE
))
1898 /* Test array length here! */
1899 if (specified_length
) {
1900 if (specified_length
!= (int)param_var
->param_binding_length
) {
1901 program_error(ctx
, Program
->Position
,
1902 "Declared parameter array length does not match parameter list");
1915 parse_param_use (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1916 struct arb_program
*Program
, struct var_cache
**new_var
)
1918 struct var_cache
*param_var
;
1920 /* First, insert a dummy entry into the var_cache */
1921 var_cache_create (¶m_var
);
1922 param_var
->name
= (const GLubyte
*) " ";
1923 param_var
->type
= vt_param
;
1925 param_var
->param_binding_length
= 0;
1926 /* Don't fill in binding_begin; We use the default value of -1
1927 * to tell if its already initialized, elsewhere.
1929 * param_var->param_binding_begin = 0;
1931 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1933 var_cache_append (vc_head
, param_var
);
1935 /* Then fill it with juicy parameter goodness */
1936 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_TRUE
))
1939 *new_var
= param_var
;
1946 * This handles the declaration of TEMP variables
1948 * \return 0 on sucess, 1 on error
1951 parse_temp (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1952 struct arb_program
*Program
)
1955 struct var_cache
*temp_var
;
1957 while (**inst
!= 0) {
1958 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
1959 Program
->Position
= parse_position (inst
);
1961 char *error_msg
= (char *)
1962 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
1963 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
1965 program_error(ctx
, Program
->Position
, error_msg
);
1966 _mesa_free (error_msg
);
1970 temp_var
->type
= vt_temp
;
1972 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1973 (Program
->Base
.NumTemporaries
>=
1974 ctx
->Const
.FragmentProgram
.MaxTemps
))
1975 || ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
1976 && (Program
->Base
.NumTemporaries
>=
1977 ctx
->Const
.VertexProgram
.MaxTemps
))) {
1978 program_error(ctx
, Program
->Position
,
1979 "Too many TEMP variables declared");
1983 temp_var
->temp_binding
= Program
->Base
.NumTemporaries
;
1984 Program
->Base
.NumTemporaries
++;
1992 * This handles variables of the OUTPUT variety
1994 * \return 0 on sucess, 1 on error
1997 parse_output (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
1998 struct arb_program
*Program
)
2001 struct var_cache
*output_var
;
2004 output_var
= parse_string (inst
, vc_head
, Program
, &found
);
2005 Program
->Position
= parse_position (inst
);
2007 char *error_msg
= (char *)
2008 _mesa_malloc (_mesa_strlen ((char *) output_var
->name
) + 40);
2009 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
2011 program_error (ctx
, Program
->Position
, error_msg
);
2012 _mesa_free (error_msg
);
2016 output_var
->type
= vt_output
;
2018 err
= parse_result_binding(ctx
, inst
, &output_var
->output_binding
, Program
);
2023 * This handles variables of the ALIAS kind
2025 * \return 0 on sucess, 1 on error
2028 parse_alias (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2029 struct arb_program
*Program
)
2032 struct var_cache
*temp_var
;
2034 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2035 Program
->Position
= parse_position (inst
);
2038 char *error_msg
= (char *)
2039 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2040 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
2042 program_error(ctx
, Program
->Position
, error_msg
);
2043 _mesa_free (error_msg
);
2047 temp_var
->type
= vt_alias
;
2048 temp_var
->alias_binding
= parse_string (inst
, vc_head
, Program
, &found
);
2049 Program
->Position
= parse_position (inst
);
2053 char *error_msg
= (char *)
2054 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2055 _mesa_sprintf (error_msg
, "Alias value %s is not defined",
2056 temp_var
->alias_binding
->name
);
2057 program_error (ctx
, Program
->Position
, error_msg
);
2058 _mesa_free (error_msg
);
2066 * This handles variables of the ADDRESS kind
2068 * \return 0 on sucess, 1 on error
2071 parse_address (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2072 struct arb_program
*Program
)
2075 struct var_cache
*temp_var
;
2077 while (**inst
!= 0) {
2078 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2079 Program
->Position
= parse_position (inst
);
2081 char *error_msg
= (char *)
2082 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2083 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
2085 program_error (ctx
, Program
->Position
, error_msg
);
2086 _mesa_free (error_msg
);
2090 temp_var
->type
= vt_address
;
2092 if (Program
->Base
.NumAddressRegs
>=
2093 ctx
->Const
.VertexProgram
.MaxAddressRegs
) {
2094 const char *msg
= "Too many ADDRESS variables declared";
2095 program_error(ctx
, Program
->Position
, msg
);
2099 temp_var
->address_binding
= Program
->Base
.NumAddressRegs
;
2100 Program
->Base
.NumAddressRegs
++;
2108 * Parse a program declaration
2110 * \return 0 on sucess, 1 on error
2113 parse_declaration (GLcontext
* ctx
, const GLubyte
** inst
, struct var_cache
**vc_head
,
2114 struct arb_program
*Program
)
2118 switch (*(*inst
)++) {
2120 err
= parse_address (ctx
, inst
, vc_head
, Program
);
2124 err
= parse_alias (ctx
, inst
, vc_head
, Program
);
2128 err
= parse_attrib (ctx
, inst
, vc_head
, Program
);
2132 err
= parse_output (ctx
, inst
, vc_head
, Program
);
2136 err
= parse_param (ctx
, inst
, vc_head
, Program
);
2140 err
= parse_temp (ctx
, inst
, vc_head
, Program
);
2148 * Handle the parsing out of a masked destination register, either for a
2149 * vertex or fragment program.
2151 * If we are a vertex program, make sure we don't write to
2152 * result.position if we have specified that the program is
2153 * position invariant
2155 * \param File - The register file we write to
2156 * \param Index - The register index we write to
2157 * \param WriteMask - The mask controlling which components we write (1->write)
2159 * \return 0 on sucess, 1 on error
2162 parse_masked_dst_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2163 struct var_cache
**vc_head
, struct arb_program
*Program
,
2164 enum register_file
*File
, GLuint
*Index
, GLint
*WriteMask
)
2167 struct var_cache
*dst
;
2169 /* We either have a result register specified, or a
2170 * variable that may or may not be writable
2172 switch (*(*inst
)++) {
2173 case REGISTER_RESULT
:
2174 if (parse_result_binding(ctx
, inst
, Index
, Program
))
2176 *File
= PROGRAM_OUTPUT
;
2179 case REGISTER_ESTABLISHED_NAME
:
2180 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2181 Program
->Position
= parse_position (inst
);
2183 /* If the name has never been added to our symbol table, we're hosed */
2185 program_error(ctx
, Program
->Position
, "0: Undefined variable");
2189 switch (dst
->type
) {
2191 *File
= PROGRAM_OUTPUT
;
2192 *Index
= dst
->output_binding
;
2196 *File
= PROGRAM_TEMPORARY
;
2197 *Index
= dst
->temp_binding
;
2200 /* If the var type is not vt_output or vt_temp, no go */
2202 program_error(ctx
, Program
->Position
,
2203 "Destination register is read only");
2209 program_error(ctx
, Program
->Position
,
2210 "Unexpected opcode in parse_masked_dst_reg()");
2215 /* Position invariance test */
2216 /* This test is done now in syntax portion - when position invariance OPTION
2217 is specified, "result.position" rule is disabled so there is no way
2218 to write the position
2220 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2222 program_error(ctx, Program->Position,
2223 "Vertex program specified position invariance and wrote vertex position");
2226 /* And then the mask.
2232 * ==> Need to reverse the order of bits for this!
2234 tmp
= (GLint
) *(*inst
)++;
2235 *WriteMask
= (((tmp
>>3) & 0x1) |
2245 * Handle the parsing of a address register
2247 * \param Index - The register index we write to
2249 * \return 0 on sucess, 1 on error
2252 parse_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2253 struct var_cache
**vc_head
,
2254 struct arb_program
*Program
, GLint
* Index
)
2256 struct var_cache
*dst
;
2259 *Index
= 0; /* XXX */
2261 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2262 Program
->Position
= parse_position (inst
);
2264 /* If the name has never been added to our symbol table, we're hosed */
2266 program_error(ctx
, Program
->Position
, "Undefined variable");
2270 if (dst
->type
!= vt_address
) {
2271 program_error(ctx
, Program
->Position
, "Variable is not of type ADDRESS");
2280 * Handle the parsing out of a masked address register
2282 * \param Index - The register index we write to
2283 * \param WriteMask - The mask controlling which components we write (1->write)
2285 * \return 0 on sucess, 1 on error
2288 parse_masked_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2289 struct var_cache
**vc_head
,
2290 struct arb_program
*Program
, GLint
* Index
,
2291 GLboolean
* WriteMask
)
2293 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, Index
))
2296 /* This should be 0x8 */
2299 /* Writemask of .x is implied */
2301 WriteMask
[1] = WriteMask
[2] = WriteMask
[3] = 0;
2308 * Parse out a swizzle mask.
2310 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
2312 * The len parameter allows us to grab 4 components for a vector
2313 * swizzle, or just 1 component for a scalar src register selection
2316 parse_swizzle_mask(const GLubyte
** inst
, GLubyte
*swizzle
, GLint len
)
2320 for (i
= 0; i
< 4; i
++)
2323 for (i
= 0; i
< len
; i
++) {
2324 switch (*(*inst
)++) {
2326 swizzle
[i
] = SWIZZLE_X
;
2329 swizzle
[i
] = SWIZZLE_Y
;
2332 swizzle
[i
] = SWIZZLE_Z
;
2335 swizzle
[i
] = SWIZZLE_W
;
2338 _mesa_problem(NULL
, "bad component in parse_swizzle_mask()");
2346 * Parse an extended swizzle mask which is a sequence of
2347 * four x/y/z/w/0/1 tokens.
2348 * \return swizzle four swizzle values
2349 * \return negateMask four element bitfield
2352 parse_extended_swizzle_mask(const GLubyte
**inst
, GLubyte swizzle
[4],
2353 GLubyte
*negateMask
)
2358 for (i
= 0; i
< 4; i
++) {
2360 if (parse_sign(inst
) == -1)
2361 *negateMask
|= (1 << i
);
2367 swizzle
[i
] = SWIZZLE_ZERO
;
2370 swizzle
[i
] = SWIZZLE_ONE
;
2373 swizzle
[i
] = SWIZZLE_X
;
2376 swizzle
[i
] = SWIZZLE_Y
;
2379 swizzle
[i
] = SWIZZLE_Z
;
2382 swizzle
[i
] = SWIZZLE_W
;
2385 _mesa_problem(NULL
, "bad case in parse_extended_swizzle_mask()");
2393 parse_src_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2394 struct var_cache
**vc_head
,
2395 struct arb_program
*Program
,
2396 enum register_file
* File
, GLint
* Index
,
2397 GLboolean
*IsRelOffset
)
2399 struct var_cache
*src
;
2400 GLuint binding
, is_generic
, found
;
2405 /* And the binding for the src */
2406 switch (*(*inst
)++) {
2407 case REGISTER_ATTRIB
:
2408 if (parse_attrib_binding
2409 (ctx
, inst
, Program
, &binding
, &is_generic
))
2411 *File
= PROGRAM_INPUT
;
2414 /* We need to insert a dummy variable into the var_cache so we can
2415 * catch generic vertex attrib aliasing errors
2417 var_cache_create(&src
);
2418 src
->type
= vt_attrib
;
2419 src
->name
= (const GLubyte
*) "Dummy Attrib Variable";
2420 src
->attrib_binding
= binding
;
2421 src
->attrib_is_generic
= is_generic
;
2422 var_cache_append(vc_head
, src
);
2423 if (generic_attrib_check(*vc_head
)) {
2424 program_error(ctx
, Program
->Position
,
2425 "Cannot use both a generic vertex attribute "
2426 "and a specific attribute of the same type");
2431 case REGISTER_PARAM
:
2433 case PARAM_ARRAY_ELEMENT
:
2435 src
= parse_string (inst
, vc_head
, Program
, &found
);
2436 Program
->Position
= parse_position (inst
);
2439 program_error(ctx
, Program
->Position
,
2440 "2: Undefined variable"); /* src->name */
2444 *File
= (enum register_file
) src
->param_binding_type
;
2446 switch (*(*inst
)++) {
2447 case ARRAY_INDEX_ABSOLUTE
:
2448 offset
= parse_integer (inst
, Program
);
2451 || (offset
>= (int)src
->param_binding_length
)) {
2452 program_error(ctx
, Program
->Position
,
2453 "Index out of range");
2454 /* offset, src->name */
2458 *Index
= src
->param_binding_begin
+ offset
;
2461 case ARRAY_INDEX_RELATIVE
:
2463 GLint addr_reg_idx
, rel_off
;
2465 /* First, grab the address regiseter */
2466 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &addr_reg_idx
))
2475 /* Then the relative offset */
2476 if (parse_relative_offset(ctx
, inst
, Program
, &rel_off
)) return 1;
2478 /* And store it properly */
2479 *Index
= src
->param_binding_begin
+ rel_off
;
2487 if (parse_param_use (ctx
, inst
, vc_head
, Program
, &src
))
2490 *File
= (enum register_file
) src
->param_binding_type
;
2491 *Index
= src
->param_binding_begin
;
2496 case REGISTER_ESTABLISHED_NAME
:
2497 src
= parse_string (inst
, vc_head
, Program
, &found
);
2498 Program
->Position
= parse_position (inst
);
2500 /* If the name has never been added to our symbol table, we're hosed */
2502 program_error(ctx
, Program
->Position
,
2503 "3: Undefined variable"); /* src->name */
2507 switch (src
->type
) {
2509 *File
= PROGRAM_INPUT
;
2510 *Index
= src
->attrib_binding
;
2513 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2515 *File
= (enum register_file
) src
->param_binding_type
;
2516 *Index
= src
->param_binding_begin
;
2520 *File
= PROGRAM_TEMPORARY
;
2521 *Index
= src
->temp_binding
;
2524 /* If the var type is vt_output no go */
2526 program_error(ctx
, Program
->Position
,
2527 "destination register is read only");
2534 program_error(ctx
, Program
->Position
,
2535 "Unknown token in parse_src_reg");
2543 * Parse fragment program vector source register.
2546 parse_fp_vector_src_reg(GLcontext
* ctx
, const GLubyte
** inst
,
2547 struct var_cache
**vc_head
,
2548 struct arb_program
*program
,
2549 struct prog_src_register
*reg
)
2551 enum register_file file
;
2555 GLboolean isRelOffset
;
2558 negate
= (parse_sign (inst
) == -1) ? 0xf : 0x0;
2560 /* And the src reg */
2561 if (parse_src_reg(ctx
, inst
, vc_head
, program
, &file
, &index
, &isRelOffset
))
2564 /* finally, the swizzle */
2565 parse_swizzle_mask(inst
, swizzle
, 4);
2569 reg
->NegateBase
= negate
;
2570 reg
->Swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
2576 * Parse fragment program destination register.
2577 * \return 1 if error, 0 if no error.
2580 parse_fp_dst_reg(GLcontext
* ctx
, const GLubyte
** inst
,
2581 struct var_cache
**vc_head
, struct arb_program
*Program
,
2582 struct prog_dst_register
*reg
)
2586 enum register_file file
;
2588 if (parse_masked_dst_reg (ctx
, inst
, vc_head
, Program
, &file
, &idx
, &mask
))
2593 reg
->WriteMask
= mask
;
2599 * Parse fragment program scalar src register.
2600 * \return 1 if error, 0 if no error.
2603 parse_fp_scalar_src_reg (GLcontext
* ctx
, const GLubyte
** inst
,
2604 struct var_cache
**vc_head
,
2605 struct arb_program
*Program
,
2606 struct prog_src_register
*reg
)
2608 enum register_file File
;
2612 GLboolean IsRelOffset
;
2615 Negate
= (parse_sign (inst
) == -1) ? 0x1 : 0x0;
2617 /* And the src reg */
2618 if (parse_src_reg (ctx
, inst
, vc_head
, Program
, &File
, &Index
, &IsRelOffset
))
2621 /* finally, the swizzle */
2622 parse_swizzle_mask(inst
, Swizzle
, 1);
2626 reg
->NegateBase
= Negate
;
2627 reg
->Swizzle
= (Swizzle
[0] << 0);
2634 * This is a big mother that handles getting opcodes into the instruction
2635 * and handling the src & dst registers for fragment program instructions
2636 * \return 1 if error, 0 if no error
2639 parse_fp_instruction (GLcontext
* ctx
, const GLubyte
** inst
,
2640 struct var_cache
**vc_head
, struct arb_program
*Program
,
2641 struct prog_instruction
*fp
)
2645 GLubyte instClass
, type
, code
;
2648 _mesa_init_instructions(fp
, 1);
2650 /* Record the position in the program string for debugging */
2651 fp
->StringPos
= Program
->Position
;
2653 /* OP_ALU_INST or OP_TEX_INST */
2654 instClass
= *(*inst
)++;
2656 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2657 * OP_TEX_{SAMPLE, KIL}
2661 /* The actual opcode name */
2664 /* Increment the correct count */
2665 switch (instClass
) {
2667 Program
->NumAluInstructions
++;
2670 Program
->NumTexInstructions
++;
2678 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2680 fp
->Opcode
= OPCODE_ABS
;
2684 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2686 fp
->Opcode
= OPCODE_FLR
;
2690 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2692 fp
->Opcode
= OPCODE_FRC
;
2696 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2698 fp
->Opcode
= OPCODE_LIT
;
2702 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2704 fp
->Opcode
= OPCODE_MOV
;
2708 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2711 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2718 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2720 fp
->Opcode
= OPCODE_COS
;
2724 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2726 fp
->Opcode
= OPCODE_EX2
;
2730 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2732 fp
->Opcode
= OPCODE_LG2
;
2736 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2738 fp
->Opcode
= OPCODE_RCP
;
2742 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2744 fp
->Opcode
= OPCODE_RSQ
;
2748 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2750 fp
->Opcode
= OPCODE_SIN
;
2754 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2757 fp
->Opcode
= OPCODE_SCS
;
2761 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2764 if (parse_fp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2771 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2773 fp
->Opcode
= OPCODE_POW
;
2777 if (parse_fp_dst_reg(ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2780 for (a
= 0; a
< 2; a
++) {
2781 if (parse_fp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2790 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2792 fp
->Opcode
= OPCODE_ADD
;
2796 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2798 fp
->Opcode
= OPCODE_DP3
;
2802 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2804 fp
->Opcode
= OPCODE_DP4
;
2808 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2810 fp
->Opcode
= OPCODE_DPH
;
2814 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2816 fp
->Opcode
= OPCODE_DST
;
2820 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2822 fp
->Opcode
= OPCODE_MAX
;
2826 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2828 fp
->Opcode
= OPCODE_MIN
;
2832 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2834 fp
->Opcode
= OPCODE_MUL
;
2838 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2840 fp
->Opcode
= OPCODE_SGE
;
2844 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2846 fp
->Opcode
= OPCODE_SLT
;
2850 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2852 fp
->Opcode
= OPCODE_SUB
;
2856 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2858 fp
->Opcode
= OPCODE_XPD
;
2862 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2864 for (a
= 0; a
< 2; a
++) {
2865 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2873 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2875 fp
->Opcode
= OPCODE_CMP
;
2879 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2881 fp
->Opcode
= OPCODE_LRP
;
2885 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2887 fp
->Opcode
= OPCODE_MAD
;
2891 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2894 for (a
= 0; a
< 3; a
++) {
2895 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2903 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2905 fp
->Opcode
= OPCODE_SWZ
;
2908 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2914 enum register_file file
;
2917 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
, &rel
))
2919 parse_extended_swizzle_mask(inst
, swizzle
, &negateMask
);
2920 fp
->SrcReg
[0].File
= file
;
2921 fp
->SrcReg
[0].Index
= index
;
2922 fp
->SrcReg
[0].NegateBase
= negateMask
;
2923 fp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
2933 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2935 fp
->Opcode
= OPCODE_TEX
;
2939 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2941 fp
->Opcode
= OPCODE_TXP
;
2945 fp
->SaturateMode
= SATURATE_ZERO_ONE
;
2947 fp
->Opcode
= OPCODE_TXB
;
2951 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2954 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2958 if (parse_texcoord_num (ctx
, inst
, Program
, &texcoord
))
2960 fp
->TexSrcUnit
= texcoord
;
2963 switch (*(*inst
)++) {
2965 fp
->TexSrcTarget
= TEXTURE_1D_INDEX
;
2968 fp
->TexSrcTarget
= TEXTURE_2D_INDEX
;
2971 fp
->TexSrcTarget
= TEXTURE_3D_INDEX
;
2973 case TEXTARGET_RECT
:
2974 fp
->TexSrcTarget
= TEXTURE_RECT_INDEX
;
2976 case TEXTARGET_CUBE
:
2977 fp
->TexSrcTarget
= TEXTURE_CUBE_INDEX
;
2979 case TEXTARGET_SHADOW1D
:
2980 case TEXTARGET_SHADOW2D
:
2981 case TEXTARGET_SHADOWRECT
:
2982 /* TODO ARB_fragment_program_shadow code */
2985 Program
->TexturesUsed
[texcoord
] |= (1 << fp
->TexSrcTarget
);
2986 /* Check that both "2D" and "CUBE" (for example) aren't both used */
2987 if (_mesa_bitcount(Program
->TexturesUsed
[texcoord
]) > 1) {
2988 program_error(ctx
, Program
->Position
,
2989 "multiple targets used on one texture image unit");
2995 Program
->UsesKill
= 1;
2996 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2998 fp
->Opcode
= OPCODE_KIL
;
3001 _mesa_problem(ctx
, "bad type 0x%x in parse_fp_instruction()", type
);
3009 parse_vp_dst_reg(GLcontext
* ctx
, const GLubyte
** inst
,
3010 struct var_cache
**vc_head
, struct arb_program
*Program
,
3011 struct prog_dst_register
*reg
)
3015 enum register_file file
;
3017 if (parse_masked_dst_reg(ctx
, inst
, vc_head
, Program
, &file
, &idx
, &mask
))
3022 reg
->WriteMask
= mask
;
3027 * Handle the parsing out of a masked address register
3029 * \param Index - The register index we write to
3030 * \param WriteMask - The mask controlling which components we write (1->write)
3032 * \return 0 on sucess, 1 on error
3035 parse_vp_address_reg (GLcontext
* ctx
, const GLubyte
** inst
,
3036 struct var_cache
**vc_head
,
3037 struct arb_program
*Program
,
3038 struct prog_dst_register
*reg
)
3042 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &idx
))
3045 /* This should be 0x8 */
3048 reg
->File
= PROGRAM_ADDRESS
;
3051 /* Writemask of .x is implied */
3052 reg
->WriteMask
= 0x1;
3057 * Parse vertex program vector source register.
3060 parse_vp_vector_src_reg(GLcontext
* ctx
, const GLubyte
** inst
,
3061 struct var_cache
**vc_head
,
3062 struct arb_program
*program
,
3063 struct prog_src_register
*reg
)
3065 enum register_file file
;
3069 GLboolean isRelOffset
;
3072 negateMask
= (parse_sign (inst
) == -1) ? 0xf : 0x0;
3074 /* And the src reg */
3075 if (parse_src_reg (ctx
, inst
, vc_head
, program
, &file
, &index
, &isRelOffset
))
3078 /* finally, the swizzle */
3079 parse_swizzle_mask(inst
, swizzle
, 4);
3083 reg
->Swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1],
3084 swizzle
[2], swizzle
[3]);
3085 reg
->NegateBase
= negateMask
;
3086 reg
->RelAddr
= isRelOffset
;
3092 parse_vp_scalar_src_reg (GLcontext
* ctx
, const GLubyte
** inst
,
3093 struct var_cache
**vc_head
,
3094 struct arb_program
*Program
,
3095 struct prog_src_register
*reg
)
3097 enum register_file File
;
3101 GLboolean IsRelOffset
;
3104 Negate
= (parse_sign (inst
) == -1) ? 0x1 : 0x0;
3106 /* And the src reg */
3107 if (parse_src_reg (ctx
, inst
, vc_head
, Program
, &File
, &Index
, &IsRelOffset
))
3110 /* finally, the swizzle */
3111 parse_swizzle_mask(inst
, Swizzle
, 1);
3115 reg
->Swizzle
= (Swizzle
[0] << 0);
3116 reg
->NegateBase
= Negate
;
3117 reg
->RelAddr
= IsRelOffset
;
3123 * This is a big mother that handles getting opcodes into the instruction
3124 * and handling the src & dst registers for vertex program instructions
3127 parse_vp_instruction (GLcontext
* ctx
, const GLubyte
** inst
,
3128 struct var_cache
**vc_head
, struct arb_program
*Program
,
3129 struct prog_instruction
*vp
)
3134 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3137 /* The actual opcode name */
3140 _mesa_init_instructions(vp
, 1);
3141 /* Record the position in the program string for debugging */
3142 vp
->StringPos
= Program
->Position
;
3147 vp
->Opcode
= OPCODE_ARL
;
3149 /* Remember to set SrcReg.RelAddr; */
3151 /* Get the masked address register [dst] */
3152 if (parse_vp_address_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3155 vp
->DstReg
.File
= PROGRAM_ADDRESS
;
3157 /* Get a scalar src register */
3158 if (parse_vp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3166 vp
->Opcode
= OPCODE_ABS
;
3169 vp
->Opcode
= OPCODE_FLR
;
3172 vp
->Opcode
= OPCODE_FRC
;
3175 vp
->Opcode
= OPCODE_LIT
;
3178 vp
->Opcode
= OPCODE_MOV
;
3182 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3185 if (parse_vp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3192 vp
->Opcode
= OPCODE_EX2
;
3195 vp
->Opcode
= OPCODE_EXP
;
3198 vp
->Opcode
= OPCODE_LG2
;
3201 vp
->Opcode
= OPCODE_LOG
;
3204 vp
->Opcode
= OPCODE_RCP
;
3207 vp
->Opcode
= OPCODE_RSQ
;
3210 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3213 if (parse_vp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3220 vp
->Opcode
= OPCODE_POW
;
3223 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3226 for (a
= 0; a
< 2; a
++) {
3227 if (parse_vp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3235 vp
->Opcode
= OPCODE_ADD
;
3238 vp
->Opcode
= OPCODE_DP3
;
3241 vp
->Opcode
= OPCODE_DP4
;
3244 vp
->Opcode
= OPCODE_DPH
;
3247 vp
->Opcode
= OPCODE_DST
;
3250 vp
->Opcode
= OPCODE_MAX
;
3253 vp
->Opcode
= OPCODE_MIN
;
3256 vp
->Opcode
= OPCODE_MUL
;
3259 vp
->Opcode
= OPCODE_SGE
;
3262 vp
->Opcode
= OPCODE_SLT
;
3265 vp
->Opcode
= OPCODE_SUB
;
3268 vp
->Opcode
= OPCODE_XPD
;
3271 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3274 for (a
= 0; a
< 2; a
++) {
3275 if (parse_vp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3283 vp
->Opcode
= OPCODE_MAD
;
3287 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3290 for (a
= 0; a
< 3; a
++) {
3291 if (parse_vp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3299 vp
->Opcode
= OPCODE_SWZ
;
3306 enum register_file file
;
3309 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3312 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
, &relAddr
))
3314 parse_extended_swizzle_mask (inst
, swizzle
, &negateMask
);
3315 vp
->SrcReg
[0].File
= file
;
3316 vp
->SrcReg
[0].Index
= index
;
3317 vp
->SrcReg
[0].NegateBase
= negateMask
;
3318 vp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
3322 vp
->SrcReg
[0].RelAddr
= relAddr
;
3332 print_state_token (GLint token
)
3335 case STATE_MATERIAL
:
3336 fprintf (stderr
, "STATE_MATERIAL ");
3339 fprintf (stderr
, "STATE_LIGHT ");
3342 case STATE_LIGHTMODEL_AMBIENT
:
3343 fprintf (stderr
, "STATE_AMBIENT ");
3346 case STATE_LIGHTMODEL_SCENECOLOR
:
3347 fprintf (stderr
, "STATE_SCENECOLOR ");
3350 case STATE_LIGHTPROD
:
3351 fprintf (stderr
, "STATE_LIGHTPROD ");
3355 fprintf (stderr
, "STATE_TEXGEN ");
3358 case STATE_FOG_COLOR
:
3359 fprintf (stderr
, "STATE_FOG_COLOR ");
3362 case STATE_FOG_PARAMS
:
3363 fprintf (stderr
, "STATE_FOG_PARAMS ");
3366 case STATE_CLIPPLANE
:
3367 fprintf (stderr
, "STATE_CLIPPLANE ");
3370 case STATE_POINT_SIZE
:
3371 fprintf (stderr
, "STATE_POINT_SIZE ");
3374 case STATE_POINT_ATTENUATION
:
3375 fprintf (stderr
, "STATE_ATTENUATION ");
3379 fprintf (stderr
, "STATE_MATRIX ");
3382 case STATE_MODELVIEW
:
3383 fprintf (stderr
, "STATE_MODELVIEW ");
3386 case STATE_PROJECTION
:
3387 fprintf (stderr
, "STATE_PROJECTION ");
3391 fprintf (stderr
, "STATE_MVP ");
3395 fprintf (stderr
, "STATE_TEXTURE ");
3399 fprintf (stderr
, "STATE_PROGRAM ");
3402 case STATE_MATRIX_INVERSE
:
3403 fprintf (stderr
, "STATE_INVERSE ");
3406 case STATE_MATRIX_TRANSPOSE
:
3407 fprintf (stderr
, "STATE_TRANSPOSE ");
3410 case STATE_MATRIX_INVTRANS
:
3411 fprintf (stderr
, "STATE_INVTRANS ");
3415 fprintf (stderr
, "STATE_AMBIENT ");
3419 fprintf (stderr
, "STATE_DIFFUSE ");
3422 case STATE_SPECULAR
:
3423 fprintf (stderr
, "STATE_SPECULAR ");
3426 case STATE_EMISSION
:
3427 fprintf (stderr
, "STATE_EMISSION ");
3430 case STATE_SHININESS
:
3431 fprintf (stderr
, "STATE_SHININESS ");
3435 fprintf (stderr
, "STATE_HALF ");
3438 case STATE_POSITION
:
3439 fprintf (stderr
, "STATE_POSITION ");
3442 case STATE_ATTENUATION
:
3443 fprintf (stderr
, "STATE_ATTENUATION ");
3446 case STATE_SPOT_DIRECTION
:
3447 fprintf (stderr
, "STATE_DIRECTION ");
3450 case STATE_TEXGEN_EYE_S
:
3451 fprintf (stderr
, "STATE_TEXGEN_EYE_S ");
3454 case STATE_TEXGEN_EYE_T
:
3455 fprintf (stderr
, "STATE_TEXGEN_EYE_T ");
3458 case STATE_TEXGEN_EYE_R
:
3459 fprintf (stderr
, "STATE_TEXGEN_EYE_R ");
3462 case STATE_TEXGEN_EYE_Q
:
3463 fprintf (stderr
, "STATE_TEXGEN_EYE_Q ");
3466 case STATE_TEXGEN_OBJECT_S
:
3467 fprintf (stderr
, "STATE_TEXGEN_EYE_S ");
3470 case STATE_TEXGEN_OBJECT_T
:
3471 fprintf (stderr
, "STATE_TEXGEN_OBJECT_T ");
3474 case STATE_TEXGEN_OBJECT_R
:
3475 fprintf (stderr
, "STATE_TEXGEN_OBJECT_R ");
3478 case STATE_TEXGEN_OBJECT_Q
:
3479 fprintf (stderr
, "STATE_TEXGEN_OBJECT_Q ");
3482 case STATE_TEXENV_COLOR
:
3483 fprintf (stderr
, "STATE_TEXENV_COLOR ");
3486 case STATE_DEPTH_RANGE
:
3487 fprintf (stderr
, "STATE_DEPTH_RANGE ");
3490 case STATE_VERTEX_PROGRAM
:
3491 fprintf (stderr
, "STATE_VERTEX_PROGRAM ");
3494 case STATE_FRAGMENT_PROGRAM
:
3495 fprintf (stderr
, "STATE_FRAGMENT_PROGRAM ");
3499 fprintf (stderr
, "STATE_ENV ");
3503 fprintf (stderr
, "STATE_LOCAL ");
3507 fprintf (stderr
, "[%d] ", token
);
3512 debug_variables (GLcontext
* ctx
, struct var_cache
*vc_head
,
3513 struct arb_program
*Program
)
3515 struct var_cache
*vc
;
3518 fprintf (stderr
, "debug_variables, vc_head: %x\n", vc_head
);
3520 /* First of all, print out the contents of the var_cache */
3523 fprintf (stderr
, "[%x]\n", vc
);
3526 fprintf (stderr
, "UNDEFINED %s\n", vc
->name
);
3529 fprintf (stderr
, "ATTRIB %s\n", vc
->name
);
3530 fprintf (stderr
, " binding: 0x%x\n", vc
->attrib_binding
);
3533 fprintf (stderr
, "PARAM %s begin: %d len: %d\n", vc
->name
,
3534 vc
->param_binding_begin
, vc
->param_binding_length
);
3535 b
= vc
->param_binding_begin
;
3536 for (a
= 0; a
< vc
->param_binding_length
; a
++) {
3537 fprintf (stderr
, "%s\n",
3538 Program
->Parameters
->Parameters
[a
+ b
].Name
);
3539 if (Program
->Parameters
->Parameters
[a
+ b
].Type
== STATE
) {
3540 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3542 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3544 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3546 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3548 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3550 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3554 fprintf (stderr
, "%f %f %f %f\n",
3555 Program
->Parameters
->Parameters
[a
+ b
].Values
[0],
3556 Program
->Parameters
->Parameters
[a
+ b
].Values
[1],
3557 Program
->Parameters
->Parameters
[a
+ b
].Values
[2],
3558 Program
->Parameters
->Parameters
[a
+ b
].Values
[3]);
3562 fprintf (stderr
, "TEMP %s\n", vc
->name
);
3563 fprintf (stderr
, " binding: 0x%x\n", vc
->temp_binding
);
3566 fprintf (stderr
, "OUTPUT %s\n", vc
->name
);
3567 fprintf (stderr
, " binding: 0x%x\n", vc
->output_binding
);
3570 fprintf (stderr
, "ALIAS %s\n", vc
->name
);
3571 fprintf (stderr
, " binding: 0x%x (%s)\n",
3572 vc
->alias_binding
, vc
->alias_binding
->name
);
3579 #endif /* DEBUG_PARSING */
3583 * The main loop for parsing a fragment or vertex program
3585 * \return 1 on error, 0 on success
3588 parse_instructions(GLcontext
* ctx
, const GLubyte
* inst
,
3589 struct var_cache
**vc_head
, struct arb_program
*Program
)
3591 const GLuint maxInst
= (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
3592 ? ctx
->Const
.FragmentProgram
.MaxInstructions
3593 : ctx
->Const
.VertexProgram
.MaxInstructions
;
3596 ASSERT(MAX_INSTRUCTIONS
>= maxInst
);
3598 Program
->MajorVersion
= (GLuint
) * inst
++;
3599 Program
->MinorVersion
= (GLuint
) * inst
++;
3601 while (*inst
!= END
) {
3606 case ARB_PRECISION_HINT_FASTEST
:
3607 Program
->PrecisionOption
= GL_FASTEST
;
3610 case ARB_PRECISION_HINT_NICEST
:
3611 Program
->PrecisionOption
= GL_NICEST
;
3615 Program
->FogOption
= GL_EXP
;
3619 Program
->FogOption
= GL_EXP2
;
3622 case ARB_FOG_LINEAR
:
3623 Program
->FogOption
= GL_LINEAR
;
3626 case ARB_POSITION_INVARIANT
:
3627 if (Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
3628 Program
->HintPositionInvariant
= GL_TRUE
;
3631 case ARB_FRAGMENT_PROGRAM_SHADOW
:
3632 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3633 /* TODO ARB_fragment_program_shadow code */
3637 case ARB_DRAW_BUFFERS
:
3638 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3639 /* do nothing for now */
3647 if (Program
->Base
.NumInstructions
+ 1 >= maxInst
) {
3648 program_error(ctx
, Program
->Position
,
3649 "Max instruction count exceeded");
3652 Program
->Position
= parse_position (&inst
);
3653 /* parse the current instruction */
3654 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3655 err
= parse_fp_instruction (ctx
, &inst
, vc_head
, Program
,
3656 &Program
->Base
.Instructions
[Program
->Base
.NumInstructions
]);
3659 err
= parse_vp_instruction (ctx
, &inst
, vc_head
, Program
,
3660 &Program
->Base
.Instructions
[Program
->Base
.NumInstructions
]);
3663 /* increment instuction count */
3664 Program
->Base
.NumInstructions
++;
3668 err
= parse_declaration (ctx
, &inst
, vc_head
, Program
);
3679 /* Finally, tag on an OPCODE_END instruction */
3681 const GLuint numInst
= Program
->Base
.NumInstructions
;
3682 _mesa_init_instructions(Program
->Base
.Instructions
+ numInst
, 1);
3683 Program
->Base
.Instructions
[numInst
].Opcode
= OPCODE_END
;
3684 /* YYY Wrong Position in program, whatever, at least not random -> crash
3685 Program->Position = parse_position (&inst);
3687 Program
->Base
.Instructions
[numInst
].StringPos
= Program
->Position
;
3689 Program
->Base
.NumInstructions
++;
3692 * Initialize native counts to logical counts. The device driver may
3693 * change them if program is translated into a hardware program.
3695 Program
->Base
.NumNativeInstructions
= Program
->Base
.NumInstructions
;
3696 Program
->Base
.NumNativeTemporaries
= Program
->Base
.NumTemporaries
;
3697 Program
->Base
.NumNativeParameters
= Program
->Base
.NumParameters
;
3698 Program
->Base
.NumNativeAttributes
= Program
->Base
.NumAttributes
;
3699 Program
->Base
.NumNativeAddressRegs
= Program
->Base
.NumAddressRegs
;
3706 LONGSTRING
static char core_grammar_text
[] =
3707 #include "grammar_syn.h"
3712 * Set a grammar parameter.
3713 * \param name the grammar parameter
3714 * \param value the new parameter value
3715 * \return 0 if OK, 1 if error
3718 set_reg8 (GLcontext
*ctx
, grammar id
, const char *name
, GLubyte value
)
3720 char error_msg
[300];
3723 if (grammar_set_reg8 (id
, (const byte
*) name
, value
))
3726 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3727 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3728 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Grammar Register Error");
3734 * Enable support for the given language option in the parser.
3735 * \return 1 if OK, 0 if error
3738 enable_ext(GLcontext
*ctx
, grammar id
, const char *name
)
3740 return !set_reg8(ctx
, id
, name
, 1);
3745 * Enable parser extensions based on which OpenGL extensions are supported
3746 * by this rendering context.
3748 * \return GL_TRUE if OK, GL_FALSE if error.
3751 enable_parser_extensions(GLcontext
*ctx
, grammar id
)
3754 /* These are not supported at this time */
3755 if ((ctx
->Extensions
.ARB_vertex_blend
||
3756 ctx
->Extensions
.EXT_vertex_weighting
)
3757 && !enable_ext(ctx
, id
, "vertex_blend"))
3759 if (ctx
->Extensions
.ARB_matrix_palette
3760 && !enable_ext(ctx
, id
, "matrix_palette"))
3762 if (ctx
->Extensions
.ARB_fragment_program_shadow
3763 && !enable_ext(ctx
, id
, "fragment_program_shadow"))
3766 if (ctx
->Extensions
.EXT_point_parameters
3767 && !enable_ext(ctx
, id
, "point_parameters"))
3769 if (ctx
->Extensions
.EXT_secondary_color
3770 && !enable_ext(ctx
, id
, "secondary_color"))
3772 if (ctx
->Extensions
.EXT_fog_coord
3773 && !enable_ext(ctx
, id
, "fog_coord"))
3775 if (ctx
->Extensions
.NV_texture_rectangle
3776 && !enable_ext(ctx
, id
, "texture_rectangle"))
3778 if (ctx
->Extensions
.ARB_draw_buffers
3779 && !enable_ext(ctx
, id
, "draw_buffers"))
3783 /* hack for Warcraft (see bug 8060) */
3784 enable_ext(ctx
, id
, "vertex_blend");
3792 * This kicks everything off.
3794 * \param ctx - The GL Context
3795 * \param str - The program string
3796 * \param len - The program string length
3797 * \param program - The arb_program struct to return all the parsed info in
3798 * \return GL_TRUE on sucess, GL_FALSE on error
3801 _mesa_parse_arb_program(GLcontext
*ctx
, GLenum target
,
3802 const GLubyte
*str
, GLsizei len
,
3803 struct arb_program
*program
)
3805 GLint a
, err
, error_pos
;
3806 char error_msg
[300];
3808 struct var_cache
*vc_head
;
3809 grammar arbprogram_syn_id
;
3810 GLubyte
*parsed
, *inst
;
3811 GLubyte
*strz
= NULL
;
3812 static int arbprogram_syn_is_ok
= 0; /* XXX temporary */
3814 /* set the program target before parsing */
3815 program
->Base
.Target
= target
;
3817 /* Reset error state */
3818 _mesa_set_program_error(ctx
, -1, NULL
);
3820 /* check if arb_grammar_text (arbprogram.syn) is syntactically correct */
3821 if (!arbprogram_syn_is_ok
) {
3822 /* One-time initialization of parsing system */
3823 grammar grammar_syn_id
;
3826 grammar_syn_id
= grammar_load_from_text ((byte
*) core_grammar_text
);
3827 if (grammar_syn_id
== 0) {
3828 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3829 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3830 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3831 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3832 "glProgramStringARB(Error loading grammar rule set)");
3836 err
= !grammar_check(grammar_syn_id
, (byte
*) arb_grammar_text
,
3837 &parsed
, &parsed_len
);
3839 /* 'parsed' is unused here */
3840 _mesa_free (parsed
);
3843 /* NOTE: we can't destroy grammar_syn_id right here because
3844 * grammar_destroy() can reset the last error
3847 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3848 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3849 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3850 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3851 "glProgramString(Error loading grammar rule set");
3852 grammar_destroy (grammar_syn_id
);
3856 grammar_destroy (grammar_syn_id
);
3858 arbprogram_syn_is_ok
= 1;
3861 /* create the grammar object */
3862 arbprogram_syn_id
= grammar_load_from_text ((byte
*) arb_grammar_text
);
3863 if (arbprogram_syn_id
== 0) {
3864 /* XXX this is not a GL error - it's an implementation bug! - FIX */
3865 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
3866 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3867 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3868 "glProgramString(Error loading grammer rule set)");
3872 /* Set program_target register value */
3873 if (set_reg8 (ctx
, arbprogram_syn_id
, "program_target",
3874 program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
? 0x10 : 0x20)) {
3875 grammar_destroy (arbprogram_syn_id
);
3879 if (!enable_parser_extensions(ctx
, arbprogram_syn_id
)) {
3880 grammar_destroy(arbprogram_syn_id
);
3884 /* check for NULL character occurences */
3887 for (i
= 0; i
< len
; i
++) {
3888 if (str
[i
] == '\0') {
3889 program_error(ctx
, i
, "illegal character");
3890 grammar_destroy (arbprogram_syn_id
);
3896 /* copy the program string to a null-terminated string */
3897 strz
= (GLubyte
*) _mesa_malloc (len
+ 1);
3899 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glProgramStringARB");
3900 grammar_destroy (arbprogram_syn_id
);
3903 _mesa_memcpy (strz
, str
, len
);
3906 /* do a fast check on program string - initial production buffer is 4K */
3907 err
= !grammar_fast_check(arbprogram_syn_id
, strz
,
3908 &parsed
, &parsed_len
, 0x1000);
3910 /* Syntax parse error */
3912 grammar_get_last_error((GLubyte
*) error_msg
, 300, &error_pos
);
3913 program_error(ctx
, error_pos
, error_msg
);
3916 /* useful for debugging */
3920 fprintf(stderr
, "program: %s\n", (char *) strz
);
3921 fprintf(stderr
, "Error Pos: %d\n", ctx
->program
.ErrorPos
);
3922 s
= (char *) _mesa_find_line_column(strz
, strz
+ctx
->program
.ErrorPos
,
3924 fprintf(stderr
, "line %d col %d: %s\n", line
, col
, s
);
3931 grammar_destroy (arbprogram_syn_id
);
3935 grammar_destroy (arbprogram_syn_id
);
3938 * Program string is syntactically correct at this point
3939 * Parse the tokenized version of the program now, generating
3940 * vertex/fragment program instructions.
3943 /* Initialize the arb_program struct */
3944 program
->Base
.String
= strz
;
3945 program
->Base
.Instructions
= _mesa_alloc_instructions(MAX_INSTRUCTIONS
);
3946 program
->Base
.NumInstructions
=
3947 program
->Base
.NumTemporaries
=
3948 program
->Base
.NumParameters
=
3949 program
->Base
.NumAttributes
= program
->Base
.NumAddressRegs
= 0;
3950 program
->Base
.Parameters
= _mesa_new_parameter_list ();
3951 program
->Base
.InputsRead
= 0x0;
3952 program
->Base
.OutputsWritten
= 0x0;
3953 program
->Position
= 0;
3954 program
->MajorVersion
= program
->MinorVersion
= 0;
3955 program
->PrecisionOption
= GL_DONT_CARE
;
3956 program
->FogOption
= GL_NONE
;
3957 program
->HintPositionInvariant
= GL_FALSE
;
3958 for (a
= 0; a
< MAX_TEXTURE_IMAGE_UNITS
; a
++)
3959 program
->TexturesUsed
[a
] = 0x0;
3960 program
->NumAluInstructions
=
3961 program
->NumTexInstructions
=
3962 program
->NumTexIndirections
= 0;
3963 program
->UsesKill
= 0;
3968 /* Start examining the tokens in the array */
3971 /* Check the grammer rev */
3972 if (*inst
++ != REVISION
) {
3973 program_error (ctx
, 0, "Grammar version mismatch");
3977 /* ignore program target */
3979 err
= parse_instructions(ctx
, inst
, &vc_head
, program
);
3982 /*debug_variables(ctx, vc_head, program); */
3984 /* We're done with the parsed binary array */
3985 var_cache_destroy (&vc_head
);
3987 _mesa_free (parsed
);
3989 /* Reallocate the instruction array from size [MAX_INSTRUCTIONS]
3990 * to size [ap.Base.NumInstructions].
3992 program
->Base
.Instructions
3993 = _mesa_realloc_instructions(program
->Base
.Instructions
,
3995 program
->Base
.NumInstructions
);
4003 _mesa_parse_arb_fragment_program(GLcontext
* ctx
, GLenum target
,
4004 const GLvoid
*str
, GLsizei len
,
4005 struct gl_fragment_program
*program
)
4007 struct arb_program ap
;
4010 ASSERT(target
== GL_FRAGMENT_PROGRAM_ARB
);
4011 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
, &ap
)) {
4012 /* Error in the program. Just return. */
4016 /* Copy the relevant contents of the arb_program struct into the
4017 * fragment_program struct.
4019 program
->Base
.String
= ap
.Base
.String
;
4020 program
->Base
.NumInstructions
= ap
.Base
.NumInstructions
;
4021 program
->Base
.NumTemporaries
= ap
.Base
.NumTemporaries
;
4022 program
->Base
.NumParameters
= ap
.Base
.NumParameters
;
4023 program
->Base
.NumAttributes
= ap
.Base
.NumAttributes
;
4024 program
->Base
.NumAddressRegs
= ap
.Base
.NumAddressRegs
;
4025 program
->Base
.NumNativeInstructions
= ap
.Base
.NumNativeInstructions
;
4026 program
->Base
.NumNativeTemporaries
= ap
.Base
.NumNativeTemporaries
;
4027 program
->Base
.NumNativeParameters
= ap
.Base
.NumNativeParameters
;
4028 program
->Base
.NumNativeAttributes
= ap
.Base
.NumNativeAttributes
;
4029 program
->Base
.NumNativeAddressRegs
= ap
.Base
.NumNativeAddressRegs
;
4030 program
->NumAluInstructions
= ap
.NumAluInstructions
;
4031 program
->NumTexInstructions
= ap
.NumTexInstructions
;
4032 program
->NumTexIndirections
= ap
.NumTexIndirections
;
4033 program
->NumNativeAluInstructions
= ap
.NumAluInstructions
;
4034 program
->NumNativeTexInstructions
= ap
.NumTexInstructions
;
4035 program
->NumNativeTexIndirections
= ap
.NumTexIndirections
;
4036 program
->Base
.InputsRead
= ap
.Base
.InputsRead
;
4037 program
->Base
.OutputsWritten
= ap
.Base
.OutputsWritten
;
4038 for (i
= 0; i
< MAX_TEXTURE_IMAGE_UNITS
; i
++)
4039 program
->TexturesUsed
[i
] = ap
.TexturesUsed
[i
];
4040 program
->FogOption
= ap
.FogOption
;
4041 program
->UsesKill
= ap
.UsesKill
;
4043 if (program
->Base
.Instructions
)
4044 _mesa_free(program
->Base
.Instructions
);
4045 program
->Base
.Instructions
= ap
.Base
.Instructions
;
4047 if (program
->Base
.Parameters
)
4048 _mesa_free_parameter_list(program
->Base
.Parameters
);
4049 program
->Base
.Parameters
= ap
.Base
.Parameters
;
4052 _mesa_printf("____________Fragment program %u ________\n", program
->Base
.ID
);
4053 _mesa_print_program(&program
->Base
);
4060 * Parse the vertex program string. If success, update the given
4061 * vertex_program object with the new program. Else, leave the vertex_program
4065 _mesa_parse_arb_vertex_program(GLcontext
*ctx
, GLenum target
,
4066 const GLvoid
*str
, GLsizei len
,
4067 struct gl_vertex_program
*program
)
4069 struct arb_program ap
;
4071 ASSERT(target
== GL_VERTEX_PROGRAM_ARB
);
4073 if (!_mesa_parse_arb_program(ctx
, target
, (const GLubyte
*) str
, len
, &ap
)) {
4074 /* Error in the program. Just return. */
4078 /* Copy the relevant contents of the arb_program struct into the
4079 * vertex_program struct.
4081 program
->Base
.String
= ap
.Base
.String
;
4082 program
->Base
.NumInstructions
= ap
.Base
.NumInstructions
;
4083 program
->Base
.NumTemporaries
= ap
.Base
.NumTemporaries
;
4084 program
->Base
.NumParameters
= ap
.Base
.NumParameters
;
4085 program
->Base
.NumAttributes
= ap
.Base
.NumAttributes
;
4086 program
->Base
.NumAddressRegs
= ap
.Base
.NumAddressRegs
;
4087 program
->Base
.NumNativeInstructions
= ap
.Base
.NumNativeInstructions
;
4088 program
->Base
.NumNativeTemporaries
= ap
.Base
.NumNativeTemporaries
;
4089 program
->Base
.NumNativeParameters
= ap
.Base
.NumNativeParameters
;
4090 program
->Base
.NumNativeAttributes
= ap
.Base
.NumNativeAttributes
;
4091 program
->Base
.NumNativeAddressRegs
= ap
.Base
.NumNativeAddressRegs
;
4092 program
->Base
.InputsRead
= ap
.Base
.InputsRead
;
4093 program
->Base
.OutputsWritten
= ap
.Base
.OutputsWritten
;
4094 program
->IsPositionInvariant
= ap
.HintPositionInvariant
;
4096 if (program
->Base
.Instructions
)
4097 _mesa_free(program
->Base
.Instructions
);
4098 program
->Base
.Instructions
= ap
.Base
.Instructions
;
4100 if (program
->Base
.Parameters
)
4101 _mesa_free_parameter_list(program
->Base
.Parameters
);
4102 program
->Base
.Parameters
= ap
.Base
.Parameters
;
4105 _mesa_printf("____________Vertex program %u __________\n", program
->Base
.Id
);
4106 _mesa_print_program(&program
->Base
);