st: rearrange some code to be a little more clear
[mesa.git] / src / mesa / state_tracker / st_cb_readpixels.c
index 646eaff190381dfe755b96cc469a2ae07a4d49a4..9ce5f3fe84cb8cece4198c5b7702135306d1d50b 100644 (file)
  */
 void
 st_read_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
-                       GLsizei width, GLsizei height, GLenum type,
+                       GLsizei width, GLsizei height,
+                       GLenum format, GLenum type,
                        const struct gl_pixelstore_attrib *packing,
                        GLvoid *pixels)
 {
    struct gl_framebuffer *fb = ctx->ReadBuffer;
    struct pipe_screen *screen = ctx->st->pipe->screen;
    struct st_renderbuffer *strb = st_renderbuffer(fb->_StencilBuffer);
-   struct pipe_surface *ps;
+   struct pipe_transfer *pt;
    ubyte *stmap;
    GLint j;
 
-   /* Create a CPU-READ surface/view into the renderbuffer's texture */
-   ps = screen->get_tex_surface(screen, strb->texture,  0, 0, 0,
-                                PIPE_BUFFER_USAGE_CPU_READ);
+   if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
+      y = ctx->DrawBuffer->Height - y - height;
+   }
+
+   /* Create a read transfer from the renderbuffer's texture */
+   pt = screen->get_tex_transfer(screen, strb->texture,  0, 0, 0,
+                                 PIPE_TRANSFER_READ, x, y, width, height);
 
    /* map the stencil buffer */
-   stmap = screen->surface_map(screen, ps, PIPE_BUFFER_USAGE_CPU_READ);
+   stmap = screen->transfer_map(screen, pt);
 
    /* width should never be > MAX_WIDTH since we did clipping earlier */
    ASSERT(width <= MAX_WIDTH);
 
    /* process image row by row */
