added missing \'s
[mesa.git] / src / mesa / swrast / s_copypix.c
index a379cf0d5ce736a171eed6633a9d4bada9b93b96..ab8198945ce992ed48b01447fc8d7797c6879faa 100644 (file)
@@ -1,21 +1,21 @@
-/* $Id: s_copypix.c,v 1.3 2000/11/05 18:24:40 keithw Exp $ */
+/* $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-2000  Brian Paul   All Rights Reserved.
- * 
+ *
+ * Copyright (C) 1999-2001  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
@@ -37,7 +37,8 @@
 
 #include "s_context.h"
 #include "s_depth.h"
-#include "s_imaging.h"
+#include "s_fog.h"
+#include "s_histogram.h"
 #include "s_pixeltex.h"
 #include "s_span.h"
 #include "s_stencil.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.
  */
 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 {
+         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;
+      }
    }
 }
 
@@ -76,7 +97,9 @@ static void
 copy_conv_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];
    GLboolean quick_draw;
    GLint row;
    GLboolean changeBuffer;
@@ -87,11 +110,18 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
 
    if (ctx->Depth.Test || ctx->Fog.Enabled) {
       /* fill in array of z values */
-      GLdepth z = (GLdepth)
-         (ctx->Current.RasterPos[2] * ctx->Visual.DepthMax);
+      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;
       }
    }
 
@@ -114,20 +144,20 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
    /* allocate space for GLfloat image */
    tmpImage = (GLfloat *) 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));
    if (!convImage) {
       FREE(tmpImage);
-      gl_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
+      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
       return;
    }
 
    dest = tmpImage;
 
    if (changeBuffer) {
-      (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
+      (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
                                     ctx->Pixel.DriverReadBuffer );
       if (ctx->Pixel.DriverReadBuffer == GL_FRONT_LEFT)
          ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontLeftAlpha;
@@ -144,7 +174,7 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
    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);
+      _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);
@@ -156,7 +186,7 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
 
    /* read from the draw buffer again (in case of blending) */
    if (changeBuffer) {
-      (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
+      (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
                                     ctx->Color.DriverDrawBuffer );
       ctx->ReadBuffer->Alpha = saveReadAlpha;
    }
@@ -167,7 +197,11 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
 
       /* scale & bias */
       if (transferOps & IMAGE_SCALE_BIAS_BIT) {
-         _mesa_scale_and_bias_rgba(ctx, width, rgba);
+         _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) {
@@ -240,11 +274,12 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
 
          MEMCPY(primary_rgba, rgba, 4 * width * sizeof(GLchan));
 
-         for (unit = 0; unit < MAX_TEXTURE_UNITS; unit++) {
+         for (unit = 0; unit < ctx->Const.MaxTextureUnits; 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);
+            _swrast_texture_fragments(ctx, unit, width, s, t, r, NULL,
+                                      (CONST GLchan (*)[4]) primary_rgba,
+                                      rgba);
          }
       }
 
@@ -252,15 +287,16 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
 
       dy = desty + row;
       if (quick_draw && dy >= 0 && dy < ctx->DrawBuffer->Height) {
-         (*ctx->Driver.WriteRGBASpan)( ctx, width, destx, dy, 
+         (*swrast->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, 
+         _mesa_write_zoomed_rgba_span( ctx, width, destx, dy, zspan, fogSpan,
                                    (const GLchan (*)[4])rgba, desty);
       }
       else {
-         gl_write_rgba_span( ctx, width, destx, dy, zspan, 0, rgba, GL_BITMAP );
+         _mesa_write_rgba_span( ctx, width, destx, dy, zspan, fogSpan, rgba,
+                                NULL, GL_BITMAP );
       }
    }
 
@@ -275,7 +311,9 @@ 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;
@@ -311,9 +349,17 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
 
    if (ctx->Depth.Test || ctx->Fog.Enabled) {
       /* fill in array of z values */
-      GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * ctx->Visual.DepthMax);
+      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;
       }
    }
 
@@ -332,19 +378,19 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
    changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer
                   || ctx->DrawBuffer != ctx->ReadBuffer;
 
