-/* $Id: nvfragparse.c,v 1.6 2003/02/23 04:09:21 brianp Exp $ */
-
/*
* Mesa 3-D graphics library
- * Version: 5.1
+ * Version: 6.0
*
- * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
+ * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
-
/**
* \file nvfragparse.c
- * \brief NVIDIA fragment program parser.
+ * NVIDIA fragment program parser.
* \author Brian Paul
*/
+/*
+ * Regarding GL_NV_fragment_program:
+ *
+ * Portions of this software may use or implement intellectual
+ * property owned and licensed by NVIDIA Corporation. NVIDIA disclaims
+ * any and all warranties with respect to such intellectual property,
+ * including any use thereof or modifications thereto.
+ */
+
#include "glheader.h"
#include "context.h"
#include "hash.h"
#include "imports.h"
#include "macros.h"
-#include "mmath.h"
#include "mtypes.h"
#include "nvfragprog.h"
#include "nvfragparse.h"
#include "nvprogram.h"
-
-
-#define FRAG_ATTRIB_WPOS 0
-#define FRAG_ATTRIB_COL0 1
-#define FRAG_ATTRIB_COL1 2
-#define FRAG_ATTRIB_FOGC 3
-#define FRAG_ATTRIB_TEX0 4
-#define FRAG_ATTRIB_TEX1 5
-#define FRAG_ATTRIB_TEX2 6
-#define FRAG_ATTRIB_TEX3 7
-#define FRAG_ATTRIB_TEX4 8
-#define FRAG_ATTRIB_TEX5 9
-#define FRAG_ATTRIB_TEX6 10
-#define FRAG_ATTRIB_TEX7 11
+#include "program.h"
#define INPUT_1V 1
#define OUTPUT_S 21
#define OUTPUT_NONE 22
-/* Optional suffixes */
-#define _R 0x01 /* real */
-#define _H 0x02 /* half */
-#define _X 0x04 /* fixed */
-#define _C 0x08 /* set cond codes */
-#define _S 0x10 /* saturate */
+/* IRIX defines some of these */
+#undef _R
+#undef _H
+#undef _X
+#undef _C
+#undef _S
-#define SINGLE _R
-#define HALF _H
-#define FIXED _X
+/* Optional suffixes */
+#define _R FLOAT32 /* float */
+#define _H FLOAT16 /* half-float */
+#define _X FIXED12 /* fixed */
+#define _C 0x08 /* set cond codes */
+#define _S 0x10 /* saturate, clamp result to [0,1] */
struct instruction_pattern {
const char *name;
{ "UP4B", FP_OPCODE_UP4B, INPUT_1S, OUTPUT_V, _C | _S },
{ "UP4UB", FP_OPCODE_UP4UB, INPUT_1S, OUTPUT_V, _C | _S },
{ "X2D", FP_OPCODE_X2D, INPUT_3V, OUTPUT_V, _R | _H | _C | _S },
- { NULL, -1, 0, 0, 0 }
+ { NULL, (enum fp_opcode) -1, 0, 0, 0 }
};
-
+/*
+ * Information needed or computed during parsing.
+ * Remember, we can't modify the target program object until we've
+ * _successfully_ parsed the program text.
+ */
struct parse_state {
- const char *pos; /* current position */
+ GLcontext *ctx;
+ const GLubyte *start; /* start of program string */
+ const GLubyte *pos; /* current position */
+ const GLubyte *curLine;
struct fragment_program *program; /* current program */
+
+ struct program_parameter_list *parameters;
+
GLuint numInst; /* number of instructions parsed */
+ GLuint inputsRead; /* bitmask of input registers used */
+ GLuint outputsWritten; /* bitmask of 1 << FRAG_OUTPUT_* bits */
+ GLuint texturesUsed[MAX_TEXTURE_IMAGE_UNITS];
};
+/*
+ * Called whenever we find an error during parsing.
+ */
+static void
+record_error(struct parse_state *parseState, const char *msg, int lineNo)
+{
+#ifdef DEBUG
+ GLint line, column;
+ const GLubyte *lineStr;
+ lineStr = _mesa_find_line_column(parseState->start,
+ parseState->pos, &line, &column);
+ _mesa_debug(parseState->ctx,
+ "nvfragparse.c(%d): line %d, column %d:%s (%s)\n",
+ lineNo, line, column, (char *) lineStr, msg);
+ _mesa_free((void *) lineStr);
+#else
+ (void) lineNo;
+#endif
+
+ /* Check that no error was already recorded. Only record the first one. */
+ if (parseState->ctx->Program.ErrorString[0] == 0) {
+ _mesa_set_program_error(parseState->ctx,
+ parseState->pos - parseState->start,
+ msg);
+ }
+}
+
+
+#define RETURN_ERROR \
+do { \
+ record_error(parseState, "Unexpected end of input.", __LINE__); \
+ return GL_FALSE; \
+} while(0)
+
+#define RETURN_ERROR1(msg) \
+do { \
+ record_error(parseState, msg, __LINE__); \
+ return GL_FALSE; \
+} while(0)
+
+#define RETURN_ERROR2(msg1, msg2) \
+do { \
+ char err[1000]; \
+ _mesa_sprintf(err, "%s %s", msg1, msg2); \
+ record_error(parseState, err, __LINE__); \
+ return GL_FALSE; \
+} while(0)
+
+
+
+
/*
* Search a list of instruction structures for a match.
*/
static struct instruction_pattern
-MatchInstruction(const char *token)
+MatchInstruction(const GLubyte *token)
{
const struct instruction_pattern *inst;
struct instruction_pattern result;
for (inst = Instructions; inst->name; inst++) {
- if (_mesa_strncmp(token, inst->name, 3) == 0) {
+ if (_mesa_strncmp((const char *) token, inst->name, 3) == 0) {
/* matched! */
int i = 3;
result = *inst;
/**********************************************************************/
-static GLboolean IsLetter(char b)
+static GLboolean IsLetter(GLubyte b)
{
- return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || (b == '_');
+ return (b >= 'a' && b <= 'z') ||
+ (b >= 'A' && b <= 'Z') ||
+ (b == '_') ||
+ (b == '$');
}
-static GLboolean IsDigit(char b)
+static GLboolean IsDigit(GLubyte b)
{
return b >= '0' && b <= '9';
}
-static GLboolean IsWhitespace(char b)
+static GLboolean IsWhitespace(GLubyte b)
{
return b == ' ' || b == '\t' || b == '\n' || b == '\r';
}
* \return <= 0 we found an error, else, return number of characters parsed.
*/
static GLint
-GetToken(const char *str, char *token)
+GetToken(struct parse_state *parseState, GLubyte *token)
{
+ const GLubyte *str = parseState->pos;
GLint i = 0, j = 0;
token[0] = 0;
while (str[i] && (str[i] != '\n' && str[i] != '\r')) {
i++;
}
+ if (str[i] == '\n' || str[i] == '\r')
+ parseState->curLine = str + i + 1;
}
else {
/* skip whitespace */
+ if (str[i] == '\n' || str[i] == '\r')
+ parseState->curLine = str + i + 1;
i++;
}
}
return i;
}
- /* punctuation */
+ /* punctuation character */
if (str[i]) {
token[0] = str[i++];
token[1] = 0;
* Get next token from input stream and increment stream pointer past token.
*/
static GLboolean
-Parse_Token(struct parse_state *parseState, char *token)
+Parse_Token(struct parse_state *parseState, GLubyte *token)
{
GLint i;
- i = GetToken(parseState->pos, token);
+ i = GetToken(parseState, token);
if (i <= 0) {
parseState->pos += (-i);
return GL_FALSE;
* Get next token from input stream but don't increment stream pointer.
*/
static GLboolean
-Peek_Token(struct parse_state *parseState, char *token)
+Peek_Token(struct parse_state *parseState, GLubyte *token)
{
GLint i, len;
- i = GetToken(parseState->pos, token);
+ i = GetToken(parseState, token);
if (i <= 0) {
parseState->pos += (-i);
return GL_FALSE;
}
- len = _mesa_strlen(token);
+ len = _mesa_strlen((const char *) token);
parseState->pos += (i - len);
return GL_TRUE;
}
-/**
- * String equality test
- */
-static GLboolean
-StrEq(const char *a, const char *b)
-{
- GLint i;
- for (i = 0; a[i] && b[i] && a[i] == (char) b[i]; i++)
- ;
- if (a[i] == 0 && b[i] == 0)
- return GL_TRUE;
- else
- return GL_FALSE;
-}
-
-
-
/**********************************************************************/
-static const char *InputRegisters[] = {
+static const char *InputRegisters[MAX_NV_FRAGMENT_PROGRAM_INPUTS + 1] = {
"WPOS", "COL0", "COL1", "FOGC",
"TEX0", "TEX1", "TEX2", "TEX3", "TEX4", "TEX5", "TEX6", "TEX7", NULL
};
-static const char *OutputRegisters[] = {
- "COLR", "COLH", "TEX0", "TEX1", "TEX2", "TEX3", "DEPR", NULL
+static const char *OutputRegisters[MAX_NV_FRAGMENT_PROGRAM_OUTPUTS + 1] = {
+ "COLR", "COLH",
+ /* These are only allows for register combiners */
+ /*
+ "TEX0", "TEX1", "TEX2", "TEX3",
+ */
+ "DEPR", NULL
};
-#ifdef DEBUG
-
-#define RETURN_ERROR \
-do { \
- _mesa_printf("nvfragparse.c error at %d: parse error\n", __LINE__); \
- return GL_FALSE; \
-} while(0)
-
-#define RETURN_ERROR1(msg) \
-do { \
- _mesa_printf("nvfragparse.c error at %d: %s\n", __LINE__, msg); \
- return GL_FALSE; \
-} while(0)
-
-#define RETURN_ERROR2(msg1, msg2) \
-do { \
- _mesa_printf("nvfragparse.c error at %d: %s %s\n", __LINE__, msg1, msg2);\
- return GL_FALSE; \
-} while(0)
-
-#else
-
-#define RETURN_ERROR return GL_FALSE
-#define RETURN_ERROR1(msg1) return GL_FALSE
-#define RETURN_ERROR2(msg1, msg2) return GL_FALSE
-
-#endif
-
-
-static GLint
-TempRegisterNumber(GLuint r)
-{
- if (r >= FP_TEMP_REG_START && r <= FP_TEMP_REG_END)
- return r - FP_TEMP_REG_START;
- else
- return -1;
-}
-
-static GLint
-HalfTempRegisterNumber(GLuint r)
-{
- if (r >= FP_TEMP_REG_START + 32 && r <= FP_TEMP_REG_END)
- return r - FP_TEMP_REG_START - 32;
- else
- return -1;
-}
-
-static GLint
-InputRegisterNumber(GLuint r)
-{
- if (r >= FP_INPUT_REG_START && r <= FP_INPUT_REG_END)
- return r - FP_INPUT_REG_START;
- else
- return -1;
-}
-
-static GLint
-OutputRegisterNumber(GLuint r)
-{
- if (r >= FP_OUTPUT_REG_START && r <= FP_OUTPUT_REG_END)
- return r - FP_OUTPUT_REG_START;
- else
- return -1;
-}
-
-static GLint
-ProgramRegisterNumber(GLuint r)
-{
- if (r >= FP_PROG_REG_START && r <= FP_PROG_REG_END)
- return r - FP_PROG_REG_START;
- else
- return -1;
-}
-
-static GLint
-DummyRegisterNumber(GLuint r)
-{
- if (r >= FP_DUMMY_REG_START && r <= FP_DUMMY_REG_END)
- return r - FP_DUMMY_REG_START;
- else
- return -1;
-}
-
/**********************************************************************/
-
/**
* Try to match 'pattern' as the next token after any whitespace/comments.
*/
static GLboolean
Parse_String(struct parse_state *parseState, const char *pattern)
{
- const char *m;
+ const GLubyte *m;
GLint i;
/* skip whitespace and comments */
while (*parseState->pos && (*parseState->pos != '\n' && *parseState->pos != '\r')) {
parseState->pos += 1;
}
+ if (*parseState->pos == '\n' || *parseState->pos == '\r')
+ parseState->curLine = parseState->pos + 1;
}
else {
/* skip whitespace */
+ if (*parseState->pos == '\n' || *parseState->pos == '\r')
+ parseState->curLine = parseState->pos + 1;
parseState->pos += 1;
}
}
/* Try to match the pattern */
m = parseState->pos;
for (i = 0; pattern[i]; i++) {
- if (*m != pattern[i])
+ if (*m != (GLubyte) pattern[i])
return GL_FALSE;
m += 1;
}
static GLboolean
-Parse_Identifier(struct parse_state *parseState, char *ident)
+Parse_Identifier(struct parse_state *parseState, GLubyte *ident)
{
if (!Parse_Token(parseState, ident))
RETURN_ERROR;
/**
* Parse a floating point constant, or a defined symbol name.
* [+/-]N[.N[eN]]
+ * Output: number[0 .. 3] will get the value.
*/
static GLboolean
Parse_ScalarConstant(struct parse_state *parseState, GLfloat *number)
{
char *end = NULL;
- *number = (GLfloat) _mesa_strtod(parseState->pos, &end);
+ *number = (GLfloat) _mesa_strtod((const char *) parseState->pos, &end);
- if (end && end > parseState->pos) {
+ if (end && end > (char *) parseState->pos) {
/* got a number */
- parseState->pos = end;
+ parseState->pos = (GLubyte *) end;
+ number[1] = *number;
+ number[2] = *number;
+ number[3] = *number;
return GL_TRUE;
}
else {
/* should be an identifier */
- char ident[100];
+ GLubyte ident[100];
+ const GLfloat *constant;
if (!Parse_Identifier(parseState, ident))
RETURN_ERROR1("Expected an identifier");
- if (!_mesa_lookup_symbol(&(parseState->program->SymbolTable),
- ident, number)) {
+ constant = _mesa_lookup_parameter_value(parseState->parameters,
+ -1, (const char *) ident);
+ /* XXX Check that it's a constant and not a parameter */
+ if (!constant) {
RETURN_ERROR1("Undefined symbol");
}
- return GL_TRUE;
+ else {
+ COPY_4V(number, constant);
+ return GL_TRUE;
+ }
}
}
static GLboolean
Parse_VectorConstant(struct parse_state *parseState, GLfloat *vec)
{
- char token[100];
+ /* "{" was already consumed */
- if (!Parse_String(parseState, "{"))
- RETURN_ERROR1("Expected {");
+ ASSIGN_4V(vec, 0.0, 0.0, 0.0, 1.0);
if (!Parse_ScalarConstant(parseState, vec+0)) /* X */
return GL_FALSE;
- if (!Parse_Token(parseState, token)) /* , or } */
- return GL_FALSE;
-
- if (token[0] == '}') {
- vec[1] = vec[2] = vec[3] = vec[0];
+ if (Parse_String(parseState, "}")) {
return GL_TRUE;
}
- if (token[0] != ',')
+ if (!Parse_String(parseState, ","))
RETURN_ERROR1("Expected comma in vector constant");
if (!Parse_ScalarConstant(parseState, vec+1)) /* Y */
return GL_FALSE;
- if (!Parse_Token(parseState, token)) /* , or } */
- return GL_FALSE;
-
- if (token[0] == '}') {
- vec[2] = vec[3] = vec[1];
+ if (Parse_String(parseState, "}")) {
return GL_TRUE;
}
- if (token[0] != ',')
+ if (!Parse_String(parseState, ","))
RETURN_ERROR1("Expected comma in vector constant");
if (!Parse_ScalarConstant(parseState, vec+2)) /* Z */
return GL_FALSE;
- if (!Parse_Token(parseState, token)) /* , or } */
- return GL_FALSE;
-
- if (token[0] == '}') {
- vec[3] = vec[2];
+ if (Parse_String(parseState, "}")) {
return GL_TRUE;
}
- if (token[0] != ',')
+ if (!Parse_String(parseState, ","))
RETURN_ERROR1("Expected comma in vector constant");
if (!Parse_ScalarConstant(parseState, vec+3)) /* W */
static GLuint
Parse_VectorOrScalarConstant(struct parse_state *parseState, GLfloat *vec)
{
- char token[100];
- if (!Peek_Token(parseState, token))
- RETURN_ERROR;
- if (token[0] == '{') {
+ if (Parse_String(parseState, "{")) {
return Parse_VectorConstant(parseState, vec);
}
else {
*/
static GLboolean
Parse_TextureImageId(struct parse_state *parseState,
- GLuint *texUnit, GLuint *texTargetIndex)
+ GLubyte *texUnit, GLubyte *texTargetBit)
{
- char imageSrc[100];
+ GLubyte imageSrc[100];
GLint unit;
if (!Parse_Token(parseState, imageSrc))
imageSrc[2] != 'X') {
RETURN_ERROR1("Expected TEX# source");
}
- unit = _mesa_atoi(imageSrc + 3);
+ unit = _mesa_atoi((const char *) imageSrc + 3);
if ((unit < 0 || unit > MAX_TEXTURE_IMAGE_UNITS) ||
(unit == 0 && (imageSrc[3] != '0' || imageSrc[4] != 0))) {
RETURN_ERROR1("Invalied TEX# source index");
RETURN_ERROR1("Expected ,");
if (Parse_String(parseState, "1D")) {
- *texTargetIndex = TEXTURE_1D_INDEX;
+ *texTargetBit = TEXTURE_1D_BIT;
}
else if (Parse_String(parseState, "2D")) {
- *texTargetIndex = TEXTURE_2D_INDEX;
+ *texTargetBit = TEXTURE_2D_BIT;
}
else if (Parse_String(parseState, "3D")) {
- *texTargetIndex = TEXTURE_3D_INDEX;
+ *texTargetBit = TEXTURE_3D_BIT;
}
else if (Parse_String(parseState, "CUBE")) {
- *texTargetIndex = TEXTURE_CUBE_INDEX;
+ *texTargetBit = TEXTURE_CUBE_BIT;
}
else if (Parse_String(parseState, "RECT")) {
- *texTargetIndex = TEXTURE_RECT_INDEX;
+ *texTargetBit = TEXTURE_RECT_BIT;
}
else {
RETURN_ERROR1("Invalid texture target token");
}
/* update record of referenced texture units */
- parseState->program->TexturesUsed[*texUnit] |= (1 << *texTargetIndex);
+ parseState->texturesUsed[*texUnit] |= *texTargetBit;
+ if (_mesa_bitcount(parseState->texturesUsed[*texUnit]) > 1) {
+ RETURN_ERROR1("Only one texture target can be used per texture unit.");
+ }
return GL_TRUE;
}
/**
- * Parse a swizzle suffix like .x or .z or .wxyz or .xxyy etc and return
- * the swizzle indexes.
+ * Parse a scalar suffix like .x, .y, .z or .w or parse a swizzle suffix
+ * like .wxyz, .xxyy, etc and return the swizzle indexes.
*/
static GLboolean
-Parse_SwizzleSuffix(const char *token, GLuint swizzle[4])
+Parse_SwizzleSuffix(const GLubyte *token, GLuint swizzle[4])
{
if (token[1] == 0) {
/* single letter swizzle (scalar) */
Parse_CondCodeMask(struct parse_state *parseState,
struct fp_dst_register *dstReg)
{
- char token[100];
-
- if (!Parse_Token(parseState, token))
- RETURN_ERROR;
-
- if (StrEq(token, "EQ"))
+ if (Parse_String(parseState, "EQ"))
dstReg->CondMask = COND_EQ;
- else if (StrEq(token, "GE"))
+ else if (Parse_String(parseState, "GE"))
dstReg->CondMask = COND_GE;
- else if (StrEq(token, "GT"))
+ else if (Parse_String(parseState, "GT"))
dstReg->CondMask = COND_GT;
- else if (StrEq(token, "LE"))
+ else if (Parse_String(parseState, "LE"))
dstReg->CondMask = COND_LE;
- else if (StrEq(token, "LT"))
+ else if (Parse_String(parseState, "LT"))
dstReg->CondMask = COND_LT;
- else if (StrEq(token, "NE"))
+ else if (Parse_String(parseState, "NE"))
dstReg->CondMask = COND_NE;
- else if (StrEq(token, "TR"))
+ else if (Parse_String(parseState, "TR"))
dstReg->CondMask = COND_TR;
- else if (StrEq(token, "FL"))
+ else if (Parse_String(parseState, "FL"))
dstReg->CondMask = COND_FL;
else
RETURN_ERROR1("Invalid condition code mask");
/* look for optional .xyzw swizzle */
- if (!Peek_Token(parseState, token))
- RETURN_ERROR;
-
- if (token[0] == '.') {
- Parse_String(parseState, "."); /* consume '.' */
+ if (Parse_String(parseState, ".")) {
+ GLubyte token[100];
if (!Parse_Token(parseState, token)) /* get xyzw suffix */
RETURN_ERROR;
if (!Parse_SwizzleSuffix(token, dstReg->CondSwizzle))
- RETURN_ERROR1("Bad swizzle suffix");
+ RETURN_ERROR1("Invalid swizzle suffix");
}
return GL_TRUE;
static GLboolean
Parse_TempReg(struct parse_state *parseState, GLint *tempRegNum)
{
- char token[100];
+ GLubyte token[100];
/* Should be 'R##' or 'H##' */
if (!Parse_Token(parseState, token))
RETURN_ERROR1("Expected R## or H##");
if (IsDigit(token[1])) {
- GLint reg = _mesa_atoi((token + 1));
+ GLint reg = _mesa_atoi((const char *) (token + 1));
if (token[0] == 'H')
reg += 32;
if (reg >= MAX_NV_FRAGMENT_PROGRAM_TEMPS)
- RETURN_ERROR1("Bad temporary register name");
- *tempRegNum = FP_TEMP_REG_START + reg;
+ RETURN_ERROR1("Invalid temporary register name");
+ *tempRegNum = reg;
}
else {
- RETURN_ERROR1("Bad temporary register name");
+ RETURN_ERROR1("Invalid temporary register name");
}
return GL_TRUE;
}
+/**
+ * Parse a write-only dummy register: RC or HC.
+ */
static GLboolean
Parse_DummyReg(struct parse_state *parseState, GLint *regNum)
{
- char token[100];
-
- /* Should be 'RC' or 'HC' */
- if (!Parse_Token(parseState, token))
- RETURN_ERROR;
-
- if (_mesa_strcmp(token, "RC")) {
- *regNum = FP_DUMMY_REG_START;
+ if (Parse_String(parseState, "RC")) {
+ *regNum = 0;
}
- else if (_mesa_strcmp(token, "HC")) {
- *regNum = FP_DUMMY_REG_START + 1;
+ else if (Parse_String(parseState, "HC")) {
+ *regNum = 1;
}
else {
- RETURN_ERROR1("Bad write-only register name");
+ RETURN_ERROR1("Invalid write-only register name");
}
return GL_TRUE;
static GLboolean
Parse_ProgramParamReg(struct parse_state *parseState, GLint *regNum)
{
- char token[100];
+ GLubyte token[100];
- if (!Parse_String(parseState, "p"))
- RETURN_ERROR1("Expected p");
-
- if (!Parse_String(parseState, "["))
- RETURN_ERROR1("Expected [");
+ if (!Parse_String(parseState, "p["))
+ RETURN_ERROR1("Expected p[");
if (!Parse_Token(parseState, token))
RETURN_ERROR;
if (IsDigit(token[0])) {
/* a numbered program parameter register */
- GLint reg = _mesa_atoi(token);
+ GLint reg = _mesa_atoi((const char *) token);
if (reg >= MAX_NV_FRAGMENT_PROGRAM_PARAMS)
- RETURN_ERROR1("Bad constant program number");
- *regNum = FP_PROG_REG_START + reg;
+ RETURN_ERROR1("Invalid constant program number");
+ *regNum = reg;
}
else {
RETURN_ERROR;
* Parse f[name] - fragment input register
*/
static GLboolean
-Parse_AttribReg(struct parse_state *parseState, GLint *tempRegNum)
+Parse_FragReg(struct parse_state *parseState, GLint *tempRegNum)
{
- char token[100];
+ GLubyte token[100];
GLint j;
- /* Match 'f' */
- if (!Parse_String(parseState, "f"))
- RETURN_ERROR1("Expected f");
-
- /* Match '[' */
- if (!Parse_String(parseState, "["))
- RETURN_ERROR1("Expected [");
+ /* Match 'f[' */
+ if (!Parse_String(parseState, "f["))
+ RETURN_ERROR1("Expected f[");
/* get <name> and look for match */
if (!Parse_Token(parseState, token)) {
RETURN_ERROR;
}
for (j = 0; InputRegisters[j]; j++) {
- if (StrEq(token, InputRegisters[j])) {
- *tempRegNum = FP_INPUT_REG_START + j;
+ if (_mesa_strcmp((const char *) token, InputRegisters[j]) == 0) {
+ *tempRegNum = j;
+ parseState->inputsRead |= (1 << j);
break;
}
}
if (!InputRegisters[j]) {
/* unknown input register label */
- RETURN_ERROR2("Bad register name", token);
+ RETURN_ERROR2("Invalid register name", token);
}
/* Match '[' */
static GLboolean
Parse_OutputReg(struct parse_state *parseState, GLint *outputRegNum)
{
- char token[100];
+ GLubyte token[100];
GLint j;
/* Match "o[" */
/* try to match an output register name */
for (j = 0; OutputRegisters[j]; j++) {
- if (StrEq(token, OutputRegisters[j])) {
- *outputRegNum = FP_OUTPUT_REG_START + j;
+ if (_mesa_strcmp((const char *) token, OutputRegisters[j]) == 0) {
+ static GLuint bothColors = (1 << FRAG_OUTPUT_COLR) | (1 << FRAG_OUTPUT_COLH);
+ *outputRegNum = j;
+ parseState->outputsWritten |= (1 << j);
+ if ((parseState->outputsWritten & bothColors) == bothColors) {
+ RETURN_ERROR1("Illegal to write to both o[COLR] and o[COLH]");
+ }
break;
}
}
if (!OutputRegisters[j])
- RETURN_ERROR1("Unrecognized output register name");
+ RETURN_ERROR1("Invalid output register name");
/* Match ']' */
if (!Parse_String(parseState, "]"))
Parse_MaskedDstReg(struct parse_state *parseState,
struct fp_dst_register *dstReg)
{
- char token[100];
+ GLubyte token[100];
/* Dst reg can be R<n>, H<n>, o[n], RC or HC */
if (!Peek_Token(parseState, token))
RETURN_ERROR;
- if (_mesa_strcmp(token, "RC") == 0 ||
- _mesa_strcmp(token, "HC") == 0) {
+ if (_mesa_strcmp((const char *) token, "RC") == 0 ||
+ _mesa_strcmp((const char *) token, "HC") == 0) {
/* a write-only register */
- if (!Parse_DummyReg(parseState, &dstReg->Register))
+ dstReg->File = PROGRAM_WRITE_ONLY;
+ if (!Parse_DummyReg(parseState, &dstReg->Index))
RETURN_ERROR;
}
else if (token[0] == 'R' || token[0] == 'H') {
/* a temporary register */
- if (!Parse_TempReg(parseState, &dstReg->Register))
+ dstReg->File = PROGRAM_TEMPORARY;
+ if (!Parse_TempReg(parseState, &dstReg->Index))
RETURN_ERROR;
}
else if (token[0] == 'o') {
/* an output register */
- if (!Parse_OutputReg(parseState, &dstReg->Register))
+ dstReg->File = PROGRAM_OUTPUT;
+ if (!Parse_OutputReg(parseState, &dstReg->Index))
RETURN_ERROR;
}
else {
- RETURN_ERROR1("Bad destination register name");
+ RETURN_ERROR1("Invalid destination register name");
}
/* Parse optional write mask */
- if (!Peek_Token(parseState, token))
- RETURN_ERROR;
-
- if (token[0] == '.') {
+ if (Parse_String(parseState, ".")) {
/* got a mask */
GLint k = 0;
- if (!Parse_String(parseState, "."))
- RETURN_ERROR1("Expected .");
-
if (!Parse_Token(parseState, token)) /* get xyzw writemask */
RETURN_ERROR;
k++;
}
if (k == 0) {
- RETURN_ERROR1("Bad writemask character");
+ RETURN_ERROR1("Invalid writemask character");
}
- /* peek optional cc mask */
- if (!Peek_Token(parseState, token))
- RETURN_ERROR;
}
else {
dstReg->WriteMask[0] = GL_TRUE;
}
/* optional condition code mask */
- if (token[0] == '(') {
+ if (Parse_String(parseState, "(")) {
/* ("EQ" | "GE" | "GT" | "LE" | "LT" | "NE" | "TR" | "FL".x|y|z|w) */
/* ("EQ" | "GE" | "GT" | "LE" | "LT" | "NE" | "TR" | "FL".[xyzw]) */
- Parse_String(parseState, "(");
-
if (!Parse_CondCodeMask(parseState, dstReg))
RETURN_ERROR;
}
+/**
+ * Parse a vector source (register, constant, etc):
+ * <vectorSrc> ::= <absVectorSrc>
+ * | <baseVectorSrc>
+ * <absVectorSrc> ::= <negate> "|" <baseVectorSrc> "|"
+ */
static GLboolean
-Parse_SwizzleSrcReg(struct parse_state *parseState,
- struct fp_src_register *srcReg)
+Parse_VectorSrc(struct parse_state *parseState,
+ struct fp_src_register *srcReg)
{
- char token[100];
-
- /* XXX need to parse absolute value and another negation ***/
- srcReg->NegateBase = GL_FALSE;
- srcReg->Abs = GL_FALSE;
- srcReg->NegateAbs = GL_FALSE;
-
- /* check for '-' */
- if (!Peek_Token(parseState, token))
- RETURN_ERROR;
- if (token[0] == '-') {
- (void) Parse_String(parseState, "-");
- srcReg->NegateBase = GL_TRUE;
- if (!Peek_Token(parseState, token))
- RETURN_ERROR;
+ GLfloat sign = 1.0F;
+ GLubyte token[100];
+
+ /*
+ * First, take care of +/- and absolute value stuff.
+ */
+ if (Parse_String(parseState, "-"))
+ sign = -1.0F;
+ else if (Parse_String(parseState, "+"))
+ sign = +1.0F;
+
+ if (Parse_String(parseState, "|")) {
+ srcReg->Abs = GL_TRUE;
+ srcReg->NegateAbs = (sign < 0.0F) ? GL_TRUE : GL_FALSE;
+
+ if (Parse_String(parseState, "-"))
+ srcReg->NegateBase = GL_TRUE;
+ else if (Parse_String(parseState, "+"))
+ srcReg->NegateBase = GL_FALSE;
+ else
+ srcReg->NegateBase = GL_FALSE;
}
else {
- srcReg->NegateBase = GL_FALSE;
+ srcReg->Abs = GL_FALSE;
+ srcReg->NegateAbs = GL_FALSE;
+ srcReg->NegateBase = (sign < 0.0F) ? GL_TRUE : GL_FALSE;
}
- /* Src reg can be R<n>, H<n> or a named fragment attrib */
+ /* This should be the real src vector/register name */
+ if (!Peek_Token(parseState, token))
+ RETURN_ERROR;
+
+ /* Src reg can be Rn, Hn, f[n], p[n], a named parameter, a scalar
+ * literal or vector literal.
+ */
if (token[0] == 'R' || token[0] == 'H') {
- if (!Parse_TempReg(parseState, &srcReg->Register))
+ srcReg->File = PROGRAM_TEMPORARY;
+ if (!Parse_TempReg(parseState, &srcReg->Index))
RETURN_ERROR;
}
else if (token[0] == 'f') {
- if (!Parse_AttribReg(parseState, &srcReg->Register))
+ /* XXX this might be an identier! */
+ srcReg->File = PROGRAM_INPUT;
+ if (!Parse_FragReg(parseState, &srcReg->Index))
RETURN_ERROR;
}
else if (token[0] == 'p') {
- if (!Parse_ProgramParamReg(parseState, &srcReg->Register))
+ /* XXX this might be an identier! */
+ srcReg->File = PROGRAM_LOCAL_PARAM;
+ if (!Parse_ProgramParamReg(parseState, &srcReg->Index))
RETURN_ERROR;
}
+ else if (IsLetter(token[0])){
+ GLubyte ident[100];
+ GLint paramIndex;
+ if (!Parse_Identifier(parseState, ident))
+ RETURN_ERROR;
+ paramIndex = _mesa_lookup_parameter_index(parseState->parameters,
+ -1, (const char *) ident);
+ if (paramIndex < 0) {
+ RETURN_ERROR2("Undefined constant or parameter: ", ident);
+ }
+ srcReg->File = PROGRAM_NAMED_PARAM;
+ srcReg->Index = paramIndex;
+ }
+ else if (IsDigit(token[0]) || token[0] == '-' || token[0] == '+' || token[0] == '.'){
+ /* literal scalar constant */
+ GLfloat values[4];
+ GLuint paramIndex;
+ if (!Parse_ScalarConstant(parseState, values))
+ RETURN_ERROR;
+ paramIndex = _mesa_add_unnamed_constant(parseState->parameters, values);
+ srcReg->File = PROGRAM_NAMED_PARAM;
+ srcReg->Index = paramIndex;
+ }
+ else if (token[0] == '{'){
+ /* literal vector constant */
+ GLfloat values[4];
+ GLuint paramIndex;
+ (void) Parse_String(parseState, "{");
+ if (!Parse_VectorConstant(parseState, values))
+ RETURN_ERROR;
+ paramIndex = _mesa_add_unnamed_constant(parseState->parameters, values);
+ srcReg->File = PROGRAM_NAMED_PARAM;
+ srcReg->Index = paramIndex;
+ }
else {
- /* Also parse defined/declared constant or vector literal */
- RETURN_ERROR2("Bad source register name", token);
+ RETURN_ERROR2("Invalid source register name", token);
}
/* init swizzle fields */
srcReg->Swizzle[3] = 3;
/* Look for optional swizzle suffix */
- if (!Peek_Token(parseState, token))
- RETURN_ERROR;
- if (token[0] == '.') {
- (void) Parse_String(parseState, "."); /* consume . */
-
+ if (Parse_String(parseState, ".")) {
if (!Parse_Token(parseState, token))
RETURN_ERROR;
if (!Parse_SwizzleSuffix(token, srcReg->Swizzle))
- RETURN_ERROR1("Bad swizzle suffix");
+ RETURN_ERROR1("Invalid swizzle suffix");
+ }
+
+ /* Finish absolute value */
+ if (srcReg->Abs && !Parse_String(parseState, "|")) {
+ RETURN_ERROR1("Expected |");
}
return GL_TRUE;
Parse_ScalarSrcReg(struct parse_state *parseState,
struct fp_src_register *srcReg)
{
- char token[100];
-
- /* check for '-' */
- if (!Peek_Token(parseState, token))
- RETURN_ERROR;
- if (token[0] == '-') {
- srcReg->NegateBase = GL_TRUE;
- (void) Parse_String(parseState, "-"); /* consume '-' */
- if (!Peek_Token(parseState, token))
- RETURN_ERROR;
+ GLubyte token[100];
+ GLfloat sign = 1.0F;
+ GLboolean needSuffix = GL_TRUE;
+
+ /*
+ * First, take care of +/- and absolute value stuff.
+ */
+ if (Parse_String(parseState, "-"))
+ sign = -1.0F;
+ else if (Parse_String(parseState, "+"))
+ sign = +1.0F;
+
+ if (Parse_String(parseState, "|")) {
+ srcReg->Abs = GL_TRUE;
+ srcReg->NegateAbs = (sign < 0.0F) ? GL_TRUE : GL_FALSE;
+
+ if (Parse_String(parseState, "-"))
+ srcReg->NegateBase = GL_TRUE;
+ else if (Parse_String(parseState, "+"))
+ srcReg->NegateBase = GL_FALSE;
+ else
+ srcReg->NegateBase = GL_FALSE;
}
else {
- srcReg->NegateBase = GL_FALSE;
+ srcReg->Abs = GL_FALSE;
+ srcReg->NegateAbs = GL_FALSE;
+ srcReg->NegateBase = (sign < 0.0F) ? GL_TRUE : GL_FALSE;
}
+ if (!Peek_Token(parseState, token))
+ RETURN_ERROR;
+
/* Src reg can be R<n>, H<n> or a named fragment attrib */
if (token[0] == 'R' || token[0] == 'H') {
- if (!Parse_TempReg(parseState, &srcReg->Register))
+ srcReg->File = PROGRAM_TEMPORARY;
+ if (!Parse_TempReg(parseState, &srcReg->Index))
RETURN_ERROR;
}
else if (token[0] == 'f') {
- if (!Parse_AttribReg(parseState, &srcReg->Register))
+ srcReg->File = PROGRAM_INPUT;
+ if (!Parse_FragReg(parseState, &srcReg->Index))
+ RETURN_ERROR;
+ }
+ else if (token[0] == '{') {
+ /* vector literal */
+ GLfloat values[4];
+ GLuint paramIndex;
+ (void) Parse_String(parseState, "{");
+ if (!Parse_VectorConstant(parseState, values))
+ RETURN_ERROR;
+ paramIndex = _mesa_add_unnamed_constant(parseState->parameters, values);
+ srcReg->File = PROGRAM_NAMED_PARAM;
+ srcReg->Index = paramIndex;
+ }
+ else if (IsDigit(token[0])) {
+ /* scalar literal */
+ GLfloat values[4];
+ GLuint paramIndex;
+ if (!Parse_ScalarConstant(parseState, values))
RETURN_ERROR;
+ paramIndex = _mesa_add_unnamed_constant(parseState->parameters, values);
+ srcReg->Index = paramIndex;
+ srcReg->File = PROGRAM_NAMED_PARAM;
+ needSuffix = GL_FALSE;
}
else {
- RETURN_ERROR2("Bad source register name", token);
+ RETURN_ERROR2("Invalid scalar source argument", token);
}
- /* Look for .[xyzw] suffix */
- if (!Parse_String(parseState, "."))
- RETURN_ERROR1("Expected .");
+ if (needSuffix) {
+ /* parse .[xyzw] suffix */
+ if (!Parse_String(parseState, "."))
+ RETURN_ERROR1("Expected .");
- if (!Parse_Token(parseState, token))
- RETURN_ERROR;
+ if (!Parse_Token(parseState, token))
+ RETURN_ERROR;
- if (token[0] == 'x' && token[1] == 0) {
- srcReg->Swizzle[0] = 0;
- }
- else if (token[0] == 'y' && token[1] == 0) {
- srcReg->Swizzle[0] = 1;
- }
- else if (token[0] == 'z' && token[1] == 0) {
- srcReg->Swizzle[0] = 2;
- }
- else if (token[0] == 'w' && token[1] == 0) {
- srcReg->Swizzle[0] = 3;
+ if (token[0] == 'x' && token[1] == 0) {
+ srcReg->Swizzle[0] = 0;
+ }
+ else if (token[0] == 'y' && token[1] == 0) {
+ srcReg->Swizzle[0] = 1;
+ }
+ else if (token[0] == 'z' && token[1] == 0) {
+ srcReg->Swizzle[0] = 2;
+ }
+ else if (token[0] == 'w' && token[1] == 0) {
+ srcReg->Swizzle[0] = 3;
+ }
+ else {
+ RETURN_ERROR1("Invalid scalar source suffix");
+ }
}
else {
- RETURN_ERROR1("Bad scalar source suffix");
+ srcReg->Swizzle[0] = 0;
}
srcReg->Swizzle[1] = srcReg->Swizzle[2] = srcReg->Swizzle[3] = 0;
+ /* Finish absolute value */
+ if (srcReg->Abs && !Parse_String(parseState, "|")) {
+ RETURN_ERROR1("Expected |");
+ }
+
return GL_TRUE;
}
Parse_InstructionSequence(struct parse_state *parseState,
struct fp_instruction program[])
{
- char token[100];
-
while (1) {
struct fp_instruction *inst = program + parseState->numInst;
struct instruction_pattern instMatch;
+ GLubyte token[100];
/* Initialize the instruction */
- inst->SrcReg[0].Register = -1;
- inst->SrcReg[1].Register = -1;
- inst->SrcReg[2].Register = -1;
- inst->DstReg.Register = -1;
-
- /* get token */
- if (!Parse_Token(parseState, token)) {
- inst->Opcode = FP_OPCODE_END;
- printf("END OF PROGRAM %d\n", parseState->numInst);
- parseState->numInst++;
- break;
- }
+ inst->SrcReg[0].File = (enum register_file) -1;
+ inst->SrcReg[1].File = (enum register_file) -1;
+ inst->SrcReg[2].File = (enum register_file) -1;
+ inst->DstReg.File = (enum register_file) -1;
+ inst->DstReg.CondSwizzle[0] = 0;
+ inst->DstReg.CondSwizzle[1] = 1;
+ inst->DstReg.CondSwizzle[2] = 2;
+ inst->DstReg.CondSwizzle[3] = 3;
/* special instructions */
- if (StrEq(token, "DEFINE")) {
- char id[100];
+ if (Parse_String(parseState, "DEFINE")) {
+ GLubyte id[100];
GLfloat value[7]; /* yes, 7 to be safe */
if (!Parse_Identifier(parseState, id))
RETURN_ERROR;
+ /* XXX make sure id is not a reserved identifer, like R9 */
if (!Parse_String(parseState, "="))
RETURN_ERROR1("Expected =");
if (!Parse_VectorOrScalarConstant(parseState, value))
RETURN_ERROR;
if (!Parse_String(parseState, ";"))
RETURN_ERROR1("Expected ;");
- printf("Parsed DEFINE %s = %f %f %f %f\n", id, value[0], value[1],
- value[2], value[3]);
- _mesa_add_symbol(&(parseState->program->SymbolTable), id, Definition, value);
+ if (_mesa_lookup_parameter_index(parseState->parameters,
+ -1, (const char *) id) >= 0) {
+ RETURN_ERROR2(id, "already defined");
+ }
+ _mesa_add_named_parameter(parseState->parameters,
+ (const char *) id, value);
}
- else if (StrEq(token, "DECLARE")) {
- char id[100];
+ else if (Parse_String(parseState, "DECLARE")) {
+ GLubyte id[100];
GLfloat value[7] = {0, 0, 0, 0, 0, 0, 0}; /* yes, to be safe */
if (!Parse_Identifier(parseState, id))
RETURN_ERROR;
- if (!Peek_Token(parseState, token))
- RETURN_ERROR;
- if (token[0] == '=') {
- Parse_String(parseState, "=");
+ /* XXX make sure id is not a reserved identifer, like R9 */
+ if (Parse_String(parseState, "=")) {
if (!Parse_VectorOrScalarConstant(parseState, value))
RETURN_ERROR;
- printf("Parsed DECLARE %s = %f %f %f %f\n", id,
- value[0], value[1], value[2], value[3]);
- }
- else {
- printf("Parsed DECLARE %s\n", id);
}
if (!Parse_String(parseState, ";"))
RETURN_ERROR1("Expected ;");
- _mesa_add_symbol(&(parseState->program->SymbolTable), id, Declaration, value);
+ if (_mesa_lookup_parameter_index(parseState->parameters,
+ -1, (const char *) id) >= 0) {
+ RETURN_ERROR2(id, "already declared");
+ }
+ _mesa_add_named_parameter(parseState->parameters,
+ (const char *) id, value);
+ }
+ else if (Parse_String(parseState, "END")) {
+ inst->Opcode = FP_OPCODE_END;
+ inst->StringPos = parseState->curLine - parseState->start;
+ assert(inst->StringPos >= 0);
+ parseState->numInst++;
+ if (Parse_Token(parseState, token)) {
+ RETURN_ERROR1("Code after END opcode.");
+ }
+ break;
}
else {
- /* arithmetic instruction */
+ /* general/arithmetic instruction */
+
+ /* get token */
+ if (!Parse_Token(parseState, token)) {
+ RETURN_ERROR1("Missing END instruction.");
+ }
/* try to find matching instuction */
instMatch = MatchInstruction(token);
if (instMatch.opcode < 0) {
/* bad instruction name */
- printf("-------- errror\n");
RETURN_ERROR2("Unexpected token: ", token);
}
inst->Precision = instMatch.suffixes & (_R | _H | _X);
inst->Saturate = (instMatch.suffixes & (_S)) ? GL_TRUE : GL_FALSE;
inst->UpdateCondRegister = (instMatch.suffixes & (_C)) ? GL_TRUE : GL_FALSE;
+ inst->StringPos = parseState->curLine - parseState->start;
+ assert(inst->StringPos >= 0);
/*
* parse the input and output operands
}
if (instMatch.inputs == INPUT_1V) {
- if (!Parse_SwizzleSrcReg(parseState, &inst->SrcReg[0]))
+ if (!Parse_VectorSrc(parseState, &inst->SrcReg[0]))
RETURN_ERROR;
}
else if (instMatch.inputs == INPUT_2V) {
- if (!Parse_SwizzleSrcReg(parseState, &inst->SrcReg[0]))
+ if (!Parse_VectorSrc(parseState, &inst->SrcReg[0]))
RETURN_ERROR;
if (!Parse_String(parseState, ","))
RETURN_ERROR1("Expected ,");
- if (!Parse_SwizzleSrcReg(parseState, &inst->SrcReg[1]))
+ if (!Parse_VectorSrc(parseState, &inst->SrcReg[1]))
RETURN_ERROR;
}
else if (instMatch.inputs == INPUT_3V) {
- if (!Parse_SwizzleSrcReg(parseState, &inst->SrcReg[1]))
+ if (!Parse_VectorSrc(parseState, &inst->SrcReg[0]))
RETURN_ERROR;
if (!Parse_String(parseState, ","))
RETURN_ERROR1("Expected ,");
- if (!Parse_SwizzleSrcReg(parseState, &inst->SrcReg[1]))
+ if (!Parse_VectorSrc(parseState, &inst->SrcReg[1]))
RETURN_ERROR;
if (!Parse_String(parseState, ","))
RETURN_ERROR1("Expected ,");
- if (!Parse_SwizzleSrcReg(parseState, &inst->SrcReg[1]))
+ if (!Parse_VectorSrc(parseState, &inst->SrcReg[2]))
RETURN_ERROR;
}
else if (instMatch.inputs == INPUT_1S) {
- if (!Parse_ScalarSrcReg(parseState, &inst->SrcReg[1]))
+ if (!Parse_ScalarSrcReg(parseState, &inst->SrcReg[0]))
RETURN_ERROR;
}
else if (instMatch.inputs == INPUT_2S) {
/* XXX to-do */
}
else if (instMatch.inputs == INPUT_1V_T) {
- if (!Parse_SwizzleSrcReg(parseState, &inst->SrcReg[0]))
+ if (!Parse_VectorSrc(parseState, &inst->SrcReg[0]))
RETURN_ERROR;
if (!Parse_String(parseState, ","))
RETURN_ERROR1("Expected ,");
if (!Parse_TextureImageId(parseState, &inst->TexSrcUnit,
- &inst->TexSrcIndex))
+ &inst->TexSrcBit))
RETURN_ERROR;
}
else if (instMatch.inputs == INPUT_3V_T) {
- if (!Parse_SwizzleSrcReg(parseState, &inst->SrcReg[0]))
+ if (!Parse_VectorSrc(parseState, &inst->SrcReg[0]))
RETURN_ERROR;
if (!Parse_String(parseState, ","))
RETURN_ERROR1("Expected ,");
- if (!Parse_SwizzleSrcReg(parseState, &inst->SrcReg[1]))
+ if (!Parse_VectorSrc(parseState, &inst->SrcReg[1]))
RETURN_ERROR;
if (!Parse_String(parseState, ","))
RETURN_ERROR1("Expected ,");
- if (!Parse_SwizzleSrcReg(parseState, &inst->SrcReg[2]))
+ if (!Parse_VectorSrc(parseState, &inst->SrcReg[2]))
RETURN_ERROR;
if (!Parse_String(parseState, ","))
RETURN_ERROR1("Expected ,");
if (!Parse_TextureImageId(parseState, &inst->TexSrcUnit,
- &inst->TexSrcIndex))
+ &inst->TexSrcBit))
RETURN_ERROR;
}
}
-static const char *
-find_line_column(const char *string, const char *pos, GLint *line, GLint *col)
-{
- const char *lineStart = string;
- const char *p = string;
- char *s;
- int len;
-
- *line = 1;
-
- while (p != pos) {
- if (*p == '\n') {
- (*line)++;
- lineStart = p + 1;
- }
- p++;
- }
-
- *col = (pos - lineStart) + 1;
-
- /* return copy of this line */
- while (*p != 0 && *p != '\n')
- p++;
- len = p - lineStart;
- s = (char *) _mesa_malloc(len + 1);
- _mesa_memcpy(s, lineStart, len);
- s[len] = 0;
-
- return s;
-}
-
/**
* Parse/compile the 'str' returning the compiled 'program'.
programString[len] = 0;
/* Get ready to parse */
+ _mesa_bzero(&parseState, sizeof(struct parse_state));
+ parseState.ctx = ctx;
+ parseState.start = programString;
parseState.program = program;
parseState.numInst = 0;
- program->InputsRead = 0;
- program->OutputsWritten = 0;
+ parseState.curLine = programString;
+ parseState.parameters = _mesa_new_parameter_list();
+
+ /* Reset error state */
+ _mesa_set_program_error(ctx, -1, NULL);
/* check the program header */
if (_mesa_strncmp((const char *) programString, "!!FP1.0", 7) == 0) {
target = GL_FRAGMENT_PROGRAM_NV;
- parseState.pos = (char *) programString + 7;
+ parseState.pos = programString + 7;
+ }
+ else if (_mesa_strncmp((const char *) programString, "!!FCP1.0", 8) == 0) {
+ /* fragment / register combiner program - not supported */
+ _mesa_set_program_error(ctx, 0, "Invalid fragment program header");
+ _mesa_error(ctx, GL_INVALID_OPERATION, "glLoadProgramNV(bad header)");
+ return;
}
else {
/* invalid header */
}
if (Parse_InstructionSequence(&parseState, instBuffer)) {
- /* success! */
+ GLuint u;
+ /* successful parse! */
+
+ if (parseState.outputsWritten == 0) {
+ /* must write at least one output! */
+ _mesa_error(ctx, GL_INVALID_OPERATION,
+ "Invalid fragment program - no outputs written.");
+ return;
+ }
/* copy the compiled instructions */
assert(parseState.numInst <= MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS);
FREE(program->Base.String);
}
program->Base.String = programString;
+ program->Base.Format = GL_PROGRAM_FORMAT_ASCII_ARB;
if (program->Instructions) {
FREE(program->Instructions);
}
program->Instructions = newInst;
+ program->InputsRead = parseState.inputsRead;
+ program->OutputsWritten = parseState.outputsWritten;
+ for (u = 0; u < ctx->Const.MaxTextureImageUnits; u++)
+ program->TexturesUsed[u] = parseState.texturesUsed[u];
+
+ /* save program parameters */
+ program->Parameters = parseState.parameters;
/* allocate registers for declared program parameters */
+#if 00
_mesa_assign_program_registers(&(program->SymbolTable));
+#endif
#ifdef DEBUG
- _mesa_printf("--- glLoadProgramNV result ---\n");
+ _mesa_printf("--- glLoadProgramNV(%d) result ---\n", program->Base.Id);
_mesa_print_nv_fragment_program(program);
- _mesa_printf("------------------------------\n");
+ _mesa_printf("----------------------------------\n");
#endif
}
else {
/* Error! */
_mesa_error(ctx, GL_INVALID_OPERATION, "glLoadProgramNV");
- ctx->Program.ErrorPos = (GLubyte *) parseState.pos - programString;
-#ifdef DEBUG
- {
- GLint line, column;
- const char *lineStr;
- lineStr = find_line_column((const char *) programString,
- parseState.pos, &line, &column);
- _mesa_debug(ctx, "Parse error on line %d, column %d:%s\n",
- line, column, lineStr);
- _mesa_free(lineStr);
- }
-#endif
+ /* NOTE: _mesa_set_program_error would have been called already */
}
}
static void
-PrintSrcReg(const struct fp_src_register *src)
+PrintSrcReg(const struct fragment_program *program,
+ const struct fp_src_register *src)
{
static const char comps[5] = "xyzw";
- GLint r;
if (src->NegateAbs) {
_mesa_printf("-");
if (src->NegateBase) {
_mesa_printf("-");
}
- if ((r = OutputRegisterNumber(src->Register)) >= 0) {
- _mesa_printf("o[%s]", OutputRegisters[r]);
+ if (src->File == PROGRAM_NAMED_PARAM) {
+ if (program->Parameters->Parameters[src->Index].Type == CONSTANT) {
+ printf("{%g, %g, %g, %g}",
+ program->Parameters->Parameters[src->Index].Values[0],
+ program->Parameters->Parameters[src->Index].Values[1],
+ program->Parameters->Parameters[src->Index].Values[2],
+ program->Parameters->Parameters[src->Index].Values[3]);
+ }
+ else {
+ ASSERT(program->Parameters->Parameters[src->Index].Type
+ == NAMED_PARAMETER);
+ printf("%s", program->Parameters->Parameters[src->Index].Name);
+ }
}
- else if ((r = InputRegisterNumber(src->Register)) >= 0) {
- _mesa_printf("f[%s]", InputRegisters[r]);
+ else if (src->File == PROGRAM_OUTPUT) {
+ _mesa_printf("o[%s]", OutputRegisters[src->Index]);
}
- else if ((r = ProgramRegisterNumber(src->Register)) >= 0) {
- _mesa_printf("p[%d]", r);
+ else if (src->File == PROGRAM_INPUT) {
+ _mesa_printf("f[%s]", InputRegisters[src->Index]);
}
- else if ((r = HalfTempRegisterNumber(src->Register)) >= 0) {
- _mesa_printf("H%d", r);
+ else if (src->File == PROGRAM_LOCAL_PARAM) {
+ _mesa_printf("p[%d]", src->Index);
}
- else if ((r = TempRegisterNumber(src->Register)) >= 0) {
- _mesa_printf("R%d", r);
+ else if (src->File == PROGRAM_TEMPORARY) {
+ if (src->Index >= 32)
+ _mesa_printf("H%d", src->Index);
+ else
+ _mesa_printf("R%d", src->Index);
}
- else if ((r = DummyRegisterNumber(src->Register)) >= 0) {
- _mesa_printf("%cC", "HR"[r]);
+ else if (src->File == PROGRAM_WRITE_ONLY) {
+ _mesa_printf("%cC", "HR"[src->Index]);
}
else {
- _mesa_problem(NULL, "Bad fragment register %d", src->Register);
+ _mesa_problem(NULL, "Invalid fragment register %d", src->Index);
return;
}
if (src->Swizzle[0] == src->Swizzle[1] &&
PrintTextureSrc(const struct fp_instruction *inst)
{
_mesa_printf("TEX%d, ", inst->TexSrcUnit);
- switch (inst->TexSrcIndex) {
- case TEXTURE_1D_INDEX:
+ switch (inst->TexSrcBit) {
+ case TEXTURE_1D_BIT:
_mesa_printf("1D");
break;
- case TEXTURE_2D_INDEX:
+ case TEXTURE_2D_BIT:
_mesa_printf("2D");
break;
- case TEXTURE_3D_INDEX:
+ case TEXTURE_3D_BIT:
_mesa_printf("3D");
break;
- case TEXTURE_RECT_INDEX:
+ case TEXTURE_RECT_BIT:
_mesa_printf("RECT");
break;
- case TEXTURE_CUBE_INDEX:
+ case TEXTURE_CUBE_BIT:
_mesa_printf("CUBE");
break;
default:
- _mesa_problem(NULL, "Bad textue target in PrintTextureSrc");
+ _mesa_problem(NULL, "Invalid textue target in PrintTextureSrc");
}
}
{
GLint w = dst->WriteMask[0] + dst->WriteMask[1]
+ dst->WriteMask[2] + dst->WriteMask[3];
- GLint r;
- if ((r = OutputRegisterNumber(dst->Register)) >= 0) {
- _mesa_printf("o[%s]", OutputRegisters[r]);
- }
- else if ((r = HalfTempRegisterNumber(dst->Register)) >= 0) {
- _mesa_printf("H[%s]", InputRegisters[r]);
+ if (dst->File == PROGRAM_OUTPUT) {
+ _mesa_printf("o[%s]", OutputRegisters[dst->Index]);
}
- else if ((r = TempRegisterNumber(dst->Register)) >= 0) {
- _mesa_printf("R%d", r);
+ else if (dst->File == PROGRAM_TEMPORARY) {
+ if (dst->Index >= 32)
+ _mesa_printf("H%d", dst->Index);
+ else
+ _mesa_printf("R%d", dst->Index);
}
- else if ((r = ProgramRegisterNumber(dst->Register)) >= 0) {
- _mesa_printf("p[%d]", r);
+ else if (dst->File == PROGRAM_LOCAL_PARAM) {
+ _mesa_printf("p[%d]", dst->Index);
}
- else if ((r = DummyRegisterNumber(dst->Register)) >= 0) {
- _mesa_printf("%cC", "HR"[r]);
+ else if (dst->File == PROGRAM_WRITE_ONLY) {
+ _mesa_printf("%cC", "HR"[dst->Index]);
}
else {
_mesa_printf("???");
if (inst->Opcode == Instructions[i].opcode) {
/* print instruction name */
_mesa_printf("%s", Instructions[i].name);
- if (inst->Precision == HALF)
+ if (inst->Precision == FLOAT16)
_mesa_printf("H");
- else if (inst->Precision == FIXED)
+ else if (inst->Precision == FIXED12)
_mesa_printf("X");
if (inst->UpdateCondRegister)
_mesa_printf("C");
/* print source register(s) */
if (Instructions[i].inputs == INPUT_1V ||
Instructions[i].inputs == INPUT_1S) {
- PrintSrcReg(&inst->SrcReg[0]);
+ PrintSrcReg(program, &inst->SrcReg[0]);
}
else if (Instructions[i].inputs == INPUT_2V ||
Instructions[i].inputs == INPUT_2S) {
- PrintSrcReg(&inst->SrcReg[0]);
+ PrintSrcReg(program, &inst->SrcReg[0]);
_mesa_printf(", ");
- PrintSrcReg(&inst->SrcReg[1]);
+ PrintSrcReg(program, &inst->SrcReg[1]);
}
else if (Instructions[i].inputs == INPUT_3V) {
- PrintSrcReg(&inst->SrcReg[0]);
+ PrintSrcReg(program, &inst->SrcReg[0]);
_mesa_printf(", ");
- PrintSrcReg(&inst->SrcReg[1]);
+ PrintSrcReg(program, &inst->SrcReg[1]);
_mesa_printf(", ");
- PrintSrcReg(&inst->SrcReg[2]);
+ PrintSrcReg(program, &inst->SrcReg[2]);
}
else if (Instructions[i].inputs == INPUT_1V_T) {
- PrintSrcReg(&inst->SrcReg[0]);
+ PrintSrcReg(program, &inst->SrcReg[0]);
_mesa_printf(", ");
PrintTextureSrc(inst);
}
else if (Instructions[i].inputs == INPUT_3V_T) {
- PrintSrcReg(&inst->SrcReg[0]);
+ PrintSrcReg(program, &inst->SrcReg[0]);
_mesa_printf(", ");
- PrintSrcReg(&inst->SrcReg[1]);
+ PrintSrcReg(program, &inst->SrcReg[1]);
_mesa_printf(", ");
- PrintSrcReg(&inst->SrcReg[2]);
+ PrintSrcReg(program, &inst->SrcReg[2]);
_mesa_printf(", ");
PrintTextureSrc(inst);
}
}
}
if (!Instructions[i].name) {
- _mesa_printf("Bad opcode %d\n", inst->Opcode);
+ _mesa_printf("Invalid opcode %d\n", inst->Opcode);
}
}
+ _mesa_printf("END\n");
+}
+
+
+const char *
+_mesa_nv_fragment_input_register_name(GLuint i)
+{
+ ASSERT(i < MAX_NV_FRAGMENT_PROGRAM_INPUTS);
+ return InputRegisters[i];
+}
+
+
+const char *
+_mesa_nv_fragment_output_register_name(GLuint i)
+{
+ ASSERT(i < MAX_NV_FRAGMENT_PROGRAM_OUTPUTS);
+ return OutputRegisters[i];
}