mesa: Remove _Active and _UseTexEnvProgram flags from fragment programs.
[mesa.git] / src / mesa / swrast / s_copypix.c
index 15e78a5d2e3ff82a9ccf81b0777b3d8aa894ac32..fc5990b261c1370a43156f8360fca7446b8879f2 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * Mesa 3-D graphics library
- * Version:  6.5
+ * Version:  7.1
  *
- * Copyright (C) 1999-2005  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"),
  */
 
 
-#include "glheader.h"
-#include "context.h"
-#include "colormac.h"
-#include "convolve.h"
-#include "histogram.h"
-#include "image.h"
-#include "macros.h"
-#include "imports.h"
-#include "pixel.h"
+#include "main/glheader.h"
+#include "main/context.h"
+#include "main/colormac.h"
+#include "main/convolve.h"
+#include "main/histogram.h"
+#include "main/image.h"
+#include "main/macros.h"
+#include "main/imports.h"
+#include "main/pixel.h"
 
 #include "s_context.h"
 #include "s_depth.h"
-#include "s_pixeltex.h"
 #include "s_span.h"
 #include "s_stencil.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,
@@ -71,13 +71,20 @@ regions_overlap(GLint srcx, GLint srcy,
    }
    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 {
@@ -88,73 +95,24 @@ regions_overlap(GLint srcx, GLint srcy,
 
 
 /**
- * Convert GLfloat[n][4] colors to GLchan[n][4].
- * XXX maybe move into image.c
- */
-static void
-float_span_to_chan(GLuint n, CONST GLfloat in[][4], GLchan out[][4])
-{
-   GLuint i;
-   for (i = 0; i < n; i++) {
-      UNCLAMPED_FLOAT_TO_CHAN(out[i][RCOMP], in[i][RCOMP]);
-      UNCLAMPED_FLOAT_TO_CHAN(out[i][GCOMP], in[i][GCOMP]);
-      UNCLAMPED_FLOAT_TO_CHAN(out[i][BCOMP], in[i][BCOMP]);
-      UNCLAMPED_FLOAT_TO_CHAN(out[i][ACOMP], in[i][ACOMP]);
-   }
-}
-
-
-/**
- * Convert GLchan[n][4] colors to GLfloat[n][4].
- * XXX maybe move into image.c
- */
-static void
-chan_span_to_float(GLuint n, CONST GLchan in[][4], GLfloat out[][4])
-{
-   GLuint i;
-   for (i = 0; i < n; i++) {
-      out[i][RCOMP] = CHAN_TO_FLOAT(in[i][RCOMP]);
-      out[i][GCOMP] = CHAN_TO_FLOAT(in[i][GCOMP]);
-      out[i][BCOMP] = CHAN_TO_FLOAT(in[i][BCOMP]);
-      out[i][ACOMP] = CHAN_TO_FLOAT(in[i][ACOMP]);
-   }
-}
-
-
-
-/*
  * RGBA copypixels with convolution.
  */
 static void
 copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
                       GLint width, GLint height, GLint destx, GLint desty)
 {
-   struct gl_renderbuffer *drawRb = NULL;
-   GLboolean quick_draw;
    GLint row;
    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;
-   struct sw_span span;
-
-   INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_RGBA);
-
-   if (ctx->Depth.Test)
-      _swrast_span_default_z(ctx, &span);
-   if (ctx->Fog.Enabled)
-      _swrast_span_default_fog(ctx, &span);
-
+   SWspan span;
 
-   if (SWRAST_CONTEXT(ctx)->_RasterMask == 0
-       && !zoom
-       && destx >= 0
-       && destx + width <= (GLint) ctx->DrawBuffer->Width) {
-      quick_draw = GL_TRUE;
-      drawRb = ctx->DrawBuffer->_ColorDrawBuffers[0][0];
-   }
-   else {
-      quick_draw = GL_FALSE;
-   }
+   INIT_SPAN(span, GL_BITMAP);
+   _swrast_span_default_attribs(ctx, &span);
+   span.arrayMask = SPAN_RGBA;
+   span.arrayAttribs = FRAG_BIT_COL0;
 
    /* allocate space for GLfloat image */
    tmpImage = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
@@ -169,15 +127,11 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
       return;
    }
 
