Merge branch 'origin' into i915-unification
[mesa.git] / src / mesa / swrast / s_copypix.c
index 03887a9e14ca582657e2b3fa36184f7a6b73970f..2383015000aaf30b1b01797b2d3e9562fae71e30 100644 (file)
@@ -1,21 +1,19 @@
-/* $Id: s_copypix.c,v 1.4 2000/11/10 18:29:18 brianp Exp $ */
-
 /*
  * Mesa 3-D graphics library
- * Version:  3.5
- * 
- * Copyright (C) 1999-2000  Brian Paul   All Rights Reserved.
- * 
+ * Version:  6.5.3
+ *
+ * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
+ *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * to deal in the Software without restriction, including without limitation
  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  * and/or sell copies of the Software, and to permit persons to whom the
  * Software is furnished to do so, subject to the following conditions:
- * 
+ *
  * The above copyright notice and this permission notice shall be included
  * in all copies or substantial portions of the Software.
- * 
+ *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 
 
 #include "glheader.h"
-#include "colormac.h"
 #include "context.h"
+#include "colormac.h"
 #include "convolve.h"
-#include "feedback.h"
+#include "histogram.h"
+#include "image.h"
 #include "macros.h"
-#include "mem.h"
-#include "mmath.h"
+#include "imports.h"
 #include "pixel.h"
 
 #include "s_context.h"
 #include "s_depth.h"
-#include "s_histogram.h"
-#include "s_pixeltex.h"
 #include "s_span.h"
 #include "s_stencil.h"
-#include "s_texture.h"
 #include "s_zoom.h"
 
 
 
-/*
- * Determine if there's overlap in an image copy
+/**
+ * Determine if there's overlap in an image copy.
+ * This test also compensates for the fact that copies are done from
+ * bottom to top and overlaps can sometimes be handled correctly
+ * without making a temporary image copy.
+ * \return GL_TRUE if the regions overlap, GL_FALSE otherwise.
  */
 static GLboolean
-regions_overlap(int srcx, int srcy, int dstx, int dsty, int width, int height,
-                float zoomX, float zoomY)
+regions_overlap(GLint srcx, GLint srcy,
+                GLint dstx, GLint dsty,
+                GLint width, GLint height,
+                GLfloat zoomX, GLfloat zoomY)
 {
-   if ((srcx > dstx + (width * zoomX) + 1) || (srcx + width + 1 < dstx)) {
-      return GL_FALSE;
-   }
-   else if ((srcy < dsty) && (srcy + height < dsty + (height * zoomY))) {
-      return GL_FALSE;
-   }
-   else if ((srcy > dsty) && (srcy + height > dsty + (height * zoomY))) {
-      return GL_FALSE;
+   if (zoomX == 1.0 && zoomY == 1.0) {
+      /* no zoom */
+      if (srcx >= dstx + width || (srcx + width <= dstx)) {
+         return GL_FALSE;
+      }
+      else if (srcy < dsty) { /* this is OK */
+         return GL_FALSE;
+      }
+      else if (srcy > dsty + height) {
+         return GL_FALSE;
+      }
+      else {
+         return GL_TRUE;
+      }
    }
    else {
-      return GL_TRUE;
+      /* add one pixel of slop when zooming, just to be safe */
+      if ((srcx > dstx + (width * zoomX) + 1) || (srcx + width + 1 < dstx)) {
+         return GL_FALSE;
+      }
+      else if ((srcy < dsty) && (srcy + height < dsty + (height * zoomY))) {
+         return GL_FALSE;
+      }
+      else if ((srcy > dsty) && (srcy + height > dsty + (height * zoomY))) {
+         return GL_FALSE;
+      }
+      else {
+         return GL_TRUE;
+      }
    }
 }
 
 
-
-/*
+/**
  * RGBA copypixels with convolution.
  */
 static void
 copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
                       GLint width, GLint height, GLint destx, GLint desty)
 {
-   GLdepth zspan[MAX_WIDTH];
-   GLboolean quick_draw;
    GLint row;
-   GLboolean changeBuffer;
-   GLchan *saveReadAlpha;
    const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
-   const GLuint transferOps = ctx->_ImageTransferState;
+   const GLbitfield transferOps = ctx->_ImageTransferState;
+   const GLboolean sink = (ctx->Pixel.MinMaxEnabled && ctx->MinMax.Sink)
+      || (ctx->Pixel.HistogramEnabled && ctx->Histogram.Sink);
    GLfloat *dest, *tmpImage, *convImage;
+   SWspan span;
 
-   if (ctx->Depth.Test || ctx->Fog.Enabled) {
-      /* fill in array of z values */
-      GLdepth z = (GLdepth)
-         (ctx->Current.RasterPos[2] * ctx->Visual.DepthMax);
-      GLint i;
-      for (i = 0; i < width; i++) {
-         zspan[i] = z;
-      }
-   }
-
-   if (SWRAST_CONTEXT(ctx)->_RasterMask == 0
-       && !zoom
-       && destx >= 0
-       && destx + width <= ctx->DrawBuffer->Width) {
-      quick_draw = GL_TRUE;
-   }
-   else {
-      quick_draw = GL_FALSE;
-   }
-
-   /* If read and draw buffer are different we must do buffer switching */
-   saveReadAlpha = ctx->ReadBuffer->Alpha;
-   changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer
-               || ctx->DrawBuffer != ctx->ReadBuffer;
-
+   INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_RGBA);
+   _swrast_span_default_attribs(ctx, &span);
 
    /* allocate space for GLfloat image */
-   tmpImage = (GLfloat *) MALLOC(width * height * 4 * sizeof(GLfloat));
+   tmpImage = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
    if (!tmpImage) {
-      gl_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
+      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
       return;
    }
-   convImage = (GLfloat *) MALLOC(width * height * 4 * sizeof(GLfloat));
+   convImage = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
    if (!convImage) {
-      FREE(tmpImage);
-      gl_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
+      _mesa_free(tmpImage);
+      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
       return;
    }
 
