Fix Windows newlines.
[mesa.git] / src / mesa / shader / program.c
index 352ed93465e7a07d92904fe90148fbf3c41eb3dd..f999e0695ba54fd87e72a5dabe5b93d7122c2fff 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Mesa 3-D graphics library
- * Version:  6.5
+ * Version:  6.5.1
  *
  * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
  *
@@ -300,10 +300,31 @@ _mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
       _mesa_free_parameter_list(prog->Parameters);
    }
 
+   /* XXX this is a little ugly */
+   if (prog->Target == GL_VERTEX_PROGRAM_ARB) {
+      struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
+      if (vprog->TnlData)
+         _mesa_free(vprog->TnlData);
+   }
+
    _mesa_free(prog);
 }
 
 
+/**
+ * Return the gl_program object for a given ID.
+ * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
+ * casts elsewhere.
+ */
+struct gl_program *
+_mesa_lookup_program(GLcontext *ctx, GLuint id)
+{
+   if (id)
+      return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
+   else
+      return NULL;
+}
+
 
 /**********************************************************************/
 /* Program parameter functions                                        */
@@ -405,7 +426,10 @@ _mesa_add_named_parameter(struct gl_program_parameter_list *paramList,
 
 
 /**
- * Add a new unnamed constant to the parameter list.
+ * Add a new named constant to the parameter list.
+ * This will be used when the program contains something like this:
+ *    PARAM myVals = { 0, 1, 2, 3 };
+ *
  * \param paramList - the parameter list
  * \param values - four float values
  * \return index of the new parameter.
@@ -420,6 +444,9 @@ _mesa_add_named_constant(struct gl_program_parameter_list *paramList,
 
 /**
  * Add a new unnamed constant to the parameter list.
+ * This will be used when the program contains something like this:
+ *    MOV r, { 0, 1, 2, 3 };
+ *
  * \param paramList - the parameter list
  * \param values - four float values
  * \return index of the new parameter.
@@ -434,9 +461,11 @@ _mesa_add_unnamed_constant(struct gl_program_parameter_list *paramList,
 
 /**
  * Add a new state reference to the parameter list.
+ * This will be used when the program contains something like this:
+ *    PARAM ambient = state.material.front.ambient;
+ *
  * \param paramList - the parameter list
  * \param state     - an array of 6 state tokens
- *
  * \return index of the new parameter.
  */
 GLint
@@ -888,6 +917,15 @@ _mesa_fetch_state(GLcontext *ctx, const enum state_index state[],
            case STATE_NORMAL_SCALE:
                ASSIGN_4V(value, ctx->_ModelViewInvScale, 0, 0, 1);
                break;
+           case STATE_TEXRECT_SCALE: {
+              const int unit = (int) state[2];
+              const struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
+              if (texObj) {
+                 struct gl_texture_image *texImage = texObj->Image[0][0];
+                 ASSIGN_4V(value, 1.0 / texImage->Width, 1.0 / texImage->Height, 0, 1);
+              }
+               break;
+           }
            default:
                _mesa_problem(ctx, "Bad state switch in _mesa_fetch_state()");
                return;
@@ -959,6 +997,8 @@ static GLuint make_state_flags(const GLint state[])
       switch (state[1]) {
       case STATE_NORMAL_SCALE:
         return _NEW_MODELVIEW;
+      case STATE_TEXRECT_SCALE:
+        return _NEW_TEXTURE;
       default:
          _mesa_problem(NULL, "unexpected int. state in make_state_flags()");
         return 0;
@@ -1290,6 +1330,43 @@ _mesa_init_instruction(struct prog_instruction *inst)
 }
 
 
+/**
+ * Allocate an array of program instructions.
+ * \param numInst  number of instructions
+ * \return pointer to instruction memory
+ */
+struct prog_instruction *
+_mesa_alloc_instructions(GLuint numInst)
+{
+   return (struct prog_instruction *)
+      _mesa_calloc(numInst * sizeof(struct prog_instruction));
+}
+
+
+/**
+ * Reallocate memory storing an array of program instructions.
+ * This is used when we need to append additional instructions onto an
+ * program.
+ * \param oldInst  pointer to first of old/src instructions
+ * \param numOldInst  number of instructions at <oldInst>
+ * \param numNewInst  desired size of new instruction array.
+ * \return  pointer to start of new instruction array.
+ */
+struct prog_instruction *
+_mesa_realloc_instructions(struct prog_instruction *oldInst,
+                           GLuint numOldInst, GLuint numNewInst)
+{
+   struct prog_instruction *newInst;
+
+   newInst = (struct prog_instruction *)
+      _mesa_realloc(oldInst,
+                    numOldInst * sizeof(struct prog_instruction),
+                    numNewInst * sizeof(struct prog_instruction));
+
+   return newInst;
+}
+
+
 /**
  * Basic info about each instruction
  */
@@ -1384,19 +1461,8 @@ static const struct instruction_info InstInfo[MAX_OPCODE] = {
 GLuint
 _mesa_num_inst_src_regs(enum prog_opcode opcode)
 {
-   GLuint i;
-#ifdef DEBUG
-   for (i = 0; i < MAX_OPCODE; i++) {
-      ASSERT(i == InstInfo[i].Opcode);
-   }
-#endif
-   for (i = 0; i < MAX_OPCODE; i++) {
-      if (InstInfo[i].Opcode == opcode) {
-         return InstInfo[i].NumSrcRegs;
-      }
-   }
-   _mesa_problem(NULL, "invalid opcode in _mesa_num_inst_src_regs");
-   return 0;
+   ASSERT(opcode == InstInfo[opcode].Opcode);
+   return InstInfo[opcode].NumSrcRegs;
 }
 
 
@@ -1535,6 +1601,38 @@ print_src_reg(const struct prog_src_register *srcReg)
                                srcReg->NegateBase, GL_FALSE));
 }
 
