rearranged order of some functions
[mesa.git] / src / mesa / main / depth.c
index 746b8188ba86932207a81a551ae426848f53900f..2f9697616e037e7918ae0de79e1b8ebfbbee6da0 100644 (file)
@@ -1,10 +1,10 @@
-/* $Id: depth.c,v 1.10 1999/11/24 18:48:31 brianp Exp $ */
+/* $Id: depth.c,v 1.15 2000/03/19 01:10:11 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
  * Version:  3.3
  * 
- * Copyright (C) 1999  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2000  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"),
@@ -24,6 +24,7 @@
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
+
 #ifdef PC_HEADER
 #include "all.h"
 #else
@@ -32,6 +33,7 @@
 #include "enums.h"
 #include "depth.h"
 #include "mem.h"
+#include "pb.h"
 #include "types.h"
 #endif
 
@@ -123,33 +125,48 @@ _mesa_DepthMask( GLboolean flag )
 
 
 /**********************************************************************/
-/*****                   Depth Testing Functions                  *****/
+/*****                           Misc                             *****/
 /**********************************************************************/
 
-
 /*
- * Depth test horizontal spans of fragments.  These functions are called
- * via ctx->Driver.depth_test_span only.
- *
- * Input:  n - number of pixels in the span
- *         x, y - location of leftmost pixel in span in window coords
- *         z - array [n] of integer depth values
- * In/Out:  mask - array [n] of flags (1=draw pixel, 0=don't draw) 
- * Return:  number of pixels which passed depth test
+ * Return address of depth buffer value for given window coord.
  */
+GLvoid *
+_mesa_zbuffer_address(GLcontext *ctx, GLint x, GLint y)
+{
+   if (ctx->Visual->DepthBits <= 16)
+      return (GLushort *) ctx->DrawBuffer->DepthBuffer + ctx->DrawBuffer->Width * y + x;
+   else
+      return (GLuint *) ctx->DrawBuffer->DepthBuffer + ctx->DrawBuffer->Width * y + x;
+}
+
+
+#define Z_ADDRESS16( CTX, X, Y )                               \
+            ( ((GLushort *) (CTX)->DrawBuffer->DepthBuffer)    \
+              + (CTX)->DrawBuffer->Width * (Y) + (X) )
+
+#define Z_ADDRESS32( CTX, X, Y )                               \
+            ( ((GLuint *) (CTX)->DrawBuffer->DepthBuffer)      \
+              + (CTX)->DrawBuffer->Width * (Y) + (X) )
+
+
+
+/**********************************************************************/
+/*****                   Depth Testing Functions                  *****/
+/**********************************************************************/
 
 
 /*
- * glDepthFunc( any ) and glDepthMask( GL_TRUE or GL_FALSE ).
+ * Do depth test for an array of fragments.  This is used both for
+ * software and hardware Z buffers.
+ * Input:  zbuffer - array of z values in the zbuffer
+ *         z - array of fragment z values
+ * Return:  number of fragments which pass the test.
  */
