Drop GLcontext typedef and use struct gl_context instead
[mesa.git] / src / mesa / swrast / s_atifragshader.c
index 292f00e9465e096b3719620134c0951f4cc3f9c8..1eb026e0092052cf73a4eeafa4cfc3a345b863ba 100644 (file)
@@ -1,5 +1,4 @@
 /*
- *
  * Copyright (C) 2004  David Airlie   All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
-#include "glheader.h"
-#include "colormac.h"
-#include "context.h"
-#include "atifragshader.h"
-#include "macros.h"
-#include "program.h"
+#include "main/glheader.h"
+#include "main/colormac.h"
+#include "main/macros.h"
+#include "main/atifragshader.h"
+#include "swrast/s_atifragshader.h"
+#include "swrast/s_context.h"
+
+
+/**
+ * State for executing ATI fragment shader.
+ */
+struct atifs_machine
+{
+   GLfloat Registers[6][4];         /** six temporary registers */
+   GLfloat PrevPassRegisters[6][4];
+   GLfloat Inputs[2][4];   /** Primary, secondary input colors */
+};
 
-#include "s_atifragshader.h"
-#include "s_nvfragprog.h"
-#include "s_span.h"
 
 
 /**
  * Fetch a texel.
  */
 static void
-fetch_texel(GLcontext * ctx, const GLfloat texcoord[4], GLfloat lambda,
+fetch_texel(struct gl_context * ctx, const GLfloat texcoord[4], GLfloat lambda,
            GLuint unit, GLfloat color[4])
 {
-   GLchan rgba[4];
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
 
    /* XXX use a float-valued TextureSample routine here!!! */
-   swrast->TextureSample[unit] (ctx, unit, ctx->Texture.Unit[unit]._Current,
-                               1, (const GLfloat(*)[4]) texcoord,
-                               &lambda, &rgba);
-   color[0] = CHAN_TO_FLOAT(rgba[0]);
-   color[1] = CHAN_TO_FLOAT(rgba[1]);
-   color[2] = CHAN_TO_FLOAT(rgba[2]);
-   color[3] = CHAN_TO_FLOAT(rgba[3]);
+   swrast->TextureSample[unit](ctx, ctx->Texture.Unit[unit]._Current,
+                               1, (const GLfloat(*)[4]) texcoord,
+                               &lambda, (GLfloat (*)[4]) color);
 }
 
 static void
@@ -80,10 +82,11 @@ apply_swizzle(GLfloat values[4], GLuint swizzle)
       break;
    case GL_SWIZZLE_STQ_DQ_ATI:
 /* make sure q is not 0 to avoid problems later with infinite values (texture lookup)? */
-      if (q == 0.0F) q = 0.000000001;
+      if (q == 0.0F)
+         q = 0.000000001F;
       values[0] = s / q;
       values[1] = t / q;
-      values[2] = 1 / q;
+      values[2] = 1.0F / q;
       break;
    }
    values[3] = 0.0;
@@ -135,7 +138,7 @@ apply_src_mod(GLint optype, GLuint mod, GLfloat * val)
         val[i] = 1 - val[i];
 
       if (mod & GL_BIAS_BIT_ATI)
-        val[i] = val[i] - 0.5;
+        val[i] = val[i] - 0.5F;
 
       if (mod & GL_2X_BIT_ATI)
         val[i] = 2 * val[i];
@@ -169,27 +172,27 @@ apply_dst_mod(GLuint optype, GLuint mod, GLfloat * val)
         val[i] = 8 * val[i];
         break;
       case GL_HALF_BIT_ATI:
-        val[i] = val[i] * 0.5;
+        val[i] = val[i] * 0.5F;
         break;
       case GL_QUARTER_BIT_ATI:
-        val[i] = val[i] * 0.25;
+        val[i] = val[i] * 0.25F;
         break;
       case GL_EIGHTH_BIT_ATI:
-        val[i] = val[i] * 0.125;
+        val[i] = val[i] * 0.125F;
         break;
       }
 
       if (has_sat) {
-        if (val[i] < 0.0)
-           val[i] = 0;
-        else if (val[i] > 1.0)
-           val[i] = 1.0;
+        if (val[i] < 0.0F)
+           val[i] = 0.0F;
+        else if (val[i] > 1.0F)
+           val[i] = 1.0F;
       }
       else {
-        if (val[i] < -8.0)
-           val[i] = -8.0;
-        else if (val[i] > 8.0)
-           val[i] = 8.0;
+        if (val[i] < -8.0F)
+           val[i] = -8.0F;
+        else if (val[i] > 8.0F)
+           val[i] = 8.0F;
       }
    }
 }
@@ -232,17 +235,6 @@ finish_pass(struct atifs_machine *machine)
    }
 }
 
