2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 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
29 * ARB_*_program parser core
30 * \author Michal Krol, Karl Rasche
41 #include "nvvertprog.h"
42 #include "nvfragprog.h"
46 * Fragment Program Stuff:
47 * -----------------------------------------------------
48 * - How does negating on SWZ work?? If any of the components have a -,
50 * - how does thing like 'foo[N]' work in src registers?
52 * - things from Michal's email
54 * + not-overflowing floats (don't use parse_integer..)
55 * + test for 0 on matrix rows, or give a default value to parse_integer()
57 * + fix multiple cases in switches, that might change
58 * (these are things that are #defined to the same value, but occur
59 * only on fp or vp's, which funkifies the switch statements)
60 * - STATE_TEX_* STATE_CLIP_PLANE, etc and PRECISION_HINT_FASTEST/
63 * - check all limits of number of various variables
65 * + modelview matrix number
69 * Vertex Program Stuff:
70 * -----------------------------------------------------
71 * - Add in cases for vp attribs
72 * + VERTEX_ATTRIB_MATRIXINDEX -- ??
74 * - ARRAY_INDEX_RELATIVE
78 * -----------------------------------------------------
79 * - vp_src swizzle is GLubyte, fp_src swizzle is GLuint
80 * - fetch state listed in program_parameters list
81 * + WTF should this go???
82 * + currently in nvvertexec.c and s_nvfragprog.c
84 * - allow for multiple address registers (and fetch address regs properly)
87 * -----------------------------------------------------
88 * - fix compiler warnings
89 * - remove any leftover unused grammer.c stuff (dict_ ?)
90 * - fix grammer.c error handling so its not static
91 * - #ifdef around stuff pertaining to extentions
93 * Outstanding Questions:
94 * -----------------------------------------------------
95 * - palette matrix? do we support this extension? what is the extention?
96 * - When can we fetch env/local params from their own register files, and
97 * when to we have to fetch them into the main state register file?
101 * -----------------------------------------------------
104 typedef GLubyte
*production
;
106 /*-----------------------------------------------------------------------
107 * From here on down is the syntax checking portion
116 The task is to check the syntax of an input string. Input string is a
117 stream of ASCII characters terminated with null-character
118 ('\0'). Checking it using C language is difficult and hard to
119 implement without bugs. It is hard to maintain and change prior to
120 further syntax changes.
122 This is because of high redundancy of the C code. Large blocks of code
123 are duplicated with only small changes. Even using macros does not
124 solve the problem, because macros cannot erase the complexity of the
127 The resolution is to create a new language that will be highly
128 oriented to our task. Once we describe particular syntax, we are
129 done. We can then focus on the code that implements the language. The
130 size and complexity of it is relatively small than the code that
131 directly checks the syntax.
133 First, we must implement our new language. Here, the language is
134 implemented in C, but it could also be implemented in any other
135 language. The code is listed below. We must take a good care that it
136 is bug free. This is simple because the code is simple and clean.
138 Next, we must describe the syntax of our new language in itself. Once
139 created and checked manually that it is correct, we can use it to
140 check another scripts.
142 Note that our new language loading code does not have to check the
143 syntax. It is because we assume that the script describing itself is
144 correct, and other scripts can be syntactically checked by the former
145 script. The loading code must only do semantic checking which leads us
146 to simple resolving references.
151 Here I will describe the syntax of the new language (further called
152 "Synek"). It is mainly a sequence of declarations terminated by a
153 semicolon. The declaration consists of a symbol, which is an
154 identifier, and its definition. A definition is in turn a sequence of
155 specifiers connected with ".and" or ".or" operator. These operators
156 cannot be mixed together in a one definition. Specifier can be a
157 symbol, string, character, character range or a special keyword
160 On the very beginning of the script there is a declaration of a root
161 symbol and is in the form:
162 .syntax <root_symbol>;
164 The <root_symbol> must be on of the symbols in declaration
165 sequence. The syntax is correct if the root symbol evaluates to
166 true. A symbol evaluates to true if the definition associated with the
167 symbol evaluates to true. Definition evaluation depends on the
168 operator used to connect specifiers in the definition. If ".and"
169 operator is used, definition evaluates to true if and only if all the
170 specifiers evaluate to true. If ".or" operator is used, definition
171 evalutes to true if any of the specifiers evaluates to true. If
172 definition contains only one specifier, it is evaluated as if it was
173 connected with ".true" keyword by ".and" operator.
175 If specifier is a ".true" keyword, it always evaluates to true.
177 If specifier is a ".false" keyword, it always evaluates to
178 false. Specifier evaluates to false when it does not evaluate to true.
180 Character range specifier is in the form:
181 '<first_character>' - '<second_character>'
183 If specifier is a character range, it evaluates to true if character
184 in the stream is greater or equal to <first_character> and less or
185 equal to <second_character>. In that situation the stream pointer is
186 advanced to point to next character in the stream. All C-style escape
187 sequences are supported although trigraph sequences are not. The
188 comparisions are performed on 8-bit unsigned integers.
190 Character specifier is in the form:
193 It evaluates to true if the following character range specifier evaluates to
195 '<single_character>' - '<single_character>'
197 String specifier is in the form:
200 Let N be the number of characters in <string>. Let <string>[i]
201 designate i-th character in <string>. Then the string specifier
202 evaluates to true if and only if for i in the range [0, N) the
203 following character specifier evaluates to true:
206 If <string>[i] is a quotation mark, '<string>[i]' is replaced with
209 Symbol specifier can be optionally preceded by a ".loop" keyword in the form:
211 where <symbol> is defined as follows:
212 <symbol> <definition>; (2)
213 Construction (1) is replaced by the following code:
215 and declaration (2) is replaced by the following:
216 <symbol$1> <symbol$2> .or .true;
217 <symbol$2> <symbol> .and <symbol$1>;
218 <symbol> <definition>;
224 Synek supports all escape sequences in character specifiers. The
225 mapping table is listed below. All occurences of the characters in
226 the first column are replaced with the corresponding character in the
229 Escape sequence Represents
230 -----------------------------------------------------------------------
238 \' Single quotation mark
239 \" Double quotation mark
241 \? Literal question mark
242 \ooo ASCII character in octal notation
243 \xhhh ASCII character in hexadecimal notation
244 -----------------------------------------------------------------------
250 Any specifier can be followed by a special construction that is
251 executed when the specifier evaluates to false. The construction is in
255 <ERROR_TEXT> is an identifier declared earlier by error text
256 declaration. The declaration is in the form:
258 .errtext <ERROR_TEXT> "<error_desc>"
260 When specifier evaluates to false and this construction is present,
261 parsing is stopped immediately and <error_desc> is returned as a
262 result of parsing. The error position is also returned and it is meant
263 as an offset from the beggining of the stream to the character that
264 was valid so far. Example:
266 (**** syntax script ****)
269 .errtext MISSING_SEMICOLON "missing ';'"
270 program declaration .and .loop space .and ';'
271 .error MISSING_SEMICOLON .and
272 .loop space .and '\0';
273 declaration "declare" .and .loop space .and identifier;
275 (**** sample code ****)
278 In the example above checking the sample code will result in error
279 message "missing ';'" and error position 12. The sample code is not
280 correct. Note the presence of '\0' specifier to assure that there is
281 no code after semicolon - only spaces. <error_desc> can optionally
282 contain identifier surrounded by dollar signs $. In such a case, the
283 identifier and dollar signs are replaced by a string retrieved by
284 invoking symbol with the identifier name. The starting position is the
285 error position. The lenght of the resulting string is the position
286 after invoking the symbol.
292 Synek not only checks the syntax but it can also produce (emit) bytes
293 associated with specifiers that evaluate to true. That is, every
294 specifier and optional error construction can be followed by a number
295 of emit constructions that are in the form:
298 <paramater> can be a HEX number, identifier, a star * or a dollar
299 $. HEX number is preceded by 0x or 0X. If <parameter> is an
300 identifier, it must be earlier declared by emit code declaration in
302 .emtcode <identifier> <hex_number>
304 When given specifier evaluates to true, all emits associated with the
305 specifier are output in order they were declared. A star means that
306 last-read character should be output instead of constant
309 (**** syntax script ****)
312 .emtcode WORD_FOO 0x01
313 .emtcode WORD_BAR 0x02
314 foobar FOO .emit WORD_FOO .or BAR .emit WORD_BAR .or .true .emit 0x00;
315 FOO "foo" .and SPACE;
316 BAR "bar" .and SPACE;
319 (**** sample text 1 ****)
323 (**** sample text 2 ****)
327 For both samples the result will be one-element array. For first
328 sample text it will be value 1, for second - 0. Note that every text
329 will be accepted because of presence of .true as an alternative.
333 (**** syntax script ****)
336 .emtcode VARIABLE 0x01
337 declaration "declare" .and .loop space .and
338 identifier .emit VARIABLE .and (1)
339 .true .emit 0x00 .and (2)
340 .loop space .and ';';
342 identifier .loop id_char .emit *; (3)
343 id_char 'a'-'z' .or 'A'-'Z' .or '_';
344 (**** sample code ****)
347 In specifier (1) symbol <identifier> is followed by .emit VARIABLE. If
348 it evaluates to true, VARIABLE constant and then production of the
349 symbol is output. Specifier (2) is used to terminate the string with
350 null to signal when the string ends. Specifier (3) outputs all
351 characters that make declared identifier. The result of sample code
352 will be the following array:
353 { 1, 'f', 'u', 'b', 'a', 'r', 0 }
355 If .emit is followed by dollar $, it means that current position
356 should be output. Current position is a 32-bit unsigned integer
357 distance from the very beginning of the parsed string to first
358 character consumed by the specifier associated with the .emit
359 instruction. Current position is stored in the output buffer in
360 Little-Endian convention (the lowest byte comes first). */
364 * This is the text describing the rules to parse the grammar
366 #include "arbparse_syn.h"
369 * These should match up with the values defined in arbparse.syn.h
372 #define REVISION 0x04
375 #define FRAGMENT_PROGRAM 0x01
376 #define VERTEX_PROGRAM 0x02
378 /* program section */
380 #define INSTRUCTION 0x02
381 #define DECLARATION 0x03
384 /* fragment program option flags */
385 #define ARB_PRECISION_HINT_FASTEST 0x01
386 #define ARB_PRECISION_HINT_NICEST 0x02
387 #define ARB_FOG_EXP 0x04
388 #define ARB_FOG_EXP2 0x08
389 #define ARB_FOG_LINEAR 0x10
391 /* vertex program option flags */
393 $4: changed from 0x01 to 0x20.
395 #define ARB_POSITION_INVARIANT 0x20
397 /* fragment program instruction class */
398 #define F_ALU_INST 0x01
399 #define F_TEX_INST 0x02
401 /* fragment program instruction type */
402 #define F_ALU_VECTOR 0x01
403 #define F_ALU_SCALAR 0x02
404 #define F_ALU_BINSC 0x03
405 #define F_ALU_BIN 0x04
406 #define F_ALU_TRI 0x05
407 #define F_ALU_SWZ 0x06
408 #define F_TEX_SAMPLE 0x07
409 #define F_TEX_KIL 0x08
411 /* vertex program instruction type */
412 #define V_GEN_ARL 0x01
413 #define V_GEN_VECTOR 0x02
414 #define V_GEN_SCALAR 0x03
415 #define V_GEN_BINSC 0x04
416 #define V_GEN_BIN 0x05
417 #define V_GEN_TRI 0x06
418 #define V_GEN_SWZ 0x07
420 /* fragment program instruction code */
422 #define F_ABS_SAT 0x01
424 #define F_FLR_SAT 0x03
426 #define F_FRC_SAT 0x05
428 #define F_LIT_SAT 0x07
430 #define F_MOV_SAT 0x09
432 #define F_COS_SAT 0x0B
434 #define F_EX2_SAT 0x0D
436 #define F_LG2_SAT 0x0F
438 #define F_RCP_SAT 0x11
440 #define F_RSQ_SAT 0x13
442 #define F_SIN_SAT 0x15
444 #define F_SCS_SAT 0x17
446 #define F_POW_SAT 0x19
448 #define F_ADD_SAT 0x1B
450 #define F_DP3_SAT 0x1D
452 #define F_DP4_SAT 0x1F
454 #define F_DPH_SAT 0x21
456 #define F_DST_SAT 0x23
458 #define F_MAX_SAT 0x25
460 #define F_MIN_SAT 0x27
462 #define F_MUL_SAT 0x29
464 #define F_SGE_SAT 0x2B
466 #define F_SLT_SAT 0x2D
468 #define F_SUB_SAT 0x2F
470 #define F_XPD_SAT 0x31
472 #define F_CMP_SAT 0x33
474 #define F_LRP_SAT 0x35
476 #define F_MAD_SAT 0x37
478 #define F_SWZ_SAT 0x39
480 #define F_TEX_SAT 0x3B
482 #define F_TXB_SAT 0x3D
484 #define F_TXP_SAT 0x3F
487 /* vertex program instruction code */
516 /* fragment attribute binding */
517 #define FRAGMENT_ATTRIB_COLOR 0x01
518 #define FRAGMENT_ATTRIB_TEXCOORD 0x02
519 #define FRAGMENT_ATTRIB_FOGCOORD 0x03
520 #define FRAGMENT_ATTRIB_POSITION 0x04
522 /* vertex attribute binding */
523 #define VERTEX_ATTRIB_POSITION 0x01
524 #define VERTEX_ATTRIB_WEIGHT 0x02
525 #define VERTEX_ATTRIB_NORMAL 0x03
526 #define VERTEX_ATTRIB_COLOR 0x04
527 #define VERTEX_ATTRIB_FOGCOORD 0x05
528 #define VERTEX_ATTRIB_TEXCOORD 0x06
529 #define VERTEX_ATTRIB_MATRIXINDEX 0x07
530 #define VERTEX_ATTRIB_GENERIC 0x08
532 /* fragment result binding */
533 #define FRAGMENT_RESULT_COLOR 0x01
534 #define FRAGMENT_RESULT_DEPTH 0x02
536 /* vertex result binding */
537 #define VERTEX_RESULT_POSITION 0x01
538 #define VERTEX_RESULT_COLOR 0x02
539 #define VERTEX_RESULT_FOGCOORD 0x03
540 #define VERTEX_RESULT_POINTSIZE 0x04
541 #define VERTEX_RESULT_TEXCOORD 0x05
544 #define TEXTARGET_1D 0x01
545 #define TEXTARGET_2D 0x02
546 #define TEXTARGET_3D 0x03
547 #define TEXTARGET_RECT 0x04
548 #define TEXTARGET_CUBE 0x05
552 $3: removed. '+' and '-' are used instead.
555 #define SIGN_PLUS 0x00
556 #define SIGN_MINUS 0x01
560 #define FACE_FRONT 0x00
561 #define FACE_BACK 0x01
564 #define COLOR_PRIMARY 0x00
565 #define COLOR_SECONDARY 0x01
569 $3: Added enumerants.
571 #define COMPONENT_X 0x00
572 #define COMPONENT_Y 0x01
573 #define COMPONENT_Z 0x02
574 #define COMPONENT_W 0x03
575 #define COMPONENT_0 0x04
576 #define COMPONENT_1 0x05
578 #define ARRAY_INDEX_ABSOLUTE 0x00
579 #define ARRAY_INDEX_RELATIVE 0x01
582 #define MATRIX_MODELVIEW 0x01
583 #define MATRIX_PROJECTION 0x02
584 #define MATRIX_MVP 0x03
585 #define MATRIX_TEXTURE 0x04
586 #define MATRIX_PALETTE 0x05
587 #define MATRIX_PROGRAM 0x06
589 /* matrix modifier */
590 #define MATRIX_MODIFIER_IDENTITY 0x00
591 #define MATRIX_MODIFIER_INVERSE 0x01
592 #define MATRIX_MODIFIER_TRANSPOSE 0x02
593 #define MATRIX_MODIFIER_INVTRANS 0x03
596 #define CONSTANT_SCALAR 0x01
597 #define CONSTANT_VECTOR 0x02
599 /* program param type */
600 #define PROGRAM_PARAM_ENV 0x01
601 #define PROGRAM_PARAM_LOCAL 0x02
604 #define REGISTER_ATTRIB 0x01
605 #define REGISTER_PARAM 0x02
606 #define REGISTER_RESULT 0x03
607 #define REGISTER_ESTABLISHED_NAME 0x04
610 #define PARAM_NULL 0x00
611 #define PARAM_ARRAY_ELEMENT 0x01
612 #define PARAM_STATE_ELEMENT 0x02
613 #define PARAM_PROGRAM_ELEMENT 0x03
614 #define PARAM_PROGRAM_ELEMENTS 0x04
615 #define PARAM_CONSTANT 0x05
617 /* param state property */
618 #define STATE_MATERIAL_PARSER 0x01
619 #define STATE_LIGHT_PARSER 0x02
620 #define STATE_LIGHT_MODEL 0x03
621 #define STATE_LIGHT_PROD 0x04
622 #define STATE_FOG 0x05
623 #define STATE_MATRIX_ROWS 0x06
624 /* fragment program only */
625 #define STATE_TEX_ENV 0x07
626 #define STATE_DEPTH 0x08
627 /* vertex program only */
629 $4: incremented all the three emit codes by two to not collide with other STATE_* emit codes.
631 #define STATE_TEX_GEN 0x09
632 #define STATE_CLIP_PLANE 0x0A
633 #define STATE_POINT 0x0B
635 /* state material property */
636 #define MATERIAL_AMBIENT 0x01
637 #define MATERIAL_DIFFUSE 0x02
638 #define MATERIAL_SPECULAR 0x03
639 #define MATERIAL_EMISSION 0x04
640 #define MATERIAL_SHININESS 0x05
642 /* state light property */
643 #define LIGHT_AMBIENT 0x01
644 #define LIGHT_DIFFUSE 0x02
645 #define LIGHT_SPECULAR 0x03
646 #define LIGHT_POSITION 0x04
647 #define LIGHT_ATTENUATION 0x05
648 #define LIGHT_HALF 0x06
649 #define LIGHT_SPOT_DIRECTION 0x07
651 /* state light model property */
652 #define LIGHT_MODEL_AMBIENT 0x01
653 #define LIGHT_MODEL_SCENECOLOR 0x02
655 /* state light product property */
656 #define LIGHT_PROD_AMBIENT 0x01
657 #define LIGHT_PROD_DIFFUSE 0x02
658 #define LIGHT_PROD_SPECULAR 0x03
660 /* state texture environment property */
661 #define TEX_ENV_COLOR 0x01
663 /* state texture generation coord property */
664 #define TEX_GEN_EYE 0x01
665 #define TEX_GEN_OBJECT 0x02
667 /* state fog property */
668 #define FOG_COLOR 0x01
669 #define FOG_PARAMS 0x02
671 /* state depth property */
672 #define DEPTH_RANGE 0x01
674 /* state point parameters property */
675 #define POINT_SIZE 0x01
676 #define POINT_ATTENUATION 0x02
684 /* vertex program 1.0 only */
688 memory management routines
690 static GLvoid
*mem_alloc (GLsizei
);
691 static GLvoid
mem_free (GLvoid
**);
692 static GLvoid
*mem_realloc (GLvoid
*, GLsizei
, GLsizei
);
693 static GLubyte
*str_duplicate (const GLubyte
*);
696 internal error messages
698 static const GLubyte
*OUT_OF_MEMORY
=
699 (GLubyte
*) "internal error 1001: out of physical memory";
700 static const GLubyte
*UNRESOLVED_REFERENCE
=
701 (GLubyte
*) "internal error 1002: unresolved reference '$'";
703 static const GLubyte *INVALID_PARAMETER =
704 (GLubyte *) "internal error 1003: invalid parameter";
707 static const GLubyte
*error_message
= NULL
;
708 static GLubyte
*error_param
= NULL
; /* this is inserted into error_message in place of $ */
709 static GLint error_position
= -1;
711 static GLubyte
*unknown
= (GLubyte
*) "???";
716 /* reset error message */
717 error_message
= NULL
;
719 /* free error parameter - if error_param is a "???" don't free it - it's static */
720 if (error_param
!= unknown
)
721 mem_free ((GLvoid
**) & error_param
);
725 /* reset error position */
730 set_last_error (const GLubyte
* msg
, GLubyte
* param
, GLint pos
)
732 if (error_message
!= NULL
)
739 error_param
= unknown
;
741 error_position
= pos
;
745 * memory management routines
748 mem_alloc (GLsizei size
)
750 GLvoid
*ptr
= _mesa_malloc (size
);
752 set_last_error (OUT_OF_MEMORY
, NULL
, -1);
757 mem_free (GLvoid
** ptr
)
764 mem_realloc (GLvoid
* ptr
, GLsizei old_size
, GLsizei new_size
)
766 GLvoid
*ptr2
= _mesa_realloc (ptr
, old_size
, new_size
);
768 set_last_error (OUT_OF_MEMORY
, NULL
, -1);
773 str_duplicate (const GLubyte
* str
)
775 return (GLubyte
*) _mesa_strdup ((const char *) str
);
781 typedef enum emit_type_
783 et_byte
, /* explicit number */
784 et_stream
, /* eaten character */
785 et_position
/* current position */
794 emit_type m_emit_type
;
795 GLubyte m_byte
; /* et_byte */
796 struct emit_
*m_next
;
801 emit_create (emit
** em
)
803 *em
= (emit
*) mem_alloc (sizeof (emit
));
805 (**em
).m_emit_type
= et_byte
;
807 (**em
).m_next
= NULL
;
812 emit_destroy (emit
** em
)
815 emit_destroy (&(**em
).m_next
);
816 mem_free ((GLvoid
**) em
);
821 emit_append (emit
** em
, emit
** ne
)
824 emit_append (&(**em
).m_next
, ne
);
832 typedef struct error_
835 GLubyte
*m_token_name
;
836 struct defntn_
*m_token
;
841 error_create (error
** er
)
843 *er
= (error
*) mem_alloc (sizeof (error
));
845 (**er
).m_text
= NULL
;
846 (**er
).m_token_name
= NULL
;
847 (**er
).m_token
= NULL
;
852 error_destroy (error
** er
)
855 mem_free ((GLvoid
**) & (**er
).m_text
);
856 mem_free ((GLvoid
**) & (**er
).m_token_name
);
857 mem_free ((GLvoid
**) er
);
862 static GLubyte
*error_get_token (error
*, struct dict_
*, const GLubyte
*, GLuint
);
865 * specifier type typedef
867 typedef enum spec_type_
885 spec_type m_spec_type
;
886 GLubyte m_byte
[2]; /* st_byte, st_byte_range */
887 GLubyte
*m_string
; /* st_string */
888 struct defntn_
*m_defntn
; /* st_identifier, st_identifier_loop */
891 struct spec_
*m_next
;
896 spec_create (spec
** sp
)
898 *sp
= (spec
*) mem_alloc (sizeof (spec
));
900 (**sp
).m_spec_type
= st_false
;
901 (**sp
).m_byte
[0] = '\0';
902 (**sp
).m_byte
[1] = '\0';
903 (**sp
).m_string
= NULL
;
904 (**sp
).m_defntn
= NULL
;
905 (**sp
).m_emits
= NULL
;
906 (**sp
).m_errtext
= NULL
;
907 (**sp
).m_next
= NULL
;
912 spec_destroy (spec
** sp
)
915 spec_destroy (&(**sp
).m_next
);
916 emit_destroy (&(**sp
).m_emits
);
917 error_destroy (&(**sp
).m_errtext
);
918 mem_free ((GLvoid
**) & (**sp
).m_string
);
919 mem_free ((GLvoid
**) sp
);
924 spec_append (spec
** sp
, spec
** ns
)
927 spec_append (&(**sp
).m_next
, ns
);
946 typedef struct defntn_
950 struct defntn_
*m_next
;
958 defntn_create (defntn
** de
)
960 *de
= (defntn
*) mem_alloc (sizeof (defntn
));
962 (**de
).m_oper
= op_none
;
963 (**de
).m_specs
= NULL
;
964 (**de
).m_next
= NULL
;
966 (**de
).m_referenced
= 0;
972 defntn_destroy (defntn
** de
)
975 defntn_destroy (&(**de
).m_next
);
976 spec_destroy (&(**de
).m_specs
);
977 mem_free ((GLvoid
**) de
);
982 defntn_append (defntn
** de
, defntn
** nd
)
985 defntn_append (&(**de
).m_next
, nd
);
998 struct dict_
*m_next
;
1003 dict_create (dict
** di
)
1005 *di
= (dict
*) mem_alloc (sizeof (dict
));
1007 (**di
).m_defntns
= NULL
;
1008 (**di
).m_syntax
= NULL
;
1009 (**di
).m_string
= NULL
;
1010 (**di
).m_next
= NULL
;
1015 dict_destroy (dict
** di
)
1018 dict_destroy (&(**di
).m_next
);
1019 defntn_destroy (&(**di
).m_defntns
);
1020 mem_free ((GLvoid
**) di
);
1025 * GLubyte array typedef
1027 typedef struct barray_
1035 barray_create (barray
** ba
)
1037 *ba
= (barray
*) mem_alloc (sizeof (barray
));
1045 barray_destroy (barray
** ba
)
1048 mem_free ((GLvoid
**) & (**ba
).data
);
1049 mem_free ((GLvoid
**) ba
);
1054 * reallocates GLubyte array to requested size,
1055 * returns 0 on success,
1056 * returns 1 otherwise
1059 barray_resize (barray
** ba
, GLuint nlen
)
1061 GLubyte
*new_pointer
;
1064 mem_free ((void **) &(**ba
).data
);
1071 new_pointer
= (GLubyte
*)
1072 mem_realloc ((**ba
).data
, (**ba
).len
* sizeof (GLubyte
),
1073 nlen
* sizeof (GLubyte
));
1075 (**ba
).data
= new_pointer
;
1086 * adds GLubyte array pointed by *nb to the end of array pointed by *ba,
1087 * returns 0 on success,
1088 * returns 1 otherwise
1091 barray_append (barray
** ba
, barray
** nb
)
1094 const GLuint len
= (**ba
).len
;
1096 if (barray_resize (ba
, (**ba
).len
+ (**nb
).len
))
1099 for (i
= 0; i
< (**nb
).len
; i
++)
1100 (**ba
).data
[len
+ i
] = (**nb
).data
[i
];
1107 * Adds emit chain pointed by em to the end of array pointed by *ba.
1108 * \return 0 on success, 1 otherwise.
1111 barray_push (barray
** ba
, emit
* em
, GLubyte c
, GLuint pos
)
1117 if (temp
->m_emit_type
== et_position
)
1118 count
+= 4; /* position is a 32-bit unsigned integer */
1122 temp
= temp
->m_next
;
1125 if (barray_resize (ba
, (**ba
).len
+ count
))
1129 if (em
->m_emit_type
== et_byte
)
1130 (**ba
).data
[(**ba
).len
- count
--] = em
->m_byte
;
1131 else if (em
->m_emit_type
== et_stream
)
1132 (**ba
).data
[(**ba
).len
- count
--] = c
;
1134 /* This is where the position is emitted into the stream */
1135 else { /* em->type == et_position */
1137 (**ba
).data
[(**ba
).len
- count
--] = (GLubyte
) pos
,
1138 (**ba
).data
[(**ba
).len
- count
--] = (GLubyte
) (pos
>> 8),
1139 (**ba
).data
[(**ba
).len
- count
--] = (GLubyte
) (pos
>> 16),
1140 (**ba
).data
[(**ba
).len
- count
--] = (GLubyte
) (pos
>> 24);
1142 (**ba
).data
[(**ba
).len
- count
--] = (GLubyte
) pos
;
1143 (**ba
).data
[(**ba
).len
- count
--] = (GLubyte
) (pos
/ 0x100);
1144 (**ba
).data
[(**ba
).len
- count
--] = (GLubyte
) (pos
/ 0x10000);
1145 (**ba
).data
[(**ba
).len
- count
--] = (GLubyte
) (pos
/ 0x1000000);
1156 * string to string map typedef
1158 typedef struct map_str_
1162 struct map_str_
*next
;
1167 map_str_create (map_str
** ma
)
1169 *ma
= (map_str
*) mem_alloc (sizeof (map_str
));
1178 map_str_destroy (map_str
** ma
)
1181 map_str_destroy (&(**ma
).next
);
1182 mem_free ((GLvoid
**) & (**ma
).key
);
1183 mem_free ((GLvoid
**) & (**ma
).data
);
1184 mem_free ((GLvoid
**) ma
);
1189 map_str_append (map_str
** ma
, map_str
** nm
)
1192 map_str_append (&(**ma
).next
, nm
);
1198 * searches the map for specified key,
1199 * if the key is matched, *data is filled with data associated with the key,
1200 * \return 0 if the key is matched, 1 otherwise
1203 map_str_find (map_str
** ma
, const GLubyte
* key
, GLubyte
** data
)
1206 if (strcmp ((const char *) (**ma
).key
, (const char *) key
) == 0) {
1207 *data
= str_duplicate ((**ma
).data
);
1217 set_last_error (UNRESOLVED_REFERENCE
, str_duplicate (key
), -1);
1222 * string to GLubyte map typedef
1224 typedef struct map_byte_
1228 struct map_byte_
*next
;
1232 map_byte_create (map_byte
** ma
)
1234 *ma
= (map_byte
*) mem_alloc (sizeof (map_byte
));
1243 map_byte_destroy (map_byte
** ma
)
1246 map_byte_destroy (&(**ma
).next
);
1247 mem_free ((GLvoid
**) & (**ma
).key
);
1248 mem_free ((GLvoid
**) ma
);
1253 map_byte_append (map_byte
** ma
, map_byte
** nm
)
1256 map_byte_append (&(**ma
).next
, nm
);
1262 * Searches the map for specified key,
1263 * If the key is matched, *data is filled with data associated with the key,
1264 * \return 0 if the is matched, 1 otherwise
1267 map_byte_find (map_byte
** ma
, const GLubyte
* key
, GLubyte
* data
)
1270 if (strcmp ((const char *) (**ma
).key
, (const char *) key
) == 0) {
1271 *data
= (**ma
).data
;
1278 set_last_error (UNRESOLVED_REFERENCE
, str_duplicate (key
), -1);
1283 * string to defntn map typedef
1285 typedef struct map_def_
1289 struct map_def_
*next
;
1293 map_def_create (map_def
** ma
)
1295 *ma
= (map_def
*) mem_alloc (sizeof (map_def
));
1304 map_def_destroy (map_def
** ma
)
1307 map_def_destroy (&(**ma
).next
);
1308 mem_free ((GLvoid
**) & (**ma
).key
);
1309 mem_free ((GLvoid
**) ma
);
1314 map_def_append (map_def
** ma
, map_def
** nm
)
1317 map_def_append (&(**ma
).next
, nm
);
1323 * searches the map for specified key,
1324 * if the key is matched, *data is filled with data associated with the key,
1325 * \return 0 if the is matched, 1 otherwise
1328 map_def_find (map_def
** ma
, const GLubyte
* key
, defntn
** data
)
1331 if (_mesa_strcmp ((const char *) (**ma
).key
, (const char *) key
) == 0) {
1332 *data
= (**ma
).data
;
1340 set_last_error (UNRESOLVED_REFERENCE
, str_duplicate (key
), -1);
1345 * returns 1 if given character is a space,
1346 * returns 0 otherwise
1349 is_space (GLubyte c
)
1351 return c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r';
1355 * advances text pointer by 1 if character pointed by *text is a space,
1356 * returns 1 if a space has been eaten,
1357 * returns 0 otherwise
1360 eat_space (const GLubyte
** text
)
1362 if (is_space (**text
)) {
1372 * returns 1 if text points to C-style comment start string "/ *",
1373 * returns 0 otherwise
1376 is_comment_start (const GLubyte
* text
)
1378 return text
[0] == '/' && text
[1] == '*';
1382 * advances text pointer to first character after C-style comment block - if any,
1383 * returns 1 if C-style comment block has been encountered and eaten,
1384 * returns 0 otherwise
1387 eat_comment (const GLubyte
** text
)
1389 if (is_comment_start (*text
)) {
1390 /* *text points to comment block - skip two characters to enter comment body */
1392 /* skip any character except consecutive '*' and '/' */
1393 while (!((*text
)[0] == '*' && (*text
)[1] == '/'))
1395 /* skip those two terminating characters */
1405 * advances text pointer to first character that is neither space nor C-style comment block
1408 eat_spaces (const GLubyte
** text
)
1410 while (eat_space (text
) || eat_comment (text
));
1414 * resizes string pointed by *ptr to successfully add character c to the end of the string,
1415 * returns 0 on success,
1416 * returns 1 otherwise
1419 string_grow (GLubyte
** ptr
, GLuint
* len
, GLubyte c
)
1421 /* reallocate the string in 16-length increments */
1422 if ((*len
& 0x0F) == 0x0F || *ptr
== NULL
) {
1423 GLubyte
*tmp
= (GLubyte
*) mem_realloc (*ptr
, (*len
) * sizeof (GLubyte
),
1425 0x0F) & ~0x0F) * sizeof (GLubyte
));
1433 /* append given character */
1437 (*ptr
)[*len
] = '\0';
1443 * returns 1 if given character is valid identifier character a-z, A-Z, 0-9 or _
1444 * returns 0 otherwise
1447 is_identifier (GLubyte c
)
1449 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') ||
1450 (c
>= '0' && c
<= '9') || c
== '_';
1454 * copies characters from *text to *id until non-identifier character is encountered,
1455 * assumes that *id points to NULL object - caller is responsible for later freeing the string,
1456 * text pointer is advanced to point past the copied identifier,
1457 * returns 0 if identifier was successfully copied,
1458 * returns 1 otherwise
1461 get_identifier (const GLubyte
** text
, GLubyte
** id
)
1463 const GLubyte
*t
= *text
;
1467 if (string_grow (&p
, &len
, '\0'))
1470 /* loop while next character in buffer is valid for identifiers */
1471 while (is_identifier (*t
)) {
1472 if (string_grow (&p
, &len
, *t
++)) {
1473 mem_free ((GLvoid
**) & p
);
1485 * returns 1 if given character is HEX digit 0-9, A-F or a-f,
1486 * returns 0 otherwise
1491 return (c
>= '0' && c
<= '9') || (c
>= 'A' && c
<= 'F') || (c
>= 'a'
1496 * returns value of passed character as if it was HEX digit
1501 if (c
>= '0' && c
<= '9')
1503 if (c
>= 'A' && c
<= 'F')
1504 return c
- 'A' + 10;
1505 return c
- 'a' + 10;
1509 * converts sequence of HEX digits pointed by *text until non-HEX digit is encountered,
1510 * advances text pointer past the converted sequence,
1511 * returns the converted value
1514 hex_convert (const GLubyte
** text
)
1518 while (is_hex (**text
)) {
1519 value
= value
* 0x10 + hex2dec (**text
);
1527 * returns 1 if given character is OCT digit 0-7,
1528 * returns 0 otherwise
1533 return c
>= '0' && c
<= '7';
1537 * returns value of passed character as if it was OCT digit
1546 get_escape_sequence (const GLubyte
** text
)
1550 /* skip '\' character */
1553 switch (*(*text
)++) {
1577 return (GLubyte
) hex_convert (text
);
1581 if (is_oct (**text
)) {
1582 value
= oct2dec (*(*text
)++);
1583 if (is_oct (**text
)) {
1584 value
= value
* 010 + oct2dec (*(*text
)++);
1585 if (is_oct (**text
))
1586 value
= value
* 010 + oct2dec (*(*text
)++);
1590 return (GLubyte
) value
;
1594 * copies characters from *text to *str until " or ' character is encountered,
1595 * assumes that *str points to NULL object - caller is responsible for later freeing the string,
1596 * assumes that *text points to " or ' character that starts the string,
1597 * text pointer is advanced to point past the " or ' character,
1598 * returns 0 if string was successfully copied,
1599 * returns 1 otherwise
1602 get_string (const GLubyte
** text
, GLubyte
** str
)
1604 const GLubyte
*t
= *text
;
1609 if (string_grow (&p
, &len
, '\0'))
1612 /* read " or ' character that starts the string */
1614 /* while next character is not the terminating character */
1615 while (*t
&& *t
!= term_char
) {
1619 c
= get_escape_sequence (&t
);
1623 if (string_grow (&p
, &len
, c
)) {
1624 mem_free ((GLvoid
**) & p
);
1629 /* skip " or ' character that ends the string */
1638 * gets emit code, the syntax is: ".emtcode" " " <symbol> " " ("0x" | "0X") <hex_value>
1639 * assumes that *text already points to <symbol>,
1640 * returns 0 if emit code is successfully read,
1641 * returns 1 otherwise
1644 get_emtcode (const GLubyte
** text
, map_byte
** ma
)
1646 const GLubyte
*t
= *text
;
1649 map_byte_create (&m
);
1653 if (get_identifier (&t
, &m
->key
)) {
1654 map_byte_destroy (&m
);
1662 if (get_string (&t
, &c
)) {
1663 map_byte_destroy (&m
);
1667 m
->data
= (GLubyte
) c
[0];
1668 mem_free ((GLvoid
**) & c
);
1671 /* skip HEX "0x" or "0X" prefix */
1673 m
->data
= (GLubyte
) hex_convert (&t
);
1684 * returns 0 on success,
1685 * returns 1 otherwise
1688 get_errtext (const GLubyte
** text
, map_str
** ma
)
1690 const GLubyte
*t
= *text
;
1693 map_str_create (&m
);
1697 if (get_identifier (&t
, &m
->key
)) {
1698 map_str_destroy (&m
);
1703 if (get_string (&t
, &m
->data
)) {
1704 map_str_destroy (&m
);
1715 * returns 0 on success,
1716 * returns 1 otherwise,
1719 get_error (const GLubyte
** text
, error
** er
, map_str
* maps
)
1721 const GLubyte
*t
= *text
;
1722 GLubyte
*temp
= NULL
;
1728 if (get_identifier (&t
, &temp
))
1732 if (_mesa_strcmp ("error", (char *) temp
) != 0) {
1733 mem_free ((GLvoid
**) & temp
);
1737 mem_free ((GLvoid
**) & temp
);
1744 if (get_string (&t
, &(**er
).m_text
)) {
1751 if (get_identifier (&t
, &temp
)) {
1757 if (map_str_find (&maps
, temp
, &(**er
).m_text
)) {
1758 mem_free ((GLvoid
**) & temp
);
1763 mem_free ((GLvoid
**) & temp
);
1766 /* try to extract "token" from "...$token$..." */
1768 char *processed
= NULL
;
1769 GLuint len
= 0, i
= 0;
1771 if (string_grow ((GLubyte
**) (&processed
), &len
, '\0')) {
1776 while (i
< _mesa_strlen ((char *) ((**er
).m_text
))) {
1777 /* check if the dollar sign is repeated - if so skip it */
1778 if ((**er
).m_text
[i
] == '$' && (**er
).m_text
[i
+ 1] == '$') {
1779 if (string_grow ((GLubyte
**) (&processed
), &len
, '$')) {
1780 mem_free ((GLvoid
**) & processed
);
1787 else if ((**er
).m_text
[i
] != '$') {
1788 if (string_grow ((GLubyte
**) (&processed
), &len
, (**er
).m_text
[i
])) {
1789 mem_free ((GLvoid
**) & processed
);
1797 if (string_grow ((GLubyte
**) (&processed
), &len
, '$')) {
1798 mem_free ((GLvoid
**) & processed
);
1804 /* length of token being extracted */
1807 if (string_grow (&(**er
).m_token_name
, &tlen
, '\0')) {
1808 mem_free ((GLvoid
**) & processed
);
1813 /* skip the dollar sign */
1816 while ((**er
).m_text
[i
] != '$') {
1818 (&(**er
).m_token_name
, &tlen
, (**er
).m_text
[i
])) {
1819 mem_free ((GLvoid
**) & processed
);
1827 /* skip the dollar sign */
1833 mem_free ((GLvoid
**) & (**er
).m_text
);
1834 (**er
).m_text
= (GLubyte
*) processed
;
1842 * returns 0 on success,
1843 * returns 1 otherwise,
1846 get_emits (const GLubyte
** text
, emit
** em
, map_byte
* mapb
)
1848 const GLubyte
*t
= *text
;
1849 GLubyte
*temp
= NULL
;
1856 if (get_identifier (&t
, &temp
))
1861 if (_mesa_strcmp ("emit", (char *) temp
) != 0) {
1862 mem_free ((GLvoid
**) & temp
);
1866 mem_free ((GLvoid
**) & temp
);
1875 e
->m_byte
= (GLubyte
) hex_convert (&t
);
1877 e
->m_emit_type
= et_byte
;
1880 else if (*t
== '*') {
1883 e
->m_emit_type
= et_stream
;
1886 else if (*t
== '$') {
1889 e
->m_emit_type
= et_position
;
1892 else if (*t
== '\'') {
1893 if (get_string (&t
, &temp
)) {
1897 e
->m_byte
= (GLubyte
) temp
[0];
1899 mem_free ((GLvoid
**) & temp
);
1901 e
->m_emit_type
= et_byte
;
1904 if (get_identifier (&t
, &temp
)) {
1909 if (map_byte_find (&mapb
, temp
, &e
->m_byte
)) {
1910 mem_free ((GLvoid
**) & temp
);
1915 mem_free ((GLvoid
**) & temp
);
1917 e
->m_emit_type
= et_byte
;
1922 if (get_emits (&t
, &e
->m_next
, mapb
)) {
1933 * returns 0 on success,
1934 * returns 1 otherwise,
1937 get_spec (const GLubyte
** text
, spec
** sp
, map_str
* maps
, map_byte
* mapb
)
1939 const GLubyte
*t
= *text
;
1947 GLubyte
*temp
= NULL
;
1949 if (get_string (&t
, &temp
)) {
1956 GLubyte
*temp2
= NULL
;
1958 /* skip the '-' character */
1962 if (get_string (&t
, &temp2
)) {
1963 mem_free ((GLvoid
**) & temp
);
1969 s
->m_spec_type
= st_byte_range
;
1970 s
->m_byte
[0] = *temp
;
1971 s
->m_byte
[1] = *temp2
;
1973 mem_free ((GLvoid
**) & temp2
);
1976 s
->m_spec_type
= st_byte
;
1980 mem_free ((GLvoid
**) & temp
);
1982 else if (*t
== '"') {
1983 if (get_string (&t
, &s
->m_string
)) {
1989 s
->m_spec_type
= st_string
;
1991 else if (*t
== '.') {
1992 GLubyte
*keyword
= NULL
;
1997 if (get_identifier (&t
, &keyword
)) {
2004 if (_mesa_strcmp ("true", (char *) keyword
) == 0) {
2005 s
->m_spec_type
= st_true
;
2008 else if (_mesa_strcmp ("false", (char *) keyword
) == 0) {
2009 s
->m_spec_type
= st_false
;
2012 else if (_mesa_strcmp ("debug", (char *) keyword
) == 0) {
2013 s
->m_spec_type
= st_debug
;
2016 else if (_mesa_strcmp ("loop", (char *) keyword
) == 0) {
2017 if (get_identifier (&t
, &s
->m_string
)) {
2018 mem_free ((GLvoid
**) & keyword
);
2024 s
->m_spec_type
= st_identifier_loop
;
2027 mem_free ((GLvoid
**) & keyword
);
2030 if (get_identifier (&t
, &s
->m_string
)) {
2036 s
->m_spec_type
= st_identifier
;
2039 if (get_error (&t
, &s
->m_errtext
, maps
)) {
2044 if (get_emits (&t
, &s
->m_emits
, mapb
)) {
2055 * returns 0 on success,
2056 * returns 1 otherwise,
2059 get_definition (const GLubyte
** text
, defntn
** de
, map_str
* maps
,
2062 const GLubyte
*t
= *text
;
2069 if (get_spec (&t
, &d
->m_specs
, maps
, mapb
)) {
2070 defntn_destroy (&d
);
2078 /* skip the dot that precedes "and" or "or" */
2081 /* read "and" or "or" keyword */
2082 if (get_identifier (&t
, &op
)) {
2083 defntn_destroy (&d
);
2088 if (d
->m_oper
== op_none
) {
2090 if (_mesa_strcmp ("and", (char *) op
) == 0)
2097 mem_free ((GLvoid
**) & op
);
2099 if (get_spec (&t
, &sp
, maps
, mapb
)) {
2100 defntn_destroy (&d
);
2104 spec_append (&d
->m_specs
, &sp
);
2107 /* skip the semicolon */
2117 * returns 0 on success,
2118 * returns 1 otherwise,
2121 update_dependency (map_def
* mapd
, GLubyte
* symbol
, defntn
** def
)
2123 if (map_def_find (&mapd
, symbol
, def
))
2127 (**def
).m_referenced
= 1;
2134 * returns 0 on success,
2135 * returns 1 otherwise,
2138 update_dependencies (dict
* di
, map_def
* mapd
, GLubyte
** syntax_symbol
,
2139 GLubyte
** string_symbol
)
2141 defntn
*de
= di
->m_defntns
;
2143 if (update_dependency (mapd
, *syntax_symbol
, &di
->m_syntax
) ||
2144 (*string_symbol
!= NULL
2145 && update_dependency (mapd
, *string_symbol
, &di
->m_string
)))
2148 mem_free ((GLvoid
**) syntax_symbol
);
2149 mem_free ((GLvoid
**) string_symbol
);
2152 spec
*sp
= de
->m_specs
;
2155 if (sp
->m_spec_type
== st_identifier
2156 || sp
->m_spec_type
== st_identifier_loop
) {
2157 if (update_dependency (mapd
, sp
->m_string
, &sp
->m_defntn
))
2160 mem_free ((GLvoid
**) & sp
->m_string
);
2163 if (sp
->m_errtext
&& sp
->m_errtext
->m_token_name
) {
2164 if (update_dependency
2165 (mapd
, sp
->m_errtext
->m_token_name
, &sp
->m_errtext
->m_token
))
2168 mem_free ((GLvoid
**) & sp
->m_errtext
->m_token_name
);
2180 typedef enum match_result_
2182 mr_not_matched
, /* the examined string does not match */
2183 mr_matched
, /* the examined string matches */
2184 mr_error_raised
, /* mr_not_matched + error has been raised */
2185 mr_dont_emit
, /* used by identifier loops only */
2186 mr_internal_error
/* an internal error has occured such as out of memory */
2190 match (dict
* di
, const GLubyte
* text
, GLuint
* index
, defntn
* de
,
2191 barray
** ba
, GLint filtering_string
)
2193 GLuint ind
= *index
;
2194 match_result status
= mr_not_matched
;
2195 spec
*sp
= de
->m_specs
;
2197 /* for every specifier in the definition */
2199 GLuint i
, len
, save_ind
= ind
;
2200 barray
*array
= NULL
;
2202 switch (sp
->m_spec_type
) {
2204 barray_create (&array
);
2206 return mr_internal_error
;
2209 match (di
, text
, &ind
, sp
->m_defntn
, &array
, filtering_string
);
2210 if (status
== mr_internal_error
) {
2211 barray_destroy (&array
);
2212 return mr_internal_error
;
2216 len
= _mesa_strlen ((char *) (sp
->m_string
));
2218 /* prefilter the stream */
2219 if (!filtering_string
&& di
->m_string
) {
2221 GLuint filter_index
= 0;
2222 match_result result
;
2224 barray_create (&ba
);
2226 return mr_internal_error
;
2229 match (di
, text
+ ind
, &filter_index
, di
->m_string
, &ba
, 1);
2231 if (result
== mr_internal_error
) {
2232 barray_destroy (&ba
);
2233 return mr_internal_error
;
2236 if (result
!= mr_matched
) {
2237 barray_destroy (&ba
);
2238 status
= mr_not_matched
;
2242 barray_destroy (&ba
);
2244 if (filter_index
!= len
2245 || _mesa_strncmp ((char *)sp
->m_string
, (char *)(text
+ ind
), len
)) {
2246 status
= mr_not_matched
;
2250 status
= mr_matched
;
2254 status
= mr_matched
;
2255 for (i
= 0; status
== mr_matched
&& i
< len
; i
++)
2256 if (text
[ind
+ i
] != sp
->m_string
[i
])
2257 status
= mr_not_matched
;
2258 if (status
== mr_matched
)
2263 status
= text
[ind
] == *sp
->m_byte
? mr_matched
: mr_not_matched
;
2264 if (status
== mr_matched
)
2268 status
= (text
[ind
] >= sp
->m_byte
[0]
2270 sp
->m_byte
[1]) ? mr_matched
: mr_not_matched
;
2271 if (status
== mr_matched
)
2275 status
= mr_matched
;
2278 status
= mr_not_matched
;
2281 status
= mr_matched
;
2283 case st_identifier_loop
:
2284 barray_create (&array
);
2286 return mr_internal_error
;
2288 status
= mr_dont_emit
;
2290 match_result result
;
2294 match (di
, text
, &ind
, sp
->m_defntn
, &array
,
2297 if (result
== mr_error_raised
) {
2301 else if (result
== mr_matched
) {
2302 if (barray_push (ba
, sp
->m_emits
, text
[ind
- 1], save_ind
)
2303 || barray_append (ba
, &array
)) {
2304 barray_destroy (&array
);
2305 return mr_internal_error
;
2307 barray_destroy (&array
);
2308 barray_create (&array
);
2310 return mr_internal_error
;
2312 else if (result
== mr_internal_error
) {
2313 barray_destroy (&array
);
2314 return mr_internal_error
;
2322 if (status
== mr_error_raised
) {
2323 barray_destroy (&array
);
2325 return mr_error_raised
;
2328 if (de
->m_oper
== op_and
&& status
!= mr_matched
2329 && status
!= mr_dont_emit
) {
2330 barray_destroy (&array
);
2332 if (sp
->m_errtext
) {
2333 set_last_error (sp
->m_errtext
->m_text
,
2334 error_get_token (sp
->m_errtext
, di
, text
, ind
),
2337 return mr_error_raised
;
2340 return mr_not_matched
;
2343 if (status
== mr_matched
) {
2345 if (barray_push (ba
, sp
->m_emits
, text
[ind
- 1], save_ind
)) {
2346 barray_destroy (&array
);
2347 return mr_internal_error
;
2351 if (barray_append (ba
, &array
)) {
2352 barray_destroy (&array
);
2353 return mr_internal_error
;
2357 barray_destroy (&array
);
2359 if (de
->m_oper
== op_or
2360 && (status
== mr_matched
|| status
== mr_dont_emit
)) {
2368 if (de
->m_oper
== op_and
2369 && (status
== mr_matched
|| status
== mr_dont_emit
)) {
2374 return mr_not_matched
;
2378 error_get_token (error
* er
, dict
* di
, const GLubyte
* text
, unsigned int ind
)
2380 GLubyte
*str
= NULL
;
2384 GLuint filter_index
= 0;
2386 barray_create (&ba
);
2388 if (match (di
, text
+ ind
, &filter_index
, er
->m_token
, &ba
, 0) ==
2389 mr_matched
&& filter_index
) {
2390 str
= (GLubyte
*) mem_alloc (filter_index
+ 1);
2392 _mesa_strncpy ((char *) str
, (char *) (text
+ ind
),
2394 str
[filter_index
] = '\0';
2397 barray_destroy (&ba
);
2404 typedef struct grammar_load_state_
2407 GLubyte
*syntax_symbol
;
2408 GLubyte
*string_symbol
;
2412 } grammar_load_state
;
2416 grammar_load_state_create (grammar_load_state
** gr
)
2418 *gr
= (grammar_load_state
*) mem_alloc (sizeof (grammar_load_state
));
2421 (**gr
).syntax_symbol
= NULL
;
2422 (**gr
).string_symbol
= NULL
;
2430 grammar_load_state_destroy (grammar_load_state
** gr
)
2433 dict_destroy (&(**gr
).di
);
2434 mem_free ((GLvoid
**) &(**gr
).syntax_symbol
);
2435 mem_free ((GLvoid
**) &(**gr
).string_symbol
);
2436 map_str_destroy (&(**gr
).maps
);
2437 map_byte_destroy (&(**gr
).mapb
);
2438 map_def_destroy (&(**gr
).mapd
);
2439 mem_free ((GLvoid
**) gr
);
2448 * loads grammar script from null-terminated ASCII text
2449 * returns the grammar object
2450 * returns NULL if an error occurs (call grammar_get_last_error to retrieve the error text)
2454 grammar_load_from_text (const GLubyte
* text
)
2457 grammar_load_state
*g
= NULL
;
2459 clear_last_error ();
2461 grammar_load_state_create (&g
);
2465 dict_create (&g
->di
);
2466 if (g
->di
== NULL
) {
2467 grammar_load_state_destroy (&g
);
2473 /* skip ".syntax" keyword */
2477 /* retrieve root symbol */
2478 if (get_identifier (&text
, &g
->syntax_symbol
)) {
2479 grammar_load_state_destroy (&g
);
2484 /* skip semicolon */
2489 GLubyte
*symbol
= NULL
;
2490 GLint is_dot
= *text
== '.';
2495 if (get_identifier (&text
, &symbol
)) {
2496 grammar_load_state_destroy (&g
);
2502 if (is_dot
&& _mesa_strcmp ((char *) symbol
, "emtcode") == 0) {
2503 map_byte
*ma
= NULL
;
2505 mem_free ((void **) &symbol
);
2507 if (get_emtcode (&text
, &ma
)) {
2508 grammar_load_state_destroy (&g
);
2512 map_byte_append (&g
->mapb
, &ma
);
2515 else if (is_dot
&& _mesa_strcmp ((char *) symbol
, "errtext") == 0) {
2518 mem_free ((GLvoid
**) &symbol
);
2520 if (get_errtext (&text
, &ma
)) {
2521 grammar_load_state_destroy (&g
);
2525 map_str_append (&g
->maps
, &ma
);
2528 else if (is_dot
&& _mesa_strcmp ((char *) symbol
, "string") == 0) {
2529 mem_free ((GLvoid
**) (&symbol
));
2531 if (g
->di
->m_string
!= NULL
) {
2532 grammar_load_state_destroy (&g
);
2536 if (get_identifier (&text
, &g
->string_symbol
)) {
2537 grammar_load_state_destroy (&g
);
2541 /* skip semicolon */
2550 if (get_definition (&text
, &de
, g
->maps
, g
->mapb
)) {
2551 grammar_load_state_destroy (&g
);
2555 defntn_append (&g
->di
->m_defntns
, &de
);
2557 /* if definition consist of only one specifier, give it an ".and" operator */
2558 if (de
->m_oper
== op_none
)
2559 de
->m_oper
= op_and
;
2561 map_def_create (&ma
);
2563 grammar_load_state_destroy (&g
);
2569 map_def_append (&g
->mapd
, &ma
);
2573 if (update_dependencies
2574 (g
->di
, g
->mapd
, &g
->syntax_symbol
, &g
->string_symbol
)) {
2575 grammar_load_state_destroy (&g
);
2582 grammar_load_state_destroy (&g
);
2588 * checks if a null-terminated text matches given grammar
2589 * returns 0 on error (call grammar_get_last_error to retrieve the error text)
2590 * returns 1 on success, the prod points to newly allocated buffer with
2591 * production and size is filled with the production size
2593 * \param id - The grammar returned from grammar_load_from_text()
2594 * \param text - The program string
2595 * \param production - The return parameter for the binary array holding the
2597 * \param size - The return parameter for the size of production
2599 * \return 1 on sucess, 0 on parser error
2602 grammar_check (dict
* di
, const GLubyte
* text
, GLubyte
** production
,
2608 clear_last_error ();
2610 barray_create (&ba
);
2617 if (match (di
, text
, &index
, di
->m_syntax
, &ba
, 0) != mr_matched
) {
2618 barray_destroy (&ba
);
2622 *production
= (GLubyte
*) mem_alloc (ba
->len
* sizeof (GLubyte
));
2623 if (*production
== NULL
) {
2624 barray_destroy (&ba
);
2628 memcpy (*production
, ba
->data
, ba
->len
* sizeof (GLubyte
));
2630 barray_destroy (&ba
);
2636 grammar_get_last_error (GLubyte
* text
, GLint size
, GLint
*pos
)
2638 GLint len
= 0, dots_made
= 0;
2639 const GLubyte
*p
= error_message
;
2642 #define APPEND_CHARACTER(x) if (dots_made == 0) {\
2643 if (len < size - 1) {\
2644 text[len++] = (x); text[len] = '\0';\
2647 for (i = 0; i < 3; i++)\
2657 const GLubyte
*r
= error_param
;
2660 APPEND_CHARACTER (*r
)
2667 APPEND_CHARACTER (*p
)
2672 *pos
= error_position
;
2675 /*-----------------------------------------------------------------------
2676 * From here on down is the semantic checking portion
2681 * Variable Table Handling functions
2696 * Setting an explicit field for each of the binding properties is a bit wasteful
2697 * of space, but it should be much more clear when reading later on..
2703 GLuint address_binding
; /* The index of the address register we should
2705 GLuint attrib_binding
; /* For type vt_attrib, see nvfragprog.h for values */
2706 GLuint attrib_binding_idx
; /* The index into the attrib register file corresponding
2707 * to the state in attrib_binding */
2708 GLuint attrib_is_generic
; /* If the attrib was specified through a generic
2710 GLuint temp_binding
; /* The index of the temp register we are to use */
2711 GLuint output_binding
; /* For type vt_output, see nvfragprog.h for values */
2712 GLuint output_binding_idx
; /* This is the index into the result register file
2713 * corresponding to the bound result state */
2714 struct var_cache
*alias_binding
; /* For type vt_alias, points to the var_cache entry
2715 * that this is aliased to */
2716 GLuint param_binding_type
; /* {PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM,
2717 * PROGRAM_ENV_PARAM} */
2718 GLuint param_binding_begin
; /* This is the offset into the program_parameter_list where
2719 * the tokens representing our bound state (or constants)
2721 GLuint param_binding_length
; /* This is how many entries in the the program_parameter_list
2722 * we take up with our state tokens or constants. Note that
2723 * this is _not_ the same as the number of param registers
2724 * we eventually use */
2725 struct var_cache
*next
;
2729 var_cache_create (struct var_cache
**va
)
2731 *va
= (struct var_cache
*) _mesa_malloc (sizeof (struct var_cache
));
2734 (**va
).type
= vt_none
;
2735 (**va
).attrib_binding
= ~0;
2736 (**va
).attrib_is_generic
= 0;
2737 (**va
).temp_binding
= ~0;
2738 (**va
).output_binding
= ~0;
2739 (**va
).output_binding_idx
= ~0;
2740 (**va
).param_binding_type
= ~0;
2741 (**va
).param_binding_begin
= ~0;
2742 (**va
).param_binding_length
= ~0;
2743 (**va
).alias_binding
= NULL
;
2749 var_cache_destroy (struct var_cache
**va
)
2752 var_cache_destroy (&(**va
).next
);
2759 var_cache_append (struct var_cache
**va
, struct var_cache
*nv
)
2762 var_cache_append (&(**va
).next
, nv
);
2767 static struct var_cache
*
2768 var_cache_find (struct var_cache
*va
, GLubyte
* name
)
2770 struct var_cache
*first
= va
;
2773 if (!strcmp ( (const char*) name
, (const char*) va
->name
)) {
2774 if (va
->type
== vt_alias
)
2775 return var_cache_find (first
, va
->name
);
2786 * constructs an integer from 4 GLubytes in LE format
2789 parse_position (GLubyte
** inst
)
2793 value
= (GLuint
) (*(*inst
)++);
2794 value
+= (GLuint
) (*(*inst
)++) * 0x100;
2795 value
+= (GLuint
) (*(*inst
)++) * 0x10000;
2796 value
+= (GLuint
) (*(*inst
)++) * 0x1000000;
2802 * This will, given a string, lookup the string as a variable name in the
2803 * var cache. If the name is found, the var cache node corresponding to the
2804 * var name is returned. If it is not found, a new entry is allocated
2806 * \param I Points into the binary array where the string identifier begins
2807 * \param found 1 if the string was found in the var_cache, 0 if it was allocated
2808 * \return The location on the var_cache corresponding the the string starting at I
2810 static struct var_cache
*
2811 parse_string (GLubyte
** inst
, struct var_cache
**vc_head
,
2812 struct arb_program
*Program
, GLuint
* found
)
2815 struct var_cache
*va
= NULL
;
2817 *inst
+= _mesa_strlen ((char *) i
) + 1;
2819 va
= var_cache_find (*vc_head
, i
);
2827 var_cache_create (&va
);
2830 var_cache_append (vc_head
, va
);
2836 parse_string_without_adding (GLubyte
** inst
, struct arb_program
*Program
)
2840 *inst
+= _mesa_strlen ((char *) i
) + 1;
2846 * \return 0 if sign is plus, 1 if sign is minus
2849 parse_sign (GLubyte
** inst
)
2851 /*return *(*inst)++ != '+'; */
2853 if (**inst
== '-') {
2857 else if (**inst
== '+') {
2866 * parses and returns signed integer
2869 parse_integer (GLubyte
** inst
, struct arb_program
*Program
)
2874 /* check if *inst points to '+' or '-'
2875 * if yes, grab the sign and increment *inst
2877 sign
= parse_sign (inst
);
2879 /* now check if *inst points to 0
2880 * if yes, increment the *inst and return the default value
2887 /* parse the integer as you normally would do it */
2888 value
= _mesa_atoi (parse_string_without_adding (inst
, Program
));
2890 /* now, after terminating 0 there is a position
2891 * to parse it - parse_position()
2893 Program
->Position
= parse_position (inst
);
2904 parse_float (GLubyte
** inst
, struct arb_program
*Program
)
2906 GLint tmp
[5], denom
;
2910 tmp
[0] = parse_sign (inst
); /* This is the sign of the number + - >0, - -> 1 */
2912 tmp
[1] = parse_integer (inst
, Program
); /* This is the integer portion of the number */
2913 tmp
[2] = parse_integer (inst
, Program
); /* This is the fractional portion of the number */
2914 tmp
[3] = parse_sign (inst
); /* This is the sign of the exponent */
2915 tmp
[4] = parse_integer (inst
, Program
); /* This is the exponent */
2917 value
= (GLfloat
) tmp
[1];
2919 while (denom
< tmp
[2])
2921 value
+= (GLfloat
) tmp
[2] / (GLfloat
) denom
;
2926 value
*= _mesa_pow (10, (GLfloat
) tmp
[3] * (GLfloat
) tmp
[4]);
2934 parse_signed_float (GLubyte
** inst
, struct arb_program
*Program
)
2939 negate
= parse_sign (inst
);
2941 value
= parse_float (inst
, Program
);
2950 * This picks out a constant value from the parsed array. The constant vector is r
2951 * returned in the *values array, which should be of length 4.
2953 * \param values - The 4 component vector with the constant value in it
2956 parse_constant (GLubyte
** inst
, GLfloat
*values
, struct arb_program
*Program
,
2959 GLuint components
, i
;
2962 switch (*(*inst
)++) {
2963 case CONSTANT_SCALAR
:
2964 if (use
== GL_TRUE
) {
2967 values
[2] = values
[3] = parse_float (inst
, Program
);
2972 values
[2] = values
[3] = parse_signed_float (inst
, Program
);
2976 case CONSTANT_VECTOR
:
2977 values
[0] = values
[1] = values
[2] = 0;
2979 components
= *(*inst
)++;
2980 for (i
= 0; i
< components
; i
++) {
2981 values
[i
] = parse_signed_float (inst
, Program
);
2989 * \param color 0 if color type is primary, 1 if color type is secondary
2990 * \return 0 on sucess, 1 on error
2993 parse_color_type (GLcontext
* ctx
, GLubyte
** inst
, struct arb_program
*Program
,
2996 *color
= *(*inst
)++ != COLOR_PRIMARY
;
3001 * Get an integer corresponding to a generic vertex attribute.
3003 * \return 0 on sucess, 1 on error
3006 parse_generic_attrib_num(GLcontext
*ctx
, GLubyte
** inst
,
3007 struct arb_program
*Program
, GLuint
*attrib
)
3009 *attrib
= parse_integer(inst
, Program
);
3011 if ((*attrib
< 0) || (*attrib
> MAX_VERTEX_PROGRAM_ATTRIBS
))
3013 _mesa_set_program_error (ctx
, Program
->Position
,
3014 "Invalid generic vertex attribute index");
3015 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid generic vertex attribute index");
3025 * \param coord The texture unit index
3026 * \return 0 on sucess, 1 on error
3029 parse_texcoord_num (GLcontext
* ctx
, GLubyte
** inst
,
3030 struct arb_program
*Program
, GLuint
* coord
)
3032 *coord
= parse_integer (inst
, Program
);
3034 if ((*coord
< 0) || (*coord
>= ctx
->Const
.MaxTextureUnits
)) {
3035 _mesa_set_program_error (ctx
, Program
->Position
,
3036 "Invalid texture unit index");
3037 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid texture unit index");
3045 * \param coord The weight index
3046 * \return 0 on sucess, 1 on error
3049 parse_weight_num (GLcontext
* ctx
, GLubyte
** inst
, struct arb_program
*Program
,
3052 *coord
= parse_integer (inst
, Program
);
3054 if ((*coord
< 0) || (*coord
>= 1)) {
3055 _mesa_set_program_error (ctx
, Program
->Position
,
3056 "Invalid weight index");
3057 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid weight index");
3065 * \param coord The clip plane index
3066 * \return 0 on sucess, 1 on error
3069 parse_clipplane_num (GLcontext
* ctx
, GLubyte
** inst
,
3070 struct arb_program
*Program
, GLint
* coord
)
3072 *coord
= parse_integer (inst
, Program
);
3074 if ((*coord
< 0) || (*coord
>= (GLint
) ctx
->Const
.MaxClipPlanes
)) {
3075 _mesa_set_program_error (ctx
, Program
->Position
,
3076 "Invalid clip plane index");
3077 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Invalid clip plane index");
3086 * \return 0 on front face, 1 on back face
3089 parse_face_type (GLubyte
** inst
)
3091 switch (*(*inst
)++) {
3103 * Given a matrix and a modifier token on the binary array, return tokens
3104 * that _mesa_fetch_state() [program.c] can understand.
3106 * \param matrix - the matrix we are talking about
3107 * \param matrix_idx - the index of the matrix we have (for texture & program matricies)
3108 * \param matrix_modifier - the matrix modifier (trans, inv, etc)
3109 * \return 0 on sucess, 1 on failure
3112 parse_matrix (GLcontext
* ctx
, GLubyte
** inst
, struct arb_program
*Program
,
3113 GLint
* matrix
, GLint
* matrix_idx
, GLint
* matrix_modifier
)
3115 GLubyte mat
= *(*inst
)++;
3120 case MATRIX_MODELVIEW
:
3121 *matrix
= STATE_MODELVIEW
;
3122 *matrix_idx
= parse_integer (inst
, Program
);
3123 /* XXX: if (*matrix_idx >= ctx->Const. */
3126 case MATRIX_PROJECTION
:
3127 *matrix
= STATE_PROJECTION
;
3131 *matrix
= STATE_MVP
;
3134 case MATRIX_TEXTURE
:
3135 *matrix
= STATE_TEXTURE
;
3136 *matrix_idx
= parse_integer (inst
, Program
);
3137 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxTextureUnits
) {
3138 _mesa_set_program_error (ctx
, Program
->Position
,
3139 "Invalid Texture Unit");
3140 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3141 "Invalid Texture Unit: %d", *matrix_idx
);
3146 /* XXX: How should we handle the palette matrix? */
3147 case MATRIX_PALETTE
:
3148 *matrix_idx
= parse_integer (inst
, Program
);
3151 case MATRIX_PROGRAM
:
3152 *matrix
= STATE_PROGRAM
;
3153 *matrix_idx
= parse_integer (inst
, Program
);
3154 if (*matrix_idx
>= (GLint
) ctx
->Const
.MaxProgramMatrices
) {
3155 _mesa_set_program_error (ctx
, Program
->Position
,
3156 "Invalid Program Matrix");
3157 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3158 "Invalid Program Matrix: %d", *matrix_idx
);
3164 switch (*(*inst
)++) {
3165 case MATRIX_MODIFIER_IDENTITY
:
3166 *matrix_modifier
= 0;
3168 case MATRIX_MODIFIER_INVERSE
:
3169 *matrix_modifier
= STATE_MATRIX_INVERSE
;
3171 case MATRIX_MODIFIER_TRANSPOSE
:
3172 *matrix_modifier
= STATE_MATRIX_TRANSPOSE
;
3174 case MATRIX_MODIFIER_INVTRANS
:
3175 *matrix_modifier
= STATE_MATRIX_INVTRANS
;
3184 * This parses a state string (rather, the binary version of it) into
3185 * a 6-token sequence as described in _mesa_fetch_state() [program.c]
3187 * \param inst - the start in the binary arry to start working from
3188 * \param state_tokens - the storage for the 6-token state description
3189 * \return - 0 on sucess, 1 on error
3192 parse_state_single_item (GLcontext
* ctx
, GLubyte
** inst
,
3193 struct arb_program
*Program
, GLint
* state_tokens
)
3195 switch (*(*inst
)++) {
3196 case STATE_MATERIAL_PARSER
:
3197 state_tokens
[0] = STATE_MATERIAL
;
3198 state_tokens
[1] = parse_face_type (inst
);
3199 switch (*(*inst
)++) {
3200 case MATERIAL_AMBIENT
:
3201 state_tokens
[2] = STATE_AMBIENT
;
3203 case MATERIAL_DIFFUSE
:
3204 state_tokens
[2] = STATE_DIFFUSE
;
3206 case MATERIAL_SPECULAR
:
3207 state_tokens
[2] = STATE_SPECULAR
;
3209 case MATERIAL_EMISSION
:
3210 state_tokens
[2] = STATE_EMISSION
;
3212 case MATERIAL_SHININESS
:
3213 state_tokens
[2] = STATE_SHININESS
;
3218 case STATE_LIGHT_PARSER
:
3219 state_tokens
[0] = STATE_LIGHT
;
3220 state_tokens
[1] = parse_integer (inst
, Program
);
3222 /* Check the value of state_tokens[1] against the # of lights */
3223 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
3224 _mesa_set_program_error (ctx
, Program
->Position
,
3225 "Invalid Light Number");
3226 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3227 "Invalid Light Number: %d", state_tokens
[1]);
3231 switch (*(*inst
)++) {
3233 state_tokens
[2] = STATE_AMBIENT
;
3236 state_tokens
[2] = STATE_DIFFUSE
;
3238 case LIGHT_SPECULAR
:
3239 state_tokens
[2] = STATE_SPECULAR
;
3241 case LIGHT_POSITION
:
3242 state_tokens
[2] = STATE_POSITION
;
3244 case LIGHT_ATTENUATION
:
3245 state_tokens
[2] = STATE_ATTENUATION
;
3248 state_tokens
[2] = STATE_HALF
;
3250 case LIGHT_SPOT_DIRECTION
:
3251 state_tokens
[2] = STATE_SPOT_DIRECTION
;
3256 case STATE_LIGHT_MODEL
:
3257 switch (*(*inst
)++) {
3258 case LIGHT_MODEL_AMBIENT
:
3259 state_tokens
[0] = STATE_LIGHTMODEL_AMBIENT
;
3261 case LIGHT_MODEL_SCENECOLOR
:
3262 state_tokens
[0] = STATE_LIGHTMODEL_SCENECOLOR
;
3263 state_tokens
[1] = parse_face_type (inst
);
3268 case STATE_LIGHT_PROD
:
3269 state_tokens
[0] = STATE_LIGHTPROD
;
3270 state_tokens
[1] = parse_integer (inst
, Program
);
3272 /* Check the value of state_tokens[1] against the # of lights */
3273 if (state_tokens
[1] >= (GLint
) ctx
->Const
.MaxLights
) {
3274 _mesa_set_program_error (ctx
, Program
->Position
,
3275 "Invalid Light Number");
3276 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3277 "Invalid Light Number: %d", state_tokens
[1]);
3281 state_tokens
[2] = parse_face_type (inst
);
3282 switch (*(*inst
)++) {
3283 case LIGHT_PROD_AMBIENT
:
3284 state_tokens
[3] = STATE_AMBIENT
;
3286 case LIGHT_PROD_DIFFUSE
:
3287 state_tokens
[3] = STATE_DIFFUSE
;
3289 case LIGHT_PROD_SPECULAR
:
3290 state_tokens
[3] = STATE_SPECULAR
;
3297 switch (*(*inst
)++) {
3299 state_tokens
[0] = STATE_FOG_COLOR
;
3302 state_tokens
[0] = STATE_FOG_PARAMS
;
3308 state_tokens
[1] = parse_integer (inst
, Program
);
3309 switch (*(*inst
)++) {
3311 state_tokens
[0] = STATE_TEXENV_COLOR
;
3320 state_tokens
[0] = STATE_TEXGEN
;
3321 /*state_tokens[1] = parse_integer (inst, Program);*/ /* Texture Unit */
3323 if (parse_texcoord_num (ctx
, inst
, Program
, &coord
))
3325 state_tokens
[1] = coord
;
3330 /* 0 - s, 1 - t, 2 - r, 3 - q */
3333 if (type
== TEX_GEN_EYE
) {
3336 state_tokens
[2] = STATE_TEXGEN_EYE_S
;
3339 state_tokens
[2] = STATE_TEXGEN_EYE_T
;
3342 state_tokens
[2] = STATE_TEXGEN_EYE_R
;
3345 state_tokens
[2] = STATE_TEXGEN_EYE_Q
;
3352 state_tokens
[2] = STATE_TEXGEN_OBJECT_S
;
3355 state_tokens
[2] = STATE_TEXGEN_OBJECT_T
;
3358 state_tokens
[2] = STATE_TEXGEN_OBJECT_R
;
3361 state_tokens
[2] = STATE_TEXGEN_OBJECT_Q
;
3369 switch (*(*inst
)++) {
3371 state_tokens
[0] = STATE_DEPTH_RANGE
;
3376 case STATE_CLIP_PLANE
:
3377 state_tokens
[0] = STATE_CLIPPLANE
;
3378 state_tokens
[1] = parse_integer (inst
, Program
);
3379 if (parse_clipplane_num (ctx
, inst
, Program
, &state_tokens
[1]))
3384 switch (*(*inst
++)) {
3386 state_tokens
[0] = STATE_POINT_SIZE
;
3389 case POINT_ATTENUATION
:
3390 state_tokens
[0] = STATE_POINT_ATTENUATION
;
3395 /* XXX: I think this is the correct format for a matrix row */
3396 case STATE_MATRIX_ROWS
:
3397 state_tokens
[0] = STATE_MATRIX
;
3400 (ctx
, inst
, Program
, &state_tokens
[1], &state_tokens
[2],
3404 state_tokens
[3] = parse_integer (inst
, Program
); /* The first row to grab */
3406 state_tokens
[4] = parse_integer (inst
, Program
); /* Either the last row, 0 */
3407 if (state_tokens
[4] == 0) {
3408 state_tokens
[4] = state_tokens
[3];
3417 * This parses a state string (rather, the binary version of it) into
3418 * a 6-token similar for the state fetching code in program.c
3420 * One might ask, why fetch these parameters into just like you fetch
3421 * state when they are already stored in other places?
3423 * Because of array offsets -> We can stick env/local parameters in the
3424 * middle of a parameter array and then index someplace into the array
3427 * One optimization might be to only do this for the cases where the
3428 * env/local parameters end up inside of an array, and leave the
3429 * single parameters (or arrays of pure env/local pareameters) in their
3430 * respective register files.
3432 * For ENV parameters, the format is:
3433 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
3434 * state_tokens[1] = STATE_ENV
3435 * state_tokens[2] = the parameter index
3437 * for LOCAL parameters, the format is:
3438 * state_tokens[0] = STATE_FRAGMENT_PROGRAM / STATE_VERTEX_PROGRAM
3439 * state_tokens[1] = STATE_LOCAL
3440 * state_tokens[2] = the parameter index
3442 * \param inst - the start in the binary arry to start working from
3443 * \param state_tokens - the storage for the 6-token state description
3444 * \return - 0 on sucess, 1 on failure
3447 parse_program_single_item (GLcontext
* ctx
, GLubyte
** inst
,
3448 struct arb_program
*Program
, GLint
* state_tokens
)
3450 if (Program
->type
== GL_FRAGMENT_PROGRAM_ARB
)
3451 state_tokens
[0] = STATE_FRAGMENT_PROGRAM
;
3453 state_tokens
[0] = STATE_VERTEX_PROGRAM
;
3456 switch (*(*inst
)++) {
3457 case PROGRAM_PARAM_ENV
:
3458 state_tokens
[1] = STATE_ENV
;
3459 state_tokens
[2] = parse_integer (inst
, Program
);
3461 /* Check state_tokens[2] against the number of ENV parameters available */
3462 if (((Program
->type
== GL_FRAGMENT_PROGRAM_ARB
) &&
3463 (state_tokens
[2] >= (GLint
) ctx
->Const
.MaxFragmentProgramEnvParams
))
3465 ((Program
->type
== GL_VERTEX_PROGRAM_ARB
) &&
3466 (state_tokens
[2] >= (GLint
) ctx
->Const
.MaxVertexProgramEnvParams
))) {
3467 _mesa_set_program_error (ctx
, Program
->Position
,
3468 "Invalid Program Env Parameter");
3469 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3470 "Invalid Program Env Parameter: %d",
3477 case PROGRAM_PARAM_LOCAL
:
3478 state_tokens
[1] = STATE_LOCAL
;
3479 state_tokens
[2] = parse_integer (inst
, Program
);
3481 /* Check state_tokens[2] against the number of LOCAL parameters available */
3482 if (((Program
->type
== GL_FRAGMENT_PROGRAM_ARB
) &&
3483 (state_tokens
[2] >= (GLint
) ctx
->Const
.MaxFragmentProgramLocalParams
))
3485 ((Program
->type
== GL_VERTEX_PROGRAM_ARB
) &&
3486 (state_tokens
[2] >= (GLint
) ctx
->Const
.MaxVertexProgramLocalParams
))) {
3487 _mesa_set_program_error (ctx
, Program
->Position
,
3488 "Invalid Program Local Parameter");
3489 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3490 "Invalid Program Local Parameter: %d",
3501 * For ARB_vertex_program, programs are not allowed to use both an explicit
3502 * vertex attribute and a generic vertex attribute corresponding to the same
3503 * state. See section 2.14.3.1 of the GL_ARB_vertex_program spec.
3505 * This will walk our var_cache and make sure that nobody does anything fishy.
3507 * \return 0 on sucess, 1 on error
3510 generic_attrib_check(struct var_cache
*vc_head
)
3513 struct var_cache
*curr
;
3514 GLboolean explicitAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
],
3515 genericAttrib
[MAX_VERTEX_PROGRAM_ATTRIBS
];
3517 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
3518 explicitAttrib
[a
] = GL_FALSE
;
3519 genericAttrib
[a
] = GL_FALSE
;
3524 if (curr
->type
== vt_attrib
) {
3525 if (curr
->attrib_is_generic
)
3526 genericAttrib
[ curr
->attrib_binding_idx
] = GL_TRUE
;
3528 explicitAttrib
[ curr
->attrib_binding_idx
] = GL_TRUE
;
3534 for (a
=0; a
<MAX_VERTEX_PROGRAM_ATTRIBS
; a
++) {
3535 if ((explicitAttrib
[a
]) && (genericAttrib
[a
]))
3543 * This will handle the binding side of an ATTRIB var declaration
3545 * \param binding - the fragment input register state, defined in nvfragprog.h
3546 * \param binding_idx - the index in the attrib register file that binding is associated with
3547 * \return returns 0 on sucess, 1 on error
3549 * See nvfragparse.c for attrib register file layout
3552 parse_attrib_binding (GLcontext
* ctx
, GLubyte
** inst
,
3553 struct arb_program
*Program
, GLuint
* binding
,
3554 GLuint
* binding_idx
, GLuint
*is_generic
)
3561 if (Program
->type
== GL_FRAGMENT_PROGRAM_ARB
) {
3562 switch (*(*inst
)++) {
3563 case FRAGMENT_ATTRIB_COLOR
:
3564 err
= parse_color_type (ctx
, inst
, Program
, &coord
);
3565 *binding
= FRAG_ATTRIB_COL0
+ coord
;
3566 *binding_idx
= 1 + coord
;
3569 case FRAGMENT_ATTRIB_TEXCOORD
:
3570 err
= parse_texcoord_num (ctx
, inst
, Program
, &texcoord
);
3571 *binding
= FRAG_ATTRIB_TEX0
+ texcoord
;
3572 *binding_idx
= 4 + texcoord
;
3575 case FRAGMENT_ATTRIB_FOGCOORD
:
3576 *binding
= FRAG_ATTRIB_FOGC
;
3580 case FRAGMENT_ATTRIB_POSITION
:
3581 *binding
= FRAG_ATTRIB_WPOS
;
3591 switch (*(*inst
)++) {
3592 case VERTEX_ATTRIB_POSITION
:
3593 *binding
= VERT_ATTRIB_POS
;
3597 case VERTEX_ATTRIB_WEIGHT
:
3601 err
= parse_weight_num (ctx
, inst
, Program
, &weight
);
3602 *binding
= VERT_ATTRIB_WEIGHT
;
3607 case VERTEX_ATTRIB_NORMAL
:
3608 *binding
= VERT_ATTRIB_NORMAL
;
3612 case VERTEX_ATTRIB_COLOR
:
3616 err
= parse_color_type (ctx
, inst
, Program
, &color
);
3618 *binding
= VERT_ATTRIB_COLOR1
;
3622 *binding
= VERT_ATTRIB_COLOR0
;
3628 case VERTEX_ATTRIB_FOGCOORD
:
3629 *binding
= VERT_ATTRIB_FOG
;
3633 case VERTEX_ATTRIB_TEXCOORD
:
3637 err
= parse_texcoord_num (ctx
, inst
, Program
, &unit
);
3638 *binding
= VERT_ATTRIB_TEX0
+ unit
;
3639 *binding_idx
= 8 + unit
;
3643 /* XXX: It looks like we don't support this at all, atm */
3644 case VERTEX_ATTRIB_MATRIXINDEX
:
3645 parse_integer (inst
, Program
);
3648 case VERTEX_ATTRIB_GENERIC
:
3652 if (!parse_generic_attrib_num(ctx
, inst
, Program
, &attrib
)) {
3656 *binding
= VERT_ATTRIB_POS
;
3659 *binding
= VERT_ATTRIB_WEIGHT
;
3662 *binding
= VERT_ATTRIB_NORMAL
;
3665 *binding
= VERT_ATTRIB_COLOR0
;
3668 *binding
= VERT_ATTRIB_COLOR1
;
3671 *binding
= VERT_ATTRIB_FOG
;
3678 *binding
= VERT_ATTRIB_TEX0
+ (attrib
-8);
3681 *binding_idx
= attrib
;
3692 /* Can this even happen? */
3694 _mesa_set_program_error (ctx
, Program
->Position
,
3695 "Bad attribute binding");
3696 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Bad attribute binding");
3699 Program
->InputsRead
|= (1 << *binding_idx
);
3705 * This translates between a binary token for an output variable type
3706 * and the mesa token for the same thing.
3709 * XXX: What is the 'name' for vertex program state? -> do we need it?
3710 * I don't think we do;
3712 * See nvfragprog.h for definitions
3714 * \param inst - The parsed tokens
3715 * \param binding - The name of the state we are binding too
3716 * \param binding_idx - The index into the result register file that this is bound too
3718 * See nvfragparse.c for the register file layout for fragment programs
3719 * See nvvertparse.c for the register file layout for vertex programs
3722 parse_result_binding (GLcontext
* ctx
, GLubyte
** inst
, GLuint
* binding
,
3723 GLuint
* binding_idx
, struct arb_program
*Program
)
3727 switch (*(*inst
)++) {
3728 case FRAGMENT_RESULT_COLOR
:
3729 /* for frag programs, this is FRAGMENT_RESULT_COLOR */
3730 if (Program
->type
== GL_FRAGMENT_PROGRAM_ARB
) {
3731 *binding
= FRAG_OUTPUT_COLR
;
3734 /* for vtx programs, this is VERTEX_RESULT_POSITION */
3740 case FRAGMENT_RESULT_DEPTH
:
3741 /* for frag programs, this is FRAGMENT_RESULT_DEPTH */
3742 if (Program
->type
== GL_FRAGMENT_PROGRAM_ARB
) {
3743 *binding
= FRAG_OUTPUT_DEPR
;
3746 /* for vtx programs, this is VERTEX_RESULT_COLOR */
3749 GLuint face_type
= parse_face_type(inst
);
3750 GLint color_type_ret
= parse_color_type(ctx
, inst
, Program
, &color_type
);
3754 if (color_type_ret
) return 1;
3756 /* secondary color */
3767 /* secondary color */
3779 case VERTEX_RESULT_FOGCOORD
:
3783 case VERTEX_RESULT_POINTSIZE
:
3787 case VERTEX_RESULT_TEXCOORD
:
3788 if (parse_texcoord_num (ctx
, inst
, Program
, &b
))
3790 *binding_idx
= 7 + b
;
3794 Program
->OutputsWritten
|= (1 << *binding_idx
);
3800 * This handles the declaration of ATTRIB variables
3803 * parse_vert_attrib_binding(), or something like that
3805 * \return 0 on sucess, 1 on error
3808 parse_attrib (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
3809 struct arb_program
*Program
)
3813 struct var_cache
*attrib_var
;
3815 attrib_var
= parse_string (inst
, vc_head
, Program
, &found
);
3816 Program
->Position
= parse_position (inst
);
3818 error_msg
= (char *)
3819 _mesa_malloc (_mesa_strlen ((char *) attrib_var
->name
) + 40);
3820 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
3823 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
3824 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
3826 _mesa_free (error_msg
);
3830 attrib_var
->type
= vt_attrib
;
3832 /* I think this is ok now - karl */
3834 /*if (Program->type == GL_FRAGMENT_PROGRAM_ARB) */
3836 if (parse_attrib_binding
3837 (ctx
, inst
, Program
, &attrib_var
->attrib_binding
,
3838 &attrib_var
->attrib_binding_idx
, &attrib_var
->attrib_is_generic
))
3840 if (generic_attrib_check(*vc_head
)) {
3841 _mesa_set_program_error (ctx
, Program
->Position
,
3842 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
3843 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3844 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
3850 Program
->Base
.NumAttributes
++;
3855 * \param use -- TRUE if we're called when declaring implicit parameters,
3856 * FALSE if we're declaraing variables. This has to do with
3857 * if we get a signed or unsigned float for scalar constants
3860 parse_param_elements (GLcontext
* ctx
, GLubyte
** inst
,
3861 struct var_cache
*param_var
,
3862 struct arb_program
*Program
, GLboolean use
)
3866 GLint state_tokens
[6];
3867 GLfloat const_values
[4];
3871 switch (*(*inst
)++) {
3872 case PARAM_STATE_ELEMENT
:
3874 if (parse_state_single_item (ctx
, inst
, Program
, state_tokens
))
3877 /* If we adding STATE_MATRIX that has multiple rows, we need to
3878 * unroll it and call _mesa_add_state_reference() for each row
3880 if ((state_tokens
[0] == STATE_MATRIX
)
3881 && (state_tokens
[3] != state_tokens
[4])) {
3883 GLint first_row
= state_tokens
[3];
3884 GLint last_row
= state_tokens
[4];
3886 for (row
= first_row
; row
<= last_row
; row
++) {
3887 state_tokens
[3] = state_tokens
[4] = row
;
3890 _mesa_add_state_reference (Program
->Parameters
,
3892 if (param_var
->param_binding_begin
== ~0U)
3893 param_var
->param_binding_begin
= idx
;
3894 param_var
->param_binding_length
++;
3895 Program
->Base
.NumParameters
++;
3900 _mesa_add_state_reference (Program
->Parameters
, state_tokens
);
3901 if (param_var
->param_binding_begin
== ~0U)
3902 param_var
->param_binding_begin
= idx
;
3903 param_var
->param_binding_length
++;
3904 Program
->Base
.NumParameters
++;
3908 case PARAM_PROGRAM_ELEMENT
:
3910 if (parse_program_single_item (ctx
, inst
, Program
, state_tokens
))
3912 idx
= _mesa_add_state_reference (Program
->Parameters
, state_tokens
);
3913 if (param_var
->param_binding_begin
== ~0U)
3914 param_var
->param_binding_begin
= idx
;
3915 param_var
->param_binding_length
++;
3916 Program
->Base
.NumParameters
++;
3918 /* Check if there is more: 0 -> we're done, else its an integer */
3920 GLuint out_of_range
, new_idx
;
3921 GLuint start_idx
= state_tokens
[2] + 1;
3922 GLuint end_idx
= parse_integer (inst
, Program
);
3925 if (Program
->type
== GL_FRAGMENT_PROGRAM_ARB
) {
3926 if (((state_tokens
[1] == STATE_ENV
)
3927 && (end_idx
>= ctx
->Const
.MaxFragmentProgramEnvParams
))
3928 || ((state_tokens
[1] == STATE_LOCAL
)
3930 ctx
->Const
.MaxFragmentProgramLocalParams
)))
3934 if (((state_tokens
[1] == STATE_ENV
)
3935 && (end_idx
>= ctx
->Const
.MaxFragmentProgramEnvParams
))
3936 || ((state_tokens
[1] == STATE_LOCAL
)
3938 ctx
->Const
.MaxFragmentProgramLocalParams
)))
3942 _mesa_set_program_error (ctx
, Program
->Position
,
3943 "Invalid Program Parameter");
3944 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3945 "Invalid Program Parameter: %d", end_idx
);
3949 for (new_idx
= start_idx
; new_idx
<= end_idx
; new_idx
++) {
3950 state_tokens
[2] = new_idx
;
3952 _mesa_add_state_reference (Program
->Parameters
,
3954 param_var
->param_binding_length
++;
3955 Program
->Base
.NumParameters
++;
3964 case PARAM_CONSTANT
:
3965 parse_constant (inst
, const_values
, Program
, use
);
3967 _mesa_add_named_constant (Program
->Parameters
,
3968 (char *) param_var
->name
, const_values
);
3969 if (param_var
->param_binding_begin
== ~0U)
3970 param_var
->param_binding_begin
= idx
;
3971 param_var
->param_binding_length
++;
3972 Program
->Base
.NumParameters
++;
3976 _mesa_set_program_error (ctx
, Program
->Position
,
3977 "Unexpected token in parse_param_elements()");
3978 _mesa_error (ctx
, GL_INVALID_OPERATION
,
3979 "Unexpected token in parse_param_elements()");
3983 /* Make sure we haven't blown past our parameter limits */
3984 if (((Program
->type
== GL_VERTEX_PROGRAM_ARB
) &&
3985 (Program
->Base
.NumParameters
>=
3986 ctx
->Const
.MaxVertexProgramLocalParams
))
3987 || ((Program
->type
== GL_FRAGMENT_PROGRAM_ARB
)
3988 && (Program
->Base
.NumParameters
>=
3989 ctx
->Const
.MaxFragmentProgramLocalParams
))) {
3990 _mesa_set_program_error (ctx
, Program
->Position
,
3991 "Too many parameter variables");
3992 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Too many parameter variables");
4000 * This picks out PARAM program parameter bindings.
4002 * XXX: This needs to be stressed & tested
4004 * \return 0 on sucess, 1 on error
4007 parse_param (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
4008 struct arb_program
*Program
)
4010 GLuint found
, specified_length
, err
;
4012 struct var_cache
*param_var
;
4015 param_var
= parse_string (inst
, vc_head
, Program
, &found
);
4016 Program
->Position
= parse_position (inst
);
4019 error_msg
= (char *) _mesa_malloc (_mesa_strlen ((char *) param_var
->name
) + 40);
4020 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
4023 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
4024 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
4026 _mesa_free (error_msg
);
4030 specified_length
= parse_integer (inst
, Program
);
4032 if (specified_length
< 0) {
4033 _mesa_set_program_error (ctx
, Program
->Position
,
4034 "Negative parameter array length");
4035 _mesa_error (ctx
, GL_INVALID_OPERATION
,
4036 "Negative parameter array length: %d", specified_length
);
4040 param_var
->type
= vt_param
;
4041 param_var
->param_binding_length
= 0;
4043 /* Right now, everything is shoved into the main state register file.
4045 * In the future, it would be nice to leave things ENV/LOCAL params
4046 * in their respective register files, if possible
4048 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
4051 * * - add each guy to the parameter list
4052 * * - increment the param_var->param_binding_len
4053 * * - store the param_var->param_binding_begin for the first one
4054 * * - compare the actual len to the specified len at the end
4056 while (**inst
!= PARAM_NULL
) {
4057 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_FALSE
))
4061 /* Test array length here! */
4062 if (specified_length
) {
4063 if (specified_length
!= param_var
->param_binding_length
) {
4064 _mesa_set_program_error (ctx
, Program
->Position
,
4065 "Declared parameter array lenght does not match parameter list");
4066 _mesa_error (ctx
, GL_INVALID_OPERATION
,
4067 "Declared parameter array lenght does not match parameter list");
4080 parse_param_use (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
4081 struct arb_program
*Program
, struct var_cache
**new_var
)
4083 struct var_cache
*param_var
;
4085 /* First, insert a dummy entry into the var_cache */
4086 var_cache_create (¶m_var
);
4087 param_var
->name
= (GLubyte
*) _mesa_strdup (" ");
4088 param_var
->type
= vt_param
;
4090 param_var
->param_binding_length
= 0;
4091 /* Don't fill in binding_begin; We use the default value of -1
4092 * to tell if its already initialized, elsewhere.
4094 * param_var->param_binding_begin = 0;
4096 param_var
->param_binding_type
= PROGRAM_STATE_VAR
;
4098 var_cache_append (vc_head
, param_var
);
4100 /* Then fill it with juicy parameter goodness */
4101 if (parse_param_elements (ctx
, inst
, param_var
, Program
, GL_TRUE
))
4104 *new_var
= param_var
;
4111 * This handles the declaration of TEMP variables
4113 * \return 0 on sucess, 1 on error
4116 parse_temp (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
4117 struct arb_program
*Program
)
4120 struct var_cache
*temp_var
;
4123 while (**inst
!= 0) {
4124 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
4125 Program
->Position
= parse_position (inst
);
4127 error_msg
= (char *)
4128 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
4129 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
4132 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
4133 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
4135 _mesa_free (error_msg
);
4139 temp_var
->type
= vt_temp
;
4141 if (((Program
->type
== GL_FRAGMENT_PROGRAM_ARB
) &&
4142 (Program
->Base
.NumTemporaries
>=
4143 ctx
->Const
.MaxFragmentProgramTemps
))
4144 || ((Program
->type
== GL_VERTEX_PROGRAM_ARB
)
4145 && (Program
->Base
.NumTemporaries
>=
4146 ctx
->Const
.MaxVertexProgramTemps
))) {
4147 _mesa_set_program_error (ctx
, Program
->Position
,
4148 "Too many TEMP variables declared");
4149 _mesa_error (ctx
, GL_INVALID_OPERATION
,
4150 "Too many TEMP variables declared");
4154 temp_var
->temp_binding
= Program
->Base
.NumTemporaries
;
4155 Program
->Base
.NumTemporaries
++;
4163 * This handles variables of the OUTPUT variety
4165 * \return 0 on sucess, 1 on error
4168 parse_output (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
4169 struct arb_program
*Program
)
4172 struct var_cache
*output_var
;
4174 output_var
= parse_string (inst
, vc_head
, Program
, &found
);
4175 Program
->Position
= parse_position (inst
);
4178 error_msg
= (char *)
4179 _mesa_malloc (_mesa_strlen ((char *) output_var
->name
) + 40);
4180 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
4183 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
4184 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
4186 _mesa_free (error_msg
);
4190 output_var
->type
= vt_output
;
4191 return parse_result_binding (ctx
, inst
, &output_var
->output_binding
,
4192 &output_var
->output_binding_idx
, Program
);
4196 * This handles variables of the ALIAS kind
4198 * \return 0 on sucess, 1 on error
4201 parse_alias (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
4202 struct arb_program
*Program
)
4205 struct var_cache
*temp_var
;
4209 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
4210 Program
->Position
= parse_position (inst
);
4213 error_msg
= (char *)
4214 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
4215 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
4218 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
4219 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
4221 _mesa_free (error_msg
);
4225 temp_var
->type
= vt_alias
;
4226 temp_var
->alias_binding
= parse_string (inst
, vc_head
, Program
, &found
);
4227 Program
->Position
= parse_position (inst
);
4231 error_msg
= (char *)
4232 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
4233 _mesa_sprintf (error_msg
, "Alias value %s is not defined",
4234 temp_var
->alias_binding
->name
);
4236 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
4237 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
4239 _mesa_free (error_msg
);
4247 * This handles variables of the ADDRESS kind
4249 * \return 0 on sucess, 1 on error
4252 parse_address (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
4253 struct arb_program
*Program
)
4256 struct var_cache
*temp_var
;
4259 while (**inst
!= 0) {
4260 temp_var
= parse_string (inst
, vc_head
, Program
, &found
);
4261 Program
->Position
= parse_position (inst
);
4263 error_msg
= (char *)
4264 _mesa_malloc (_mesa_strlen ((char *) temp_var
->name
) + 40);
4265 _mesa_sprintf (error_msg
, "Duplicate Varible Declaration: %s",
4268 _mesa_set_program_error (ctx
, Program
->Position
, error_msg
);
4269 _mesa_error (ctx
, GL_INVALID_OPERATION
, error_msg
);
4271 _mesa_free (error_msg
);
4275 temp_var
->type
= vt_address
;
4277 if (Program
->Base
.NumAddressRegs
>=
4278 ctx
->Const
.MaxVertexProgramAddressRegs
) {
4279 _mesa_set_program_error (ctx
, Program
->Position
,
4280 "Too many ADDRESS variables declared");
4281 _mesa_error (ctx
, GL_INVALID_OPERATION
,
4282 "Too many ADDRESS variables declared");
4286 temp_var
->address_binding
= Program
->Base
.NumAddressRegs
;
4287 Program
->Base
.NumAddressRegs
++;
4295 * Parse a program declaration
4297 * \return 0 on sucess, 1 on error
4300 parse_declaration (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
4301 struct arb_program
*Program
)
4305 switch (*(*inst
)++) {
4307 err
= parse_address (ctx
, inst
, vc_head
, Program
);
4311 err
= parse_alias (ctx
, inst
, vc_head
, Program
);
4315 err
= parse_attrib (ctx
, inst
, vc_head
, Program
);
4319 err
= parse_output (ctx
, inst
, vc_head
, Program
);
4323 err
= parse_param (ctx
, inst
, vc_head
, Program
);
4327 err
= parse_temp (ctx
, inst
, vc_head
, Program
);
4335 * Handle the parsing out of a masked destination register
4337 * \param File - The register file we write to
4338 * \param Index - The register index we write to
4339 * \param WriteMask - The mask controlling which components we write (1->write)
4341 * \return 0 on sucess, 1 on error
4344 parse_masked_dst_reg (GLcontext
* ctx
, GLubyte
** inst
,
4345 struct var_cache
**vc_head
, struct arb_program
*Program
,
4346 GLint
* File
, GLint
* Index
, GLboolean
* WriteMask
)
4350 struct var_cache
*dst
;
4352 /* We either have a result register specified, or a
4353 * variable that may or may not be writable
4355 switch (*(*inst
)++) {
4356 case REGISTER_RESULT
:
4357 if (parse_result_binding
4358 (ctx
, inst
, &result
, (GLuint
*) Index
, Program
))
4360 *File
= PROGRAM_OUTPUT
;
4363 case REGISTER_ESTABLISHED_NAME
:
4364 dst
= parse_string (inst
, vc_head
, Program
, &result
);
4365 Program
->Position
= parse_position (inst
);
4367 /* If the name has never been added to our symbol table, we're hosed */
4369 _mesa_set_program_error (ctx
, Program
->Position
,
4370 "0: Undefined variable");
4371 _mesa_error (ctx
, GL_INVALID_OPERATION
, "0: Undefined variable: %s",
4376 switch (dst
->type
) {
4378 *File
= PROGRAM_OUTPUT
;
4379 *Index
= dst
->output_binding_idx
;
4383 *File
= PROGRAM_TEMPORARY
;
4384 *Index
= dst
->temp_binding
;
4387 /* If the var type is not vt_output or vt_temp, no go */
4389 _mesa_set_program_error (ctx
, Program
->Position
,
4390 "Destination register is read only");
4391 _mesa_error (ctx
, GL_INVALID_OPERATION
,
4392 "Destination register is read only: %s",
4399 _mesa_set_program_error (ctx
, Program
->Position
,
4400 "Unexpected opcode in parse_masked_dst_reg()");
4401 _mesa_error (ctx
, GL_INVALID_OPERATION
,
4402 "Unexpected opcode in parse_masked_dst_reg()");
4406 /* And then the mask.
4414 WriteMask
[0] = (mask
& (1 << 3)) >> 3;
4415 WriteMask
[1] = (mask
& (1 << 2)) >> 2;
4416 WriteMask
[2] = (mask
& (1 << 1)) >> 1;
4417 WriteMask
[3] = (mask
& (1));
4423 * Handle the parsing out of a masked address register
4425 * \param Index - The register index we write to
4426 * \param WriteMask - The mask controlling which components we write (1->write)
4428 * \return 0 on sucess, 1 on error
4431 parse_masked_address_reg (GLcontext
* ctx
, GLubyte
** inst
,
4432 struct var_cache
**vc_head
,
4433 struct arb_program
*Program
, GLint
* Index
,
4434 GLboolean
* WriteMask
)
4436 struct var_cache
*dst
;
4439 dst
= parse_string (inst
, vc_head
, Program
, &result
);
4440 Program
->Position
= parse_position (inst
);
4442 /* If the name has never been added to our symbol table, we're hosed */
4444 _mesa_set_program_error (ctx
, Program
->Position
, "1: Undefined variable");
4445 _mesa_error (ctx
, GL_INVALID_OPERATION
, "1: Undefined variable: %s",
4450 if (dst
->type
!= vt_address
) {
4451 _mesa_set_program_error (ctx
, Program
->Position
,
4452 "Variable is not of type ADDRESS");
4453 _mesa_error (ctx
, GL_INVALID_OPERATION
,
4454 "Variable: %s is not of type ADDRESS", dst
->name
);
4458 /* Writemask of .x is implied */
4460 WriteMask
[1] = WriteMask
[2] = WriteMask
[3] = 0;
4466 * Parse out a swizzle mask.
4468 * The values in the input stream are:
4469 * COMPONENT_X -> x/r
4470 * COMPONENT_Y -> y/g
4474 * The values in the output mask are:
4480 * The len parameter allows us to grab 4 components for a vector
4481 * swizzle, or just 1 component for a scalar src register selection
4484 parse_swizzle_mask (GLubyte
** inst
, GLubyte
* mask
, GLint len
)
4488 for (a
= 0; a
< 4; a
++)
4491 for (a
= 0; a
< len
; a
++) {
4492 switch (*(*inst
)++) {
4517 parse_extended_swizzle_mask (GLubyte
** inst
, GLubyte
* mask
, GLboolean
* Negate
)
4523 for (a
= 0; a
< 4; a
++) {
4524 if (parse_sign (inst
))
4531 mask
[a
] = SWIZZLE_ZERO
;
4534 mask
[a
] = SWIZZLE_ONE
;
4552 mask
[a
] = SWIZZLE_ZERO
;
4554 mask
[a
] = SWIZZLE_ONE
;
4566 parse_src_reg (GLcontext
* ctx
, GLubyte
** inst
, struct var_cache
**vc_head
,
4567 struct arb_program
*Program
, GLint
* File
, GLint
* Index
)
4569 struct var_cache
*src
;
4570 GLuint binding_state
, binding_idx
, is_generic
, found
, offset
;
4572 /* And the binding for the src */
4573 switch (*(*inst
)++) {
4574 case REGISTER_ATTRIB
:
4575 if (parse_attrib_binding
4576 (ctx
, inst
, Program
, &binding_state
, &binding_idx
, &is_generic
))
4578 *File
= PROGRAM_INPUT
;
4579 *Index
= binding_idx
;
4581 /* We need to insert a dummy variable into the var_cache so we can
4582 * catch generic vertex attrib aliasing errors
4584 var_cache_create(&src
);
4585 src
->type
= vt_attrib
;
4586 src
->name
= (GLubyte
*)_mesa_strdup("Dummy Attrib Variable");
4587 src
->attrib_binding
= binding_state
;
4588 src
->attrib_binding_idx
= binding_idx
;
4589 src
->attrib_is_generic
= is_generic
;
4590 var_cache_append(vc_head
, src
);
4591 if (generic_attrib_check(*vc_head
)) {
4592 _mesa_set_program_error (ctx
, Program
->Position
,
4593 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
4594 _mesa_error (ctx
, GL_INVALID_OPERATION
,
4595 "Cannot use both a generic vertex attribute and a specific attribute of the same type");
4600 case REGISTER_PARAM
:
4602 case PARAM_ARRAY_ELEMENT
:
4604 src
= parse_string (inst
, vc_head
, Program
, &found
);
4605 Program
->Position
= parse_position (inst
);
4608 _mesa_set_program_error (ctx
, Program
->Position
,
4609 "2: Undefined variable");
4610 _mesa_error (ctx
, GL_INVALID_OPERATION
,
4611 "2: Undefined variable: %s", src
->name
);
4615 *File
= src
->param_binding_type
;
4617 switch (*(*inst
)++) {
4618 case ARRAY_INDEX_ABSOLUTE
:
4619 offset
= parse_integer (inst
, Program
);
4622 || (offset
>= src
->param_binding_length
)) {
4623 _mesa_set_program_error (ctx
, Program
->Position
,
4624 "Index out of range");
4625 _mesa_error (ctx
, GL_INVALID_OPERATION
,
4626 "Index %d out of range for %s", offset
,
4631 *Index
= src
->param_binding_begin
+ offset
;
4635 case ARRAY_INDEX_RELATIVE
:
4642 if (parse_param_use (ctx
, inst
, vc_head
, Program
, &src
))
4645 *File
= src
->param_binding_type
;
4646 *Index
= src
->param_binding_begin
;
4651 case REGISTER_ESTABLISHED_NAME
:
4653 src
= parse_string (inst
, vc_head
, Program
, &found
);
4654 Program
->Position
= parse_position (inst
);
4656 /* If the name has never been added to our symbol table, we're hosed */
4658 _mesa_set_program_error (ctx
, Program
->Position
,
4659 "3: Undefined variable");
4660 _mesa_error (ctx
, GL_INVALID_OPERATION
, "3: Undefined variable: %s",
4665 switch (src
->type
) {
4667 *File
= PROGRAM_INPUT
;
4668 *Index
= src
->attrib_binding_idx
;
4671 /* XXX: We have to handle offsets someplace in here! -- or are those above? */
4673 *File
= src
->param_binding_type
;
4674 *Index
= src
->param_binding_begin
;
4678 *File
= PROGRAM_TEMPORARY
;
4679 *Index
= src
->temp_binding
;
4682 /* If the var type is vt_output no go */
4684 _mesa_set_program_error (ctx
, Program
->Position
,
4685 "destination register is read only");
4686 _mesa_error (ctx
, GL_INVALID_OPERATION
,
4687 "destination register is read only: %s",
4694 _mesa_set_program_error (ctx
, Program
->Position
,
4695 "Unknown token in parse_src_reg");
4696 _mesa_error (ctx
, GL_INVALID_OPERATION
,
4697 "Unknown token in parse_src_reg");
4707 parse_vector_src_reg (GLcontext
* ctx
, GLubyte
** inst
,
4708 struct var_cache
**vc_head
, struct arb_program
*Program
,
4709 GLint
* File
, GLint
* Index
, GLboolean
* Negate
,
4713 *Negate
= parse_sign (inst
);
4715 /* And the src reg */
4716 if (parse_src_reg (ctx
, inst
, vc_head
, Program
, File
, Index
))
4719 /* finally, the swizzle */
4720 parse_swizzle_mask (inst
, Swizzle
, 4);
4728 parse_scalar_src_reg (GLcontext
* ctx
, GLubyte
** inst
,
4729 struct var_cache
**vc_head
, struct arb_program
*Program
,
4730 GLint
* File
, GLint
* Index
, GLboolean
* Negate
,
4734 *Negate
= parse_sign (inst
);
4736 /* And the src reg */
4737 if (parse_src_reg (ctx
, inst
, vc_head
, Program
, File
, Index
))
4740 /* Now, get the component and shove it into all the swizzle slots */
4741 parse_swizzle_mask (inst
, Swizzle
, 1);
4747 * This is a big mother that handles getting opcodes into the instruction
4748 * and handling the src & dst registers for fragment program instructions
4751 parse_fp_instruction (GLcontext
* ctx
, GLubyte
** inst
,
4752 struct var_cache
**vc_head
, struct arb_program
*Program
,
4753 struct fp_instruction
*fp
)
4756 GLubyte swz
[4]; /* FP's swizzle mask is a GLubyte, while VP's is GLuint */
4758 GLubyte instClass
, type
, code
;
4760 /* No condition codes in ARB_fp */
4761 fp
->UpdateCondRegister
= 0;
4763 /* Record the position in the program string for debugging */
4764 fp
->StringPos
= Program
->Position
;
4766 /* F_ALU_INST or F_TEX_INST */
4767 instClass
= *(*inst
)++;
4769 /* F_ALU_{VECTOR, SCALAR, BINSC, BIN, TRI, SWZ},
4770 * F_TEX_{SAMPLE, KIL}
4774 /* The actual opcode name */
4777 /* Increment the correct count */
4778 switch (instClass
) {
4780 Program
->NumAluInstructions
++;
4783 Program
->NumTexInstructions
++;
4788 fp
->Precision
= FLOAT32
;
4790 fp
->DstReg
.CondMask
= COND_TR
;
4798 fp
->Opcode
= FP_OPCODE_ABS
;
4804 fp
->Opcode
= FP_OPCODE_FLR
;
4810 fp
->Opcode
= FP_OPCODE_FRC
;
4816 fp
->Opcode
= FP_OPCODE_LIT
;
4822 fp
->Opcode
= FP_OPCODE_MOV
;
4826 if (parse_masked_dst_reg
4827 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
4828 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
4831 fp
->SrcReg
[0].Abs
= GL_FALSE
;
4832 fp
->SrcReg
[0].NegateAbs
= GL_FALSE
;
4833 if (parse_vector_src_reg
4834 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[0].File
,
4835 &fp
->SrcReg
[0].Index
, &fp
->SrcReg
[0].NegateBase
,
4839 fp
->SrcReg
[0].Swizzle
[b
] = swz
[b
];
4847 fp
->Opcode
= FP_OPCODE_COS
;
4853 fp
->Opcode
= FP_OPCODE_EX2
;
4859 fp
->Opcode
= FP_OPCODE_LG2
;
4865 fp
->Opcode
= FP_OPCODE_RCP
;
4871 fp
->Opcode
= FP_OPCODE_RSQ
;
4877 fp
->Opcode
= FP_OPCODE_SIN
;
4883 fp
->Opcode
= FP_OPCODE_SCS
;
4887 if (parse_masked_dst_reg
4888 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
4889 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
4891 fp
->SrcReg
[0].Abs
= GL_FALSE
;
4892 fp
->SrcReg
[0].NegateAbs
= GL_FALSE
;
4893 if (parse_scalar_src_reg
4894 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[0].File
,
4895 &fp
->SrcReg
[0].Index
, &fp
->SrcReg
[0].NegateBase
,
4899 fp
->SrcReg
[0].Swizzle
[b
] = swz
[b
];
4907 fp
->Opcode
= FP_OPCODE_POW
;
4911 if (parse_masked_dst_reg
4912 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
4913 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
4915 for (a
= 0; a
< 2; a
++) {
4916 fp
->SrcReg
[a
].Abs
= GL_FALSE
;
4917 fp
->SrcReg
[a
].NegateAbs
= GL_FALSE
;
4918 if (parse_scalar_src_reg
4919 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[a
].File
,
4920 &fp
->SrcReg
[a
].Index
, &fp
->SrcReg
[a
].NegateBase
,
4924 fp
->SrcReg
[a
].Swizzle
[b
] = swz
[b
];
4934 fp
->Opcode
= FP_OPCODE_ADD
;
4940 fp
->Opcode
= FP_OPCODE_DP3
;
4946 fp
->Opcode
= FP_OPCODE_DP4
;
4952 fp
->Opcode
= FP_OPCODE_DPH
;
4958 fp
->Opcode
= FP_OPCODE_DST
;
4964 fp
->Opcode
= FP_OPCODE_MAX
;
4970 fp
->Opcode
= FP_OPCODE_MIN
;
4976 fp
->Opcode
= FP_OPCODE_MUL
;
4982 fp
->Opcode
= FP_OPCODE_SGE
;
4988 fp
->Opcode
= FP_OPCODE_SLT
;
4994 fp
->Opcode
= FP_OPCODE_SUB
;
5000 fp
->Opcode
= FP_OPCODE_X2D
;
5004 if (parse_masked_dst_reg
5005 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
5006 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
5008 for (a
= 0; a
< 2; a
++) {
5009 fp
->SrcReg
[a
].Abs
= GL_FALSE
;
5010 fp
->SrcReg
[a
].NegateAbs
= GL_FALSE
;
5011 if (parse_vector_src_reg
5012 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[a
].File
,
5013 &fp
->SrcReg
[a
].Index
, &fp
->SrcReg
[a
].NegateBase
,
5017 fp
->SrcReg
[a
].Swizzle
[b
] = swz
[b
];
5026 fp
->Opcode
= FP_OPCODE_CMP
;
5032 fp
->Opcode
= FP_OPCODE_LRP
;
5038 fp
->Opcode
= FP_OPCODE_MAD
;
5042 if (parse_masked_dst_reg
5043 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
5044 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
5046 for (a
= 0; a
< 3; a
++) {
5047 fp
->SrcReg
[a
].Abs
= GL_FALSE
;
5048 fp
->SrcReg
[a
].NegateAbs
= GL_FALSE
;
5049 if (parse_vector_src_reg
5050 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[a
].File
,
5051 &fp
->SrcReg
[a
].Index
, &fp
->SrcReg
[a
].NegateBase
,
5055 fp
->SrcReg
[a
].Swizzle
[b
] = swz
[b
];
5064 fp
->Opcode
= FP_OPCODE_SWZ
;
5067 if (parse_masked_dst_reg
5068 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
5069 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
5073 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[0].File
,
5074 &fp
->SrcReg
[0].Index
))
5076 parse_extended_swizzle_mask (inst
, swz
,
5077 &fp
->SrcReg
[0].NegateBase
);
5079 fp
->SrcReg
[0].Swizzle
[b
] = swz
[b
];
5087 fp
->Opcode
= FP_OPCODE_TEX
;
5093 fp
->Opcode
= FP_OPCODE_TXP
;
5099 fp
->Opcode
= FP_OPCODE_TXB
;
5103 if (parse_masked_dst_reg
5104 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->DstReg
.File
,
5105 &fp
->DstReg
.Index
, fp
->DstReg
.WriteMask
))
5107 fp
->SrcReg
[0].Abs
= GL_FALSE
;
5108 fp
->SrcReg
[0].NegateAbs
= GL_FALSE
;
5109 if (parse_vector_src_reg
5110 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[0].File
,
5111 &fp
->SrcReg
[0].Index
, &fp
->SrcReg
[0].NegateBase
,
5115 fp
->SrcReg
[0].Swizzle
[b
] = swz
[b
];
5118 if (parse_texcoord_num (ctx
, inst
, Program
, &texcoord
))
5120 fp
->TexSrcUnit
= texcoord
;
5123 switch (*(*inst
)++) {
5125 fp
->TexSrcBit
= TEXTURE_1D_BIT
;
5128 fp
->TexSrcBit
= TEXTURE_2D_BIT
;
5131 fp
->TexSrcBit
= TEXTURE_3D_BIT
;
5133 case TEXTARGET_RECT
:
5134 fp
->TexSrcBit
= TEXTURE_RECT_BIT
;
5136 case TEXTARGET_CUBE
:
5137 fp
->TexSrcBit
= TEXTURE_CUBE_BIT
;
5140 Program
->TexturesUsed
[texcoord
] |= fp
->TexSrcBit
;
5144 fp
->Opcode
= FP_OPCODE_KIL
;
5145 fp
->SrcReg
[0].Abs
= GL_FALSE
;
5146 fp
->SrcReg
[0].NegateAbs
= GL_FALSE
;
5147 if (parse_vector_src_reg
5148 (ctx
, inst
, vc_head
, Program
, (GLint
*) & fp
->SrcReg
[0].File
,
5149 &fp
->SrcReg
[0].Index
, &fp
->SrcReg
[0].NegateBase
,
5153 fp
->SrcReg
[0].Swizzle
[b
] = swz
[b
];
5161 * This is a big mother that handles getting opcodes into the instruction
5162 * and handling the src & dst registers for vertex program instructions
5165 parse_vp_instruction (GLcontext
* ctx
, GLubyte
** inst
,
5166 struct var_cache
**vc_head
, struct arb_program
*Program
,
5167 struct vp_instruction
*vp
)
5172 /* V_GEN_{ARL, VECTOR, SCALAR, BINSC, BIN, TRI, SWZ} */
5175 /* The actual opcode name */
5178 vp
->SrcReg
[0].RelAddr
= vp
->SrcReg
[1].RelAddr
= vp
->SrcReg
[2].RelAddr
= 0;
5180 for (a
= 0; a
< 4; a
++) {
5181 vp
->SrcReg
[0].Swizzle
[a
] = a
;
5182 vp
->SrcReg
[1].Swizzle
[a
] = a
;
5183 vp
->SrcReg
[2].Swizzle
[a
] = a
;
5184 vp
->DstReg
.WriteMask
[a
] = 1;
5190 vp
->Opcode
= VP_OPCODE_ARL
;
5192 /* Remember to set SrcReg.RelAddr; */
5194 /* Get the masked address register [dst] */
5195 if (parse_masked_address_reg
5196 (ctx
, inst
, vc_head
, Program
, &vp
->DstReg
.Index
,
5197 vp
->DstReg
.WriteMask
))
5199 vp
->DstReg
.File
= PROGRAM_ADDRESS
;
5202 /* Get a scalar src register */
5203 if (parse_scalar_src_reg
5204 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[0].File
,
5205 &vp
->SrcReg
[0].Index
, &vp
->SrcReg
[0].Negate
,
5206 vp
->SrcReg
[0].Swizzle
))
5214 vp
->Opcode
= VP_OPCODE_ABS
;
5217 vp
->Opcode
= VP_OPCODE_FLR
;
5220 vp
->Opcode
= VP_OPCODE_FRC
;
5223 vp
->Opcode
= VP_OPCODE_LIT
;
5226 vp
->Opcode
= VP_OPCODE_MOV
;
5229 if (parse_masked_dst_reg
5230 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->DstReg
.File
,
5231 &vp
->DstReg
.Index
, vp
->DstReg
.WriteMask
))
5233 if (parse_vector_src_reg
5234 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[0].File
,
5235 &vp
->SrcReg
[0].Index
, &vp
->SrcReg
[0].Negate
,
5236 vp
->SrcReg
[0].Swizzle
))
5243 vp
->Opcode
= VP_OPCODE_EX2
;
5246 vp
->Opcode
= VP_OPCODE_EXP
;
5249 vp
->Opcode
= VP_OPCODE_LG2
;
5252 vp
->Opcode
= VP_OPCODE_LOG
;
5255 vp
->Opcode
= VP_OPCODE_RCP
;
5258 vp
->Opcode
= VP_OPCODE_RSQ
;
5261 if (parse_masked_dst_reg
5262 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->DstReg
.File
,
5263 &vp
->DstReg
.Index
, vp
->DstReg
.WriteMask
))
5265 if (parse_scalar_src_reg
5266 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[0].File
,
5267 &vp
->SrcReg
[0].Index
, &vp
->SrcReg
[0].Negate
,
5268 vp
->SrcReg
[0].Swizzle
))
5275 vp
->Opcode
= VP_OPCODE_POW
;
5278 if (parse_masked_dst_reg
5279 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->DstReg
.File
,
5280 &vp
->DstReg
.Index
, vp
->DstReg
.WriteMask
))
5282 for (a
= 0; a
< 2; a
++) {
5283 if (parse_scalar_src_reg
5284 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[a
].File
,
5285 &vp
->SrcReg
[a
].Index
, &vp
->SrcReg
[a
].Negate
,
5286 vp
->SrcReg
[a
].Swizzle
))
5294 vp
->Opcode
= VP_OPCODE_ADD
;
5297 vp
->Opcode
= VP_OPCODE_DP3
;
5300 vp
->Opcode
= VP_OPCODE_DP4
;
5303 vp
->Opcode
= VP_OPCODE_DPH
;
5306 vp
->Opcode
= VP_OPCODE_DST
;
5309 vp
->Opcode
= VP_OPCODE_MAX
;
5312 vp
->Opcode
= VP_OPCODE_MIN
;
5315 vp
->Opcode
= VP_OPCODE_MUL
;
5318 vp
->Opcode
= VP_OPCODE_SGE
;
5321 vp
->Opcode
= VP_OPCODE_SLT
;
5324 vp
->Opcode
= VP_OPCODE_SUB
;
5327 vp
->Opcode
= VP_OPCODE_XPD
;
5330 if (parse_masked_dst_reg
5331 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->DstReg
.File
,
5332 &vp
->DstReg
.Index
, vp
->DstReg
.WriteMask
))
5334 for (a
= 0; a
< 2; a
++) {
5335 if (parse_vector_src_reg
5336 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[a
].File
,
5337 &vp
->SrcReg
[a
].Index
, &vp
->SrcReg
[a
].Negate
,
5338 vp
->SrcReg
[a
].Swizzle
))
5346 vp
->Opcode
= VP_OPCODE_MAD
;
5350 if (parse_masked_dst_reg
5351 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->DstReg
.File
,
5352 &vp
->DstReg
.Index
, vp
->DstReg
.WriteMask
))
5354 for (a
= 0; a
< 3; a
++) {
5355 if (parse_vector_src_reg
5356 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[a
].File
,
5357 &vp
->SrcReg
[a
].Index
, &vp
->SrcReg
[a
].Negate
,
5358 vp
->SrcReg
[a
].Swizzle
))
5366 vp
->Opcode
= VP_OPCODE_SWZ
;
5369 if (parse_masked_dst_reg
5370 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->DstReg
.File
,
5371 &vp
->DstReg
.Index
, vp
->DstReg
.WriteMask
))
5375 (ctx
, inst
, vc_head
, Program
, (GLint
*) & vp
->SrcReg
[0].File
,
5376 &vp
->SrcReg
[0].Index
))
5378 parse_extended_swizzle_mask (inst
, vp
->SrcReg
[0].Swizzle
,
5379 &vp
->SrcReg
[0].Negate
);
5388 print_state_token (GLint token
)
5391 case STATE_MATERIAL
:
5392 fprintf (stderr
, "STATE_MATERIAL ");
5395 fprintf (stderr
, "STATE_LIGHT ");
5398 case STATE_LIGHTMODEL_AMBIENT
:
5399 fprintf (stderr
, "STATE_AMBIENT ");
5402 case STATE_LIGHTMODEL_SCENECOLOR
:
5403 fprintf (stderr
, "STATE_SCENECOLOR ");
5406 case STATE_LIGHTPROD
:
5407 fprintf (stderr
, "STATE_LIGHTPROD ");
5411 fprintf (stderr
, "STATE_TEXGEN ");
5414 case STATE_FOG_COLOR
:
5415 fprintf (stderr
, "STATE_FOG_COLOR ");
5418 case STATE_FOG_PARAMS
:
5419 fprintf (stderr
, "STATE_FOG_PARAMS ");
5422 case STATE_CLIPPLANE
:
5423 fprintf (stderr
, "STATE_CLIPPLANE ");
5426 case STATE_POINT_SIZE
:
5427 fprintf (stderr
, "STATE_POINT_SIZE ");
5430 case STATE_POINT_ATTENUATION
:
5431 fprintf (stderr
, "STATE_ATTENUATION ");
5435 fprintf (stderr
, "STATE_MATRIX ");
5438 case STATE_MODELVIEW
:
5439 fprintf (stderr
, "STATE_MODELVIEW ");
5442 case STATE_PROJECTION
:
5443 fprintf (stderr
, "STATE_PROJECTION ");
5447 fprintf (stderr
, "STATE_MVP ");
5451 fprintf (stderr
, "STATE_TEXTURE ");
5455 fprintf (stderr
, "STATE_PROGRAM ");
5458 case STATE_MATRIX_INVERSE
:
5459 fprintf (stderr
, "STATE_INVERSE ");
5462 case STATE_MATRIX_TRANSPOSE
:
5463 fprintf (stderr
, "STATE_TRANSPOSE ");
5466 case STATE_MATRIX_INVTRANS
:
5467 fprintf (stderr
, "STATE_INVTRANS ");
5471 fprintf (stderr
, "STATE_AMBIENT ");
5475 fprintf (stderr
, "STATE_DIFFUSE ");
5478 case STATE_SPECULAR
:
5479 fprintf (stderr
, "STATE_SPECULAR ");
5482 case STATE_EMISSION
:
5483 fprintf (stderr
, "STATE_EMISSION ");
5486 case STATE_SHININESS
:
5487 fprintf (stderr
, "STATE_SHININESS ");
5491 fprintf (stderr
, "STATE_HALF ");
5494 case STATE_POSITION
:
5495 fprintf (stderr
, "STATE_POSITION ");
5498 case STATE_ATTENUATION
:
5499 fprintf (stderr
, "STATE_ATTENUATION ");
5502 case STATE_SPOT_DIRECTION
:
5503 fprintf (stderr
, "STATE_DIRECTION ");
5506 case STATE_TEXGEN_EYE_S
:
5507 fprintf (stderr
, "STATE_TEXGEN_EYE_S ");
5510 case STATE_TEXGEN_EYE_T
:
5511 fprintf (stderr
, "STATE_TEXGEN_EYE_T ");
5514 case STATE_TEXGEN_EYE_R
:
5515 fprintf (stderr
, "STATE_TEXGEN_EYE_R ");
5518 case STATE_TEXGEN_EYE_Q
:
5519 fprintf (stderr
, "STATE_TEXGEN_EYE_Q ");
5522 case STATE_TEXGEN_OBJECT_S
:
5523 fprintf (stderr
, "STATE_TEXGEN_EYE_S ");
5526 case STATE_TEXGEN_OBJECT_T
:
5527 fprintf (stderr
, "STATE_TEXGEN_OBJECT_T ");
5530 case STATE_TEXGEN_OBJECT_R
:
5531 fprintf (stderr
, "STATE_TEXGEN_OBJECT_R ");
5534 case STATE_TEXGEN_OBJECT_Q
:
5535 fprintf (stderr
, "STATE_TEXGEN_OBJECT_Q ");
5538 case STATE_TEXENV_COLOR
:
5539 fprintf (stderr
, "STATE_TEXENV_COLOR ");
5542 case STATE_DEPTH_RANGE
:
5543 fprintf (stderr
, "STATE_DEPTH_RANGE ");
5546 case STATE_VERTEX_PROGRAM
:
5547 fprintf (stderr
, "STATE_VERTEX_PROGRAM ");
5550 case STATE_FRAGMENT_PROGRAM
:
5551 fprintf (stderr
, "STATE_FRAGMENT_PROGRAM ");
5555 fprintf (stderr
, "STATE_ENV ");
5559 fprintf (stderr
, "STATE_LOCAL ");
5563 fprintf (stderr
, "[%d] ", token
);
5568 debug_variables (GLcontext
* ctx
, struct var_cache
*vc_head
,
5569 struct arb_program
*Program
)
5571 struct var_cache
*vc
;
5574 fprintf (stderr
, "debug_variables, vc_head: %x\n", vc_head
);
5576 /* First of all, print out the contents of the var_cache */
5579 fprintf (stderr
, "[%x]\n", vc
);
5582 fprintf (stderr
, "UNDEFINED %s\n", vc
->name
);
5585 fprintf (stderr
, "ATTRIB %s\n", vc
->name
);
5586 fprintf (stderr
, " binding: 0x%x\n", vc
->attrib_binding
);
5589 fprintf (stderr
, "PARAM %s begin: %d len: %d\n", vc
->name
,
5590 vc
->param_binding_begin
, vc
->param_binding_length
);
5591 b
= vc
->param_binding_begin
;
5592 for (a
= 0; a
< vc
->param_binding_length
; a
++) {
5593 fprintf (stderr
, "%s\n",
5594 Program
->Parameters
->Parameters
[a
+ b
].Name
);
5595 if (Program
->Parameters
->Parameters
[a
+ b
].Type
== STATE
) {
5596 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
5598 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
5600 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
5602 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
5604 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
5606 print_state_token (Program
->Parameters
->Parameters
[a
+ b
].
5610 fprintf (stderr
, "%f %f %f %f\n",
5611 Program
->Parameters
->Parameters
[a
+ b
].Values
[0],
5612 Program
->Parameters
->Parameters
[a
+ b
].Values
[1],
5613 Program
->Parameters
->Parameters
[a
+ b
].Values
[2],
5614 Program
->Parameters
->Parameters
[a
+ b
].Values
[3]);
5618 fprintf (stderr
, "TEMP %s\n", vc
->name
);
5619 fprintf (stderr
, " binding: 0x%x\n", vc
->temp_binding
);
5622 fprintf (stderr
, "OUTPUT %s\n", vc
->name
);
5623 fprintf (stderr
, " binding: 0x%x\n", vc
->output_binding
);
5626 fprintf (stderr
, "ALIAS %s\n", vc
->name
);
5627 fprintf (stderr
, " binding: 0x%x (%s)\n",
5628 vc
->alias_binding
, vc
->alias_binding
->name
);
5639 * The main loop for parsing a fragment or vertex program
5641 * \return 0 on sucess, 1 on error
5644 parse_arb_program (GLcontext
* ctx
, GLubyte
* inst
, struct var_cache
**vc_head
,
5645 struct arb_program
*Program
)
5649 Program
->MajorVersion
= (GLuint
) * inst
++;
5650 Program
->MinorVersion
= (GLuint
) * inst
++;
5652 while (*inst
!= END
) {
5657 case ARB_PRECISION_HINT_FASTEST
:
5658 Program
->HintPrecisionFastest
= 1;
5661 case ARB_PRECISION_HINT_NICEST
:
5662 Program
->HintPrecisionNicest
= 1;
5666 Program
->HintFogExp
= 1;
5670 Program
->HintFogExp2
= 1;
5673 case ARB_FOG_LINEAR
:
5674 Program
->HintFogLinear
= 1;
5677 case ARB_POSITION_INVARIANT
:
5678 Program
->HintPositionInvariant
= 1;
5684 Program
->Position
= parse_position (&inst
);
5686 if (Program
->type
== GL_FRAGMENT_PROGRAM_ARB
) {
5687 /* Realloc Program->FPInstructions */
5688 Program
->FPInstructions
=
5689 (struct fp_instruction
*) _mesa_realloc (Program
->FPInstructions
,
5690 Program
->Base
.NumInstructions
*sizeof(struct fp_instruction
),
5691 (Program
->Base
.NumInstructions
+1)*sizeof (struct fp_instruction
));
5693 /* parse the current instruction */
5694 err
= parse_fp_instruction (ctx
, &inst
, vc_head
, Program
,
5695 &Program
->FPInstructions
[Program
->Base
.NumInstructions
]);
5699 /* Realloc Program->VPInstructions */
5700 Program
->VPInstructions
=
5701 (struct vp_instruction
*) _mesa_realloc (Program
->VPInstructions
,
5702 Program
->Base
.NumInstructions
*sizeof(struct vp_instruction
),
5703 (Program
->Base
.NumInstructions
+1)*sizeof(struct vp_instruction
));
5705 /* parse the current instruction */
5706 err
= parse_vp_instruction (ctx
, &inst
, vc_head
, Program
,
5707 &Program
->VPInstructions
[Program
->Base
.NumInstructions
]);
5710 /* increment Program->Base.NumInstructions */
5711 Program
->Base
.NumInstructions
++;
5715 err
= parse_declaration (ctx
, &inst
, vc_head
, Program
);
5726 /* Finally, tag on an OPCODE_END instruction */
5727 if (Program
->type
== GL_FRAGMENT_PROGRAM_ARB
) {
5728 Program
->FPInstructions
=
5729 (struct fp_instruction
*) _mesa_realloc (Program
->FPInstructions
,
5730 Program
->Base
.NumInstructions
*sizeof(struct fp_instruction
),
5731 (Program
->Base
.NumInstructions
+1)*sizeof(struct fp_instruction
));
5733 Program
->FPInstructions
[Program
->Base
.NumInstructions
].Opcode
= FP_OPCODE_END
;
5736 Program
->VPInstructions
=
5737 (struct vp_instruction
*) _mesa_realloc (Program
->VPInstructions
,
5738 Program
->Base
.NumInstructions
*sizeof(struct vp_instruction
),
5739 (Program
->Base
.NumInstructions
+1)*sizeof(struct vp_instruction
));
5741 Program
->VPInstructions
[Program
->Base
.NumInstructions
].Opcode
= VP_OPCODE_END
;
5744 /* increment Program->Base.NumInstructions */
5745 Program
->Base
.NumInstructions
++;
5752 * This kicks everything off.
5754 * \param ctx - The GL Context
5755 * \param str - The program string
5756 * \param len - The program string length
5757 * \param Program - The arb_program struct to return all the parsed info in
5758 * \return 0 on sucess, 1 on error
5761 _mesa_parse_arb_program (GLcontext
* ctx
, const GLubyte
* str
, GLsizei len
,
5762 struct arb_program
* program
)
5764 GLint a
, err
, error_pos
;
5765 char error_msg
[300];
5767 struct var_cache
*vc_head
;
5769 GLubyte
*parsed
, *inst
;
5772 fprintf (stderr
, "Loading grammar text!\n");
5774 dt
= grammar_load_from_text ((GLubyte
*) arb_grammar_text
);
5776 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
5777 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
5778 _mesa_error (ctx
, GL_INVALID_OPERATION
,
5779 "Error loading grammer rule set");
5784 printf ("Checking Grammar!\n");
5786 err
= grammar_check (dt
, str
, &parsed
, &parsed_len
);
5789 /* Syntax parse error */
5791 grammar_get_last_error ((GLubyte
*) error_msg
, 300, &error_pos
);
5792 _mesa_set_program_error (ctx
, error_pos
, error_msg
);
5793 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Parse Error");
5800 printf ("Destroying grammer dict [parse retval: %d]\n", err
);
5804 /* Initialize the arb_program struct */
5805 program
->Base
.NumInstructions
=
5806 program
->Base
.NumTemporaries
=
5807 program
->Base
.NumParameters
=
5808 program
->Base
.NumAttributes
= program
->Base
.NumAddressRegs
= 0;
5809 program
->Parameters
= _mesa_new_parameter_list ();
5810 program
->InputsRead
= 0;
5811 program
->OutputsWritten
= 0;
5812 program
->Position
= 0;
5813 program
->MajorVersion
= program
->MinorVersion
= 0;
5814 program
->HintPrecisionFastest
=
5815 program
->HintPrecisionNicest
=
5816 program
->HintFogExp2
=
5817 program
->HintFogExp
=
5818 program
->HintFogLinear
= program
->HintPositionInvariant
= 0;
5819 for (a
= 0; a
< MAX_TEXTURE_IMAGE_UNITS
; a
++)
5820 program
->TexturesUsed
[a
] = 0;
5821 program
->NumAluInstructions
=
5822 program
->NumTexInstructions
=
5823 program
->NumTexIndirections
= 0;
5825 program
->FPInstructions
= NULL
;
5826 program
->VPInstructions
= NULL
;
5831 /* Start examining the tokens in the array */
5834 /* Check the grammer rev */
5835 if (*inst
++ != REVISION
) {
5836 _mesa_set_program_error (ctx
, 0, "Grammar version mismatch");
5837 _mesa_error (ctx
, GL_INVALID_OPERATION
, "Grammar verison mismatch");
5842 case FRAGMENT_PROGRAM
:
5843 program
->type
= GL_FRAGMENT_PROGRAM_ARB
;
5846 case VERTEX_PROGRAM
:
5847 program
->type
= GL_VERTEX_PROGRAM_ARB
;
5851 err
= parse_arb_program (ctx
, inst
, &vc_head
, program
);
5853 fprintf (stderr
, "Symantic analysis returns %d [1 is bad!]\n", err
);
5857 /*debug_variables(ctx, vc_head, program); */
5859 /* We're done with the parsed binary array */
5860 var_cache_destroy (&vc_head
);
5862 _mesa_free (parsed
);
5864 printf ("_mesa_parse_arb_program() done\n");