-   dest = tmpImage;
-
-   if (changeBuffer) {
-      (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
-                                    ctx->Pixel.DriverReadBuffer );
-      if (ctx->Pixel.DriverReadBuffer == GL_FRONT_LEFT)
-         ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontLeftAlpha;
-      else if (ctx->Pixel.DriverReadBuffer == GL_BACK_LEFT)
-         ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackLeftAlpha;
-      else if (ctx->Pixel.DriverReadBuffer == GL_FRONT_RIGHT)
-         ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontRightAlpha;
-      else
-         ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackRightAlpha;
-   }
-
-   /* read source image */
+   /* read source image as float/RGBA */
    dest = tmpImage;
    for (row = 0; row < height; row++) {
-      GLchan rgba[MAX_WIDTH][4];
-      GLint i;
-      gl_read_rgba_span(ctx, ctx->ReadBuffer, width, srcx, srcy + row, rgba);
-      /* convert GLchan to GLfloat */
-      for (i = 0; i < width; i++) {
-         *dest++ = (GLfloat) rgba[i][RCOMP] * (1.0F / CHAN_MAXF);
-         *dest++ = (GLfloat) rgba[i][GCOMP] * (1.0F / CHAN_MAXF);
-         *dest++ = (GLfloat) rgba[i][BCOMP] * (1.0F / CHAN_MAXF);
-         *dest++ = (GLfloat) rgba[i][ACOMP] * (1.0F / CHAN_MAXF);
-      }
+      _swrast_read_rgba_span(ctx, ctx->ReadBuffer->_ColorReadBuffer,
+                             width, srcx, srcy + row, GL_FLOAT, dest);
+      dest += 4 * width;
    }
 
-   /* read from the draw buffer again (in case of blending) */
-   if (changeBuffer) {
-      (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
-                                    ctx->Color.DriverDrawBuffer );
-      ctx->ReadBuffer->Alpha = saveReadAlpha;
-   }
-
-   /* do image transfer ops up until convolution */
+   /* do the image transfer ops which preceed convolution */
    for (row = 0; row < height; row++) {
-      GLfloat (*rgba)[4] = (GLfloat (*)[4]) tmpImage + row * width * 4;
-
-      /* scale & bias */
-      if (transferOps & IMAGE_SCALE_BIAS_BIT) {
-         _mesa_scale_and_bias_rgba(ctx, width, rgba);
-      }
-      /* color map lookup */
-      if (transferOps & IMAGE_MAP_COLOR_BIT) {
-         _mesa_map_rgba(ctx, width, rgba);
-      }
-      /* GL_COLOR_TABLE lookup */
-      if (transferOps & IMAGE_COLOR_TABLE_BIT) {
-         _mesa_lookup_rgba(&ctx->ColorTable, width, rgba);
-      }
+      GLfloat (*rgba)[4] = (GLfloat (*)[4]) (tmpImage + row * width * 4);
+      _mesa_apply_rgba_transfer_ops(ctx,
+                                    transferOps & IMAGE_PRE_CONVOLUTION_BITS,
+                                    width, rgba);
    }
 
    /* do convolution */
@@ -187,113 +142,89 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
       ASSERT(ctx->Pixel.Separable2DEnabled);
       _mesa_convolve_sep_image(ctx, &width, &height, tmpImage, convImage);
    }
-   FREE(tmpImage);
+   _mesa_free(tmpImage);
 