-   /* read source image */
+   /* read source image as float/RGBA */
    dest = tmpImage;
    for (row = 0; row < height; row++) {
-      GLchan rgba[MAX_WIDTH][4];
-      /* Read GLchan and convert to GLfloat */
       _swrast_read_rgba_span(ctx, ctx->ReadBuffer->_ColorReadBuffer,
-                             width, srcx, srcy + row, rgba);
-      chan_span_to_float(width, (CONST GLchan (*)[4]) rgba,
-                         (GLfloat (*)[4]) dest);
+                             width, srcx, srcy + row, GL_FLOAT, dest);
       dest += 4 * width;
    }
 
@@ -207,73 +161,74 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
                                     width, rgba);
    }
 
-   /* write the new image */
-   for (row = 0; row < height; row++) {
-      const GLfloat *src = convImage + row * width * 4;
-      GLint dy;
+   if (!sink) {
+      /* write the new image */
+      for (row = 0; row < height; row++) {
+         const GLfloat *src = convImage + row * width * 4;
+         GLfloat *rgba = (GLfloat *) span.array->attribs[FRAG_ATTRIB_COL0];
 
-      /* convert floats back to chan */
-      float_span_to_chan(width, (const GLfloat (*)[4]) src, span.array->rgba);
-
-      if (ctx->Pixel.PixelTextureEnabled && ctx->Texture._EnabledUnits) {
-         span.end = width;
-         _swrast_pixel_texture(ctx, &span);
-      }
+         /* copy convolved colors into span array */
+         _mesa_memcpy(rgba, src, width * 4 * sizeof(GLfloat));
 
-      /* write row to framebuffer */
-
-      dy = desty + row;
-      if (quick_draw && dy >= 0 && dy < (GLint) ctx->DrawBuffer->Height) {
-         drawRb->PutRow(ctx, drawRb, width, destx, dy, span.array->rgba, NULL);
-      }
-      else {
+         /* write span */
          span.x = destx;
-         span.y = dy;
+         span.y = desty + row;
          span.end = width;
+         span.array->ChanType = GL_FLOAT;
          if (zoom) {
-            _swrast_write_zoomed_rgba_span(ctx, destx, desty, &span, 
-                                        (CONST GLchan (*)[4])span.array->rgba);
+            _swrast_write_zoomed_rgba_span(ctx, destx, desty, &span, rgba);
          }
          else {
             _swrast_write_rgba_span(ctx, &span);
          }
       }
+      /* restore this */
+      span.array->ChanType = CHAN_TYPE;
    }
 
    _mesa_free(convImage);
 }
 
 
-/*
+/**
  * RGBA copypixels
  */
 static void
 copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
                  GLint width, GLint height, GLint destx, GLint desty)
 {
-   struct gl_renderbuffer *drawRb;
-   GLchan *tmpImage,*p;
-   GLboolean quick_draw;
-   GLint sy, dy, stepy, j;
+   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;
-   struct sw_span span;
+   GLuint transferOps = ctx->_ImageTransferState;
+   SWspan span;
 
    if (!ctx->ReadBuffer->_ColorReadBuffer) {
       /* no readbuffer - OK */
       return;
    }
 
-   INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_RGBA);
-
    if (ctx->Pixel.Convolution2DEnabled || ctx->Pixel.Separable2DEnabled) {
       copy_conv_rgba_pixels(ctx, srcx, srcy, width, height, destx, desty);
       return;
    }
+   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;
@@ -286,43 +241,22 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
       stepy = 1;
    }
 
