Clean-up and optimize alpha test code.
[mesa.git] / src / mesa / swrast / s_span.c
index 3afa91341b4207cfaec93b030ce55736a3c39d3b..4266f085e21aaf38338fff0dc7d7282ebaf46326 100644 (file)
@@ -1,21 +1,21 @@
-/* $Id: s_span.c,v 1.7 2001/02/14 22:40:21 brianp Exp $ */
+/* $Id: s_span.c,v 1.28 2002/01/31 00:27:43 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
- * Version:  3.5
- * 
- * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
- * 
+ * Version:  4.1
+ *
+ * Copyright (C) 1999-2002  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"),
  * to deal in the Software without restriction, including without limitation
  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  * and/or sell copies of the Software, and to permit persons to whom the
  * Software is furnished to do so, subject to the following conditions:
- * 
+ *
  * The above copyright notice and this permission notice shall be included
  * in all copies or substantial portions of the Software.
- * 
+ *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
@@ -33,6 +33,7 @@
 
 #include "glheader.h"
 #include "colormac.h"
+#include "context.h"
 #include "macros.h"
 #include "mem.h"
 
 #include "s_texture.h"
 
 
+/*
+ * Init span's Z interpolation values to the RasterPos Z.
+ * Used during setup for glDraw/CopyPixels.
+ */
+void
+_mesa_span_default_z( GLcontext *ctx, struct sw_span *span )
+{
+   if (ctx->Visual.depthBits <= 16)
+      span->z = FloatToFixed(ctx->Current.RasterPos[2] * ctx->DepthMax);
+   else
+      span->z = (GLint) (ctx->Current.RasterPos[2] * ctx->DepthMax);
+   span->zStep = 0;
+   span->interpMask |= SPAN_Z;
+}
+
+
+/*
+ * Init span's fog interpolation values to the RasterPos fog.
+ * Used during setup for glDraw/CopyPixels.
+ */
+void
+_mesa_span_default_fog( GLcontext *ctx, struct sw_span *span )
+{
+   if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT)
+      span->fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterFogCoord);
+   else
+      span->fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterDistance);
+   span->fogStep = 0;
+   span->interpMask |= SPAN_FOG;
+}
+
+
+/*
+ * Init span's color or index interpolation values to the RasterPos color.
+ * Used during setup for glDraw/CopyPixels.
+ */
+void
+_mesa_span_default_color( GLcontext *ctx, struct sw_span *span )
+{
+   if (ctx->Visual.rgbMode) {
+      GLchan r, g, b, a;
+      UNCLAMPED_FLOAT_TO_CHAN(r, ctx->Current.RasterColor[0]);
+      UNCLAMPED_FLOAT_TO_CHAN(g, ctx->Current.RasterColor[1]);
+      UNCLAMPED_FLOAT_TO_CHAN(b, ctx->Current.RasterColor[2]);
+      UNCLAMPED_FLOAT_TO_CHAN(a, ctx->Current.RasterColor[3]);
+#if CHAN_TYPE == GL_FLOAT
+      span->red = r;
+      span->green = g;
+      span->blue = b;
+      span->alpha = a;
+#else
+      span->red   = IntToFixed(r);
+      span->green = IntToFixed(g);
+      span->blue  = IntToFixed(b);
+      span->alpha = IntToFixed(a);
+#endif
+      span->redStep = 0;
+      span->greenStep = 0;
+      span->blueStep = 0;
+      span->alphaStep = 0;
+      span->interpMask |= SPAN_RGBA;
+   }
+   else {
+      span->index = IntToFixed(ctx->Current.RasterIndex);
+      span->indexStep = 0;
+      span->interpMask |= SPAN_INDEX;
+   }
+}
+
+
+/* Fill in the span.color.rgba array from the interpolation values */
+static void
+interpolate_colors(GLcontext *ctx, struct sw_span *span)
+{
+   GLfixed r = span->red;
+   GLfixed g = span->green;
+   GLfixed b = span->blue;
+   GLfixed a = span->alpha;
+   const GLint dr = span->redStep;
+   const GLint dg = span->greenStep;
+   const GLint db = span->blueStep;
+   const GLint da = span->alphaStep;
+   const GLuint n = span->end;
+   GLchan (*rgba)[4] = span->color.rgba;
+   GLuint i;
+
+   ASSERT(span->interpMask & SPAN_RGBA);
+
+   if (span->interpMask & SPAN_FLAT) {
+      /* constant color */
+      GLchan color[4];
+      color[RCOMP] = FixedToChan(r);
+      color[GCOMP] = FixedToChan(g);
+      color[BCOMP] = FixedToChan(b);
+      color[ACOMP] = FixedToChan(a);
+      for (i = 0; i < n; i++) {
+         COPY_CHAN4(span->color.rgba[i], color);
+      }
+   }
+   else {
+      /* interpolate */
+      for (i = 0; i < n; i++) {
+         rgba[i][RCOMP] = FixedToChan(r);
+         rgba[i][GCOMP] = FixedToChan(g);
+         rgba[i][BCOMP] = FixedToChan(b);
+         rgba[i][ACOMP] = FixedToChan(a);
+         r += dr;
+         g += dg;
+         b += db;
+         a += da;
+      }
+   }
+   span->arrayMask |= SPAN_RGBA;
+}
+
+
+/* Fill in the span.color.index array from the interpolation values */
+static void
+interpolate_indexes(GLcontext *ctx, struct sw_span *span)
+{
+   GLfixed index = span->index;
+   const GLint indexStep = span->indexStep;
+   const GLuint n = span->end;
+   GLuint *indexes = span->color.index;
+   GLuint i;
+   ASSERT(span->interpMask & SPAN_INDEX);
+
+   if ((span->interpMask & SPAN_FLAT) || (indexStep == 0)) {
+      /* constant color */
+      index = FixedToInt(index);
+      for (i = 0; i < n; i++) {
+         indexes[i] = index;
+      }
+   }
+   else {
+      /* interpolate */
+      for (i = 0; i < n; i++) {
+         indexes[i] = FixedToInt(index);
+         index += indexStep;
+      }
+   }
+   span->arrayMask |= SPAN_INDEX;
+}
+
+
+/* Fill in the span.specArray array from the interpolation values */
+static void
+interpolate_specular(GLcontext *ctx, struct sw_span *span)
+{
+   if (span->interpMask & SPAN_FLAT) {
+      /* constant color */
+      const GLchan r = FixedToChan(span->specRed);
+      const GLchan g = FixedToChan(span->specGreen);
+      const GLchan b = FixedToChan(span->specBlue);
+      GLuint i;
+      for (i = 0; i < span->end; i++) {
+         span->specArray[i][RCOMP] = r;
+         span->specArray[i][GCOMP] = g;
+         span->specArray[i][BCOMP] = b;
+      }
+   }
+   else {
+      /* interpolate */
+#if CHAN_TYPE == GL_FLOAT
+      GLfloat r = span->specRed;
+      GLfloat g = span->specGreen;
+      GLfloat b = span->specBlue;
+#else
+      GLfixed r = span->specRed;
+      GLfixed g = span->specGreen;
+      GLfixed b = span->specBlue;
+#endif
+      GLuint i;
+      for (i = 0; i < span->end; i++) {
+         span->specArray[i][RCOMP] = FixedToChan(r);
+         span->specArray[i][GCOMP] = FixedToChan(g);
+         span->specArray[i][BCOMP] = FixedToChan(b);
+         r += span->specRedStep;
+         g += span->specGreenStep;
+         b += span->specBlueStep;
+      }
+   }
+   span->arrayMask |= SPAN_SPEC;
+}
+
+
+/* Fill in the span.zArray array from the interpolation values */
+static void
+interpolate_z(GLcontext *ctx, struct sw_span *span)
+{
+   const GLuint n = span->end;
+   GLuint i;
+
+   ASSERT(span->interpMask & SPAN_Z);
+
+   if (ctx->Visual.depthBits <= 16) {
+      GLfixed zval = span->z;
+      for (i = 0; i < n; i++) {
+         span->zArray[i] = FixedToInt(zval);
+         zval += span->zStep;
+      }
+   }
+   else {
+      /* Deep Z buffer, no fixed->int shift */
+      GLfixed zval = span->z;
+      for (i = 0; i < n; i++) {
+         span->zArray[i] = zval;
+         zval += span->zStep;
+      }
+   }
+   span->arrayMask |= SPAN_Z;
+}
+
+
+
+/* Fill in the span.texcoords array from the interpolation values */
+static void
+interpolate_texcoords(GLcontext *ctx, struct sw_span *span)
+{
+   ASSERT(span->interpMask & SPAN_TEXTURE);
+
+   if (ctx->Texture._ReallyEnabled & ~TEXTURE0_ANY) {
+      if (span->interpMask & SPAN_LAMBDA) {
+         /* multitexture, lambda */
+         GLuint u;
+         for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
+            if (ctx->Texture.Unit[u]._ReallyEnabled) {
+               const GLfloat ds = span->texStep[u][0];
+               const GLfloat dt = span->texStep[u][1];
+               const GLfloat dr = span->texStep[u][2];
+               const GLfloat dq = span->texStep[u][3];
+               GLfloat s = span->tex[u][0];
+               GLfloat t = span->tex[u][1];
+               GLfloat r = span->tex[u][2];
+               GLfloat q = span->tex[u][3];
+               GLuint i;
+               for (i = 0; i < span->end; i++) {
+                  const GLfloat invQ = (q == 0.0F) ? 1.0F : (1.0F / q);
+                  span->texcoords[u][i][0] = s * invQ;
+                  span->texcoords[u][i][1] = t * invQ;
+                  span->texcoords[u][i][2] = r * invQ;
+                  span->lambda[u][i] = (GLfloat) 
+                     (log(span->rho[u] * invQ * invQ) * 1.442695F * 0.5F);
+                  s += ds;
+                  t += dt;
+                  r += dr;
+                  q += dq;
+               }
+            }
+         }
+         span->arrayMask |= SPAN_LAMBDA;
+      }
+      else {
+         /* multitexture, no lambda */
+         GLuint u;
+         for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
+            if (ctx->Texture.Unit[u]._ReallyEnabled) {
+               const GLfloat ds = span->texStep[u][0];
+               const GLfloat dt = span->texStep[u][1];
+               const GLfloat dr = span->texStep[u][2];
+               const GLfloat dq = span->texStep[u][3];
+               GLfloat s = span->tex[u][0];
+               GLfloat t = span->tex[u][1];
+               GLfloat r = span->tex[u][2];
+               GLfloat q = span->tex[u][3];
+               GLuint i;
+               for (i = 0; i < span->end; i++) {
+                  const GLfloat invQ = (q == 0.0F) ? 1.0F : (1.0F / q);
+                  span->texcoords[u][i][0] = s * invQ;
+                  span->texcoords[u][i][1] = t * invQ;
+                  span->texcoords[u][i][2] = r * invQ;
+                  s += ds;
+                  t += dt;
+                  r += dr;
+                  q += dq;
+               }
+            }
+         }
+      }
+   }
+   else {
+      if (span->interpMask & SPAN_LAMBDA) {
+         /* just texture unit 0, with lambda */
+         const GLfloat ds = span->texStep[0][0];
+         const GLfloat dt = span->texStep[0][1];
+         const GLfloat dr = span->texStep[0][2];
+         const GLfloat dq = span->texStep[0][3];
+         GLfloat s = span->tex[0][0];
+         GLfloat t = span->tex[0][1];
+         GLfloat r = span->tex[0][2];
+         GLfloat q = span->tex[0][3];
+         GLuint i;
+         for (i = 0; i < span->end; i++) {
+            const GLfloat invQ = (q == 0.0F) ? 1.0F : (1.0F / q);
+            span->texcoords[0][i][0] = s * invQ;
+            span->texcoords[0][i][1] = t * invQ;
+            span->texcoords[0][i][2] = r * invQ;
+            span->lambda[0][i] = (GLfloat)
+               (log(span->rho[0] * invQ * invQ) * 1.442695F * 0.5F);
+            s += ds;
+            t += dt;
+            r += dr;
+            q += dq;
+         }
+         span->arrayMask |= SPAN_LAMBDA;
+      }
+      else {
+         /* just texture 0, witout lambda */
+         const GLfloat ds = span->texStep[0][0];
+         const GLfloat dt = span->texStep[0][1];
+         const GLfloat dr = span->texStep[0][2];
+         const GLfloat dq = span->texStep[0][3];
+         GLfloat s = span->tex[0][0];
+         GLfloat t = span->tex[0][1];
+         GLfloat r = span->tex[0][2];
+         GLfloat q = span->tex[0][3];
+         GLuint i;
+         for (i = 0; i < span->end; i++) {
+            const GLfloat invQ = (q == 0.0F) ? 1.0F : (1.0F / q);
+            span->texcoords[0][i][0] = s * invQ;
+            span->texcoords[0][i][1] = t * invQ;
+            span->texcoords[0][i][2] = r * invQ;
+            s += ds;
+            t += dt;
+            r += dr;
+            q += dq;
+         }
+      }
+   }
+}
 
 
 /*
  * Apply the current polygon stipple pattern to a span of pixels.
  */
