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"
45 #if !defined(__GNUC__) && !defined(__extension__)
46 # define __extension__
50 * Fragment Program Stuff:
51 * -----------------------------------------------------
53 * - things from Michal's email
55 * + not-overflowing floats (don't use parse_integer..)
56 * + can remove range checking in arbparse.c
58 * - check all limits of number of various variables
63 * Vertex Program Stuff:
64 * -----------------------------------------------------
65 * - Optimize param array usage and count limits correctly, see spec,
67 * + Record if an array is reference absolutly or relatively (or both)
68 * + For absolute arrays, store a bitmap of accesses
69 * + For single parameters, store an access flag
70 * + After parsing, make a parameter cleanup and merging pass, where
71 * relative arrays are layed out first, followed by abs arrays, and
72 * finally single state.
73 * + Remap offsets for param src and dst registers
74 * + Now we can properly count parameter usage
76 * - Multiple state binding errors in param arrays (see spec, just before
81 * -----------------------------------------------------
82 * - User clipping planes vs. PositionInvariant
83 * - Is it sufficient to just multiply by the mvp to transform in the
84 * PositionInvariant case? Or do we need something more involved?
86 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
87 * - fetch state listed in program_parameters list
88 * + WTF should this go???
89 * + currently in nvvertexec.c and s_nvfragprog.c
91 * - allow for multiple address registers (and fetch address regs properly)
94 * -----------------------------------------------------
95 * - remove any leftover unused grammer.c stuff (dict_ ?)
96 * - fix grammer.c error handling so its not static
97 * - #ifdef around stuff pertaining to extentions
99 * Outstanding Questions:
100 * -----------------------------------------------------
101 * - ARB_matrix_palette / ARB_vertex_blend -- not supported
102 * what gets hacked off because of this:
103 * + VERTEX_ATTRIB_MATRIXINDEX
104 * + VERTEX_ATTRIB_WEIGHT
108 * - When can we fetch env/local params from their own register files, and
109 * when to we have to fetch them into the main state register file?
113 * -----------------------------------------------------
116 /* Changes since moving the file to shader directory
118 2004-III-4 ------------------------------------------------------------
119 - added #include "grammar_mesa.h"
120 - removed grammar specific code part (it resides now in grammar.c)
121 - added GL_ARB_fragment_program_shadow tokens
122 - modified #include "arbparse_syn.h"
123 - major changes inside _mesa_parse_arb_program()
124 - check the program string for '\0' characters
125 - copy the program string to a one-byte-longer location to have
127 - position invariance test (not writing to result.position) moved
131 typedef GLubyte
*production
;
134 * This is the text describing the rules to parse the grammar
136 __extension__
static char arb_grammar_text
[] =
137 #include "arbprogram_syn.h"
141 * These should match up with the values defined in arbprogram.syn
146 - changed and merged V_* and F_* opcode values to OP_*.
147 - added GL_ARB_fragment_program_shadow specific tokens (michal)
149 #define REVISION 0x09
152 #define FRAGMENT_PROGRAM 0x01
153 #define VERTEX_PROGRAM 0x02
155 /* program section */
157 #define INSTRUCTION 0x02
158 #define DECLARATION 0x03
161 /* GL_ARB_fragment_program option */
162 #define ARB_PRECISION_HINT_FASTEST 0x00
163 #define ARB_PRECISION_HINT_NICEST 0x01
164 #define ARB_FOG_EXP 0x02
165 #define ARB_FOG_EXP2 0x03
166 #define ARB_FOG_LINEAR 0x04
168 /* GL_ARB_vertex_program option */
169 #define ARB_POSITION_INVARIANT 0x05
171 /* GL_ARB_fragment_program_shadow option */
172 #define ARB_FRAGMENT_PROGRAM_SHADOW 0x06
174 /* GL_ARB_draw_buffers option */
175 #define ARB_DRAW_BUFFERS 0x07
177 /* GL_ARB_fragment_program instruction class */
178 #define OP_ALU_INST 0x00
179 #define OP_TEX_INST 0x01
181 /* GL_ARB_vertex_program instruction class */
184 /* GL_ARB_fragment_program instruction type */
185 #define OP_ALU_VECTOR 0x00
186 #define OP_ALU_SCALAR 0x01
187 #define OP_ALU_BINSC 0x02
188 #define OP_ALU_BIN 0x03
189 #define OP_ALU_TRI 0x04
190 #define OP_ALU_SWZ 0x05
191 #define OP_TEX_SAMPLE 0x06
192 #define OP_TEX_KIL 0x07
194 /* GL_ARB_vertex_program instruction type */
195 #define OP_ALU_ARL 0x08
203 /* GL_ARB_fragment_program instruction code */
205 #define OP_ABS_SAT 0x1B
207 #define OP_FLR_SAT 0x26
209 #define OP_FRC_SAT 0x27
211 #define OP_LIT_SAT 0x2A
213 #define OP_MOV_SAT 0x30
215 #define OP_COS_SAT 0x20
217 #define OP_EX2_SAT 0x25
219 #define OP_LG2_SAT 0x29
221 #define OP_RCP_SAT 0x33
223 #define OP_RSQ_SAT 0x34
225 #define OP_SIN_SAT 0x39
227 #define OP_SCS_SAT 0x36
229 #define OP_POW_SAT 0x32
231 #define OP_ADD_SAT 0x1C
233 #define OP_DP3_SAT 0x21
235 #define OP_DP4_SAT 0x22
237 #define OP_DPH_SAT 0x23
239 #define OP_DST_SAT 0x24
241 #define OP_MAX_SAT 0x2E
243 #define OP_MIN_SAT 0x2F
245 #define OP_MUL_SAT 0x31
247 #define OP_SGE_SAT 0x37
249 #define OP_SLT_SAT 0x3A
251 #define OP_SUB_SAT 0x3B
253 #define OP_XPD_SAT 0x43
255 #define OP_CMP_SAT 0x1E
257 #define OP_LRP_SAT 0x2C
259 #define OP_MAD_SAT 0x2D
261 #define OP_SWZ_SAT 0x3C
263 #define OP_TEX_SAT 0x3E
265 #define OP_TXB_SAT 0x40
267 #define OP_TXP_SAT 0x42
270 /* GL_ARB_vertex_program instruction code */
299 /* fragment attribute binding */
300 #define FRAGMENT_ATTRIB_COLOR 0x01
301 #define FRAGMENT_ATTRIB_TEXCOORD 0x02
302 #define FRAGMENT_ATTRIB_FOGCOORD 0x03
303 #define FRAGMENT_ATTRIB_POSITION 0x04
305 /* vertex attribute binding */
306 #define VERTEX_ATTRIB_POSITION 0x01
307 #define VERTEX_ATTRIB_WEIGHT 0x02
308 #define VERTEX_ATTRIB_NORMAL 0x03
309 #define VERTEX_ATTRIB_COLOR 0x04
310 #define VERTEX_ATTRIB_FOGCOORD 0x05
311 #define VERTEX_ATTRIB_TEXCOORD 0x06
312 #define VERTEX_ATTRIB_MATRIXINDEX 0x07
313 #define VERTEX_ATTRIB_GENERIC 0x08
315 /* fragment result binding */
316 #define FRAGMENT_RESULT_COLOR 0x01
317 #define FRAGMENT_RESULT_DEPTH 0x02
319 /* vertex result binding */
320 #define VERTEX_RESULT_POSITION 0x01
321 #define VERTEX_RESULT_COLOR 0x02
322 #define VERTEX_RESULT_FOGCOORD 0x03
323 #define VERTEX_RESULT_POINTSIZE 0x04
324 #define VERTEX_RESULT_TEXCOORD 0x05
327 #define TEXTARGET_1D 0x01
328 #define TEXTARGET_2D 0x02
329 #define TEXTARGET_3D 0x03
330 #define TEXTARGET_RECT 0x04
331 #define TEXTARGET_CUBE 0x05
332 /* GL_ARB_fragment_program_shadow */
333 #define TEXTARGET_SHADOW1D 0x06
334 #define TEXTARGET_SHADOW2D 0x07
335 #define TEXTARGET_SHADOWRECT 0x08
338 #define FACE_FRONT 0x00
339 #define FACE_BACK 0x01
342 #define COLOR_PRIMARY 0x00
343 #define COLOR_SECONDARY 0x01
346 #define COMPONENT_X 0x00
347 #define COMPONENT_Y 0x01
348 #define COMPONENT_Z 0x02
349 #define COMPONENT_W 0x03
350 #define COMPONENT_0 0x04
351 #define COMPONENT_1 0x05
353 /* array index type */
354 #define ARRAY_INDEX_ABSOLUTE 0x00
355 #define ARRAY_INDEX_RELATIVE 0x01
358 #define MATRIX_MODELVIEW 0x01
359 #define MATRIX_PROJECTION 0x02
360 #define MATRIX_MVP 0x03
361 #define MATRIX_TEXTURE 0x04
362 #define MATRIX_PALETTE 0x05
363 #define MATRIX_PROGRAM 0x06
365 /* matrix modifier */
366 #define MATRIX_MODIFIER_IDENTITY 0x00
367 #define MATRIX_MODIFIER_INVERSE 0x01
368 #define MATRIX_MODIFIER_TRANSPOSE 0x02
369 #define MATRIX_MODIFIER_INVTRANS 0x03
372 #define CONSTANT_SCALAR 0x01
373 #define CONSTANT_VECTOR 0x02
375 /* program param type */
376 #define PROGRAM_PARAM_ENV 0x01
377 #define PROGRAM_PARAM_LOCAL 0x02
380 #define REGISTER_ATTRIB 0x01
381 #define REGISTER_PARAM 0x02
382 #define REGISTER_RESULT 0x03
383 #define REGISTER_ESTABLISHED_NAME 0x04
386 #define PARAM_NULL 0x00
387 #define PARAM_ARRAY_ELEMENT 0x01
388 #define PARAM_STATE_ELEMENT 0x02
389 #define PARAM_PROGRAM_ELEMENT 0x03
390 #define PARAM_PROGRAM_ELEMENTS 0x04
391 #define PARAM_CONSTANT 0x05
393 /* param state property */
394 #define STATE_MATERIAL_PARSER 0x01
395 #define STATE_LIGHT_PARSER 0x02
396 #define STATE_LIGHT_MODEL 0x03
397 #define STATE_LIGHT_PROD 0x04
398 #define STATE_FOG 0x05
399 #define STATE_MATRIX_ROWS 0x06
400 /* GL_ARB_fragment_program */
401 #define STATE_TEX_ENV 0x07
402 #define STATE_DEPTH 0x08
403 /* GL_ARB_vertex_program */
404 #define STATE_TEX_GEN 0x09
405 #define STATE_CLIP_PLANE 0x0A
406 #define STATE_POINT 0x0B
408 /* state material property */
409 #define MATERIAL_AMBIENT 0x01
410 #define MATERIAL_DIFFUSE 0x02
411 #define MATERIAL_SPECULAR 0x03
412 #define MATERIAL_EMISSION 0x04
413 #define MATERIAL_SHININESS 0x05
415 /* state light property */
416 #define LIGHT_AMBIENT 0x01
417 #define LIGHT_DIFFUSE 0x02
418 #define LIGHT_SPECULAR 0x03
419 #define LIGHT_POSITION 0x04
420 #define LIGHT_ATTENUATION 0x05
421 #define LIGHT_HALF 0x06
422 #define LIGHT_SPOT_DIRECTION 0x07
424 /* state light model property */
425 #define LIGHT_MODEL_AMBIENT 0x01
426 #define LIGHT_MODEL_SCENECOLOR 0x02
428 /* state light product property */
429 #define LIGHT_PROD_AMBIENT 0x01
430 #define LIGHT_PROD_DIFFUSE 0x02
431 #define LIGHT_PROD_SPECULAR 0x03
433 /* state texture environment property */
434 #define TEX_ENV_COLOR 0x01
436 /* state texture generation coord property */
437 #define TEX_GEN_EYE 0x01
438 #define TEX_GEN_OBJECT 0x02
440 /* state fog property */
441 #define FOG_COLOR 0x01
442 #define FOG_PARAMS 0x02
444 /* state depth property */
445 #define DEPTH_RANGE 0x01
447 /* state point parameters property */
448 #define POINT_SIZE 0x01
449 #define POINT_ATTENUATION 0x02
457 /* GL_ARB_vertex_program */
460 /*-----------------------------------------------------------------------
461 * From here on down is the semantic checking portion
466 * Variable Table Handling functions
481 * Setting an explicit field for each of the binding properties is a bit wasteful
482 * of space, but it should be much more clear when reading later on..
488 GLuint address_binding
; /* The index of the address register we should
490 GLuint attrib_binding
; /* For type vt_attrib, see nvfragprog.h for values */
491 GLuint attrib_binding_idx
; /* The index into the attrib register file corresponding
492 * to the state in attrib_binding */
493 GLuint attrib_is_generic
; /* If the attrib was specified through a generic
495 GLuint temp_binding
; /* The index of the temp register we are to use */
496 GLuint output_binding
; /* For type vt_output, see nvfragprog.h for values */
497 GLuint output_binding_idx
; /* This is the index into the result register file
498 * corresponding to the bound result state */
499 struct var_cache
*alias_binding
; /* For type vt_alias, points to the var_cache entry
500 * that this is aliased to */
501 GLuint param_binding_type
; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
502 * PROGRAM_ENV_PARAM} */
503 GLuint param_binding_begin
; /* This is the offset into the program_parameter_list where
504 * the tokens representing our bound state (or constants)
506 GLuint param_binding_length
; /* This is how many entries in the the program_parameter_list
507 * we take up with our state tokens or constants. Note that
508 * this is _not_ the same as the number of param registers
509 * we eventually use */
510 struct var_cache
*next
;
514 var_cache_create (struct var_cache
**va
)
516 *va
= (struct var_cache
*) _mesa_malloc (sizeof (struct var_cache
));
519 (**va
).type
= vt_none
;
520 (**va
).attrib_binding
= ~0;
521 (**va
).attrib_is_generic
= 0;
522 (**va
).temp_binding
= ~0;
523 (**va
).output_binding
= ~0;
524 (**va
).output_binding_idx
= ~0;
525 (**va
).param_binding_type
= ~0;
526 (**va
).param_binding_begin
= ~0;
527 (**va
).param_binding_length
= ~0;
528 (**va
).alias_binding
= NULL
;
534 var_cache_destroy (struct var_cache
**va
)
537 var_cache_destroy (&(**va
).next
);
544 var_cache_append (struct var_cache
**va
, struct var_cache
*nv
)
547 var_cache_append (&(**va
).next
, nv
);
552 static struct var_cache
*
553 var_cache_find (struct var_cache
*va
, GLubyte
* name
)
555 struct var_cache
*first
= va
;
558 if (!strcmp ( (const char*) name
, (const char*) va
->name
)) {
559 if (va
->type
== vt_alias
)
560 return var_cache_find (first
, va
->name
);
571 * constructs an integer from 4 GLubytes in LE format
574 parse_position (GLubyte
** inst
)
578 value
= (GLuint
) (*(*inst
)++);
579 value
+= (GLuint
) (*(*inst
)++) * 0x100;
580 value
+= (GLuint
) (*(*inst
)++) * 0x10000;
581 value
+= (GLuint
) (*(*inst
)++) * 0x1000000;
587 * This will, given a string, lookup the string as a variable name in the
588 * var cache. If the name is found, the var cache node corresponding to the
589 * var name is returned. If it is not found, a new entry is allocated
591 * \param I Points into the binary array where the string identifier begins
592 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
593 * \return The location on the var_cache corresponding the the string starting at I
595 static struct var_cache
*
596 parse_string (GLubyte
** inst
, struct var_cache
**vc_head
,
597 struct arb_program
*Program
, GLuint
* found
)
600 struct var_cache
*va
= NULL
;
603 *inst
+= _mesa_strlen ((char *) i
) + 1;
605 va
= var_cache_find (*vc_head
, i
);
613 var_cache_create (&va
);
616 var_cache_append (vc_head
, va
);
622 parse_string_without_adding (GLubyte
** inst
, struct arb_program
*Program
)
627 *inst
+= _mesa_strlen ((char *) i
) + 1;
633 * \return -1 if we parse '-', return 1 otherwise
636 parse_sign (GLubyte
** inst
)
638 /*return *(*inst)++ != '+'; */
644 else if (**inst
== '+') {
653 * parses and returns signed integer
656 parse_integer (GLubyte
** inst
, struct arb_program
*Program
)
661 /* check if *inst points to '+' or '-'
662 * if yes, grab the sign and increment *inst
664 sign
= parse_sign (inst
);
666 /* now check if *inst points to 0
667 * if yes, increment the *inst and return the default value
674 /* parse the integer as you normally would do it */
675 value
= _mesa_atoi (parse_string_without_adding (inst
, Program
));
677 /* now, after terminating 0 there is a position
678 * to parse it - parse_position()
680 Program
->Position
= parse_position (inst
);
688 parse_float (GLubyte
** inst
, struct arb_program
*Program
)
691 GLuint leading_zeros
=0;
694 tmp
[1] = parse_integer (inst
, Program
); /* This is the integer portion of the number */
696 /* Now we grab the fractional portion of the number (the digits after
697 * the .). We can have leading 0's here, which parse_integer will ignore,
698 * so we'll check for those first
700 while ((**inst
== '0') && ( *(*inst
+1) != 0))
705 tmp
[2] = parse_integer (inst
, Program
); /* This is the fractional portion of the number */
706 tmp
[3] = parse_sign (inst
); /* This is the sign of the exponent */
707 tmp
[4] = parse_integer (inst
, Program
); /* This is the exponent */
709 value
= (GLfloat
) tmp
[1];
711 while (denom
< tmp
[2])
713 denom
*= (GLint
) _mesa_pow( 10, leading_zeros
);
714 value
+= (GLfloat
) tmp
[2] / (GLfloat
) denom
;
716 value
*= (GLfloat
) _mesa_pow (10, (GLfloat
) tmp
[3] * (GLfloat
) tmp
[4]);
725 parse_signed_float (GLubyte
** inst
, struct arb_program
*Program
)
727 GLint sign
= parse_sign (inst
);
728 GLfloat value
= parse_float (inst
, Program
);
733 * This picks out a constant value from the parsed array. The constant vector is r
734 * returned in the *values array, which should be of length 4.
736 * \param values - The 4 component vector with the constant value in it
739 parse_constant (GLubyte
** inst
, GLfloat
*values
, struct arb_program
*Program
,
742 GLuint components
, i
;
745 switch (*(*inst
)++) {
746 case CONSTANT_SCALAR
:
747 if (use
== GL_TRUE
) {
750 values
[2] = values
[3] = parse_float (inst
, Program
);
755 values
[2] = values
[3] = parse_signed_float (inst
, Program
);
759 case CONSTANT_VECTOR
:
760 values
[0] = values
[1] = values
[2] = 0;
762 components
= *(*inst
)++;
763 for (i
= 0; i
< components
; i
++) {
764 values
[i
] = parse_signed_float (inst
, Program
);
771 * \param offset The offset from the address register that we should
774 * \return 0 on sucess, 1 on error
777 parse_relative_offset (GLcontext
*ctx
, GLubyte
**inst
, struct arb_program
*Program
,
780 *offset
= parse_integer(inst
, Program
);
785 * \param color 0 if color type is primary, 1 if color type is secondary
786 * \return 0 on sucess, 1 on error
789 parse_color_type (GLcontext
* ctx
, GLubyte
** inst
, struct arb_program
*Program
,
792 (void) ctx
; (void) Program
;
793 *color
= *(*inst
)++ != COLOR_PRIMARY
;
798 * Get an integer corresponding to a generic vertex attribute.
800 * \return 0 on sucess, 1 on error
803 parse_generic_attrib_num(GLcontext
*ctx
, GLubyte
** inst
,
804 struct arb_program
*Program
, GLuint
*attrib
)
806 GLint i
= parse_integer(inst
, Program
);
808 if ((i
< 0) || (i
> MAX_VERTEX_PROGRAM_ATTRIBS
))
810 _mesa_set_program_error (ctx
, Program
->Position
,
811 "Invalid generic vertex attribute index");
812 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid generic vertex attribute index");
817 *attrib
= (GLuint
) i
;
824 * \param color The index of the color buffer to write into
825 * \return 0 on sucess, 1 on error
828 parse_output_color_num (GLcontext
* ctx
, GLubyte
** inst
,
829 struct arb_program
*Program
, GLuint
* color
)
831 GLint i
= parse_integer (inst
, Program
);
833 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxDrawBuffers
)) {
834 _mesa_set_program_error (ctx
, Program
->Position
,
835 "Invalid draw buffer index");
836 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid draw buffer index");
846 * \param coord The texture unit index
847 * \return 0 on sucess, 1 on error
850 parse_texcoord_num (GLcontext
* ctx
, GLubyte
** inst
,
851 struct arb_program
*Program
, GLuint
* coord
)
853 GLint i
= parse_integer (inst
, Program
);
855 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxTextureUnits
)) {
856 _mesa_set_program_error (ctx
, Program
->Position
,
857 "Invalid texture unit index");
858 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid texture unit index");
867 * \param coord The weight index
868 * \return 0 on sucess, 1 on error
871 parse_weight_num (GLcontext
* ctx
, GLubyte
** inst
, struct arb_program
*Program
,
874 *coord
= parse_integer (inst
, Program
);
876 if ((*coord
< 0) || (*coord
>= 1)) {
877 _mesa_set_program_error (ctx
, Program
->Position
,
878 "Invalid weight index");
879 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid weight index");
887 * \param coord The clip plane index
888 * \return 0 on sucess, 1 on error
891 parse_clipplane_num (GLcontext
* ctx
, GLubyte
** inst
,
892 struct arb_program
*Program
, GLint
* coord
)
894 *coord
= parse_integer (inst
, Program
);
896 if ((*coord
< 0) || (*coord
>= (GLint
) ctx
->Const
.MaxClipPlanes
)) {
897 _mesa_set_program_error (ctx
, Program
->Position
,
898 "Invalid clip plane index");
899 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid clip plane index");
908 * \return 0 on front face, 1 on back face
911 parse_face_type (GLubyte
** inst
)
913 switch (*(*inst
)++) {
925 * Given a matrix and a modifier token on the binary array, return tokens
926 * that _mesa_fetch_state() [program.c] can understand.
928 * \param matrix - the matrix we are talking about
929 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
930 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
931 * \return 0 on sucess, 1 on failure
934 parse_matrix (GLcontext
* ctx
, GLubyte
** inst
, struct arb_program
*Program
,
935 GLint
* matrix
, GLint
* matrix_idx
, GLint
* matrix_modifier
)
937 GLubyte mat
= *(*inst
)++;
942 case MATRIX_MODELVIEW
:
943 *matrix
= STATE_MODELVIEW
;
944 *matrix_idx
= parse_integer (inst
, Program
);
945 if (*matrix_idx
> 0) {
946 _mesa_set_program_error (ctx
, Program
->Position
,
947 "ARB_vertex_blend not supported\n");
948 _mesa_error (ctx
, GL_INVALID_OPERATION
,
949 "ARB_vertex_blend not supported\n");
954 case MATRIX_PROJECTION
:
955 *matrix
= STATE_PROJECTION
;
963 *matrix
= STATE_TEXTURE
;
964 *matrix_idx
= parse_integer (inst
, Program
);
965 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxTextureUnits
) {
966 _mesa_set_program_error (ctx
, Program
->Position
,
967 "Invalid Texture Unit");
968 _mesa_error (ctx
, GL_INVALID_OPERATION
,
969 "Invalid Texture Unit: %d", *matrix_idx
);
974 /* This is not currently supported (ARB_matrix_palette) */
976 *matrix_idx
= parse_integer (inst
, Program
);
977 _mesa_set_program_error (ctx
, Program
->Position
,
978 "ARB_matrix_palette not supported\n");
979 _mesa_error (ctx
, GL_INVALID_OPERATION
,
980 "ARB_matrix_palette not supported\n");
985 *matrix
= STATE_PROGRAM
;
986 *matrix_idx
= parse_integer (inst
, Program
);
987 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxProgramMatrices
) {
988 _mesa_set_program_error (ctx
, Program
->Position
,
989 "Invalid Program Matrix");
990 _mesa_error (ctx
, GL_INVALID_OPERATION
,
991 "Invalid Program Matrix: %d", *matrix_idx
);
997 switch (*(*inst
)++) {
998 case MATRIX_MODIFIER_IDENTITY
:
999 *matrix_modifier
= 0;
1001 case MATRIX_MODIFIER_INVERSE
:
1002 *matrix_modifier
= STATE_MATRIX_INVERSE
;
1004 case MATRIX_MODIFIER_TRANSPOSE
:
1005 *matrix_modifier
= STATE_MATRIX_TRANSPOSE
;
1007 case MATRIX_MODIFIER_INVTRANS
:
1008 *matrix_modifier
= STATE_MATRIX_INVTRANS
;
1017 * This parses a state string (rather, the binary version of it) into
1018 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1020 * \param inst - the start in the binary arry to start working from
1021 * \param state_tokens - the storage for the 6-token state description
1022 * \return - 0 on sucess, 1 on error
1025 parse_state_single_item (GLcontext
* ctx
, GLubyte
** inst
,
1026 struct arb_program
*Program
, GLint
* state_tokens
)
1028 switch (*(*inst
)++) {
1029 case STATE_MATERIAL_PARSER
:
1030 state_tokens
[0] = STATE_MATERIAL
;
1031 state_tokens
[1] = parse_face_type (inst
);
1032 switch (*(*inst
)++) {
1033 case MATERIAL_AMBIENT
:
1034 state_tokens
[2] = STATE_AMBIENT
;
1036 case MATERIAL_DIFFUSE
:
1037 state_tokens
[2] = STATE_DIFFUSE
;
1039 case MATERIAL_SPECULAR
:
1040 state_tokens
[2] = STATE_SPECULAR
;
1042 case MATERIAL_EMISSION
:
1043 state_tokens
[2] = STATE_EMISSION
;
1045 case MATERIAL_SHININESS
:
1046 state_tokens
[2] = STATE_SHININESS
;
1051 case STATE_LIGHT_PARSER
:
1052 state_tokens
[0] = STATE_LIGHT
;
1053 state_tokens
[1] = parse_integer (inst
, Program
);
1055 /* Check the value of state_tokens[1] against the # of lights */
1056 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1057 _mesa_set_program_error (ctx
, Program
->Position
,
1058 "Invalid Light Number");
1059 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1060 "Invalid Light Number: %d", state_tokens
[1]);
1064 switch (*(*inst
)++) {
1066 state_tokens
[2] = STATE_AMBIENT
;
1069 state_tokens
[2] = STATE_DIFFUSE
;
1071 case LIGHT_SPECULAR
:
1072 state_tokens
[2] = STATE_SPECULAR
;
1074 case LIGHT_POSITION
:
1075 state_tokens
[2] = STATE_POSITION
;
1077 case LIGHT_ATTENUATION
:
1078 state_tokens
[2] = STATE_ATTENUATION
;
1081 state_tokens
[2] = STATE_HALF
;
1083 case LIGHT_SPOT_DIRECTION
:
1084 state_tokens
[2] = STATE_SPOT_DIRECTION
;
1089 case STATE_LIGHT_MODEL
:
1090 switch (*(*inst
)++) {
1091 case LIGHT_MODEL_AMBIENT
:
1092 state_tokens
[0] = STATE_LIGHTMODEL_AMBIENT
;
1094 case LIGHT_MODEL_SCENECOLOR
:
1095 state_tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
1096 state_tokens
[1] = parse_face_type (inst
);
1101 case STATE_LIGHT_PROD
:
1102 state_tokens
[0] = STATE_LIGHTPROD
;
1103 state_tokens
[1] = parse_integer (inst
, Program
);
1105 /* Check the value of state_tokens[1] against the # of lights */
1106 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1107 _mesa_set_program_error (ctx
, Program
->Position
,
1108 "Invalid Light Number");
1109 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1110 "Invalid Light Number: %d", state_tokens
[1]);
1114 state_tokens
[2] = parse_face_type (inst
);
1115 switch (*(*inst
)++) {
1116 case LIGHT_PROD_AMBIENT
:
1117 state_tokens
[3] = STATE_AMBIENT
;
1119 case LIGHT_PROD_DIFFUSE
:
1120 state_tokens
[3] = STATE_DIFFUSE
;
1122 case LIGHT_PROD_SPECULAR
:
1123 state_tokens
[3] = STATE_SPECULAR
;
1130 switch (*(*inst
)++) {
1132 state_tokens
[0] = STATE_FOG_COLOR
;
1135 state_tokens
[0] = STATE_FOG_PARAMS
;
1141 state_tokens
[1] = parse_integer (inst
, Program
);
1142 switch (*(*inst
)++) {
1144 state_tokens
[0] = STATE_TEXENV_COLOR
;
1153 state_tokens
[0] = STATE_TEXGEN
;
1154 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1156 if (parse_texcoord_num (ctx
, inst
, Program
, &coord
))
1158 state_tokens
[1] = coord
;
1163 /* 0 - s, 1 - t, 2 - r, 3 - q */
1166 if (type
== TEX_GEN_EYE
) {
1169 state_tokens
[2] = STATE_TEXGEN_EYE_S
;
1172 state_tokens
[2] = STATE_TEXGEN_EYE_T
;
1175 state_tokens
[2] = STATE_TEXGEN_EYE_R
;
1178 state_tokens
[2] = STATE_TEXGEN_EYE_Q
;
1185 state_tokens
[2] = STATE_TEXGEN_OBJECT_S
;
1188 state_tokens
[2] = STATE_TEXGEN_OBJECT_T
;
1191 state_tokens
[2] = STATE_TEXGEN_OBJECT_R
;
1194 state_tokens
[2] = STATE_TEXGEN_OBJECT_Q
;
1202 switch (*(*inst
)++) {
1204 state_tokens
[0] = STATE_DEPTH_RANGE
;
1209 case STATE_CLIP_PLANE
:
1210 state_tokens
[0] = STATE_CLIPPLANE
;
1211 state_tokens
[1] = parse_integer (inst
, Program
);
1212 if (parse_clipplane_num (ctx
, inst
, Program
, &state_tokens
[1]))
1217 switch (*(*inst
++)) {
1219 state_tokens
[0] = STATE_POINT_SIZE
;
1222 case POINT_ATTENUATION
:
1223 state_tokens
[0] = STATE_POINT_ATTENUATION
;
1228 /* XXX: I think this is the correct format for a matrix row */
1229 case STATE_MATRIX_ROWS
:
1230 state_tokens
[0] = STATE_MATRIX
;
1232 (ctx
, inst
, Program
, &state_tokens
[1], &state_tokens
[2],
1236 state_tokens
[3] = parse_integer (inst
, Program
); /* The first row to grab */
1238 if ((**inst
) != 0) { /* Either the last row, 0 */
1239 state_tokens
[4] = parse_integer (inst
, Program
);
1240 if (state_tokens
[4] < state_tokens
[3]) {
1241 _mesa_set_program_error (ctx
, Program
->Position
,
1242 "Second matrix index less than the first");
1243 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1244 "Second matrix index (%d) less than the first (%d)",
1245 state_tokens
[4], state_tokens
[3]);
1250 state_tokens
[4] = state_tokens
[3];
1260 * This parses a state string (rather, the binary version of it) into
1261 * a 6-token similar for the state fetching code in program.c
1263 * One might ask, why fetch these parameters into just like you fetch
1264 * state when they are already stored in other places?
1266 * Because of array offsets -> We can stick env/local parameters in the
1267 * middle of a parameter array and then index someplace into the array
1270 * One optimization might be to only do this for the cases where the
1271 * env/local parameters end up inside of an array, and leave the
1272 * single parameters (or arrays of pure env/local pareameters) in their
1273 * respective register files.
1275 * For ENV parameters, the format is:
1276 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1277 * state_tokens[1] = STATE_ENV
1278 * state_tokens[2] = the parameter index
1280 * for LOCAL parameters, the format is:
1281 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1282 * state_tokens[1] = STATE_LOCAL
1283 * state_tokens[2] = the parameter index
1285 * \param inst - the start in the binary arry to start working from
1286 * \param state_tokens - the storage for the 6-token state description
1287 * \return - 0 on sucess, 1 on failure
1290 parse_program_single_item (GLcontext
* ctx
, GLubyte
** inst
,
1291 struct arb_program
*Program
, GLint
* state_tokens
)
1293 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1294 state_tokens
[0] = STATE_FRAGMENT_PROGRAM
;
1296 state_tokens
[0] = STATE_VERTEX_PROGRAM
;
1299 switch (*(*inst
)++) {
1300 case PROGRAM_PARAM_ENV
:
1301 state_tokens
[1] = STATE_ENV
;
1302 state_tokens
[2] = parse_integer (inst
, Program
);
1304 /* Check state_tokens[2] against the number of ENV parameters available */
1305 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1306 (state_tokens
[2] >= (GLint
) ctx
->Const
.MaxFragmentProgramEnvParams
))
1308 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1309 (state_tokens
[2] >= (GLint
) ctx
->Const
.MaxVertexProgramEnvParams
))) {
1310 _mesa_set_program_error (ctx
, Program
->Position
,
1311 "Invalid Program Env Parameter");
1312 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1313 "Invalid Program Env Parameter: %d",
1320 case PROGRAM_PARAM_LOCAL
:
1321 state_tokens
[1] = STATE_LOCAL
;
1322 state_tokens
[2] = parse_integer (inst
, Program
);
1324 /* Check state_tokens[2] against the number of LOCAL parameters available */
1325 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1326 (state_tokens
[2] >= (GLint
) ctx
->Const
.MaxFragmentProgramLocalParams
))
1328 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1329 (state_tokens
[2] >= (GLint
) ctx
->Const
.MaxVertexProgramLocalParams
))) {
1330 _mesa_set_program_error (ctx
, Program
->Position
,
1331 "Invalid Program Local Parameter");
1332 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1333 "Invalid Program Local Parameter: %d",
1344 * For ARB_vertex_program, programs are not allowed to use both an explicit
1345 * vertex attribute and a generic vertex attribute corresponding to the same
1346 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1348 * This will walk our var_cache and make sure that nobody does anything fishy.
1350 * \return 0 on sucess, 1 on error
1353 generic_attrib_check(struct var_cache
*vc_head
)
1356 struct var_cache
*curr
;
1357 GLboolean explicitAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
],
1358 genericAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
];
1360 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1361 explicitAttrib
[a
] = GL_FALSE
;
1362 genericAttrib
[a
] = GL_FALSE
;
1367 if (curr
->type
== vt_attrib
) {
1368 if (curr
->attrib_is_generic
)
1369 genericAttrib
[ curr
->attrib_binding_idx
] = GL_TRUE
;
1371 explicitAttrib
[ curr
->attrib_binding_idx
] = GL_TRUE
;
1377 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1378 if ((explicitAttrib
[a
]) && (genericAttrib
[a
]))
1386 * This will handle the binding side of an ATTRIB var declaration
1388 * \param binding - the fragment input register state, defined in nvfragprog.h
1389 * \param binding_idx - the index in the attrib register file that binding is associated with
1390 * \return returns 0 on sucess, 1 on error
1392 * See nvfragparse.c for attrib register file layout
1395 parse_attrib_binding (GLcontext
* ctx
, GLubyte
** inst
,
1396 struct arb_program
*Program
, GLuint
* binding
,
1397 GLuint
* binding_idx
, GLuint
*is_generic
)
1404 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1405 switch (*(*inst
)++) {
1406 case FRAGMENT_ATTRIB_COLOR
:
1407 err
= parse_color_type (ctx
, inst
, Program
, &coord
);
1408 *binding
= FRAG_ATTRIB_COL0
+ coord
;
1409 *binding_idx
= 1 + coord
;
1412 case FRAGMENT_ATTRIB_TEXCOORD
:
1413 err
= parse_texcoord_num (ctx
, inst
, Program
, &texcoord
);
1414 *binding
= FRAG_ATTRIB_TEX0
+ texcoord
;
1415 *binding_idx
= 4 + texcoord
;
1418 case FRAGMENT_ATTRIB_FOGCOORD
:
1419 *binding
= FRAG_ATTRIB_FOGC
;
1423 case FRAGMENT_ATTRIB_POSITION
:
1424 *binding
= FRAG_ATTRIB_WPOS
;
1434 switch (*(*inst
)++) {
1435 case VERTEX_ATTRIB_POSITION
:
1436 *binding
= VERT_ATTRIB_POS
;
1440 case VERTEX_ATTRIB_WEIGHT
:
1444 err
= parse_weight_num (ctx
, inst
, Program
, &weight
);
1445 *binding
= VERT_ATTRIB_WEIGHT
;
1448 _mesa_set_program_error (ctx
, Program
->Position
,
1449 "ARB_vertex_blend not supported\n");
1450 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1451 "ARB_vertex_blend not supported\n");
1455 case VERTEX_ATTRIB_NORMAL
:
1456 *binding
= VERT_ATTRIB_NORMAL
;
1460 case VERTEX_ATTRIB_COLOR
:
1464 err
= parse_color_type (ctx
, inst
, Program
, &color
);
1466 *binding
= VERT_ATTRIB_COLOR1
;
1470 *binding
= VERT_ATTRIB_COLOR0
;
1476 case VERTEX_ATTRIB_FOGCOORD
:
1477 *binding
= VERT_ATTRIB_FOG
;
1481 case VERTEX_ATTRIB_TEXCOORD
:
1485 err
= parse_texcoord_num (ctx
, inst
, Program
, &unit
);
1486 *binding
= VERT_ATTRIB_TEX0
+ unit
;
1487 *binding_idx
= 8 + unit
;
1491 /* It looks like we don't support this at all, atm */
1492 case VERTEX_ATTRIB_MATRIXINDEX
:
1493 parse_integer (inst
, Program
);
1494 _mesa_set_program_error (ctx
, Program
->Position
,
1495 "ARB_palette_matrix not supported");
1496 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1497 "ARB_palette_matrix not supported");
1501 case VERTEX_ATTRIB_GENERIC
:
1505 if (!parse_generic_attrib_num(ctx
, inst
, Program
, &attrib
)) {
1509 *binding
= VERT_ATTRIB_POS
;
1512 *binding
= VERT_ATTRIB_WEIGHT
;
1515 *binding
= VERT_ATTRIB_NORMAL
;
1518 *binding
= VERT_ATTRIB_COLOR0
;
1521 *binding
= VERT_ATTRIB_COLOR1
;
1524 *binding
= VERT_ATTRIB_FOG
;
1531 *binding
= VERT_ATTRIB_TEX0
+ (attrib
-8);
1534 *binding_idx
= attrib
;
1545 /* Can this even happen? */
1547 _mesa_set_program_error (ctx
, Program
->Position
,
1548 "Bad attribute binding");
1549 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Bad attribute binding");
1552 Program
->InputsRead
|= (1 << *binding_idx
);
1558 * This translates between a binary token for an output variable type
1559 * and the mesa token for the same thing.
1562 * XXX: What is the 'name' for vertex program state? -> do we need it?
1563 * I don't think we do;
1565 * See nvfragprog.h for definitions
1567 * \param inst - The parsed tokens
1568 * \param binding - The name of the state we are binding too
1569 * \param binding_idx - The index into the result register file that this is bound too
1571 * See nvfragparse.c for the register file layout for fragment programs
1572 * See nvvertparse.c for the register file layout for vertex programs
1575 parse_result_binding (GLcontext
* ctx
, GLubyte
** inst
, GLuint
* binding
,
1576 GLuint
* binding_idx
, struct arb_program
*Program
)
1578 GLuint b
, out_color
;
1580 switch (*(*inst
)++) {
1581 case FRAGMENT_RESULT_COLOR
:
1582 /* for frag programs, this is FRAGMENT_RESULT_COLOR */
1583 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1584 /* This gets result of the color buffer we're supposed to
1587 parse_output_color_num(ctx
, inst
, Program
, &out_color
);
1589 *binding
= FRAG_OUTPUT_COLR
;
1591 /* XXX: We're ignoring the color buffer for now. */
1594 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1600 case FRAGMENT_RESULT_DEPTH
:
1601 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
1602 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1603 *binding
= FRAG_OUTPUT_DEPR
;
1606 /* for vtx programs, this is VERTEX_RESULT_COLOR */
1609 GLuint face_type
= parse_face_type(inst
);
1610 GLint color_type_ret
= parse_color_type(ctx
, inst
, Program
, &color_type
);
1614 if (color_type_ret
) return 1;
1616 /* secondary color */
1627 /* secondary color */
1639 case VERTEX_RESULT_FOGCOORD
:
1643 case VERTEX_RESULT_POINTSIZE
:
1647 case VERTEX_RESULT_TEXCOORD
:
1648 if (parse_texcoord_num (ctx
, inst
, Program
, &b
))
1650 *binding_idx
= 7 + b
;
1654 Program
->OutputsWritten
|= (1 << *binding_idx
);
1660 * This handles the declaration of ATTRIB variables
1663 * parse_vert_attrib_binding(), or something like that
1665 * \return 0 on sucess, 1 on error
1668 parse_attrib (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
1669 struct arb_program
*Program
)
1673 struct var_cache
*attrib_var
;
1675 attrib_var
= parse_string (inst
, vc_head
, Program
, &found
);
1676 Program
->Position
= parse_position (inst
);
1678 error_msg
= (char *)
1679 _mesa_malloc (_mesa_strlen ((char *) attrib_var
->name
) + 40);
1680 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
1683 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
1684 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
1686 _mesa_free (error_msg
);
1690 attrib_var
->type
= vt_attrib
;
1692 /* I think this is ok now - karl */
1694 /*if (Program->type == GL_FRAGMENT_PROGRAM_ARB) */
1696 if (parse_attrib_binding
1697 (ctx
, inst
, Program
, &attrib_var
->attrib_binding
,
1698 &attrib_var
->attrib_binding_idx
, &attrib_var
->attrib_is_generic
))
1700 if (generic_attrib_check(*vc_head
)) {
1701 _mesa_set_program_error (ctx
, Program
->Position
,
1702 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
1703 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1704 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
1710 Program
->Base
.NumAttributes
++;
1715 * \param use -- TRUE if we're called when declaring implicit parameters,
1716 * FALSE if we're declaraing variables. This has to do with
1717 * if we get a signed or unsigned float for scalar constants
1720 parse_param_elements (GLcontext
* ctx
, GLubyte
** inst
,
1721 struct var_cache
*param_var
,
1722 struct arb_program
*Program
, GLboolean use
)
1726 GLint state_tokens
[6];
1727 GLfloat const_values
[4];
1731 switch (*(*inst
)++) {
1732 case PARAM_STATE_ELEMENT
:
1734 if (parse_state_single_item (ctx
, inst
, Program
, state_tokens
))
1737 /* If we adding STATE_MATRIX that has multiple rows, we need to
1738 * unroll it and call _mesa_add_state_reference() for each row
1740 if ((state_tokens
[0] == STATE_MATRIX
)
1741 && (state_tokens
[3] != state_tokens
[4])) {
1743 GLint first_row
= state_tokens
[3];
1744 GLint last_row
= state_tokens
[4];
1746 for (row
= first_row
; row
<= last_row
; row
++) {
1747 state_tokens
[3] = state_tokens
[4] = row
;
1750 _mesa_add_state_reference (Program
->Parameters
,
1752 if (param_var
->param_binding_begin
== ~0U)
1753 param_var
->param_binding_begin
= idx
;
1754 param_var
->param_binding_length
++;
1755 Program
->Base
.NumParameters
++;
1760 _mesa_add_state_reference (Program
->Parameters
, state_tokens
);
1761 if (param_var
->param_binding_begin
== ~0U)
1762 param_var
->param_binding_begin
= idx
;
1763 param_var
->param_binding_length
++;
1764 Program
->Base
.NumParameters
++;
1768 case PARAM_PROGRAM_ELEMENT
:
1770 if (parse_program_single_item (ctx
, inst
, Program
, state_tokens
))
1772 idx
= _mesa_add_state_reference (Program
->Parameters
, state_tokens
);
1773 if (param_var
->param_binding_begin
== ~0U)
1774 param_var
->param_binding_begin
= idx
;
1775 param_var
->param_binding_length
++;
1776 Program
->Base
.NumParameters
++;
1778 /* Check if there is more: 0 -> we're done, else its an integer */
1780 GLuint out_of_range
, new_idx
;
1781 GLuint start_idx
= state_tokens
[2] + 1;
1782 GLuint end_idx
= parse_integer (inst
, Program
);
1785 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1786 if (((state_tokens
[1] == STATE_ENV
)
1787 && (end_idx
>= ctx
->Const
.MaxFragmentProgramEnvParams
))
1788 || ((state_tokens
[1] == STATE_LOCAL
)
1790 ctx
->Const
.MaxFragmentProgramLocalParams
)))
1794 if (((state_tokens
[1] == STATE_ENV
)
1795 && (end_idx
>= ctx
->Const
.MaxVertexProgramEnvParams
))
1796 || ((state_tokens
[1] == STATE_LOCAL
)
1798 ctx
->Const
.MaxVertexProgramLocalParams
)))
1802 _mesa_set_program_error (ctx
, Program
->Position
,
1803 "Invalid Program Parameter");
1804 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1805 "Invalid Program Parameter: %d", end_idx
);
1809 for (new_idx
= start_idx
; new_idx
<= end_idx
; new_idx
++) {
1810 state_tokens
[2] = new_idx
;
1812 _mesa_add_state_reference (Program
->Parameters
,
1814 param_var
->param_binding_length
++;
1815 Program
->Base
.NumParameters
++;
1824 case PARAM_CONSTANT
:
1825 parse_constant (inst
, const_values
, Program
, use
);
1827 _mesa_add_named_constant (Program
->Parameters
,
1828 (char *) param_var
->name
, const_values
);
1829 if (param_var
->param_binding_begin
== ~0U)
1830 param_var
->param_binding_begin
= idx
;
1831 param_var
->param_binding_length
++;
1832 Program
->Base
.NumParameters
++;
1836 _mesa_set_program_error (ctx
, Program
->Position
,
1837 "Unexpected token in parse_param_elements()");
1838 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1839 "Unexpected token in parse_param_elements()");
1843 /* Make sure we haven't blown past our parameter limits */
1844 if (((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1845 (Program
->Base
.NumParameters
>=
1846 ctx
->Const
.MaxVertexProgramLocalParams
))
1847 || ((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1848 && (Program
->Base
.NumParameters
>=
1849 ctx
->Const
.MaxFragmentProgramLocalParams
))) {
1850 _mesa_set_program_error (ctx
, Program
->Position
,
1851 "Too many parameter variables");
1852 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Too many parameter variables");
1860 * This picks out PARAM program parameter bindings.
1862 * XXX: This needs to be stressed & tested
1864 * \return 0 on sucess, 1 on error
1867 parse_param (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
1868 struct arb_program
*Program
)
1871 GLint specified_length
;
1873 struct var_cache
*param_var
;
1876 param_var
= parse_string (inst
, vc_head
, Program
, &found
);
1877 Program
->Position
= parse_position (inst
);
1880 error_msg
= (char *) _mesa_malloc (_mesa_strlen ((char *) param_var
->name
) + 40);
1881 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
1884 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
1885 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
1887 _mesa_free (error_msg
);
1891 specified_length
= parse_integer (inst
, Program
);
1893 if (specified_length
< 0) {
1894 _mesa_set_program_error (ctx
, Program
->Position
,
1895 "Negative parameter array length");
1896 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1897 "Negative parameter array length: %d", specified_length
);
1901 param_var
->type
= vt_param
;
1902 param_var
->param_binding_length
= 0;
1904 /* Right now, everything is shoved into the main state register file.
1906 * In the future, it would be nice to leave things ENV/LOCAL params
1907 * in their respective register files, if possible
1909 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1912 * * - add each guy to the parameter list
1913 * * - increment the param_var->param_binding_len
1914 * * - store the param_var->param_binding_begin for the first one
1915 * * - compare the actual len to the specified len at the end
1917 while (**inst
!= PARAM_NULL
) {
1918 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_FALSE
))
1922 /* Test array length here! */
1923 if (specified_length
) {
1924 if (specified_length
!= (int)param_var
->param_binding_length
) {
1925 _mesa_set_program_error (ctx
, Program
->Position
,
1926 "Declared parameter array lenght does not match parameter list");
1927 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1928 "Declared parameter array lenght does not match parameter list");
1941 parse_param_use (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
1942 struct arb_program
*Program
, struct var_cache
**new_var
)
1944 struct var_cache
*param_var
;
1946 /* First, insert a dummy entry into the var_cache */
1947 var_cache_create (¶m_var
);
1948 param_var
->name
= (GLubyte
*) _mesa_strdup (" ");
1949 param_var
->type
= vt_param
;
1951 param_var
->param_binding_length
= 0;
1952 /* Don't fill in binding_begin; We use the default value of -1
1953 * to tell if its already initialized, elsewhere.
1955 * param_var->param_binding_begin = 0;
1957 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1959 var_cache_append (vc_head
, param_var
);
1961 /* Then fill it with juicy parameter goodness */
1962 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_TRUE
))
1965 *new_var
= param_var
;
1972 * This handles the declaration of TEMP variables
1974 * \return 0 on sucess, 1 on error
1977 parse_temp (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
1978 struct arb_program
*Program
)
1981 struct var_cache
*temp_var
;
1984 while (**inst
!= 0) {
1985 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
1986 Program
->Position
= parse_position (inst
);
1988 error_msg
= (char *)
1989 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
1990 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
1993 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
1994 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
1996 _mesa_free (error_msg
);
2000 temp_var
->type
= vt_temp
;
2002 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
2003 (Program
->Base
.NumTemporaries
>=
2004 ctx
->Const
.MaxFragmentProgramTemps
))
2005 || ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
2006 && (Program
->Base
.NumTemporaries
>=
2007 ctx
->Const
.MaxVertexProgramTemps
))) {
2008 _mesa_set_program_error (ctx
, Program
->Position
,
2009 "Too many TEMP variables declared");
2010 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2011 "Too many TEMP variables declared");
2015 temp_var
->temp_binding
= Program
->Base
.NumTemporaries
;
2016 Program
->Base
.NumTemporaries
++;
2024 * This handles variables of the OUTPUT variety
2026 * \return 0 on sucess, 1 on error
2029 parse_output (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2030 struct arb_program
*Program
)
2033 struct var_cache
*output_var
;
2035 output_var
= parse_string (inst
, vc_head
, Program
, &found
);
2036 Program
->Position
= parse_position (inst
);
2039 error_msg
= (char *)
2040 _mesa_malloc (_mesa_strlen ((char *) output_var
->name
) + 40);
2041 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
2044 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
2045 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
2047 _mesa_free (error_msg
);
2051 output_var
->type
= vt_output
;
2052 return parse_result_binding (ctx
, inst
, &output_var
->output_binding
,
2053 &output_var
->output_binding_idx
, Program
);
2057 * This handles variables of the ALIAS kind
2059 * \return 0 on sucess, 1 on error
2062 parse_alias (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2063 struct arb_program
*Program
)
2066 struct var_cache
*temp_var
;
2070 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2071 Program
->Position
= parse_position (inst
);
2074 error_msg
= (char *)
2075 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2076 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
2079 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
2080 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
2082 _mesa_free (error_msg
);
2086 temp_var
->type
= vt_alias
;
2087 temp_var
->alias_binding
= parse_string (inst
, vc_head
, Program
, &found
);
2088 Program
->Position
= parse_position (inst
);
2092 error_msg
= (char *)
2093 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2094 _mesa_sprintf (error_msg
, "Alias value %s is not defined",
2095 temp_var
->alias_binding
->name
);
2097 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
2098 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
2100 _mesa_free (error_msg
);
2108 * This handles variables of the ADDRESS kind
2110 * \return 0 on sucess, 1 on error
2113 parse_address (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2114 struct arb_program
*Program
)
2117 struct var_cache
*temp_var
;
2120 while (**inst
!= 0) {
2121 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2122 Program
->Position
= parse_position (inst
);
2124 error_msg
= (char *)
2125 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2126 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
2129 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
2130 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
2132 _mesa_free (error_msg
);
2136 temp_var
->type
= vt_address
;
2138 if (Program
->Base
.NumAddressRegs
>=
2139 ctx
->Const
.MaxVertexProgramAddressRegs
) {
2140 _mesa_set_program_error (ctx
, Program
->Position
,
2141 "Too many ADDRESS variables declared");
2142 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2143 "Too many ADDRESS variables declared");
2147 temp_var
->address_binding
= Program
->Base
.NumAddressRegs
;
2148 Program
->Base
.NumAddressRegs
++;
2156 * Parse a program declaration
2158 * \return 0 on sucess, 1 on error
2161 parse_declaration (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2162 struct arb_program
*Program
)
2166 switch (*(*inst
)++) {
2168 err
= parse_address (ctx
, inst
, vc_head
, Program
);
2172 err
= parse_alias (ctx
, inst
, vc_head
, Program
);
2176 err
= parse_attrib (ctx
, inst
, vc_head
, Program
);
2180 err
= parse_output (ctx
, inst
, vc_head
, Program
);
2184 err
= parse_param (ctx
, inst
, vc_head
, Program
);
2188 err
= parse_temp (ctx
, inst
, vc_head
, Program
);
2196 * Handle the parsing out of a masked destination register
2198 * If we are a vertex program, make sure we don't write to
2199 * result.position of we have specified that the program is
2200 * position invariant
2202 * \param File - The register file we write to
2203 * \param Index - The register index we write to
2204 * \param WriteMask - The mask controlling which components we write (1->write)
2206 * \return 0 on sucess, 1 on error
2209 parse_masked_dst_reg (GLcontext
* ctx
, GLubyte
** inst
,
2210 struct var_cache
**vc_head
, struct arb_program
*Program
,
2211 GLint
* File
, GLint
* Index
, GLboolean
* WriteMask
)
2215 struct var_cache
*dst
;
2217 /* We either have a result register specified, or a
2218 * variable that may or may not be writable
2220 switch (*(*inst
)++) {
2221 case REGISTER_RESULT
:
2222 if (parse_result_binding
2223 (ctx
, inst
, &result
, (GLuint
*) Index
, Program
))
2225 *File
= PROGRAM_OUTPUT
;
2228 case REGISTER_ESTABLISHED_NAME
:
2229 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2230 Program
->Position
= parse_position (inst
);
2232 /* If the name has never been added to our symbol table, we're hosed */
2234 _mesa_set_program_error (ctx
, Program
->Position
,
2235 "0: Undefined variable");
2236 _mesa_error (ctx
, GL_INVALID_OPERATION
, "0: Undefined variable: %s",
2241 switch (dst
->type
) {
2243 *File
= PROGRAM_OUTPUT
;
2244 *Index
= dst
->output_binding_idx
;
2248 *File
= PROGRAM_TEMPORARY
;
2249 *Index
= dst
->temp_binding
;
2252 /* If the var type is not vt_output or vt_temp, no go */
2254 _mesa_set_program_error (ctx
, Program
->Position
,
2255 "Destination register is read only");
2256 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2257 "Destination register is read only: %s",
2264 _mesa_set_program_error (ctx
, Program
->Position
,
2265 "Unexpected opcode in parse_masked_dst_reg()");
2266 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2267 "Unexpected opcode in parse_masked_dst_reg()");
2272 /* Position invariance test */
2273 /* This test is done now in syntax portion - when position invariance OPTION
2274 is specified, "result.position" rule is disabled so there is no way
2275 to write the position
2277 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2279 _mesa_set_program_error (ctx, Program->Position,
2280 "Vertex program specified position invariance and wrote vertex position");
2281 _mesa_error (ctx, GL_INVALID_OPERATION,
2282 "Vertex program specified position invariance and wrote vertex position");
2285 /* And then the mask.
2293 WriteMask
[0] = (GLboolean
) (mask
& (1 << 3)) >> 3;
2294 WriteMask
[1] = (GLboolean
) (mask
& (1 << 2)) >> 2;
2295 WriteMask
[2] = (GLboolean
) (mask
& (1 << 1)) >> 1;
2296 WriteMask
[3] = (GLboolean
) (mask
& (1));
2303 * Handle the parsing of a address register
2305 * \param Index - The register index we write to
2307 * \return 0 on sucess, 1 on error
2310 parse_address_reg (GLcontext
* ctx
, GLubyte
** inst
,
2311 struct var_cache
**vc_head
,
2312 struct arb_program
*Program
, GLint
* Index
)
2314 struct var_cache
*dst
;
2318 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2319 Program
->Position
= parse_position (inst
);
2321 /* If the name has never been added to our symbol table, we're hosed */
2323 _mesa_set_program_error (ctx
, Program
->Position
, "Undefined variable");
2324 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Undefined variable: %s",
2329 if (dst
->type
!= vt_address
) {
2330 _mesa_set_program_error (ctx
, Program
->Position
,
2331 "Variable is not of type ADDRESS");
2332 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2333 "Variable: %s is not of type ADDRESS", dst
->name
);
2341 * Handle the parsing out of a masked address register
2343 * \param Index - The register index we write to
2344 * \param WriteMask - The mask controlling which components we write (1->write)
2346 * \return 0 on sucess, 1 on error
2349 parse_masked_address_reg (GLcontext
* ctx
, GLubyte
** inst
,
2350 struct var_cache
**vc_head
,
2351 struct arb_program
*Program
, GLint
* Index
,
2352 GLboolean
* WriteMask
)
2354 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, Index
))
2357 /* This should be 0x8 */
2360 /* Writemask of .x is implied */
2362 WriteMask
[1] = WriteMask
[2] = WriteMask
[3] = 0;
2369 * Parse out a swizzle mask.
2371 * The values in the input stream are:
2372 * COMPONENT_X -> x/r
2373 * COMPONENT_Y -> y/g
2377 * The values in the output mask are:
2383 * The len parameter allows us to grab 4 components for a vector
2384 * swizzle, or just 1 component for a scalar src register selection
2387 parse_swizzle_mask (GLubyte
** inst
, GLubyte
* mask
, GLint len
)
2391 for (a
= 0; a
< 4; a
++)
2394 for (a
= 0; a
< len
; a
++) {
2395 switch (*(*inst
)++) {
2420 parse_extended_swizzle_mask (GLubyte
** inst
, GLubyte
* mask
, GLboolean
* Negate
)
2426 for (a
= 0; a
< 4; a
++) {
2427 if (parse_sign (inst
) == -1)
2434 mask
[a
] = SWIZZLE_ZERO
;
2437 mask
[a
] = SWIZZLE_ONE
;
2440 mask
[a
] = SWIZZLE_X
;
2443 mask
[a
] = SWIZZLE_Y
;
2446 mask
[a
] = SWIZZLE_Z
;
2449 mask
[a
] = SWIZZLE_W
;
2455 mask
[a
] = SWIZZLE_ZERO
;
2457 mask
[a
] = SWIZZLE_ONE
;
2469 parse_src_reg (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2470 struct arb_program
*Program
, GLint
* File
, GLint
* Index
,
2471 GLboolean
*IsRelOffset
)
2473 struct var_cache
*src
;
2474 GLuint binding_state
, binding_idx
, is_generic
, found
;
2477 /* And the binding for the src */
2478 switch (*(*inst
)++) {
2479 case REGISTER_ATTRIB
:
2480 if (parse_attrib_binding
2481 (ctx
, inst
, Program
, &binding_state
, &binding_idx
, &is_generic
))
2483 *File
= PROGRAM_INPUT
;
2484 *Index
= binding_idx
;
2486 /* We need to insert a dummy variable into the var_cache so we can
2487 * catch generic vertex attrib aliasing errors
2489 var_cache_create(&src
);
2490 src
->type
= vt_attrib
;
2491 src
->name
= (GLubyte
*)_mesa_strdup("Dummy Attrib Variable");
2492 src
->attrib_binding
= binding_state
;
2493 src
->attrib_binding_idx
= binding_idx
;
2494 src
->attrib_is_generic
= is_generic
;
2495 var_cache_append(vc_head
, src
);
2496 if (generic_attrib_check(*vc_head
)) {
2497 _mesa_set_program_error (ctx
, Program
->Position
,
2498 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
2499 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2500 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
2505 case REGISTER_PARAM
:
2507 case PARAM_ARRAY_ELEMENT
:
2509 src
= parse_string (inst
, vc_head
, Program
, &found
);
2510 Program
->Position
= parse_position (inst
);
2513 _mesa_set_program_error (ctx
, Program
->Position
,
2514 "2: Undefined variable");
2515 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2516 "2: Undefined variable: %s", src
->name
);
2520 *File
= src
->param_binding_type
;
2522 switch (*(*inst
)++) {
2523 case ARRAY_INDEX_ABSOLUTE
:
2524 offset
= parse_integer (inst
, Program
);
2527 || (offset
>= (int)src
->param_binding_length
)) {
2528 _mesa_set_program_error (ctx
, Program
->Position
,
2529 "Index out of range");
2530 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2531 "Index %d out of range for %s", offset
,
2536 *Index
= src
->param_binding_begin
+ offset
;
2539 case ARRAY_INDEX_RELATIVE
:
2541 GLint addr_reg_idx
, rel_off
;
2543 /* First, grab the address regiseter */
2544 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &addr_reg_idx
))
2553 /* Then the relative offset */
2554 if (parse_relative_offset(ctx
, inst
, Program
, &rel_off
)) return 1;
2556 /* And store it properly */
2557 *Index
= src
->param_binding_begin
+ rel_off
;
2566 if (parse_param_use (ctx
, inst
, vc_head
, Program
, &src
))
2569 *File
= src
->param_binding_type
;
2570 *Index
= src
->param_binding_begin
;
2575 case REGISTER_ESTABLISHED_NAME
:
2577 src
= parse_string (inst
, vc_head
, Program
, &found
);
2578 Program
->Position
= parse_position (inst
);
2580 /* If the name has never been added to our symbol table, we're hosed */
2582 _mesa_set_program_error (ctx
, Program
->Position
,
2583 "3: Undefined variable");
2584 _mesa_error (ctx
, GL_INVALID_OPERATION
, "3: Undefined variable: %s",
2589 switch (src
->type
) {
2591 *File
= PROGRAM_INPUT
;
2592 *Index
= src
->attrib_binding_idx
;
2595 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2597 *File
= src
->param_binding_type
;
2598 *Index
= src
->param_binding_begin
;
2602 *File
= PROGRAM_TEMPORARY
;
2603 *Index
= src
->temp_binding
;
2606 /* If the var type is vt_output no go */
2608 _mesa_set_program_error (ctx
, Program
->Position
,
2609 "destination register is read only");
2610 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2611 "destination register is read only: %s",
2618 _mesa_set_program_error (ctx
, Program
->Position
,
2619 "Unknown token in parse_src_reg");
2620 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2621 "Unknown token in parse_src_reg");
2631 parse_vector_src_reg (GLcontext
* ctx
, GLubyte
** inst
,
2632 struct var_cache
**vc_head
, struct arb_program
*Program
,
2633 GLint
* File
, GLint
* Index
, GLboolean
* Negate
,
2634 GLubyte
* Swizzle
, GLboolean
*IsRelOffset
)
2637 *Negate
= (parse_sign (inst
) == -1);
2639 /* And the src reg */
2640 if (parse_src_reg (ctx
, inst
, vc_head
, Program
, File
, Index
, IsRelOffset
))
2643 /* finally, the swizzle */
2644 parse_swizzle_mask (inst
, Swizzle
, 4);
2652 parse_scalar_src_reg (GLcontext
* ctx
, GLubyte
** inst
,
2653 struct var_cache
**vc_head
, struct arb_program
*Program
,
2654 GLint
* File
, GLint
* Index
, GLboolean
* Negate
,
2655 GLubyte
* Swizzle
, GLboolean
*IsRelOffset
)
2658 *Negate
= (parse_sign (inst
) == -1);
2660 /* And the src reg */
2661 if (parse_src_reg (ctx
, inst
, vc_head
, Program
, File
, Index
, IsRelOffset
))
2664 /* Now, get the component and shove it into all the swizzle slots */
2665 parse_swizzle_mask (inst
, Swizzle
, 1);
2671 * This is a big mother that handles getting opcodes into the instruction
2672 * and handling the src & dst registers for fragment program instructions
2675 parse_fp_instruction (GLcontext
* ctx
, GLubyte
** inst
,
2676 struct var_cache
**vc_head
, struct arb_program
*Program
,
2677 struct fp_instruction
*fp
)
2680 GLubyte swz
[4]; /* FP's swizzle mask is a GLubyte, while VP's is GLuint */
2682 GLubyte instClass
, type
, code
;
2685 /* No condition codes in ARB_fp */
2686 fp
->UpdateCondRegister
= 0;
2688 /* Record the position in the program string for debugging */
2689 fp
->StringPos
= Program
->Position
;
2691 /* OP_ALU_INST or OP_TEX_INST */
2692 instClass
= *(*inst
)++;
2694 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2695 * OP_TEX_{SAMPLE, KIL}
2699 /* The actual opcode name */
2702 /* Increment the correct count */
2703 switch (instClass
) {
2705 Program
->NumAluInstructions
++;
2708 Program
->NumTexInstructions
++;
2713 fp
->Precision
= FLOAT32
;
2715 fp
->DstReg
.CondMask
= COND_TR
;
2723 fp
->Opcode
= FP_OPCODE_ABS
;
2729 fp
->Opcode
= FP_OPCODE_FLR
;
2735 fp
->Opcode
= FP_OPCODE_FRC
;
2741 fp
->Opcode
= FP_OPCODE_LIT
;
2747 fp
->Opcode
= FP_OPCODE_MOV
;
2751 if (parse_masked_dst_reg
2752 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
2753 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
2756 fp
->SrcReg
[0].Abs
= GL_FALSE
;
2757 fp
->SrcReg
[0].NegateAbs
= GL_FALSE
;
2758 if (parse_vector_src_reg
2759 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[0].File
,
2760 &fp
->SrcReg
[0].Index
, &fp
->SrcReg
[0].NegateBase
,
2764 fp
->SrcReg
[0].Swizzle
[b
] = swz
[b
];
2772 fp
->Opcode
= FP_OPCODE_COS
;
2778 fp
->Opcode
= FP_OPCODE_EX2
;
2784 fp
->Opcode
= FP_OPCODE_LG2
;
2790 fp
->Opcode
= FP_OPCODE_RCP
;
2796 fp
->Opcode
= FP_OPCODE_RSQ
;
2802 fp
->Opcode
= FP_OPCODE_SIN
;
2809 fp
->Opcode
= FP_OPCODE_SCS
;
2813 if (parse_masked_dst_reg
2814 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
2815 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
2817 fp
->SrcReg
[0].Abs
= GL_FALSE
;
2818 fp
->SrcReg
[0].NegateAbs
= GL_FALSE
;
2819 if (parse_scalar_src_reg
2820 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[0].File
,
2821 &fp
->SrcReg
[0].Index
, &fp
->SrcReg
[0].NegateBase
,
2825 fp
->SrcReg
[0].Swizzle
[b
] = swz
[b
];
2833 fp
->Opcode
= FP_OPCODE_POW
;
2837 if (parse_masked_dst_reg
2838 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
2839 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
2841 for (a
= 0; a
< 2; a
++) {
2842 fp
->SrcReg
[a
].Abs
= GL_FALSE
;
2843 fp
->SrcReg
[a
].NegateAbs
= GL_FALSE
;
2844 if (parse_scalar_src_reg
2845 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[a
].File
,
2846 &fp
->SrcReg
[a
].Index
, &fp
->SrcReg
[a
].NegateBase
,
2850 fp
->SrcReg
[a
].Swizzle
[b
] = swz
[b
];
2860 fp
->Opcode
= FP_OPCODE_ADD
;
2866 fp
->Opcode
= FP_OPCODE_DP3
;
2872 fp
->Opcode
= FP_OPCODE_DP4
;
2878 fp
->Opcode
= FP_OPCODE_DPH
;
2884 fp
->Opcode
= FP_OPCODE_DST
;
2890 fp
->Opcode
= FP_OPCODE_MAX
;
2896 fp
->Opcode
= FP_OPCODE_MIN
;
2902 fp
->Opcode
= FP_OPCODE_MUL
;
2908 fp
->Opcode
= FP_OPCODE_SGE
;
2914 fp
->Opcode
= FP_OPCODE_SLT
;
2920 fp
->Opcode
= FP_OPCODE_SUB
;
2926 fp
->Opcode
= FP_OPCODE_XPD
;
2930 if (parse_masked_dst_reg
2931 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
2932 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
2934 for (a
= 0; a
< 2; a
++) {
2935 fp
->SrcReg
[a
].Abs
= GL_FALSE
;
2936 fp
->SrcReg
[a
].NegateAbs
= GL_FALSE
;
2937 if (parse_vector_src_reg
2938 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[a
].File
,
2939 &fp
->SrcReg
[a
].Index
, &fp
->SrcReg
[a
].NegateBase
,
2943 fp
->SrcReg
[a
].Swizzle
[b
] = swz
[b
];
2952 fp
->Opcode
= FP_OPCODE_CMP
;
2958 fp
->Opcode
= FP_OPCODE_LRP
;
2964 fp
->Opcode
= FP_OPCODE_MAD
;
2968 if (parse_masked_dst_reg
2969 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
2970 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
2972 for (a
= 0; a
< 3; a
++) {
2973 fp
->SrcReg
[a
].Abs
= GL_FALSE
;
2974 fp
->SrcReg
[a
].NegateAbs
= GL_FALSE
;
2975 if (parse_vector_src_reg
2976 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[a
].File
,
2977 &fp
->SrcReg
[a
].Index
, &fp
->SrcReg
[a
].NegateBase
,
2981 fp
->SrcReg
[a
].Swizzle
[b
] = swz
[b
];
2990 fp
->Opcode
= FP_OPCODE_SWZ
;
2993 if (parse_masked_dst_reg
2994 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
2995 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
2999 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[0].File
,
3000 &fp
->SrcReg
[0].Index
, &rel
))
3002 parse_extended_swizzle_mask (inst
, swz
,
3003 &fp
->SrcReg
[0].NegateBase
);
3005 fp
->SrcReg
[0].Swizzle
[b
] = swz
[b
];
3013 fp
->Opcode
= FP_OPCODE_TEX
;
3019 fp
->Opcode
= FP_OPCODE_TXP
;
3026 fp
->Opcode
= FP_OPCODE_TXB
;
3030 if (parse_masked_dst_reg
3031 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
3032 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
3034 fp
->SrcReg
[0].Abs
= GL_FALSE
;
3035 fp
->SrcReg
[0].NegateAbs
= GL_FALSE
;
3036 if (parse_vector_src_reg
3037 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[0].File
,
3038 &fp
->SrcReg
[0].Index
, &fp
->SrcReg
[0].NegateBase
,
3042 fp
->SrcReg
[0].Swizzle
[b
] = swz
[b
];
3045 if (parse_texcoord_num (ctx
, inst
, Program
, &texcoord
))
3047 fp
->TexSrcUnit
= texcoord
;
3050 switch (*(*inst
)++) {
3052 fp
->TexSrcBit
= TEXTURE_1D_BIT
;
3055 fp
->TexSrcBit
= TEXTURE_2D_BIT
;
3058 fp
->TexSrcBit
= TEXTURE_3D_BIT
;
3060 case TEXTARGET_RECT
:
3061 fp
->TexSrcBit
= TEXTURE_RECT_BIT
;
3063 case TEXTARGET_CUBE
:
3064 fp
->TexSrcBit
= TEXTURE_CUBE_BIT
;
3066 case TEXTARGET_SHADOW1D
:
3067 case TEXTARGET_SHADOW2D
:
3068 case TEXTARGET_SHADOWRECT
:
3069 /* TODO ARB_fragment_program_shadow code */
3072 Program
->TexturesUsed
[texcoord
] |= fp
->TexSrcBit
;
3076 fp
->Opcode
= FP_OPCODE_KIL
;
3077 fp
->SrcReg
[0].Abs
= GL_FALSE
;
3078 fp
->SrcReg
[0].NegateAbs
= GL_FALSE
;
3079 if (parse_vector_src_reg
3080 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[0].File
,
3081 &fp
->SrcReg
[0].Index
, &fp
->SrcReg
[0].NegateBase
,
3085 fp
->SrcReg
[0].Swizzle
[b
] = swz
[b
];
3093 * This is a big mother that handles getting opcodes into the instruction
3094 * and handling the src & dst registers for vertex program instructions
3097 parse_vp_instruction (GLcontext
* ctx
, GLubyte
** inst
,
3098 struct var_cache
**vc_head
, struct arb_program
*Program
,
3099 struct vp_instruction
*vp
)
3104 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3107 /* The actual opcode name */
3110 /* Record the position in the program string for debugging */
3111 vp
->StringPos
= Program
->Position
;
3113 vp
->SrcReg
[0].RelAddr
= vp
->SrcReg
[1].RelAddr
= vp
->SrcReg
[2].RelAddr
= 0;
3115 for (a
= 0; a
< 4; a
++) {
3116 vp
->SrcReg
[0].Swizzle
[a
] = a
;
3117 vp
->SrcReg
[1].Swizzle
[a
] = a
;
3118 vp
->SrcReg
[2].Swizzle
[a
] = a
;
3119 vp
->DstReg
.WriteMask
[a
] = 1;
3125 vp
->Opcode
= VP_OPCODE_ARL
;
3127 /* Remember to set SrcReg.RelAddr; */
3129 /* Get the masked address register [dst] */
3130 if (parse_masked_address_reg
3131 (ctx
, inst
, vc_head
, Program
, &vp
->DstReg
.Index
,
3132 vp
->DstReg
.WriteMask
))
3134 vp
->DstReg
.File
= PROGRAM_ADDRESS
;
3136 /* Get a scalar src register */
3137 if (parse_scalar_src_reg
3138 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[0].File
,
3139 &vp
->SrcReg
[0].Index
, &vp
->SrcReg
[0].Negate
,
3140 vp
->SrcReg
[0].Swizzle
, &vp
->SrcReg
[0].RelAddr
))
3148 vp
->Opcode
= VP_OPCODE_ABS
;
3151 vp
->Opcode
= VP_OPCODE_FLR
;
3154 vp
->Opcode
= VP_OPCODE_FRC
;
3157 vp
->Opcode
= VP_OPCODE_LIT
;
3160 vp
->Opcode
= VP_OPCODE_MOV
;
3163 if (parse_masked_dst_reg
3164 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->DstReg
.File
,
3165 &vp
->DstReg
.Index
, vp
->DstReg
.WriteMask
))
3167 if (parse_vector_src_reg
3168 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[0].File
,
3169 &vp
->SrcReg
[0].Index
, &vp
->SrcReg
[0].Negate
,
3170 vp
->SrcReg
[0].Swizzle
, &vp
->SrcReg
[0].RelAddr
))
3177 vp
->Opcode
= VP_OPCODE_EX2
;
3180 vp
->Opcode
= VP_OPCODE_EXP
;
3183 vp
->Opcode
= VP_OPCODE_LG2
;
3186 vp
->Opcode
= VP_OPCODE_LOG
;
3189 vp
->Opcode
= VP_OPCODE_RCP
;
3192 vp
->Opcode
= VP_OPCODE_RSQ
;
3195 if (parse_masked_dst_reg
3196 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->DstReg
.File
,
3197 &vp
->DstReg
.Index
, vp
->DstReg
.WriteMask
))
3199 if (parse_scalar_src_reg
3200 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[0].File
,
3201 &vp
->SrcReg
[0].Index
, &vp
->SrcReg
[0].Negate
,
3202 vp
->SrcReg
[0].Swizzle
, &vp
->SrcReg
[0].RelAddr
))
3209 vp
->Opcode
= VP_OPCODE_POW
;
3212 if (parse_masked_dst_reg
3213 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->DstReg
.File
,
3214 &vp
->DstReg
.Index
, vp
->DstReg
.WriteMask
))
3216 for (a
= 0; a
< 2; a
++) {
3217 if (parse_scalar_src_reg
3218 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[a
].File
,
3219 &vp
->SrcReg
[a
].Index
, &vp
->SrcReg
[a
].Negate
,
3220 vp
->SrcReg
[a
].Swizzle
, &vp
->SrcReg
[a
].RelAddr
))
3228 vp
->Opcode
= VP_OPCODE_ADD
;
3231 vp
->Opcode
= VP_OPCODE_DP3
;
3234 vp
->Opcode
= VP_OPCODE_DP4
;
3237 vp
->Opcode
= VP_OPCODE_DPH
;
3240 vp
->Opcode
= VP_OPCODE_DST
;
3243 vp
->Opcode
= VP_OPCODE_MAX
;
3246 vp
->Opcode
= VP_OPCODE_MIN
;
3249 vp
->Opcode
= VP_OPCODE_MUL
;
3252 vp
->Opcode
= VP_OPCODE_SGE
;
3255 vp
->Opcode
= VP_OPCODE_SLT
;
3258 vp
->Opcode
= VP_OPCODE_SUB
;
3261 vp
->Opcode
= VP_OPCODE_XPD
;
3264 if (parse_masked_dst_reg
3265 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->DstReg
.File
,
3266 &vp
->DstReg
.Index
, vp
->DstReg
.WriteMask
))
3268 for (a
= 0; a
< 2; a
++) {
3269 if (parse_vector_src_reg
3270 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[a
].File
,
3271 &vp
->SrcReg
[a
].Index
, &vp
->SrcReg
[a
].Negate
,
3272 vp
->SrcReg
[a
].Swizzle
, &vp
->SrcReg
[a
].RelAddr
))
3280 vp
->Opcode
= VP_OPCODE_MAD
;
3284 if (parse_masked_dst_reg
3285 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->DstReg
.File
,
3286 &vp
->DstReg
.Index
, vp
->DstReg
.WriteMask
))
3288 for (a
= 0; a
< 3; a
++) {
3289 if (parse_vector_src_reg
3290 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[a
].File
,
3291 &vp
->SrcReg
[a
].Index
, &vp
->SrcReg
[a
].Negate
,
3292 vp
->SrcReg
[a
].Swizzle
, &vp
->SrcReg
[a
].RelAddr
))
3300 vp
->Opcode
= VP_OPCODE_SWZ
;
3303 if (parse_masked_dst_reg
3304 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->DstReg
.File
,
3305 &vp
->DstReg
.Index
, vp
->DstReg
.WriteMask
))
3309 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[0].File
,
3310 &vp
->SrcReg
[0].Index
, &vp
->SrcReg
[0].RelAddr
))
3312 parse_extended_swizzle_mask (inst
, vp
->SrcReg
[0].Swizzle
,
3313 &vp
->SrcReg
[0].Negate
);
3322 print_state_token (GLint token
)
3325 case STATE_MATERIAL
:
3326 fprintf (stderr
, "STATE_MATERIAL ");
3329 fprintf (stderr
, "STATE_LIGHT ");
3332 case STATE_LIGHTMODEL_AMBIENT
:
3333 fprintf (stderr
, "STATE_AMBIENT ");
3336 case STATE_LIGHTMODEL_SCENECOLOR
:
3337 fprintf (stderr
, "STATE_SCENECOLOR ");
3340 case STATE_LIGHTPROD
:
3341 fprintf (stderr
, "STATE_LIGHTPROD ");
3345 fprintf (stderr
, "STATE_TEXGEN ");
3348 case STATE_FOG_COLOR
:
3349 fprintf (stderr
, "STATE_FOG_COLOR ");
3352 case STATE_FOG_PARAMS
:
3353 fprintf (stderr
, "STATE_FOG_PARAMS ");
3356 case STATE_CLIPPLANE
:
3357 fprintf (stderr
, "STATE_CLIPPLANE ");
3360 case STATE_POINT_SIZE
:
3361 fprintf (stderr
, "STATE_POINT_SIZE ");
3364 case STATE_POINT_ATTENUATION
:
3365 fprintf (stderr
, "STATE_ATTENUATION ");
3369 fprintf (stderr
, "STATE_MATRIX ");
3372 case STATE_MODELVIEW
:
3373 fprintf (stderr
, "STATE_MODELVIEW ");
3376 case STATE_PROJECTION
:
3377 fprintf (stderr
, "STATE_PROJECTION ");
3381 fprintf (stderr
, "STATE_MVP ");
3385 fprintf (stderr
, "STATE_TEXTURE ");
3389 fprintf (stderr
, "STATE_PROGRAM ");
3392 case STATE_MATRIX_INVERSE
:
3393 fprintf (stderr
, "STATE_INVERSE ");
3396 case STATE_MATRIX_TRANSPOSE
:
3397 fprintf (stderr
, "STATE_TRANSPOSE ");
3400 case STATE_MATRIX_INVTRANS
:
3401 fprintf (stderr
, "STATE_INVTRANS ");
3405 fprintf (stderr
, "STATE_AMBIENT ");
3409 fprintf (stderr
, "STATE_DIFFUSE ");
3412 case STATE_SPECULAR
:
3413 fprintf (stderr
, "STATE_SPECULAR ");
3416 case STATE_EMISSION
:
3417 fprintf (stderr
, "STATE_EMISSION ");
3420 case STATE_SHININESS
:
3421 fprintf (stderr
, "STATE_SHININESS ");
3425 fprintf (stderr
, "STATE_HALF ");
3428 case STATE_POSITION
:
3429 fprintf (stderr
, "STATE_POSITION ");
3432 case STATE_ATTENUATION
:
3433 fprintf (stderr
, "STATE_ATTENUATION ");
3436 case STATE_SPOT_DIRECTION
:
3437 fprintf (stderr
, "STATE_DIRECTION ");
3440 case STATE_TEXGEN_EYE_S
:
3441 fprintf (stderr
, "STATE_TEXGEN_EYE_S ");
3444 case STATE_TEXGEN_EYE_T
:
3445 fprintf (stderr
, "STATE_TEXGEN_EYE_T ");
3448 case STATE_TEXGEN_EYE_R
:
3449 fprintf (stderr
, "STATE_TEXGEN_EYE_R ");
3452 case STATE_TEXGEN_EYE_Q
:
3453 fprintf (stderr
, "STATE_TEXGEN_EYE_Q ");
3456 case STATE_TEXGEN_OBJECT_S
:
3457 fprintf (stderr
, "STATE_TEXGEN_EYE_S ");
3460 case STATE_TEXGEN_OBJECT_T
:
3461 fprintf (stderr
, "STATE_TEXGEN_OBJECT_T ");
3464 case STATE_TEXGEN_OBJECT_R
:
3465 fprintf (stderr
, "STATE_TEXGEN_OBJECT_R ");
3468 case STATE_TEXGEN_OBJECT_Q
:
3469 fprintf (stderr
, "STATE_TEXGEN_OBJECT_Q ");
3472 case STATE_TEXENV_COLOR
:
3473 fprintf (stderr
, "STATE_TEXENV_COLOR ");
3476 case STATE_DEPTH_RANGE
:
3477 fprintf (stderr
, "STATE_DEPTH_RANGE ");
3480 case STATE_VERTEX_PROGRAM
:
3481 fprintf (stderr
, "STATE_VERTEX_PROGRAM ");
3484 case STATE_FRAGMENT_PROGRAM
:
3485 fprintf (stderr
, "STATE_FRAGMENT_PROGRAM ");
3489 fprintf (stderr
, "STATE_ENV ");
3493 fprintf (stderr
, "STATE_LOCAL ");
3497 fprintf (stderr
, "[%d] ", token
);
3502 debug_variables (GLcontext
* ctx
, struct var_cache
*vc_head
,
3503 struct arb_program
*Program
)
3505 struct var_cache
*vc
;
3508 fprintf (stderr
, "debug_variables, vc_head: %x\n", vc_head
);
3510 /* First of all, print out the contents of the var_cache */
3513 fprintf (stderr
, "[%x]\n", vc
);
3516 fprintf (stderr
, "UNDEFINED %s\n", vc
->name
);
3519 fprintf (stderr
, "ATTRIB %s\n", vc
->name
);
3520 fprintf (stderr
, " binding: 0x%x\n", vc
->attrib_binding
);
3523 fprintf (stderr
, "PARAM %s begin: %d len: %d\n", vc
->name
,
3524 vc
->param_binding_begin
, vc
->param_binding_length
);
3525 b
= vc
->param_binding_begin
;
3526 for (a
= 0; a
< vc
->param_binding_length
; a
++) {
3527 fprintf (stderr
, "%s\n",
3528 Program
->Parameters
->Parameters
[a
+ b
].Name
);
3529 if (Program
->Parameters
->Parameters
[a
+ b
].Type
== STATE
) {
3530 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3532 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3534 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3536 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3538 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3540 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3544 fprintf (stderr
, "%f %f %f %f\n",
3545 Program
->Parameters
->Parameters
[a
+ b
].Values
[0],
3546 Program
->Parameters
->Parameters
[a
+ b
].Values
[1],
3547 Program
->Parameters
->Parameters
[a
+ b
].Values
[2],
3548 Program
->Parameters
->Parameters
[a
+ b
].Values
[3]);
3552 fprintf (stderr
, "TEMP %s\n", vc
->name
);
3553 fprintf (stderr
, " binding: 0x%x\n", vc
->temp_binding
);
3556 fprintf (stderr
, "OUTPUT %s\n", vc
->name
);
3557 fprintf (stderr
, " binding: 0x%x\n", vc
->output_binding
);
3560 fprintf (stderr
, "ALIAS %s\n", vc
->name
);
3561 fprintf (stderr
, " binding: 0x%x (%s)\n",
3562 vc
->alias_binding
, vc
->alias_binding
->name
);
3573 * The main loop for parsing a fragment or vertex program
3575 * \return 0 on sucess, 1 on error
3578 parse_arb_program (GLcontext
* ctx
, GLubyte
* inst
, struct var_cache
**vc_head
,
3579 struct arb_program
*Program
)
3583 Program
->MajorVersion
= (GLuint
) * inst
++;
3584 Program
->MinorVersion
= (GLuint
) * inst
++;
3586 while (*inst
!= END
) {
3591 case ARB_PRECISION_HINT_FASTEST
:
3592 Program
->PrecisionOption
= GL_FASTEST
;
3595 case ARB_PRECISION_HINT_NICEST
:
3596 Program
->PrecisionOption
= GL_NICEST
;
3600 Program
->FogOption
= GL_EXP
;
3604 Program
->FogOption
= GL_EXP2
;
3607 case ARB_FOG_LINEAR
:
3608 Program
->FogOption
= GL_LINEAR
;
3611 case ARB_POSITION_INVARIANT
:
3612 if (Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
3613 Program
->HintPositionInvariant
= 1;
3616 case ARB_FRAGMENT_PROGRAM_SHADOW
:
3617 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3618 /* TODO ARB_fragment_program_shadow code */
3622 case ARB_DRAW_BUFFERS
:
3623 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3624 /* do nothing for now */
3631 Program
->Position
= parse_position (&inst
);
3633 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3635 /* Check the instruction count
3636 * XXX: Does END count as an instruction?
3638 if (Program
->Base
.NumInstructions
+1 == MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS
) {
3639 _mesa_set_program_error (ctx
, Program
->Position
,
3640 "Max instruction count exceeded!");
3641 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3642 "Max instruction count exceeded!");
3645 /* Realloc Program->FPInstructions */
3646 Program
->FPInstructions
=
3647 (struct fp_instruction
*) _mesa_realloc (Program
->FPInstructions
,
3648 Program
->Base
.NumInstructions
*sizeof(struct fp_instruction
),
3649 (Program
->Base
.NumInstructions
+1)*sizeof (struct fp_instruction
));
3651 /* parse the current instruction */
3652 err
= parse_fp_instruction (ctx
, &inst
, vc_head
, Program
,
3653 &Program
->FPInstructions
[Program
->Base
.NumInstructions
]);
3657 /* Check the instruction count
3658 * XXX: Does END count as an instruction?
3660 if (Program
->Base
.NumInstructions
+1 == MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
) {
3661 _mesa_set_program_error (ctx
, Program
->Position
,
3662 "Max instruction count exceeded!");
3663 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3664 "Max instruction count exceeded!");
3667 /* Realloc Program->VPInstructions */
3668 Program
->VPInstructions
=
3669 (struct vp_instruction
*) _mesa_realloc (Program
->VPInstructions
,
3670 Program
->Base
.NumInstructions
*sizeof(struct vp_instruction
),
3671 (Program
->Base
.NumInstructions
+1)*sizeof(struct vp_instruction
));
3673 /* parse the current instruction */
3674 err
= parse_vp_instruction (ctx
, &inst
, vc_head
, Program
,
3675 &Program
->VPInstructions
[Program
->Base
.NumInstructions
]);
3678 /* increment Program->Base.NumInstructions */
3679 Program
->Base
.NumInstructions
++;
3683 err
= parse_declaration (ctx
, &inst
, vc_head
, Program
);
3694 /* Finally, tag on an OPCODE_END instruction */
3695 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3696 Program
->FPInstructions
=
3697 (struct fp_instruction
*) _mesa_realloc (Program
->FPInstructions
,
3698 Program
->Base
.NumInstructions
*sizeof(struct fp_instruction
),
3699 (Program
->Base
.NumInstructions
+1)*sizeof(struct fp_instruction
));
3701 Program
->FPInstructions
[Program
->Base
.NumInstructions
].Opcode
= FP_OPCODE_END
;
3702 /* YYY Wrong Position in program, whatever, at least not random -> crash
3703 Program->Position = parse_position (&inst);
3705 Program
->FPInstructions
[Program
->Base
.NumInstructions
].StringPos
= Program
->Position
;
3708 Program
->VPInstructions
=
3709 (struct vp_instruction
*) _mesa_realloc (Program
->VPInstructions
,
3710 Program
->Base
.NumInstructions
*sizeof(struct vp_instruction
),
3711 (Program
->Base
.NumInstructions
+1)*sizeof(struct vp_instruction
));
3713 Program
->VPInstructions
[Program
->Base
.NumInstructions
].Opcode
= VP_OPCODE_END
;
3714 /* YYY Wrong Position in program, whatever, at least not random -> crash
3715 Program->Position = parse_position (&inst);
3717 Program
->VPInstructions
[Program
->Base
.NumInstructions
].StringPos
= Program
->Position
;
3720 /* increment Program->Base.NumInstructions */
3721 Program
->Base
.NumInstructions
++;
3727 __extension__
static char core_grammar_text
[] =
3728 #include "grammar_syn.h"
3731 static int set_reg8 (GLcontext
*ctx
, grammar id
, const byte
*name
, byte value
)
3733 char error_msg
[300];
3736 if (grammar_set_reg8 (id
, name
, value
))
3739 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3740 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3741 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Grammar Register Error");
3745 static int extension_is_supported (const GLubyte
*ext
)
3747 const GLubyte
*extensions
= GL_CALL(GetString
)(GL_EXTENSIONS
);
3748 const GLubyte
*end
= extensions
+ _mesa_strlen ((const char *) extensions
);
3749 const GLint ext_len
= _mesa_strlen ((const char *) ext
);
3751 while (extensions
< end
)
3753 const GLubyte
*name_end
= (const GLubyte
*) strchr ((const char *) extensions
, ' ');
3754 if (name_end
== NULL
)
3756 if (name_end
- extensions
== ext_len
&& _mesa_strncmp ((const char *) ext
,
3757 (const char *) extensions
, ext_len
) == 0)
3759 extensions
= name_end
+ 1;
3765 static int enable_ext (GLcontext
*ctx
, grammar id
, const byte
*name
, const byte
*extname
)
3767 if (extension_is_supported (extname
))
3768 if (set_reg8 (ctx
, id
, name
, 0x01))
3774 * This kicks everything off.
3776 * \param ctx - The GL Context
3777 * \param str - The program string
3778 * \param len - The program string length
3779 * \param Program - The arb_program struct to return all the parsed info in
3780 * \return 0 on sucess, 1 on error
3783 _mesa_parse_arb_program (GLcontext
* ctx
, const GLubyte
* str
, GLsizei len
,
3784 struct arb_program
* program
)
3786 GLint a
, err
, error_pos
;
3787 char error_msg
[300];
3789 struct var_cache
*vc_head
;
3790 grammar arbprogram_syn_id
;
3791 GLubyte
*parsed
, *inst
;
3792 GLubyte
*strz
= NULL
;
3793 static int arbprogram_syn_is_ok
= 0; /* XXX temporary */
3795 /* Reset error state */
3796 _mesa_set_program_error(ctx
, -1, NULL
);
3799 fprintf (stderr
, "Loading grammar text!\n");
3802 /* check if the arb_grammar_text (arbprogram.syn) is syntactically correct */
3803 if (!arbprogram_syn_is_ok
) {
3804 grammar grammar_syn_id
;
3809 grammar_syn_id
= grammar_load_from_text ((byte
*) core_grammar_text
);
3810 if (grammar_syn_id
== 0) {
3811 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3812 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3813 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3814 "Error loading grammar rule set");
3818 err
= grammar_check (grammar_syn_id
, (byte
*) arb_grammar_text
, &parsed
, &parsed_len
);
3820 /* NOTE: we cant destroy grammar_syn_id right here because grammar_destroy() can
3821 reset the last error
3825 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3826 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3827 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Error loading grammar rule set");
3829 grammar_destroy (grammar_syn_id
);
3833 grammar_destroy (grammar_syn_id
);
3835 arbprogram_syn_is_ok
= 1;
3838 /* create the grammar object */
3839 arbprogram_syn_id
= grammar_load_from_text ((byte
*) arb_grammar_text
);
3840 if (arbprogram_syn_id
== 0) {
3841 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
3842 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3843 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3844 "Error loading grammer rule set");
3848 /* Set program_target register value */
3849 if (set_reg8 (ctx
, arbprogram_syn_id
, (byte
*) "program_target",
3850 program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
? 0x10 : 0x20)) {
3851 grammar_destroy (arbprogram_syn_id
);
3855 /* Enable all active extensions */
3856 if (enable_ext (ctx
, arbprogram_syn_id
,
3857 (byte
*) "vertex_blend", (byte
*) "GL_ARB_vertex_blend") ||
3858 enable_ext (ctx
, arbprogram_syn_id
,
3859 (byte
*) "vertex_blend", (byte
*) "GL_EXT_vertex_weighting") ||
3860 enable_ext (ctx
, arbprogram_syn_id
,
3861 (byte
*) "matrix_palette", (byte
*) "GL_ARB_matrix_palette") ||
3862 enable_ext (ctx
, arbprogram_syn_id
,
3863 (byte
*) "point_parameters", (byte
*) "GL_ARB_point_parameters") ||
3864 enable_ext (ctx
, arbprogram_syn_id
,
3865 (byte
*) "point_parameters", (byte
*) "GL_EXT_point_parameters") ||
3866 enable_ext (ctx
, arbprogram_syn_id
,
3867 (byte
*) "secondary_color", (byte
*) "GL_EXT_secondary_color") ||
3868 enable_ext (ctx
, arbprogram_syn_id
,
3869 (byte
*) "fog_coord", (byte
*) "GL_EXT_fog_coord") ||
3870 enable_ext (ctx
, arbprogram_syn_id
,
3871 (byte
*) "texture_rectangle", (byte
*) "GL_ARB_texture_rectangle") ||
3872 enable_ext (ctx
, arbprogram_syn_id
,
3873 (byte
*) "texture_rectangle", (byte
*) "GL_EXT_texture_rectangle") ||
3874 enable_ext (ctx
, arbprogram_syn_id
,
3875 (byte
*) "texture_rectangle", (byte
*) "GL_NV_texture_rectangle") ||
3876 enable_ext (ctx
, arbprogram_syn_id
,
3877 (byte
*) "fragment_program_shadow", (byte
*) "GL_ARB_fragment_program_shadow") ||
3878 enable_ext (ctx
, arbprogram_syn_id
,
3879 (byte
*) "draw_buffers", (byte
*) "GL_ARB_draw_buffers")) {
3880 grammar_destroy (arbprogram_syn_id
);
3884 /* check for NULL character occurences */
3887 for (i
= 0; i
< len
; i
++)
3888 if (str
[i
] == '\0') {
3889 _mesa_set_program_error (ctx
, i
, "invalid character");
3890 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Lexical Error");
3892 grammar_destroy (arbprogram_syn_id
);
3897 /* copy the program string to a null-terminated string */
3898 /* XXX should I check for NULL from malloc()? */
3899 strz
= (GLubyte
*) _mesa_malloc (len
+ 1);
3900 _mesa_memcpy (strz
, str
, len
);
3904 printf ("Checking Grammar!\n");
3906 /* do a fast check on program string - initial production buffer is 4K */
3907 err
= grammar_fast_check (arbprogram_syn_id
, strz
, &parsed
, &parsed_len
, 0x1000);
3909 /* Syntax parse error */
3912 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
3913 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3914 _mesa_error (ctx
, GL_INVALID_OPERATION
, "glProgramStringARB(syntax error)");
3916 /* useful for debugging */
3920 printf("Program: %s\n", (char *) strz
);
3921 printf("Error Pos: %d\n", ctx
->Program
.ErrorPos
);
3922 s
= (char *) _mesa_find_line_column(strz
, strz
+ctx
->Program
.ErrorPos
, &line
, &col
);
3923 printf("line %d col %d: %s\n", line
, col
, s
);
3926 grammar_destroy (arbprogram_syn_id
);
3931 printf ("Destroying grammer dict [parse retval: %d]\n", err
);
3933 grammar_destroy (arbprogram_syn_id
);
3935 /* Initialize the arb_program struct */
3936 program
->Base
.String
= strz
;
3937 program
->Base
.NumInstructions
=
3938 program
->Base
.NumTemporaries
=
3939 program
->Base
.NumParameters
=
3940 program
->Base
.NumAttributes
= program
->Base
.NumAddressRegs
= 0;
3941 program
->Parameters
= _mesa_new_parameter_list ();
3942 program
->InputsRead
= 0;
3943 program
->OutputsWritten
= 0;
3944 program
->Position
= 0;
3945 program
->MajorVersion
= program
->MinorVersion
= 0;
3946 program
->PrecisionOption
= GL_DONT_CARE
;
3947 program
->FogOption
= GL_NONE
;
3948 program
->HintPositionInvariant
= GL_FALSE
;
3949 for (a
= 0; a
< MAX_TEXTURE_IMAGE_UNITS
; a
++)
3950 program
->TexturesUsed
[a
] = 0;
3951 program
->NumAluInstructions
=
3952 program
->NumTexInstructions
=
3953 program
->NumTexIndirections
= 0;
3955 program
->FPInstructions
= NULL
;
3956 program
->VPInstructions
= NULL
;
3961 /* Start examining the tokens in the array */
3964 /* Check the grammer rev */
3965 if (*inst
++ != REVISION
) {
3966 _mesa_set_program_error (ctx
, 0, "Grammar version mismatch");
3967 _mesa_error (ctx
, GL_INVALID_OPERATION
, "glProgramStringARB(Grammar verison mismatch)");
3971 /* ignore program target */
3974 err
= parse_arb_program (ctx
, inst
, &vc_head
, program
);
3976 fprintf (stderr
, "Symantic analysis returns %d [1 is bad!]\n", err
);
3980 /*debug_variables(ctx, vc_head, program); */
3982 /* We're done with the parsed binary array */
3983 var_cache_destroy (&vc_head
);
3985 _mesa_free (parsed
);
3987 printf ("_mesa_parse_arb_program() done\n");