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.
27 * NVIDIA vertex program parser.
38 #include "nvprogram.h"
39 #include "nvvertparse.h"
40 #include "nvvertprog.h"
43 * Current parsing state. This structure is passed among the parsing
44 * functions and keeps track of the current parser position and various
51 const GLubyte
*curLine
;
52 GLboolean isStateProgram
;
53 GLboolean isPositionInvariant
;
54 GLboolean isVersion1_1
;
56 GLuint outputsWritten
;
57 GLboolean anyProgRegsWritten
;
58 GLuint numInst
; /* number of instructions parsed */
63 * Called whenever we find an error during parsing.
66 record_error(struct parse_state
*parseState
, const char *msg
, int lineNo
)
70 const GLubyte
*lineStr
;
71 lineStr
= _mesa_find_line_column(parseState
->start
,
72 parseState
->pos
, &line
, &column
);
73 _mesa_debug(parseState
->ctx
,
74 "nvfragparse.c(%d): line %d, column %d:%s (%s)\n",
75 lineNo
, line
, column
, (char *) lineStr
, msg
);
76 _mesa_free((void *) lineStr
);
81 /* Check that no error was already recorded. Only record the first one. */
82 if (parseState
->ctx
->Program
.ErrorString
[0] == 0) {
83 _mesa_set_program_error(parseState
->ctx
,
84 parseState
->pos
- parseState
->start
,
90 #define RETURN_ERROR \
92 record_error(parseState, "Unexpected end of input.", __LINE__); \
96 #define RETURN_ERROR1(msg) \
98 record_error(parseState, msg, __LINE__); \
102 #define RETURN_ERROR2(msg1, msg2) \
105 _mesa_sprintf(err, "%s %s", msg1, msg2); \
106 record_error(parseState, err, __LINE__); \
114 static GLboolean
IsLetter(GLubyte b
)
116 return (b
>= 'a' && b
<= 'z') || (b
>= 'A' && b
<= 'Z');
120 static GLboolean
IsDigit(GLubyte b
)
122 return b
>= '0' && b
<= '9';
126 static GLboolean
IsWhitespace(GLubyte b
)
128 return b
== ' ' || b
== '\t' || b
== '\n' || b
== '\r';
133 * Starting at 'str' find the next token. A token can be an integer,
134 * an identifier or punctuation symbol.
135 * \return <= 0 we found an error, else, return number of characters parsed.
138 GetToken(struct parse_state
*parseState
, GLubyte
*token
)
140 const GLubyte
*str
= parseState
->pos
;
145 /* skip whitespace and comments */
146 while (str
[i
] && (IsWhitespace(str
[i
]) || str
[i
] == '#')) {
149 while (str
[i
] && (str
[i
] != '\n' && str
[i
] != '\r')) {
152 if (str
[i
] == '\n' || str
[i
] == '\r')
153 parseState
->curLine
= str
+ i
+ 1;
156 /* skip whitespace */
157 if (str
[i
] == '\n' || str
[i
] == '\r')
158 parseState
->curLine
= str
+ i
+ 1;
166 /* try matching an integer */
167 while (str
[i
] && IsDigit(str
[i
])) {
168 token
[j
++] = str
[i
++];
170 if (j
> 0 || !str
[i
]) {
175 /* try matching an identifier */
176 if (IsLetter(str
[i
])) {
177 while (str
[i
] && (IsLetter(str
[i
]) || IsDigit(str
[i
]))) {
178 token
[j
++] = str
[i
++];
184 /* punctuation character */
198 * Get next token from input stream and increment stream pointer past token.
201 Parse_Token(struct parse_state
*parseState
, GLubyte
*token
)
204 i
= GetToken(parseState
, token
);
206 parseState
->pos
+= (-i
);
209 parseState
->pos
+= i
;
215 * Get next token from input stream but don't increment stream pointer.
218 Peek_Token(struct parse_state
*parseState
, GLubyte
*token
)
221 i
= GetToken(parseState
, token
);
223 parseState
->pos
+= (-i
);
226 len
= _mesa_strlen((const char *) token
);
227 parseState
->pos
+= (i
- len
);
233 * Try to match 'pattern' as the next token after any whitespace/comments.
234 * Advance the current parsing position only if we match the pattern.
235 * \return GL_TRUE if pattern is matched, GL_FALSE otherwise.
238 Parse_String(struct parse_state
*parseState
, const char *pattern
)
243 /* skip whitespace and comments */
244 while (IsWhitespace(*parseState
->pos
) || *parseState
->pos
== '#') {
245 if (*parseState
->pos
== '#') {
246 while (*parseState
->pos
&& (*parseState
->pos
!= '\n' && *parseState
->pos
!= '\r')) {
247 parseState
->pos
+= 1;
249 if (*parseState
->pos
== '\n' || *parseState
->pos
== '\r')
250 parseState
->curLine
= parseState
->pos
+ 1;
253 /* skip whitespace */
254 if (*parseState
->pos
== '\n' || *parseState
->pos
== '\r')
255 parseState
->curLine
= parseState
->pos
+ 1;
256 parseState
->pos
+= 1;
260 /* Try to match the pattern */
262 for (i
= 0; pattern
[i
]; i
++) {
263 if (*m
!= (GLubyte
) pattern
[i
])
269 return GL_TRUE
; /* success */
273 /**********************************************************************/
275 static const char *InputRegisters
[MAX_NV_VERTEX_PROGRAM_INPUTS
+ 1] = {
276 "OPOS", "WGHT", "NRML", "COL0", "COL1", "FOGC", "6", "7",
277 "TEX0", "TEX1", "TEX2", "TEX3", "TEX4", "TEX5", "TEX6", "TEX7", NULL
280 static const char *OutputRegisters
[MAX_NV_VERTEX_PROGRAM_OUTPUTS
+ 1] = {
281 "HPOS", "COL0", "COL1", "BFC0", "BFC1", "FOGC", "PSIZ",
282 "TEX0", "TEX1", "TEX2", "TEX3", "TEX4", "TEX5", "TEX6", "TEX7", NULL
285 /* NOTE: the order here must match opcodes in nvvertprog.h */
286 static const char *Opcodes
[] = {
287 "MOV", "LIT", "RCP", "RSQ", "EXP", "LOG", "MUL", "ADD", "DP3", "DP4",
288 "DST", "MIN", "MAX", "SLT", "SGE", "MAD", "ARL", "DPH", "RCC", "SUB",
290 /* GL_ARB_vertex_program */
291 "FLR", "FRC", "EX2", "LG2", "POW", "XPD", "SWZ",
298 * Parse a temporary register: Rnn
301 Parse_TempReg(struct parse_state
*parseState
, GLint
*tempRegNum
)
305 /* Should be 'R##' */
306 if (!Parse_Token(parseState
, token
))
309 RETURN_ERROR1("Expected R##");
311 if (IsDigit(token
[1])) {
312 GLint reg
= _mesa_atoi((char *) (token
+ 1));
313 if (reg
>= MAX_NV_VERTEX_PROGRAM_TEMPS
)
314 RETURN_ERROR1("Bad temporary register name");
318 RETURN_ERROR1("Bad temporary register name");
326 * Parse address register "A0.x"
329 Parse_AddrReg(struct parse_state
*parseState
)
332 if (!Parse_String(parseState
, "A0"))
336 if (!Parse_String(parseState
, "."))
340 if (!Parse_String(parseState
, "x"))
348 * Parse absolute program parameter register "c[##]"
351 Parse_AbsParamReg(struct parse_state
*parseState
, GLint
*regNum
)
355 if (!Parse_String(parseState
, "c"))
358 if (!Parse_String(parseState
, "["))
361 if (!Parse_Token(parseState
, token
))
364 if (IsDigit(token
[0])) {
365 /* a numbered program parameter register */
366 GLint reg
= _mesa_atoi((char *) token
);
367 if (reg
>= MAX_NV_VERTEX_PROGRAM_PARAMS
)
368 RETURN_ERROR1("Bad program parameter number");
375 if (!Parse_String(parseState
, "]"))
383 Parse_ParamReg(struct parse_state
*parseState
, struct vp_src_register
*srcReg
)
387 if (!Parse_String(parseState
, "c"))
390 if (!Parse_String(parseState
, "["))
393 if (!Peek_Token(parseState
, token
))
396 if (IsDigit(token
[0])) {
397 /* a numbered program parameter register */
399 (void) Parse_Token(parseState
, token
);
400 reg
= _mesa_atoi((char *) token
);
401 if (reg
>= MAX_NV_VERTEX_PROGRAM_PARAMS
)
402 RETURN_ERROR1("Bad program parameter number");
403 srcReg
->File
= PROGRAM_ENV_PARAM
;
406 else if (_mesa_strcmp((const char *) token
, "A0") == 0) {
407 /* address register "A0.x" */
408 if (!Parse_AddrReg(parseState
))
411 srcReg
->RelAddr
= GL_TRUE
;
412 srcReg
->File
= PROGRAM_ENV_PARAM
;
413 /* Look for +/-N offset */
414 if (!Peek_Token(parseState
, token
))
417 if (token
[0] == '-' || token
[0] == '+') {
418 const GLubyte sign
= token
[0];
419 (void) Parse_Token(parseState
, token
); /* consume +/- */
421 /* an integer should be next */
422 if (!Parse_Token(parseState
, token
))
425 if (IsDigit(token
[0])) {
426 const GLint k
= _mesa_atoi((char *) token
);
429 RETURN_ERROR1("Bad address offset");
434 RETURN_ERROR1("Bad address offset");
443 /* probably got a ']', catch it below */
450 /* Match closing ']' */
451 if (!Parse_String(parseState
, "]"))
459 * Parse v[#] or v[<name>]
462 Parse_AttribReg(struct parse_state
*parseState
, GLint
*tempRegNum
)
468 if (!Parse_String(parseState
, "v"))
472 if (!Parse_String(parseState
, "["))
475 /* match number or named register */
476 if (!Parse_Token(parseState
, token
))
479 if (parseState
->isStateProgram
&& token
[0] != '0')
480 RETURN_ERROR1("Only v[0] accessible in vertex state programs");
482 if (IsDigit(token
[0])) {
483 GLint reg
= _mesa_atoi((char *) token
);
484 if (reg
>= MAX_NV_VERTEX_PROGRAM_INPUTS
)
485 RETURN_ERROR1("Bad vertex attribute register name");
489 for (j
= 0; InputRegisters
[j
]; j
++) {
490 if (_mesa_strcmp((const char *) token
, InputRegisters
[j
]) == 0) {
495 if (!InputRegisters
[j
]) {
496 /* unknown input register label */
497 RETURN_ERROR2("Bad register name", token
);
502 if (!Parse_String(parseState
, "]"))
510 Parse_OutputReg(struct parse_state
*parseState
, GLint
*outputRegNum
)
516 if (!Parse_String(parseState
, "o"))
520 if (!Parse_String(parseState
, "["))
523 /* Get output reg name */
524 if (!Parse_Token(parseState
, token
))
527 if (parseState
->isPositionInvariant
)
528 start
= 1; /* skip HPOS register name */
532 /* try to match an output register name */
533 for (j
= start
; OutputRegisters
[j
]; j
++) {
534 if (_mesa_strcmp((const char *) token
, OutputRegisters
[j
]) == 0) {
539 if (!OutputRegisters
[j
])
540 RETURN_ERROR1("Unrecognized output register name");
543 if (!Parse_String(parseState
, "]"))
544 RETURN_ERROR1("Expected ]");
551 Parse_MaskedDstReg(struct parse_state
*parseState
, struct vp_dst_register
*dstReg
)
555 /* Dst reg can be R<n> or o[n] */
556 if (!Peek_Token(parseState
, token
))
559 if (token
[0] == 'R') {
560 /* a temporary register */
561 dstReg
->File
= PROGRAM_TEMPORARY
;
562 if (!Parse_TempReg(parseState
, &dstReg
->Index
))
565 else if (!parseState
->isStateProgram
&& token
[0] == 'o') {
566 /* an output register */
567 dstReg
->File
= PROGRAM_OUTPUT
;
568 if (!Parse_OutputReg(parseState
, &dstReg
->Index
))
571 else if (parseState
->isStateProgram
&& token
[0] == 'c') {
572 /* absolute program parameter register */
573 dstReg
->File
= PROGRAM_ENV_PARAM
;
574 if (!Parse_AbsParamReg(parseState
, &dstReg
->Index
))
578 RETURN_ERROR1("Bad destination register name");
581 /* Parse optional write mask */
582 if (!Peek_Token(parseState
, token
))
585 if (token
[0] == '.') {
589 if (!Parse_String(parseState
, "."))
592 if (!Parse_Token(parseState
, token
))
595 dstReg
->WriteMask
[0] = GL_FALSE
;
596 dstReg
->WriteMask
[1] = GL_FALSE
;
597 dstReg
->WriteMask
[2] = GL_FALSE
;
598 dstReg
->WriteMask
[3] = GL_FALSE
;
600 if (token
[k
] == 'x') {
601 dstReg
->WriteMask
[0] = GL_TRUE
;
604 if (token
[k
] == 'y') {
605 dstReg
->WriteMask
[1] = GL_TRUE
;
608 if (token
[k
] == 'z') {
609 dstReg
->WriteMask
[2] = GL_TRUE
;
612 if (token
[k
] == 'w') {
613 dstReg
->WriteMask
[3] = GL_TRUE
;
617 RETURN_ERROR1("Bad writemask character");
622 dstReg
->WriteMask
[0] = GL_TRUE
;
623 dstReg
->WriteMask
[1] = GL_TRUE
;
624 dstReg
->WriteMask
[2] = GL_TRUE
;
625 dstReg
->WriteMask
[3] = GL_TRUE
;
632 Parse_SwizzleSrcReg(struct parse_state
*parseState
, struct vp_src_register
*srcReg
)
636 srcReg
->RelAddr
= GL_FALSE
;
639 if (!Peek_Token(parseState
, token
))
641 if (token
[0] == '-') {
642 (void) Parse_String(parseState
, "-");
643 srcReg
->Negate
= GL_TRUE
;
644 if (!Peek_Token(parseState
, token
))
648 srcReg
->Negate
= GL_FALSE
;
651 /* Src reg can be R<n>, c[n], c[n +/- offset], or a named vertex attrib */
652 if (token
[0] == 'R') {
653 srcReg
->File
= PROGRAM_TEMPORARY
;
654 if (!Parse_TempReg(parseState
, &srcReg
->Index
))
657 else if (token
[0] == 'c') {
658 if (!Parse_ParamReg(parseState
, srcReg
))
661 else if (token
[0] == 'v') {
662 srcReg
->File
= PROGRAM_INPUT
;
663 if (!Parse_AttribReg(parseState
, &srcReg
->Index
))
667 RETURN_ERROR2("Bad source register name", token
);
670 /* init swizzle fields */
671 srcReg
->Swizzle
[0] = 0;
672 srcReg
->Swizzle
[1] = 1;
673 srcReg
->Swizzle
[2] = 2;
674 srcReg
->Swizzle
[3] = 3;
676 /* Look for optional swizzle suffix */
677 if (!Peek_Token(parseState
, token
))
679 if (token
[0] == '.') {
680 (void) Parse_String(parseState
, "."); /* consume . */
682 if (!Parse_Token(parseState
, token
))
686 /* single letter swizzle */
688 ASSIGN_4V(srcReg
->Swizzle
, 0, 0, 0, 0);
689 else if (token
[0] == 'y')
690 ASSIGN_4V(srcReg
->Swizzle
, 1, 1, 1, 1);
691 else if (token
[0] == 'z')
692 ASSIGN_4V(srcReg
->Swizzle
, 2, 2, 2, 2);
693 else if (token
[0] == 'w')
694 ASSIGN_4V(srcReg
->Swizzle
, 3, 3, 3, 3);
696 RETURN_ERROR1("Expected x, y, z, or w");
699 /* 2, 3 or 4-component swizzle */
701 for (k
= 0; token
[k
] && k
< 5; k
++) {
703 srcReg
->Swizzle
[k
] = 0;
704 else if (token
[k
] == 'y')
705 srcReg
->Swizzle
[k
] = 1;
706 else if (token
[k
] == 'z')
707 srcReg
->Swizzle
[k
] = 2;
708 else if (token
[k
] == 'w')
709 srcReg
->Swizzle
[k
] = 3;
723 Parse_ScalarSrcReg(struct parse_state
*parseState
, struct vp_src_register
*srcReg
)
727 srcReg
->RelAddr
= GL_FALSE
;
730 if (!Peek_Token(parseState
, token
))
732 if (token
[0] == '-') {
733 srcReg
->Negate
= GL_TRUE
;
734 (void) Parse_String(parseState
, "-"); /* consume '-' */
735 if (!Peek_Token(parseState
, token
))
739 srcReg
->Negate
= GL_FALSE
;
742 /* Src reg can be R<n>, c[n], c[n +/- offset], or a named vertex attrib */
743 if (token
[0] == 'R') {
744 srcReg
->File
= PROGRAM_TEMPORARY
;
745 if (!Parse_TempReg(parseState
, &srcReg
->Index
))
748 else if (token
[0] == 'c') {
749 if (!Parse_ParamReg(parseState
, srcReg
))
752 else if (token
[0] == 'v') {
753 srcReg
->File
= PROGRAM_INPUT
;
754 if (!Parse_AttribReg(parseState
, &srcReg
->Index
))
758 RETURN_ERROR2("Bad source register name", token
);
761 /* Look for .[xyzw] suffix */
762 if (!Parse_String(parseState
, "."))
765 if (!Parse_Token(parseState
, token
))
768 if (token
[0] == 'x' && token
[1] == 0) {
769 srcReg
->Swizzle
[0] = 0;
771 else if (token
[0] == 'y' && token
[1] == 0) {
772 srcReg
->Swizzle
[0] = 1;
774 else if (token
[0] == 'z' && token
[1] == 0) {
775 srcReg
->Swizzle
[0] = 2;
777 else if (token
[0] == 'w' && token
[1] == 0) {
778 srcReg
->Swizzle
[0] = 3;
781 RETURN_ERROR1("Bad scalar source suffix");
783 srcReg
->Swizzle
[1] = srcReg
->Swizzle
[2] = srcReg
->Swizzle
[3] = 0;
790 Parse_UnaryOpInstruction(struct parse_state
*parseState
,
791 struct vp_instruction
*inst
, enum vp_opcode opcode
)
793 if (opcode
== VP_OPCODE_ABS
&& !parseState
->isVersion1_1
)
794 RETURN_ERROR1("ABS illegal for vertex program 1.0");
796 inst
->Opcode
= opcode
;
797 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
800 if (!Parse_MaskedDstReg(parseState
, &inst
->DstReg
))
804 if (!Parse_String(parseState
, ","))
808 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[0]))
812 if (!Parse_String(parseState
, ";"))
820 Parse_BiOpInstruction(struct parse_state
*parseState
,
821 struct vp_instruction
*inst
, enum vp_opcode opcode
)
823 if (opcode
== VP_OPCODE_DPH
&& !parseState
->isVersion1_1
)
824 RETURN_ERROR1("DPH illegal for vertex program 1.0");
825 if (opcode
== VP_OPCODE_SUB
&& !parseState
->isVersion1_1
)
826 RETURN_ERROR1("SUB illegal for vertex program 1.0");
828 inst
->Opcode
= opcode
;
829 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
832 if (!Parse_MaskedDstReg(parseState
, &inst
->DstReg
))
836 if (!Parse_String(parseState
, ","))
840 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[0]))
844 if (!Parse_String(parseState
, ","))
848 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[1]))
852 if (!Parse_String(parseState
, ";"))
855 /* make sure we don't reference more than one program parameter register */
856 if (inst
->SrcReg
[0].File
== PROGRAM_ENV_PARAM
&&
857 inst
->SrcReg
[1].File
== PROGRAM_ENV_PARAM
&&
858 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[1].Index
)
859 RETURN_ERROR1("Can't reference two program parameter registers");
861 /* make sure we don't reference more than one vertex attribute register */
862 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
863 inst
->SrcReg
[1].File
== PROGRAM_INPUT
&&
864 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[1].Index
)
865 RETURN_ERROR1("Can't reference two vertex attribute registers");
872 Parse_TriOpInstruction(struct parse_state
*parseState
,
873 struct vp_instruction
*inst
, enum vp_opcode opcode
)
875 inst
->Opcode
= opcode
;
876 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
879 if (!Parse_MaskedDstReg(parseState
, &inst
->DstReg
))
883 if (!Parse_String(parseState
, ","))
887 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[0]))
891 if (!Parse_String(parseState
, ","))
895 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[1]))
899 if (!Parse_String(parseState
, ","))
903 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[2]))
907 if (!Parse_String(parseState
, ";"))
910 /* make sure we don't reference more than one program parameter register */
911 if ((inst
->SrcReg
[0].File
== PROGRAM_ENV_PARAM
&&
912 inst
->SrcReg
[1].File
== PROGRAM_ENV_PARAM
&&
913 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[1].Index
) ||
914 (inst
->SrcReg
[0].File
== PROGRAM_ENV_PARAM
&&
915 inst
->SrcReg
[2].File
== PROGRAM_ENV_PARAM
&&
916 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[2].Index
) ||
917 (inst
->SrcReg
[1].File
== PROGRAM_ENV_PARAM
&&
918 inst
->SrcReg
[2].File
== PROGRAM_ENV_PARAM
&&
919 inst
->SrcReg
[1].Index
!= inst
->SrcReg
[2].Index
))
920 RETURN_ERROR1("Can only reference one program register");
922 /* make sure we don't reference more than one vertex attribute register */
923 if ((inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
924 inst
->SrcReg
[1].File
== PROGRAM_INPUT
&&
925 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[1].Index
) ||
926 (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
927 inst
->SrcReg
[2].File
== PROGRAM_INPUT
&&
928 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[2].Index
) ||
929 (inst
->SrcReg
[1].File
== PROGRAM_INPUT
&&
930 inst
->SrcReg
[2].File
== PROGRAM_INPUT
&&
931 inst
->SrcReg
[1].Index
!= inst
->SrcReg
[2].Index
))
932 RETURN_ERROR1("Can only reference one input register");
939 Parse_ScalarInstruction(struct parse_state
*parseState
,
940 struct vp_instruction
*inst
, enum vp_opcode opcode
)
942 if (opcode
== VP_OPCODE_RCC
&& !parseState
->isVersion1_1
)
943 RETURN_ERROR1("RCC illegal for vertex program 1.0");
945 inst
->Opcode
= opcode
;
946 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
949 if (!Parse_MaskedDstReg(parseState
, &inst
->DstReg
))
953 if (!Parse_String(parseState
, ","))
957 if (!Parse_ScalarSrcReg(parseState
, &inst
->SrcReg
[0]))
961 if (!Parse_String(parseState
, ";"))
969 Parse_AddressInstruction(struct parse_state
*parseState
, struct vp_instruction
*inst
)
971 inst
->Opcode
= VP_OPCODE_ARL
;
972 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
975 if (!Parse_AddrReg(parseState
))
979 if (!Parse_String(parseState
, ","))
983 if (!Parse_ScalarSrcReg(parseState
, &inst
->SrcReg
[0]))
987 if (!Parse_String(parseState
, ";"))
995 Parse_EndInstruction(struct parse_state
*parseState
, struct vp_instruction
*inst
)
999 inst
->Opcode
= VP_OPCODE_END
;
1000 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
1002 /* this should fail! */
1003 if (Parse_Token(parseState
, token
))
1004 RETURN_ERROR2("Unexpected token after END:", token
);
1011 Parse_OptionSequence(struct parse_state
*parseState
,
1012 struct vp_instruction program
[])
1015 if (!Parse_String(parseState
, "OPTION"))
1016 return GL_TRUE
; /* ok, not an OPTION statement */
1017 if (Parse_String(parseState
, "NV_position_invariant")) {
1018 parseState
->isPositionInvariant
= GL_TRUE
;
1021 RETURN_ERROR1("unexpected OPTION statement");
1023 if (!Parse_String(parseState
, ";"))
1030 Parse_InstructionSequence(struct parse_state
*parseState
,
1031 struct vp_instruction program
[])
1034 struct vp_instruction
*inst
= program
+ parseState
->numInst
;
1036 /* Initialize the instruction */
1037 inst
->SrcReg
[0].File
= (enum register_file
) -1;
1038 inst
->SrcReg
[1].File
= (enum register_file
) -1;
1039 inst
->SrcReg
[2].File
= (enum register_file
) -1;
1040 inst
->DstReg
.File
= (enum register_file
) -1;
1042 if (Parse_String(parseState
, "MOV")) {
1043 if (!Parse_UnaryOpInstruction(parseState
, inst
, VP_OPCODE_MOV
))
1046 else if (Parse_String(parseState
, "LIT")) {
1047 if (!Parse_UnaryOpInstruction(parseState
, inst
, VP_OPCODE_LIT
))
1050 else if (Parse_String(parseState
, "ABS")) {
1051 if (!Parse_UnaryOpInstruction(parseState
, inst
, VP_OPCODE_ABS
))
1054 else if (Parse_String(parseState
, "MUL")) {
1055 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_MUL
))
1058 else if (Parse_String(parseState
, "ADD")) {
1059 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_ADD
))
1062 else if (Parse_String(parseState
, "DP3")) {
1063 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_DP3
))
1066 else if (Parse_String(parseState
, "DP4")) {
1067 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_DP4
))
1070 else if (Parse_String(parseState
, "DST")) {
1071 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_DST
))
1074 else if (Parse_String(parseState
, "MIN")) {
1075 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_MIN
))
1078 else if (Parse_String(parseState
, "MAX")) {
1079 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_MAX
))
1082 else if (Parse_String(parseState
, "SLT")) {
1083 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_SLT
))
1086 else if (Parse_String(parseState
, "SGE")) {
1087 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_SGE
))
1090 else if (Parse_String(parseState
, "DPH")) {
1091 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_DPH
))
1094 else if (Parse_String(parseState
, "SUB")) {
1095 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_SUB
))
1098 else if (Parse_String(parseState
, "MAD")) {
1099 if (!Parse_TriOpInstruction(parseState
, inst
, VP_OPCODE_MAD
))
1102 else if (Parse_String(parseState
, "RCP")) {
1103 if (!Parse_ScalarInstruction(parseState
, inst
, VP_OPCODE_RCP
))
1106 else if (Parse_String(parseState
, "RSQ")) {
1107 if (!Parse_ScalarInstruction(parseState
, inst
, VP_OPCODE_RSQ
))
1110 else if (Parse_String(parseState
, "EXP")) {
1111 if (!Parse_ScalarInstruction(parseState
, inst
, VP_OPCODE_EXP
))
1114 else if (Parse_String(parseState
, "LOG")) {
1115 if (!Parse_ScalarInstruction(parseState
, inst
, VP_OPCODE_LOG
))
1118 else if (Parse_String(parseState
, "RCC")) {
1119 if (!Parse_ScalarInstruction(parseState
, inst
, VP_OPCODE_RCC
))
1122 else if (Parse_String(parseState
, "ARL")) {
1123 if (!Parse_AddressInstruction(parseState
, inst
))
1126 else if (Parse_String(parseState
, "END")) {
1127 if (!Parse_EndInstruction(parseState
, inst
))
1130 parseState
->numInst
++;
1131 return GL_TRUE
; /* all done */
1135 /* bad instruction name */
1136 RETURN_ERROR1("Unexpected token");
1139 /* examine input/output registers */
1140 if (inst
->DstReg
.File
== PROGRAM_OUTPUT
)
1141 parseState
->outputsWritten
|= (1 << inst
->DstReg
.Index
);
1142 else if (inst
->DstReg
.File
== PROGRAM_ENV_PARAM
)
1143 parseState
->anyProgRegsWritten
= GL_TRUE
;
1145 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
)
1146 parseState
->inputsRead
|= (1 << inst
->SrcReg
[0].Index
);
1147 if (inst
->SrcReg
[1].File
== PROGRAM_INPUT
)
1148 parseState
->inputsRead
|= (1 << inst
->SrcReg
[1].Index
);
1149 if (inst
->SrcReg
[2].File
== PROGRAM_INPUT
)
1150 parseState
->inputsRead
|= (1 << inst
->SrcReg
[2].Index
);
1152 parseState
->numInst
++;
1154 if (parseState
->numInst
>= MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
)
1155 RETURN_ERROR1("Program too long");
1163 Parse_Program(struct parse_state
*parseState
,
1164 struct vp_instruction instBuffer
[])
1166 if (parseState
->isVersion1_1
) {
1167 if (!Parse_OptionSequence(parseState
, instBuffer
)) {
1171 return Parse_InstructionSequence(parseState
, instBuffer
);
1176 * Parse/compile the 'str' returning the compiled 'program'.
1177 * ctx->Program.ErrorPos will be -1 if successful. Otherwise, ErrorPos
1178 * indicates the position of the error in 'str'.
1181 _mesa_parse_nv_vertex_program(GLcontext
*ctx
, GLenum dstTarget
,
1182 const GLubyte
*str
, GLsizei len
,
1183 struct vertex_program
*program
)
1185 struct parse_state parseState
;
1186 struct vp_instruction instBuffer
[MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
];
1187 struct vp_instruction
*newInst
;
1189 GLubyte
*programString
;
1191 /* Make a null-terminated copy of the program string */
1192 programString
= (GLubyte
*) MALLOC(len
+ 1);
1193 if (!programString
) {
1194 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glLoadProgramNV");
1197 MEMCPY(programString
, str
, len
);
1198 programString
[len
] = 0;
1200 /* Get ready to parse */
1201 parseState
.ctx
= ctx
;
1202 parseState
.start
= programString
;
1203 parseState
.isPositionInvariant
= GL_FALSE
;
1204 parseState
.isVersion1_1
= GL_FALSE
;
1205 parseState
.numInst
= 0;
1206 parseState
.inputsRead
= 0;
1207 parseState
.outputsWritten
= 0;
1208 parseState
.anyProgRegsWritten
= GL_FALSE
;
1210 /* Reset error state */
1211 _mesa_set_program_error(ctx
, -1, NULL
);
1213 /* check the program header */
1214 if (_mesa_strncmp((const char *) programString
, "!!VP1.0", 7) == 0) {
1215 target
= GL_VERTEX_PROGRAM_NV
;
1216 parseState
.pos
= programString
+ 7;
1217 parseState
.isStateProgram
= GL_FALSE
;
1219 else if (_mesa_strncmp((const char *) programString
, "!!VP1.1", 7) == 0) {
1220 target
= GL_VERTEX_PROGRAM_NV
;
1221 parseState
.pos
= programString
+ 7;
1222 parseState
.isStateProgram
= GL_FALSE
;
1223 parseState
.isVersion1_1
= GL_TRUE
;
1225 else if (_mesa_strncmp((const char *) programString
, "!!VSP1.0", 8) == 0) {
1226 target
= GL_VERTEX_STATE_PROGRAM_NV
;
1227 parseState
.pos
= programString
+ 8;
1228 parseState
.isStateProgram
= GL_TRUE
;
1231 /* invalid header */
1232 ctx
->Program
.ErrorPos
= 0;
1233 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glLoadProgramNV(bad header)");
1237 /* make sure target and header match */
1238 if (target
!= dstTarget
) {
1239 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1240 "glLoadProgramNV(target mismatch)");
1245 if (Parse_Program(&parseState
, instBuffer
)) {
1246 /* successful parse! */
1248 if (parseState
.isStateProgram
) {
1249 if (!parseState
.anyProgRegsWritten
) {
1250 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1251 "glLoadProgramNV(c[#] not written)");
1256 if (!parseState
.isPositionInvariant
&&
1257 !(parseState
.outputsWritten
& 1)) {
1258 /* bit 1 = HPOS register */
1259 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1260 "glLoadProgramNV(HPOS not written)");
1265 /* copy the compiled instructions */
1266 assert(parseState
.numInst
<= MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
);
1267 newInst
= (struct vp_instruction
*)
1268 MALLOC(parseState
.numInst
* sizeof(struct vp_instruction
));
1270 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glLoadProgramNV");
1271 FREE(programString
);
1272 return; /* out of memory */
1274 MEMCPY(newInst
, instBuffer
,
1275 parseState
.numInst
* sizeof(struct vp_instruction
));
1277 /* install the program */
1278 program
->Base
.Target
= target
;
1279 if (program
->Base
.String
) {
1280 FREE(program
->Base
.String
);
1282 program
->Base
.String
= programString
;
1283 program
->Base
.Format
= GL_PROGRAM_FORMAT_ASCII_ARB
;
1284 if (program
->Instructions
) {
1285 FREE(program
->Instructions
);
1287 program
->Instructions
= newInst
;
1288 program
->InputsRead
= parseState
.inputsRead
;
1289 program
->OutputsWritten
= parseState
.outputsWritten
;
1290 program
->IsPositionInvariant
= parseState
.isPositionInvariant
;
1293 _mesa_printf("--- glLoadProgramNV result ---\n");
1294 _mesa_print_nv_vertex_program(program
);
1295 _mesa_printf("------------------------------\n");
1300 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glLoadProgramNV");
1301 /* NOTE: _mesa_set_program_error would have been called already */
1302 /* GL_NV_vertex_program isn't supposed to set the error string
1303 * so we reset it here.
1305 _mesa_set_program_error(ctx
, ctx
->Program
.ErrorPos
, NULL
);
1311 PrintSrcReg(const struct vp_src_register
*src
)
1313 static const char comps
[5] = "xyzw";
1318 _mesa_printf("c[A0.x + %d]", src
->Index
);
1319 else if (src
->Index
< 0)
1320 _mesa_printf("c[A0.x - %d]", -src
->Index
);
1322 _mesa_printf("c[A0.x]");
1324 else if (src
->File
== PROGRAM_OUTPUT
) {
1325 _mesa_printf("o[%s]", OutputRegisters
[src
->Index
]);
1327 else if (src
->File
== PROGRAM_INPUT
) {
1328 _mesa_printf("v[%s]", InputRegisters
[src
->Index
]);
1330 else if (src
->File
== PROGRAM_ENV_PARAM
) {
1331 _mesa_printf("c[%d]", src
->Index
);
1334 ASSERT(src
->File
== PROGRAM_TEMPORARY
);
1335 _mesa_printf("R%d", src
->Index
);
1338 if (src
->Swizzle
[0] == src
->Swizzle
[1] &&
1339 src
->Swizzle
[0] == src
->Swizzle
[2] &&
1340 src
->Swizzle
[0] == src
->Swizzle
[3]) {
1341 _mesa_printf(".%c", comps
[src
->Swizzle
[0]]);
1343 else if (src
->Swizzle
[0] != 0 ||
1344 src
->Swizzle
[1] != 1 ||
1345 src
->Swizzle
[2] != 2 ||
1346 src
->Swizzle
[3] != 3) {
1347 _mesa_printf(".%c%c%c%c",
1348 comps
[src
->Swizzle
[0]],
1349 comps
[src
->Swizzle
[1]],
1350 comps
[src
->Swizzle
[2]],
1351 comps
[src
->Swizzle
[3]]);
1357 PrintDstReg(const struct vp_dst_register
*dst
)
1359 GLint w
= dst
->WriteMask
[0] + dst
->WriteMask
[1]
1360 + dst
->WriteMask
[2] + dst
->WriteMask
[3];
1362 if (dst
->File
== PROGRAM_OUTPUT
) {
1363 _mesa_printf("o[%s]", OutputRegisters
[dst
->Index
]);
1365 else if (dst
->File
== PROGRAM_INPUT
) {
1366 _mesa_printf("v[%s]", InputRegisters
[dst
->Index
]);
1368 else if (dst
->File
== PROGRAM_ENV_PARAM
) {
1369 _mesa_printf("c[%d]", dst
->Index
);
1372 ASSERT(dst
->File
== PROGRAM_TEMPORARY
);
1373 _mesa_printf("R%d", dst
->Index
);
1376 if (w
!= 0 && w
!= 4) {
1378 if (dst
->WriteMask
[0])
1380 if (dst
->WriteMask
[1])
1382 if (dst
->WriteMask
[2])
1384 if (dst
->WriteMask
[3])
1391 * Print a single NVIDIA vertex program instruction.
1394 _mesa_print_nv_vertex_instruction(const struct vp_instruction
*inst
)
1396 switch (inst
->Opcode
) {
1405 _mesa_printf("%s ", Opcodes
[(int) inst
->Opcode
]);
1406 PrintDstReg(&inst
->DstReg
);
1408 PrintSrcReg(&inst
->SrcReg
[0]);
1409 _mesa_printf(";\n");
1422 _mesa_printf("%s ", Opcodes
[(int) inst
->Opcode
]);
1423 PrintDstReg(&inst
->DstReg
);
1425 PrintSrcReg(&inst
->SrcReg
[0]);
1427 PrintSrcReg(&inst
->SrcReg
[1]);
1428 _mesa_printf(";\n");
1431 _mesa_printf("MAD ");
1432 PrintDstReg(&inst
->DstReg
);
1434 PrintSrcReg(&inst
->SrcReg
[0]);
1436 PrintSrcReg(&inst
->SrcReg
[1]);
1438 PrintSrcReg(&inst
->SrcReg
[2]);
1439 _mesa_printf(";\n");
1442 _mesa_printf("ARL A0.x, ");
1443 PrintSrcReg(&inst
->SrcReg
[0]);
1444 _mesa_printf(";\n");
1447 _mesa_printf("END\n");
1450 _mesa_printf("BAD INSTRUCTION\n");
1456 * Print (unparse) the given vertex program. Just for debugging.
1459 _mesa_print_nv_vertex_program(const struct vertex_program
*program
)
1461 const struct vp_instruction
*inst
;
1463 for (inst
= program
->Instructions
; ; inst
++) {
1464 _mesa_print_nv_vertex_instruction(inst
);
1465 if (inst
->Opcode
== VP_OPCODE_END
)
1472 _mesa_nv_vertex_input_register_name(GLuint i
)
1474 ASSERT(i
< MAX_NV_VERTEX_PROGRAM_INPUTS
);
1475 return InputRegisters
[i
];
1480 _mesa_nv_vertex_output_register_name(GLuint i
)
1482 ASSERT(i
< MAX_NV_VERTEX_PROGRAM_OUTPUTS
);
1483 return OutputRegisters
[i
];