2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 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 fragment program parser.
32 * Regarding GL_NV_fragment_program:
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 "program_instruction.h"
47 #include "nvfragparse.h"
48 #include "nvprogram.h"
58 #define INPUT_1V_T 7 /* one source vector, plus textureId */
59 #define INPUT_3V_T 8 /* one source vector, plus textureId */
61 #define INPUT_1V_S 10 /* a string and a vector register */
64 #define OUTPUT_NONE 22
66 /* IRIX defines some of these */
73 /* Optional suffixes */
74 #define _R FLOAT32 /* float */
75 #define _H FLOAT16 /* half-float */
76 #define _X FIXED12 /* fixed */
77 #define _C 0x08 /* set cond codes */
78 #define _S 0x10 /* saturate, clamp result to [0,1] */
80 struct instruction_pattern
{
82 enum prog_opcode opcode
;
88 static const struct instruction_pattern Instructions
[] = {
89 { "ADD", OPCODE_ADD
, INPUT_2V
, OUTPUT_V
, _R
| _H
| _X
| _C
| _S
},
90 { "COS", OPCODE_COS
, INPUT_1S
, OUTPUT_S
, _R
| _H
| _C
| _S
},
91 { "DDX", OPCODE_DDX
, INPUT_1V
, OUTPUT_V
, _R
| _H
| _C
| _S
},
92 { "DDY", OPCODE_DDY
, INPUT_1V
, OUTPUT_V
, _R
| _H
| _C
| _S
},
93 { "DP3", OPCODE_DP3
, INPUT_2V
, OUTPUT_S
, _R
| _H
| _X
| _C
| _S
},
94 { "DP4", OPCODE_DP4
, INPUT_2V
, OUTPUT_S
, _R
| _H
| _X
| _C
| _S
},
95 { "DST", OPCODE_DP4
, INPUT_2V
, OUTPUT_V
, _R
| _H
| _C
| _S
},
96 { "EX2", OPCODE_DP4
, INPUT_1S
, OUTPUT_S
, _R
| _H
| _C
| _S
},
97 { "FLR", OPCODE_FLR
, INPUT_1V
, OUTPUT_V
, _R
| _H
| _X
| _C
| _S
},
98 { "FRC", OPCODE_FRC
, INPUT_1V
, OUTPUT_V
, _R
| _H
| _X
| _C
| _S
},
99 { "KIL", OPCODE_KIL_NV
, INPUT_CC
, OUTPUT_NONE
, 0 },
100 { "LG2", OPCODE_LG2
, INPUT_1S
, OUTPUT_S
, _R
| _H
| _C
| _S
},
101 { "LIT", OPCODE_LIT
, INPUT_1V
, OUTPUT_V
, _R
| _H
| _C
| _S
},
102 { "LRP", OPCODE_LRP
, INPUT_3V
, OUTPUT_V
, _R
| _H
| _X
| _C
| _S
},
103 { "MAD", OPCODE_MAD
, INPUT_3V
, OUTPUT_V
, _R
| _H
| _X
| _C
| _S
},
104 { "MAX", OPCODE_MAX
, INPUT_2V
, OUTPUT_V
, _R
| _H
| _X
| _C
| _S
},
105 { "MIN", OPCODE_MIN
, INPUT_2V
, OUTPUT_V
, _R
| _H
| _X
| _C
| _S
},
106 { "MOV", OPCODE_MOV
, INPUT_1V
, OUTPUT_V
, _R
| _H
| _X
| _C
| _S
},
107 { "MUL", OPCODE_MUL
, INPUT_2V
, OUTPUT_V
, _R
| _H
| _X
| _C
| _S
},
108 { "PK2H", OPCODE_PK2H
, INPUT_1V
, OUTPUT_S
, 0 },
109 { "PK2US", OPCODE_PK2US
, INPUT_1V
, OUTPUT_S
, 0 },
110 { "PK4B", OPCODE_PK4B
, INPUT_1V
, OUTPUT_S
, 0 },
111 { "PK4UB", OPCODE_PK4UB
, INPUT_1V
, OUTPUT_S
, 0 },
112 { "POW", OPCODE_POW
, INPUT_2S
, OUTPUT_S
, _R
| _H
| _C
| _S
},
113 { "RCP", OPCODE_RCP
, INPUT_1S
, OUTPUT_S
, _R
| _H
| _C
| _S
},
114 { "RFL", OPCODE_RFL
, INPUT_2V
, OUTPUT_V
, _R
| _H
| _C
| _S
},
115 { "RSQ", OPCODE_RSQ
, INPUT_1S
, OUTPUT_S
, _R
| _H
| _C
| _S
},
116 { "SEQ", OPCODE_SEQ
, INPUT_2V
, OUTPUT_V
, _R
| _H
| _X
| _C
| _S
},
117 { "SFL", OPCODE_SFL
, INPUT_2V
, OUTPUT_V
, _R
| _H
| _X
| _C
| _S
},
118 { "SGE", OPCODE_SGE
, INPUT_2V
, OUTPUT_V
, _R
| _H
| _X
| _C
| _S
},
119 { "SGT", OPCODE_SGT
, INPUT_2V
, OUTPUT_V
, _R
| _H
| _X
| _C
| _S
},
120 { "SIN", OPCODE_SIN
, INPUT_1S
, OUTPUT_S
, _R
| _H
| _C
| _S
},
121 { "SLE", OPCODE_SLE
, INPUT_2V
, OUTPUT_V
, _R
| _H
| _X
| _C
| _S
},
122 { "SLT", OPCODE_SLT
, INPUT_2V
, OUTPUT_V
, _R
| _H
| _X
| _C
| _S
},
123 { "SNE", OPCODE_SNE
, INPUT_2V
, OUTPUT_V
, _R
| _H
| _X
| _C
| _S
},
124 { "STR", OPCODE_STR
, INPUT_2V
, OUTPUT_V
, _R
| _H
| _X
| _C
| _S
},
125 { "SUB", OPCODE_SUB
, INPUT_2V
, OUTPUT_V
, _R
| _H
| _X
| _C
| _S
},
126 { "TEX", OPCODE_TEX
, INPUT_1V_T
, OUTPUT_V
, _C
| _S
},
127 { "TXD", OPCODE_TXD
, INPUT_3V_T
, OUTPUT_V
, _C
| _S
},
128 { "TXP", OPCODE_TXP_NV
, INPUT_1V_T
, OUTPUT_V
, _C
| _S
},
129 { "UP2H", OPCODE_UP2H
, INPUT_1S
, OUTPUT_V
, _C
| _S
},
130 { "UP2US", OPCODE_UP2US
, INPUT_1S
, OUTPUT_V
, _C
| _S
},
131 { "UP4B", OPCODE_UP4B
, INPUT_1S
, OUTPUT_V
, _C
| _S
},
132 { "UP4UB", OPCODE_UP4UB
, INPUT_1S
, OUTPUT_V
, _C
| _S
},
133 { "X2D", OPCODE_X2D
, INPUT_3V
, OUTPUT_V
, _R
| _H
| _C
| _S
},
134 { "PRINT", OPCODE_PRINT
, INPUT_1V_S
, OUTPUT_NONE
, 0 },
135 { NULL
, (enum prog_opcode
) -1, 0, 0, 0 }
140 * Information needed or computed during parsing.
141 * Remember, we can't modify the target program object until we've
142 * _successfully_ parsed the program text.
146 const GLubyte
*start
; /* start of program string */
147 const GLubyte
*pos
; /* current position */
148 const GLubyte
*curLine
;
149 struct gl_fragment_program
*program
; /* current program */
151 struct gl_program_parameter_list
*parameters
;
153 GLuint numInst
; /* number of instructions parsed */
154 GLuint inputsRead
; /* bitmask of input registers used */
155 GLuint outputsWritten
; /* bitmask of 1 << FRAG_OUTPUT_* bits */
156 GLuint texturesUsed
[MAX_TEXTURE_IMAGE_UNITS
];
162 * Called whenever we find an error during parsing.
165 record_error(struct parse_state
*parseState
, const char *msg
, int lineNo
)
169 const GLubyte
*lineStr
;
170 lineStr
= _mesa_find_line_column(parseState
->start
,
171 parseState
->pos
, &line
, &column
);
172 _mesa_debug(parseState
->ctx
,
173 "nvfragparse.c(%d): line %d, column %d:%s (%s)\n",
174 lineNo
, line
, column
, (char *) lineStr
, msg
);
175 _mesa_free((void *) lineStr
);
180 /* Check that no error was already recorded. Only record the first one. */
181 if (parseState
->ctx
->Program
.ErrorString
[0] == 0) {
182 _mesa_set_program_error(parseState
->ctx
,
183 parseState
->pos
- parseState
->start
,
189 #define RETURN_ERROR \
191 record_error(parseState, "Unexpected end of input.", __LINE__); \
195 #define RETURN_ERROR1(msg) \
197 record_error(parseState, msg, __LINE__); \
201 #define RETURN_ERROR2(msg1, msg2) \
204 _mesa_sprintf(err, "%s %s", msg1, msg2); \
205 record_error(parseState, err, __LINE__); \
213 * Search a list of instruction structures for a match.
215 static struct instruction_pattern
216 MatchInstruction(const GLubyte
*token
)
218 const struct instruction_pattern
*inst
;
219 struct instruction_pattern result
;
221 for (inst
= Instructions
; inst
->name
; inst
++) {
222 if (_mesa_strncmp((const char *) token
, inst
->name
, 3) == 0) {
228 if (token
[i
] == 'R') {
229 result
.suffixes
|= _R
;
232 else if (token
[i
] == 'H') {
233 result
.suffixes
|= _H
;
236 else if (token
[i
] == 'X') {
237 result
.suffixes
|= _X
;
240 if (token
[i
] == 'C') {
241 result
.suffixes
|= _C
;
244 if (token
[i
] == '_' && token
[i
+1] == 'S' &&
245 token
[i
+2] == 'A' && token
[i
+3] == 'T') {
246 result
.suffixes
|= _S
;
251 result
.opcode
= MAX_OPCODE
; /* i.e. invalid instruction */
258 /**********************************************************************/
261 static GLboolean
IsLetter(GLubyte b
)
263 return (b
>= 'a' && b
<= 'z') ||
264 (b
>= 'A' && b
<= 'Z') ||
270 static GLboolean
IsDigit(GLubyte b
)
272 return b
>= '0' && b
<= '9';
276 static GLboolean
IsWhitespace(GLubyte b
)
278 return b
== ' ' || b
== '\t' || b
== '\n' || b
== '\r';
283 * Starting at 'str' find the next token. A token can be an integer,
284 * an identifier or punctuation symbol.
285 * \return <= 0 we found an error, else, return number of characters parsed.
288 GetToken(struct parse_state
*parseState
, GLubyte
*token
)
290 const GLubyte
*str
= parseState
->pos
;
295 /* skip whitespace and comments */
296 while (str
[i
] && (IsWhitespace(str
[i
]) || str
[i
] == '#')) {
299 while (str
[i
] && (str
[i
] != '\n' && str
[i
] != '\r')) {
302 if (str
[i
] == '\n' || str
[i
] == '\r')
303 parseState
->curLine
= str
+ i
+ 1;
306 /* skip whitespace */
307 if (str
[i
] == '\n' || str
[i
] == '\r')
308 parseState
->curLine
= str
+ i
+ 1;
316 /* try matching an integer */
317 while (str
[i
] && IsDigit(str
[i
])) {
318 token
[j
++] = str
[i
++];
320 if (j
> 0 || !str
[i
]) {
325 /* try matching an identifier */
326 if (IsLetter(str
[i
])) {
327 while (str
[i
] && (IsLetter(str
[i
]) || IsDigit(str
[i
]))) {
328 token
[j
++] = str
[i
++];
334 /* punctuation character */
348 * Get next token from input stream and increment stream pointer past token.
351 Parse_Token(struct parse_state
*parseState
, GLubyte
*token
)
354 i
= GetToken(parseState
, token
);
356 parseState
->pos
+= (-i
);
359 parseState
->pos
+= i
;
365 * Get next token from input stream but don't increment stream pointer.
368 Peek_Token(struct parse_state
*parseState
, GLubyte
*token
)
371 i
= GetToken(parseState
, token
);
373 parseState
->pos
+= (-i
);
376 len
= (GLint
)_mesa_strlen((const char *) token
);
377 parseState
->pos
+= (i
- len
);
382 /**********************************************************************/
384 static const char *InputRegisters
[MAX_NV_FRAGMENT_PROGRAM_INPUTS
+ 1] = {
385 "WPOS", "COL0", "COL1", "FOGC",
386 "TEX0", "TEX1", "TEX2", "TEX3", "TEX4", "TEX5", "TEX6", "TEX7", NULL
389 static const char *OutputRegisters
[MAX_NV_FRAGMENT_PROGRAM_OUTPUTS
+ 1] = {
391 /* These are only allows for register combiners */
393 "TEX0", "TEX1", "TEX2", "TEX3",
401 /**********************************************************************/
404 * Try to match 'pattern' as the next token after any whitespace/comments.
407 Parse_String(struct parse_state
*parseState
, const char *pattern
)
412 /* skip whitespace and comments */
413 while (IsWhitespace(*parseState
->pos
) || *parseState
->pos
== '#') {
414 if (*parseState
->pos
== '#') {
415 while (*parseState
->pos
&& (*parseState
->pos
!= '\n' && *parseState
->pos
!= '\r')) {
416 parseState
->pos
+= 1;
418 if (*parseState
->pos
== '\n' || *parseState
->pos
== '\r')
419 parseState
->curLine
= parseState
->pos
+ 1;
422 /* skip whitespace */
423 if (*parseState
->pos
== '\n' || *parseState
->pos
== '\r')
424 parseState
->curLine
= parseState
->pos
+ 1;
425 parseState
->pos
+= 1;
429 /* Try to match the pattern */
431 for (i
= 0; pattern
[i
]; i
++) {
432 if (*m
!= (GLubyte
) pattern
[i
])
438 return GL_TRUE
; /* success */
443 Parse_Identifier(struct parse_state
*parseState
, GLubyte
*ident
)
445 if (!Parse_Token(parseState
, ident
))
447 if (IsLetter(ident
[0]))
450 RETURN_ERROR1("Expected an identfier");
455 * Parse a floating point constant, or a defined symbol name.
457 * Output: number[0 .. 3] will get the value.
460 Parse_ScalarConstant(struct parse_state
*parseState
, GLfloat
*number
)
464 *number
= (GLfloat
) _mesa_strtod((const char *) parseState
->pos
, &end
);
466 if (end
&& end
> (char *) parseState
->pos
) {
468 parseState
->pos
= (GLubyte
*) end
;
475 /* should be an identifier */
477 const GLfloat
*constant
;
478 if (!Parse_Identifier(parseState
, ident
))
479 RETURN_ERROR1("Expected an identifier");
480 constant
= _mesa_lookup_parameter_value(parseState
->parameters
,
481 -1, (const char *) ident
);
482 /* XXX Check that it's a constant and not a parameter */
484 RETURN_ERROR1("Undefined symbol");
487 COPY_4V(number
, constant
);
496 * Parse a vector constant, one of:
499 * { float, float, float }
500 * { float, float, float, float }
503 Parse_VectorConstant(struct parse_state
*parseState
, GLfloat
*vec
)
505 /* "{" was already consumed */
507 ASSIGN_4V(vec
, 0.0, 0.0, 0.0, 1.0);
509 if (!Parse_ScalarConstant(parseState
, vec
+0)) /* X */
512 if (Parse_String(parseState
, "}")) {
516 if (!Parse_String(parseState
, ","))
517 RETURN_ERROR1("Expected comma in vector constant");
519 if (!Parse_ScalarConstant(parseState
, vec
+1)) /* Y */
522 if (Parse_String(parseState
, "}")) {
526 if (!Parse_String(parseState
, ","))
527 RETURN_ERROR1("Expected comma in vector constant");
529 if (!Parse_ScalarConstant(parseState
, vec
+2)) /* Z */
532 if (Parse_String(parseState
, "}")) {
536 if (!Parse_String(parseState
, ","))
537 RETURN_ERROR1("Expected comma in vector constant");
539 if (!Parse_ScalarConstant(parseState
, vec
+3)) /* W */
542 if (!Parse_String(parseState
, "}"))
543 RETURN_ERROR1("Expected closing brace in vector constant");
550 * Parse <number>, <varname> or {a, b, c, d}.
551 * Return number of values in the vector or scalar, or zero if parse error.
554 Parse_VectorOrScalarConstant(struct parse_state
*parseState
, GLfloat
*vec
)
556 if (Parse_String(parseState
, "{")) {
557 return Parse_VectorConstant(parseState
, vec
);
560 GLboolean b
= Parse_ScalarConstant(parseState
, vec
);
562 vec
[1] = vec
[2] = vec
[3] = vec
[0];
570 * Parse a texture image source:
571 * [TEX0 | TEX1 | .. | TEX15] , [1D | 2D | 3D | CUBE | RECT]
574 Parse_TextureImageId(struct parse_state
*parseState
,
575 GLubyte
*texUnit
, GLubyte
*texTargetBit
)
577 GLubyte imageSrc
[100];
580 if (!Parse_Token(parseState
, imageSrc
))
583 if (imageSrc
[0] != 'T' ||
584 imageSrc
[1] != 'E' ||
585 imageSrc
[2] != 'X') {
586 RETURN_ERROR1("Expected TEX# source");
588 unit
= _mesa_atoi((const char *) imageSrc
+ 3);
589 if ((unit
< 0 || unit
> MAX_TEXTURE_IMAGE_UNITS
) ||
590 (unit
== 0 && (imageSrc
[3] != '0' || imageSrc
[4] != 0))) {
591 RETURN_ERROR1("Invalied TEX# source index");
595 if (!Parse_String(parseState
, ","))
596 RETURN_ERROR1("Expected ,");
598 if (Parse_String(parseState
, "1D")) {
599 *texTargetBit
= TEXTURE_1D_BIT
;
601 else if (Parse_String(parseState
, "2D")) {
602 *texTargetBit
= TEXTURE_2D_BIT
;
604 else if (Parse_String(parseState
, "3D")) {
605 *texTargetBit
= TEXTURE_3D_BIT
;
607 else if (Parse_String(parseState
, "CUBE")) {
608 *texTargetBit
= TEXTURE_CUBE_BIT
;
610 else if (Parse_String(parseState
, "RECT")) {
611 *texTargetBit
= TEXTURE_RECT_BIT
;
614 RETURN_ERROR1("Invalid texture target token");
617 /* update record of referenced texture units */
618 parseState
->texturesUsed
[*texUnit
] |= *texTargetBit
;
619 if (_mesa_bitcount(parseState
->texturesUsed
[*texUnit
]) > 1) {
620 RETURN_ERROR1("Only one texture target can be used per texture unit.");
628 * Parse a scalar suffix like .x, .y, .z or .w or parse a swizzle suffix
629 * like .wxyz, .xxyy, etc and return the swizzle indexes.
632 Parse_SwizzleSuffix(const GLubyte
*token
, GLuint swizzle
[4])
635 /* single letter swizzle (scalar) */
637 ASSIGN_4V(swizzle
, 0, 0, 0, 0);
638 else if (token
[0] == 'y')
639 ASSIGN_4V(swizzle
, 1, 1, 1, 1);
640 else if (token
[0] == 'z')
641 ASSIGN_4V(swizzle
, 2, 2, 2, 2);
642 else if (token
[0] == 'w')
643 ASSIGN_4V(swizzle
, 3, 3, 3, 3);
648 /* 4-component swizzle (vector) */
650 for (k
= 0; token
[k
] && k
< 4; k
++) {
653 else if (token
[k
] == 'y')
655 else if (token
[k
] == 'z')
657 else if (token
[k
] == 'w')
670 Parse_CondCodeMask(struct parse_state
*parseState
,
671 struct prog_dst_register
*dstReg
)
673 if (Parse_String(parseState
, "EQ"))
674 dstReg
->CondMask
= COND_EQ
;
675 else if (Parse_String(parseState
, "GE"))
676 dstReg
->CondMask
= COND_GE
;
677 else if (Parse_String(parseState
, "GT"))
678 dstReg
->CondMask
= COND_GT
;
679 else if (Parse_String(parseState
, "LE"))
680 dstReg
->CondMask
= COND_LE
;
681 else if (Parse_String(parseState
, "LT"))
682 dstReg
->CondMask
= COND_LT
;
683 else if (Parse_String(parseState
, "NE"))
684 dstReg
->CondMask
= COND_NE
;
685 else if (Parse_String(parseState
, "TR"))
686 dstReg
->CondMask
= COND_TR
;
687 else if (Parse_String(parseState
, "FL"))
688 dstReg
->CondMask
= COND_FL
;
690 RETURN_ERROR1("Invalid condition code mask");
692 /* look for optional .xyzw swizzle */
693 if (Parse_String(parseState
, ".")) {
697 if (!Parse_Token(parseState
, token
)) /* get xyzw suffix */
700 if (!Parse_SwizzleSuffix(token
, swz
))
701 RETURN_ERROR1("Invalid swizzle suffix");
703 dstReg
->CondSwizzle
= MAKE_SWIZZLE4(swz
[0], swz
[1], swz
[2], swz
[3]);
711 * Parse a temporary register: Rnn or Hnn
714 Parse_TempReg(struct parse_state
*parseState
, GLint
*tempRegNum
)
718 /* Should be 'R##' or 'H##' */
719 if (!Parse_Token(parseState
, token
))
721 if (token
[0] != 'R' && token
[0] != 'H')
722 RETURN_ERROR1("Expected R## or H##");
724 if (IsDigit(token
[1])) {
725 GLint reg
= _mesa_atoi((const char *) (token
+ 1));
728 if (reg
>= MAX_NV_FRAGMENT_PROGRAM_TEMPS
)
729 RETURN_ERROR1("Invalid temporary register name");
733 RETURN_ERROR1("Invalid temporary register name");
741 * Parse a write-only dummy register: RC or HC.
744 Parse_DummyReg(struct parse_state
*parseState
, GLint
*regNum
)
746 if (Parse_String(parseState
, "RC")) {
749 else if (Parse_String(parseState
, "HC")) {
753 RETURN_ERROR1("Invalid write-only register name");
761 * Parse a program local parameter register "p[##]"
764 Parse_ProgramParamReg(struct parse_state
*parseState
, GLint
*regNum
)
768 if (!Parse_String(parseState
, "p["))
769 RETURN_ERROR1("Expected p[");
771 if (!Parse_Token(parseState
, token
))
774 if (IsDigit(token
[0])) {
775 /* a numbered program parameter register */
776 GLint reg
= _mesa_atoi((const char *) token
);
777 if (reg
>= MAX_NV_FRAGMENT_PROGRAM_PARAMS
)
778 RETURN_ERROR1("Invalid constant program number");
785 if (!Parse_String(parseState
, "]"))
786 RETURN_ERROR1("Expected ]");
793 * Parse f[name] - fragment input register
796 Parse_FragReg(struct parse_state
*parseState
, GLint
*tempRegNum
)
802 if (!Parse_String(parseState
, "f["))
803 RETURN_ERROR1("Expected f[");
805 /* get <name> and look for match */
806 if (!Parse_Token(parseState
, token
)) {
809 for (j
= 0; InputRegisters
[j
]; j
++) {
810 if (_mesa_strcmp((const char *) token
, InputRegisters
[j
]) == 0) {
812 parseState
->inputsRead
|= (1 << j
);
816 if (!InputRegisters
[j
]) {
817 /* unknown input register label */
818 RETURN_ERROR2("Invalid register name", token
);
822 if (!Parse_String(parseState
, "]"))
823 RETURN_ERROR1("Expected ]");
830 Parse_OutputReg(struct parse_state
*parseState
, GLint
*outputRegNum
)
836 if (!Parse_String(parseState
, "o["))
837 RETURN_ERROR1("Expected o[");
839 /* Get output reg name */
840 if (!Parse_Token(parseState
, token
))
843 /* try to match an output register name */
844 for (j
= 0; OutputRegisters
[j
]; j
++) {
845 if (_mesa_strcmp((const char *) token
, OutputRegisters
[j
]) == 0) {
846 static GLuint bothColors
= (1 << FRAG_RESULT_COLR
) | (1 << FRAG_RESULT_COLH
);
848 parseState
->outputsWritten
|= (1 << j
);
849 if ((parseState
->outputsWritten
& bothColors
) == bothColors
) {
850 RETURN_ERROR1("Illegal to write to both o[COLR] and o[COLH]");
855 if (!OutputRegisters
[j
])
856 RETURN_ERROR1("Invalid output register name");
859 if (!Parse_String(parseState
, "]"))
860 RETURN_ERROR1("Expected ]");
867 Parse_MaskedDstReg(struct parse_state
*parseState
,
868 struct prog_dst_register
*dstReg
)
873 /* Dst reg can be R<n>, H<n>, o[n], RC or HC */
874 if (!Peek_Token(parseState
, token
))
877 if (_mesa_strcmp((const char *) token
, "RC") == 0 ||
878 _mesa_strcmp((const char *) token
, "HC") == 0) {
879 /* a write-only register */
880 dstReg
->File
= PROGRAM_WRITE_ONLY
;
881 if (!Parse_DummyReg(parseState
, &idx
))
885 else if (token
[0] == 'R' || token
[0] == 'H') {
886 /* a temporary register */
887 dstReg
->File
= PROGRAM_TEMPORARY
;
888 if (!Parse_TempReg(parseState
, &idx
))
892 else if (token
[0] == 'o') {
893 /* an output register */
894 dstReg
->File
= PROGRAM_OUTPUT
;
895 if (!Parse_OutputReg(parseState
, &idx
))
900 RETURN_ERROR1("Invalid destination register name");
903 /* Parse optional write mask */
904 if (Parse_String(parseState
, ".")) {
908 if (!Parse_Token(parseState
, token
)) /* get xyzw writemask */
911 dstReg
->WriteMask
= 0;
913 if (token
[k
] == 'x') {
914 dstReg
->WriteMask
|= WRITEMASK_X
;
917 if (token
[k
] == 'y') {
918 dstReg
->WriteMask
|= WRITEMASK_Y
;
921 if (token
[k
] == 'z') {
922 dstReg
->WriteMask
|= WRITEMASK_Z
;
925 if (token
[k
] == 'w') {
926 dstReg
->WriteMask
|= WRITEMASK_W
;
930 RETURN_ERROR1("Invalid writemask character");
935 dstReg
->WriteMask
= WRITEMASK_XYZW
;
938 /* optional condition code mask */
939 if (Parse_String(parseState
, "(")) {
940 /* ("EQ" | "GE" | "GT" | "LE" | "LT" | "NE" | "TR" | "FL".x|y|z|w) */
941 /* ("EQ" | "GE" | "GT" | "LE" | "LT" | "NE" | "TR" | "FL".[xyzw]) */
942 if (!Parse_CondCodeMask(parseState
, dstReg
))
945 if (!Parse_String(parseState
, ")")) /* consume ")" */
946 RETURN_ERROR1("Expected )");
951 /* no cond code mask */
952 dstReg
->CondMask
= COND_TR
;
953 dstReg
->CondSwizzle
= SWIZZLE_NOOP
;
960 * Parse a vector source (register, constant, etc):
961 * <vectorSrc> ::= <absVectorSrc>
963 * <absVectorSrc> ::= <negate> "|" <baseVectorSrc> "|"
966 Parse_VectorSrc(struct parse_state
*parseState
,
967 struct prog_src_register
*srcReg
)
974 * First, take care of +/- and absolute value stuff.
976 if (Parse_String(parseState
, "-"))
978 else if (Parse_String(parseState
, "+"))
981 if (Parse_String(parseState
, "|")) {
982 srcReg
->Abs
= GL_TRUE
;
983 srcReg
->NegateAbs
= (sign
< 0.0F
) ? GL_TRUE
: GL_FALSE
;
985 if (Parse_String(parseState
, "-"))
986 srcReg
->NegateBase
= NEGATE_XYZW
;
987 else if (Parse_String(parseState
, "+"))
988 srcReg
->NegateBase
= NEGATE_NONE
;
990 srcReg
->NegateBase
= NEGATE_NONE
;
993 srcReg
->Abs
= GL_FALSE
;
994 srcReg
->NegateAbs
= GL_FALSE
;
995 srcReg
->NegateBase
= (sign
< 0.0F
) ? NEGATE_XYZW
: NEGATE_NONE
;
998 /* This should be the real src vector/register name */
999 if (!Peek_Token(parseState
, token
))
1002 /* Src reg can be Rn, Hn, f[n], p[n], a named parameter, a scalar
1003 * literal or vector literal.
1005 if (token
[0] == 'R' || token
[0] == 'H') {
1006 srcReg
->File
= PROGRAM_TEMPORARY
;
1007 if (!Parse_TempReg(parseState
, &idx
))
1009 srcReg
->Index
= idx
;
1011 else if (token
[0] == 'f') {
1012 /* XXX this might be an identifier! */
1013 srcReg
->File
= PROGRAM_INPUT
;
1014 if (!Parse_FragReg(parseState
, &idx
))
1016 srcReg
->Index
= idx
;
1018 else if (token
[0] == 'p') {
1019 /* XXX this might be an identifier! */
1020 srcReg
->File
= PROGRAM_LOCAL_PARAM
;
1021 if (!Parse_ProgramParamReg(parseState
, &idx
))
1023 srcReg
->Index
= idx
;
1025 else if (IsLetter(token
[0])){
1028 if (!Parse_Identifier(parseState
, ident
))
1030 paramIndex
= _mesa_lookup_parameter_index(parseState
->parameters
,
1031 -1, (const char *) ident
);
1032 if (paramIndex
< 0) {
1033 RETURN_ERROR2("Undefined constant or parameter: ", ident
);
1035 srcReg
->File
= PROGRAM_NAMED_PARAM
;
1036 srcReg
->Index
= paramIndex
;
1038 else if (IsDigit(token
[0]) || token
[0] == '-' || token
[0] == '+' || token
[0] == '.'){
1039 /* literal scalar constant */
1041 GLuint paramIndex
, swizzle
;
1042 if (!Parse_ScalarConstant(parseState
, values
))
1044 paramIndex
= _mesa_add_unnamed_constant(parseState
->parameters
,
1045 values
, 4, &swizzle
);
1046 ASSERT(swizzle
== SWIZZLE_NOOP
);
1047 srcReg
->File
= PROGRAM_NAMED_PARAM
;
1048 srcReg
->Index
= paramIndex
;
1050 else if (token
[0] == '{'){
1051 /* literal vector constant */
1053 GLuint paramIndex
, swizzle
;
1054 (void) Parse_String(parseState
, "{");
1055 if (!Parse_VectorConstant(parseState
, values
))
1057 paramIndex
= _mesa_add_unnamed_constant(parseState
->parameters
,
1058 values
, 4, &swizzle
);
1059 ASSERT(swizzle
== SWIZZLE_NOOP
);
1060 srcReg
->File
= PROGRAM_NAMED_PARAM
;
1061 srcReg
->Index
= paramIndex
;
1064 RETURN_ERROR2("Invalid source register name", token
);
1067 /* init swizzle fields */
1068 srcReg
->Swizzle
= SWIZZLE_NOOP
;
1070 /* Look for optional swizzle suffix */
1071 if (Parse_String(parseState
, ".")) {
1074 if (!Parse_Token(parseState
, token
))
1077 if (!Parse_SwizzleSuffix(token
, swz
))
1078 RETURN_ERROR1("Invalid swizzle suffix");
1080 srcReg
->Swizzle
= MAKE_SWIZZLE4(swz
[0], swz
[1], swz
[2], swz
[3]);
1083 /* Finish absolute value */
1084 if (srcReg
->Abs
&& !Parse_String(parseState
, "|")) {
1085 RETURN_ERROR1("Expected |");
1093 Parse_ScalarSrcReg(struct parse_state
*parseState
,
1094 struct prog_src_register
*srcReg
)
1097 GLfloat sign
= 1.0F
;
1098 GLboolean needSuffix
= GL_TRUE
;
1102 * First, take care of +/- and absolute value stuff.
1104 if (Parse_String(parseState
, "-"))
1106 else if (Parse_String(parseState
, "+"))
1109 if (Parse_String(parseState
, "|")) {
1110 srcReg
->Abs
= GL_TRUE
;
1111 srcReg
->NegateAbs
= (sign
< 0.0F
) ? GL_TRUE
: GL_FALSE
;
1113 if (Parse_String(parseState
, "-"))
1114 srcReg
->NegateBase
= NEGATE_XYZW
;
1115 else if (Parse_String(parseState
, "+"))
1116 srcReg
->NegateBase
= NEGATE_NONE
;
1118 srcReg
->NegateBase
= NEGATE_NONE
;
1121 srcReg
->Abs
= GL_FALSE
;
1122 srcReg
->NegateAbs
= GL_FALSE
;
1123 srcReg
->NegateBase
= (sign
< 0.0F
) ? NEGATE_XYZW
: NEGATE_NONE
;
1126 if (!Peek_Token(parseState
, token
))
1129 /* Src reg can be R<n>, H<n> or a named fragment attrib */
1130 if (token
[0] == 'R' || token
[0] == 'H') {
1131 srcReg
->File
= PROGRAM_TEMPORARY
;
1132 if (!Parse_TempReg(parseState
, &idx
))
1134 srcReg
->Index
= idx
;
1136 else if (token
[0] == 'f') {
1137 srcReg
->File
= PROGRAM_INPUT
;
1138 if (!Parse_FragReg(parseState
, &idx
))
1140 srcReg
->Index
= idx
;
1142 else if (token
[0] == '{') {
1143 /* vector literal */
1145 GLuint paramIndex
, swizzle
;
1146 (void) Parse_String(parseState
, "{");
1147 if (!Parse_VectorConstant(parseState
, values
))
1149 paramIndex
= _mesa_add_unnamed_constant(parseState
->parameters
,
1150 values
, 4, &swizzle
);
1151 ASSERT(swizzle
== SWIZZLE_NOOP
);
1152 srcReg
->File
= PROGRAM_NAMED_PARAM
;
1153 srcReg
->Index
= paramIndex
;
1155 else if (IsLetter(token
[0])){
1156 /* named param/constant */
1159 if (!Parse_Identifier(parseState
, ident
))
1161 paramIndex
= _mesa_lookup_parameter_index(parseState
->parameters
,
1162 -1, (const char *) ident
);
1163 if (paramIndex
< 0) {
1164 RETURN_ERROR2("Undefined constant or parameter: ", ident
);
1166 srcReg
->File
= PROGRAM_NAMED_PARAM
;
1167 srcReg
->Index
= paramIndex
;
1169 else if (IsDigit(token
[0])) {
1170 /* scalar literal */
1172 GLuint paramIndex
, swizzle
;
1173 if (!Parse_ScalarConstant(parseState
, values
))
1175 paramIndex
= _mesa_add_unnamed_constant(parseState
->parameters
,
1176 values
, 4, &swizzle
);
1177 ASSERT(swizzle
== SWIZZLE_NOOP
);
1178 srcReg
->Index
= paramIndex
;
1179 srcReg
->File
= PROGRAM_NAMED_PARAM
;
1180 needSuffix
= GL_FALSE
;
1183 RETURN_ERROR2("Invalid scalar source argument", token
);
1186 srcReg
->Swizzle
= 0;
1188 /* parse .[xyzw] suffix */
1189 if (!Parse_String(parseState
, "."))
1190 RETURN_ERROR1("Expected .");
1192 if (!Parse_Token(parseState
, token
))
1195 if (token
[0] == 'x' && token
[1] == 0) {
1196 srcReg
->Swizzle
= 0;
1198 else if (token
[0] == 'y' && token
[1] == 0) {
1199 srcReg
->Swizzle
= 1;
1201 else if (token
[0] == 'z' && token
[1] == 0) {
1202 srcReg
->Swizzle
= 2;
1204 else if (token
[0] == 'w' && token
[1] == 0) {
1205 srcReg
->Swizzle
= 3;
1208 RETURN_ERROR1("Invalid scalar source suffix");
1212 /* Finish absolute value */
1213 if (srcReg
->Abs
&& !Parse_String(parseState
, "|")) {
1214 RETURN_ERROR1("Expected |");
1222 Parse_PrintInstruction(struct parse_state
*parseState
,
1223 struct prog_instruction
*inst
)
1230 /* The first argument is a literal string 'just like this' */
1231 if (!Parse_String(parseState
, "'"))
1232 RETURN_ERROR1("Expected '");
1234 str
= parseState
->pos
;
1235 for (len
= 0; str
[len
] != '\''; len
++) /* find closing quote */
1237 parseState
->pos
+= len
+ 1;
1238 msg
= (GLubyte
*) _mesa_malloc(len
+ 1);
1240 _mesa_memcpy(msg
, str
, len
);
1244 if (Parse_String(parseState
, ",")) {
1245 /* got an optional register to print */
1247 GetToken(parseState
, token
);
1248 if (token
[0] == 'o') {
1250 if (!Parse_OutputReg(parseState
, &idx
))
1252 inst
->SrcReg
[0].Index
= idx
;
1253 inst
->SrcReg
[0].File
= PROGRAM_OUTPUT
;
1257 if (!Parse_VectorSrc(parseState
, &inst
->SrcReg
[0]))
1262 inst
->SrcReg
[0].File
= PROGRAM_UNDEFINED
;
1265 inst
->SrcReg
[0].Swizzle
= SWIZZLE_NOOP
;
1266 inst
->SrcReg
[0].NegateBase
= NEGATE_NONE
;
1267 inst
->SrcReg
[0].Abs
= GL_FALSE
;
1268 inst
->SrcReg
[0].NegateAbs
= GL_FALSE
;
1275 Parse_InstructionSequence(struct parse_state
*parseState
,
1276 struct prog_instruction program
[])
1279 struct prog_instruction
*inst
= program
+ parseState
->numInst
;
1280 struct instruction_pattern instMatch
;
1283 /* Initialize the instruction */
1284 _mesa_init_instructions(inst
, 1);
1286 /* special instructions */
1287 if (Parse_String(parseState
, "DEFINE")) {
1289 GLfloat value
[7]; /* yes, 7 to be safe */
1290 if (!Parse_Identifier(parseState
, id
))
1292 /* XXX make sure id is not a reserved identifer, like R9 */
1293 if (!Parse_String(parseState
, "="))
1294 RETURN_ERROR1("Expected =");
1295 if (!Parse_VectorOrScalarConstant(parseState
, value
))
1297 if (!Parse_String(parseState
, ";"))
1298 RETURN_ERROR1("Expected ;");
1299 if (_mesa_lookup_parameter_index(parseState
->parameters
,
1300 -1, (const char *) id
) >= 0) {
1301 RETURN_ERROR2(id
, "already defined");
1303 _mesa_add_named_parameter(parseState
->parameters
,
1304 (const char *) id
, value
);
1306 else if (Parse_String(parseState
, "DECLARE")) {
1308 GLfloat value
[7] = {0, 0, 0, 0, 0, 0, 0}; /* yes, to be safe */
1309 if (!Parse_Identifier(parseState
, id
))
1311 /* XXX make sure id is not a reserved identifer, like R9 */
1312 if (Parse_String(parseState
, "=")) {
1313 if (!Parse_VectorOrScalarConstant(parseState
, value
))
1316 if (!Parse_String(parseState
, ";"))
1317 RETURN_ERROR1("Expected ;");
1318 if (_mesa_lookup_parameter_index(parseState
->parameters
,
1319 -1, (const char *) id
) >= 0) {
1320 RETURN_ERROR2(id
, "already declared");
1322 _mesa_add_named_parameter(parseState
->parameters
,
1323 (const char *) id
, value
);
1325 else if (Parse_String(parseState
, "END")) {
1326 inst
->Opcode
= OPCODE_END
;
1327 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
1328 assert(inst
->StringPos
>= 0);
1329 parseState
->numInst
++;
1330 if (Parse_Token(parseState
, token
)) {
1331 RETURN_ERROR1("Code after END opcode.");
1336 /* general/arithmetic instruction */
1339 if (!Parse_Token(parseState
, token
)) {
1340 RETURN_ERROR1("Missing END instruction.");
1343 /* try to find matching instuction */
1344 instMatch
= MatchInstruction(token
);
1345 if (instMatch
.opcode
>= MAX_OPCODE
) {
1346 /* bad instruction name */
1347 RETURN_ERROR2("Unexpected token: ", token
);
1350 inst
->Opcode
= instMatch
.opcode
;
1351 inst
->Precision
= instMatch
.suffixes
& (_R
| _H
| _X
);
1352 inst
->SaturateMode
= (instMatch
.suffixes
& (_S
))
1353 ? SATURATE_ZERO_ONE
: SATURATE_OFF
;
1354 inst
->CondUpdate
= (instMatch
.suffixes
& (_C
)) ? GL_TRUE
: GL_FALSE
;
1355 inst
->StringPos
= parseState
->curLine
- parseState
->start
;
1356 assert(inst
->StringPos
>= 0);
1359 * parse the input and output operands
1361 if (instMatch
.outputs
== OUTPUT_S
|| instMatch
.outputs
== OUTPUT_V
) {
1362 if (!Parse_MaskedDstReg(parseState
, &inst
->DstReg
))
1364 if (!Parse_String(parseState
, ","))
1365 RETURN_ERROR1("Expected ,");
1367 else if (instMatch
.outputs
== OUTPUT_NONE
) {
1368 if (instMatch
.opcode
== OPCODE_KIL_NV
) {
1369 /* This is a little weird, the cond code info is in
1370 * the dest register.
1372 if (!Parse_CondCodeMask(parseState
, &inst
->DstReg
))
1376 ASSERT(instMatch
.opcode
== OPCODE_PRINT
);
1380 if (instMatch
.inputs
== INPUT_1V
) {
1381 if (!Parse_VectorSrc(parseState
, &inst
->SrcReg
[0]))
1384 else if (instMatch
.inputs
== INPUT_2V
) {
1385 if (!Parse_VectorSrc(parseState
, &inst
->SrcReg
[0]))
1387 if (!Parse_String(parseState
, ","))
1388 RETURN_ERROR1("Expected ,");
1389 if (!Parse_VectorSrc(parseState
, &inst
->SrcReg
[1]))
1392 else if (instMatch
.inputs
== INPUT_3V
) {
1393 if (!Parse_VectorSrc(parseState
, &inst
->SrcReg
[0]))
1395 if (!Parse_String(parseState
, ","))
1396 RETURN_ERROR1("Expected ,");
1397 if (!Parse_VectorSrc(parseState
, &inst
->SrcReg
[1]))
1399 if (!Parse_String(parseState
, ","))
1400 RETURN_ERROR1("Expected ,");
1401 if (!Parse_VectorSrc(parseState
, &inst
->SrcReg
[2]))
1404 else if (instMatch
.inputs
== INPUT_1S
) {
1405 if (!Parse_ScalarSrcReg(parseState
, &inst
->SrcReg
[0]))
1408 else if (instMatch
.inputs
== INPUT_2S
) {
1409 if (!Parse_ScalarSrcReg(parseState
, &inst
->SrcReg
[0]))
1411 if (!Parse_String(parseState
, ","))
1412 RETURN_ERROR1("Expected ,");
1413 if (!Parse_ScalarSrcReg(parseState
, &inst
->SrcReg
[1]))
1416 else if (instMatch
.inputs
== INPUT_CC
) {
1419 else if (instMatch
.inputs
== INPUT_1V_T
) {
1421 if (!Parse_VectorSrc(parseState
, &inst
->SrcReg
[0]))
1423 if (!Parse_String(parseState
, ","))
1424 RETURN_ERROR1("Expected ,");
1425 if (!Parse_TextureImageId(parseState
, &unit
, &idx
))
1427 inst
->TexSrcUnit
= unit
;
1428 inst
->TexSrcTarget
= idx
;
1430 else if (instMatch
.inputs
== INPUT_3V_T
) {
1432 if (!Parse_VectorSrc(parseState
, &inst
->SrcReg
[0]))
1434 if (!Parse_String(parseState
, ","))
1435 RETURN_ERROR1("Expected ,");
1436 if (!Parse_VectorSrc(parseState
, &inst
->SrcReg
[1]))
1438 if (!Parse_String(parseState
, ","))
1439 RETURN_ERROR1("Expected ,");
1440 if (!Parse_VectorSrc(parseState
, &inst
->SrcReg
[2]))
1442 if (!Parse_String(parseState
, ","))
1443 RETURN_ERROR1("Expected ,");
1444 if (!Parse_TextureImageId(parseState
, &unit
, &idx
))
1446 inst
->TexSrcUnit
= unit
;
1447 inst
->TexSrcTarget
= idx
;
1449 else if (instMatch
.inputs
== INPUT_1V_S
) {
1450 if (!Parse_PrintInstruction(parseState
, inst
))
1454 /* end of statement semicolon */
1455 if (!Parse_String(parseState
, ";"))
1456 RETURN_ERROR1("Expected ;");
1458 parseState
->numInst
++;
1460 if (parseState
->numInst
>= MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS
)
1461 RETURN_ERROR1("Program too long");
1470 * Parse/compile the 'str' returning the compiled 'program'.
1471 * ctx->Program.ErrorPos will be -1 if successful. Otherwise, ErrorPos
1472 * indicates the position of the error in 'str'.
1475 _mesa_parse_nv_fragment_program(GLcontext
*ctx
, GLenum dstTarget
,
1476 const GLubyte
*str
, GLsizei len
,
1477 struct gl_fragment_program
*program
)
1479 struct parse_state parseState
;
1480 struct prog_instruction instBuffer
[MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS
];
1481 struct prog_instruction
*newInst
;
1483 GLubyte
*programString
;
1485 /* Make a null-terminated copy of the program string */
1486 programString
= (GLubyte
*) MALLOC(len
+ 1);
1487 if (!programString
) {
1488 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glLoadProgramNV");
1491 MEMCPY(programString
, str
, len
);
1492 programString
[len
] = 0;
1494 /* Get ready to parse */
1495 _mesa_bzero(&parseState
, sizeof(struct parse_state
));
1496 parseState
.ctx
= ctx
;
1497 parseState
.start
= programString
;
1498 parseState
.program
= program
;
1499 parseState
.numInst
= 0;
1500 parseState
.curLine
= programString
;
1501 parseState
.parameters
= _mesa_new_parameter_list();
1503 /* Reset error state */
1504 _mesa_set_program_error(ctx
, -1, NULL
);
1506 /* check the program header */
1507 if (_mesa_strncmp((const char *) programString
, "!!FP1.0", 7) == 0) {
1508 target
= GL_FRAGMENT_PROGRAM_NV
;
1509 parseState
.pos
= programString
+ 7;
1511 else if (_mesa_strncmp((const char *) programString
, "!!FCP1.0", 8) == 0) {
1512 /* fragment / register combiner program - not supported */
1513 _mesa_set_program_error(ctx
, 0, "Invalid fragment program header");
1514 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glLoadProgramNV(bad header)");
1518 /* invalid header */
1519 _mesa_set_program_error(ctx
, 0, "Invalid fragment program header");
1520 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glLoadProgramNV(bad header)");
1524 /* make sure target and header match */
1525 if (target
!= dstTarget
) {
1526 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1527 "glLoadProgramNV(target mismatch 0x%x != 0x%x)",
1532 if (Parse_InstructionSequence(&parseState
, instBuffer
)) {
1534 /* successful parse! */
1536 if (parseState
.outputsWritten
== 0) {
1537 /* must write at least one output! */
1538 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1539 "Invalid fragment program - no outputs written.");
1543 /* copy the compiled instructions */
1544 assert(parseState
.numInst
<= MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS
);
1545 newInst
= _mesa_alloc_instructions(parseState
.numInst
);
1547 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glLoadProgramNV");
1548 return; /* out of memory */
1550 _mesa_memcpy(newInst
, instBuffer
,
1551 parseState
.numInst
* sizeof(struct prog_instruction
));
1553 /* install the program */
1554 program
->Base
.Target
= target
;
1555 if (program
->Base
.String
) {
1556 FREE(program
->Base
.String
);
1558 program
->Base
.String
= programString
;
1559 program
->Base
.Format
= GL_PROGRAM_FORMAT_ASCII_ARB
;
1560 if (program
->Base
.Instructions
) {
1561 _mesa_free(program
->Base
.Instructions
);
1563 program
->Base
.Instructions
= newInst
;
1564 program
->Base
.NumInstructions
= parseState
.numInst
;
1565 program
->Base
.InputsRead
= parseState
.inputsRead
;
1566 program
->Base
.OutputsWritten
= parseState
.outputsWritten
;
1567 for (u
= 0; u
< ctx
->Const
.MaxTextureImageUnits
; u
++)
1568 program
->TexturesUsed
[u
] = parseState
.texturesUsed
[u
];
1570 /* save program parameters */
1571 program
->Base
.Parameters
= parseState
.parameters
;
1573 /* allocate registers for declared program parameters */
1575 _mesa_assign_program_registers(&(program
->SymbolTable
));
1579 _mesa_printf("--- glLoadProgramNV(%d) result ---\n", program
->Base
.Id
);
1580 _mesa_print_nv_fragment_program(program
);
1581 _mesa_printf("----------------------------------\n");
1586 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glLoadProgramNV");
1587 /* NOTE: _mesa_set_program_error would have been called already */
1593 PrintSrcReg(const struct gl_fragment_program
*program
,
1594 const struct prog_src_register
*src
)
1596 static const char comps
[5] = "xyzw";
1598 if (src
->NegateAbs
) {
1604 if (src
->NegateBase
) {
1607 if (src
->File
== PROGRAM_NAMED_PARAM
) {
1608 if (program
->Base
.Parameters
->Parameters
[src
->Index
].Type
1609 == PROGRAM_CONSTANT
) {
1611 v
= program
->Base
.Parameters
->ParameterValues
[src
->Index
];
1612 _mesa_printf("{%g, %g, %g, %g}", v
[0], v
[1], v
[2], v
[3]);
1615 ASSERT(program
->Base
.Parameters
->Parameters
[src
->Index
].Type
1616 == PROGRAM_NAMED_PARAM
);
1617 _mesa_printf("%s", program
->Base
.Parameters
->Parameters
[src
->Index
].Name
);
1620 else if (src
->File
== PROGRAM_OUTPUT
) {
1621 _mesa_printf("o[%s]", OutputRegisters
[src
->Index
]);
1623 else if (src
->File
== PROGRAM_INPUT
) {
1624 _mesa_printf("f[%s]", InputRegisters
[src
->Index
]);
1626 else if (src
->File
== PROGRAM_LOCAL_PARAM
) {
1627 _mesa_printf("p[%d]", src
->Index
);
1629 else if (src
->File
== PROGRAM_TEMPORARY
) {
1630 if (src
->Index
>= 32)
1631 _mesa_printf("H%d", src
->Index
);
1633 _mesa_printf("R%d", src
->Index
);
1635 else if (src
->File
== PROGRAM_WRITE_ONLY
) {
1636 _mesa_printf("%cC", "HR"[src
->Index
]);
1639 _mesa_problem(NULL
, "Invalid fragment register %d", src
->Index
);
1642 if (GET_SWZ(src
->Swizzle
, 0) == GET_SWZ(src
->Swizzle
, 1) &&
1643 GET_SWZ(src
->Swizzle
, 0) == GET_SWZ(src
->Swizzle
, 2) &&
1644 GET_SWZ(src
->Swizzle
, 0) == GET_SWZ(src
->Swizzle
, 3)) {
1645 _mesa_printf(".%c", comps
[GET_SWZ(src
->Swizzle
, 0)]);
1647 else if (src
->Swizzle
!= SWIZZLE_NOOP
) {
1648 _mesa_printf(".%c%c%c%c",
1649 comps
[GET_SWZ(src
->Swizzle
, 0)],
1650 comps
[GET_SWZ(src
->Swizzle
, 1)],
1651 comps
[GET_SWZ(src
->Swizzle
, 2)],
1652 comps
[GET_SWZ(src
->Swizzle
, 3)]);
1660 PrintTextureSrc(const struct prog_instruction
*inst
)
1662 _mesa_printf("TEX%d, ", inst
->TexSrcUnit
);
1663 switch (inst
->TexSrcTarget
) {
1664 case TEXTURE_1D_INDEX
:
1667 case TEXTURE_2D_INDEX
:
1670 case TEXTURE_3D_INDEX
:
1673 case TEXTURE_RECT_INDEX
:
1674 _mesa_printf("RECT");
1676 case TEXTURE_CUBE_INDEX
:
1677 _mesa_printf("CUBE");
1680 _mesa_problem(NULL
, "Invalid textue target in PrintTextureSrc");
1685 PrintCondCode(const struct prog_dst_register
*dst
)
1687 static const char *comps
= "xyzw";
1688 static const char *ccString
[] = {
1689 "??", "GT", "EQ", "LT", "UN", "GE", "LE", "NE", "TR", "FL", "??"
1692 _mesa_printf("%s", ccString
[dst
->CondMask
]);
1693 if (GET_SWZ(dst
->CondSwizzle
, 0) == GET_SWZ(dst
->CondSwizzle
, 1) &&
1694 GET_SWZ(dst
->CondSwizzle
, 0) == GET_SWZ(dst
->CondSwizzle
, 2) &&
1695 GET_SWZ(dst
->CondSwizzle
, 0) == GET_SWZ(dst
->CondSwizzle
, 3)) {
1696 _mesa_printf(".%c", comps
[GET_SWZ(dst
->CondSwizzle
, 0)]);
1698 else if (dst
->CondSwizzle
!= SWIZZLE_NOOP
) {
1699 _mesa_printf(".%c%c%c%c",
1700 comps
[GET_SWZ(dst
->CondSwizzle
, 0)],
1701 comps
[GET_SWZ(dst
->CondSwizzle
, 1)],
1702 comps
[GET_SWZ(dst
->CondSwizzle
, 2)],
1703 comps
[GET_SWZ(dst
->CondSwizzle
, 3)]);
1709 PrintDstReg(const struct prog_dst_register
*dst
)
1711 if (dst
->File
== PROGRAM_OUTPUT
) {
1712 _mesa_printf("o[%s]", OutputRegisters
[dst
->Index
]);
1714 else if (dst
->File
== PROGRAM_TEMPORARY
) {
1715 if (dst
->Index
>= 32)
1716 _mesa_printf("H%d", dst
->Index
);
1718 _mesa_printf("R%d", dst
->Index
);
1720 else if (dst
->File
== PROGRAM_LOCAL_PARAM
) {
1721 _mesa_printf("p[%d]", dst
->Index
);
1723 else if (dst
->File
== PROGRAM_WRITE_ONLY
) {
1724 _mesa_printf("%cC", "HR"[dst
->Index
]);
1727 _mesa_printf("???");
1730 if (dst
->WriteMask
!= 0 && dst
->WriteMask
!= WRITEMASK_XYZW
) {
1732 if (dst
->WriteMask
& WRITEMASK_X
)
1734 if (dst
->WriteMask
& WRITEMASK_Y
)
1736 if (dst
->WriteMask
& WRITEMASK_Z
)
1738 if (dst
->WriteMask
& WRITEMASK_W
)
1742 if (dst
->CondMask
!= COND_TR
||
1743 dst
->CondSwizzle
!= SWIZZLE_NOOP
) {
1752 * Print (unparse) the given vertex program. Just for debugging.
1755 _mesa_print_nv_fragment_program(const struct gl_fragment_program
*program
)
1757 const struct prog_instruction
*inst
;
1759 for (inst
= program
->Base
.Instructions
; inst
->Opcode
!= OPCODE_END
; inst
++) {
1761 for (i
= 0; Instructions
[i
].name
; i
++) {
1762 if (inst
->Opcode
== Instructions
[i
].opcode
) {
1763 /* print instruction name */
1764 _mesa_printf("%s", Instructions
[i
].name
);
1765 if (inst
->Precision
== FLOAT16
)
1767 else if (inst
->Precision
== FIXED12
)
1769 if (inst
->CondUpdate
)
1771 if (inst
->SaturateMode
== SATURATE_ZERO_ONE
)
1772 _mesa_printf("_SAT");
1775 if (Instructions
[i
].inputs
== INPUT_CC
) {
1776 PrintCondCode(&inst
->DstReg
);
1778 else if (Instructions
[i
].outputs
== OUTPUT_V
||
1779 Instructions
[i
].outputs
== OUTPUT_S
) {
1780 /* print dest register */
1781 PrintDstReg(&inst
->DstReg
);
1785 /* print source register(s) */
1786 if (Instructions
[i
].inputs
== INPUT_1V
||
1787 Instructions
[i
].inputs
== INPUT_1S
) {
1788 PrintSrcReg(program
, &inst
->SrcReg
[0]);
1790 else if (Instructions
[i
].inputs
== INPUT_2V
||
1791 Instructions
[i
].inputs
== INPUT_2S
) {
1792 PrintSrcReg(program
, &inst
->SrcReg
[0]);
1794 PrintSrcReg(program
, &inst
->SrcReg
[1]);
1796 else if (Instructions
[i
].inputs
== INPUT_3V
) {
1797 PrintSrcReg(program
, &inst
->SrcReg
[0]);
1799 PrintSrcReg(program
, &inst
->SrcReg
[1]);
1801 PrintSrcReg(program
, &inst
->SrcReg
[2]);
1803 else if (Instructions
[i
].inputs
== INPUT_1V_T
) {
1804 PrintSrcReg(program
, &inst
->SrcReg
[0]);
1806 PrintTextureSrc(inst
);
1808 else if (Instructions
[i
].inputs
== INPUT_3V_T
) {
1809 PrintSrcReg(program
, &inst
->SrcReg
[0]);
1811 PrintSrcReg(program
, &inst
->SrcReg
[1]);
1813 PrintSrcReg(program
, &inst
->SrcReg
[2]);
1815 PrintTextureSrc(inst
);
1817 _mesa_printf(";\n");
1821 if (!Instructions
[i
].name
) {
1822 _mesa_printf("Invalid opcode %d\n", inst
->Opcode
);
1825 _mesa_printf("END\n");
1830 _mesa_nv_fragment_input_register_name(GLuint i
)
1832 ASSERT(i
< MAX_NV_FRAGMENT_PROGRAM_INPUTS
);
1833 return InputRegisters
[i
];
1838 _mesa_nv_fragment_output_register_name(GLuint i
)
1840 ASSERT(i
< MAX_NV_FRAGMENT_PROGRAM_OUTPUTS
);
1841 return OutputRegisters
[i
];