2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 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.
44 #include "nvprogram.h"
45 #include "nvvertparse.h"
46 #include "prog_instruction.h"
51 * Current parsing state. This structure is passed among the parsing
52 * functions and keeps track of the current parser position and various
59 const GLubyte
*curLine
;
60 GLboolean isStateProgram
;
61 GLboolean isPositionInvariant
;
62 GLboolean isVersion1_1
;
63 GLbitfield inputsRead
;
64 GLbitfield outputsWritten
;
65 GLboolean anyProgRegsWritten
;
66 GLuint numInst
; /* number of instructions parsed */
71 * Called whenever we find an error during parsing.
74 record_error(struct parse_state
*parseState
, const char *msg
, int lineNo
)
78 const GLubyte
*lineStr
;
79 lineStr
= _mesa_find_line_column(parseState
->start
,
80 parseState
->pos
, &line
, &column
);
81 _mesa_debug(parseState
->ctx
,
82 "nvfragparse.c(%d): line %d, column %d:%s (%s)\n",
83 lineNo
, line
, column
, (char *) lineStr
, msg
);
84 _mesa_free((void *) lineStr
);
89 /* Check that no error was already recorded. Only record the first one. */
90 if (parseState
->ctx
->Program
.ErrorString
[0] == 0) {
91 _mesa_set_program_error(parseState
->ctx
,
92 parseState
->pos
- parseState
->start
,
98 #define RETURN_ERROR \
100 record_error(parseState, "Unexpected end of input.", __LINE__); \
104 #define RETURN_ERROR1(msg) \
106 record_error(parseState, msg, __LINE__); \
110 #define RETURN_ERROR2(msg1, msg2) \
113 _mesa_sprintf(err, "%s %s", msg1, msg2); \
114 record_error(parseState, err, __LINE__); \
122 static GLboolean
IsLetter(GLubyte b
)
124 return (b
>= 'a' && b
<= 'z') || (b
>= 'A' && b
<= 'Z');
128 static GLboolean
IsDigit(GLubyte b
)
130 return b
>= '0' && b
<= '9';
134 static GLboolean
IsWhitespace(GLubyte b
)
136 return b
== ' ' || b
== '\t' || b
== '\n' || b
== '\r';
141 * Starting at 'str' find the next token. A token can be an integer,
142 * an identifier or punctuation symbol.
143 * \return <= 0 we found an error, else, return number of characters parsed.
146 GetToken(struct parse_state
*parseState
, GLubyte
*token
)
148 const GLubyte
*str
= parseState
->pos
;
153 /* skip whitespace and comments */
154 while (str
[i
] && (IsWhitespace(str
[i
]) || str
[i
] == '#')) {
157 while (str
[i
] && (str
[i
] != '\n' && str
[i
] != '\r')) {
160 if (str
[i
] == '\n' || str
[i
] == '\r')
161 parseState
->curLine
= str
+ i
+ 1;
164 /* skip whitespace */
165 if (str
[i
] == '\n' || str
[i
] == '\r')
166 parseState
->curLine
= str
+ i
+ 1;
174 /* try matching an integer */
175 while (str
[i
] && IsDigit(str
[i
])) {
176 token
[j
++] = str
[i
++];
178 if (j
> 0 || !str
[i
]) {
183 /* try matching an identifier */
184 if (IsLetter(str
[i
])) {
185 while (str
[i
] && (IsLetter(str
[i
]) || IsDigit(str
[i
]))) {
186 token
[j
++] = str
[i
++];
192 /* punctuation character */
206 * Get next token from input stream and increment stream pointer past token.
209 Parse_Token(struct parse_state
*parseState
, GLubyte
*token
)
212 i
= GetToken(parseState
, token
);
214 parseState
->pos
+= (-i
);
217 parseState
->pos
+= i
;
223 * Get next token from input stream but don't increment stream pointer.
226 Peek_Token(struct parse_state
*parseState
, GLubyte
*token
)
229 i
= GetToken(parseState
, token
);
231 parseState
->pos
+= (-i
);
234 len
= (GLint
)_mesa_strlen((const char *) token
);
235 parseState
->pos
+= (i
- len
);
241 * Try to match 'pattern' as the next token after any whitespace/comments.
242 * Advance the current parsing position only if we match the pattern.
243 * \return GL_TRUE if pattern is matched, GL_FALSE otherwise.
246 Parse_String(struct parse_state
*parseState
, const char *pattern
)
251 /* skip whitespace and comments */
252 while (IsWhitespace(*parseState
->pos
) || *parseState
->pos
== '#') {
253 if (*parseState
->pos
== '#') {
254 while (*parseState
->pos
&& (*parseState
->pos
!= '\n' && *parseState
->pos
!= '\r')) {
255 parseState
->pos
+= 1;
257 if (*parseState
->pos
== '\n' || *parseState
->pos
== '\r')
258 parseState
->curLine
= parseState
->pos
+ 1;
261 /* skip whitespace */
262 if (*parseState
->pos
== '\n' || *parseState
->pos
== '\r')
263 parseState
->curLine
= parseState
->pos
+ 1;
264 parseState
->pos
+= 1;
268 /* Try to match the pattern */
270 for (i
= 0; pattern
[i
]; i
++) {
271 if (*m
!= (GLubyte
) pattern
[i
])
277 return GL_TRUE
; /* success */
281 /**********************************************************************/
283 static const char *InputRegisters
[MAX_NV_VERTEX_PROGRAM_INPUTS
+ 1] = {
284 "OPOS", "WGHT", "NRML", "COL0", "COL1", "FOGC", "6", "7",
285 "TEX0", "TEX1", "TEX2", "TEX3", "TEX4", "TEX5", "TEX6", "TEX7", NULL
288 static const char *OutputRegisters
[MAX_NV_VERTEX_PROGRAM_OUTPUTS
+ 1] = {
289 "HPOS", "COL0", "COL1", "FOGC",
290 "TEX0", "TEX1", "TEX2", "TEX3", "TEX4", "TEX5", "TEX6", "TEX7",
291 "PSIZ", "BFC0", "BFC1", NULL
297 * Parse a temporary register: Rnn
300 Parse_TempReg(struct parse_state
*parseState
, GLint
*tempRegNum
)
304 /* Should be 'R##' */
305 if (!Parse_Token(parseState
, token
))
308 RETURN_ERROR1("Expected R##");
310 if (IsDigit(token
[1])) {
311 GLint reg
= _mesa_atoi((char *) (token
+ 1));
312 if (reg
>= MAX_NV_VERTEX_PROGRAM_TEMPS
)
313 RETURN_ERROR1("Bad temporary register name");
317 RETURN_ERROR1("Bad temporary register name");
325 * Parse address register "A0.x"
328 Parse_AddrReg(struct parse_state
*parseState
)
331 if (!Parse_String(parseState
, "A0"))
335 if (!Parse_String(parseState
, "."))
339 if (!Parse_String(parseState
, "x"))
347 * Parse absolute program parameter register "c[##]"
350 Parse_AbsParamReg(struct parse_state
*parseState
, GLint
*regNum
)
354 if (!Parse_String(parseState
, "c"))
357 if (!Parse_String(parseState
, "["))
360 if (!Parse_Token(parseState
, token
))
363 if (IsDigit(token
[0])) {
364 /* a numbered program parameter register */
365 GLint reg
= _mesa_atoi((char *) token
);
366 if (reg
>= MAX_NV_VERTEX_PROGRAM_PARAMS
)
367 RETURN_ERROR1("Bad program parameter number");
374 if (!Parse_String(parseState
, "]"))
382 Parse_ParamReg(struct parse_state
*parseState
, struct prog_src_register
*srcReg
)
386 if (!Parse_String(parseState
, "c"))
389 if (!Parse_String(parseState
, "["))
392 if (!Peek_Token(parseState
, token
))
395 if (IsDigit(token
[0])) {
396 /* a numbered program parameter register */
398 (void) Parse_Token(parseState
, token
);
399 reg
= _mesa_atoi((char *) token
);
400 if (reg
>= MAX_NV_VERTEX_PROGRAM_PARAMS
)
401 RETURN_ERROR1("Bad program parameter number");
402 srcReg
->File
= PROGRAM_ENV_PARAM
;
405 else if (_mesa_strcmp((const char *) token
, "A0") == 0) {
406 /* address register "A0.x" */
407 if (!Parse_AddrReg(parseState
))
410 srcReg
->RelAddr
= GL_TRUE
;
411 srcReg
->File
= PROGRAM_ENV_PARAM
;
412 /* Look for +/-N offset */
413 if (!Peek_Token(parseState
, token
))
416 if (token
[0] == '-' || token
[0] == '+') {
417 const GLubyte sign
= token
[0];
418 (void) Parse_Token(parseState
, token
); /* consume +/- */
420 /* an integer should be next */
421 if (!Parse_Token(parseState
, token
))
424 if (IsDigit(token
[0])) {
425 const GLint k
= _mesa_atoi((char *) token
);
428 RETURN_ERROR1("Bad address offset");
433 RETURN_ERROR1("Bad address offset");
442 /* probably got a ']', catch it below */
449 /* Match closing ']' */
450 if (!Parse_String(parseState
, "]"))
458 * Parse v[#] or v[<name>]
461 Parse_AttribReg(struct parse_state
*parseState
, GLint
*tempRegNum
)
467 if (!Parse_String(parseState
, "v"))
471 if (!Parse_String(parseState
, "["))
474 /* match number or named register */
475 if (!Parse_Token(parseState
, token
))
478 if (parseState
->isStateProgram
&& token
[0] != '0')
479 RETURN_ERROR1("Only v[0] accessible in vertex state programs");
481 if (IsDigit(token
[0])) {
482 GLint reg
= _mesa_atoi((char *) token
);
483 if (reg
>= MAX_NV_VERTEX_PROGRAM_INPUTS
)
484 RETURN_ERROR1("Bad vertex attribute register name");
488 for (j
= 0; InputRegisters
[j
]; j
++) {
489 if (_mesa_strcmp((const char *) token
, InputRegisters
[j
]) == 0) {
494 if (!InputRegisters
[j
]) {
495 /* unknown input register label */
496 RETURN_ERROR2("Bad register name", token
);
501 if (!Parse_String(parseState
, "]"))
509 Parse_OutputReg(struct parse_state
*parseState
, GLint
*outputRegNum
)
515 if (!Parse_String(parseState
, "o"))
519 if (!Parse_String(parseState
, "["))
522 /* Get output reg name */
523 if (!Parse_Token(parseState
, token
))
526 if (parseState
->isPositionInvariant
)
527 start
= 1; /* skip HPOS register name */
531 /* try to match an output register name */
532 for (j
= start
; OutputRegisters
[j
]; j
++) {
533 if (_mesa_strcmp((const char *) token
, OutputRegisters
[j
]) == 0) {
538 if (!OutputRegisters
[j
])
539 RETURN_ERROR1("Unrecognized output register name");
542 if (!Parse_String(parseState
, "]"))
543 RETURN_ERROR1("Expected ]");
550 Parse_MaskedDstReg(struct parse_state
*parseState
, struct prog_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
, &idx
))
566 else if (!parseState
->isStateProgram
&& token
[0] == 'o') {
567 /* an output register */
568 dstReg
->File
= PROGRAM_OUTPUT
;
569 if (!Parse_OutputReg(parseState
, &idx
))
573 else if (parseState
->isStateProgram
&& token
[0] == 'c' &&
574 parseState
->isStateProgram
) {
575 /* absolute program parameter register */
576 /* Only valid for vertex state programs */
577 dstReg
->File
= PROGRAM_ENV_PARAM
;
578 if (!Parse_AbsParamReg(parseState
, &idx
))
583 RETURN_ERROR1("Bad destination register name");
586 /* Parse optional write mask */
587 if (!Peek_Token(parseState
, token
))
590 if (token
[0] == '.') {
594 if (!Parse_String(parseState
, "."))
597 if (!Parse_Token(parseState
, token
))
600 dstReg
->WriteMask
= 0;
602 if (token
[k
] == 'x') {
603 dstReg
->WriteMask
|= WRITEMASK_X
;
606 if (token
[k
] == 'y') {
607 dstReg
->WriteMask
|= WRITEMASK_Y
;
610 if (token
[k
] == 'z') {
611 dstReg
->WriteMask
|= WRITEMASK_Z
;
614 if (token
[k
] == 'w') {
615 dstReg
->WriteMask
|= WRITEMASK_W
;
619 RETURN_ERROR1("Bad writemask character");
624 dstReg
->WriteMask
= WRITEMASK_XYZW
;
631 Parse_SwizzleSrcReg(struct parse_state
*parseState
, struct prog_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
->NegateBase
= NEGATE_XYZW
;
644 if (!Peek_Token(parseState
, token
))
648 srcReg
->NegateBase
= NEGATE_NONE
;
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
, &idx
))
658 else if (token
[0] == 'c') {
659 if (!Parse_ParamReg(parseState
, srcReg
))
662 else if (token
[0] == 'v') {
663 srcReg
->File
= PROGRAM_INPUT
;
664 if (!Parse_AttribReg(parseState
, &idx
))
669 RETURN_ERROR2("Bad source register name", token
);
672 /* init swizzle fields */
673 srcReg
->Swizzle
= SWIZZLE_NOOP
;
675 /* Look for optional swizzle suffix */
676 if (!Peek_Token(parseState
, token
))
678 if (token
[0] == '.') {
679 (void) Parse_String(parseState
, "."); /* consume . */
681 if (!Parse_Token(parseState
, token
))
685 /* single letter swizzle */
687 srcReg
->Swizzle
= MAKE_SWIZZLE4(0, 0, 0, 0);
688 else if (token
[0] == 'y')
689 srcReg
->Swizzle
= MAKE_SWIZZLE4(1, 1, 1, 1);
690 else if (token
[0] == 'z')
691 srcReg
->Swizzle
= MAKE_SWIZZLE4(2, 2, 2, 2);
692 else if (token
[0] == 'w')
693 srcReg
->Swizzle
= MAKE_SWIZZLE4(3, 3, 3, 3);
695 RETURN_ERROR1("Expected x, y, z, or w");
698 /* 2, 3 or 4-component swizzle */
703 for (k
= 0; token
[k
] && k
< 5; k
++) {
705 srcReg
->Swizzle
|= 0 << (k
*3);
706 else if (token
[k
] == 'y')
707 srcReg
->Swizzle
|= 1 << (k
*3);
708 else if (token
[k
] == 'z')
709 srcReg
->Swizzle
|= 2 << (k
*3);
710 else if (token
[k
] == 'w')
711 srcReg
->Swizzle
|= 3 << (k
*3);
725 Parse_ScalarSrcReg(struct parse_state
*parseState
, struct prog_src_register
*srcReg
)
730 srcReg
->RelAddr
= GL_FALSE
;
733 if (!Peek_Token(parseState
, token
))
735 if (token
[0] == '-') {
736 srcReg
->NegateBase
= NEGATE_XYZW
;
737 (void) Parse_String(parseState
, "-"); /* consume '-' */
738 if (!Peek_Token(parseState
, token
))
742 srcReg
->NegateBase
= NEGATE_NONE
;
745 /* Src reg can be R<n>, c[n], c[n +/- offset], or a named vertex attrib */
746 if (token
[0] == 'R') {
747 srcReg
->File
= PROGRAM_TEMPORARY
;
748 if (!Parse_TempReg(parseState
, &idx
))
752 else if (token
[0] == 'c') {
753 if (!Parse_ParamReg(parseState
, srcReg
))
756 else if (token
[0] == 'v') {
757 srcReg
->File
= PROGRAM_INPUT
;
758 if (!Parse_AttribReg(parseState
, &idx
))
763 RETURN_ERROR2("Bad source register name", token
);
766 /* Look for .[xyzw] suffix */
767 if (!Parse_String(parseState
, "."))
770 if (!Parse_Token(parseState
, token
))
773 if (token
[0] == 'x' && token
[1] == 0) {
776 else if (token
[0] == 'y' && token
[1] == 0) {
779 else if (token
[0] == 'z' && token
[1] == 0) {
782 else if (token
[0] == 'w' && token
[1] == 0) {
786 RETURN_ERROR1("Bad scalar source suffix");
794 Parse_UnaryOpInstruction(struct parse_state
*parseState
,
795 struct prog_instruction
*inst
,
796 enum prog_opcode opcode
)
798 if (opcode
== OPCODE_ABS
&& !parseState
->isVersion1_1
)
799 RETURN_ERROR1("ABS illegal for vertex program 1.0");
801 inst
->Opcode
= opcode
;
802 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
805 if (!Parse_MaskedDstReg(parseState
, &inst
->DstReg
))
809 if (!Parse_String(parseState
, ","))
813 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[0]))
817 if (!Parse_String(parseState
, ";"))
825 Parse_BiOpInstruction(struct parse_state
*parseState
,
826 struct prog_instruction
*inst
,
827 enum prog_opcode opcode
)
829 if (opcode
== OPCODE_DPH
&& !parseState
->isVersion1_1
)
830 RETURN_ERROR1("DPH illegal for vertex program 1.0");
831 if (opcode
== OPCODE_SUB
&& !parseState
->isVersion1_1
)
832 RETURN_ERROR1("SUB illegal for vertex program 1.0");
834 inst
->Opcode
= opcode
;
835 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
838 if (!Parse_MaskedDstReg(parseState
, &inst
->DstReg
))
842 if (!Parse_String(parseState
, ","))
846 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[0]))
850 if (!Parse_String(parseState
, ","))
854 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[1]))
858 if (!Parse_String(parseState
, ";"))
861 /* make sure we don't reference more than one program parameter register */
862 if (inst
->SrcReg
[0].File
== PROGRAM_ENV_PARAM
&&
863 inst
->SrcReg
[1].File
== PROGRAM_ENV_PARAM
&&
864 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[1].Index
)
865 RETURN_ERROR1("Can't reference two program parameter registers");
867 /* make sure we don't reference more than one vertex attribute register */
868 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
869 inst
->SrcReg
[1].File
== PROGRAM_INPUT
&&
870 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[1].Index
)
871 RETURN_ERROR1("Can't reference two vertex attribute registers");
878 Parse_TriOpInstruction(struct parse_state
*parseState
,
879 struct prog_instruction
*inst
,
880 enum prog_opcode opcode
)
882 inst
->Opcode
= opcode
;
883 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
886 if (!Parse_MaskedDstReg(parseState
, &inst
->DstReg
))
890 if (!Parse_String(parseState
, ","))
894 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[0]))
898 if (!Parse_String(parseState
, ","))
902 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[1]))
906 if (!Parse_String(parseState
, ","))
910 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[2]))
914 if (!Parse_String(parseState
, ";"))
917 /* make sure we don't reference more than one program parameter register */
918 if ((inst
->SrcReg
[0].File
== PROGRAM_ENV_PARAM
&&
919 inst
->SrcReg
[1].File
== PROGRAM_ENV_PARAM
&&
920 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[1].Index
) ||
921 (inst
->SrcReg
[0].File
== PROGRAM_ENV_PARAM
&&
922 inst
->SrcReg
[2].File
== PROGRAM_ENV_PARAM
&&
923 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[2].Index
) ||
924 (inst
->SrcReg
[1].File
== PROGRAM_ENV_PARAM
&&
925 inst
->SrcReg
[2].File
== PROGRAM_ENV_PARAM
&&
926 inst
->SrcReg
[1].Index
!= inst
->SrcReg
[2].Index
))
927 RETURN_ERROR1("Can only reference one program register");
929 /* make sure we don't reference more than one vertex attribute register */
930 if ((inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
931 inst
->SrcReg
[1].File
== PROGRAM_INPUT
&&
932 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[1].Index
) ||
933 (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
934 inst
->SrcReg
[2].File
== PROGRAM_INPUT
&&
935 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[2].Index
) ||
936 (inst
->SrcReg
[1].File
== PROGRAM_INPUT
&&
937 inst
->SrcReg
[2].File
== PROGRAM_INPUT
&&
938 inst
->SrcReg
[1].Index
!= inst
->SrcReg
[2].Index
))
939 RETURN_ERROR1("Can only reference one input register");
946 Parse_ScalarInstruction(struct parse_state
*parseState
,
947 struct prog_instruction
*inst
,
948 enum prog_opcode opcode
)
950 if (opcode
== OPCODE_RCC
&& !parseState
->isVersion1_1
)
951 RETURN_ERROR1("RCC illegal for vertex program 1.0");
953 inst
->Opcode
= opcode
;
954 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
957 if (!Parse_MaskedDstReg(parseState
, &inst
->DstReg
))
961 if (!Parse_String(parseState
, ","))
965 if (!Parse_ScalarSrcReg(parseState
, &inst
->SrcReg
[0]))
969 if (!Parse_String(parseState
, ";"))
977 Parse_AddressInstruction(struct parse_state
*parseState
, struct prog_instruction
*inst
)
979 inst
->Opcode
= OPCODE_ARL
;
980 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
982 /* Make ARB_vp backends happy */
983 inst
->DstReg
.File
= PROGRAM_ADDRESS
;
984 inst
->DstReg
.WriteMask
= WRITEMASK_X
;
985 inst
->DstReg
.Index
= 0;
988 if (!Parse_AddrReg(parseState
))
992 if (!Parse_String(parseState
, ","))
996 if (!Parse_ScalarSrcReg(parseState
, &inst
->SrcReg
[0]))
1000 if (!Parse_String(parseState
, ";"))
1008 Parse_EndInstruction(struct parse_state
*parseState
, struct prog_instruction
*inst
)
1012 inst
->Opcode
= OPCODE_END
;
1013 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
1015 /* this should fail! */
1016 if (Parse_Token(parseState
, token
))
1017 RETURN_ERROR2("Unexpected token after END:", token
);
1024 * The PRINT instruction is Mesa-specific and is meant as a debugging aid for
1025 * the vertex program developer.
1026 * The NV_vertex_program extension grammar is modified as follows:
1028 * <instruction> ::= <ARL-instruction>
1030 * | <PRINT-instruction>
1032 * <PRINT-instruction> ::= "PRINT" <string literal>
1033 * | "PRINT" <string literal> "," <srcReg>
1034 * | "PRINT" <string literal> "," <dstReg>
1037 Parse_PrintInstruction(struct parse_state
*parseState
, struct prog_instruction
*inst
)
1043 struct prog_src_register
*srcReg
= &inst
->SrcReg
[0];
1046 inst
->Opcode
= OPCODE_PRINT
;
1047 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
1049 /* The first argument is a literal string 'just like this' */
1050 if (!Parse_String(parseState
, "'"))
1053 str
= parseState
->pos
;
1054 for (len
= 0; str
[len
] != '\''; len
++) /* find closing quote */
1056 parseState
->pos
+= len
+ 1;
1057 msg
= (GLubyte
*) _mesa_malloc(len
+ 1);
1059 _mesa_memcpy(msg
, str
, len
);
1064 if (Parse_String(parseState
, ",")) {
1066 /* The second argument is a register name */
1067 if (!Peek_Token(parseState
, token
))
1070 srcReg
->RelAddr
= GL_FALSE
;
1071 srcReg
->NegateBase
= NEGATE_NONE
;
1072 srcReg
->Swizzle
= SWIZZLE_NOOP
;
1074 /* Register can be R<n>, c[n], c[n +/- offset], a named vertex attrib,
1075 * or an o[n] output register.
1077 if (token
[0] == 'R') {
1078 srcReg
->File
= PROGRAM_TEMPORARY
;
1079 if (!Parse_TempReg(parseState
, &idx
))
1081 srcReg
->Index
= idx
;
1083 else if (token
[0] == 'c') {
1084 srcReg
->File
= PROGRAM_ENV_PARAM
;
1085 if (!Parse_ParamReg(parseState
, srcReg
))
1088 else if (token
[0] == 'v') {
1089 srcReg
->File
= PROGRAM_INPUT
;
1090 if (!Parse_AttribReg(parseState
, &idx
))
1092 srcReg
->Index
= idx
;
1094 else if (token
[0] == 'o') {
1095 srcReg
->File
= PROGRAM_OUTPUT
;
1096 if (!Parse_OutputReg(parseState
, &idx
))
1098 srcReg
->Index
= idx
;
1101 RETURN_ERROR2("Bad source register name", token
);
1109 if (!Parse_String(parseState
, ";"))
1117 Parse_OptionSequence(struct parse_state
*parseState
,
1118 struct prog_instruction program
[])
1122 if (!Parse_String(parseState
, "OPTION"))
1123 return GL_TRUE
; /* ok, not an OPTION statement */
1124 if (Parse_String(parseState
, "NV_position_invariant")) {
1125 parseState
->isPositionInvariant
= GL_TRUE
;
1128 RETURN_ERROR1("unexpected OPTION statement");
1130 if (!Parse_String(parseState
, ";"))
1137 Parse_InstructionSequence(struct parse_state
*parseState
,
1138 struct prog_instruction program
[])
1141 struct prog_instruction
*inst
= program
+ parseState
->numInst
;
1143 /* Initialize the instruction */
1144 _mesa_init_instructions(inst
, 1);
1146 if (Parse_String(parseState
, "MOV")) {
1147 if (!Parse_UnaryOpInstruction(parseState
, inst
, OPCODE_MOV
))
1150 else if (Parse_String(parseState
, "LIT")) {
1151 if (!Parse_UnaryOpInstruction(parseState
, inst
, OPCODE_LIT
))
1154 else if (Parse_String(parseState
, "ABS")) {
1155 if (!Parse_UnaryOpInstruction(parseState
, inst
, OPCODE_ABS
))
1158 else if (Parse_String(parseState
, "MUL")) {
1159 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_MUL
))
1162 else if (Parse_String(parseState
, "ADD")) {
1163 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_ADD
))
1166 else if (Parse_String(parseState
, "DP3")) {
1167 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_DP3
))
1170 else if (Parse_String(parseState
, "DP4")) {
1171 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_DP4
))
1174 else if (Parse_String(parseState
, "DST")) {
1175 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_DST
))
1178 else if (Parse_String(parseState
, "MIN")) {
1179 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_MIN
))
1182 else if (Parse_String(parseState
, "MAX")) {
1183 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_MAX
))
1186 else if (Parse_String(parseState
, "SLT")) {
1187 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_SLT
))
1190 else if (Parse_String(parseState
, "SGE")) {
1191 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_SGE
))
1194 else if (Parse_String(parseState
, "DPH")) {
1195 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_DPH
))
1198 else if (Parse_String(parseState
, "SUB")) {
1199 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_SUB
))
1202 else if (Parse_String(parseState
, "MAD")) {
1203 if (!Parse_TriOpInstruction(parseState
, inst
, OPCODE_MAD
))
1206 else if (Parse_String(parseState
, "RCP")) {
1207 if (!Parse_ScalarInstruction(parseState
, inst
, OPCODE_RCP
))
1210 else if (Parse_String(parseState
, "RSQ")) {
1211 if (!Parse_ScalarInstruction(parseState
, inst
, OPCODE_RSQ
))
1214 else if (Parse_String(parseState
, "EXP")) {
1215 if (!Parse_ScalarInstruction(parseState
, inst
, OPCODE_EXP
))
1218 else if (Parse_String(parseState
, "LOG")) {
1219 if (!Parse_ScalarInstruction(parseState
, inst
, OPCODE_LOG
))
1222 else if (Parse_String(parseState
, "RCC")) {
1223 if (!Parse_ScalarInstruction(parseState
, inst
, OPCODE_RCC
))
1226 else if (Parse_String(parseState
, "ARL")) {
1227 if (!Parse_AddressInstruction(parseState
, inst
))
1230 else if (Parse_String(parseState
, "PRINT")) {
1231 if (!Parse_PrintInstruction(parseState
, inst
))
1234 else if (Parse_String(parseState
, "END")) {
1235 if (!Parse_EndInstruction(parseState
, inst
))
1238 parseState
->numInst
++;
1239 return GL_TRUE
; /* all done */
1243 /* bad instruction name */
1244 RETURN_ERROR1("Unexpected token");
1247 /* examine input/output registers */
1248 if (inst
->DstReg
.File
== PROGRAM_OUTPUT
)
1249 parseState
->outputsWritten
|= (1 << inst
->DstReg
.Index
);
1250 else if (inst
->DstReg
.File
== PROGRAM_ENV_PARAM
)
1251 parseState
->anyProgRegsWritten
= GL_TRUE
;
1253 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
)
1254 parseState
->inputsRead
|= (1 << inst
->SrcReg
[0].Index
);
1255 if (inst
->SrcReg
[1].File
== PROGRAM_INPUT
)
1256 parseState
->inputsRead
|= (1 << inst
->SrcReg
[1].Index
);
1257 if (inst
->SrcReg
[2].File
== PROGRAM_INPUT
)
1258 parseState
->inputsRead
|= (1 << inst
->SrcReg
[2].Index
);
1260 parseState
->numInst
++;
1262 if (parseState
->numInst
>= MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
)
1263 RETURN_ERROR1("Program too long");
1271 Parse_Program(struct parse_state
*parseState
,
1272 struct prog_instruction instBuffer
[])
1274 if (parseState
->isVersion1_1
) {
1275 if (!Parse_OptionSequence(parseState
, instBuffer
)) {
1279 return Parse_InstructionSequence(parseState
, instBuffer
);
1284 * Parse/compile the 'str' returning the compiled 'program'.
1285 * ctx->Program.ErrorPos will be -1 if successful. Otherwise, ErrorPos
1286 * indicates the position of the error in 'str'.
1289 _mesa_parse_nv_vertex_program(GLcontext
*ctx
, GLenum dstTarget
,
1290 const GLubyte
*str
, GLsizei len
,
1291 struct gl_vertex_program
*program
)
1293 struct parse_state parseState
;
1294 struct prog_instruction instBuffer
[MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
];
1295 struct prog_instruction
*newInst
;
1297 GLubyte
*programString
;
1299 /* Make a null-terminated copy of the program string */
1300 programString
= (GLubyte
*) MALLOC(len
+ 1);
1301 if (!programString
) {
1302 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glLoadProgramNV");
1305 MEMCPY(programString
, str
, len
);
1306 programString
[len
] = 0;
1308 /* Get ready to parse */
1309 parseState
.ctx
= ctx
;
1310 parseState
.start
= programString
;
1311 parseState
.isPositionInvariant
= GL_FALSE
;
1312 parseState
.isVersion1_1
= GL_FALSE
;
1313 parseState
.numInst
= 0;
1314 parseState
.inputsRead
= 0;
1315 parseState
.outputsWritten
= 0;
1316 parseState
.anyProgRegsWritten
= GL_FALSE
;
1318 /* Reset error state */
1319 _mesa_set_program_error(ctx
, -1, NULL
);
1321 /* check the program header */
1322 if (_mesa_strncmp((const char *) programString
, "!!VP1.0", 7) == 0) {
1323 target
= GL_VERTEX_PROGRAM_NV
;
1324 parseState
.pos
= programString
+ 7;
1325 parseState
.isStateProgram
= GL_FALSE
;
1327 else if (_mesa_strncmp((const char *) programString
, "!!VP1.1", 7) == 0) {
1328 target
= GL_VERTEX_PROGRAM_NV
;
1329 parseState
.pos
= programString
+ 7;
1330 parseState
.isStateProgram
= GL_FALSE
;
1331 parseState
.isVersion1_1
= GL_TRUE
;
1333 else if (_mesa_strncmp((const char *) programString
, "!!VSP1.0", 8) == 0) {
1334 target
= GL_VERTEX_STATE_PROGRAM_NV
;
1335 parseState
.pos
= programString
+ 8;
1336 parseState
.isStateProgram
= GL_TRUE
;
1339 /* invalid header */
1340 ctx
->Program
.ErrorPos
= 0;
1341 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glLoadProgramNV(bad header)");
1345 /* make sure target and header match */
1346 if (target
!= dstTarget
) {
1347 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1348 "glLoadProgramNV(target mismatch)");
1353 if (Parse_Program(&parseState
, instBuffer
)) {
1354 /* successful parse! */
1356 if (parseState
.isStateProgram
) {
1357 if (!parseState
.anyProgRegsWritten
) {
1358 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1359 "glLoadProgramNV(c[#] not written)");
1364 if (!parseState
.isPositionInvariant
&&
1365 !(parseState
.outputsWritten
& (1 << VERT_RESULT_HPOS
))) {
1366 /* bit 1 = HPOS register */
1367 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1368 "glLoadProgramNV(HPOS not written)");
1373 /* copy the compiled instructions */
1374 assert(parseState
.numInst
<= MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
);
1375 newInst
= _mesa_alloc_instructions(parseState
.numInst
);
1377 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glLoadProgramNV");
1378 _mesa_free(programString
);
1379 return; /* out of memory */
1381 _mesa_memcpy(newInst
, instBuffer
,
1382 parseState
.numInst
* sizeof(struct prog_instruction
));
1384 /* install the program */
1385 program
->Base
.Target
= target
;
1386 if (program
->Base
.String
) {
1387 _mesa_free(program
->Base
.String
);
1389 program
->Base
.String
= programString
;
1390 program
->Base
.Format
= GL_PROGRAM_FORMAT_ASCII_ARB
;
1391 if (program
->Base
.Instructions
) {
1392 _mesa_free(program
->Base
.Instructions
);
1394 program
->Base
.Instructions
= newInst
;
1395 program
->Base
.InputsRead
= parseState
.inputsRead
;
1396 if (parseState
.isPositionInvariant
)
1397 program
->Base
.InputsRead
|= VERT_BIT_POS
;
1398 program
->Base
.NumInstructions
= parseState
.numInst
;
1399 program
->Base
.OutputsWritten
= parseState
.outputsWritten
;
1400 program
->IsPositionInvariant
= parseState
.isPositionInvariant
;
1401 program
->IsNVProgram
= GL_TRUE
;
1404 _mesa_printf("--- glLoadProgramNV result ---\n");
1405 _mesa_print_nv_vertex_program(program
);
1406 _mesa_printf("------------------------------\n");
1411 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glLoadProgramNV");
1412 /* NOTE: _mesa_set_program_error would have been called already */
1413 /* GL_NV_vertex_program isn't supposed to set the error string
1414 * so we reset it here.
1416 _mesa_set_program_error(ctx
, ctx
->Program
.ErrorPos
, NULL
);
1422 PrintSrcReg(const struct prog_src_register
*src
)
1424 static const char comps
[5] = "xyzw";
1425 if (src
->NegateBase
)
1429 _mesa_printf("c[A0.x + %d]", src
->Index
);
1430 else if (src
->Index
< 0)
1431 _mesa_printf("c[A0.x - %d]", -src
->Index
);
1433 _mesa_printf("c[A0.x]");
1435 else if (src
->File
== PROGRAM_OUTPUT
) {
1436 _mesa_printf("o[%s]", OutputRegisters
[src
->Index
]);
1438 else if (src
->File
== PROGRAM_INPUT
) {
1439 _mesa_printf("v[%s]", InputRegisters
[src
->Index
]);
1441 else if (src
->File
== PROGRAM_ENV_PARAM
) {
1442 _mesa_printf("c[%d]", src
->Index
);
1445 ASSERT(src
->File
== PROGRAM_TEMPORARY
);
1446 _mesa_printf("R%d", src
->Index
);
1449 if (GET_SWZ(src
->Swizzle
, 0) == GET_SWZ(src
->Swizzle
, 1) &&
1450 GET_SWZ(src
->Swizzle
, 0) == GET_SWZ(src
->Swizzle
, 2) &&
1451 GET_SWZ(src
->Swizzle
, 0) == GET_SWZ(src
->Swizzle
, 3)) {
1452 _mesa_printf(".%c", comps
[GET_SWZ(src
->Swizzle
, 0)]);
1454 else if (src
->Swizzle
!= SWIZZLE_NOOP
) {
1455 _mesa_printf(".%c%c%c%c",
1456 comps
[GET_SWZ(src
->Swizzle
, 0)],
1457 comps
[GET_SWZ(src
->Swizzle
, 1)],
1458 comps
[GET_SWZ(src
->Swizzle
, 2)],
1459 comps
[GET_SWZ(src
->Swizzle
, 3)]);
1465 PrintDstReg(const struct prog_dst_register
*dst
)
1467 if (dst
->File
== PROGRAM_OUTPUT
) {
1468 _mesa_printf("o[%s]", OutputRegisters
[dst
->Index
]);
1470 else if (dst
->File
== PROGRAM_INPUT
) {
1471 _mesa_printf("v[%s]", InputRegisters
[dst
->Index
]);
1473 else if (dst
->File
== PROGRAM_ENV_PARAM
) {
1474 _mesa_printf("c[%d]", dst
->Index
);
1477 ASSERT(dst
->File
== PROGRAM_TEMPORARY
);
1478 _mesa_printf("R%d", dst
->Index
);
1481 if (dst
->WriteMask
!= 0 && dst
->WriteMask
!= WRITEMASK_XYZW
) {
1483 if (dst
->WriteMask
& WRITEMASK_X
)
1485 if (dst
->WriteMask
& WRITEMASK_Y
)
1487 if (dst
->WriteMask
& WRITEMASK_Z
)
1489 if (dst
->WriteMask
& WRITEMASK_W
)
1496 * Print a single NVIDIA vertex program instruction.
1499 _mesa_print_nv_vertex_instruction(const struct prog_instruction
*inst
)
1503 switch (inst
->Opcode
) {
1524 _mesa_printf("%s ", _mesa_opcode_string(inst
->Opcode
));
1525 PrintDstReg(&inst
->DstReg
);
1527 n
= _mesa_num_inst_src_regs(inst
->Opcode
);
1528 for (i
= 0; i
< n
; i
++) {
1529 PrintSrcReg(&inst
->SrcReg
[i
]);
1533 _mesa_printf(";\n");
1536 _mesa_printf("ARL A0.x, ");
1537 PrintSrcReg(&inst
->SrcReg
[0]);
1538 _mesa_printf(";\n");
1541 _mesa_printf("PRINT '%s'", inst
->Data
);
1542 if (inst
->SrcReg
[0].File
!= PROGRAM_UNDEFINED
) {
1544 PrintSrcReg(&inst
->SrcReg
[0]);
1545 _mesa_printf(";\n");
1552 _mesa_printf("END\n");
1555 _mesa_printf("BAD INSTRUCTION\n");
1561 * Print (unparse) the given vertex program. Just for debugging.
1564 _mesa_print_nv_vertex_program(const struct gl_vertex_program
*program
)
1566 const struct prog_instruction
*inst
;
1568 for (inst
= program
->Base
.Instructions
; ; inst
++) {
1569 _mesa_print_nv_vertex_instruction(inst
);
1570 if (inst
->Opcode
== OPCODE_END
)
1577 _mesa_nv_vertex_input_register_name(GLuint i
)
1579 ASSERT(i
< MAX_NV_VERTEX_PROGRAM_INPUTS
);
1580 return InputRegisters
[i
];
1585 _mesa_nv_vertex_output_register_name(GLuint i
)
1587 ASSERT(i
< MAX_NV_VERTEX_PROGRAM_OUTPUTS
);
1588 return OutputRegisters
[i
];