2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 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
40 #include "nvvertprog.h"
41 #include "nvfragprog.h"
42 #include "arbprogparse.h"
43 #include "grammar_mesa.h"
48 #if !defined(__GNUC__) || (__GNUC__ < 2) || \
49 ((__GNUC__ == 2) && (__GNUC_MINOR__ <= 7))
50 # define __extension__
55 * Fragment Program Stuff:
56 * -----------------------------------------------------
58 * - things from Michal's email
60 * + not-overflowing floats (don't use parse_integer..)
61 * + can remove range checking in arbparse.c
63 * - check all limits of number of various variables
68 * Vertex Program Stuff:
69 * -----------------------------------------------------
70 * - Optimize param array usage and count limits correctly, see spec,
72 * + Record if an array is reference absolutly or relatively (or both)
73 * + For absolute arrays, store a bitmap of accesses
74 * + For single parameters, store an access flag
75 * + After parsing, make a parameter cleanup and merging pass, where
76 * relative arrays are layed out first, followed by abs arrays, and
77 * finally single state.
78 * + Remap offsets for param src and dst registers
79 * + Now we can properly count parameter usage
81 * - Multiple state binding errors in param arrays (see spec, just before
86 * -----------------------------------------------------
87 * - User clipping planes vs. PositionInvariant
88 * - Is it sufficient to just multiply by the mvp to transform in the
89 * PositionInvariant case? Or do we need something more involved?
91 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
92 * - fetch state listed in program_parameters list
93 * + WTF should this go???
94 * + currently in nvvertexec.c and s_nvfragprog.c
96 * - allow for multiple address registers (and fetch address regs properly)
99 * -----------------------------------------------------
100 * - remove any leftover unused grammer.c stuff (dict_ ?)
101 * - fix grammer.c error handling so its not static
102 * - #ifdef around stuff pertaining to extentions
104 * Outstanding Questions:
105 * -----------------------------------------------------
106 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
107 * what gets hacked off because of this:
108 * + VERTEX_ATTRIB_MATRIXINDEX
109 * + VERTEX_ATTRIB_WEIGHT
113 * - When can we fetch env/local params from their own register files, and
114 * when to we have to fetch them into the main state register file?
118 * -----------------------------------------------------
121 /* Changes since moving the file to shader directory
123 2004-III-4 ------------------------------------------------------------
124 - added #include "grammar_mesa.h"
125 - removed grammar specific code part (it resides now in grammar.c)
126 - added GL_ARB_fragment_program_shadow tokens
127 - modified #include "arbparse_syn.h"
128 - major changes inside _mesa_parse_arb_program()
129 - check the program string for '\0' characters
130 - copy the program string to a one-byte-longer location to have
132 - position invariance test (not writing to result.position) moved
136 typedef GLubyte
*production
;
139 * This is the text describing the rules to parse the grammar
141 __extension__
static char arb_grammar_text
[] =
142 #include "arbprogram_syn.h"
146 * These should match up with the values defined in arbprogram.syn
151 - changed and merged V_* and F_* opcode values to OP_*.
152 - added GL_ARB_fragment_program_shadow specific tokens (michal)
154 #define REVISION 0x09
157 #define FRAGMENT_PROGRAM 0x01
158 #define VERTEX_PROGRAM 0x02
160 /* program section */
162 #define INSTRUCTION 0x02
163 #define DECLARATION 0x03
166 /* GL_ARB_fragment_program option */
167 #define ARB_PRECISION_HINT_FASTEST 0x00
168 #define ARB_PRECISION_HINT_NICEST 0x01
169 #define ARB_FOG_EXP 0x02
170 #define ARB_FOG_EXP2 0x03
171 #define ARB_FOG_LINEAR 0x04
173 /* GL_ARB_vertex_program option */
174 #define ARB_POSITION_INVARIANT 0x05
176 /* GL_ARB_fragment_program_shadow option */
177 #define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
179 /* GL_ARB_draw_buffers option */
180 #define ARB_DRAW_BUFFERS 0x07
182 /* GL_ARB_fragment_program instruction class */
183 #define OP_ALU_INST 0x00
184 #define OP_TEX_INST 0x01
186 /* GL_ARB_vertex_program instruction class */
189 /* GL_ARB_fragment_program instruction type */
190 #define OP_ALU_VECTOR 0x00
191 #define OP_ALU_SCALAR 0x01
192 #define OP_ALU_BINSC 0x02
193 #define OP_ALU_BIN 0x03
194 #define OP_ALU_TRI 0x04
195 #define OP_ALU_SWZ 0x05
196 #define OP_TEX_SAMPLE 0x06
197 #define OP_TEX_KIL 0x07
199 /* GL_ARB_vertex_program instruction type */
200 #define OP_ALU_ARL 0x08
208 /* GL_ARB_fragment_program instruction code */
210 #define OP_ABS_SAT 0x1B
212 #define OP_FLR_SAT 0x26
214 #define OP_FRC_SAT 0x27
216 #define OP_LIT_SAT 0x2A
218 #define OP_MOV_SAT 0x30
220 #define OP_COS_SAT 0x20
222 #define OP_EX2_SAT 0x25
224 #define OP_LG2_SAT 0x29
226 #define OP_RCP_SAT 0x33
228 #define OP_RSQ_SAT 0x34
230 #define OP_SIN_SAT 0x39
232 #define OP_SCS_SAT 0x36
234 #define OP_POW_SAT 0x32
236 #define OP_ADD_SAT 0x1C
238 #define OP_DP3_SAT 0x21
240 #define OP_DP4_SAT 0x22
242 #define OP_DPH_SAT 0x23
244 #define OP_DST_SAT 0x24
246 #define OP_MAX_SAT 0x2E
248 #define OP_MIN_SAT 0x2F
250 #define OP_MUL_SAT 0x31
252 #define OP_SGE_SAT 0x37
254 #define OP_SLT_SAT 0x3A
256 #define OP_SUB_SAT 0x3B
258 #define OP_XPD_SAT 0x43
260 #define OP_CMP_SAT 0x1E
262 #define OP_LRP_SAT 0x2C
264 #define OP_MAD_SAT 0x2D
266 #define OP_SWZ_SAT 0x3C
268 #define OP_TEX_SAT 0x3E
270 #define OP_TXB_SAT 0x40
272 #define OP_TXP_SAT 0x42
275 /* GL_ARB_vertex_program instruction code */
304 /* fragment attribute binding */
305 #define FRAGMENT_ATTRIB_COLOR 0x01
306 #define FRAGMENT_ATTRIB_TEXCOORD 0x02
307 #define FRAGMENT_ATTRIB_FOGCOORD 0x03
308 #define FRAGMENT_ATTRIB_POSITION 0x04
310 /* vertex attribute binding */
311 #define VERTEX_ATTRIB_POSITION 0x01
312 #define VERTEX_ATTRIB_WEIGHT 0x02
313 #define VERTEX_ATTRIB_NORMAL 0x03
314 #define VERTEX_ATTRIB_COLOR 0x04
315 #define VERTEX_ATTRIB_FOGCOORD 0x05
316 #define VERTEX_ATTRIB_TEXCOORD 0x06
317 #define VERTEX_ATTRIB_MATRIXINDEX 0x07
318 #define VERTEX_ATTRIB_GENERIC 0x08
320 /* fragment result binding */
321 #define FRAGMENT_RESULT_COLOR 0x01
322 #define FRAGMENT_RESULT_DEPTH 0x02
324 /* vertex result binding */
325 #define VERTEX_RESULT_POSITION 0x01
326 #define VERTEX_RESULT_COLOR 0x02
327 #define VERTEX_RESULT_FOGCOORD 0x03
328 #define VERTEX_RESULT_POINTSIZE 0x04
329 #define VERTEX_RESULT_TEXCOORD 0x05
332 #define TEXTARGET_1D 0x01
333 #define TEXTARGET_2D 0x02
334 #define TEXTARGET_3D 0x03
335 #define TEXTARGET_RECT 0x04
336 #define TEXTARGET_CUBE 0x05
337 /* GL_ARB_fragment_program_shadow */
338 #define TEXTARGET_SHADOW1D 0x06
339 #define TEXTARGET_SHADOW2D 0x07
340 #define TEXTARGET_SHADOWRECT 0x08
343 #define FACE_FRONT 0x00
344 #define FACE_BACK 0x01
347 #define COLOR_PRIMARY 0x00
348 #define COLOR_SECONDARY 0x01
351 #define COMPONENT_X 0x00
352 #define COMPONENT_Y 0x01
353 #define COMPONENT_Z 0x02
354 #define COMPONENT_W 0x03
355 #define COMPONENT_0 0x04
356 #define COMPONENT_1 0x05
358 /* array index type */
359 #define ARRAY_INDEX_ABSOLUTE 0x00
360 #define ARRAY_INDEX_RELATIVE 0x01
363 #define MATRIX_MODELVIEW 0x01
364 #define MATRIX_PROJECTION 0x02
365 #define MATRIX_MVP 0x03
366 #define MATRIX_TEXTURE 0x04
367 #define MATRIX_PALETTE 0x05
368 #define MATRIX_PROGRAM 0x06
370 /* matrix modifier */
371 #define MATRIX_MODIFIER_IDENTITY 0x00
372 #define MATRIX_MODIFIER_INVERSE 0x01
373 #define MATRIX_MODIFIER_TRANSPOSE 0x02
374 #define MATRIX_MODIFIER_INVTRANS 0x03
377 #define CONSTANT_SCALAR 0x01
378 #define CONSTANT_VECTOR 0x02
380 /* program param type */
381 #define PROGRAM_PARAM_ENV 0x01
382 #define PROGRAM_PARAM_LOCAL 0x02
385 #define REGISTER_ATTRIB 0x01
386 #define REGISTER_PARAM 0x02
387 #define REGISTER_RESULT 0x03
388 #define REGISTER_ESTABLISHED_NAME 0x04
391 #define PARAM_NULL 0x00
392 #define PARAM_ARRAY_ELEMENT 0x01
393 #define PARAM_STATE_ELEMENT 0x02
394 #define PARAM_PROGRAM_ELEMENT 0x03
395 #define PARAM_PROGRAM_ELEMENTS 0x04
396 #define PARAM_CONSTANT 0x05
398 /* param state property */
399 #define STATE_MATERIAL_PARSER 0x01
400 #define STATE_LIGHT_PARSER 0x02
401 #define STATE_LIGHT_MODEL 0x03
402 #define STATE_LIGHT_PROD 0x04
403 #define STATE_FOG 0x05
404 #define STATE_MATRIX_ROWS 0x06
405 /* GL_ARB_fragment_program */
406 #define STATE_TEX_ENV 0x07
407 #define STATE_DEPTH 0x08
408 /* GL_ARB_vertex_program */
409 #define STATE_TEX_GEN 0x09
410 #define STATE_CLIP_PLANE 0x0A
411 #define STATE_POINT 0x0B
413 /* state material property */
414 #define MATERIAL_AMBIENT 0x01
415 #define MATERIAL_DIFFUSE 0x02
416 #define MATERIAL_SPECULAR 0x03
417 #define MATERIAL_EMISSION 0x04
418 #define MATERIAL_SHININESS 0x05
420 /* state light property */
421 #define LIGHT_AMBIENT 0x01
422 #define LIGHT_DIFFUSE 0x02
423 #define LIGHT_SPECULAR 0x03
424 #define LIGHT_POSITION 0x04
425 #define LIGHT_ATTENUATION 0x05
426 #define LIGHT_HALF 0x06
427 #define LIGHT_SPOT_DIRECTION 0x07
429 /* state light model property */
430 #define LIGHT_MODEL_AMBIENT 0x01
431 #define LIGHT_MODEL_SCENECOLOR 0x02
433 /* state light product property */
434 #define LIGHT_PROD_AMBIENT 0x01
435 #define LIGHT_PROD_DIFFUSE 0x02
436 #define LIGHT_PROD_SPECULAR 0x03
438 /* state texture environment property */
439 #define TEX_ENV_COLOR 0x01
441 /* state texture generation coord property */
442 #define TEX_GEN_EYE 0x01
443 #define TEX_GEN_OBJECT 0x02
445 /* state fog property */
446 #define FOG_COLOR 0x01
447 #define FOG_PARAMS 0x02
449 /* state depth property */
450 #define DEPTH_RANGE 0x01
452 /* state point parameters property */
453 #define POINT_SIZE 0x01
454 #define POINT_ATTENUATION 0x02
462 /* GL_ARB_vertex_program */
465 /*-----------------------------------------------------------------------
466 * From here on down is the semantic checking portion
471 * Variable Table Handling functions
486 * Setting an explicit field for each of the binding properties is a bit wasteful
487 * of space, but it should be much more clear when reading later on..
493 GLuint address_binding
; /* The index of the address register we should
495 GLuint attrib_binding
; /* For type vt_attrib, see nvfragprog.h for values */
496 GLuint attrib_binding_idx
; /* The index into the attrib register file corresponding
497 * to the state in attrib_binding */
498 GLuint attrib_is_generic
; /* If the attrib was specified through a generic
500 GLuint temp_binding
; /* The index of the temp register we are to use */
501 GLuint output_binding
; /* For type vt_output, see nvfragprog.h for values */
502 GLuint output_binding_idx
; /* This is the index into the result register file
503 * corresponding to the bound result state */
504 struct var_cache
*alias_binding
; /* For type vt_alias, points to the var_cache entry
505 * that this is aliased to */
506 GLuint param_binding_type
; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
507 * PROGRAM_ENV_PARAM} */
508 GLuint param_binding_begin
; /* This is the offset into the program_parameter_list where
509 * the tokens representing our bound state (or constants)
511 GLuint param_binding_length
; /* This is how many entries in the the program_parameter_list
512 * we take up with our state tokens or constants. Note that
513 * this is _not_ the same as the number of param registers
514 * we eventually use */
515 struct var_cache
*next
;
519 var_cache_create (struct var_cache
**va
)
521 *va
= (struct var_cache
*) _mesa_malloc (sizeof (struct var_cache
));
524 (**va
).type
= vt_none
;
525 (**va
).attrib_binding
= ~0;
526 (**va
).attrib_is_generic
= 0;
527 (**va
).temp_binding
= ~0;
528 (**va
).output_binding
= ~0;
529 (**va
).output_binding_idx
= ~0;
530 (**va
).param_binding_type
= ~0;
531 (**va
).param_binding_begin
= ~0;
532 (**va
).param_binding_length
= ~0;
533 (**va
).alias_binding
= NULL
;
539 var_cache_destroy (struct var_cache
**va
)
542 var_cache_destroy (&(**va
).next
);
549 var_cache_append (struct var_cache
**va
, struct var_cache
*nv
)
552 var_cache_append (&(**va
).next
, nv
);
557 static struct var_cache
*
558 var_cache_find (struct var_cache
*va
, GLubyte
* name
)
560 /*struct var_cache *first = va;*/
563 if (!_mesa_strcmp ( (const char*) name
, (const char*) va
->name
)) {
564 if (va
->type
== vt_alias
)
565 return va
->alias_binding
;
576 * constructs an integer from 4 GLubytes in LE format
579 parse_position (GLubyte
** inst
)
583 value
= (GLuint
) (*(*inst
)++);
584 value
+= (GLuint
) (*(*inst
)++) * 0x100;
585 value
+= (GLuint
) (*(*inst
)++) * 0x10000;
586 value
+= (GLuint
) (*(*inst
)++) * 0x1000000;
592 * This will, given a string, lookup the string as a variable name in the
593 * var cache. If the name is found, the var cache node corresponding to the
594 * var name is returned. If it is not found, a new entry is allocated
596 * \param I Points into the binary array where the string identifier begins
597 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
598 * \return The location on the var_cache corresponding the the string starting at I
600 static struct var_cache
*
601 parse_string (GLubyte
** inst
, struct var_cache
**vc_head
,
602 struct arb_program
*Program
, GLuint
* found
)
605 struct var_cache
*va
= NULL
;
608 *inst
+= _mesa_strlen ((char *) i
) + 1;
610 va
= var_cache_find (*vc_head
, i
);
618 var_cache_create (&va
);
621 var_cache_append (vc_head
, va
);
627 parse_string_without_adding (GLubyte
** inst
, struct arb_program
*Program
)
632 *inst
+= _mesa_strlen ((char *) i
) + 1;
638 * \return -1 if we parse '-', return 1 otherwise
641 parse_sign (GLubyte
** inst
)
643 /*return *(*inst)++ != '+'; */
649 else if (**inst
== '+') {
658 * parses and returns signed integer
661 parse_integer (GLubyte
** inst
, struct arb_program
*Program
)
666 /* check if *inst points to '+' or '-'
667 * if yes, grab the sign and increment *inst
669 sign
= parse_sign (inst
);
671 /* now check if *inst points to 0
672 * if yes, increment the *inst and return the default value
679 /* parse the integer as you normally would do it */
680 value
= _mesa_atoi (parse_string_without_adding (inst
, Program
));
682 /* now, after terminating 0 there is a position
683 * to parse it - parse_position()
685 Program
->Position
= parse_position (inst
);
691 Accumulate this string of digits, and return them as
692 a large integer represented in floating point (for range).
693 If scale is not NULL, also accumulates a power-of-ten
694 integer scale factor that represents the number of digits
698 parse_float_string(GLubyte
** inst
, struct arb_program
*Program
, GLdouble
*scale
)
700 GLdouble value
= 0.0;
701 GLdouble oscale
= 1.0;
703 if (**inst
== 0) { /* this string of digits is empty-- do nothing */
706 else { /* nonempty string-- parse out the digits */
707 while (**inst
>= '0' && **inst
<= '9') {
708 GLubyte digit
= *((*inst
)++);
709 value
= value
* 10.0 + (GLint
) (digit
- '0');
712 assert(**inst
== 0); /* integer string should end with 0 */
713 (*inst
)++; /* skip over terminating 0 */
714 Program
->Position
= parse_position(inst
); /* skip position (from integer) */
722 Parse an unsigned floating-point number from this stream of tokenized
723 characters. Example floating-point formats supported:
731 parse_float (GLubyte
** inst
, struct arb_program
*Program
)
734 GLdouble whole
, fraction
, fracScale
= 1.0;
736 whole
= parse_float_string(inst
, Program
, 0);
737 fraction
= parse_float_string(inst
, Program
, &fracScale
);
739 /* Parse signed exponent */
740 exponent
= parse_integer(inst
, Program
); /* This is the exponent */
742 /* Assemble parts of floating-point number: */
743 return (GLfloat
) ((whole
+ fraction
/ fracScale
) *
744 _mesa_pow(10.0, (GLfloat
) exponent
));
751 parse_signed_float (GLubyte
** inst
, struct arb_program
*Program
)
753 GLint sign
= parse_sign (inst
);
754 GLfloat value
= parse_float (inst
, Program
);
759 * This picks out a constant value from the parsed array. The constant vector is r
760 * returned in the *values array, which should be of length 4.
762 * \param values - The 4 component vector with the constant value in it
765 parse_constant (GLubyte
** inst
, GLfloat
*values
, struct arb_program
*Program
,
768 GLuint components
, i
;
771 switch (*(*inst
)++) {
772 case CONSTANT_SCALAR
:
773 if (use
== GL_TRUE
) {
776 values
[2] = values
[3] = parse_float (inst
, Program
);
781 values
[2] = values
[3] = parse_signed_float (inst
, Program
);
785 case CONSTANT_VECTOR
:
786 values
[0] = values
[1] = values
[2] = 0;
788 components
= *(*inst
)++;
789 for (i
= 0; i
< components
; i
++) {
790 values
[i
] = parse_signed_float (inst
, Program
);
797 * \param offset The offset from the address register that we should
800 * \return 0 on sucess, 1 on error
803 parse_relative_offset (GLcontext
*ctx
, GLubyte
**inst
, struct arb_program
*Program
,
806 *offset
= parse_integer(inst
, Program
);
811 * \param color 0 if color type is primary, 1 if color type is secondary
812 * \return 0 on sucess, 1 on error
815 parse_color_type (GLcontext
* ctx
, GLubyte
** inst
, struct arb_program
*Program
,
818 (void) ctx
; (void) Program
;
819 *color
= *(*inst
)++ != COLOR_PRIMARY
;
824 * Get an integer corresponding to a generic vertex attribute.
826 * \return 0 on sucess, 1 on error
829 parse_generic_attrib_num(GLcontext
*ctx
, GLubyte
** inst
,
830 struct arb_program
*Program
, GLuint
*attrib
)
832 GLint i
= parse_integer(inst
, Program
);
834 if ((i
< 0) || (i
> MAX_VERTEX_PROGRAM_ATTRIBS
))
836 _mesa_set_program_error (ctx
, Program
->Position
,
837 "Invalid generic vertex attribute index");
838 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid generic vertex attribute index");
843 *attrib
= (GLuint
) i
;
850 * \param color The index of the color buffer to write into
851 * \return 0 on sucess, 1 on error
854 parse_output_color_num (GLcontext
* ctx
, GLubyte
** inst
,
855 struct arb_program
*Program
, GLuint
* color
)
857 GLint i
= parse_integer (inst
, Program
);
859 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxDrawBuffers
)) {
860 _mesa_set_program_error (ctx
, Program
->Position
,
861 "Invalid draw buffer index");
862 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid draw buffer index");
872 * \param coord The texture unit index
873 * \return 0 on sucess, 1 on error
876 parse_texcoord_num (GLcontext
* ctx
, GLubyte
** inst
,
877 struct arb_program
*Program
, GLuint
* coord
)
879 GLint i
= parse_integer (inst
, Program
);
881 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxTextureUnits
)) {
882 _mesa_set_program_error (ctx
, Program
->Position
,
883 "Invalid texture unit index");
884 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid texture unit index");
893 * \param coord The weight index
894 * \return 0 on sucess, 1 on error
897 parse_weight_num (GLcontext
* ctx
, GLubyte
** inst
, struct arb_program
*Program
,
900 *coord
= parse_integer (inst
, Program
);
902 if ((*coord
< 0) || (*coord
>= 1)) {
903 _mesa_set_program_error (ctx
, Program
->Position
,
904 "Invalid weight index");
905 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid weight index");
913 * \param coord The clip plane index
914 * \return 0 on sucess, 1 on error
917 parse_clipplane_num (GLcontext
* ctx
, GLubyte
** inst
,
918 struct arb_program
*Program
, GLint
* coord
)
920 *coord
= parse_integer (inst
, Program
);
922 if ((*coord
< 0) || (*coord
>= (GLint
) ctx
->Const
.MaxClipPlanes
)) {
923 _mesa_set_program_error (ctx
, Program
->Position
,
924 "Invalid clip plane index");
925 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid clip plane index");
934 * \return 0 on front face, 1 on back face
937 parse_face_type (GLubyte
** inst
)
939 switch (*(*inst
)++) {
951 * Given a matrix and a modifier token on the binary array, return tokens
952 * that _mesa_fetch_state() [program.c] can understand.
954 * \param matrix - the matrix we are talking about
955 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
956 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
957 * \return 0 on sucess, 1 on failure
960 parse_matrix (GLcontext
* ctx
, GLubyte
** inst
, struct arb_program
*Program
,
961 GLint
* matrix
, GLint
* matrix_idx
, GLint
* matrix_modifier
)
963 GLubyte mat
= *(*inst
)++;
968 case MATRIX_MODELVIEW
:
969 *matrix
= STATE_MODELVIEW
;
970 *matrix_idx
= parse_integer (inst
, Program
);
971 if (*matrix_idx
> 0) {
972 _mesa_set_program_error (ctx
, Program
->Position
,
973 "ARB_vertex_blend not supported\n");
974 _mesa_error (ctx
, GL_INVALID_OPERATION
,
975 "ARB_vertex_blend not supported\n");
980 case MATRIX_PROJECTION
:
981 *matrix
= STATE_PROJECTION
;
989 *matrix
= STATE_TEXTURE
;
990 *matrix_idx
= parse_integer (inst
, Program
);
991 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxTextureUnits
) {
992 _mesa_set_program_error (ctx
, Program
->Position
,
993 "Invalid Texture Unit");
994 _mesa_error (ctx
, GL_INVALID_OPERATION
,
995 "Invalid Texture Unit: %d", *matrix_idx
);
1000 /* This is not currently supported (ARB_matrix_palette) */
1001 case MATRIX_PALETTE
:
1002 *matrix_idx
= parse_integer (inst
, Program
);
1003 _mesa_set_program_error (ctx
, Program
->Position
,
1004 "ARB_matrix_palette not supported\n");
1005 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1006 "ARB_matrix_palette not supported\n");
1010 case MATRIX_PROGRAM
:
1011 *matrix
= STATE_PROGRAM
;
1012 *matrix_idx
= parse_integer (inst
, Program
);
1013 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxProgramMatrices
) {
1014 _mesa_set_program_error (ctx
, Program
->Position
,
1015 "Invalid Program Matrix");
1016 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1017 "Invalid Program Matrix: %d", *matrix_idx
);
1023 switch (*(*inst
)++) {
1024 case MATRIX_MODIFIER_IDENTITY
:
1025 *matrix_modifier
= 0;
1027 case MATRIX_MODIFIER_INVERSE
:
1028 *matrix_modifier
= STATE_MATRIX_INVERSE
;
1030 case MATRIX_MODIFIER_TRANSPOSE
:
1031 *matrix_modifier
= STATE_MATRIX_TRANSPOSE
;
1033 case MATRIX_MODIFIER_INVTRANS
:
1034 *matrix_modifier
= STATE_MATRIX_INVTRANS
;
1043 * This parses a state string (rather, the binary version of it) into
1044 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1046 * \param inst - the start in the binary arry to start working from
1047 * \param state_tokens - the storage for the 6-token state description
1048 * \return - 0 on sucess, 1 on error
1051 parse_state_single_item (GLcontext
* ctx
, GLubyte
** inst
,
1052 struct arb_program
*Program
, GLint
* state_tokens
)
1054 switch (*(*inst
)++) {
1055 case STATE_MATERIAL_PARSER
:
1056 state_tokens
[0] = STATE_MATERIAL
;
1057 state_tokens
[1] = parse_face_type (inst
);
1058 switch (*(*inst
)++) {
1059 case MATERIAL_AMBIENT
:
1060 state_tokens
[2] = STATE_AMBIENT
;
1062 case MATERIAL_DIFFUSE
:
1063 state_tokens
[2] = STATE_DIFFUSE
;
1065 case MATERIAL_SPECULAR
:
1066 state_tokens
[2] = STATE_SPECULAR
;
1068 case MATERIAL_EMISSION
:
1069 state_tokens
[2] = STATE_EMISSION
;
1071 case MATERIAL_SHININESS
:
1072 state_tokens
[2] = STATE_SHININESS
;
1077 case STATE_LIGHT_PARSER
:
1078 state_tokens
[0] = STATE_LIGHT
;
1079 state_tokens
[1] = parse_integer (inst
, Program
);
1081 /* Check the value of state_tokens[1] against the # of lights */
1082 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1083 _mesa_set_program_error (ctx
, Program
->Position
,
1084 "Invalid Light Number");
1085 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1086 "Invalid Light Number: %d", state_tokens
[1]);
1090 switch (*(*inst
)++) {
1092 state_tokens
[2] = STATE_AMBIENT
;
1095 state_tokens
[2] = STATE_DIFFUSE
;
1097 case LIGHT_SPECULAR
:
1098 state_tokens
[2] = STATE_SPECULAR
;
1100 case LIGHT_POSITION
:
1101 state_tokens
[2] = STATE_POSITION
;
1103 case LIGHT_ATTENUATION
:
1104 state_tokens
[2] = STATE_ATTENUATION
;
1107 state_tokens
[2] = STATE_HALF
;
1109 case LIGHT_SPOT_DIRECTION
:
1110 state_tokens
[2] = STATE_SPOT_DIRECTION
;
1115 case STATE_LIGHT_MODEL
:
1116 switch (*(*inst
)++) {
1117 case LIGHT_MODEL_AMBIENT
:
1118 state_tokens
[0] = STATE_LIGHTMODEL_AMBIENT
;
1120 case LIGHT_MODEL_SCENECOLOR
:
1121 state_tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
1122 state_tokens
[1] = parse_face_type (inst
);
1127 case STATE_LIGHT_PROD
:
1128 state_tokens
[0] = STATE_LIGHTPROD
;
1129 state_tokens
[1] = parse_integer (inst
, Program
);
1131 /* Check the value of state_tokens[1] against the # of lights */
1132 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1133 _mesa_set_program_error (ctx
, Program
->Position
,
1134 "Invalid Light Number");
1135 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1136 "Invalid Light Number: %d", state_tokens
[1]);
1140 state_tokens
[2] = parse_face_type (inst
);
1141 switch (*(*inst
)++) {
1142 case LIGHT_PROD_AMBIENT
:
1143 state_tokens
[3] = STATE_AMBIENT
;
1145 case LIGHT_PROD_DIFFUSE
:
1146 state_tokens
[3] = STATE_DIFFUSE
;
1148 case LIGHT_PROD_SPECULAR
:
1149 state_tokens
[3] = STATE_SPECULAR
;
1156 switch (*(*inst
)++) {
1158 state_tokens
[0] = STATE_FOG_COLOR
;
1161 state_tokens
[0] = STATE_FOG_PARAMS
;
1167 state_tokens
[1] = parse_integer (inst
, Program
);
1168 switch (*(*inst
)++) {
1170 state_tokens
[0] = STATE_TEXENV_COLOR
;
1179 state_tokens
[0] = STATE_TEXGEN
;
1180 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1182 if (parse_texcoord_num (ctx
, inst
, Program
, &coord
))
1184 state_tokens
[1] = coord
;
1189 /* 0 - s, 1 - t, 2 - r, 3 - q */
1192 if (type
== TEX_GEN_EYE
) {
1195 state_tokens
[2] = STATE_TEXGEN_EYE_S
;
1198 state_tokens
[2] = STATE_TEXGEN_EYE_T
;
1201 state_tokens
[2] = STATE_TEXGEN_EYE_R
;
1204 state_tokens
[2] = STATE_TEXGEN_EYE_Q
;
1211 state_tokens
[2] = STATE_TEXGEN_OBJECT_S
;
1214 state_tokens
[2] = STATE_TEXGEN_OBJECT_T
;
1217 state_tokens
[2] = STATE_TEXGEN_OBJECT_R
;
1220 state_tokens
[2] = STATE_TEXGEN_OBJECT_Q
;
1228 switch (*(*inst
)++) {
1230 state_tokens
[0] = STATE_DEPTH_RANGE
;
1235 case STATE_CLIP_PLANE
:
1236 state_tokens
[0] = STATE_CLIPPLANE
;
1237 state_tokens
[1] = parse_integer (inst
, Program
);
1238 if (parse_clipplane_num (ctx
, inst
, Program
, &state_tokens
[1]))
1243 switch (*(*inst
++)) {
1245 state_tokens
[0] = STATE_POINT_SIZE
;
1248 case POINT_ATTENUATION
:
1249 state_tokens
[0] = STATE_POINT_ATTENUATION
;
1254 /* XXX: I think this is the correct format for a matrix row */
1255 case STATE_MATRIX_ROWS
:
1256 state_tokens
[0] = STATE_MATRIX
;
1258 (ctx
, inst
, Program
, &state_tokens
[1], &state_tokens
[2],
1262 state_tokens
[3] = parse_integer (inst
, Program
); /* The first row to grab */
1264 if ((**inst
) != 0) { /* Either the last row, 0 */
1265 state_tokens
[4] = parse_integer (inst
, Program
);
1266 if (state_tokens
[4] < state_tokens
[3]) {
1267 _mesa_set_program_error (ctx
, Program
->Position
,
1268 "Second matrix index less than the first");
1269 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1270 "Second matrix index (%d) less than the first (%d)",
1271 state_tokens
[4], state_tokens
[3]);
1276 state_tokens
[4] = state_tokens
[3];
1286 * This parses a state string (rather, the binary version of it) into
1287 * a 6-token similar for the state fetching code in program.c
1289 * One might ask, why fetch these parameters into just like you fetch
1290 * state when they are already stored in other places?
1292 * Because of array offsets -> We can stick env/local parameters in the
1293 * middle of a parameter array and then index someplace into the array
1296 * One optimization might be to only do this for the cases where the
1297 * env/local parameters end up inside of an array, and leave the
1298 * single parameters (or arrays of pure env/local pareameters) in their
1299 * respective register files.
1301 * For ENV parameters, the format is:
1302 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1303 * state_tokens[1] = STATE_ENV
1304 * state_tokens[2] = the parameter index
1306 * for LOCAL parameters, the format is:
1307 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1308 * state_tokens[1] = STATE_LOCAL
1309 * state_tokens[2] = the parameter index
1311 * \param inst - the start in the binary arry to start working from
1312 * \param state_tokens - the storage for the 6-token state description
1313 * \return - 0 on sucess, 1 on failure
1316 parse_program_single_item (GLcontext
* ctx
, GLubyte
** inst
,
1317 struct arb_program
*Program
, GLint
* state_tokens
)
1319 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1320 state_tokens
[0] = STATE_FRAGMENT_PROGRAM
;
1322 state_tokens
[0] = STATE_VERTEX_PROGRAM
;
1325 switch (*(*inst
)++) {
1326 case PROGRAM_PARAM_ENV
:
1327 state_tokens
[1] = STATE_ENV
;
1328 state_tokens
[2] = parse_integer (inst
, Program
);
1330 /* Check state_tokens[2] against the number of ENV parameters available */
1331 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1332 (state_tokens
[2] >= (GLint
) ctx
->Const
.MaxFragmentProgramEnvParams
))
1334 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1335 (state_tokens
[2] >= (GLint
) ctx
->Const
.MaxVertexProgramEnvParams
))) {
1336 _mesa_set_program_error (ctx
, Program
->Position
,
1337 "Invalid Program Env Parameter");
1338 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1339 "Invalid Program Env Parameter: %d",
1346 case PROGRAM_PARAM_LOCAL
:
1347 state_tokens
[1] = STATE_LOCAL
;
1348 state_tokens
[2] = parse_integer (inst
, Program
);
1350 /* Check state_tokens[2] against the number of LOCAL parameters available */
1351 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1352 (state_tokens
[2] >= (GLint
) ctx
->Const
.MaxFragmentProgramLocalParams
))
1354 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1355 (state_tokens
[2] >= (GLint
) ctx
->Const
.MaxVertexProgramLocalParams
))) {
1356 _mesa_set_program_error (ctx
, Program
->Position
,
1357 "Invalid Program Local Parameter");
1358 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1359 "Invalid Program Local Parameter: %d",
1370 * For ARB_vertex_program, programs are not allowed to use both an explicit
1371 * vertex attribute and a generic vertex attribute corresponding to the same
1372 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1374 * This will walk our var_cache and make sure that nobody does anything fishy.
1376 * \return 0 on sucess, 1 on error
1379 generic_attrib_check(struct var_cache
*vc_head
)
1382 struct var_cache
*curr
;
1383 GLboolean explicitAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
],
1384 genericAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
];
1386 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1387 explicitAttrib
[a
] = GL_FALSE
;
1388 genericAttrib
[a
] = GL_FALSE
;
1393 if (curr
->type
== vt_attrib
) {
1394 if (curr
->attrib_is_generic
)
1395 genericAttrib
[ curr
->attrib_binding_idx
] = GL_TRUE
;
1397 explicitAttrib
[ curr
->attrib_binding_idx
] = GL_TRUE
;
1403 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1404 if ((explicitAttrib
[a
]) && (genericAttrib
[a
]))
1412 * This will handle the binding side of an ATTRIB var declaration
1414 * \param binding - the fragment input register state, defined in nvfragprog.h
1415 * \param binding_idx - the index in the attrib register file that binding is associated with
1416 * \return returns 0 on sucess, 1 on error
1418 * See nvfragparse.c for attrib register file layout
1421 parse_attrib_binding (GLcontext
* ctx
, GLubyte
** inst
,
1422 struct arb_program
*Program
, GLuint
* binding
,
1423 GLuint
* binding_idx
, GLuint
*is_generic
)
1430 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1431 switch (*(*inst
)++) {
1432 case FRAGMENT_ATTRIB_COLOR
:
1433 err
= parse_color_type (ctx
, inst
, Program
, &coord
);
1434 *binding
= FRAG_ATTRIB_COL0
+ coord
;
1435 *binding_idx
= 1 + coord
;
1438 case FRAGMENT_ATTRIB_TEXCOORD
:
1439 err
= parse_texcoord_num (ctx
, inst
, Program
, &texcoord
);
1440 *binding
= FRAG_ATTRIB_TEX0
+ texcoord
;
1441 *binding_idx
= 4 + texcoord
;
1444 case FRAGMENT_ATTRIB_FOGCOORD
:
1445 *binding
= FRAG_ATTRIB_FOGC
;
1449 case FRAGMENT_ATTRIB_POSITION
:
1450 *binding
= FRAG_ATTRIB_WPOS
;
1460 switch (*(*inst
)++) {
1461 case VERTEX_ATTRIB_POSITION
:
1462 *binding
= VERT_ATTRIB_POS
;
1466 case VERTEX_ATTRIB_WEIGHT
:
1470 err
= parse_weight_num (ctx
, inst
, Program
, &weight
);
1471 *binding
= VERT_ATTRIB_WEIGHT
;
1474 _mesa_set_program_error (ctx
, Program
->Position
,
1475 "ARB_vertex_blend not supported\n");
1476 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1477 "ARB_vertex_blend not supported\n");
1481 case VERTEX_ATTRIB_NORMAL
:
1482 *binding
= VERT_ATTRIB_NORMAL
;
1486 case VERTEX_ATTRIB_COLOR
:
1490 err
= parse_color_type (ctx
, inst
, Program
, &color
);
1492 *binding
= VERT_ATTRIB_COLOR1
;
1496 *binding
= VERT_ATTRIB_COLOR0
;
1502 case VERTEX_ATTRIB_FOGCOORD
:
1503 *binding
= VERT_ATTRIB_FOG
;
1507 case VERTEX_ATTRIB_TEXCOORD
:
1511 err
= parse_texcoord_num (ctx
, inst
, Program
, &unit
);
1512 *binding
= VERT_ATTRIB_TEX0
+ unit
;
1513 *binding_idx
= 8 + unit
;
1517 /* It looks like we don't support this at all, atm */
1518 case VERTEX_ATTRIB_MATRIXINDEX
:
1519 parse_integer (inst
, Program
);
1520 _mesa_set_program_error (ctx
, Program
->Position
,
1521 "ARB_palette_matrix not supported");
1522 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1523 "ARB_palette_matrix not supported");
1527 case VERTEX_ATTRIB_GENERIC
:
1531 if (!parse_generic_attrib_num(ctx
, inst
, Program
, &attrib
)) {
1535 *binding
= VERT_ATTRIB_POS
;
1538 *binding
= VERT_ATTRIB_WEIGHT
;
1541 *binding
= VERT_ATTRIB_NORMAL
;
1544 *binding
= VERT_ATTRIB_COLOR0
;
1547 *binding
= VERT_ATTRIB_COLOR1
;
1550 *binding
= VERT_ATTRIB_FOG
;
1557 *binding
= VERT_ATTRIB_TEX0
+ (attrib
-8);
1560 *binding_idx
= attrib
;
1571 /* Can this even happen? */
1573 _mesa_set_program_error (ctx
, Program
->Position
,
1574 "Bad attribute binding");
1575 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Bad attribute binding");
1578 Program
->InputsRead
|= (1 << *binding_idx
);
1584 * This translates between a binary token for an output variable type
1585 * and the mesa token for the same thing.
1588 * XXX: What is the 'name' for vertex program state? -> do we need it?
1589 * I don't think we do;
1591 * See nvfragprog.h for definitions
1593 * \param inst - The parsed tokens
1594 * \param binding - The name of the state we are binding too
1595 * \param binding_idx - The index into the result register file that this is bound too
1597 * See nvfragparse.c for the register file layout for fragment programs
1598 * See nvvertparse.c for the register file layout for vertex programs
1601 parse_result_binding (GLcontext
* ctx
, GLubyte
** inst
, GLuint
* binding
,
1602 GLuint
* binding_idx
, struct arb_program
*Program
)
1604 GLuint b
, out_color
;
1606 switch (*(*inst
)++) {
1607 case FRAGMENT_RESULT_COLOR
:
1608 /* for frag programs, this is FRAGMENT_RESULT_COLOR */
1609 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1610 /* This gets result of the color buffer we're supposed to
1613 parse_output_color_num(ctx
, inst
, Program
, &out_color
);
1615 *binding
= FRAG_OUTPUT_COLR
;
1617 /* XXX: We're ignoring the color buffer for now. */
1620 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1626 case FRAGMENT_RESULT_DEPTH
:
1627 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
1628 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1629 *binding
= FRAG_OUTPUT_DEPR
;
1632 /* for vtx programs, this is VERTEX_RESULT_COLOR */
1635 GLuint face_type
= parse_face_type(inst
);
1636 GLint color_type_ret
= parse_color_type(ctx
, inst
, Program
, &color_type
);
1640 if (color_type_ret
) return 1;
1642 /* secondary color */
1653 /* secondary color */
1665 case VERTEX_RESULT_FOGCOORD
:
1669 case VERTEX_RESULT_POINTSIZE
:
1673 case VERTEX_RESULT_TEXCOORD
:
1674 if (parse_texcoord_num (ctx
, inst
, Program
, &b
))
1676 *binding_idx
= 7 + b
;
1680 Program
->OutputsWritten
|= (1 << *binding_idx
);
1686 * This handles the declaration of ATTRIB variables
1689 * parse_vert_attrib_binding(), or something like that
1691 * \return 0 on sucess, 1 on error
1694 parse_attrib (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
1695 struct arb_program
*Program
)
1699 struct var_cache
*attrib_var
;
1701 attrib_var
= parse_string (inst
, vc_head
, Program
, &found
);
1702 Program
->Position
= parse_position (inst
);
1704 error_msg
= (char *)
1705 _mesa_malloc (_mesa_strlen ((char *) attrib_var
->name
) + 40);
1706 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
1709 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
1710 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
1712 _mesa_free (error_msg
);
1716 attrib_var
->type
= vt_attrib
;
1718 /* I think this is ok now - karl */
1720 /*if (Program->type == GL_FRAGMENT_PROGRAM_ARB) */
1722 if (parse_attrib_binding
1723 (ctx
, inst
, Program
, &attrib_var
->attrib_binding
,
1724 &attrib_var
->attrib_binding_idx
, &attrib_var
->attrib_is_generic
))
1726 if (generic_attrib_check(*vc_head
)) {
1727 _mesa_set_program_error (ctx
, Program
->Position
,
1728 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
1729 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1730 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
1736 Program
->Base
.NumAttributes
++;
1741 * \param use -- TRUE if we're called when declaring implicit parameters,
1742 * FALSE if we're declaraing variables. This has to do with
1743 * if we get a signed or unsigned float for scalar constants
1746 parse_param_elements (GLcontext
* ctx
, GLubyte
** inst
,
1747 struct var_cache
*param_var
,
1748 struct arb_program
*Program
, GLboolean use
)
1752 GLint state_tokens
[6];
1753 GLfloat const_values
[4];
1757 switch (*(*inst
)++) {
1758 case PARAM_STATE_ELEMENT
:
1760 if (parse_state_single_item (ctx
, inst
, Program
, state_tokens
))
1763 /* If we adding STATE_MATRIX that has multiple rows, we need to
1764 * unroll it and call _mesa_add_state_reference() for each row
1766 if ((state_tokens
[0] == STATE_MATRIX
)
1767 && (state_tokens
[3] != state_tokens
[4])) {
1769 GLint first_row
= state_tokens
[3];
1770 GLint last_row
= state_tokens
[4];
1772 for (row
= first_row
; row
<= last_row
; row
++) {
1773 state_tokens
[3] = state_tokens
[4] = row
;
1776 _mesa_add_state_reference (Program
->Parameters
,
1778 if (param_var
->param_binding_begin
== ~0U)
1779 param_var
->param_binding_begin
= idx
;
1780 param_var
->param_binding_length
++;
1781 Program
->Base
.NumParameters
++;
1786 _mesa_add_state_reference (Program
->Parameters
, state_tokens
);
1787 if (param_var
->param_binding_begin
== ~0U)
1788 param_var
->param_binding_begin
= idx
;
1789 param_var
->param_binding_length
++;
1790 Program
->Base
.NumParameters
++;
1794 case PARAM_PROGRAM_ELEMENT
:
1796 if (parse_program_single_item (ctx
, inst
, Program
, state_tokens
))
1798 idx
= _mesa_add_state_reference (Program
->Parameters
, state_tokens
);
1799 if (param_var
->param_binding_begin
== ~0U)
1800 param_var
->param_binding_begin
= idx
;
1801 param_var
->param_binding_length
++;
1802 Program
->Base
.NumParameters
++;
1804 /* Check if there is more: 0 -> we're done, else its an integer */
1806 GLuint out_of_range
, new_idx
;
1807 GLuint start_idx
= state_tokens
[2] + 1;
1808 GLuint end_idx
= parse_integer (inst
, Program
);
1811 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1812 if (((state_tokens
[1] == STATE_ENV
)
1813 && (end_idx
>= ctx
->Const
.MaxFragmentProgramEnvParams
))
1814 || ((state_tokens
[1] == STATE_LOCAL
)
1816 ctx
->Const
.MaxFragmentProgramLocalParams
)))
1820 if (((state_tokens
[1] == STATE_ENV
)
1821 && (end_idx
>= ctx
->Const
.MaxVertexProgramEnvParams
))
1822 || ((state_tokens
[1] == STATE_LOCAL
)
1824 ctx
->Const
.MaxVertexProgramLocalParams
)))
1828 _mesa_set_program_error (ctx
, Program
->Position
,
1829 "Invalid Program Parameter");
1830 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1831 "Invalid Program Parameter: %d", end_idx
);
1835 for (new_idx
= start_idx
; new_idx
<= end_idx
; new_idx
++) {
1836 state_tokens
[2] = new_idx
;
1838 _mesa_add_state_reference (Program
->Parameters
,
1840 param_var
->param_binding_length
++;
1841 Program
->Base
.NumParameters
++;
1850 case PARAM_CONSTANT
:
1851 parse_constant (inst
, const_values
, Program
, use
);
1853 _mesa_add_named_constant (Program
->Parameters
,
1854 (char *) param_var
->name
, const_values
);
1855 if (param_var
->param_binding_begin
== ~0U)
1856 param_var
->param_binding_begin
= idx
;
1857 param_var
->param_binding_length
++;
1858 Program
->Base
.NumParameters
++;
1862 _mesa_set_program_error (ctx
, Program
->Position
,
1863 "Unexpected token in parse_param_elements()");
1864 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1865 "Unexpected token in parse_param_elements()");
1869 /* Make sure we haven't blown past our parameter limits */
1870 if (((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1871 (Program
->Base
.NumParameters
>=
1872 ctx
->Const
.MaxVertexProgramLocalParams
))
1873 || ((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1874 && (Program
->Base
.NumParameters
>=
1875 ctx
->Const
.MaxFragmentProgramLocalParams
))) {
1876 _mesa_set_program_error (ctx
, Program
->Position
,
1877 "Too many parameter variables");
1878 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Too many parameter variables");
1886 * This picks out PARAM program parameter bindings.
1888 * XXX: This needs to be stressed & tested
1890 * \return 0 on sucess, 1 on error
1893 parse_param (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
1894 struct arb_program
*Program
)
1897 GLint specified_length
;
1899 struct var_cache
*param_var
;
1902 param_var
= parse_string (inst
, vc_head
, Program
, &found
);
1903 Program
->Position
= parse_position (inst
);
1906 error_msg
= (char *) _mesa_malloc (_mesa_strlen ((char *) param_var
->name
) + 40);
1907 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
1910 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
1911 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
1913 _mesa_free (error_msg
);
1917 specified_length
= parse_integer (inst
, Program
);
1919 if (specified_length
< 0) {
1920 _mesa_set_program_error (ctx
, Program
->Position
,
1921 "Negative parameter array length");
1922 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1923 "Negative parameter array length: %d", specified_length
);
1927 param_var
->type
= vt_param
;
1928 param_var
->param_binding_length
= 0;
1930 /* Right now, everything is shoved into the main state register file.
1932 * In the future, it would be nice to leave things ENV/LOCAL params
1933 * in their respective register files, if possible
1935 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1938 * * - add each guy to the parameter list
1939 * * - increment the param_var->param_binding_len
1940 * * - store the param_var->param_binding_begin for the first one
1941 * * - compare the actual len to the specified len at the end
1943 while (**inst
!= PARAM_NULL
) {
1944 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_FALSE
))
1948 /* Test array length here! */
1949 if (specified_length
) {
1950 if (specified_length
!= (int)param_var
->param_binding_length
) {
1951 _mesa_set_program_error (ctx
, Program
->Position
,
1952 "Declared parameter array length does not match parameter list");
1953 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1954 "Declared parameter array length does not match parameter list");
1967 parse_param_use (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
1968 struct arb_program
*Program
, struct var_cache
**new_var
)
1970 struct var_cache
*param_var
;
1972 /* First, insert a dummy entry into the var_cache */
1973 var_cache_create (¶m_var
);
1974 param_var
->name
= (GLubyte
*) _mesa_strdup (" ");
1975 param_var
->type
= vt_param
;
1977 param_var
->param_binding_length
= 0;
1978 /* Don't fill in binding_begin; We use the default value of -1
1979 * to tell if its already initialized, elsewhere.
1981 * param_var->param_binding_begin = 0;
1983 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1985 var_cache_append (vc_head
, param_var
);
1987 /* Then fill it with juicy parameter goodness */
1988 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_TRUE
))
1991 *new_var
= param_var
;
1998 * This handles the declaration of TEMP variables
2000 * \return 0 on sucess, 1 on error
2003 parse_temp (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2004 struct arb_program
*Program
)
2007 struct var_cache
*temp_var
;
2010 while (**inst
!= 0) {
2011 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2012 Program
->Position
= parse_position (inst
);
2014 error_msg
= (char *)
2015 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2016 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
2019 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
2020 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
2022 _mesa_free (error_msg
);
2026 temp_var
->type
= vt_temp
;
2028 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
2029 (Program
->Base
.NumTemporaries
>=
2030 ctx
->Const
.MaxFragmentProgramTemps
))
2031 || ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
2032 && (Program
->Base
.NumTemporaries
>=
2033 ctx
->Const
.MaxVertexProgramTemps
))) {
2034 _mesa_set_program_error (ctx
, Program
->Position
,
2035 "Too many TEMP variables declared");
2036 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2037 "Too many TEMP variables declared");
2041 temp_var
->temp_binding
= Program
->Base
.NumTemporaries
;
2042 Program
->Base
.NumTemporaries
++;
2050 * This handles variables of the OUTPUT variety
2052 * \return 0 on sucess, 1 on error
2055 parse_output (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2056 struct arb_program
*Program
)
2059 struct var_cache
*output_var
;
2061 output_var
= parse_string (inst
, vc_head
, Program
, &found
);
2062 Program
->Position
= parse_position (inst
);
2065 error_msg
= (char *)
2066 _mesa_malloc (_mesa_strlen ((char *) output_var
->name
) + 40);
2067 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
2070 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
2071 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
2073 _mesa_free (error_msg
);
2077 output_var
->type
= vt_output
;
2078 return parse_result_binding (ctx
, inst
, &output_var
->output_binding
,
2079 &output_var
->output_binding_idx
, Program
);
2083 * This handles variables of the ALIAS kind
2085 * \return 0 on sucess, 1 on error
2088 parse_alias (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2089 struct arb_program
*Program
)
2092 struct var_cache
*temp_var
;
2096 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2097 Program
->Position
= parse_position (inst
);
2100 error_msg
= (char *)
2101 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2102 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
2105 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
2106 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
2108 _mesa_free (error_msg
);
2112 temp_var
->type
= vt_alias
;
2113 temp_var
->alias_binding
= parse_string (inst
, vc_head
, Program
, &found
);
2114 Program
->Position
= parse_position (inst
);
2118 error_msg
= (char *)
2119 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2120 _mesa_sprintf (error_msg
, "Alias value %s is not defined",
2121 temp_var
->alias_binding
->name
);
2123 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
2124 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
2126 _mesa_free (error_msg
);
2134 * This handles variables of the ADDRESS kind
2136 * \return 0 on sucess, 1 on error
2139 parse_address (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2140 struct arb_program
*Program
)
2143 struct var_cache
*temp_var
;
2146 while (**inst
!= 0) {
2147 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2148 Program
->Position
= parse_position (inst
);
2150 error_msg
= (char *)
2151 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2152 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
2155 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
2156 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
2158 _mesa_free (error_msg
);
2162 temp_var
->type
= vt_address
;
2164 if (Program
->Base
.NumAddressRegs
>=
2165 ctx
->Const
.MaxVertexProgramAddressRegs
) {
2166 _mesa_set_program_error (ctx
, Program
->Position
,
2167 "Too many ADDRESS variables declared");
2168 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2169 "Too many ADDRESS variables declared");
2173 temp_var
->address_binding
= Program
->Base
.NumAddressRegs
;
2174 Program
->Base
.NumAddressRegs
++;
2182 * Parse a program declaration
2184 * \return 0 on sucess, 1 on error
2187 parse_declaration (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2188 struct arb_program
*Program
)
2192 switch (*(*inst
)++) {
2194 err
= parse_address (ctx
, inst
, vc_head
, Program
);
2198 err
= parse_alias (ctx
, inst
, vc_head
, Program
);
2202 err
= parse_attrib (ctx
, inst
, vc_head
, Program
);
2206 err
= parse_output (ctx
, inst
, vc_head
, Program
);
2210 err
= parse_param (ctx
, inst
, vc_head
, Program
);
2214 err
= parse_temp (ctx
, inst
, vc_head
, Program
);
2222 * Handle the parsing out of a masked destination register
2224 * If we are a vertex program, make sure we don't write to
2225 * result.position of we have specified that the program is
2226 * position invariant
2228 * \param File - The register file we write to
2229 * \param Index - The register index we write to
2230 * \param WriteMask - The mask controlling which components we write (1->write)
2232 * \return 0 on sucess, 1 on error
2235 parse_masked_dst_reg (GLcontext
* ctx
, GLubyte
** inst
,
2236 struct var_cache
**vc_head
, struct arb_program
*Program
,
2237 GLint
* File
, GLint
* Index
, GLint
*WriteMask
)
2240 struct var_cache
*dst
;
2242 /* We either have a result register specified, or a
2243 * variable that may or may not be writable
2245 switch (*(*inst
)++) {
2246 case REGISTER_RESULT
:
2247 if (parse_result_binding
2248 (ctx
, inst
, &result
, (GLuint
*) Index
, Program
))
2250 *File
= PROGRAM_OUTPUT
;
2253 case REGISTER_ESTABLISHED_NAME
:
2254 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2255 Program
->Position
= parse_position (inst
);
2257 /* If the name has never been added to our symbol table, we're hosed */
2259 _mesa_set_program_error (ctx
, Program
->Position
,
2260 "0: Undefined variable");
2261 _mesa_error (ctx
, GL_INVALID_OPERATION
, "0: Undefined variable: %s",
2266 switch (dst
->type
) {
2268 *File
= PROGRAM_OUTPUT
;
2269 *Index
= dst
->output_binding_idx
;
2273 *File
= PROGRAM_TEMPORARY
;
2274 *Index
= dst
->temp_binding
;
2277 /* If the var type is not vt_output or vt_temp, no go */
2279 _mesa_set_program_error (ctx
, Program
->Position
,
2280 "Destination register is read only");
2281 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2282 "Destination register is read only: %s",
2289 _mesa_set_program_error (ctx
, Program
->Position
,
2290 "Unexpected opcode in parse_masked_dst_reg()");
2291 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2292 "Unexpected opcode in parse_masked_dst_reg()");
2297 /* Position invariance test */
2298 /* This test is done now in syntax portion - when position invariance OPTION
2299 is specified, "result.position" rule is disabled so there is no way
2300 to write the position
2302 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2304 _mesa_set_program_error (ctx, Program->Position,
2305 "Vertex program specified position invariance and wrote vertex position");
2306 _mesa_error (ctx, GL_INVALID_OPERATION,
2307 "Vertex program specified position invariance and wrote vertex position");
2310 /* And then the mask.
2316 * ==> Need to reverse the order of bits for this!
2318 tmp
= (GLint
) *(*inst
)++;
2319 *WriteMask
= (((tmp
>>3) & 0x1) |
2329 * Handle the parsing of a address register
2331 * \param Index - The register index we write to
2333 * \return 0 on sucess, 1 on error
2336 parse_address_reg (GLcontext
* ctx
, GLubyte
** inst
,
2337 struct var_cache
**vc_head
,
2338 struct arb_program
*Program
, GLint
* Index
)
2340 struct var_cache
*dst
;
2344 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2345 Program
->Position
= parse_position (inst
);
2347 /* If the name has never been added to our symbol table, we're hosed */
2349 _mesa_set_program_error (ctx
, Program
->Position
, "Undefined variable");
2350 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Undefined variable: %s",
2355 if (dst
->type
!= vt_address
) {
2356 _mesa_set_program_error (ctx
, Program
->Position
,
2357 "Variable is not of type ADDRESS");
2358 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2359 "Variable: %s is not of type ADDRESS", dst
->name
);
2368 * Handle the parsing out of a masked address register
2370 * \param Index - The register index we write to
2371 * \param WriteMask - The mask controlling which components we write (1->write)
2373 * \return 0 on sucess, 1 on error
2376 parse_masked_address_reg (GLcontext
* ctx
, GLubyte
** inst
,
2377 struct var_cache
**vc_head
,
2378 struct arb_program
*Program
, GLint
* Index
,
2379 GLboolean
* WriteMask
)
2381 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, Index
))
2384 /* This should be 0x8 */
2387 /* Writemask of .x is implied */
2389 WriteMask
[1] = WriteMask
[2] = WriteMask
[3] = 0;
2396 * Parse out a swizzle mask.
2398 * Basically convert COMPONENT_X/Y/Z/W to SWIZZLE_X/Y/Z/W
2400 * The len parameter allows us to grab 4 components for a vector
2401 * swizzle, or just 1 component for a scalar src register selection
2404 parse_swizzle_mask(GLubyte
** inst
, GLubyte
*swizzle
, GLint len
)
2408 for (i
= 0; i
< 4; i
++)
2411 for (i
= 0; i
< len
; i
++) {
2412 switch (*(*inst
)++) {
2414 swizzle
[i
] = SWIZZLE_X
;
2417 swizzle
[i
] = SWIZZLE_Y
;
2420 swizzle
[i
] = SWIZZLE_Z
;
2423 swizzle
[i
] = SWIZZLE_W
;
2426 _mesa_problem(NULL
, "bad component in parse_swizzle_mask()");
2434 * Parse an extended swizzle mask which is a sequence of
2435 * four x/y/z/w/0/1 tokens.
2436 * \return swizzle four swizzle values
2437 * \return negateMask four element bitfield
2440 parse_extended_swizzle_mask(GLubyte
**inst
, GLubyte swizzle
[4],
2441 GLubyte
*negateMask
)
2446 for (i
= 0; i
< 4; i
++) {
2448 if (parse_sign(inst
) == -1)
2449 *negateMask
|= (1 << i
);
2455 swizzle
[i
] = SWIZZLE_ZERO
;
2458 swizzle
[i
] = SWIZZLE_ONE
;
2461 swizzle
[i
] = SWIZZLE_X
;
2464 swizzle
[i
] = SWIZZLE_Y
;
2467 swizzle
[i
] = SWIZZLE_Z
;
2470 swizzle
[i
] = SWIZZLE_W
;
2473 _mesa_problem(NULL
, "bad case in parse_extended_swizzle_mask()");
2481 parse_src_reg (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2482 struct arb_program
*Program
, GLint
* File
, GLint
* Index
,
2483 GLboolean
*IsRelOffset
)
2485 struct var_cache
*src
;
2486 GLuint binding_state
, binding_idx
, is_generic
, found
;
2491 /* And the binding for the src */
2492 switch (*(*inst
)++) {
2493 case REGISTER_ATTRIB
:
2494 if (parse_attrib_binding
2495 (ctx
, inst
, Program
, &binding_state
, &binding_idx
, &is_generic
))
2497 *File
= PROGRAM_INPUT
;
2498 *Index
= binding_idx
;
2500 /* We need to insert a dummy variable into the var_cache so we can
2501 * catch generic vertex attrib aliasing errors
2503 var_cache_create(&src
);
2504 src
->type
= vt_attrib
;
2505 src
->name
= (GLubyte
*)_mesa_strdup("Dummy Attrib Variable");
2506 src
->attrib_binding
= binding_state
;
2507 src
->attrib_binding_idx
= binding_idx
;
2508 src
->attrib_is_generic
= is_generic
;
2509 var_cache_append(vc_head
, src
);
2510 if (generic_attrib_check(*vc_head
)) {
2511 _mesa_set_program_error (ctx
, Program
->Position
,
2512 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
2513 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2514 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
2519 case REGISTER_PARAM
:
2521 case PARAM_ARRAY_ELEMENT
:
2523 src
= parse_string (inst
, vc_head
, Program
, &found
);
2524 Program
->Position
= parse_position (inst
);
2527 _mesa_set_program_error (ctx
, Program
->Position
,
2528 "2: Undefined variable");
2529 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2530 "2: Undefined variable: %s", src
->name
);
2534 *File
= src
->param_binding_type
;
2536 switch (*(*inst
)++) {
2537 case ARRAY_INDEX_ABSOLUTE
:
2538 offset
= parse_integer (inst
, Program
);
2541 || (offset
>= (int)src
->param_binding_length
)) {
2542 _mesa_set_program_error (ctx
, Program
->Position
,
2543 "Index out of range");
2544 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2545 "Index %d out of range for %s", offset
,
2550 *Index
= src
->param_binding_begin
+ offset
;
2553 case ARRAY_INDEX_RELATIVE
:
2555 GLint addr_reg_idx
, rel_off
;
2557 /* First, grab the address regiseter */
2558 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &addr_reg_idx
))
2567 /* Then the relative offset */
2568 if (parse_relative_offset(ctx
, inst
, Program
, &rel_off
)) return 1;
2570 /* And store it properly */
2571 *Index
= src
->param_binding_begin
+ rel_off
;
2580 if (parse_param_use (ctx
, inst
, vc_head
, Program
, &src
))
2583 *File
= src
->param_binding_type
;
2584 *Index
= src
->param_binding_begin
;
2589 case REGISTER_ESTABLISHED_NAME
:
2591 src
= parse_string (inst
, vc_head
, Program
, &found
);
2592 Program
->Position
= parse_position (inst
);
2594 /* If the name has never been added to our symbol table, we're hosed */
2596 _mesa_set_program_error (ctx
, Program
->Position
,
2597 "3: Undefined variable");
2598 _mesa_error (ctx
, GL_INVALID_OPERATION
, "3: Undefined variable: %s",
2603 switch (src
->type
) {
2605 *File
= PROGRAM_INPUT
;
2606 *Index
= src
->attrib_binding_idx
;
2609 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2611 *File
= src
->param_binding_type
;
2612 *Index
= src
->param_binding_begin
;
2616 *File
= PROGRAM_TEMPORARY
;
2617 *Index
= src
->temp_binding
;
2620 /* If the var type is vt_output no go */
2622 _mesa_set_program_error (ctx
, Program
->Position
,
2623 "destination register is read only");
2624 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2625 "destination register is read only: %s",
2632 _mesa_set_program_error (ctx
, Program
->Position
,
2633 "Unknown token in parse_src_reg");
2634 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2635 "Unknown token in parse_src_reg");
2645 parse_fp_vector_src_reg(GLcontext
* ctx
, GLubyte
** inst
,
2646 struct var_cache
**vc_head
,
2647 struct arb_program
*program
,
2648 struct fp_src_register
*reg
)
2654 GLboolean isRelOffset
;
2657 negate
= (parse_sign (inst
) == -1) ? 0xf : 0x0;
2659 /* And the src reg */
2660 if (parse_src_reg(ctx
, inst
, vc_head
, program
, &file
, &index
, &isRelOffset
))
2663 /* finally, the swizzle */
2664 parse_swizzle_mask(inst
, swizzle
, 4);
2668 reg
->Abs
= 0; /* NV only */
2669 reg
->NegateAbs
= 0; /* NV only */
2670 reg
->NegateBase
= negate
;
2671 reg
->Swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
2677 parse_fp_dst_reg(GLcontext
* ctx
, GLubyte
** inst
,
2678 struct var_cache
**vc_head
, struct arb_program
*Program
,
2679 struct fp_dst_register
*reg
)
2681 GLint file
, idx
, mask
;
2683 if (parse_masked_dst_reg (ctx
, inst
, vc_head
, Program
, &file
, &idx
, &mask
))
2686 reg
->CondMask
= 0; /* NV only */
2687 reg
->CondSwizzle
= 0; /* NV only */
2690 reg
->WriteMask
= mask
;
2697 parse_fp_scalar_src_reg (GLcontext
* ctx
, GLubyte
** inst
,
2698 struct var_cache
**vc_head
, struct arb_program
*Program
,
2699 struct fp_src_register
*reg
)
2706 GLboolean IsRelOffset
;
2709 Negate
= (parse_sign (inst
) == -1) ? 0x1 : 0x0;
2711 /* And the src reg */
2712 if (parse_src_reg (ctx
, inst
, vc_head
, Program
, &File
, &Index
, &IsRelOffset
))
2715 /* finally, the swizzle */
2716 parse_swizzle_mask(inst
, Swizzle
, 1);
2720 reg
->Abs
= 0; /* NV only */
2721 reg
->NegateAbs
= 0; /* NV only */
2722 reg
->NegateBase
= Negate
;
2723 reg
->Swizzle
= (Swizzle
[0] << 0);
2730 * This is a big mother that handles getting opcodes into the instruction
2731 * and handling the src & dst registers for fragment program instructions
2734 parse_fp_instruction (GLcontext
* ctx
, GLubyte
** inst
,
2735 struct var_cache
**vc_head
, struct arb_program
*Program
,
2736 struct fp_instruction
*fp
)
2740 GLubyte instClass
, type
, code
;
2743 /* No condition codes in ARB_fp */
2744 fp
->UpdateCondRegister
= 0;
2746 /* Record the position in the program string for debugging */
2747 fp
->StringPos
= Program
->Position
;
2751 fp
->DstReg
.File
= 0xf; /* mark as undef */
2752 fp
->SrcReg
[0].File
= 0xf; /* mark as undef */
2753 fp
->SrcReg
[1].File
= 0xf; /* mark as undef */
2754 fp
->SrcReg
[2].File
= 0xf; /* mark as undef */
2756 /* OP_ALU_INST or OP_TEX_INST */
2757 instClass
= *(*inst
)++;
2759 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2760 * OP_TEX_{SAMPLE, KIL}
2764 /* The actual opcode name */
2767 /* Increment the correct count */
2768 switch (instClass
) {
2770 Program
->NumAluInstructions
++;
2773 Program
->NumTexInstructions
++;
2778 fp
->Precision
= FLOAT32
;
2780 fp
->DstReg
.CondMask
= COND_TR
;
2788 fp
->Opcode
= FP_OPCODE_ABS
;
2794 fp
->Opcode
= FP_OPCODE_FLR
;
2800 fp
->Opcode
= FP_OPCODE_FRC
;
2806 fp
->Opcode
= FP_OPCODE_LIT
;
2812 fp
->Opcode
= FP_OPCODE_MOV
;
2816 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2819 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2828 fp
->Opcode
= FP_OPCODE_COS
;
2834 fp
->Opcode
= FP_OPCODE_EX2
;
2840 fp
->Opcode
= FP_OPCODE_LG2
;
2846 fp
->Opcode
= FP_OPCODE_RCP
;
2852 fp
->Opcode
= FP_OPCODE_RSQ
;
2858 fp
->Opcode
= FP_OPCODE_SIN
;
2865 fp
->Opcode
= FP_OPCODE_SCS
;
2869 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2872 if (parse_fp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2881 fp
->Opcode
= FP_OPCODE_POW
;
2885 if (parse_fp_dst_reg(ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2888 for (a
= 0; a
< 2; a
++) {
2889 if (parse_fp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2900 fp
->Opcode
= FP_OPCODE_ADD
;
2906 fp
->Opcode
= FP_OPCODE_DP3
;
2912 fp
->Opcode
= FP_OPCODE_DP4
;
2918 fp
->Opcode
= FP_OPCODE_DPH
;
2924 fp
->Opcode
= FP_OPCODE_DST
;
2930 fp
->Opcode
= FP_OPCODE_MAX
;
2936 fp
->Opcode
= FP_OPCODE_MIN
;
2942 fp
->Opcode
= FP_OPCODE_MUL
;
2948 fp
->Opcode
= FP_OPCODE_SGE
;
2954 fp
->Opcode
= FP_OPCODE_SLT
;
2960 fp
->Opcode
= FP_OPCODE_SUB
;
2966 fp
->Opcode
= FP_OPCODE_XPD
;
2970 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2972 for (a
= 0; a
< 2; a
++) {
2973 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2983 fp
->Opcode
= FP_OPCODE_CMP
;
2989 fp
->Opcode
= FP_OPCODE_LRP
;
2995 fp
->Opcode
= FP_OPCODE_MAD
;
2999 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3002 for (a
= 0; a
< 3; a
++) {
3003 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
3013 fp
->Opcode
= FP_OPCODE_SWZ
;
3016 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3024 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
, &rel
))
3026 parse_extended_swizzle_mask(inst
, swizzle
, &negateMask
);
3027 fp
->SrcReg
[0].File
= file
;
3028 fp
->SrcReg
[0].Index
= index
;
3029 fp
->SrcReg
[0].NegateBase
= negateMask
;
3030 fp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
3042 fp
->Opcode
= FP_OPCODE_TEX
;
3048 fp
->Opcode
= FP_OPCODE_TXP
;
3054 fp
->Opcode
= FP_OPCODE_TXB
;
3058 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3061 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
3065 if (parse_texcoord_num (ctx
, inst
, Program
, &texcoord
))
3067 fp
->TexSrcUnit
= texcoord
;
3070 switch (*(*inst
)++) {
3072 fp
->TexSrcIdx
= TEXTURE_1D_INDEX
;
3075 fp
->TexSrcIdx
= TEXTURE_2D_INDEX
;
3078 fp
->TexSrcIdx
= TEXTURE_3D_INDEX
;
3080 case TEXTARGET_RECT
:
3081 fp
->TexSrcIdx
= TEXTURE_RECT_INDEX
;
3083 case TEXTARGET_CUBE
:
3084 fp
->TexSrcIdx
= TEXTURE_CUBE_INDEX
;
3086 case TEXTARGET_SHADOW1D
:
3087 case TEXTARGET_SHADOW2D
:
3088 case TEXTARGET_SHADOWRECT
:
3089 /* TODO ARB_fragment_program_shadow code */
3092 Program
->TexturesUsed
[texcoord
] |= (1<<fp
->TexSrcIdx
);
3096 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
3098 fp
->Opcode
= FP_OPCODE_KIL
;
3106 parse_vp_dst_reg(GLcontext
* ctx
, GLubyte
** inst
,
3107 struct var_cache
**vc_head
, struct arb_program
*Program
,
3108 struct vp_dst_register
*reg
)
3110 GLint file
, idx
, mask
;
3112 if (parse_masked_dst_reg(ctx
, inst
, vc_head
, Program
, &file
, &idx
, &mask
))
3117 reg
->WriteMask
= mask
;
3122 * Handle the parsing out of a masked address register
3124 * \param Index - The register index we write to
3125 * \param WriteMask - The mask controlling which components we write (1->write)
3127 * \return 0 on sucess, 1 on error
3130 parse_vp_address_reg (GLcontext
* ctx
, GLubyte
** inst
,
3131 struct var_cache
**vc_head
,
3132 struct arb_program
*Program
,
3133 struct vp_dst_register
*reg
)
3137 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &idx
))
3140 /* This should be 0x8 */
3143 reg
->File
= PROGRAM_ADDRESS
;
3146 /* Writemask of .x is implied */
3147 reg
->WriteMask
= 0x1;
3154 parse_vp_vector_src_reg(GLcontext
* ctx
, GLubyte
** inst
,
3155 struct var_cache
**vc_head
,
3156 struct arb_program
*program
,
3157 struct vp_src_register
*reg
)
3163 GLboolean isRelOffset
;
3166 negate
= (parse_sign (inst
) == -1) ? 0xf : 0x0;
3168 /* And the src reg */
3169 if (parse_src_reg (ctx
, inst
, vc_head
, program
, &file
, &index
, &isRelOffset
))
3172 /* finally, the swizzle */
3173 parse_swizzle_mask(inst
, swizzle
, 4);
3177 reg
->Swizzle
= MAKE_SWIZZLE4(swizzle
[0], swizzle
[1],
3178 swizzle
[2], swizzle
[3]);
3179 reg
->Negate
= negate
;
3180 reg
->RelAddr
= isRelOffset
;
3186 parse_vp_scalar_src_reg (GLcontext
* ctx
, GLubyte
** inst
,
3187 struct var_cache
**vc_head
, struct arb_program
*Program
,
3188 struct vp_src_register
*reg
)
3195 GLboolean IsRelOffset
;
3198 Negate
= (parse_sign (inst
) == -1) ? 0x1 : 0x0;
3200 /* And the src reg */
3201 if (parse_src_reg (ctx
, inst
, vc_head
, Program
, &File
, &Index
, &IsRelOffset
))
3204 /* finally, the swizzle */
3205 parse_swizzle_mask(inst
, Swizzle
, 1);
3209 reg
->Swizzle
= (Swizzle
[0] << 0);
3210 reg
->Negate
= Negate
;
3211 reg
->RelAddr
= IsRelOffset
;
3217 * This is a big mother that handles getting opcodes into the instruction
3218 * and handling the src & dst registers for vertex program instructions
3221 parse_vp_instruction (GLcontext
* ctx
, GLubyte
** inst
,
3222 struct var_cache
**vc_head
, struct arb_program
*Program
,
3223 struct vp_instruction
*vp
)
3228 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3231 /* The actual opcode name */
3234 /* Record the position in the program string for debugging */
3235 vp
->StringPos
= Program
->Position
;
3237 vp
->SrcReg
[0].RelAddr
= vp
->SrcReg
[1].RelAddr
= vp
->SrcReg
[2].RelAddr
= 0;
3238 vp
->SrcReg
[0].Swizzle
= SWIZZLE_NOOP
;
3239 vp
->SrcReg
[1].Swizzle
= SWIZZLE_NOOP
;
3240 vp
->SrcReg
[2].Swizzle
= SWIZZLE_NOOP
;
3241 vp
->SrcReg
[3].Swizzle
= SWIZZLE_NOOP
;
3242 vp
->DstReg
.WriteMask
= 0xf;
3247 vp
->Opcode
= VP_OPCODE_ARL
;
3249 /* Remember to set SrcReg.RelAddr; */
3251 /* Get the masked address register [dst] */
3252 if (parse_vp_address_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3255 vp
->DstReg
.File
= PROGRAM_ADDRESS
;
3257 /* Get a scalar src register */
3258 if (parse_vp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3266 vp
->Opcode
= VP_OPCODE_ABS
;
3269 vp
->Opcode
= VP_OPCODE_FLR
;
3272 vp
->Opcode
= VP_OPCODE_FRC
;
3275 vp
->Opcode
= VP_OPCODE_LIT
;
3278 vp
->Opcode
= VP_OPCODE_MOV
;
3282 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3285 if (parse_vp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3292 vp
->Opcode
= VP_OPCODE_EX2
;
3295 vp
->Opcode
= VP_OPCODE_EXP
;
3298 vp
->Opcode
= VP_OPCODE_LG2
;
3301 vp
->Opcode
= VP_OPCODE_LOG
;
3304 vp
->Opcode
= VP_OPCODE_RCP
;
3307 vp
->Opcode
= VP_OPCODE_RSQ
;
3310 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3313 if (parse_vp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3320 vp
->Opcode
= VP_OPCODE_POW
;
3323 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3326 for (a
= 0; a
< 2; a
++) {
3327 if (parse_vp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3335 vp
->Opcode
= VP_OPCODE_ADD
;
3338 vp
->Opcode
= VP_OPCODE_DP3
;
3341 vp
->Opcode
= VP_OPCODE_DP4
;
3344 vp
->Opcode
= VP_OPCODE_DPH
;
3347 vp
->Opcode
= VP_OPCODE_DST
;
3350 vp
->Opcode
= VP_OPCODE_MAX
;
3353 vp
->Opcode
= VP_OPCODE_MIN
;
3356 vp
->Opcode
= VP_OPCODE_MUL
;
3359 vp
->Opcode
= VP_OPCODE_SGE
;
3362 vp
->Opcode
= VP_OPCODE_SLT
;
3365 vp
->Opcode
= VP_OPCODE_SUB
;
3368 vp
->Opcode
= VP_OPCODE_XPD
;
3371 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3374 for (a
= 0; a
< 2; a
++) {
3375 if (parse_vp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3383 vp
->Opcode
= VP_OPCODE_MAD
;
3387 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3390 for (a
= 0; a
< 3; a
++) {
3391 if (parse_vp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3399 vp
->Opcode
= VP_OPCODE_SWZ
;
3408 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3411 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &file
, &index
, &relAddr
))
3413 parse_extended_swizzle_mask (inst
, swizzle
, &negateMask
);
3414 vp
->SrcReg
[0].File
= file
;
3415 vp
->SrcReg
[0].Index
= index
;
3416 vp
->SrcReg
[0].Negate
= negateMask
;
3417 vp
->SrcReg
[0].Swizzle
= MAKE_SWIZZLE4(swizzle
[0],
3421 vp
->SrcReg
[0].RelAddr
= relAddr
;
3431 print_state_token (GLint token
)
3434 case STATE_MATERIAL
:
3435 fprintf (stderr
, "STATE_MATERIAL ");
3438 fprintf (stderr
, "STATE_LIGHT ");
3441 case STATE_LIGHTMODEL_AMBIENT
:
3442 fprintf (stderr
, "STATE_AMBIENT ");
3445 case STATE_LIGHTMODEL_SCENECOLOR
:
3446 fprintf (stderr
, "STATE_SCENECOLOR ");
3449 case STATE_LIGHTPROD
:
3450 fprintf (stderr
, "STATE_LIGHTPROD ");
3454 fprintf (stderr
, "STATE_TEXGEN ");
3457 case STATE_FOG_COLOR
:
3458 fprintf (stderr
, "STATE_FOG_COLOR ");
3461 case STATE_FOG_PARAMS
:
3462 fprintf (stderr
, "STATE_FOG_PARAMS ");
3465 case STATE_CLIPPLANE
:
3466 fprintf (stderr
, "STATE_CLIPPLANE ");
3469 case STATE_POINT_SIZE
:
3470 fprintf (stderr
, "STATE_POINT_SIZE ");
3473 case STATE_POINT_ATTENUATION
:
3474 fprintf (stderr
, "STATE_ATTENUATION ");
3478 fprintf (stderr
, "STATE_MATRIX ");
3481 case STATE_MODELVIEW
:
3482 fprintf (stderr
, "STATE_MODELVIEW ");
3485 case STATE_PROJECTION
:
3486 fprintf (stderr
, "STATE_PROJECTION ");
3490 fprintf (stderr
, "STATE_MVP ");
3494 fprintf (stderr
, "STATE_TEXTURE ");
3498 fprintf (stderr
, "STATE_PROGRAM ");
3501 case STATE_MATRIX_INVERSE
:
3502 fprintf (stderr
, "STATE_INVERSE ");
3505 case STATE_MATRIX_TRANSPOSE
:
3506 fprintf (stderr
, "STATE_TRANSPOSE ");
3509 case STATE_MATRIX_INVTRANS
:
3510 fprintf (stderr
, "STATE_INVTRANS ");
3514 fprintf (stderr
, "STATE_AMBIENT ");
3518 fprintf (stderr
, "STATE_DIFFUSE ");
3521 case STATE_SPECULAR
:
3522 fprintf (stderr
, "STATE_SPECULAR ");
3525 case STATE_EMISSION
:
3526 fprintf (stderr
, "STATE_EMISSION ");
3529 case STATE_SHININESS
:
3530 fprintf (stderr
, "STATE_SHININESS ");
3534 fprintf (stderr
, "STATE_HALF ");
3537 case STATE_POSITION
:
3538 fprintf (stderr
, "STATE_POSITION ");
3541 case STATE_ATTENUATION
:
3542 fprintf (stderr
, "STATE_ATTENUATION ");
3545 case STATE_SPOT_DIRECTION
:
3546 fprintf (stderr
, "STATE_DIRECTION ");
3549 case STATE_TEXGEN_EYE_S
:
3550 fprintf (stderr
, "STATE_TEXGEN_EYE_S ");
3553 case STATE_TEXGEN_EYE_T
:
3554 fprintf (stderr
, "STATE_TEXGEN_EYE_T ");
3557 case STATE_TEXGEN_EYE_R
:
3558 fprintf (stderr
, "STATE_TEXGEN_EYE_R ");
3561 case STATE_TEXGEN_EYE_Q
:
3562 fprintf (stderr
, "STATE_TEXGEN_EYE_Q ");
3565 case STATE_TEXGEN_OBJECT_S
:
3566 fprintf (stderr
, "STATE_TEXGEN_EYE_S ");
3569 case STATE_TEXGEN_OBJECT_T
:
3570 fprintf (stderr
, "STATE_TEXGEN_OBJECT_T ");
3573 case STATE_TEXGEN_OBJECT_R
:
3574 fprintf (stderr
, "STATE_TEXGEN_OBJECT_R ");
3577 case STATE_TEXGEN_OBJECT_Q
:
3578 fprintf (stderr
, "STATE_TEXGEN_OBJECT_Q ");
3581 case STATE_TEXENV_COLOR
:
3582 fprintf (stderr
, "STATE_TEXENV_COLOR ");
3585 case STATE_DEPTH_RANGE
:
3586 fprintf (stderr
, "STATE_DEPTH_RANGE ");
3589 case STATE_VERTEX_PROGRAM
:
3590 fprintf (stderr
, "STATE_VERTEX_PROGRAM ");
3593 case STATE_FRAGMENT_PROGRAM
:
3594 fprintf (stderr
, "STATE_FRAGMENT_PROGRAM ");
3598 fprintf (stderr
, "STATE_ENV ");
3602 fprintf (stderr
, "STATE_LOCAL ");
3606 fprintf (stderr
, "[%d] ", token
);
3611 debug_variables (GLcontext
* ctx
, struct var_cache
*vc_head
,
3612 struct arb_program
*Program
)
3614 struct var_cache
*vc
;
3617 fprintf (stderr
, "debug_variables, vc_head: %x\n", vc_head
);
3619 /* First of all, print out the contents of the var_cache */
3622 fprintf (stderr
, "[%x]\n", vc
);
3625 fprintf (stderr
, "UNDEFINED %s\n", vc
->name
);
3628 fprintf (stderr
, "ATTRIB %s\n", vc
->name
);
3629 fprintf (stderr
, " binding: 0x%x\n", vc
->attrib_binding
);
3632 fprintf (stderr
, "PARAM %s begin: %d len: %d\n", vc
->name
,
3633 vc
->param_binding_begin
, vc
->param_binding_length
);
3634 b
= vc
->param_binding_begin
;
3635 for (a
= 0; a
< vc
->param_binding_length
; a
++) {
3636 fprintf (stderr
, "%s\n",
3637 Program
->Parameters
->Parameters
[a
+ b
].Name
);
3638 if (Program
->Parameters
->Parameters
[a
+ b
].Type
== STATE
) {
3639 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3641 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3643 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3645 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3647 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3649 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3653 fprintf (stderr
, "%f %f %f %f\n",
3654 Program
->Parameters
->Parameters
[a
+ b
].Values
[0],
3655 Program
->Parameters
->Parameters
[a
+ b
].Values
[1],
3656 Program
->Parameters
->Parameters
[a
+ b
].Values
[2],
3657 Program
->Parameters
->Parameters
[a
+ b
].Values
[3]);
3661 fprintf (stderr
, "TEMP %s\n", vc
->name
);
3662 fprintf (stderr
, " binding: 0x%x\n", vc
->temp_binding
);
3665 fprintf (stderr
, "OUTPUT %s\n", vc
->name
);
3666 fprintf (stderr
, " binding: 0x%x\n", vc
->output_binding
);
3669 fprintf (stderr
, "ALIAS %s\n", vc
->name
);
3670 fprintf (stderr
, " binding: 0x%x (%s)\n",
3671 vc
->alias_binding
, vc
->alias_binding
->name
);
3682 * The main loop for parsing a fragment or vertex program
3684 * \return GL_TRUE on success, GL_FALSE on error.
3687 parse_arb_program(GLcontext
* ctx
, GLubyte
* inst
, struct var_cache
**vc_head
,
3688 struct arb_program
*Program
)
3692 Program
->MajorVersion
= (GLuint
) * inst
++;
3693 Program
->MinorVersion
= (GLuint
) * inst
++;
3695 while (*inst
!= END
) {
3700 case ARB_PRECISION_HINT_FASTEST
:
3701 Program
->PrecisionOption
= GL_FASTEST
;
3704 case ARB_PRECISION_HINT_NICEST
:
3705 Program
->PrecisionOption
= GL_NICEST
;
3709 Program
->FogOption
= GL_EXP
;
3713 Program
->FogOption
= GL_EXP2
;
3716 case ARB_FOG_LINEAR
:
3717 Program
->FogOption
= GL_LINEAR
;
3720 case ARB_POSITION_INVARIANT
:
3721 if (Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
3722 Program
->HintPositionInvariant
= 1;
3725 case ARB_FRAGMENT_PROGRAM_SHADOW
:
3726 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3727 /* TODO ARB_fragment_program_shadow code */
3731 case ARB_DRAW_BUFFERS
:
3732 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3733 /* do nothing for now */
3740 Program
->Position
= parse_position (&inst
);
3742 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3744 /* Check the instruction count
3745 * XXX: Does END count as an instruction?
3747 if (Program
->Base
.NumInstructions
+1 == MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS
) {
3748 _mesa_set_program_error (ctx
, Program
->Position
,
3749 "Max instruction count exceeded!");
3750 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3751 "Max instruction count exceeded!");
3754 /* Realloc Program->FPInstructions */
3755 Program
->FPInstructions
=
3756 (struct fp_instruction
*) _mesa_realloc (Program
->FPInstructions
,
3757 Program
->Base
.NumInstructions
*sizeof(struct fp_instruction
),
3758 (Program
->Base
.NumInstructions
+1)*sizeof (struct fp_instruction
));
3760 /* parse the current instruction */
3761 err
= parse_fp_instruction (ctx
, &inst
, vc_head
, Program
,
3762 &Program
->FPInstructions
[Program
->Base
.NumInstructions
]);
3766 /* Check the instruction count
3767 * XXX: Does END count as an instruction?
3769 if (Program
->Base
.NumInstructions
+1 == MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
) {
3770 _mesa_set_program_error (ctx
, Program
->Position
,
3771 "Max instruction count exceeded!");
3772 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3773 "Max instruction count exceeded!");
3776 /* Realloc Program->VPInstructions */
3777 Program
->VPInstructions
=
3778 (struct vp_instruction
*) _mesa_realloc (Program
->VPInstructions
,
3779 Program
->Base
.NumInstructions
*sizeof(struct vp_instruction
),
3780 (Program
->Base
.NumInstructions
+1)*sizeof(struct vp_instruction
));
3782 /* parse the current instruction */
3783 err
= parse_vp_instruction (ctx
, &inst
, vc_head
, Program
,
3784 &Program
->VPInstructions
[Program
->Base
.NumInstructions
]);
3787 /* increment Program->Base.NumInstructions */
3788 Program
->Base
.NumInstructions
++;
3792 err
= parse_declaration (ctx
, &inst
, vc_head
, Program
);
3803 /* Finally, tag on an OPCODE_END instruction */
3804 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3805 Program
->FPInstructions
=
3806 (struct fp_instruction
*) _mesa_realloc (Program
->FPInstructions
,
3807 Program
->Base
.NumInstructions
*sizeof(struct fp_instruction
),
3808 (Program
->Base
.NumInstructions
+1)*sizeof(struct fp_instruction
));
3810 Program
->FPInstructions
[Program
->Base
.NumInstructions
].Opcode
= FP_OPCODE_END
;
3811 Program
->FPInstructions
[Program
->Base
.NumInstructions
].Saturate
= 0;
3812 Program
->FPInstructions
[Program
->Base
.NumInstructions
].DstReg
.File
= 0xf;
3813 Program
->FPInstructions
[Program
->Base
.NumInstructions
].SrcReg
[0].File
= 0xf;
3814 Program
->FPInstructions
[Program
->Base
.NumInstructions
].SrcReg
[1].File
= 0xf;
3815 Program
->FPInstructions
[Program
->Base
.NumInstructions
].SrcReg
[2].File
= 0xf;
3816 /* YYY Wrong Position in program, whatever, at least not random -> crash
3817 Program->Position = parse_position (&inst);
3819 Program
->FPInstructions
[Program
->Base
.NumInstructions
].StringPos
= Program
->Position
;
3820 Program
->FPInstructions
[Program
->Base
.NumInstructions
].Data
= NULL
;
3823 Program
->VPInstructions
=
3824 (struct vp_instruction
*) _mesa_realloc (Program
->VPInstructions
,
3825 Program
->Base
.NumInstructions
*sizeof(struct vp_instruction
),
3826 (Program
->Base
.NumInstructions
+1)*sizeof(struct vp_instruction
));
3828 Program
->VPInstructions
[Program
->Base
.NumInstructions
].Opcode
= VP_OPCODE_END
;
3829 /* YYY Wrong Position in program, whatever, at least not random -> crash
3830 Program->Position = parse_position (&inst);
3832 Program
->VPInstructions
[Program
->Base
.NumInstructions
].StringPos
= Program
->Position
;
3833 Program
->VPInstructions
[Program
->Base
.NumInstructions
].Data
= NULL
;
3836 /* increment Program->Base.NumInstructions */
3837 Program
->Base
.NumInstructions
++;
3843 __extension__
static char core_grammar_text
[] =
3844 #include "grammar_syn.h"
3847 static int set_reg8 (GLcontext
*ctx
, grammar id
, const byte
*name
, byte value
)
3849 char error_msg
[300];
3852 if (grammar_set_reg8 (id
, name
, value
))
3855 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3856 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3857 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Grammar Register Error");
3861 static int extension_is_supported (const GLubyte
*ext
)
3863 const GLubyte
*extensions
= CALL_GetString(GET_DISPATCH(), (GL_EXTENSIONS
));
3864 const GLubyte
*end
= extensions
+ _mesa_strlen ((const char *) extensions
);
3865 const GLint ext_len
= (GLint
)_mesa_strlen ((const char *) ext
);
3867 while (extensions
< end
)
3869 const GLubyte
*name_end
= (const GLubyte
*) _mesa_strstr ((const char *) extensions
, " ");
3870 if (name_end
== NULL
)
3872 if (name_end
- extensions
== ext_len
&& _mesa_strncmp ((const char *) ext
,
3873 (const char *) extensions
, ext_len
) == 0)
3875 extensions
= name_end
+ 1;
3881 static int enable_ext (GLcontext
*ctx
, grammar id
, const byte
*name
, const byte
*extname
)
3883 if (extension_is_supported (extname
))
3884 if (set_reg8 (ctx
, id
, name
, 0x01))
3890 * This kicks everything off.
3892 * \param ctx - The GL Context
3893 * \param str - The program string
3894 * \param len - The program string length
3895 * \param program - The arb_program struct to return all the parsed info in
3896 * \return GL_TRUE on sucess, GL_FALSE on error
3899 _mesa_parse_arb_program (GLcontext
* ctx
, const GLubyte
* str
, GLsizei len
,
3900 struct arb_program
* program
)
3902 GLint a
, err
, error_pos
;
3903 char error_msg
[300];
3905 struct var_cache
*vc_head
;
3906 grammar arbprogram_syn_id
;
3907 GLubyte
*parsed
, *inst
;
3908 GLubyte
*strz
= NULL
;
3909 static int arbprogram_syn_is_ok
= 0; /* XXX temporary */
3911 /* Reset error state */
3912 _mesa_set_program_error(ctx
, -1, NULL
);
3915 fprintf (stderr
, "Loading grammar text!\n");
3918 /* check if the arb_grammar_text (arbprogram.syn) is syntactically correct */
3919 if (!arbprogram_syn_is_ok
) {
3920 grammar grammar_syn_id
;
3925 grammar_syn_id
= grammar_load_from_text ((byte
*) core_grammar_text
);
3926 if (grammar_syn_id
== 0) {
3927 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3928 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3929 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3930 "Error loading grammar rule set");
3934 err
= grammar_check (grammar_syn_id
, (byte
*) arb_grammar_text
, &parsed
, &parsed_len
);
3936 /* NOTE: we can't destroy grammar_syn_id right here because
3937 * grammar_destroy() can reset the last error
3940 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3941 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3942 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Error loading grammar rule set");
3944 grammar_destroy (grammar_syn_id
);
3948 grammar_destroy (grammar_syn_id
);
3950 arbprogram_syn_is_ok
= 1;
3953 /* create the grammar object */
3954 arbprogram_syn_id
= grammar_load_from_text ((byte
*) arb_grammar_text
);
3955 if (arbprogram_syn_id
== 0) {
3956 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
3957 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3958 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3959 "Error loading grammer rule set");
3963 /* Set program_target register value */
3964 if (set_reg8 (ctx
, arbprogram_syn_id
, (byte
*) "program_target",
3965 program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
? 0x10 : 0x20)) {
3966 grammar_destroy (arbprogram_syn_id
);
3970 /* Enable all active extensions */
3971 if (enable_ext (ctx
, arbprogram_syn_id
,
3972 (byte
*) "vertex_blend", (byte
*) "GL_ARB_vertex_blend") ||
3973 enable_ext (ctx
, arbprogram_syn_id
,
3974 (byte
*) "vertex_blend", (byte
*) "GL_EXT_vertex_weighting") ||
3975 enable_ext (ctx
, arbprogram_syn_id
,
3976 (byte
*) "matrix_palette", (byte
*) "GL_ARB_matrix_palette") ||
3977 enable_ext (ctx
, arbprogram_syn_id
,
3978 (byte
*) "point_parameters", (byte
*) "GL_ARB_point_parameters") ||
3979 enable_ext (ctx
, arbprogram_syn_id
,
3980 (byte
*) "point_parameters", (byte
*) "GL_EXT_point_parameters") ||
3981 enable_ext (ctx
, arbprogram_syn_id
,
3982 (byte
*) "secondary_color", (byte
*) "GL_EXT_secondary_color") ||
3983 enable_ext (ctx
, arbprogram_syn_id
,
3984 (byte
*) "fog_coord", (byte
*) "GL_EXT_fog_coord") ||
3985 enable_ext (ctx
, arbprogram_syn_id
,
3986 (byte
*) "texture_rectangle", (byte
*) "GL_ARB_texture_rectangle") ||
3987 enable_ext (ctx
, arbprogram_syn_id
,
3988 (byte
*) "texture_rectangle", (byte
*) "GL_EXT_texture_rectangle") ||
3989 enable_ext (ctx
, arbprogram_syn_id
,
3990 (byte
*) "texture_rectangle", (byte
*) "GL_NV_texture_rectangle") ||
3991 enable_ext (ctx
, arbprogram_syn_id
,
3992 (byte
*) "fragment_program_shadow", (byte
*) "GL_ARB_fragment_program_shadow") ||
3993 enable_ext (ctx
, arbprogram_syn_id
,
3994 (byte
*) "draw_buffers", (byte
*) "GL_ARB_draw_buffers")) {
3995 grammar_destroy (arbprogram_syn_id
);
3999 /* check for NULL character occurences */
4002 for (i
= 0; i
< len
; i
++)
4003 if (str
[i
] == '\0') {
4004 _mesa_set_program_error (ctx
, i
, "invalid character");
4005 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Lexical Error");
4007 grammar_destroy (arbprogram_syn_id
);
4012 /* copy the program string to a null-terminated string */
4013 strz
= (GLubyte
*) _mesa_malloc (len
+ 1);
4015 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glprogramStringARB");
4018 _mesa_memcpy (strz
, str
, len
);
4022 fprintf (stderr
, "Checking Grammar!\n");
4024 /* do a fast check on program string - initial production buffer is 4K */
4025 err
= grammar_fast_check (arbprogram_syn_id
, strz
, &parsed
, &parsed_len
, 0x1000);
4027 /* Syntax parse error */
4030 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
4031 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
4032 _mesa_error (ctx
, GL_INVALID_OPERATION
, "glprogramStringARB(syntax error)");
4034 /* useful for debugging */
4039 fprintf(stderr
, "program: %s\n", (char *) strz
);
4040 fprintf(stderr
, "Error Pos: %d\n", ctx
->program
.ErrorPos
);
4041 s
= (char *) _mesa_find_line_column(strz
, strz
+ctx
->program
.ErrorPos
, &line
, &col
);
4042 fprintf(stderr
, "line %d col %d: %s\n", line
, col
, s
);
4046 grammar_destroy (arbprogram_syn_id
);
4051 fprintf (stderr
, "Destroying grammer dict [parse retval: %d]\n", err
);
4053 grammar_destroy (arbprogram_syn_id
);
4055 /* Initialize the arb_program struct */
4056 program
->Base
.String
= strz
;
4057 program
->Base
.NumInstructions
=
4058 program
->Base
.NumTemporaries
=
4059 program
->Base
.NumParameters
=
4060 program
->Base
.NumAttributes
= program
->Base
.NumAddressRegs
= 0;
4061 program
->Parameters
= _mesa_new_parameter_list ();
4062 program
->InputsRead
= 0;
4063 program
->OutputsWritten
= 0;
4064 program
->Position
= 0;
4065 program
->MajorVersion
= program
->MinorVersion
= 0;
4066 program
->PrecisionOption
= GL_DONT_CARE
;
4067 program
->FogOption
= GL_NONE
;
4068 program
->HintPositionInvariant
= GL_FALSE
;
4069 for (a
= 0; a
< MAX_TEXTURE_IMAGE_UNITS
; a
++)
4070 program
->TexturesUsed
[a
] = 0;
4071 program
->NumAluInstructions
=
4072 program
->NumTexInstructions
=
4073 program
->NumTexIndirections
= 0;
4075 program
->FPInstructions
= NULL
;
4076 program
->VPInstructions
= NULL
;
4081 /* Start examining the tokens in the array */
4084 /* Check the grammer rev */
4085 if (*inst
++ != REVISION
) {
4086 _mesa_set_program_error (ctx
, 0, "Grammar version mismatch");
4087 _mesa_error(ctx
, GL_INVALID_OPERATION
,
4088 "glProgramStringARB(Grammar verison mismatch)");
4092 /* ignore program target */
4095 err
= parse_arb_program (ctx
, inst
, &vc_head
, program
);
4097 fprintf (stderr
, "Symantic analysis returns %d [1 is bad!]\n", err
);
4101 /*debug_variables(ctx, vc_head, program); */
4103 /* We're done with the parsed binary array */
4104 var_cache_destroy (&vc_head
);
4106 _mesa_free (parsed
);
4108 fprintf (stderr
, "_mesa_parse_arb_program() done\n");