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"
40 #include "main/pack.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_defines.h"
45 #include "util/u_format.h"
46 #include "util/u_inlines.h"
47 #include "util/u_tile.h"
50 #include "st_context.h"
52 #include "st_cb_bitmap.h"
53 #include "st_cb_readpixels.h"
54 #include "st_cb_fbo.h"
57 * Special case for reading stencil buffer.
58 * For color/depth we use get_tile(). For stencil, map the stencil buffer.
61 st_read_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
62 GLsizei width
, GLsizei height
,
63 GLenum format
, GLenum type
,
64 const struct gl_pixelstore_attrib
*packing
,
67 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
68 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
69 struct st_renderbuffer
*strb
= st_renderbuffer(fb
->_StencilBuffer
);
70 struct pipe_transfer
*pt
;
74 if (strb
->Base
.Wrapped
) {
75 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
78 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
79 y
= ctx
->DrawBuffer
->Height
- y
- height
;
82 /* Create a read transfer from the renderbuffer's texture */
84 pt
= pipe_get_transfer(pipe
, strb
->texture
,
86 strb
->rtt_face
+ strb
->rtt_slice
,
90 /* map the stencil buffer */
91 stmap
= pipe_transfer_map(pipe
, pt
);
93 /* width should never be > MAX_WIDTH since we did clipping earlier */
94 ASSERT(width
<= MAX_WIDTH
);
96 /* process image row by row */
97 for (j
= 0; j
< height
; j
++) {
99 GLstencil sValues
[MAX_WIDTH
];
100 GLfloat zValues
[MAX_WIDTH
];
103 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
104 srcY
= height
- j
- 1;
110 /* get stencil (and Z) values */
111 switch (pt
->resource
->format
) {
112 case PIPE_FORMAT_S8_USCALED
:
114 const ubyte
*src
= stmap
+ srcY
* pt
->stride
;
115 memcpy(sValues
, src
, width
);
118 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
119 if (format
== GL_DEPTH_STENCIL
) {
120 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
121 const GLfloat scale
= 1.0f
/ (0xffffff);
123 for (k
= 0; k
< width
; k
++) {
124 sValues
[k
] = src
[k
] >> 24;
125 zValues
[k
] = (src
[k
] & 0xffffff) * scale
;
129 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
131 for (k
= 0; k
< width
; k
++) {
132 sValues
[k
] = src
[k
] >> 24;
136 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
137 if (format
== GL_DEPTH_STENCIL
) {
138 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
139 const GLfloat scale
= 1.0f
/ (0xffffff);
141 for (k
= 0; k
< width
; k
++) {
142 sValues
[k
] = src
[k
] & 0xff;
143 zValues
[k
] = (src
[k
] >> 8) * scale
;
147 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
149 for (k
= 0; k
< width
; k
++) {
150 sValues
[k
] = src
[k
] & 0xff;
154 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED
:
155 if (format
== GL_DEPTH_STENCIL
) {
156 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
157 const GLfloat
*srcf
= (const GLfloat
*)src
;
159 for (k
= 0; k
< width
; k
++) {
160 zValues
[k
] = srcf
[k
*2];
161 sValues
[k
] = src
[k
*2+1] & 0xff;
165 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
167 for (k
= 0; k
< width
; k
++) {
168 sValues
[k
] = src
[k
*2+1] & 0xff;
177 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
179 if (format
== GL_DEPTH_STENCIL
) {
180 _mesa_pack_depth_stencil_span(ctx
, width
, type
, dest
,
181 zValues
, sValues
, packing
);
184 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, sValues
, packing
);
188 /* unmap the stencil buffer */
189 pipe_transfer_unmap(pipe
, pt
);
190 pipe
->transfer_destroy(pipe
, pt
);
195 * Return renderbuffer to use for reading color pixels for glRead/CopyPixel
198 struct st_renderbuffer
*
199 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
201 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
202 struct st_renderbuffer
*strb
=
203 st_renderbuffer(fb
->_ColorReadBuffer
);
210 * Try to do glReadPixels in a fast manner for common cases.
211 * \return GL_TRUE for success, GL_FALSE for failure
214 st_fast_readpixels(struct gl_context
*ctx
, struct st_renderbuffer
*strb
,
215 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
216 GLenum format
, GLenum type
,
217 const struct gl_pixelstore_attrib
*pack
,
220 GLubyte alphaORoperand
;
222 A8R8G8B8_UNORM_TO_RGBA_UBYTE
,
223 A8R8G8B8_UNORM_TO_RGB_UBYTE
,
224 A8R8G8B8_UNORM_TO_BGRA_UINT
,
225 A8R8G8B8_UNORM_TO_RGBA_UINT
228 if (ctx
->_ImageTransferState
)
231 if (strb
->format
== PIPE_FORMAT_B8G8R8A8_UNORM
) {
234 else if (strb
->format
== PIPE_FORMAT_B8G8R8X8_UNORM
) {
235 alphaORoperand
= 0xff;
241 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
242 combo
= A8R8G8B8_UNORM_TO_RGBA_UBYTE
;
244 else if (format
== GL_RGB
&& type
== GL_UNSIGNED_BYTE
) {
245 combo
= A8R8G8B8_UNORM_TO_RGB_UBYTE
;
247 else if (format
== GL_BGRA
&& type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
248 combo
= A8R8G8B8_UNORM_TO_BGRA_UINT
;
250 else if (format
== GL_RGBA
&& type
== GL_UNSIGNED_INT_8_8_8_8
) {
251 combo
= A8R8G8B8_UNORM_TO_RGBA_UINT
;
257 /*printf("st_fast_readpixels combo %d\n", (GLint) combo);*/
260 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
261 struct pipe_transfer
*trans
;
264 GLint row
, col
, dy
, dstStride
;
266 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
267 /* convert GL Y to Gallium Y */
268 y
= strb
->texture
->height0
- y
- height
;
271 trans
= pipe_get_transfer(pipe
, strb
->texture
,
273 strb
->rtt_face
+ strb
->rtt_slice
,
275 x
, y
, width
, height
);
280 map
= pipe_transfer_map(pipe
, trans
);
282 pipe
->transfer_destroy(pipe
, trans
);
286 /* We always write to the user/dest buffer from low addr to high addr
287 * but the read order depends on renderbuffer orientation
289 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
290 /* read source rows from bottom to top */
295 /* read source rows from top to bottom */
300 dst
= _mesa_image_address2d(pack
, dest
, width
, height
,
302 dstStride
= _mesa_image_row_stride(pack
, width
, format
, type
);
305 case A8R8G8B8_UNORM_TO_RGBA_UBYTE
:
306 for (row
= 0; row
< height
; row
++) {
307 const GLubyte
*src
= map
+ y
* trans
->stride
;
308 for (col
= 0; col
< width
; col
++) {
309 GLuint pixel
= ((GLuint
*) src
)[col
];
310 dst
[col
*4+0] = (pixel
>> 16) & 0xff;
311 dst
[col
*4+1] = (pixel
>> 8) & 0xff;
312 dst
[col
*4+2] = (pixel
>> 0) & 0xff;
313 dst
[col
*4+3] = ((pixel
>> 24) & 0xff) | alphaORoperand
;
319 case A8R8G8B8_UNORM_TO_RGB_UBYTE
:
320 for (row
= 0; row
< height
; row
++) {
321 const GLubyte
*src
= map
+ y
* trans
->stride
;
322 for (col
= 0; col
< width
; col
++) {
323 GLuint pixel
= ((GLuint
*) src
)[col
];
324 dst
[col
*3+0] = (pixel
>> 16) & 0xff;
325 dst
[col
*3+1] = (pixel
>> 8) & 0xff;
326 dst
[col
*3+2] = (pixel
>> 0) & 0xff;
332 case A8R8G8B8_UNORM_TO_BGRA_UINT
:
333 for (row
= 0; row
< height
; row
++) {
334 const GLubyte
*src
= map
+ y
* trans
->stride
;
335 memcpy(dst
, src
, 4 * width
);
336 if (alphaORoperand
) {
337 assert(alphaORoperand
== 0xff);
338 for (col
= 0; col
< width
; col
++) {
346 case A8R8G8B8_UNORM_TO_RGBA_UINT
:
347 for (row
= 0; row
< height
; row
++) {
348 const GLubyte
*src
= map
+ y
* trans
->stride
;
349 for (col
= 0; col
< width
; col
++) {
350 GLuint pixel
= ((GLuint
*) src
)[col
];
351 dst
[col
*4+0] = ((pixel
>> 24) & 0xff) | alphaORoperand
;
352 dst
[col
*4+1] = (pixel
>> 0) & 0xff;
353 dst
[col
*4+2] = (pixel
>> 8) & 0xff;
354 dst
[col
*4+3] = (pixel
>> 16) & 0xff;
364 pipe_transfer_unmap(pipe
, trans
);
365 pipe
->transfer_destroy(pipe
, trans
);
373 * Do glReadPixels by getting rows from the framebuffer transfer with
374 * get_tile(). Convert to requested format/type with Mesa image routines.
375 * Image transfer ops are done in software too.
378 st_readpixels(struct gl_context
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
379 GLenum format
, GLenum type
,
380 const struct gl_pixelstore_attrib
*pack
,
383 struct st_context
*st
= st_context(ctx
);
384 struct pipe_context
*pipe
= st
->pipe
;
386 GLbitfield transferOps
= ctx
->_ImageTransferState
;
388 GLint yStep
, dfStride
;
390 struct st_renderbuffer
*strb
;
391 struct gl_pixelstore_attrib clippedPacking
= *pack
;
392 struct pipe_transfer
*trans
;
393 enum pipe_format pformat
;
395 assert(ctx
->ReadBuffer
->Width
> 0);
397 st_validate_state(st
);
399 /* Do all needed clipping here, so that we can forget about it later */
400 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
401 /* The ReadPixels transfer is totally outside the window bounds */
405 st_flush_bitmap_cache(st
);
407 dest
= _mesa_map_pbo_dest(ctx
, &clippedPacking
, dest
);
411 if (format
== GL_STENCIL_INDEX
||
412 format
== GL_DEPTH_STENCIL
) {
413 st_read_stencil_pixels(ctx
, x
, y
, width
, height
,
414 format
, type
, pack
, dest
);
417 else if (format
== GL_DEPTH_COMPONENT
) {
418 strb
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
419 if (strb
->Base
.Wrapped
) {
420 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
424 /* Read color buffer */
425 strb
= st_get_color_read_renderbuffer(ctx
);
431 /* try a fast-path readpixels before anything else */
432 if (st_fast_readpixels(ctx
, strb
, x
, y
, width
, height
,
433 format
, type
, pack
, dest
)) {
435 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
439 /* allocate temp pixel row buffer */
440 temp
= (GLfloat (*)[4]) malloc(4 * width
* sizeof(GLfloat
));
442 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
446 if(ctx
->Color
._ClampReadColor
)
447 transferOps
|= IMAGE_CLAMP_BIT
;
449 if (format
== GL_RGBA
&& type
== GL_FLOAT
&& !transferOps
) {
450 /* write tile(row) directly into user's buffer */
451 df
= (GLfloat
*) _mesa_image_address2d(&clippedPacking
, dest
, width
,
452 height
, format
, type
, 0, 0);
453 dfStride
= width
* 4;
456 /* write tile(row) into temp row buffer */
457 df
= (GLfloat
*) temp
;
461 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
462 /* convert GL Y to Gallium Y */
463 y
= strb
->Base
.Height
- y
- height
;
466 /* Create a read transfer from the renderbuffer's texture */
467 trans
= pipe_get_transfer(pipe
, strb
->texture
,
468 strb
->rtt_level
, /* level */
469 strb
->rtt_face
+ strb
->rtt_slice
, /* layer */
471 x
, y
, width
, height
);
473 /* determine bottom-to-top vs. top-to-bottom order */
474 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
483 /* possibly convert sRGB format to linear RGB format */
484 pformat
= util_format_linear(trans
->resource
->format
);
486 if (ST_DEBUG
& DEBUG_FALLBACK
)
487 debug_printf("%s: fallback processing\n", __FUNCTION__
);
490 * Copy pixels from pipe_transfer to user memory
493 /* dest of first pixel in client memory */
494 GLubyte
*dst
= _mesa_image_address2d(&clippedPacking
, dest
, width
,
495 height
, format
, type
, 0, 0);
496 /* dest row stride */
497 const GLint dstStride
= _mesa_image_row_stride(&clippedPacking
, width
,
500 if (pformat
== PIPE_FORMAT_Z24_UNORM_S8_USCALED
||
501 pformat
== PIPE_FORMAT_Z24X8_UNORM
) {
502 if (format
== GL_DEPTH_COMPONENT
) {
503 for (i
= 0; i
< height
; i
++) {
504 GLuint ztemp
[MAX_WIDTH
];
505 GLfloat zfloat
[MAX_WIDTH
];
506 const double scale
= 1.0 / ((1 << 24) - 1);
507 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
509 for (j
= 0; j
< width
; j
++) {
510 zfloat
[j
] = (float) (scale
* (ztemp
[j
] & 0xffffff));
512 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
513 zfloat
, &clippedPacking
);
518 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
519 assert(format
== GL_DEPTH_STENCIL_EXT
);
520 for (i
= 0; i
< height
; i
++) {
521 GLuint
*zshort
= (GLuint
*)dst
;
522 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, dst
, 0);
524 /* Reverse into 24/8 */
525 for (j
= 0; j
< width
; j
++) {
526 zshort
[j
] = (zshort
[j
] << 8) | (zshort
[j
] >> 24);
532 else if (pformat
== PIPE_FORMAT_S8_USCALED_Z24_UNORM
||
533 pformat
== PIPE_FORMAT_X8Z24_UNORM
) {
534 if (format
== GL_DEPTH_COMPONENT
) {
535 for (i
= 0; i
< height
; i
++) {
536 GLuint ztemp
[MAX_WIDTH
];
537 GLfloat zfloat
[MAX_WIDTH
];
538 const double scale
= 1.0 / ((1 << 24) - 1);
539 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
541 for (j
= 0; j
< width
; j
++) {
542 zfloat
[j
] = (float) (scale
* ((ztemp
[j
] >> 8) & 0xffffff));
544 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
545 zfloat
, &clippedPacking
);
550 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
551 assert(format
== GL_DEPTH_STENCIL_EXT
);
552 for (i
= 0; i
< height
; i
++) {
553 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, dst
, 0);
559 else if (pformat
== PIPE_FORMAT_Z16_UNORM
) {
560 for (i
= 0; i
< height
; i
++) {
561 GLushort ztemp
[MAX_WIDTH
];
562 GLfloat zfloat
[MAX_WIDTH
];
563 const double scale
= 1.0 / 0xffff;
564 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
566 for (j
= 0; j
< width
; j
++) {
567 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
569 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
570 zfloat
, &clippedPacking
);
574 else if (pformat
== PIPE_FORMAT_Z32_UNORM
) {
575 for (i
= 0; i
< height
; i
++) {
576 GLuint ztemp
[MAX_WIDTH
];
577 GLfloat zfloat
[MAX_WIDTH
];
578 const double scale
= 1.0 / 0xffffffff;
579 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
581 for (j
= 0; j
< width
; j
++) {
582 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
584 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
585 zfloat
, &clippedPacking
);
589 else if (pformat
== PIPE_FORMAT_Z32_FLOAT
) {
590 for (i
= 0; i
< height
; i
++) {
591 GLfloat zfloat
[MAX_WIDTH
];
592 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, zfloat
, 0);
594 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
595 zfloat
, &clippedPacking
);
599 else if (pformat
== PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED
) {
600 assert(format
== GL_DEPTH_COMPONENT
);
601 for (i
= 0; i
< height
; i
++) {
602 GLfloat zfloat
[MAX_WIDTH
]; /* Z32 */
603 GLfloat zfloat2
[MAX_WIDTH
*2]; /* Z32X32 */
604 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, zfloat2
, 0);
606 for (j
= 0; j
< width
; j
++) {
607 zfloat
[j
] = zfloat2
[j
*2];
609 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
610 zfloat
, &clippedPacking
);
616 /* Do a row at a time to flip image data vertically */
617 for (i
= 0; i
< height
; i
++) {
618 pipe_get_tile_rgba_format(pipe
, trans
, 0, y
, width
, 1,
623 _mesa_pack_rgba_span_float(ctx
, width
, temp
, format
, type
, dst
,
624 &clippedPacking
, transferOps
);
633 pipe
->transfer_destroy(pipe
, trans
);
635 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
639 void st_init_readpixels_functions(struct dd_function_table
*functions
)
641 functions
->ReadPixels
= st_readpixels
;