swrast: Mark MAX_GLUINT literal with u suffix.
[mesa.git] / src / mesa / swrast / s_copypix.c
index ab8198945ce992ed48b01447fc8d7797c6879faa..68c83e44e1297e5cdd589afde48a35889e76f378 100644 (file)
@@ -1,10 +1,7 @@
-/* $Id: s_copypix.c,v 1.21 2001/06/26 21:15:36 brianp Exp $ */
-
 /*
  * Mesa 3-D graphics library
- * Version:  3.5
  *
- * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
+ * 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"),
  * 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
- * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
  */
 
 
-#include "glheader.h"
-#include "colormac.h"
-#include "context.h"
-#include "convolve.h"
-#include "feedback.h"
-#include "macros.h"
-#include "mem.h"
-#include "mmath.h"
-#include "pixel.h"
+#include "main/glheader.h"
+#include "main/context.h"
+#include "main/condrender.h"
+#include "main/macros.h"
+#include "main/pixeltransfer.h"
+#include "main/imports.h"
 
 #include "s_context.h"
 #include "s_depth.h"
-#include "s_fog.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.
  * 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(GLint srcx, GLint srcy,
@@ -67,19 +59,29 @@ regions_overlap(GLint srcx, GLint srcy,
       else if (srcy < dsty) { /* this is OK */
          return GL_FALSE;
       }
+      else if (srcy > dsty + height) {
+         return GL_FALSE;
+      }
       else {
          return GL_TRUE;
       }
    }
    else {
       /* add one pixel of slop when zooming, just to be safe */
-      if ((srcx > dstx + (width * zoomX) + 1) || (srcx + width + 1 < dstx)) {
+      if (srcx > (dstx + ((zoomX > 0.0F) ? (width * zoomX + 1.0F) : 0.0F))) {
+         /* src is completely right of dest */
+         return GL_FALSE;
+      }
+      else if (srcx + width + 1.0F < dstx + ((zoomX > 0.0F) ? 0.0F : (width * zoomX))) {
+         /* src is completely left of dest */
          return GL_FALSE;
       }
       else if ((srcy < dsty) && (srcy + height < dsty + (height * zoomY))) {
+         /* src is completely below dest */
          return GL_FALSE;
       }
       else if ((srcy > dsty) && (srcy + height > dsty + (height * zoomY))) {
+         /* src is completely above dest */
          return GL_FALSE;
       }
       else {
@@ -89,249 +91,35 @@ regions_overlap(GLint srcx, GLint srcy,
 }
 
 
-
-/*
- * RGBA copypixels with convolution.
+/**
+ * RGBA copypixels
  */
 static void
-copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
-                      GLint width, GLint height, GLint destx, GLint desty)
+copy_rgba_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
+                 GLint width, GLint height, GLint destx, GLint desty)
 {
-   SWcontext *swrast = SWRAST_CONTEXT(ctx);
-   GLdepth zspan[MAX_WIDTH];
-   GLfloat fogSpan[MAX_WIDTH];
-   GLboolean quick_draw;
-   GLint row;
-   GLboolean changeBuffer;
-   GLchan *saveReadAlpha;
+   GLfloat *tmpImage, *p;
+   GLint sy, dy, stepy, row;
    const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
-   const GLuint transferOps = ctx->_ImageTransferState;
-   GLfloat *dest, *tmpImage, *convImage;
-
-   if (ctx->Depth.Test || ctx->Fog.Enabled) {
-      /* fill in array of z values */
-      GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * ctx->DepthMax);
-      GLfloat fog;
-      GLint i;
-
-      if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT)
-         fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterFogCoord);
-      else
-         fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterDistance);
-
-      for (i = 0; i < width; i++) {
-         zspan[i] = z;
-         fogSpan[i] = fog;
-      }
-   }
-
-   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;
-
+   GLint overlapping;
+   GLuint transferOps = ctx->_ImageTransferState;
+   SWspan span;
 
