more clean-ups
[mesa.git] / src / mesa / swrast / s_depth.c
index 03a5ec9735153c5d3486eecf6d1a374b655274da..b15638b658a112577081644b4c9bbce476dae073 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: s_depth.c,v 1.15 2002/02/02 21:40:33 brianp Exp $ */
+/* $Id: s_depth.c,v 1.25 2002/10/30 19:49:30 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
 #include "glheader.h"
 #include "context.h"
 #include "macros.h"
-#include "mem.h"
+#include "imports.h"
 
 #include "s_depth.h"
 #include "s_context.h"
 
 
-
-
-/*
+/**
  * Return address of depth buffer value for given window coord.
  */
 GLvoid *
@@ -292,7 +290,7 @@ depth_test_span16( GLcontext *ctx, GLuint n,
         }
         break;
       case GL_NEVER:
-         BZERO(mask, n * sizeof(GLubyte));
+         _mesa_bzero(mask, n * sizeof(GLubyte));
         break;
       default:
          _mesa_problem(ctx, "Bad depth func in depth_test_span16");
@@ -521,7 +519,7 @@ depth_test_span32( GLcontext *ctx, GLuint n,
         }
         break;
       case GL_NEVER:
-         BZERO(mask, n * sizeof(GLubyte));
+         _mesa_bzero(mask, n * sizeof(GLubyte));
         break;
       default:
          _mesa_problem(ctx, "Bad depth func in depth_test_span32");
@@ -532,58 +530,31 @@ depth_test_span32( GLcontext *ctx, GLuint n,
 
 
 
-
-GLuint
-_old_depth_test_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
-                     const GLdepth z[], GLubyte mask[] )
-{
-   SWcontext *swrast = SWRAST_CONTEXT(ctx);
-   if (swrast->Driver.ReadDepthSpan) {
-      /* hardware-based depth buffer */
-      GLdepth zbuffer[MAX_WIDTH];
-      GLuint passed;
-      (*swrast->Driver.ReadDepthSpan)(ctx, n, x, y, zbuffer);
-      passed = depth_test_span32(ctx, n, zbuffer, z, mask);
-      assert(swrast->Driver.WriteDepthSpan);
-      (*swrast->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, zptr, z, mask);
-         return passed;
-      }
-      else {
-         GLuint *zptr = (GLuint *) Z_ADDRESS32(ctx, x, y);
-         GLuint passed = depth_test_span32(ctx, n, zptr, z, mask);
-         return passed;
-      }
-   }
-}
-
-
 /*
  * Apply depth test to span of fragments.  Hardware or software z buffer.
  */
 static GLuint
 depth_test_span( GLcontext *ctx, struct sw_span *span)
 {
+   const GLint x = span->x;
+   const GLint y = span->y;
+   const GLuint n = span->end;
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
 
+   ASSERT((span->arrayMask & SPAN_XY) == 0);
    ASSERT(span->arrayMask & SPAN_Z);
    
    if (swrast->Driver.ReadDepthSpan) {
       /* hardware-based depth buffer */
       GLdepth zbuffer[MAX_WIDTH];
       GLuint passed;
-      (*swrast->Driver.ReadDepthSpan)(ctx, span->end, span->x, span->y, zbuffer);
-      passed = depth_test_span32(ctx, span->end,
-                                zbuffer, span->zArray, span->mask);
+      (*swrast->Driver.ReadDepthSpan)(ctx, n, x, y, zbuffer);
+      passed = depth_test_span32(ctx, n, zbuffer, span->array->z,
+                                 span->array->mask);
       ASSERT(swrast->Driver.WriteDepthSpan);
-      (*swrast->Driver.WriteDepthSpan)(ctx, span->end, span->x, span->y, zbuffer, span->mask);
-      if (passed < span->end)
+      (*swrast->Driver.WriteDepthSpan)(ctx, n, x, y, zbuffer,
+                                       span->array->mask);
+      if (passed < n)
          span->writeAll = GL_FALSE;
       return passed;
    }
@@ -591,15 +562,29 @@ depth_test_span( GLcontext *ctx, struct sw_span *span)
       GLuint passed;
       /* software depth buffer */
       if (ctx->Visual.depthBits <= 16) {
-         GLushort *zptr = (GLushort *) Z_ADDRESS16(ctx, span->x, span->y);
-         passed = depth_test_span16(ctx, span->end, zptr, span->zArray, span->mask);
+         GLushort *zptr = (GLushort *) Z_ADDRESS16(ctx, x, y);
+         passed = depth_test_span16(ctx, n, zptr, span->array->z, span->array->mask);
       }
       else {
-         GLuint *zptr = (GLuint *) Z_ADDRESS32(ctx, span->x, span->y);
-         passed = depth_test_span32(ctx, span->end, zptr, span->zArray, span->mask);
+         GLuint *zptr = (GLuint *) Z_ADDRESS32(ctx, x, y);
+         passed = depth_test_span32(ctx, n, zptr, span->array->z, span->array->mask);
       }
-      if (passed < span->end)
+#if 1
+      if (passed < span->end) {
          span->writeAll = GL_FALSE;
+      }
+#else
+      /* this causes a glDrawPixels(GL_DEPTH_COMPONENT) conformance failure */
+      if (passed < span->end) {
+         span->writeAll = GL_FALSE;
+         if (passed == 0) {
+             span->end = 0;
+             return 0;
+         }
+         while (span->end > 0  &&  span->mask[span->end - 1] == 0)
+             span->end --;
+      }
+#endif
       return passed;
    }
 }
