Don't cast the return value of malloc/realloc
[mesa.git] / src / mesa / swrast / s_depth.c
index 806e62b5ea0cf9287012933b27c8f322c5c1471a..969b75f07a62c02eafe528a31bd2414ea8f357bf 100644 (file)
@@ -31,6 +31,7 @@
 #include "main/macros.h"
 #include "main/imports.h"
 
+#include "s_context.h"
 #include "s_depth.h"
 #include "s_span.h"
 
@@ -204,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));
@@ -224,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;
@@ -234,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);
@@ -255,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;
@@ -266,18 +275,6 @@ put_z32_values(struct gl_context *ctx, struct gl_renderbuffer *rb,
 }
 
 
-/**
- * Return the address of a Z value in a renderbuffer.
- */
-static INLINE void *
-get_z_address(struct gl_renderbuffer *rb, GLint x, GLint y)
-{
-   const GLint bpp = _mesa_get_format_bytes(rb->Format);
-   const GLint rowStride = rb->RowStride * bpp;
-   return (GLubyte *) rb->Data + y * rowStride + x * bpp;
-}
-
-
 /**
  * Apply depth (Z) buffer testing to the span.
  * \return approx number of pixels that passed (only zero is reliable)
@@ -288,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 = get_z_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 */
@@ -309,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;
 
@@ -321,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.
@@ -330,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;
    }
@@ -343,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 */
@@ -394,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;
@@ -432,6 +459,8 @@ _swrast_depth_bounds_test( struct gl_context *ctx, SWspan *span )
       }
    }
 
+   free(zBufferTemp);
+
    return anyPass;
 }
 
@@ -486,7 +515,8 @@ _swrast_read_depth_span_float(struct gl_context *ctx,
       return;
    }
 
-   _mesa_unpack_float_z_row(rb->Format, n, get_z_address(rb, x, y), depth);
+   _mesa_unpack_float_z_row(rb->Format, n, _swrast_pixel_address(rb, x, y),
+                            depth);
 }
 
 
@@ -499,7 +529,6 @@ _swrast_clear_depth_buffer(struct gl_context *ctx)
 {
    struct gl_renderbuffer *rb =
       ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
-   GLuint clearValue;
    GLint x, y, width, height;
    GLubyte *map;
    GLint rowStride, i, j;
@@ -510,14 +539,6 @@ _swrast_clear_depth_buffer(struct gl_context *ctx)
       return;
    }
 
-   /* compute integer clearing value */
-   if (ctx->Depth.Clear == 1.0) {
-      clearValue = ctx->DrawBuffer->_DepthMax;
-   }
-   else {
-      clearValue = (GLuint) (ctx->Depth.Clear * ctx->DrawBuffer->_DepthMaxF);
-   }
-
    /* compute region to clear */
    x = ctx->DrawBuffer->_Xmin;
    y = ctx->DrawBuffer->_Ymin;