-   /* allocate space for GLfloat image */
-   tmpImage = (GLfloat *) MALLOC(width * height * 4 * sizeof(GLfloat));
-   if (!tmpImage) {
-      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
-      return;
-   }
-   convImage = (GLfloat *) MALLOC(width * height * 4 * sizeof(GLfloat));
-   if (!convImage) {
-      FREE(tmpImage);
-      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
+   if (!ctx->ReadBuffer->_ColorReadBuffer) {
+      /* no readbuffer - OK */
       return;
    }
 
-   dest = tmpImage;
-
-   if (changeBuffer) {
-      (*swrast->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 */
-   dest = tmpImage;
-   for (row = 0; row < height; row++) {
-      GLchan rgba[MAX_WIDTH][4];
-      GLint i;
-      _mesa_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);
-      }
-   }
-
-   /* read from the draw buffer again (in case of blending) */
-   if (changeBuffer) {
-      (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
-                                    ctx->Color.DriverDrawBuffer );
-      ctx->ReadBuffer->Alpha = saveReadAlpha;
-   }
-
-   /* do image transfer ops up until 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,
-                                   ctx->Pixel.RedScale, ctx->Pixel.GreenScale,
-                                   ctx->Pixel.BlueScale, ctx->Pixel.AlphaScale,
-                                   ctx->Pixel.RedBias, ctx->Pixel.GreenBias,
-                                   ctx->Pixel.BlueBias, ctx->Pixel.AlphaBias);
-      }
-      /* 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);
-      }
-   }
-
-   /* do convolution */
-   if (ctx->Pixel.Convolution2DEnabled) {
-      _mesa_convolve_2d_image(ctx, &width, &height, tmpImage, convImage);
+   if (ctx->DrawBuffer == ctx->ReadBuffer) {
+      overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
+                                    ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
    }
    else {
-      ASSERT(ctx->Pixel.Separable2DEnabled);
-      _mesa_convolve_sep_image(ctx, &width, &height, tmpImage, convImage);
-   }
-   FREE(tmpImage);
-
-   /* do remaining 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 < ctx->Const.MaxTextureUnits; unit++) {
-            _mesa_pixeltexgen(ctx, width, (const GLchan (*)[4]) rgba,
-                              s, t, r, q);
-            _swrast_texture_fragments(ctx, unit, width, s, t, r, NULL,
-                                      (CONST GLchan (*)[4]) primary_rgba,
-                                      rgba);
-         }
-      }
-
-      /* write row to framebuffer */
-
-      dy = desty + row;
-      if (quick_draw && dy >= 0 && dy < ctx->DrawBuffer->Height) {
-         (*swrast->Driver.WriteRGBASpan)( ctx, width, destx, dy,
-                                      (const GLchan (*)[4])rgba, NULL );
-      }
-      else if (zoom) {
-         _mesa_write_zoomed_rgba_span( ctx, width, destx, dy, zspan, fogSpan,
-                                   (const GLchan (*)[4])rgba, desty);
-      }
-      else {
-         _mesa_write_rgba_span( ctx, width, destx, dy, zspan, fogSpan, rgba,
-                                NULL, GL_BITMAP );
-      }
-   }
-
-   FREE(convImage);
-}
-
-
-/*
- * RGBA copypixels
- */
-static void
-copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
-                 GLint width, GLint height, GLint destx, GLint desty)
-{
-   SWcontext *swrast = SWRAST_CONTEXT(ctx);
-   GLdepth zspan[MAX_WIDTH];
-   GLfloat fogSpan[MAX_WIDTH];
-   GLchan rgba[MAX_WIDTH][4];
-   GLchan *tmpImage,*p;
-   GLboolean quick_draw;
-   GLint sy, dy, stepy;
-   GLint i, j;
-   GLboolean changeBuffer;
-   GLchan *saveReadAlpha;
-   const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
-   GLint overlapping;
-   const GLuint transferOps = ctx->_ImageTransferState;
-
-   if (ctx->Pixel.Convolution2DEnabled || ctx->Pixel.Separable2DEnabled) {
-      copy_conv_rgba_pixels(ctx, srcx, srcy, width, height, destx, desty);
-      return;
+      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;
@@ -344,67 +132,23 @@ 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->DepthMax);
-      GLfloat fog;
-
-      if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT)
-         fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterFogCoord);
-      else
-         fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterDistance);
-
-      for (i=0;i<width;i++) {
-         zspan[i] = z;
-         fogSpan[i] = fog;
-      }
-   }
-
-   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;
-
-   (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
-                                 ctx->Pixel.DriverReadBuffer );
+   INIT_SPAN(span, GL_BITMAP);
+   _swrast_span_default_attribs(ctx, &span);
+   span.arrayMask = SPAN_RGBA;
+   span.arrayAttribs = VARYING_BIT_COL0; /* we'll fill in COL0 attrib values */
 
    if (overlapping) {
-      GLint ssy = sy;
-      tmpImage = (GLchan *) MALLOC(width * height * sizeof(GLchan) * 4);
+      tmpImage = malloc(width * height * sizeof(GLfloat) * 4);
       if (!tmpImage) {
          _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
          return;
       }
+      /* read the source image as RGBA/float */
       p = tmpImage;
-      if (changeBuffer) {
-         (*swrast->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) {
-         _mesa_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, p );
+         p += width * 4;
       }
       p = tmpImage;
    }
