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.
40 #include "main/glheader.h"
41 #include "main/context.h"
42 #include "main/imports.h"
43 #include "main/macros.h"
44 #include "nvprogram.h"
45 #include "nvvertparse.h"
46 #include "prog_instruction.h"
47 #include "prog_parameter.h"
48 #include "prog_print.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
;
65 GLbitfield inputsRead
;
66 GLbitfield 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
= (GLint
)_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", "FOGC",
292 "TEX0", "TEX1", "TEX2", "TEX3", "TEX4", "TEX5", "TEX6", "TEX7",
293 "PSIZ", "BFC0", "BFC1", NULL
299 * Parse a temporary register: Rnn
302 Parse_TempReg(struct parse_state
*parseState
, GLint
*tempRegNum
)
306 /* Should be 'R##' */
307 if (!Parse_Token(parseState
, token
))
310 RETURN_ERROR1("Expected R##");
312 if (IsDigit(token
[1])) {
313 GLint reg
= _mesa_atoi((char *) (token
+ 1));
314 if (reg
>= MAX_NV_VERTEX_PROGRAM_TEMPS
)
315 RETURN_ERROR1("Bad temporary register name");
319 RETURN_ERROR1("Bad temporary register name");
327 * Parse address register "A0.x"
330 Parse_AddrReg(struct parse_state
*parseState
)
333 if (!Parse_String(parseState
, "A0"))
337 if (!Parse_String(parseState
, "."))
341 if (!Parse_String(parseState
, "x"))
349 * Parse absolute program parameter register "c[##]"
352 Parse_AbsParamReg(struct parse_state
*parseState
, GLint
*regNum
)
356 if (!Parse_String(parseState
, "c"))
359 if (!Parse_String(parseState
, "["))
362 if (!Parse_Token(parseState
, token
))
365 if (IsDigit(token
[0])) {
366 /* a numbered program parameter register */
367 GLint reg
= _mesa_atoi((char *) token
);
368 if (reg
>= MAX_NV_VERTEX_PROGRAM_PARAMS
)
369 RETURN_ERROR1("Bad program parameter number");
376 if (!Parse_String(parseState
, "]"))
384 Parse_ParamReg(struct parse_state
*parseState
, struct prog_src_register
*srcReg
)
388 if (!Parse_String(parseState
, "c"))
391 if (!Parse_String(parseState
, "["))
394 if (!Peek_Token(parseState
, token
))
397 if (IsDigit(token
[0])) {
398 /* a numbered program parameter register */
400 (void) Parse_Token(parseState
, token
);
401 reg
= _mesa_atoi((char *) token
);
402 if (reg
>= MAX_NV_VERTEX_PROGRAM_PARAMS
)
403 RETURN_ERROR1("Bad program parameter number");
404 srcReg
->File
= PROGRAM_ENV_PARAM
;
407 else if (_mesa_strcmp((const char *) token
, "A0") == 0) {
408 /* address register "A0.x" */
409 if (!Parse_AddrReg(parseState
))
412 srcReg
->RelAddr
= GL_TRUE
;
413 srcReg
->File
= PROGRAM_ENV_PARAM
;
414 /* Look for +/-N offset */
415 if (!Peek_Token(parseState
, token
))
418 if (token
[0] == '-' || token
[0] == '+') {
419 const GLubyte sign
= token
[0];
420 (void) Parse_Token(parseState
, token
); /* consume +/- */
422 /* an integer should be next */
423 if (!Parse_Token(parseState
, token
))
426 if (IsDigit(token
[0])) {
427 const GLint k
= _mesa_atoi((char *) token
);
430 RETURN_ERROR1("Bad address offset");
435 RETURN_ERROR1("Bad address offset");
444 /* probably got a ']', catch it below */
451 /* Match closing ']' */
452 if (!Parse_String(parseState
, "]"))
460 * Parse v[#] or v[<name>]
463 Parse_AttribReg(struct parse_state
*parseState
, GLint
*tempRegNum
)
469 if (!Parse_String(parseState
, "v"))
473 if (!Parse_String(parseState
, "["))
476 /* match number or named register */
477 if (!Parse_Token(parseState
, token
))
480 if (parseState
->isStateProgram
&& token
[0] != '0')
481 RETURN_ERROR1("Only v[0] accessible in vertex state programs");
483 if (IsDigit(token
[0])) {
484 GLint reg
= _mesa_atoi((char *) token
);
485 if (reg
>= MAX_NV_VERTEX_PROGRAM_INPUTS
)
486 RETURN_ERROR1("Bad vertex attribute register name");
490 for (j
= 0; InputRegisters
[j
]; j
++) {
491 if (_mesa_strcmp((const char *) token
, InputRegisters
[j
]) == 0) {
496 if (!InputRegisters
[j
]) {
497 /* unknown input register label */
498 RETURN_ERROR2("Bad register name", token
);
503 if (!Parse_String(parseState
, "]"))
511 Parse_OutputReg(struct parse_state
*parseState
, GLint
*outputRegNum
)
517 if (!Parse_String(parseState
, "o"))
521 if (!Parse_String(parseState
, "["))
524 /* Get output reg name */
525 if (!Parse_Token(parseState
, token
))
528 if (parseState
->isPositionInvariant
)
529 start
= 1; /* skip HPOS register name */
533 /* try to match an output register name */
534 for (j
= start
; OutputRegisters
[j
]; j
++) {
535 if (_mesa_strcmp((const char *) token
, OutputRegisters
[j
]) == 0) {
540 if (!OutputRegisters
[j
])
541 RETURN_ERROR1("Unrecognized output register name");
544 if (!Parse_String(parseState
, "]"))
545 RETURN_ERROR1("Expected ]");
552 Parse_MaskedDstReg(struct parse_state
*parseState
, struct prog_dst_register
*dstReg
)
557 /* Dst reg can be R<n> or o[n] */
558 if (!Peek_Token(parseState
, token
))
561 if (token
[0] == 'R') {
562 /* a temporary register */
563 dstReg
->File
= PROGRAM_TEMPORARY
;
564 if (!Parse_TempReg(parseState
, &idx
))
568 else if (!parseState
->isStateProgram
&& token
[0] == 'o') {
569 /* an output register */
570 dstReg
->File
= PROGRAM_OUTPUT
;
571 if (!Parse_OutputReg(parseState
, &idx
))
575 else if (parseState
->isStateProgram
&& token
[0] == 'c' &&
576 parseState
->isStateProgram
) {
577 /* absolute program parameter register */
578 /* Only valid for vertex state programs */
579 dstReg
->File
= PROGRAM_ENV_PARAM
;
580 if (!Parse_AbsParamReg(parseState
, &idx
))
585 RETURN_ERROR1("Bad destination register name");
588 /* Parse optional write mask */
589 if (!Peek_Token(parseState
, token
))
592 if (token
[0] == '.') {
596 if (!Parse_String(parseState
, "."))
599 if (!Parse_Token(parseState
, token
))
602 dstReg
->WriteMask
= 0;
604 if (token
[k
] == 'x') {
605 dstReg
->WriteMask
|= WRITEMASK_X
;
608 if (token
[k
] == 'y') {
609 dstReg
->WriteMask
|= WRITEMASK_Y
;
612 if (token
[k
] == 'z') {
613 dstReg
->WriteMask
|= WRITEMASK_Z
;
616 if (token
[k
] == 'w') {
617 dstReg
->WriteMask
|= WRITEMASK_W
;
621 RETURN_ERROR1("Bad writemask character");
626 dstReg
->WriteMask
= WRITEMASK_XYZW
;
633 Parse_SwizzleSrcReg(struct parse_state
*parseState
, struct prog_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
= NEGATE_XYZW
;
646 if (!Peek_Token(parseState
, token
))
650 srcReg
->Negate
= NEGATE_NONE
;
653 /* Src reg can be R<n>, c[n], c[n +/- offset], or a named vertex attrib */
654 if (token
[0] == 'R') {
655 srcReg
->File
= PROGRAM_TEMPORARY
;
656 if (!Parse_TempReg(parseState
, &idx
))
660 else if (token
[0] == 'c') {
661 if (!Parse_ParamReg(parseState
, srcReg
))
664 else if (token
[0] == 'v') {
665 srcReg
->File
= PROGRAM_INPUT
;
666 if (!Parse_AttribReg(parseState
, &idx
))
671 RETURN_ERROR2("Bad source register name", token
);
674 /* init swizzle fields */
675 srcReg
->Swizzle
= SWIZZLE_NOOP
;
677 /* Look for optional swizzle suffix */
678 if (!Peek_Token(parseState
, token
))
680 if (token
[0] == '.') {
681 (void) Parse_String(parseState
, "."); /* consume . */
683 if (!Parse_Token(parseState
, token
))
687 /* single letter swizzle */
689 srcReg
->Swizzle
= SWIZZLE_XXXX
;
690 else if (token
[0] == 'y')
691 srcReg
->Swizzle
= SWIZZLE_YYYY
;
692 else if (token
[0] == 'z')
693 srcReg
->Swizzle
= SWIZZLE_ZZZZ
;
694 else if (token
[0] == 'w')
695 srcReg
->Swizzle
= SWIZZLE_WWWW
;
697 RETURN_ERROR1("Expected x, y, z, or w");
700 /* 2, 3 or 4-component swizzle */
705 for (k
= 0; token
[k
] && k
< 5; k
++) {
707 srcReg
->Swizzle
|= 0 << (k
*3);
708 else if (token
[k
] == 'y')
709 srcReg
->Swizzle
|= 1 << (k
*3);
710 else if (token
[k
] == 'z')
711 srcReg
->Swizzle
|= 2 << (k
*3);
712 else if (token
[k
] == 'w')
713 srcReg
->Swizzle
|= 3 << (k
*3);
727 Parse_ScalarSrcReg(struct parse_state
*parseState
, struct prog_src_register
*srcReg
)
732 srcReg
->RelAddr
= GL_FALSE
;
735 if (!Peek_Token(parseState
, token
))
737 if (token
[0] == '-') {
738 srcReg
->Negate
= NEGATE_XYZW
;
739 (void) Parse_String(parseState
, "-"); /* consume '-' */
740 if (!Peek_Token(parseState
, token
))
744 srcReg
->Negate
= NEGATE_NONE
;
747 /* Src reg can be R<n>, c[n], c[n +/- offset], or a named vertex attrib */
748 if (token
[0] == 'R') {
749 srcReg
->File
= PROGRAM_TEMPORARY
;
750 if (!Parse_TempReg(parseState
, &idx
))
754 else if (token
[0] == 'c') {
755 if (!Parse_ParamReg(parseState
, srcReg
))
758 else if (token
[0] == 'v') {
759 srcReg
->File
= PROGRAM_INPUT
;
760 if (!Parse_AttribReg(parseState
, &idx
))
765 RETURN_ERROR2("Bad source register name", token
);
768 /* Look for .[xyzw] suffix */
769 if (!Parse_String(parseState
, "."))
772 if (!Parse_Token(parseState
, token
))
775 if (token
[0] == 'x' && token
[1] == 0) {
778 else if (token
[0] == 'y' && token
[1] == 0) {
781 else if (token
[0] == 'z' && token
[1] == 0) {
784 else if (token
[0] == 'w' && token
[1] == 0) {
788 RETURN_ERROR1("Bad scalar source suffix");
796 Parse_UnaryOpInstruction(struct parse_state
*parseState
,
797 struct prog_instruction
*inst
,
798 enum prog_opcode opcode
)
800 if (opcode
== OPCODE_ABS
&& !parseState
->isVersion1_1
)
801 RETURN_ERROR1("ABS illegal for vertex program 1.0");
803 inst
->Opcode
= opcode
;
806 if (!Parse_MaskedDstReg(parseState
, &inst
->DstReg
))
810 if (!Parse_String(parseState
, ","))
814 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[0]))
818 if (!Parse_String(parseState
, ";"))
826 Parse_BiOpInstruction(struct parse_state
*parseState
,
827 struct prog_instruction
*inst
,
828 enum prog_opcode opcode
)
830 if (opcode
== OPCODE_DPH
&& !parseState
->isVersion1_1
)
831 RETURN_ERROR1("DPH illegal for vertex program 1.0");
832 if (opcode
== OPCODE_SUB
&& !parseState
->isVersion1_1
)
833 RETURN_ERROR1("SUB illegal for vertex program 1.0");
835 inst
->Opcode
= opcode
;
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
;
885 if (!Parse_MaskedDstReg(parseState
, &inst
->DstReg
))
889 if (!Parse_String(parseState
, ","))
893 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[0]))
897 if (!Parse_String(parseState
, ","))
901 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[1]))
905 if (!Parse_String(parseState
, ","))
909 if (!Parse_SwizzleSrcReg(parseState
, &inst
->SrcReg
[2]))
913 if (!Parse_String(parseState
, ";"))
916 /* make sure we don't reference more than one program parameter register */
917 if ((inst
->SrcReg
[0].File
== PROGRAM_ENV_PARAM
&&
918 inst
->SrcReg
[1].File
== PROGRAM_ENV_PARAM
&&
919 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[1].Index
) ||
920 (inst
->SrcReg
[0].File
== PROGRAM_ENV_PARAM
&&
921 inst
->SrcReg
[2].File
== PROGRAM_ENV_PARAM
&&
922 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[2].Index
) ||
923 (inst
->SrcReg
[1].File
== PROGRAM_ENV_PARAM
&&
924 inst
->SrcReg
[2].File
== PROGRAM_ENV_PARAM
&&
925 inst
->SrcReg
[1].Index
!= inst
->SrcReg
[2].Index
))
926 RETURN_ERROR1("Can only reference one program register");
928 /* make sure we don't reference more than one vertex attribute register */
929 if ((inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
930 inst
->SrcReg
[1].File
== PROGRAM_INPUT
&&
931 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[1].Index
) ||
932 (inst
->SrcReg
[0].File
== PROGRAM_INPUT
&&
933 inst
->SrcReg
[2].File
== PROGRAM_INPUT
&&
934 inst
->SrcReg
[0].Index
!= inst
->SrcReg
[2].Index
) ||
935 (inst
->SrcReg
[1].File
== PROGRAM_INPUT
&&
936 inst
->SrcReg
[2].File
== PROGRAM_INPUT
&&
937 inst
->SrcReg
[1].Index
!= inst
->SrcReg
[2].Index
))
938 RETURN_ERROR1("Can only reference one input register");
945 Parse_ScalarInstruction(struct parse_state
*parseState
,
946 struct prog_instruction
*inst
,
947 enum prog_opcode opcode
)
949 if (opcode
== OPCODE_RCC
&& !parseState
->isVersion1_1
)
950 RETURN_ERROR1("RCC illegal for vertex program 1.0");
952 inst
->Opcode
= opcode
;
955 if (!Parse_MaskedDstReg(parseState
, &inst
->DstReg
))
959 if (!Parse_String(parseState
, ","))
963 if (!Parse_ScalarSrcReg(parseState
, &inst
->SrcReg
[0]))
967 if (!Parse_String(parseState
, ";"))
975 Parse_AddressInstruction(struct parse_state
*parseState
, struct prog_instruction
*inst
)
977 inst
->Opcode
= OPCODE_ARL
;
979 /* Make ARB_vp backends happy */
980 inst
->DstReg
.File
= PROGRAM_ADDRESS
;
981 inst
->DstReg
.WriteMask
= WRITEMASK_X
;
982 inst
->DstReg
.Index
= 0;
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 prog_instruction
*inst
)
1009 inst
->Opcode
= OPCODE_END
;
1011 /* this should fail! */
1012 if (Parse_Token(parseState
, token
))
1013 RETURN_ERROR2("Unexpected token after END:", token
);
1020 * The PRINT instruction is Mesa-specific and is meant as a debugging aid for
1021 * the vertex program developer.
1022 * The NV_vertex_program extension grammar is modified as follows:
1024 * <instruction> ::= <ARL-instruction>
1026 * | <PRINT-instruction>
1028 * <PRINT-instruction> ::= "PRINT" <string literal>
1029 * | "PRINT" <string literal> "," <srcReg>
1030 * | "PRINT" <string literal> "," <dstReg>
1033 Parse_PrintInstruction(struct parse_state
*parseState
, struct prog_instruction
*inst
)
1039 struct prog_src_register
*srcReg
= &inst
->SrcReg
[0];
1042 inst
->Opcode
= OPCODE_PRINT
;
1044 /* The first argument is a literal string 'just like this' */
1045 if (!Parse_String(parseState
, "'"))
1048 str
= parseState
->pos
;
1049 for (len
= 0; str
[len
] != '\''; len
++) /* find closing quote */
1051 parseState
->pos
+= len
+ 1;
1052 msg
= (GLubyte
*) _mesa_malloc(len
+ 1);
1054 _mesa_memcpy(msg
, str
, len
);
1059 if (Parse_String(parseState
, ",")) {
1061 /* The second argument is a register name */
1062 if (!Peek_Token(parseState
, token
))
1065 srcReg
->RelAddr
= GL_FALSE
;
1066 srcReg
->Negate
= NEGATE_NONE
;
1067 srcReg
->Swizzle
= SWIZZLE_NOOP
;
1069 /* Register can be R<n>, c[n], c[n +/- offset], a named vertex attrib,
1070 * or an o[n] output register.
1072 if (token
[0] == 'R') {
1073 srcReg
->File
= PROGRAM_TEMPORARY
;
1074 if (!Parse_TempReg(parseState
, &idx
))
1076 srcReg
->Index
= idx
;
1078 else if (token
[0] == 'c') {
1079 srcReg
->File
= PROGRAM_ENV_PARAM
;
1080 if (!Parse_ParamReg(parseState
, srcReg
))
1083 else if (token
[0] == 'v') {
1084 srcReg
->File
= PROGRAM_INPUT
;
1085 if (!Parse_AttribReg(parseState
, &idx
))
1087 srcReg
->Index
= idx
;
1089 else if (token
[0] == 'o') {
1090 srcReg
->File
= PROGRAM_OUTPUT
;
1091 if (!Parse_OutputReg(parseState
, &idx
))
1093 srcReg
->Index
= idx
;
1096 RETURN_ERROR2("Bad source register name", token
);
1104 if (!Parse_String(parseState
, ";"))
1112 Parse_OptionSequence(struct parse_state
*parseState
,
1113 struct prog_instruction program
[])
1117 if (!Parse_String(parseState
, "OPTION"))
1118 return GL_TRUE
; /* ok, not an OPTION statement */
1119 if (Parse_String(parseState
, "NV_position_invariant")) {
1120 parseState
->isPositionInvariant
= GL_TRUE
;
1123 RETURN_ERROR1("unexpected OPTION statement");
1125 if (!Parse_String(parseState
, ";"))
1132 Parse_InstructionSequence(struct parse_state
*parseState
,
1133 struct prog_instruction program
[])
1136 struct prog_instruction
*inst
= program
+ parseState
->numInst
;
1138 /* Initialize the instruction */
1139 _mesa_init_instructions(inst
, 1);
1141 if (Parse_String(parseState
, "MOV")) {
1142 if (!Parse_UnaryOpInstruction(parseState
, inst
, OPCODE_MOV
))
1145 else if (Parse_String(parseState
, "LIT")) {
1146 if (!Parse_UnaryOpInstruction(parseState
, inst
, OPCODE_LIT
))
1149 else if (Parse_String(parseState
, "ABS")) {
1150 if (!Parse_UnaryOpInstruction(parseState
, inst
, OPCODE_ABS
))
1153 else if (Parse_String(parseState
, "MUL")) {
1154 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_MUL
))
1157 else if (Parse_String(parseState
, "ADD")) {
1158 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_ADD
))
1161 else if (Parse_String(parseState
, "DP3")) {
1162 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_DP3
))
1165 else if (Parse_String(parseState
, "DP4")) {
1166 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_DP4
))
1169 else if (Parse_String(parseState
, "DST")) {
1170 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_DST
))
1173 else if (Parse_String(parseState
, "MIN")) {
1174 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_MIN
))
1177 else if (Parse_String(parseState
, "MAX")) {
1178 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_MAX
))
1181 else if (Parse_String(parseState
, "SLT")) {
1182 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_SLT
))
1185 else if (Parse_String(parseState
, "SGE")) {
1186 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_SGE
))
1189 else if (Parse_String(parseState
, "DPH")) {
1190 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_DPH
))
1193 else if (Parse_String(parseState
, "SUB")) {
1194 if (!Parse_BiOpInstruction(parseState
, inst
, OPCODE_SUB
))
1197 else if (Parse_String(parseState
, "MAD")) {
1198 if (!Parse_TriOpInstruction(parseState
, inst
, OPCODE_MAD
))
1201 else if (Parse_String(parseState
, "RCP")) {
1202 if (!Parse_ScalarInstruction(parseState
, inst
, OPCODE_RCP
))
1205 else if (Parse_String(parseState
, "RSQ")) {
1206 if (!Parse_ScalarInstruction(parseState
, inst
, OPCODE_RSQ
))
1209 else if (Parse_String(parseState
, "EXP")) {
1210 if (!Parse_ScalarInstruction(parseState
, inst
, OPCODE_EXP
))
1213 else if (Parse_String(parseState
, "LOG")) {
1214 if (!Parse_ScalarInstruction(parseState
, inst
, OPCODE_LOG
))
1217 else if (Parse_String(parseState
, "RCC")) {
1218 if (!Parse_ScalarInstruction(parseState
, inst
, OPCODE_RCC
))
1221 else if (Parse_String(parseState
, "ARL")) {
1222 if (!Parse_AddressInstruction(parseState
, inst
))
1225 else if (Parse_String(parseState
, "PRINT")) {
1226 if (!Parse_PrintInstruction(parseState
, inst
))
1229 else if (Parse_String(parseState
, "END")) {
1230 if (!Parse_EndInstruction(parseState
, inst
))
1233 parseState
->numInst
++;
1234 return GL_TRUE
; /* all done */
1238 /* bad instruction name */
1239 RETURN_ERROR1("Unexpected token");
1242 /* examine input/output registers */
1243 if (inst
->DstReg
.File
== PROGRAM_OUTPUT
)
1244 parseState
->outputsWritten
|= (1 << inst
->DstReg
.Index
);
1245 else if (inst
->DstReg
.File
== PROGRAM_ENV_PARAM
)
1246 parseState
->anyProgRegsWritten
= GL_TRUE
;
1248 if (inst
->SrcReg
[0].File
== PROGRAM_INPUT
)
1249 parseState
->inputsRead
|= (1 << inst
->SrcReg
[0].Index
);
1250 if (inst
->SrcReg
[1].File
== PROGRAM_INPUT
)
1251 parseState
->inputsRead
|= (1 << inst
->SrcReg
[1].Index
);
1252 if (inst
->SrcReg
[2].File
== PROGRAM_INPUT
)
1253 parseState
->inputsRead
|= (1 << inst
->SrcReg
[2].Index
);
1255 parseState
->numInst
++;
1257 if (parseState
->numInst
>= MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
)
1258 RETURN_ERROR1("Program too long");
1266 Parse_Program(struct parse_state
*parseState
,
1267 struct prog_instruction instBuffer
[])
1269 if (parseState
->isVersion1_1
) {
1270 if (!Parse_OptionSequence(parseState
, instBuffer
)) {
1274 return Parse_InstructionSequence(parseState
, instBuffer
);
1279 * Parse/compile the 'str' returning the compiled 'program'.
1280 * ctx->Program.ErrorPos will be -1 if successful. Otherwise, ErrorPos
1281 * indicates the position of the error in 'str'.
1284 _mesa_parse_nv_vertex_program(GLcontext
*ctx
, GLenum dstTarget
,
1285 const GLubyte
*str
, GLsizei len
,
1286 struct gl_vertex_program
*program
)
1288 struct parse_state parseState
;
1289 struct prog_instruction instBuffer
[MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
];
1290 struct prog_instruction
*newInst
;
1292 GLubyte
*programString
;
1294 /* Make a null-terminated copy of the program string */
1295 programString
= (GLubyte
*) MALLOC(len
+ 1);
1296 if (!programString
) {
1297 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glLoadProgramNV");
1300 MEMCPY(programString
, str
, len
);
1301 programString
[len
] = 0;
1303 /* Get ready to parse */
1304 parseState
.ctx
= ctx
;
1305 parseState
.start
= programString
;
1306 parseState
.isPositionInvariant
= GL_FALSE
;
1307 parseState
.isVersion1_1
= GL_FALSE
;
1308 parseState
.numInst
= 0;
1309 parseState
.inputsRead
= 0;
1310 parseState
.outputsWritten
= 0;
1311 parseState
.anyProgRegsWritten
= GL_FALSE
;
1313 /* Reset error state */
1314 _mesa_set_program_error(ctx
, -1, NULL
);
1316 /* check the program header */
1317 if (_mesa_strncmp((const char *) programString
, "!!VP1.0", 7) == 0) {
1318 target
= GL_VERTEX_PROGRAM_NV
;
1319 parseState
.pos
= programString
+ 7;
1320 parseState
.isStateProgram
= GL_FALSE
;
1322 else if (_mesa_strncmp((const char *) programString
, "!!VP1.1", 7) == 0) {
1323 target
= GL_VERTEX_PROGRAM_NV
;
1324 parseState
.pos
= programString
+ 7;
1325 parseState
.isStateProgram
= GL_FALSE
;
1326 parseState
.isVersion1_1
= GL_TRUE
;
1328 else if (_mesa_strncmp((const char *) programString
, "!!VSP1.0", 8) == 0) {
1329 target
= GL_VERTEX_STATE_PROGRAM_NV
;
1330 parseState
.pos
= programString
+ 8;
1331 parseState
.isStateProgram
= GL_TRUE
;
1334 /* invalid header */
1335 ctx
->Program
.ErrorPos
= 0;
1336 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glLoadProgramNV(bad header)");
1340 /* make sure target and header match */
1341 if (target
!= dstTarget
) {
1342 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1343 "glLoadProgramNV(target mismatch)");
1348 if (Parse_Program(&parseState
, instBuffer
)) {
1349 gl_state_index state_tokens
[STATE_LENGTH
] = {0, 0, 0, 0, 0};
1352 /* successful parse! */
1354 if (parseState
.isStateProgram
) {
1355 if (!parseState
.anyProgRegsWritten
) {
1356 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1357 "glLoadProgramNV(c[#] not written)");
1362 if (!parseState
.isPositionInvariant
&&
1363 !(parseState
.outputsWritten
& (1 << VERT_RESULT_HPOS
))) {
1364 /* bit 1 = HPOS register */
1365 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1366 "glLoadProgramNV(HPOS not written)");
1371 /* copy the compiled instructions */
1372 assert(parseState
.numInst
<= MAX_NV_VERTEX_PROGRAM_INSTRUCTIONS
);
1373 newInst
= _mesa_alloc_instructions(parseState
.numInst
);
1375 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glLoadProgramNV");
1376 _mesa_free(programString
);
1377 return; /* out of memory */
1379 _mesa_copy_instructions(newInst
, instBuffer
, parseState
.numInst
);
1381 /* install the program */
1382 program
->Base
.Target
= target
;
1383 if (program
->Base
.String
) {
1384 _mesa_free(program
->Base
.String
);
1386 program
->Base
.String
= programString
;
1387 program
->Base
.Format
= GL_PROGRAM_FORMAT_ASCII_ARB
;
1388 if (program
->Base
.Instructions
) {
1389 _mesa_free(program
->Base
.Instructions
);
1391 program
->Base
.Instructions
= newInst
;
1392 program
->Base
.InputsRead
= parseState
.inputsRead
;
1393 if (parseState
.isPositionInvariant
)
1394 program
->Base
.InputsRead
|= VERT_BIT_POS
;
1395 program
->Base
.NumInstructions
= parseState
.numInst
;
1396 program
->Base
.OutputsWritten
= parseState
.outputsWritten
;
1397 program
->IsPositionInvariant
= parseState
.isPositionInvariant
;
1398 program
->IsNVProgram
= GL_TRUE
;
1401 _mesa_printf("--- glLoadProgramNV result ---\n");
1402 _mesa_fprint_program_opt(stdout
, &program
->Base
, PROG_PRINT_NV
, 0);
1403 _mesa_printf("------------------------------\n");
1406 if (program
->Base
.Parameters
)
1407 _mesa_free_parameter_list(program
->Base
.Parameters
);
1409 program
->Base
.Parameters
= _mesa_new_parameter_list ();
1410 program
->Base
.NumParameters
= 0;
1412 state_tokens
[0] = STATE_VERTEX_PROGRAM
;
1413 state_tokens
[1] = STATE_ENV
;
1414 /* Add refs to all of the potential params, in order. If we want to not
1415 * upload everything, _mesa_layout_parameters is the answer.
1417 for (i
= 0; i
< MAX_NV_VERTEX_PROGRAM_PARAMS
; i
++) {
1419 state_tokens
[2] = i
;
1420 index
= _mesa_add_state_reference(program
->Base
.Parameters
,
1424 program
->Base
.NumParameters
= program
->Base
.Parameters
->NumParameters
;
1426 _mesa_setup_nv_temporary_count(ctx
, &program
->Base
);
1427 _mesa_emit_nv_temp_initialization(ctx
, &program
->Base
);
1431 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glLoadProgramNV");
1432 /* NOTE: _mesa_set_program_error would have been called already */
1433 /* GL_NV_vertex_program isn't supposed to set the error string
1434 * so we reset it here.
1436 _mesa_set_program_error(ctx
, ctx
->Program
.ErrorPos
, NULL
);
1442 _mesa_nv_vertex_input_register_name(GLuint i
)
1444 ASSERT(i
< MAX_NV_VERTEX_PROGRAM_INPUTS
);
1445 return InputRegisters
[i
];
1450 _mesa_nv_vertex_output_register_name(GLuint i
)
1452 ASSERT(i
< MAX_NV_VERTEX_PROGRAM_OUTPUTS
);
1453 return OutputRegisters
[i
];