-   if (ctx->DrawBuffer == ctx->ReadBuffer) {
-      overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
-                                    ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
-   }
-   else {
-      overlapping = GL_FALSE;
-   }
-
-   if (ctx->Depth.Test)
-      _swrast_span_default_z(ctx, &span);
-   if (ctx->Fog.Enabled)
-      _swrast_span_default_fog(ctx, &span);
-
-   if (SWRAST_CONTEXT(ctx)->_RasterMask == 0
-       && !zoom
-       && destx >= 0
-       && destx + width <= (GLint) ctx->DrawBuffer->Width) {
-      quick_draw = GL_TRUE;
-      drawRb = ctx->DrawBuffer->_ColorDrawBuffers[0][0];
-   }
-   else {
-      quick_draw = GL_FALSE;
-      drawRb = NULL;
-   }
+   INIT_SPAN(span, GL_BITMAP);
+   _swrast_span_default_attribs(ctx, &span);
+   span.arrayMask = SPAN_RGBA;
+   span.arrayAttribs = FRAG_BIT_COL0; /* we'll fill in COL0 attrib values */
 
    if (overlapping) {
-      GLint ssy = sy;
-      tmpImage = (GLchan *) _mesa_malloc(width * height * sizeof(GLchan) * 4);
+      tmpImage = (GLfloat *) _mesa_malloc(width * height * sizeof(GLfloat) * 4);
       if (!tmpImage) {
          _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
          return;
       }
-      /* read the source image */
+      /* read the source image as RGBA/float */
       p = tmpImage;
-      for (j = 0; j < height; j++, ssy += stepy) {
+      for (row = 0; row < height; row++) {
          _swrast_read_rgba_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
-                                 width, srcx, ssy, (GLchan (*)[4]) p );
+                                 width, srcx, sy + row, GL_FLOAT, p );
          p += width * 4;
       }
       p = tmpImage;
@@ -332,54 +266,43 @@ 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 */
-         ASSERT(width < MAX_WIDTH);
-         MEMCPY(span.array->rgba, p, width * sizeof(GLchan) * 4);
+         _mesa_memcpy(rgba, p, width * sizeof(GLfloat) * 4);
          p += width * 4;
       }
       else {
          /* get from framebuffer */
-         ASSERT(width < MAX_WIDTH);
          _swrast_read_rgba_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
-                                 width, srcx, sy, span.array->rgba );
+                                 width, srcx, sy, GL_FLOAT, rgba );
       }
 
       if (transferOps) {
-         GLfloat rgbaFloat[MAX_WIDTH][4];
-         /* convert to float, transfer, convert back to chan */
-         chan_span_to_float(width, (CONST GLchan (*)[4]) span.array->rgba,
-                            rgbaFloat);
-         _mesa_apply_rgba_transfer_ops(ctx, transferOps, width, rgbaFloat);
-         float_span_to_chan(width, (CONST GLfloat (*)[4]) rgbaFloat,
-                            span.array->rgba);
-      }
-
-      if (ctx->Pixel.PixelTextureEnabled && ctx->Texture._EnabledUnits) {
-         span.end = width;
-         _swrast_pixel_texture(ctx, &span);
+         _mesa_apply_rgba_transfer_ops(ctx, transferOps, width,
+                                       (GLfloat (*)[4]) rgba);
       }
 
       /* Write color span */
-      if (quick_draw && dy >= 0 && dy < (GLint) ctx->DrawBuffer->Height) {
-         drawRb->PutRow(ctx, drawRb, width, destx, dy, span.array->rgba, NULL);
+      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 {
-         span.x = destx;
-         span.y = dy;
-         span.end = width;
-         if (zoom) {
-            _swrast_write_zoomed_rgba_span(ctx, destx, desty, &span,
-                                       (CONST GLchan (*)[4]) span.array->rgba);
-         }
-         else {
-            _swrast_write_rgba_span(ctx, &span);
-         }
+         _swrast_write_rgba_span(ctx, &span);
       }
    }
 
+   span.array->ChanType = CHAN_TYPE; /* restore */
+
    if (overlapping)
       _mesa_free(tmpImage);
 }
@@ -394,19 +317,28 @@ copy_ci_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
    GLint sy, dy, stepy;
    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;
-   struct sw_span span;
+   SWspan span;
 
    if (!ctx->ReadBuffer->_ColorReadBuffer) {
       /* no readbuffer - OK */
       return;
    }
 
-   INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_INDEX);
+   INIT_SPAN(span, GL_BITMAP);
+   _swrast_span_default_attribs(ctx, &span);
+   span.arrayMask = SPAN_INDEX;
+
+   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;
@@ -419,19 +351,6 @@ copy_ci_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
       stepy = 1;
    }
 