-   (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
+   (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
                                  ctx->Pixel.DriverReadBuffer );
 
    if (overlapping) {
       GLint ssy = sy;
       tmpImage = (GLchan *) MALLOC(width * height * sizeof(GLchan) * 4);
       if (!tmpImage) {
-         gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
+         _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
          return;
       }
       p = tmpImage;
       if (changeBuffer) {
-         (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
+         (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
                                        ctx->Pixel.DriverReadBuffer );
          if (ctx->Pixel.DriverReadBuffer == GL_FRONT_LEFT)
             ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontLeftAlpha;
@@ -356,7 +402,7 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
             ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackRightAlpha;
       }
       for (j = 0; j < height; j++, ssy += stepy) {
-         gl_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, ssy,
+         _mesa_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, ssy,
                             (GLchan (*)[4]) p );
          p += (width * sizeof(GLchan) * 4);
       }
@@ -377,7 +423,7 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
       else {
          /* get from framebuffer */
          if (changeBuffer) {
-            (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
+            (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
                                           ctx->Pixel.DriverReadBuffer );
             if (ctx->Pixel.DriverReadBuffer == GL_FRONT_LEFT) {
                ctx->ReadBuffer->Alpha = ctx->ReadBuffer->FrontLeftAlpha;
@@ -392,20 +438,22 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
                ctx->ReadBuffer->Alpha = ctx->ReadBuffer->BackRightAlpha;
             }
          }
-         gl_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, sy, rgba );
+         _mesa_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,
+         (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
                                        ctx->Color.DriverDrawBuffer );
          ctx->ReadBuffer->Alpha = saveReadAlpha;
       }
 
       if (transferOps) {
          const GLfloat scale = (1.0F / CHAN_MAXF);
-         GLfloat rgbaFloat[MAX_WIDTH][4];
-         GLuint k;
+         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;
@@ -415,7 +463,11 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
          }
          /* scale & bias */
          if (transferOps & IMAGE_SCALE_BIAS_BIT) {
-            _mesa_scale_and_bias_rgba(ctx, width, rgbaFloat);
+            _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) {
@@ -429,6 +481,18 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
          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);
@@ -460,39 +524,54 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
             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) {
-         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 */
+         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 < MAX_TEXTURE_UNITS; unit++) {
+         for (unit = 0; unit < ctx->Const.MaxTextureUnits; 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);
+            _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) {
-         (*ctx->Driver.WriteRGBASpan)( ctx, width, destx, dy, 
+         (*swrast->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,
+         _mesa_write_zoomed_rgba_span( ctx, width, destx, dy, zspan, fogSpan,
                                    (const GLchan (*)[4])rgba, desty);
       }
       else {
-         gl_write_rgba_span( ctx, width, destx, dy, zspan, 0, rgba, GL_BITMAP );
+         _mesa_write_rgba_span( ctx, width, destx, dy, zspan, fogSpan, rgba,
+                                NULL, GL_BITMAP );
       }
    }
 
    /* Restore pixel source to be the draw buffer (for blending, etc) */
-   (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
+   (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
                                  ctx->Color.DriverDrawBuffer );
 
    if (overlapping)
@@ -504,7 +583,9 @@ static void copy_ci_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];
    GLuint *tmpImage,*p;
    GLint sy, dy, stepy;
    GLint i, j;
@@ -532,9 +613,17 @@ static void copy_ci_pixels( GLcontext *ctx,
 
    if (ctx->Depth.Test || ctx->Fog.Enabled) {
       /* fill in array of z values */
-      GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * ctx->Visual.DepthMax);
+      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;
       }
    }
 
@@ -542,23 +631,23 @@ static void copy_ci_pixels( GLcontext *ctx,
    changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer
                || ctx->DrawBuffer != ctx->ReadBuffer;
 
-   (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
+   (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
                                  ctx->Pixel.DriverReadBuffer );
 
    if (overlapping) {
       GLint ssy = sy;
       tmpImage = (GLuint *) MALLOC(width * height * sizeof(GLuint));
       if (!tmpImage) {
-         gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
+         _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
          return;
       }
       p = tmpImage;
       if (changeBuffer) {
-         (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
+         (*swrast->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 );
+         _mesa_read_index_span( ctx, ctx->ReadBuffer, width, srcx, ssy, p );
          p += width;
       }
       p = tmpImage;
@@ -576,15 +665,15 @@ static void copy_ci_pixels( GLcontext *ctx,
       }
       else {
          if (changeBuffer) {
-            (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
+            (*swrast->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
                                           ctx->Pixel.DriverReadBuffer );
          }
-         gl_read_index_span( ctx, ctx->ReadBuffer, width, srcx, sy, indexes );
+         _mesa_read_index_span( ctx, ctx->ReadBuffer, width, srcx, sy, indexes );
       }
 
       if (changeBuffer) {
          /* set read buffer back to draw buffer (in case of logicops) */
-         (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
+         (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
                                        ctx->Color.DriverDrawBuffer );
       }
 
@@ -596,15 +685,17 @@ static void copy_ci_pixels( GLcontext *ctx,
       }
 
       if (zoom) {
-         gl_write_zoomed_index_span( ctx, width, destx, dy, zspan, 0, indexes, desty );
+         _mesa_write_zoomed_index_span(ctx, width, destx, dy, zspan, fogSpan,
+                                       indexes, desty );
       }
       else {
-         gl_write_index_span(ctx, width, destx, dy, zspan, 0, indexes, GL_BITMAP);
+         _mesa_write_index_span(ctx, width, destx, dy, zspan, fogSpan, indexes,
+                                NULL, GL_BITMAP);
       }
    }
 
    /* Restore pixel source to be the draw buffer (for blending, etc) */
-   (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
+   (*swrast->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
                                  ctx->Color.DriverDrawBuffer );
 
    if (overlapping)
@@ -622,16 +713,19 @@ static void copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
 {
    GLfloat depth[MAX_WIDTH];
    GLdepth zspan[MAX_WIDTH];
+   GLfloat fogSpan[MAX_WIDTH];
    GLfloat *p, *tmpImage;
    GLuint indexes[MAX_WIDTH];
-   GLchan rgba[MAX_WIDTH][4];
    GLint sy, dy, stepy;
    GLint i, j;
    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->ReadBuffer->DepthBuffer || !ctx->DrawBuffer->DepthBuffer) {
-      gl_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
+   if (!ctx->Visual.depthBits) {
+      _mesa_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
+      UNDEFARRAY(rgba);  /* mac 32k limitation */
       return;
    }
 
@@ -653,7 +747,7 @@ static void copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
                                  ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
 
    /* setup colors or indexes */
-   if (ctx->Visual.RGBAflag) {
+   if (ctx->Visual.rgbMode) {
       GLuint *rgba32 = (GLuint *) rgba;
       GLuint color = *(GLuint*)( ctx->Current.Color );
       for (i = 0; i < width; i++) {
@@ -666,11 +760,25 @@ static void copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
       }
    }
 
+   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));
       if (!tmpImage) {
-         gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
+         _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
+         UNDEFARRAY(rgba);  /* mac 32k limitation */
          return;
       }
       p = tmpImage;
@@ -696,33 +804,35 @@ static void copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
 
       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);
+         zspan[i] = (GLdepth) (CLAMP(d, 0.0F, 1.0F) * ctx->DepthMax);
       }
 
-      if (ctx->Visual.RGBAflag) {
+      if (ctx->Visual.rgbMode) {
          if (zoom) {
-            gl_write_zoomed_rgba_span( ctx, width, destx, dy, zspan, 0,
-                                      (const GLchan (*)[4])rgba, desty );
+            _mesa_write_zoomed_rgba_span( ctx, width, destx, dy, zspan,
+                                   fogSpan, (const GLchan (*)[4])rgba, desty );
          }
          else {
-            gl_write_rgba_span( ctx, width, destx, dy, zspan, 0, 
-                               rgba, GL_BITMAP);
+            _mesa_write_rgba_span( ctx, width, destx, dy, zspan, fogSpan,
+                                   rgba, NULL, GL_BITMAP);
          }
       }
       else {
          if (zoom) {
-            gl_write_zoomed_index_span( ctx, width, destx, dy,
-                                        zspan, 0, indexes, desty );
+            _mesa_write_zoomed_index_span( ctx, width, destx, dy,
+                                           zspan, fogSpan, indexes, desty );
          }
          else {
-            gl_write_index_span( ctx, width, destx, dy,
-                                 zspan, 0, indexes, GL_BITMAP );
+            _mesa_write_index_span( ctx, width, destx, dy,
+                                    zspan, fogSpan, indexes, NULL, GL_BITMAP );
          }
       }
    }
 
-  if (overlapping)
-     FREE(tmpImage);
+   UNDEFARRAY(rgba);  /* mac 32k limitation */
+
+   if (overlapping)
+      FREE(tmpImage);
 }
 
 
@@ -738,8 +848,8 @@ static void copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
    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 (!ctx->Visual.stencilBits) {
+      _mesa_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
       return;
    }
 
@@ -764,7 +874,7 @@ static void copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
       GLint ssy = sy;
       tmpImage = (GLstencil *) 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;
@@ -798,7 +908,7 @@ static void copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
       }
 
       if (zoom) {
-         gl_write_zoomed_stencil_span( ctx, width, destx, dy, stencil, desty );
+         _mesa_write_zoomed_stencil_span( ctx, width, destx, dy, stencil, desty );
       }
       else {
          _mesa_write_stencil_span( ctx, width, destx, dy, stencil );
@@ -818,13 +928,16 @@ _swrast_CopyPixels( GLcontext *ctx,
                    GLint destx, GLint desty,
                    GLenum type )
 {
-   if (SWRAST_CONTEXT(ctx)->NewState)
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
+   RENDER_START(swrast,ctx);
+      
+   if (swrast->NewState)
       _swrast_validate_derived( ctx );
 
-   if (type == GL_COLOR && ctx->Visual.RGBAflag) {
+   if (type == GL_COLOR && ctx->Visual.rgbMode) {
       copy_rgba_pixels( ctx, srcx, srcy, width, height, destx, desty );
    }
-   else if (type == GL_COLOR && !ctx->Visual.RGBAflag) {
+   else if (type == GL_COLOR && !ctx->Visual.rgbMode) {
       copy_ci_pixels( ctx, srcx, srcy, width, height, destx, desty );
    }
    else if (type == GL_DEPTH) {
@@ -834,6 +947,8 @@ _swrast_CopyPixels( GLcontext *ctx,
       copy_stencil_pixels( ctx, srcx, srcy, width, height, destx, desty );
    }
    else {
-      gl_error( ctx, GL_INVALID_ENUM, "glCopyPixels" );
+      _mesa_error( ctx, GL_INVALID_ENUM, "glCopyPixels" );
    }
+
+   RENDER_FINISH(swrast,ctx);
 }