r600g: Implement GL_ARB_texture_gather
[mesa.git] / src / mesa / swrast / s_depth.c
index 09c0be68f07b2962a1c969f942a8a9478527727c..134f897c039fb31cd4743051b88119ca7f19eeed 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * Mesa 3-D graphics library
- * Version:  7.2.1
  *
  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
  *
  * 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 "main/glheader.h"
 #include "main/context.h"
 #include "main/formats.h"
+#include "main/format_unpack.h"
 #include "main/format_pack.h"
 #include "main/macros.h"
 #include "main/imports.h"
 
+#include "s_context.h"
 #include "s_depth.h"
 #include "s_span.h"
 
 
+
+#define Z_TEST(COMPARE)                      \
+   do {                                      \
+      GLuint i;                              \
+      for (i = 0; i < n; i++) {              \
+         if (mask[i]) {                      \
+            if (COMPARE) {                   \
+               /* pass */                    \
+               if (write) {                  \
+                  zbuffer[i] = zfrag[i];     \
+               }                             \
+               passed++;                     \
+            }                                \
+            else {                           \
+               /* fail */                    \
+               mask[i] = 0;                  \
+            }                                \
+         }                                   \
+      }                                      \
+   } while (0)
+
+
 /**
- * Do depth test for a horizontal span of fragments.
- * Input:  zbuffer - array of z values in the zbuffer
- *         z - array of fragment z values
- * Return:  number of fragments which pass the test.
+ * Do depth test for an array of 16-bit Z values.
+ * @param zbuffer  array of Z buffer values (16-bit)
+ * @param zfrag  array of fragment Z values (use 16-bit in 32-bit uint)
+ * @param mask  which fragments are alive, killed afterward
+ * @return  number of fragments which pass the test.
  */
 static GLuint
 depth_test_span16( struct gl_context *ctx, GLuint n,
-                   GLushort zbuffer[], const GLuint z[], GLubyte mask[] )
+                   GLushort zbuffer[], const GLuint zfrag[], GLubyte mask[] )
 {
+   const GLboolean write = ctx->Depth.Mask;
    GLuint passed = 0;
 
    /* switch cases ordered from most frequent to less frequent */
    switch (ctx->Depth.Func) {
-      case GL_LESS:
-         if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 if (z[i] < zbuffer[i]) {
-                    /* pass */
-                    zbuffer[i] = z[i];
-                    passed++;
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 if (z[i] < zbuffer[i]) {
-                    /* pass */
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_LEQUAL:
-        if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 if (z[i] <= zbuffer[i]) {
-                    zbuffer[i] = z[i];
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 if (z[i] <= zbuffer[i]) {
-                    /* pass */
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_GEQUAL:
-        if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 if (z[i] >= zbuffer[i]) {
-                    zbuffer[i] = z[i];
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 if (z[i] >= zbuffer[i]) {
-                    /* pass */
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_GREATER:
-        if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 if (z[i] > zbuffer[i]) {
-                    zbuffer[i] = z[i];
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 if (z[i] > zbuffer[i]) {
-                    /* pass */
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_NOTEQUAL:
-        if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 if (z[i] != zbuffer[i]) {
-                    zbuffer[i] = z[i];
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 if (z[i] != zbuffer[i]) {
-                    /* pass */
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_EQUAL:
-        if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 if (z[i] == zbuffer[i]) {
-                    zbuffer[i] = z[i];
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 if (z[i] == zbuffer[i]) {
-                    /* pass */
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_ALWAYS:
-        if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 zbuffer[i] = z[i];
-                 passed++;
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer or mask */
-           passed = n;
-        }
-        break;
-      case GL_NEVER:
-         memset(mask, 0, n * sizeof(GLubyte));
-        break;
-      default:
-         _mesa_problem(ctx, "Bad depth func in depth_test_span16");
+   case GL_LESS:
+      Z_TEST(zfrag[i] < zbuffer[i]);
+      break;
+   case GL_LEQUAL:
+      Z_TEST(zfrag[i] <= zbuffer[i]);
+      break;
+   case GL_GEQUAL:
+      Z_TEST(zfrag[i] >= zbuffer[i]);
+      break;
+   case GL_GREATER:
+      Z_TEST(zfrag[i] > zbuffer[i]);
+      break;
+   case GL_NOTEQUAL:
+      Z_TEST(zfrag[i] != zbuffer[i]);
+      break;
+   case GL_EQUAL:
+      Z_TEST(zfrag[i] == zbuffer[i]);
+      break;
+   case GL_ALWAYS:
+      Z_TEST(1);
+      break;
+   case GL_NEVER:
+      memset(mask, 0, n * sizeof(GLubyte));
+      break;
+   default:
+      _mesa_problem(ctx, "Bad depth func in depth_test_span16");
    }
 
    return passed;
 }
 
 
+/**
+ * Do depth test for an array of 32-bit Z values.
+ * @param zbuffer  array of Z buffer values (32-bit)
+ * @param zfrag  array of fragment Z values (use 32-bits in 32-bit uint)
+ * @param mask  which fragments are alive, killed afterward
+ * @return  number of fragments which pass the test.
+ */
 static GLuint
 depth_test_span32( struct gl_context *ctx, GLuint n,
-                   GLuint zbuffer[], const GLuint z[], GLubyte mask[] )
+                   GLuint zbuffer[], const GLuint zfrag[], GLubyte mask[])
 {
+   const GLboolean write = ctx->Depth.Mask;
    GLuint passed = 0;
 
    /* switch cases ordered from most frequent to less frequent */
    switch (ctx->Depth.Func) {
-      case GL_LESS:
-         if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 if (z[i] < zbuffer[i]) {
-                    /* pass */
-                    zbuffer[i] = z[i];
-                    passed++;
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 if (z[i] < zbuffer[i]) {
-                    /* pass */
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_LEQUAL:
-        if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 if (z[i] <= zbuffer[i]) {
-                    zbuffer[i] = z[i];
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 if (z[i] <= zbuffer[i]) {
-                    /* pass */
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_GEQUAL:
-        if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 if (z[i] >= zbuffer[i]) {
-                    zbuffer[i] = z[i];
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 if (z[i] >= zbuffer[i]) {
-                    /* pass */
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_GREATER:
-        if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 if (z[i] > zbuffer[i]) {
-                    zbuffer[i] = z[i];
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 if (z[i] > zbuffer[i]) {
-                    /* pass */
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_NOTEQUAL:
-        if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 if (z[i] != zbuffer[i]) {
-                    zbuffer[i] = z[i];
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 if (z[i] != zbuffer[i]) {
-                    /* pass */
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_EQUAL:
-        if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 if (z[i] == zbuffer[i]) {
-                    zbuffer[i] = z[i];
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 if (z[i] == zbuffer[i]) {
-                    /* pass */
-                    passed++;
-                 }
-                 else {
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_ALWAYS:
-        if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0;i<n;i++) {
-              if (mask[i]) {
-                 zbuffer[i] = z[i];
-                 passed++;
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer or mask */
-           passed = n;
-        }
-        break;
-      case GL_NEVER:
-         memset(mask, 0, n * sizeof(GLubyte));
-        break;
-      default:
-         _mesa_problem(ctx, "Bad depth func in depth_test_span32");
+   case GL_LESS:
+      Z_TEST(zfrag[i] < zbuffer[i]);
+      break;
+   case GL_LEQUAL:
+      Z_TEST(zfrag[i] <= zbuffer[i]);
+      break;
+   case GL_GEQUAL:
+      Z_TEST(zfrag[i] >= zbuffer[i]);
+      break;
+   case GL_GREATER:
+      Z_TEST(zfrag[i] > zbuffer[i]);
+      break;
+   case GL_NOTEQUAL:
+      Z_TEST(zfrag[i] != zbuffer[i]);
+      break;
+   case GL_EQUAL:
+      Z_TEST(zfrag[i] == zbuffer[i]);
+      break;
+   case GL_ALWAYS:
+      Z_TEST(1);
+      break;
+   case GL_NEVER:
+      memset(mask, 0, n * sizeof(GLubyte));
+      break;
+   default:
+      _mesa_problem(ctx, "Bad depth func in depth_test_span32");
    }
 
    return passed;
 }
 
 
-
 /**
  * Clamp fragment Z values to the depth near/far range (glDepthRange()).
  * This is used when GL_ARB_depth_clamp/GL_DEPTH_CLAMP is turned on.
@@ -516,12 +171,12 @@ _swrast_depth_clamp_span( struct gl_context *ctx, SWspan *span )
    GLfloat min_f, max_f;
    GLuint i;
 
-   if (ctx->Viewport.Near < ctx->Viewport.Far) {
-      min_f = ctx->Viewport.Near;
-      max_f = ctx->Viewport.Far;
+   if (ctx->ViewportArray[0].Near < ctx->ViewportArray[0].Far) {
+      min_f = ctx->ViewportArray[0].Near;
+      max_f = ctx->ViewportArray[0].Far;
    } else {
-      min_f = ctx->Viewport.Far;
-      max_f = ctx->Viewport.Near;
+      min_f = ctx->ViewportArray[0].Far;
+      max_f = ctx->ViewportArray[0].Near;
    }
 
    /* Convert floating point values in [0,1] to device Z coordinates in
@@ -548,615 +203,206 @@ _swrast_depth_clamp_span( struct gl_context *ctx, SWspan *span )
 }
 
 
-
-/*
- * Apply depth test to span of fragments.
+/**
+ * Get array of 32-bit z values from the depth buffer.  With clipping.
+ * Note: the returned values are always in the range [0, 2^32-1].
  */
-static GLuint
-depth_test_span( struct gl_context *ctx, SWspan *span)
+static void
+get_z32_values(struct gl_context *ctx, struct gl_renderbuffer *rb,
+               GLuint count, const GLint x[], const GLint y[],
+               GLuint zbuffer[])
 {
-   struct gl_framebuffer *fb = ctx->DrawBuffer;
-   struct gl_renderbuffer *rb = fb->_DepthBuffer;
-   const GLint x = span->x;
-   const GLint y = span->y;
-   const GLuint count = span->end;
-   const GLuint *zValues = span->array->z;
-   GLubyte *mask = span->array->mask;
-   GLuint passed;
+   struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
+   const GLint w = rb->Width, h = rb->Height;
+   const GLubyte *map = _swrast_pixel_address(rb, 0, 0);
+   GLuint i;
 
-   ASSERT((span->arrayMask & SPAN_XY) == 0);
-   ASSERT(span->arrayMask & SPAN_Z);
-   
-   if (rb->GetPointer(ctx, rb, 0, 0)) {
-      /* Directly access buffer */
-      if (rb->DataType == GL_UNSIGNED_SHORT) {
-         GLushort *zbuffer = (GLushort *) rb->GetPointer(ctx, rb, x, y);
-         passed = depth_test_span16(ctx, count, zbuffer, zValues, mask);
-      }
-      else {
-         GLuint *zbuffer = (GLuint *) rb->GetPointer(ctx, rb, x, y);
-         ASSERT(rb->DataType == GL_UNSIGNED_INT);
-         passed = depth_test_span32(ctx, count, zbuffer, zValues, mask);
+   if (rb->Format == MESA_FORMAT_Z_UNORM32) {
+      const GLint rowStride = srb->RowStride;
+      for (i = 0; i < count; i++) {
+         if (x[i] >= 0 && y[i] >= 0 && x[i] < w && y[i] < h) {
+            zbuffer[i] = *((GLuint *) (map + y[i] * rowStride + x[i] * 4));
+         }
       }
    }
    else {
-      /* read depth values from buffer, test, write back */
-      if (rb->DataType == GL_UNSIGNED_SHORT) {
-         GLushort zbuffer[MAX_WIDTH];
-         rb->GetRow(ctx, rb, count, x, y, zbuffer);
-         passed = depth_test_span16(ctx, count, zbuffer, zValues, mask);
-         rb->PutRow(ctx, rb, count, x, y, zbuffer, mask);
-      }
-      else {
-         GLuint zbuffer[MAX_WIDTH];
-         ASSERT(rb->DataType == GL_UNSIGNED_INT);
-         rb->GetRow(ctx, rb, count, x, y, zbuffer);
-         passed = depth_test_span32(ctx, count, zbuffer, zValues, mask);
-         rb->PutRow(ctx, rb, count, x, y, zbuffer, mask);
+      const GLint bpp = _mesa_get_format_bytes(rb->Format);
+      const GLint rowStride = srb->RowStride;
+      for (i = 0; i < count; i++) {
+         if (x[i] >= 0 && y[i] >= 0 && x[i] < w && y[i] < h) {
+            const GLubyte *src = map + y[i] * rowStride+ x[i] * bpp;
+            _mesa_unpack_uint_z_row(rb->Format, 1, src, &zbuffer[i]);
+         }
       }
    }
-
-   if (passed < count) {
-      span->writeAll = GL_FALSE;
-   }
-   return passed;
 }
 
 
-
-#define Z_ADDRESS(X, Y)   (zStart + (Y) * stride + (X))
-
-
-/*
- * Do depth testing for an array of fragments at assorted locations.
+/**
+ * Put an array of 32-bit z values into the depth buffer.
+ * Note: the z values are always in the range [0, 2^32-1].
  */
 static void
-direct_depth_test_pixels16(struct gl_context *ctx, GLushort *zStart, GLuint stride,
-                           GLuint n, const GLint x[], const GLint y[],
-                           const GLuint z[], GLubyte mask[] )
+put_z32_values(struct gl_context *ctx, struct gl_renderbuffer *rb,
+               GLuint count, const GLint x[], const GLint y[],
+               const GLuint zvalues[], const GLubyte mask[])
 {
-   /* switch cases ordered from most frequent to less frequent */
-   switch (ctx->Depth.Func) {
-      case GL_LESS:
-         if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] < *zptr) {
-                    /* pass */
-                    *zptr = z[i];
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] < *zptr) {
-                    /* pass */
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_LEQUAL:
-         if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] <= *zptr) {
-                    /* pass */
-                    *zptr = z[i];
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] <= *zptr) {
-                    /* pass */
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_GEQUAL:
-         if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] >= *zptr) {
-                    /* pass */
-                    *zptr = z[i];
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] >= *zptr) {
-                    /* pass */
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_GREATER:
-         if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] > *zptr) {
-                    /* pass */
-                    *zptr = z[i];
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] > *zptr) {
-                    /* pass */
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_NOTEQUAL:
-         if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] != *zptr) {
-                    /* pass */
-                    *zptr = z[i];
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] != *zptr) {
-                    /* pass */
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_EQUAL:
-         if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] == *zptr) {
-                    /* pass */
-                    *zptr = z[i];
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] == *zptr) {
-                    /* pass */
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_ALWAYS:
-        if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
-                 *zptr = z[i];
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer or mask */
-        }
-        break;
-      case GL_NEVER:
-        /* depth test never passes */
-         memset(mask, 0, n * sizeof(GLubyte));
-        break;
-      default:
-         _mesa_problem(ctx, "Bad depth func in direct_depth_test_pixels");
-   }
-}
-
-
+   struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
+   const GLint w = rb->Width, h = rb->Height;
+   GLubyte *map = _swrast_pixel_address(rb, 0, 0);
+   GLuint i;
 
-/*
- * Do depth testing for an array of fragments with direct access to zbuffer.
- */
-static void
-direct_depth_test_pixels32(struct gl_context *ctx, GLuint *zStart, GLuint stride,
-                           GLuint n, const GLint x[], const GLint y[],
-                           const GLuint z[], GLubyte mask[] )
-{
-   /* switch cases ordered from most frequent to less frequent */
-   switch (ctx->Depth.Func) {
-      case GL_LESS:
-         if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] < *zptr) {
-                    /* pass */
-                    *zptr = z[i];
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] < *zptr) {
-                    /* pass */
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_LEQUAL:
-         if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] <= *zptr) {
-                    /* pass */
-                    *zptr = z[i];
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] <= *zptr) {
-                    /* pass */
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_GEQUAL:
-         if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] >= *zptr) {
-                    /* pass */
-                    *zptr = z[i];
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] >= *zptr) {
-                    /* pass */
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_GREATER:
-         if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] > *zptr) {
-                    /* pass */
-                    *zptr = z[i];
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] > *zptr) {
-                    /* pass */
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_NOTEQUAL:
-         if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] != *zptr) {
-                    /* pass */
-                    *zptr = z[i];
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] != *zptr) {
-                    /* pass */
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_EQUAL:
-         if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] == *zptr) {
-                    /* pass */
-                    *zptr = z[i];
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
-                 if (z[i] == *zptr) {
-                    /* pass */
-                 }
-                 else {
-                    /* fail */
-                    mask[i] = 0;
-                 }
-              }
-           }
-        }
-        break;
-      case GL_ALWAYS:
-        if (ctx->Depth.Mask) {
-           /* Update Z buffer */
-            GLuint i;
-           for (i=0; i<n; i++) {
-              if (mask[i]) {
-                 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
-                 *zptr = z[i];
-              }
-           }
-        }
-        else {
-           /* Don't update Z buffer or mask */
-        }
-        break;
-      case GL_NEVER:
-        /* depth test never passes */
-         memset(mask, 0, n * sizeof(GLubyte));
-        break;
-      default:
-         _mesa_problem(ctx, "Bad depth func in direct_depth_test_pixels");
+   if (rb->Format == MESA_FORMAT_Z_UNORM32) {
+      const GLint rowStride = srb->RowStride;
+      for (i = 0; i < count; i++) {
+         if (mask[i] && x[i] >= 0 && y[i] >= 0 && x[i] < w && y[i] < h) {
+            GLuint *dst = (GLuint *) (map + y[i] * rowStride + x[i] * 4);
+            *dst = zvalues[i];
+         }
+      }
+   }
+   else {
+      gl_pack_uint_z_func packZ = _mesa_get_pack_uint_z_func(rb->Format);
+      const GLint bpp = _mesa_get_format_bytes(rb->Format);
+      const GLint rowStride = srb->RowStride;
+      for (i = 0; i < count; i++) {
+         if (mask[i] && x[i] >= 0 && y[i] >= 0 && x[i] < w && y[i] < h) {
+            void *dst = map + y[i] * rowStride + x[i] * bpp;
+            packZ(zvalues + i, dst);
+         }
+      }
    }
 }
 
 