-/**
- * Execute the given fragment shader
- * NOTE: we do everything in single-precision floating point; we don't
- * currently observe the single/half/fixed-precision qualifiers.
- * \param ctx - rendering context
- * \param program - the fragment program to execute
- * \param machine - machine state (register file)
- * \param maxInst - max number of instructions to execute
- * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
- */
-
 struct ati_fs_opcode_st ati_fs_opcodes[] = {
    {GL_ADD_ATI, 2},
    {GL_SUB_ATI, 2},
@@ -261,7 +253,7 @@ struct ati_fs_opcode_st ati_fs_opcodes[] = {
 
 static void
 handle_pass_op(struct atifs_machine *machine, struct atifs_setupinst *texinst,
-              const struct sw_span *span, GLuint column, GLuint idx)
+              const SWspan *span, GLuint column, GLuint idx)
 {
    GLuint swizzle = texinst->swizzle;
    GLuint pass_tex = texinst->src;
@@ -269,7 +261,7 @@ handle_pass_op(struct atifs_machine *machine, struct atifs_setupinst *texinst,
    if (pass_tex >= GL_TEXTURE0_ARB && pass_tex <= GL_TEXTURE7_ARB) {
       pass_tex -= GL_TEXTURE0_ARB;
       COPY_4V(machine->Registers[idx],
-             span->array->texcoords[pass_tex][column]);
+             span->array->attribs[FRAG_ATTRIB_TEX0 + pass_tex][column]);
    }
    else if (pass_tex >= GL_REG_0_ATI && pass_tex <= GL_REG_5_ATI) {
       pass_tex -= GL_REG_0_ATI;
@@ -280,18 +272,19 @@ handle_pass_op(struct atifs_machine *machine, struct atifs_setupinst *texinst,
 }
 
 static void
-handle_sample_op(GLcontext * ctx, struct atifs_machine *machine,
-                struct atifs_setupinst *texinst, const struct sw_span *span,
+handle_sample_op(struct gl_context * ctx, struct atifs_machine *machine,
+                struct atifs_setupinst *texinst, const SWspan *span,
                 GLuint column, GLuint idx)
 {
 /* sample from unit idx using texinst->src as coords */
    GLuint swizzle = texinst->swizzle;
    GLuint coord_source = texinst->src;
-   GLfloat tex_coords[4];
+   GLfloat tex_coords[4] = { 0 };
 
    if (coord_source >= GL_TEXTURE0_ARB && coord_source <= GL_TEXTURE7_ARB) {
       coord_source -= GL_TEXTURE0_ARB;
-      COPY_4V(tex_coords, span->array->texcoords[coord_source][column]);
+      COPY_4V(tex_coords,
+              span->array->attribs[FRAG_ATTRIB_TEX0 + coord_source][column]);
    }
    else if (coord_source >= GL_REG_0_ATI && coord_source <= GL_REG_5_ATI) {
       coord_source -= GL_REG_0_ATI;
@@ -306,17 +299,28 @@ do {                                              \
    COPY_4V(src[optype][i], x);                         \
 } while (0)
 
-static GLboolean
-execute_shader(GLcontext * ctx,
-              const struct ati_fragment_shader *shader, GLuint maxInst,
-              struct atifs_machine *machine, const struct sw_span *span,
-              GLuint column)
+
+
+/**
+ * Execute the given fragment shader.
+ * NOTE: we do everything in single-precision floating point
+ * \param ctx - rendering context
+ * \param shader - the shader to execute
+ * \param machine - virtual machine state
+ * \param span - the SWspan we're operating on
+ * \param column - which pixel [i] we're operating on in the span
+ */
+static void
+execute_shader(struct gl_context *ctx, const struct ati_fragment_shader *shader,
+              struct atifs_machine *machine, const SWspan *span,
+               GLuint column)
 {
    GLuint pc;
    struct atifs_instruction *inst;
    struct atifs_setupinst *texinst;
    GLint optype;
-   GLint i, j, pass;
+   GLuint i;
+   GLint j, pass;
    GLint dstreg;
    GLfloat src[2][3][4];
    GLfloat zeros[4] = { 0.0, 0.0, 0.0, 0.0 };
@@ -339,19 +343,19 @@ execute_shader(GLcontext * ctx,
 
         /* setup the source registers for color and alpha ops */
         for (optype = 0; optype < 2; optype++) {
-           for (i = 0; i < inst->ArgCount[optype]; i++) {
+           for (i = 0; i < inst->ArgCount[optype]; i++) {
               GLint index = inst->SrcReg[optype][i].Index;
 
               if (index >= GL_REG_0_ATI && index <= GL_REG_5_ATI)
                  SETUP_SRC_REG(optype, i,
                                machine->Registers[index - GL_REG_0_ATI]);
               else if (index >= GL_CON_0_ATI && index <= GL_CON_7_ATI) {
-                 if (shader->localConstDef & (1 << (index - GL_CON_0_ATI))) {
+                 if (shader->LocalConstDef & (1 << (index - GL_CON_0_ATI))) {
                     SETUP_SRC_REG(optype, i,
                                shader->Constants[index - GL_CON_0_ATI]);
                  } else {
                     SETUP_SRC_REG(optype, i,
-                               ctx->ATIFragmentShader.globalConstants[index - GL_CON_0_ATI]);
+                               ctx->ATIFragmentShader.GlobalConstants[index - GL_CON_0_ATI]);
                  }
               }
               else if (index == GL_ONE)
@@ -544,81 +548,57 @@ execute_shader(GLcontext * ctx,
         }
       }
    }
-   return GL_TRUE;
 }
 
+
+/**
+ * Init fragment shader virtual machine state.
+ */
 static void
-init_machine(GLcontext * ctx, struct atifs_machine *machine,
+init_machine(struct gl_context * ctx, struct atifs_machine *machine,
             const struct ati_fragment_shader *shader,
-            const struct sw_span *span, GLuint col)
+            const SWspan *span, GLuint col)
 {
+   GLfloat (*inputs)[4] = machine->Inputs;
    GLint i, j;
 
    for (i = 0; i < 6; i++) {
       for (j = 0; j < 4; j++)
-        ctx->ATIFragmentShader.Machine.Registers[i][j] = 0.0;
-
+        machine->Registers[i][j] = 0.0;
    }
 
-   ctx->ATIFragmentShader.Machine.Inputs[ATI_FS_INPUT_PRIMARY][0] =
-      CHAN_TO_FLOAT(span->array->rgba[col][0]);
-   ctx->ATIFragmentShader.Machine.Inputs[ATI_FS_INPUT_PRIMARY][1] =
-      CHAN_TO_FLOAT(span->array->rgba[col][1]);
-   ctx->ATIFragmentShader.Machine.Inputs[ATI_FS_INPUT_PRIMARY][2] =
-      CHAN_TO_FLOAT(span->array->rgba[col][2]);
-   ctx->ATIFragmentShader.Machine.Inputs[ATI_FS_INPUT_PRIMARY][3] =
-      CHAN_TO_FLOAT(span->array->rgba[col][3]);
-
-   ctx->ATIFragmentShader.Machine.Inputs[ATI_FS_INPUT_SECONDARY][0] =
-      CHAN_TO_FLOAT(span->array->spec[col][0]);
-   ctx->ATIFragmentShader.Machine.Inputs[ATI_FS_INPUT_SECONDARY][1] =
-      CHAN_TO_FLOAT(span->array->spec[col][1]);
-   ctx->ATIFragmentShader.Machine.Inputs[ATI_FS_INPUT_SECONDARY][2] =
-      CHAN_TO_FLOAT(span->array->spec[col][2]);
-   ctx->ATIFragmentShader.Machine.Inputs[ATI_FS_INPUT_SECONDARY][3] =
-      CHAN_TO_FLOAT(span->array->spec[col][3]);
-
-   ctx->ATIFragmentShader.Machine.pass = 0;
+   COPY_4V(inputs[ATI_FS_INPUT_PRIMARY], span->array->attribs[FRAG_ATTRIB_COL0][col]);
+   COPY_4V(inputs[ATI_FS_INPUT_SECONDARY], span->array->attribs[FRAG_ATTRIB_COL1][col]);
 }
 
 
 
 /**
- * Execute the current fragment program, operating on the given span.
+ * Execute the current ATI shader program, operating on the given span.
  */
 void
-_swrast_exec_fragment_shader(GLcontext * ctx, struct sw_span *span)
+_swrast_exec_fragment_shader(struct gl_context * ctx, SWspan *span)
 {
    const struct ati_fragment_shader *shader = ctx->ATIFragmentShader.Current;
+   struct atifs_machine machine;
    GLuint i;
 
-   ctx->_CurrentProgram = GL_FRAGMENT_SHADER_ATI;
+   /* incoming colors should be floats */
+   ASSERT(span->array->ChanType == GL_FLOAT);
 
    for (i = 0; i < span->end; i++) {
       if (span->array->mask[i]) {
-        init_machine(ctx, &ctx->ATIFragmentShader.Machine,
-                     ctx->ATIFragmentShader.Current, span, i);
+        init_machine(ctx, &machine, shader, span, i);
 
-        if (execute_shader(ctx, shader, ~0,
-                           &ctx->ATIFragmentShader.Machine, span, i)) {
-           span->array->mask[i] = GL_FALSE;
-        }
+        execute_shader(ctx, shader, &machine, span, i);
 
+         /* store result color */
         {
-           const GLfloat *colOut =
-              ctx->ATIFragmentShader.Machine.Registers[0];
-
-           /*fprintf(stderr,"outputs %f %f %f %f\n", colOut[0], colOut[1], colOut[2], colOut[3]); */
-           UNCLAMPED_FLOAT_TO_CHAN(span->array->rgba[i][RCOMP], colOut[0]);
-           UNCLAMPED_FLOAT_TO_CHAN(span->array->rgba[i][GCOMP], colOut[1]);
-           UNCLAMPED_FLOAT_TO_CHAN(span->array->rgba[i][BCOMP], colOut[2]);
-           UNCLAMPED_FLOAT_TO_CHAN(span->array->rgba[i][ACOMP], colOut[3]);
+           const GLfloat *colOut = machine.Registers[0];
+            /*fprintf(stderr,"outputs %f %f %f %f\n",
+              colOut[0], colOut[1], colOut[2], colOut[3]); */
+            COPY_4V(span->array->attribs[FRAG_ATTRIB_COL0][i], colOut);
         }
       }
-
    }
-
-
-   ctx->_CurrentProgram = 0;
-
 }