Merge branch 'gallium-polygon-stipple'
[mesa.git] / src / mesa / swrast / s_span.c
index 905cf3d55010d363a60cd573631e3dbfa1aee836..db102ac7946afd4e711f4aa62731a42a5041faed 100644 (file)
@@ -33,7 +33,6 @@
 
 #include "main/glheader.h"
 #include "main/colormac.h"
-#include "main/context.h"
 #include "main/macros.h"
 #include "main/imports.h"
 #include "main/image.h"
@@ -58,8 +57,9 @@
  * and glBitmap.
  */
 void
-_swrast_span_default_attribs(GLcontext *ctx, SWspan *span)
+_swrast_span_default_attribs(struct gl_context *ctx, SWspan *span)
 {
+   GLchan r, g, b, a;
    /* Z*/
    {
       const GLfloat depthMax = ctx->DrawBuffer->_DepthMaxF;
@@ -80,41 +80,33 @@ _swrast_span_default_attribs(GLcontext *ctx, SWspan *span)
    span->attrStepY[FRAG_ATTRIB_WPOS][3] = 0.0;
 
    /* primary color, or color index */
-   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]);
+   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;
+   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);
+   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;
-
-      COPY_4V(span->attrStart[FRAG_ATTRIB_COL0], ctx->Current.RasterColor);
-      ASSIGN_4V(span->attrStepX[FRAG_ATTRIB_COL0], 0.0, 0.0, 0.0, 0.0);
-      ASSIGN_4V(span->attrStepY[FRAG_ATTRIB_COL0], 0.0, 0.0, 0.0, 0.0);
-   }
-   else {
-      span->index = FloatToFixed(ctx->Current.RasterIndex);
-      span->indexStep = 0;
-      span->interpMask |= SPAN_INDEX;
-   }
+   span->redStep = 0;
+   span->greenStep = 0;
+   span->blueStep = 0;
+   span->alphaStep = 0;
+   span->interpMask |= SPAN_RGBA;
+
+   COPY_4V(span->attrStart[FRAG_ATTRIB_COL0], ctx->Current.RasterColor);
+   ASSIGN_4V(span->attrStepX[FRAG_ATTRIB_COL0], 0.0, 0.0, 0.0, 0.0);
+   ASSIGN_4V(span->attrStepY[FRAG_ATTRIB_COL0], 0.0, 0.0, 0.0, 0.0);
 
    /* Secondary color */