@@ -844,7 +829,7 @@ software_depth_test_pixels16( GLcontext *ctx, GLuint n,
         break;
       case GL_NEVER:
         /* depth test never passes */
-         BZERO(mask, n * sizeof(GLubyte));
+         _mesa_bzero(mask, n * sizeof(GLubyte));
         break;
       default:
          _mesa_problem(ctx, "Bad depth func in software_depth_test_pixels");
@@ -1090,7 +1075,7 @@ software_depth_test_pixels32( GLcontext *ctx, GLuint n,
         break;
       case GL_NEVER:
         /* depth test never passes */
-         BZERO(mask, n * sizeof(GLubyte));
+         _mesa_bzero(mask, n * sizeof(GLubyte));
         break;
       default:
          _mesa_problem(ctx, "Bad depth func in software_depth_test_pixels");
@@ -1324,19 +1309,25 @@ hardware_depth_test_pixels( GLcontext *ctx, GLuint n, GLdepth zbuffer[],
         break;
       case GL_NEVER:
         /* depth test never passes */
-         BZERO(mask, n * sizeof(GLubyte));
+         _mesa_bzero(mask, n * sizeof(GLubyte));
         break;
       default:
          _mesa_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[] )
+
+
+static GLuint
+depth_test_pixels( GLcontext *ctx, struct sw_span *span )
 {
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
+   const GLuint n = span->end;
+   const GLint *x = span->array->x;
+   const GLint *y = span->array->y;
+   const GLdepth *z = span->array->z;
+   GLubyte *mask = span->array->mask;
+
    if (swrast->Driver.ReadDepthPixels) {
       /* read depth values from hardware Z buffer */
       GLdepth zbuffer[MAX_WIDTH];
@@ -1355,22 +1346,21 @@ _mesa_depth_test_pixels( GLcontext *ctx,
       else
          software_depth_test_pixels32(ctx, n, x, y, z, mask);
    }
+   return n; /* not really correct, but OK */
 }
 
 
-
+/**
+ * Apply depth (Z) buffer testing to the span.
+ * \return approx number of pixels that passed (only zero is reliable)
+ */
 GLuint
 _mesa_depth_test_span( GLcontext *ctx, struct sw_span *span)
 {
-   if (span->arrayMask & SPAN_XY) {
-      _mesa_depth_test_pixels(ctx, span->end,
-                              span->xArray, span->yArray,
-                              span->zArray, span->mask);
-      return 1;
-   }
-   else {
+   if (span->arrayMask & SPAN_XY)
+      return depth_test_pixels(ctx, span);
+   else
       return depth_test_span(ctx, span);
-   }
 }
 
 
@@ -1380,7 +1370,7 @@ _mesa_depth_test_span( GLcontext *ctx, struct sw_span *span)
 /**********************************************************************/
 
 
-/*
+/**
  * Read a span of depth values from the depth buffer.
  * This function does clipping before calling the device driver function.
  */
@@ -1390,8 +1380,8 @@ _mesa_read_depth_span( GLcontext *ctx,
 {
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
 
-   if (y < 0 || y >= ctx->DrawBuffer->Height ||
-       x + (GLint) n <= 0 || x >= ctx->DrawBuffer->Width) {
+   if (y < 0 || y >= (GLint) ctx->DrawBuffer->Height ||
+       x + (GLint) n <= 0 || x >= (GLint) ctx->DrawBuffer->Width) {
       /* span is completely outside framebuffer */
       GLint i;
       for (i = 0; i < n; i++)
@@ -1408,8 +1398,8 @@ _mesa_read_depth_span( GLcontext *ctx,
       n -= dx;
       depth += dx;
    }
-   if (x + n > ctx->DrawBuffer->Width) {
-      GLint dx = x + n - ctx->DrawBuffer->Width;
+   if (x + n > (GLint) ctx->DrawBuffer->Width) {
+      GLint dx = x + n - (GLint) ctx->DrawBuffer->Width;
       GLint i;
       for (i = 0; i < dx; i++)
          depth[n - i - 1] = 0;
@@ -1442,7 +1432,7 @@ _mesa_read_depth_span( GLcontext *ctx,
    }
    else {
       /* no depth buffer */
-      BZERO(depth, n * sizeof(GLfloat));
+      _mesa_bzero(depth, n * sizeof(GLfloat));
    }
 
 }
@@ -1450,7 +1440,7 @@ _mesa_read_depth_span( GLcontext *ctx,
 
 
 
-/*
+/**
  * 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
@@ -1464,8 +1454,8 @@ _mesa_read_depth_span_float( GLcontext *ctx,
    SWcontext *swrast = SWRAST_CONTEXT(ctx);
    const GLfloat scale = 1.0F / ctx->DepthMaxF;
 
-   if (y < 0 || y >= ctx->DrawBuffer->Height ||
-       x + (GLint) n <= 0 || x >= ctx->DrawBuffer->Width) {
+   if (y < 0 || y >= (GLint) ctx->DrawBuffer->Height ||
+       x + (GLint) n <= 0 || x >= (GLint) ctx->DrawBuffer->Width) {
       /* span is completely outside framebuffer */
       GLint i;
       for (i = 0; i < n; i++)
@@ -1481,8 +1471,8 @@ _mesa_read_depth_span_float( GLcontext *ctx,
       n -= dx;
       x = 0;
    }
-   if (x + n > ctx->DrawBuffer->Width) {
-      GLint dx = x + n - ctx->DrawBuffer->Width;
+   if (x + n > (GLint) ctx->DrawBuffer->Width) {
+      GLint dx = x + n - (GLint) ctx->DrawBuffer->Width;
       GLint i;
       for (i = 0; i < dx; i++)
          depth[n - i - 1] = 0.0F;
@@ -1521,7 +1511,7 @@ _mesa_read_depth_span_float( GLcontext *ctx,
    }
    else {
       /* no depth buffer */
-      BZERO(depth, n * sizeof(GLfloat));
+      _mesa_bzero(depth, n * sizeof(GLfloat));
    }
 }
 
@@ -1533,46 +1523,46 @@ _mesa_read_depth_span_float( GLcontext *ctx,
 
 
 
-/*
+/**
  * 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 )
+_mesa_alloc_depth_buffer( GLframebuffer *buffer )
 {
-   /* deallocate current depth buffer if present */
-   if (ctx->DrawBuffer->UseSoftwareDepthBuffer) {
-      GLint bytesPerValue;
+   GLint bytesPerValue;
 
-      if (ctx->DrawBuffer->DepthBuffer) {
-         FREE(ctx->DrawBuffer->DepthBuffer);
-         ctx->DrawBuffer->DepthBuffer = NULL;
-      }
+   ASSERT(buffer->UseSoftwareDepthBuffer);
 
-      /* allocate new depth buffer, but don't initialize it */
-      if (ctx->Visual.depthBits <= 16)
-         bytesPerValue = sizeof(GLushort);
-      else
-         bytesPerValue = sizeof(GLuint);
+   /* deallocate current depth buffer if present */
+   if (buffer->DepthBuffer) {
+      MESA_PBUFFER_FREE(buffer->DepthBuffer);
+      buffer->DepthBuffer = NULL;
+   }
+
+   /* allocate new depth buffer, but don't initialize it */
+   if (buffer->Visual.depthBits <= 16)
+      bytesPerValue = sizeof(GLushort);
+   else
+      bytesPerValue = sizeof(GLuint);
 
-      ctx->DrawBuffer->DepthBuffer = MALLOC( ctx->DrawBuffer->Width
-                                             * ctx->DrawBuffer->Height
-                                             * bytesPerValue );
+   buffer->DepthBuffer = MESA_PBUFFER_ALLOC(buffer->Width * buffer->Height
+                                            * bytesPerValue);
 
-      if (!ctx->DrawBuffer->DepthBuffer) {
-         /* out of memory */
+   if (!buffer->DepthBuffer) {
+      /* out of memory */
+      GET_CURRENT_CONTEXT(ctx);
+      if (ctx) {
          ctx->Depth.Test = GL_FALSE;
          ctx->NewState |= _NEW_DEPTH;
-         _mesa_error( ctx, GL_OUT_OF_MEMORY, "Couldn't allocate depth buffer" );
+         _mesa_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.
@@ -1630,7 +1620,7 @@ _mesa_clear_depth_buffer( GLcontext *ctx )
          const GLushort clearValue = (GLushort) (ctx->Depth.Clear * ctx->DepthMax);
          if ((clearValue & 0xff) == (clearValue >> 8)) {
             if (clearValue == 0) {
-               BZERO(ctx->DrawBuffer->DepthBuffer,
+               _mesa_bzero(ctx->DrawBuffer->DepthBuffer,
                      2*ctx->DrawBuffer->Width*ctx->DrawBuffer->Height);
             }
             else {
@@ -1664,7 +1654,7 @@ _mesa_clear_depth_buffer( GLcontext *ctx )
          /* >16 bit depth buffer */
          const GLuint clearValue = (GLuint) (ctx->Depth.Clear * ctx->DepthMax);
          if (clearValue == 0) {
-            BZERO(ctx->DrawBuffer->DepthBuffer,
+            _mesa_bzero(ctx->DrawBuffer->DepthBuffer,
                 ctx->DrawBuffer->Width*ctx->DrawBuffer->Height*sizeof(GLuint));
          }
          else {