@@ -413,189 +157,120 @@ 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 < SWRAST_MAX_WIDTH);
+
+   for (row = 0; row < height; row++, sy += stepy, dy += stepy) {
+      GLvoid *rgba = span.array->attribs[VARYING_SLOT_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);
+         memcpy(rgba, p, width * sizeof(GLfloat) * 4);
+         p += width * 4;
       }
       else {
          /* get from framebuffer */
-         if (changeBuffer) {
-            (*swrast->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;
-            }
-         }
-         _mesa_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, sy, rgba );
-      }
-
-      if (changeBuffer) {
-         /* read from the draw buffer again (in case of blending) */
-         (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
-                                       ctx->Color.DriverDrawBuffer );
-         ctx->ReadBuffer->Alpha = saveReadAlpha;
+         _swrast_read_rgba_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
+                                 width, srcx, sy, rgba );
       }
 
       if (transferOps) {
-         const GLfloat scale = (1.0F / CHAN_MAXF);
-         GLint k;
-         DEFMARRAY(GLfloat, rgbaFloat, MAX_WIDTH, 4);  /* mac 32k limitation */
-         CHECKARRAY(rgbaFloat, return);
-
-         /* 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,
-                                   ctx->Pixel.RedScale, ctx->Pixel.GreenScale,
-                                   ctx->Pixel.BlueScale, ctx->Pixel.AlphaScale,
-                                   ctx->Pixel.RedBias, ctx->Pixel.GreenBias,
-                                   ctx->Pixel.BlueBias, ctx->Pixel.AlphaBias);
-         }
-         /* 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_RED/GREEN/BLUE/ALPHA_SCALE/BIAS */
-         if (transferOps & IMAGE_POST_CONVOLUTION_SCALE_BIAS) {
-            _mesa_scale_and_bias_rgba(ctx, width, rgbaFloat,
-                                      ctx->Pixel.PostConvolutionScale[RCOMP],
-                                      ctx->Pixel.PostConvolutionScale[GCOMP],
-                                      ctx->Pixel.PostConvolutionScale[BCOMP],
-                                      ctx->Pixel.PostConvolutionScale[ACOMP],
-                                      ctx->Pixel.PostConvolutionBias[RCOMP],
-                                      ctx->Pixel.PostConvolutionBias[GCOMP],
-                                      ctx->Pixel.PostConvolutionBias[BCOMP],
-                                      ctx->Pixel.PostConvolutionBias[ACOMP]);
-         }
-         /* 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);
-         }
-         UNDEFARRAY(rgbaFloat);  /* mac 32k limitation */
-      }
-
-      if (ctx->Texture._ReallyEnabled && ctx->Pixel.PixelTextureEnabled) {
-         GLuint unit;
-         GLchan primary_rgba[MAX_WIDTH][4];
-         DEFARRAY(GLfloat, s, MAX_WIDTH);  /* mac 32k limitation */
-         DEFARRAY(GLfloat, t, MAX_WIDTH);  /* mac 32k limitation */
-         DEFARRAY(GLfloat, r, MAX_WIDTH);  /* mac 32k limitation */
-         DEFARRAY(GLfloat, q, MAX_WIDTH);  /* mac 32k limitation */
-         CHECKARRAY(s, return); /* mac 32k limitation */
-         CHECKARRAY(t, return);
-         CHECKARRAY(r, return);
-         CHECKARRAY(q, return);
-
-         /* XXX not sure how multitexture is supposed to work here */
-         MEMCPY(primary_rgba, rgba, 4 * width * sizeof(GLchan));
-
-         for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
-            _mesa_pixeltexgen(ctx, width, (const GLchan (*)[4]) rgba,
-                              s, t, r, q);
-            _swrast_texture_fragments(ctx, unit, width, s, t, r, NULL,
-                                      (CONST GLchan (*)[4]) primary_rgba,
-                                      rgba);
-         }
-
-         UNDEFARRAY(s);  /* mac 32k limitation */
-         UNDEFARRAY(t);
-         UNDEFARRAY(r);
-         UNDEFARRAY(q);
-      }
-
-      if (quick_draw && dy >= 0 && dy < ctx->DrawBuffer->Height) {
-         (*swrast->Driver.WriteRGBASpan)( ctx, width, destx, dy,
-                                      (const GLchan (*)[4])rgba, NULL );
-      }
-      else if (zoom) {
-         _mesa_write_zoomed_rgba_span( ctx, width, destx, dy, zspan, fogSpan,
-                                   (const GLchan (*)[4])rgba, desty);
+         _mesa_apply_rgba_transfer_ops(ctx, transferOps, width,
+                                       (GLfloat (*)[4]) rgba);
+      }
+
+      /* 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 {
-         _mesa_write_rgba_span( ctx, width, destx, dy, zspan, fogSpan, rgba,
-                                NULL, GL_BITMAP );
+         _swrast_write_rgba_span(ctx, &span);
       }
    }
 
-   /* Restore pixel source to be the draw buffer (for blending, etc) */
-   (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
-                                 ctx->Color.DriverDrawBuffer );
+   span.array->ChanType = CHAN_TYPE; /* restore */
 
    if (overlapping)
