Merge branch 'master' into i915-unification
[mesa.git] / src / mesa / shader / nvfragparse.c
index 3a480058440631bbad926b23e109884276c8148e..0f1a1eade4afcd7ae75667125527d94f986104d2 100644 (file)
 
 #include "glheader.h"
 #include "context.h"
-#include "hash.h"
 #include "imports.h"
 #include "macros.h"
-#include "mtypes.h"
-#include "program_instruction.h"
+#include "prog_parameter.h"
+#include "prog_instruction.h"
 #include "nvfragparse.h"
-#include "nvprogram.h"
 #include "program.h"
 
 
@@ -146,9 +144,9 @@ struct parse_state {
    const GLubyte *start;              /* start of program string */
    const GLubyte *pos;                /* current position */
    const GLubyte *curLine;
-   struct fragment_program *program;  /* current program */
+   struct gl_fragment_program *program;  /* current program */
 
-   struct program_parameter_list *parameters;
+   struct gl_program_parameter_list *parameters;
 
    GLuint numInst;                    /* number of instructions parsed */
    GLuint inputsRead;                 /* bitmask of input registers used */
@@ -248,7 +246,7 @@ MatchInstruction(const GLubyte *token)
          return result;
       }
    }
-   result.opcode = (enum prog_opcode) -1;
+   result.opcode = MAX_OPCODE; /* i.e. invalid instruction */
    return result;
 }
 
@@ -983,16 +981,16 @@ Parse_VectorSrc(struct parse_state *parseState,
       srcReg->NegateAbs = (sign < 0.0F) ? GL_TRUE : GL_FALSE;
 
       if (Parse_String(parseState, "-"))
-         srcReg->NegateBase = 0xf;
+         srcReg->NegateBase = NEGATE_XYZW;
       else if (Parse_String(parseState, "+"))
-         srcReg->NegateBase = 0;
+         srcReg->NegateBase = NEGATE_NONE;
       else
-         srcReg->NegateBase = 0;
+         srcReg->NegateBase = NEGATE_NONE;
    }
    else {
       srcReg->Abs = GL_FALSE;
       srcReg->NegateAbs = GL_FALSE;
-      srcReg->NegateBase = (sign < 0.0F) ? 0xf : 0x0;
+      srcReg->NegateBase = (sign < 0.0F) ? NEGATE_XYZW : NEGATE_NONE;
    }
 
    /* This should be the real src vector/register name */