+void
+_mesa_print_alu_instruction(const struct prog_instruction *inst,
+                           const char *opcode_string, 
+                           GLuint numRegs)
+{
+   GLuint j;
+
+   _mesa_printf("%s", opcode_string);
+
+   /* frag prog only */
+   if (inst->SaturateMode == SATURATE_ZERO_ONE)
+      _mesa_printf("_SAT");
+
+   if (inst->DstReg.File != PROGRAM_UNDEFINED) {
+      _mesa_printf(" %s[%d]%s",
+                  program_file_string((enum register_file) inst->DstReg.File),
+                  inst->DstReg.Index,
+                  writemask_string(inst->DstReg.WriteMask));
+   }
+
+   if (numRegs > 0)
+      _mesa_printf(", ");
+
+   for (j = 0; j < numRegs; j++) {
+      print_src_reg(inst->SrcReg + j);
+      if (j + 1 < numRegs)
+        _mesa_printf(", ");
+   }
+
+   _mesa_printf(";\n");
+}
+
 
 /**
  * Print a single vertex/fragment program instruction.
@@ -1596,34 +1694,10 @@ _mesa_print_instruction(const struct prog_instruction *inst)
    /* XXX may need for other special-case instructions */
    default:
       /* typical alu instruction */
-      {
-         const GLuint numRegs = _mesa_num_inst_src_regs(inst->Opcode);
-         GLuint j;
-
-         _mesa_printf("%s", _mesa_opcode_string(inst->Opcode));
-
-         /* frag prog only */
-         if (inst->SaturateMode == SATURATE_ZERO_ONE)
-            _mesa_printf("_SAT");
-
-         if (inst->DstReg.File != PROGRAM_UNDEFINED) {
-            _mesa_printf(" %s[%d]%s",
-                         program_file_string((enum register_file) inst->DstReg.File),
-                         inst->DstReg.Index,
-                         writemask_string(inst->DstReg.WriteMask));
-         }
-
-         if (numRegs > 0)
-            _mesa_printf(", ");
-
-         for (j = 0; j < numRegs; j++) {
-            print_src_reg(inst->SrcReg + j);
-            if (j + 1 < numRegs)
-               _mesa_printf(", ");
-         }
-
-         _mesa_printf(";\n");
-      }
+      _mesa_print_alu_instruction(inst,
+                                 _mesa_opcode_string(inst->Opcode),
+                                 _mesa_num_inst_src_regs(inst->Opcode));
+      break;
    }
 }
 
