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"
42 #include "pipe/p_context.h"
43 #include "pipe/p_defines.h"
44 #include "util/u_format.h"
45 #include "util/u_inlines.h"
46 #include "util/u_tile.h"
49 #include "st_context.h"
51 #include "st_cb_bitmap.h"
52 #include "st_cb_readpixels.h"
53 #include "st_cb_fbo.h"
56 * Special case for reading stencil buffer.
57 * For color/depth we use get_tile(). For stencil, map the stencil buffer.
60 st_read_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
61 GLsizei width
, GLsizei height
,
62 GLenum format
, GLenum type
,
63 const struct gl_pixelstore_attrib
*packing
,
66 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
67 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
68 struct st_renderbuffer
*strb
= st_renderbuffer(fb
->_StencilBuffer
);
69 struct pipe_transfer
*pt
;
73 if (strb
->Base
.Wrapped
) {
74 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
77 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
78 y
= ctx
->DrawBuffer
->Height
- y
- height
;
81 /* Create a read transfer from the renderbuffer's texture */
83 pt
= pipe_get_transfer(pipe
, strb
->texture
,
88 /* map the stencil buffer */
89 stmap
= pipe_transfer_map(pipe
, pt
);
91 /* width should never be > MAX_WIDTH since we did clipping earlier */
92 ASSERT(width
<= MAX_WIDTH
);
94 /* process image row by row */
95 for (j
= 0; j
< height
; j
++) {
97 GLstencil sValues
[MAX_WIDTH
];
98 GLfloat zValues
[MAX_WIDTH
];
101 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
102 srcY
= height
- j
- 1;
108 /* get stencil (and Z) values */
109 switch (pt
->resource
->format
) {
110 case PIPE_FORMAT_S8_USCALED
:
112 const ubyte
*src
= stmap
+ srcY
* pt
->stride
;
113 memcpy(sValues
, src
, width
);
116 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
117 if (format
== GL_DEPTH_STENCIL
) {
118 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
119 const GLfloat scale
= 1.0f
/ (0xffffff);
121 for (k
= 0; k
< width
; k
++) {
122 sValues
[k
] = src
[k
] >> 24;
123 zValues
[k
] = (src
[k
] & 0xffffff) * scale
;
127 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
129 for (k
= 0; k
< width
; k
++) {
130 sValues
[k
] = src
[k
] >> 24;
134 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
135 if (format
== GL_DEPTH_STENCIL
) {
136 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
137 const GLfloat scale
= 1.0f
/ (0xffffff);
139 for (k
= 0; k
< width
; k
++) {
140 sValues
[k
] = src
[k
] & 0xff;
141 zValues
[k
] = (src
[k
] >> 8) * scale
;
145 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
147 for (k
= 0; k
< width
; k
++) {
148 sValues
[k
] = src
[k
] & 0xff;
157 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
159 if (format
== GL_DEPTH_STENCIL
) {
160 _mesa_pack_depth_stencil_span(ctx
, width
, dest
,
161 zValues
, sValues
, packing
);
164 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, sValues
, packing
);
168 /* unmap the stencil buffer */
169 pipe_transfer_unmap(pipe
, pt
);
170 pipe
->transfer_destroy(pipe
, pt
);
175 * Return renderbuffer to use for reading color pixels for glRead/CopyPixel
178 struct st_renderbuffer
*
179 st_get_color_read_renderbuffer(struct gl_context
*ctx
)
181 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
182 struct st_renderbuffer
*strb
=
183 st_renderbuffer(fb
->_ColorReadBuffer
);
190 * Try to do glReadPixels in a fast manner for common cases.
191 * \return GL_TRUE for success, GL_FALSE for failure
194 st_fast_readpixels(struct gl_context
*ctx
, struct st_renderbuffer
*strb
,
195 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
196 GLenum format
, GLenum type
,
197 const struct gl_pixelstore_attrib
*pack
,
201 A8R8G8B8_UNORM_TO_RGBA_UBYTE
,
202 A8R8G8B8_UNORM_TO_RGB_UBYTE
,
203 A8R8G8B8_UNORM_TO_BGRA_UINT
206 if (ctx
->_ImageTransferState
)
209 if (strb
->format
== PIPE_FORMAT_B8G8R8A8_UNORM
&&
210 format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
211 combo
= A8R8G8B8_UNORM_TO_RGBA_UBYTE
;
213 else if (strb
->format
== PIPE_FORMAT_B8G8R8A8_UNORM
&&
214 format
== GL_RGB
&& type
== GL_UNSIGNED_BYTE
) {
215 combo
= A8R8G8B8_UNORM_TO_RGB_UBYTE
;
217 else if (strb
->format
== PIPE_FORMAT_B8G8R8A8_UNORM
&&
218 format
== GL_BGRA
&& type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
219 combo
= A8R8G8B8_UNORM_TO_BGRA_UINT
;
225 /*printf("st_fast_readpixels combo %d\n", (GLint) combo);*/
228 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
229 struct pipe_transfer
*trans
;
232 GLint row
, col
, dy
, dstStride
;
234 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
235 /* convert GL Y to Gallium Y */
236 y
= strb
->texture
->height0
- y
- height
;
239 trans
= pipe_get_transfer(pipe
, strb
->texture
,
242 x
, y
, width
, height
);
247 map
= pipe_transfer_map(pipe
, trans
);
249 pipe
->transfer_destroy(pipe
, trans
);
253 /* We always write to the user/dest buffer from low addr to high addr
254 * but the read order depends on renderbuffer orientation
256 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
257 /* read source rows from bottom to top */
262 /* read source rows from top to bottom */
267 dst
= _mesa_image_address2d(pack
, dest
, width
, height
,
269 dstStride
= _mesa_image_row_stride(pack
, width
, format
, type
);
272 case A8R8G8B8_UNORM_TO_RGBA_UBYTE
:
273 for (row
= 0; row
< height
; row
++) {
274 const GLubyte
*src
= map
+ y
* trans
->stride
;
275 for (col
= 0; col
< width
; col
++) {
276 GLuint pixel
= ((GLuint
*) src
)[col
];
277 dst
[col
*4+0] = (pixel
>> 16) & 0xff;
278 dst
[col
*4+1] = (pixel
>> 8) & 0xff;
279 dst
[col
*4+2] = (pixel
>> 0) & 0xff;
280 dst
[col
*4+3] = (pixel
>> 24) & 0xff;
286 case A8R8G8B8_UNORM_TO_RGB_UBYTE
:
287 for (row
= 0; row
< height
; row
++) {
288 const GLubyte
*src
= map
+ y
* trans
->stride
;
289 for (col
= 0; col
< width
; col
++) {
290 GLuint pixel
= ((GLuint
*) src
)[col
];
291 dst
[col
*3+0] = (pixel
>> 16) & 0xff;
292 dst
[col
*3+1] = (pixel
>> 8) & 0xff;
293 dst
[col
*3+2] = (pixel
>> 0) & 0xff;
299 case A8R8G8B8_UNORM_TO_BGRA_UINT
:
300 for (row
= 0; row
< height
; row
++) {
301 const GLubyte
*src
= map
+ y
* trans
->stride
;
302 memcpy(dst
, src
, 4 * width
);
311 pipe_transfer_unmap(pipe
, trans
);
312 pipe
->transfer_destroy(pipe
, trans
);
320 * Do glReadPixels by getting rows from the framebuffer transfer with
321 * get_tile(). Convert to requested format/type with Mesa image routines.
322 * Image transfer ops are done in software too.
325 st_readpixels(struct gl_context
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
326 GLenum format
, GLenum type
,
327 const struct gl_pixelstore_attrib
*pack
,
330 struct st_context
*st
= st_context(ctx
);
331 struct pipe_context
*pipe
= st
->pipe
;
333 const GLbitfield transferOps
= ctx
->_ImageTransferState
;
335 GLint yStep
, dfStride
;
337 struct st_renderbuffer
*strb
;
338 struct gl_pixelstore_attrib clippedPacking
= *pack
;
339 struct pipe_transfer
*trans
;
340 enum pipe_format pformat
;
342 assert(ctx
->ReadBuffer
->Width
> 0);
344 st_validate_state(st
);
346 /* Do all needed clipping here, so that we can forget about it later */
347 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
348 /* The ReadPixels transfer is totally outside the window bounds */
352 st_flush_bitmap_cache(st
);
354 dest
= _mesa_map_pbo_dest(ctx
, &clippedPacking
, dest
);
358 if (format
== GL_STENCIL_INDEX
||
359 format
== GL_DEPTH_STENCIL
) {
360 st_read_stencil_pixels(ctx
, x
, y
, width
, height
,
361 format
, type
, pack
, dest
);
364 else if (format
== GL_DEPTH_COMPONENT
) {
365 strb
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
366 if (strb
->Base
.Wrapped
) {
367 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
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 /* allocate temp pixel row buffer */
387 temp
= (GLfloat (*)[4]) malloc(4 * width
* sizeof(GLfloat
));
389 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
393 if (format
== GL_RGBA
&& type
== GL_FLOAT
) {
394 /* write tile(row) directly into user's buffer */
395 df
= (GLfloat
*) _mesa_image_address2d(&clippedPacking
, dest
, width
,
396 height
, format
, type
, 0, 0);
397 dfStride
= width
* 4;
400 /* write tile(row) into temp row buffer */
401 df
= (GLfloat
*) temp
;
405 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
406 /* convert GL Y to Gallium Y */
407 y
= strb
->Base
.Height
- y
- height
;
410 /* Create a read transfer from the renderbuffer's texture */
411 trans
= pipe_get_transfer(pipe
, strb
->texture
,
414 x
, y
, width
, height
);
416 /* determine bottom-to-top vs. top-to-bottom order */
417 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
426 /* possibly convert sRGB format to linear RGB format */
427 pformat
= util_format_linear(trans
->resource
->format
);
429 if (ST_DEBUG
& DEBUG_FALLBACK
)
430 debug_printf("%s: fallback processing\n", __FUNCTION__
);
433 * Copy pixels from pipe_transfer to user memory
436 /* dest of first pixel in client memory */
437 GLubyte
*dst
= _mesa_image_address2d(&clippedPacking
, dest
, width
,
438 height
, format
, type
, 0, 0);
439 /* dest row stride */
440 const GLint dstStride
= _mesa_image_row_stride(&clippedPacking
, width
,
443 if (pformat
== PIPE_FORMAT_Z24_UNORM_S8_USCALED
||
444 pformat
== PIPE_FORMAT_Z24X8_UNORM
) {
445 if (format
== GL_DEPTH_COMPONENT
) {
446 for (i
= 0; i
< height
; i
++) {
447 GLuint ztemp
[MAX_WIDTH
];
448 GLfloat zfloat
[MAX_WIDTH
];
449 const double scale
= 1.0 / ((1 << 24) - 1);
450 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
452 for (j
= 0; j
< width
; j
++) {
453 zfloat
[j
] = (float) (scale
* (ztemp
[j
] & 0xffffff));
455 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
456 zfloat
, &clippedPacking
);
461 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
462 assert(format
== GL_DEPTH_STENCIL_EXT
);
463 for (i
= 0; i
< height
; i
++) {
464 GLuint
*zshort
= (GLuint
*)dst
;
465 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, dst
, 0);
467 /* Reverse into 24/8 */
468 for (j
= 0; j
< width
; j
++) {
469 zshort
[j
] = (zshort
[j
] << 8) | (zshort
[j
] >> 24);
475 else if (pformat
== PIPE_FORMAT_S8_USCALED_Z24_UNORM
||
476 pformat
== PIPE_FORMAT_X8Z24_UNORM
) {
477 if (format
== GL_DEPTH_COMPONENT
) {
478 for (i
= 0; i
< height
; i
++) {
479 GLuint ztemp
[MAX_WIDTH
];
480 GLfloat zfloat
[MAX_WIDTH
];
481 const double scale
= 1.0 / ((1 << 24) - 1);
482 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
484 for (j
= 0; j
< width
; j
++) {
485 zfloat
[j
] = (float) (scale
* ((ztemp
[j
] >> 8) & 0xffffff));
487 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
488 zfloat
, &clippedPacking
);
493 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
494 assert(format
== GL_DEPTH_STENCIL_EXT
);
495 for (i
= 0; i
< height
; i
++) {
496 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, dst
, 0);
502 else if (pformat
== PIPE_FORMAT_Z16_UNORM
) {
503 for (i
= 0; i
< height
; i
++) {
504 GLushort ztemp
[MAX_WIDTH
];
505 GLfloat zfloat
[MAX_WIDTH
];
506 const double scale
= 1.0 / 0xffff;
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
]);
512 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
513 zfloat
, &clippedPacking
);
517 else if (pformat
== PIPE_FORMAT_Z32_UNORM
) {
518 for (i
= 0; i
< height
; i
++) {
519 GLuint ztemp
[MAX_WIDTH
];
520 GLfloat zfloat
[MAX_WIDTH
];
521 const double scale
= 1.0 / 0xffffffff;
522 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
524 for (j
= 0; j
< width
; j
++) {
525 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
527 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
528 zfloat
, &clippedPacking
);
534 /* Do a row at a time to flip image data vertically */
535 for (i
= 0; i
< height
; i
++) {
536 pipe_get_tile_rgba_format(pipe
, trans
, 0, y
, width
, 1,
541 _mesa_pack_rgba_span_float(ctx
, width
, temp
, format
, type
, dst
,
542 &clippedPacking
, transferOps
);
551 pipe
->transfer_destroy(pipe
, trans
);
553 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
557 void st_init_readpixels_functions(struct dd_function_table
*functions
)
559 functions
->ReadPixels
= st_readpixels
;