-
-
-static GLuint
-depth_test_pixels( struct gl_context *ctx, SWspan *span )
+/**
+ * Apply depth (Z) buffer testing to the span.
+ * \return approx number of pixels that passed (only zero is reliable)
+ */
+GLuint
+_swrast_depth_test_span(struct gl_context *ctx, SWspan *span)
 {
    struct gl_framebuffer *fb = ctx->DrawBuffer;
-   struct gl_renderbuffer *rb = fb->_DepthBuffer;
+   struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
+   const GLint bpp = _mesa_get_format_bytes(rb->Format);
+   void *zStart;
    const GLuint count = span->end;
-   const GLint *x = span->array->x;
-   const GLint *y = span->array->y;
-   const GLuint *z = span->array->z;
+   const GLuint *fragZ = span->array->z;
    GLubyte *mask = span->array->mask;
+   void *zBufferVals;
+   GLuint *zBufferTemp = NULL;
+   GLuint passed;
+   GLuint zBits = _mesa_get_format_bits(rb->Format, GL_DEPTH_BITS);
+   GLboolean ztest16 = GL_FALSE;
+
+   if (span->arrayMask & SPAN_XY)
+      zStart = NULL;
+   else
+      zStart = _swrast_pixel_address(rb, span->x, span->y);
+
+   if (rb->Format == MESA_FORMAT_Z_UNORM16 && !(span->arrayMask & SPAN_XY)) {
+      /* directly read/write row of 16-bit Z values */
+      zBufferVals = zStart;
+      ztest16 = GL_TRUE;
+   }
+   else if (rb->Format == MESA_FORMAT_Z_UNORM32 && !(span->arrayMask & SPAN_XY)) {
+      /* directly read/write row of 32-bit Z values */
+      zBufferVals = zStart;
+   }
+   else {
+      if (_mesa_get_format_datatype(rb->Format) != GL_UNSIGNED_NORMALIZED) {
+         _mesa_problem(ctx, "Incorrectly writing swrast's integer depth "
+                       "values to %s depth buffer",
+                       _mesa_get_format_name(rb->Format));
+      }
+
+      /* copy Z buffer values into temp buffer (32-bit Z values) */
+      zBufferTemp = malloc(count * sizeof(GLuint));
+      if (!zBufferTemp)
+         return 0;
 
-   if (rb->GetPointer(ctx, rb, 0, 0)) {
-      /* Directly access values */
-      if (rb->DataType == GL_UNSIGNED_SHORT) {
-         GLushort *zStart = (GLushort *) rb->Data;
-         GLuint stride = rb->Width;
-         direct_depth_test_pixels16(ctx, zStart, stride, count, x, y, z, mask);
+      if (span->arrayMask & SPAN_XY) {
+         get_z32_values(ctx, rb, count,
+                        span->array->x, span->array->y, zBufferTemp);
       }
       else {
-         GLuint *zStart = (GLuint *) rb->Data;
-         GLuint stride = rb->Width;
-         ASSERT(rb->DataType == GL_UNSIGNED_INT);
-         direct_depth_test_pixels32(ctx, zStart, stride, count, x, y, z, mask);
+         _mesa_unpack_uint_z_row(rb->Format, count, zStart, zBufferTemp);
       }
-   }
-   else {
-      /* read depth values from buffer, test, write back */
-      if (rb->DataType == GL_UNSIGNED_SHORT) {
-         GLushort zbuffer[MAX_WIDTH];
-         _swrast_get_values(ctx, rb, count, x, y, zbuffer, sizeof(GLushort));
-         depth_test_span16(ctx, count, zbuffer, z, mask);
-         rb->PutValues(ctx, rb, count, x, y, zbuffer, mask);
+
+      if (zBits == 24) {
+         GLuint i;
+         /* Convert depth buffer values from 32 to 24 bits to match the
+          * fragment Z values generated by rasterization.
+          */
+         for (i = 0; i < count; i++) {
+            zBufferTemp[i] >>= 8;
+         }
+      }
+      else if (zBits == 16) {
+         GLuint i;
+         /* Convert depth buffer values from 32 to 16 bits */
+         for (i = 0; i < count; i++) {
+            zBufferTemp[i] >>= 16;
+         }
       }
       else {
-         GLuint zbuffer[MAX_WIDTH];
-         ASSERT(rb->DataType == GL_UNSIGNED_INT);
-         _swrast_get_values(ctx, rb, count, x, y, zbuffer, sizeof(GLuint));
-         depth_test_span32(ctx, count, zbuffer, z, mask);
-         rb->PutValues(ctx, rb, count, x, y, zbuffer, mask);
+         assert(zBits == 32);
       }
+
+      zBufferVals = zBufferTemp;
    }
 
-   return count; /* not really correct, but OK */
-}
+   /* do the depth test either with 16 or 32-bit values */
+   if (ztest16)
+      passed = depth_test_span16(ctx, count, zBufferVals, fragZ, mask);
+   else
+      passed = depth_test_span32(ctx, count, zBufferVals, fragZ, mask);
+
+   if (zBufferTemp) {
+      /* need to write temp Z values back into the buffer */
+
+      /* Convert depth buffer values back to 32-bit values.  The least
+       * significant bits don't matter since they'll get dropped when
+       * they're packed back into the depth buffer.
+       */
+      if (zBits == 24) {
+         GLuint i;
+         for (i = 0; i < count; i++) {
+            zBufferTemp[i] = (zBufferTemp[i] << 8);
+         }
+      }
+      else if (zBits == 16) {
+         GLuint i;
+         for (i = 0; i < count; i++) {
+            zBufferTemp[i] = zBufferTemp[i] << 16;
+         }
+      }
 
+      if (span->arrayMask & SPAN_XY) {
+         /* random locations */
+         put_z32_values(ctx, rb, count, span->array->x, span->array->y,
+                        zBufferTemp, mask);
+      }
+      else {
+         /* horizontal row */
+         gl_pack_uint_z_func packZ = _mesa_get_pack_uint_z_func(rb->Format);
+         GLubyte *dst = zStart;
+         GLuint i;
+         for (i = 0; i < count; i++) {
+            if (mask[i]) {
+               packZ(&zBufferTemp[i], dst);
+            }
+            dst += bpp;
+         }
+      }
 
-/**
- * Apply depth (Z) buffer testing to the span.
- * \return approx number of pixels that passed (only zero is reliable)
- */
-GLuint
-_swrast_depth_test_span( struct gl_context *ctx, SWspan *span)
-{
-   if (span->arrayMask & SPAN_XY)
-      return depth_test_pixels(ctx, span);
-   else
-      return depth_test_span(ctx, span);
+      free(zBufferTemp);
+   }
+
+   if (passed < count) {
+      span->writeAll = GL_FALSE;
+   }
+   return passed;
 }
 
 
@@ -1171,70 +417,56 @@ GLboolean
 _swrast_depth_bounds_test( struct gl_context *ctx, SWspan *span )
 {
    struct gl_framebuffer *fb = ctx->DrawBuffer;
-   struct gl_renderbuffer *rb = fb->_DepthBuffer;
+   struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
+   GLubyte *zStart;
    GLuint zMin = (GLuint) (ctx->Depth.BoundsMin * fb->_DepthMaxF + 0.5F);
    GLuint zMax = (GLuint) (ctx->Depth.BoundsMax * fb->_DepthMaxF + 0.5F);
    GLubyte *mask = span->array->mask;
    const GLuint count = span->end;
    GLuint i;
    GLboolean anyPass = GL_FALSE;
+   GLuint *zBufferTemp;
+   const GLuint *zBufferVals;
 
-   if (rb->DataType == GL_UNSIGNED_SHORT) {
-      /* get 16-bit values */
-      GLushort zbuffer16[MAX_WIDTH], *zbuffer;
-      if (span->arrayMask & SPAN_XY) {
-         _swrast_get_values(ctx, rb, count, span->array->x, span->array->y,
-                            zbuffer16, sizeof(GLushort));
-         zbuffer = zbuffer16;
-      }
-      else {
-         zbuffer = (GLushort*) rb->GetPointer(ctx, rb, span->x, span->y);
-         if (!zbuffer) {
-            rb->GetRow(ctx, rb, count, span->x, span->y, zbuffer16);
-            zbuffer = zbuffer16;
-         }
-      }
-      assert(zbuffer);
+   zBufferTemp = malloc(count * sizeof(GLuint));
+   if (!zBufferTemp) {
+      /* don't generate a stream of OUT_OF_MEMORY errors here */
+      return GL_FALSE;
+   }
 
-      /* Now do the tests */
-      for (i = 0; i < count; i++) {
-         if (mask[i]) {
-            if (zbuffer[i] < zMin || zbuffer[i] > zMax)
-               mask[i] = GL_FALSE;
-            else
-               anyPass = GL_TRUE;
-         }
-      }
+   if (span->arrayMask & SPAN_XY)
+      zStart = NULL;
+   else
+      zStart = _swrast_pixel_address(rb, span->x, span->y);
+
+   if (rb->Format == MESA_FORMAT_Z_UNORM32 && !(span->arrayMask & SPAN_XY)) {
+      /* directly access 32-bit values in the depth buffer */
+      zBufferVals = (const GLuint *) zStart;
    }
    else {
-      /* get 32-bit values */
-      GLuint zbuffer32[MAX_WIDTH], *zbuffer;
-      ASSERT(rb->DataType == GL_UNSIGNED_INT);
+      /* unpack Z values into a temporary array */
       if (span->arrayMask & SPAN_XY) {
-         _swrast_get_values(ctx, rb, count, span->array->x, span->array->y,
-                            zbuffer32, sizeof(GLuint));
-         zbuffer = zbuffer32;
+         get_z32_values(ctx, rb, count, span->array->x, span->array->y,
+                        zBufferTemp);
       }
       else {
-         zbuffer = (GLuint*) rb->GetPointer(ctx, rb, span->x, span->y);
-         if (!zbuffer) {
-            rb->GetRow(ctx, rb, count, span->x, span->y, zbuffer32);
-            zbuffer = zbuffer32;
-         }
+         _mesa_unpack_uint_z_row(rb->Format, count, zStart, zBufferTemp);
       }
-      assert(zbuffer);
+      zBufferVals = zBufferTemp;
+   }
 
-      /* Now do the tests */
-      for (i = 0; i < count; i++) {
-         if (mask[i]) {
-            if (zbuffer[i] < zMin || zbuffer[i] > zMax)
-               mask[i] = GL_FALSE;
-            else
-               anyPass = GL_TRUE;
-         }
+   /* Now do the tests */
+   for (i = 0; i < count; i++) {
+      if (mask[i]) {
+         if (zBufferVals[i] < zMin || zBufferVals[i] > zMax)
+            mask[i] = GL_FALSE;
+         else
+            anyPass = GL_TRUE;
       }
    }
 
+   free(zBufferTemp);
+
    return anyPass;
 }
 
@@ -1252,19 +484,16 @@ _swrast_depth_bounds_test( struct gl_context *ctx, SWspan *span )
  * bounds.
  */
 void
-_swrast_read_depth_span_float( struct gl_context *ctx, struct gl_renderbuffer *rb,
-                               GLint n, GLint x, GLint y, GLfloat depth[] )
+_swrast_read_depth_span_float(struct gl_context *ctx,
+                              struct gl_renderbuffer *rb,
+                              GLint n, GLint x, GLint y, GLfloat depth[])
 {
-   const GLfloat scale = 1.0F / ctx->DrawBuffer->_DepthMaxF;
-
    if (!rb) {
       /* really only doing this to prevent FP exceptions later */
       memset(depth, 0, n * sizeof(GLfloat));
       return;
    }
 
-   ASSERT(rb->_BaseFormat == GL_DEPTH_COMPONENT);
-
    if (y < 0 || y >= (GLint) rb->Height ||
        x + n <= 0 || x >= (GLint) rb->Width) {
       /* span is completely outside framebuffer */
@@ -1292,25 +521,8 @@ _swrast_read_depth_span_float( struct gl_context *ctx, struct gl_renderbuffer *r
       return;
    }
 
-   if (rb->DataType == GL_UNSIGNED_INT) {
-      GLuint temp[MAX_WIDTH];
-      GLint i;
-      rb->GetRow(ctx, rb, n, x, y, temp);
-      for (i = 0; i < n; i++) {
-         depth[i] = temp[i] * scale;
-      }
-   }
-   else if (rb->DataType == GL_UNSIGNED_SHORT) {
-      GLushort temp[MAX_WIDTH];
-      GLint i;
-      rb->GetRow(ctx, rb, n, x, y, temp);
-      for (i = 0; i < n; i++) {
-         depth[i] = temp[i] * scale;
-      }
-   }
-   else {
-      _mesa_problem(ctx, "Invalid depth renderbuffer data type");
-   }
+   _mesa_unpack_float_z_row(rb->Format, n, _swrast_pixel_address(rb, x, y),
+                            depth);
 }
 
 
@@ -1323,7 +535,6 @@ _swrast_clear_depth_buffer(struct gl_context *ctx)
 {
    struct gl_renderbuffer *rb =
       ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
-   GLuint clearValue;
    GLint x, y, width, height;
    GLubyte *map;
    GLint rowStride, i, j;
@@ -1334,14 +545,6 @@ _swrast_clear_depth_buffer(struct gl_context *ctx)
       return;
    }
 
-   /* compute integer clearing value */
-   if (ctx->Depth.Clear == 1.0) {
-      clearValue = ctx->DrawBuffer->_DepthMax;
-   }
-   else {
-      clearValue = (GLuint) (ctx->Depth.Clear * ctx->DrawBuffer->_DepthMaxF);
-   }
-
    /* compute region to clear */
    x = ctx->DrawBuffer->_Xmin;
    y = ctx->DrawBuffer->_Ymin;
@@ -1349,10 +552,10 @@ _swrast_clear_depth_buffer(struct gl_context *ctx)
    height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
 
    mapMode = GL_MAP_WRITE_BIT;
-   if (rb->Format == MESA_FORMAT_S8_Z24 ||
-       rb->Format == MESA_FORMAT_X8_Z24 ||
-       rb->Format == MESA_FORMAT_Z24_S8 ||
-       rb->Format == MESA_FORMAT_Z24_X8) {
+   if (rb->Format == MESA_FORMAT_Z24_UNORM_S8_UINT ||
+       rb->Format == MESA_FORMAT_Z24_UNORM_X8_UINT ||
+       rb->Format == MESA_FORMAT_S8_UINT_Z24_UNORM ||
+       rb->Format == MESA_FORMAT_X8_UINT_Z24_UNORM) {
       mapMode |= GL_MAP_READ_BIT;
    }
 
@@ -1364,7 +567,7 @@ _swrast_clear_depth_buffer(struct gl_context *ctx)
    }
 
    switch (rb->Format) {
-   case MESA_FORMAT_Z16:
+   case MESA_FORMAT_Z_UNORM16:
       {
          GLfloat clear = (GLfloat) ctx->Depth.Clear;
          GLushort clearVal = 0;
@@ -1384,8 +587,8 @@ _swrast_clear_depth_buffer(struct gl_context *ctx)
          }
       }
       break;
-   case MESA_FORMAT_Z32:
-   case MESA_FORMAT_Z32_FLOAT:
+   case MESA_FORMAT_Z_UNORM32:
+   case MESA_FORMAT_Z_FLOAT32:
       {
          GLfloat clear = (GLfloat) ctx->Depth.Clear;
          GLuint clearVal = 0;
@@ -1399,17 +602,17 @@ _swrast_clear_depth_buffer(struct gl_context *ctx)
          }
       }
       break;
-   case MESA_FORMAT_S8_Z24:
-   case MESA_FORMAT_X8_Z24:
-   case MESA_FORMAT_Z24_S8:
-   case MESA_FORMAT_Z24_X8:
+   case MESA_FORMAT_Z24_UNORM_S8_UINT:
+   case MESA_FORMAT_Z24_UNORM_X8_UINT:
+   case MESA_FORMAT_S8_UINT_Z24_UNORM:
+   case MESA_FORMAT_X8_UINT_Z24_UNORM:
       {
          GLfloat clear = (GLfloat) ctx->Depth.Clear;
          GLuint clearVal = 0;
          GLuint mask;
 
-         if (rb->Format == MESA_FORMAT_S8_Z24 ||
-             rb->Format == MESA_FORMAT_X8_Z24)
+         if (rb->Format == MESA_FORMAT_Z24_UNORM_S8_UINT ||
+             rb->Format == MESA_FORMAT_Z24_UNORM_X8_UINT)
             mask = 0xff000000;
          else
             mask = 0xff;
@@ -1425,7 +628,7 @@ _swrast_clear_depth_buffer(struct gl_context *ctx)
 
       }
       break;
-   case MESA_FORMAT_Z32_FLOAT_X24S8:
+   case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
       /* XXX untested */
       {
          GLfloat clearVal = (GLfloat) ctx->Depth.Clear;
@@ -1489,15 +692,15 @@ _swrast_clear_depth_stencil_buffer(struct gl_context *ctx)
    }
 
    switch (rb->Format) {
-   case MESA_FORMAT_S8_Z24:
-   case MESA_FORMAT_Z24_S8:
+   case MESA_FORMAT_Z24_UNORM_S8_UINT:
+   case MESA_FORMAT_S8_UINT_Z24_UNORM:
       {
          GLfloat zClear = (GLfloat) ctx->Depth.Clear;
          GLuint clear = 0, mask;
 
          _mesa_pack_float_z_row(rb->Format, 1, &zClear, &clear);
 
-         if (rb->Format == MESA_FORMAT_S8_Z24) {
+         if (rb->Format == MESA_FORMAT_Z24_UNORM_S8_UINT) {
             mask = ((~writeMask) & 0xff) << 24;
             clear |= (ctx->Stencil.Clear & writeMask & 0xff) << 24;
          }
@@ -1522,7 +725,7 @@ _swrast_clear_depth_stencil_buffer(struct gl_context *ctx)
          }
       }
       break;
-   case MESA_FORMAT_Z32_FLOAT_X24S8:
+   case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
       /* XXX untested */
       {
          const GLfloat zClear = (GLfloat) ctx->Depth.Clear;