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 (strb
->Base
.Wrapped
) {
72 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
75 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
76 y
= ctx
->DrawBuffer
->Height
- y
- height
;
79 /* Create a read transfer from the renderbuffer's texture */
81 pt
= pipe_get_transfer(pipe
, strb
->texture
,
82 0, 0, 0, /* face, level, zslice */
86 /* map the stencil buffer */
87 stmap
= pipe_transfer_map(pipe
, pt
);
89 /* width should never be > MAX_WIDTH since we did clipping earlier */
90 ASSERT(width
<= MAX_WIDTH
);
92 /* process image row by row */
93 for (j
= 0; j
< height
; j
++) {
95 GLstencil sValues
[MAX_WIDTH
];
96 GLfloat zValues
[MAX_WIDTH
];
99 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
100 srcY
= height
- j
- 1;
106 /* get stencil (and Z) values */
107 switch (pt
->resource
->format
) {
108 case PIPE_FORMAT_S8_USCALED
:
110 const ubyte
*src
= stmap
+ srcY
* pt
->stride
;
111 memcpy(sValues
, src
, width
);
114 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
115 if (format
== GL_DEPTH_STENCIL
) {
116 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
117 const GLfloat scale
= 1.0f
/ (0xffffff);
119 for (k
= 0; k
< width
; k
++) {
120 sValues
[k
] = src
[k
] >> 24;
121 zValues
[k
] = (src
[k
] & 0xffffff) * scale
;
125 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
127 for (k
= 0; k
< width
; k
++) {
128 sValues
[k
] = src
[k
] >> 24;
132 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
133 if (format
== GL_DEPTH_STENCIL
) {
134 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
135 const GLfloat scale
= 1.0f
/ (0xffffff);
137 for (k
= 0; k
< width
; k
++) {
138 sValues
[k
] = src
[k
] & 0xff;
139 zValues
[k
] = (src
[k
] >> 8) * scale
;
143 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
145 for (k
= 0; k
< width
; k
++) {
146 sValues
[k
] = src
[k
] & 0xff;
155 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
157 if (format
== GL_DEPTH_STENCIL
) {
158 _mesa_pack_depth_stencil_span(ctx
, width
, dest
,
159 zValues
, sValues
, packing
);
162 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, sValues
, packing
);
166 /* unmap the stencil buffer */
167 pipe_transfer_unmap(pipe
, pt
);
168 pipe
->transfer_destroy(pipe
, pt
);
173 * Return renderbuffer to use for reading color pixels for glRead/CopyPixel
176 struct st_renderbuffer
*
177 st_get_color_read_renderbuffer(GLcontext
*ctx
)
179 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
180 struct st_renderbuffer
*strb
=
181 st_renderbuffer(fb
->_ColorReadBuffer
);
188 * Try to do glReadPixels in a fast manner for common cases.
189 * \return GL_TRUE for success, GL_FALSE for failure
192 st_fast_readpixels(GLcontext
*ctx
, struct st_renderbuffer
*strb
,
193 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
194 GLenum format
, GLenum type
,
195 const struct gl_pixelstore_attrib
*pack
,
199 A8R8G8B8_UNORM_TO_RGBA_UBYTE
,
200 A8R8G8B8_UNORM_TO_RGB_UBYTE
,
201 A8R8G8B8_UNORM_TO_BGRA_UINT
204 if (ctx
->_ImageTransferState
)
207 if (strb
->format
== PIPE_FORMAT_B8G8R8A8_UNORM
&&
208 format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
209 combo
= A8R8G8B8_UNORM_TO_RGBA_UBYTE
;
211 else if (strb
->format
== PIPE_FORMAT_B8G8R8A8_UNORM
&&
212 format
== GL_RGB
&& type
== GL_UNSIGNED_BYTE
) {
213 combo
= A8R8G8B8_UNORM_TO_RGB_UBYTE
;
215 else if (strb
->format
== PIPE_FORMAT_B8G8R8A8_UNORM
&&
216 format
== GL_BGRA
&& type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
217 combo
= A8R8G8B8_UNORM_TO_BGRA_UINT
;
223 /*printf("st_fast_readpixels combo %d\n", (GLint) combo);*/
226 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
227 struct pipe_transfer
*trans
;
230 GLint row
, col
, dy
, dstStride
;
232 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
233 /* convert GL Y to Gallium Y */
234 y
= strb
->texture
->height0
- y
- height
;
237 trans
= pipe_get_transfer(pipe
, strb
->texture
,
238 0, 0, 0, /* face, level, zslice */
240 x
, y
, width
, height
);
245 map
= pipe_transfer_map(pipe
, trans
);
247 pipe
->transfer_destroy(pipe
, trans
);
251 /* We always write to the user/dest buffer from low addr to high addr
252 * but the read order depends on renderbuffer orientation
254 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
255 /* read source rows from bottom to top */
260 /* read source rows from top to bottom */
265 dst
= _mesa_image_address2d(pack
, dest
, width
, height
,
267 dstStride
= _mesa_image_row_stride(pack
, width
, format
, type
);
270 case A8R8G8B8_UNORM_TO_RGBA_UBYTE
:
271 for (row
= 0; row
< height
; row
++) {
272 const GLubyte
*src
= map
+ y
* trans
->stride
;
273 for (col
= 0; col
< width
; col
++) {
274 GLuint pixel
= ((GLuint
*) src
)[col
];
275 dst
[col
*4+0] = (pixel
>> 16) & 0xff;
276 dst
[col
*4+1] = (pixel
>> 8) & 0xff;
277 dst
[col
*4+2] = (pixel
>> 0) & 0xff;
278 dst
[col
*4+3] = (pixel
>> 24) & 0xff;
284 case A8R8G8B8_UNORM_TO_RGB_UBYTE
:
285 for (row
= 0; row
< height
; row
++) {
286 const GLubyte
*src
= map
+ y
* trans
->stride
;
287 for (col
= 0; col
< width
; col
++) {
288 GLuint pixel
= ((GLuint
*) src
)[col
];
289 dst
[col
*3+0] = (pixel
>> 16) & 0xff;
290 dst
[col
*3+1] = (pixel
>> 8) & 0xff;
291 dst
[col
*3+2] = (pixel
>> 0) & 0xff;
297 case A8R8G8B8_UNORM_TO_BGRA_UINT
:
298 for (row
= 0; row
< height
; row
++) {
299 const GLubyte
*src
= map
+ y
* trans
->stride
;
300 memcpy(dst
, src
, 4 * width
);
309 pipe_transfer_unmap(pipe
, trans
);
310 pipe
->transfer_destroy(pipe
, trans
);
318 * Do glReadPixels by getting rows from the framebuffer transfer with
319 * get_tile(). Convert to requested format/type with Mesa image routines.
320 * Image transfer ops are done in software too.
323 st_readpixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
324 GLenum format
, GLenum type
,
325 const struct gl_pixelstore_attrib
*pack
,
328 struct st_context
*st
= st_context(ctx
);
329 struct pipe_context
*pipe
= st
->pipe
;
330 GLfloat temp
[MAX_WIDTH
][4];
331 const GLbitfield transferOps
= ctx
->_ImageTransferState
;
333 GLint yStep
, dfStride
;
335 struct st_renderbuffer
*strb
;
336 struct gl_pixelstore_attrib clippedPacking
= *pack
;
337 struct pipe_transfer
*trans
;
339 assert(ctx
->ReadBuffer
->Width
> 0);
341 /* XXX convolution not done yet */
342 assert((transferOps
& IMAGE_CONVOLUTION_BIT
) == 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 if (format
== GL_RGBA
&& type
== GL_FLOAT
) {
387 /* write tile(row) directly into user's buffer */
388 df
= (GLfloat
*) _mesa_image_address2d(&clippedPacking
, dest
, width
,
389 height
, format
, type
, 0, 0);
390 dfStride
= width
* 4;
393 /* write tile(row) into temp row buffer */
394 df
= (GLfloat
*) temp
;
398 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
399 /* convert GL Y to Gallium Y */
400 y
= strb
->Base
.Height
- y
- height
;
403 /* Create a read transfer from the renderbuffer's texture */
404 trans
= pipe_get_transfer(pipe
, strb
->texture
,
405 0, 0, 0, /* face, level, zslice */
407 x
, y
, width
, height
);
409 /* determine bottom-to-top vs. top-to-bottom order */
410 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
419 if (ST_DEBUG
& DEBUG_FALLBACK
)
420 debug_printf("%s: fallback processing\n", __FUNCTION__
);
423 * Copy pixels from pipe_transfer to user memory
426 /* dest of first pixel in client memory */
427 GLubyte
*dst
= _mesa_image_address2d(&clippedPacking
, dest
, width
,
428 height
, format
, type
, 0, 0);
429 /* dest row stride */
430 const GLint dstStride
= _mesa_image_row_stride(&clippedPacking
, width
,
433 if (trans
->resource
->format
== PIPE_FORMAT_Z24_UNORM_S8_USCALED
||
434 trans
->resource
->format
== PIPE_FORMAT_Z24X8_UNORM
) {
435 if (format
== GL_DEPTH_COMPONENT
) {
436 for (i
= 0; i
< height
; i
++) {
437 GLuint ztemp
[MAX_WIDTH
];
438 GLfloat zfloat
[MAX_WIDTH
];
439 const double scale
= 1.0 / ((1 << 24) - 1);
440 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
442 for (j
= 0; j
< width
; j
++) {
443 zfloat
[j
] = (float) (scale
* (ztemp
[j
] & 0xffffff));
445 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
446 zfloat
, &clippedPacking
);
451 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
452 assert(format
== GL_DEPTH_STENCIL_EXT
);
453 for (i
= 0; i
< height
; i
++) {
454 GLuint
*zshort
= (GLuint
*)dst
;
455 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, dst
, 0);
457 /* Reverse into 24/8 */
458 for (j
= 0; j
< width
; j
++) {
459 zshort
[j
] = (zshort
[j
] << 8) | (zshort
[j
] >> 24);
465 else if (trans
->resource
->format
== PIPE_FORMAT_S8_USCALED_Z24_UNORM
||
466 trans
->resource
->format
== PIPE_FORMAT_X8Z24_UNORM
) {
467 if (format
== GL_DEPTH_COMPONENT
) {
468 for (i
= 0; i
< height
; i
++) {
469 GLuint ztemp
[MAX_WIDTH
];
470 GLfloat zfloat
[MAX_WIDTH
];
471 const double scale
= 1.0 / ((1 << 24) - 1);
472 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
474 for (j
= 0; j
< width
; j
++) {
475 zfloat
[j
] = (float) (scale
* ((ztemp
[j
] >> 8) & 0xffffff));
477 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
478 zfloat
, &clippedPacking
);
483 /* XXX: unreachable code -- should be before st_read_stencil_pixels */
484 assert(format
== GL_DEPTH_STENCIL_EXT
);
485 for (i
= 0; i
< height
; i
++) {
486 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, dst
, 0);
492 else if (trans
->resource
->format
== PIPE_FORMAT_Z16_UNORM
) {
493 for (i
= 0; i
< height
; i
++) {
494 GLushort ztemp
[MAX_WIDTH
];
495 GLfloat zfloat
[MAX_WIDTH
];
496 const double scale
= 1.0 / 0xffff;
497 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
499 for (j
= 0; j
< width
; j
++) {
500 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
502 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
503 zfloat
, &clippedPacking
);
507 else if (trans
->resource
->format
== PIPE_FORMAT_Z32_UNORM
) {
508 for (i
= 0; i
< height
; i
++) {
509 GLuint ztemp
[MAX_WIDTH
];
510 GLfloat zfloat
[MAX_WIDTH
];
511 const double scale
= 1.0 / 0xffffffff;
512 pipe_get_tile_raw(pipe
, trans
, 0, y
, width
, 1, ztemp
, 0);
514 for (j
= 0; j
< width
; j
++) {
515 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
517 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
518 zfloat
, &clippedPacking
);
524 /* Do a row at a time to flip image data vertically */
525 for (i
= 0; i
< height
; i
++) {
526 pipe_get_tile_rgba(pipe
, trans
, 0, y
, width
, 1, df
);
530 _mesa_pack_rgba_span_float(ctx
, width
, temp
, format
, type
, dst
,
531 &clippedPacking
, transferOps
);
538 pipe
->transfer_destroy(pipe
, trans
);
540 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
544 void st_init_readpixels_functions(struct dd_function_table
*functions
)
546 functions
->ReadPixels
= st_readpixels
;