@@ -1009,14 +1007,14 @@ Parse_VectorSrc(struct parse_state *parseState,
       srcReg->Index = idx;
    }
    else if (token[0] == 'f') {
-      /* XXX this might be an identier! */
+      /* XXX this might be an identifier! */
       srcReg->File = PROGRAM_INPUT;
       if (!Parse_FragReg(parseState, &idx))
          RETURN_ERROR;
       srcReg->Index = idx;
    }
    else if (token[0] == 'p') {
-      /* XXX this might be an identier! */
+      /* XXX this might be an identifier! */
       srcReg->File = PROGRAM_LOCAL_PARAM;
       if (!Parse_ProgramParamReg(parseState, &idx))
          RETURN_ERROR;
@@ -1041,7 +1039,8 @@ Parse_VectorSrc(struct parse_state *parseState,
       GLuint paramIndex;
       if (!Parse_ScalarConstant(parseState, values))
          RETURN_ERROR;
-      paramIndex = _mesa_add_unnamed_constant(parseState->parameters, values);
+      paramIndex = _mesa_add_unnamed_constant(parseState->parameters,
+                                              values, 4, NULL);
       srcReg->File = PROGRAM_NAMED_PARAM;
       srcReg->Index = paramIndex;
    }
@@ -1052,7 +1051,8 @@ Parse_VectorSrc(struct parse_state *parseState,
       (void) Parse_String(parseState, "{");
       if (!Parse_VectorConstant(parseState, values))
          RETURN_ERROR;
-      paramIndex = _mesa_add_unnamed_constant(parseState->parameters, values);
+      paramIndex = _mesa_add_unnamed_constant(parseState->parameters,
+                                              values, 4, NULL);
       srcReg->File = PROGRAM_NAMED_PARAM;
       srcReg->Index = paramIndex;      
    }
@@ -1107,16 +1107,16 @@ Parse_ScalarSrcReg(struct parse_state *parseState,
       srcReg->NegateAbs = (sign < 0.0F) ? GL_TRUE : GL_FALSE;
 
       if (Parse_String(parseState, "-"))
-         srcReg->NegateBase = 0xf;
+         srcReg->NegateBase = NEGATE_XYZW;
       else if (Parse_String(parseState, "+"))
-         srcReg->NegateBase = 0x0;
+         srcReg->NegateBase = NEGATE_NONE;
       else
-         srcReg->NegateBase = 0x0;
+         srcReg->NegateBase = NEGATE_NONE;
    }
    else {
       srcReg->Abs = GL_FALSE;
       srcReg->NegateAbs = GL_FALSE;
-      srcReg->NegateBase = (sign < 0.0F) ? 0xf : 0x0;
+      srcReg->NegateBase = (sign < 0.0F) ? NEGATE_XYZW : NEGATE_NONE;
    }
 
    if (!Peek_Token(parseState, token))
@@ -1142,7 +1142,22 @@ Parse_ScalarSrcReg(struct parse_state *parseState,
       (void) Parse_String(parseState, "{");
       if (!Parse_VectorConstant(parseState, values))
          RETURN_ERROR;
-      paramIndex = _mesa_add_unnamed_constant(parseState->parameters, values);
+      paramIndex = _mesa_add_unnamed_constant(parseState->parameters,
+                                              values, 4, NULL);
+      srcReg->File = PROGRAM_NAMED_PARAM;
+      srcReg->Index = paramIndex;      
+   }
+   else if (IsLetter(token[0])){
+      /* named param/constant */
+      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;      
    }
@@ -1152,7 +1167,8 @@ Parse_ScalarSrcReg(struct parse_state *parseState,
       GLuint paramIndex;
       if (!Parse_ScalarConstant(parseState, values))
          RETURN_ERROR;
-      paramIndex = _mesa_add_unnamed_constant(parseState->parameters, values);
+      paramIndex = _mesa_add_unnamed_constant(parseState->parameters,
+                                              values, 4, NULL);
       srcReg->Index = paramIndex;      
       srcReg->File = PROGRAM_NAMED_PARAM;
       needSuffix = GL_FALSE;
@@ -1213,7 +1229,7 @@ Parse_PrintInstruction(struct parse_state *parseState,
    for (len = 0; str[len] != '\''; len++) /* find closing quote */
       ;
    parseState->pos += len + 1;
-   msg = _mesa_malloc(len + 1);
+   msg = (GLubyte*) _mesa_malloc(len + 1);
 
    _mesa_memcpy(msg, str, len);
    msg[len] = 0;
@@ -1241,7 +1257,7 @@ Parse_PrintInstruction(struct parse_state *parseState,
    }
 
    inst->SrcReg[0].Swizzle = SWIZZLE_NOOP;
-   inst->SrcReg[0].NegateBase = 0x0;
+   inst->SrcReg[0].NegateBase = NEGATE_NONE;
    inst->SrcReg[0].Abs = GL_FALSE;
    inst->SrcReg[0].NegateAbs = GL_FALSE;
 
@@ -1259,7 +1275,7 @@ Parse_InstructionSequence(struct parse_state *parseState,
       GLubyte token[100];
 
       /* Initialize the instruction */
-      _mesa_init_instruction(inst);
+      _mesa_init_instructions(inst, 1);
 
       /* special instructions */
       if (Parse_String(parseState, "DEFINE")) {
@@ -1320,14 +1336,15 @@ Parse_InstructionSequence(struct parse_state *parseState,
 
          /* try to find matching instuction */
          instMatch = MatchInstruction(token);
-         if (instMatch.opcode < 0) {
+         if (instMatch.opcode >= MAX_OPCODE) {
             /* bad instruction name */
             RETURN_ERROR2("Unexpected token: ", token);
          }
 
          inst->Opcode = instMatch.opcode;
          inst->Precision = instMatch.suffixes & (_R | _H | _X);
-         inst->Saturate = (instMatch.suffixes & (_S)) ? GL_TRUE : GL_FALSE;
+         inst->SaturateMode = (instMatch.suffixes & (_S))
+            ? SATURATE_ZERO_ONE : SATURATE_OFF;
          inst->CondUpdate = (instMatch.suffixes & (_C)) ? GL_TRUE : GL_FALSE;
          inst->StringPos = parseState->curLine - parseState->start;
          assert(inst->StringPos >= 0);
@@ -1451,7 +1468,7 @@ Parse_InstructionSequence(struct parse_state *parseState,
 void
 _mesa_parse_nv_fragment_program(GLcontext *ctx, GLenum dstTarget,
                                 const GLubyte *str, GLsizei len,
-                                struct fragment_program *program)
+                                struct gl_fragment_program *program)
 {
    struct parse_state parseState;
    struct prog_instruction instBuffer[MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS];
@@ -1519,14 +1536,12 @@ _mesa_parse_nv_fragment_program(GLcontext *ctx, GLenum dstTarget,
 
       /* copy the compiled instructions */
       assert(parseState.numInst <= MAX_NV_FRAGMENT_PROGRAM_INSTRUCTIONS);
-      newInst = (struct prog_instruction *)
-         MALLOC(parseState.numInst * sizeof(struct prog_instruction));
+      newInst = _mesa_alloc_instructions(parseState.numInst);
       if (!newInst) {
          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glLoadProgramNV");
          return;  /* out of memory */
       }
-      MEMCPY(newInst, instBuffer,
-             parseState.numInst * sizeof(struct prog_instruction));
+      _mesa_copy_instructions(newInst, instBuffer, parseState.numInst);
 
       /* install the program */
       program->Base.Target = target;
@@ -1535,17 +1550,18 @@ _mesa_parse_nv_fragment_program(GLcontext *ctx, GLenum dstTarget,
       }
       program->Base.String = programString;
       program->Base.Format = GL_PROGRAM_FORMAT_ASCII_ARB;
-      if (program->Instructions) {
-         FREE(program->Instructions);
+      if (program->Base.Instructions) {
+         _mesa_free(program->Base.Instructions);
       }
-      program->Instructions = newInst;
-      program->InputsRead = parseState.inputsRead;
-      program->OutputsWritten = parseState.outputsWritten;
+      program->Base.Instructions = newInst;
+      program->Base.NumInstructions = parseState.numInst;
+      program->Base.InputsRead = parseState.inputsRead;
+      program->Base.OutputsWritten = parseState.outputsWritten;
       for (u = 0; u < ctx->Const.MaxTextureImageUnits; u++)
-         program->TexturesUsed[u] = parseState.texturesUsed[u];
+         program->Base.TexturesUsed[u] = parseState.texturesUsed[u];
 
       /* save program parameters */
-      program->Parameters = parseState.parameters;
+      program->Base.Parameters = parseState.parameters;
 
       /* allocate registers for declared program parameters */
 #if 00
@@ -1567,7 +1583,7 @@ _mesa_parse_nv_fragment_program(GLcontext *ctx, GLenum dstTarget,
 
 
 static void
-PrintSrcReg(const struct fragment_program *program,
+PrintSrcReg(const struct gl_fragment_program *program,
             const struct prog_src_register *src)
 {
    static const char comps[5] = "xyzw";
@@ -1582,17 +1598,16 @@ PrintSrcReg(const struct fragment_program *program,
       _mesa_printf("-");
    }
    if (src->File == PROGRAM_NAMED_PARAM) {
-      if (program->Parameters->Parameters[src->Index].Type == PROGRAM_CONSTANT) {
-         _mesa_printf("{%g, %g, %g, %g}",
-                program->Parameters->ParameterValues[src->Index][0],
-                program->Parameters->ParameterValues[src->Index][1],
-                program->Parameters->ParameterValues[src->Index][2],
-                program->Parameters->ParameterValues[src->Index][3]);
+      if (program->Base.Parameters->Parameters[src->Index].Type
+          == PROGRAM_CONSTANT) {
+         const GLfloat *v;
+         v = program->Base.Parameters->ParameterValues[src->Index];
+         _mesa_printf("{%g, %g, %g, %g}", v[0], v[1], v[2], v[3]);
       }
       else {
-         ASSERT(program->Parameters->Parameters[src->Index].Type
+         ASSERT(program->Base.Parameters->Parameters[src->Index].Type
                 == PROGRAM_NAMED_PARAM);
-         _mesa_printf("%s", program->Parameters->Parameters[src->Index].Name);
+         _mesa_printf("%s", program->Base.Parameters->Parameters[src->Index].Name);
       }
    }
    else if (src->File == PROGRAM_OUTPUT) {
@@ -1730,11 +1745,11 @@ PrintDstReg(const struct prog_dst_register *dst)
  * Print (unparse) the given vertex program.  Just for debugging.
  */
 void
-_mesa_print_nv_fragment_program(const struct fragment_program *program)
+_mesa_print_nv_fragment_program(const struct gl_fragment_program *program)
 {
    const struct prog_instruction *inst;
 
-   for (inst = program->Instructions; inst->Opcode != OPCODE_END; inst++) {
+   for (inst = program->Base.Instructions; inst->Opcode != OPCODE_END; inst++) {
       int i;
       for (i = 0; Instructions[i].name; i++) {
          if (inst->Opcode == Instructions[i].opcode) {
@@ -1746,7 +1761,7 @@ _mesa_print_nv_fragment_program(const struct fragment_program *program)
                _mesa_printf("X");
             if (inst->CondUpdate)
                _mesa_printf("C");
-            if (inst->Saturate)
+            if (inst->SaturateMode == SATURATE_ZERO_ONE)
                _mesa_printf("_SAT");
             _mesa_printf(" ");