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
,
89 /* map the stencil buffer */
90 stmap
= pipe_transfer_map(pipe
, pt
);
92 /* width should never be > MAX_WIDTH since we did clipping earlier */
93 ASSERT(width
<= MAX_WIDTH
);
95 /* process image row by row */
96 for (j
= 0; j
< height
; j
++) {
98 GLstencil sValues
[MAX_WIDTH
];
99 GLfloat zValues
[MAX_WIDTH
];
102 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
103 srcY
= height
- j
- 1;
109 /* get stencil (and Z) values */
110 switch (pt
->resource
->format
) {
111 case PIPE_FORMAT_S8_USCALED
:
113 const ubyte
*src
= stmap
+ srcY
* pt
->stride
;
114 memcpy(sValues
, src
, width
);
117 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
118 if (format
== GL_DEPTH_STENCIL
) {
119 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
120 const GLfloat scale
= 1.0f
/ (0xffffff);
122 for (k
= 0; k
< width
; k
++) {
123 sValues
[k
] = src
[k
] >> 24;
124 zValues
[k
] = (src
[k
] & 0xffffff) * scale
;
128 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
130 for (k
= 0; k
< width
; k
++) {
131 sValues
[k
] = src
[k
] >> 24;
135 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
136 if (format
== GL_DEPTH_STENCIL
) {
137 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
138 const GLfloat scale
= 1.0f
/ (0xffffff);
140 for (k
= 0; k
< width
; k
++) {
141 sValues
[k
] = src
[k
] & 0xff;
142 zValues
[k
] = (src
[k
] >> 8) * scale
;
146 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
148 for (k
= 0; k
< width
; k
++) {
149 sValues
[k
] = src
[k
] & 0xff;
158 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
160 if (format
== GL_DEPTH_STENCIL
) {
161 _mesa_pack_depth_stencil_span(ctx
, width
, dest
,
162 zValues
, sValues
, packing
);
165 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, sValues
, packing
);
169 /* unmap the stencil buffer */
170 pipe_transfer_unmap(pipe
, pt
);
171 pipe
->transfer_destroy(pipe
, pt
);
176 * Return renderbuffer to use for reading color pixels for glRead/CopyPixel
179 struct st_renderbuffer
*
180 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
182 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
183 struct st_renderbuffer
*strb
=
184 st_renderbuffer(fb
->_ColorReadBuffer
);
191 * Try to do glReadPixels in a fast manner for common cases.
192 * \return GL_TRUE for success, GL_FALSE for failure
195 st_fast_readpixels(struct gl_context
*ctx
, struct st_renderbuffer
*strb
,
196 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
197 GLenum format
, GLenum type
,
198 const struct gl_pixelstore_attrib
*pack
,
201 GLubyte alphaORoperand
;
203 A8R8G8B8_UNORM_TO_RGBA_UBYTE
,
204 A8R8G8B8_UNORM_TO_RGB_UBYTE
,
205 A8R8G8B8_UNORM_TO_BGRA_UINT
,
206 A8R8G8B8_UNORM_TO_RGBA_UINT
209 if (ctx
->_ImageTransferState
)
212 if (strb
->format
== PIPE_FORMAT_B8G8R8A8_UNORM
) {
214 } else if (strb
->format
== PIPE_FORMAT_B8G8R8X8_UNORM
) {
215 alphaORoperand
= 0xff;
220 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
221 combo
= A8R8G8B8_UNORM_TO_RGBA_UBYTE
;
223 else if (format
== GL_RGB
&& type
== GL_UNSIGNED_BYTE
) {
224 combo
= A8R8G8B8_UNORM_TO_RGB_UBYTE
;
226 else if (format
== GL_BGRA
&& type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
227 combo
= A8R8G8B8_UNORM_TO_BGRA_UINT
;
229 else if (format
== GL_RGBA
&& type
== GL_UNSIGNED_INT_8_8_8_8
) {
230 combo
= A8R8G8B8_UNORM_TO_RGBA_UINT
;
236 /*printf("st_fast_readpixels combo %d\n", (GLint) combo);*/
239 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
240 struct pipe_transfer
*trans
;
243 GLint row
, col
, dy
, dstStride
;
245 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
246 /* convert GL Y to Gallium Y */
247 y
= strb
->texture
->height0
- y
- height
;
250 trans
= pipe_get_transfer(pipe
, strb
->texture
,
253 x
, y
, width
, height
);
258 map
= pipe_transfer_map(pipe
, trans
);
260 pipe
->transfer_destroy(pipe
, trans
);
264 /* We always write to the user/dest buffer from low addr to high addr
265 * but the read order depends on renderbuffer orientation
267 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
268 /* read source rows from bottom to top */
273 /* read source rows from top to bottom */
278 dst
= _mesa_image_address2d(pack
, dest
, width
, height
,
280 dstStride
= _mesa_image_row_stride(pack
, width
, format
, type
);
283 case A8R8G8B8_UNORM_TO_RGBA_UBYTE
:
284 for (row
= 0; row
< height
; row
++) {
285 const GLubyte
*src
= map
+ y
* trans
->stride
;
286 for (col
= 0; col
< width
; col
++) {
287 GLuint pixel
= ((GLuint
*) src
)[col
];
288 dst
[col
*4+0] = (pixel
>> 16) & 0xff;
289 dst
[col
*4+1] = (pixel
>> 8) & 0xff;
290 dst
[col
*4+2] = (pixel
>> 0) & 0xff;
291 dst
[col
*4+3] = ((pixel
>> 24) & 0xff) | alphaORoperand
;
297 case A8R8G8B8_UNORM_TO_RGB_UBYTE
:
298 for (row
= 0; row
< height
; row
++) {
299 const GLubyte
*src
= map
+ y
* trans
->stride
;
300 for (col
= 0; col
< width
; col
++) {
301 GLuint pixel
= ((GLuint
*) src
)[col
];
302 dst
[col
*3+0] = (pixel
>> 16) & 0xff;
303 dst
[col
*3+1] = (pixel
>> 8) & 0xff;
304 dst
[col
*3+2] = (pixel
>> 0) & 0xff;
310 case A8R8G8B8_UNORM_TO_BGRA_UINT
:
311 for (row
= 0; row
< height
; row
++) {
312 const GLubyte
*src
= map
+ y
* trans
->stride
;
313 memcpy(dst
, src
, 4 * width
);
314 if (alphaORoperand
) {
315 for (col
= 0; col
< width
; col
++) {
316 dst
[col
*4+3] |= alphaORoperand
;
323 case A8R8G8B8_UNORM_TO_RGBA_UINT
:
324 for (row
= 0; row
< height
; row
++) {
325 const GLubyte
*src
= map
+ y
* trans
->stride
;
326 for (col
= 0; col
< width
; col
++) {
327 GLuint pixel
= ((GLuint
*) src
)[col
];
328 dst
[col
*4+0] = ((pixel
>> 24) & 0xff) | alphaORoperand
;
329 dst
[col
*4+1] = (pixel
>> 0) & 0xff;
330 dst
[col
*4+2] = (pixel
>> 8) & 0xff;
331 dst
[col
*4+3] = (pixel
>> 16) & 0xff;
341 pipe_transfer_unmap(pipe
, trans
);
342 pipe
->transfer_destroy(pipe
, trans
);
350 * Do glReadPixels by getting rows from the framebuffer transfer with
351 * get_tile(). Convert to requested format/type with Mesa image routines.
352 * Image transfer ops are done in software too.
355 st_readpixels(struct gl_context
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
356 GLenum format
, GLenum type
,
357 const struct gl_pixelstore_attrib
*pack
,
360 struct st_context
*st
= st_context(ctx
);
361 struct pipe_context
*pipe
= st
->pipe
;
363 GLbitfield transferOps
= ctx
->_ImageTransferState
;
365 GLint yStep
, dfStride
;
367 struct st_renderbuffer
*strb
;
368 struct gl_pixelstore_attrib clippedPacking
= *pack
;
369 struct pipe_transfer
*trans
;
370 enum pipe_format pformat
;
372 assert(ctx
->ReadBuffer
->Width
> 0);
374 st_validate_state(st
);
376 /* Do all needed clipping here, so that we can forget about it later */
377 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
378 /* The ReadPixels transfer is totally outside the window bounds */
382 st_flush_bitmap_cache(st
);
384 dest
= _mesa_map_pbo_dest(ctx
, &clippedPacking
, dest
);
388 if (format
== GL_STENCIL_INDEX
||
389 format
== GL_DEPTH_STENCIL
) {
390 st_read_stencil_pixels(ctx
, x
, y
, width
, height
,
391 format
, type
, pack
, dest
);
394 else if (format
== GL_DEPTH_COMPONENT
) {
395 strb
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
396 if (strb
->Base
.Wrapped
) {
397 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
401 /* Read color buffer */
402 strb
= st_get_color_read_renderbuffer(ctx
);
408 /* try a fast-path readpixels before anything else */
409 if (st_fast_readpixels(ctx
, strb
, x
, y
, width
, height
,
410 format
, type
, pack
, dest
)) {
412 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
416 /* allocate temp pixel row buffer */
417 temp
= (GLfloat (*)[4]) malloc(4 * width
* sizeof(GLfloat
));
419 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
423 if(ctx
->Color
._ClampReadColor
)
424 transferOps
|= IMAGE_CLAMP_BIT
;
426 if (format
== GL_RGBA
&& type
== GL_FLOAT
&& !transferOps
) {
427 /* write tile(row) directly into user's buffer */
428 df
= (GLfloat
*) _mesa_image_address2d(&clippedPacking
, dest
, width
,
429 height
, format
, type
, 0, 0);
430 dfStride
= width
* 4;
433 /* write tile(row) into temp row buffer */
434 df
= (GLfloat
*) temp
;
438 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
439 /* convert GL Y to Gallium Y */
440 y
= strb
->Base
.Height
- y
- height
;
443 /* Create a read transfer from the renderbuffer's texture */
444 trans
= pipe_get_transfer(pipe
, strb
->texture
,
447 x
, y
, width
, height
);
449 /* determine bottom-to-top vs. top-to-bottom order */
450 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
459 /* possibly convert sRGB format to linear RGB format */
460 pformat
= util_format_linear(trans
->resource
->format
);
462 if (ST_DEBUG
& DEBUG_FALLBACK
)
463 debug_printf("%s: fallback processing\n", __FUNCTION__
);
466 * Copy pixels from pipe_transfer to user memory
469 /* dest of first pixel in client memory */
470 GLubyte
*dst
= _mesa_image_address2d(&clippedPacking
, dest
, width
,
471 height
, format
, type
, 0, 0);
472 /* dest row stride */
473 const GLint dstStride
= _mesa_image_row_stride(&clippedPacking
, width
,
476 if (pformat
== PIPE_FORMAT_Z24_UNORM_S8_USCALED
||
477 pformat
== PIPE_FORMAT_Z24X8_UNORM
) {
478 if (format
== GL_DEPTH_COMPONENT
) {
479 for (i
= 0; i
< height
; i
++) {
480 GLuint ztemp
[MAX_WIDTH
];
481 GLfloat zfloat
[MAX_WIDTH
];
482 const double scale
= 1.0 / ((1 << 24) - 1);
483 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
485 for (j
= 0; j
< width
; j
++) {
486 zfloat
[j
] = (float) (scale
* (ztemp
[j
] & 0xffffff));
488 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
489 zfloat
, &clippedPacking
);
494 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
495 assert(format
== GL_DEPTH_STENCIL_EXT
);
496 for (i
= 0; i
< height
; i
++) {
497 GLuint
*zshort
= (GLuint
*)dst
;
498 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, dst
, 0);
500 /* Reverse into 24/8 */
501 for (j
= 0; j
< width
; j
++) {
502 zshort
[j
] = (zshort
[j
] << 8) | (zshort
[j
] >> 24);
508 else if (pformat
== PIPE_FORMAT_S8_USCALED_Z24_UNORM
||
509 pformat
== PIPE_FORMAT_X8Z24_UNORM
) {
510 if (format
== GL_DEPTH_COMPONENT
) {
511 for (i
= 0; i
< height
; i
++) {
512 GLuint ztemp
[MAX_WIDTH
];
513 GLfloat zfloat
[MAX_WIDTH
];
514 const double scale
= 1.0 / ((1 << 24) - 1);
515 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
517 for (j
= 0; j
< width
; j
++) {
518 zfloat
[j
] = (float) (scale
* ((ztemp
[j
] >> 8) & 0xffffff));
520 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
521 zfloat
, &clippedPacking
);
526 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
527 assert(format
== GL_DEPTH_STENCIL_EXT
);
528 for (i
= 0; i
< height
; i
++) {
529 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, dst
, 0);
535 else if (pformat
== PIPE_FORMAT_Z16_UNORM
) {
536 for (i
= 0; i
< height
; i
++) {
537 GLushort ztemp
[MAX_WIDTH
];
538 GLfloat zfloat
[MAX_WIDTH
];
539 const double scale
= 1.0 / 0xffff;
540 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
542 for (j
= 0; j
< width
; j
++) {
543 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
545 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
546 zfloat
, &clippedPacking
);
550 else if (pformat
== PIPE_FORMAT_Z32_UNORM
) {
551 for (i
= 0; i
< height
; i
++) {
552 GLuint ztemp
[MAX_WIDTH
];
553 GLfloat zfloat
[MAX_WIDTH
];
554 const double scale
= 1.0 / 0xffffffff;
555 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
557 for (j
= 0; j
< width
; j
++) {
558 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
560 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
561 zfloat
, &clippedPacking
);
567 /* Do a row at a time to flip image data vertically */
568 for (i
= 0; i
< height
; i
++) {
569 pipe_get_tile_rgba_format(pipe
, trans
, 0, y
, width
, 1,
574 _mesa_pack_rgba_span_float(ctx
, width
, temp
, format
, type
, dst
,
575 &clippedPacking
, transferOps
);
584 pipe
->transfer_destroy(pipe
, trans
);
586 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
590 void st_init_readpixels_functions(struct dd_function_table
*functions
)
592 functions
->ReadPixels
= st_readpixels
;