-   /* do remaining image transfer ops */
+   /* do remaining post-convolution image transfer ops */
    for (row = 0; row < height; row++) {
-      GLfloat (*rgba)[4] = (GLfloat (*)[4]) convImage + row * width * 4;
-
-      /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
-      if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
-         _mesa_lookup_rgba(&ctx->PostConvolutionColorTable, width, rgba);
-      }
-      /* color matrix */
-      if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
-         _mesa_transform_rgba(ctx, width, rgba);
-      }
-      /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
-      if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
-         _mesa_lookup_rgba(&ctx->PostColorMatrixColorTable, width, rgba);
-      }
-      /* update histogram count */
-      if (transferOps & IMAGE_HISTOGRAM_BIT) {
-         _mesa_update_histogram(ctx, width, (CONST GLfloat (*)[4]) rgba);
-      }
-      /* update min/max */
-      if (transferOps & IMAGE_MIN_MAX_BIT) {
-         _mesa_update_minmax(ctx, width, (CONST GLfloat (*)[4]) rgba);
-      }
-   }
-
-   for (row = 0; row < height; row++) {
-      const GLfloat *src = convImage + row * width * 4;
-      GLchan rgba[MAX_WIDTH][4];
-      GLint i, dy;
-
-      /* clamp to [0,1] and convert float back to chan */
-      for (i = 0; i < width; i++) {
-         GLint r = (GLint) (src[i * 4 + RCOMP] * CHAN_MAXF);
-         GLint g = (GLint) (src[i * 4 + GCOMP] * CHAN_MAXF);
-         GLint b = (GLint) (src[i * 4 + BCOMP] * CHAN_MAXF);
-         GLint a = (GLint) (src[i * 4 + ACOMP] * CHAN_MAXF);
-         rgba[i][RCOMP] = (GLchan) CLAMP(r, 0, CHAN_MAX);
-         rgba[i][GCOMP] = (GLchan) CLAMP(g, 0, CHAN_MAX);
-         rgba[i][BCOMP] = (GLchan) CLAMP(b, 0, CHAN_MAX);
-         rgba[i][ACOMP] = (GLchan) CLAMP(a, 0, CHAN_MAX);
-      }
-
-      if (ctx->Texture._ReallyEnabled && ctx->Pixel.PixelTextureEnabled) {
-         GLfloat s[MAX_WIDTH], t[MAX_WIDTH], r[MAX_WIDTH], q[MAX_WIDTH];
-         GLchan primary_rgba[MAX_WIDTH][4];
-         GLuint unit;
-         /* XXX not sure how multitexture is supposed to work here */
-
-         MEMCPY(primary_rgba, rgba, 4 * width * sizeof(GLchan));
-
-         for (unit = 0; unit < MAX_TEXTURE_UNITS; unit++) {
-            _mesa_pixeltexgen(ctx, width, (const GLchan (*)[4]) rgba,
-                              s, t, r, q);
-            gl_texture_pixels(ctx, unit, width, s, t, r, NULL,
-                              primary_rgba, rgba);
+      GLfloat (*rgba)[4] = (GLfloat (*)[4]) (convImage + row * width * 4);
+      _mesa_apply_rgba_transfer_ops(ctx,
+                                    transferOps & IMAGE_POST_CONVOLUTION_BITS,
+                                    width, rgba);
+   }
+
+   if (!sink) {
+      /* write the new image */
+      for (row = 0; row < height; row++) {
+         const GLfloat *src = convImage + row * width * 4;
+         GLvoid *rgba = (GLvoid *) span.array->attribs[FRAG_ATTRIB_COL0];
+
+         /* copy convolved colors into span array */
+         _mesa_memcpy(rgba, src, width * 4 * sizeof(GLfloat));
+
+         /* write span */
+         span.x = destx;
+         span.y = desty + row;
+         span.end = width;
+         span.array->ChanType = GL_FLOAT;
+         if (zoom) {
+            _swrast_write_zoomed_rgba_span(ctx, destx, desty, &span, rgba);
+         }
+         else {
+            _swrast_write_rgba_span(ctx, &span);
          }
       }
-
-      /* write row to framebuffer */
-
-      dy = desty + row;
-      if (quick_draw && dy >= 0 && dy < ctx->DrawBuffer->Height) {
-         (*ctx->Driver.WriteRGBASpan)( ctx, width, destx, dy, 
-                                      (const GLchan (*)[4])rgba, NULL );
-      }
-      else if (zoom) {
-         gl_write_zoomed_rgba_span( ctx, width, destx, dy, zspan, 0, 
-                                   (const GLchan (*)[4])rgba, desty);
-      }
-      else {
-         gl_write_rgba_span( ctx, width, destx, dy, zspan, 0, rgba, GL_BITMAP );
-      }
+      /* restore this */
+      span.array->ChanType = CHAN_TYPE;
    }
 
-   FREE(convImage);
+   _mesa_free(convImage);
 }
 
 
-/*
+/**
  * RGBA copypixels
  */
 static void
 copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
                  GLint width, GLint height, GLint destx, GLint desty)
 {
-   GLdepth zspan[MAX_WIDTH];
-   GLchan rgba[MAX_WIDTH][4];
-   GLchan *tmpImage,*p;
-   GLboolean quick_draw;
-   GLint sy, dy, stepy;
-   GLint i, j;
-   GLboolean changeBuffer;
-   GLchan *saveReadAlpha;
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
+   const GLbitfield prevActiveAttribs = swrast->_ActiveAttribMask;
+   GLfloat *tmpImage, *p;
+   GLint sy, dy, stepy, row;
    const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
    GLint overlapping;
-   const GLuint transferOps = ctx->_ImageTransferState;
+   GLuint transferOps = ctx->_ImageTransferState;
+   SWspan span;
+
+   if (!ctx->ReadBuffer->_ColorReadBuffer) {
+      /* no readbuffer - OK */
+      goto end;
+   }
+
+   /* don't interpolate COL0 and overwrite the glDrawPixel colors! */
+   swrast->_ActiveAttribMask &= ~FRAG_BIT_COL0;
 
    if (ctx->Pixel.Convolution2DEnabled || ctx->Pixel.Separable2DEnabled) {
       copy_conv_rgba_pixels(ctx, srcx, srcy, width, height, destx, desty);
-      return;
+      goto end;
+   }
+   else if (ctx->Pixel.Convolution1DEnabled) {
+      /* make sure we don't apply 1D convolution */
+      transferOps &= ~(IMAGE_CONVOLUTION_BIT |
+                       IMAGE_POST_CONVOLUTION_SCALE_BIAS);
+   }
+
+   if (ctx->DrawBuffer == ctx->ReadBuffer) {
+      overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
+                                    ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
+   }
+   else {
+      overlapping = GL_FALSE;
    }
 
    /* Determine if copy should be done bottom-to-top or top-to-bottom */
-   if (srcy < desty) {
+   if (!overlapping && srcy < desty) {
       /* top-down  max-to-min */
       sy = srcy + height - 1;
       dy = desty + height - 1;
@@ -306,59 +237,21 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
       stepy = 1;
    }
 
-   overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
-                                 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
-
-   if (ctx->Depth.Test || ctx->Fog.Enabled) {
-      /* fill in array of z values */
-      GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * ctx->Visual.DepthMax);
-      for (i=0;i<width;i++) {
-         zspan[i] = z;
-      }
-   }
-
-   if (SWRAST_CONTEXT(ctx)->_RasterMask == 0
-       && !zoom
-       && destx >= 0
-       && destx + width <= ctx->DrawBuffer->Width) {
-      quick_draw = GL_TRUE;
-   }
-   else {
-      quick_draw = GL_FALSE;
-   }
-
-   /* If read and draw buffer are different we must do buffer switching */
-   saveReadAlpha = ctx->ReadBuffer->Alpha;
-   changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer
-                  || ctx->DrawBuffer != ctx->ReadBuffer;
-
-   (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
-                                 ctx->Pixel.DriverReadBuffer );
+   INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_RGBA);
+   _swrast_span_default_attribs(ctx, &span);
 
    if (overlapping) {
-      GLint ssy = sy;
-      tmpImage = (GLchan *) MALLOC(width * height * sizeof(GLchan) * 4);
+      tmpImage = (GLfloat *) _mesa_malloc(width * height * sizeof(GLfloat) * 4);
       if (!tmpImage) {
-         gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
-         return;
+         _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
+         goto end;
       }
+      /* read the source image as RGBA/float */
       p = tmpImage;
-      if (changeBuffer) {
-         (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
-                                       ctx->Pixel.DriverReadBuffer );
-         if (ctx->Pixel.DriverReadBuffer == GL_FRONT_LEFT)
-            ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontLeftAlpha;
-         else if (ctx->Pixel.DriverReadBuffer == GL_BACK_LEFT)
-            ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackLeftAlpha;
-         else if (ctx->Pixel.DriverReadBuffer == GL_FRONT_RIGHT)
-            ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontRightAlpha;
-         else
-            ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackRightAlpha;
-      }
-      for (j = 0; j < height; j++, ssy += stepy) {
-         gl_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, ssy,
-                            (GLchan (*)[4]) p );
-         p += (width * sizeof(GLchan) * 4);
+      for (row = 0; row < height; row++) {
+         _swrast_read_rgba_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
+                                 width, srcx, sy + row, GL_FLOAT, p );
+         p += width * 4;
       }
       p = tmpImage;
    }
@@ -367,154 +260,81 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
       p = NULL;
    }
 
