radeon: Fix memory leak in radeonCreateScreen2.
[mesa.git] / src / mesa / swrast / s_depth.c
index 53f21cb698e378199ee2384471630c798023bcd5..969b75f07a62c02eafe528a31bd2414ea8f357bf 100644 (file)
@@ -205,18 +205,20 @@ _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,
                GLuint count, const GLint x[], const GLint y[],
                GLuint zbuffer[])
 {
+   struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
    const GLint w = rb->Width, h = rb->Height;
-   const GLubyte *map = (const GLubyte *) rb->Data;
+   const GLubyte *map = _swrast_pixel_address(rb, 0, 0);
    GLuint i;
 
    if (rb->Format == MESA_FORMAT_Z32) {
-      const GLint rowStride = rb->RowStride * 4;
+      const GLint rowStride = srb->RowStride;
       for (i = 0; i < count; i++) {
          if (x[i] >= 0 && y[i] >= 0 && x[i] < w && y[i] < h) {
             zbuffer[i] = *((GLuint *) (map + y[i] * rowStride + x[i] * 4));
@@ -225,7 +227,7 @@ get_z32_values(struct gl_context *ctx, struct gl_renderbuffer *rb,
    }
    else {
       const GLint bpp = _mesa_get_format_bytes(rb->Format);
-      const GLint rowStride = rb->RowStride * bpp;
+      const GLint rowStride = srb->RowStride;
       for (i = 0; i < count; i++) {
          if (x[i] >= 0 && y[i] >= 0 && x[i] < w && y[i] < h) {
             const GLubyte *src = map + y[i] * rowStride+ x[i] * bpp;
@@ -235,17 +237,23 @@ 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[],
                const GLuint zvalues[], const GLubyte mask[])
 {
+   struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
    const GLint w = rb->Width, h = rb->Height;
-   GLubyte *map = (GLubyte *) rb->Data;
+   GLubyte *map = _swrast_pixel_address(rb, 0, 0);
    GLuint i;
 
    if (rb->Format == MESA_FORMAT_Z32) {
-      const GLuint rowStride = rb->RowStride * 4;
+      const GLint rowStride = srb->RowStride;
       for (i = 0; i < count; i++) {
          if (mask[i] && x[i] >= 0 && y[i] >= 0 && x[i] < w && y[i] < h) {
             GLuint *dst = (GLuint *) (map + y[i] * rowStride + x[i] * 4);
@@ -256,7 +264,7 @@ put_z32_values(struct gl_context *ctx, struct gl_renderbuffer *rb,
    else {
       gl_pack_uint_z_func packZ = _mesa_get_pack_uint_z_func(rb->Format);
       const GLint bpp = _mesa_get_format_bytes(rb->Format);
-      const GLint rowStride = rb->RowStride * bpp;
+      const GLint rowStride = srb->RowStride;
       for (i = 0; i < count; i++) {
          if (mask[i] && x[i] >= 0 && y[i] >= 0 && x[i] < w && y[i] < h) {
             void *dst = map + y[i] * rowStride + x[i] * bpp;
@@ -277,15 +285,20 @@ _swrast_depth_test_span(struct gl_context *ctx, SWspan *span)
    struct gl_framebuffer *fb = ctx->DrawBuffer;
    struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
    const GLint bpp = _mesa_get_format_bytes(rb->Format);
-   void *zStart = _swrast_pixel_address(rb, span->x, span->y);
+   void *zStart;
    const GLuint count = span->end;
    const GLuint *fragZ = span->array->z;
    GLubyte *mask = span->array->mask;
    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 (span->arrayMask & SPAN_XY)
+      zStart = NULL;
+   else
+      zStart = _swrast_pixel_address(rb, span->x, span->y);
 
    if (rb->Format == MESA_FORMAT_Z16 && !(span->arrayMask & SPAN_XY)) {
       /* directly read/write row of 16-bit Z values */
@@ -298,7 +311,7 @@ _swrast_depth_test_span(struct gl_context *ctx, SWspan *span)
    }
    else {
       /* copy Z buffer values into temp buffer (32-bit Z values) */
-      zBufferTemp = (GLuint *) malloc(count * sizeof(GLuint));
+      zBufferTemp = malloc(count * sizeof(GLuint));
       if (!zBufferTemp)
          return 0;
 
@@ -310,7 +323,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 +332,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 +355,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 */
@@ -383,18 +412,27 @@ _swrast_depth_bounds_test( struct gl_context *ctx, SWspan *span )
 {
    struct gl_framebuffer *fb = ctx->DrawBuffer;
    struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
-   const GLint bpp = _mesa_get_format_bytes(rb->Format);
-   const GLint rowStride = rb->RowStride * bpp;
-   GLubyte *zStart = (GLubyte*) rb->Data + span->y * rowStride + span->x * bpp;
+   GLubyte *zStart;
    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;
    const GLuint count = span->end;
    GLuint i;
    GLboolean anyPass = GL_FALSE;
-   GLuint zBufferTemp[MAX_WIDTH];
+   GLuint *zBufferTemp;
    const GLuint *zBufferVals;
 
+   zBufferTemp = malloc(count * sizeof(GLuint));
+   if (!zBufferTemp) {
+      /* don't generate a stream of OUT_OF_MEMORY errors here */
+      return GL_FALSE;
+   }
+
+   if (span->arrayMask & SPAN_XY)
+      zStart = NULL;
+   else
+      zStart = _swrast_pixel_address(rb, span->x, span->y);
+
    if (rb->Format == MESA_FORMAT_Z32 && !(span->arrayMask & SPAN_XY)) {
       /* directly access 32-bit values in the depth buffer */
       zBufferVals = (const GLuint *) zStart;
@@ -421,6 +459,8 @@ _swrast_depth_bounds_test( struct gl_context *ctx, SWspan *span )
       }
    }
 
+   free(zBufferTemp);
+
    return anyPass;
 }