s/TEXTURE/ATTRIBS/
[mesa.git] / src / mesa / swrast / s_depth.c
index abc034c7f244c8347b0e43482b5658ee97075aaf..408174c990fb165941a8ee0df4015852e42347b5 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * Mesa 3-D graphics library
- * Version:  6.5
+ * Version:  6.5.1
  *
- * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2006  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"),
@@ -503,10 +503,10 @@ depth_test_span32( GLcontext *ctx, GLuint n,
  * Apply depth test to span of fragments.
  */
 static GLuint
-depth_test_span( GLcontext *ctx, struct sw_span *span)
+depth_test_span( GLcontext *ctx, SWspan *span)
 {
    struct gl_framebuffer *fb = ctx->DrawBuffer;
-   struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
+   struct gl_renderbuffer *rb = fb->_DepthBuffer;
    const GLint x = span->x;
    const GLint y = span->y;
    const GLuint count = span->end;
@@ -519,12 +519,13 @@ depth_test_span( GLcontext *ctx, struct sw_span *span)
    
    if (rb->GetPointer(ctx, rb, 0, 0)) {
       /* Directly access buffer */
-      if (ctx->DrawBuffer->Visual.depthBits <= 16) {
+      if (rb->DataType == GL_UNSIGNED_SHORT) {
          GLushort *zbuffer = (GLushort *) rb->GetPointer(ctx, rb, x, y);
          passed = depth_test_span16(ctx, count, zbuffer, zValues, mask);
       }
       else {
          GLuint *zbuffer = (GLuint *) rb->GetPointer(ctx, rb, x, y);
+         ASSERT(rb->DataType == GL_UNSIGNED_INT);
          passed = depth_test_span32(ctx, count, zbuffer, zValues, mask);
       }
    }
@@ -1050,10 +1051,10 @@ direct_depth_test_pixels32(GLcontext *ctx, GLuint *zStart, GLuint stride,
 
 
 static GLuint
-depth_test_pixels( GLcontext *ctx, struct sw_span *span )
+depth_test_pixels( GLcontext *ctx, SWspan *span )
 {
    struct gl_framebuffer *fb = ctx->DrawBuffer;
-   struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
+   struct gl_renderbuffer *rb = fb->_DepthBuffer;
    const GLuint count = span->end;
    const GLint *x = span->array->x;
    const GLint *y = span->array->y;
@@ -1070,6 +1071,7 @@ depth_test_pixels( GLcontext *ctx, struct sw_span *span )
       else {
          GLuint *zStart = (GLuint *) rb->Data;
          GLuint stride = rb->Width;
+         ASSERT(rb->DataType == GL_UNSIGNED_INT);
          direct_depth_test_pixels32(ctx, zStart, stride, count, x, y, z, mask);
       }
    }
@@ -1099,7 +1101,7 @@ depth_test_pixels( GLcontext *ctx, struct sw_span *span )
  * \return approx number of pixels that passed (only zero is reliable)
  */
 GLuint
-_swrast_depth_test_span( GLcontext *ctx, struct sw_span *span)
+_swrast_depth_test_span( GLcontext *ctx, SWspan *span)
 {
    if (span->arrayMask & SPAN_XY)
       return depth_test_pixels(ctx, span);
@@ -1116,10 +1118,10 @@ _swrast_depth_test_span( GLcontext *ctx, struct sw_span *span)
  * \return GL_TRUE if any fragments pass, GL_FALSE if no fragments pass
  */
 GLboolean
-_swrast_depth_bounds_test( GLcontext *ctx, struct sw_span *span )
+_swrast_depth_bounds_test( GLcontext *ctx, SWspan *span )
 {
    struct gl_framebuffer *fb = ctx->DrawBuffer;
-   struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
+   struct gl_renderbuffer *rb = fb->_DepthBuffer;
    GLuint zMin = (GLuint) (ctx->Depth.BoundsMin * fb->_DepthMaxF + 0.5F);
    GLuint zMax = (GLuint) (ctx->Depth.BoundsMax * fb->_DepthMaxF + 0.5F);
    GLubyte *mask = span->array->mask;
@@ -1136,7 +1138,7 @@ _swrast_depth_bounds_test( GLcontext *ctx, struct sw_span *span )
          zbuffer = zbuffer16;
       }
       else {
-         zbuffer = rb->GetPointer(ctx, rb, span->x, span->y);
+         zbuffer = (GLushort*) rb->GetPointer(ctx, rb, span->x, span->y);
          if (!zbuffer) {
             rb->GetRow(ctx, rb, count, span->x, span->y, zbuffer16);
             zbuffer = zbuffer16;
@@ -1157,13 +1159,14 @@ _swrast_depth_bounds_test( GLcontext *ctx, struct sw_span *span )
    else {
       /* get 32-bit values */
       GLuint zbuffer32[MAX_WIDTH], *zbuffer;
+      ASSERT(rb->DataType == GL_UNSIGNED_INT);
       if (span->arrayMask & SPAN_XY) {
          _swrast_get_values(ctx, rb, count, span->array->x, span->array->y,
                             zbuffer32, sizeof(GLuint));
          zbuffer = zbuffer32;
       }
       else {
-         zbuffer = rb->GetPointer(ctx, rb, span->x, span->y);
+         zbuffer = (GLuint*) rb->GetPointer(ctx, rb, span->x, span->y);
          if (!zbuffer) {
             rb->GetRow(ctx, rb, count, span->x, span->y, zbuffer32);
             zbuffer = zbuffer32;
@@ -1213,7 +1216,7 @@ _swrast_read_depth_span_float( GLcontext *ctx, struct gl_renderbuffer *rb,
    ASSERT(rb->_BaseFormat == GL_DEPTH_COMPONENT);
 
    if (y < 0 || y >= (GLint) rb->Height ||
-       x + (GLint) n <= 0 || x >= (GLint) rb->Width) {
+       x + n <= 0 || x >= (GLint) rb->Width) {
       /* span is completely outside framebuffer */
       _mesa_bzero(depth, n * sizeof(GLfloat));
       return;
@@ -1252,15 +1255,92 @@ _swrast_read_depth_span_float( GLcontext *ctx, struct gl_renderbuffer *rb,
       GLint i;
       rb->GetRow(ctx, rb, n, x, y, temp);
       for (i = 0; i < n; i++) {
-         depth[i] = temp[i];
+         depth[i] = temp[i] * scale;
+      }
+   }
+   else {
+      _mesa_problem(ctx, "Invalid depth renderbuffer data type");
+   }
+}
+
+
+/**
+ * As above, but return 32-bit GLuint values.
+ */
+void
+_swrast_read_depth_span_uint( GLcontext *ctx, struct gl_renderbuffer *rb,
+                              GLint n, GLint x, GLint y, GLuint depth[] )
+{
+   if (!rb) {
+      /* really only doing this to prevent FP exceptions later */
+      _mesa_bzero(depth, n * sizeof(GLfloat));
+   }
+
+   ASSERT(rb->_BaseFormat == GL_DEPTH_COMPONENT);
+
+   if (y < 0 || y >= (GLint) rb->Height ||
+       x + n <= 0 || x >= (GLint) rb->Width) {
+      /* span is completely outside framebuffer */
+      _mesa_bzero(depth, n * sizeof(GLfloat));
+      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 > (GLint) rb->Width) {
+      GLint dx = x + n - (GLint) rb->Width;
+      GLint i;
+      for (i = 0; i < dx; i++)
+         depth[n - i - 1] = 0;
+      n -= dx;
+   }
+   if (n <= 0) {
+      return;
+   }
+
+   if (rb->DataType == GL_UNSIGNED_INT) {
+      rb->GetRow(ctx, rb, n, x, y, depth);
+      if (rb->DepthBits < 32) {
+         GLuint shift = 32 - rb->DepthBits;
+         GLint i;
+         for (i = 0; i < n; i++) {
+            GLuint z = depth[i];
+            depth[i] = z << shift; /* XXX lsb bits? */
+         }
+      }
+   }
+   else if (rb->DataType == GL_UNSIGNED_SHORT) {
+      GLushort temp[MAX_WIDTH];
+      GLint i;
+      rb->GetRow(ctx, rb, n, x, y, temp);
+      if (rb->DepthBits == 16) {
+         for (i = 0; i < n; i++) {
+            GLuint z = temp[i];
+            depth[i] = (z << 16) | z;
+         }
+      }
+      else {
+         GLuint shift = 16 - rb->DepthBits;
+         for (i = 0; i < n; i++) {
+            GLuint z = temp[i];
+            depth[i] = (z << (shift + 16)) | (z << shift); /* XXX lsb bits? */
+         }
       }
    }
    else {
-      _mesa_problem(ctx, "Invalid depth renderbuffer internal format");
+      _mesa_problem(ctx, "Invalid depth renderbuffer data type");
    }
 }
 
 
+
 /**
  * Clear the given z/depth renderbuffer.
  */