1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * glReadPixels interface to pipe
36 #include "main/imports.h"
37 #include "main/bufferobj.h"
38 #include "main/context.h"
39 #include "main/image.h"
41 #include "pipe/p_context.h"
42 #include "pipe/p_defines.h"
43 #include "util/u_inlines.h"
44 #include "util/u_tile.h"
47 #include "st_context.h"
48 #include "st_cb_readpixels.h"
49 #include "st_cb_fbo.h"
50 #include "st_public.h"
51 #include "st_texture.h"
52 #include "st_inlines.h"
55 * Special case for reading stencil buffer.
56 * For color/depth we use get_tile(). For stencil, map the stencil buffer.
59 st_read_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
60 GLsizei width
, GLsizei height
,
61 GLenum format
, GLenum type
,
62 const struct gl_pixelstore_attrib
*packing
,
65 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
66 struct pipe_context
*pipe
= ctx
->st
->pipe
;
67 struct st_renderbuffer
*strb
= st_renderbuffer(fb
->_StencilBuffer
);
68 struct pipe_transfer
*pt
;
72 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
73 y
= ctx
->DrawBuffer
->Height
- y
- height
;
76 /* Create a read transfer from the renderbuffer's texture */
78 pt
= st_cond_flush_get_tex_transfer(st_context(ctx
), strb
->texture
,
80 PIPE_TRANSFER_READ
, x
, y
,
83 /* map the stencil buffer */
84 stmap
= pipe
->transfer_map(pipe
, pt
);
86 /* width should never be > MAX_WIDTH since we did clipping earlier */
87 ASSERT(width
<= MAX_WIDTH
);
89 /* process image row by row */
90 for (j
= 0; j
< height
; j
++) {
92 GLstencil sValues
[MAX_WIDTH
];
93 GLfloat zValues
[MAX_WIDTH
];
96 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
97 srcY
= height
- j
- 1;
103 /* get stencil (and Z) values */
104 switch (pt
->texture
->format
) {
105 case PIPE_FORMAT_S8_UNORM
:
107 const ubyte
*src
= stmap
+ srcY
* pt
->stride
;
108 memcpy(sValues
, src
, width
);
111 case PIPE_FORMAT_Z24S8_UNORM
:
112 if (format
== GL_DEPTH_STENCIL
) {
113 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
114 const GLfloat scale
= 1.0f
/ (0xffffff);
116 for (k
= 0; k
< width
; k
++) {
117 sValues
[k
] = src
[k
] >> 24;
118 zValues
[k
] = (src
[k
] & 0xffffff) * scale
;
122 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
124 for (k
= 0; k
< width
; k
++) {
125 sValues
[k
] = src
[k
] >> 24;
129 case PIPE_FORMAT_S8Z24_UNORM
:
130 if (format
== GL_DEPTH_STENCIL
) {
131 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
132 const GLfloat scale
= 1.0f
/ (0xffffff);
134 for (k
= 0; k
< width
; k
++) {
135 sValues
[k
] = src
[k
] & 0xff;
136 zValues
[k
] = (src
[k
] >> 8) * scale
;
140 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
142 for (k
= 0; k
< width
; k
++) {
143 sValues
[k
] = src
[k
] & 0xff;
152 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
154 if (format
== GL_DEPTH_STENCIL
) {
155 _mesa_pack_depth_stencil_span(ctx
, width
, dest
,
156 zValues
, sValues
, packing
);
159 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, sValues
, packing
);
163 /* unmap the stencil buffer */
164 pipe
->transfer_unmap(pipe
, pt
);
165 pipe
->tex_transfer_destroy(pipe
, pt
);
170 * Return renderbuffer to use for reading color pixels for glRead/CopyPixel
172 * Special care is needed for the front buffer.
174 struct st_renderbuffer
*
175 st_get_color_read_renderbuffer(GLcontext
*ctx
)
177 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
178 struct st_renderbuffer
*strb
=
179 st_renderbuffer(fb
->_ColorReadBuffer
);
180 struct st_renderbuffer
*front
=
181 st_renderbuffer(fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
184 && ctx
->st
->frontbuffer_status
== FRONT_STATUS_COPY_OF_BACK
) {
185 /* reading from front color buffer, which is a logical copy of the
188 struct st_renderbuffer
*back
=
189 st_renderbuffer(fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
198 * Try to do glReadPixels in a fast manner for common cases.
199 * \return GL_TRUE for success, GL_FALSE for failure
202 st_fast_readpixels(GLcontext
*ctx
, struct st_renderbuffer
*strb
,
203 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
204 GLenum format
, GLenum type
,
205 const struct gl_pixelstore_attrib
*pack
,
209 A8R8G8B8_UNORM_TO_RGBA_UBYTE
,
210 A8R8G8B8_UNORM_TO_RGB_UBYTE
,
211 A8R8G8B8_UNORM_TO_BGRA_UINT
214 if (ctx
->_ImageTransferState
)
217 if (strb
->format
== PIPE_FORMAT_B8G8R8A8_UNORM
&&
218 format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
219 combo
= A8R8G8B8_UNORM_TO_RGBA_UBYTE
;
221 else if (strb
->format
== PIPE_FORMAT_B8G8R8A8_UNORM
&&
222 format
== GL_RGB
&& type
== GL_UNSIGNED_BYTE
) {
223 combo
= A8R8G8B8_UNORM_TO_RGB_UBYTE
;
225 else if (strb
->format
== PIPE_FORMAT_B8G8R8A8_UNORM
&&
226 format
== GL_BGRA
&& type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
227 combo
= A8R8G8B8_UNORM_TO_BGRA_UINT
;
233 /*printf("st_fast_readpixels combo %d\n", (GLint) combo);*/
236 struct pipe_context
*pipe
= ctx
->st
->pipe
;
237 struct pipe_transfer
*trans
;
240 GLint row
, col
, dy
, dstStride
;
242 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
243 /* convert GL Y to Gallium Y */
244 y
= strb
->texture
->height0
- y
- height
;
247 trans
= st_cond_flush_get_tex_transfer(st_context(ctx
), strb
->texture
,
249 PIPE_TRANSFER_READ
, x
, y
,
255 map
= pipe
->transfer_map(pipe
, trans
);
257 pipe
->tex_transfer_destroy(pipe
, trans
);
261 /* We always write to the user/dest buffer from low addr to high addr
262 * but the read order depends on renderbuffer orientation
264 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
265 /* read source rows from bottom to top */
270 /* read source rows from top to bottom */
275 dst
= _mesa_image_address2d(pack
, dest
, width
, height
,
277 dstStride
= _mesa_image_row_stride(pack
, width
, format
, type
);
280 case A8R8G8B8_UNORM_TO_RGBA_UBYTE
:
281 for (row
= 0; row
< height
; row
++) {
282 const GLubyte
*src
= map
+ y
* trans
->stride
;
283 for (col
= 0; col
< width
; col
++) {
284 GLuint pixel
= ((GLuint
*) src
)[col
];
285 dst
[col
*4+0] = (pixel
>> 16) & 0xff;
286 dst
[col
*4+1] = (pixel
>> 8) & 0xff;
287 dst
[col
*4+2] = (pixel
>> 0) & 0xff;
288 dst
[col
*4+3] = (pixel
>> 24) & 0xff;
294 case A8R8G8B8_UNORM_TO_RGB_UBYTE
:
295 for (row
= 0; row
< height
; row
++) {
296 const GLubyte
*src
= map
+ y
* trans
->stride
;
297 for (col
= 0; col
< width
; col
++) {
298 GLuint pixel
= ((GLuint
*) src
)[col
];
299 dst
[col
*3+0] = (pixel
>> 16) & 0xff;
300 dst
[col
*3+1] = (pixel
>> 8) & 0xff;
301 dst
[col
*3+2] = (pixel
>> 0) & 0xff;
307 case A8R8G8B8_UNORM_TO_BGRA_UINT
:
308 for (row
= 0; row
< height
; row
++) {
309 const GLubyte
*src
= map
+ y
* trans
->stride
;
310 memcpy(dst
, src
, 4 * width
);
319 pipe
->transfer_unmap(pipe
, trans
);
320 pipe
->tex_transfer_destroy(pipe
, trans
);
328 * Do glReadPixels by getting rows from the framebuffer transfer with
329 * get_tile(). Convert to requested format/type with Mesa image routines.
330 * Image transfer ops are done in software too.
333 st_readpixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
334 GLenum format
, GLenum type
,
335 const struct gl_pixelstore_attrib
*pack
,
338 struct pipe_context
*pipe
= ctx
->st
->pipe
;
339 GLfloat temp
[MAX_WIDTH
][4];
340 const GLbitfield transferOps
= ctx
->_ImageTransferState
;
342 GLint yStep
, dfStride
;
344 struct st_renderbuffer
*strb
;
345 struct gl_pixelstore_attrib clippedPacking
= *pack
;
346 struct pipe_transfer
*trans
;
348 assert(ctx
->ReadBuffer
->Width
> 0);
350 /* XXX convolution not done yet */
351 assert((transferOps
& IMAGE_CONVOLUTION_BIT
) == 0);
353 /* Do all needed clipping here, so that we can forget about it later */
354 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
355 /* The ReadPixels transfer is totally outside the window bounds */
359 dest
= _mesa_map_pbo_dest(ctx
, &clippedPacking
, dest
);
363 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
365 if (format
== GL_STENCIL_INDEX
||
366 format
== GL_DEPTH_STENCIL
) {
367 st_read_stencil_pixels(ctx
, x
, y
, width
, height
,
368 format
, type
, pack
, dest
);
371 else if (format
== GL_DEPTH_COMPONENT
) {
372 strb
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
375 /* Read color buffer */
376 strb
= st_get_color_read_renderbuffer(ctx
);
382 /* try a fast-path readpixels before anything else */
383 if (st_fast_readpixels(ctx
, strb
, x
, y
, width
, height
,
384 format
, type
, pack
, dest
)) {
386 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
390 if (format
== GL_RGBA
&& type
== GL_FLOAT
) {
391 /* write tile(row) directly into user's buffer */
392 df
= (GLfloat
*) _mesa_image_address2d(&clippedPacking
, dest
, width
,
393 height
, format
, type
, 0, 0);
394 dfStride
= width
* 4;
397 /* write tile(row) into temp row buffer */
398 df
= (GLfloat
*) temp
;
402 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
403 /* convert GL Y to Gallium Y */
404 y
= strb
->Base
.Height
- y
- height
;
407 /* Create a read transfer from the renderbuffer's texture */
408 trans
= st_cond_flush_get_tex_transfer(st_context(ctx
), strb
->texture
,
410 PIPE_TRANSFER_READ
, x
, y
,
413 /* determine bottom-to-top vs. top-to-bottom order */
414 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
423 if (ST_DEBUG
& DEBUG_FALLBACK
)
424 debug_printf("%s: fallback processing\n", __FUNCTION__
);
427 * Copy pixels from pipe_transfer to user memory
430 /* dest of first pixel in client memory */
431 GLubyte
*dst
= _mesa_image_address2d(&clippedPacking
, dest
, width
,
432 height
, format
, type
, 0, 0);
433 /* dest row stride */
434 const GLint dstStride
= _mesa_image_row_stride(&clippedPacking
, width
,
437 if (trans
->texture
->format
== PIPE_FORMAT_Z24S8_UNORM
||
438 trans
->texture
->format
== PIPE_FORMAT_Z24X8_UNORM
) {
439 if (format
== GL_DEPTH_COMPONENT
) {
440 for (i
= 0; i
< height
; i
++) {
441 GLuint ztemp
[MAX_WIDTH
];
442 GLfloat zfloat
[MAX_WIDTH
];
443 const double scale
= 1.0 / ((1 << 24) - 1);
444 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
446 for (j
= 0; j
< width
; j
++) {
447 zfloat
[j
] = (float) (scale
* (ztemp
[j
] & 0xffffff));
449 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
450 zfloat
, &clippedPacking
);
455 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
456 assert(format
== GL_DEPTH_STENCIL_EXT
);
457 for (i
= 0; i
< height
; i
++) {
458 GLuint
*zshort
= (GLuint
*)dst
;
459 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, dst
, 0);
461 /* Reverse into 24/8 */
462 for (j
= 0; j
< width
; j
++) {
463 zshort
[j
] = (zshort
[j
] << 8) | (zshort
[j
] >> 24);
469 else if (trans
->texture
->format
== PIPE_FORMAT_S8Z24_UNORM
||
470 trans
->texture
->format
== PIPE_FORMAT_X8Z24_UNORM
) {
471 if (format
== GL_DEPTH_COMPONENT
) {
472 for (i
= 0; i
< height
; i
++) {
473 GLuint ztemp
[MAX_WIDTH
];
474 GLfloat zfloat
[MAX_WIDTH
];
475 const double scale
= 1.0 / ((1 << 24) - 1);
476 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
478 for (j
= 0; j
< width
; j
++) {
479 zfloat
[j
] = (float) (scale
* ((ztemp
[j
] >> 8) & 0xffffff));
481 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
482 zfloat
, &clippedPacking
);
487 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
488 assert(format
== GL_DEPTH_STENCIL_EXT
);
489 for (i
= 0; i
< height
; i
++) {
490 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, dst
, 0);
496 else if (trans
->texture
->format
== PIPE_FORMAT_Z16_UNORM
) {
497 for (i
= 0; i
< height
; i
++) {
498 GLushort ztemp
[MAX_WIDTH
];
499 GLfloat zfloat
[MAX_WIDTH
];
500 const double scale
= 1.0 / 0xffff;
501 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
503 for (j
= 0; j
< width
; j
++) {
504 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
506 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
507 zfloat
, &clippedPacking
);
511 else if (trans
->texture
->format
== PIPE_FORMAT_Z32_UNORM
) {
512 for (i
= 0; i
< height
; i
++) {
513 GLuint ztemp
[MAX_WIDTH
];
514 GLfloat zfloat
[MAX_WIDTH
];
515 const double scale
= 1.0 / 0xffffffff;
516 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
518 for (j
= 0; j
< width
; j
++) {
519 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
521 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
522 zfloat
, &clippedPacking
);
528 /* Do a row at a time to flip image data vertically */
529 for (i
= 0; i
< height
; i
++) {
530 pipe_get_tile_rgba(pipe
, trans
, 0, y
, width
, 1, df
);
534 _mesa_pack_rgba_span_float(ctx
, width
, temp
, format
, type
, dst
,
535 &clippedPacking
, transferOps
);
542 pipe
->tex_transfer_destroy(pipe
, trans
);
544 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
548 void st_init_readpixels_functions(struct dd_function_table
*functions
)
550 functions
->ReadPixels
= st_readpixels
;