more work on GL_ARB_texture_compression
[mesa.git] / src / mesa / main / depth.c
index b2fa94757e3afd6f5df6bb21f3e939994a93282d..8a09206a6a9d4b2fea34a6775ac136fce8d89ab6 100644 (file)
@@ -1,10 +1,10 @@
-/* $Id: depth.c,v 1.11 1999/12/10 19:09:22 brianp Exp $ */
+/* $Id: depth.c,v 1.17 2000/04/11 20:42:22 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"),
@@ -124,24 +124,547 @@ _mesa_DepthMask( GLboolean flag )
 
 
 
+/**********************************************************************/
+/*****                           Misc                             *****/
+/**********************************************************************/
+
+/*
+ * 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                  *****/
 /**********************************************************************/
 
 
 /*
- * 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.
+ * 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.
+ */
+static GLuint
+depth_test_span16( GLcontext *ctx, GLuint n, GLint x, GLint y,
+                   GLushort zbuffer[], const GLdepth z[], GLubyte 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:
+         BZERO(mask, n * sizeof(GLubyte));
+        break;
+      default:
+         gl_problem(ctx, "Bad depth func in depth_test_span16");
+   }
+
+   return passed;
+}
+
+
+static GLuint
+depth_test_span32( GLcontext *ctx, GLuint n, GLint x, GLint y,
+                   GLuint zbuffer[], const GLdepth z[], GLubyte 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:
+         BZERO(mask, n * sizeof(GLubyte));
+        break;
+      default:
+         gl_problem(ctx, "Bad depth func in depth_test_span32");
+   }
+
+   return passed;
+}
+
+
+
+/*
+ * Apply depth test to span of fragments.  Hardware or software z buffer.
  */