-   if (ctx->DrawBuffer == ctx->ReadBuffer) {
-      overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
-                                    ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
-   }
-   else {
-      overlapping = GL_FALSE;
-   }
-
-   if (ctx->Depth.Test)
-      _swrast_span_default_z(ctx, &span);
-   if (ctx->Fog.Enabled)
-      _swrast_span_default_fog(ctx, &span);
-
    if (overlapping) {
       GLint ssy = sy;
       tmpImage = (GLuint *) _mesa_malloc(width * height * sizeof(GLuint));
@@ -456,7 +375,7 @@ copy_ci_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
    for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
       /* Get color indexes */
       if (overlapping) {
-         MEMCPY(span.array->index, p, width * sizeof(GLuint));
+         _mesa_memcpy(span.array->index, p, width * sizeof(GLuint));
          p += width;
       }
       else {
@@ -464,13 +383,9 @@ copy_ci_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
                                   width, srcx, sy, span.array->index );
       }
 
-      /* Apply shift, offset, look-up table */
-      if (shift_or_offset) {
-         _mesa_shift_and_offset_ci( ctx, width, span.array->index );
-      }
-      if (ctx->Pixel.MapColorFlag) {
-         _mesa_map_ci( ctx, width, span.array->index );
-      }
+      if (ctx->_ImageTransferState)
+         _mesa_apply_ci_transfer_ops(ctx, ctx->_ImageTransferState,
+                                     width, span.array->index);
 
       /* write color indexes */
       span.x = destx;
@@ -487,6 +402,41 @@ copy_ci_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
 }
 
 
+/**
+ * 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 {
+      /* 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!!!!
@@ -497,24 +447,33 @@ copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
                    GLint destx, GLint desty )
 {
    struct gl_framebuffer *fb = ctx->ReadBuffer;
-   struct gl_renderbuffer *readRb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
-   const GLfloat depthMax = fb->_DepthMaxF;
+   struct gl_renderbuffer *readRb = fb->_DepthBuffer;
    GLfloat *p, *tmpImage;
    GLint sy, dy, stepy;
-   GLint i, j;
+   GLint j;
    const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
    GLint overlapping;
-   struct sw_span span;
+   SWspan span;
 
    if (!readRb) {
       /* no readbuffer - OK */
       return;
    }
 
-   INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_Z);
+   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;
@@ -527,18 +486,6 @@ copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
       stepy = 1;
    }
 