-   for (j = 0; j < height; j++, y++) {
+   for (j = 0; j < height; j++) {
       GLvoid *dest;
-      GLstencil values[MAX_WIDTH];
+      GLstencil sValues[MAX_WIDTH];
+      GLfloat zValues[MAX_WIDTH];
       GLint srcY;
 
       if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
-         srcY = ctx->DrawBuffer->Height - y - 1;
+         srcY = height - j - 1;
       }
       else {
-         srcY = y;
+         srcY = j;
       }
 
-      /* get stencil values */
-      switch (ps->format) {
+      /* get stencil (and Z) values */
+      switch (pt->format) {
       case PIPE_FORMAT_S8_UNORM:
          {
-            const ubyte *src = stmap + srcY * ps->stride + x;
-            memcpy(values, src, width);
+            const ubyte *src = stmap + srcY * pt->stride;
+            memcpy(sValues, src, width);
          }
          break;
       case PIPE_FORMAT_S8Z24_UNORM:
-         {
-            const uint *src = (uint *) (stmap + srcY * ps->stride + x*4);
+         if (format == GL_DEPTH_STENCIL) {
+            const uint *src = (uint *) (stmap + srcY * pt->stride);
+            const GLfloat scale = 1.0 / (0xffffff);
             GLint k;
             for (k = 0; k < width; k++) {
-               values[k] = src[k] >> 24;
+               sValues[k] = src[k] >> 24;
+               zValues[k] = (src[k] & 0xffffff) * scale;
+            }
+         }
+         else {
+            const uint *src = (uint *) (stmap + srcY * pt->stride);
+            GLint k;
+            for (k = 0; k < width; k++) {
+               sValues[k] = src[k] >> 24;
             }
          }
          break;
       case PIPE_FORMAT_Z24S8_UNORM:
-         {
-            const uint *src = (uint *) (stmap + srcY * ps->stride + x*4);
+         if (format == GL_DEPTH_STENCIL) {
+            const uint *src = (uint *) (stmap + srcY * pt->stride);
+            const GLfloat scale = 1.0 / (0xffffff);
             GLint k;
             for (k = 0; k < width; k++) {
-               values[k] = src[k] & 0xff;
+               sValues[k] = src[k] & 0xff;
+               zValues[k] = (src[k] >> 8) * scale;
+            }
+         }
+         else {
+            const uint *src = (uint *) (stmap + srcY * pt->stride);
+            GLint k;
+            for (k = 0; k < width; k++) {
+               sValues[k] = src[k] & 0xff;
             }
          }
          break;
@@ -122,15 +146,19 @@ st_read_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
 
       /* store */
       dest = _mesa_image_address2d(packing, pixels, width, height,
-                                   GL_STENCIL_INDEX, type, j, 0);
-
-      _mesa_pack_stencil_span(ctx, width, type, dest, values, packing);
+                                   format, type, j, 0);
+      if (format == GL_DEPTH_STENCIL) {
+         _mesa_pack_depth_stencil_span(ctx, width, dest,
+                                       zValues, sValues, packing);
+      }
+      else {
+         _mesa_pack_stencil_span(ctx, width, type, dest, sValues, packing);
+      }
    }
 
-
    /* unmap the stencil buffer */
-   screen->surface_unmap(screen, ps);
-   pipe_surface_reference(&ps, NULL);
+   screen->transfer_unmap(screen, pt);
+   screen->tex_transfer_destroy(pt);
 }
 
 
@@ -203,28 +231,33 @@ st_fast_readpixels(GLcontext *ctx, struct st_renderbuffer *strb,
    {
       struct pipe_context *pipe = ctx->st->pipe;
       struct pipe_screen *screen = pipe->screen;
-      struct pipe_surface *surf;
+      struct pipe_transfer *trans;
       const GLubyte *map;
       GLubyte *dst;
       GLint row, col, dy, dstStride;
 
-      surf = screen->get_tex_surface(screen, strb->texture,  0, 0, 0,
-                                     PIPE_BUFFER_USAGE_CPU_READ);
-      if (!surf) {
+      if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
+         y = strb->texture->height[0] - y - height;
+      }
+
+      trans = screen->get_tex_transfer(screen, strb->texture, 0, 0, 0,
+                                       PIPE_TRANSFER_READ, x, y, width, height);
+      if (!trans) {
          return GL_FALSE;
       }
 
-      map = screen->surface_map(screen, surf, PIPE_BUFFER_USAGE_CPU_READ);
+      map = screen->transfer_map(screen, trans);
       if (!map) {
-         pipe_surface_reference(&surf, NULL);
+         screen->tex_transfer_destroy(trans);
          return GL_FALSE;
       }
 
       if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
-         y = surf->height - y - 1;
+         y = height - 1;
          dy = -1;
       }
       else {
+         y = 0;
          dy = 1;
       }
 
@@ -235,7 +268,7 @@ st_fast_readpixels(GLcontext *ctx, struct st_renderbuffer *strb,
       switch (combo) {
       case A8R8G8B8_UNORM_TO_RGBA_UBYTE:
          for (row = 0; row < height; row++) {
-            const GLubyte *src = map + y * surf->stride + x * 4;
+            const GLubyte *src = map + y * trans->stride;
             for (col = 0; col < width; col++) {
                GLuint pixel = ((GLuint *) src)[col];
                dst[col*4+0] = (pixel >> 16) & 0xff;
@@ -249,7 +282,7 @@ st_fast_readpixels(GLcontext *ctx, struct st_renderbuffer *strb,
          break;
       case A8R8G8B8_UNORM_TO_RGB_UBYTE:
          for (row = 0; row < height; row++) {
-            const GLubyte *src = map + y * surf->stride + x * 4;
+            const GLubyte *src = map + y * trans->stride;
             for (col = 0; col < width; col++) {
                GLuint pixel = ((GLuint *) src)[col];
                dst[col*3+0] = (pixel >> 16) & 0xff;
@@ -262,7 +295,7 @@ st_fast_readpixels(GLcontext *ctx, struct st_renderbuffer *strb,
          break;
       case A8R8G8B8_UNORM_TO_BGRA_UINT:
          for (row = 0; row < height; row++) {
-            const GLubyte *src = map + y * surf->stride + x * 4;
+            const GLubyte *src = map + y * trans->stride;
             memcpy(dst, src, 4 * width);
             dst += dstStride;
             y += dy;
@@ -272,8 +305,8 @@ st_fast_readpixels(GLcontext *ctx, struct st_renderbuffer *strb,
          ; /* nothing */
       }
 
-      screen->surface_unmap(screen, surf);
-      pipe_surface_reference(&surf, NULL);
+      screen->transfer_unmap(screen, trans);
+      screen->tex_transfer_destroy(trans);
    }
 
    return GL_TRUE;
@@ -281,7 +314,7 @@ st_fast_readpixels(GLcontext *ctx, struct st_renderbuffer *strb,
 
 
 /**
- * Do glReadPixels by getting rows from the framebuffer surface with
+ * Do glReadPixels by getting rows from the framebuffer transfer with
  * get_tile().  Convert to requested format/type with Mesa image routines.
  * Image transfer ops are done in software too.
  */
@@ -300,7 +333,7 @@ st_readpixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
    GLfloat *df;
    struct st_renderbuffer *strb;
    struct gl_pixelstore_attrib clippedPacking = *pack;
-   struct pipe_surface *surf;
+   struct pipe_transfer *trans;
 
    assert(ctx->ReadBuffer->Width > 0);
 
@@ -309,7 +342,7 @@ st_readpixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
 
    /* Do all needed clipping here, so that we can forget about it later */
    if (!_mesa_clip_readpixels(ctx, &x, &y, &width, &height, &clippedPacking)) {
-      /* The ReadPixels surface is totally outside the window bounds */
+      /* The ReadPixels transfer is totally outside the window bounds */
       return;
    }
 
@@ -320,8 +353,10 @@ st_readpixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
    /* make sure rendering has completed */
    st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
 
-   if (format == GL_STENCIL_INDEX) {
-      st_read_stencil_pixels(ctx, x, y, width, height, type, pack, dest);
+   if (format == GL_STENCIL_INDEX ||
+       format == GL_DEPTH_STENCIL) {
+      st_read_stencil_pixels(ctx, x, y, width, height,
+                             format, type, pack, dest);
       return;
    }
    else if (format == GL_DEPTH_COMPONENT) {
@@ -355,21 +390,26 @@ st_readpixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
       dfStride = 0;
    }
 
+   if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
+      y = strb->Base.Height - y - height;
+   }
+
+   /* Create a read transfer from the renderbuffer's texture */
+   trans = screen->get_tex_transfer(screen, strb->texture,  0, 0, 0,
+                                    PIPE_TRANSFER_READ, x, y, width, height);
+
    /* determine bottom-to-top vs. top-to-bottom order */
    if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
-      y = strb->Base.Height - 1 - y;
+      y = height - 1;
       yStep = -1;
    }
    else {
+      y = 0;
       yStep = 1;
    }
 
-   /* Create a CPU-READ surface/view into the renderbuffer's texture */
-   surf = screen->get_tex_surface(screen, strb->texture,  0, 0, 0,
-                                  PIPE_BUFFER_USAGE_CPU_READ);
-
    /*
-    * Copy pixels from pipe_surface to user memory
+    * Copy pixels from pipe_transfer to user memory
     */
    {
       /* dest of first pixel in client memory */
@@ -379,14 +419,14 @@ st_readpixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
       const GLint dstStride = _mesa_image_row_stride(&clippedPacking, width,
                                                      format, type);
 
-      if (surf->format == PIPE_FORMAT_S8Z24_UNORM ||
-          surf->format == PIPE_FORMAT_X8Z24_UNORM) {
+      if (trans->format == PIPE_FORMAT_S8Z24_UNORM ||
+          trans->format == PIPE_FORMAT_X8Z24_UNORM) {
          if (format == GL_DEPTH_COMPONENT) {
             for (i = 0; i < height; i++) {
                GLuint ztemp[MAX_WIDTH];
                GLfloat zfloat[MAX_WIDTH];
                const double scale = 1.0 / ((1 << 24) - 1);
-               pipe_get_tile_raw(surf, x, y, width, 1, ztemp, 0);
+               pipe_get_tile_raw(trans, 0, y, width, 1, ztemp, 0);
                y += yStep;
                for (j = 0; j < width; j++) {
                   zfloat[j] = (float) (scale * (ztemp[j] & 0xffffff));
@@ -400,18 +440,45 @@ st_readpixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
             /* untested, but simple: */
             assert(format == GL_DEPTH_STENCIL_EXT);
             for (i = 0; i < height; i++) {
-               pipe_get_tile_raw(surf, x, y, width, 1, dst, 0);
+               pipe_get_tile_raw(trans, 0, y, width, 1, dst, 0);
+               y += yStep;
+               dst += dstStride;
+            }
+         }
+      }
+      else if (trans->format == PIPE_FORMAT_Z24S8_UNORM ||
+               trans->format == PIPE_FORMAT_Z24X8_UNORM) {
+         if (format == GL_DEPTH_COMPONENT) {
+            for (i = 0; i < height; i++) {
+               GLuint ztemp[MAX_WIDTH];
+               GLfloat zfloat[MAX_WIDTH];
+               const double scale = 1.0 / ((1 << 24) - 1);
+               pipe_get_tile_raw(trans, 0, y, width, 1, ztemp, 0);
+               y += yStep;
+               for (j = 0; j < width; j++) {
+                  zfloat[j] = (float) (scale * ((ztemp[j] >> 8) & 0xffffff));
+               }
+               _mesa_pack_depth_span(ctx, width, dst, type,
+                                     zfloat, &clippedPacking);
+               dst += dstStride;
+            }
+         }
+         else {
+            /* untested, but simple: */
+            assert(format == GL_DEPTH_STENCIL_EXT);
+            for (i = 0; i < height; i++) {
+               pipe_get_tile_raw(trans, 0, y, width, 1, dst, 0);
                y += yStep;
                dst += dstStride;
             }
          }
       }
-      else if (surf->format == PIPE_FORMAT_Z16_UNORM) {
+      else if (trans->format == PIPE_FORMAT_Z16_UNORM) {
          for (i = 0; i < height; i++) {
             GLushort ztemp[MAX_WIDTH];
             GLfloat zfloat[MAX_WIDTH];
             const double scale = 1.0 / 0xffff;
-            pipe_get_tile_raw(surf, x, y, width, 1, ztemp, 0);
+            pipe_get_tile_raw(trans, 0, y, width, 1, ztemp, 0);
             y += yStep;
             for (j = 0; j < width; j++) {
                zfloat[j] = (float) (scale * ztemp[j]);
@@ -421,12 +488,12 @@ st_readpixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
             dst += dstStride;
          }
       }
-      else if (surf->format == PIPE_FORMAT_Z32_UNORM) {
+      else if (trans->format == PIPE_FORMAT_Z32_UNORM) {
          for (i = 0; i < height; i++) {
             GLuint ztemp[MAX_WIDTH];
             GLfloat zfloat[MAX_WIDTH];
             const double scale = 1.0 / 0xffffffff;
-            pipe_get_tile_raw(surf, x, y, width, 1, ztemp, 0);
+            pipe_get_tile_raw(trans, 0, y, width, 1, ztemp, 0);
             y += yStep;
             for (j = 0; j < width; j++) {
                zfloat[j] = (float) (scale * ztemp[j]);
@@ -440,7 +507,7 @@ st_readpixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
          /* RGBA format */
          /* Do a row at a time to flip image data vertically */
          for (i = 0; i < height; i++) {
-            pipe_get_tile_rgba(surf, x, y, width, 1, df);
+            pipe_get_tile_rgba(trans, 0, y, width, 1, df);
             y += yStep;
             df += dfStride;
             if (!dfStride) {
@@ -452,7 +519,7 @@ st_readpixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
       }
    }
 
-   pipe_surface_reference(&surf, NULL);
+   screen->tex_transfer_destroy(trans);
 
    _mesa_unmap_readpix_pbo(ctx, &clippedPacking);
 }