switch to using driFillInModes fix depthbuffer = 0
[mesa.git] / src / mesa / swrast / s_span.c
index 4ed37c66040dcaec39d947e92c7c74c4f78aa217..41db42e2b80726689fe1b1fdf39acdb224fab2f3 100644 (file)
@@ -1,10 +1,8 @@
-/* $Id: s_span.c,v 1.35 2002/02/17 01:49:31 brianp Exp $ */
-
 /*
  * Mesa 3-D graphics library
- * Version:  4.1
+ * Version:  6.1
  *
- * Copyright (C) 1999-2002  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2004  Brian Paul   All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -36,7 +34,7 @@
 #include "colormac.h"
 #include "context.h"
 #include "macros.h"
-#include "mem.h"
+#include "imports.h"
 
 #include "s_alpha.h"
 #include "s_alphabuf.h"
@@ -46,6 +44,7 @@
 #include "s_fog.h"
 #include "s_logic.h"
 #include "s_masking.h"
+#include "s_nvfragprog.h"
 #include "s_span.h"
 #include "s_stencil.h"
 #include "s_texture.h"
  * Used during setup for glDraw/CopyPixels.
  */
 void
-_mesa_span_default_z( GLcontext *ctx, struct sw_span *span )
+_swrast_span_default_z( GLcontext *ctx, struct sw_span *span )
 {
    if (ctx->Visual.depthBits <= 16)
-      span->z = FloatToFixed(ctx->Current.RasterPos[2] * ctx->DepthMax);
+      span->z = FloatToFixed(ctx->Current.RasterPos[2] * ctx->DepthMax + 0.5F);
    else
-      span->z = (GLint) (ctx->Current.RasterPos[2] * ctx->DepthMax);
+      span->z = (GLint) (ctx->Current.RasterPos[2] * ctx->DepthMax + 0.5F);
    span->zStep = 0;
    span->interpMask |= SPAN_Z;
 }
@@ -72,10 +71,10 @@ _mesa_span_default_z( GLcontext *ctx, struct sw_span *span )
  * Used during setup for glDraw/CopyPixels.
  */
 void
-_mesa_span_default_fog( GLcontext *ctx, struct sw_span *span )
+_swrast_span_default_fog( GLcontext *ctx, struct sw_span *span )
 {
-   span->fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterDistance);
-   span->fogStep = 0;
+   span->fog = _swrast_z_to_fogfactor(ctx, ctx->Current.RasterDistance);
+   span->fogStep = span->dfogdx = span->dfogdy = 0.0F;
    span->interpMask |= SPAN_FOG;
 }
 
@@ -85,7 +84,7 @@ _mesa_span_default_fog( GLcontext *ctx, struct sw_span *span )
  * Used during setup for glDraw/CopyPixels.
  */
 void
-_mesa_span_default_color( GLcontext *ctx, struct sw_span *span )
+_swrast_span_default_color( GLcontext *ctx, struct sw_span *span )
 {
    if (ctx->Visual.rgbMode) {
       GLchan r, g, b, a;
@@ -111,44 +110,83 @@ _mesa_span_default_color( GLcontext *ctx, struct sw_span *span )
       span->interpMask |= SPAN_RGBA;
    }
    else {
-      span->index = IntToFixed(ctx->Current.RasterIndex);
+      span->index = FloatToFixed(ctx->Current.RasterIndex);
       span->indexStep = 0;
       span->interpMask |= SPAN_INDEX;
    }
 }
 
 
+/**
+ * Init span's texcoord interpolation values to the RasterPos texcoords.
+ * Used during setup for glDraw/CopyPixels.
+ */
+void
+_swrast_span_default_texcoords( GLcontext *ctx, struct sw_span *span )
+{
+   GLuint i;
+   for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
+      const GLfloat *tc = ctx->Current.RasterTexCoords[i];
+      if (tc[3] > 0.0F) {
+         /* use (s/q, t/q, r/q, 1) */
+         span->tex[i][0] = tc[0] / tc[3];
+         span->tex[i][1] = tc[1] / tc[3];
+         span->tex[i][2] = tc[2] / tc[3];
+         span->tex[i][3] = 1.0;
+      }
+      else {
+         ASSIGN_4V(span->tex[i], 0.0F, 0.0F, 0.0F, 1.0F);
+      }
+      ASSIGN_4V(span->texStepX[i], 0.0F, 0.0F, 0.0F, 0.0F);
+      ASSIGN_4V(span->texStepY[i], 0.0F, 0.0F, 0.0F, 0.0F);
+   }
+   span->interpMask |= SPAN_TEXTURE;
+}
+
+
 /* 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;
+   GLchan (*rgba)[4] = span->array->rgba;
    GLuint i;
 
-   ASSERT(span->interpMask & SPAN_RGBA);
+   ASSERT((span->interpMask & SPAN_RGBA)  &&
+         !(span->arrayMask & 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);
+      color[RCOMP] = FixedToChan(span->red);
+      color[GCOMP] = FixedToChan(span->green);
+      color[BCOMP] = FixedToChan(span->blue);
+      color[ACOMP] = FixedToChan(span->alpha);
       for (i = 0; i < n; i++) {
-         COPY_CHAN4(span->color.rgba[i], color);
+         COPY_CHAN4(span->array->rgba[i], color);
       }
    }
    else {
       /* interpolate */
