2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 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 (!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 lenght does not match parameter list");
1953 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1954 "Declared parameter array lenght 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 * The values in the input stream are:
2399 * COMPONENT_X -> x/r
2400 * COMPONENT_Y -> y/g
2404 * The values in the output mask are:
2410 * The len parameter allows us to grab 4 components for a vector
2411 * swizzle, or just 1 component for a scalar src register selection
2414 parse_swizzle_mask (GLubyte
** inst
, GLubyte
* mask
, GLint len
)
2418 for (a
= 0; a
< 4; a
++)
2421 for (a
= 0; a
< len
; a
++) {
2422 switch (*(*inst
)++) {
2447 parse_extended_swizzle_mask (GLubyte
** inst
, GLubyte
* mask
, GLboolean
* Negate
)
2453 for (a
= 0; a
< 4; a
++) {
2454 if (parse_sign (inst
) == -1)
2461 mask
[a
] = SWIZZLE_ZERO
;
2464 mask
[a
] = SWIZZLE_ONE
;
2467 mask
[a
] = SWIZZLE_X
;
2470 mask
[a
] = SWIZZLE_Y
;
2473 mask
[a
] = SWIZZLE_Z
;
2476 mask
[a
] = SWIZZLE_W
;
2482 mask
[a
] = SWIZZLE_ZERO
;
2484 mask
[a
] = SWIZZLE_ONE
;
2496 parse_src_reg (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2497 struct arb_program
*Program
, GLint
* File
, GLint
* Index
,
2498 GLboolean
*IsRelOffset
)
2500 struct var_cache
*src
;
2501 GLuint binding_state
, binding_idx
, is_generic
, found
;
2506 /* And the binding for the src */
2507 switch (*(*inst
)++) {
2508 case REGISTER_ATTRIB
:
2509 if (parse_attrib_binding
2510 (ctx
, inst
, Program
, &binding_state
, &binding_idx
, &is_generic
))
2512 *File
= PROGRAM_INPUT
;
2513 *Index
= binding_idx
;
2515 /* We need to insert a dummy variable into the var_cache so we can
2516 * catch generic vertex attrib aliasing errors
2518 var_cache_create(&src
);
2519 src
->type
= vt_attrib
;
2520 src
->name
= (GLubyte
*)_mesa_strdup("Dummy Attrib Variable");
2521 src
->attrib_binding
= binding_state
;
2522 src
->attrib_binding_idx
= binding_idx
;
2523 src
->attrib_is_generic
= is_generic
;
2524 var_cache_append(vc_head
, src
);
2525 if (generic_attrib_check(*vc_head
)) {
2526 _mesa_set_program_error (ctx
, Program
->Position
,
2527 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
2528 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2529 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
2534 case REGISTER_PARAM
:
2536 case PARAM_ARRAY_ELEMENT
:
2538 src
= parse_string (inst
, vc_head
, Program
, &found
);
2539 Program
->Position
= parse_position (inst
);
2542 _mesa_set_program_error (ctx
, Program
->Position
,
2543 "2: Undefined variable");
2544 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2545 "2: Undefined variable: %s", src
->name
);
2549 *File
= src
->param_binding_type
;
2551 switch (*(*inst
)++) {
2552 case ARRAY_INDEX_ABSOLUTE
:
2553 offset
= parse_integer (inst
, Program
);
2556 || (offset
>= (int)src
->param_binding_length
)) {
2557 _mesa_set_program_error (ctx
, Program
->Position
,
2558 "Index out of range");
2559 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2560 "Index %d out of range for %s", offset
,
2565 *Index
= src
->param_binding_begin
+ offset
;
2568 case ARRAY_INDEX_RELATIVE
:
2570 GLint addr_reg_idx
, rel_off
;
2572 /* First, grab the address regiseter */
2573 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &addr_reg_idx
))
2582 /* Then the relative offset */
2583 if (parse_relative_offset(ctx
, inst
, Program
, &rel_off
)) return 1;
2585 /* And store it properly */
2586 *Index
= src
->param_binding_begin
+ rel_off
;
2595 if (parse_param_use (ctx
, inst
, vc_head
, Program
, &src
))
2598 *File
= src
->param_binding_type
;
2599 *Index
= src
->param_binding_begin
;
2604 case REGISTER_ESTABLISHED_NAME
:
2606 src
= parse_string (inst
, vc_head
, Program
, &found
);
2607 Program
->Position
= parse_position (inst
);
2609 /* If the name has never been added to our symbol table, we're hosed */
2611 _mesa_set_program_error (ctx
, Program
->Position
,
2612 "3: Undefined variable");
2613 _mesa_error (ctx
, GL_INVALID_OPERATION
, "3: Undefined variable: %s",
2618 switch (src
->type
) {
2620 *File
= PROGRAM_INPUT
;
2621 *Index
= src
->attrib_binding_idx
;
2624 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2626 *File
= src
->param_binding_type
;
2627 *Index
= src
->param_binding_begin
;
2631 *File
= PROGRAM_TEMPORARY
;
2632 *Index
= src
->temp_binding
;
2635 /* If the var type is vt_output no go */
2637 _mesa_set_program_error (ctx
, Program
->Position
,
2638 "destination register is read only");
2639 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2640 "destination register is read only: %s",
2647 _mesa_set_program_error (ctx
, Program
->Position
,
2648 "Unknown token in parse_src_reg");
2649 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2650 "Unknown token in parse_src_reg");
2660 parse_fp_vector_src_reg (GLcontext
* ctx
, GLubyte
** inst
,
2661 struct var_cache
**vc_head
, struct arb_program
*Program
,
2662 struct fp_src_register
*reg
)
2669 GLboolean IsRelOffset
;
2672 Negate
= (parse_sign (inst
) == -1) ? 0xf : 0x0;
2674 /* And the src reg */
2675 if (parse_src_reg (ctx
, inst
, vc_head
, Program
, &File
, &Index
, &IsRelOffset
))
2678 /* finally, the swizzle */
2679 parse_swizzle_mask (inst
, Swizzle
, 4);
2683 reg
->Abs
= 0; /* NV only */
2684 reg
->NegateAbs
= 0; /* NV only */
2685 reg
->NegateBase
= Negate
;
2686 reg
->Swizzle
= (Swizzle
[0] << 0 |
2696 parse_fp_dst_reg(GLcontext
* ctx
, GLubyte
** inst
,
2697 struct var_cache
**vc_head
, struct arb_program
*Program
,
2698 struct fp_dst_register
*reg
)
2700 GLint file
, idx
, mask
;
2702 if (parse_masked_dst_reg (ctx
, inst
, vc_head
, Program
, &file
, &idx
, &mask
))
2705 reg
->CondMask
= 0; /* NV only */
2706 reg
->CondSwizzle
= 0; /* NV only */
2709 reg
->WriteMask
= mask
;
2716 parse_fp_scalar_src_reg (GLcontext
* ctx
, GLubyte
** inst
,
2717 struct var_cache
**vc_head
, struct arb_program
*Program
,
2718 struct fp_src_register
*reg
)
2725 GLboolean IsRelOffset
;
2728 Negate
= (parse_sign (inst
) == -1) ? 0x1 : 0x0;
2730 /* And the src reg */
2731 if (parse_src_reg (ctx
, inst
, vc_head
, Program
, &File
, &Index
, &IsRelOffset
))
2734 /* finally, the swizzle */
2735 parse_swizzle_mask (inst
, Swizzle
, 1);
2739 reg
->Abs
= 0; /* NV only */
2740 reg
->NegateAbs
= 0; /* NV only */
2741 reg
->NegateBase
= Negate
;
2742 reg
->Swizzle
= (Swizzle
[0] << 0);
2749 * This is a big mother that handles getting opcodes into the instruction
2750 * and handling the src & dst registers for fragment program instructions
2753 parse_fp_instruction (GLcontext
* ctx
, GLubyte
** inst
,
2754 struct var_cache
**vc_head
, struct arb_program
*Program
,
2755 struct fp_instruction
*fp
)
2759 GLubyte instClass
, type
, code
;
2762 /* No condition codes in ARB_fp */
2763 fp
->UpdateCondRegister
= 0;
2765 /* Record the position in the program string for debugging */
2766 fp
->StringPos
= Program
->Position
;
2770 fp
->DstReg
.File
= 0xf; /* mark as undef */
2771 fp
->SrcReg
[0].File
= 0xf; /* mark as undef */
2772 fp
->SrcReg
[1].File
= 0xf; /* mark as undef */
2773 fp
->SrcReg
[2].File
= 0xf; /* mark as undef */
2775 /* OP_ALU_INST or OP_TEX_INST */
2776 instClass
= *(*inst
)++;
2778 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2779 * OP_TEX_{SAMPLE, KIL}
2783 /* The actual opcode name */
2786 /* Increment the correct count */
2787 switch (instClass
) {
2789 Program
->NumAluInstructions
++;
2792 Program
->NumTexInstructions
++;
2797 fp
->Precision
= FLOAT32
;
2799 fp
->DstReg
.CondMask
= COND_TR
;
2807 fp
->Opcode
= FP_OPCODE_ABS
;
2813 fp
->Opcode
= FP_OPCODE_FLR
;
2819 fp
->Opcode
= FP_OPCODE_FRC
;
2825 fp
->Opcode
= FP_OPCODE_LIT
;
2831 fp
->Opcode
= FP_OPCODE_MOV
;
2835 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2838 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2847 fp
->Opcode
= FP_OPCODE_COS
;
2853 fp
->Opcode
= FP_OPCODE_EX2
;
2859 fp
->Opcode
= FP_OPCODE_LG2
;
2865 fp
->Opcode
= FP_OPCODE_RCP
;
2871 fp
->Opcode
= FP_OPCODE_RSQ
;
2877 fp
->Opcode
= FP_OPCODE_SIN
;
2884 fp
->Opcode
= FP_OPCODE_SCS
;
2888 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2891 if (parse_fp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
2900 fp
->Opcode
= FP_OPCODE_POW
;
2904 if (parse_fp_dst_reg(ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2907 for (a
= 0; a
< 2; a
++) {
2908 if (parse_fp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
2919 fp
->Opcode
= FP_OPCODE_ADD
;
2925 fp
->Opcode
= FP_OPCODE_DP3
;
2931 fp
->Opcode
= FP_OPCODE_DP4
;
2937 fp
->Opcode
= FP_OPCODE_DPH
;
2943 fp
->Opcode
= FP_OPCODE_DST
;
2949 fp
->Opcode
= FP_OPCODE_MAX
;
2955 fp
->Opcode
= FP_OPCODE_MIN
;
2961 fp
->Opcode
= FP_OPCODE_MUL
;
2967 fp
->Opcode
= FP_OPCODE_SGE
;
2973 fp
->Opcode
= FP_OPCODE_SLT
;
2979 fp
->Opcode
= FP_OPCODE_SUB
;
2985 fp
->Opcode
= FP_OPCODE_XPD
;
2989 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
2991 for (a
= 0; a
< 2; a
++) {
2992 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
3002 fp
->Opcode
= FP_OPCODE_CMP
;
3008 fp
->Opcode
= FP_OPCODE_LRP
;
3014 fp
->Opcode
= FP_OPCODE_MAD
;
3018 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3021 for (a
= 0; a
< 3; a
++) {
3022 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[a
]))
3032 fp
->Opcode
= FP_OPCODE_SWZ
;
3035 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3039 GLubyte Swizzle
[4]; /* FP's swizzle mask is a GLubyte, while VP's is GLuint */
3043 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &File
, &Index
, &rel
))
3045 parse_extended_swizzle_mask (inst
, Swizzle
, Negate
);
3046 fp
->SrcReg
[0].File
= File
;
3047 fp
->SrcReg
[0].Index
= Index
;
3048 fp
->SrcReg
[0].NegateBase
= (Negate
[0] << 0 |
3052 fp
->SrcReg
[0].Swizzle
= (Swizzle
[0] << 0 |
3064 fp
->Opcode
= FP_OPCODE_TEX
;
3070 fp
->Opcode
= FP_OPCODE_TXP
;
3076 fp
->Opcode
= FP_OPCODE_TXB
;
3080 if (parse_fp_dst_reg (ctx
, inst
, vc_head
, Program
, &fp
->DstReg
))
3083 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
3087 if (parse_texcoord_num (ctx
, inst
, Program
, &texcoord
))
3089 fp
->TexSrcUnit
= texcoord
;
3092 switch (*(*inst
)++) {
3094 fp
->TexSrcIdx
= TEXTURE_1D_INDEX
;
3097 fp
->TexSrcIdx
= TEXTURE_2D_INDEX
;
3100 fp
->TexSrcIdx
= TEXTURE_3D_INDEX
;
3102 case TEXTARGET_RECT
:
3103 fp
->TexSrcIdx
= TEXTURE_RECT_INDEX
;
3105 case TEXTARGET_CUBE
:
3106 fp
->TexSrcIdx
= TEXTURE_CUBE_INDEX
;
3108 case TEXTARGET_SHADOW1D
:
3109 case TEXTARGET_SHADOW2D
:
3110 case TEXTARGET_SHADOWRECT
:
3111 /* TODO ARB_fragment_program_shadow code */
3114 Program
->TexturesUsed
[texcoord
] |= (1<<fp
->TexSrcIdx
);
3118 if (parse_fp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &fp
->SrcReg
[0]))
3120 fp
->Opcode
= FP_OPCODE_KIL
;
3128 parse_vp_dst_reg(GLcontext
* ctx
, GLubyte
** inst
,
3129 struct var_cache
**vc_head
, struct arb_program
*Program
,
3130 struct vp_dst_register
*reg
)
3132 GLint file
, idx
, mask
;
3134 if (parse_masked_dst_reg(ctx
, inst
, vc_head
, Program
, &file
, &idx
, &mask
))
3139 reg
->WriteMask
= mask
;
3144 * Handle the parsing out of a masked address register
3146 * \param Index - The register index we write to
3147 * \param WriteMask - The mask controlling which components we write (1->write)
3149 * \return 0 on sucess, 1 on error
3152 parse_vp_address_reg (GLcontext
* ctx
, GLubyte
** inst
,
3153 struct var_cache
**vc_head
,
3154 struct arb_program
*Program
,
3155 struct vp_dst_register
*reg
)
3159 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &idx
))
3162 /* This should be 0x8 */
3165 reg
->File
= PROGRAM_ADDRESS
;
3168 /* Writemask of .x is implied */
3169 reg
->WriteMask
= 0x1;
3176 parse_vp_vector_src_reg (GLcontext
* ctx
, GLubyte
** inst
,
3177 struct var_cache
**vc_head
, struct arb_program
*Program
,
3178 struct vp_src_register
*reg
)
3185 GLboolean IsRelOffset
;
3188 Negate
= (parse_sign (inst
) == -1) ? 0xf : 0x0;
3190 /* And the src reg */
3191 if (parse_src_reg (ctx
, inst
, vc_head
, Program
, &File
, &Index
, &IsRelOffset
))
3194 /* finally, the swizzle */
3195 parse_swizzle_mask (inst
, Swizzle
, 4);
3199 reg
->Swizzle
= ((Swizzle
[0] << 0) |
3203 reg
->Negate
= Negate
;
3204 reg
->RelAddr
= IsRelOffset
;
3210 parse_vp_scalar_src_reg (GLcontext
* ctx
, GLubyte
** inst
,
3211 struct var_cache
**vc_head
, struct arb_program
*Program
,
3212 struct vp_src_register
*reg
)
3219 GLboolean IsRelOffset
;
3222 Negate
= (parse_sign (inst
) == -1) ? 0x1 : 0x0;
3224 /* And the src reg */
3225 if (parse_src_reg (ctx
, inst
, vc_head
, Program
, &File
, &Index
, &IsRelOffset
))
3228 /* finally, the swizzle */
3229 parse_swizzle_mask (inst
, Swizzle
, 1);
3233 reg
->Swizzle
= (Swizzle
[0] << 0);
3234 reg
->Negate
= Negate
;
3235 reg
->RelAddr
= IsRelOffset
;
3241 * This is a big mother that handles getting opcodes into the instruction
3242 * and handling the src & dst registers for vertex program instructions
3245 parse_vp_instruction (GLcontext
* ctx
, GLubyte
** inst
,
3246 struct var_cache
**vc_head
, struct arb_program
*Program
,
3247 struct vp_instruction
*vp
)
3252 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3255 /* The actual opcode name */
3258 /* Record the position in the program string for debugging */
3259 vp
->StringPos
= Program
->Position
;
3261 vp
->SrcReg
[0].RelAddr
= vp
->SrcReg
[1].RelAddr
= vp
->SrcReg
[2].RelAddr
= 0;
3262 vp
->SrcReg
[0].Swizzle
= SWIZZLE_NOOP
;
3263 vp
->SrcReg
[1].Swizzle
= SWIZZLE_NOOP
;
3264 vp
->SrcReg
[2].Swizzle
= SWIZZLE_NOOP
;
3265 vp
->DstReg
.WriteMask
= 0xf;
3270 vp
->Opcode
= VP_OPCODE_ARL
;
3272 /* Remember to set SrcReg.RelAddr; */
3274 /* Get the masked address register [dst] */
3275 if (parse_vp_address_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3278 vp
->DstReg
.File
= PROGRAM_ADDRESS
;
3280 /* Get a scalar src register */
3281 if (parse_vp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3289 vp
->Opcode
= VP_OPCODE_ABS
;
3292 vp
->Opcode
= VP_OPCODE_FLR
;
3295 vp
->Opcode
= VP_OPCODE_FRC
;
3298 vp
->Opcode
= VP_OPCODE_LIT
;
3301 vp
->Opcode
= VP_OPCODE_MOV
;
3305 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3308 if (parse_vp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3315 vp
->Opcode
= VP_OPCODE_EX2
;
3318 vp
->Opcode
= VP_OPCODE_EXP
;
3321 vp
->Opcode
= VP_OPCODE_LG2
;
3324 vp
->Opcode
= VP_OPCODE_LOG
;
3327 vp
->Opcode
= VP_OPCODE_RCP
;
3330 vp
->Opcode
= VP_OPCODE_RSQ
;
3333 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3336 if (parse_vp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[0]))
3343 vp
->Opcode
= VP_OPCODE_POW
;
3346 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3349 for (a
= 0; a
< 2; a
++) {
3350 if (parse_vp_scalar_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3358 vp
->Opcode
= VP_OPCODE_ADD
;
3361 vp
->Opcode
= VP_OPCODE_DP3
;
3364 vp
->Opcode
= VP_OPCODE_DP4
;
3367 vp
->Opcode
= VP_OPCODE_DPH
;
3370 vp
->Opcode
= VP_OPCODE_DST
;
3373 vp
->Opcode
= VP_OPCODE_MAX
;
3376 vp
->Opcode
= VP_OPCODE_MIN
;
3379 vp
->Opcode
= VP_OPCODE_MUL
;
3382 vp
->Opcode
= VP_OPCODE_SGE
;
3385 vp
->Opcode
= VP_OPCODE_SLT
;
3388 vp
->Opcode
= VP_OPCODE_SUB
;
3391 vp
->Opcode
= VP_OPCODE_XPD
;
3394 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3397 for (a
= 0; a
< 2; a
++) {
3398 if (parse_vp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3406 vp
->Opcode
= VP_OPCODE_MAD
;
3410 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3413 for (a
= 0; a
< 3; a
++) {
3414 if (parse_vp_vector_src_reg(ctx
, inst
, vc_head
, Program
, &vp
->SrcReg
[a
]))
3422 vp
->Opcode
= VP_OPCODE_SWZ
;
3426 GLubyte Swizzle
[4]; /* FP's swizzle mask is a GLubyte, while VP's is GLuint */
3431 if (parse_vp_dst_reg(ctx
, inst
, vc_head
, Program
, &vp
->DstReg
))
3434 if (parse_src_reg(ctx
, inst
, vc_head
, Program
, &File
, &Index
, &RelAddr
))
3436 parse_extended_swizzle_mask (inst
, Swizzle
, Negate
);
3437 vp
->SrcReg
[0].File
= File
;
3438 vp
->SrcReg
[0].Index
= Index
;
3439 vp
->SrcReg
[0].Negate
= (Negate
[0] << 0 |
3443 vp
->SrcReg
[0].Swizzle
= (Swizzle
[0] << 0 |
3447 vp
->SrcReg
[0].RelAddr
= RelAddr
;
3457 print_state_token (GLint token
)
3460 case STATE_MATERIAL
:
3461 fprintf (stderr
, "STATE_MATERIAL ");
3464 fprintf (stderr
, "STATE_LIGHT ");
3467 case STATE_LIGHTMODEL_AMBIENT
:
3468 fprintf (stderr
, "STATE_AMBIENT ");
3471 case STATE_LIGHTMODEL_SCENECOLOR
:
3472 fprintf (stderr
, "STATE_SCENECOLOR ");
3475 case STATE_LIGHTPROD
:
3476 fprintf (stderr
, "STATE_LIGHTPROD ");
3480 fprintf (stderr
, "STATE_TEXGEN ");
3483 case STATE_FOG_COLOR
:
3484 fprintf (stderr
, "STATE_FOG_COLOR ");
3487 case STATE_FOG_PARAMS
:
3488 fprintf (stderr
, "STATE_FOG_PARAMS ");
3491 case STATE_CLIPPLANE
:
3492 fprintf (stderr
, "STATE_CLIPPLANE ");
3495 case STATE_POINT_SIZE
:
3496 fprintf (stderr
, "STATE_POINT_SIZE ");
3499 case STATE_POINT_ATTENUATION
:
3500 fprintf (stderr
, "STATE_ATTENUATION ");
3504 fprintf (stderr
, "STATE_MATRIX ");
3507 case STATE_MODELVIEW
:
3508 fprintf (stderr
, "STATE_MODELVIEW ");
3511 case STATE_PROJECTION
:
3512 fprintf (stderr
, "STATE_PROJECTION ");
3516 fprintf (stderr
, "STATE_MVP ");
3520 fprintf (stderr
, "STATE_TEXTURE ");
3524 fprintf (stderr
, "STATE_PROGRAM ");
3527 case STATE_MATRIX_INVERSE
:
3528 fprintf (stderr
, "STATE_INVERSE ");
3531 case STATE_MATRIX_TRANSPOSE
:
3532 fprintf (stderr
, "STATE_TRANSPOSE ");
3535 case STATE_MATRIX_INVTRANS
:
3536 fprintf (stderr
, "STATE_INVTRANS ");
3540 fprintf (stderr
, "STATE_AMBIENT ");
3544 fprintf (stderr
, "STATE_DIFFUSE ");
3547 case STATE_SPECULAR
:
3548 fprintf (stderr
, "STATE_SPECULAR ");
3551 case STATE_EMISSION
:
3552 fprintf (stderr
, "STATE_EMISSION ");
3555 case STATE_SHININESS
:
3556 fprintf (stderr
, "STATE_SHININESS ");
3560 fprintf (stderr
, "STATE_HALF ");
3563 case STATE_POSITION
:
3564 fprintf (stderr
, "STATE_POSITION ");
3567 case STATE_ATTENUATION
:
3568 fprintf (stderr
, "STATE_ATTENUATION ");
3571 case STATE_SPOT_DIRECTION
:
3572 fprintf (stderr
, "STATE_DIRECTION ");
3575 case STATE_TEXGEN_EYE_S
:
3576 fprintf (stderr
, "STATE_TEXGEN_EYE_S ");
3579 case STATE_TEXGEN_EYE_T
:
3580 fprintf (stderr
, "STATE_TEXGEN_EYE_T ");
3583 case STATE_TEXGEN_EYE_R
:
3584 fprintf (stderr
, "STATE_TEXGEN_EYE_R ");
3587 case STATE_TEXGEN_EYE_Q
:
3588 fprintf (stderr
, "STATE_TEXGEN_EYE_Q ");
3591 case STATE_TEXGEN_OBJECT_S
:
3592 fprintf (stderr
, "STATE_TEXGEN_EYE_S ");
3595 case STATE_TEXGEN_OBJECT_T
:
3596 fprintf (stderr
, "STATE_TEXGEN_OBJECT_T ");
3599 case STATE_TEXGEN_OBJECT_R
:
3600 fprintf (stderr
, "STATE_TEXGEN_OBJECT_R ");
3603 case STATE_TEXGEN_OBJECT_Q
:
3604 fprintf (stderr
, "STATE_TEXGEN_OBJECT_Q ");
3607 case STATE_TEXENV_COLOR
:
3608 fprintf (stderr
, "STATE_TEXENV_COLOR ");
3611 case STATE_DEPTH_RANGE
:
3612 fprintf (stderr
, "STATE_DEPTH_RANGE ");
3615 case STATE_VERTEX_PROGRAM
:
3616 fprintf (stderr
, "STATE_VERTEX_PROGRAM ");
3619 case STATE_FRAGMENT_PROGRAM
:
3620 fprintf (stderr
, "STATE_FRAGMENT_PROGRAM ");
3624 fprintf (stderr
, "STATE_ENV ");
3628 fprintf (stderr
, "STATE_LOCAL ");
3632 fprintf (stderr
, "[%d] ", token
);
3637 debug_variables (GLcontext
* ctx
, struct var_cache
*vc_head
,
3638 struct arb_program
*Program
)
3640 struct var_cache
*vc
;
3643 fprintf (stderr
, "debug_variables, vc_head: %x\n", vc_head
);
3645 /* First of all, print out the contents of the var_cache */
3648 fprintf (stderr
, "[%x]\n", vc
);
3651 fprintf (stderr
, "UNDEFINED %s\n", vc
->name
);
3654 fprintf (stderr
, "ATTRIB %s\n", vc
->name
);
3655 fprintf (stderr
, " binding: 0x%x\n", vc
->attrib_binding
);
3658 fprintf (stderr
, "PARAM %s begin: %d len: %d\n", vc
->name
,
3659 vc
->param_binding_begin
, vc
->param_binding_length
);
3660 b
= vc
->param_binding_begin
;
3661 for (a
= 0; a
< vc
->param_binding_length
; a
++) {
3662 fprintf (stderr
, "%s\n",
3663 Program
->Parameters
->Parameters
[a
+ b
].Name
);
3664 if (Program
->Parameters
->Parameters
[a
+ b
].Type
== STATE
) {
3665 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3667 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3669 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3671 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3673 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3675 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3679 fprintf (stderr
, "%f %f %f %f\n",
3680 Program
->Parameters
->Parameters
[a
+ b
].Values
[0],
3681 Program
->Parameters
->Parameters
[a
+ b
].Values
[1],
3682 Program
->Parameters
->Parameters
[a
+ b
].Values
[2],
3683 Program
->Parameters
->Parameters
[a
+ b
].Values
[3]);
3687 fprintf (stderr
, "TEMP %s\n", vc
->name
);
3688 fprintf (stderr
, " binding: 0x%x\n", vc
->temp_binding
);
3691 fprintf (stderr
, "OUTPUT %s\n", vc
->name
);
3692 fprintf (stderr
, " binding: 0x%x\n", vc
->output_binding
);
3695 fprintf (stderr
, "ALIAS %s\n", vc
->name
);
3696 fprintf (stderr
, " binding: 0x%x (%s)\n",
3697 vc
->alias_binding
, vc
->alias_binding
->name
);
3708 * The main loop for parsing a fragment or vertex program
3710 * \return 0 on sucess, 1 on error
3713 parse_arb_program (GLcontext
* ctx
, GLubyte
* inst
, struct var_cache
**vc_head
,
3714 struct arb_program
*Program
)
3718 Program
->MajorVersion
= (GLuint
) * inst
++;
3719 Program
->MinorVersion
= (GLuint
) * inst
++;
3721 while (*inst
!= END
) {
3726 case ARB_PRECISION_HINT_FASTEST
:
3727 Program
->PrecisionOption
= GL_FASTEST
;
3730 case ARB_PRECISION_HINT_NICEST
:
3731 Program
->PrecisionOption
= GL_NICEST
;
3735 Program
->FogOption
= GL_EXP
;
3739 Program
->FogOption
= GL_EXP2
;
3742 case ARB_FOG_LINEAR
:
3743 Program
->FogOption
= GL_LINEAR
;
3746 case ARB_POSITION_INVARIANT
:
3747 if (Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
3748 Program
->HintPositionInvariant
= 1;
3751 case ARB_FRAGMENT_PROGRAM_SHADOW
:
3752 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3753 /* TODO ARB_fragment_program_shadow code */
3757 case ARB_DRAW_BUFFERS
:
3758 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3759 /* do nothing for now */
3766 Program
->Position
= parse_position (&inst
);
3768 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3770 /* Check the instruction count
3771 * XXX: Does END count as an instruction?
3773 if (Program
->Base
.NumInstructions
+1 == MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS
) {
3774 _mesa_set_program_error (ctx
, Program
->Position
,
3775 "Max instruction count exceeded!");
3776 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3777 "Max instruction count exceeded!");
3780 /* Realloc Program->FPInstructions */
3781 Program
->FPInstructions
=
3782 (struct fp_instruction
*) _mesa_realloc (Program
->FPInstructions
,
3783 Program
->Base
.NumInstructions
*sizeof(struct fp_instruction
),
3784 (Program
->Base
.NumInstructions
+1)*sizeof (struct fp_instruction
));
3786 /* parse the current instruction */
3787 err
= parse_fp_instruction (ctx
, &inst
, vc_head
, Program
,
3788 &Program
->FPInstructions
[Program
->Base
.NumInstructions
]);
3792 /* Check the instruction count
3793 * XXX: Does END count as an instruction?
3795 if (Program
->Base
.NumInstructions
+1 == MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
) {
3796 _mesa_set_program_error (ctx
, Program
->Position
,
3797 "Max instruction count exceeded!");
3798 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3799 "Max instruction count exceeded!");
3802 /* Realloc Program->VPInstructions */
3803 Program
->VPInstructions
=
3804 (struct vp_instruction
*) _mesa_realloc (Program
->VPInstructions
,
3805 Program
->Base
.NumInstructions
*sizeof(struct vp_instruction
),
3806 (Program
->Base
.NumInstructions
+1)*sizeof(struct vp_instruction
));
3808 /* parse the current instruction */
3809 err
= parse_vp_instruction (ctx
, &inst
, vc_head
, Program
,
3810 &Program
->VPInstructions
[Program
->Base
.NumInstructions
]);
3813 /* increment Program->Base.NumInstructions */
3814 Program
->Base
.NumInstructions
++;
3818 err
= parse_declaration (ctx
, &inst
, vc_head
, Program
);
3829 /* Finally, tag on an OPCODE_END instruction */
3830 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3831 Program
->FPInstructions
=
3832 (struct fp_instruction
*) _mesa_realloc (Program
->FPInstructions
,
3833 Program
->Base
.NumInstructions
*sizeof(struct fp_instruction
),
3834 (Program
->Base
.NumInstructions
+1)*sizeof(struct fp_instruction
));
3836 Program
->FPInstructions
[Program
->Base
.NumInstructions
].Opcode
= FP_OPCODE_END
;
3837 Program
->FPInstructions
[Program
->Base
.NumInstructions
].Saturate
= 0;
3838 Program
->FPInstructions
[Program
->Base
.NumInstructions
].DstReg
.File
= 0xf;
3839 Program
->FPInstructions
[Program
->Base
.NumInstructions
].SrcReg
[0].File
= 0xf;
3840 Program
->FPInstructions
[Program
->Base
.NumInstructions
].SrcReg
[1].File
= 0xf;
3841 Program
->FPInstructions
[Program
->Base
.NumInstructions
].SrcReg
[2].File
= 0xf;
3842 /* YYY Wrong Position in program, whatever, at least not random -> crash
3843 Program->Position = parse_position (&inst);
3845 Program
->FPInstructions
[Program
->Base
.NumInstructions
].StringPos
= Program
->Position
;
3846 Program
->FPInstructions
[Program
->Base
.NumInstructions
].Data
= NULL
;
3849 Program
->VPInstructions
=
3850 (struct vp_instruction
*) _mesa_realloc (Program
->VPInstructions
,
3851 Program
->Base
.NumInstructions
*sizeof(struct vp_instruction
),
3852 (Program
->Base
.NumInstructions
+1)*sizeof(struct vp_instruction
));
3854 Program
->VPInstructions
[Program
->Base
.NumInstructions
].Opcode
= VP_OPCODE_END
;
3855 /* YYY Wrong Position in program, whatever, at least not random -> crash
3856 Program->Position = parse_position (&inst);
3858 Program
->VPInstructions
[Program
->Base
.NumInstructions
].StringPos
= Program
->Position
;
3859 Program
->VPInstructions
[Program
->Base
.NumInstructions
].Data
= NULL
;
3862 /* increment Program->Base.NumInstructions */
3863 Program
->Base
.NumInstructions
++;
3869 __extension__
static char core_grammar_text
[] =
3870 #include "grammar_syn.h"
3873 static int set_reg8 (GLcontext
*ctx
, grammar id
, const byte
*name
, byte value
)
3875 char error_msg
[300];
3878 if (grammar_set_reg8 (id
, name
, value
))
3881 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3882 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3883 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Grammar Register Error");
3887 static int extension_is_supported (const GLubyte
*ext
)
3889 GET_CURRENT_CONTEXT(ctx
);
3890 const GLubyte
*extensions
= CALL_GetString(GET_DISPATCH(), (GL_EXTENSIONS
));
3891 const GLubyte
*end
= extensions
+ _mesa_strlen ((const char *) extensions
);
3892 const GLint ext_len
= (GLint
)_mesa_strlen ((const char *) ext
);
3894 while (extensions
< end
)
3896 const GLubyte
*name_end
= (const GLubyte
*) strchr ((const char *) extensions
, ' ');
3897 if (name_end
== NULL
)
3899 if (name_end
- extensions
== ext_len
&& _mesa_strncmp ((const char *) ext
,
3900 (const char *) extensions
, ext_len
) == 0)
3902 extensions
= name_end
+ 1;
3908 static int enable_ext (GLcontext
*ctx
, grammar id
, const byte
*name
, const byte
*extname
)
3910 if (extension_is_supported (extname
))
3911 if (set_reg8 (ctx
, id
, name
, 0x01))
3917 * This kicks everything off.
3919 * \param ctx - The GL Context
3920 * \param str - The program string
3921 * \param len - The program string length
3922 * \param Program - The arb_program struct to return all the parsed info in
3923 * \return 0 on sucess, 1 on error
3926 _mesa_parse_arb_program (GLcontext
* ctx
, const GLubyte
* str
, GLsizei len
,
3927 struct arb_program
* program
)
3929 GLint a
, err
, error_pos
;
3930 char error_msg
[300];
3932 struct var_cache
*vc_head
;
3933 grammar arbprogram_syn_id
;
3934 GLubyte
*parsed
, *inst
;
3935 GLubyte
*strz
= NULL
;
3936 static int arbprogram_syn_is_ok
= 0; /* XXX temporary */
3938 /* Reset error state */
3939 _mesa_set_program_error(ctx
, -1, NULL
);
3942 fprintf (stderr
, "Loading grammar text!\n");
3945 /* check if the arb_grammar_text (arbprogram.syn) is syntactically correct */
3946 if (!arbprogram_syn_is_ok
) {
3947 grammar grammar_syn_id
;
3952 grammar_syn_id
= grammar_load_from_text ((byte
*) core_grammar_text
);
3953 if (grammar_syn_id
== 0) {
3954 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3955 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3956 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3957 "Error loading grammar rule set");
3961 err
= grammar_check (grammar_syn_id
, (byte
*) arb_grammar_text
, &parsed
, &parsed_len
);
3963 /* NOTE: we cant destroy grammar_syn_id right here because grammar_destroy() can
3964 reset the last error
3968 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3969 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3970 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Error loading grammar rule set");
3972 grammar_destroy (grammar_syn_id
);
3976 grammar_destroy (grammar_syn_id
);
3978 arbprogram_syn_is_ok
= 1;
3981 /* create the grammar object */
3982 arbprogram_syn_id
= grammar_load_from_text ((byte
*) arb_grammar_text
);
3983 if (arbprogram_syn_id
== 0) {
3984 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
3985 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3986 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3987 "Error loading grammer rule set");
3991 /* Set program_target register value */
3992 if (set_reg8 (ctx
, arbprogram_syn_id
, (byte
*) "program_target",
3993 program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
? 0x10 : 0x20)) {
3994 grammar_destroy (arbprogram_syn_id
);
3998 /* Enable all active extensions */
3999 if (enable_ext (ctx
, arbprogram_syn_id
,
4000 (byte
*) "vertex_blend", (byte
*) "GL_ARB_vertex_blend") ||
4001 enable_ext (ctx
, arbprogram_syn_id
,
4002 (byte
*) "vertex_blend", (byte
*) "GL_EXT_vertex_weighting") ||
4003 enable_ext (ctx
, arbprogram_syn_id
,
4004 (byte
*) "matrix_palette", (byte
*) "GL_ARB_matrix_palette") ||
4005 enable_ext (ctx
, arbprogram_syn_id
,
4006 (byte
*) "point_parameters", (byte
*) "GL_ARB_point_parameters") ||
4007 enable_ext (ctx
, arbprogram_syn_id
,
4008 (byte
*) "point_parameters", (byte
*) "GL_EXT_point_parameters") ||
4009 enable_ext (ctx
, arbprogram_syn_id
,
4010 (byte
*) "secondary_color", (byte
*) "GL_EXT_secondary_color") ||
4011 enable_ext (ctx
, arbprogram_syn_id
,
4012 (byte
*) "fog_coord", (byte
*) "GL_EXT_fog_coord") ||
4013 enable_ext (ctx
, arbprogram_syn_id
,
4014 (byte
*) "texture_rectangle", (byte
*) "GL_ARB_texture_rectangle") ||
4015 enable_ext (ctx
, arbprogram_syn_id
,
4016 (byte
*) "texture_rectangle", (byte
*) "GL_EXT_texture_rectangle") ||
4017 enable_ext (ctx
, arbprogram_syn_id
,
4018 (byte
*) "texture_rectangle", (byte
*) "GL_NV_texture_rectangle") ||
4019 enable_ext (ctx
, arbprogram_syn_id
,
4020 (byte
*) "fragment_program_shadow", (byte
*) "GL_ARB_fragment_program_shadow") ||
4021 enable_ext (ctx
, arbprogram_syn_id
,
4022 (byte
*) "draw_buffers", (byte
*) "GL_ARB_draw_buffers")) {
4023 grammar_destroy (arbprogram_syn_id
);
4027 /* check for NULL character occurences */
4030 for (i
= 0; i
< len
; i
++)
4031 if (str
[i
] == '\0') {
4032 _mesa_set_program_error (ctx
, i
, "invalid character");
4033 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Lexical Error");
4035 grammar_destroy (arbprogram_syn_id
);
4040 /* copy the program string to a null-terminated string */
4041 /* XXX should I check for NULL from malloc()? */
4042 strz
= (GLubyte
*) _mesa_malloc (len
+ 1);
4043 _mesa_memcpy (strz
, str
, len
);
4047 printf ("Checking Grammar!\n");
4049 /* do a fast check on program string - initial production buffer is 4K */
4050 err
= grammar_fast_check (arbprogram_syn_id
, strz
, &parsed
, &parsed_len
, 0x1000);
4052 /* Syntax parse error */
4055 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
4056 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
4057 _mesa_error (ctx
, GL_INVALID_OPERATION
, "glProgramStringARB(syntax error)");
4059 /* useful for debugging */
4063 printf("Program: %s\n", (char *) strz
);
4064 printf("Error Pos: %d\n", ctx
->Program
.ErrorPos
);
4065 s
= (char *) _mesa_find_line_column(strz
, strz
+ctx
->Program
.ErrorPos
, &line
, &col
);
4066 printf("line %d col %d: %s\n", line
, col
, s
);
4069 grammar_destroy (arbprogram_syn_id
);
4074 printf ("Destroying grammer dict [parse retval: %d]\n", err
);
4076 grammar_destroy (arbprogram_syn_id
);
4078 /* Initialize the arb_program struct */
4079 program
->Base
.String
= strz
;
4080 program
->Base
.NumInstructions
=
4081 program
->Base
.NumTemporaries
=
4082 program
->Base
.NumParameters
=
4083 program
->Base
.NumAttributes
= program
->Base
.NumAddressRegs
= 0;
4084 program
->Parameters
= _mesa_new_parameter_list ();
4085 program
->InputsRead
= 0;
4086 program
->OutputsWritten
= 0;
4087 program
->Position
= 0;
4088 program
->MajorVersion
= program
->MinorVersion
= 0;
4089 program
->PrecisionOption
= GL_DONT_CARE
;
4090 program
->FogOption
= GL_NONE
;
4091 program
->HintPositionInvariant
= GL_FALSE
;
4092 for (a
= 0; a
< MAX_TEXTURE_IMAGE_UNITS
; a
++)
4093 program
->TexturesUsed
[a
] = 0;
4094 program
->NumAluInstructions
=
4095 program
->NumTexInstructions
=
4096 program
->NumTexIndirections
= 0;
4098 program
->FPInstructions
= NULL
;
4099 program
->VPInstructions
= NULL
;
4104 /* Start examining the tokens in the array */
4107 /* Check the grammer rev */
4108 if (*inst
++ != REVISION
) {
4109 _mesa_set_program_error (ctx
, 0, "Grammar version mismatch");
4110 _mesa_error (ctx
, GL_INVALID_OPERATION
, "glProgramStringARB(Grammar verison mismatch)");
4114 /* ignore program target */
4117 err
= parse_arb_program (ctx
, inst
, &vc_head
, program
);
4119 fprintf (stderr
, "Symantic analysis returns %d [1 is bad!]\n", err
);
4123 /*debug_variables(ctx, vc_head, program); */
4125 /* We're done with the parsed binary array */
4126 var_cache_destroy (&vc_head
);
4128 _mesa_free (parsed
);
4130 printf ("_mesa_parse_arb_program() done\n");