-      FREE(tmpImage);
+      free(tmpImage);
 }
 
 
-static void copy_ci_pixels( GLcontext *ctx,
-                            GLint srcx, GLint srcy, GLint width, GLint height,
-                            GLint destx, GLint desty )
+/**
+ * Convert floating point Z values to integer Z values with pixel transfer's
+ * Z scale and bias.
+ */
+static void
+scale_and_bias_z(struct gl_context *ctx, GLuint width,
+                 const GLfloat depth[], GLuint z[])
 {
-   SWcontext *swrast = SWRAST_CONTEXT(ctx);
-   GLdepth zspan[MAX_WIDTH];
-   GLfloat fogSpan[MAX_WIDTH];
-   GLuint *tmpImage,*p;
+   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 {
+      /* 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;
+      }
+   }
+}
+
+
+
+/*
+ * TODO: Optimize!!!!
+ */
+static void
+copy_depth_pixels( struct gl_context *ctx, GLint srcx, GLint srcy,
+                   GLint width, GLint height,
+                   GLint destx, GLint desty )
+{
+   struct gl_framebuffer *fb = ctx->ReadBuffer;
+   struct gl_renderbuffer *readRb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
+   GLfloat *p, *tmpImage, *depth;
    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 (!readRb) {
+      /* no readbuffer - OK */
+      return;
+   }
+
+   INIT_SPAN(span, GL_BITMAP);
+   _swrast_span_default_attribs(ctx, &span);
+   span.arrayMask = SPAN_Z;
+
+   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;
@@ -608,46 +283,16 @@ 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->DepthMax);
-      GLfloat fog;
-
-      if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT)
-         fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterFogCoord);
-      else
-         fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterDistance);
-
-      for (i=0;i<width;i++) {
-         zspan[i] = z;
-         fogSpan[i] = fog;
-      }
-   }
-
-   /* If read and draw buffer are different we must do buffer switching */
-   changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer
-               || ctx->DrawBuffer != ctx->ReadBuffer;
-
-   (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
-                                 ctx->Pixel.DriverReadBuffer );
-
    if (overlapping) {
       GLint ssy = sy;
-      tmpImage = (GLuint *) MALLOC(width * height * sizeof(GLuint));
+      tmpImage = malloc(width * height * sizeof(GLfloat));
       if (!tmpImage) {
          _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
          return;
       }
       p = tmpImage;
-      if (changeBuffer) {
-         (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
-                                       ctx->Pixel.DriverReadBuffer );
-      }
       for (j = 0; j < height; j++, ssy += stepy) {
-         _mesa_read_index_span( ctx, ctx->ReadBuffer, width, srcx, ssy, p );
+         _swrast_read_depth_span_float(ctx, readRb, width, srcx, ssy, p);
          p += width;
       }
       p = tmpImage;
@@ -657,80 +302,72 @@ static void copy_ci_pixels( GLcontext *ctx,
       p = NULL;
    }
 
+   depth = malloc(width * sizeof(GLfloat));
+   if (!depth) {
+      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels()");
+      goto end;
+   }
+
    for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
-      GLuint indexes[MAX_WIDTH];
+      /* get depth values */
       if (overlapping) {
-         MEMCPY(indexes, p, width * sizeof(GLuint));
+         memcpy(depth, p, width * sizeof(GLfloat));
          p += width;
       }
       else {
-         if (changeBuffer) {
-            (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
-                                          ctx->Pixel.DriverReadBuffer );
-         }
-         _mesa_read_index_span( ctx, ctx->ReadBuffer, width, srcx, sy, indexes );
+         _swrast_read_depth_span_float(ctx, readRb, width, srcx, sy, depth);
       }
 
-      if (changeBuffer) {
-         /* set read buffer back to draw buffer (in case of logicops) */
-         (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
-                                       ctx->Color.DriverDrawBuffer );
-      }
+      /* apply scale and bias */
+      scale_and_bias_z(ctx, width, depth, span.array->z);
 
-      if (shift_or_offset) {
-         _mesa_shift_and_offset_ci( ctx, width, indexes );
-      }
-      if (ctx->Pixel.MapColorFlag) {
-         _mesa_map_ci( ctx, width, indexes );
-      }
-
-      if (zoom) {
-         _mesa_write_zoomed_index_span(ctx, width, destx, dy, zspan, fogSpan,
-                                       indexes, desty );
-      }
-      else {
-         _mesa_write_index_span(ctx, width, destx, dy, zspan, fogSpan, indexes,
-                                NULL, GL_BITMAP);
-      }
+      /* write depth values */
+      span.x = destx;
+      span.y = dy;
+      span.end = width;
+      if (zoom)
+         _swrast_write_zoomed_depth_span(ctx, destx, desty, &span);
+      else
+         _swrast_write_rgba_span(ctx, &span);
    }
 
