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_readpixels.h"
50 #include "st_cb_fbo.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_context
*pipe
= st_context(ctx
)->pipe
;
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
= pipe_transfer_map(pipe
, 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
->resource
->format
) {
105 case PIPE_FORMAT_S8_USCALED
:
107 const ubyte
*src
= stmap
+ srcY
* pt
->stride
;
108 memcpy(sValues
, src
, width
);
111 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
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_S8_USCALED_Z24_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 pipe_transfer_unmap(pipe
, pt
);
165 pipe
->transfer_destroy(pipe
, pt
);
170 * Return renderbuffer to use for reading color pixels for glRead/CopyPixel
173 struct st_renderbuffer
*
174 st_get_color_read_renderbuffer(GLcontext
*ctx
)
176 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
177 struct st_renderbuffer
*strb
=
178 st_renderbuffer(fb
->_ColorReadBuffer
);
185 * Try to do glReadPixels in a fast manner for common cases.
186 * \return GL_TRUE for success, GL_FALSE for failure
189 st_fast_readpixels(GLcontext
*ctx
, struct st_renderbuffer
*strb
,
190 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
191 GLenum format
, GLenum type
,
192 const struct gl_pixelstore_attrib
*pack
,
196 A8R8G8B8_UNORM_TO_RGBA_UBYTE
,
197 A8R8G8B8_UNORM_TO_RGB_UBYTE
,
198 A8R8G8B8_UNORM_TO_BGRA_UINT
201 if (ctx
->_ImageTransferState
)
204 if (strb
->format
== PIPE_FORMAT_B8G8R8A8_UNORM
&&
205 format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
206 combo
= A8R8G8B8_UNORM_TO_RGBA_UBYTE
;
208 else if (strb
->format
== PIPE_FORMAT_B8G8R8A8_UNORM
&&
209 format
== GL_RGB
&& type
== GL_UNSIGNED_BYTE
) {
210 combo
= A8R8G8B8_UNORM_TO_RGB_UBYTE
;
212 else if (strb
->format
== PIPE_FORMAT_B8G8R8A8_UNORM
&&
213 format
== GL_BGRA
&& type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
214 combo
= A8R8G8B8_UNORM_TO_BGRA_UINT
;
220 /*printf("st_fast_readpixels combo %d\n", (GLint) combo);*/
223 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
224 struct pipe_transfer
*trans
;
227 GLint row
, col
, dy
, dstStride
;
229 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
230 /* convert GL Y to Gallium Y */
231 y
= strb
->texture
->height0
- y
- height
;
234 trans
= st_cond_flush_get_tex_transfer(st_context(ctx
), strb
->texture
,
236 PIPE_TRANSFER_READ
, x
, y
,
242 map
= pipe_transfer_map(pipe
, trans
);
244 pipe
->transfer_destroy(pipe
, trans
);
248 /* We always write to the user/dest buffer from low addr to high addr
249 * but the read order depends on renderbuffer orientation
251 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
252 /* read source rows from bottom to top */
257 /* read source rows from top to bottom */
262 dst
= _mesa_image_address2d(pack
, dest
, width
, height
,
264 dstStride
= _mesa_image_row_stride(pack
, width
, format
, type
);
267 case A8R8G8B8_UNORM_TO_RGBA_UBYTE
:
268 for (row
= 0; row
< height
; row
++) {
269 const GLubyte
*src
= map
+ y
* trans
->stride
;
270 for (col
= 0; col
< width
; col
++) {
271 GLuint pixel
= ((GLuint
*) src
)[col
];
272 dst
[col
*4+0] = (pixel
>> 16) & 0xff;
273 dst
[col
*4+1] = (pixel
>> 8) & 0xff;
274 dst
[col
*4+2] = (pixel
>> 0) & 0xff;
275 dst
[col
*4+3] = (pixel
>> 24) & 0xff;
281 case A8R8G8B8_UNORM_TO_RGB_UBYTE
:
282 for (row
= 0; row
< height
; row
++) {
283 const GLubyte
*src
= map
+ y
* trans
->stride
;
284 for (col
= 0; col
< width
; col
++) {
285 GLuint pixel
= ((GLuint
*) src
)[col
];
286 dst
[col
*3+0] = (pixel
>> 16) & 0xff;
287 dst
[col
*3+1] = (pixel
>> 8) & 0xff;
288 dst
[col
*3+2] = (pixel
>> 0) & 0xff;
294 case A8R8G8B8_UNORM_TO_BGRA_UINT
:
295 for (row
= 0; row
< height
; row
++) {
296 const GLubyte
*src
= map
+ y
* trans
->stride
;
297 memcpy(dst
, src
, 4 * width
);
306 pipe_transfer_unmap(pipe
, trans
);
307 pipe
->transfer_destroy(pipe
, trans
);
315 * Do glReadPixels by getting rows from the framebuffer transfer with
316 * get_tile(). Convert to requested format/type with Mesa image routines.
317 * Image transfer ops are done in software too.
320 st_readpixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
321 GLenum format
, GLenum type
,
322 const struct gl_pixelstore_attrib
*pack
,
325 struct st_context
*st
= st_context(ctx
);
326 struct pipe_context
*pipe
= st
->pipe
;
327 GLfloat temp
[MAX_WIDTH
][4];
328 const GLbitfield transferOps
= ctx
->_ImageTransferState
;
330 GLint yStep
, dfStride
;
332 struct st_renderbuffer
*strb
;
333 struct gl_pixelstore_attrib clippedPacking
= *pack
;
334 struct pipe_transfer
*trans
;
336 assert(ctx
->ReadBuffer
->Width
> 0);
338 /* XXX convolution not done yet */
339 assert((transferOps
& IMAGE_CONVOLUTION_BIT
) == 0);
341 st_validate_state(st
);
343 /* Do all needed clipping here, so that we can forget about it later */
344 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
345 /* The ReadPixels transfer is totally outside the window bounds */
349 dest
= _mesa_map_pbo_dest(ctx
, &clippedPacking
, dest
);
353 st_flush(st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
355 if (format
== GL_STENCIL_INDEX
||
356 format
== GL_DEPTH_STENCIL
) {
357 st_read_stencil_pixels(ctx
, x
, y
, width
, height
,
358 format
, type
, pack
, dest
);
361 else if (format
== GL_DEPTH_COMPONENT
) {
362 strb
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
365 /* Read color buffer */
366 strb
= st_get_color_read_renderbuffer(ctx
);
372 /* try a fast-path readpixels before anything else */
373 if (st_fast_readpixels(ctx
, strb
, x
, y
, width
, height
,
374 format
, type
, pack
, dest
)) {
376 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
380 if (format
== GL_RGBA
&& type
== GL_FLOAT
) {
381 /* write tile(row) directly into user's buffer */
382 df
= (GLfloat
*) _mesa_image_address2d(&clippedPacking
, dest
, width
,
383 height
, format
, type
, 0, 0);
384 dfStride
= width
* 4;
387 /* write tile(row) into temp row buffer */
388 df
= (GLfloat
*) temp
;
392 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
393 /* convert GL Y to Gallium Y */
394 y
= strb
->Base
.Height
- y
- height
;
397 /* Create a read transfer from the renderbuffer's texture */
398 trans
= st_cond_flush_get_tex_transfer(st_context(ctx
), strb
->texture
,
400 PIPE_TRANSFER_READ
, x
, y
,
403 /* determine bottom-to-top vs. top-to-bottom order */
404 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
413 if (ST_DEBUG
& DEBUG_FALLBACK
)
414 debug_printf("%s: fallback processing\n", __FUNCTION__
);
417 * Copy pixels from pipe_transfer to user memory
420 /* dest of first pixel in client memory */
421 GLubyte
*dst
= _mesa_image_address2d(&clippedPacking
, dest
, width
,
422 height
, format
, type
, 0, 0);
423 /* dest row stride */
424 const GLint dstStride
= _mesa_image_row_stride(&clippedPacking
, width
,
427 if (trans
->resource
->format
== PIPE_FORMAT_Z24_UNORM_S8_USCALED
||
428 trans
->resource
->format
== PIPE_FORMAT_Z24X8_UNORM
) {
429 if (format
== GL_DEPTH_COMPONENT
) {
430 for (i
= 0; i
< height
; i
++) {
431 GLuint ztemp
[MAX_WIDTH
];
432 GLfloat zfloat
[MAX_WIDTH
];
433 const double scale
= 1.0 / ((1 << 24) - 1);
434 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
436 for (j
= 0; j
< width
; j
++) {
437 zfloat
[j
] = (float) (scale
* (ztemp
[j
] & 0xffffff));
439 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
440 zfloat
, &clippedPacking
);
445 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
446 assert(format
== GL_DEPTH_STENCIL_EXT
);
447 for (i
= 0; i
< height
; i
++) {
448 GLuint
*zshort
= (GLuint
*)dst
;
449 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, dst
, 0);
451 /* Reverse into 24/8 */
452 for (j
= 0; j
< width
; j
++) {
453 zshort
[j
] = (zshort
[j
] << 8) | (zshort
[j
] >> 24);
459 else if (trans
->resource
->format
== PIPE_FORMAT_S8_USCALED_Z24_UNORM
||
460 trans
->resource
->format
== PIPE_FORMAT_X8Z24_UNORM
) {
461 if (format
== GL_DEPTH_COMPONENT
) {
462 for (i
= 0; i
< height
; i
++) {
463 GLuint ztemp
[MAX_WIDTH
];
464 GLfloat zfloat
[MAX_WIDTH
];
465 const double scale
= 1.0 / ((1 << 24) - 1);
466 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
468 for (j
= 0; j
< width
; j
++) {
469 zfloat
[j
] = (float) (scale
* ((ztemp
[j
] >> 8) & 0xffffff));
471 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
472 zfloat
, &clippedPacking
);
477 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
478 assert(format
== GL_DEPTH_STENCIL_EXT
);
479 for (i
= 0; i
< height
; i
++) {
480 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, dst
, 0);
486 else if (trans
->resource
->format
== PIPE_FORMAT_Z16_UNORM
) {
487 for (i
= 0; i
< height
; i
++) {
488 GLushort ztemp
[MAX_WIDTH
];
489 GLfloat zfloat
[MAX_WIDTH
];
490 const double scale
= 1.0 / 0xffff;
491 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
493 for (j
= 0; j
< width
; j
++) {
494 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
496 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
497 zfloat
, &clippedPacking
);
501 else if (trans
->resource
->format
== PIPE_FORMAT_Z32_UNORM
) {
502 for (i
= 0; i
< height
; i
++) {
503 GLuint ztemp
[MAX_WIDTH
];
504 GLfloat zfloat
[MAX_WIDTH
];
505 const double scale
= 1.0 / 0xffffffff;
506 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
508 for (j
= 0; j
< width
; j
++) {
509 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
511 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
512 zfloat
, &clippedPacking
);
518 /* Do a row at a time to flip image data vertically */
519 for (i
= 0; i
< height
; i
++) {
520 pipe_get_tile_rgba(pipe
, trans
, 0, y
, width
, 1, df
);
524 _mesa_pack_rgba_span_float(ctx
, width
, temp
, format
, type
, dst
,
525 &clippedPacking
, transferOps
);
532 pipe
->transfer_destroy(pipe
, trans
);
534 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
538 void st_init_readpixels_functions(struct dd_function_table
*functions
)
540 functions
->ReadPixels
= st_readpixels
;