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 GLboolean isStateProgram
;
52 GLboolean isPositionInvariant
;
53 GLboolean isVersion1_1
;
55 GLuint outputsWritten
;
56 GLuint progRegsWritten
;
57 GLuint numInst
; /* number of instructions parsed */
62 * Called whenever we find an error during parsing.
65 record_error(struct parse_state
*parseState
, const char *msg
, int lineNo
)
69 const GLubyte
*lineStr
;
70 lineStr
= _mesa_find_line_column(parseState
->start
,
71 parseState
->pos
, &line
, &column
);
72 _mesa_debug(parseState
->ctx
,
73 "nvfragparse.c(%d): line %d, column %d:%s (%s)\n",
74 lineNo
, line
, column
, (char *) lineStr
, msg
);
75 _mesa_free((void *) lineStr
);
80 /* Check that no error was already recorded. Only record the first one. */
81 if (parseState
->ctx
->Program
.ErrorString
[0] == 0) {
82 _mesa_set_program_error(parseState
->ctx
,
83 parseState
->pos
- parseState
->start
,
89 #define RETURN_ERROR \
91 record_error(parseState, "Unexpected end of input.", __LINE__); \
95 #define RETURN_ERROR1(msg) \
97 record_error(parseState, msg, __LINE__); \
101 #define RETURN_ERROR2(msg1, msg2) \
104 _mesa_sprintf(err, "%s %s", msg1, msg2); \
105 record_error(parseState, err, __LINE__); \
113 static GLboolean
IsLetter(GLubyte b
)
115 return (b
>= 'a' && b
<= 'z') || (b
>= 'A' && b
<= 'Z');
119 static GLboolean
IsDigit(GLubyte b
)
121 return b
>= '0' && b
<= '9';
125 static GLboolean
IsWhitespace(GLubyte b
)
127 return b
== ' ' || b
== '\t' || b
== '\n' || b
== '\r';
132 * Starting at 'str' find the next token. A token can be an integer,
133 * an identifier or punctuation symbol.
134 * \return <= 0 we found an error, else, return number of characters parsed.
137 GetToken(const GLubyte
*str
, GLubyte
*token
)
143 /* skip whitespace and comments */
144 while (str
[i
] && (IsWhitespace(str
[i
]) || str
[i
] == '#')) {
147 while (str
[i
] && (str
[i
] != '\n' && str
[i
] != '\r')) {
152 /* skip whitespace */
160 /* try matching an integer */
161 while (str
[i
] && IsDigit(str
[i
])) {
162 token
[j
++] = str
[i
++];
164 if (j
> 0 || !str
[i
]) {
169 /* try matching an identifier */
170 if (IsLetter(str
[i
])) {
171 while (str
[i
] && (IsLetter(str
[i
]) || IsDigit(str
[i
]))) {
172 token
[j
++] = str
[i
++];
192 * Get next token from input stream and increment stream pointer past token.
195 Parse_Token(struct parse_state
*parseState
, GLubyte
*token
)
198 i
= GetToken(parseState
->pos
, token
);
200 parseState
->pos
+= (-i
);
203 parseState
->pos
+= i
;
209 * Get next token from input stream but don't increment stream pointer.
212 Peek_Token(struct parse_state
*parseState
, GLubyte
*token
)
215 i
= GetToken(parseState
->pos
, token
);
217 parseState
->pos
+= (-i
);
220 len
= _mesa_strlen((const char *) token
);
221 parseState
->pos
+= (i
- len
);
227 * Try to match 'pattern' as the next token after any whitespace/comments.
228 * Advance the current parsing position only if we match the pattern.
229 * \return GL_TRUE if pattern is matched, GL_FALSE otherwise.
232 Parse_String(struct parse_state
*parseState
, const char *pattern
)
237 /* skip whitespace and comments */
238 while (IsWhitespace(*parseState
->pos
) || *parseState
->pos
== '#') {
239 if (*parseState
->pos
== '#') {
240 while (*parseState
->pos
&& (*parseState
->pos
!= '\n' && *parseState
->pos
!= '\r')) {
241 parseState
->pos
+= 1;
245 /* skip whitespace */
246 parseState
->pos
+= 1;
250 /* Try to match the pattern */
252 for (i
= 0; pattern
[i
]; i
++) {
253 if (*m
!= (GLubyte
) pattern
[i
])
259 return GL_TRUE
; /* success */
263 /**********************************************************************/
265 static const char *InputRegisters
[MAX_NV_VERTEX_PROGRAM_INPUTS
+ 1] = {
266 "OPOS", "WGHT", "NRML", "COL0", "COL1", "FOGC", "6", "7",
267 "TEX0", "TEX1", "TEX2", "TEX3", "TEX4", "TEX5", "TEX6", "TEX7", NULL
270 static const char *OutputRegisters
[MAX_NV_VERTEX_PROGRAM_OUTPUTS
+ 1] = {
271 "HPOS", "COL0", "COL1", "BFC0", "BFC1", "FOGC", "PSIZ",
272 "TEX0", "TEX1", "TEX2", "TEX3", "TEX4", "TEX5", "TEX6", "TEX7", NULL
275 static const char *Opcodes
[] = {
276 "MOV", "LIT", "RCP", "RSQ", "EXP", "LOG", "MUL", "ADD", "DP3", "DP4",
277 "DST", "MIN", "MAX", "SLT", "SGE", "MAD", "ARL", "DPH", "RCC", "SUB",
284 IsProgRegister(GLuint r
)
286 return (GLuint
) (r
>= VP_PROG_REG_START
&& r
<= VP_PROG_REG_END
);
290 IsInputRegister(GLuint r
)
292 return (GLuint
) (r
>= VP_INPUT_REG_START
&& r
<= VP_INPUT_REG_END
);
296 IsOutputRegister(GLuint r
)
298 return (GLuint
) (r
>= VP_OUTPUT_REG_START
&& r
<= VP_OUTPUT_REG_END
);
303 * Parse a temporary register: Rnn
306 Parse_TempReg(struct parse_state
*parseState
, GLint
*tempRegNum
)
310 /* Should be 'R##' */
311 if (!Parse_Token(parseState
, token
))
314 RETURN_ERROR1("Expected R##");
316 if (IsDigit(token
[1])) {
317 GLint reg
= _mesa_atoi((char *) (token
+ 1));
318 if (reg
>= VP_NUM_TEMP_REGS
)
319 RETURN_ERROR1("Bad temporary register name");
320 *tempRegNum
= VP_TEMP_REG_START
+ reg
;
323 RETURN_ERROR1("Bad temporary register name");
331 * Parse address register "A0.x"
334 Parse_AddrReg(struct parse_state
*parseState
)
337 if (!Parse_String(parseState
, "A0"))
341 if (!Parse_String(parseState
, "."))
345 if (!Parse_String(parseState
, "x"))
353 * Parse absolute program parameter register "c[##]"
356 Parse_AbsParamReg(struct parse_state
*parseState
, GLint
*regNum
)
360 if (!Parse_String(parseState
, "c"))
363 if (!Parse_String(parseState
, "["))
366 if (!Parse_Token(parseState
, token
))
369 if (IsDigit(token
[0])) {
370 /* a numbered program parameter register */
371 GLint reg
= _mesa_atoi((char *) token
);
372 if (reg
>= VP_NUM_PROG_REGS
)
373 RETURN_ERROR1("Bad constant program number");
374 *regNum
= VP_PROG_REG_START
+ reg
;
380 if (!Parse_String(parseState
, "]"))
388 Parse_ParamReg(struct parse_state
*parseState
, struct vp_src_register
*srcReg
)
392 if (!Parse_String(parseState
, "c"))
395 if (!Parse_String(parseState
, "["))
398 if (!Peek_Token(parseState
, token
))
401 if (IsDigit(token
[0])) {
402 /* a numbered program parameter register */
404 (void) Parse_Token(parseState
, token
);
405 reg
= _mesa_atoi((char *) token
);
406 if (reg
>= VP_NUM_PROG_REGS
)
407 RETURN_ERROR1("Bad constant program number");
408 srcReg
->Register
= VP_PROG_REG_START
+ reg
;
410 else if (_mesa_strcmp((const char *) token
, "A0") == 0) {
411 /* address register "A0.x" */
412 if (!Parse_AddrReg(parseState
))
415 srcReg
->RelAddr
= GL_TRUE
;
416 srcReg
->Register
= 0;
418 /* Look for +/-N offset */
419 if (!Peek_Token(parseState
, token
))
422 if (token
[0] == '-' || token
[0] == '+') {
423 const GLubyte sign
= token
[0];
424 (void) Parse_Token(parseState
, token
); /* consume +/- */
426 /* an integer should be next */
427 if (!Parse_Token(parseState
, token
))
430 if (IsDigit(token
[0])) {
431 const GLint k
= _mesa_atoi((char *) token
);
434 RETURN_ERROR1("Bad address offset");
435 srcReg
->Register
= -k
;
439 RETURN_ERROR1("Bad address offset");
440 srcReg
->Register
= k
;
448 /* probably got a ']', catch it below */
455 /* Match closing ']' */
456 if (!Parse_String(parseState
, "]"))
464 * Parse v[#] or v[<name>]
467 Parse_AttribReg(struct parse_state
*parseState
, GLint
*tempRegNum
)
473 if (!Parse_String(parseState
, "v"))
477 if (!Parse_String(parseState
, "["))
480 /* match number or named register */
481 if (!Parse_Token(parseState
, token
))
484 if (parseState
->isStateProgram
&& token
[0] != '0')
485 RETURN_ERROR1("Only v[0] accessible in vertex state programs");
487 if (IsDigit(token
[0])) {
488 GLint reg
= _mesa_atoi((char *) token
);
489 if (reg
>= VP_NUM_INPUT_REGS
)
490 RETURN_ERROR1("Bad vertex attribute register name");
491 *tempRegNum
= VP_INPUT_REG_START
+ reg
;
494 for (j
= 0; InputRegisters
[j
]; j
++) {
495 if (_mesa_strcmp((const char *) token
, InputRegisters
[j
]) == 0) {
496 *tempRegNum
= VP_INPUT_REG_START
+ j
;
500 if (!InputRegisters
[j
]) {
501 /* unknown input register label */
502 RETURN_ERROR2("Bad register name", token
);
507 if (!Parse_String(parseState
, "]"))
515 Parse_OutputReg(struct parse_state
*parseState
, GLint
*outputRegNum
)
521 if (!Parse_String(parseState
, "o"))
525 if (!Parse_String(parseState
, "["))
528 /* Get output reg name */
529 if (!Parse_Token(parseState
, token
))
532 if (parseState
->isPositionInvariant
)
533 start
= 1; /* skip HPOS register name */
537 /* try to match an output register name */
538 for (j
= start
; OutputRegisters
[j
]; j
++) {
539 if (_mesa_strcmp((const char *) token
, OutputRegisters
[j
]) == 0) {
540 *outputRegNum
= VP_OUTPUT_REG_START
+ j
;
544 if (!OutputRegisters
[j
])
545 RETURN_ERROR1("Unrecognized output register name");
548 if (!Parse_String(parseState
, "]"))
549 RETURN_ERROR1("Expected ]");
556 Parse_MaskedDstReg(struct parse_state
*parseState
, struct vp_dst_register
*dstReg
)
560 /* Dst reg can be R<n> or o[n] */
561 if (!Peek_Token(parseState
, token
))
564 if (token
[0] == 'R') {
565 /* a temporary register */
566 if (!Parse_TempReg(parseState
, &dstReg
->Register
))
569 else if (!parseState
->isStateProgram
&& token
[0] == 'o') {
570 /* an output register */
571 if (!Parse_OutputReg(parseState
, &dstReg
->Register
))
574 else if (parseState
->isStateProgram
&& token
[0] == 'c') {
575 /* absolute program parameter register */
576 if (!Parse_AbsParamReg(parseState
, &dstReg
->Register
))
580 RETURN_ERROR1("Bad destination register name");
583 /* Parse optional write mask */
584 if (!Peek_Token(parseState
, token
))
587 if (token
[0] == '.') {
591 if (!Parse_String(parseState
, "."))
594 if (!Parse_Token(parseState
, token
))
597 dstReg
->WriteMask
[0] = GL_FALSE
;
598 dstReg
->WriteMask
[1] = GL_FALSE
;
599 dstReg
->WriteMask
[2] = GL_FALSE
;
600 dstReg
->WriteMask
[3] = GL_FALSE
;
602 if (token
[k
] == 'x') {
603 dstReg
->WriteMask
[0] = GL_TRUE
;
606 if (token
[k
] == 'y') {
607 dstReg
->WriteMask
[1] = GL_TRUE
;
610 if (token
[k
] == 'z') {
611 dstReg
->WriteMask
[2] = GL_TRUE
;
614 if (token
[k
] == 'w') {
615 dstReg
->WriteMask
[3] = GL_TRUE
;
619 RETURN_ERROR1("Bad writemask character");
624 dstReg
->WriteMask
[0] = GL_TRUE
;
625 dstReg
->WriteMask
[1] = GL_TRUE
;
626 dstReg
->WriteMask
[2] = GL_TRUE
;
627 dstReg
->WriteMask
[3] = GL_TRUE
;
634 Parse_SwizzleSrcReg(struct parse_state
*parseState
, struct vp_src_register
*srcReg
)
638 srcReg
->RelAddr
= GL_FALSE
;
641 if (!Peek_Token(parseState
, token
))
643 if (token
[0] == '-') {
644 (void) Parse_String(parseState
, "-");
645 srcReg
->Negate
= GL_TRUE
;
646 if (!Peek_Token(parseState
, token
))
650 srcReg
->Negate
= GL_FALSE
;
653 /* Src reg can be R<n>, c[n], c[n +/- offset], or a named vertex attrib */
654 if (token
[0] == 'R') {
655 if (!Parse_TempReg(parseState
, &srcReg
->Register
))
658 else if (token
[0] == 'c') {
659 if (!Parse_ParamReg(parseState
, srcReg
))
662 else if (token
[0] == 'v') {
663 if (!Parse_AttribReg(parseState
, &srcReg
->Register
))
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 if (!Parse_TempReg(parseState
, &srcReg
->Register
))
747 else if (token
[0] == 'c') {
748 if (!Parse_ParamReg(parseState
, srcReg
))
751 else if (token
[0] == 'v') {
752 if (!Parse_AttribReg(parseState
, &srcReg
->Register
))
756 RETURN_ERROR2("Bad source register name", token
);
759 /* Look for .[xyzw] suffix */
760 if (!Parse_String(parseState
, "."))
763 if (!Parse_Token(parseState
, token
))
766 if (token
[0] == 'x' && token
[1] == 0) {
767 srcReg
->Swizzle
[0] = 0;
769 else if (token
[0] == 'y' && token
[1] == 0) {
770 srcReg
->Swizzle
[0] = 1;
772 else if (token
[0] == 'z' && token
[1] == 0) {
773 srcReg
->Swizzle
[0] = 2;
775 else if (token
[0] == 'w' && token
[1] == 0) {
776 srcReg
->Swizzle
[0] = 3;
779 RETURN_ERROR1("Bad scalar source suffix");
781 srcReg
->Swizzle
[1] = srcReg
->Swizzle
[2] = srcReg
->Swizzle
[3] = 0;
788 Parse_UnaryOpInstruction(struct parse_state
*parseState
,
789 struct vp_instruction
*inst
, enum vp_opcode opcode
)
791 if (opcode
== VP_OPCODE_ABS
&& !parseState
->isVersion1_1
)
792 RETURN_ERROR1("ABS illegal for vertex program 1.0");
794 inst
->Opcode
= opcode
;
797 if (!Parse_MaskedDstReg(parseState
, &inst
->DstReg
))
801 if (!Parse_String(parseState
, ","))
805 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[0]))
809 if (!Parse_String(parseState
, ";"))
817 Parse_BiOpInstruction(struct parse_state
*parseState
,
818 struct vp_instruction
*inst
, enum vp_opcode opcode
)
820 if (opcode
== VP_OPCODE_DPH
&& !parseState
->isVersion1_1
)
821 RETURN_ERROR1("DPH illegal for vertex program 1.0");
822 if (opcode
== VP_OPCODE_SUB
&& !parseState
->isVersion1_1
)
823 RETURN_ERROR1("SUB illegal for vertex program 1.0");
825 inst
->Opcode
= opcode
;
828 if (!Parse_MaskedDstReg(parseState
, &inst
->DstReg
))
832 if (!Parse_String(parseState
, ","))
836 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[0]))
840 if (!Parse_String(parseState
, ","))
844 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[1]))
848 if (!Parse_String(parseState
, ";"))
851 /* make sure we don't reference more than one program parameter register */
852 if (IsProgRegister(inst
->SrcReg
[0].Register
) &&
853 IsProgRegister(inst
->SrcReg
[1].Register
) &&
854 inst
->SrcReg
[0].Register
!= inst
->SrcReg
[1].Register
)
855 RETURN_ERROR1("Can't reference two program parameter registers");
857 /* make sure we don't reference more than one vertex attribute register */
858 if (IsInputRegister(inst
->SrcReg
[0].Register
) &&
859 IsInputRegister(inst
->SrcReg
[1].Register
) &&
860 inst
->SrcReg
[0].Register
!= inst
->SrcReg
[1].Register
)
861 RETURN_ERROR1("Can't reference two vertex attribute registers");
868 Parse_TriOpInstruction(struct parse_state
*parseState
,
869 struct vp_instruction
*inst
, enum vp_opcode opcode
)
871 inst
->Opcode
= opcode
;
874 if (!Parse_MaskedDstReg(parseState
, &inst
->DstReg
))
878 if (!Parse_String(parseState
, ","))
882 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[0]))
886 if (!Parse_String(parseState
, ","))
890 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[1]))
894 if (!Parse_String(parseState
, ","))
898 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[2]))
902 if (!Parse_String(parseState
, ";"))
905 /* make sure we don't reference more than one program parameter register */
906 if ((IsProgRegister(inst
->SrcReg
[0].Register
) &&
907 IsProgRegister(inst
->SrcReg
[1].Register
) &&
908 inst
->SrcReg
[0].Register
!= inst
->SrcReg
[1].Register
) ||
909 (IsProgRegister(inst
->SrcReg
[0].Register
) &&
910 IsProgRegister(inst
->SrcReg
[2].Register
) &&
911 inst
->SrcReg
[0].Register
!= inst
->SrcReg
[2].Register
) ||
912 (IsProgRegister(inst
->SrcReg
[1].Register
) &&
913 IsProgRegister(inst
->SrcReg
[2].Register
) &&
914 inst
->SrcReg
[1].Register
!= inst
->SrcReg
[2].Register
))
915 RETURN_ERROR1("Can only reference one program register");
917 /* make sure we don't reference more than one vertex attribute register */
918 if ((IsInputRegister(inst
->SrcReg
[0].Register
) &&
919 IsInputRegister(inst
->SrcReg
[1].Register
) &&
920 inst
->SrcReg
[0].Register
!= inst
->SrcReg
[1].Register
) ||
921 (IsInputRegister(inst
->SrcReg
[0].Register
) &&
922 IsInputRegister(inst
->SrcReg
[2].Register
) &&
923 inst
->SrcReg
[0].Register
!= inst
->SrcReg
[2].Register
) ||
924 (IsInputRegister(inst
->SrcReg
[1].Register
) &&
925 IsInputRegister(inst
->SrcReg
[2].Register
) &&
926 inst
->SrcReg
[1].Register
!= inst
->SrcReg
[2].Register
))
927 RETURN_ERROR1("Can only reference one input register");
934 Parse_ScalarInstruction(struct parse_state
*parseState
,
935 struct vp_instruction
*inst
, enum vp_opcode opcode
)
937 if (opcode
== VP_OPCODE_RCC
&& !parseState
->isVersion1_1
)
938 RETURN_ERROR1("RCC illegal for vertex program 1.0");
940 inst
->Opcode
= opcode
;
943 if (!Parse_MaskedDstReg(parseState
, &inst
->DstReg
))
947 if (!Parse_String(parseState
, ","))
951 if (!Parse_ScalarSrcReg(parseState
, &inst
->SrcReg
[0]))
955 if (!Parse_String(parseState
, ";"))
963 Parse_AddressInstruction(struct parse_state
*parseState
, struct vp_instruction
*inst
)
965 inst
->Opcode
= VP_OPCODE_ARL
;
968 if (!Parse_AddrReg(parseState
))
972 if (!Parse_String(parseState
, ","))
976 if (!Parse_ScalarSrcReg(parseState
, &inst
->SrcReg
[0]))
980 if (!Parse_String(parseState
, ";"))
988 Parse_EndInstruction(struct parse_state
*parseState
, struct vp_instruction
*inst
)
992 inst
->Opcode
= VP_OPCODE_END
;
994 /* this should fail! */
995 if (Parse_Token(parseState
, token
))
996 RETURN_ERROR2("Unexpected token after END:", token
);
1003 Parse_OptionSequence(struct parse_state
*parseState
,
1004 struct vp_instruction program
[])
1007 if (!Parse_String(parseState
, "OPTION"))
1008 return GL_TRUE
; /* ok, not an OPTION statement */
1009 if (Parse_String(parseState
, "NV_position_invariant")) {
1010 parseState
->isPositionInvariant
= GL_TRUE
;
1013 RETURN_ERROR1("unexpected OPTION statement");
1015 if (!Parse_String(parseState
, ";"))
1022 Parse_InstructionSequence(struct parse_state
*parseState
,
1023 struct vp_instruction program
[])
1026 struct vp_instruction
*inst
= program
+ parseState
->numInst
;
1028 /* Initialize the instruction */
1029 inst
->SrcReg
[0].Register
= -1;
1030 inst
->SrcReg
[1].Register
= -1;
1031 inst
->SrcReg
[2].Register
= -1;
1032 inst
->DstReg
.Register
= -1;
1034 if (Parse_String(parseState
, "MOV")) {
1035 if (!Parse_UnaryOpInstruction(parseState
, inst
, VP_OPCODE_MOV
))
1038 else if (Parse_String(parseState
, "LIT")) {
1039 if (!Parse_UnaryOpInstruction(parseState
, inst
, VP_OPCODE_LIT
))
1042 else if (Parse_String(parseState
, "ABS")) {
1043 if (!Parse_UnaryOpInstruction(parseState
, inst
, VP_OPCODE_ABS
))
1046 else if (Parse_String(parseState
, "MUL")) {
1047 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_MUL
))
1050 else if (Parse_String(parseState
, "ADD")) {
1051 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_ADD
))
1054 else if (Parse_String(parseState
, "DP3")) {
1055 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_DP3
))
1058 else if (Parse_String(parseState
, "DP4")) {
1059 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_DP4
))
1062 else if (Parse_String(parseState
, "DST")) {
1063 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_DST
))
1066 else if (Parse_String(parseState
, "MIN")) {
1067 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_MIN
))
1070 else if (Parse_String(parseState
, "MAX")) {
1071 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_MAX
))
1074 else if (Parse_String(parseState
, "SLT")) {
1075 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_SLT
))
1078 else if (Parse_String(parseState
, "SGE")) {
1079 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_SGE
))
1082 else if (Parse_String(parseState
, "DPH")) {
1083 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_DPH
))
1086 else if (Parse_String(parseState
, "SUB")) {
1087 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_SUB
))
1090 else if (Parse_String(parseState
, "MAD")) {
1091 if (!Parse_TriOpInstruction(parseState
, inst
, VP_OPCODE_MAD
))
1094 else if (Parse_String(parseState
, "RCP")) {
1095 if (!Parse_ScalarInstruction(parseState
, inst
, VP_OPCODE_RCP
))
1098 else if (Parse_String(parseState
, "RSQ")) {
1099 if (!Parse_ScalarInstruction(parseState
, inst
, VP_OPCODE_RSQ
))
1102 else if (Parse_String(parseState
, "EXP")) {
1103 if (!Parse_ScalarInstruction(parseState
, inst
, VP_OPCODE_EXP
))
1106 else if (Parse_String(parseState
, "LOG")) {
1107 if (!Parse_ScalarInstruction(parseState
, inst
, VP_OPCODE_LOG
))
1110 else if (Parse_String(parseState
, "RCC")) {
1111 if (!Parse_ScalarInstruction(parseState
, inst
, VP_OPCODE_RCC
))
1114 else if (Parse_String(parseState
, "ARL")) {
1115 if (!Parse_AddressInstruction(parseState
, inst
))
1118 else if (Parse_String(parseState
, "END")) {
1119 if (!Parse_EndInstruction(parseState
, inst
))
1122 parseState
->numInst
++;
1123 return GL_TRUE
; /* all done */
1127 /* bad instruction name */
1128 RETURN_ERROR1("Unexpected token");
1131 /* examine input/output registers */
1133 const GLint srcReg0
= inst
->SrcReg
[0].Register
;
1134 const GLint srcReg1
= inst
->SrcReg
[1].Register
;
1135 const GLint srcReg2
= inst
->SrcReg
[2].Register
;
1136 const GLint dstReg
= inst
->DstReg
.Register
;
1138 if (IsOutputRegister(dstReg
))
1139 parseState
->outputsWritten
|= (1 << (dstReg
- VP_OUTPUT_REG_START
));
1140 else if (IsProgRegister(dstReg
))
1141 parseState
->progRegsWritten
|= (1 << (dstReg
- VP_PROG_REG_START
));
1143 if (IsInputRegister(srcReg0
) && !inst
->SrcReg
[0].RelAddr
)
1144 parseState
->inputsRead
|= (1 << (srcReg0
- VP_INPUT_REG_START
));
1146 if (IsInputRegister(srcReg1
) && !inst
->SrcReg
[1].RelAddr
)
1147 parseState
->inputsRead
|= (1 << (srcReg1
- VP_INPUT_REG_START
));
1149 if (IsInputRegister(srcReg2
) && !inst
->SrcReg
[2].RelAddr
)
1150 parseState
->inputsRead
|= (1 << (srcReg2
- VP_INPUT_REG_START
));
1153 parseState
->numInst
++;
1155 if (parseState
->numInst
>= MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
)
1156 RETURN_ERROR1("Program too long");
1164 Parse_Program(struct parse_state
*parseState
,
1165 struct vp_instruction instBuffer
[])
1167 if (parseState
->isVersion1_1
) {
1168 if (!Parse_OptionSequence(parseState
, instBuffer
)) {
1172 return Parse_InstructionSequence(parseState
, instBuffer
);
1177 * Parse/compile the 'str' returning the compiled 'program'.
1178 * ctx->Program.ErrorPos will be -1 if successful. Otherwise, ErrorPos
1179 * indicates the position of the error in 'str'.
1182 _mesa_parse_nv_vertex_program(GLcontext
*ctx
, GLenum dstTarget
,
1183 const GLubyte
*str
, GLsizei len
,
1184 struct vertex_program
*program
)
1186 struct parse_state parseState
;
1187 struct vp_instruction instBuffer
[MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
];
1188 struct vp_instruction
*newInst
;
1190 GLubyte
*programString
;
1192 /* Make a null-terminated copy of the program string */
1193 programString
= (GLubyte
*) MALLOC(len
+ 1);
1194 if (!programString
) {
1195 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glLoadProgramNV");
1198 MEMCPY(programString
, str
, len
);
1199 programString
[len
] = 0;
1201 /* Get ready to parse */
1202 parseState
.ctx
= ctx
;
1203 parseState
.start
= programString
;
1204 parseState
.isPositionInvariant
= GL_FALSE
;
1205 parseState
.isVersion1_1
= GL_FALSE
;
1206 parseState
.numInst
= 0;
1207 parseState
.inputsRead
= 0;
1208 parseState
.outputsWritten
= 0;
1209 parseState
.progRegsWritten
= 0;
1211 /* Reset error state */
1212 _mesa_set_program_error(ctx
, -1, NULL
);
1214 /* check the program header */
1215 if (_mesa_strncmp((const char *) programString
, "!!VP1.0", 7) == 0) {
1216 target
= GL_VERTEX_PROGRAM_NV
;
1217 parseState
.pos
= programString
+ 7;
1218 parseState
.isStateProgram
= GL_FALSE
;
1220 else if (_mesa_strncmp((const char *) programString
, "!!VP1.1", 7) == 0) {
1221 target
= GL_VERTEX_PROGRAM_NV
;
1222 parseState
.pos
= programString
+ 7;
1223 parseState
.isStateProgram
= GL_FALSE
;
1224 parseState
.isVersion1_1
= GL_TRUE
;
1226 else if (_mesa_strncmp((const char *) programString
, "!!VSP1.0", 8) == 0) {
1227 target
= GL_VERTEX_STATE_PROGRAM_NV
;
1228 parseState
.pos
= programString
+ 8;
1229 parseState
.isStateProgram
= GL_TRUE
;
1232 /* invalid header */
1233 ctx
->Program
.ErrorPos
= 0;
1234 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glLoadProgramNV(bad header)");
1238 /* make sure target and header match */
1239 if (target
!= dstTarget
) {
1240 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1241 "glLoadProgramNV(target mismatch)");
1246 if (Parse_Program(&parseState
, instBuffer
)) {
1247 /* successful parse! */
1249 if (parseState
.isStateProgram
) {
1250 if (parseState
.progRegsWritten
== 0) {
1251 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1252 "glLoadProgramNV(c[#] not written)");
1257 if (!parseState
.isPositionInvariant
&&
1258 !(parseState
.outputsWritten
& 1)) {
1259 /* bit 1 = HPOS register */
1260 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1261 "glLoadProgramNV(HPOS not written)");
1266 /* copy the compiled instructions */
1267 assert(parseState
.numInst
<= MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
);
1268 newInst
= (struct vp_instruction
*)
1269 MALLOC(parseState
.numInst
* sizeof(struct vp_instruction
));
1271 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glLoadProgramNV");
1272 FREE(programString
);
1273 return; /* out of memory */
1275 MEMCPY(newInst
, instBuffer
,
1276 parseState
.numInst
* sizeof(struct vp_instruction
));
1278 /* install the program */
1279 program
->Base
.Target
= target
;
1280 if (program
->Base
.String
) {
1281 FREE(program
->Base
.String
);
1283 program
->Base
.String
= programString
;
1284 program
->Base
.Format
= GL_PROGRAM_FORMAT_ASCII_ARB
;
1285 if (program
->Instructions
) {
1286 FREE(program
->Instructions
);
1288 program
->Instructions
= newInst
;
1289 program
->InputsRead
= parseState
.inputsRead
;
1290 program
->OutputsWritten
= parseState
.outputsWritten
;
1291 program
->IsPositionInvariant
= parseState
.isPositionInvariant
;
1294 _mesa_printf("--- glLoadProgramNV result ---\n");
1295 _mesa_print_nv_vertex_program(program
);
1296 _mesa_printf("------------------------------\n");
1301 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glLoadProgramNV");
1302 /* NOTE: _mesa_set_program_error would have been called already */
1303 /* GL_NV_vertex_program isn't supposed to set the error string
1304 * so we reset it here.
1306 _mesa_set_program_error(ctx
, ctx
->Program
.ErrorPos
, NULL
);
1312 PrintSrcReg(const struct vp_src_register
*src
)
1314 static const char comps
[5] = "xyzw";
1318 if (src
->Register
> 0)
1319 _mesa_printf("c[A0.x + %d]", src
->Register
);
1320 else if (src
->Register
< 0)
1321 _mesa_printf("c[A0.x - %d]", -src
->Register
);
1323 _mesa_printf("c[A0.x]");
1325 else if (src
->Register
>= VP_OUTPUT_REG_START
1326 && src
->Register
<= VP_OUTPUT_REG_END
) {
1327 _mesa_printf("o[%s]", OutputRegisters
[src
->Register
- VP_OUTPUT_REG_START
]);
1329 else if (src
->Register
>= VP_INPUT_REG_START
1330 && src
->Register
<= VP_INPUT_REG_END
) {
1331 _mesa_printf("v[%s]", InputRegisters
[src
->Register
- VP_INPUT_REG_START
]);
1333 else if (src
->Register
>= VP_PROG_REG_START
1334 && src
->Register
<= VP_PROG_REG_END
) {
1335 _mesa_printf("c[%d]", src
->Register
- VP_PROG_REG_START
);
1338 _mesa_printf("R%d", src
->Register
- VP_TEMP_REG_START
);
1341 if (src
->Swizzle
[0] == src
->Swizzle
[1] &&
1342 src
->Swizzle
[0] == src
->Swizzle
[2] &&
1343 src
->Swizzle
[0] == src
->Swizzle
[3]) {
1344 _mesa_printf(".%c", comps
[src
->Swizzle
[0]]);
1346 else if (src
->Swizzle
[0] != 0 ||
1347 src
->Swizzle
[1] != 1 ||
1348 src
->Swizzle
[2] != 2 ||
1349 src
->Swizzle
[3] != 3) {
1350 _mesa_printf(".%c%c%c%c",
1351 comps
[src
->Swizzle
[0]],
1352 comps
[src
->Swizzle
[1]],
1353 comps
[src
->Swizzle
[2]],
1354 comps
[src
->Swizzle
[3]]);
1360 PrintDstReg(const struct vp_dst_register
*dst
)
1362 GLint w
= dst
->WriteMask
[0] + dst
->WriteMask
[1]
1363 + dst
->WriteMask
[2] + dst
->WriteMask
[3];
1365 if (dst
->Register
>= VP_OUTPUT_REG_START
1366 && dst
->Register
<= VP_OUTPUT_REG_END
) {
1367 _mesa_printf("o[%s]", OutputRegisters
[dst
->Register
- VP_OUTPUT_REG_START
]);
1369 else if (dst
->Register
>= VP_INPUT_REG_START
1370 && dst
->Register
<= VP_INPUT_REG_END
) {
1371 _mesa_printf("v[%s]", InputRegisters
[dst
->Register
- VP_INPUT_REG_START
]);
1373 else if (dst
->Register
>= VP_PROG_REG_START
1374 && dst
->Register
<= VP_PROG_REG_END
) {
1375 _mesa_printf("c[%d]", dst
->Register
- VP_PROG_REG_START
);
1378 _mesa_printf("R%d", dst
->Register
- VP_TEMP_REG_START
);
1381 if (w
!= 0 && w
!= 4) {
1383 if (dst
->WriteMask
[0])
1385 if (dst
->WriteMask
[1])
1387 if (dst
->WriteMask
[2])
1389 if (dst
->WriteMask
[3])
1396 * Print a single NVIDIA vertex program instruction.
1399 _mesa_print_nv_vertex_instruction(const struct vp_instruction
*inst
)
1401 switch (inst
->Opcode
) {
1410 _mesa_printf("%s ", Opcodes
[(int) inst
->Opcode
]);
1411 PrintDstReg(&inst
->DstReg
);
1413 PrintSrcReg(&inst
->SrcReg
[0]);
1414 _mesa_printf(";\n");
1427 _mesa_printf("%s ", Opcodes
[(int) inst
->Opcode
]);
1428 PrintDstReg(&inst
->DstReg
);
1430 PrintSrcReg(&inst
->SrcReg
[0]);
1432 PrintSrcReg(&inst
->SrcReg
[1]);
1433 _mesa_printf(";\n");
1436 _mesa_printf("MAD ");
1437 PrintDstReg(&inst
->DstReg
);
1439 PrintSrcReg(&inst
->SrcReg
[0]);
1441 PrintSrcReg(&inst
->SrcReg
[1]);
1443 PrintSrcReg(&inst
->SrcReg
[2]);
1444 _mesa_printf(";\n");
1447 _mesa_printf("ARL A0.x, ");
1448 PrintSrcReg(&inst
->SrcReg
[0]);
1449 _mesa_printf(";\n");
1452 _mesa_printf("END\n");
1455 _mesa_printf("BAD INSTRUCTION\n");
1461 * Print (unparse) the given vertex program. Just for debugging.
1464 _mesa_print_nv_vertex_program(const struct vertex_program
*program
)
1466 const struct vp_instruction
*inst
;
1468 for (inst
= program
->Instructions
; ; inst
++) {
1469 _mesa_print_nv_vertex_instruction(inst
);
1470 if (inst
->Opcode
== VP_OPCODE_END
)
1477 _mesa_nv_vertex_input_register_name(GLuint i
)
1479 ASSERT(i
< MAX_NV_VERTEX_PROGRAM_INPUTS
);
1480 return InputRegisters
[i
];
1485 _mesa_nv_vertex_output_register_name(GLuint i
)
1487 ASSERT(i
< MAX_NV_VERTEX_PROGRAM_OUTPUTS
);
1488 return OutputRegisters
[i
];