-   /* Restore pixel source to be the draw buffer (for blending, etc) */
-   (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
-                                 ctx->Color.DriverDrawBuffer );
+   free(depth);
 
+end:
    if (overlapping)
-      FREE(tmpImage);
+      free(tmpImage);
 }
 
 
 
-/*
- * TODO: Optimize!!!!
- */
-static void copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
-                               GLint width, GLint height,
-                               GLint destx, GLint desty )
+static void
+copy_stencil_pixels( struct gl_context *ctx, GLint srcx, GLint srcy,
+                     GLint width, GLint height,
+                     GLint destx, GLint desty )
 {
-   GLfloat depth[MAX_WIDTH];
-   GLdepth zspan[MAX_WIDTH];
-   GLfloat fogSpan[MAX_WIDTH];
-   GLfloat *p, *tmpImage;
-   GLuint indexes[MAX_WIDTH];
+   struct gl_framebuffer *fb = ctx->ReadBuffer;
+   struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
    GLint sy, dy, stepy;
-   GLint i, j;
+   GLint j;
+   GLubyte *p, *tmpImage, *stencil;
    const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
    GLint overlapping;
-   DEFMARRAY(GLubyte, rgba, MAX_WIDTH, 4);  /* mac 32k limitation */
-   CHECKARRAY(rgba, return);  /* mac 32k limitation */
 
-   if (!ctx->Visual.depthBits) {
-      _mesa_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
-      UNDEFARRAY(rgba);  /* mac 32k limitation */
+   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;
@@ -743,47 +380,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.rgbMode) {
-      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 (ctx->Fog.Enabled) {
-      GLfloat fog;
-
-      if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT)
-         fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterFogCoord);
-      else
-         fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterDistance);
-
-      for (i = 0; i < width; i++) {
-         fogSpan[i] = fog;
-      }
-   }
-
    if (overlapping) {
       GLint ssy = sy;
-      tmpImage = (GLfloat *) MALLOC(width * height * sizeof(GLfloat));
+      tmpImage = malloc(width * height * sizeof(GLubyte));
       if (!tmpImage) {
          _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
-         UNDEFARRAY(rgba);  /* mac 32k limitation */
          return;
       }
       p = tmpImage;
       for (j = 0; j < height; j++, ssy += stepy) {
-         _mesa_read_depth_span_float(ctx, width, srcx, ssy, p);
+         _swrast_read_stencil_span( ctx, rb, width, srcx, ssy, p );
          p += width;
       }
       p = tmpImage;
@@ -793,162 +399,277 @@ static void copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
       p = NULL;
    }
 