-   for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
-      /* Get source pixels */
+   ASSERT(width < MAX_WIDTH);
+
+   for (row = 0; row < height; row++, sy += stepy, dy += stepy) {
+      GLvoid *rgba = span.array->attribs[FRAG_ATTRIB_COL0];
+
+      /* Get row/span of source pixels */
       if (overlapping) {
          /* get from buffered image */
-         MEMCPY(rgba, p, width * sizeof(GLchan) * 4);
-         p += (width * sizeof(GLchan) * 4);
+         _mesa_memcpy(rgba, p, width * sizeof(GLfloat) * 4);
+         p += width * 4;
       }
       else {
          /* get from framebuffer */
-         if (changeBuffer) {
-            (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
-                                          ctx->Pixel.DriverReadBuffer );
-            if (ctx->Pixel.DriverReadBuffer == GL_FRONT_LEFT) {
-               ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontLeftAlpha;
-            }
-            else if (ctx->Pixel.DriverReadBuffer == GL_BACK_LEFT) {
-               ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackLeftAlpha;
-            }
-            else if (ctx->Pixel.DriverReadBuffer == GL_FRONT_RIGHT) {
-               ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontRightAlpha;
-            }
-            else {
-               ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackRightAlpha;
-            }
-         }
-         gl_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, sy, rgba );
-      }
-
-      if (changeBuffer) {
-         /* read from the draw buffer again (in case of blending) */
-         (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
-                                       ctx->Color.DriverDrawBuffer );
-         ctx->ReadBuffer->Alpha = saveReadAlpha;
+         _swrast_read_rgba_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
+                                 width, srcx, sy, GL_FLOAT, rgba );
       }
 
       if (transferOps) {
-         const GLfloat scale = (1.0F / CHAN_MAXF);
-         GLfloat rgbaFloat[MAX_WIDTH][4];
-         GLuint k;
-         /* convert chan to float */
-         for (k = 0; k < width; k++) {
-            rgbaFloat[k][RCOMP] = (GLfloat) rgba[k][RCOMP] * scale;
-            rgbaFloat[k][GCOMP] = (GLfloat) rgba[k][GCOMP] * scale;
-            rgbaFloat[k][BCOMP] = (GLfloat) rgba[k][BCOMP] * scale;
-            rgbaFloat[k][ACOMP] = (GLfloat) rgba[k][ACOMP] * scale;
-         }
-         /* scale & bias */
-         if (transferOps & IMAGE_SCALE_BIAS_BIT) {
-            _mesa_scale_and_bias_rgba(ctx, width, rgbaFloat);
-         }
-         /* color map lookup */
-         if (transferOps & IMAGE_MAP_COLOR_BIT) {
-            _mesa_map_rgba(ctx, width, rgbaFloat);
-         }
-         /* GL_COLOR_TABLE lookup */
-         if (transferOps & IMAGE_COLOR_TABLE_BIT) {
-            _mesa_lookup_rgba(&ctx->ColorTable, width, rgbaFloat);
-         }
-         /* convolution */
-         if (transferOps & IMAGE_CONVOLUTION_BIT) {
-            /* XXX to do */
-         }
-         /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
-         if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
-            _mesa_lookup_rgba(&ctx->PostConvolutionColorTable, width, rgbaFloat);
-         }
-         /* color matrix */
-         if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
-            _mesa_transform_rgba(ctx, width, rgbaFloat);
-         }
-         /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
-         if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
-            _mesa_lookup_rgba(&ctx->PostColorMatrixColorTable, width, rgbaFloat);
-         }
-         /* update histogram count */
-         if (transferOps & IMAGE_HISTOGRAM_BIT) {
-            _mesa_update_histogram(ctx, width, (CONST GLfloat (*)[4]) rgbaFloat);
-         }
-         /* update min/max */
-         if (transferOps & IMAGE_MIN_MAX_BIT) {
-            _mesa_update_minmax(ctx, width, (CONST GLfloat (*)[4]) rgbaFloat);
-         }
-         /* clamp to [0,1] and convert float back to chan */
-         for (k = 0; k < width; k++) {
-            GLint r = (GLint) (rgbaFloat[k][RCOMP] * CHAN_MAXF);
-            GLint g = (GLint) (rgbaFloat[k][GCOMP] * CHAN_MAXF);
-            GLint b = (GLint) (rgbaFloat[k][BCOMP] * CHAN_MAXF);
-            GLint a = (GLint) (rgbaFloat[k][ACOMP] * CHAN_MAXF);
-            rgba[k][RCOMP] = (GLchan) CLAMP(r, 0, CHAN_MAX);
-            rgba[k][GCOMP] = (GLchan) CLAMP(g, 0, CHAN_MAX);
-            rgba[k][BCOMP] = (GLchan) CLAMP(b, 0, CHAN_MAX);
-            rgba[k][ACOMP] = (GLchan) CLAMP(a, 0, CHAN_MAX);
-         }
+         _mesa_apply_rgba_transfer_ops(ctx, transferOps, width,
+                                       (GLfloat (*)[4]) rgba);
       }
 
-      if (ctx->Texture._ReallyEnabled && ctx->Pixel.PixelTextureEnabled) {
-         GLfloat s[MAX_WIDTH], t[MAX_WIDTH], r[MAX_WIDTH], q[MAX_WIDTH];
-         GLchan primary_rgba[MAX_WIDTH][4];
-         GLuint unit;
-         /* XXX not sure how multitexture is supposed to work here */
-
-         MEMCPY(primary_rgba, rgba, 4 * width * sizeof(GLchan));
-
-         for (unit = 0; unit < MAX_TEXTURE_UNITS; unit++) {
-            _mesa_pixeltexgen(ctx, width, (const GLchan (*)[4]) rgba,
-                              s, t, r, q);
-            gl_texture_pixels(ctx, unit, width, s, t, r, NULL,
-                              primary_rgba, rgba);
-         }
-      }
-
-      if (quick_draw && dy >= 0 && dy < ctx->DrawBuffer->Height) {
-         (*ctx->Driver.WriteRGBASpan)( ctx, width, destx, dy, 
-                                      (const GLchan (*)[4])rgba, NULL );
-      }
-      else if (zoom) {
-         gl_write_zoomed_rgba_span( ctx, width, destx, dy, zspan, 0,
-                                   (const GLchan (*)[4])rgba, desty);
+      /* Write color span */
+      span.x = destx;
+      span.y = dy;
+      span.end = width;
+      span.array->ChanType = GL_FLOAT;
+      if (zoom) {
+         _swrast_write_zoomed_rgba_span(ctx, destx, desty, &span, rgba);
       }
       else {
-         gl_write_rgba_span( ctx, width, destx, dy, zspan, 0, rgba, GL_BITMAP );
+         _swrast_write_rgba_span(ctx, &span);
       }
    }
 
-   /* Restore pixel source to be the draw buffer (for blending, etc) */
-   (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
-                                 ctx->Color.DriverDrawBuffer );
+   span.array->ChanType = CHAN_TYPE; /* restore */
 
    if (overlapping)
-      FREE(tmpImage);
+      _mesa_free(tmpImage);
+
+end:
+   swrast->_ActiveAttribMask = prevActiveAttribs;
 }
 
 
