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"
49 #include "st_cb_bitmap.h"
50 #include "st_cb_readpixels.h"
51 #include "st_cb_fbo.h"
54 * Special case for reading stencil buffer.
55 * For color/depth we use get_tile(). For stencil, map the stencil buffer.
58 st_read_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
59 GLsizei width
, GLsizei height
,
60 GLenum format
, GLenum type
,
61 const struct gl_pixelstore_attrib
*packing
,
64 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
65 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
66 struct st_renderbuffer
*strb
= st_renderbuffer(fb
->_StencilBuffer
);
67 struct pipe_transfer
*pt
;
71 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
72 y
= ctx
->DrawBuffer
->Height
- y
- height
;
75 /* Create a read transfer from the renderbuffer's texture */
77 pt
= pipe_get_transfer(st_context(ctx
)->pipe
, strb
->texture
,
79 PIPE_TRANSFER_READ
, x
, y
,
82 /* map the stencil buffer */
83 stmap
= pipe_transfer_map(pipe
, pt
);
85 /* width should never be > MAX_WIDTH since we did clipping earlier */
86 ASSERT(width
<= MAX_WIDTH
);
88 /* process image row by row */
89 for (j
= 0; j
< height
; j
++) {
91 GLstencil sValues
[MAX_WIDTH
];
92 GLfloat zValues
[MAX_WIDTH
];
95 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
96 srcY
= height
- j
- 1;
102 /* get stencil (and Z) values */
103 switch (pt
->resource
->format
) {
104 case PIPE_FORMAT_S8_USCALED
:
106 const ubyte
*src
= stmap
+ srcY
* pt
->stride
;
107 memcpy(sValues
, src
, width
);
110 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
111 if (format
== GL_DEPTH_STENCIL
) {
112 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
113 const GLfloat scale
= 1.0f
/ (0xffffff);
115 for (k
= 0; k
< width
; k
++) {
116 sValues
[k
] = src
[k
] >> 24;
117 zValues
[k
] = (src
[k
] & 0xffffff) * scale
;
121 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
123 for (k
= 0; k
< width
; k
++) {
124 sValues
[k
] = src
[k
] >> 24;
128 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
129 if (format
== GL_DEPTH_STENCIL
) {
130 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
131 const GLfloat scale
= 1.0f
/ (0xffffff);
133 for (k
= 0; k
< width
; k
++) {
134 sValues
[k
] = src
[k
] & 0xff;
135 zValues
[k
] = (src
[k
] >> 8) * scale
;
139 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
141 for (k
= 0; k
< width
; k
++) {
142 sValues
[k
] = src
[k
] & 0xff;
151 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
153 if (format
== GL_DEPTH_STENCIL
) {
154 _mesa_pack_depth_stencil_span(ctx
, width
, dest
,
155 zValues
, sValues
, packing
);
158 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, sValues
, packing
);
162 /* unmap the stencil buffer */
163 pipe_transfer_unmap(pipe
, pt
);
164 pipe
->transfer_destroy(pipe
, pt
);
169 * Return renderbuffer to use for reading color pixels for glRead/CopyPixel
172 struct st_renderbuffer
*
173 st_get_color_read_renderbuffer(GLcontext
*ctx
)
175 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
176 struct st_renderbuffer
*strb
=
177 st_renderbuffer(fb
->_ColorReadBuffer
);
184 * Try to do glReadPixels in a fast manner for common cases.
185 * \return GL_TRUE for success, GL_FALSE for failure
188 st_fast_readpixels(GLcontext
*ctx
, struct st_renderbuffer
*strb
,
189 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
190 GLenum format
, GLenum type
,
191 const struct gl_pixelstore_attrib
*pack
,
195 A8R8G8B8_UNORM_TO_RGBA_UBYTE
,
196 A8R8G8B8_UNORM_TO_RGB_UBYTE
,
197 A8R8G8B8_UNORM_TO_BGRA_UINT
200 if (ctx
->_ImageTransferState
)
203 if (strb
->format
== PIPE_FORMAT_B8G8R8A8_UNORM
&&
204 format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
205 combo
= A8R8G8B8_UNORM_TO_RGBA_UBYTE
;
207 else if (strb
->format
== PIPE_FORMAT_B8G8R8A8_UNORM
&&
208 format
== GL_RGB
&& type
== GL_UNSIGNED_BYTE
) {
209 combo
= A8R8G8B8_UNORM_TO_RGB_UBYTE
;
211 else if (strb
->format
== PIPE_FORMAT_B8G8R8A8_UNORM
&&
212 format
== GL_BGRA
&& type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
213 combo
= A8R8G8B8_UNORM_TO_BGRA_UINT
;
219 /*printf("st_fast_readpixels combo %d\n", (GLint) combo);*/
222 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
223 struct pipe_transfer
*trans
;
226 GLint row
, col
, dy
, dstStride
;
228 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
229 /* convert GL Y to Gallium Y */
230 y
= strb
->texture
->height0
- y
- height
;
233 trans
= pipe_get_transfer(st_context(ctx
)->pipe
, strb
->texture
,
235 PIPE_TRANSFER_READ
, x
, y
,
241 map
= pipe_transfer_map(pipe
, trans
);
243 pipe
->transfer_destroy(pipe
, trans
);
247 /* We always write to the user/dest buffer from low addr to high addr
248 * but the read order depends on renderbuffer orientation
250 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
251 /* read source rows from bottom to top */
256 /* read source rows from top to bottom */
261 dst
= _mesa_image_address2d(pack
, dest
, width
, height
,
263 dstStride
= _mesa_image_row_stride(pack
, width
, format
, type
);
266 case A8R8G8B8_UNORM_TO_RGBA_UBYTE
:
267 for (row
= 0; row
< height
; row
++) {
268 const GLubyte
*src
= map
+ y
* trans
->stride
;
269 for (col
= 0; col
< width
; col
++) {
270 GLuint pixel
= ((GLuint
*) src
)[col
];
271 dst
[col
*4+0] = (pixel
>> 16) & 0xff;
272 dst
[col
*4+1] = (pixel
>> 8) & 0xff;
273 dst
[col
*4+2] = (pixel
>> 0) & 0xff;
274 dst
[col
*4+3] = (pixel
>> 24) & 0xff;
280 case A8R8G8B8_UNORM_TO_RGB_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
*3+0] = (pixel
>> 16) & 0xff;
286 dst
[col
*3+1] = (pixel
>> 8) & 0xff;
287 dst
[col
*3+2] = (pixel
>> 0) & 0xff;
293 case A8R8G8B8_UNORM_TO_BGRA_UINT
:
294 for (row
= 0; row
< height
; row
++) {
295 const GLubyte
*src
= map
+ y
* trans
->stride
;
296 memcpy(dst
, src
, 4 * width
);
305 pipe_transfer_unmap(pipe
, trans
);
306 pipe
->transfer_destroy(pipe
, trans
);
314 * Do glReadPixels by getting rows from the framebuffer transfer with
315 * get_tile(). Convert to requested format/type with Mesa image routines.
316 * Image transfer ops are done in software too.
319 st_readpixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
320 GLenum format
, GLenum type
,
321 const struct gl_pixelstore_attrib
*pack
,
324 struct st_context
*st
= st_context(ctx
);
325 struct pipe_context
*pipe
= st
->pipe
;
326 GLfloat temp
[MAX_WIDTH
][4];
327 const GLbitfield transferOps
= ctx
->_ImageTransferState
;
329 GLint yStep
, dfStride
;
331 struct st_renderbuffer
*strb
;
332 struct gl_pixelstore_attrib clippedPacking
= *pack
;
333 struct pipe_transfer
*trans
;
335 assert(ctx
->ReadBuffer
->Width
> 0);
337 /* XXX convolution not done yet */
338 assert((transferOps
& IMAGE_CONVOLUTION_BIT
) == 0);
340 st_validate_state(st
);
342 /* Do all needed clipping here, so that we can forget about it later */
343 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
344 /* The ReadPixels transfer is totally outside the window bounds */
348 st_flush_bitmap_cache(st
);
350 dest
= _mesa_map_pbo_dest(ctx
, &clippedPacking
, dest
);
354 if (format
== GL_STENCIL_INDEX
||
355 format
== GL_DEPTH_STENCIL
) {
356 st_read_stencil_pixels(ctx
, x
, y
, width
, height
,
357 format
, type
, pack
, dest
);
360 else if (format
== GL_DEPTH_COMPONENT
) {
361 strb
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
364 /* Read color buffer */
365 strb
= st_get_color_read_renderbuffer(ctx
);
371 /* try a fast-path readpixels before anything else */
372 if (st_fast_readpixels(ctx
, strb
, x
, y
, width
, height
,
373 format
, type
, pack
, dest
)) {
375 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
379 if (format
== GL_RGBA
&& type
== GL_FLOAT
) {
380 /* write tile(row) directly into user's buffer */
381 df
= (GLfloat
*) _mesa_image_address2d(&clippedPacking
, dest
, width
,
382 height
, format
, type
, 0, 0);
383 dfStride
= width
* 4;
386 /* write tile(row) into temp row buffer */
387 df
= (GLfloat
*) temp
;
391 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
392 /* convert GL Y to Gallium Y */
393 y
= strb
->Base
.Height
- y
- height
;
396 /* Create a read transfer from the renderbuffer's texture */
397 trans
= pipe_get_transfer(st_context(ctx
)->pipe
, strb
->texture
,
399 PIPE_TRANSFER_READ
, x
, y
,
402 /* determine bottom-to-top vs. top-to-bottom order */
403 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
412 if (ST_DEBUG
& DEBUG_FALLBACK
)
413 debug_printf("%s: fallback processing\n", __FUNCTION__
);
416 * Copy pixels from pipe_transfer to user memory
419 /* dest of first pixel in client memory */
420 GLubyte
*dst
= _mesa_image_address2d(&clippedPacking
, dest
, width
,
421 height
, format
, type
, 0, 0);
422 /* dest row stride */
423 const GLint dstStride
= _mesa_image_row_stride(&clippedPacking
, width
,
426 if (trans
->resource
->format
== PIPE_FORMAT_Z24_UNORM_S8_USCALED
||
427 trans
->resource
->format
== PIPE_FORMAT_Z24X8_UNORM
) {
428 if (format
== GL_DEPTH_COMPONENT
) {
429 for (i
= 0; i
< height
; i
++) {
430 GLuint ztemp
[MAX_WIDTH
];
431 GLfloat zfloat
[MAX_WIDTH
];
432 const double scale
= 1.0 / ((1 << 24) - 1);
433 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
435 for (j
= 0; j
< width
; j
++) {
436 zfloat
[j
] = (float) (scale
* (ztemp
[j
] & 0xffffff));
438 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
439 zfloat
, &clippedPacking
);
444 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
445 assert(format
== GL_DEPTH_STENCIL_EXT
);
446 for (i
= 0; i
< height
; i
++) {
447 GLuint
*zshort
= (GLuint
*)dst
;
448 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, dst
, 0);
450 /* Reverse into 24/8 */
451 for (j
= 0; j
< width
; j
++) {
452 zshort
[j
] = (zshort
[j
] << 8) | (zshort
[j
] >> 24);
458 else if (trans
->resource
->format
== PIPE_FORMAT_S8_USCALED_Z24_UNORM
||
459 trans
->resource
->format
== PIPE_FORMAT_X8Z24_UNORM
) {
460 if (format
== GL_DEPTH_COMPONENT
) {
461 for (i
= 0; i
< height
; i
++) {
462 GLuint ztemp
[MAX_WIDTH
];
463 GLfloat zfloat
[MAX_WIDTH
];
464 const double scale
= 1.0 / ((1 << 24) - 1);
465 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
467 for (j
= 0; j
< width
; j
++) {
468 zfloat
[j
] = (float) (scale
* ((ztemp
[j
] >> 8) & 0xffffff));
470 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
471 zfloat
, &clippedPacking
);
476 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
477 assert(format
== GL_DEPTH_STENCIL_EXT
);
478 for (i
= 0; i
< height
; i
++) {
479 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, dst
, 0);
485 else if (trans
->resource
->format
== PIPE_FORMAT_Z16_UNORM
) {
486 for (i
= 0; i
< height
; i
++) {
487 GLushort ztemp
[MAX_WIDTH
];
488 GLfloat zfloat
[MAX_WIDTH
];
489 const double scale
= 1.0 / 0xffff;
490 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
492 for (j
= 0; j
< width
; j
++) {
493 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
495 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
496 zfloat
, &clippedPacking
);
500 else if (trans
->resource
->format
== PIPE_FORMAT_Z32_UNORM
) {
501 for (i
= 0; i
< height
; i
++) {
502 GLuint ztemp
[MAX_WIDTH
];
503 GLfloat zfloat
[MAX_WIDTH
];
504 const double scale
= 1.0 / 0xffffffff;
505 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
507 for (j
= 0; j
< width
; j
++) {
508 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
510 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
511 zfloat
, &clippedPacking
);
517 /* Do a row at a time to flip image data vertically */
518 for (i
= 0; i
< height
; i
++) {
519 pipe_get_tile_rgba(pipe
, trans
, 0, y
, width
, 1, df
);
523 _mesa_pack_rgba_span_float(ctx
, width
, temp
, format
, type
, dst
,
524 &clippedPacking
, transferOps
);
531 pipe
->transfer_destroy(pipe
, trans
);
533 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
537 void st_init_readpixels_functions(struct dd_function_table
*functions
)
539 functions
->ReadPixels
= st_readpixels
;