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.
32 * Regarding GL_NV_vertex_program, GL_NV_vertex_program1_1:
34 * Portions of this software may use or implement intellectual
35 * property owned and licensed by NVIDIA Corporation. NVIDIA disclaims
36 * any and all warranties with respect to such intellectual property,
37 * including any use thereof or modifications thereto.
46 #include "nvprogram.h"
47 #include "nvvertparse.h"
48 #include "nvvertprog.h"
53 * Current parsing state. This structure is passed among the parsing
54 * functions and keeps track of the current parser position and various
61 const GLubyte
*curLine
;
62 GLboolean isStateProgram
;
63 GLboolean isPositionInvariant
;
64 GLboolean isVersion1_1
;
66 GLuint outputsWritten
;
67 GLboolean anyProgRegsWritten
;
68 GLuint numInst
; /* number of instructions parsed */
73 * Called whenever we find an error during parsing.
76 record_error(struct parse_state
*parseState
, const char *msg
, int lineNo
)
80 const GLubyte
*lineStr
;
81 lineStr
= _mesa_find_line_column(parseState
->start
,
82 parseState
->pos
, &line
, &column
);
83 _mesa_debug(parseState
->ctx
,
84 "nvfragparse.c(%d): line %d, column %d:%s (%s)\n",
85 lineNo
, line
, column
, (char *) lineStr
, msg
);
86 _mesa_free((void *) lineStr
);
91 /* Check that no error was already recorded. Only record the first one. */
92 if (parseState
->ctx
->Program
.ErrorString
[0] == 0) {
93 _mesa_set_program_error(parseState
->ctx
,
94 parseState
->pos
- parseState
->start
,
100 #define RETURN_ERROR \
102 record_error(parseState, "Unexpected end of input.", __LINE__); \
106 #define RETURN_ERROR1(msg) \
108 record_error(parseState, msg, __LINE__); \
112 #define RETURN_ERROR2(msg1, msg2) \
115 _mesa_sprintf(err, "%s %s", msg1, msg2); \
116 record_error(parseState, err, __LINE__); \
124 static GLboolean
IsLetter(GLubyte b
)
126 return (b
>= 'a' && b
<= 'z') || (b
>= 'A' && b
<= 'Z');
130 static GLboolean
IsDigit(GLubyte b
)
132 return b
>= '0' && b
<= '9';
136 static GLboolean
IsWhitespace(GLubyte b
)
138 return b
== ' ' || b
== '\t' || b
== '\n' || b
== '\r';
143 * Starting at 'str' find the next token. A token can be an integer,
144 * an identifier or punctuation symbol.
145 * \return <= 0 we found an error, else, return number of characters parsed.
148 GetToken(struct parse_state
*parseState
, GLubyte
*token
)
150 const GLubyte
*str
= parseState
->pos
;
155 /* skip whitespace and comments */
156 while (str
[i
] && (IsWhitespace(str
[i
]) || str
[i
] == '#')) {
159 while (str
[i
] && (str
[i
] != '\n' && str
[i
] != '\r')) {
162 if (str
[i
] == '\n' || str
[i
] == '\r')
163 parseState
->curLine
= str
+ i
+ 1;
166 /* skip whitespace */
167 if (str
[i
] == '\n' || str
[i
] == '\r')
168 parseState
->curLine
= str
+ i
+ 1;
176 /* try matching an integer */
177 while (str
[i
] && IsDigit(str
[i
])) {
178 token
[j
++] = str
[i
++];
180 if (j
> 0 || !str
[i
]) {
185 /* try matching an identifier */
186 if (IsLetter(str
[i
])) {
187 while (str
[i
] && (IsLetter(str
[i
]) || IsDigit(str
[i
]))) {
188 token
[j
++] = str
[i
++];
194 /* punctuation character */
208 * Get next token from input stream and increment stream pointer past token.
211 Parse_Token(struct parse_state
*parseState
, GLubyte
*token
)
214 i
= GetToken(parseState
, token
);
216 parseState
->pos
+= (-i
);
219 parseState
->pos
+= i
;
225 * Get next token from input stream but don't increment stream pointer.
228 Peek_Token(struct parse_state
*parseState
, GLubyte
*token
)
231 i
= GetToken(parseState
, token
);
233 parseState
->pos
+= (-i
);
236 len
= _mesa_strlen((const char *) token
);
237 parseState
->pos
+= (i
- len
);
243 * Try to match 'pattern' as the next token after any whitespace/comments.
244 * Advance the current parsing position only if we match the pattern.
245 * \return GL_TRUE if pattern is matched, GL_FALSE otherwise.
248 Parse_String(struct parse_state
*parseState
, const char *pattern
)
253 /* skip whitespace and comments */
254 while (IsWhitespace(*parseState
->pos
) || *parseState
->pos
== '#') {
255 if (*parseState
->pos
== '#') {
256 while (*parseState
->pos
&& (*parseState
->pos
!= '\n' && *parseState
->pos
!= '\r')) {
257 parseState
->pos
+= 1;
259 if (*parseState
->pos
== '\n' || *parseState
->pos
== '\r')
260 parseState
->curLine
= parseState
->pos
+ 1;
263 /* skip whitespace */
264 if (*parseState
->pos
== '\n' || *parseState
->pos
== '\r')
265 parseState
->curLine
= parseState
->pos
+ 1;
266 parseState
->pos
+= 1;
270 /* Try to match the pattern */
272 for (i
= 0; pattern
[i
]; i
++) {
273 if (*m
!= (GLubyte
) pattern
[i
])
279 return GL_TRUE
; /* success */
283 /**********************************************************************/
285 static const char *InputRegisters
[MAX_NV_VERTEX_PROGRAM_INPUTS
+ 1] = {
286 "OPOS", "WGHT", "NRML", "COL0", "COL1", "FOGC", "6", "7",
287 "TEX0", "TEX1", "TEX2", "TEX3", "TEX4", "TEX5", "TEX6", "TEX7", NULL
290 static const char *OutputRegisters
[MAX_NV_VERTEX_PROGRAM_OUTPUTS
+ 1] = {
291 "HPOS", "COL0", "COL1", "BFC0", "BFC1", "FOGC", "PSIZ",
292 "TEX0", "TEX1", "TEX2", "TEX3", "TEX4", "TEX5", "TEX6", "TEX7", NULL
295 /* NOTE: the order here must match opcodes in nvvertprog.h */
296 static const char *Opcodes
[] = {
297 "MOV", "LIT", "RCP", "RSQ", "EXP", "LOG", "MUL", "ADD", "DP3", "DP4",
298 "DST", "MIN", "MAX", "SLT", "SGE", "MAD", "ARL", "DPH", "RCC", "SUB",
300 /* GL_ARB_vertex_program */
301 "FLR", "FRC", "EX2", "LG2", "POW", "XPD", "SWZ",
308 * Parse a temporary register: Rnn
311 Parse_TempReg(struct parse_state
*parseState
, GLint
*tempRegNum
)
315 /* Should be 'R##' */
316 if (!Parse_Token(parseState
, token
))
319 RETURN_ERROR1("Expected R##");
321 if (IsDigit(token
[1])) {
322 GLint reg
= _mesa_atoi((char *) (token
+ 1));
323 if (reg
>= MAX_NV_VERTEX_PROGRAM_TEMPS
)
324 RETURN_ERROR1("Bad temporary register name");
328 RETURN_ERROR1("Bad temporary register name");
336 * Parse address register "A0.x"
339 Parse_AddrReg(struct parse_state
*parseState
)
342 if (!Parse_String(parseState
, "A0"))
346 if (!Parse_String(parseState
, "."))
350 if (!Parse_String(parseState
, "x"))
358 * Parse absolute program parameter register "c[##]"
361 Parse_AbsParamReg(struct parse_state
*parseState
, GLint
*regNum
)
365 if (!Parse_String(parseState
, "c"))
368 if (!Parse_String(parseState
, "["))
371 if (!Parse_Token(parseState
, token
))
374 if (IsDigit(token
[0])) {
375 /* a numbered program parameter register */
376 GLint reg
= _mesa_atoi((char *) token
);
377 if (reg
>= MAX_NV_VERTEX_PROGRAM_PARAMS
)
378 RETURN_ERROR1("Bad program parameter number");
385 if (!Parse_String(parseState
, "]"))
393 Parse_ParamReg(struct parse_state
*parseState
, struct vp_src_register
*srcReg
)
397 if (!Parse_String(parseState
, "c"))
400 if (!Parse_String(parseState
, "["))
403 if (!Peek_Token(parseState
, token
))
406 if (IsDigit(token
[0])) {
407 /* a numbered program parameter register */
409 (void) Parse_Token(parseState
, token
);
410 reg
= _mesa_atoi((char *) token
);
411 if (reg
>= MAX_NV_VERTEX_PROGRAM_PARAMS
)
412 RETURN_ERROR1("Bad program parameter number");
413 srcReg
->File
= PROGRAM_ENV_PARAM
;
416 else if (_mesa_strcmp((const char *) token
, "A0") == 0) {
417 /* address register "A0.x" */
418 if (!Parse_AddrReg(parseState
))
421 srcReg
->RelAddr
= GL_TRUE
;
422 srcReg
->File
= PROGRAM_ENV_PARAM
;
423 /* Look for +/-N offset */
424 if (!Peek_Token(parseState
, token
))
427 if (token
[0] == '-' || token
[0] == '+') {
428 const GLubyte sign
= token
[0];
429 (void) Parse_Token(parseState
, token
); /* consume +/- */
431 /* an integer should be next */
432 if (!Parse_Token(parseState
, token
))
435 if (IsDigit(token
[0])) {
436 const GLint k
= _mesa_atoi((char *) token
);
439 RETURN_ERROR1("Bad address offset");
444 RETURN_ERROR1("Bad address offset");
453 /* probably got a ']', catch it below */
460 /* Match closing ']' */
461 if (!Parse_String(parseState
, "]"))
469 * Parse v[#] or v[<name>]
472 Parse_AttribReg(struct parse_state
*parseState
, GLint
*tempRegNum
)
478 if (!Parse_String(parseState
, "v"))
482 if (!Parse_String(parseState
, "["))
485 /* match number or named register */
486 if (!Parse_Token(parseState
, token
))
489 if (parseState
->isStateProgram
&& token
[0] != '0')
490 RETURN_ERROR1("Only v[0] accessible in vertex state programs");
492 if (IsDigit(token
[0])) {
493 GLint reg
= _mesa_atoi((char *) token
);
494 if (reg
>= MAX_NV_VERTEX_PROGRAM_INPUTS
)
495 RETURN_ERROR1("Bad vertex attribute register name");
499 for (j
= 0; InputRegisters
[j
]; j
++) {
500 if (_mesa_strcmp((const char *) token
, InputRegisters
[j
]) == 0) {
505 if (!InputRegisters
[j
]) {
506 /* unknown input register label */
507 RETURN_ERROR2("Bad register name", token
);
512 if (!Parse_String(parseState
, "]"))
520 Parse_OutputReg(struct parse_state
*parseState
, GLint
*outputRegNum
)
526 if (!Parse_String(parseState
, "o"))
530 if (!Parse_String(parseState
, "["))
533 /* Get output reg name */
534 if (!Parse_Token(parseState
, token
))
537 if (parseState
->isPositionInvariant
)
538 start
= 1; /* skip HPOS register name */
542 /* try to match an output register name */
543 for (j
= start
; OutputRegisters
[j
]; j
++) {
544 if (_mesa_strcmp((const char *) token
, OutputRegisters
[j
]) == 0) {
549 if (!OutputRegisters
[j
])
550 RETURN_ERROR1("Unrecognized output register name");
553 if (!Parse_String(parseState
, "]"))
554 RETURN_ERROR1("Expected ]");
561 Parse_MaskedDstReg(struct parse_state
*parseState
, struct vp_dst_register
*dstReg
)
565 /* Dst reg can be R<n> or o[n] */
566 if (!Peek_Token(parseState
, token
))
569 if (token
[0] == 'R') {
570 /* a temporary register */
571 dstReg
->File
= PROGRAM_TEMPORARY
;
572 if (!Parse_TempReg(parseState
, &dstReg
->Index
))
575 else if (!parseState
->isStateProgram
&& token
[0] == 'o') {
576 /* an output register */
577 dstReg
->File
= PROGRAM_OUTPUT
;
578 if (!Parse_OutputReg(parseState
, &dstReg
->Index
))
581 else if (parseState
->isStateProgram
&& token
[0] == 'c') {
582 /* absolute program parameter register */
583 dstReg
->File
= PROGRAM_ENV_PARAM
;
584 if (!Parse_AbsParamReg(parseState
, &dstReg
->Index
))
588 RETURN_ERROR1("Bad destination register name");
591 /* Parse optional write mask */
592 if (!Peek_Token(parseState
, token
))
595 if (token
[0] == '.') {
599 if (!Parse_String(parseState
, "."))
602 if (!Parse_Token(parseState
, token
))
605 dstReg
->WriteMask
[0] = GL_FALSE
;
606 dstReg
->WriteMask
[1] = GL_FALSE
;
607 dstReg
->WriteMask
[2] = GL_FALSE
;
608 dstReg
->WriteMask
[3] = GL_FALSE
;
610 if (token
[k
] == 'x') {
611 dstReg
->WriteMask
[0] = GL_TRUE
;
614 if (token
[k
] == 'y') {
615 dstReg
->WriteMask
[1] = GL_TRUE
;
618 if (token
[k
] == 'z') {
619 dstReg
->WriteMask
[2] = GL_TRUE
;
622 if (token
[k
] == 'w') {
623 dstReg
->WriteMask
[3] = GL_TRUE
;
627 RETURN_ERROR1("Bad writemask character");
632 dstReg
->WriteMask
[0] = GL_TRUE
;
633 dstReg
->WriteMask
[1] = GL_TRUE
;
634 dstReg
->WriteMask
[2] = GL_TRUE
;
635 dstReg
->WriteMask
[3] = GL_TRUE
;
642 Parse_SwizzleSrcReg(struct parse_state
*parseState
, struct vp_src_register
*srcReg
)
646 srcReg
->RelAddr
= GL_FALSE
;
649 if (!Peek_Token(parseState
, token
))
651 if (token
[0] == '-') {
652 (void) Parse_String(parseState
, "-");
653 srcReg
->Negate
= GL_TRUE
;
654 if (!Peek_Token(parseState
, token
))
658 srcReg
->Negate
= GL_FALSE
;
661 /* Src reg can be R<n>, c[n], c[n +/- offset], or a named vertex attrib */
662 if (token
[0] == 'R') {
663 srcReg
->File
= PROGRAM_TEMPORARY
;
664 if (!Parse_TempReg(parseState
, &srcReg
->Index
))
667 else if (token
[0] == 'c') {
668 if (!Parse_ParamReg(parseState
, srcReg
))
671 else if (token
[0] == 'v') {
672 srcReg
->File
= PROGRAM_INPUT
;
673 if (!Parse_AttribReg(parseState
, &srcReg
->Index
))
677 RETURN_ERROR2("Bad source register name", token
);
680 /* init swizzle fields */
681 srcReg
->Swizzle
[0] = 0;
682 srcReg
->Swizzle
[1] = 1;
683 srcReg
->Swizzle
[2] = 2;
684 srcReg
->Swizzle
[3] = 3;
686 /* Look for optional swizzle suffix */
687 if (!Peek_Token(parseState
, token
))
689 if (token
[0] == '.') {
690 (void) Parse_String(parseState
, "."); /* consume . */
692 if (!Parse_Token(parseState
, token
))
696 /* single letter swizzle */
698 ASSIGN_4V(srcReg
->Swizzle
, 0, 0, 0, 0);
699 else if (token
[0] == 'y')
700 ASSIGN_4V(srcReg
->Swizzle
, 1, 1, 1, 1);
701 else if (token
[0] == 'z')
702 ASSIGN_4V(srcReg
->Swizzle
, 2, 2, 2, 2);
703 else if (token
[0] == 'w')
704 ASSIGN_4V(srcReg
->Swizzle
, 3, 3, 3, 3);
706 RETURN_ERROR1("Expected x, y, z, or w");
709 /* 2, 3 or 4-component swizzle */
711 for (k
= 0; token
[k
] && k
< 5; k
++) {
713 srcReg
->Swizzle
[k
] = 0;
714 else if (token
[k
] == 'y')
715 srcReg
->Swizzle
[k
] = 1;
716 else if (token
[k
] == 'z')
717 srcReg
->Swizzle
[k
] = 2;
718 else if (token
[k
] == 'w')
719 srcReg
->Swizzle
[k
] = 3;
733 Parse_ScalarSrcReg(struct parse_state
*parseState
, struct vp_src_register
*srcReg
)
737 srcReg
->RelAddr
= GL_FALSE
;
740 if (!Peek_Token(parseState
, token
))
742 if (token
[0] == '-') {
743 srcReg
->Negate
= GL_TRUE
;
744 (void) Parse_String(parseState
, "-"); /* consume '-' */
745 if (!Peek_Token(parseState
, token
))
749 srcReg
->Negate
= GL_FALSE
;
752 /* Src reg can be R<n>, c[n], c[n +/- offset], or a named vertex attrib */
753 if (token
[0] == 'R') {
754 srcReg
->File
= PROGRAM_TEMPORARY
;
755 if (!Parse_TempReg(parseState
, &srcReg
->Index
))
758 else if (token
[0] == 'c') {
759 if (!Parse_ParamReg(parseState
, srcReg
))
762 else if (token
[0] == 'v') {
763 srcReg
->File
= PROGRAM_INPUT
;
764 if (!Parse_AttribReg(parseState
, &srcReg
->Index
))
768 RETURN_ERROR2("Bad source register name", token
);
771 /* Look for .[xyzw] suffix */
772 if (!Parse_String(parseState
, "."))
775 if (!Parse_Token(parseState
, token
))
778 if (token
[0] == 'x' && token
[1] == 0) {
779 srcReg
->Swizzle
[0] = 0;
781 else if (token
[0] == 'y' && token
[1] == 0) {
782 srcReg
->Swizzle
[0] = 1;
784 else if (token
[0] == 'z' && token
[1] == 0) {
785 srcReg
->Swizzle
[0] = 2;
787 else if (token
[0] == 'w' && token
[1] == 0) {
788 srcReg
->Swizzle
[0] = 3;
791 RETURN_ERROR1("Bad scalar source suffix");
793 srcReg
->Swizzle
[1] = srcReg
->Swizzle
[2] = srcReg
->Swizzle
[3] = 0;
800 Parse_UnaryOpInstruction(struct parse_state
*parseState
,
801 struct vp_instruction
*inst
, enum vp_opcode opcode
)
803 if (opcode
== VP_OPCODE_ABS
&& !parseState
->isVersion1_1
)
804 RETURN_ERROR1("ABS illegal for vertex program 1.0");
806 inst
->Opcode
= opcode
;
807 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
810 if (!Parse_MaskedDstReg(parseState
, &inst
->DstReg
))
814 if (!Parse_String(parseState
, ","))
818 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[0]))
822 if (!Parse_String(parseState
, ";"))
830 Parse_BiOpInstruction(struct parse_state
*parseState
,
831 struct vp_instruction
*inst
, enum vp_opcode opcode
)
833 if (opcode
== VP_OPCODE_DPH
&& !parseState
->isVersion1_1
)
834 RETURN_ERROR1("DPH illegal for vertex program 1.0");
835 if (opcode
== VP_OPCODE_SUB
&& !parseState
->isVersion1_1
)
836 RETURN_ERROR1("SUB illegal for vertex program 1.0");
838 inst
->Opcode
= opcode
;
839 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
842 if (!Parse_MaskedDstReg(parseState
, &inst
->DstReg
))
846 if (!Parse_String(parseState
, ","))
850 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[0]))
854 if (!Parse_String(parseState
, ","))
858 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[1]))
862 if (!Parse_String(parseState
, ";"))
865 /* make sure we don't reference more than one program parameter register */
866 if (inst
->SrcReg
[0].File
== PROGRAM_ENV_PARAM
&&
867 inst
->SrcReg
[1].File
== PROGRAM_ENV_PARAM
&&
868 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[1].Index
)
869 RETURN_ERROR1("Can't reference two program parameter registers");
871 /* make sure we don't reference more than one vertex attribute register */
872 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
873 inst
->SrcReg
[1].File
== PROGRAM_INPUT
&&
874 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[1].Index
)
875 RETURN_ERROR1("Can't reference two vertex attribute registers");
882 Parse_TriOpInstruction(struct parse_state
*parseState
,
883 struct vp_instruction
*inst
, enum vp_opcode opcode
)
885 inst
->Opcode
= opcode
;
886 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
889 if (!Parse_MaskedDstReg(parseState
, &inst
->DstReg
))
893 if (!Parse_String(parseState
, ","))
897 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[0]))
901 if (!Parse_String(parseState
, ","))
905 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[1]))
909 if (!Parse_String(parseState
, ","))
913 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[2]))
917 if (!Parse_String(parseState
, ";"))
920 /* make sure we don't reference more than one program parameter register */
921 if ((inst
->SrcReg
[0].File
== PROGRAM_ENV_PARAM
&&
922 inst
->SrcReg
[1].File
== PROGRAM_ENV_PARAM
&&
923 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[1].Index
) ||
924 (inst
->SrcReg
[0].File
== PROGRAM_ENV_PARAM
&&
925 inst
->SrcReg
[2].File
== PROGRAM_ENV_PARAM
&&
926 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[2].Index
) ||
927 (inst
->SrcReg
[1].File
== PROGRAM_ENV_PARAM
&&
928 inst
->SrcReg
[2].File
== PROGRAM_ENV_PARAM
&&
929 inst
->SrcReg
[1].Index
!= inst
->SrcReg
[2].Index
))
930 RETURN_ERROR1("Can only reference one program register");
932 /* make sure we don't reference more than one vertex attribute register */
933 if ((inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
934 inst
->SrcReg
[1].File
== PROGRAM_INPUT
&&
935 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[1].Index
) ||
936 (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
937 inst
->SrcReg
[2].File
== PROGRAM_INPUT
&&
938 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[2].Index
) ||
939 (inst
->SrcReg
[1].File
== PROGRAM_INPUT
&&
940 inst
->SrcReg
[2].File
== PROGRAM_INPUT
&&
941 inst
->SrcReg
[1].Index
!= inst
->SrcReg
[2].Index
))
942 RETURN_ERROR1("Can only reference one input register");
949 Parse_ScalarInstruction(struct parse_state
*parseState
,
950 struct vp_instruction
*inst
, enum vp_opcode opcode
)
952 if (opcode
== VP_OPCODE_RCC
&& !parseState
->isVersion1_1
)
953 RETURN_ERROR1("RCC illegal for vertex program 1.0");
955 inst
->Opcode
= opcode
;
956 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
959 if (!Parse_MaskedDstReg(parseState
, &inst
->DstReg
))
963 if (!Parse_String(parseState
, ","))
967 if (!Parse_ScalarSrcReg(parseState
, &inst
->SrcReg
[0]))
971 if (!Parse_String(parseState
, ";"))
979 Parse_AddressInstruction(struct parse_state
*parseState
, struct vp_instruction
*inst
)
981 inst
->Opcode
= VP_OPCODE_ARL
;
982 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
985 if (!Parse_AddrReg(parseState
))
989 if (!Parse_String(parseState
, ","))
993 if (!Parse_ScalarSrcReg(parseState
, &inst
->SrcReg
[0]))
997 if (!Parse_String(parseState
, ";"))
1005 Parse_EndInstruction(struct parse_state
*parseState
, struct vp_instruction
*inst
)
1009 inst
->Opcode
= VP_OPCODE_END
;
1010 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
1012 /* this should fail! */
1013 if (Parse_Token(parseState
, token
))
1014 RETURN_ERROR2("Unexpected token after END:", token
);
1021 Parse_OptionSequence(struct parse_state
*parseState
,
1022 struct vp_instruction program
[])
1025 if (!Parse_String(parseState
, "OPTION"))
1026 return GL_TRUE
; /* ok, not an OPTION statement */
1027 if (Parse_String(parseState
, "NV_position_invariant")) {
1028 parseState
->isPositionInvariant
= GL_TRUE
;
1031 RETURN_ERROR1("unexpected OPTION statement");
1033 if (!Parse_String(parseState
, ";"))
1040 Parse_InstructionSequence(struct parse_state
*parseState
,
1041 struct vp_instruction program
[])
1044 struct vp_instruction
*inst
= program
+ parseState
->numInst
;
1046 /* Initialize the instruction */
1047 inst
->SrcReg
[0].File
= (enum register_file
) -1;
1048 inst
->SrcReg
[1].File
= (enum register_file
) -1;
1049 inst
->SrcReg
[2].File
= (enum register_file
) -1;
1050 inst
->DstReg
.File
= (enum register_file
) -1;
1052 if (Parse_String(parseState
, "MOV")) {
1053 if (!Parse_UnaryOpInstruction(parseState
, inst
, VP_OPCODE_MOV
))
1056 else if (Parse_String(parseState
, "LIT")) {
1057 if (!Parse_UnaryOpInstruction(parseState
, inst
, VP_OPCODE_LIT
))
1060 else if (Parse_String(parseState
, "ABS")) {
1061 if (!Parse_UnaryOpInstruction(parseState
, inst
, VP_OPCODE_ABS
))
1064 else if (Parse_String(parseState
, "MUL")) {
1065 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_MUL
))
1068 else if (Parse_String(parseState
, "ADD")) {
1069 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_ADD
))
1072 else if (Parse_String(parseState
, "DP3")) {
1073 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_DP3
))
1076 else if (Parse_String(parseState
, "DP4")) {
1077 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_DP4
))
1080 else if (Parse_String(parseState
, "DST")) {
1081 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_DST
))
1084 else if (Parse_String(parseState
, "MIN")) {
1085 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_MIN
))
1088 else if (Parse_String(parseState
, "MAX")) {
1089 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_MAX
))
1092 else if (Parse_String(parseState
, "SLT")) {
1093 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_SLT
))
1096 else if (Parse_String(parseState
, "SGE")) {
1097 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_SGE
))
1100 else if (Parse_String(parseState
, "DPH")) {
1101 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_DPH
))
1104 else if (Parse_String(parseState
, "SUB")) {
1105 if (!Parse_BiOpInstruction(parseState
, inst
, VP_OPCODE_SUB
))
1108 else if (Parse_String(parseState
, "MAD")) {
1109 if (!Parse_TriOpInstruction(parseState
, inst
, VP_OPCODE_MAD
))
1112 else if (Parse_String(parseState
, "RCP")) {
1113 if (!Parse_ScalarInstruction(parseState
, inst
, VP_OPCODE_RCP
))
1116 else if (Parse_String(parseState
, "RSQ")) {
1117 if (!Parse_ScalarInstruction(parseState
, inst
, VP_OPCODE_RSQ
))
1120 else if (Parse_String(parseState
, "EXP")) {
1121 if (!Parse_ScalarInstruction(parseState
, inst
, VP_OPCODE_EXP
))
1124 else if (Parse_String(parseState
, "LOG")) {
1125 if (!Parse_ScalarInstruction(parseState
, inst
, VP_OPCODE_LOG
))
1128 else if (Parse_String(parseState
, "RCC")) {
1129 if (!Parse_ScalarInstruction(parseState
, inst
, VP_OPCODE_RCC
))
1132 else if (Parse_String(parseState
, "ARL")) {
1133 if (!Parse_AddressInstruction(parseState
, inst
))
1136 else if (Parse_String(parseState
, "END")) {
1137 if (!Parse_EndInstruction(parseState
, inst
))
1140 parseState
->numInst
++;
1141 return GL_TRUE
; /* all done */
1145 /* bad instruction name */
1146 RETURN_ERROR1("Unexpected token");
1149 /* examine input/output registers */
1150 if (inst
->DstReg
.File
== PROGRAM_OUTPUT
)
1151 parseState
->outputsWritten
|= (1 << inst
->DstReg
.Index
);
1152 else if (inst
->DstReg
.File
== PROGRAM_ENV_PARAM
)
1153 parseState
->anyProgRegsWritten
= GL_TRUE
;
1155 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
)
1156 parseState
->inputsRead
|= (1 << inst
->SrcReg
[0].Index
);
1157 if (inst
->SrcReg
[1].File
== PROGRAM_INPUT
)
1158 parseState
->inputsRead
|= (1 << inst
->SrcReg
[1].Index
);
1159 if (inst
->SrcReg
[2].File
== PROGRAM_INPUT
)
1160 parseState
->inputsRead
|= (1 << inst
->SrcReg
[2].Index
);
1162 parseState
->numInst
++;
1164 if (parseState
->numInst
>= MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
)
1165 RETURN_ERROR1("Program too long");
1173 Parse_Program(struct parse_state
*parseState
,
1174 struct vp_instruction instBuffer
[])
1176 if (parseState
->isVersion1_1
) {
1177 if (!Parse_OptionSequence(parseState
, instBuffer
)) {
1181 return Parse_InstructionSequence(parseState
, instBuffer
);
1186 * Parse/compile the 'str' returning the compiled 'program'.
1187 * ctx->Program.ErrorPos will be -1 if successful. Otherwise, ErrorPos
1188 * indicates the position of the error in 'str'.
1191 _mesa_parse_nv_vertex_program(GLcontext
*ctx
, GLenum dstTarget
,
1192 const GLubyte
*str
, GLsizei len
,
1193 struct vertex_program
*program
)
1195 struct parse_state parseState
;
1196 struct vp_instruction instBuffer
[MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
];
1197 struct vp_instruction
*newInst
;
1199 GLubyte
*programString
;
1201 /* Make a null-terminated copy of the program string */
1202 programString
= (GLubyte
*) MALLOC(len
+ 1);
1203 if (!programString
) {
1204 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glLoadProgramNV");
1207 MEMCPY(programString
, str
, len
);
1208 programString
[len
] = 0;
1210 /* Get ready to parse */
1211 parseState
.ctx
= ctx
;
1212 parseState
.start
= programString
;
1213 parseState
.isPositionInvariant
= GL_FALSE
;
1214 parseState
.isVersion1_1
= GL_FALSE
;
1215 parseState
.numInst
= 0;
1216 parseState
.inputsRead
= 0;
1217 parseState
.outputsWritten
= 0;
1218 parseState
.anyProgRegsWritten
= GL_FALSE
;
1220 /* Reset error state */
1221 _mesa_set_program_error(ctx
, -1, NULL
);
1223 /* check the program header */
1224 if (_mesa_strncmp((const char *) programString
, "!!VP1.0", 7) == 0) {
1225 target
= GL_VERTEX_PROGRAM_NV
;
1226 parseState
.pos
= programString
+ 7;
1227 parseState
.isStateProgram
= GL_FALSE
;
1229 else if (_mesa_strncmp((const char *) programString
, "!!VP1.1", 7) == 0) {
1230 target
= GL_VERTEX_PROGRAM_NV
;
1231 parseState
.pos
= programString
+ 7;
1232 parseState
.isStateProgram
= GL_FALSE
;
1233 parseState
.isVersion1_1
= GL_TRUE
;
1235 else if (_mesa_strncmp((const char *) programString
, "!!VSP1.0", 8) == 0) {
1236 target
= GL_VERTEX_STATE_PROGRAM_NV
;
1237 parseState
.pos
= programString
+ 8;
1238 parseState
.isStateProgram
= GL_TRUE
;
1241 /* invalid header */
1242 ctx
->Program
.ErrorPos
= 0;
1243 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glLoadProgramNV(bad header)");
1247 /* make sure target and header match */
1248 if (target
!= dstTarget
) {
1249 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1250 "glLoadProgramNV(target mismatch)");
1255 if (Parse_Program(&parseState
, instBuffer
)) {
1256 /* successful parse! */
1258 if (parseState
.isStateProgram
) {
1259 if (!parseState
.anyProgRegsWritten
) {
1260 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1261 "glLoadProgramNV(c[#] not written)");
1266 if (!parseState
.isPositionInvariant
&&
1267 !(parseState
.outputsWritten
& 1)) {
1268 /* bit 1 = HPOS register */
1269 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1270 "glLoadProgramNV(HPOS not written)");
1275 /* copy the compiled instructions */
1276 assert(parseState
.numInst
<= MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
);
1277 newInst
= (struct vp_instruction
*)
1278 MALLOC(parseState
.numInst
* sizeof(struct vp_instruction
));
1280 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glLoadProgramNV");
1281 FREE(programString
);
1282 return; /* out of memory */
1284 MEMCPY(newInst
, instBuffer
,
1285 parseState
.numInst
* sizeof(struct vp_instruction
));
1287 /* install the program */
1288 program
->Base
.Target
= target
;
1289 if (program
->Base
.String
) {
1290 FREE(program
->Base
.String
);
1292 program
->Base
.String
= programString
;
1293 program
->Base
.Format
= GL_PROGRAM_FORMAT_ASCII_ARB
;
1294 if (program
->Instructions
) {
1295 FREE(program
->Instructions
);
1297 program
->Instructions
= newInst
;
1298 program
->InputsRead
= parseState
.inputsRead
;
1299 program
->OutputsWritten
= parseState
.outputsWritten
;
1300 program
->IsPositionInvariant
= parseState
.isPositionInvariant
;
1303 _mesa_printf("--- glLoadProgramNV result ---\n");
1304 _mesa_print_nv_vertex_program(program
);
1305 _mesa_printf("------------------------------\n");
1310 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glLoadProgramNV");
1311 /* NOTE: _mesa_set_program_error would have been called already */
1312 /* GL_NV_vertex_program isn't supposed to set the error string
1313 * so we reset it here.
1315 _mesa_set_program_error(ctx
, ctx
->Program
.ErrorPos
, NULL
);
1321 PrintSrcReg(const struct vp_src_register
*src
)
1323 static const char comps
[5] = "xyzw";
1328 _mesa_printf("c[A0.x + %d]", src
->Index
);
1329 else if (src
->Index
< 0)
1330 _mesa_printf("c[A0.x - %d]", -src
->Index
);
1332 _mesa_printf("c[A0.x]");
1334 else if (src
->File
== PROGRAM_OUTPUT
) {
1335 _mesa_printf("o[%s]", OutputRegisters
[src
->Index
]);
1337 else if (src
->File
== PROGRAM_INPUT
) {
1338 _mesa_printf("v[%s]", InputRegisters
[src
->Index
]);
1340 else if (src
->File
== PROGRAM_ENV_PARAM
) {
1341 _mesa_printf("c[%d]", src
->Index
);
1344 ASSERT(src
->File
== PROGRAM_TEMPORARY
);
1345 _mesa_printf("R%d", src
->Index
);
1348 if (src
->Swizzle
[0] == src
->Swizzle
[1] &&
1349 src
->Swizzle
[0] == src
->Swizzle
[2] &&
1350 src
->Swizzle
[0] == src
->Swizzle
[3]) {
1351 _mesa_printf(".%c", comps
[src
->Swizzle
[0]]);
1353 else if (src
->Swizzle
[0] != 0 ||
1354 src
->Swizzle
[1] != 1 ||
1355 src
->Swizzle
[2] != 2 ||
1356 src
->Swizzle
[3] != 3) {
1357 _mesa_printf(".%c%c%c%c",
1358 comps
[src
->Swizzle
[0]],
1359 comps
[src
->Swizzle
[1]],
1360 comps
[src
->Swizzle
[2]],
1361 comps
[src
->Swizzle
[3]]);
1367 PrintDstReg(const struct vp_dst_register
*dst
)
1369 GLint w
= dst
->WriteMask
[0] + dst
->WriteMask
[1]
1370 + dst
->WriteMask
[2] + dst
->WriteMask
[3];
1372 if (dst
->File
== PROGRAM_OUTPUT
) {
1373 _mesa_printf("o[%s]", OutputRegisters
[dst
->Index
]);
1375 else if (dst
->File
== PROGRAM_INPUT
) {
1376 _mesa_printf("v[%s]", InputRegisters
[dst
->Index
]);
1378 else if (dst
->File
== PROGRAM_ENV_PARAM
) {
1379 _mesa_printf("c[%d]", dst
->Index
);
1382 ASSERT(dst
->File
== PROGRAM_TEMPORARY
);
1383 _mesa_printf("R%d", dst
->Index
);
1386 if (w
!= 0 && w
!= 4) {
1388 if (dst
->WriteMask
[0])
1390 if (dst
->WriteMask
[1])
1392 if (dst
->WriteMask
[2])
1394 if (dst
->WriteMask
[3])
1401 * Print a single NVIDIA vertex program instruction.
1404 _mesa_print_nv_vertex_instruction(const struct vp_instruction
*inst
)
1406 switch (inst
->Opcode
) {
1415 _mesa_printf("%s ", Opcodes
[(int) inst
->Opcode
]);
1416 PrintDstReg(&inst
->DstReg
);
1418 PrintSrcReg(&inst
->SrcReg
[0]);
1419 _mesa_printf(";\n");
1432 _mesa_printf("%s ", Opcodes
[(int) inst
->Opcode
]);
1433 PrintDstReg(&inst
->DstReg
);
1435 PrintSrcReg(&inst
->SrcReg
[0]);
1437 PrintSrcReg(&inst
->SrcReg
[1]);
1438 _mesa_printf(";\n");
1441 _mesa_printf("MAD ");
1442 PrintDstReg(&inst
->DstReg
);
1444 PrintSrcReg(&inst
->SrcReg
[0]);
1446 PrintSrcReg(&inst
->SrcReg
[1]);
1448 PrintSrcReg(&inst
->SrcReg
[2]);
1449 _mesa_printf(";\n");
1452 _mesa_printf("ARL A0.x, ");
1453 PrintSrcReg(&inst
->SrcReg
[0]);
1454 _mesa_printf(";\n");
1457 _mesa_printf("END\n");
1460 _mesa_printf("BAD INSTRUCTION\n");
1466 * Print (unparse) the given vertex program. Just for debugging.
1469 _mesa_print_nv_vertex_program(const struct vertex_program
*program
)
1471 const struct vp_instruction
*inst
;
1473 for (inst
= program
->Instructions
; ; inst
++) {
1474 _mesa_print_nv_vertex_instruction(inst
);
1475 if (inst
->Opcode
== VP_OPCODE_END
)
1482 _mesa_nv_vertex_input_register_name(GLuint i
)
1484 ASSERT(i
< MAX_NV_VERTEX_PROGRAM_INPUTS
);
1485 return InputRegisters
[i
];
1490 _mesa_nv_vertex_output_register_name(GLuint i
)
1492 ASSERT(i
< MAX_NV_VERTEX_PROGRAM_OUTPUTS
);
1493 return OutputRegisters
[i
];