+#if CHAN_TYPE == GL_FLOAT
+      GLfloat r = span->red;
+      GLfloat g = span->green;
+      GLfloat b = span->blue;
+      GLfloat a = span->alpha;
+      const GLfloat dr = span->redStep;
+      const GLfloat dg = span->greenStep;
+      const GLfloat db = span->blueStep;
+      const GLfloat da = span->alphaStep;
+#else
+      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;
+#endif
       for (i = 0; i < n; i++) {
          rgba[i][RCOMP] = FixedToChan(r);
          rgba[i][GCOMP] = FixedToChan(g);
@@ -171,9 +209,10 @@ 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 *indexes = span->array->index;
    GLuint i;
-   ASSERT(span->interpMask & SPAN_INDEX);
+   ASSERT((span->interpMask & SPAN_INDEX)  &&
+         !(span->arrayMask & SPAN_INDEX));
 
    if ((span->interpMask & SPAN_FLAT) || (indexStep == 0)) {
       /* constant color */
@@ -193,7 +232,7 @@ interpolate_indexes(GLcontext *ctx, struct sw_span *span)
 }
 
 
-/* Fill in the span.specArray array from the interpolation values */
+/* Fill in the span.->array->spec array from the interpolation values */
 static void
 interpolate_specular(GLcontext *ctx, struct sw_span *span)
 {
@@ -204,9 +243,9 @@ interpolate_specular(GLcontext *ctx, struct sw_span *span)
       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;
+         span->array->spec[i][RCOMP] = r;
+         span->array->spec[i][GCOMP] = g;
+         span->array->spec[i][BCOMP] = b;
       }
    }
    else {
@@ -222,9 +261,9 @@ interpolate_specular(GLcontext *ctx, struct sw_span *span)
 #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);
+         span->array->spec[i][RCOMP] = FixedToChan(r);
+         span->array->spec[i][GCOMP] = FixedToChan(g);
+         span->array->spec[i][BCOMP] = FixedToChan(b);
          r += span->specRedStep;
          g += span->specGreenStep;
          b += span->specBlueStep;
@@ -235,26 +274,29 @@ interpolate_specular(GLcontext *ctx, struct sw_span *span)
 
 
 /* Fill in the span.zArray array from the interpolation values */
-static void
-interpolate_z(GLcontext *ctx, struct sw_span *span)
+void
+_swrast_span_interpolate_z( const GLcontext *ctx, struct sw_span *span )
 {
    const GLuint n = span->end;
    GLuint i;
 
-   ASSERT(span->interpMask & SPAN_Z);
+   ASSERT((span->interpMask & SPAN_Z)  &&
+         !(span->arrayMask & SPAN_Z));
 
    if (ctx->Visual.depthBits <= 16) {
       GLfixed zval = span->z;
+      GLdepth *z = span->array->z; 
       for (i = 0; i < n; i++) {
-         span->zArray[i] = FixedToInt(zval);
+         z[i] = FixedToInt(zval);
          zval += span->zStep;
       }
    }
    else {
       /* Deep Z buffer, no fixed->int shift */
       GLfixed zval = span->z;
+      GLdepth *z = span->array->z;
       for (i = 0; i < n; i++) {
-         span->zArray[i] = zval;
+         z[i] = zval;
          zval += span->zStep;
       }
    }
@@ -263,226 +305,331 @@ interpolate_z(GLcontext *ctx, struct sw_span *span)
 
 
 /*
- * Return log_base_2(x) / 2.
- * We divide by two here since we didn't square rho in the triangle function.
+ * This the ideal solution, as given in the OpenGL spec.
  */
-#ifdef USE_IEEE
-
 #if 0
-/* This is pretty fast, but not accurate enough (only 2 fractional bits).
- * Based on code from http://www.stereopsis.com/log2.html
- */
-static INLINE GLfloat HALF_LOG2(GLfloat x)
+static GLfloat
+compute_lambda(GLfloat dsdx, GLfloat dsdy, GLfloat dtdx, GLfloat dtdy,
+               GLfloat dqdx, GLfloat dqdy, GLfloat texW, GLfloat texH,
+               GLfloat s, GLfloat t, GLfloat q, GLfloat invQ)
 {
-   const GLfloat y = x * x * x * x;
-   const GLuint ix = *((GLuint *) &y);
-   const GLuint exp = (ix >> 23) & 0xFF;
-   const GLint log2 = ((GLint) exp) - 127;
-   return (GLfloat) log2 * (0.5 / 4.0);  /* 4, because of x^4 above */
+   GLfloat dudx = texW * ((s + dsdx) / (q + dqdx) - s * invQ);
+   GLfloat dvdx = texH * ((t + dtdx) / (q + dqdx) - t * invQ);
+   GLfloat dudy = texW * ((s + dsdy) / (q + dqdy) - s * invQ);
+   GLfloat dvdy = texH * ((t + dtdy) / (q + dqdy) - t * invQ);
+   GLfloat x = SQRTF(dudx * dudx + dvdx * dvdx);
+   GLfloat y = SQRTF(dudy * dudy + dvdy * dvdy);
+   GLfloat rho = MAX2(x, y);
+   GLfloat lambda = LOG2(rho);
+   return lambda;
 }
 #endif
 
-/* Pretty fast, and accurate.
- * Based on code from http://www.flipcode.com/totd/
+
+/*
+ * This is a faster approximation
  */
-static INLINE GLfloat HALF_LOG2(GLfloat val)
+GLfloat
+_swrast_compute_lambda(GLfloat dsdx, GLfloat dsdy, GLfloat dtdx, GLfloat dtdy,
+                     GLfloat dqdx, GLfloat dqdy, GLfloat texW, GLfloat texH,
+                     GLfloat s, GLfloat t, GLfloat q, GLfloat invQ)
 {
-   GLint *exp_ptr = (GLint *) &val;
-   GLint x = *exp_ptr;
-   const GLint log_2 = ((x >> 23) & 255) - 128;
-   x &= ~(255 << 23);
-   x += 127 << 23;
-   *exp_ptr = x;
-   val = ((-1.0f/3) * val + 2) * val - 2.0f/3;
-   return 0.5F * (val + log_2);
+   GLfloat dsdx2 = (s + dsdx) / (q + dqdx) - s * invQ;
+   GLfloat dtdx2 = (t + dtdx) / (q + dqdx) - t * invQ;
+   GLfloat dsdy2 = (s + dsdy) / (q + dqdy) - s * invQ;
+   GLfloat dtdy2 = (t + dtdy) / (q + dqdy) - t * invQ;
+   GLfloat maxU, maxV, rho, lambda;
+   dsdx2 = FABSF(dsdx2);
+   dsdy2 = FABSF(dsdy2);
+   dtdx2 = FABSF(dtdx2);
+   dtdy2 = FABSF(dtdy2);
+   maxU = MAX2(dsdx2, dsdy2) * texW;
+   maxV = MAX2(dtdx2, dtdy2) * texH;
+   rho = MAX2(maxU, maxV);
+   lambda = LOG2(rho);
+   return lambda;
 }
 
-#else /* USE_IEEE */
-
-/* Slow, portable solution.
- * NOTE: log_base_2(x) = log(x) / log(2)
- * NOTE: 1.442695 = 1/log(2).
- */
-#define HALF_LOG2(x)  ((GLfloat) (log(x) * (1.442695F * 0.5F)))
-
-#endif /* USE_IEEE */
-
 
-
-/*
+/**
  * Fill in the span.texcoords array from the interpolation values.
- * XXX We could optimize here for the case when dq = 0.  That would
- * usually be the case when using an orthographic projection.
+ * Note: in the places where we divide by Q (or mult by invQ) we're
+ * really doing two things: perspective correction and texcoord
+ * projection.  Remember, for texcoord (s,t,r,q) we need to index
+ * texels with (s/q, t/q, r/q).
+ * If we're using a fragment program, we never do the division
+ * for texcoord projection.  That's done by the TXP instruction
+ * or user-written code.
  */
 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 rho = span->rho[u];
-               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];
+   ASSERT(!(span->arrayMask & SPAN_TEXTURE));
+
+   if (ctx->Texture._EnabledCoordUnits > 1) {
+      /* multitexture */
+      GLuint u;
+      span->arrayMask |= SPAN_TEXTURE;
+      for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
+         if (ctx->Texture._EnabledCoordUnits & (1 << u)) {
+            const struct gl_texture_object *obj =ctx->Texture.Unit[u]._Current;
+            GLfloat texW, texH;
+            GLboolean needLambda;
+            if (obj) {
+               const struct gl_texture_image *img = obj->Image[0][obj->BaseLevel];
+               needLambda = (obj->MinFilter != obj->MagFilter)
+                  || ctx->FragmentProgram._Enabled;
+               texW = img->WidthScale;
+               texH = img->HeightScale;
+            }
+            else {
+               /* using a fragment program */
+               texW = 1.0;
+               texH = 1.0;
+               needLambda = GL_FALSE;
+            }
+            if (needLambda) {
+               GLfloat (*texcoord)[4] = span->array->texcoords[u];
+               GLfloat *lambda = span->array->lambda[u];
+               const GLfloat dsdx = span->texStepX[u][0];
+               const GLfloat dsdy = span->texStepY[u][0];
+               const GLfloat dtdx = span->texStepX[u][1];
+               const GLfloat dtdy = span->texStepY[u][1];
+               const GLfloat drdx = span->texStepX[u][2];
+               const GLfloat dqdx = span->texStepX[u][3];
+               const GLfloat dqdy = span->texStepY[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;
-               if (dq == 0.0) {
-                  /* Ortho projection or polygon's parallel to window X axis */
-                  const GLfloat invQ = (q == 0.0F) ? 1.0F : (1.0F / q);
-                  const GLfloat lambda = HALF_LOG2(rho * invQ * invQ);
+               if (ctx->FragmentProgram._Enabled) {
+                  /* do perspective correction but don't divide s, t, r by q */
+                  const GLfloat dwdx = span->dwdx;
+                  GLfloat w = span->w;
                   for (i = 0; i < span->end; i++) {
-                     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] = lambda;
-                     s += ds;
-                     t += dt;
-                     r += dr;
+                     const GLfloat invW = 1.0F / w;
+                     texcoord[i][0] = s * invW;
+                     texcoord[i][1] = t * invW;
+                     texcoord[i][2] = r * invW;
+                     texcoord[i][3] = q * invW;
+                     lambda[i] = _swrast_compute_lambda(dsdx, dsdy, dtdx, dtdy,
+                                                        dqdx, dqdy, texW, texH,
+                                                        s, t, q, invW);
+                     s += dsdx;
+                     t += dtdx;
+                     r += drdx;
+                     q += dqdx;
+                     w += dwdx;
                   }
+
                }
                else {
                   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] = HALF_LOG2(rho * invQ * invQ);
-                     s += ds;
-                     t += dt;
-                     r += dr;
-                     q += dq;
+                     texcoord[i][0] = s * invQ;
+                     texcoord[i][1] = t * invQ;
+                     texcoord[i][2] = r * invQ;
+                     texcoord[i][3] = q;
+                     lambda[i] = _swrast_compute_lambda(dsdx, dsdy, dtdx, dtdy,
+                                                        dqdx, dqdy, texW, texH,
+                                                        s, t, q, invQ);
+                     s += dsdx;
+                     t += dtdx;
+                     r += drdx;
+                     q += dqdx;
                   }
                }
+               span->arrayMask |= SPAN_LAMBDA;
             }
-         }
-         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];
+            else {
+               GLfloat (*texcoord)[4] = span->array->texcoords[u];
+               GLfloat *lambda = span->array->lambda[u];
+               const GLfloat dsdx = span->texStepX[u][0];
+               const GLfloat dtdx = span->texStepX[u][1];
+               const GLfloat drdx = span->texStepX[u][2];
+               const GLfloat dqdx = span->texStepX[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;
-               if (dq == 0.0) {
+               if (ctx->FragmentProgram._Enabled) {
+                  /* do perspective correction but don't divide s, t, r by q */
+                  const GLfloat dwdx = span->dwdx;
+                  GLfloat w = span->w;
+                  for (i = 0; i < span->end; i++) {
+                     const GLfloat invW = 1.0F / w;
+                     texcoord[i][0] = s * invW;
+                     texcoord[i][1] = t * invW;
+                     texcoord[i][2] = r * invW;
+                     texcoord[i][3] = q * invW;
+                     lambda[i] = 0.0;
+                     s += dsdx;
+                     t += dtdx;
+                     r += drdx;
+                     q += dqdx;
+                     w += dwdx;
+                  }
+               }
+               else if (dqdx == 0.0F) {
                   /* Ortho projection or polygon's parallel to window X axis */
                   const GLfloat invQ = (q == 0.0F) ? 1.0F : (1.0F / q);
                   for (i = 0; i < span->end; i++) {
-                     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;
+                     texcoord[i][0] = s * invQ;
+                     texcoord[i][1] = t * invQ;
+                     texcoord[i][2] = r * invQ;
+                     texcoord[i][3] = q;
+                     lambda[i] = 0.0;
+                     s += dsdx;
+                     t += dtdx;
+                     r += drdx;
                   }
                }
                else {
                   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;
+                     texcoord[i][0] = s * invQ;
+                     texcoord[i][1] = t * invQ;
+                     texcoord[i][2] = r * invQ;
+                     texcoord[i][3] = q;
+                     lambda[i] = 0.0;
+                     s += dsdx;
+                     t += dtdx;
+                     r += drdx;
+                     q += dqdx;
                   }
                }
-            }
-         }
-      }
+            } /* lambda */
+         } /* if */
+      } /* for */
    }
    else {
-      if (span->interpMask & SPAN_LAMBDA) {
+      /* single texture */
+      const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current;
+      GLfloat texW, texH;
+      GLboolean needLambda;
+      if (obj) {
+         const struct gl_texture_image *img = obj->Image[0][obj->BaseLevel];
+         needLambda = (obj->MinFilter != obj->MagFilter)
+            || ctx->FragmentProgram._Enabled;
+         texW = (GLfloat) img->WidthScale;
+         texH = (GLfloat) img->HeightScale;
+      }
+      else {
+         needLambda = GL_FALSE;
+         texW = texH = 1.0;
+      }
+      span->arrayMask |= SPAN_TEXTURE;
+      if (needLambda) {
          /* just texture unit 0, with lambda */
-         const GLfloat rho = span->rho[0];
-         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 (*texcoord)[4] = span->array->texcoords[0];
+         GLfloat *lambda = span->array->lambda[0];
+         const GLfloat dsdx = span->texStepX[0][0];
+         const GLfloat dsdy = span->texStepY[0][0];
+         const GLfloat dtdx = span->texStepX[0][1];
+         const GLfloat dtdy = span->texStepY[0][1];
+         const GLfloat drdx = span->texStepX[0][2];
+         const GLfloat dqdx = span->texStepX[0][3];
+         const GLfloat dqdy = span->texStepY[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;
-         if (dq == 0.0) {
-            /* Ortho projection or polygon's parallel to window X axis */
-            const GLfloat invQ = (q == 0.0F) ? 1.0F : (1.0F / q);
-            const GLfloat lambda = HALF_LOG2(rho * invQ * invQ);
+         if (ctx->FragmentProgram._Enabled) {
+            /* do perspective correction but don't divide s, t, r by q */
+            const GLfloat dwdx = span->dwdx;
+            GLfloat w = span->w;
             for (i = 0; i < span->end; i++) {
-               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] = lambda;
-               s += ds;
-               t += dt;
-               r += dr;
+               const GLfloat invW = 1.0F / w;
+               texcoord[i][0] = s * invW;
+               texcoord[i][1] = t * invW;
+               texcoord[i][2] = r * invW;
+               texcoord[i][3] = q * invW;
+               lambda[i] = _swrast_compute_lambda(dsdx, dsdy, dtdx, dtdy,
+                                                  dqdx, dqdy, texW, texH,
+                                                  s, t, q, invW);
+               s += dsdx;
+               t += dtdx;
+               r += drdx;
+               q += dqdx;
+               w += dwdx;
             }
          }
          else {
+            /* tex.c */
             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] = HALF_LOG2(rho * invQ * invQ);
-               s += ds;
-               t += dt;
-               r += dr;
-               q += dq;
+               lambda[i] = _swrast_compute_lambda(dsdx, dsdy, dtdx, dtdy,
+                                                dqdx, dqdy, texW, texH,
+                                                s, t, q, invQ);
+               texcoord[i][0] = s * invQ;
+               texcoord[i][1] = t * invQ;
+               texcoord[i][2] = r * invQ;
+               texcoord[i][3] = q;
+               s += dsdx;
+               t += dtdx;
+               r += drdx;
+               q += dqdx;
             }
          }
          span->arrayMask |= SPAN_LAMBDA;
       }
       else {
          /* just texture 0, without 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 (*texcoord)[4] = span->array->texcoords[0];
+         const GLfloat dsdx = span->texStepX[0][0];
+         const GLfloat dtdx = span->texStepX[0][1];
+         const GLfloat drdx = span->texStepX[0][2];
+         const GLfloat dqdx = span->texStepX[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;
-         if (dq == 0.0) {
+         if (ctx->FragmentProgram._Enabled) {
+            /* do perspective correction but don't divide s, t, r by q */
+            const GLfloat dwdx = span->dwdx;
+            GLfloat w = span->w;
+            for (i = 0; i < span->end; i++) {
+               const GLfloat invW = 1.0F / w;
+               texcoord[i][0] = s * invW;
+               texcoord[i][1] = t * invW;
+               texcoord[i][2] = r * invW;
+               texcoord[i][3] = q * invW;
+               s += dsdx;
+               t += dtdx;
+               r += drdx;
+               q += dqdx;
+               w += dwdx;
+            }
+         }
+         else if (dqdx == 0.0F) {
             /* Ortho projection or polygon's parallel to window X axis */
             const GLfloat invQ = (q == 0.0F) ? 1.0F : (1.0F / q);
             for (i = 0; i < span->end; i++) {
-               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;
+               texcoord[i][0] = s * invQ;
+               texcoord[i][1] = t * invQ;
+               texcoord[i][2] = r * invQ;
+               texcoord[i][3] = q;
+               s += dsdx;
+               t += dtdx;
+               r += drdx;
             }
          }
          else {
             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;
+               texcoord[i][0] = s * invQ;
+               texcoord[i][1] = t * invQ;
+               texcoord[i][2] = r * invQ;
+               texcoord[i][3] = q;
+               s += dsdx;
+               t += dtdx;
+               r += drdx;
+               q += dqdx;
             }
          }
       }