-static void copy_ci_pixels( GLcontext *ctx,
-                            GLint srcx, GLint srcy, GLint width, GLint height,
-                            GLint destx, GLint desty )
+static void
+copy_ci_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
+                GLint width, GLint height,
+                GLint destx, GLint desty )
 {
-   GLdepth zspan[MAX_WIDTH];
    GLuint *tmpImage,*p;
    GLint sy, dy, stepy;
-   GLint i, j;
-   GLboolean changeBuffer;
+   GLint j;
    const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
-   const GLboolean shift_or_offset = ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset;
    GLint overlapping;
+   SWspan span;
+
+   if (!ctx->ReadBuffer->_ColorReadBuffer) {
+      /* no readbuffer - OK */
+      return;
+   }
+
+   INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_INDEX);
+   _swrast_span_default_attribs(ctx, &span);
+
+   if (ctx->DrawBuffer == ctx->ReadBuffer) {
+      overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
+                                    ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
+   }
+   else {
+      overlapping = GL_FALSE;
+   }
 
    /* Determine if copy should be bottom-to-top or top-to-bottom */
-   if (srcy<desty) {
+   if (!overlapping && srcy < desty) {
       /* top-down  max-to-min */
       sy = srcy + height - 1;
       dy = desty + height - 1;
@@ -527,38 +347,18 @@ static void copy_ci_pixels( GLcontext *ctx,
       stepy = 1;
    }
 
-   overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
-                                 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
-
-   if (ctx->Depth.Test || ctx->Fog.Enabled) {
-      /* fill in array of z values */
-      GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * ctx->Visual.DepthMax);
-      for (i=0;i<width;i++) {
-         zspan[i] = z;
-      }
-   }
-
-   /* If read and draw buffer are different we must do buffer switching */
-   changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer
-               || ctx->DrawBuffer != ctx->ReadBuffer;
-
-   (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
-                                 ctx->Pixel.DriverReadBuffer );
-
    if (overlapping) {
       GLint ssy = sy;
-      tmpImage = (GLuint *) MALLOC(width * height * sizeof(GLuint));
+      tmpImage = (GLuint *) _mesa_malloc(width * height * sizeof(GLuint));
       if (!tmpImage) {
-         gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
+         _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
          return;
       }
+      /* read the image */
       p = tmpImage;
-      if (changeBuffer) {
-         (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
-                                       ctx->Pixel.DriverReadBuffer );
-      }
       for (j = 0; j < height; j++, ssy += stepy) {
-         gl_read_index_span( ctx, ctx->ReadBuffer, width, srcx, ssy, p );
+         _swrast_read_index_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
+                                  width, srcx, ssy, p );
          p += width;
       }
       p = tmpImage;
@@ -569,46 +369,67 @@ static void copy_ci_pixels( GLcontext *ctx,
    }
 
    for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
-      GLuint indexes[MAX_WIDTH];
+      /* Get color indexes */
       if (overlapping) {
-         MEMCPY(indexes, p, width * sizeof(GLuint));
+         _mesa_memcpy(span.array->index, p, width * sizeof(GLuint));
          p += width;
       }
       else {
-         if (changeBuffer) {
-            (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
-                                          ctx->Pixel.DriverReadBuffer );
-         }
-         gl_read_index_span( ctx, ctx->ReadBuffer, width, srcx, sy, indexes );
+         _swrast_read_index_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
+                                  width, srcx, sy, span.array->index );
       }
 
-      if (changeBuffer) {
-         /* set read buffer back to draw buffer (in case of logicops) */
-         (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
-                                       ctx->Color.DriverDrawBuffer );
-      }
+      if (ctx->_ImageTransferState)
+         _mesa_apply_ci_transfer_ops(ctx, ctx->_ImageTransferState,
+                                     width, span.array->index);
 
-      if (shift_or_offset) {
-         _mesa_shift_and_offset_ci( ctx, width, indexes );
-      }
-      if (ctx->Pixel.MapColorFlag) {
-         _mesa_map_ci( ctx, width, indexes );
-      }
+      /* write color indexes */
+      span.x = destx;
+      span.y = dy;
+      span.end = width;
+      if (zoom)
+         _swrast_write_zoomed_index_span(ctx, destx, desty, &span);
+      else
+         _swrast_write_index_span(ctx, &span);
+   }
 