-GLuint gl_depth_test_span_generic( GLcontext* ctx,
-                                   GLuint n, GLint x, GLint y,
-                                   const GLdepth z[],
-                                   GLubyte mask[] )
+static GLuint
+depth_test_span16( GLcontext *ctx, GLuint n, GLint x, GLint y,
+                   GLushort zbuffer[], const GLdepth z[], GLubyte mask[] )
 {
-   GLdepth *zptr = Z_ADDRESS( ctx, x, y );
-   GLubyte *m = mask;
-   GLuint i;
    GLuint passed = 0;
 
    /* switch cases ordered from most frequent to less frequent */
@@ -157,30 +174,32 @@ GLuint gl_depth_test_span_generic( GLcontext* ctx,
       case GL_LESS:
          if (ctx->Depth.Mask) {
            /* Update Z buffer */
-           for (i=0; i<n; i++,zptr++,m++) {
-              if (*m) {
-                 if (z[i] < *zptr) {
+            GLuint i;
+           for (i=0; i<n; i++) {
+              if (mask[i]) {
+                 if (z[i] < zbuffer[i]) {
                     /* pass */
-                    *zptr = z[i];
+                    zbuffer[i] = z[i];
                     passed++;
                  }
                  else {
                     /* fail */
-                    *m = 0;
+                    mask[i] = 0;
                  }
               }
            }
         }
         else {
            /* Don't update Z buffer */
-           for (i=0; i<n; i++,zptr++,m++) {
-              if (*m) {
-                 if (z[i] < *zptr) {
+            GLuint i;
+           for (i=0; i<n; i++) {
+              if (mask[i]) {
+                 if (z[i] < zbuffer[i]) {
                     /* pass */
                     passed++;
                  }
                  else {
-                    *m = 0;
+                    mask[i] = 0;
                  }
               }
            }
@@ -189,28 +208,30 @@ GLuint gl_depth_test_span_generic( GLcontext* ctx,
       case GL_LEQUAL:
         if (ctx->Depth.Mask) {
            /* Update Z buffer */
-           for (i=0;i<n;i++,zptr++,m++) {
-              if (*m) {
-                 if (z[i] <= *zptr) {
-                    *zptr = z[i];
+            GLuint i;
+           for (i=0;i<n;i++) {
+              if (mask[i]) {
+                 if (z[i] <= zbuffer[i]) {
+                    zbuffer[i] = z[i];
                     passed++;
                  }
                  else {
-                    *m = 0;
+                    mask[i] = 0;
                  }
               }
            }
         }
         else {
            /* Don't update Z buffer */
-           for (i=0;i<n;i++,zptr++,m++) {
-              if (*m) {
-                 if (z[i] <= *zptr) {
+            GLuint i;
+           for (i=0;i<n;i++) {
+              if (mask[i]) {
+                 if (z[i] <= zbuffer[i]) {
                     /* pass */
                     passed++;
                  }
                  else {
-                    *m = 0;
+                    mask[i] = 0;
                  }
               }
            }
@@ -219,28 +240,30 @@ GLuint gl_depth_test_span_generic( GLcontext* ctx,
       case GL_GEQUAL:
         if (ctx->Depth.Mask) {
            /* Update Z buffer */
-           for (i=0;i<n;i++,zptr++,m++) {
-              if (*m) {
-                 if (z[i] >= *zptr) {
-                    *zptr = z[i];
+            GLuint i;
+           for (i=0;i<n;i++) {
+              if (mask[i]) {
+                 if (z[i] >= zbuffer[i]) {
+                    zbuffer[i] = z[i];
                     passed++;
                  }
                  else {
-                    *m = 0;
+                    mask[i] = 0;
                  }
               }
            }
         }
         else {
            /* Don't update Z buffer */
-           for (i=0;i<n;i++,zptr++,m++) {
-              if (*m) {
-                 if (z[i] >= *zptr) {
+            GLuint i;
+           for (i=0;i<n;i++) {
+              if (mask[i]) {
+                 if (z[i] >= zbuffer[i]) {
                     /* pass */
                     passed++;
                  }
                  else {
-                    *m = 0;
+                    mask[i] = 0;
                  }
               }
            }
@@ -249,28 +272,30 @@ GLuint gl_depth_test_span_generic( GLcontext* ctx,
       case GL_GREATER:
         if (ctx->Depth.Mask) {
            /* Update Z buffer */
-           for (i=0;i<n;i++,zptr++,m++) {
-              if (*m) {
-                 if (z[i] > *zptr) {
-                    *zptr = z[i];
+            GLuint i;
+           for (i=0;i<n;i++) {
+              if (mask[i]) {
+                 if (z[i] > zbuffer[i]) {
+                    zbuffer[i] = z[i];
                     passed++;
                  }
                  else {
-                    *m = 0;
+                    mask[i] = 0;
                  }
               }
            }
         }
         else {
            /* Don't update Z buffer */
-           for (i=0;i<n;i++,zptr++,m++) {
-              if (*m) {
-                 if (z[i] > *zptr) {
+            GLuint i;
+           for (i=0;i<n;i++) {
+              if (mask[i]) {
+                 if (z[i] > zbuffer[i]) {
                     /* pass */
                     passed++;
                  }
                  else {
-                    *m = 0;
+                    mask[i] = 0;
                  }
               }
            }
@@ -279,28 +304,30 @@ GLuint gl_depth_test_span_generic( GLcontext* ctx,
       case GL_NOTEQUAL:
         if (ctx->Depth.Mask) {
            /* Update Z buffer */
-           for (i=0;i<n;i++,zptr++,m++) {
-              if (*m) {
-                 if (z[i] != *zptr) {
-                    *zptr = z[i];
+            GLuint i;
+           for (i=0;i<n;i++) {
+              if (mask[i]) {
+                 if (z[i] != zbuffer[i]) {
+                    zbuffer[i] = z[i];
                     passed++;
                  }
                  else {
-                    *m = 0;
+                    mask[i] = 0;
                  }
               }
            }
         }
         else {
            /* Don't update Z buffer */
-           for (i=0;i<n;i++,zptr++,m++) {
-              if (*m) {
-                 if (z[i] != *zptr) {
+            GLuint i;
+           for (i=0;i<n;i++) {
+              if (mask[i]) {
+                 if (z[i] != zbuffer[i]) {
                     /* pass */
                     passed++;
                  }
                  else {
-                    *m = 0;
+                    mask[i] = 0;
                  }
               }
            }
@@ -309,28 +336,30 @@ GLuint gl_depth_test_span_generic( GLcontext* ctx,
       case GL_EQUAL:
         if (ctx->Depth.Mask) {
            /* Update Z buffer */
-           for (i=0;i<n;i++,zptr++,m++) {
-              if (*m) {
-                 if (z[i] == *zptr) {
-                    *zptr = z[i];
+            GLuint i;
+           for (i=0;i<n;i++) {
+              if (mask[i]) {
+                 if (z[i] == zbuffer[i]) {
+                    zbuffer[i] = z[i];
                     passed++;
                  }
                  else {
-                    *m =0;
+                    mask[i] = 0;
                  }
               }
            }
         }
         else {
            /* Don't update Z buffer */
-           for (i=0;i<n;i++,zptr++,m++) {
-              if (*m) {
-                 if (z[i] == *zptr) {
+            GLuint i;
+           for (i=0;i<n;i++) {
+              if (mask[i]) {
+                 if (z[i] == zbuffer[i]) {
                     /* pass */
                     passed++;
                  }
                  else {
-                    *m =0;
+                    mask[i] = 0;
                  }
               }
            }
@@ -339,9 +368,10 @@ GLuint gl_depth_test_span_generic( GLcontext* ctx,
       case GL_ALWAYS:
         if (ctx->Depth.Mask) {
            /* Update Z buffer */
-           for (i=0;i<n;i++,zptr++,m++) {
-              if (*m) {
-                 *zptr = z[i];
+            GLuint i;
+           for (i=0;i<n;i++) {
+              if (mask[i]) {
+                 zbuffer[i] = z[i];
                  passed++;
               }
            }
@@ -352,110 +382,34 @@ GLuint gl_depth_test_span_generic( GLcontext* ctx,
         }
         break;
       case GL_NEVER:
-        for (i=0;i<n;i++) {
-           mask[i] = 0;
-        }
+         MEMSET(mask, 0, n * sizeof(GLubyte));
         break;
       default:
-         gl_problem(ctx, "Bad depth func in gl_depth_test_span_generic");
-   } /*switch*/
-
-   return passed;
-}
-
-
-
-/*
- * glDepthFunc(GL_LESS) and glDepthMask(GL_TRUE).
- */
-GLuint gl_depth_test_span_less( GLcontext* ctx,
-                                GLuint n, GLint x, GLint y, const GLdepth z[],
-                                GLubyte mask[] )
-{
-   GLdepth *zptr = Z_ADDRESS( ctx, x, y );
-   GLuint i;
-   GLuint passed = 0;
-
-   for (i=0; i<n; i++) {
-      if (mask[i]) {
-         if (z[i] < zptr[i]) {
-            /* pass */
-            zptr[i] = z[i];
-            passed++;
-         }
-         else {
-            /* fail */
-            mask[i] = 0;
-         }
-      }
+         gl_problem(ctx, "Bad depth func in depth_test_span16");
    }
-   return passed;
-}
-
-
-/*
- * glDepthFunc(GL_GREATER) and glDepthMask(GL_TRUE).
- */
-GLuint gl_depth_test_span_greater( GLcontext* ctx,
-                                   GLuint n, GLint x, GLint y,
-                                   const GLdepth z[],
-                                   GLubyte mask[] )
-{
-   GLdepth *zptr = Z_ADDRESS( ctx, x, y );
-   GLuint i;
-   GLuint passed = 0;
 
-   for (i=0; i<n; i++) {
-      if (mask[i]) {
-         if (z[i] > zptr[i]) {
-            /* pass */
-            zptr[i] = z[i];
-            passed++;
-         }
-         else {
-            /* fail */
-            mask[i] = 0;
-         }
-      }
-   }
    return passed;
 }
 
 
-
-/*
- * Depth test an array of randomly positioned fragments.
- */
-
-
-#define ZADDR_SETUP   GLdepth *depthbuffer = ctx->Buffer->Depth; \
-                      GLint width = ctx->Buffer->Width;
-
-#define ZADDR( X, Y )   (depthbuffer + (Y) * width + (X) )
-
-
-
-/*
- * glDepthFunc( any ) and glDepthMask( GL_TRUE or GL_FALSE ).
- */
-void gl_depth_test_pixels_generic( GLcontext* ctx,
-                                   GLuint n, const GLint x[], const GLint y[],
-                                   const GLdepth z[], GLubyte mask[] )
+static GLuint
+depth_test_span32( GLcontext *ctx, GLuint n, GLint x, GLint y,
+                   GLuint zbuffer[], const GLdepth z[], GLubyte mask[] )
 {
-   register GLdepth *zptr;
-   register GLuint i;
+   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]) {
-                 zptr = Z_ADDRESS(ctx,x[i],y[i]);
-                 if (z[i] < *zptr) {
+                 if (z[i] < zbuffer[i]) {
                     /* pass */
-                    *zptr = z[i];
+                    zbuffer[i] = z[i];
+                    passed++;
                  }
                  else {
                     /* fail */
@@ -466,14 +420,14 @@ void gl_depth_test_pixels_generic( GLcontext* ctx,
         }
         else {
            /* Don't update Z buffer */
+            GLuint i;
            for (i=0; i<n; i++) {
               if (mask[i]) {
-                 zptr = Z_ADDRESS(ctx,x[i],y[i]);
-                 if (z[i] < *zptr) {
+                 if (z[i] < zbuffer[i]) {
                     /* pass */
+                    passed++;
                  }
                  else {
-                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -481,17 +435,16 @@ void gl_depth_test_pixels_generic( GLcontext* ctx,
         }
         break;
       case GL_LEQUAL:
-         if (ctx->Depth.Mask) {
+        if (ctx->Depth.Mask) {
            /* Update Z buffer */
-           for (i=0; i<n; i++) {
+            GLuint i;
+           for (i=0;i<n;i++) {
               if (mask[i]) {
-                 zptr = Z_ADDRESS(ctx,x[i],y[i]);
-                 if (z[i] <= *zptr) {
-                    /* pass */
-                    *zptr = z[i];
+                 if (z[i] <= zbuffer[i]) {
+                    zbuffer[i] = z[i];
+                    passed++;
                  }
                  else {
-                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -499,14 +452,14 @@ void gl_depth_test_pixels_generic( GLcontext* ctx,
         }
         else {
            /* Don't update Z buffer */
-           for (i=0; i<n; i++) {
+            GLuint i;
+           for (i=0;i<n;i++) {
               if (mask[i]) {
-                 zptr = Z_ADDRESS(ctx,x[i],y[i]);
-                 if (z[i] <= *zptr) {
+                 if (z[i] <= zbuffer[i]) {
                     /* pass */
+                    passed++;
                  }
                  else {
-                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -514,17 +467,16 @@ void gl_depth_test_pixels_generic( GLcontext* ctx,
         }
         break;
       case GL_GEQUAL:
-         if (ctx->Depth.Mask) {
+        if (ctx->Depth.Mask) {
            /* Update Z buffer */
-           for (i=0; i<n; i++) {
+            GLuint i;
+           for (i=0;i<n;i++) {
               if (mask[i]) {
-                 zptr = Z_ADDRESS(ctx,x[i],y[i]);
-                 if (z[i] >= *zptr) {
-                    /* pass */
-                    *zptr = z[i];
+                 if (z[i] >= zbuffer[i]) {
+                    zbuffer[i] = z[i];
+                    passed++;
                  }
                  else {
-                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -532,14 +484,14 @@ void gl_depth_test_pixels_generic( GLcontext* ctx,
         }
         else {
            /* Don't update Z buffer */
-           for (i=0; i<n; i++) {
+            GLuint i;
+           for (i=0;i<n;i++) {
               if (mask[i]) {
-                 zptr = Z_ADDRESS(ctx,x[i],y[i]);
-                 if (z[i] >= *zptr) {
+                 if (z[i] >= zbuffer[i]) {
                     /* pass */
+                    passed++;
                  }
                  else {
-                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -547,17 +499,16 @@ void gl_depth_test_pixels_generic( GLcontext* ctx,
         }
         break;
       case GL_GREATER:
-         if (ctx->Depth.Mask) {
+        if (ctx->Depth.Mask) {
            /* Update Z buffer */
-           for (i=0; i<n; i++) {
+            GLuint i;
+           for (i=0;i<n;i++) {
               if (mask[i]) {
-                 zptr = Z_ADDRESS(ctx,x[i],y[i]);
-                 if (z[i] > *zptr) {
-                    /* pass */
-                    *zptr = z[i];
+                 if (z[i] > zbuffer[i]) {
+                    zbuffer[i] = z[i];
+                    passed++;
                  }
                  else {
-                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -565,14 +516,14 @@ void gl_depth_test_pixels_generic( GLcontext* ctx,
         }
         else {
            /* Don't update Z buffer */
-           for (i=0; i<n; i++) {
+            GLuint i;
+           for (i=0;i<n;i++) {
               if (mask[i]) {
-                 zptr = Z_ADDRESS(ctx,x[i],y[i]);
-                 if (z[i] > *zptr) {
+                 if (z[i] > zbuffer[i]) {
                     /* pass */
+                    passed++;
                  }
                  else {
-                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -580,17 +531,16 @@ void gl_depth_test_pixels_generic( GLcontext* ctx,
         }
         break;
       case GL_NOTEQUAL:
-         if (ctx->Depth.Mask) {
+        if (ctx->Depth.Mask) {
            /* Update Z buffer */
-           for (i=0; i<n; i++) {
+            GLuint i;
+           for (i=0;i<n;i++) {
               if (mask[i]) {
-                 zptr = Z_ADDRESS(ctx,x[i],y[i]);
-                 if (z[i] != *zptr) {
-                    /* pass */
-                    *zptr = z[i];
+                 if (z[i] != zbuffer[i]) {
+                    zbuffer[i] = z[i];
+                    passed++;
                  }
                  else {
-                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -598,14 +548,14 @@ void gl_depth_test_pixels_generic( GLcontext* ctx,
         }
         else {
            /* Don't update Z buffer */
-           for (i=0; i<n; i++) {
+            GLuint i;
+           for (i=0;i<n;i++) {
               if (mask[i]) {
-                 zptr = Z_ADDRESS(ctx,x[i],y[i]);
-                 if (z[i] != *zptr) {
+                 if (z[i] != zbuffer[i]) {
                     /* pass */
+                    passed++;
                  }
                  else {
-                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -613,17 +563,16 @@ void gl_depth_test_pixels_generic( GLcontext* ctx,
         }
         break;
       case GL_EQUAL:
-         if (ctx->Depth.Mask) {
+        if (ctx->Depth.Mask) {
            /* Update Z buffer */
-           for (i=0; i<n; i++) {
+            GLuint i;
+           for (i=0;i<n;i++) {
               if (mask[i]) {
-                 zptr = Z_ADDRESS(ctx,x[i],y[i]);
-                 if (z[i] == *zptr) {
-                    /* pass */
-                    *zptr = z[i];
+                 if (z[i] == zbuffer[i]) {
+                    zbuffer[i] = z[i];
+                    passed++;
                  }
                  else {
-                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -631,14 +580,14 @@ void gl_depth_test_pixels_generic( GLcontext* ctx,
         }
         else {
            /* Don't update Z buffer */
-           for (i=0; i<n; i++) {
+            GLuint i;
+           for (i=0;i<n;i++) {
               if (mask[i]) {
-                 zptr = Z_ADDRESS(ctx,x[i],y[i]);
-                 if (z[i] == *zptr) {
+                 if (z[i] == zbuffer[i]) {
                     /* pass */
+                    passed++;
                  }
                  else {
-                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -648,237 +597,1020 @@ void gl_depth_test_pixels_generic( GLcontext* ctx,
       case GL_ALWAYS:
         if (ctx->Depth.Mask) {
            /* Update Z buffer */
-           for (i=0; i<n; i++) {
+            GLuint i;
+           for (i=0;i<n;i++) {
               if (mask[i]) {
-                 zptr = Z_ADDRESS(ctx,x[i],y[i]);
-                 *zptr = z[i];
+                 zbuffer[i] = z[i];
+                 passed++;
               }
            }
         }
         else {
            /* Don't update Z buffer or mask */
+           passed = n;
         }
         break;
       case GL_NEVER:
-        /* depth test never passes */
-        for (i=0;i<n;i++) {
-           mask[i] = 0;
-        }
+         MEMSET(mask, 0, n * sizeof(GLubyte));
         break;
       default:
-         gl_problem(ctx, "Bad depth func in gl_depth_test_pixels_generic");
-   } /*switch*/
-}
-
-
-
-/*
- * glDepthFunc( GL_LESS ) and glDepthMask( GL_TRUE ).
- */
-void gl_depth_test_pixels_less( GLcontext* ctx,
-                                GLuint n, const GLint x[], const GLint y[],
-                                const GLdepth z[], GLubyte mask[] )
-{
-   GLdepth *zptr;
-   GLuint i;
-
-   for (i=0; i<n; i++) {
-      if (mask[i]) {
-         zptr = Z_ADDRESS(ctx,x[i],y[i]);
-         if (z[i] < *zptr) {
-            /* pass */
-            *zptr = z[i];
-         }
-         else {
-            /* fail */
-            mask[i] = 0;
-         }
-      }
+         gl_problem(ctx, "Bad depth func in depth_test_span32");
    }
-}
-
 
-/*
- * glDepthFunc( GL_GREATER ) and glDepthMask( GL_TRUE ).
- */
-void gl_depth_test_pixels_greater( GLcontext* ctx,
-                                   GLuint n, const GLint x[], const GLint y[],
-                                   const GLdepth z[], GLubyte mask[] )
-{
-   GLdepth *zptr;
-   GLuint i;
-
-   for (i=0; i<n; i++) {
-      if (mask[i]) {
-         zptr = Z_ADDRESS(ctx,x[i],y[i]);
-         if (z[i] > *zptr) {
-            /* pass */
-            *zptr = z[i];
-         }
-         else {
-            /* fail */
-            mask[i] = 0;
-         }
-      }
-   }
+   return passed;
 }
 
 
 
-
-/**********************************************************************/
-/*****                      Read Depth Buffer                     *****/
-/**********************************************************************/
-
-
 /*
- * Return a span of depth values from the depth buffer as floats in [0,1].
- * This function is only called through Driver.read_depth_span_float()
- * Input:  n - how many pixels
- *         x,y - location of first pixel
- * Output:  depth - the array of depth values
+ * Apply depth test to span of fragments.  Hardware or software z buffer.
  */
-void gl_read_depth_span_float( GLcontext* ctx,
-                               GLuint n, GLint x, GLint y, GLfloat depth[] )
+GLuint
+_mesa_depth_test_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
+                       const GLdepth z[], GLubyte mask[] )
 {
-   GLdepth *zptr;
-   GLfloat scale;
-   GLuint i;
-
-   scale = 1.0F / DEPTH_SCALE;
-
-   if (ctx->DrawBuffer->Depth) {
-      zptr = Z_ADDRESS( ctx, x, y );
-      for (i=0;i<n;i++) {
-        depth[i] = (GLfloat) zptr[i] * scale;
-      }
+   if (ctx->Driver.ReadDepthSpan) {
+      /* hardware-based depth buffer */
+      GLdepth zbuffer[MAX_WIDTH];
+      GLuint passed;
+      (*ctx->Driver.ReadDepthSpan)(ctx, n, x, y, zbuffer);
+      passed = depth_test_span32(ctx, n, x, y, zbuffer, z, mask);
+      assert(ctx->Driver.WriteDepthSpan);
+      (*ctx->Driver.WriteDepthSpan)(ctx, n, x, y, zbuffer, mask);
+      return passed;
    }
    else {
-      for (i=0;i<n;i++) {
-        depth[i] = 0.0F;
+      /* software depth buffer */
+      if (ctx->Visual->DepthBits <= 16) {
+         GLushort *zptr = (GLushort *) Z_ADDRESS16(ctx, x, y);
+         GLuint passed = depth_test_span16(ctx, n, x, y, zptr, z, mask);
+         return passed;
       }
-   }
-}
-
-
-/*
- * Return a span of depth values from the depth buffer as integers in
- * [0,MAX_DEPTH].
- * This function is only called through Driver.read_depth_span_int()
- * Input:  n - how many pixels
- *         x,y - location of first pixel
- * Output:  depth - the array of depth values
- */
-void gl_read_depth_span_int( GLcontext* ctx,
-                             GLuint n, GLint x, GLint y, GLdepth depth[] )
-{
-   if (ctx->DrawBuffer->Depth) {
-      GLdepth *zptr = Z_ADDRESS( ctx, x, y );
-      MEMCPY( depth, zptr, n * sizeof(GLdepth) );
-   }
-   else {
-      GLuint i;
-      for (i=0;i<n;i++) {
-        depth[i] = 0;
+      else {
+         GLuint *zptr = (GLuint *) Z_ADDRESS32(ctx, x, y);
+         GLuint passed = depth_test_span32(ctx, n, x, y, zptr, z, mask);
+         return passed;
       }
    }
 }
 
 
 
-/**********************************************************************/
-/*****                Allocate and Clear Depth Buffer             *****/
-/**********************************************************************/
-
-
-
-/*
- * Allocate a new depth buffer.  If there's already a depth buffer allocated
- * it will be free()'d.  The new depth buffer will be uniniitalized.
- * This function is only called through Driver.alloc_depth_buffer.
- */
-void gl_alloc_depth_buffer( GLcontext* ctx )
-{
-   /* deallocate current depth buffer if present */
-   if (ctx->DrawBuffer->Depth) {
-      FREE(ctx->DrawBuffer->Depth);
-      ctx->DrawBuffer->Depth = NULL;
-   }
-
-   /* allocate new depth buffer, but don't initialize it */
-   ctx->DrawBuffer->Depth = (GLdepth *) MALLOC( ctx->DrawBuffer->Width
-                                            * ctx->DrawBuffer->Height
-                                            * sizeof(GLdepth) );
-   if (!ctx->DrawBuffer->Depth) {
-      /* out of memory */
-      ctx->Depth.Test = GL_FALSE;
-      ctx->NewState |= NEW_RASTER_OPS;
-      gl_error( ctx, GL_OUT_OF_MEMORY, "Couldn't allocate depth buffer" );
-   }
-}
-
-
-
 
 /*
- * Clear the depth buffer.  If the depth buffer doesn't exist yet we'll
- * allocate it now.
- * This function is only called through Driver.clear_depth_buffer.
+ * Do depth testing for an array of fragments using software Z buffer.
  */
-void gl_clear_depth_buffer( GLcontext* ctx )
+static void
+software_depth_test_pixels16( GLcontext *ctx, GLuint n,
+                              const GLint x[], const GLint y[],
+                              const GLdepth z[], GLubyte mask[] )
 {
-   GLdepth clear_value = (GLdepth) (ctx->Depth.Clear * DEPTH_SCALE);
-   
-   if (ctx->Visual->DepthBits==0 || !ctx->DrawBuffer->Depth || !ctx->Depth.Mask) {
-      /* no depth buffer, or writing to it is disabled */
-      return;
-   }
-
-   /* The loops in this function have been written so the IRIX 5.3
-    * C compiler can unroll them.  Hopefully other compilers can too!
-    */
-
-   if (ctx->Scissor.Enabled) {
-      /* only clear scissor region */
-      GLint y;
-      for (y=ctx->DrawBuffer->Ymin; y<=ctx->DrawBuffer->Ymax; y++) {
-         GLdepth *d = Z_ADDRESS( ctx, ctx->DrawBuffer->Xmin, y );
-         GLint n = ctx->DrawBuffer->Xmax - ctx->DrawBuffer->Xmin + 1;
-         do {
-            *d++ = clear_value;
-            n--;
-         } while (n);
+   /* 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_ADDRESS16(ctx,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_ADDRESS16(ctx,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_ADDRESS16(ctx,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_ADDRESS16(ctx,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_ADDRESS16(ctx,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_ADDRESS16(ctx,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_ADDRESS16(ctx,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_ADDRESS16(ctx,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_ADDRESS16(ctx,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_ADDRESS16(ctx,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_ADDRESS16(ctx,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_ADDRESS16(ctx,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_ADDRESS16(ctx,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:
+         gl_problem(ctx, "Bad depth func in software_depth_test_pixels");
+   }
+}
+
+
+
+/*
+ * Do depth testing for an array of fragments using software Z buffer.
+ */
+static void
+software_depth_test_pixels32( GLcontext *ctx, GLuint n,
+                              const GLint x[], const GLint y[],
+                              const GLdepth 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_ADDRESS32(ctx,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_ADDRESS32(ctx,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_ADDRESS32(ctx,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_ADDRESS32(ctx,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_ADDRESS32(ctx,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_ADDRESS32(ctx,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_ADDRESS32(ctx,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_ADDRESS32(ctx,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_ADDRESS32(ctx,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_ADDRESS32(ctx,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_ADDRESS32(ctx,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_ADDRESS32(ctx,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_ADDRESS32(ctx,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:
+         gl_problem(ctx, "Bad depth func in software_depth_test_pixels");
+   }
+}
+
+
+
+/*
+ * Do depth testing for an array of pixels using hardware Z buffer.
+ * Input/output:  zbuffer - array of depth values from Z buffer
+ * Input:  z - array of fragment z values.
+ */
+static void
+hardware_depth_test_pixels( GLcontext *ctx, GLuint n, GLdepth zbuffer[],
+                            const GLdepth 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]) {
+                 if (z[i] < zbuffer[i]) {
+                    /* pass */
+                    zbuffer[i] = z[i];
+                 }
+                 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 */
+                 }
+                 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]) {
+                 if (z[i] <= zbuffer[i]) {
+                    /* pass */
+                    zbuffer[i] = z[i];
+                 }
+                 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 */
+                 }
+                 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]) {
+                 if (z[i] >= zbuffer[i]) {
+                    /* pass */
+                    zbuffer[i] = z[i];
+                 }
+                 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 */
+                 }
+                 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]) {
+                 if (z[i] > zbuffer[i]) {
+                    /* pass */
+                    zbuffer[i] = z[i];
+                 }
+                 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 */
+                 }
+                 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]) {
+                 if (z[i] != zbuffer[i]) {
+                    /* pass */
+                    zbuffer[i] = z[i];
+                 }
+                 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 */
+                 }
+                 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]) {
+                 if (z[i] == zbuffer[i]) {
+                    /* pass */
+                    zbuffer[i] = z[i];
+                 }
+                 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 */
+                 }
+                 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]) {
+                 zbuffer[i] = 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:
+         gl_problem(ctx, "Bad depth func in hardware_depth_test_pixels");
+   }
+}
+
+
+
+void
+_mesa_depth_test_pixels( GLcontext *ctx,
+                         GLuint n, const GLint x[], const GLint y[],
+                         const GLdepth z[], GLubyte mask[] )
+{
+   if (ctx->Driver.ReadDepthPixels) {
+      /* read depth values from hardware Z buffer */
+      GLdepth zbuffer[PB_SIZE];
+      (*ctx->Driver.ReadDepthPixels)(ctx, n, x, y, zbuffer);
+
+      hardware_depth_test_pixels( ctx, n, zbuffer, z, mask );
+
+      /* update hardware Z buffer with new values */
+      assert(ctx->Driver.WriteDepthPixels);
+      (*ctx->Driver.WriteDepthPixels)(ctx, n, x, y, zbuffer, mask );
+   }
+   else {
+      /* software depth testing */
+      if (ctx->Visual->DepthBits <= 16)
+         software_depth_test_pixels16(ctx, n, x, y, z, mask);
+      else
+         software_depth_test_pixels32(ctx, n, x, y, z, mask);
+   }
+}
+
+
+
+
+
+/**********************************************************************/
+/*****                      Read Depth Buffer                     *****/
+/**********************************************************************/
+
+
+/*
+ * Return a span of depth values from the depth buffer as floats in [0,1].
+ * This is used for both hardware and software depth buffers.
+ * Input:  n - how many pixels
+ *         x,y - location of first pixel
+ * Output:  depth - the array of depth values
+ */
+void
+_mesa_read_depth_span_float( GLcontext* ctx,
+                             GLuint n, GLint x, GLint y, GLfloat depth[] )
+{
+   const GLfloat scale = 1.0F / ctx->Visual->DepthMaxF;
+
+   if (ctx->DrawBuffer->DepthBuffer) {
+      /* read from software depth buffer */
+      if (ctx->Visual->DepthBits <= 16) {
+         const GLushort *zptr = Z_ADDRESS16( ctx, x, y );
+         GLuint i;
+         for (i = 0; i < n; i++) {
+            depth[i] = (GLfloat) zptr[i] * scale;
+         }
+      }
+      else {
+         const GLuint *zptr = Z_ADDRESS32( ctx, x, y );
+         GLuint i;
+         for (i = 0; i < n; i++) {
+            depth[i] = (GLfloat) zptr[i] * scale;
+         }
+      }
+   }
+   else if (ctx->Driver.ReadDepthSpan) {
+      /* read from hardware depth buffer */
+      GLdepth d[MAX_WIDTH];
+      GLuint i;
+      assert(n <= MAX_WIDTH);
+      (*ctx->Driver.ReadDepthSpan)( ctx, n, x, y, d );
+      for (i = 0; i < n; i++) {
+        depth[i] = d[i] * scale;
+      }
+   }
+   else {
+      /* no depth buffer */
+      MEMSET(depth, 0, n * sizeof(GLfloat));
+   }
+}
+
+
+
+/**********************************************************************/
+/*****                Allocate and Clear Depth Buffer             *****/
+/**********************************************************************/
+
+
+
+/*
+ * Allocate a new depth buffer.  If there's already a depth buffer allocated
+ * it will be free()'d.  The new depth buffer will be uniniitalized.
+ * This function is only called through Driver.alloc_depth_buffer.
+ */
+void
+_mesa_alloc_depth_buffer( GLcontext *ctx )
+{
+   /* deallocate current depth buffer if present */
+   if (ctx->DrawBuffer->UseSoftwareDepthBuffer) {
+      GLint bytesPerValue;
+
+      if (ctx->DrawBuffer->DepthBuffer) {
+         FREE(ctx->DrawBuffer->DepthBuffer);
+         ctx->DrawBuffer->DepthBuffer = NULL;
+      }
+
+      /* allocate new depth buffer, but don't initialize it */
+      if (ctx->Visual->DepthBits <= 16)
+         bytesPerValue = sizeof(GLushort);
+      else
+         bytesPerValue = sizeof(GLuint);
+
+      ctx->DrawBuffer->DepthBuffer = MALLOC( ctx->DrawBuffer->Width
+                                             * ctx->DrawBuffer->Height
+                                             * bytesPerValue );
+
+      if (!ctx->DrawBuffer->DepthBuffer) {
+         /* out of memory */
+         ctx->Depth.Test = GL_FALSE;
+         ctx->NewState |= NEW_RASTER_OPS;
+         gl_error( ctx, GL_OUT_OF_MEMORY, "Couldn't allocate depth buffer" );
+      }
+   }
+}
+
+
+
+
+/*
+ * Clear the depth buffer.  If the depth buffer doesn't exist yet we'll
+ * allocate it now.
+ * This function is only called through Driver.clear_depth_buffer.
+ */
+void
+_mesa_clear_depth_buffer( GLcontext *ctx )
+{
+   if (ctx->Visual->DepthBits == 0
+       || !ctx->DrawBuffer->DepthBuffer
+       || !ctx->Depth.Mask) {
+      /* no depth buffer, or writing to it is disabled */
+      return;
+   }
+
+   /* The loops in this function have been written so the IRIX 5.3
+    * C compiler can unroll them.  Hopefully other compilers can too!
+    */
+
+   if (ctx->Scissor.Enabled) {
+      /* only clear scissor region */
+      if (ctx->Visual->DepthBits <= 16) {
+         const GLushort clearValue = (GLushort) (ctx->Depth.Clear * ctx->Visual->DepthMax);
+         const GLint rows = ctx->DrawBuffer->Ymax - ctx->DrawBuffer->Ymin + 1;
+         const GLint width = ctx->DrawBuffer->Width;
+         GLushort *dRow = (GLushort *) ctx->DrawBuffer->DepthBuffer
+            + ctx->DrawBuffer->Ymin * width + ctx->DrawBuffer->Xmin;
+         GLint i, j;
+         for (i = 0; i < rows; i++) {
+            for (j = 0; j < width; j++) {
+               dRow[j] = clearValue;
+            }
+            dRow += width;
+         }
+      }
+      else {
+         const GLuint clearValue = (GLuint) (ctx->Depth.Clear * ctx->Visual->DepthMax);
+         const GLint rows = ctx->DrawBuffer->Ymax - ctx->DrawBuffer->Ymin + 1;
+         const GLint width = ctx->DrawBuffer->Width;
+         GLuint *dRow = (GLuint *) ctx->DrawBuffer->DepthBuffer
+            + ctx->DrawBuffer->Ymin * width + ctx->DrawBuffer->Xmin;
+         GLint i, j;
+         for (i = 0; i < rows; i++) {
+            for (j = 0; j < width; j++) {
+               dRow[j] = clearValue;
+            }
+            dRow += width;
+         }
       }
    }
    else {
       /* clear whole buffer */
-      if (sizeof(GLdepth)==2 && (clear_value&0xff)==(clear_value>>8)) {
-         /* lower and upper bytes of clear_value are same, use MEMSET */
-         MEMSET( ctx->DrawBuffer->Depth, clear_value&0xff,
-                 2*ctx->DrawBuffer->Width*ctx->DrawBuffer->Height);
+      if (ctx->Visual->DepthBits <= 16) {
+         const GLushort clearValue = (GLushort) (ctx->Depth.Clear * ctx->Visual->DepthMax);
+         if ((clearValue & 0xff) == (clearValue >> 8)) {
+            /* lower and upper bytes of clear_value are same, use MEMSET */
+            MEMSET( ctx->DrawBuffer->DepthBuffer, clearValue & 0xff,
+                    2 * ctx->DrawBuffer->Width * ctx->DrawBuffer->Height);
+         }
+         else {
+            GLushort *d = (GLushort *) ctx->DrawBuffer->DepthBuffer;
+            GLint n = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height;
+            while (n >= 16) {
+               d[0] = clearValue;    d[1] = clearValue;
+               d[2] = clearValue;    d[3] = clearValue;
+               d[4] = clearValue;    d[5] = clearValue;
+               d[6] = clearValue;    d[7] = clearValue;
+               d[8] = clearValue;    d[9] = clearValue;
+               d[10] = clearValue;   d[11] = clearValue;
+               d[12] = clearValue;   d[13] = clearValue;
+               d[14] = clearValue;   d[15] = clearValue;
+               d += 16;
+               n -= 16;
+            }
+            while (n > 0) {
+               *d++ = clearValue;
+               n--;
+            }
+         }
       }
       else {
-         GLdepth *d = ctx->DrawBuffer->Depth;
+         /* >16 bit depth buffer */
+         GLuint *d = (GLuint *) ctx->DrawBuffer->DepthBuffer;
+         const GLuint clearValue = (GLuint) (ctx->Depth.Clear * ctx->Visual->DepthMax);
          GLint n = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height;
-         while (n>=16) {
-            d[0] = clear_value;    d[1] = clear_value;
-            d[2] = clear_value;    d[3] = clear_value;
-            d[4] = clear_value;    d[5] = clear_value;
-            d[6] = clear_value;    d[7] = clear_value;
-            d[8] = clear_value;    d[9] = clear_value;
-            d[10] = clear_value;   d[11] = clear_value;
-            d[12] = clear_value;   d[13] = clear_value;
-            d[14] = clear_value;   d[15] = clear_value;
+         while (n >= 16) {
+            d[0] = clearValue;    d[1] = clearValue;
+            d[2] = clearValue;    d[3] = clearValue;
+            d[4] = clearValue;    d[5] = clearValue;
+            d[6] = clearValue;    d[7] = clearValue;
+            d[8] = clearValue;    d[9] = clearValue;
+            d[10] = clearValue;   d[11] = clearValue;
+            d[12] = clearValue;   d[13] = clearValue;
+            d[14] = clearValue;   d[15] = clearValue;
             d += 16;
             n -= 16;
          }
-         while (n>0) {
-            *d++ = clear_value;
+         while (n > 0) {
+            *d++ = clearValue;
             n--;
          }
       }
    }
 }
-
-
-