+   stencil = malloc(width * sizeof(GLubyte));
+   if (!stencil) {
+      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels()");
+      goto end;
+   }
+
    for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
+      /* Get stencil values */
       if (overlapping) {
-         MEMCPY(depth, p, width * sizeof(GLfloat));
+         memcpy(stencil, p, width * sizeof(GLubyte));
          p += width;
       }
       else {
-         _mesa_read_depth_span_float(ctx, width, srcx, sy, depth);
+         _swrast_read_stencil_span( ctx, rb, width, srcx, sy, stencil );
       }
 
-      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->DepthMax);
-      }
+      _mesa_apply_stencil_transfer_ops(ctx, width, stencil);
 
-      if (ctx->Visual.rgbMode) {
-         if (zoom) {
-            _mesa_write_zoomed_rgba_span( ctx, width, destx, dy, zspan,
-                                   fogSpan, (const GLchan (*)[4])rgba, desty );
-         }
-         else {
-            _mesa_write_rgba_span( ctx, width, destx, dy, zspan, fogSpan,
-                                   rgba, NULL, GL_BITMAP);
-         }
+      /* Write stencil values */
+      if (zoom) {
+         _swrast_write_zoomed_stencil_span(ctx, destx, desty, width,
+                                           destx, dy, stencil);
       }
       else {
-         if (zoom) {
-            _mesa_write_zoomed_index_span( ctx, width, destx, dy,
-                                           zspan, fogSpan, indexes, desty );
-         }
-         else {
-            _mesa_write_index_span( ctx, width, destx, dy,
-                                    zspan, fogSpan, indexes, NULL, GL_BITMAP );
-         }
+         _swrast_write_stencil_span( ctx, width, destx, dy, stencil );
       }
    }
 
-   UNDEFARRAY(rgba);  /* mac 32k limitation */
+   free(stencil);
 
+end:
    if (overlapping)
-      FREE(tmpImage);
+      free(tmpImage);
 }
 
 