-static GLuint
-depth_test_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
-                 GLdepth zbuffer[], const GLdepth z[], GLubyte mask[] )
+GLuint
+_mesa_depth_test_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
+                       const GLdepth z[], GLubyte mask[] )
 {
-   GLuint passed = 0;
+   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 {
+      /* 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;
+      }
+      else {
+         GLuint *zptr = (GLuint *) Z_ADDRESS32(ctx, x, y);
+         GLuint passed = depth_test_span32(ctx, n, x, y, zptr, z, mask);
+         return passed;
+      }
+   }
+}
 
+
+
+
+/*
+ * Do depth testing for an array of fragments using software Z buffer.
+ */
+static void
+software_depth_test_pixels16( 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:
@@ -150,10 +673,10 @@ depth_test_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
             GLuint i;
            for (i=0; i<n; i++) {
               if (mask[i]) {
-                 if (z[i] < zbuffer[i]) {
+                 GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
+                 if (z[i] < *zptr) {
                     /* pass */
-                    zbuffer[i] = z[i];
-                    passed++;
+                    *zptr = z[i];
                  }
                  else {
                     /* fail */
@@ -167,11 +690,12 @@ depth_test_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
             GLuint i;
            for (i=0; i<n; i++) {
               if (mask[i]) {
-                 if (z[i] < zbuffer[i]) {
+                 GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
+                 if (z[i] < *zptr) {
                     /* pass */
-                    passed++;
                  }
                  else {
+                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -179,16 +703,18 @@ depth_test_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
         }
         break;
       case GL_LEQUAL:
-        if (ctx->Depth.Mask) {
+         if (ctx->Depth.Mask) {
            /* Update Z buffer */
             GLuint i;
-           for (i=0;i<n;i++) {
+           for (i=0; i<n; i++) {
               if (mask[i]) {
-                 if (z[i] <= zbuffer[i]) {
-                    zbuffer[i] = z[i];
-                    passed++;
+                 GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
+                 if (z[i] <= *zptr) {
+                    /* pass */
+                    *zptr = z[i];
                  }
                  else {
+                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -197,13 +723,14 @@ depth_test_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
         else {
            /* Don't update Z buffer */
             GLuint i;
-           for (i=0;i<n;i++) {
+           for (i=0; i<n; i++) {
               if (mask[i]) {
-                 if (z[i] <= zbuffer[i]) {
+                 GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
+                 if (z[i] <= *zptr) {
                     /* pass */
-                    passed++;
                  }
                  else {
+                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -211,16 +738,18 @@ depth_test_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
         }
         break;
       case GL_GEQUAL:
-        if (ctx->Depth.Mask) {
+         if (ctx->Depth.Mask) {
            /* Update Z buffer */
             GLuint i;
-           for (i=0;i<n;i++) {
+           for (i=0; i<n; i++) {
               if (mask[i]) {
-                 if (z[i] >= zbuffer[i]) {
-                    zbuffer[i] = z[i];
-                    passed++;
+                 GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
+                 if (z[i] >= *zptr) {
+                    /* pass */
+                    *zptr = z[i];
                  }
                  else {
+                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -229,13 +758,14 @@ depth_test_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
         else {
            /* Don't update Z buffer */
             GLuint i;
-           for (i=0;i<n;i++) {
+           for (i=0; i<n; i++) {
               if (mask[i]) {
-                 if (z[i] >= zbuffer[i]) {
+                 GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
+                 if (z[i] >= *zptr) {
                     /* pass */
-                    passed++;
                  }
                  else {
+                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -243,16 +773,18 @@ depth_test_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
         }
         break;
       case GL_GREATER:
-        if (ctx->Depth.Mask) {
+         if (ctx->Depth.Mask) {
            /* Update Z buffer */
             GLuint i;
-           for (i=0;i<n;i++) {
+           for (i=0; i<n; i++) {
               if (mask[i]) {
-                 if (z[i] > zbuffer[i]) {
-                    zbuffer[i] = z[i];
-                    passed++;
+                 GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
+                 if (z[i] > *zptr) {
+                    /* pass */
+                    *zptr = z[i];
                  }
                  else {
+                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -261,13 +793,14 @@ depth_test_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
         else {
            /* Don't update Z buffer */
             GLuint i;
-           for (i=0;i<n;i++) {
+           for (i=0; i<n; i++) {
               if (mask[i]) {
-                 if (z[i] > zbuffer[i]) {
+                 GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
+                 if (z[i] > *zptr) {
                     /* pass */
-                    passed++;
                  }
                  else {
+                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -275,16 +808,18 @@ depth_test_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
         }
         break;
       case GL_NOTEQUAL:
-        if (ctx->Depth.Mask) {
+         if (ctx->Depth.Mask) {
            /* Update Z buffer */
             GLuint i;
-           for (i=0;i<n;i++) {
+           for (i=0; i<n; i++) {
               if (mask[i]) {
-                 if (z[i] != zbuffer[i]) {
-                    zbuffer[i] = z[i];
-                    passed++;
+                 GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
+                 if (z[i] != *zptr) {
+                    /* pass */
+                    *zptr = z[i];
                  }
                  else {
+                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -293,13 +828,14 @@ depth_test_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
         else {
            /* Don't update Z buffer */
             GLuint i;
-           for (i=0;i<n;i++) {
+           for (i=0; i<n; i++) {
               if (mask[i]) {
-                 if (z[i] != zbuffer[i]) {
+                 GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
+                 if (z[i] != *zptr) {
                     /* pass */
-                    passed++;
                  }
                  else {
+                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -307,16 +843,18 @@ depth_test_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
         }
         break;
       case GL_EQUAL:
-        if (ctx->Depth.Mask) {
+         if (ctx->Depth.Mask) {
            /* Update Z buffer */
             GLuint i;
-           for (i=0;i<n;i++) {
+           for (i=0; i<n; i++) {
               if (mask[i]) {
-                 if (z[i] == zbuffer[i]) {
-                    zbuffer[i] = z[i];
-                    passed++;
+                 GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
+                 if (z[i] == *zptr) {
+                    /* pass */
+                    *zptr = z[i];
                  }
                  else {
+                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -325,13 +863,14 @@ depth_test_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
         else {
            /* Don't update Z buffer */
             GLuint i;
-           for (i=0;i<n;i++) {
+           for (i=0; i<n; i++) {
               if (mask[i]) {
-                 if (z[i] == zbuffer[i]) {
+                 GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
+                 if (z[i] == *zptr) {
                     /* pass */
-                    passed++;
                  }
                  else {
+                    /* fail */
                     mask[i] = 0;
                  }
               }
@@ -342,72 +881,35 @@ depth_test_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
         if (ctx->Depth.Mask) {
            /* Update Z buffer */
             GLuint i;
-           for (i=0;i<n;i++) {
+           for (i=0; i<n; i++) {
               if (mask[i]) {
-                 zbuffer[i] = z[i];
-                 passed++;
+                 GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
+                 *zptr = z[i];
               }
            }
         }
         else {
            /* Don't update Z buffer or mask */
-           passed = n;
         }
         break;
       case GL_NEVER:
-         MEMSET(mask, 0, n * sizeof(GLubyte));
+        /* depth test never passes */
+         BZERO(mask, n * sizeof(GLubyte));
         break;
       default:
-         gl_problem(ctx, "Bad depth func in depth_test_span");
-   }
-
-   return passed;
-}
-
-
-
-/*
- * Apply depth test to span of fragments.  Hardware or software z buffer.
- */
-GLuint
-gl_depth_test_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
-                    const GLdepth z[], GLubyte mask[] )
-{
-   GLdepth zbuffer[MAX_WIDTH];
-   GLdepth *zptr;
-   GLuint passed;
-
-   if (ctx->Driver.ReadDepthSpan) {
-      /* read depth values out of hardware Z buffer */
-      (*ctx->Driver.ReadDepthSpan)(ctx, n, x, y, zbuffer);
-      zptr = zbuffer;
-   }
-   else {
-      /* test against software depth buffer values */
-      zptr = Z_ADDRESS( ctx, x, y );
-   }
-
-   passed = depth_test_span( ctx, n, x, y, zptr, z, mask );
-
-   if (ctx->Driver.WriteDepthSpan) {
-      /* write updated depth values into hardware Z buffer */
-      assert(zptr == zbuffer);
-      (*ctx->Driver.WriteDepthSpan)(ctx, n, x, y, zbuffer, mask);
+         gl_problem(ctx, "Bad depth func in software_depth_test_pixels");
    }
-
-   return passed;
 }
 
 
 
-
 /*
  * Do depth testing for an array of fragments using software Z buffer.
  */
 static void
-software_depth_test_pixels( GLcontext *ctx, GLuint n,
-                            const GLint x[], const GLint y[],
-                            const GLdepth z[], GLubyte mask[] )
+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) {
@@ -417,7 +919,7 @@ software_depth_test_pixels( GLcontext *ctx, GLuint n,
             GLuint i;
            for (i=0; i<n; i++) {
               if (mask[i]) {
-                 GLdepth *zptr = Z_ADDRESS(ctx,x[i],y[i]);
+                 GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
                  if (z[i] < *zptr) {
                     /* pass */
                     *zptr = z[i];
@@ -434,7 +936,7 @@ software_depth_test_pixels( GLcontext *ctx, GLuint n,
             GLuint i;
            for (i=0; i<n; i++) {
               if (mask[i]) {
-                 GLdepth *zptr = Z_ADDRESS(ctx,x[i],y[i]);
+                 GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
                  if (z[i] < *zptr) {
                     /* pass */
                  }
@@ -452,7 +954,7 @@ software_depth_test_pixels( GLcontext *ctx, GLuint n,
             GLuint i;
            for (i=0; i<n; i++) {
               if (mask[i]) {
-                 GLdepth *zptr = Z_ADDRESS(ctx,x[i],y[i]);
+                 GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
                  if (z[i] <= *zptr) {
                     /* pass */
                     *zptr = z[i];
@@ -469,7 +971,7 @@ software_depth_test_pixels( GLcontext *ctx, GLuint n,
             GLuint i;
            for (i=0; i<n; i++) {
               if (mask[i]) {
-                 GLdepth *zptr = Z_ADDRESS(ctx,x[i],y[i]);
+                 GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
                  if (z[i] <= *zptr) {
                     /* pass */
                  }
@@ -487,7 +989,7 @@ software_depth_test_pixels( GLcontext *ctx, GLuint n,
             GLuint i;
            for (i=0; i<n; i++) {
               if (mask[i]) {
-                 GLdepth *zptr = Z_ADDRESS(ctx,x[i],y[i]);
+                 GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
                  if (z[i] >= *zptr) {
                     /* pass */
                     *zptr = z[i];
@@ -504,7 +1006,7 @@ software_depth_test_pixels( GLcontext *ctx, GLuint n,
             GLuint i;
            for (i=0; i<n; i++) {
               if (mask[i]) {
-                 GLdepth *zptr = Z_ADDRESS(ctx,x[i],y[i]);
+                 GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
                  if (z[i] >= *zptr) {
                     /* pass */
                  }
@@ -522,7 +1024,7 @@ software_depth_test_pixels( GLcontext *ctx, GLuint n,
             GLuint i;
            for (i=0; i<n; i++) {
               if (mask[i]) {
-                 GLdepth *zptr = Z_ADDRESS(ctx,x[i],y[i]);
+                 GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
                  if (z[i] > *zptr) {
                     /* pass */
                     *zptr = z[i];
@@ -539,7 +1041,7 @@ software_depth_test_pixels( GLcontext *ctx, GLuint n,
             GLuint i;
            for (i=0; i<n; i++) {
               if (mask[i]) {
-                 GLdepth *zptr = Z_ADDRESS(ctx,x[i],y[i]);
+                 GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
                  if (z[i] > *zptr) {
                     /* pass */
                  }
@@ -557,7 +1059,7 @@ software_depth_test_pixels( GLcontext *ctx, GLuint n,
             GLuint i;
            for (i=0; i<n; i++) {
               if (mask[i]) {
-                 GLdepth *zptr = Z_ADDRESS(ctx,x[i],y[i]);
+                 GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
                  if (z[i] != *zptr) {
                     /* pass */
                     *zptr = z[i];
@@ -574,7 +1076,7 @@ software_depth_test_pixels( GLcontext *ctx, GLuint n,
             GLuint i;
            for (i=0; i<n; i++) {
               if (mask[i]) {
-                 GLdepth *zptr = Z_ADDRESS(ctx,x[i],y[i]);
+                 GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
                  if (z[i] != *zptr) {
                     /* pass */
                  }
@@ -592,7 +1094,7 @@ software_depth_test_pixels( GLcontext *ctx, GLuint n,
             GLuint i;
            for (i=0; i<n; i++) {
               if (mask[i]) {
-                 GLdepth *zptr = Z_ADDRESS(ctx,x[i],y[i]);
+                 GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
                  if (z[i] == *zptr) {
                     /* pass */
                     *zptr = z[i];
@@ -609,7 +1111,7 @@ software_depth_test_pixels( GLcontext *ctx, GLuint n,
             GLuint i;
            for (i=0; i<n; i++) {
               if (mask[i]) {
-                 GLdepth *zptr = Z_ADDRESS(ctx,x[i],y[i]);
+                 GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
                  if (z[i] == *zptr) {
                     /* pass */
                  }
@@ -627,7 +1129,7 @@ software_depth_test_pixels( GLcontext *ctx, GLuint n,
             GLuint i;
            for (i=0; i<n; i++) {
               if (mask[i]) {
-                 GLdepth *zptr = Z_ADDRESS(ctx,x[i],y[i]);
+                 GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
                  *zptr = z[i];
               }
            }
@@ -638,7 +1140,7 @@ software_depth_test_pixels( GLcontext *ctx, GLuint n,
         break;
       case GL_NEVER:
         /* depth test never passes */
-         MEMSET(mask, 0, n * sizeof(GLubyte));
+         BZERO(mask, n * sizeof(GLubyte));
         break;
       default:
          gl_problem(ctx, "Bad depth func in software_depth_test_pixels");
@@ -872,7 +1374,7 @@ hardware_depth_test_pixels( GLcontext *ctx, GLuint n, GLdepth zbuffer[],
         break;
       case GL_NEVER:
         /* depth test never passes */
-         MEMSET(mask, 0, n * sizeof(GLubyte));
+         BZERO(mask, n * sizeof(GLubyte));
         break;
       default:
          gl_problem(ctx, "Bad depth func in hardware_depth_test_pixels");
@@ -881,9 +1383,10 @@ hardware_depth_test_pixels( GLcontext *ctx, GLuint n, GLdepth zbuffer[],
 
 
 
-void gl_depth_test_pixels( GLcontext *ctx,
-                           GLuint n, const GLint x[], const GLint y[],
-                           const GLdepth z[], GLubyte mask[] )
+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 */
@@ -894,11 +1397,14 @@ void gl_depth_test_pixels( GLcontext *ctx,
 
       /* update hardware Z buffer with new values */
       assert(ctx->Driver.WriteDepthPixels);
-      (*ctx->Driver.WriteDepthPixels)(ctx, n, x, y, z, mask );
+      (*ctx->Driver.WriteDepthPixels)(ctx, n, x, y, zbuffer, mask );
    }
    else {
       /* software depth testing */
-      software_depth_test_pixels(ctx, n, x, y, z, mask);
+      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);
    }
 }
 
@@ -911,6 +1417,74 @@ void gl_depth_test_pixels( GLcontext *ctx,
 /**********************************************************************/
 
 
+/*
+ * Read a span of depth values from the depth buffer.
+ * This function does clipping before calling the device driver function.
+ */
+void
+_mesa_read_depth_span( GLcontext *ctx,
+                       GLint n, GLint x, GLint y, GLdepth depth[] )
+{
+   if (y < 0 || y >= ctx->DrawBuffer->Height ||
+       x + (GLint) n <= 0 || x >= ctx->DrawBuffer->Width) {
+      /* span is completely outside framebuffer */
+      GLint i;
+      for (i = 0; i < n; i++)
+         depth[i] = 0;
+      return;
+   }
+
+   if (x < 0) {
+      GLint dx = -x;
+      GLint i;
+      for (i = 0; i < dx; i++)
+         depth[i] = 0;
+      x = 0;
+      n -= dx;
+      depth += dx;
+   }
+   if (x + n > ctx->DrawBuffer->Width) {
+      GLint dx = x + n - ctx->DrawBuffer->Width;
+      GLint i;
+      for (i = 0; i < dx; i++)
+         depth[n - i - 1] = 0;
+      n -= dx;
+   }
+   if (n <= 0) {
+      return;
+   }
+
+   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] = zptr[i];
+         }
+      }
+      else {
+         const GLuint *zptr = Z_ADDRESS32( ctx, x, y );
+         GLuint i;
+         for (i = 0; i < n; i++) {
+            depth[i] = zptr[i];
+         }
+      }
+   }
+   else if (ctx->Driver.ReadDepthSpan) {
+      /* read from hardware depth buffer */
+      (*ctx->Driver.ReadDepthSpan)( ctx, n, x, y, depth );
+   }
+   else {
+      /* no depth buffer */
+      BZERO(depth, n * sizeof(GLfloat));
+   }
+
+}
+
+
+
+
 /*
  * 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.
@@ -918,17 +1492,55 @@ void gl_depth_test_pixels( GLcontext *ctx,
  *         x,y - location of first pixel
  * Output:  depth - the array of depth values
  */
-void gl_read_depth_span_float( GLcontext* ctx,
-                               GLuint n, GLint x, GLint y, GLfloat depth[] )
+void
+_mesa_read_depth_span_float( GLcontext *ctx,
+                             GLint n, GLint x, GLint y, GLfloat depth[] )
 {
-   const GLfloat scale = 1.0F / DEPTH_SCALE;
+   const GLfloat scale = 1.0F / ctx->Visual->DepthMaxF;
+
+   if (y < 0 || y >= ctx->DrawBuffer->Height ||
+       x + (GLint) n <= 0 || x >= ctx->DrawBuffer->Width) {
+      /* span is completely outside framebuffer */
+      GLint i;
+      for (i = 0; i < n; i++)
+         depth[i] = 0.0F;
+      return;
+   }
+
+   if (x < 0) {
+      GLint dx = -x;
+      GLint i;
+      for (i = 0; i < dx; i++)
+         depth[i] = 0.0F;
+      n -= dx;
+      x = 0;
+   }
+   if (x + n > ctx->DrawBuffer->Width) {
+      GLint dx = x + n - ctx->DrawBuffer->Width;
+      GLint i;
+      for (i = 0; i < dx; i++)
+         depth[n - i - 1] = 0.0F;
+      n -= dx;
+   }
+   if (n <= 0) {
+      return;
+   }
 
-   if (ctx->DrawBuffer->Depth) {
+   if (ctx->DrawBuffer->DepthBuffer) {
       /* read from software depth buffer */
-      const GLdepth *zptr = Z_ADDRESS( ctx, x, y );
-      GLuint i;
-      for (i = 0; i < n; i++) {
-        depth[i] = (GLfloat) zptr[i] * scale;
+      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) {
@@ -943,7 +1555,7 @@ void gl_read_depth_span_float( GLcontext* ctx,
    }
    else {
       /* no depth buffer */
-      MEMSET(depth, 0, n * sizeof(GLfloat));
+      BZERO(depth, n * sizeof(GLfloat));
    }
 }
 
@@ -960,20 +1572,29 @@ void gl_read_depth_span_float( GLcontext* ctx,
  * 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 )
+void
+_mesa_alloc_depth_buffer( GLcontext *ctx )
 {
    /* deallocate current depth buffer if present */
    if (ctx->DrawBuffer->UseSoftwareDepthBuffer) {
-      if (ctx->DrawBuffer->Depth) {
-         FREE(ctx->DrawBuffer->Depth);
-         ctx->DrawBuffer->Depth = NULL;
+      GLint bytesPerValue;
+
+      if (ctx->DrawBuffer->DepthBuffer) {
+         FREE(ctx->DrawBuffer->DepthBuffer);
+         ctx->DrawBuffer->DepthBuffer = 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) {
+      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;
@@ -990,11 +1611,12 @@ void gl_alloc_depth_buffer( GLcontext* ctx )
  * allocate it now.
  * This function is only called through Driver.clear_depth_buffer.
  */
-void gl_clear_depth_buffer( GLcontext* ctx )
+void
+_mesa_clear_depth_buffer( GLcontext *ctx )
 {
-   GLdepth clear_value = (GLdepth) (ctx->Depth.Clear * DEPTH_SCALE);
-   
-   if (ctx->Visual->DepthBits==0 || !ctx->DrawBuffer->Depth || !ctx->Depth.Mask) {
+   if (ctx->Visual->DepthBits == 0
+       || !ctx->DrawBuffer->DepthBuffer
+       || !ctx->Depth.Mask) {
       /* no depth buffer, or writing to it is disabled */
       return;
    }
@@ -1005,41 +1627,97 @@ void gl_clear_depth_buffer( GLcontext* ctx )
 
    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);
+      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)) {
+            if (clearValue == 0) {
+               BZERO(ctx->DrawBuffer->DepthBuffer,
+                     2*ctx->DrawBuffer->Width*ctx->DrawBuffer->Height);
+            }
+            else {
+               /* 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;
-         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;
-            d += 16;
-            n -= 16;
+         /* >16 bit depth buffer */
+         const GLuint clearValue = (GLuint) (ctx->Depth.Clear * ctx->Visual->DepthMax);
+         if (clearValue == 0) {
+            BZERO(ctx->DrawBuffer->DepthBuffer,
+                ctx->DrawBuffer->Width*ctx->DrawBuffer->Height*sizeof(GLuint));
          }
-         while (n>0) {
-            *d++ = clear_value;
-            n--;
+         else {
+            GLint n = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height;
+            GLuint *d = (GLuint *) ctx->DrawBuffer->DepthBuffer;
+            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--;
+            }
          }
       }
    }