-      if (zoom) {
-         gl_write_zoomed_index_span( ctx, width, destx, dy, zspan, 0, indexes, desty );
+   if (overlapping)
+      _mesa_free(tmpImage);
+}
+
+
+/**
+ * Convert floating point Z values to integer Z values with pixel transfer's
+ * Z scale and bias.
+ */
+static void
+scale_and_bias_z(GLcontext *ctx, GLuint width,
+                 const GLfloat depth[], GLuint z[])
+{
+   const GLuint depthMax = ctx->DrawBuffer->_DepthMax;
+   GLuint i;
+
+   if (depthMax <= 0xffffff &&
+       ctx->Pixel.DepthScale == 1.0 &&
+       ctx->Pixel.DepthBias == 0.0) {
+      /* no scale or bias and no clamping and no worry of overflow */
+      const GLfloat depthMaxF = ctx->DrawBuffer->_DepthMaxF;
+      for (i = 0; i < width; i++) {
+         z[i] = (GLuint) (depth[i] * depthMaxF);
       }
-      else {
-         gl_write_index_span(ctx, width, destx, dy, zspan, 0, indexes, GL_BITMAP);
+   }
+   else {
+      /* need to be careful with overflow */
+      const GLdouble depthMaxF = ctx->DrawBuffer->_DepthMaxF;
+      for (i = 0; i < width; i++) {
+         GLdouble d = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
+         d = CLAMP(d, 0.0, 1.0) * depthMaxF;
+         if (d >= depthMaxF)
+            z[i] = depthMax;
+         else
+            z[i] = (GLuint) d;
       }
    }
-
-   /* Restore pixel source to be the draw buffer (for blending, etc) */
-   (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
-                                 ctx->Color.DriverDrawBuffer );
-
-   if (overlapping)
-      FREE(tmpImage);
 }
 
 
@@ -616,27 +437,38 @@ static void copy_ci_pixels( GLcontext *ctx,
 /*
  * TODO: Optimize!!!!
  */
-static void copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
-                               GLint width, GLint height,
-                               GLint destx, GLint desty )
+static void
+copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
+                   GLint width, GLint height,
+                   GLint destx, GLint desty )
 {
-   GLfloat depth[MAX_WIDTH];
-   GLdepth zspan[MAX_WIDTH];
+   struct gl_framebuffer *fb = ctx->ReadBuffer;
+   struct gl_renderbuffer *readRb = fb->_DepthBuffer;
    GLfloat *p, *tmpImage;
-   GLuint indexes[MAX_WIDTH];
-   GLchan rgba[MAX_WIDTH][4];
    GLint sy, dy, stepy;
-   GLint i, j;
+   GLint j;
    const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
    GLint overlapping;
+   SWspan span;
 
-   if (!ctx->ReadBuffer->DepthBuffer || !ctx->DrawBuffer->DepthBuffer) {
-      gl_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
+   if (!readRb) {
+      /* no readbuffer - OK */
       return;
    }
 
+   INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_Z);
+   _swrast_span_default_attribs(ctx, &span);
+
+   if (ctx->DrawBuffer == ctx->ReadBuffer) {
+      overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
+                                    ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
+   }
+   else {
+      overlapping = GL_FALSE;
+   }
+
    /* Determine if copy should be bottom-to-top or top-to-bottom */
-   if (srcy<desty) {
+   if (!overlapping && srcy < desty) {
       /* top-down  max-to-min */
       sy = srcy + height - 1;
       dy = desty + height - 1;
@@ -649,33 +481,16 @@ static void copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
       stepy = 1;
    }
 
-   overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
-                                 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
-
-   /* setup colors or indexes */
-   if (ctx->Visual.RGBAflag) {
-      GLuint *rgba32 = (GLuint *) rgba;
-      GLuint color = *(GLuint*)( ctx->Current.Color );
-      for (i = 0; i < width; i++) {
-         rgba32[i] = color;
-      }
-   }
-   else {
-      for (i = 0; i < width; i++) {
-         indexes[i] = ctx->Current.Index;
-      }
-   }
-
    if (overlapping) {
       GLint ssy = sy;
-      tmpImage = (GLfloat *) MALLOC(width * height * sizeof(GLfloat));
+      tmpImage = (GLfloat *) _mesa_malloc(width * height * sizeof(GLfloat));
       if (!tmpImage) {
-         gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
+         _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
          return;
       }
       p = tmpImage;
       for (j = 0; j < height; j++, ssy += stepy) {
-         _mesa_read_depth_span_float(ctx, width, srcx, ssy, p);
+         _swrast_read_depth_span_float(ctx, readRb, width, srcx, ssy, p);
          p += width;
       }
       p = tmpImage;
@@ -686,65 +501,71 @@ static void copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
    }
 
    for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
+      GLfloat depth[MAX_WIDTH];
+      /* get depth values */
       if (overlapping) {
-         MEMCPY(depth, p, width * sizeof(GLfloat));
+         _mesa_memcpy(depth, p, width * sizeof(GLfloat));
          p += width;
       }
       else {
-         _mesa_read_depth_span_float(ctx, width, srcx, sy, depth);
+         _swrast_read_depth_span_float(ctx, readRb, width, srcx, sy, depth);
       }
 
-      for (i = 0; i < width; i++) {
-         GLfloat d = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
-         zspan[i] = (GLdepth) (CLAMP(d, 0.0F, 1.0F) * ctx->Visual.DepthMax);
-      }
+      /* apply scale and bias */
+      scale_and_bias_z(ctx, width, depth, span.array->z);
 
-      if (ctx->Visual.RGBAflag) {
-         if (zoom) {
-            gl_write_zoomed_rgba_span( ctx, width, destx, dy, zspan, 0,
-                                      (const GLchan (*)[4])rgba, desty );
-         }
-         else {
-            gl_write_rgba_span( ctx, width, destx, dy, zspan, 0, 
-                               rgba, GL_BITMAP);
-         }
+      /* write depth values */
+      span.x = destx;
+      span.y = dy;
+      span.end = width;
+      if (fb->Visual.rgbMode) {
+         if (zoom)
+            _swrast_write_zoomed_depth_span(ctx, destx, desty, &span);
+         else
+            _swrast_write_rgba_span(ctx, &span);
       }
       else {
-         if (zoom) {
-            gl_write_zoomed_index_span( ctx, width, destx, dy,
-                                        zspan, 0, indexes, desty );
-         }
-         else {
-            gl_write_index_span( ctx, width, destx, dy,
-                                 zspan, 0, indexes, GL_BITMAP );
-         }
+         if (zoom)
+            _swrast_write_zoomed_depth_span(ctx, destx, desty, &span);
+         else
+            _swrast_write_index_span(ctx, &span);
       }
    }
 
-  if (overlapping)
-     FREE(tmpImage);
+   if (overlapping)
+      _mesa_free(tmpImage);
 }
 
 
 
-static void copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
-                                 GLint width, GLint height,
-                                 GLint destx, GLint desty )
+static void
+copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
+                     GLint width, GLint height,
+                     GLint destx, GLint desty )
 {
+   struct gl_framebuffer *fb = ctx->ReadBuffer;
+   struct gl_renderbuffer *rb = fb->_StencilBuffer;
    GLint sy, dy, stepy;
    GLint j;
    GLstencil *p, *tmpImage;
    const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
-   const GLboolean shift_or_offset = ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset;
    GLint overlapping;
 
-   if (!ctx->DrawBuffer->Stencil || !ctx->ReadBuffer->Stencil) {
-      gl_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
+   if (!rb) {
+      /* no readbuffer - OK */
       return;
    }
 
+   if (ctx->DrawBuffer == ctx->ReadBuffer) {
+      overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
+                                    ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
+   }
+   else {
+      overlapping = GL_FALSE;
+   }
+
    /* Determine if copy should be bottom-to-top or top-to-bottom */
-   if (srcy < desty) {
+   if (!overlapping && srcy < desty) {
       /* top-down  max-to-min */
       sy = srcy + height - 1;
       dy = desty + height - 1;
@@ -757,19 +578,16 @@ static void copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
       stepy = 1;
    }
 
-   overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
-                                 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
-
    if (overlapping) {
       GLint ssy = sy;
-      tmpImage = (GLstencil *) MALLOC(width * height * sizeof(GLstencil));
+      tmpImage = (GLstencil *) _mesa_malloc(width * height * sizeof(GLstencil));
       if (!tmpImage) {
-         gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
+         _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
          return;
       }
       p = tmpImage;
       for (j = 0; j < height; j++, ssy += stepy) {
-         _mesa_read_stencil_span( ctx, width, srcx, ssy, p );
+         _swrast_read_stencil_span( ctx, rb, width, srcx, ssy, p );
          p += width;
       }
       p = tmpImage;
@@ -782,58 +600,328 @@ static void copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
    for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
       GLstencil stencil[MAX_WIDTH];
 
+      /* Get stencil values */
       if (overlapping) {
-         MEMCPY(stencil, p, width * sizeof(GLstencil));
+         _mesa_memcpy(stencil, p, width * sizeof(GLstencil));
          p += width;
       }
       else {
-         _mesa_read_stencil_span( ctx, width, srcx, sy, stencil );
+         _swrast_read_stencil_span( ctx, rb, width, srcx, sy, stencil );
       }
 
-      if (shift_or_offset) {
-         _mesa_shift_and_offset_stencil( ctx, width, stencil );
-      }
-      if (ctx->Pixel.MapStencilFlag) {
-         _mesa_map_stencil( ctx, width, stencil );
-      }
+      _mesa_apply_stencil_transfer_ops(ctx, width, stencil);
 
+      /* Write stencil values */
       if (zoom) {
-         gl_write_zoomed_stencil_span( ctx, width, destx, dy, stencil, desty );
+         _swrast_write_zoomed_stencil_span(ctx, destx, desty, width,
+                                           destx, dy, stencil);
       }
       else {
-         _mesa_write_stencil_span( ctx, width, destx, dy, stencil );
+         _swrast_write_stencil_span( ctx, width, destx, dy, stencil );
       }
    }
 
    if (overlapping)
-      FREE(tmpImage);
+      _mesa_free(tmpImage);
 }
 
 
+/**
+ * This isn't terribly efficient.  If a driver really has combined
+ * depth/stencil buffers the driver should implement an optimized
+ * CopyPixels function.
+ */
+static void
+copy_depth_stencil_pixels(GLcontext *ctx,
+                          const GLint srcX, const GLint srcY,
+                          const GLint width, const GLint height,
+                          const GLint destX, const GLint destY)
+{
+   struct gl_renderbuffer *stencilReadRb, *depthReadRb, *depthDrawRb;
+   GLint sy, dy, stepy;
+   GLint j;
+   GLstencil *tempStencilImage = NULL, *stencilPtr = NULL;
+   GLfloat *tempDepthImage = NULL, *depthPtr = NULL;
+   const GLfloat depthScale = ctx->DrawBuffer->_DepthMaxF;
+   const GLuint stencilMask = ctx->Stencil.WriteMask[0];
+   const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
+   const GLboolean scaleOrBias
+      = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
+   GLint overlapping;
 
+   depthDrawRb = ctx->DrawBuffer->_DepthBuffer;
+   depthReadRb = ctx->ReadBuffer->_DepthBuffer;
+   stencilReadRb = ctx->ReadBuffer->_StencilBuffer;
 
-void
-_swrast_CopyPixels( GLcontext *ctx,
-                   GLint srcx, GLint srcy, GLsizei width, GLsizei height,
-                   GLint destx, GLint desty,
-                   GLenum type )
+   ASSERT(depthDrawRb);
+   ASSERT(depthReadRb);
+   ASSERT(stencilReadRb);
+
+   if (ctx->DrawBuffer == ctx->ReadBuffer) {
+      overlapping = regions_overlap(srcX, srcY, destX, destY, width, height,
+                                    ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
+   }
+   else {
+      overlapping = GL_FALSE;
+   }
+
+   /* Determine if copy should be bottom-to-top or top-to-bottom */
+   if (!overlapping && srcY < destY) {
+      /* top-down  max-to-min */
+      sy = srcY + height - 1;
+      dy = destY + height - 1;
+      stepy = -1;
+   }
+   else {
+      /* bottom-up  min-to-max */
+      sy = srcY;
+      dy = destY;
+      stepy = 1;
+   }
+
+   if (overlapping) {
+      GLint ssy = sy;
+
+      if (stencilMask != 0x0) {
+         tempStencilImage
+            = (GLstencil *) _mesa_malloc(width * height * sizeof(GLstencil));
+         if (!tempStencilImage) {
+            _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
+            return;
+         }
+
+         /* get copy of stencil pixels */
+         stencilPtr = tempStencilImage;
+         for (j = 0; j < height; j++, ssy += stepy) {
+            _swrast_read_stencil_span(ctx, stencilReadRb,
+                                      width, srcX, ssy, stencilPtr);
+            stencilPtr += width;
+         }
+         stencilPtr = tempStencilImage;
+      }
+
+      if (ctx->Depth.Mask) {
+         tempDepthImage
+            = (GLfloat *) _mesa_malloc(width * height * sizeof(GLfloat));
+         if (!tempDepthImage) {
+            _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
+            _mesa_free(tempStencilImage);
+            return;
+         }
+
+         /* get copy of depth pixels */
+         depthPtr = tempDepthImage;
+         for (j = 0; j < height; j++, ssy += stepy) {
+            _swrast_read_depth_span_float(ctx, depthReadRb,
+                                          width, srcX, ssy, depthPtr);
+            depthPtr += width;
+         }
+         depthPtr = tempDepthImage;
+      }
+   }
+
+   for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
+      if (stencilMask != 0x0) {
+         GLstencil stencil[MAX_WIDTH];
+
+         /* Get stencil values */
+         if (overlapping) {
+            _mesa_memcpy(stencil, stencilPtr, width * sizeof(GLstencil));
+            stencilPtr += width;
+         }
+         else {
+            _swrast_read_stencil_span(ctx, stencilReadRb,
+                                      width, srcX, sy, stencil);
+         }
+
+         _mesa_apply_stencil_transfer_ops(ctx, width, stencil);
+
+         /* Write values */
+         if (zoom) {
+            _swrast_write_zoomed_stencil_span(ctx, destX, destY, width,
+                                              destX, dy, stencil);
+         }
+         else {
+            _swrast_write_stencil_span( ctx, width, destX, dy, stencil );
+         }
+      }
+
+      if (ctx->Depth.Mask) {
+         GLfloat depth[MAX_WIDTH];
+         GLuint zVals32[MAX_WIDTH];
+         GLushort zVals16[MAX_WIDTH];
+         GLvoid *zVals;
+         GLuint zBytes;
+
+         /* get depth values */
+         if (overlapping) {
+            _mesa_memcpy(depth, depthPtr, width * sizeof(GLfloat));
+            depthPtr += width;
+         }
+         else {
+            _swrast_read_depth_span_float(ctx, depthReadRb,
+                                          width, srcX, sy, depth);
+         }
+
+         /* scale & bias */
+         if (scaleOrBias) {
+            _mesa_scale_and_bias_depth(ctx, width, depth);
+         }
+         /* convert to integer Z values */
+         if (depthDrawRb->DataType == GL_UNSIGNED_SHORT) {
+            GLint k;
+            for (k = 0; k < width; k++)
+               zVals16[k] = (GLushort) (depth[k] * depthScale);
+            zVals = zVals16;
+            zBytes = 2;
+         }
+         else {
+            GLint k;
+            for (k = 0; k < width; k++)
+               zVals32[k] = (GLuint) (depth[k] * depthScale);
+            zVals = zVals32;
+            zBytes = 4;
+         }
+
+         /* Write values */
+         if (zoom) {
+            _swrast_write_zoomed_z_span(ctx, destX, destY, width,
+                                        destX, dy, zVals);
+         }
+         else {
+            _swrast_put_row(ctx, depthDrawRb, width, destX, dy, zVals, zBytes);
+         }
+      }
+   }
+
+   if (tempStencilImage)
+      _mesa_free(tempStencilImage);
+
+   if (tempDepthImage)
+      _mesa_free(tempDepthImage);
+}
+
+
+
+/**
+ * Try to do a fast copy pixels.
+ */
+static GLboolean
+fast_copy_pixels(GLcontext *ctx,
+                 GLint srcX, GLint srcY, GLsizei width, GLsizei height,
+                 GLint dstX, GLint dstY, GLenum type)
 {
-   if (SWRAST_CONTEXT(ctx)->NewState)
-      _swrast_validate_derived( ctx );
+   struct gl_framebuffer *srcFb = ctx->ReadBuffer;
+   struct gl_framebuffer *dstFb = ctx->DrawBuffer;
+   struct gl_renderbuffer *srcRb, *dstRb;
+   GLint row, yStep;
+
+   if (SWRAST_CONTEXT(ctx)->_RasterMask != 0x0 ||
+       ctx->Pixel.ZoomX != 1.0F ||
+       ctx->Pixel.ZoomY != 1.0F ||
+       ctx->_ImageTransferState) {
+      /* can't handle these */
+      return GL_FALSE;
+   }
 
-   if (type == GL_COLOR && ctx->Visual.RGBAflag) {
-      copy_rgba_pixels( ctx, srcx, srcy, width, height, destx, desty );
+   if (type == GL_COLOR) {
+      if (dstFb->_NumColorDrawBuffers[0] != 1)
+         return GL_FALSE;
+      srcRb = srcFb->_ColorReadBuffer;
+      dstRb = dstFb->_ColorDrawBuffers[0][0];
    }
-   else if (type == GL_COLOR && !ctx->Visual.RGBAflag) {
-      copy_ci_pixels( ctx, srcx, srcy, width, height, destx, desty );
+   else if (type == GL_STENCIL) {
+      srcRb = srcFb->_StencilBuffer;
+      dstRb = dstFb->_StencilBuffer;
    }
    else if (type == GL_DEPTH) {
-      copy_depth_pixels( ctx, srcx, srcy, width, height, destx, desty );
+      srcRb = srcFb->_DepthBuffer;
+      dstRb = dstFb->_DepthBuffer;
    }
-   else if (type == GL_STENCIL) {
-      copy_stencil_pixels( ctx, srcx, srcy, width, height, destx, desty );
+   else {
+      ASSERT(type == GL_DEPTH_STENCIL_EXT);
+      /* XXX correct? */
+      srcRb = srcFb->Attachment[BUFFER_DEPTH].Renderbuffer;
+      dstRb = dstFb->Attachment[BUFFER_DEPTH].Renderbuffer;
+   }
+
+   /* src and dst renderbuffers must be same format and type */
+   if (!srcRb || !dstRb ||
+       srcRb->DataType != dstRb->DataType ||
+       srcRb->_BaseFormat != dstRb->_BaseFormat) {
+      return GL_FALSE;
+   }
+
+   /* clipping not supported */
+   if (srcX < 0 || srcX + width > (GLint) srcFb->Width ||
+       srcY < 0 || srcY + height > (GLint) srcFb->Height ||
+       dstX < dstFb->_Xmin || dstX + width > dstFb->_Xmax ||
+       dstY < dstFb->_Ymin || dstY + height > dstFb->_Ymax) {
+      return GL_FALSE;
+   }
+
+   /* overlapping src/dst doesn't matter, just determine Y direction */
+   if (srcY < dstY) {
+      /* top-down  max-to-min */
+      srcY = srcY + height - 1;
+      dstY = dstY + height - 1;
+      yStep = -1;
    }
    else {
-      gl_error( ctx, GL_INVALID_ENUM, "glCopyPixels" );
+      /* bottom-up  min-to-max */
+      yStep = 1;
    }
+
+   for (row = 0; row < height; row++) {
+      GLuint temp[MAX_WIDTH][4];
+      srcRb->GetRow(ctx, srcRb, width, srcX, srcY, temp);
+      dstRb->PutRow(ctx, dstRb, width, dstX, dstY, temp, NULL);
+      srcY += yStep;
+      dstY += yStep;
+   }
+
+   return GL_TRUE;
+}
+
+
+/**
+ * Do software-based glCopyPixels.
+ * By time we get here, all parameters will have been error-checked.
+ */
+void
+_swrast_CopyPixels( GLcontext *ctx,
+                   GLint srcx, GLint srcy, GLsizei width, GLsizei height,
+                   GLint destx, GLint desty, GLenum type )
+{
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
+   RENDER_START(swrast,ctx);
+      
+   if (swrast->NewState)
+      _swrast_validate_derived( ctx );
+
+   if (!fast_copy_pixels(ctx, srcx, srcy, width, height, destx, desty, type)) {
+      switch (type) {
+      case GL_COLOR:
+         if (ctx->Visual.rgbMode) {
+            copy_rgba_pixels( ctx, srcx, srcy, width, height, destx, desty );
+         }
+         else {
+            copy_ci_pixels( ctx, srcx, srcy, width, height, destx, desty );
+         }
+         break;
+      case GL_DEPTH:
+         copy_depth_pixels( ctx, srcx, srcy, width, height, destx, desty );
+         break;
+      case GL_STENCIL:
+         copy_stencil_pixels( ctx, srcx, srcy, width, height, destx, desty );
+         break;
+      case GL_DEPTH_STENCIL_EXT:
+         copy_depth_stencil_pixels(ctx, srcx, srcy, width, height, destx, desty);
+         break;
+      default:
+         _mesa_problem(ctx, "unexpected type in _swrast_CopyPixels");
+      }
+   }
+
+   RENDER_FINISH(swrast,ctx);
 }