/*
* Mesa 3-D graphics library
- * Version: 5.1
+ * Version: 6.0.1
*
- * 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"),
/**
* \file nvvertparse.c
- * \brief NVIDIA vertex program parser.
+ * NVIDIA vertex program parser.
* \author Brian Paul
*/
+/*
+ * Regarding GL_NV_vertex_program, GL_NV_vertex_program1_1:
+ *
+ * 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 "nvprogram.h"
#include "nvvertparse.h"
#include "nvvertprog.h"
+#include "program.h"
+
/**
* Current parsing state. This structure is passed among the parsing
GLcontext *ctx;
const GLubyte *start;
const GLubyte *pos;
+ const GLubyte *curLine;
GLboolean isStateProgram;
GLboolean isPositionInvariant;
GLboolean isVersion1_1;
GLuint inputsRead;
GLuint outputsWritten;
- GLuint progRegsWritten;
+ GLboolean anyProgRegsWritten;
GLuint numInst; /* number of instructions parsed */
};
* \return <= 0 we found an error, else, return number of characters parsed.
*/
static GLint
-GetToken(const GLubyte *str, GLubyte *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;
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;
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;
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;
}
}
/**********************************************************************/
-static const char *InputRegisters[] = {
+static const char *InputRegisters[MAX_NV_VERTEX_PROGRAM_INPUTS + 1] = {
"OPOS", "WGHT", "NRML", "COL0", "COL1", "FOGC", "6", "7",
"TEX0", "TEX1", "TEX2", "TEX3", "TEX4", "TEX5", "TEX6", "TEX7", NULL
};
-static const char *OutputRegisters[] = {
+static const char *OutputRegisters[MAX_NV_VERTEX_PROGRAM_OUTPUTS + 1] = {
"HPOS", "COL0", "COL1", "BFC0", "BFC1", "FOGC", "PSIZ",
"TEX0", "TEX1", "TEX2", "TEX3", "TEX4", "TEX5", "TEX6", "TEX7", NULL
};
+/* NOTE: the order here must match opcodes in nvvertprog.h */
static const char *Opcodes[] = {
"MOV", "LIT", "RCP", "RSQ", "EXP", "LOG", "MUL", "ADD", "DP3", "DP4",
"DST", "MIN", "MAX", "SLT", "SGE", "MAD", "ARL", "DPH", "RCC", "SUB",
- "ABS", "END", NULL
+ "ABS", "END",
+ /* GL_ARB_vertex_program */
+ "FLR", "FRC", "EX2", "LG2", "POW", "XPD", "SWZ",
+ NULL
};
-static GLuint
-IsProgRegister(GLuint r)
-{
- return (GLuint) (r >= VP_PROG_REG_START && r <= VP_PROG_REG_END);
-}
-
-static GLuint
-IsInputRegister(GLuint r)
-{
- return (GLuint) (r >= VP_INPUT_REG_START && r <= VP_INPUT_REG_END);
-}
-
-static GLuint
-IsOutputRegister(GLuint r)
-{
- return (GLuint) (r >= VP_OUTPUT_REG_START && r <= VP_OUTPUT_REG_END);
-}
-
-
/**
* Parse a temporary register: Rnn
*/
if (IsDigit(token[1])) {
GLint reg = _mesa_atoi((char *) (token + 1));
- if (reg >= VP_NUM_TEMP_REGS)
+ if (reg >= MAX_NV_VERTEX_PROGRAM_TEMPS)
RETURN_ERROR1("Bad temporary register name");
- *tempRegNum = VP_TEMP_REG_START + reg;
+ *tempRegNum = reg;
}
else {
RETURN_ERROR1("Bad temporary register name");
if (IsDigit(token[0])) {
/* a numbered program parameter register */
GLint reg = _mesa_atoi((char *) token);
- if (reg >= VP_NUM_PROG_REGS)
- RETURN_ERROR1("Bad constant program number");
- *regNum = VP_PROG_REG_START + reg;
+ if (reg >= MAX_NV_VERTEX_PROGRAM_PARAMS)
+ RETURN_ERROR1("Bad program parameter number");
+ *regNum = reg;
}
else {
RETURN_ERROR;
GLint reg;
(void) Parse_Token(parseState, token);
reg = _mesa_atoi((char *) token);
- if (reg >= VP_NUM_PROG_REGS)
- RETURN_ERROR1("Bad constant program number");
- srcReg->Register = VP_PROG_REG_START + reg;
+ if (reg >= MAX_NV_VERTEX_PROGRAM_PARAMS)
+ RETURN_ERROR1("Bad program parameter number");
+ srcReg->File = PROGRAM_ENV_PARAM;
+ srcReg->Index = reg;
}
else if (_mesa_strcmp((const char *) token, "A0") == 0) {
/* address register "A0.x" */
RETURN_ERROR;
srcReg->RelAddr = GL_TRUE;
- srcReg->Register = 0;
-
+ srcReg->File = PROGRAM_ENV_PARAM;
/* Look for +/-N offset */
if (!Peek_Token(parseState, token))
RETURN_ERROR;
if (sign == '-') {
if (k > 64)
RETURN_ERROR1("Bad address offset");
- srcReg->Register = -k;
+ srcReg->Index = -k;
}
else {
if (k > 63)
RETURN_ERROR1("Bad address offset");
- srcReg->Register = k;
+ srcReg->Index = k;
}
}
else {
if (IsDigit(token[0])) {
GLint reg = _mesa_atoi((char *) token);
- if (reg >= VP_NUM_INPUT_REGS)
+ if (reg >= MAX_NV_VERTEX_PROGRAM_INPUTS)
RETURN_ERROR1("Bad vertex attribute register name");
- *tempRegNum = VP_INPUT_REG_START + reg;
+ *tempRegNum = reg;
}
else {
for (j = 0; InputRegisters[j]; j++) {
if (_mesa_strcmp((const char *) token, InputRegisters[j]) == 0) {
- *tempRegNum = VP_INPUT_REG_START + j;
+ *tempRegNum = j;
break;
}
}
/* try to match an output register name */
for (j = start; OutputRegisters[j]; j++) {
if (_mesa_strcmp((const char *) token, OutputRegisters[j]) == 0) {
- *outputRegNum = VP_OUTPUT_REG_START + j;
+ *outputRegNum = j;
break;
}
}
if (token[0] == 'R') {
/* a temporary register */
- if (!Parse_TempReg(parseState, &dstReg->Register))
+ dstReg->File = PROGRAM_TEMPORARY;
+ if (!Parse_TempReg(parseState, &dstReg->Index))
RETURN_ERROR;
}
else if (!parseState->isStateProgram && 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 if (parseState->isStateProgram && token[0] == 'c') {
+ else if (parseState->isStateProgram && token[0] == 'c' &&
+ parseState->isStateProgram) {
/* absolute program parameter register */
- if (!Parse_AbsParamReg(parseState, &dstReg->Register))
+ /* Only valid for vertex state programs */
+ dstReg->File = PROGRAM_ENV_PARAM;
+ if (!Parse_AbsParamReg(parseState, &dstReg->Index))
RETURN_ERROR;
}
else {
/* Src reg can be R<n>, c[n], c[n +/- offset], or a named vertex attrib */
if (token[0] == 'R') {
- if (!Parse_TempReg(parseState, &srcReg->Register))
+ srcReg->File = PROGRAM_TEMPORARY;
+ if (!Parse_TempReg(parseState, &srcReg->Index))
RETURN_ERROR;
}
else if (token[0] == 'c') {
RETURN_ERROR;
}
else if (token[0] == 'v') {
- if (!Parse_AttribReg(parseState, &srcReg->Register))
+ srcReg->File = PROGRAM_INPUT;
+ if (!Parse_AttribReg(parseState, &srcReg->Index))
RETURN_ERROR;
}
else {
/* Src reg can be R<n>, c[n], c[n +/- offset], or a named vertex attrib */
if (token[0] == 'R') {
- if (!Parse_TempReg(parseState, &srcReg->Register))
+ srcReg->File = PROGRAM_TEMPORARY;
+ if (!Parse_TempReg(parseState, &srcReg->Index))
RETURN_ERROR;
}
else if (token[0] == 'c') {
RETURN_ERROR;
}
else if (token[0] == 'v') {
- if (!Parse_AttribReg(parseState, &srcReg->Register))
+ srcReg->File = PROGRAM_INPUT;
+ if (!Parse_AttribReg(parseState, &srcReg->Index))
RETURN_ERROR;
}
else {
RETURN_ERROR1("ABS illegal for vertex program 1.0");
inst->Opcode = opcode;
+ inst->StringPos = parseState->curLine - parseState->start;
/* dest reg */
if (!Parse_MaskedDstReg(parseState, &inst->DstReg))
RETURN_ERROR1("SUB illegal for vertex program 1.0");
inst->Opcode = opcode;
+ inst->StringPos = parseState->curLine - parseState->start;
/* dest reg */
if (!Parse_MaskedDstReg(parseState, &inst->DstReg))
RETURN_ERROR;
/* make sure we don't reference more than one program parameter register */
- if (IsProgRegister(inst->SrcReg[0].Register) &&
- IsProgRegister(inst->SrcReg[1].Register) &&
- inst->SrcReg[0].Register != inst->SrcReg[1].Register)
+ if (inst->SrcReg[0].File == PROGRAM_ENV_PARAM &&
+ inst->SrcReg[1].File == PROGRAM_ENV_PARAM &&
+ inst->SrcReg[0].Index != inst->SrcReg[1].Index)
RETURN_ERROR1("Can't reference two program parameter registers");
/* make sure we don't reference more than one vertex attribute register */
- if (IsInputRegister(inst->SrcReg[0].Register) &&
- IsInputRegister(inst->SrcReg[1].Register) &&
- inst->SrcReg[0].Register != inst->SrcReg[1].Register)
+ if (inst->SrcReg[0].File == PROGRAM_INPUT &&
+ inst->SrcReg[1].File == PROGRAM_INPUT &&
+ inst->SrcReg[0].Index != inst->SrcReg[1].Index)
RETURN_ERROR1("Can't reference two vertex attribute registers");
return GL_TRUE;
struct vp_instruction *inst, enum vp_opcode opcode)
{
inst->Opcode = opcode;
+ inst->StringPos = parseState->curLine - parseState->start;
/* dest reg */
if (!Parse_MaskedDstReg(parseState, &inst->DstReg))
RETURN_ERROR;
/* make sure we don't reference more than one program parameter register */
- if ((IsProgRegister(inst->SrcReg[0].Register) &&
- IsProgRegister(inst->SrcReg[1].Register) &&
- inst->SrcReg[0].Register != inst->SrcReg[1].Register) ||
- (IsProgRegister(inst->SrcReg[0].Register) &&
- IsProgRegister(inst->SrcReg[2].Register) &&
- inst->SrcReg[0].Register != inst->SrcReg[2].Register) ||
- (IsProgRegister(inst->SrcReg[1].Register) &&
- IsProgRegister(inst->SrcReg[2].Register) &&
- inst->SrcReg[1].Register != inst->SrcReg[2].Register))
+ if ((inst->SrcReg[0].File == PROGRAM_ENV_PARAM &&
+ inst->SrcReg[1].File == PROGRAM_ENV_PARAM &&
+ inst->SrcReg[0].Index != inst->SrcReg[1].Index) ||
+ (inst->SrcReg[0].File == PROGRAM_ENV_PARAM &&
+ inst->SrcReg[2].File == PROGRAM_ENV_PARAM &&
+ inst->SrcReg[0].Index != inst->SrcReg[2].Index) ||
+ (inst->SrcReg[1].File == PROGRAM_ENV_PARAM &&
+ inst->SrcReg[2].File == PROGRAM_ENV_PARAM &&
+ inst->SrcReg[1].Index != inst->SrcReg[2].Index))
RETURN_ERROR1("Can only reference one program register");
/* make sure we don't reference more than one vertex attribute register */
- if ((IsInputRegister(inst->SrcReg[0].Register) &&
- IsInputRegister(inst->SrcReg[1].Register) &&
- inst->SrcReg[0].Register != inst->SrcReg[1].Register) ||
- (IsInputRegister(inst->SrcReg[0].Register) &&
- IsInputRegister(inst->SrcReg[2].Register) &&
- inst->SrcReg[0].Register != inst->SrcReg[2].Register) ||
- (IsInputRegister(inst->SrcReg[1].Register) &&
- IsInputRegister(inst->SrcReg[2].Register) &&
- inst->SrcReg[1].Register != inst->SrcReg[2].Register))
+ if ((inst->SrcReg[0].File == PROGRAM_INPUT &&
+ inst->SrcReg[1].File == PROGRAM_INPUT &&
+ inst->SrcReg[0].Index != inst->SrcReg[1].Index) ||
+ (inst->SrcReg[0].File == PROGRAM_INPUT &&
+ inst->SrcReg[2].File == PROGRAM_INPUT &&
+ inst->SrcReg[0].Index != inst->SrcReg[2].Index) ||
+ (inst->SrcReg[1].File == PROGRAM_INPUT &&
+ inst->SrcReg[2].File == PROGRAM_INPUT &&
+ inst->SrcReg[1].Index != inst->SrcReg[2].Index))
RETURN_ERROR1("Can only reference one input register");
return GL_TRUE;
RETURN_ERROR1("RCC illegal for vertex program 1.0");
inst->Opcode = opcode;
+ inst->StringPos = parseState->curLine - parseState->start;
/* dest reg */
if (!Parse_MaskedDstReg(parseState, &inst->DstReg))
Parse_AddressInstruction(struct parse_state *parseState, struct vp_instruction *inst)
{
inst->Opcode = VP_OPCODE_ARL;
+ inst->StringPos = parseState->curLine - parseState->start;
/* dest A0 reg */
if (!Parse_AddrReg(parseState))
GLubyte token[100];
inst->Opcode = VP_OPCODE_END;
+ inst->StringPos = parseState->curLine - parseState->start;
/* this should fail! */
if (Parse_Token(parseState, token))
struct vp_instruction *inst = program + parseState->numInst;
/* Initialize the instruction */
- inst->SrcReg[0].Register = -1;
- inst->SrcReg[1].Register = -1;
- inst->SrcReg[2].Register = -1;
- inst->DstReg.Register = -1;
+ 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;
if (Parse_String(parseState, "MOV")) {
if (!Parse_UnaryOpInstruction(parseState, inst, VP_OPCODE_MOV))
}
/* examine input/output registers */
- {
- const GLint srcReg0 = inst->SrcReg[0].Register;
- const GLint srcReg1 = inst->SrcReg[1].Register;
- const GLint srcReg2 = inst->SrcReg[2].Register;
- const GLint dstReg = inst->DstReg.Register;
-
- if (IsOutputRegister(dstReg))
- parseState->outputsWritten |= (1 << (dstReg - VP_OUTPUT_REG_START));
- else if (IsProgRegister(dstReg))
- parseState->progRegsWritten |= (1 << (dstReg - VP_PROG_REG_START));
-
- if (IsInputRegister(srcReg0) && !inst->SrcReg[0].RelAddr)
- parseState->inputsRead |= (1 << (srcReg0 - VP_INPUT_REG_START));
-
- if (IsInputRegister(srcReg1) && !inst->SrcReg[1].RelAddr)
- parseState->inputsRead |= (1 << (srcReg1 - VP_INPUT_REG_START));
-
- if (IsInputRegister(srcReg2) && !inst->SrcReg[2].RelAddr)
- parseState->inputsRead |= (1 << (srcReg2 - VP_INPUT_REG_START));
- }
+ if (inst->DstReg.File == PROGRAM_OUTPUT)
+ parseState->outputsWritten |= (1 << inst->DstReg.Index);
+ else if (inst->DstReg.File == PROGRAM_ENV_PARAM)
+ parseState->anyProgRegsWritten = GL_TRUE;
+
+ if (inst->SrcReg[0].File == PROGRAM_INPUT)
+ parseState->inputsRead |= (1 << inst->SrcReg[0].Index);
+ if (inst->SrcReg[1].File == PROGRAM_INPUT)
+ parseState->inputsRead |= (1 << inst->SrcReg[1].Index);
+ if (inst->SrcReg[2].File == PROGRAM_INPUT)
+ parseState->inputsRead |= (1 << inst->SrcReg[2].Index);
parseState->numInst++;
parseState.numInst = 0;
parseState.inputsRead = 0;
parseState.outputsWritten = 0;
- parseState.progRegsWritten = 0;
+ parseState.anyProgRegsWritten = GL_FALSE;
/* Reset error state */
_mesa_set_program_error(ctx, -1, NULL);
/* successful parse! */
if (parseState.isStateProgram) {
- if (parseState.progRegsWritten == 0) {
+ if (!parseState.anyProgRegsWritten) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glLoadProgramNV(c[#] not written)");
return;
if (src->Negate)
_mesa_printf("-");
if (src->RelAddr) {
- if (src->Register > 0)
- _mesa_printf("c[A0.x + %d]", src->Register);
- else if (src->Register < 0)
- _mesa_printf("c[A0.x - %d]", -src->Register);
+ if (src->Index > 0)
+ _mesa_printf("c[A0.x + %d]", src->Index);
+ else if (src->Index < 0)
+ _mesa_printf("c[A0.x - %d]", -src->Index);
else
_mesa_printf("c[A0.x]");
}
- else if (src->Register >= VP_OUTPUT_REG_START
- && src->Register <= VP_OUTPUT_REG_END) {
- _mesa_printf("o[%s]", OutputRegisters[src->Register - VP_OUTPUT_REG_START]);
+ else if (src->File == PROGRAM_OUTPUT) {
+ _mesa_printf("o[%s]", OutputRegisters[src->Index]);
}
- else if (src->Register >= VP_INPUT_REG_START
- && src->Register <= VP_INPUT_REG_END) {
- _mesa_printf("v[%s]", InputRegisters[src->Register - VP_INPUT_REG_START]);
+ else if (src->File == PROGRAM_INPUT) {
+ _mesa_printf("v[%s]", InputRegisters[src->Index]);
}
- else if (src->Register >= VP_PROG_REG_START
- && src->Register <= VP_PROG_REG_END) {
- _mesa_printf("c[%d]", src->Register - VP_PROG_REG_START);
+ else if (src->File == PROGRAM_ENV_PARAM) {
+ _mesa_printf("c[%d]", src->Index);
}
else {
- _mesa_printf("R%d", src->Register - VP_TEMP_REG_START);
+ ASSERT(src->File == PROGRAM_TEMPORARY);
+ _mesa_printf("R%d", src->Index);
}
if (src->Swizzle[0] == src->Swizzle[1] &&
GLint w = dst->WriteMask[0] + dst->WriteMask[1]
+ dst->WriteMask[2] + dst->WriteMask[3];
- if (dst->Register >= VP_OUTPUT_REG_START
- && dst->Register <= VP_OUTPUT_REG_END) {
- _mesa_printf("o[%s]", OutputRegisters[dst->Register - VP_OUTPUT_REG_START]);
+ if (dst->File == PROGRAM_OUTPUT) {
+ _mesa_printf("o[%s]", OutputRegisters[dst->Index]);
}
- else if (dst->Register >= VP_INPUT_REG_START
- && dst->Register <= VP_INPUT_REG_END) {
- _mesa_printf("v[%s]", InputRegisters[dst->Register - VP_INPUT_REG_START]);
+ else if (dst->File == PROGRAM_INPUT) {
+ _mesa_printf("v[%s]", InputRegisters[dst->Index]);
}
- else if (dst->Register >= VP_PROG_REG_START
- && dst->Register <= VP_PROG_REG_END) {
- _mesa_printf("c[%d]", dst->Register - VP_PROG_REG_START);
+ else if (dst->File == PROGRAM_ENV_PARAM) {
+ _mesa_printf("c[%d]", dst->Index);
}
else {
- _mesa_printf("R%d", dst->Register - VP_TEMP_REG_START);
+ ASSERT(dst->File == PROGRAM_TEMPORARY);
+ _mesa_printf("R%d", dst->Index);
}
if (w != 0 && w != 4) {
}
}
+
+const char *
+_mesa_nv_vertex_input_register_name(GLuint i)
+{
+ ASSERT(i < MAX_NV_VERTEX_PROGRAM_INPUTS);
+ return InputRegisters[i];
+}
+
+
+const char *
+_mesa_nv_vertex_output_register_name(GLuint i)
+{
+ ASSERT(i < MAX_NV_VERTEX_PROGRAM_OUTPUTS);
+ return OutputRegisters[i];
+}