X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fmesa%2Fstate_tracker%2Fst_cb_readpixels.c;h=9ce5f3fe84cb8cece4198c5b7702135306d1d50b;hb=e53d6ab39bf04b2bb39ad23d5990494321ee77ce;hp=08934af3195291fa2aecc2870f56b3ff0ce69ecf;hpb=019fc3aa04f11d61e6d0f4791c171d1790577ab5;p=mesa.git diff --git a/src/mesa/state_tracker/st_cb_readpixels.c b/src/mesa/state_tracker/st_cb_readpixels.c index 08934af3195..9ce5f3fe84c 100644 --- a/src/mesa/state_tracker/st_cb_readpixels.c +++ b/src/mesa/state_tracker/st_cb_readpixels.c @@ -41,7 +41,7 @@ #include "pipe/p_context.h" #include "pipe/p_defines.h" #include "pipe/p_inlines.h" -#include "util/p_tile.h" +#include "util/u_tile.h" #include "st_context.h" #include "st_cb_bitmap.h" #include "st_cb_readpixels.h" @@ -56,63 +56,87 @@ */ 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->pitch + 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->pitch + x; + 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++) { + 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++) { - values[k] = src[k] >> 24; + sValues[k] = src[k] >> 24; } } break; case PIPE_FORMAT_Z24S8_UNORM: - { - const uint *src = (uint *) stmap + srcY * ps->pitch + x; + 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); } @@ -163,7 +191,130 @@ st_get_color_read_renderbuffer(GLcontext *ctx) /** - * Do glReadPixels by getting rows from the framebuffer surface with + * Try to do glReadPixels in a fast manner for common cases. + * \return GL_TRUE for success, GL_FALSE for failure + */ +static GLboolean +st_fast_readpixels(GLcontext *ctx, struct st_renderbuffer *strb, + GLint x, GLint y, GLsizei width, GLsizei height, + GLenum format, GLenum type, + const struct gl_pixelstore_attrib *pack, + GLvoid *dest) +{ + enum combination { + A8R8G8B8_UNORM_TO_RGBA_UBYTE, + A8R8G8B8_UNORM_TO_RGB_UBYTE, + A8R8G8B8_UNORM_TO_BGRA_UINT + } combo; + + if (ctx->_ImageTransferState) + return GL_FALSE; + + if (strb->format == PIPE_FORMAT_A8R8G8B8_UNORM && + format == GL_RGBA && type == GL_UNSIGNED_BYTE) { + combo = A8R8G8B8_UNORM_TO_RGBA_UBYTE; + } + else if (strb->format == PIPE_FORMAT_A8R8G8B8_UNORM && + format == GL_RGB && type == GL_UNSIGNED_BYTE) { + combo = A8R8G8B8_UNORM_TO_RGB_UBYTE; + } + else if (strb->format == PIPE_FORMAT_A8R8G8B8_UNORM && + format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8_REV) { + combo = A8R8G8B8_UNORM_TO_BGRA_UINT; + } + else { + return GL_FALSE; + } + + /*printf("st_fast_readpixels combo %d\n", (GLint) combo);*/ + + { + struct pipe_context *pipe = ctx->st->pipe; + struct pipe_screen *screen = pipe->screen; + struct pipe_transfer *trans; + const GLubyte *map; + GLubyte *dst; + GLint row, col, dy, dstStride; + + 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->transfer_map(screen, trans); + if (!map) { + screen->tex_transfer_destroy(trans); + return GL_FALSE; + } + + if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { + y = height - 1; + dy = -1; + } + else { + y = 0; + dy = 1; + } + + dst = _mesa_image_address2d(pack, dest, width, height, + format, type, 0, 0); + dstStride = _mesa_image_row_stride(pack, width, format, type); + + switch (combo) { + case A8R8G8B8_UNORM_TO_RGBA_UBYTE: + for (row = 0; row < height; row++) { + 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; + dst[col*4+1] = (pixel >> 8) & 0xff; + dst[col*4+2] = (pixel >> 0) & 0xff; + dst[col*4+3] = (pixel >> 24) & 0xff; + } + dst += dstStride; + y += dy; + } + break; + case A8R8G8B8_UNORM_TO_RGB_UBYTE: + for (row = 0; row < height; row++) { + 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; + dst[col*3+1] = (pixel >> 8) & 0xff; + dst[col*3+2] = (pixel >> 0) & 0xff; + } + dst += dstStride; + y += dy; + } + break; + case A8R8G8B8_UNORM_TO_BGRA_UINT: + for (row = 0; row < height; row++) { + const GLubyte *src = map + y * trans->stride; + memcpy(dst, src, 4 * width); + dst += dstStride; + y += dy; + } + break; + default: + ; /* nothing */ + } + + screen->transfer_unmap(screen, trans); + screen->tex_transfer_destroy(trans); + } + + return GL_TRUE; +} + + +/** + * 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. */ @@ -177,11 +328,12 @@ st_readpixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height, struct pipe_screen *screen = pipe->screen; GLfloat temp[MAX_WIDTH][4]; const GLbitfield transferOps = ctx->_ImageTransferState; - GLint i, yStep, dfStride; + GLsizei i, j; + GLint yStep, dfStride; 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); @@ -190,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; } @@ -198,13 +350,13 @@ st_readpixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height, if (!dest) return; - st_flush_bitmap_cache(ctx->st); - /* make sure rendering has completed */ - pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL); + 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) { @@ -218,6 +370,14 @@ st_readpixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height, if (!strb) return; + /* try a fast-path readpixels before anything else */ + if (st_fast_readpixels(ctx, strb, x, y, width, height, + format, type, pack, dest)) { + /* success! */ + _mesa_unmap_readpix_pbo(ctx, &clippedPacking); + return; + } + if (format == GL_RGBA && type == GL_FLOAT) { /* write tile(row) directly into user's buffer */ df = (GLfloat *) _mesa_image_address2d(&clippedPacking, dest, width, @@ -230,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 */ @@ -254,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], j; + GLuint ztemp[MAX_WIDTH]; GLfloat zfloat[MAX_WIDTH]; const double scale = 1.0 / ((1 << 24) - 1); - pipe_get_tile_raw(pipe, 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)); @@ -275,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(pipe, 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], j; + GLushort ztemp[MAX_WIDTH]; GLfloat zfloat[MAX_WIDTH]; const double scale = 1.0 / 0xffff; - pipe_get_tile_raw(pipe, 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]); @@ -296,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], j; + GLuint ztemp[MAX_WIDTH]; GLfloat zfloat[MAX_WIDTH]; const double scale = 1.0 / 0xffffffff; - pipe_get_tile_raw(pipe, 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]); @@ -315,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(pipe, surf, x, y, width, 1, df); + pipe_get_tile_rgba(trans, 0, y, width, 1, df); y += yStep; df += dfStride; if (!dfStride) { @@ -327,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); }