-static void stipple_polygon_span( GLcontext *ctx,
-                                  GLuint n, GLint x, GLint y, GLubyte mask[] )
+static void
+stipple_polygon_span( GLcontext *ctx, struct sw_span *span)
 {
    const GLuint highbit = 0x80000000;
    GLuint i, m, stipple;
 
-   stipple = ctx->PolygonStipple[y % 32];
-   m = highbit >> (GLuint) (x % 32);
+   stipple = ctx->PolygonStipple[span->y % 32];
+   m = highbit >> (GLuint) (span->x % 32);
 
-   for (i = 0; i < n; i++) {
+   for (i = 0; i < span->end; i++) {
       if ((m & stipple) == 0) {
-         mask[i] = 0;
+        span->mask[i] = 0;
       }
       m = m >> 1;
       if (m == 0) {
          m = highbit;
       }
    }
+   span->writeAll = GL_FALSE;
 }
 
 
-
 /*
  * Clip a pixel span to the current buffer/window boundaries.
- * Return:  'n' such that pixel 'n', 'n+1' etc. are clipped,
- *           as a special case:
- *           0 = all pixels clipped
+ * Return:   GL_TRUE   some pixel still visible
+ *           GL_FALSE  nothing visible
  */
-static GLuint clip_span( GLcontext *ctx,
-                         GLint n, GLint x, GLint y, GLubyte mask[] )
+static GLuint
+clip_span( GLcontext *ctx, struct sw_span *span)
 {
+   GLint x = span->x, y = span->y, n = span->end;
+
    /* Clip to top and bottom */
    if (y < 0 || y >= ctx->DrawBuffer->Height) {
-      return 0;
+      span->end = 0;
+      return GL_FALSE;
    }
 
    /* Clip to the left */
    if (x < 0) {
       if (x + n <= 0) {
          /* completely off left side */
-         return 0;
+        span->end = 0;
+         return GL_FALSE;
       }
       else {
          /* partially off left side */
-         MEMSET(mask, 0, -x); 
+        span->writeAll = GL_FALSE;
+         BZERO(span->mask, -x * sizeof(GLubyte));
+        return GL_TRUE;
       }
    }
 
@@ -107,15 +443,17 @@ static GLuint clip_span( GLcontext *ctx,
    if (x + n > ctx->DrawBuffer->Width) {
       if (x >= ctx->DrawBuffer->Width) {
          /* completely off right side */
-         return 0;
+        span->end = 0;
+         return GL_FALSE;
       }
       else {
          /* partially off right side */
-         return ctx->DrawBuffer->Width - x;
+         span->end = ctx->DrawBuffer->Width - x;
+        return GL_TRUE;
       }
    }
 
-   return n;
+   return GL_TRUE;
 }
 
 
@@ -123,10 +461,11 @@ static GLuint clip_span( GLcontext *ctx,
 /*
  * Draw to more than one color buffer (or none).
  */
-static void multi_write_index_span( GLcontext *ctx, GLuint n,
-                                    GLint x, GLint y, const GLuint indexes[],
-                                    const GLubyte mask[] )
+static void
+multi_write_index_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
+                        const GLuint indexes[], const GLubyte mask[] )
 {
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
    GLuint bufferBit;
 
    if (ctx->Color.DrawBuffer == GL_NONE)
@@ -152,13 +491,10 @@ static void multi_write_index_span( GLcontext *ctx, GLuint n,
          if (ctx->Color.IndexLogicOpEnabled) {
             _mesa_logicop_ci_span( ctx, n, x, y, indexTmp, mask );
          }
-         if (ctx->Color.IndexMask == 0) {
-            break;
-         }
-         else if (ctx->Color.IndexMask != 0xffffffff) {
+         if (ctx->Color.IndexMask != 0xffffffff) {
             _mesa_mask_index_span( ctx, n, x, y, indexTmp );
          }
-         (*ctx->Driver.WriteCI32Span)( ctx, n, x, y, indexTmp, mask );
+         (*swrast->Driver.WriteCI32Span)( ctx, n, x, y, indexTmp, mask );
       }
    }
 
@@ -167,218 +503,14 @@ static void multi_write_index_span( GLcontext *ctx, GLuint n,
 }
 
 
-
-/*
- * Write a horizontal span of color index pixels to the frame buffer.
- * Stenciling, Depth-testing, etc. are done as needed.
- * Input:  n - number of pixels in the span
- *         x, y - location of leftmost pixel in the span
- *         z - array of [n] z-values
- *         index - array of [n] color indexes
- *         primitive - either GL_POINT, GL_LINE, GL_POLYGON, or GL_BITMAP
- */
-void gl_write_index_span( GLcontext *ctx,
-                          GLuint n, GLint x, GLint y, const GLdepth z[],
-                         const GLfixed fog[],
-                         GLuint indexIn[], GLenum primitive )
-{
-   const GLuint modBits = FOG_BIT | BLEND_BIT | MASKING_BIT | LOGIC_OP_BIT;
-   GLubyte mask[MAX_WIDTH];
-   GLuint indexBackup[MAX_WIDTH];
-   GLuint *index;  /* points to indexIn or indexBackup */
-   SWcontext *swrast = SWRAST_CONTEXT(ctx);
-
-   /* init mask to 1's (all pixels are to be written) */
-   MEMSET(mask, 1, n);
-
-   if ((swrast->_RasterMask & WINCLIP_BIT) || primitive==GL_BITMAP) {
-      if ((n = clip_span(ctx,n,x,y,mask)) == 0) {
-        return;
-      }
-   }
-
-   if ((primitive==GL_BITMAP && (swrast->_RasterMask & modBits))
-       || (swrast->_RasterMask & MULTI_DRAW_BIT)) {
-      /* Make copy of color indexes */
-      MEMCPY( indexBackup, indexIn, n * sizeof(GLuint) );
-      index = indexBackup;
-   }
-   else {
-      index = indexIn;
-   }
-
-
-   /* Do the scissor test */
-   if (ctx->Scissor.Enabled) {
-      if (gl_scissor_span( ctx, n, x, y, mask ) == 0) {
-        return;
-      }
-   }
-
-   /* Polygon Stippling */
-   if (ctx->Polygon.StippleFlag && primitive==GL_POLYGON) {
-      stipple_polygon_span( ctx, n, x, y, mask );
-   }
-
-   if (ctx->Stencil.Enabled) {
-      /* first stencil test */
-      if (_mesa_stencil_and_ztest_span(ctx, n, x, y, z, mask) == GL_FALSE) {
-        return;
-      }
-   }
-   else if (ctx->Depth.Test) {
-      /* regular depth testing */
-      if (_mesa_depth_test_span( ctx, n, x, y, z, mask ) == 0)
-         return;
-   }
-
-   /* if we get here, something passed the depth test */
-   ctx->OcclusionResult = GL_TRUE;
-
-   /* Per-pixel fog */
-   if (ctx->Fog.Enabled) {
-      if (fog && !swrast->_PreferPixelFog)
-         _mesa_fog_ci_pixels( ctx, n, fog, index );
-      else
-         _mesa_depth_fog_ci_pixels( ctx, n, z, index );
-   }
-
-   if (swrast->_RasterMask & MULTI_DRAW_BIT) {
-      /* draw to zero or two or more buffers */
-      multi_write_index_span( ctx, n, x, y, index, mask );
-   }
-   else {
-      /* normal situation: draw to exactly one buffer */
-      if (ctx->Color.IndexLogicOpEnabled) {
-         _mesa_logicop_ci_span( ctx, n, x, y, index, mask );
-      }
-
-      if (ctx->Color.IndexMask == 0) {
-         return;
-      }
-      else if (ctx->Color.IndexMask != 0xffffffff) {
-         _mesa_mask_index_span( ctx, n, x, y, index );
-      }
-
-      /* write pixels */
-      (*ctx->Driver.WriteCI32Span)( ctx, n, x, y, index, mask );
-   }
-}
-
-
-
-
-void gl_write_monoindex_span( GLcontext *ctx,
-                              GLuint n, GLint x, GLint y, 
-                             const GLdepth z[],
-                             const GLfixed fog[],
-                             GLuint index, GLenum primitive )
-{
-   SWcontext *swrast = SWRAST_CONTEXT(ctx);
-   GLubyte mask[MAX_WIDTH];
-   GLuint i;
-
-   /* init mask to 1's (all pixels are to be written) */
-   MEMSET(mask, 1, n);
-
-   if ((swrast->_RasterMask & WINCLIP_BIT) || primitive==GL_BITMAP) {
-      if ((n = clip_span( ctx, n, x, y, mask)) == 0) {
-        return;
-      }
-   }
-
-   /* Do the scissor test */
-   if (ctx->Scissor.Enabled) {
-      if (gl_scissor_span( ctx, n, x, y, mask ) == 0) {
-        return;
-      }
-   }
-
-   /* Polygon Stippling */
-   if (ctx->Polygon.StippleFlag && primitive==GL_POLYGON) {
-      stipple_polygon_span( ctx, n, x, y, mask );
-   }
-
-   if (ctx->Stencil.Enabled) {
-      /* first stencil test */
-      if (_mesa_stencil_and_ztest_span(ctx, n, x, y, z, mask) == GL_FALSE) {
-        return;
-      }
-   }
-   else if (ctx->Depth.Test) {
-      /* regular depth testing */
-      if (_mesa_depth_test_span( ctx, n, x, y, z, mask ) == 0)
-         return;
-   }
-
-   /* if we get here, something passed the depth test */
-   ctx->OcclusionResult = GL_TRUE;
-
-   if (ctx->Color.DrawBuffer == GL_NONE) {
-      /* write no pixels */
-      return;
-   }
-
-   if (ctx->Fog.Enabled
-       || ctx->Color.IndexLogicOpEnabled
-       || ctx->Color.IndexMask != 0xffffffff) {
-      /* different index per pixel */
-      GLuint indexes[MAX_WIDTH];
-      for (i = 0; i < n; i++) {
-        indexes[i] = index;
-      }
-
-      if (ctx->Fog.Enabled) {
-        if (fog && !swrast->_PreferPixelFog)
-           _mesa_fog_ci_pixels( ctx, n, fog, indexes );
-        else
-           _mesa_depth_fog_ci_pixels( ctx, n, z, indexes );
-      }
-
-      if (swrast->_RasterMask & MULTI_DRAW_BIT) {
-         /* draw to zero or two or more buffers */
-         multi_write_index_span( ctx, n, x, y, indexes, mask );
-      }
-      else {
-         /* normal situation: draw to exactly one buffer */
-         if (ctx->Color.IndexLogicOpEnabled) {
-            _mesa_logicop_ci_span( ctx, n, x, y, indexes, mask );
-         }
-         if (ctx->Color.IndexMask == 0) {
-            return;
-         }
-         else if (ctx->Color.IndexMask != 0xffffffff) {
-            _mesa_mask_index_span( ctx, n, x, y, indexes );
-         }
-         (*ctx->Driver.WriteCI32Span)( ctx, n, x, y, indexes, mask );
-      }
-   }
-   else {
-      /* same color index for all pixels */
-      ASSERT(!ctx->Color.IndexLogicOpEnabled);
-      ASSERT(ctx->Color.IndexMask == 0xffffffff);
-      if (swrast->_RasterMask & MULTI_DRAW_BIT) {
-         /* draw to zero or two or more buffers */
-         GLuint indexes[MAX_WIDTH];
-         for (i = 0; i < n; i++)
-            indexes[i] = index;
-         multi_write_index_span( ctx, n, x, y, indexes, mask );
-      }
-      else {
-         /* normal situation: draw to exactly one buffer */
-         (*ctx->Driver.WriteMonoCISpan)( ctx, n, x, y, index, mask );
-      }
-   }
-}
-
-
-
 /*
  * Draw to more than one RGBA color buffer (or none).
+ * All fragment operations, up to (but not) blending/logicop should
+ * have been done first.
  */
-static void multi_write_rgba_span( GLcontext *ctx, GLuint n,
-                                   GLint x, GLint y, CONST GLchan rgba[][4],
-                                   const GLubyte mask[] )
+static void
+multi_write_rgba_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
+                       CONST GLchan rgba[][4], const GLubyte mask[] )
 {
    const GLuint colorMask = *((GLuint *) ctx->Color.ColorMask);
    GLuint bufferBit;
@@ -426,10 +558,10 @@ static void multi_write_rgba_span( GLcontext *ctx, GLuint n,
             _mesa_mask_rgba_span( ctx, n, x, y, rgbaTmp );
          }
 
-         (*ctx->Driver.WriteRGBASpan)( ctx, n, x, y, 
+         (*swrast->Driver.WriteRGBASpan)( ctx, n, x, y,
                                       (const GLchan (*)[4]) rgbaTmp, mask );
          if (swrast->_RasterMask & ALPHABUF_BIT) {
-            _mesa_write_alpha_span( ctx, n, x, y, 
+            _mesa_write_alpha_span( ctx, n, x, y,
                                     (const GLchan (*)[4])rgbaTmp, mask );
          }
       }
@@ -441,608 +573,516 @@ static void multi_write_rgba_span( GLcontext *ctx, GLuint n,
 
 
 
-void gl_write_rgba_span( GLcontext *ctx,
-                         GLuint n, GLint x, GLint y, const GLdepth z[],
-                        const GLfixed *fog,
-                         GLchan rgbaIn[][4],
-                         GLenum primitive )
+/*
+ * This function may modify any of the array values in the span.
+ * span->interpMask and span->arrayMask may be changed but will be restored
+ * to their original values before returning.
+ */
+void
+_mesa_write_index_span( GLcontext *ctx, struct sw_span *span,
+                       GLenum primitive)
 {
-   const GLuint modBits = FOG_BIT | BLEND_BIT | MASKING_BIT |
-                          LOGIC_OP_BIT | TEXTURE_BIT;
-   GLubyte mask[MAX_WIDTH];
-   GLboolean write_all = GL_TRUE;
-   GLchan rgbaBackup[MAX_WIDTH][4];
-   GLchan (*rgba)[4];
-   const GLubyte *Null = 0;
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
+   const GLuint origInterpMask = span->interpMask;
+   const GLuint origArrayMask = span->arrayMask;
+
+   ASSERT((span->interpMask & span->arrayMask) == 0);
 
-   /* init mask to 1's (all pixels are to be written) */
-   MEMSET(mask, 1, n);
+   MEMSET(span->mask, 1, span->end);
+   span->writeAll = GL_TRUE;
 
+   /* Window clipping */
    if ((swrast->_RasterMask & WINCLIP_BIT) || primitive==GL_BITMAP) {
-      if ((n = clip_span( ctx,n,x,y,mask)) == 0) {
+      if (clip_span(ctx,span) == GL_FALSE) {
          return;
       }
-      if (mask[0] == 0)
-         write_all = GL_FALSE;
    }
 
-   if ((primitive==GL_BITMAP && (swrast->_RasterMask & modBits))
-       || (swrast->_RasterMask & MULTI_DRAW_BIT)) {
-      /* must make a copy of the colors since they may be modified */
-      MEMCPY( rgbaBackup, rgbaIn, 4 * n * sizeof(GLchan) );
-      rgba = rgbaBackup;
-   }
-   else {
-      rgba = rgbaIn;
-   }
-
-   /* Do the scissor test */
+   /* Scissor test */
    if (ctx->Scissor.Enabled) {
-      if (gl_scissor_span( ctx, n, x, y, mask ) == 0) {
-        return;
+      if (_mesa_scissor_span( ctx, span ) == GL_FALSE) {
+         return;
       }
-      write_all = GL_FALSE;
    }
 
    /* Polygon Stippling */
    if (ctx->Polygon.StippleFlag && primitive==GL_POLYGON) {
-      stipple_polygon_span( ctx, n, x, y, mask );
-      write_all = GL_FALSE;
+      stipple_polygon_span(ctx, span);
    }
 
-   /* Do the alpha test */
-   if (ctx->Color.AlphaEnabled) {
-      if (_mesa_alpha_test( ctx, n, (const GLchan (*)[4]) rgba, mask ) == 0) {
-        return;
-      }
-      write_all = GL_FALSE;
-   }
+   /* Depth test and stencil */
+   if (ctx->Depth.Test || ctx->Stencil.Enabled) {
+      if (span->interpMask & SPAN_Z)
+         interpolate_z(ctx, span);
 
-   if (ctx->Stencil.Enabled) {
-      /* first stencil test */
-      if (_mesa_stencil_and_ztest_span(ctx, n, x, y, z, mask) == GL_FALSE) {
-        return;
-      }
-      write_all = GL_FALSE;
-   }
-   else if (ctx->Depth.Test) {
-      /* regular depth testing */
-      GLuint m = _mesa_depth_test_span( ctx, n, x, y, z, mask );
-      if (m == 0) {
-         return;
+      if (ctx->Stencil.Enabled) {
+         if (_mesa_stencil_and_ztest_span(ctx, span) == GL_FALSE) {
+            span->arrayMask = origArrayMask;
+            return;
+         }
       }
-      if (m < n) {
-         write_all = GL_FALSE;
+      else {
+         ASSERT(ctx->Depth.Test);
+         if (_mesa_depth_test_span(ctx, span) == 0) {
+            span->arrayMask = origArrayMask;
+            return;
+         }
       }
    }
 
    /* if we get here, something passed the depth test */
    ctx->OcclusionResult = GL_TRUE;
 
-   /* Per-pixel fog */
+   /* we have to wait until after occlusion to do this test */
+   if (ctx->Color.DrawBuffer == GL_NONE || ctx->Color.IndexMask == 0) {
+      /* write no pixels */
+      span->arrayMask = origArrayMask;
+      return;
+   }
+
+   /* Interpolate the color indexes if needed */
+   if (span->interpMask & SPAN_INDEX) {
+      interpolate_indexes(ctx, span);
+      /* clear the bit - this allows the WriteMonoCISpan optimization below */
+      span->interpMask &= ~SPAN_INDEX;
+   }
+
+   /* Fog */
+   /* XXX try to simplify the fog code! */
    if (ctx->Fog.Enabled) {
-      if (fog && !swrast->_PreferPixelFog) 
-        _mesa_fog_rgba_pixels( ctx, n, fog, rgba );
+      if ((span->arrayMask & SPAN_FOG) && !swrast->_PreferPixelFog)
+         _mesa_fog_ci_pixels_with_array( ctx, span, span->fogArray,
+                                         span->color.index);
+      else if ((span->interpMask & SPAN_FOG) && !swrast->_PreferPixelFog)
+         _mesa_fog_ci_pixels( ctx, span, span->color.index);
       else
-        _mesa_depth_fog_rgba_pixels( ctx, n, z, rgba );
+         _mesa_depth_fog_ci_pixels( ctx, span, span->color.index);
+   }
+
+   /* Antialias coverage application */
+   if (span->arrayMask & SPAN_COVERAGE) {
+      GLuint i;
+      GLuint *index = span->color.index;
+      for (i = 0; i < span->end; i++) {
+         ASSERT(span->coverage[i] < 16);
+         index[i] = (index[i] & ~0xf) | ((GLuint) (span->coverage[i]));
+      }
    }
 
    if (swrast->_RasterMask & MULTI_DRAW_BIT) {
-      multi_write_rgba_span( ctx, n, x, y, (const GLchan (*)[4]) rgba, mask );
+      /* draw to zero or two or more buffers */
+      multi_write_index_span( ctx, span->end, span->x, span->y,
+                             span->color.index, span->mask );
    }
    else {
-      /* normal: write to exactly one buffer */
-      /* logic op or blending */
-      const GLuint colorMask = *((GLuint *) ctx->Color.ColorMask);
-
-      if (ctx->Color.ColorLogicOpEnabled) {
-         _mesa_logicop_rgba_span( ctx, n, x, y, rgba, mask );
-      }
-      else if (ctx->Color.BlendEnabled) {
-         _mesa_blend_span( ctx, n, x, y, rgba, mask );
+      /* normal situation: draw to exactly one buffer */
+      if (ctx->Color.IndexLogicOpEnabled) {
+         _mesa_logicop_ci_span( ctx, span->end, span->x, span->y,
+                               span->color.index, span->mask );
       }
 
-      /* Color component masking */
-      if (colorMask == 0x0) {
-         return;
-      }
-      else if (colorMask != 0xffffffff) {
-         _mesa_mask_rgba_span( ctx, n, x, y, rgba );
+      if (ctx->Color.IndexMask != 0xffffffff) {
+         _mesa_mask_index_span( ctx, span->end, span->x, span->y,
+                                span->color.index );
       }
 
       /* write pixels */
-      (*ctx->Driver.WriteRGBASpan)( ctx, n, x, y, 
-                                   (const GLchan (*)[4]) rgba, 
-                                   write_all ? Null : mask );
-
-      if (swrast->_RasterMask & ALPHABUF_BIT) {
-         _mesa_write_alpha_span( ctx, n, x, y, 
-                                 (const GLchan (*)[4]) rgba, 
-                                 write_all ? Null : mask );
+      if ((span->interpMask & SPAN_INDEX) && span->indexStep == 0) {
+         /* all pixels have same color index */
+         (*swrast->Driver.WriteMonoCISpan)( ctx, span->end, span->x, span->y,
+                                            FixedToInt(span->index),
+                                            span->mask );
+      }
+      else {
+         (*swrast->Driver.WriteCI32Span)( ctx, span->end, span->x, span->y,
+                                          span->color.index, span->mask );
       }
    }
-}
 
+   span->interpMask = origInterpMask;
+   span->arrayMask = origArrayMask;
+}
 
 
 /*
- * Write a horizontal span of color pixels to the frame buffer.
- * The color is initially constant for the whole span.
- * Alpha-testing, stenciling, depth-testing, and blending are done as needed.
- * Input:  n - number of pixels in the span
- *         x, y - location of leftmost pixel in the span
- *         z - array of [n] z-values
- *         r, g, b, a - the color of the pixels
- *         primitive - either GL_POINT, GL_LINE, GL_POLYGON or GL_BITMAP.
+ * This function may modify any of the array values in the span.
+ * span->interpMask and span->arrayMask may be changed but will be restored
+ * to their original values before returning.
  */
-void gl_write_monocolor_span( GLcontext *ctx,
-                              GLuint n, GLint x, GLint y, const GLdepth z[],
-                             const GLfixed fog[],
-                             const GLchan color[4],
-                              GLenum primitive )
+void
+_mesa_write_rgba_span( GLcontext *ctx, struct sw_span *span,
+                      GLenum primitive)
 {
-   const GLuint colorMask = *((GLuint *) ctx->Color.ColorMask);
-   GLuint i;
-   GLubyte mask[MAX_WIDTH];
-   GLboolean write_all = GL_TRUE;
-   GLchan rgba[MAX_WIDTH][4];
-   const GLubyte *Null = 0;
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
+   const GLuint colorMask = *((GLuint *) ctx->Color.ColorMask);
+   const GLuint origInterpMask = span->interpMask;
+   const GLuint origArrayMask = span->arrayMask;
+   GLboolean monoColor;
 
-   /* init mask to 1's (all pixels are to be written) */
-   MEMSET(mask, 1, n);
+   ASSERT((span->interpMask & span->arrayMask) == 0);
+   ASSERT((span->interpMask | span->arrayMask) & SPAN_RGBA);
 
-   if ((swrast->_RasterMask & WINCLIP_BIT) || primitive==GL_BITMAP) {
-      if ((n = clip_span( ctx,n,x,y,mask)) == 0) {
-        return;
+   MEMSET(span->mask, 1, span->end);
+   span->writeAll = GL_TRUE;
+
+   /* Determine if we have mono-chromatic colors */
+   monoColor = (span->interpMask & SPAN_RGBA) &&
+      span->redStep == 0 && span->greenStep == 0 &&
+      span->blueStep == 0 && span->alphaStep == 0;
+
+   /* Window clipping */
+   if ((swrast->_RasterMask & WINCLIP_BIT) || primitive == GL_BITMAP) {
+      if (clip_span(ctx, span) == GL_FALSE) {
+         return;
       }
-      if (mask[0] == 0)
-         write_all = GL_FALSE;
    }
 
-   /* Do the scissor test */
+   /* Scissor test */
    if (ctx->Scissor.Enabled) {
-      if (gl_scissor_span( ctx, n, x, y, mask ) == 0) {
-        return;
+      if (!_mesa_scissor_span(ctx, span)) {
+         return;
       }
-      write_all = GL_FALSE;
    }
 
    /* Polygon Stippling */
-   if (ctx->Polygon.StippleFlag && primitive==GL_POLYGON) {
-      stipple_polygon_span( ctx, n, x, y, mask );
-      write_all = GL_FALSE;
+   if (ctx->Polygon.StippleFlag && primitive == GL_POLYGON) {
+      stipple_polygon_span(ctx, span);
    }
 
    /* Do the alpha test */
    if (ctx->Color.AlphaEnabled) {
-      for (i = 0; i < n; i++) {
-         rgba[i][ACOMP] = color[ACOMP];
-      }
-      if (_mesa_alpha_test( ctx, n, (const GLchan (*)[4])rgba, mask ) == 0) {
+      if (!_mesa_alpha_test(ctx, span)) {
+         span->interpMask = origInterpMask;
+         span->arrayMask = origArrayMask;
         return;
       }
-      write_all = GL_FALSE;
    }
 
-   if (ctx->Stencil.Enabled) {
-      /* first stencil test */
-      if (_mesa_stencil_and_ztest_span(ctx, n, x, y, z, mask) == GL_FALSE) {
-        return;
-      }
-      write_all = GL_FALSE;
-   }
-   else if (ctx->Depth.Test) {
-      /* regular depth testing */
-      GLuint m = _mesa_depth_test_span( ctx, n, x, y, z, mask );
-      if (m == 0) {
-         return;
+   /* Stencil and Z testing */
+   if (ctx->Stencil.Enabled || ctx->Depth.Test) {
+      if (span->interpMask & SPAN_Z)
+         interpolate_z(ctx, span);
+
+      if (ctx->Stencil.Enabled) {
+         if (!_mesa_stencil_and_ztest_span(ctx, span)) {
+            span->interpMask = origInterpMask;
+            span->arrayMask = origArrayMask;
+            return;
+         }
       }
-      if (m < n) {
-         write_all = GL_FALSE;
+      else {
+         ASSERT(ctx->Depth.Test);
+         ASSERT(span->arrayMask & SPAN_Z);
+         /* regular depth testing */
+         if (!_mesa_depth_test_span(ctx, span)) {
+            span->interpMask = origInterpMask;
+            span->arrayMask = origArrayMask;
+            return;
+         }
       }
    }
 
    /* if we get here, something passed the depth test */
    ctx->OcclusionResult = GL_TRUE;
 
-   if (ctx->Color.DrawBuffer == GL_NONE) {
-      /* write no pixels */
+   /* can't abort span-writing until after occlusion testing */
+   if (colorMask == 0x0) {
+      span->interpMask = origInterpMask;
+      span->arrayMask = origArrayMask;
       return;
    }
 
-   if (ctx->Color.ColorLogicOpEnabled || colorMask != 0xffffffff ||
-       (swrast->_RasterMask & (BLEND_BIT | FOG_BIT))) {
-      /* assign same color to each pixel */
-      for (i = 0; i < n; i++) {
-        if (mask[i]) {
-            COPY_CHAN4(rgba[i], color);
-        }
-      }
+   /* Now we may need to interpolate the colors */
+   if ((span->interpMask & SPAN_RGBA) && (span->arrayMask & SPAN_RGBA) == 0) {
+      interpolate_colors(ctx, span);
+      /* clear the bit - this allows the WriteMonoCISpan optimization below */
+      span->interpMask &= ~SPAN_RGBA;
+   }
 
-      /* Per-pixel fog */
-      if (ctx->Fog.Enabled) {
-        if (fog && !swrast->_PreferPixelFog)
-           _mesa_fog_rgba_pixels( ctx, n, fog, rgba );
-        else
-           _mesa_depth_fog_rgba_pixels( ctx, n, z, rgba );
+   /* Fog */
+   /* XXX try to simplify the fog code! */
+   if (ctx->Fog.Enabled) {
+      if ((span->arrayMask & SPAN_FOG) && !swrast->_PreferPixelFog)
+        _mesa_fog_rgba_pixels_with_array(ctx, span, span->fogArray,
+                                          span->color.rgba);
+      else if ((span->interpMask & SPAN_FOG) && !swrast->_PreferPixelFog)
+         _mesa_fog_rgba_pixels(ctx, span, span->color.rgba);
+      else {
+         if ((span->interpMask & SPAN_Z) && (span->arrayMask & SPAN_Z) == 0)
+            interpolate_z(ctx, span);
+         _mesa_depth_fog_rgba_pixels(ctx, span, span->color.rgba);
       }
+      monoColor = GL_FALSE;
+   }
 
-      if (swrast->_RasterMask & MULTI_DRAW_BIT) {
-         multi_write_rgba_span( ctx, n, x, y,
-                                (const GLchan (*)[4]) rgba, mask );
+   /* Antialias coverage application */
+   if (span->arrayMask & SPAN_COVERAGE) {
+      GLchan (*rgba)[4] = span->color.rgba;
+      GLuint i;
+      for (i = 0; i < span->end; i++) {
+         rgba[i][ACOMP] = (GLchan) (rgba[i][ACOMP] * span->coverage[i]);
       }
-      else {
-         /* normal: write to exactly one buffer */
-         if (ctx->Color.ColorLogicOpEnabled) {
-            _mesa_logicop_rgba_span( ctx, n, x, y, rgba, mask );
-         }
-         else if (ctx->Color.BlendEnabled) {
-            _mesa_blend_span( ctx, n, x, y, rgba, mask );
-         }
-
-         /* Color component masking */
-         if (colorMask == 0x0) {
-            return;
-         }
-         else if (colorMask != 0xffffffff) {
-            _mesa_mask_rgba_span( ctx, n, x, y, rgba );
-         }
+      monoColor = GL_FALSE;
+   }
 
-         /* write pixels */
-         (*ctx->Driver.WriteRGBASpan)( ctx, n, x, y, 
-                                      (const GLchan (*)[4]) rgba, 
-                                      write_all ? Null : mask );
-         if (swrast->_RasterMask & ALPHABUF_BIT) {
-            _mesa_write_alpha_span( ctx, n, x, y, 
-                                    (const GLchan (*)[4]) rgba, 
-                                    write_all ? Null : mask );
-         }
-      }
+   if (swrast->_RasterMask & MULTI_DRAW_BIT) {
+      multi_write_rgba_span( ctx, span->end, span->x, span->y,
+                             (const GLchan (*)[4]) span->color.rgba,
+                             span->mask );
    }
    else {
-      /* same color for all pixels */
-      ASSERT(!ctx->Color.BlendEnabled);
-      ASSERT(!ctx->Color.ColorLogicOpEnabled);
-
-      if (swrast->_RasterMask & MULTI_DRAW_BIT) {
-         for (i = 0; i < n; i++) {
-            if (mask[i]) {
-               COPY_CHAN4(rgba[i], color);
-            }
-         }
-         multi_write_rgba_span( ctx, n, x, y, 
-                               (const GLchan (*)[4]) rgba, mask );
+      /* normal: write to exactly one buffer */
+      if (ctx->Color.ColorLogicOpEnabled) {
+         _mesa_logicop_rgba_span( ctx, span->end, span->x, span->y,
+                                  span->color.rgba, span->mask );
+         monoColor = GL_FALSE;
+      }
+      else if (ctx->Color.BlendEnabled) {
+         _mesa_blend_span( ctx, span->end, span->x, span->y,
+                           span->color.rgba, span->mask );
+         monoColor = GL_FALSE;
+      }
+
+      /* Color component masking */
+      if (colorMask != 0xffffffff) {
+         _mesa_mask_rgba_span( ctx, span->end, span->x, span->y,
+                               span->color.rgba );
+         monoColor = GL_FALSE;
+      }
+
+      /* write pixels */
+      if (monoColor) {
+         /* all pixels have same color */
+         GLchan color[4];
+         color[RCOMP] = FixedToChan(span->red);
+         color[GCOMP] = FixedToChan(span->green);
+         color[BCOMP] = FixedToChan(span->blue);
+         color[ACOMP] = FixedToChan(span->alpha);
+         (*swrast->Driver.WriteMonoRGBASpan)( ctx, span->end, span->x, span->y,
+                                              color, span->mask);
       }
       else {
-         (*ctx->Driver.WriteMonoRGBASpan)( ctx, n, x, y, color, mask );
-         if (swrast->_RasterMask & ALPHABUF_BIT) {
-            _mesa_write_mono_alpha_span( ctx, n, x, y, (GLchan) color[ACOMP],
-                                         write_all ? Null : mask );
-         }
+         (*swrast->Driver.WriteRGBASpan)( ctx, span->end, span->x, span->y,
+                      (const GLchan (*)[4]) span->color.rgba,
+                      span->writeAll ? ((const GLubyte *) NULL) : span->mask );
+      }
+
+      if (swrast->_RasterMask & ALPHABUF_BIT) {
+         _mesa_write_alpha_span( ctx, span->end, span->x, span->y,
+                      (const GLchan (*)[4]) span->color.rgba,
+                      span->writeAll ? ((const GLubyte *) NULL) : span->mask );
       }
    }
-}
 
+   span->interpMask = origInterpMask;
+   span->arrayMask = origArrayMask;
+}
 
 
 /*
  * Add specular color to base color.  This is used only when
  * GL_LIGHT_MODEL_COLOR_CONTROL = GL_SEPARATE_SPECULAR_COLOR.
  */
-static void add_colors(GLuint n, GLchan rgba[][4], CONST GLchan specular[][4] )
+static void
+add_colors(GLuint n, GLchan rgba[][4], GLchan specular[][4] )
 {
    GLuint i;
    for (i = 0; i < n; i++) {
+#if CHAN_TYPE == GL_FLOAT
+      /* no clamping */
+      rgba[i][RCOMP] += specular[i][RCOMP];
+      rgba[i][GCOMP] += specular[i][GCOMP];
+      rgba[i][BCOMP] += specular[i][BCOMP];
+#else
       GLint r = rgba[i][RCOMP] + specular[i][RCOMP];
       GLint g = rgba[i][GCOMP] + specular[i][GCOMP];
       GLint b = rgba[i][BCOMP] + specular[i][BCOMP];
       rgba[i][RCOMP] = (GLchan) MIN2(r, CHAN_MAX);
       rgba[i][GCOMP] = (GLchan) MIN2(g, CHAN_MAX);
       rgba[i][BCOMP] = (GLchan) MIN2(b, CHAN_MAX);
+#endif
    }
 }
 
 
 /*
- * Write a horizontal span of textured pixels to the frame buffer.
- * The color of each pixel is different.
- * Alpha-testing, stenciling, depth-testing, and blending are done
- * as needed.
- * Input:  n - number of pixels in the span
- *         x, y - location of leftmost pixel in the span
- *         z - array of [n] z-values
- *         s, t - array of (s,t) texture coordinates for each pixel
- *         lambda - array of texture lambda values
- *         rgba - array of [n] color components
- *         primitive - either GL_POINT, GL_LINE, GL_POLYGON or GL_BITMAP.
+ * This function may modify any of the array values in the span.
+ * span->interpMask and span->arrayMask may be changed but will be restored
+ * to their original values before returning.
  */
-void gl_write_texture_span( GLcontext *ctx,
-                            GLuint n, GLint x, GLint y, const GLdepth z[],
-                           const GLfixed fog[],
-                           const GLfloat s[], const GLfloat t[],
-                            const GLfloat u[], GLfloat lambda[],
-                           GLchan rgbaIn[][4], CONST GLchan spec[][4],
-                           GLenum primitive )
+void
+_mesa_write_texture_span( GLcontext *ctx, struct sw_span *span,
+                          GLenum primitive )
 {
    const GLuint colorMask = *((GLuint *) ctx->Color.ColorMask);
-   GLubyte mask[MAX_WIDTH];
-   GLboolean write_all = GL_TRUE;
-   GLchan rgbaBackup[MAX_WIDTH][4];
-   GLchan (*rgba)[4];   /* points to either rgbaIn or rgbaBackup */
-   const GLubyte *Null = 0;
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
+   const GLuint origArrayMask = span->arrayMask;
+
+   ASSERT((span->interpMask & span->arrayMask) == 0);
+   ASSERT(ctx->Texture._ReallyEnabled);
+
+   /*
+   printf("%s()  interp 0x%x  array 0x%x\n", __FUNCTION__, span->interpMask, span->arrayMask);
+   */
 
-   /* init mask to 1's (all pixels are to be written) */
-   MEMSET(mask, 1, n);
+   MEMSET(span->mask, 1, span->end);
+   span->writeAll = GL_TRUE;
 
+   /* clip against window bounds */
    if ((swrast->_RasterMask & WINCLIP_BIT) || primitive==GL_BITMAP) {
-      if ((n=clip_span(ctx, n, x, y, mask)) == 0) {
+      if (clip_span(ctx,span) == GL_FALSE) {
         return;
       }
-      if (mask[0] == 0)
-       write_all = GL_FALSE;
    }
 
-
-   if (primitive==GL_BITMAP || (swrast->_RasterMask & MULTI_DRAW_BIT)) {
-      /* must make a copy of the colors since they may be modified */
-      MEMCPY(rgbaBackup, rgbaIn, 4 * n * sizeof(GLchan));
-      rgba = rgbaBackup;
-   }
-   else {
-      rgba = rgbaIn;
-   }
-
-   /* Do the scissor test */
+   /* Scissor test */
    if (ctx->Scissor.Enabled) {
-      if (gl_scissor_span( ctx, n, x, y, mask ) == 0) {
-        return;
+      if (_mesa_scissor_span( ctx, span ) == GL_FALSE) {
+         return;
       }
-      write_all = GL_FALSE;
    }
 
    /* Polygon Stippling */
    if (ctx->Polygon.StippleFlag && primitive==GL_POLYGON) {
-      stipple_polygon_span( ctx, n, x, y, mask );
-      write_all = GL_FALSE;
+      stipple_polygon_span( ctx, span);
    }
-   
-   /* Texture with alpha test*/
+
+   /* Need texture coordinates now */
+   if ((span->interpMask & SPAN_TEXTURE)
+       && (span->arrayMask & SPAN_TEXTURE) == 0)
+      interpolate_texcoords(ctx, span);
+
+   /* Texture with alpha test */
    if (ctx->Color.AlphaEnabled) {
+
+      /* Now we need the rgba array, fill it in if needed */
+      if ((span->interpMask & SPAN_RGBA) && (span->arrayMask & SPAN_RGBA) == 0)
+         interpolate_colors(ctx, span);
+
       /* Texturing without alpha is done after depth-testing which
-         gives a potential speed-up. */
-      ASSERT(ctx->Texture._ReallyEnabled);
-      _swrast_texture_fragments( ctx, 0, n, s, t, u, lambda, rgba, rgba );
-     
-      /* Do the alpha test */
-      if (_mesa_alpha_test( ctx, n, (const GLchan (*)[4]) rgba, mask ) == 0) {
-         return;
-      }
-      write_all = GL_FALSE;
-   }
+       * gives a potential speed-up.
+       */
+      _swrast_multitexture_fragments( ctx, span );
 
-   if (ctx->Stencil.Enabled) {
-      /* first stencil test */
-      if (_mesa_stencil_and_ztest_span(ctx, n, x, y, z, mask) == GL_FALSE) {
+      /* Do the alpha test */
+      if (!_mesa_alpha_test(ctx, span)) {
+         span->arrayMask = origArrayMask;
         return;
       }
-      write_all = GL_FALSE;
-   }
-   else if (ctx->Depth.Test) {
-      /* regular depth testing */
-      GLuint m = _mesa_depth_test_span( ctx, n, x, y, z, mask );
-      if (m == 0) {
-         return;
-      }
-      if (m < n) {
-         write_all = GL_FALSE;
-      }
    }
 
-   /* if we get here, something passed the depth test */
-   ctx->OcclusionResult = GL_TRUE;
-
-   /* Texture without alpha test */
-   if (! ctx->Color.AlphaEnabled) {
-      ASSERT(ctx->Texture._ReallyEnabled);
-      _swrast_texture_fragments( ctx, 0, n, s, t, u, lambda, rgba, rgba );
-   }
+   /* Stencil and Z testing */
+   if (ctx->Stencil.Enabled || ctx->Depth.Test) {
+      if (span->interpMask & SPAN_Z)
+         interpolate_z(ctx, span);
 
-   /* Add base and specular colors */
-   if (spec && 
-       (ctx->Fog.ColorSumEnabled ||
-       (ctx->Light.Enabled && ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)))
-     add_colors( n, rgba, spec );   /* rgba = rgba + spec */
-
-   /* Per-pixel fog */
-   if (ctx->Fog.Enabled) {
-      if (fog && !swrast->_PreferPixelFog)
-        _mesa_fog_rgba_pixels( ctx, n, fog, rgba );
-      else
-        _mesa_depth_fog_rgba_pixels( ctx, n, z, rgba );
-   }
-
-   if (swrast->_RasterMask & MULTI_DRAW_BIT) {
-      multi_write_rgba_span( ctx, n, x, y, (const GLchan (*)[4]) rgba, mask );
-   }
-   else {
-      /* normal: write to exactly one buffer */
-      if (ctx->Color.ColorLogicOpEnabled) {
-         _mesa_logicop_rgba_span( ctx, n, x, y, rgba, mask );
-      }
-      else  if (ctx->Color.BlendEnabled) {
-         _mesa_blend_span( ctx, n, x, y, rgba, mask );
-      }
-      if (colorMask == 0x0) {
-         return;
-      }
-      else if (colorMask != 0xffffffff) {
-         _mesa_mask_rgba_span( ctx, n, x, y, rgba );
+      if (ctx->Stencil.Enabled) {
+         if (!_mesa_stencil_and_ztest_span(ctx, span)) {
+            span->arrayMask = origArrayMask;
+            return;
+         }
       }
-
-      (*ctx->Driver.WriteRGBASpan)( ctx, n, x, y, (const GLchan (*)[4])rgba,
-                                   write_all ? Null : mask );
-      if (swrast->_RasterMask & ALPHABUF_BIT) {
-         _mesa_write_alpha_span( ctx, n, x, y, (const GLchan (*)[4]) rgba, 
-                                 write_all ? Null : mask );
+      else {
+         ASSERT(ctx->Depth.Test);
+         ASSERT(span->arrayMask & SPAN_Z);
+         /* regular depth testing */
+         if (!_mesa_depth_test_span(ctx, span)) {
+            span->arrayMask = origArrayMask;
+            return;
+         }
       }
    }
-}
-
-
-
-/*
- * As above but perform multiple stages of texture application.
- */
-void
-gl_write_multitexture_span( GLcontext *ctx,
-                            GLuint n, GLint x, GLint y,
-                            const GLdepth z[],
-                           const GLfixed fog[],
-                            CONST GLfloat s[MAX_TEXTURE_UNITS][MAX_WIDTH],
-                            CONST GLfloat t[MAX_TEXTURE_UNITS][MAX_WIDTH],
-                            CONST GLfloat u[MAX_TEXTURE_UNITS][MAX_WIDTH],
-                            GLfloat lambda[][MAX_WIDTH],
-                            GLchan rgbaIn[MAX_TEXTURE_UNITS][4],
-                            CONST GLchan spec[MAX_TEXTURE_UNITS][4],
-                            GLenum primitive )
-{
-   GLubyte mask[MAX_WIDTH];
-   GLboolean write_all = GL_TRUE;
-   GLchan rgbaBackup[MAX_WIDTH][4];
-   GLchan (*rgba)[4];   /* points to either rgbaIn or rgbaBackup */
-   GLuint i;
-   const GLubyte *Null = 0;
-   const GLuint texUnits = ctx->Const.MaxTextureUnits;
-   SWcontext *swrast = SWRAST_CONTEXT(ctx);
 
-   /* init mask to 1's (all pixels are to be written) */
-   MEMSET(mask, 1, n);
+   /* if we get here, some fragments passed the depth test */
+   ctx->OcclusionResult = GL_TRUE;
 
-   if ((swrast->_RasterMask & WINCLIP_BIT) || primitive==GL_BITMAP) {
-      if ((n=clip_span(ctx, n, x, y, mask)) == 0) {
-        return;
-      }
-      if (mask[0] == 0)
-       write_all = GL_FALSE;
+   /* We had to wait until now to check for glColorMask(F,F,F,F) because of
+    * the occlusion test.
+    */
+   if (colorMask == 0x0) {
+      span->arrayMask = origArrayMask;
+      return;
    }
 
+   /* Texture without alpha test */
+   if (!ctx->Color.AlphaEnabled) {
 
-   if (primitive==GL_BITMAP || (swrast->_RasterMask & MULTI_DRAW_BIT)
-                            || texUnits > 1) {
-      /* must make a copy of the colors since they may be modified */
-      MEMCPY(rgbaBackup, rgbaIn, 4 * n * sizeof(GLchan));
-      rgba = rgbaBackup;
-   }
-   else {
-      rgba = rgbaIn;
-   }
+      /* Now we need the rgba array, fill it in if needed */
+      if ((span->interpMask & SPAN_RGBA) && (span->arrayMask & SPAN_RGBA) == 0)
+         interpolate_colors(ctx, span);
 
-   /* Do the scissor test */
-   if (ctx->Scissor.Enabled) {
-      if (gl_scissor_span( ctx, n, x, y, mask ) == 0) {
-        return;
-      }
-      write_all = GL_FALSE;
+      _swrast_multitexture_fragments( ctx, span );
    }
 
-   /* Polygon Stippling */
-   if (ctx->Polygon.StippleFlag && primitive==GL_POLYGON) {
-      stipple_polygon_span( ctx, n, x, y, mask );
-      write_all = GL_FALSE;
-   }
+   ASSERT(span->arrayMask & SPAN_RGBA);
 
-   /* Texture with alpha test*/
-   if (ctx->Color.AlphaEnabled) {
-      /* Texturing without alpha is done after depth-testing which
-       * gives a potential speed-up.
-       */
-      ASSERT(ctx->Texture._ReallyEnabled);
-      for (i = 0; i < texUnits; i++)
-         _swrast_texture_fragments( ctx, i, n, s[i], t[i], u[i],
-                                    lambda[i], rgbaIn, rgba );
-     
-      /* Do the alpha test */
-      if (_mesa_alpha_test( ctx, n, (const GLchan (*)[4])rgba, mask ) == 0) {
-         return;
+   /* Add base and specular colors */
+   if (ctx->Fog.ColorSumEnabled ||
+       (ctx->Light.Enabled &&
+        ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)) {
+      if (span->interpMask & SPAN_SPEC) {
+         interpolate_specular(ctx, span);
       }
-      write_all = GL_FALSE;
+      ASSERT(span->arrayMask & SPAN_SPEC);
+      add_colors( span->end, span->color.rgba, span->specArray );
    }
 
-   if (ctx->Stencil.Enabled) {
-      /* first stencil test */
-      if (_mesa_stencil_and_ztest_span(ctx, n, x, y, z, mask) == GL_FALSE) {
-        return;
-      }
-      write_all = GL_FALSE;
-   }
-   else if (ctx->Depth.Test) {
-      /* regular depth testing */
-      GLuint m = _mesa_depth_test_span( ctx, n, x, y, z, mask );
-      if (m == 0) {
-         return;
-      }
-      if (m < n) {
-         write_all = GL_FALSE;
+   /* Fog */
+   /* XXX try to simplify the fog code! */
+   if (ctx->Fog.Enabled) {
+      if ((span->arrayMask & SPAN_FOG) && !swrast->_PreferPixelFog)
+        _mesa_fog_rgba_pixels_with_array( ctx, span, span->fogArray,
+                                           span->color.rgba);
+      else if ((span->interpMask & SPAN_FOG)  &&  !swrast->_PreferPixelFog)
+        _mesa_fog_rgba_pixels( ctx, span, span->color.rgba );
+      else {
+         if ((span->interpMask & SPAN_Z) && (span->arrayMask & SPAN_Z) == 0)
+            interpolate_z(ctx, span);
+        _mesa_depth_fog_rgba_pixels(ctx, span, span->color.rgba);
       }
    }
 
-   /* if we get here, something passed the depth test */
-   ctx->OcclusionResult = GL_TRUE;
-
-   /* Texture without alpha test */
-   if (! ctx->Color.AlphaEnabled) {
-      ASSERT(ctx->Texture._ReallyEnabled);
-      for (i = 0; i < texUnits; i++)
-         _swrast_texture_fragments( ctx, i, n, s[i], t[i], u[i],
-                                    lambda[i], rgbaIn, rgba );
-   }
-
-   /* Add base and specular colors */
-   if (spec && 
-       (ctx->Fog.ColorSumEnabled ||
-       (ctx->Light.Enabled && 
-        ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)))
-      add_colors( n, rgba, spec );   /* rgba = rgba + spec */
-
-   /* Per-pixel fog */
-   if (ctx->Fog.Enabled) {
-      if (fog && !swrast->_PreferPixelFog)
-        _mesa_fog_rgba_pixels( ctx, n, fog, rgba );
-      else
-        _mesa_depth_fog_rgba_pixels( ctx, n, z, rgba );
+   /* Antialias coverage application */
+   if (span->arrayMask & SPAN_COVERAGE) {
+      GLchan (*rgba)[4] = span->color.rgba;
+      GLuint i;
+      for (i = 0; i < span->end; i++) {
+         rgba[i][ACOMP] = (GLchan) (rgba[i][ACOMP] * span->coverage[i]);
+      }
    }
 
    if (swrast->_RasterMask & MULTI_DRAW_BIT) {
-      multi_write_rgba_span( ctx, n, x, y, (const GLchan (*)[4]) rgba, mask );
+      multi_write_rgba_span( ctx, span->end, span->x, span->y,
+                             (const GLchan (*)[4]) span->color.rgba,
+                             span->mask );
    }
    else {
       /* normal: write to exactly one buffer */
-      const GLuint colorMask = *((GLuint *) ctx->Color.ColorMask);
-
       if (ctx->Color.ColorLogicOpEnabled) {
-         _mesa_logicop_rgba_span( ctx, n, x, y, rgba, mask );
+         _mesa_logicop_rgba_span( ctx, span->end, span->x, span->y,
+                                  span->color.rgba, span->mask );
       }
       else  if (ctx->Color.BlendEnabled) {
-         _mesa_blend_span( ctx, n, x, y, rgba, mask );
+         _mesa_blend_span( ctx, span->end, span->x, span->y,
+                           span->color.rgba, span->mask);
       }
 
-      if (colorMask == 0x0) {
-         return;
-      }
-      else if (colorMask != 0xffffffff) {
-         _mesa_mask_rgba_span( ctx, n, x, y, rgba );
+      if (colorMask != 0xffffffff) {
+         _mesa_mask_rgba_span( ctx, span->end, span->x, span->y,
+                               span->color.rgba );
       }
 
-      (*ctx->Driver.WriteRGBASpan)( ctx, n, x, y, (const GLchan (*)[4])rgba,
-                                    write_all ? Null : mask );
+      (*swrast->Driver.WriteRGBASpan)( ctx, span->end, span->x, span->y,
+                                       (const GLchan (*)[4]) span->color.rgba,
+                                       span->writeAll ? NULL : span->mask );
       if (swrast->_RasterMask & ALPHABUF_BIT) {
-         _mesa_write_alpha_span( ctx, n, x, y, (const GLchan (*)[4])rgba,
-                                 write_all ? Null : mask );
+         _mesa_write_alpha_span( ctx, span->end, span->x, span->y,
+                                 (const GLchan (*)[4]) span->color.rgba,
+                                 span->writeAll ? NULL : span->mask );
       }
    }
+
+   span->arrayMask = origArrayMask;
 }
 
 
@@ -1051,10 +1091,11 @@ gl_write_multitexture_span( GLcontext *ctx,
  * Read RGBA pixels from frame buffer.  Clipping will be done to prevent
  * reading ouside the buffer's boundaries.
  */
-void gl_read_rgba_span( GLcontext *ctx, GLframebuffer *buffer,
-                        GLuint n, GLint x, GLint y,
-                        GLchan rgba[][4] )
+void
+_mesa_read_rgba_span( GLcontext *ctx, GLframebuffer *buffer,
+                      GLuint n, GLint x, GLint y, GLchan rgba[][4] )
 {
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
    if (y < 0 || y >= buffer->Height
        || x + (GLint) n < 0 || x >= buffer->Width) {
       /* completely above, below, or right */
@@ -1090,7 +1131,7 @@ void gl_read_rgba_span( GLcontext *ctx, GLframebuffer *buffer,
          length = (GLint) n;
       }
 
-      (*ctx->Driver.ReadRGBASpan)( ctx, length, x + skip, y, rgba + skip );
+      (*swrast->Driver.ReadRGBASpan)( ctx, length, x + skip, y, rgba + skip );
       if (buffer->UseSoftwareAlphaBuffers) {
          _mesa_read_alpha_span( ctx, length, x + skip, y, rgba + skip );
       }
@@ -1104,9 +1145,11 @@ void gl_read_rgba_span( GLcontext *ctx, GLframebuffer *buffer,
  * Read CI pixels from frame buffer.  Clipping will be done to prevent
  * reading ouside the buffer's boundaries.
  */
-void gl_read_index_span( GLcontext *ctx, GLframebuffer *buffer,
-                         GLuint n, GLint x, GLint y, GLuint indx[] )
+void
+_mesa_read_index_span( GLcontext *ctx, GLframebuffer *buffer,
+                       GLuint n, GLint x, GLint y, GLuint indx[] )
 {
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
    if (y < 0 || y >= buffer->Height
        || x + (GLint) n < 0 || x >= buffer->Width) {
       /* completely above, below, or right */
@@ -1141,6 +1184,6 @@ void gl_read_index_span( GLcontext *ctx, GLframebuffer *buffer,
          length = (GLint) n;
       }
 
-      (*ctx->Driver.ReadCI32Span)( ctx, length, skip + x, y, indx + skip );
+      (*swrast->Driver.ReadCI32Span)( ctx, length, skip + x, y, indx + skip );
    }
 }