-   if (ctx->Visual.rgbMode && (ctx->Light.Enabled || ctx->Fog.ColorSumEnabled))
+   if (ctx->Light.Enabled || ctx->Fog.ColorSumEnabled)
    {
       COPY_4V(span->attrStart[FRAG_ATTRIB_COL1], ctx->Current.RasterSecondaryColor);
       ASSIGN_4V(span->attrStepX[FRAG_ATTRIB_COL1], 0.0, 0.0, 0.0, 0.0);
@@ -171,7 +163,7 @@ _swrast_span_default_attribs(GLcontext *ctx, SWspan *span)
  * should have computed attrStart/Step values for FRAG_ATTRIB_WPOS[3]!
  */
 static INLINE void
-interpolate_active_attribs(GLcontext *ctx, SWspan *span, GLbitfield attrMask)
+interpolate_active_attribs(struct gl_context *ctx, SWspan *span, GLbitfield attrMask)
 {
    const SWcontext *swrast = SWRAST_CONTEXT(ctx);
 
@@ -218,7 +210,7 @@ interpolate_active_attribs(GLcontext *ctx, SWspan *span, GLbitfield attrMask)
  * color array.
  */
 static INLINE void
-interpolate_int_colors(GLcontext *ctx, SWspan *span)
+interpolate_int_colors(struct gl_context *ctx, SWspan *span)
 {
    const GLuint n = span->end;
    GLuint i;
@@ -307,7 +299,8 @@ interpolate_int_colors(GLcontext *ctx, SWspan *span)
       interpolate_active_attribs(ctx, span, FRAG_BIT_COL0);
       break;
    default:
-      _mesa_problem(NULL, "bad datatype in interpolate_int_colors");
+      _mesa_problem(ctx, "bad datatype 0x%x in interpolate_int_colors",
+                    span->array->ChanType);
    }
    span->arrayMask |= SPAN_RGBA;
 }
@@ -374,43 +367,11 @@ interpolate_float_colors(SWspan *span)
 
 
 
-/* Fill in the span.color.index array from the interpolation values */
-static INLINE void
-interpolate_indexes(GLcontext *ctx, SWspan *span)
-{
-   GLfixed index = span->index;
-   const GLint indexStep = span->indexStep;
-   const GLuint n = span->end;
-   GLuint *indexes = span->array->index;
-   GLuint i;
-   (void) ctx;
-
-   ASSERT(!(span->arrayMask & 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;
-   span->interpMask &= ~SPAN_INDEX;
-}
-
-
 /**
  * Fill in the span.zArray array from the span->z, zStep values.
  */
 void
-_swrast_span_interpolate_z( const GLcontext *ctx, SWspan *span )
+_swrast_span_interpolate_z( const struct gl_context *ctx, SWspan *span )
 {
    const GLuint n = span->end;
    GLuint i;
@@ -500,7 +461,7 @@ _swrast_compute_lambda(GLfloat dsdx, GLfloat dsdy, GLfloat dtdx, GLfloat dtdy,
  * texels with (s/q, t/q, r/q).
  */
 static void
-interpolate_texcoords(GLcontext *ctx, SWspan *span)
+interpolate_texcoords(struct gl_context *ctx, SWspan *span)
 {
    const GLuint maxUnit
       = (ctx->Texture._EnabledCoordUnits > 1) ? ctx->Const.MaxTextureUnits : 1;
@@ -529,8 +490,15 @@ interpolate_texcoords(GLcontext *ctx, SWspan *span)
 
          if (obj) {
             const struct gl_texture_image *img = obj->Image[0][obj->BaseLevel];
-            needLambda = (obj->MinFilter != obj->MagFilter)
+            needLambda = (obj->Sampler.MinFilter != obj->Sampler.MagFilter)
                || ctx->FragmentProgram._Current;
+            /* LOD is calculated directly in the ansiotropic filter, we can
+             * skip the normal lambda function as the result is ignored.
+             */
+            if (obj->Sampler.MaxAnisotropy > 1.0 &&
+                obj->Sampler.MinFilter == GL_LINEAR_MIPMAP_LINEAR) {
+               needLambda = GL_FALSE;
+            }
             texW = img->WidthScale;
             texH = img->HeightScale;
          }
@@ -641,7 +609,7 @@ interpolate_texcoords(GLcontext *ctx, SWspan *span)
  * Fill in the arrays->attribs[FRAG_ATTRIB_WPOS] array.
  */
 static INLINE void
-interpolate_wpos(GLcontext *ctx, SWspan *span)
+interpolate_wpos(struct gl_context *ctx, SWspan *span)
 {
    GLfloat (*wpos)[4] = span->array->attribs[FRAG_ATTRIB_WPOS];
    GLuint i;
@@ -675,7 +643,7 @@ interpolate_wpos(GLcontext *ctx, SWspan *span)
  * Apply the current polygon stipple pattern to a span of pixels.
  */
 static INLINE void
-stipple_polygon_span(GLcontext *ctx, SWspan *span)
+stipple_polygon_span(struct gl_context *ctx, SWspan *span)
 {
    GLubyte *mask = span->array->mask;
 
@@ -720,7 +688,7 @@ stipple_polygon_span(GLcontext *ctx, SWspan *span)
  *           GL_FALSE  nothing visible
  */
 static INLINE GLuint
-clip_span( GLcontext *ctx, SWspan *span )
+clip_span( struct gl_context *ctx, SWspan *span )
 {
    const GLint xmin = ctx->DrawBuffer->_Xmin;
    const GLint xmax = ctx->DrawBuffer->_Xmax;
@@ -783,15 +751,48 @@ clip_span( GLcontext *ctx, SWspan *span )
           * fragment attributes.
           * For arrays of values, shift them left.
           */
+         for (i = 0; i < FRAG_ATTRIB_MAX; i++) {
+            if (span->interpMask & (1 << i)) {
+               GLuint j;
+               for (j = 0; j < 4; j++) {
+                  span->attrStart[i][j] += leftClip * span->attrStepX[i][j];
+               }
+            }
+         }
+
+         span->red += leftClip * span->redStep;
+         span->green += leftClip * span->greenStep;
+         span->blue += leftClip * span->blueStep;
+         span->alpha += leftClip * span->alphaStep;
+         span->index += leftClip * span->indexStep;
+         span->z += leftClip * span->zStep;
+         span->intTex[0] += leftClip * span->intTexStep[0];
+         span->intTex[1] += leftClip * span->intTexStep[1];
+
+#define SHIFT_ARRAY(ARRAY, SHIFT, LEN) \
+         memcpy(ARRAY, ARRAY + (SHIFT), (LEN) * sizeof(ARRAY[0]))
+
          for (i = 0; i < FRAG_ATTRIB_MAX; i++) {
             if (span->arrayAttribs & (1 << i)) {
                /* shift array elements left by 'leftClip' */
-               _mesa_memcpy(span->array->attribs[i],
-                            span->array->attribs[i] + leftClip,
-                            (n - leftClip) * 4 * sizeof(GLfloat));
+               SHIFT_ARRAY(span->array->attribs[i], leftClip, n - leftClip);
             }
          }
 
+         SHIFT_ARRAY(span->array->mask, leftClip, n - leftClip);
+         SHIFT_ARRAY(span->array->rgba8, leftClip, n - leftClip);
+         SHIFT_ARRAY(span->array->rgba16, leftClip, n - leftClip);
+         SHIFT_ARRAY(span->array->x, leftClip, n - leftClip);
+         SHIFT_ARRAY(span->array->y, leftClip, n - leftClip);
+         SHIFT_ARRAY(span->array->z, leftClip, n - leftClip);
+         SHIFT_ARRAY(span->array->index, leftClip, n - leftClip);
+         for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) {
+            SHIFT_ARRAY(span->array->lambda[i], leftClip, n - leftClip);
+         }
+         SHIFT_ARRAY(span->array->coverage, leftClip, n - leftClip);
+
+#undef SHIFT_ARRAY
+
          span->leftClip = leftClip;
          span->x = xmin;
          span->end -= leftClip;
@@ -808,256 +809,13 @@ clip_span( GLcontext *ctx, SWspan *span )
 }
 
 
-/**
- * Apply all the per-fragment opertions to a span of color index fragments
- * and write them to the enabled color drawbuffers.
- * The 'span' parameter can be considered to be const.  Note that
- * span->interpMask and span->arrayMask may be changed but will be restored
- * to their original values before returning.
- */
-void
-_swrast_write_index_span( GLcontext *ctx, SWspan *span)
-{
-   const SWcontext *swrast = SWRAST_CONTEXT(ctx);
-   const GLbitfield origInterpMask = span->interpMask;
-   const GLbitfield origArrayMask = span->arrayMask;
-   struct gl_framebuffer *fb = ctx->DrawBuffer;
-
-   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);
-   */
-
-   if (span->arrayMask & SPAN_MASK) {
-      /* mask was initialized by caller, probably glBitmap */
-      span->writeAll = GL_FALSE;
-   }
-   else {
-      _mesa_memset(span->array->mask, 1, span->end);
-      span->writeAll = GL_TRUE;
-   }
-
-   /* Clipping */
-   if ((swrast->_RasterMask & CLIP_BIT) || (span->primitive != GL_POLYGON)) {
-      if (!clip_span(ctx, span)) {
-         return;
-      }
-   }
-
-   if (!(span->arrayMask & SPAN_MASK)) {
-      /* post-clip sanity check */
-      assert(span->x >= 0);
-      assert(span->y >= 0);
-   }
-
-   /* Depth bounds test */
-   if (ctx->Depth.BoundsTest && fb->Visual.depthBits > 0) {
-      if (!_swrast_depth_bounds_test(ctx, span)) {
-         return;
-      }
-   }
-
-#ifdef DEBUG
-   /* Make sure all fragments are within window bounds */
-   if (span->arrayMask & SPAN_XY) {
-      GLuint i;
-      for (i = 0; i < span->end; i++) {
-         if (span->array->mask[i]) {
-            assert(span->array->x[i] >= fb->_Xmin);
-            assert(span->array->x[i] < fb->_Xmax);
-            assert(span->array->y[i] >= fb->_Ymin);
-            assert(span->array->y[i] < fb->_Ymax);
-         }
-      }
-   }
-#endif
-
-   /* Polygon Stippling */
-   if (ctx->Polygon.StippleFlag && span->primitive == GL_POLYGON) {
-      stipple_polygon_span(ctx, span);
-   }
-
-   /* Stencil and Z testing */
-   if (ctx->Stencil._Enabled || ctx->Depth.Test) {
-      if (!(span->arrayMask & SPAN_Z))
-         _swrast_span_interpolate_z(ctx, span);
-
-      if (ctx->Transform.DepthClamp)
-        _swrast_depth_clamp_span(ctx, span);
-
-      if (ctx->Stencil._Enabled) {
-         if (!_swrast_stencil_and_ztest_span(ctx, span)) {
-            span->arrayMask = origArrayMask;
-            return;
-         }
-      }
-      else {
-         ASSERT(ctx->Depth.Test);
-         if (!_swrast_depth_test_span(ctx, span)) {
-            span->interpMask = origInterpMask;
-            span->arrayMask = origArrayMask;
-            return;
-         }
-      }
-   }
-
-   if (ctx->Query.CurrentOcclusionObject) {
-      /* update count of 'passed' fragments */
-      struct gl_query_object *q = ctx->Query.CurrentOcclusionObject;
-      GLuint i;
-      for (i = 0; i < span->end; i++)
-         q->Result += span->array->mask[i];
-   }
-
-   /* we have to wait until after occlusion to do this test */
-   if (ctx->Color.IndexMask == 0) {
-      /* write no pixels */
-      span->arrayMask = origArrayMask;
-      return;
-   }
-
-   /* Interpolate the color indexes if needed */
-   if (swrast->_FogEnabled ||
-       ctx->Color.IndexLogicOpEnabled ||
-       ctx->Color.IndexMask != 0xffffffff ||
-       (span->arrayMask & SPAN_COVERAGE)) {
-      if (!(span->arrayMask & SPAN_INDEX) /*span->interpMask & SPAN_INDEX*/) {
-         interpolate_indexes(ctx, span);
-      }
-   }
-
-   /* Fog */
-   if (swrast->_FogEnabled) {
-      _swrast_fog_ci_span(ctx, span);
-   }
-
-   /* Antialias coverage application */
-   if (span->arrayMask & SPAN_COVERAGE) {
-      const GLfloat *coverage = span->array->coverage;
-      GLuint *index = span->array->index;
-      GLuint i;
-      for (i = 0; i < span->end; i++) {
-         ASSERT(coverage[i] < 16);
-         index[i] = (index[i] & ~0xf) | ((GLuint) coverage[i]);
-      }
-   }
-
-   /*
-    * Write to renderbuffers
-    */
-   {
-      const GLuint numBuffers = fb->_NumColorDrawBuffers;
-      GLuint buf;
-
-      for (buf = 0; buf < numBuffers; buf++) {
-         struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[buf];
-         GLuint indexSave[MAX_WIDTH];
-
-         ASSERT(rb->_BaseFormat == GL_COLOR_INDEX);
-
-         if (numBuffers > 1) {
-            /* save indexes for second, third renderbuffer writes */
-            _mesa_memcpy(indexSave, span->array->index,
-                         span->end * sizeof(indexSave[0]));
-         }
-
-         if (ctx->Color.IndexLogicOpEnabled) {
-            _swrast_logicop_ci_span(ctx, rb, span);
-         }
-
-         if (ctx->Color.IndexMask != 0xffffffff) {
-            _swrast_mask_ci_span(ctx, rb, span);
-         }
-
-         if (!(span->arrayMask & SPAN_INDEX) && span->indexStep == 0) {
-            /* all fragments have same color index */
-            GLubyte index8;
-            GLushort index16;
-            GLuint index32;
-            void *value;
-
-            if (rb->DataType == GL_UNSIGNED_BYTE) {
-               index8 = FixedToInt(span->index);
-               value = &index8;
-            }
-            else if (rb->DataType == GL_UNSIGNED_SHORT) {
-               index16 = FixedToInt(span->index);
-               value = &index16;
-            }
-            else {
-               ASSERT(rb->DataType == GL_UNSIGNED_INT);
-               index32 = FixedToInt(span->index);
-               value = &index32;
-            }
-
-            if (span->arrayMask & SPAN_XY) {
-               rb->PutMonoValues(ctx, rb, span->end, span->array->x, 
-                                 span->array->y, value, span->array->mask);
-            }
-            else {
-               rb->PutMonoRow(ctx, rb, span->end, span->x, span->y,
-                              value, span->array->mask);
-            }
-         }
-         else {
-            /* each fragment is a different color */
-            GLubyte index8[MAX_WIDTH];
-            GLushort index16[MAX_WIDTH];
-            void *values;
-
-            if (rb->DataType == GL_UNSIGNED_BYTE) {
-               GLuint k;
-               for (k = 0; k < span->end; k++) {
-                  index8[k] = (GLubyte) span->array->index[k];
-               }
-               values = index8;
-            }
-            else if (rb->DataType == GL_UNSIGNED_SHORT) {
-               GLuint k;
-               for (k = 0; k < span->end; k++) {
-                  index16[k] = (GLushort) span->array->index[k];
-               }
-               values = index16;
-            }
-            else {
-               ASSERT(rb->DataType == GL_UNSIGNED_INT);
-               values = span->array->index;
-            }
-
-            if (span->arrayMask & SPAN_XY) {
-               rb->PutValues(ctx, rb, span->end,
-                             span->array->x, span->array->y,
-                             values, span->array->mask);
-            }
-            else {
-               rb->PutRow(ctx, rb, span->end, span->x, span->y,
-                          values, span->array->mask);
-            }
-         }
-
-         if (buf + 1 < numBuffers) {
-            /* restore original span values */
-            _mesa_memcpy(span->array->index, indexSave,
-                         span->end * sizeof(indexSave[0]));
-         }
-      } /* for buf */
-   }
-
-   span->interpMask = origInterpMask;
-   span->arrayMask = origArrayMask;
-}
-
-
 /**
  * Add specular colors to primary colors.
  * Only called during fixed-function operation.
  * Result is float color array (FRAG_ATTRIB_COL0).
  */
 static INLINE void
-add_specular(GLcontext *ctx, SWspan *span)
+add_specular(struct gl_context *ctx, SWspan *span)
 {
    const SWcontext *swrast = SWRAST_CONTEXT(ctx);
    const GLubyte *mask = span->array->mask;
@@ -1201,25 +959,18 @@ convert_color_type(SWspan *span, GLenum newType, GLuint output)
  * Apply fragment shader, fragment program or normal texturing to span.
  */
 static INLINE void
-shade_texture_span(GLcontext *ctx, SWspan *span)
+shade_texture_span(struct gl_context *ctx, SWspan *span)
 {
-   GLbitfield inputsRead;
-
-   /* Determine which fragment attributes are actually needed */
-   if (ctx->FragmentProgram._Current) {
-      inputsRead = ctx->FragmentProgram._Current->Base.InputsRead;
-   }
-   else {
-      /* XXX we could be a bit smarter about this */
-      inputsRead = ~0;
-   }
-
    if (ctx->FragmentProgram._Current ||
        ctx->ATIFragmentShader._Enabled) {
       /* programmable shading */
       if (span->primitive == GL_BITMAP && span->array->ChanType != GL_FLOAT) {
          convert_color_type(span, GL_FLOAT, 0);
       }
+      else {
+         span->array->rgba = (void *) span->array->attribs[FRAG_ATTRIB_COL0];
+      }
+
       if (span->primitive != GL_POINT ||
          (span->interpMask & SPAN_RGBA) ||
          ctx->Point.PointSprite) {
@@ -1275,7 +1026,7 @@ shade_texture_span(GLcontext *ctx, SWspan *span)
  * to their original values before returning.
  */
 void
-_swrast_write_rgba_span( GLcontext *ctx, SWspan *span)
+_swrast_write_rgba_span( struct gl_context *ctx, SWspan *span)
 {
    const SWcontext *swrast = SWRAST_CONTEXT(ctx);
    const GLuint *colorMask = (GLuint *) ctx->Color.ColorMask;
@@ -1305,7 +1056,7 @@ _swrast_write_rgba_span( GLcontext *ctx, SWspan *span)
       span->writeAll = GL_FALSE;
    }
    else {
-      _mesa_memset(span->array->mask, 1, span->end);
+      memset(span->array->mask, 1, span->end);
       span->writeAll = GL_TRUE;
    }
 
@@ -1471,17 +1222,31 @@ _swrast_write_rgba_span( GLcontext *ctx, SWspan *span)
             GLchan rgbaSave[MAX_WIDTH][4];
             const GLuint fragOutput = multiFragOutputs ? buf : 0;
 
+            /* set span->array->rgba to colors for render buffer's datatype */
             if (rb->DataType != span->array->ChanType || fragOutput > 0) {
                convert_color_type(span, rb->DataType, fragOutput);
             }
+            else {
+               if (rb->DataType == GL_UNSIGNED_BYTE) {
+                  span->array->rgba = span->array->rgba8;
+               }
+               else if (rb->DataType == GL_UNSIGNED_SHORT) {
+                  span->array->rgba = (void *) span->array->rgba16;
+               }
+               else {
+                  span->array->rgba = (void *)
+                     span->array->attribs[FRAG_ATTRIB_COL0];
+               }
+            }
 
             if (!multiFragOutputs && numBuffers > 1) {
                /* save colors for second, third renderbuffer writes */
-               _mesa_memcpy(rgbaSave, span->array->rgba,
-                            4 * span->end * sizeof(GLchan));
+               memcpy(rgbaSave, span->array->rgba,
+                      4 * span->end * sizeof(GLchan));
             }
 
-            ASSERT(rb->_BaseFormat == GL_RGBA || rb->_BaseFormat == GL_RGB);
+            ASSERT(rb->_BaseFormat == GL_RGBA || rb->_BaseFormat == GL_RGB ||
+                  rb->_BaseFormat == GL_ALPHA);
 
             if (ctx->Color._LogicOpEnabled) {
                _swrast_logicop_rgba_span(ctx, rb, span);
@@ -1511,8 +1276,8 @@ _swrast_write_rgba_span( GLcontext *ctx, SWspan *span)
 
             if (!multiFragOutputs && numBuffers > 1) {
                /* restore original span values */
-               _mesa_memcpy(span->array->rgba, rgbaSave,
-                            4 * span->end * sizeof(GLchan));
+               memcpy(span->array->rgba, rgbaSave,
+                      4 * span->end * sizeof(GLchan));
             }
 
          } /* if rb */
@@ -1536,7 +1301,7 @@ end:
  * \param rgba  the returned colors
  */
 void
-_swrast_read_rgba_span( GLcontext *ctx, struct gl_renderbuffer *rb,
+_swrast_read_rgba_span( struct gl_context *ctx, struct gl_renderbuffer *rb,
                         GLuint n, GLint x, GLint y, GLenum dstType,
                         GLvoid *rgba)
 {
@@ -1546,7 +1311,7 @@ _swrast_read_rgba_span( GLcontext *ctx, struct gl_renderbuffer *rb,
    if (y < 0 || y >= bufHeight || x + (GLint) n < 0 || x >= bufWidth) {
       /* completely above, below, or right */
       /* XXX maybe leave rgba values undefined? */
-      _mesa_bzero(rgba, 4 * n * sizeof(GLchan));
+      memset(rgba, 0, 4 * n * sizeof(GLchan));
    }
    else {
       GLint skip, length;
@@ -1579,7 +1344,14 @@ _swrast_read_rgba_span( GLcontext *ctx, struct gl_renderbuffer *rb,
 
       ASSERT(rb);
       ASSERT(rb->GetRow);
-      ASSERT(rb->_BaseFormat == GL_RGB || rb->_BaseFormat == GL_RGBA);
+      ASSERT(rb->_BaseFormat == GL_RGBA ||
+            rb->_BaseFormat == GL_RGB ||
+            rb->_BaseFormat == GL_RG ||
+            rb->_BaseFormat == GL_RED ||
+            rb->_BaseFormat == GL_LUMINANCE ||
+            rb->_BaseFormat == GL_INTENSITY ||
+            rb->_BaseFormat == GL_LUMINANCE_ALPHA ||
+            rb->_BaseFormat == GL_ALPHA);
 
       if (rb->DataType == dstType) {
          rb->GetRow(ctx, rb, length, x + skip, y,
@@ -1596,74 +1368,6 @@ _swrast_read_rgba_span( GLcontext *ctx, struct gl_renderbuffer *rb,
 }
 
 
-/**
- * Read CI pixels from a renderbuffer.  Clipping will be done to prevent
- * reading ouside the buffer's boundaries.
- */
-void
-_swrast_read_index_span( GLcontext *ctx, struct gl_renderbuffer *rb,
-                         GLuint n, GLint x, GLint y, GLuint index[] )
-{
-   const GLint bufWidth = (GLint) rb->Width;
-   const GLint bufHeight = (GLint) rb->Height;
-
-   if (y < 0 || y >= bufHeight || x + (GLint) n < 0 || x >= bufWidth) {
-      /* completely above, below, or right */
-      _mesa_bzero(index, n * sizeof(GLuint));
-   }
-   else {
-      GLint skip, length;
-      if (x < 0) {
-         /* left edge clipping */
-         skip = -x;
-         length = (GLint) n - skip;
-         if (length < 0) {
-            /* completely left of window */
-            return;
-         }
-         if (length > bufWidth) {
-            length = bufWidth;
-         }
-      }
-      else if ((GLint) (x + n) > bufWidth) {
-         /* right edge clipping */
-         skip = 0;
-         length = bufWidth - x;
-         if (length < 0) {
-            /* completely to right of window */
-            return;
-         }
-      }
-      else {
-         /* no clipping */
-         skip = 0;
-         length = (GLint) n;
-      }
-
-      ASSERT(rb->GetRow);
-      ASSERT(rb->_BaseFormat == GL_COLOR_INDEX);
-
-      if (rb->DataType == GL_UNSIGNED_BYTE) {
-         GLubyte index8[MAX_WIDTH];
-         GLint i;
-         rb->GetRow(ctx, rb, length, x + skip, y, index8);
-         for (i = 0; i < length; i++)
-            index[skip + i] = index8[i];
-      }
-      else if (rb->DataType == GL_UNSIGNED_SHORT) {
-         GLushort index16[MAX_WIDTH];
-         GLint i;
-         rb->GetRow(ctx, rb, length, x + skip, y, index16);
-         for (i = 0; i < length; i++)
-            index[skip + i] = index16[i];
-      }
-      else if (rb->DataType == GL_UNSIGNED_INT) {
-         rb->GetRow(ctx, rb, length, x + skip, y, index + skip);
-      }
-   }
-}
-
-
 /**
  * Wrapper for gl_renderbuffer::GetValues() which does clipping to avoid
  * reading values outside the buffer bounds.
@@ -1672,7 +1376,7 @@ _swrast_read_index_span( GLcontext *ctx, struct gl_renderbuffer *rb,
  *                  values array.
  */
 void
-_swrast_get_values(GLcontext *ctx, struct gl_renderbuffer *rb,
+_swrast_get_values(struct gl_context *ctx, struct gl_renderbuffer *rb,
                    GLuint count, const GLint x[], const GLint y[],
                    void *values, GLuint valueSize)
 {
@@ -1708,7 +1412,7 @@ _swrast_get_values(GLcontext *ctx, struct gl_renderbuffer *rb,
  * \param valueSize  size of each value (pixel) in bytes
  */
 void
-_swrast_put_row(GLcontext *ctx, struct gl_renderbuffer *rb,
+_swrast_put_row(struct gl_context *ctx, struct gl_renderbuffer *rb,
                 GLuint count, GLint x, GLint y,
                 const GLvoid *values, GLuint valueSize)
 {
@@ -1743,7 +1447,7 @@ _swrast_put_row(GLcontext *ctx, struct gl_renderbuffer *rb,
  * \param valueSize  size of each value (pixel) in bytes
  */
 void
-_swrast_get_row(GLcontext *ctx, struct gl_renderbuffer *rb,
+_swrast_get_row(struct gl_context *ctx, struct gl_renderbuffer *rb,
                 GLuint count, GLint x, GLint y,
                 GLvoid *values, GLuint valueSize)
 {
@@ -1778,7 +1482,7 @@ _swrast_get_row(GLcontext *ctx, struct gl_renderbuffer *rb,
  * \return pointer to the colors we read.
  */
 void *
-_swrast_get_dest_rgba(GLcontext *ctx, struct gl_renderbuffer *rb,
+_swrast_get_dest_rgba(struct gl_context *ctx, struct gl_renderbuffer *rb,
                       SWspan *span)
 {
    const GLuint pixelSize = RGBA_PIXEL_SIZE(span->array->ChanType);