@@ -1676,6 +1750,27 @@ _mesa_print_program_parameters(GLcontext *ctx, const struct gl_program *prog)
 }
 
 
+/**
+ * Mixing ARB and NV vertex/fragment programs can be tricky.
+ * Note: GL_VERTEX_PROGRAM_ARB == GL_VERTEX_PROGRAM_NV
+ *  but, GL_FRAGMENT_PROGRAM_ARB != GL_FRAGMENT_PROGRAM_NV
+ * The two different fragment program targets are supposed to be compatible
+ * to some extent (see GL_ARB_fragment_program spec).
+ * This function does the compatibility check.
+ */
+static GLboolean
+compatible_program_targets(GLenum t1, GLenum t2)
+{
+   if (t1 == t2)
+      return GL_TRUE;
+   if (t1 == GL_FRAGMENT_PROGRAM_ARB && t2 == GL_FRAGMENT_PROGRAM_NV)
+      return GL_TRUE;
+   if (t1 == GL_FRAGMENT_PROGRAM_NV && t2 == GL_FRAGMENT_PROGRAM_ARB)
+      return GL_TRUE;
+   return GL_FALSE;
+}
+
+
 
 /**********************************************************************/
 /* API functions                                                      */
@@ -1690,103 +1785,95 @@ _mesa_print_program_parameters(GLcontext *ctx, const struct gl_program *prog)
 void GLAPIENTRY
 _mesa_BindProgram(GLenum target, GLuint id)
 {
-   struct gl_program *prog;
+   struct gl_program *curProg, *newProg;
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    FLUSH_VERTICES(ctx, _NEW_PROGRAM);
 
+   /* Error-check target and get curProg */
    if ((target == GL_VERTEX_PROGRAM_ARB) && /* == GL_VERTEX_PROGRAM_NV */
         (ctx->Extensions.NV_vertex_program ||
          ctx->Extensions.ARB_vertex_program)) {
-      /*** Vertex program binding ***/
-      struct gl_vertex_program *curProg = ctx->VertexProgram.Current;
-      if (curProg->Base.Id == id) {
-         /* binding same program - no change */
-         return;
-      }
-      if (curProg->Base.Id != 0) {
-         /* decrement refcount on previously bound vertex program */
-         curProg->Base.RefCount--;
-         /* and delete if refcount goes below one */
-         if (curProg->Base.RefCount <= 0) {
-            /* the program ID was already removed from the hash table */
-            ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
-         }
-      }
+      curProg = &ctx->VertexProgram.Current->Base;
    }
    else if ((target == GL_FRAGMENT_PROGRAM_NV
              && ctx->Extensions.NV_fragment_program) ||
             (target == GL_FRAGMENT_PROGRAM_ARB
              && ctx->Extensions.ARB_fragment_program)) {
-      /*** Fragment program binding ***/
-      struct gl_fragment_program *curProg = ctx->FragmentProgram.Current;
-      if (curProg->Base.Id == id) {
-         /* binding same program - no change */
-         return;
-      }
-      if (curProg->Base.Id != 0) {
-         /* decrement refcount on previously bound fragment program */
-         curProg->Base.RefCount--;
-         /* and delete if refcount goes below one */
-         if (curProg->Base.RefCount <= 0) {
-            /* the program ID was already removed from the hash table */
-            ctx->Driver.DeleteProgram(ctx, &(curProg->Base));
-         }
-      }
+      curProg = &ctx->FragmentProgram.Current->Base;
    }
    else {
       _mesa_error(ctx, GL_INVALID_ENUM, "glBindProgramNV/ARB(target)");
       return;
    }
 
-   /* NOTE: binding to a non-existant program is not an error.
+   /*
+    * Get pointer to new program to bind.
+    * NOTE: binding to a non-existant program is not an error.
     * That's supposed to be caught in glBegin.
     */
    if (id == 0) {
-      /* Bind default program */
-      prog = NULL;
+      /* Bind default program */
+      newProg = NULL;
       if (target == GL_VERTEX_PROGRAM_ARB) /* == GL_VERTEX_PROGRAM_NV */
-         prog = ctx->Shared->DefaultVertexProgram;
+         newProg = ctx->Shared->DefaultVertexProgram;
       else
-         prog = ctx->Shared->DefaultFragmentProgram;
+         newProg = ctx->Shared->DefaultFragmentProgram;
    }
    else {
-      /* Bind user program */
-      prog = (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
-      if (!prog || prog == &_mesa_DummyProgram) {
+      /* Bind user program */
+      newProg = _mesa_lookup_program(ctx, id);
+      if (!newProg || newProg == &_mesa_DummyProgram) {
          /* allocate a new program now */
-         prog = ctx->Driver.NewProgram(ctx, target, id);
-         if (!prog) {
+         newProg = ctx->Driver.NewProgram(ctx, target, id);
+         if (!newProg) {
             _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindProgramNV/ARB");
             return;
          }
-         _mesa_HashInsert(ctx->Shared->Programs, id, prog);
+         _mesa_HashInsert(ctx->Shared->Programs, id, newProg);
       }
-      else if (prog->Target != target) {
+      else if (!compatible_program_targets(newProg->Target, target)) {
          _mesa_error(ctx, GL_INVALID_OPERATION,
                      "glBindProgramNV/ARB(target mismatch)");
          return;
       }
    }
 
-   /* bind now */
+   /** All error checking is complete now **/
+
+   if (curProg->Id == id) {
+      /* binding same program - no change */
+      return;
+   }
+
+   /* unbind/delete oldProg */
+   if (curProg->Id != 0) {
+      /* decrement refcount on previously bound fragment program */
+      curProg->RefCount--;
+      /* and delete if refcount goes below one */
+      if (curProg->RefCount <= 0) {
+         /* the program ID was already removed from the hash table */
+         ctx->Driver.DeleteProgram(ctx, curProg);
+      }
+   }
+
+   /* bind newProg */
    if (target == GL_VERTEX_PROGRAM_ARB) { /* == GL_VERTEX_PROGRAM_NV */
-      ctx->VertexProgram.Current = (struct gl_vertex_program *) prog;
+      ctx->VertexProgram.Current = (struct gl_vertex_program *) newProg;
    }
-   else if (target == GL_FRAGMENT_PROGRAM_NV || target == GL_FRAGMENT_PROGRAM_ARB) {
-      ctx->FragmentProgram.Current = (struct gl_fragment_program *) prog;
+   else if (target == GL_FRAGMENT_PROGRAM_NV ||
+            target == GL_FRAGMENT_PROGRAM_ARB) {
+      ctx->FragmentProgram.Current = (struct gl_fragment_program *) newProg;
    }
+   newProg->RefCount++;
 
    /* Never null pointers */
    ASSERT(ctx->VertexProgram.Current);
    ASSERT(ctx->FragmentProgram.Current);
 
-   if (prog)
-      prog->RefCount++;
-
    if (ctx->Driver.BindProgram)
-      ctx->Driver.BindProgram(ctx, target, prog);
+      ctx->Driver.BindProgram(ctx, target, newProg);
 }
 
 
@@ -1809,8 +1896,7 @@ _mesa_DeletePrograms(GLsizei n, const GLuint *ids)
 
    for (i = 0; i < n; i++) {
       if (ids[i] != 0) {
-         struct gl_program *prog = (struct gl_program *)
-            _mesa_HashLookup(ctx->Shared->Programs, ids[i]);
+         struct gl_program *prog = _mesa_lookup_program(ctx, ids[i]);
          if (prog == &_mesa_DummyProgram) {
             _mesa_HashRemove(ctx->Shared->Programs, ids[i]);
          }
@@ -1899,7 +1985,7 @@ _mesa_IsProgram(GLuint id)
    if (id == 0)
       return GL_FALSE;
 
-   if (_mesa_HashLookup(ctx->Shared->Programs, id))
+   if (_mesa_lookup_program(ctx, id))
       return GL_TRUE;
    else
       return GL_FALSE;