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 0x08
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 flags */
162 #define ARB_PRECISION_HINT_FASTEST 0x01
163 #define ARB_PRECISION_HINT_NICEST 0x02
164 #define ARB_FOG_EXP 0x04
165 #define ARB_FOG_EXP2 0x08
166 #define ARB_FOG_LINEAR 0x10
168 /* GL_ARB_vertex_program option flags */
169 #define ARB_POSITION_INVARIANT 0x20
171 /* GL_ARB_fragment_program_shadow option flags */
172 #define ARB_FRAGMENT_PROGRAM_SHADOW 0x40
174 /* GL_ARB_draw_buffers option flags */
175 #define ARB_DRAW_BUFFERS 0x80
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
);
781 if ((*offset
> 63) || (*offset
< -64)) {
782 _mesa_set_program_error (ctx
, Program
->Position
,
783 "Relative offset out of range");
784 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Relative offset %d out of range",
793 * \param color 0 if color type is primary, 1 if color type is secondary
794 * \return 0 on sucess, 1 on error
797 parse_color_type (GLcontext
* ctx
, GLubyte
** inst
, struct arb_program
*Program
,
800 (void) ctx
; (void) Program
;
801 *color
= *(*inst
)++ != COLOR_PRIMARY
;
806 * Get an integer corresponding to a generic vertex attribute.
808 * \return 0 on sucess, 1 on error
811 parse_generic_attrib_num(GLcontext
*ctx
, GLubyte
** inst
,
812 struct arb_program
*Program
, GLuint
*attrib
)
814 GLint i
= parse_integer(inst
, Program
);
816 if ((i
< 0) || (i
> MAX_VERTEX_PROGRAM_ATTRIBS
))
818 _mesa_set_program_error (ctx
, Program
->Position
,
819 "Invalid generic vertex attribute index");
820 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid generic vertex attribute index");
825 *attrib
= (GLuint
) i
;
832 * \param color The index of the color buffer to write into
833 * \return 0 on sucess, 1 on error
836 parse_output_color_num (GLcontext
* ctx
, GLubyte
** inst
,
837 struct arb_program
*Program
, GLuint
* color
)
839 GLint i
= parse_integer (inst
, Program
);
841 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxDrawBuffers
)) {
842 _mesa_set_program_error (ctx
, Program
->Position
,
843 "Invalid draw buffer index");
844 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid draw buffer index");
854 * \param coord The texture unit index
855 * \return 0 on sucess, 1 on error
858 parse_texcoord_num (GLcontext
* ctx
, GLubyte
** inst
,
859 struct arb_program
*Program
, GLuint
* coord
)
861 GLint i
= parse_integer (inst
, Program
);
863 if ((i
< 0) || (i
>= (int)ctx
->Const
.MaxTextureUnits
)) {
864 _mesa_set_program_error (ctx
, Program
->Position
,
865 "Invalid texture unit index");
866 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid texture unit index");
875 * \param coord The weight index
876 * \return 0 on sucess, 1 on error
879 parse_weight_num (GLcontext
* ctx
, GLubyte
** inst
, struct arb_program
*Program
,
882 *coord
= parse_integer (inst
, Program
);
884 if ((*coord
< 0) || (*coord
>= 1)) {
885 _mesa_set_program_error (ctx
, Program
->Position
,
886 "Invalid weight index");
887 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid weight index");
895 * \param coord The clip plane index
896 * \return 0 on sucess, 1 on error
899 parse_clipplane_num (GLcontext
* ctx
, GLubyte
** inst
,
900 struct arb_program
*Program
, GLint
* coord
)
902 *coord
= parse_integer (inst
, Program
);
904 if ((*coord
< 0) || (*coord
>= (GLint
) ctx
->Const
.MaxClipPlanes
)) {
905 _mesa_set_program_error (ctx
, Program
->Position
,
906 "Invalid clip plane index");
907 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid clip plane index");
916 * \return 0 on front face, 1 on back face
919 parse_face_type (GLubyte
** inst
)
921 switch (*(*inst
)++) {
933 * Given a matrix and a modifier token on the binary array, return tokens
934 * that _mesa_fetch_state() [program.c] can understand.
936 * \param matrix - the matrix we are talking about
937 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
938 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
939 * \return 0 on sucess, 1 on failure
942 parse_matrix (GLcontext
* ctx
, GLubyte
** inst
, struct arb_program
*Program
,
943 GLint
* matrix
, GLint
* matrix_idx
, GLint
* matrix_modifier
)
945 GLubyte mat
= *(*inst
)++;
950 case MATRIX_MODELVIEW
:
951 *matrix
= STATE_MODELVIEW
;
952 *matrix_idx
= parse_integer (inst
, Program
);
953 if (*matrix_idx
> 0) {
954 _mesa_set_program_error (ctx
, Program
->Position
,
955 "ARB_vertex_blend not supported\n");
956 _mesa_error (ctx
, GL_INVALID_OPERATION
,
957 "ARB_vertex_blend not supported\n");
962 case MATRIX_PROJECTION
:
963 *matrix
= STATE_PROJECTION
;
971 *matrix
= STATE_TEXTURE
;
972 *matrix_idx
= parse_integer (inst
, Program
);
973 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxTextureUnits
) {
974 _mesa_set_program_error (ctx
, Program
->Position
,
975 "Invalid Texture Unit");
976 _mesa_error (ctx
, GL_INVALID_OPERATION
,
977 "Invalid Texture Unit: %d", *matrix_idx
);
982 /* This is not currently supported (ARB_matrix_palette) */
984 *matrix_idx
= parse_integer (inst
, Program
);
985 _mesa_set_program_error (ctx
, Program
->Position
,
986 "ARB_matrix_palette not supported\n");
987 _mesa_error (ctx
, GL_INVALID_OPERATION
,
988 "ARB_matrix_palette not supported\n");
993 *matrix
= STATE_PROGRAM
;
994 *matrix_idx
= parse_integer (inst
, Program
);
995 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxProgramMatrices
) {
996 _mesa_set_program_error (ctx
, Program
->Position
,
997 "Invalid Program Matrix");
998 _mesa_error (ctx
, GL_INVALID_OPERATION
,
999 "Invalid Program Matrix: %d", *matrix_idx
);
1005 switch (*(*inst
)++) {
1006 case MATRIX_MODIFIER_IDENTITY
:
1007 *matrix_modifier
= 0;
1009 case MATRIX_MODIFIER_INVERSE
:
1010 *matrix_modifier
= STATE_MATRIX_INVERSE
;
1012 case MATRIX_MODIFIER_TRANSPOSE
:
1013 *matrix_modifier
= STATE_MATRIX_TRANSPOSE
;
1015 case MATRIX_MODIFIER_INVTRANS
:
1016 *matrix_modifier
= STATE_MATRIX_INVTRANS
;
1025 * This parses a state string (rather, the binary version of it) into
1026 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
1028 * \param inst - the start in the binary arry to start working from
1029 * \param state_tokens - the storage for the 6-token state description
1030 * \return - 0 on sucess, 1 on error
1033 parse_state_single_item (GLcontext
* ctx
, GLubyte
** inst
,
1034 struct arb_program
*Program
, GLint
* state_tokens
)
1036 switch (*(*inst
)++) {
1037 case STATE_MATERIAL_PARSER
:
1038 state_tokens
[0] = STATE_MATERIAL
;
1039 state_tokens
[1] = parse_face_type (inst
);
1040 switch (*(*inst
)++) {
1041 case MATERIAL_AMBIENT
:
1042 state_tokens
[2] = STATE_AMBIENT
;
1044 case MATERIAL_DIFFUSE
:
1045 state_tokens
[2] = STATE_DIFFUSE
;
1047 case MATERIAL_SPECULAR
:
1048 state_tokens
[2] = STATE_SPECULAR
;
1050 case MATERIAL_EMISSION
:
1051 state_tokens
[2] = STATE_EMISSION
;
1053 case MATERIAL_SHININESS
:
1054 state_tokens
[2] = STATE_SHININESS
;
1059 case STATE_LIGHT_PARSER
:
1060 state_tokens
[0] = STATE_LIGHT
;
1061 state_tokens
[1] = parse_integer (inst
, Program
);
1063 /* Check the value of state_tokens[1] against the # of lights */
1064 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1065 _mesa_set_program_error (ctx
, Program
->Position
,
1066 "Invalid Light Number");
1067 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1068 "Invalid Light Number: %d", state_tokens
[1]);
1072 switch (*(*inst
)++) {
1074 state_tokens
[2] = STATE_AMBIENT
;
1077 state_tokens
[2] = STATE_DIFFUSE
;
1079 case LIGHT_SPECULAR
:
1080 state_tokens
[2] = STATE_SPECULAR
;
1082 case LIGHT_POSITION
:
1083 state_tokens
[2] = STATE_POSITION
;
1085 case LIGHT_ATTENUATION
:
1086 state_tokens
[2] = STATE_ATTENUATION
;
1089 state_tokens
[2] = STATE_HALF
;
1091 case LIGHT_SPOT_DIRECTION
:
1092 state_tokens
[2] = STATE_SPOT_DIRECTION
;
1097 case STATE_LIGHT_MODEL
:
1098 switch (*(*inst
)++) {
1099 case LIGHT_MODEL_AMBIENT
:
1100 state_tokens
[0] = STATE_LIGHTMODEL_AMBIENT
;
1102 case LIGHT_MODEL_SCENECOLOR
:
1103 state_tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
1104 state_tokens
[1] = parse_face_type (inst
);
1109 case STATE_LIGHT_PROD
:
1110 state_tokens
[0] = STATE_LIGHTPROD
;
1111 state_tokens
[1] = parse_integer (inst
, Program
);
1113 /* Check the value of state_tokens[1] against the # of lights */
1114 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
1115 _mesa_set_program_error (ctx
, Program
->Position
,
1116 "Invalid Light Number");
1117 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1118 "Invalid Light Number: %d", state_tokens
[1]);
1122 state_tokens
[2] = parse_face_type (inst
);
1123 switch (*(*inst
)++) {
1124 case LIGHT_PROD_AMBIENT
:
1125 state_tokens
[3] = STATE_AMBIENT
;
1127 case LIGHT_PROD_DIFFUSE
:
1128 state_tokens
[3] = STATE_DIFFUSE
;
1130 case LIGHT_PROD_SPECULAR
:
1131 state_tokens
[3] = STATE_SPECULAR
;
1138 switch (*(*inst
)++) {
1140 state_tokens
[0] = STATE_FOG_COLOR
;
1143 state_tokens
[0] = STATE_FOG_PARAMS
;
1149 state_tokens
[1] = parse_integer (inst
, Program
);
1150 switch (*(*inst
)++) {
1152 state_tokens
[0] = STATE_TEXENV_COLOR
;
1161 state_tokens
[0] = STATE_TEXGEN
;
1162 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
1164 if (parse_texcoord_num (ctx
, inst
, Program
, &coord
))
1166 state_tokens
[1] = coord
;
1171 /* 0 - s, 1 - t, 2 - r, 3 - q */
1174 if (type
== TEX_GEN_EYE
) {
1177 state_tokens
[2] = STATE_TEXGEN_EYE_S
;
1180 state_tokens
[2] = STATE_TEXGEN_EYE_T
;
1183 state_tokens
[2] = STATE_TEXGEN_EYE_R
;
1186 state_tokens
[2] = STATE_TEXGEN_EYE_Q
;
1193 state_tokens
[2] = STATE_TEXGEN_OBJECT_S
;
1196 state_tokens
[2] = STATE_TEXGEN_OBJECT_T
;
1199 state_tokens
[2] = STATE_TEXGEN_OBJECT_R
;
1202 state_tokens
[2] = STATE_TEXGEN_OBJECT_Q
;
1210 switch (*(*inst
)++) {
1212 state_tokens
[0] = STATE_DEPTH_RANGE
;
1217 case STATE_CLIP_PLANE
:
1218 state_tokens
[0] = STATE_CLIPPLANE
;
1219 state_tokens
[1] = parse_integer (inst
, Program
);
1220 if (parse_clipplane_num (ctx
, inst
, Program
, &state_tokens
[1]))
1225 switch (*(*inst
++)) {
1227 state_tokens
[0] = STATE_POINT_SIZE
;
1230 case POINT_ATTENUATION
:
1231 state_tokens
[0] = STATE_POINT_ATTENUATION
;
1236 /* XXX: I think this is the correct format for a matrix row */
1237 case STATE_MATRIX_ROWS
:
1238 state_tokens
[0] = STATE_MATRIX
;
1240 (ctx
, inst
, Program
, &state_tokens
[1], &state_tokens
[2],
1244 state_tokens
[3] = parse_integer (inst
, Program
); /* The first row to grab */
1246 if ((**inst
) != 0) { /* Either the last row, 0 */
1247 state_tokens
[4] = parse_integer (inst
, Program
);
1248 if (state_tokens
[4] < state_tokens
[3]) {
1249 _mesa_set_program_error (ctx
, Program
->Position
,
1250 "Second matrix index less than the first");
1251 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1252 "Second matrix index (%d) less than the first (%d)",
1253 state_tokens
[4], state_tokens
[3]);
1258 state_tokens
[4] = state_tokens
[3];
1268 * This parses a state string (rather, the binary version of it) into
1269 * a 6-token similar for the state fetching code in program.c
1271 * One might ask, why fetch these parameters into just like you fetch
1272 * state when they are already stored in other places?
1274 * Because of array offsets -> We can stick env/local parameters in the
1275 * middle of a parameter array and then index someplace into the array
1278 * One optimization might be to only do this for the cases where the
1279 * env/local parameters end up inside of an array, and leave the
1280 * single parameters (or arrays of pure env/local pareameters) in their
1281 * respective register files.
1283 * For ENV parameters, the format is:
1284 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1285 * state_tokens[1] = STATE_ENV
1286 * state_tokens[2] = the parameter index
1288 * for LOCAL parameters, the format is:
1289 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
1290 * state_tokens[1] = STATE_LOCAL
1291 * state_tokens[2] = the parameter index
1293 * \param inst - the start in the binary arry to start working from
1294 * \param state_tokens - the storage for the 6-token state description
1295 * \return - 0 on sucess, 1 on failure
1298 parse_program_single_item (GLcontext
* ctx
, GLubyte
** inst
,
1299 struct arb_program
*Program
, GLint
* state_tokens
)
1301 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1302 state_tokens
[0] = STATE_FRAGMENT_PROGRAM
;
1304 state_tokens
[0] = STATE_VERTEX_PROGRAM
;
1307 switch (*(*inst
)++) {
1308 case PROGRAM_PARAM_ENV
:
1309 state_tokens
[1] = STATE_ENV
;
1310 state_tokens
[2] = parse_integer (inst
, Program
);
1312 /* Check state_tokens[2] against the number of ENV parameters available */
1313 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1314 (state_tokens
[2] >= (GLint
) ctx
->Const
.MaxFragmentProgramEnvParams
))
1316 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1317 (state_tokens
[2] >= (GLint
) ctx
->Const
.MaxVertexProgramEnvParams
))) {
1318 _mesa_set_program_error (ctx
, Program
->Position
,
1319 "Invalid Program Env Parameter");
1320 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1321 "Invalid Program Env Parameter: %d",
1328 case PROGRAM_PARAM_LOCAL
:
1329 state_tokens
[1] = STATE_LOCAL
;
1330 state_tokens
[2] = parse_integer (inst
, Program
);
1332 /* Check state_tokens[2] against the number of LOCAL parameters available */
1333 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
1334 (state_tokens
[2] >= (GLint
) ctx
->Const
.MaxFragmentProgramLocalParams
))
1336 ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1337 (state_tokens
[2] >= (GLint
) ctx
->Const
.MaxVertexProgramLocalParams
))) {
1338 _mesa_set_program_error (ctx
, Program
->Position
,
1339 "Invalid Program Local Parameter");
1340 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1341 "Invalid Program Local Parameter: %d",
1352 * For ARB_vertex_program, programs are not allowed to use both an explicit
1353 * vertex attribute and a generic vertex attribute corresponding to the same
1354 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
1356 * This will walk our var_cache and make sure that nobody does anything fishy.
1358 * \return 0 on sucess, 1 on error
1361 generic_attrib_check(struct var_cache
*vc_head
)
1364 struct var_cache
*curr
;
1365 GLboolean explicitAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
],
1366 genericAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
];
1368 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1369 explicitAttrib
[a
] = GL_FALSE
;
1370 genericAttrib
[a
] = GL_FALSE
;
1375 if (curr
->type
== vt_attrib
) {
1376 if (curr
->attrib_is_generic
)
1377 genericAttrib
[ curr
->attrib_binding_idx
] = GL_TRUE
;
1379 explicitAttrib
[ curr
->attrib_binding_idx
] = GL_TRUE
;
1385 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
1386 if ((explicitAttrib
[a
]) && (genericAttrib
[a
]))
1394 * This will handle the binding side of an ATTRIB var declaration
1396 * \param binding - the fragment input register state, defined in nvfragprog.h
1397 * \param binding_idx - the index in the attrib register file that binding is associated with
1398 * \return returns 0 on sucess, 1 on error
1400 * See nvfragparse.c for attrib register file layout
1403 parse_attrib_binding (GLcontext
* ctx
, GLubyte
** inst
,
1404 struct arb_program
*Program
, GLuint
* binding
,
1405 GLuint
* binding_idx
, GLuint
*is_generic
)
1412 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1413 switch (*(*inst
)++) {
1414 case FRAGMENT_ATTRIB_COLOR
:
1415 err
= parse_color_type (ctx
, inst
, Program
, &coord
);
1416 *binding
= FRAG_ATTRIB_COL0
+ coord
;
1417 *binding_idx
= 1 + coord
;
1420 case FRAGMENT_ATTRIB_TEXCOORD
:
1421 err
= parse_texcoord_num (ctx
, inst
, Program
, &texcoord
);
1422 *binding
= FRAG_ATTRIB_TEX0
+ texcoord
;
1423 *binding_idx
= 4 + texcoord
;
1426 case FRAGMENT_ATTRIB_FOGCOORD
:
1427 *binding
= FRAG_ATTRIB_FOGC
;
1431 case FRAGMENT_ATTRIB_POSITION
:
1432 *binding
= FRAG_ATTRIB_WPOS
;
1442 switch (*(*inst
)++) {
1443 case VERTEX_ATTRIB_POSITION
:
1444 *binding
= VERT_ATTRIB_POS
;
1448 case VERTEX_ATTRIB_WEIGHT
:
1452 err
= parse_weight_num (ctx
, inst
, Program
, &weight
);
1453 *binding
= VERT_ATTRIB_WEIGHT
;
1456 _mesa_set_program_error (ctx
, Program
->Position
,
1457 "ARB_vertex_blend not supported\n");
1458 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1459 "ARB_vertex_blend not supported\n");
1463 case VERTEX_ATTRIB_NORMAL
:
1464 *binding
= VERT_ATTRIB_NORMAL
;
1468 case VERTEX_ATTRIB_COLOR
:
1472 err
= parse_color_type (ctx
, inst
, Program
, &color
);
1474 *binding
= VERT_ATTRIB_COLOR1
;
1478 *binding
= VERT_ATTRIB_COLOR0
;
1484 case VERTEX_ATTRIB_FOGCOORD
:
1485 *binding
= VERT_ATTRIB_FOG
;
1489 case VERTEX_ATTRIB_TEXCOORD
:
1493 err
= parse_texcoord_num (ctx
, inst
, Program
, &unit
);
1494 *binding
= VERT_ATTRIB_TEX0
+ unit
;
1495 *binding_idx
= 8 + unit
;
1499 /* It looks like we don't support this at all, atm */
1500 case VERTEX_ATTRIB_MATRIXINDEX
:
1501 parse_integer (inst
, Program
);
1502 _mesa_set_program_error (ctx
, Program
->Position
,
1503 "ARB_palette_matrix not supported");
1504 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1505 "ARB_palette_matrix not supported");
1509 case VERTEX_ATTRIB_GENERIC
:
1513 if (!parse_generic_attrib_num(ctx
, inst
, Program
, &attrib
)) {
1517 *binding
= VERT_ATTRIB_POS
;
1520 *binding
= VERT_ATTRIB_WEIGHT
;
1523 *binding
= VERT_ATTRIB_NORMAL
;
1526 *binding
= VERT_ATTRIB_COLOR0
;
1529 *binding
= VERT_ATTRIB_COLOR1
;
1532 *binding
= VERT_ATTRIB_FOG
;
1539 *binding
= VERT_ATTRIB_TEX0
+ (attrib
-8);
1542 *binding_idx
= attrib
;
1553 /* Can this even happen? */
1555 _mesa_set_program_error (ctx
, Program
->Position
,
1556 "Bad attribute binding");
1557 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Bad attribute binding");
1560 Program
->InputsRead
|= (1 << *binding_idx
);
1566 * This translates between a binary token for an output variable type
1567 * and the mesa token for the same thing.
1570 * XXX: What is the 'name' for vertex program state? -> do we need it?
1571 * I don't think we do;
1573 * See nvfragprog.h for definitions
1575 * \param inst - The parsed tokens
1576 * \param binding - The name of the state we are binding too
1577 * \param binding_idx - The index into the result register file that this is bound too
1579 * See nvfragparse.c for the register file layout for fragment programs
1580 * See nvvertparse.c for the register file layout for vertex programs
1583 parse_result_binding (GLcontext
* ctx
, GLubyte
** inst
, GLuint
* binding
,
1584 GLuint
* binding_idx
, struct arb_program
*Program
)
1586 GLuint b
, out_color
;
1588 switch (*(*inst
)++) {
1589 case FRAGMENT_RESULT_COLOR
:
1590 /* for frag programs, this is FRAGMENT_RESULT_COLOR */
1591 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1592 /* This gets result of the color buffer we're supposed to
1595 parse_output_color_num(ctx
, inst
, Program
, &out_color
);
1597 *binding
= FRAG_OUTPUT_COLR
;
1599 /* XXX: We're ignoring the color buffer for now. */
1602 /* for vtx programs, this is VERTEX_RESULT_POSITION */
1608 case FRAGMENT_RESULT_DEPTH
:
1609 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
1610 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1611 *binding
= FRAG_OUTPUT_DEPR
;
1614 /* for vtx programs, this is VERTEX_RESULT_COLOR */
1617 GLuint face_type
= parse_face_type(inst
);
1618 GLint color_type_ret
= parse_color_type(ctx
, inst
, Program
, &color_type
);
1622 if (color_type_ret
) return 1;
1624 /* secondary color */
1635 /* secondary color */
1647 case VERTEX_RESULT_FOGCOORD
:
1651 case VERTEX_RESULT_POINTSIZE
:
1655 case VERTEX_RESULT_TEXCOORD
:
1656 if (parse_texcoord_num (ctx
, inst
, Program
, &b
))
1658 *binding_idx
= 7 + b
;
1662 Program
->OutputsWritten
|= (1 << *binding_idx
);
1668 * This handles the declaration of ATTRIB variables
1671 * parse_vert_attrib_binding(), or something like that
1673 * \return 0 on sucess, 1 on error
1676 parse_attrib (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
1677 struct arb_program
*Program
)
1681 struct var_cache
*attrib_var
;
1683 attrib_var
= parse_string (inst
, vc_head
, Program
, &found
);
1684 Program
->Position
= parse_position (inst
);
1686 error_msg
= (char *)
1687 _mesa_malloc (_mesa_strlen ((char *) attrib_var
->name
) + 40);
1688 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
1691 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
1692 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
1694 _mesa_free (error_msg
);
1698 attrib_var
->type
= vt_attrib
;
1700 /* I think this is ok now - karl */
1702 /*if (Program->type == GL_FRAGMENT_PROGRAM_ARB) */
1704 if (parse_attrib_binding
1705 (ctx
, inst
, Program
, &attrib_var
->attrib_binding
,
1706 &attrib_var
->attrib_binding_idx
, &attrib_var
->attrib_is_generic
))
1708 if (generic_attrib_check(*vc_head
)) {
1709 _mesa_set_program_error (ctx
, Program
->Position
,
1710 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
1711 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1712 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
1718 Program
->Base
.NumAttributes
++;
1723 * \param use -- TRUE if we're called when declaring implicit parameters,
1724 * FALSE if we're declaraing variables. This has to do with
1725 * if we get a signed or unsigned float for scalar constants
1728 parse_param_elements (GLcontext
* ctx
, GLubyte
** inst
,
1729 struct var_cache
*param_var
,
1730 struct arb_program
*Program
, GLboolean use
)
1734 GLint state_tokens
[6];
1735 GLfloat const_values
[4];
1739 switch (*(*inst
)++) {
1740 case PARAM_STATE_ELEMENT
:
1742 if (parse_state_single_item (ctx
, inst
, Program
, state_tokens
))
1745 /* If we adding STATE_MATRIX that has multiple rows, we need to
1746 * unroll it and call _mesa_add_state_reference() for each row
1748 if ((state_tokens
[0] == STATE_MATRIX
)
1749 && (state_tokens
[3] != state_tokens
[4])) {
1751 GLint first_row
= state_tokens
[3];
1752 GLint last_row
= state_tokens
[4];
1754 for (row
= first_row
; row
<= last_row
; row
++) {
1755 state_tokens
[3] = state_tokens
[4] = row
;
1758 _mesa_add_state_reference (Program
->Parameters
,
1760 if (param_var
->param_binding_begin
== ~0U)
1761 param_var
->param_binding_begin
= idx
;
1762 param_var
->param_binding_length
++;
1763 Program
->Base
.NumParameters
++;
1768 _mesa_add_state_reference (Program
->Parameters
, state_tokens
);
1769 if (param_var
->param_binding_begin
== ~0U)
1770 param_var
->param_binding_begin
= idx
;
1771 param_var
->param_binding_length
++;
1772 Program
->Base
.NumParameters
++;
1776 case PARAM_PROGRAM_ELEMENT
:
1778 if (parse_program_single_item (ctx
, inst
, Program
, state_tokens
))
1780 idx
= _mesa_add_state_reference (Program
->Parameters
, state_tokens
);
1781 if (param_var
->param_binding_begin
== ~0U)
1782 param_var
->param_binding_begin
= idx
;
1783 param_var
->param_binding_length
++;
1784 Program
->Base
.NumParameters
++;
1786 /* Check if there is more: 0 -> we're done, else its an integer */
1788 GLuint out_of_range
, new_idx
;
1789 GLuint start_idx
= state_tokens
[2] + 1;
1790 GLuint end_idx
= parse_integer (inst
, Program
);
1793 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
1794 if (((state_tokens
[1] == STATE_ENV
)
1795 && (end_idx
>= ctx
->Const
.MaxFragmentProgramEnvParams
))
1796 || ((state_tokens
[1] == STATE_LOCAL
)
1798 ctx
->Const
.MaxFragmentProgramLocalParams
)))
1802 if (((state_tokens
[1] == STATE_ENV
)
1803 && (end_idx
>= ctx
->Const
.MaxVertexProgramEnvParams
))
1804 || ((state_tokens
[1] == STATE_LOCAL
)
1806 ctx
->Const
.MaxVertexProgramLocalParams
)))
1810 _mesa_set_program_error (ctx
, Program
->Position
,
1811 "Invalid Program Parameter");
1812 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1813 "Invalid Program Parameter: %d", end_idx
);
1817 for (new_idx
= start_idx
; new_idx
<= end_idx
; new_idx
++) {
1818 state_tokens
[2] = new_idx
;
1820 _mesa_add_state_reference (Program
->Parameters
,
1822 param_var
->param_binding_length
++;
1823 Program
->Base
.NumParameters
++;
1832 case PARAM_CONSTANT
:
1833 parse_constant (inst
, const_values
, Program
, use
);
1835 _mesa_add_named_constant (Program
->Parameters
,
1836 (char *) param_var
->name
, const_values
);
1837 if (param_var
->param_binding_begin
== ~0U)
1838 param_var
->param_binding_begin
= idx
;
1839 param_var
->param_binding_length
++;
1840 Program
->Base
.NumParameters
++;
1844 _mesa_set_program_error (ctx
, Program
->Position
,
1845 "Unexpected token in parse_param_elements()");
1846 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1847 "Unexpected token in parse_param_elements()");
1851 /* Make sure we haven't blown past our parameter limits */
1852 if (((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
) &&
1853 (Program
->Base
.NumParameters
>=
1854 ctx
->Const
.MaxVertexProgramLocalParams
))
1855 || ((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
)
1856 && (Program
->Base
.NumParameters
>=
1857 ctx
->Const
.MaxFragmentProgramLocalParams
))) {
1858 _mesa_set_program_error (ctx
, Program
->Position
,
1859 "Too many parameter variables");
1860 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Too many parameter variables");
1868 * This picks out PARAM program parameter bindings.
1870 * XXX: This needs to be stressed & tested
1872 * \return 0 on sucess, 1 on error
1875 parse_param (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
1876 struct arb_program
*Program
)
1879 GLint specified_length
;
1881 struct var_cache
*param_var
;
1884 param_var
= parse_string (inst
, vc_head
, Program
, &found
);
1885 Program
->Position
= parse_position (inst
);
1888 error_msg
= (char *) _mesa_malloc (_mesa_strlen ((char *) param_var
->name
) + 40);
1889 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
1892 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
1893 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
1895 _mesa_free (error_msg
);
1899 specified_length
= parse_integer (inst
, Program
);
1901 if (specified_length
< 0) {
1902 _mesa_set_program_error (ctx
, Program
->Position
,
1903 "Negative parameter array length");
1904 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1905 "Negative parameter array length: %d", specified_length
);
1909 param_var
->type
= vt_param
;
1910 param_var
->param_binding_length
= 0;
1912 /* Right now, everything is shoved into the main state register file.
1914 * In the future, it would be nice to leave things ENV/LOCAL params
1915 * in their respective register files, if possible
1917 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1920 * * - add each guy to the parameter list
1921 * * - increment the param_var->param_binding_len
1922 * * - store the param_var->param_binding_begin for the first one
1923 * * - compare the actual len to the specified len at the end
1925 while (**inst
!= PARAM_NULL
) {
1926 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_FALSE
))
1930 /* Test array length here! */
1931 if (specified_length
) {
1932 if (specified_length
!= (int)param_var
->param_binding_length
) {
1933 _mesa_set_program_error (ctx
, Program
->Position
,
1934 "Declared parameter array lenght does not match parameter list");
1935 _mesa_error (ctx
, GL_INVALID_OPERATION
,
1936 "Declared parameter array lenght does not match parameter list");
1949 parse_param_use (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
1950 struct arb_program
*Program
, struct var_cache
**new_var
)
1952 struct var_cache
*param_var
;
1954 /* First, insert a dummy entry into the var_cache */
1955 var_cache_create (¶m_var
);
1956 param_var
->name
= (GLubyte
*) _mesa_strdup (" ");
1957 param_var
->type
= vt_param
;
1959 param_var
->param_binding_length
= 0;
1960 /* Don't fill in binding_begin; We use the default value of -1
1961 * to tell if its already initialized, elsewhere.
1963 * param_var->param_binding_begin = 0;
1965 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
1967 var_cache_append (vc_head
, param_var
);
1969 /* Then fill it with juicy parameter goodness */
1970 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_TRUE
))
1973 *new_var
= param_var
;
1980 * This handles the declaration of TEMP variables
1982 * \return 0 on sucess, 1 on error
1985 parse_temp (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
1986 struct arb_program
*Program
)
1989 struct var_cache
*temp_var
;
1992 while (**inst
!= 0) {
1993 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
1994 Program
->Position
= parse_position (inst
);
1996 error_msg
= (char *)
1997 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
1998 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
2001 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
2002 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
2004 _mesa_free (error_msg
);
2008 temp_var
->type
= vt_temp
;
2010 if (((Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) &&
2011 (Program
->Base
.NumTemporaries
>=
2012 ctx
->Const
.MaxFragmentProgramTemps
))
2013 || ((Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
2014 && (Program
->Base
.NumTemporaries
>=
2015 ctx
->Const
.MaxVertexProgramTemps
))) {
2016 _mesa_set_program_error (ctx
, Program
->Position
,
2017 "Too many TEMP variables declared");
2018 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2019 "Too many TEMP variables declared");
2023 temp_var
->temp_binding
= Program
->Base
.NumTemporaries
;
2024 Program
->Base
.NumTemporaries
++;
2032 * This handles variables of the OUTPUT variety
2034 * \return 0 on sucess, 1 on error
2037 parse_output (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2038 struct arb_program
*Program
)
2041 struct var_cache
*output_var
;
2043 output_var
= parse_string (inst
, vc_head
, Program
, &found
);
2044 Program
->Position
= parse_position (inst
);
2047 error_msg
= (char *)
2048 _mesa_malloc (_mesa_strlen ((char *) output_var
->name
) + 40);
2049 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
2052 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
2053 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
2055 _mesa_free (error_msg
);
2059 output_var
->type
= vt_output
;
2060 return parse_result_binding (ctx
, inst
, &output_var
->output_binding
,
2061 &output_var
->output_binding_idx
, Program
);
2065 * This handles variables of the ALIAS kind
2067 * \return 0 on sucess, 1 on error
2070 parse_alias (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2071 struct arb_program
*Program
)
2074 struct var_cache
*temp_var
;
2078 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2079 Program
->Position
= parse_position (inst
);
2082 error_msg
= (char *)
2083 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2084 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
2087 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
2088 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
2090 _mesa_free (error_msg
);
2094 temp_var
->type
= vt_alias
;
2095 temp_var
->alias_binding
= parse_string (inst
, vc_head
, Program
, &found
);
2096 Program
->Position
= parse_position (inst
);
2100 error_msg
= (char *)
2101 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2102 _mesa_sprintf (error_msg
, "Alias value %s is not defined",
2103 temp_var
->alias_binding
->name
);
2105 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
2106 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
2108 _mesa_free (error_msg
);
2116 * This handles variables of the ADDRESS kind
2118 * \return 0 on sucess, 1 on error
2121 parse_address (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2122 struct arb_program
*Program
)
2125 struct var_cache
*temp_var
;
2128 while (**inst
!= 0) {
2129 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
2130 Program
->Position
= parse_position (inst
);
2132 error_msg
= (char *)
2133 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
2134 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
2137 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
2138 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
2140 _mesa_free (error_msg
);
2144 temp_var
->type
= vt_address
;
2146 if (Program
->Base
.NumAddressRegs
>=
2147 ctx
->Const
.MaxVertexProgramAddressRegs
) {
2148 _mesa_set_program_error (ctx
, Program
->Position
,
2149 "Too many ADDRESS variables declared");
2150 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2151 "Too many ADDRESS variables declared");
2155 temp_var
->address_binding
= Program
->Base
.NumAddressRegs
;
2156 Program
->Base
.NumAddressRegs
++;
2164 * Parse a program declaration
2166 * \return 0 on sucess, 1 on error
2169 parse_declaration (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2170 struct arb_program
*Program
)
2174 switch (*(*inst
)++) {
2176 err
= parse_address (ctx
, inst
, vc_head
, Program
);
2180 err
= parse_alias (ctx
, inst
, vc_head
, Program
);
2184 err
= parse_attrib (ctx
, inst
, vc_head
, Program
);
2188 err
= parse_output (ctx
, inst
, vc_head
, Program
);
2192 err
= parse_param (ctx
, inst
, vc_head
, Program
);
2196 err
= parse_temp (ctx
, inst
, vc_head
, Program
);
2204 * Handle the parsing out of a masked destination register
2206 * If we are a vertex program, make sure we don't write to
2207 * result.position of we have specified that the program is
2208 * position invariant
2210 * \param File - The register file we write to
2211 * \param Index - The register index we write to
2212 * \param WriteMask - The mask controlling which components we write (1->write)
2214 * \return 0 on sucess, 1 on error
2217 parse_masked_dst_reg (GLcontext
* ctx
, GLubyte
** inst
,
2218 struct var_cache
**vc_head
, struct arb_program
*Program
,
2219 GLint
* File
, GLint
* Index
, GLboolean
* WriteMask
)
2223 struct var_cache
*dst
;
2225 /* We either have a result register specified, or a
2226 * variable that may or may not be writable
2228 switch (*(*inst
)++) {
2229 case REGISTER_RESULT
:
2230 if (parse_result_binding
2231 (ctx
, inst
, &result
, (GLuint
*) Index
, Program
))
2233 *File
= PROGRAM_OUTPUT
;
2236 case REGISTER_ESTABLISHED_NAME
:
2237 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2238 Program
->Position
= parse_position (inst
);
2240 /* If the name has never been added to our symbol table, we're hosed */
2242 _mesa_set_program_error (ctx
, Program
->Position
,
2243 "0: Undefined variable");
2244 _mesa_error (ctx
, GL_INVALID_OPERATION
, "0: Undefined variable: %s",
2249 switch (dst
->type
) {
2251 *File
= PROGRAM_OUTPUT
;
2252 *Index
= dst
->output_binding_idx
;
2256 *File
= PROGRAM_TEMPORARY
;
2257 *Index
= dst
->temp_binding
;
2260 /* If the var type is not vt_output or vt_temp, no go */
2262 _mesa_set_program_error (ctx
, Program
->Position
,
2263 "Destination register is read only");
2264 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2265 "Destination register is read only: %s",
2272 _mesa_set_program_error (ctx
, Program
->Position
,
2273 "Unexpected opcode in parse_masked_dst_reg()");
2274 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2275 "Unexpected opcode in parse_masked_dst_reg()");
2280 /* Position invariance test */
2281 /* This test is done now in syntax portion - when position invariance OPTION
2282 is specified, "result.position" rule is disabled so there is no way
2283 to write the position
2285 /*if ((Program->HintPositionInvariant) && (*File == PROGRAM_OUTPUT) &&
2287 _mesa_set_program_error (ctx, Program->Position,
2288 "Vertex program specified position invariance and wrote vertex position");
2289 _mesa_error (ctx, GL_INVALID_OPERATION,
2290 "Vertex program specified position invariance and wrote vertex position");
2293 /* And then the mask.
2301 WriteMask
[0] = (GLboolean
) (mask
& (1 << 3)) >> 3;
2302 WriteMask
[1] = (GLboolean
) (mask
& (1 << 2)) >> 2;
2303 WriteMask
[2] = (GLboolean
) (mask
& (1 << 1)) >> 1;
2304 WriteMask
[3] = (GLboolean
) (mask
& (1));
2311 * Handle the parsing of a address register
2313 * \param Index - The register index we write to
2315 * \return 0 on sucess, 1 on error
2318 parse_address_reg (GLcontext
* ctx
, GLubyte
** inst
,
2319 struct var_cache
**vc_head
,
2320 struct arb_program
*Program
, GLint
* Index
)
2322 struct var_cache
*dst
;
2326 dst
= parse_string (inst
, vc_head
, Program
, &result
);
2327 Program
->Position
= parse_position (inst
);
2329 /* If the name has never been added to our symbol table, we're hosed */
2331 _mesa_set_program_error (ctx
, Program
->Position
, "Undefined variable");
2332 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Undefined variable: %s",
2337 if (dst
->type
!= vt_address
) {
2338 _mesa_set_program_error (ctx
, Program
->Position
,
2339 "Variable is not of type ADDRESS");
2340 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2341 "Variable: %s is not of type ADDRESS", dst
->name
);
2349 * Handle the parsing out of a masked address register
2351 * \param Index - The register index we write to
2352 * \param WriteMask - The mask controlling which components we write (1->write)
2354 * \return 0 on sucess, 1 on error
2357 parse_masked_address_reg (GLcontext
* ctx
, GLubyte
** inst
,
2358 struct var_cache
**vc_head
,
2359 struct arb_program
*Program
, GLint
* Index
,
2360 GLboolean
* WriteMask
)
2362 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, Index
))
2365 /* This should be 0x8 */
2368 /* Writemask of .x is implied */
2370 WriteMask
[1] = WriteMask
[2] = WriteMask
[3] = 0;
2377 * Parse out a swizzle mask.
2379 * The values in the input stream are:
2380 * COMPONENT_X -> x/r
2381 * COMPONENT_Y -> y/g
2385 * The values in the output mask are:
2391 * The len parameter allows us to grab 4 components for a vector
2392 * swizzle, or just 1 component for a scalar src register selection
2395 parse_swizzle_mask (GLubyte
** inst
, GLubyte
* mask
, GLint len
)
2399 for (a
= 0; a
< 4; a
++)
2402 for (a
= 0; a
< len
; a
++) {
2403 switch (*(*inst
)++) {
2428 parse_extended_swizzle_mask (GLubyte
** inst
, GLubyte
* mask
, GLboolean
* Negate
)
2434 for (a
= 0; a
< 4; a
++) {
2435 if (parse_sign (inst
) == -1)
2442 mask
[a
] = SWIZZLE_ZERO
;
2445 mask
[a
] = SWIZZLE_ONE
;
2448 mask
[a
] = SWIZZLE_X
;
2451 mask
[a
] = SWIZZLE_Y
;
2454 mask
[a
] = SWIZZLE_Z
;
2457 mask
[a
] = SWIZZLE_W
;
2463 mask
[a
] = SWIZZLE_ZERO
;
2465 mask
[a
] = SWIZZLE_ONE
;
2477 parse_src_reg (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
2478 struct arb_program
*Program
, GLint
* File
, GLint
* Index
,
2479 GLboolean
*IsRelOffset
)
2481 struct var_cache
*src
;
2482 GLuint binding_state
, binding_idx
, is_generic
, found
;
2485 /* And the binding for the src */
2486 switch (*(*inst
)++) {
2487 case REGISTER_ATTRIB
:
2488 if (parse_attrib_binding
2489 (ctx
, inst
, Program
, &binding_state
, &binding_idx
, &is_generic
))
2491 *File
= PROGRAM_INPUT
;
2492 *Index
= binding_idx
;
2494 /* We need to insert a dummy variable into the var_cache so we can
2495 * catch generic vertex attrib aliasing errors
2497 var_cache_create(&src
);
2498 src
->type
= vt_attrib
;
2499 src
->name
= (GLubyte
*)_mesa_strdup("Dummy Attrib Variable");
2500 src
->attrib_binding
= binding_state
;
2501 src
->attrib_binding_idx
= binding_idx
;
2502 src
->attrib_is_generic
= is_generic
;
2503 var_cache_append(vc_head
, src
);
2504 if (generic_attrib_check(*vc_head
)) {
2505 _mesa_set_program_error (ctx
, Program
->Position
,
2506 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
2507 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2508 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
2513 case REGISTER_PARAM
:
2515 case PARAM_ARRAY_ELEMENT
:
2517 src
= parse_string (inst
, vc_head
, Program
, &found
);
2518 Program
->Position
= parse_position (inst
);
2521 _mesa_set_program_error (ctx
, Program
->Position
,
2522 "2: Undefined variable");
2523 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2524 "2: Undefined variable: %s", src
->name
);
2528 *File
= src
->param_binding_type
;
2530 switch (*(*inst
)++) {
2531 case ARRAY_INDEX_ABSOLUTE
:
2532 offset
= parse_integer (inst
, Program
);
2535 || (offset
>= (int)src
->param_binding_length
)) {
2536 _mesa_set_program_error (ctx
, Program
->Position
,
2537 "Index out of range");
2538 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2539 "Index %d out of range for %s", offset
,
2544 *Index
= src
->param_binding_begin
+ offset
;
2547 case ARRAY_INDEX_RELATIVE
:
2549 GLint addr_reg_idx
, rel_off
;
2551 /* First, grab the address regiseter */
2552 if (parse_address_reg (ctx
, inst
, vc_head
, Program
, &addr_reg_idx
))
2561 /* Then the relative offset */
2562 if (parse_relative_offset(ctx
, inst
, Program
, &rel_off
)) return 1;
2564 /* And store it properly */
2565 *Index
= src
->param_binding_begin
+ rel_off
;
2574 if (parse_param_use (ctx
, inst
, vc_head
, Program
, &src
))
2577 *File
= src
->param_binding_type
;
2578 *Index
= src
->param_binding_begin
;
2583 case REGISTER_ESTABLISHED_NAME
:
2585 src
= parse_string (inst
, vc_head
, Program
, &found
);
2586 Program
->Position
= parse_position (inst
);
2588 /* If the name has never been added to our symbol table, we're hosed */
2590 _mesa_set_program_error (ctx
, Program
->Position
,
2591 "3: Undefined variable");
2592 _mesa_error (ctx
, GL_INVALID_OPERATION
, "3: Undefined variable: %s",
2597 switch (src
->type
) {
2599 *File
= PROGRAM_INPUT
;
2600 *Index
= src
->attrib_binding_idx
;
2603 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
2605 *File
= src
->param_binding_type
;
2606 *Index
= src
->param_binding_begin
;
2610 *File
= PROGRAM_TEMPORARY
;
2611 *Index
= src
->temp_binding
;
2614 /* If the var type is vt_output no go */
2616 _mesa_set_program_error (ctx
, Program
->Position
,
2617 "destination register is read only");
2618 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2619 "destination register is read only: %s",
2626 _mesa_set_program_error (ctx
, Program
->Position
,
2627 "Unknown token in parse_src_reg");
2628 _mesa_error (ctx
, GL_INVALID_OPERATION
,
2629 "Unknown token in parse_src_reg");
2639 parse_vector_src_reg (GLcontext
* ctx
, GLubyte
** inst
,
2640 struct var_cache
**vc_head
, struct arb_program
*Program
,
2641 GLint
* File
, GLint
* Index
, GLboolean
* Negate
,
2642 GLubyte
* Swizzle
, GLboolean
*IsRelOffset
)
2645 *Negate
= (parse_sign (inst
) == -1);
2647 /* And the src reg */
2648 if (parse_src_reg (ctx
, inst
, vc_head
, Program
, File
, Index
, IsRelOffset
))
2651 /* finally, the swizzle */
2652 parse_swizzle_mask (inst
, Swizzle
, 4);
2660 parse_scalar_src_reg (GLcontext
* ctx
, GLubyte
** inst
,
2661 struct var_cache
**vc_head
, struct arb_program
*Program
,
2662 GLint
* File
, GLint
* Index
, GLboolean
* Negate
,
2663 GLubyte
* Swizzle
, GLboolean
*IsRelOffset
)
2666 *Negate
= (parse_sign (inst
) == -1);
2668 /* And the src reg */
2669 if (parse_src_reg (ctx
, inst
, vc_head
, Program
, File
, Index
, IsRelOffset
))
2672 /* Now, get the component and shove it into all the swizzle slots */
2673 parse_swizzle_mask (inst
, Swizzle
, 1);
2679 * This is a big mother that handles getting opcodes into the instruction
2680 * and handling the src & dst registers for fragment program instructions
2683 parse_fp_instruction (GLcontext
* ctx
, GLubyte
** inst
,
2684 struct var_cache
**vc_head
, struct arb_program
*Program
,
2685 struct fp_instruction
*fp
)
2688 GLubyte swz
[4]; /* FP's swizzle mask is a GLubyte, while VP's is GLuint */
2690 GLubyte instClass
, type
, code
;
2693 /* No condition codes in ARB_fp */
2694 fp
->UpdateCondRegister
= 0;
2696 /* Record the position in the program string for debugging */
2697 fp
->StringPos
= Program
->Position
;
2699 /* OP_ALU_INST or OP_TEX_INST */
2700 instClass
= *(*inst
)++;
2702 /* OP_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
2703 * OP_TEX_{SAMPLE, KIL}
2707 /* The actual opcode name */
2710 /* Increment the correct count */
2711 switch (instClass
) {
2713 Program
->NumAluInstructions
++;
2716 Program
->NumTexInstructions
++;
2721 fp
->Precision
= FLOAT32
;
2723 fp
->DstReg
.CondMask
= COND_TR
;
2731 fp
->Opcode
= FP_OPCODE_ABS
;
2737 fp
->Opcode
= FP_OPCODE_FLR
;
2743 fp
->Opcode
= FP_OPCODE_FRC
;
2749 fp
->Opcode
= FP_OPCODE_LIT
;
2755 fp
->Opcode
= FP_OPCODE_MOV
;
2759 if (parse_masked_dst_reg
2760 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
2761 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
2764 fp
->SrcReg
[0].Abs
= GL_FALSE
;
2765 fp
->SrcReg
[0].NegateAbs
= GL_FALSE
;
2766 if (parse_vector_src_reg
2767 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[0].File
,
2768 &fp
->SrcReg
[0].Index
, &fp
->SrcReg
[0].NegateBase
,
2772 fp
->SrcReg
[0].Swizzle
[b
] = swz
[b
];
2780 fp
->Opcode
= FP_OPCODE_COS
;
2786 fp
->Opcode
= FP_OPCODE_EX2
;
2792 fp
->Opcode
= FP_OPCODE_LG2
;
2798 fp
->Opcode
= FP_OPCODE_RCP
;
2804 fp
->Opcode
= FP_OPCODE_RSQ
;
2810 fp
->Opcode
= FP_OPCODE_SIN
;
2817 fp
->Opcode
= FP_OPCODE_SCS
;
2821 if (parse_masked_dst_reg
2822 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
2823 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
2825 fp
->SrcReg
[0].Abs
= GL_FALSE
;
2826 fp
->SrcReg
[0].NegateAbs
= GL_FALSE
;
2827 if (parse_scalar_src_reg
2828 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[0].File
,
2829 &fp
->SrcReg
[0].Index
, &fp
->SrcReg
[0].NegateBase
,
2833 fp
->SrcReg
[0].Swizzle
[b
] = swz
[b
];
2841 fp
->Opcode
= FP_OPCODE_POW
;
2845 if (parse_masked_dst_reg
2846 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
2847 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
2849 for (a
= 0; a
< 2; a
++) {
2850 fp
->SrcReg
[a
].Abs
= GL_FALSE
;
2851 fp
->SrcReg
[a
].NegateAbs
= GL_FALSE
;
2852 if (parse_scalar_src_reg
2853 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[a
].File
,
2854 &fp
->SrcReg
[a
].Index
, &fp
->SrcReg
[a
].NegateBase
,
2858 fp
->SrcReg
[a
].Swizzle
[b
] = swz
[b
];
2868 fp
->Opcode
= FP_OPCODE_ADD
;
2874 fp
->Opcode
= FP_OPCODE_DP3
;
2880 fp
->Opcode
= FP_OPCODE_DP4
;
2886 fp
->Opcode
= FP_OPCODE_DPH
;
2892 fp
->Opcode
= FP_OPCODE_DST
;
2898 fp
->Opcode
= FP_OPCODE_MAX
;
2904 fp
->Opcode
= FP_OPCODE_MIN
;
2910 fp
->Opcode
= FP_OPCODE_MUL
;
2916 fp
->Opcode
= FP_OPCODE_SGE
;
2922 fp
->Opcode
= FP_OPCODE_SLT
;
2928 fp
->Opcode
= FP_OPCODE_SUB
;
2934 fp
->Opcode
= FP_OPCODE_XPD
;
2938 if (parse_masked_dst_reg
2939 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
2940 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
2942 for (a
= 0; a
< 2; a
++) {
2943 fp
->SrcReg
[a
].Abs
= GL_FALSE
;
2944 fp
->SrcReg
[a
].NegateAbs
= GL_FALSE
;
2945 if (parse_vector_src_reg
2946 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[a
].File
,
2947 &fp
->SrcReg
[a
].Index
, &fp
->SrcReg
[a
].NegateBase
,
2951 fp
->SrcReg
[a
].Swizzle
[b
] = swz
[b
];
2960 fp
->Opcode
= FP_OPCODE_CMP
;
2966 fp
->Opcode
= FP_OPCODE_LRP
;
2972 fp
->Opcode
= FP_OPCODE_MAD
;
2976 if (parse_masked_dst_reg
2977 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
2978 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
2980 for (a
= 0; a
< 3; a
++) {
2981 fp
->SrcReg
[a
].Abs
= GL_FALSE
;
2982 fp
->SrcReg
[a
].NegateAbs
= GL_FALSE
;
2983 if (parse_vector_src_reg
2984 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[a
].File
,
2985 &fp
->SrcReg
[a
].Index
, &fp
->SrcReg
[a
].NegateBase
,
2989 fp
->SrcReg
[a
].Swizzle
[b
] = swz
[b
];
2998 fp
->Opcode
= FP_OPCODE_SWZ
;
3001 if (parse_masked_dst_reg
3002 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
3003 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
3007 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[0].File
,
3008 &fp
->SrcReg
[0].Index
, &rel
))
3010 parse_extended_swizzle_mask (inst
, swz
,
3011 &fp
->SrcReg
[0].NegateBase
);
3013 fp
->SrcReg
[0].Swizzle
[b
] = swz
[b
];
3021 fp
->Opcode
= FP_OPCODE_TEX
;
3027 fp
->Opcode
= FP_OPCODE_TXP
;
3034 fp
->Opcode
= FP_OPCODE_TXB
;
3038 if (parse_masked_dst_reg
3039 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
3040 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
3042 fp
->SrcReg
[0].Abs
= GL_FALSE
;
3043 fp
->SrcReg
[0].NegateAbs
= GL_FALSE
;
3044 if (parse_vector_src_reg
3045 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[0].File
,
3046 &fp
->SrcReg
[0].Index
, &fp
->SrcReg
[0].NegateBase
,
3050 fp
->SrcReg
[0].Swizzle
[b
] = swz
[b
];
3053 if (parse_texcoord_num (ctx
, inst
, Program
, &texcoord
))
3055 fp
->TexSrcUnit
= texcoord
;
3058 switch (*(*inst
)++) {
3060 fp
->TexSrcBit
= TEXTURE_1D_BIT
;
3063 fp
->TexSrcBit
= TEXTURE_2D_BIT
;
3066 fp
->TexSrcBit
= TEXTURE_3D_BIT
;
3068 case TEXTARGET_RECT
:
3069 fp
->TexSrcBit
= TEXTURE_RECT_BIT
;
3071 case TEXTARGET_CUBE
:
3072 fp
->TexSrcBit
= TEXTURE_CUBE_BIT
;
3074 case TEXTARGET_SHADOW1D
:
3075 case TEXTARGET_SHADOW2D
:
3076 case TEXTARGET_SHADOWRECT
:
3077 /* TODO ARB_fragment_program_shadow code */
3080 Program
->TexturesUsed
[texcoord
] |= fp
->TexSrcBit
;
3084 fp
->Opcode
= FP_OPCODE_KIL
;
3085 fp
->SrcReg
[0].Abs
= GL_FALSE
;
3086 fp
->SrcReg
[0].NegateAbs
= GL_FALSE
;
3087 if (parse_vector_src_reg
3088 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[0].File
,
3089 &fp
->SrcReg
[0].Index
, &fp
->SrcReg
[0].NegateBase
,
3093 fp
->SrcReg
[0].Swizzle
[b
] = swz
[b
];
3101 * This is a big mother that handles getting opcodes into the instruction
3102 * and handling the src & dst registers for vertex program instructions
3105 parse_vp_instruction (GLcontext
* ctx
, GLubyte
** inst
,
3106 struct var_cache
**vc_head
, struct arb_program
*Program
,
3107 struct vp_instruction
*vp
)
3112 /* OP_ALU_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
3115 /* The actual opcode name */
3118 /* Record the position in the program string for debugging */
3119 vp
->StringPos
= Program
->Position
;
3121 vp
->SrcReg
[0].RelAddr
= vp
->SrcReg
[1].RelAddr
= vp
->SrcReg
[2].RelAddr
= 0;
3123 for (a
= 0; a
< 4; a
++) {
3124 vp
->SrcReg
[0].Swizzle
[a
] = a
;
3125 vp
->SrcReg
[1].Swizzle
[a
] = a
;
3126 vp
->SrcReg
[2].Swizzle
[a
] = a
;
3127 vp
->DstReg
.WriteMask
[a
] = 1;
3133 vp
->Opcode
= VP_OPCODE_ARL
;
3135 /* Remember to set SrcReg.RelAddr; */
3137 /* Get the masked address register [dst] */
3138 if (parse_masked_address_reg
3139 (ctx
, inst
, vc_head
, Program
, &vp
->DstReg
.Index
,
3140 vp
->DstReg
.WriteMask
))
3142 vp
->DstReg
.File
= PROGRAM_ADDRESS
;
3144 /* Get a scalar src register */
3145 if (parse_scalar_src_reg
3146 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[0].File
,
3147 &vp
->SrcReg
[0].Index
, &vp
->SrcReg
[0].Negate
,
3148 vp
->SrcReg
[0].Swizzle
, &vp
->SrcReg
[0].RelAddr
))
3156 vp
->Opcode
= VP_OPCODE_ABS
;
3159 vp
->Opcode
= VP_OPCODE_FLR
;
3162 vp
->Opcode
= VP_OPCODE_FRC
;
3165 vp
->Opcode
= VP_OPCODE_LIT
;
3168 vp
->Opcode
= VP_OPCODE_MOV
;
3171 if (parse_masked_dst_reg
3172 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->DstReg
.File
,
3173 &vp
->DstReg
.Index
, vp
->DstReg
.WriteMask
))
3175 if (parse_vector_src_reg
3176 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[0].File
,
3177 &vp
->SrcReg
[0].Index
, &vp
->SrcReg
[0].Negate
,
3178 vp
->SrcReg
[0].Swizzle
, &vp
->SrcReg
[0].RelAddr
))
3185 vp
->Opcode
= VP_OPCODE_EX2
;
3188 vp
->Opcode
= VP_OPCODE_EXP
;
3191 vp
->Opcode
= VP_OPCODE_LG2
;
3194 vp
->Opcode
= VP_OPCODE_LOG
;
3197 vp
->Opcode
= VP_OPCODE_RCP
;
3200 vp
->Opcode
= VP_OPCODE_RSQ
;
3203 if (parse_masked_dst_reg
3204 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->DstReg
.File
,
3205 &vp
->DstReg
.Index
, vp
->DstReg
.WriteMask
))
3207 if (parse_scalar_src_reg
3208 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[0].File
,
3209 &vp
->SrcReg
[0].Index
, &vp
->SrcReg
[0].Negate
,
3210 vp
->SrcReg
[0].Swizzle
, &vp
->SrcReg
[0].RelAddr
))
3217 vp
->Opcode
= VP_OPCODE_POW
;
3220 if (parse_masked_dst_reg
3221 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->DstReg
.File
,
3222 &vp
->DstReg
.Index
, vp
->DstReg
.WriteMask
))
3224 for (a
= 0; a
< 2; a
++) {
3225 if (parse_scalar_src_reg
3226 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[a
].File
,
3227 &vp
->SrcReg
[a
].Index
, &vp
->SrcReg
[a
].Negate
,
3228 vp
->SrcReg
[a
].Swizzle
, &vp
->SrcReg
[a
].RelAddr
))
3236 vp
->Opcode
= VP_OPCODE_ADD
;
3239 vp
->Opcode
= VP_OPCODE_DP3
;
3242 vp
->Opcode
= VP_OPCODE_DP4
;
3245 vp
->Opcode
= VP_OPCODE_DPH
;
3248 vp
->Opcode
= VP_OPCODE_DST
;
3251 vp
->Opcode
= VP_OPCODE_MAX
;
3254 vp
->Opcode
= VP_OPCODE_MIN
;
3257 vp
->Opcode
= VP_OPCODE_MUL
;
3260 vp
->Opcode
= VP_OPCODE_SGE
;
3263 vp
->Opcode
= VP_OPCODE_SLT
;
3266 vp
->Opcode
= VP_OPCODE_SUB
;
3269 vp
->Opcode
= VP_OPCODE_XPD
;
3272 if (parse_masked_dst_reg
3273 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->DstReg
.File
,
3274 &vp
->DstReg
.Index
, vp
->DstReg
.WriteMask
))
3276 for (a
= 0; a
< 2; a
++) {
3277 if (parse_vector_src_reg
3278 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[a
].File
,
3279 &vp
->SrcReg
[a
].Index
, &vp
->SrcReg
[a
].Negate
,
3280 vp
->SrcReg
[a
].Swizzle
, &vp
->SrcReg
[a
].RelAddr
))
3288 vp
->Opcode
= VP_OPCODE_MAD
;
3292 if (parse_masked_dst_reg
3293 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->DstReg
.File
,
3294 &vp
->DstReg
.Index
, vp
->DstReg
.WriteMask
))
3296 for (a
= 0; a
< 3; a
++) {
3297 if (parse_vector_src_reg
3298 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[a
].File
,
3299 &vp
->SrcReg
[a
].Index
, &vp
->SrcReg
[a
].Negate
,
3300 vp
->SrcReg
[a
].Swizzle
, &vp
->SrcReg
[a
].RelAddr
))
3308 vp
->Opcode
= VP_OPCODE_SWZ
;
3311 if (parse_masked_dst_reg
3312 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->DstReg
.File
,
3313 &vp
->DstReg
.Index
, vp
->DstReg
.WriteMask
))
3317 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[0].File
,
3318 &vp
->SrcReg
[0].Index
, &vp
->SrcReg
[0].RelAddr
))
3320 parse_extended_swizzle_mask (inst
, vp
->SrcReg
[0].Swizzle
,
3321 &vp
->SrcReg
[0].Negate
);
3330 print_state_token (GLint token
)
3333 case STATE_MATERIAL
:
3334 fprintf (stderr
, "STATE_MATERIAL ");
3337 fprintf (stderr
, "STATE_LIGHT ");
3340 case STATE_LIGHTMODEL_AMBIENT
:
3341 fprintf (stderr
, "STATE_AMBIENT ");
3344 case STATE_LIGHTMODEL_SCENECOLOR
:
3345 fprintf (stderr
, "STATE_SCENECOLOR ");
3348 case STATE_LIGHTPROD
:
3349 fprintf (stderr
, "STATE_LIGHTPROD ");
3353 fprintf (stderr
, "STATE_TEXGEN ");
3356 case STATE_FOG_COLOR
:
3357 fprintf (stderr
, "STATE_FOG_COLOR ");
3360 case STATE_FOG_PARAMS
:
3361 fprintf (stderr
, "STATE_FOG_PARAMS ");
3364 case STATE_CLIPPLANE
:
3365 fprintf (stderr
, "STATE_CLIPPLANE ");
3368 case STATE_POINT_SIZE
:
3369 fprintf (stderr
, "STATE_POINT_SIZE ");
3372 case STATE_POINT_ATTENUATION
:
3373 fprintf (stderr
, "STATE_ATTENUATION ");
3377 fprintf (stderr
, "STATE_MATRIX ");
3380 case STATE_MODELVIEW
:
3381 fprintf (stderr
, "STATE_MODELVIEW ");
3384 case STATE_PROJECTION
:
3385 fprintf (stderr
, "STATE_PROJECTION ");
3389 fprintf (stderr
, "STATE_MVP ");
3393 fprintf (stderr
, "STATE_TEXTURE ");
3397 fprintf (stderr
, "STATE_PROGRAM ");
3400 case STATE_MATRIX_INVERSE
:
3401 fprintf (stderr
, "STATE_INVERSE ");
3404 case STATE_MATRIX_TRANSPOSE
:
3405 fprintf (stderr
, "STATE_TRANSPOSE ");
3408 case STATE_MATRIX_INVTRANS
:
3409 fprintf (stderr
, "STATE_INVTRANS ");
3413 fprintf (stderr
, "STATE_AMBIENT ");
3417 fprintf (stderr
, "STATE_DIFFUSE ");
3420 case STATE_SPECULAR
:
3421 fprintf (stderr
, "STATE_SPECULAR ");
3424 case STATE_EMISSION
:
3425 fprintf (stderr
, "STATE_EMISSION ");
3428 case STATE_SHININESS
:
3429 fprintf (stderr
, "STATE_SHININESS ");
3433 fprintf (stderr
, "STATE_HALF ");
3436 case STATE_POSITION
:
3437 fprintf (stderr
, "STATE_POSITION ");
3440 case STATE_ATTENUATION
:
3441 fprintf (stderr
, "STATE_ATTENUATION ");
3444 case STATE_SPOT_DIRECTION
:
3445 fprintf (stderr
, "STATE_DIRECTION ");
3448 case STATE_TEXGEN_EYE_S
:
3449 fprintf (stderr
, "STATE_TEXGEN_EYE_S ");
3452 case STATE_TEXGEN_EYE_T
:
3453 fprintf (stderr
, "STATE_TEXGEN_EYE_T ");
3456 case STATE_TEXGEN_EYE_R
:
3457 fprintf (stderr
, "STATE_TEXGEN_EYE_R ");
3460 case STATE_TEXGEN_EYE_Q
:
3461 fprintf (stderr
, "STATE_TEXGEN_EYE_Q ");
3464 case STATE_TEXGEN_OBJECT_S
:
3465 fprintf (stderr
, "STATE_TEXGEN_EYE_S ");
3468 case STATE_TEXGEN_OBJECT_T
:
3469 fprintf (stderr
, "STATE_TEXGEN_OBJECT_T ");
3472 case STATE_TEXGEN_OBJECT_R
:
3473 fprintf (stderr
, "STATE_TEXGEN_OBJECT_R ");
3476 case STATE_TEXGEN_OBJECT_Q
:
3477 fprintf (stderr
, "STATE_TEXGEN_OBJECT_Q ");
3480 case STATE_TEXENV_COLOR
:
3481 fprintf (stderr
, "STATE_TEXENV_COLOR ");
3484 case STATE_DEPTH_RANGE
:
3485 fprintf (stderr
, "STATE_DEPTH_RANGE ");
3488 case STATE_VERTEX_PROGRAM
:
3489 fprintf (stderr
, "STATE_VERTEX_PROGRAM ");
3492 case STATE_FRAGMENT_PROGRAM
:
3493 fprintf (stderr
, "STATE_FRAGMENT_PROGRAM ");
3497 fprintf (stderr
, "STATE_ENV ");
3501 fprintf (stderr
, "STATE_LOCAL ");
3505 fprintf (stderr
, "[%d] ", token
);
3510 debug_variables (GLcontext
* ctx
, struct var_cache
*vc_head
,
3511 struct arb_program
*Program
)
3513 struct var_cache
*vc
;
3516 fprintf (stderr
, "debug_variables, vc_head: %x\n", vc_head
);
3518 /* First of all, print out the contents of the var_cache */
3521 fprintf (stderr
, "[%x]\n", vc
);
3524 fprintf (stderr
, "UNDEFINED %s\n", vc
->name
);
3527 fprintf (stderr
, "ATTRIB %s\n", vc
->name
);
3528 fprintf (stderr
, " binding: 0x%x\n", vc
->attrib_binding
);
3531 fprintf (stderr
, "PARAM %s begin: %d len: %d\n", vc
->name
,
3532 vc
->param_binding_begin
, vc
->param_binding_length
);
3533 b
= vc
->param_binding_begin
;
3534 for (a
= 0; a
< vc
->param_binding_length
; a
++) {
3535 fprintf (stderr
, "%s\n",
3536 Program
->Parameters
->Parameters
[a
+ b
].Name
);
3537 if (Program
->Parameters
->Parameters
[a
+ b
].Type
== STATE
) {
3538 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3540 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3542 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3544 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3546 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3548 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
3552 fprintf (stderr
, "%f %f %f %f\n",
3553 Program
->Parameters
->Parameters
[a
+ b
].Values
[0],
3554 Program
->Parameters
->Parameters
[a
+ b
].Values
[1],
3555 Program
->Parameters
->Parameters
[a
+ b
].Values
[2],
3556 Program
->Parameters
->Parameters
[a
+ b
].Values
[3]);
3560 fprintf (stderr
, "TEMP %s\n", vc
->name
);
3561 fprintf (stderr
, " binding: 0x%x\n", vc
->temp_binding
);
3564 fprintf (stderr
, "OUTPUT %s\n", vc
->name
);
3565 fprintf (stderr
, " binding: 0x%x\n", vc
->output_binding
);
3568 fprintf (stderr
, "ALIAS %s\n", vc
->name
);
3569 fprintf (stderr
, " binding: 0x%x (%s)\n",
3570 vc
->alias_binding
, vc
->alias_binding
->name
);
3581 * The main loop for parsing a fragment or vertex program
3583 * \return 0 on sucess, 1 on error
3586 parse_arb_program (GLcontext
* ctx
, GLubyte
* inst
, struct var_cache
**vc_head
,
3587 struct arb_program
*Program
)
3591 Program
->MajorVersion
= (GLuint
) * inst
++;
3592 Program
->MinorVersion
= (GLuint
) * inst
++;
3594 while (*inst
!= END
) {
3599 case ARB_PRECISION_HINT_FASTEST
:
3600 Program
->PrecisionOption
= GL_FASTEST
;
3603 case ARB_PRECISION_HINT_NICEST
:
3604 Program
->PrecisionOption
= GL_NICEST
;
3608 Program
->FogOption
= GL_EXP
;
3612 Program
->FogOption
= GL_EXP2
;
3615 case ARB_FOG_LINEAR
:
3616 Program
->FogOption
= GL_LINEAR
;
3619 case ARB_POSITION_INVARIANT
:
3620 if (Program
->Base
.Target
== GL_VERTEX_PROGRAM_ARB
)
3621 Program
->HintPositionInvariant
= 1;
3624 case ARB_FRAGMENT_PROGRAM_SHADOW
:
3625 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3626 /* TODO ARB_fragment_program_shadow code */
3630 case ARB_DRAW_BUFFERS
:
3631 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3632 /* do nothing for now */
3639 Program
->Position
= parse_position (&inst
);
3641 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3643 /* Check the instruction count
3644 * XXX: Does END count as an instruction?
3646 if (Program
->Base
.NumInstructions
+1 == MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS
) {
3647 _mesa_set_program_error (ctx
, Program
->Position
,
3648 "Max instruction count exceeded!");
3649 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3650 "Max instruction count exceeded!");
3653 /* Realloc Program->FPInstructions */
3654 Program
->FPInstructions
=
3655 (struct fp_instruction
*) _mesa_realloc (Program
->FPInstructions
,
3656 Program
->Base
.NumInstructions
*sizeof(struct fp_instruction
),
3657 (Program
->Base
.NumInstructions
+1)*sizeof (struct fp_instruction
));
3659 /* parse the current instruction */
3660 err
= parse_fp_instruction (ctx
, &inst
, vc_head
, Program
,
3661 &Program
->FPInstructions
[Program
->Base
.NumInstructions
]);
3665 /* Check the instruction count
3666 * XXX: Does END count as an instruction?
3668 if (Program
->Base
.NumInstructions
+1 == MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
) {
3669 _mesa_set_program_error (ctx
, Program
->Position
,
3670 "Max instruction count exceeded!");
3671 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3672 "Max instruction count exceeded!");
3675 /* Realloc Program->VPInstructions */
3676 Program
->VPInstructions
=
3677 (struct vp_instruction
*) _mesa_realloc (Program
->VPInstructions
,
3678 Program
->Base
.NumInstructions
*sizeof(struct vp_instruction
),
3679 (Program
->Base
.NumInstructions
+1)*sizeof(struct vp_instruction
));
3681 /* parse the current instruction */
3682 err
= parse_vp_instruction (ctx
, &inst
, vc_head
, Program
,
3683 &Program
->VPInstructions
[Program
->Base
.NumInstructions
]);
3686 /* increment Program->Base.NumInstructions */
3687 Program
->Base
.NumInstructions
++;
3691 err
= parse_declaration (ctx
, &inst
, vc_head
, Program
);
3702 /* Finally, tag on an OPCODE_END instruction */
3703 if (Program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
) {
3704 Program
->FPInstructions
=
3705 (struct fp_instruction
*) _mesa_realloc (Program
->FPInstructions
,
3706 Program
->Base
.NumInstructions
*sizeof(struct fp_instruction
),
3707 (Program
->Base
.NumInstructions
+1)*sizeof(struct fp_instruction
));
3709 Program
->FPInstructions
[Program
->Base
.NumInstructions
].Opcode
= FP_OPCODE_END
;
3710 /* YYY Wrong Position in program, whatever, at least not random -> crash
3711 Program->Position = parse_position (&inst);
3713 Program
->FPInstructions
[Program
->Base
.NumInstructions
].StringPos
= Program
->Position
;
3716 Program
->VPInstructions
=
3717 (struct vp_instruction
*) _mesa_realloc (Program
->VPInstructions
,
3718 Program
->Base
.NumInstructions
*sizeof(struct vp_instruction
),
3719 (Program
->Base
.NumInstructions
+1)*sizeof(struct vp_instruction
));
3721 Program
->VPInstructions
[Program
->Base
.NumInstructions
].Opcode
= VP_OPCODE_END
;
3722 /* YYY Wrong Position in program, whatever, at least not random -> crash
3723 Program->Position = parse_position (&inst);
3725 Program
->VPInstructions
[Program
->Base
.NumInstructions
].StringPos
= Program
->Position
;
3728 /* increment Program->Base.NumInstructions */
3729 Program
->Base
.NumInstructions
++;
3735 __extension__
static char core_grammar_text
[] =
3736 #include "grammar_syn.h"
3739 static int set_reg8 (GLcontext
*ctx
, grammar id
, const byte
*name
, byte value
)
3741 char error_msg
[300];
3744 if (grammar_set_reg8 (id
, name
, value
))
3747 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3748 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3749 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Grammar Register Error");
3753 static int extension_is_supported (const GLubyte
*ext
)
3755 const GLubyte
*extensions
= glGetString (GL_EXTENSIONS
);
3756 const GLubyte
*end
= extensions
+ _mesa_strlen ((const char *) extensions
);
3757 const GLint ext_len
= _mesa_strlen ((const char *) ext
);
3759 while (extensions
< end
)
3761 const GLubyte
*name_end
= (const GLubyte
*) strchr ((const char *) extensions
, ' ');
3762 if (name_end
== NULL
)
3764 if (name_end
- extensions
== ext_len
&& _mesa_strncmp ((const char *) ext
,
3765 (const char *) extensions
, ext_len
) == 0)
3767 extensions
= name_end
+ 1;
3773 static int enable_ext (GLcontext
*ctx
, grammar id
, const byte
*name
, const byte
*extname
)
3775 if (extension_is_supported (extname
))
3776 if (set_reg8 (ctx
, id
, name
, 0x01))
3782 * This kicks everything off.
3784 * \param ctx - The GL Context
3785 * \param str - The program string
3786 * \param len - The program string length
3787 * \param Program - The arb_program struct to return all the parsed info in
3788 * \return 0 on sucess, 1 on error
3791 _mesa_parse_arb_program (GLcontext
* ctx
, const GLubyte
* str
, GLsizei len
,
3792 struct arb_program
* program
)
3794 GLint a
, err
, error_pos
;
3795 char error_msg
[300];
3797 struct var_cache
*vc_head
;
3798 grammar arbprogram_syn_id
;
3799 GLubyte
*parsed
, *inst
;
3800 GLubyte
*strz
= NULL
;
3801 static int arbprogram_syn_is_ok
= 0; /* XXX temporary */
3803 /* Reset error state */
3804 _mesa_set_program_error(ctx
, -1, NULL
);
3807 fprintf (stderr
, "Loading grammar text!\n");
3810 /* check if the arb_grammar_text (arbprogram.syn) is syntactically correct */
3811 if (!arbprogram_syn_is_ok
) {
3812 grammar grammar_syn_id
;
3817 grammar_syn_id
= grammar_load_from_text ((byte
*) core_grammar_text
);
3818 if (grammar_syn_id
== 0) {
3819 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3820 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3821 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3822 "Error loading grammar rule set");
3826 err
= grammar_check (grammar_syn_id
, (byte
*) arb_grammar_text
, &parsed
, &parsed_len
);
3828 /* NOTE: we cant destroy grammar_syn_id right here because grammar_destroy() can
3829 reset the last error
3833 grammar_get_last_error ((byte
*) error_msg
, 300, &error_pos
);
3834 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3835 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Error loading grammar rule set");
3837 grammar_destroy (grammar_syn_id
);
3841 grammar_destroy (grammar_syn_id
);
3843 arbprogram_syn_is_ok
= 1;
3846 /* create the grammar object */
3847 arbprogram_syn_id
= grammar_load_from_text ((byte
*) arb_grammar_text
);
3848 if (arbprogram_syn_id
== 0) {
3849 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
3850 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3851 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3852 "Error loading grammer rule set");
3856 /* Set program_target register value */
3857 if (set_reg8 (ctx
, arbprogram_syn_id
, (byte
*) "program_target",
3858 program
->Base
.Target
== GL_FRAGMENT_PROGRAM_ARB
? 0x10 : 0x20)) {
3859 grammar_destroy (arbprogram_syn_id
);
3863 /* Enable all active extensions */
3864 if (enable_ext (ctx
, arbprogram_syn_id
,
3865 (byte
*) "vertex_blend", (byte
*) "GL_ARB_vertex_blend") ||
3866 enable_ext (ctx
, arbprogram_syn_id
,
3867 (byte
*) "vertex_blend", (byte
*) "GL_EXT_vertex_weighting") ||
3868 enable_ext (ctx
, arbprogram_syn_id
,
3869 (byte
*) "matrix_palette", (byte
*) "GL_ARB_matrix_palette") ||
3870 enable_ext (ctx
, arbprogram_syn_id
,
3871 (byte
*) "point_parameters", (byte
*) "GL_ARB_point_parameters") ||
3872 enable_ext (ctx
, arbprogram_syn_id
,
3873 (byte
*) "point_parameters", (byte
*) "GL_EXT_point_parameters") ||
3874 enable_ext (ctx
, arbprogram_syn_id
,
3875 (byte
*) "secondary_color", (byte
*) "GL_EXT_secondary_color") ||
3876 enable_ext (ctx
, arbprogram_syn_id
,
3877 (byte
*) "fog_coord", (byte
*) "GL_EXT_fog_coord") ||
3878 enable_ext (ctx
, arbprogram_syn_id
,
3879 (byte
*) "texture_rectangle", (byte
*) "GL_ARB_texture_rectangle") ||
3880 enable_ext (ctx
, arbprogram_syn_id
,
3881 (byte
*) "texture_rectangle", (byte
*) "GL_EXT_texture_rectangle") ||
3882 enable_ext (ctx
, arbprogram_syn_id
,
3883 (byte
*) "texture_rectangle", (byte
*) "GL_NV_texture_rectangle") ||
3884 enable_ext (ctx
, arbprogram_syn_id
,
3885 (byte
*) "fragment_program_shadow", (byte
*) "GL_ARB_fragment_program_shadow") ||
3886 enable_ext (ctx
, arbprogram_syn_id
,
3887 (byte
*) "draw_buffers", (byte
*) "GL_ARB_draw_buffers")) {
3888 grammar_destroy (arbprogram_syn_id
);
3892 /* check for NULL character occurences */
3895 for (i
= 0; i
< len
; i
++)
3896 if (str
[i
] == '\0') {
3897 _mesa_set_program_error (ctx
, i
, "invalid character");
3898 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Lexical Error");
3900 grammar_destroy (arbprogram_syn_id
);
3905 /* copy the program string to a null-terminated string */
3906 /* XXX should I check for NULL from malloc()? */
3907 strz
= (GLubyte
*) _mesa_malloc (len
+ 1);
3908 _mesa_memcpy (strz
, str
, len
);
3912 printf ("Checking Grammar!\n");
3914 err
= grammar_check (arbprogram_syn_id
, strz
, &parsed
, &parsed_len
);
3916 /* Syntax parse error */
3919 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
3920 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
3921 _mesa_error (ctx
, GL_INVALID_OPERATION
, "glProgramStringARB(syntax error)");
3923 /* useful for debugging */
3927 printf("Program: %s\n", (char *) strz
);
3928 printf("Error Pos: %d\n", ctx
->Program
.ErrorPos
);
3929 s
= (char *) _mesa_find_line_column(strz
, strz
+ctx
->Program
.ErrorPos
, &line
, &col
);
3930 printf("line %d col %d: %s\n", line
, col
, s
);
3933 grammar_destroy (arbprogram_syn_id
);
3938 printf ("Destroying grammer dict [parse retval: %d]\n", err
);
3940 grammar_destroy (arbprogram_syn_id
);
3942 /* Initialize the arb_program struct */
3943 program
->Base
.String
= strz
;
3944 program
->Base
.NumInstructions
=
3945 program
->Base
.NumTemporaries
=
3946 program
->Base
.NumParameters
=
3947 program
->Base
.NumAttributes
= program
->Base
.NumAddressRegs
= 0;
3948 program
->Parameters
= _mesa_new_parameter_list ();
3949 program
->InputsRead
= 0;
3950 program
->OutputsWritten
= 0;
3951 program
->Position
= 0;
3952 program
->MajorVersion
= program
->MinorVersion
= 0;
3953 program
->PrecisionOption
= GL_DONT_CARE
;
3954 program
->FogOption
= GL_NONE
;
3955 program
->HintPositionInvariant
= GL_FALSE
;
3956 for (a
= 0; a
< MAX_TEXTURE_IMAGE_UNITS
; a
++)
3957 program
->TexturesUsed
[a
] = 0;
3958 program
->NumAluInstructions
=
3959 program
->NumTexInstructions
=
3960 program
->NumTexIndirections
= 0;
3962 program
->FPInstructions
= NULL
;
3963 program
->VPInstructions
= NULL
;
3968 /* Start examining the tokens in the array */
3971 /* Check the grammer rev */
3972 if (*inst
++ != REVISION
) {
3973 _mesa_set_program_error (ctx
, 0, "Grammar version mismatch");
3974 _mesa_error (ctx
, GL_INVALID_OPERATION
, "glProgramStringARB(Grammar verison mismatch)");
3979 case FRAGMENT_PROGRAM
:
3980 program
->Base
.Target
= GL_FRAGMENT_PROGRAM_ARB
;
3983 case VERTEX_PROGRAM
:
3984 program
->Base
.Target
= GL_VERTEX_PROGRAM_ARB
;
3988 err
= parse_arb_program (ctx
, inst
, &vc_head
, program
);
3990 fprintf (stderr
, "Symantic analysis returns %d [1 is bad!]\n", err
);
3994 /*debug_variables(ctx, vc_head, program); */
3996 /* We're done with the parsed binary array */
3997 var_cache_destroy (&vc_head
);
3999 _mesa_free (parsed
);
4001 printf ("_mesa_parse_arb_program() done\n");