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 "pipe/p_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_screen
*screen
= ctx
->st
->pipe
->screen
;
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
= screen
->transfer_map(screen
, 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_S8Z24_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_Z24S8_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 screen
->transfer_unmap(screen
, pt
);
165 screen
->tex_transfer_destroy(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_A8R8G8B8_UNORM
&&
218 format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
219 combo
= A8R8G8B8_UNORM_TO_RGBA_UBYTE
;
221 else if (strb
->format
== PIPE_FORMAT_A8R8G8B8_UNORM
&&
222 format
== GL_RGB
&& type
== GL_UNSIGNED_BYTE
) {
223 combo
= A8R8G8B8_UNORM_TO_RGB_UBYTE
;
225 else if (strb
->format
== PIPE_FORMAT_A8R8G8B8_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_screen
*screen
= pipe
->screen
;
238 struct pipe_transfer
*trans
;
241 GLint row
, col
, dy
, dstStride
;
243 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
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
= screen
->transfer_map(screen
, trans
);
257 screen
->tex_transfer_destroy(trans
);
261 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
270 dst
= _mesa_image_address2d(pack
, dest
, width
, height
,
272 dstStride
= _mesa_image_row_stride(pack
, width
, format
, type
);
275 case A8R8G8B8_UNORM_TO_RGBA_UBYTE
:
276 for (row
= 0; row
< height
; row
++) {
277 const GLubyte
*src
= map
+ y
* trans
->stride
;
278 for (col
= 0; col
< width
; col
++) {
279 GLuint pixel
= ((GLuint
*) src
)[col
];
280 dst
[col
*4+0] = (pixel
>> 16) & 0xff;
281 dst
[col
*4+1] = (pixel
>> 8) & 0xff;
282 dst
[col
*4+2] = (pixel
>> 0) & 0xff;
283 dst
[col
*4+3] = (pixel
>> 24) & 0xff;
289 case A8R8G8B8_UNORM_TO_RGB_UBYTE
:
290 for (row
= 0; row
< height
; row
++) {
291 const GLubyte
*src
= map
+ y
* trans
->stride
;
292 for (col
= 0; col
< width
; col
++) {
293 GLuint pixel
= ((GLuint
*) src
)[col
];
294 dst
[col
*3+0] = (pixel
>> 16) & 0xff;
295 dst
[col
*3+1] = (pixel
>> 8) & 0xff;
296 dst
[col
*3+2] = (pixel
>> 0) & 0xff;
302 case A8R8G8B8_UNORM_TO_BGRA_UINT
:
303 for (row
= 0; row
< height
; row
++) {
304 const GLubyte
*src
= map
+ y
* trans
->stride
;
305 memcpy(dst
, src
, 4 * width
);
314 screen
->transfer_unmap(screen
, trans
);
315 screen
->tex_transfer_destroy(trans
);
323 * Do glReadPixels by getting rows from the framebuffer transfer with
324 * get_tile(). Convert to requested format/type with Mesa image routines.
325 * Image transfer ops are done in software too.
328 st_readpixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
329 GLenum format
, GLenum type
,
330 const struct gl_pixelstore_attrib
*pack
,
333 struct pipe_context
*pipe
= ctx
->st
->pipe
;
334 struct pipe_screen
*screen
= pipe
->screen
;
335 GLfloat temp
[MAX_WIDTH
][4];
336 const GLbitfield transferOps
= ctx
->_ImageTransferState
;
338 GLint yStep
, dfStride
;
340 struct st_renderbuffer
*strb
;
341 struct gl_pixelstore_attrib clippedPacking
= *pack
;
342 struct pipe_transfer
*trans
;
344 assert(ctx
->ReadBuffer
->Width
> 0);
346 /* XXX convolution not done yet */
347 assert((transferOps
& IMAGE_CONVOLUTION_BIT
) == 0);
349 /* Do all needed clipping here, so that we can forget about it later */
350 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
351 /* The ReadPixels transfer is totally outside the window bounds */
355 dest
= _mesa_map_pbo_dest(ctx
, &clippedPacking
, dest
);
359 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
361 if (format
== GL_STENCIL_INDEX
||
362 format
== GL_DEPTH_STENCIL
) {
363 st_read_stencil_pixels(ctx
, x
, y
, width
, height
,
364 format
, type
, pack
, dest
);
367 else if (format
== GL_DEPTH_COMPONENT
) {
368 strb
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
371 /* Read color buffer */
372 strb
= st_get_color_read_renderbuffer(ctx
);
378 /* try a fast-path readpixels before anything else */
379 if (st_fast_readpixels(ctx
, strb
, x
, y
, width
, height
,
380 format
, type
, pack
, dest
)) {
382 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
386 if (format
== GL_RGBA
&& type
== GL_FLOAT
) {
387 /* write tile(row) directly into user's buffer */
388 df
= (GLfloat
*) _mesa_image_address2d(&clippedPacking
, dest
, width
,
389 height
, format
, type
, 0, 0);
390 dfStride
= width
* 4;
393 /* write tile(row) into temp row buffer */
394 df
= (GLfloat
*) temp
;
398 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
399 y
= strb
->Base
.Height
- y
- height
;
402 /* Create a read transfer from the renderbuffer's texture */
403 trans
= st_cond_flush_get_tex_transfer(st_context(ctx
), strb
->texture
,
405 PIPE_TRANSFER_READ
, x
, y
,
408 /* determine bottom-to-top vs. top-to-bottom order */
409 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
418 if (ST_DEBUG
& DEBUG_FALLBACK
)
419 debug_printf("%s: fallback processing\n", __FUNCTION__
);
422 * Copy pixels from pipe_transfer to user memory
425 /* dest of first pixel in client memory */
426 GLubyte
*dst
= _mesa_image_address2d(&clippedPacking
, dest
, width
,
427 height
, format
, type
, 0, 0);
428 /* dest row stride */
429 const GLint dstStride
= _mesa_image_row_stride(&clippedPacking
, width
,
432 if (trans
->texture
->format
== PIPE_FORMAT_S8Z24_UNORM
||
433 trans
->texture
->format
== PIPE_FORMAT_X8Z24_UNORM
) {
434 if (format
== GL_DEPTH_COMPONENT
) {
435 for (i
= 0; i
< height
; i
++) {
436 GLuint ztemp
[MAX_WIDTH
];
437 GLfloat zfloat
[MAX_WIDTH
];
438 const double scale
= 1.0 / ((1 << 24) - 1);
439 pipe_get_tile_raw(trans
, 0, y
, width
, 1, ztemp
, 0);
441 for (j
= 0; j
< width
; j
++) {
442 zfloat
[j
] = (float) (scale
* (ztemp
[j
] & 0xffffff));
444 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
445 zfloat
, &clippedPacking
);
450 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
451 assert(format
== GL_DEPTH_STENCIL_EXT
);
452 for (i
= 0; i
< height
; i
++) {
453 GLuint
*zshort
= (GLuint
*)dst
;
454 pipe_get_tile_raw(trans
, 0, y
, width
, 1, dst
, 0);
456 /* Reverse into 24/8 */
457 for (j
= 0; j
< width
; j
++) {
458 zshort
[j
] = (zshort
[j
] << 8) | (zshort
[j
] >> 24);
464 else if (trans
->texture
->format
== PIPE_FORMAT_Z24S8_UNORM
||
465 trans
->texture
->format
== PIPE_FORMAT_Z24X8_UNORM
) {
466 if (format
== GL_DEPTH_COMPONENT
) {
467 for (i
= 0; i
< height
; i
++) {
468 GLuint ztemp
[MAX_WIDTH
];
469 GLfloat zfloat
[MAX_WIDTH
];
470 const double scale
= 1.0 / ((1 << 24) - 1);
471 pipe_get_tile_raw(trans
, 0, y
, width
, 1, ztemp
, 0);
473 for (j
= 0; j
< width
; j
++) {
474 zfloat
[j
] = (float) (scale
* ((ztemp
[j
] >> 8) & 0xffffff));
476 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
477 zfloat
, &clippedPacking
);
482 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
483 assert(format
== GL_DEPTH_STENCIL_EXT
);
484 for (i
= 0; i
< height
; i
++) {
485 pipe_get_tile_raw(trans
, 0, y
, width
, 1, dst
, 0);
491 else if (trans
->texture
->format
== PIPE_FORMAT_Z16_UNORM
) {
492 for (i
= 0; i
< height
; i
++) {
493 GLushort ztemp
[MAX_WIDTH
];
494 GLfloat zfloat
[MAX_WIDTH
];
495 const double scale
= 1.0 / 0xffff;
496 pipe_get_tile_raw(trans
, 0, y
, width
, 1, ztemp
, 0);
498 for (j
= 0; j
< width
; j
++) {
499 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
501 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
502 zfloat
, &clippedPacking
);
506 else if (trans
->texture
->format
== PIPE_FORMAT_Z32_UNORM
) {
507 for (i
= 0; i
< height
; i
++) {
508 GLuint ztemp
[MAX_WIDTH
];
509 GLfloat zfloat
[MAX_WIDTH
];
510 const double scale
= 1.0 / 0xffffffff;
511 pipe_get_tile_raw(trans
, 0, y
, width
, 1, ztemp
, 0);
513 for (j
= 0; j
< width
; j
++) {
514 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
516 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
517 zfloat
, &clippedPacking
);
523 /* Do a row at a time to flip image data vertically */
524 for (i
= 0; i
< height
; i
++) {
525 pipe_get_tile_rgba(trans
, 0, y
, width
, 1, df
);
529 _mesa_pack_rgba_span_float(ctx
, width
, temp
, format
, type
, dst
,
530 &clippedPacking
, transferOps
);
537 screen
->tex_transfer_destroy(trans
);
539 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
543 void st_init_readpixels_functions(struct dd_function_table
*functions
)
545 functions
->ReadPixels
= st_readpixels
;