-   if (ctx->DrawBuffer == ctx->ReadBuffer) {
-      overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
-                                    ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
-   }
-   else {
-      overlapping = GL_FALSE;
-   }
-
-   _swrast_span_default_color(ctx, &span);
-   if (ctx->Fog.Enabled)
-      _swrast_span_default_fog(ctx, &span);
-
    if (overlapping) {
       GLint ssy = sy;
       tmpImage = (GLfloat *) _mesa_malloc(width * height * sizeof(GLfloat));
@@ -562,7 +509,7 @@ copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
       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 {
@@ -570,10 +517,7 @@ copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
       }
 
       /* apply scale and bias */
-      for (i = 0; i < width; i++) {
-         GLfloat d = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
-         span.array->z[i] = (GLuint) (CLAMP(d, 0.0F, 1.0F) * depthMax);
-      }
+      scale_and_bias_z(ctx, width, depth, span.array->z);
 
       /* write depth values */
       span.x = destx;
@@ -581,14 +525,13 @@ copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
       span.end = width;
       if (fb->Visual.rgbMode) {
          if (zoom)
-            _swrast_write_zoomed_rgba_span(ctx, destx, desty, &span, 
-                                       (const GLchan (*)[4]) span.array->rgba);
+            _swrast_write_zoomed_depth_span(ctx, destx, desty, &span);
          else
             _swrast_write_rgba_span(ctx, &span);
       }
       else {
          if (zoom)
-            _swrast_write_zoomed_index_span(ctx, destx, desty, &span);
+            _swrast_write_zoomed_depth_span(ctx, destx, desty, &span);
          else
             _swrast_write_index_span(ctx, &span);
       }
@@ -606,12 +549,11 @@ copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
                      GLint destx, GLint desty )
 {
    struct gl_framebuffer *fb = ctx->ReadBuffer;
-   struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
+   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 (!rb) {
@@ -619,8 +561,16 @@ copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
       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;
@@ -633,14 +583,6 @@ copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
       stepy = 1;
    }
 
-   if (ctx->DrawBuffer == ctx->ReadBuffer) {
-      overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
-                                    ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
-   }
-   else {
-      overlapping = GL_FALSE;
-   }
-
    if (overlapping) {
       GLint ssy = sy;
       tmpImage = (GLstencil *) _mesa_malloc(width * height * sizeof(GLstencil));
@@ -665,20 +607,14 @@ copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
 
       /* Get stencil values */
       if (overlapping) {
-         MEMCPY(stencil, p, width * sizeof(GLstencil));
+         _mesa_memcpy(stencil, p, width * sizeof(GLstencil));
          p += width;
       }
       else {
          _swrast_read_stencil_span( ctx, rb, width, srcx, sy, stencil );
       }
 
-      /* Apply shift, offset, look-up table */
-      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) {
@@ -696,7 +632,7 @@ copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
 
 
 /**
- * This isn't terribly efficient.  If a drivef really has combined
+ * This isn't terribly efficient.  If a driver really has combined
  * depth/stencil buffers the driver should implement an optimized
  * CopyPixels function.
  */
@@ -714,22 +650,28 @@ copy_depth_stencil_pixels(GLcontext *ctx,
    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 shiftOrOffset
-      = ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset;
    const GLboolean scaleOrBias
       = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
    GLint overlapping;
 
-   depthDrawRb = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
-   depthReadRb = ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
-   stencilReadRb = ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
+   depthDrawRb = ctx->DrawBuffer->_DepthBuffer;
+   depthReadRb = ctx->ReadBuffer->_DepthBuffer;
+   stencilReadRb = ctx->ReadBuffer->_StencilBuffer;
 
    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 (srcY < destY) {
+   if (!overlapping && srcY < destY) {
       /* top-down  max-to-min */
       sy = srcY + height - 1;
       dy = destY + height - 1;
@@ -742,14 +684,6 @@ copy_depth_stencil_pixels(GLcontext *ctx,
       stepy = 1;
    }
 
-   if (ctx->DrawBuffer == ctx->ReadBuffer) {
-      overlapping = regions_overlap(srcX, srcY, destX, destY, width, height,
-                                    ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
-   }
-   else {
-      overlapping = GL_FALSE;
-   }
-
    if (overlapping) {
       GLint ssy = sy;
 
@@ -805,13 +739,7 @@ copy_depth_stencil_pixels(GLcontext *ctx,
                                       width, srcX, sy, stencil);
          }
 
-         /* Apply shift, offset, look-up table */
-         if (shiftOrOffset) {
-            _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 values */
          if (zoom) {
@@ -879,6 +807,88 @@ copy_depth_stencil_pixels(GLcontext *ctx,
 }
 
 
+
+/**
+ * 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)
+{
+   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) {
+      if (dstFb->_NumColorDrawBuffers != 1)
+         return GL_FALSE;
+      srcRb = srcFb->_ColorReadBuffer;
+      dstRb = dstFb->_ColorDrawBuffers[0];
+   }
+   else if (type == GL_STENCIL) {
+      srcRb = srcFb->_StencilBuffer;
+      dstRb = dstFb->_StencilBuffer;
+   }
+   else if (type == GL_DEPTH) {
+      srcRb = srcFb->_DepthBuffer;
+      dstRb = dstFb->_DepthBuffer;
+   }
+   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 {
+      /* 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.
@@ -894,26 +904,28 @@ _swrast_CopyPixels( GLcontext *ctx,
    if (swrast->NewState)
       _swrast_validate_derived( ctx );
 
-   switch (type) {
-   case GL_COLOR:
-      if (ctx->Visual.rgbMode) {
-         copy_rgba_pixels( ctx, srcx, srcy, width, height, destx, desty );
+   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");
       }
-      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);