@@ -498,6 +645,7 @@ stipple_polygon_span( GLcontext *ctx, struct sw_span *span )
 {
    const GLuint highbit = 0x80000000;
    const GLuint stipple = ctx->PolygonStipple[span->y % 32];
+   GLubyte *mask = span->array->mask;
    GLuint i, m;
 
    ASSERT(ctx->Polygon.StippleFlag);
@@ -507,7 +655,7 @@ stipple_polygon_span( GLcontext *ctx, struct sw_span *span )
 
    for (i = 0; i < span->end; i++) {
       if ((m & stipple) == 0) {
-        span->mask[i] = 0;
+        mask[i] = 0;
       }
       m = m >> 1;
       if (m == 0) {
@@ -535,10 +683,10 @@ clip_span( GLcontext *ctx, struct sw_span *span )
 
    if (span->arrayMask & SPAN_XY) {
       /* arrays of x/y pixel coords */
-      const GLint *x = span->xArray;
-      const GLint *y = span->yArray;
+      const GLint *x = span->array->x;
+      const GLint *y = span->array->y;
       const GLint n = span->end;
-      GLubyte *mask = span->mask;
+      GLubyte *mask = span->array->mask;
       GLint i;
       if (span->arrayMask & SPAN_MASK) {
          /* note: using & intead of && to reduce branches */
@@ -572,7 +720,7 @@ clip_span( GLcontext *ctx, struct sw_span *span )
       if (x < xmin) {
          ASSERT(x + n > xmin);
          span->writeAll = GL_FALSE;
-         BZERO(span->mask, (xmin - x) * sizeof(GLubyte));
+         _mesa_bzero(span->array->mask, (xmin - x) * sizeof(GLubyte));
       }
 
       /* Clip to right */
@@ -597,47 +745,42 @@ multi_write_index_span( GLcontext *ctx, struct sw_span *span )
    GLuint bufferBit;
 
    /* loop over four possible dest color buffers */
-   for (bufferBit = 1; bufferBit <= 8; bufferBit = bufferBit << 1) {
-      if (bufferBit & ctx->Color.DrawDestMask) {
+   for (bufferBit = 1; bufferBit <= 8; bufferBit <<= 1) {
+      if (bufferBit & ctx->Color._DrawDestMask) {
          GLuint indexTmp[MAX_WIDTH];
          ASSERT(span->end < MAX_WIDTH);
 
-         if (bufferBit == FRONT_LEFT_BIT)
-            (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_FRONT_LEFT);
-         else if (bufferBit == FRONT_RIGHT_BIT)
-            (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_FRONT_RIGHT);
-         else if (bufferBit == BACK_LEFT_BIT)
-            (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_BACK_LEFT);
-         else
-            (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_BACK_RIGHT);
+         /* Set the current read/draw buffer */
+         swrast->CurrentBufferBit = bufferBit;
+         (*swrast->Driver.SetBuffer)(ctx, ctx->DrawBuffer, bufferBit);
 
          /* make copy of incoming indexes */
-         MEMCPY( indexTmp, span->color.index, span->end * sizeof(GLuint) );
+         MEMCPY( indexTmp, span->array->index, span->end * sizeof(GLuint) );
 
          if (ctx->Color.IndexLogicOpEnabled) {
-            _mesa_logicop_ci_span(ctx, span, indexTmp);
+            _swrast_logicop_ci_span(ctx, span, indexTmp);
          }
 
          if (ctx->Color.IndexMask != 0xffffffff) {
-            _mesa_mask_index_span(ctx, span, indexTmp);
+            _swrast_mask_index_span(ctx, span, indexTmp);
          }
 
          if (span->arrayMask & SPAN_XY) {
             /* array of pixel coords */
             (*swrast->Driver.WriteCI32Pixels)(ctx, span->end,
-                                              span->xArray, span->yArray,
-                                              indexTmp, span->mask);
+                                              span->array->x, span->array->y,
+                                              indexTmp, span->array->mask);
          }
          else {
             /* horizontal run of pixels */
             (*swrast->Driver.WriteCI32Span)(ctx, span->end, span->x, span->y,
-                                            indexTmp, span->mask);
+                                            indexTmp, span->array->mask);
          }
       }
    }
 
    /* restore default dest buffer */
-   (void) (*ctx->Driver.SetDrawBuffer)( ctx, ctx->Color.DriverDrawBuffer);
+   _swrast_use_draw_buffer(ctx);
 }
 
 
@@ -659,71 +802,58 @@ multi_write_rgba_span( GLcontext *ctx, struct sw_span *span )
       return;
 
    /* loop over four possible dest color buffers */
-   for (bufferBit = 1; bufferBit <= 8; bufferBit = bufferBit << 1) {
-      if (bufferBit & ctx->Color.DrawDestMask) {
+   for (bufferBit = 1; bufferBit <= 8; bufferBit <<= 1) {
+      if (bufferBit & ctx->Color._DrawDestMask) {
          GLchan rgbaTmp[MAX_WIDTH][4];
          ASSERT(span->end < MAX_WIDTH);
 
-         if (bufferBit == FRONT_LEFT_BIT) {
-            (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_FRONT_LEFT);
-            ctx->DrawBuffer->Alpha = ctx->DrawBuffer->FrontLeftAlpha;
-         }
-         else if (bufferBit == FRONT_RIGHT_BIT) {
-            (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_FRONT_RIGHT);
-            ctx->DrawBuffer->Alpha = ctx->DrawBuffer->FrontRightAlpha;
-         }
-         else if (bufferBit == BACK_LEFT_BIT) {
-            (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_BACK_LEFT);
-            ctx->DrawBuffer->Alpha = ctx->DrawBuffer->BackLeftAlpha;
-         }
-         else {
-            (void) (*ctx->Driver.SetDrawBuffer)( ctx, GL_BACK_RIGHT);
-            ctx->DrawBuffer->Alpha = ctx->DrawBuffer->BackRightAlpha;
-         }
+         /* Set the current read/draw buffer */
+         swrast->CurrentBufferBit = bufferBit;
+         (*swrast->Driver.SetBuffer)(ctx, ctx->DrawBuffer, bufferBit);
 
          /* make copy of incoming colors */
-         MEMCPY( rgbaTmp, span->color.rgba, 4 * span->end * sizeof(GLchan) );
+         MEMCPY( rgbaTmp, span->array->rgba, 4 * span->end * sizeof(GLchan) );
 
-         if (ctx->Color.ColorLogicOpEnabled) {
-            _mesa_logicop_rgba_span(ctx, span, rgbaTmp);
+         if (ctx->Color._LogicOpEnabled) {
+            _swrast_logicop_rgba_span(ctx, span, rgbaTmp);
          }
          else if (ctx->Color.BlendEnabled) {
-            _mesa_blend_span(ctx, span, rgbaTmp);
+            _swrast_blend_span(ctx, span, rgbaTmp);
          }
 
          if (colorMask != 0xffffffff) {
-            _mesa_mask_rgba_span(ctx, span, rgbaTmp);
+            _swrast_mask_rgba_span(ctx, span, rgbaTmp);
          }
 
          if (span->arrayMask & SPAN_XY) {
             /* array of pixel coords */
             (*swrast->Driver.WriteRGBAPixels)(ctx, span->end,
-                                              span->xArray, span->yArray,
+                                              span->array->x, span->array->y,
                                               (const GLchan (*)[4]) rgbaTmp,
-                                              span->mask);
+                                              span->array->mask);
             if (SWRAST_CONTEXT(ctx)->_RasterMask & ALPHABUF_BIT) {
-               _mesa_write_alpha_pixels(ctx, span->end,
-                                        span->xArray, span->yArray,
+               _swrast_write_alpha_pixels(ctx, span->end,
+                                        span->array->x, span->array->y,
                                         (const GLchan (*)[4]) rgbaTmp,
-                                        span->mask);
+                                        span->array->mask);
             }
          }
          else {
             /* horizontal run of pixels */
             (*swrast->Driver.WriteRGBASpan)(ctx, span->end, span->x, span->y,
                                             (const GLchan (*)[4]) rgbaTmp,
-                                            span->mask);
+                                            span->array->mask);
             if (swrast->_RasterMask & ALPHABUF_BIT) {
-               _mesa_write_alpha_span(ctx, span->end, span->x, span->y,
+               _swrast_write_alpha_span(ctx, span->end, span->x, span->y,
                                       (const GLchan (*)[4]) rgbaTmp,
-                                      span->mask);
+                                      span->array->mask);
             }
          }
       }
    }
 
    /* restore default dest buffer */
-   (void) (*ctx->Driver.SetDrawBuffer)( ctx, ctx->Color.DriverDrawBuffer );
+   _swrast_use_draw_buffer(ctx);
 }
 
 
@@ -734,14 +864,15 @@ multi_write_rgba_span( GLcontext *ctx, struct sw_span *span )
  * to their original values before returning.
  */
 void
-_mesa_write_index_span( GLcontext *ctx, struct sw_span *span,
-                       GLenum primitive)
+_swrast_write_index_span( GLcontext *ctx, struct sw_span *span)
 {
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
    const GLuint origInterpMask = span->interpMask;
    const GLuint origArrayMask = span->arrayMask;
 
    ASSERT(span->end <= MAX_WIDTH);
+   ASSERT(span->primitive == GL_POINT  ||  span->primitive == GL_LINE ||
+         span->primitive == GL_POLYGON  ||  span->primitive == GL_BITMAP);
    ASSERT((span->interpMask | span->arrayMask) & SPAN_INDEX);
    ASSERT((span->interpMask & span->arrayMask) == 0);
 
@@ -750,51 +881,57 @@ _mesa_write_index_span( GLcontext *ctx, struct sw_span *span,
       span->writeAll = GL_FALSE;
    }
    else {
-      MEMSET(span->mask, 1, span->end);
+      MEMSET(span->array->mask, 1, span->end);
       span->writeAll = GL_TRUE;
    }
 
    /* Clipping */
-   if ((swrast->_RasterMask & CLIP_BIT) || (primitive == GL_BITMAP)
-       || (primitive == GL_POINT) || (primitive == GL_LINE)) {
+   if ((swrast->_RasterMask & CLIP_BIT) || (span->primitive != GL_POLYGON)) {
       if (!clip_span(ctx, span)) {
          return;
       }
    }
 
+   /* Depth bounds test */
+   if (ctx->Depth.BoundsTest && ctx->Visual.depthBits > 0) {
+      if (!_swrast_depth_bounds_test(ctx, span)) {
+         return;
+      }
+   }
+
 #ifdef DEBUG
    if (span->arrayMask & SPAN_XY) {
-      int i;
+      GLuint i;
       for (i = 0; i < span->end; i++) {
-         if (span->mask[i]) {
-            assert(span->xArray[i] >= ctx->DrawBuffer->_Xmin);
-            assert(span->xArray[i] < ctx->DrawBuffer->_Xmax);
-            assert(span->yArray[i] >= ctx->DrawBuffer->_Ymin);
-            assert(span->yArray[i] < ctx->DrawBuffer->_Ymax);
+         if (span->array->mask[i]) {
+            assert(span->array->x[i] >= ctx->DrawBuffer->_Xmin);
+            assert(span->array->x[i] < ctx->DrawBuffer->_Xmax);
+            assert(span->array->y[i] >= ctx->DrawBuffer->_Ymin);
+            assert(span->array->y[i] < ctx->DrawBuffer->_Ymax);
          }
       }
    }
 #endif
 
    /* Polygon Stippling */
-   if (ctx->Polygon.StippleFlag && primitive == GL_POLYGON) {
+   if (ctx->Polygon.StippleFlag && span->primitive == GL_POLYGON) {
       stipple_polygon_span(ctx, span);
    }
 
    /* Depth test and stencil */
    if (ctx->Depth.Test || ctx->Stencil.Enabled) {
       if (span->interpMask & SPAN_Z)
-         interpolate_z(ctx, span);
+         _swrast_span_interpolate_z(ctx, span);
 
       if (ctx->Stencil.Enabled) {
-         if (!_mesa_stencil_and_ztest_span(ctx, span)) {
+         if (!_swrast_stencil_and_ztest_span(ctx, span)) {
             span->arrayMask = origArrayMask;
             return;
          }
       }
       else {
          ASSERT(ctx->Depth.Test);
-         if (!_mesa_depth_test_span(ctx, span)) {
+         if (!_swrast_depth_test_span(ctx, span)) {
             span->arrayMask = origArrayMask;
             return;
          }
@@ -804,6 +941,14 @@ _mesa_write_index_span( GLcontext *ctx, struct sw_span *span,
    /* if we get here, something passed the depth test */
    ctx->OcclusionResult = GL_TRUE;
 
+#if FEATURE_ARB_occlusion_query
+   if (ctx->Occlusion.Active) {
+      GLuint i;
+      for (i = 0; i < span->end; i++)
+         ctx->Occlusion.PassedCounter += span->array->mask[i];
+   }
+#endif
+
    /* we have to wait until after occlusion to do this test */
    if (ctx->Color.DrawBuffer == GL_NONE || ctx->Color.IndexMask == 0) {
       /* write no pixels */
@@ -819,24 +964,18 @@ _mesa_write_index_span( GLcontext *ctx, struct sw_span *span,
    }
 
    /* Fog */
-   /* XXX try to simplify the fog code! */
    if (ctx->Fog.Enabled) {
-      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_ci_pixels( ctx, span, span->color.index);
+      _swrast_fog_ci_span(ctx, span);
    }
 
    /* Antialias coverage application */
    if (span->arrayMask & SPAN_COVERAGE) {
       GLuint i;
-      GLuint *index = span->color.index;
+      GLuint *index = span->array->index;
+      GLfloat *coverage = span->array->coverage;
       for (i = 0; i < span->end; i++) {
-         ASSERT(span->coverage[i] < 16);
-         index[i] = (index[i] & ~0xf) | ((GLuint) (span->coverage[i]));
+         ASSERT(coverage[i] < 16);
+         index[i] = (index[i] & ~0xf) | ((GLuint) coverage[i]);
       }
    }
 
@@ -847,11 +986,11 @@ _mesa_write_index_span( GLcontext *ctx, struct sw_span *span,
    else {
       /* normal situation: draw to exactly one buffer */
       if (ctx->Color.IndexLogicOpEnabled) {
-         _mesa_logicop_ci_span(ctx, span, span->color.index);
+         _swrast_logicop_ci_span(ctx, span, span->array->index);
       }
 
       if (ctx->Color.IndexMask != 0xffffffff) {
-         _mesa_mask_index_span(ctx, span, span->color.index);
+         _swrast_mask_index_span(ctx, span, span->array->index);
       }
 
       /* write pixels */
@@ -860,14 +999,14 @@ _mesa_write_index_span( GLcontext *ctx, struct sw_span *span,
          if ((span->interpMask & SPAN_INDEX) && span->indexStep == 0) {
             /* all pixels have same color index */
             (*swrast->Driver.WriteMonoCIPixels)(ctx, span->end,
-                                                span->xArray, span->yArray,
+                                                span->array->x, span->array->y,
                                                 FixedToInt(span->index),
-                                                span->mask);
+                                                span->array->mask);
          }
          else {
-            (*swrast->Driver.WriteCI32Pixels)(ctx, span->end, span->xArray,
-                                              span->yArray, span->color.index,
-                                              span->mask );
+            (*swrast->Driver.WriteCI32Pixels)(ctx, span->end, span->array->x,
+                                              span->array->y, span->array->index,
+                                              span->array->mask );
          }
       }
       else {
@@ -876,11 +1015,12 @@ _mesa_write_index_span( GLcontext *ctx, struct sw_span *span,
             /* all pixels have same color index */
             (*swrast->Driver.WriteMonoCISpan)(ctx, span->end, span->x, span->y,
                                               FixedToInt(span->index),
-                                              span->mask);
+                                              span->array->mask);
          }
          else {
             (*swrast->Driver.WriteCI32Span)(ctx, span->end, span->x, span->y,
-                                            span->color.index, span->mask);
+                                            span->array->index,
+                                            span->array->mask);
          }
       }
    }
@@ -896,8 +1036,7 @@ _mesa_write_index_span( GLcontext *ctx, struct sw_span *span,
  * to their original values before returning.
  */
 void
-_mesa_write_rgba_span( GLcontext *ctx, struct sw_span *span,
-                      GLenum primitive)
+_swrast_write_rgba_span( GLcontext *ctx, struct sw_span *span)
 {
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
    const GLuint colorMask = *((GLuint *) ctx->Color.ColorMask);
@@ -906,6 +1045,8 @@ _mesa_write_rgba_span( GLcontext *ctx, struct sw_span *span,
    GLboolean monoColor;
 
    ASSERT(span->end <= MAX_WIDTH);
+   ASSERT(span->primitive == GL_POINT  ||  span->primitive == GL_LINE ||
+         span->primitive == GL_POLYGON  ||  span->primitive == GL_BITMAP);
    ASSERT((span->interpMask & span->arrayMask) == 0);
    ASSERT((span->interpMask | span->arrayMask) & SPAN_RGBA);
 #ifdef DEBUG
@@ -915,16 +1056,12 @@ _mesa_write_rgba_span( GLcontext *ctx, struct sw_span *span,
       ASSERT((span->interpMask | span->arrayMask) & SPAN_Z);
 #endif
 
-   /*
-   printf("%s()  interp 0x%x  array 0x%x  p=0x%x\n", __FUNCTION__, span->interpMask, span->arrayMask, primitive);
-   */
-
    if (span->arrayMask & SPAN_MASK) {
       /* mask was initialized by caller, probably glBitmap */
       span->writeAll = GL_FALSE;
    }
    else {
-      MEMSET(span->mask, 1, span->end);
+      MEMSET(span->array->mask, 1, span->end);
       span->writeAll = GL_TRUE;
    }
 
@@ -934,35 +1071,59 @@ _mesa_write_rgba_span( GLcontext *ctx, struct sw_span *span,
       span->blueStep == 0 && span->alphaStep == 0;
 
    /* Clipping */
-   if ((swrast->_RasterMask & CLIP_BIT) || (primitive == GL_BITMAP)
-       || (primitive == GL_POINT) || (primitive == GL_LINE)) {
+   if ((swrast->_RasterMask & CLIP_BIT) || (span->primitive != GL_POLYGON)) {
       if (!clip_span(ctx, span)) {
          return;
       }
    }
 
+   /* Depth bounds test */
+   if (ctx->Depth.BoundsTest && ctx->Visual.depthBits > 0) {
+      if (!_swrast_depth_bounds_test(ctx, span)) {
+         return;
+      }
+   }
+
 #ifdef DEBUG
    if (span->arrayMask & SPAN_XY) {
-      int i;
+      GLuint i;
       for (i = 0; i < span->end; i++) {
-         if (span->mask[i]) {
-            assert(span->xArray[i] >= ctx->DrawBuffer->_Xmin);
-            assert(span->xArray[i] < ctx->DrawBuffer->_Xmax);
-            assert(span->yArray[i] >= ctx->DrawBuffer->_Ymin);
-            assert(span->yArray[i] < ctx->DrawBuffer->_Ymax);
+         if (span->array->mask[i]) {
+            assert(span->array->x[i] >= ctx->DrawBuffer->_Xmin);
+            assert(span->array->x[i] < ctx->DrawBuffer->_Xmax);
+            assert(span->array->y[i] >= ctx->DrawBuffer->_Ymin);
+            assert(span->array->y[i] < ctx->DrawBuffer->_Ymax);
          }
       }
    }
 #endif
 
    /* Polygon Stippling */
-   if (ctx->Polygon.StippleFlag && primitive == GL_POLYGON) {
+   if (ctx->Polygon.StippleFlag && span->primitive == GL_POLYGON) {
       stipple_polygon_span(ctx, span);
    }
 
+   /* Fragment program */
+   if (ctx->FragmentProgram._Enabled) {
+      /* Now we may need to interpolate the colors and texcoords */
+      if ((span->interpMask & SPAN_RGBA) &&
+          (span->arrayMask & SPAN_RGBA) == 0) {
+         interpolate_colors(ctx, span);
+         span->interpMask &= ~SPAN_RGBA;
+      }
+      if (span->interpMask & SPAN_SPEC) {
+         interpolate_specular(ctx, span);
+      }
+      if ((span->interpMask & SPAN_TEXTURE)
+          && (span->arrayMask & SPAN_TEXTURE) == 0)
+         interpolate_texcoords(ctx, span);
+      _swrast_exec_fragment_program(ctx, span);
+      monoColor = GL_FALSE;
+   }
+
    /* Do the alpha test */
    if (ctx->Color.AlphaEnabled) {
-      if (!_mesa_alpha_test(ctx, span)) {
+      if (!_swrast_alpha_test(ctx, span)) {
          span->interpMask = origInterpMask;
          span->arrayMask = origArrayMask;
         return;
@@ -972,10 +1133,10 @@ _mesa_write_rgba_span( GLcontext *ctx, struct sw_span *span,
    /* Stencil and Z testing */
    if (ctx->Stencil.Enabled || ctx->Depth.Test) {
       if (span->interpMask & SPAN_Z)
-         interpolate_z(ctx, span);
+         _swrast_span_interpolate_z(ctx, span);
 
       if (ctx->Stencil.Enabled) {
-         if (!_mesa_stencil_and_ztest_span(ctx, span)) {
+         if (!_swrast_stencil_and_ztest_span(ctx, span)) {
             span->interpMask = origInterpMask;
             span->arrayMask = origArrayMask;
             return;
@@ -985,7 +1146,7 @@ _mesa_write_rgba_span( GLcontext *ctx, struct sw_span *span,
          ASSERT(ctx->Depth.Test);
          ASSERT(span->arrayMask & SPAN_Z);
          /* regular depth testing */
-         if (!_mesa_depth_test_span(ctx, span)) {
+         if (!_swrast_depth_test_span(ctx, span)) {
             span->interpMask = origInterpMask;
             span->arrayMask = origArrayMask;
             return;
@@ -996,6 +1157,14 @@ _mesa_write_rgba_span( GLcontext *ctx, struct sw_span *span,
    /* if we get here, something passed the depth test */
    ctx->OcclusionResult = GL_TRUE;
 
+#if FEATURE_ARB_occlusion_query
+   if (ctx->Occlusion.Active) {
+      GLuint i;
+      for (i = 0; i < span->end; i++)
+         ctx->Occlusion.PassedCounter += span->array->mask[i];
+   }
+#endif
+
    /* can't abort span-writing until after occlusion testing */
    if (colorMask == 0x0) {
       span->interpMask = origInterpMask;
@@ -1011,29 +1180,18 @@ _mesa_write_rgba_span( GLcontext *ctx, struct sw_span *span,
    }
 
    /* 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 (swrast->_FogEnabled) {
+      _swrast_fog_rgba_span(ctx, span);
       monoColor = GL_FALSE;
    }
 
    /* Antialias coverage application */
    if (span->arrayMask & SPAN_COVERAGE) {
-      GLchan (*rgba)[4] = span->color.rgba;
+      GLchan (*rgba)[4] = span->array->rgba;
+      GLfloat *coverage = span->array->coverage;
       GLuint i;
       for (i = 0; i < span->end; i++) {
-         rgba[i][ACOMP] = (GLchan) (rgba[i][ACOMP] * span->coverage[i]);
+         rgba[i][ACOMP] = (GLchan) (rgba[i][ACOMP] * coverage[i]);
       }
       monoColor = GL_FALSE;
    }
@@ -1043,32 +1201,50 @@ _mesa_write_rgba_span( GLcontext *ctx, struct sw_span *span,
    }
    else {
       /* normal: write to exactly one buffer */
-      if (ctx->Color.ColorLogicOpEnabled) {
-         _mesa_logicop_rgba_span(ctx, span, span->color.rgba);
+      if (ctx->Color._LogicOpEnabled) {
+         _swrast_logicop_rgba_span(ctx, span, span->array->rgba);
          monoColor = GL_FALSE;
       }
       else if (ctx->Color.BlendEnabled) {
-         _mesa_blend_span(ctx, span, span->color.rgba);
+         _swrast_blend_span(ctx, span, span->array->rgba);
          monoColor = GL_FALSE;
       }
 
       /* Color component masking */
       if (colorMask != 0xffffffff) {
-         _mesa_mask_rgba_span(ctx, span, span->color.rgba);
+         _swrast_mask_rgba_span(ctx, span, span->array->rgba);
          monoColor = GL_FALSE;
       }
 
       /* write pixels */
       if (span->arrayMask & SPAN_XY) {
          /* array of pixel coords */
-         /* XXX test for mono color */
-         (*swrast->Driver.WriteRGBAPixels)(ctx, span->end, span->xArray,
-             span->yArray, (const GLchan (*)[4]) span->color.rgba, span->mask);
-         if (SWRAST_CONTEXT(ctx)->_RasterMask & ALPHABUF_BIT) {
-            _mesa_write_alpha_pixels(ctx, span->end,
-                                     span->xArray, span->yArray,
-                                     (const GLchan (*)[4]) span->color.rgba,
-                                     span->mask);
+         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.WriteMonoRGBAPixels)(ctx, span->end,
+                     span->array->x, span->array->y, color, span->array->mask);
+            if (SWRAST_CONTEXT(ctx)->_RasterMask & ALPHABUF_BIT) {
+               _swrast_write_mono_alpha_pixels(ctx, span->end,
+                                              span->array->x, span->array->y,
+                                              color[ACOMP], span->array->mask);
+            }
+         }
+         else {
+            (*swrast->Driver.WriteRGBAPixels)(ctx, span->end,
+                                       span->array->x, span->array->y,
+                                       (const GLchan (*)[4]) span->array->rgba,
+                                       span->array->mask);
+            if (SWRAST_CONTEXT(ctx)->_RasterMask & ALPHABUF_BIT) {
+               _swrast_write_alpha_pixels(ctx, span->end,
+                                       span->array->x, span->array->y,
+                                       (const GLchan (*)[4]) span->array->rgba,
+                                       span->array->mask);
+            }
          }
       }
       else {
@@ -1081,18 +1257,22 @@ _mesa_write_rgba_span( GLcontext *ctx, struct sw_span *span,
             color[BCOMP] = FixedToChan(span->blue);
             color[ACOMP] = FixedToChan(span->alpha);
             (*swrast->Driver.WriteMonoRGBASpan)(ctx, span->end, span->x,
-                                                span->y, color, span->mask);
-            /* XXX software alpha buffer writes! */
+                                           span->y, color, span->array->mask);
+            if (swrast->_RasterMask & ALPHABUF_BIT) {
+               _swrast_write_mono_alpha_span(ctx, span->end, span->x, span->y,
+                      color[ACOMP],
+                      span->writeAll ? ((const GLubyte *) NULL) : span->array->mask);
+            }
          }
          else {
             /* each pixel is a different color */
             (*swrast->Driver.WriteRGBASpan)(ctx, span->end, span->x, span->y,
-                      (const GLchan (*)[4]) span->color.rgba,
-                      span->writeAll ? ((const GLubyte *) NULL) : span->mask);
+                      (const GLchan (*)[4]) span->array->rgba,
+                      span->writeAll ? ((const GLubyte *) NULL) : span->array->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);
+               _swrast_write_alpha_span(ctx, span->end, span->x, span->y,
+                      (const GLchan (*)[4]) span->array->rgba,
+                      span->writeAll ? ((const GLubyte *) NULL) : span->array->mask);
             }
          }
       }
@@ -1135,16 +1315,18 @@ add_colors(GLuint n, GLchan rgba[][4], GLchan specular[][4] )
  * to their original values before returning.
  */
 void
-_mesa_write_texture_span( GLcontext *ctx, struct sw_span *span,
-                          GLenum primitive )
+_swrast_write_texture_span( GLcontext *ctx, struct sw_span *span)
 {
    const GLuint colorMask = *((GLuint *) ctx->Color.ColorMask);
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
+   const GLuint origInterpMask = span->interpMask;
    const GLuint origArrayMask = span->arrayMask;
 
+   ASSERT(span->primitive == GL_POINT  ||  span->primitive == GL_LINE ||
+         span->primitive == GL_POLYGON  ||  span->primitive == GL_BITMAP);
    ASSERT(span->end <= MAX_WIDTH);
    ASSERT((span->interpMask & span->arrayMask) == 0);
-   ASSERT(ctx->Texture._ReallyEnabled);
+   ASSERT(ctx->Texture._EnabledCoordUnits || ctx->FragmentProgram._Enabled);
 
    /*
    printf("%s()  interp 0x%x  array 0x%x\n", __FUNCTION__, span->interpMask, span->arrayMask);
@@ -1155,13 +1337,12 @@ _mesa_write_texture_span( GLcontext *ctx, struct sw_span *span,
       span->writeAll = GL_FALSE;
    }
    else {
-      MEMSET(span->mask, 1, span->end);
+      MEMSET(span->array->mask, 1, span->end);
       span->writeAll = GL_TRUE;
    }
 
    /* Clipping */
-   if ((swrast->_RasterMask & CLIP_BIT) || (primitive == GL_BITMAP)
-       || (primitive == GL_POINT) || (primitive == GL_LINE)) {
+   if ((swrast->_RasterMask & CLIP_BIT) || (span->primitive != GL_POLYGON)) {
       if (!clip_span(ctx, span)) {
         return;
       }
@@ -1169,20 +1350,20 @@ _mesa_write_texture_span( GLcontext *ctx, struct sw_span *span,
 
 #ifdef DEBUG
    if (span->arrayMask & SPAN_XY) {
-      int i;
+      GLuint i;
       for (i = 0; i < span->end; i++) {
-         if (span->mask[i]) {
-            assert(span->xArray[i] >= ctx->DrawBuffer->_Xmin);
-            assert(span->xArray[i] < ctx->DrawBuffer->_Xmax);
-            assert(span->yArray[i] >= ctx->DrawBuffer->_Ymin);
-            assert(span->yArray[i] < ctx->DrawBuffer->_Ymax);
+         if (span->array->mask[i]) {
+            assert(span->array->x[i] >= ctx->DrawBuffer->_Xmin);
+            assert(span->array->x[i] < ctx->DrawBuffer->_Xmax);
+            assert(span->array->y[i] >= ctx->DrawBuffer->_Ymin);
+            assert(span->array->y[i] < ctx->DrawBuffer->_Ymax);
          }
       }
    }
 #endif
 
    /* Polygon Stippling */
-   if (ctx->Polygon.StippleFlag && primitive == GL_POLYGON) {
+   if (ctx->Polygon.StippleFlag && span->primitive == GL_POLYGON) {
       stipple_polygon_span(ctx, span);
    }
 
@@ -1198,13 +1379,20 @@ _mesa_write_texture_span( GLcontext *ctx, struct sw_span *span,
       if ((span->interpMask & SPAN_RGBA) && (span->arrayMask & SPAN_RGBA) == 0)
          interpolate_colors(ctx, span);
 
+      if (span->interpMask & SPAN_SPEC) {
+         interpolate_specular(ctx, span);
+      }
+
       /* Texturing without alpha is done after depth-testing which
        * gives a potential speed-up.
        */
-      _swrast_multitexture_fragments( ctx, span );
+      if (ctx->FragmentProgram._Enabled)
+         _swrast_exec_fragment_program( ctx, span );
+      else
+         _swrast_texture_span( ctx, span );
 
       /* Do the alpha test */
-      if (!_mesa_alpha_test(ctx, span)) {
+      if (!_swrast_alpha_test(ctx, span)) {
          span->arrayMask = origArrayMask;
         return;
       }
@@ -1213,10 +1401,11 @@ _mesa_write_texture_span( GLcontext *ctx, struct sw_span *span,
    /* Stencil and Z testing */
    if (ctx->Stencil.Enabled || ctx->Depth.Test) {
       if (span->interpMask & SPAN_Z)
-         interpolate_z(ctx, span);
+         _swrast_span_interpolate_z(ctx, span);
 
       if (ctx->Stencil.Enabled) {
-         if (!_mesa_stencil_and_ztest_span(ctx, span)) {
+         if (!_swrast_stencil_and_ztest_span(ctx, span)) {
+            span->interpMask = origInterpMask;
             span->arrayMask = origArrayMask;
             return;
          }
@@ -1225,7 +1414,8 @@ _mesa_write_texture_span( GLcontext *ctx, struct sw_span *span,
          ASSERT(ctx->Depth.Test);
          ASSERT(span->arrayMask & SPAN_Z);
          /* regular depth testing */
-         if (!_mesa_depth_test_span(ctx, span)) {
+         if (!_swrast_depth_test_span(ctx, span)) {
+            span->interpMask = origInterpMask;
             span->arrayMask = origArrayMask;
             return;
          }
@@ -1235,10 +1425,19 @@ _mesa_write_texture_span( GLcontext *ctx, struct sw_span *span,
    /* if we get here, some fragments passed the depth test */
    ctx->OcclusionResult = GL_TRUE;
 
+#if FEATURE_ARB_occlusion_query
+   if (ctx->Occlusion.Active) {
+      GLuint i;
+      for (i = 0; i < span->end; i++)
+         ctx->Occlusion.PassedCounter += span->array->mask[i];
+   }
+#endif
+
    /* We had to wait until now to check for glColorMask(F,F,F,F) because of
     * the occlusion test.
     */
    if (colorMask == 0x0) {
+      span->interpMask = origInterpMask;
       span->arrayMask = origArrayMask;
       return;
    }
@@ -1250,7 +1449,14 @@ _mesa_write_texture_span( GLcontext *ctx, struct sw_span *span,
       if ((span->interpMask & SPAN_RGBA) && (span->arrayMask & SPAN_RGBA) == 0)
          interpolate_colors(ctx, span);
 
-      _swrast_multitexture_fragments( ctx, span );
+      if (span->interpMask & SPAN_SPEC) {
+         interpolate_specular(ctx, span);
+      }
+
+      if (ctx->FragmentProgram._Enabled)
+         _swrast_exec_fragment_program( ctx, span );
+      else
+         _swrast_texture_span( ctx, span );
    }
 
    ASSERT(span->arrayMask & SPAN_RGBA);
@@ -1263,32 +1469,21 @@ _mesa_write_texture_span( GLcontext *ctx, struct sw_span *span,
          interpolate_specular(ctx, span);
       }
       ASSERT(span->arrayMask & SPAN_SPEC);
-      add_colors( span->end, span->color.rgba, span->specArray );
+      add_colors( span->end, span->array->rgba, span->array->spec );
    }
 
    /* 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 (swrast->_FogEnabled) {
+      _swrast_fog_rgba_span(ctx, span);
    }
 
    /* Antialias coverage application */
    if (span->arrayMask & SPAN_COVERAGE) {
-      GLchan (*rgba)[4] = span->color.rgba;
+      GLchan (*rgba)[4] = span->array->rgba;
+      GLfloat *coverage = span->array->coverage;
       GLuint i;
       for (i = 0; i < span->end; i++) {
-         rgba[i][ACOMP] = (GLchan) (rgba[i][ACOMP] * span->coverage[i]);
+         rgba[i][ACOMP] = (GLchan) (rgba[i][ACOMP] * coverage[i]);
       }
    }
 
@@ -1297,42 +1492,44 @@ _mesa_write_texture_span( GLcontext *ctx, struct sw_span *span,
    }
    else {
       /* normal: write to exactly one buffer */
-      if (ctx->Color.ColorLogicOpEnabled) {
-         _mesa_logicop_rgba_span(ctx, span, span->color.rgba);
+      if (ctx->Color._LogicOpEnabled) {
+         _swrast_logicop_rgba_span(ctx, span, span->array->rgba);
       }
       else if (ctx->Color.BlendEnabled) {
-         _mesa_blend_span(ctx, span, span->color.rgba);
+         _swrast_blend_span(ctx, span, span->array->rgba);
       }
 
+      /* Color component masking */
       if (colorMask != 0xffffffff) {
-         _mesa_mask_rgba_span(ctx, span, span->color.rgba);
+         _swrast_mask_rgba_span(ctx, span, span->array->rgba);
       }
 
+      /* write pixels */
       if (span->arrayMask & SPAN_XY) {
          /* array of pixel coords */
-         (*swrast->Driver.WriteRGBAPixels)(ctx, span->end, span->xArray,
-             span->yArray, (const GLchan (*)[4]) span->color.rgba, span->mask);
+         (*swrast->Driver.WriteRGBAPixels)(ctx, span->end, span->array->x,
+             span->array->y, (const GLchan (*)[4]) span->array->rgba, span->array->mask);
          if (SWRAST_CONTEXT(ctx)->_RasterMask & ALPHABUF_BIT) {
-            _mesa_write_alpha_pixels(ctx, span->end,
-                                     span->xArray, span->yArray,
-                                     (const GLchan (*)[4]) span->color.rgba,
-                                     span->mask);
+            _swrast_write_alpha_pixels(ctx, span->end,
+                                     span->array->x, span->array->y,
+                                     (const GLchan (*)[4]) span->array->rgba,
+                                     span->array->mask);
          }
       }
       else {
          /* horizontal run of pixels */
          (*swrast->Driver.WriteRGBASpan)(ctx, span->end, span->x, span->y,
-                                       (const GLchan (*)[4]) span->color.rgba,
-                                       span->writeAll ? NULL : span->mask);
+                                       (const GLchan (*)[4]) span->array->rgba,
+                                       span->writeAll ? NULL : span->array->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 ? NULL : span->mask);
+            _swrast_write_alpha_span(ctx, span->end, span->x, span->y,
+                                   (const GLchan (*)[4]) span->array->rgba,
+                                   span->writeAll ? NULL : span->array->mask);
          }
       }
    }
 
+   span->interpMask = origInterpMask;
    span->arrayMask = origArrayMask;
 }
 
@@ -1343,15 +1540,17 @@ _mesa_write_texture_span( GLcontext *ctx, struct sw_span *span,
  * reading ouside the buffer's boundaries.
  */
 void
-_mesa_read_rgba_span( GLcontext *ctx, GLframebuffer *buffer,
+_swrast_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) {
+   const GLint bufWidth = (GLint) buffer->Width;
+   const GLint bufHeight = (GLint) buffer->Height;
+
+   if (y < 0 || y >= bufHeight || x + (GLint) n < 0 || x >= bufWidth) {
       /* completely above, below, or right */
       /* XXX maybe leave undefined? */
-      BZERO(rgba, 4 * n * sizeof(GLchan));
+      _mesa_bzero(rgba, 4 * n * sizeof(GLchan));
    }
    else {
       GLint skip, length;
@@ -1363,14 +1562,14 @@ _mesa_read_rgba_span( GLcontext *ctx, GLframebuffer *buffer,
             /* completely left of window */
             return;
          }
-         if (length > buffer->Width) {
-            length = buffer->Width;
+         if (length > bufWidth) {
+            length = bufWidth;
          }
       }
-      else if ((GLint) (x + n) > buffer->Width) {
+      else if ((GLint) (x + n) > bufWidth) {
          /* right edge clipping */
          skip = 0;
-         length = buffer->Width - x;
+         length = bufWidth - x;
          if (length < 0) {
             /* completely to right of window */
             return;
@@ -1384,7 +1583,7 @@ _mesa_read_rgba_span( GLcontext *ctx, GLframebuffer *buffer,
 
       (*swrast->Driver.ReadRGBASpan)( ctx, length, x + skip, y, rgba + skip );
       if (buffer->UseSoftwareAlphaBuffers) {
-         _mesa_read_alpha_span(ctx, length, x + skip, y, rgba + skip);
+         _swrast_read_alpha_span(ctx, length, x + skip, y, rgba + skip);
       }
    }
 }
@@ -1395,14 +1594,16 @@ _mesa_read_rgba_span( GLcontext *ctx, GLframebuffer *buffer,
  * reading ouside the buffer's boundaries.
  */
 void
-_mesa_read_index_span( GLcontext *ctx, GLframebuffer *buffer,
+_swrast_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) {
+   const GLint bufWidth = (GLint) buffer->Width;
+   const GLint bufHeight = (GLint) buffer->Height;
+
+   if (y < 0 || y >= bufHeight || x + (GLint) n < 0 || x >= bufWidth) {
       /* completely above, below, or right */
-      BZERO(indx, n * sizeof(GLuint));
+      _mesa_bzero(indx, n * sizeof(GLuint));
    }
    else {
       GLint skip, length;
@@ -1414,14 +1615,14 @@ _mesa_read_index_span( GLcontext *ctx, GLframebuffer *buffer,
             /* completely left of window */
             return;
          }
-         if (length > buffer->Width) {
-            length = buffer->Width;
+         if (length > bufWidth) {
+            length = bufWidth;
          }
       }
-      else if ((GLint) (x + n) > buffer->Width) {
+      else if ((GLint) (x + n) > bufWidth) {
          /* right edge clipping */
          skip = 0;
-         length = buffer->Width - x;
+         length = bufWidth - x;
          if (length < 0) {
             /* completely to right of window */
             return;