-
-static void copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
-                                 GLint width, GLint height,
-                                 GLint destx, GLint desty )
+/**
+ * Try to do a fast 1:1 blit with memcpy.
+ * \return GL_TRUE if successful, GL_FALSE otherwise.
+ */
+GLboolean
+swrast_fast_copy_pixels(struct gl_context *ctx,
+                        struct gl_framebuffer *srcFb,
+                        struct gl_framebuffer *dstFb,
+                        GLint srcX, GLint srcY, GLsizei width, GLsizei height,
+                        GLint dstX, GLint dstY, GLenum type)
 {
-   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;
+   struct gl_renderbuffer *srcRb, *dstRb;
+   GLint row;
+   GLuint pixelBytes, widthInBytes;
+   GLubyte *srcMap, *dstMap;
+   GLint srcRowStride, dstRowStride;
 
-   if (!ctx->Visual.stencilBits) {
-      _mesa_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
-      return;
+   if (type == GL_COLOR) {
+      if (dstFb->_NumColorDrawBuffers != 1)
+         return GL_FALSE;
+      srcRb = srcFb->_ColorReadBuffer;
+      dstRb = dstFb->_ColorDrawBuffers[0];
    }
-
-   /* Determine if copy should be bottom-to-top or top-to-bottom */
-   if (srcy < desty) {
-      /* top-down  max-to-min */
-      sy = srcy + height - 1;
-      dy = desty + height - 1;
-      stepy = -1;
+   else if (type == GL_STENCIL) {
+      srcRb = srcFb->Attachment[BUFFER_STENCIL].Renderbuffer;
+      dstRb = dstFb->Attachment[BUFFER_STENCIL].Renderbuffer;
+   }
+   else if (type == GL_DEPTH) {
+      srcRb = srcFb->Attachment[BUFFER_DEPTH].Renderbuffer;
+      dstRb = dstFb->Attachment[BUFFER_DEPTH].Renderbuffer;
    }
    else {
-      /* bottom-up  min-to-max */
-      sy = srcy;
-      dy = desty;
-      stepy = 1;
+      assert(type == GL_DEPTH_STENCIL_EXT);
+      /* XXX correct? */
+      srcRb = srcFb->Attachment[BUFFER_DEPTH].Renderbuffer;
+      dstRb = dstFb->Attachment[BUFFER_DEPTH].Renderbuffer;
    }
 
-   overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
-                                 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
+   /* src and dst renderbuffers must be same format */
+   if (!srcRb || !dstRb || srcRb->Format != dstRb->Format) {
+      return GL_FALSE;
+   }
 
-   if (overlapping) {
-      GLint ssy = sy;
-      tmpImage = (GLstencil *) MALLOC(width * height * sizeof(GLstencil));
-      if (!tmpImage) {
-         _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 );
-         p += width;
-      }
-      p = tmpImage;
+   if (type == GL_STENCIL || type == GL_DEPTH_COMPONENT) {
+      /* can't handle packed depth+stencil here */
+      if (_mesa_is_format_packed_depth_stencil(srcRb->Format) ||
+          _mesa_is_format_packed_depth_stencil(dstRb->Format))
+         return GL_FALSE;
    }
-   else {
-      tmpImage = NULL;  /* silence compiler warning */
-      p = NULL;
+   else if (type == GL_DEPTH_STENCIL) {
+      /* can't handle separate depth/stencil buffers */
+      if (srcRb != srcFb->Attachment[BUFFER_STENCIL].Renderbuffer ||
+          dstRb != dstFb->Attachment[BUFFER_STENCIL].Renderbuffer)
+         return GL_FALSE;
    }
 
-   for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
-      GLstencil stencil[MAX_WIDTH];
+   /* 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;
+   }
 
-      if (overlapping) {
-         MEMCPY(stencil, p, width * sizeof(GLstencil));
-         p += width;
-      }
-      else {
-         _mesa_read_stencil_span( ctx, width, srcx, sy, stencil );
-      }
+   pixelBytes = _mesa_get_format_bytes(srcRb->Format);
+   widthInBytes = width * pixelBytes;
 
-      if (shift_or_offset) {
-         _mesa_shift_and_offset_stencil( ctx, width, stencil );
-      }
-      if (ctx->Pixel.MapStencilFlag) {
-         _mesa_map_stencil( ctx, width, stencil );
+   if (srcRb == dstRb) {
+      /* map whole buffer for read/write */
+      /* XXX we could be clever and just map the union region of the
+       * source and dest rects.
+       */
+      GLubyte *map;
+      GLint rowStride;
+
+      ctx->Driver.MapRenderbuffer(ctx, srcRb, 0, 0,
+                                  srcRb->Width, srcRb->Height,
+                                  GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
+                                  &map, &rowStride);
+      if (!map) {
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
+         return GL_TRUE; /* don't retry with slow path */
       }
 
-      if (zoom) {
-         _mesa_write_zoomed_stencil_span( ctx, width, destx, dy, stencil, desty );
+      srcMap = map + srcY * rowStride + srcX * pixelBytes;
+      dstMap = map + dstY * rowStride + dstX * pixelBytes;
+
+      /* this handles overlapping copies */
+      if (srcY < dstY) {
+         /* copy in reverse (top->down) order */
+         srcMap += rowStride * (height - 1);
+         dstMap += rowStride * (height - 1);
+         srcRowStride = -rowStride;
+         dstRowStride = -rowStride;
       }
       else {
-         _mesa_write_stencil_span( ctx, width, destx, dy, stencil );
+         /* copy in normal (bottom->up) order */
+         srcRowStride = rowStride;
+         dstRowStride = rowStride;
+      }
+   }
+   else {
+      /* different src/dst buffers */
+      ctx->Driver.MapRenderbuffer(ctx, srcRb, srcX, srcY,
+                                  width, height,
+                                  GL_MAP_READ_BIT, &srcMap, &srcRowStride);
+      if (!srcMap) {
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
+         return GL_TRUE; /* don't retry with slow path */
+      }
+      ctx->Driver.MapRenderbuffer(ctx, dstRb, dstX, dstY,
+                                  width, height,
+                                  GL_MAP_WRITE_BIT, &dstMap, &dstRowStride);
+      if (!dstMap) {
+         ctx->Driver.UnmapRenderbuffer(ctx, srcRb);
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
+         return GL_TRUE; /* don't retry with slow path */
       }
    }
 
-   if (overlapping)
-      FREE(tmpImage);
+   for (row = 0; row < height; row++) {
+      /* memmove() in case of overlap */
+      memmove(dstMap, srcMap, widthInBytes);
+      dstMap += dstRowStride;
+      srcMap += srcRowStride;
+   }
+
+   ctx->Driver.UnmapRenderbuffer(ctx, srcRb);
+   if (dstRb != srcRb) {
+      ctx->Driver.UnmapRenderbuffer(ctx, dstRb);
+   }
+
+   return GL_TRUE;
 }
 
 
+/**
+ * Find/map the renderbuffer that we'll be reading from.
+ * The swrast_render_start() function only maps the drawing buffers,
+ * not the read buffer.
+ */
+static struct gl_renderbuffer *
+map_readbuffer(struct gl_context *ctx, GLenum type)
+{
+   struct gl_framebuffer *fb = ctx->ReadBuffer;
+   struct gl_renderbuffer *rb;
+   struct swrast_renderbuffer *srb;
+
+   switch (type) {
+   case GL_COLOR:
+      rb = fb->Attachment[fb->_ColorReadBufferIndex].Renderbuffer;
+      break;
+   case GL_DEPTH:
+   case GL_DEPTH_STENCIL:
+      rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
+      break;
+   case GL_STENCIL:
+      rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
+      break;
+   default:
+      return NULL;
+   }
+
+   srb = swrast_renderbuffer(rb);
+
+   if (!srb || srb->Map) {
+      /* no buffer, or buffer is mapped already, we're done */
+      return NULL;
+   }
+
+   ctx->Driver.MapRenderbuffer(ctx, rb,
+                               0, 0, rb->Width, rb->Height,
+                               GL_MAP_READ_BIT,
+                               &srb->Map, &srb->RowStride);
+
+   return rb;
+}
 
 
+/**
+ * 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 )
+_swrast_CopyPixels(struct gl_context *ctx,
+                   GLint srcx, GLint srcy, GLsizei width, GLsizei height,
+                   GLint destx, GLint desty, GLenum type)
 {
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
-   RENDER_START(swrast,ctx);
+   struct gl_renderbuffer *rb;
       
+   if (!_mesa_check_conditional_render(ctx))
+      return; /* don't copy */
+
    if (swrast->NewState)
       _swrast_validate_derived( ctx );
 
-   if (type == GL_COLOR && ctx->Visual.rgbMode) {
-      copy_rgba_pixels( ctx, srcx, srcy, width, height, destx, desty );
-   }
-   else if (type == GL_COLOR && !ctx->Visual.rgbMode) {
-      copy_ci_pixels( ctx, srcx, srcy, width, height, destx, desty );
+   if (!(SWRAST_CONTEXT(ctx)->_RasterMask != 0x0 ||
+       ctx->Pixel.ZoomX != 1.0F ||
+       ctx->Pixel.ZoomY != 1.0F ||
+       ctx->_ImageTransferState) &&
+      swrast_fast_copy_pixels(ctx, ctx->ReadBuffer, ctx->DrawBuffer,
+                              srcx, srcy, width, height, destx, desty,
+                              type)) {
+      /* all done */
+      return;
    }
-   else if (type == GL_DEPTH) {
+
+   swrast_render_start(ctx);
+   rb = map_readbuffer(ctx, type);
+
+   switch (type) {
+   case GL_COLOR:
+      copy_rgba_pixels( ctx, srcx, srcy, width, height, destx, desty );
+      break;
+   case GL_DEPTH:
       copy_depth_pixels( ctx, srcx, srcy, width, height, destx, desty );
-   }
-   else if (type == GL_STENCIL) {
+      break;
+   case GL_STENCIL:
       copy_stencil_pixels( ctx, srcx, srcy, width, height, destx, desty );
-   }
-   else {
-      _mesa_error( ctx, GL_INVALID_ENUM, "glCopyPixels" );
+      break;
+   case GL_DEPTH_STENCIL_EXT:
+      /* Copy buffers separately (if the fast copy path wasn't taken) */
+      copy_depth_pixels(ctx, srcx, srcy, width, height, destx, desty);
+      copy_stencil_pixels(ctx, srcx, srcy, width, height, destx, desty);
+      break;
+   default:
+      _mesa_problem(ctx, "unexpected type in _swrast_CopyPixels");
    }
 
-   RENDER_FINISH(swrast,ctx);
+   swrast_render_finish(ctx);
+
+   if (rb) {
+      struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
+      ctx->Driver.UnmapRenderbuffer(ctx, rb);
+      srb->Map = NULL;
+   }
 }