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 "pipe/p_inlines.h"
44 #include "util/u_tile.h"
45 #include "st_context.h"
46 #include "st_cb_bitmap.h"
47 #include "st_cb_readpixels.h"
48 #include "st_cb_fbo.h"
49 #include "st_format.h"
50 #include "st_public.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_screen
*screen
= ctx
->st
->pipe
->screen
;
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 */
76 pt
= screen
->get_tex_transfer(screen
, strb
->texture
, 0, 0, 0,
77 PIPE_TRANSFER_READ
, x
, y
, width
, height
);
79 /* map the stencil buffer */
80 stmap
= screen
->transfer_map(screen
, pt
);
82 /* width should never be > MAX_WIDTH since we did clipping earlier */
83 ASSERT(width
<= MAX_WIDTH
);
85 /* process image row by row */
86 for (j
= 0; j
< height
; j
++) {
88 GLstencil sValues
[MAX_WIDTH
];
89 GLfloat zValues
[MAX_WIDTH
];
92 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
93 srcY
= height
- j
- 1;
99 /* get stencil (and Z) values */
100 switch (pt
->format
) {
101 case PIPE_FORMAT_S8_UNORM
:
103 const ubyte
*src
= stmap
+ srcY
* pt
->stride
;
104 memcpy(sValues
, src
, width
);
107 case PIPE_FORMAT_S8Z24_UNORM
:
108 if (format
== GL_DEPTH_STENCIL
) {
109 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
110 const GLfloat scale
= 1.0 / (0xffffff);
112 for (k
= 0; k
< width
; k
++) {
113 sValues
[k
] = src
[k
] >> 24;
114 zValues
[k
] = (src
[k
] & 0xffffff) * scale
;
118 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
120 for (k
= 0; k
< width
; k
++) {
121 sValues
[k
] = src
[k
] >> 24;
125 case PIPE_FORMAT_Z24S8_UNORM
:
126 if (format
== GL_DEPTH_STENCIL
) {
127 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
128 const GLfloat scale
= 1.0 / (0xffffff);
130 for (k
= 0; k
< width
; k
++) {
131 sValues
[k
] = src
[k
] & 0xff;
132 zValues
[k
] = (src
[k
] >> 8) * scale
;
136 const uint
*src
= (uint
*) (stmap
+ srcY
* pt
->stride
);
138 for (k
= 0; k
< width
; k
++) {
139 sValues
[k
] = src
[k
] & 0xff;
148 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
150 if (format
== GL_DEPTH_STENCIL
) {
151 _mesa_pack_depth_stencil_span(ctx
, width
, dest
,
152 zValues
, sValues
, packing
);
155 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, sValues
, packing
);
159 /* unmap the stencil buffer */
160 screen
->transfer_unmap(screen
, pt
);
161 screen
->tex_transfer_destroy(pt
);
166 * Return renderbuffer to use for reading color pixels for glRead/CopyPixel
168 * Special care is needed for the front buffer.
170 struct st_renderbuffer
*
171 st_get_color_read_renderbuffer(GLcontext
*ctx
)
173 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
174 struct st_renderbuffer
*strb
=
175 st_renderbuffer(fb
->_ColorReadBuffer
);
176 struct st_renderbuffer
*front
=
177 st_renderbuffer(fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
180 && ctx
->st
->frontbuffer_status
== FRONT_STATUS_COPY_OF_BACK
) {
181 /* reading from front color buffer, which is a logical copy of the
184 struct st_renderbuffer
*back
=
185 st_renderbuffer(fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
194 * Try to do glReadPixels in a fast manner for common cases.
195 * \return GL_TRUE for success, GL_FALSE for failure
198 st_fast_readpixels(GLcontext
*ctx
, struct st_renderbuffer
*strb
,
199 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
200 GLenum format
, GLenum type
,
201 const struct gl_pixelstore_attrib
*pack
,
205 A8R8G8B8_UNORM_TO_RGBA_UBYTE
,
206 A8R8G8B8_UNORM_TO_RGB_UBYTE
,
207 A8R8G8B8_UNORM_TO_BGRA_UINT
210 if (ctx
->_ImageTransferState
)
213 if (strb
->format
== PIPE_FORMAT_A8R8G8B8_UNORM
&&
214 format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
215 combo
= A8R8G8B8_UNORM_TO_RGBA_UBYTE
;
217 else if (strb
->format
== PIPE_FORMAT_A8R8G8B8_UNORM
&&
218 format
== GL_RGB
&& type
== GL_UNSIGNED_BYTE
) {
219 combo
= A8R8G8B8_UNORM_TO_RGB_UBYTE
;
221 else if (strb
->format
== PIPE_FORMAT_A8R8G8B8_UNORM
&&
222 format
== GL_BGRA
&& type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
223 combo
= A8R8G8B8_UNORM_TO_BGRA_UINT
;
229 /*printf("st_fast_readpixels combo %d\n", (GLint) combo);*/
232 struct pipe_context
*pipe
= ctx
->st
->pipe
;
233 struct pipe_screen
*screen
= pipe
->screen
;
234 struct pipe_transfer
*trans
;
237 GLint row
, col
, dy
, dstStride
;
239 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
240 y
= strb
->texture
->height
[0] - y
- height
;
243 trans
= screen
->get_tex_transfer(screen
, strb
->texture
, 0, 0, 0,
244 PIPE_TRANSFER_READ
, x
, y
, width
, height
);
249 map
= screen
->transfer_map(screen
, trans
);
251 screen
->tex_transfer_destroy(trans
);
255 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
264 dst
= _mesa_image_address2d(pack
, dest
, width
, height
,
266 dstStride
= _mesa_image_row_stride(pack
, width
, format
, type
);
269 case A8R8G8B8_UNORM_TO_RGBA_UBYTE
:
270 for (row
= 0; row
< height
; row
++) {
271 const GLubyte
*src
= map
+ y
* trans
->stride
;
272 for (col
= 0; col
< width
; col
++) {
273 GLuint pixel
= ((GLuint
*) src
)[col
];
274 dst
[col
*4+0] = (pixel
>> 16) & 0xff;
275 dst
[col
*4+1] = (pixel
>> 8) & 0xff;
276 dst
[col
*4+2] = (pixel
>> 0) & 0xff;
277 dst
[col
*4+3] = (pixel
>> 24) & 0xff;
283 case A8R8G8B8_UNORM_TO_RGB_UBYTE
:
284 for (row
= 0; row
< height
; row
++) {
285 const GLubyte
*src
= map
+ y
* trans
->stride
;
286 for (col
= 0; col
< width
; col
++) {
287 GLuint pixel
= ((GLuint
*) src
)[col
];
288 dst
[col
*3+0] = (pixel
>> 16) & 0xff;
289 dst
[col
*3+1] = (pixel
>> 8) & 0xff;
290 dst
[col
*3+2] = (pixel
>> 0) & 0xff;
296 case A8R8G8B8_UNORM_TO_BGRA_UINT
:
297 for (row
= 0; row
< height
; row
++) {
298 const GLubyte
*src
= map
+ y
* trans
->stride
;
299 memcpy(dst
, src
, 4 * width
);
308 screen
->transfer_unmap(screen
, trans
);
309 screen
->tex_transfer_destroy(trans
);
317 * Do glReadPixels by getting rows from the framebuffer transfer with
318 * get_tile(). Convert to requested format/type with Mesa image routines.
319 * Image transfer ops are done in software too.
322 st_readpixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
323 GLenum format
, GLenum type
,
324 const struct gl_pixelstore_attrib
*pack
,
327 struct pipe_context
*pipe
= ctx
->st
->pipe
;
328 struct pipe_screen
*screen
= pipe
->screen
;
329 GLfloat temp
[MAX_WIDTH
][4];
330 const GLbitfield transferOps
= ctx
->_ImageTransferState
;
332 GLint yStep
, dfStride
;
334 struct st_renderbuffer
*strb
;
335 struct gl_pixelstore_attrib clippedPacking
= *pack
;
336 struct pipe_transfer
*trans
;
338 assert(ctx
->ReadBuffer
->Width
> 0);
340 /* XXX convolution not done yet */
341 assert((transferOps
& IMAGE_CONVOLUTION_BIT
) == 0);
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_readpix_pbo(ctx
, &clippedPacking
, dest
);
353 /* make sure rendering has completed */
354 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
356 if (format
== GL_STENCIL_INDEX
||
357 format
== GL_DEPTH_STENCIL
) {
358 st_read_stencil_pixels(ctx
, x
, y
, width
, height
,
359 format
, type
, pack
, dest
);
362 else if (format
== GL_DEPTH_COMPONENT
) {
363 strb
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
366 /* Read color buffer */
367 strb
= st_get_color_read_renderbuffer(ctx
);
373 /* try a fast-path readpixels before anything else */
374 if (st_fast_readpixels(ctx
, strb
, x
, y
, width
, height
,
375 format
, type
, pack
, dest
)) {
377 _mesa_unmap_readpix_pbo(ctx
, &clippedPacking
);
381 if (format
== GL_RGBA
&& type
== GL_FLOAT
) {
382 /* write tile(row) directly into user's buffer */
383 df
= (GLfloat
*) _mesa_image_address2d(&clippedPacking
, dest
, width
,
384 height
, format
, type
, 0, 0);
385 dfStride
= width
* 4;
388 /* write tile(row) into temp row buffer */
389 df
= (GLfloat
*) temp
;
393 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
394 y
= strb
->Base
.Height
- y
- height
;
397 /* Create a read transfer from the renderbuffer's texture */
398 trans
= screen
->get_tex_transfer(screen
, strb
->texture
, 0, 0, 0,
399 PIPE_TRANSFER_READ
, x
, y
, width
, height
);
401 /* determine bottom-to-top vs. top-to-bottom order */
402 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
412 * Copy pixels from pipe_transfer to user memory
415 /* dest of first pixel in client memory */
416 GLubyte
*dst
= _mesa_image_address2d(&clippedPacking
, dest
, width
,
417 height
, format
, type
, 0, 0);
418 /* dest row stride */
419 const GLint dstStride
= _mesa_image_row_stride(&clippedPacking
, width
,
422 if (trans
->format
== PIPE_FORMAT_S8Z24_UNORM
||
423 trans
->format
== PIPE_FORMAT_X8Z24_UNORM
) {
424 if (format
== GL_DEPTH_COMPONENT
) {
425 for (i
= 0; i
< height
; i
++) {
426 GLuint ztemp
[MAX_WIDTH
];
427 GLfloat zfloat
[MAX_WIDTH
];
428 const double scale
= 1.0 / ((1 << 24) - 1);
429 pipe_get_tile_raw(trans
, 0, y
, width
, 1, ztemp
, 0);
431 for (j
= 0; j
< width
; j
++) {
432 zfloat
[j
] = (float) (scale
* (ztemp
[j
] & 0xffffff));
434 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
435 zfloat
, &clippedPacking
);
440 /* untested, but simple: */
441 assert(format
== GL_DEPTH_STENCIL_EXT
);
442 for (i
= 0; i
< height
; i
++) {
443 pipe_get_tile_raw(trans
, 0, y
, width
, 1, dst
, 0);
449 else if (trans
->format
== PIPE_FORMAT_Z24S8_UNORM
||
450 trans
->format
== PIPE_FORMAT_Z24X8_UNORM
) {
451 if (format
== GL_DEPTH_COMPONENT
) {
452 for (i
= 0; i
< height
; i
++) {
453 GLuint ztemp
[MAX_WIDTH
];
454 GLfloat zfloat
[MAX_WIDTH
];
455 const double scale
= 1.0 / ((1 << 24) - 1);
456 pipe_get_tile_raw(trans
, 0, y
, width
, 1, ztemp
, 0);
458 for (j
= 0; j
< width
; j
++) {
459 zfloat
[j
] = (float) (scale
* ((ztemp
[j
] >> 8) & 0xffffff));
461 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
462 zfloat
, &clippedPacking
);
467 /* untested, but simple: */
468 assert(format
== GL_DEPTH_STENCIL_EXT
);
469 for (i
= 0; i
< height
; i
++) {
470 pipe_get_tile_raw(trans
, 0, y
, width
, 1, dst
, 0);
476 else if (trans
->format
== PIPE_FORMAT_Z16_UNORM
) {
477 for (i
= 0; i
< height
; i
++) {
478 GLushort ztemp
[MAX_WIDTH
];
479 GLfloat zfloat
[MAX_WIDTH
];
480 const double scale
= 1.0 / 0xffff;
481 pipe_get_tile_raw(trans
, 0, y
, width
, 1, ztemp
, 0);
483 for (j
= 0; j
< width
; j
++) {
484 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
486 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
487 zfloat
, &clippedPacking
);
491 else if (trans
->format
== PIPE_FORMAT_Z32_UNORM
) {
492 for (i
= 0; i
< height
; i
++) {
493 GLuint ztemp
[MAX_WIDTH
];
494 GLfloat zfloat
[MAX_WIDTH
];
495 const double scale
= 1.0 / 0xffffffff;
496 pipe_get_tile_raw(trans
, 0, y
, width
, 1, ztemp
, 0);
498 for (j
= 0; j
< width
; j
++) {
499 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
501 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
502 zfloat
, &clippedPacking
);
508 /* Do a row at a time to flip image data vertically */
509 for (i
= 0; i
< height
; i
++) {
510 pipe_get_tile_rgba(trans
, 0, y
, width
, 1, df
);
514 _mesa_pack_rgba_span_float(ctx
, width
, temp
, format
, type
, dst
,
515 &clippedPacking
, transferOps
);
522 screen
->tex_transfer_destroy(trans
);
524 _mesa_unmap_readpix_pbo(ctx
, &clippedPacking
);
528 void st_init_readpixels_functions(struct dd_function_table
*functions
)
530 functions
->ReadPixels
= st_readpixels
;