[Mesa-dev] [PATCH] swrast: fix Z testing of points/lines for 16-bit depth buffers

Brian Paul brianp at vmware.com
Fri Jan 6 12:58:50 PST 2012


We were comparing 32-bit Z buffer values against 16-bit fragment values.
Need to do scaling like for the 24-bit case.

Triangle Z testing was OK since it didn't hit this code path.
---
 src/mesa/swrast/s_depth.c |   36 +++++++++++++++++++++++++++++-------
 1 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/src/mesa/swrast/s_depth.c b/src/mesa/swrast/s_depth.c
index 53f21cb..42724c7 100644
--- a/src/mesa/swrast/s_depth.c
+++ b/src/mesa/swrast/s_depth.c
@@ -205,6 +205,7 @@ _swrast_depth_clamp_span( struct gl_context *ctx, SWspan *span )
 
 /**
  * Get array of 32-bit z values from the depth buffer.  With clipping.
+ * Note: the returned values are always in the range [0, 2^32-1].
  */
 static void
 get_z32_values(struct gl_context *ctx, struct gl_renderbuffer *rb,
@@ -235,6 +236,11 @@ get_z32_values(struct gl_context *ctx, struct gl_renderbuffer *rb,
    }
 }
 
+
+/**
+ * Put an array of 32-bit z values into the depth buffer.
+ * Note: the z values are always in the range [0, 2^32-1].
+ */
 static void
 put_z32_values(struct gl_context *ctx, struct gl_renderbuffer *rb,
                GLuint count, const GLint x[], const GLint y[],
@@ -284,8 +290,8 @@ _swrast_depth_test_span(struct gl_context *ctx, SWspan *span)
    void *zBufferVals;
    GLuint *zBufferTemp = NULL;
    GLuint passed;
+   GLuint zBits = _mesa_get_format_bits(rb->Format, GL_DEPTH_BITS);
    GLboolean ztest16 = GL_FALSE;
-   GLboolean ztest24 = _mesa_get_format_bits(rb->Format, GL_DEPTH_BITS) == 24;
 
    if (rb->Format == MESA_FORMAT_Z16 && !(span->arrayMask & SPAN_XY)) {
       /* directly read/write row of 16-bit Z values */
@@ -310,7 +316,7 @@ _swrast_depth_test_span(struct gl_context *ctx, SWspan *span)
          _mesa_unpack_uint_z_row(rb->Format, count, zStart, zBufferTemp);
       }
 
-      if (ztest24) {
+      if (zBits == 24) {
          GLuint i;
          /* Convert depth buffer values from 32 to 24 bits to match the
           * fragment Z values generated by rasterization.
@@ -319,6 +325,16 @@ _swrast_depth_test_span(struct gl_context *ctx, SWspan *span)
             zBufferTemp[i] >>= 8;
          }
       }
+      else if (zBits == 16) {
+         GLuint i;
+         /* Convert depth buffer values from 32 to 16 bits */
+         for (i = 0; i < count; i++) {
+            zBufferTemp[i] >>= 16;
+         }
+      }
+      else {
+         assert(zBits == 32);
+      }
 
       zBufferVals = zBufferTemp;
    }
@@ -332,16 +348,22 @@ _swrast_depth_test_span(struct gl_context *ctx, SWspan *span)
    if (zBufferTemp) {
       /* need to write temp Z values back into the buffer */
 
-      if (ztest24) {
+      /* Convert depth buffer values back to 32-bit values.  The least
+       * significant bits don't matter since they'll get dropped when
+       * they're packed back into the depth buffer.
+       */
+      if (zBits == 24) {
          GLuint i;
-         /* Convert depth buffer values back to 32-bit values.  The least
-          * significant bits don't matter since they'll get dropped when
-          * they're packed back into the depth buffer.
-          */
          for (i = 0; i < count; i++) {
             zBufferTemp[i] = (zBufferTemp[i] << 8);
          }
       }
+      else if (zBits == 16) {
+         GLuint i;
+         for (i = 0; i < count; i++) {
+            zBufferTemp[i] = zBufferTemp[i] << 16;
+         }
+      }
 
       if (span->arrayMask & SPAN_XY) {
          /* random locations */
-- 
1.7.3.4



More information about the mesa-dev mailing list