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_public.h"
52 #include "st_texture.h"
53 #include "st_inlines.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(GLcontext
*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
= ctx
->st
->pipe
;
68 struct st_renderbuffer
*strb
= st_renderbuffer(fb
->_StencilBuffer
);
69 struct pipe_transfer
*pt
;
73 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
74 y
= ctx
->DrawBuffer
->Height
- y
- height
;
77 /* Create a read transfer from the renderbuffer's texture */
79 pt
= st_cond_flush_get_tex_transfer(st_context(ctx
), strb
->texture
,
81 PIPE_TRANSFER_READ
, x
, y
,
84 /* map the stencil buffer */
85 stmap
= pipe
->transfer_map(pipe
, pt
);
87 /* width should never be > MAX_WIDTH since we did clipping earlier */
88 ASSERT(width
<= MAX_WIDTH
);
90 /* process image row by row */
91 for (j
= 0; j
< height
; j
++) {
93 GLstencil sValues
[MAX_WIDTH
];
94 GLfloat zValues
[MAX_WIDTH
];
97 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
98 srcY
= height
- j
- 1;
104 /* get stencil (and Z) values */
105 switch (pt
->texture
->format
) {
106 case PIPE_FORMAT_S8_UNORM
:
108 const ubyte
*src
= stmap
+ srcY
* pt
->stride
;
109 memcpy(sValues
, src
, width
);
112 case PIPE_FORMAT_Z24S8_UNORM
:
113 if (format
== GL_DEPTH_STENCIL
) {
114 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
115 const GLfloat scale
= 1.0f
/ (0xffffff);
117 for (k
= 0; k
< width
; k
++) {
118 sValues
[k
] = src
[k
] >> 24;
119 zValues
[k
] = (src
[k
] & 0xffffff) * scale
;
123 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
125 for (k
= 0; k
< width
; k
++) {
126 sValues
[k
] = src
[k
] >> 24;
130 case PIPE_FORMAT_S8Z24_UNORM
:
131 if (format
== GL_DEPTH_STENCIL
) {
132 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
133 const GLfloat scale
= 1.0f
/ (0xffffff);
135 for (k
= 0; k
< width
; k
++) {
136 sValues
[k
] = src
[k
] & 0xff;
137 zValues
[k
] = (src
[k
] >> 8) * scale
;
141 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
143 for (k
= 0; k
< width
; k
++) {
144 sValues
[k
] = src
[k
] & 0xff;
153 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
155 if (format
== GL_DEPTH_STENCIL
) {
156 _mesa_pack_depth_stencil_span(ctx
, width
, dest
,
157 zValues
, sValues
, packing
);
160 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, sValues
, packing
);
164 /* unmap the stencil buffer */
165 pipe
->transfer_unmap(pipe
, pt
);
166 pipe
->tex_transfer_destroy(pipe
, pt
);
171 * Return renderbuffer to use for reading color pixels for glRead/CopyPixel
173 * Special care is needed for the front buffer.
175 struct st_renderbuffer
*
176 st_get_color_read_renderbuffer(GLcontext
*ctx
)
178 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
179 struct st_renderbuffer
*strb
=
180 st_renderbuffer(fb
->_ColorReadBuffer
);
181 struct st_renderbuffer
*front
=
182 st_renderbuffer(fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
185 && ctx
->st
->frontbuffer_status
== FRONT_STATUS_COPY_OF_BACK
) {
186 /* reading from front color buffer, which is a logical copy of the
189 struct st_renderbuffer
*back
=
190 st_renderbuffer(fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
199 * Try to do glReadPixels in a fast manner for common cases.
200 * \return GL_TRUE for success, GL_FALSE for failure
203 st_fast_readpixels(GLcontext
*ctx
, struct st_renderbuffer
*strb
,
204 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
205 GLenum format
, GLenum type
,
206 const struct gl_pixelstore_attrib
*pack
,
210 A8R8G8B8_UNORM_TO_RGBA_UBYTE
,
211 A8R8G8B8_UNORM_TO_RGB_UBYTE
,
212 A8R8G8B8_UNORM_TO_BGRA_UINT
215 if (ctx
->_ImageTransferState
)
218 if (strb
->format
== PIPE_FORMAT_B8G8R8A8_UNORM
&&
219 format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
220 combo
= A8R8G8B8_UNORM_TO_RGBA_UBYTE
;
222 else if (strb
->format
== PIPE_FORMAT_B8G8R8A8_UNORM
&&
223 format
== GL_RGB
&& type
== GL_UNSIGNED_BYTE
) {
224 combo
= A8R8G8B8_UNORM_TO_RGB_UBYTE
;
226 else if (strb
->format
== PIPE_FORMAT_B8G8R8A8_UNORM
&&
227 format
== GL_BGRA
&& type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
228 combo
= A8R8G8B8_UNORM_TO_BGRA_UINT
;
234 /*printf("st_fast_readpixels combo %d\n", (GLint) combo);*/
237 struct pipe_context
*pipe
= ctx
->st
->pipe
;
238 struct pipe_transfer
*trans
;
241 GLint row
, col
, dy
, dstStride
;
243 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
244 /* convert GL Y to Gallium Y */
245 y
= strb
->texture
->height0
- y
- height
;
248 trans
= st_cond_flush_get_tex_transfer(st_context(ctx
), strb
->texture
,
250 PIPE_TRANSFER_READ
, x
, y
,
256 map
= pipe
->transfer_map(pipe
, trans
);
258 pipe
->tex_transfer_destroy(pipe
, trans
);
262 /* We always write to the user/dest buffer from low addr to high addr
263 * but the read order depends on renderbuffer orientation
265 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
266 /* read source rows from bottom to top */
271 /* read source rows from top to bottom */
276 dst
= _mesa_image_address2d(pack
, dest
, width
, height
,
278 dstStride
= _mesa_image_row_stride(pack
, width
, format
, type
);
281 case A8R8G8B8_UNORM_TO_RGBA_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
*4+0] = (pixel
>> 16) & 0xff;
287 dst
[col
*4+1] = (pixel
>> 8) & 0xff;
288 dst
[col
*4+2] = (pixel
>> 0) & 0xff;
289 dst
[col
*4+3] = (pixel
>> 24) & 0xff;
295 case A8R8G8B8_UNORM_TO_RGB_UBYTE
:
296 for (row
= 0; row
< height
; row
++) {
297 const GLubyte
*src
= map
+ y
* trans
->stride
;
298 for (col
= 0; col
< width
; col
++) {
299 GLuint pixel
= ((GLuint
*) src
)[col
];
300 dst
[col
*3+0] = (pixel
>> 16) & 0xff;
301 dst
[col
*3+1] = (pixel
>> 8) & 0xff;
302 dst
[col
*3+2] = (pixel
>> 0) & 0xff;
308 case A8R8G8B8_UNORM_TO_BGRA_UINT
:
309 for (row
= 0; row
< height
; row
++) {
310 const GLubyte
*src
= map
+ y
* trans
->stride
;
311 memcpy(dst
, src
, 4 * width
);
320 pipe
->transfer_unmap(pipe
, trans
);
321 pipe
->tex_transfer_destroy(pipe
, trans
);
329 * Do glReadPixels by getting rows from the framebuffer transfer with
330 * get_tile(). Convert to requested format/type with Mesa image routines.
331 * Image transfer ops are done in software too.
334 st_readpixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
335 GLenum format
, GLenum type
,
336 const struct gl_pixelstore_attrib
*pack
,
339 struct pipe_context
*pipe
= ctx
->st
->pipe
;
340 GLfloat temp
[MAX_WIDTH
][4];
341 const GLbitfield transferOps
= ctx
->_ImageTransferState
;
343 GLint yStep
, dfStride
;
345 struct st_renderbuffer
*strb
;
346 struct gl_pixelstore_attrib clippedPacking
= *pack
;
347 struct pipe_transfer
*trans
;
349 assert(ctx
->ReadBuffer
->Width
> 0);
351 /* XXX convolution not done yet */
352 assert((transferOps
& IMAGE_CONVOLUTION_BIT
) == 0);
354 st_validate_state(ctx
->st
);
356 /* Do all needed clipping here, so that we can forget about it later */
357 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
358 /* The ReadPixels transfer is totally outside the window bounds */
362 dest
= _mesa_map_pbo_dest(ctx
, &clippedPacking
, dest
);
366 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
368 if (format
== GL_STENCIL_INDEX
||
369 format
== GL_DEPTH_STENCIL
) {
370 st_read_stencil_pixels(ctx
, x
, y
, width
, height
,
371 format
, type
, pack
, dest
);
374 else if (format
== GL_DEPTH_COMPONENT
) {
375 strb
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
378 /* Read color buffer */
379 strb
= st_get_color_read_renderbuffer(ctx
);
385 /* try a fast-path readpixels before anything else */
386 if (st_fast_readpixels(ctx
, strb
, x
, y
, width
, height
,
387 format
, type
, pack
, dest
)) {
389 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
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
= st_cond_flush_get_tex_transfer(st_context(ctx
), strb
->texture
,
413 PIPE_TRANSFER_READ
, x
, y
,
416 /* determine bottom-to-top vs. top-to-bottom order */
417 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
426 if (ST_DEBUG
& DEBUG_FALLBACK
)
427 debug_printf("%s: fallback processing\n", __FUNCTION__
);
430 * Copy pixels from pipe_transfer to user memory
433 /* dest of first pixel in client memory */
434 GLubyte
*dst
= _mesa_image_address2d(&clippedPacking
, dest
, width
,
435 height
, format
, type
, 0, 0);
436 /* dest row stride */
437 const GLint dstStride
= _mesa_image_row_stride(&clippedPacking
, width
,
440 if (trans
->texture
->format
== PIPE_FORMAT_Z24S8_UNORM
||
441 trans
->texture
->format
== PIPE_FORMAT_Z24X8_UNORM
) {
442 if (format
== GL_DEPTH_COMPONENT
) {
443 for (i
= 0; i
< height
; i
++) {
444 GLuint ztemp
[MAX_WIDTH
];
445 GLfloat zfloat
[MAX_WIDTH
];
446 const double scale
= 1.0 / ((1 << 24) - 1);
447 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
449 for (j
= 0; j
< width
; j
++) {
450 zfloat
[j
] = (float) (scale
* (ztemp
[j
] & 0xffffff));
452 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
453 zfloat
, &clippedPacking
);
458 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
459 assert(format
== GL_DEPTH_STENCIL_EXT
);
460 for (i
= 0; i
< height
; i
++) {
461 GLuint
*zshort
= (GLuint
*)dst
;
462 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, dst
, 0);
464 /* Reverse into 24/8 */
465 for (j
= 0; j
< width
; j
++) {
466 zshort
[j
] = (zshort
[j
] << 8) | (zshort
[j
] >> 24);
472 else if (trans
->texture
->format
== PIPE_FORMAT_S8Z24_UNORM
||
473 trans
->texture
->format
== PIPE_FORMAT_X8Z24_UNORM
) {
474 if (format
== GL_DEPTH_COMPONENT
) {
475 for (i
= 0; i
< height
; i
++) {
476 GLuint ztemp
[MAX_WIDTH
];
477 GLfloat zfloat
[MAX_WIDTH
];
478 const double scale
= 1.0 / ((1 << 24) - 1);
479 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
481 for (j
= 0; j
< width
; j
++) {
482 zfloat
[j
] = (float) (scale
* ((ztemp
[j
] >> 8) & 0xffffff));
484 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
485 zfloat
, &clippedPacking
);
490 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
491 assert(format
== GL_DEPTH_STENCIL_EXT
);
492 for (i
= 0; i
< height
; i
++) {
493 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, dst
, 0);
499 else if (trans
->texture
->format
== PIPE_FORMAT_Z16_UNORM
) {
500 for (i
= 0; i
< height
; i
++) {
501 GLushort ztemp
[MAX_WIDTH
];
502 GLfloat zfloat
[MAX_WIDTH
];
503 const double scale
= 1.0 / 0xffff;
504 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
506 for (j
= 0; j
< width
; j
++) {
507 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
509 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
510 zfloat
, &clippedPacking
);
514 else if (trans
->texture
->format
== PIPE_FORMAT_Z32_UNORM
) {
515 for (i
= 0; i
< height
; i
++) {
516 GLuint ztemp
[MAX_WIDTH
];
517 GLfloat zfloat
[MAX_WIDTH
];
518 const double scale
= 1.0 / 0xffffffff;
519 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
521 for (j
= 0; j
< width
; j
++) {
522 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
524 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
525 zfloat
, &clippedPacking
);
531 /* Do a row at a time to flip image data vertically */
532 for (i
= 0; i
< height
; i
++) {
533 pipe_get_tile_rgba(pipe
, trans
, 0, y
, width
, 1, df
);
537 _mesa_pack_rgba_span_float(ctx
, width
, temp
, format
, type
, dst
,
538 &clippedPacking
, transferOps
);
545 pipe
->tex_transfer_destroy(pipe
, trans
);
547 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
551 void st_init_readpixels_functions(struct dd_function_table
*functions
)
553 functions
->ReadPixels
= st_readpixels
;