2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include "bufferobj.h"
31 #include "framebuffer.h"
33 #include "format_unpack.h"
42 * Tries to implement glReadPixels() of GL_DEPTH_COMPONENT using memcpy of the
46 fast_read_depth_pixels( struct gl_context
*ctx
,
48 GLsizei width
, GLsizei height
,
49 GLenum type
, GLvoid
*pixels
,
50 const struct gl_pixelstore_attrib
*packing
)
52 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
53 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
55 int stride
, dstStride
, j
;
57 if (ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0)
60 if (packing
->SwapBytes
)
63 if (_mesa_get_format_datatype(rb
->Format
) != GL_UNSIGNED_NORMALIZED
)
66 if (!((type
== GL_UNSIGNED_SHORT
&& rb
->Format
== MESA_FORMAT_Z16
) ||
67 type
== GL_UNSIGNED_INT
))
70 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
74 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
75 return GL_TRUE
; /* don't bother trying the slow path */
78 dstStride
= _mesa_image_row_stride(packing
, width
, GL_DEPTH_COMPONENT
, type
);
79 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
80 GL_DEPTH_COMPONENT
, type
, 0, 0);
82 for (j
= 0; j
< height
; j
++) {
83 if (type
== GL_UNSIGNED_INT
) {
84 _mesa_unpack_uint_z_row(rb
->Format
, width
, map
, (GLuint
*)dst
);
86 ASSERT(type
== GL_UNSIGNED_SHORT
&& rb
->Format
== MESA_FORMAT_Z16
);
87 memcpy(dst
, map
, width
* 2);
93 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
99 * Read pixels for format=GL_DEPTH_COMPONENT.
102 read_depth_pixels( struct gl_context
*ctx
,
104 GLsizei width
, GLsizei height
,
105 GLenum type
, GLvoid
*pixels
,
106 const struct gl_pixelstore_attrib
*packing
)
108 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
109 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
112 int dstStride
, stride
;
117 /* clipping should have been done already */
120 ASSERT(x
+ width
<= (GLint
) rb
->Width
);
121 ASSERT(y
+ height
<= (GLint
) rb
->Height
);
122 /* width should never be > MAX_WIDTH since we did clipping earlier */
123 ASSERT(width
<= MAX_WIDTH
);
125 if (fast_read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
, packing
))
128 dstStride
= _mesa_image_row_stride(packing
, width
, GL_DEPTH_COMPONENT
, type
);
129 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
130 GL_DEPTH_COMPONENT
, type
, 0, 0);
132 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
135 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
139 /* General case (slower) */
140 for (j
= 0; j
< height
; j
++, y
++) {
141 GLfloat depthValues
[MAX_WIDTH
];
142 _mesa_unpack_float_z_row(rb
->Format
, width
, map
, depthValues
);
143 _mesa_pack_depth_span(ctx
, width
, dst
, type
, depthValues
, packing
);
149 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
154 * Read pixels for format=GL_STENCIL_INDEX.
157 read_stencil_pixels( struct gl_context
*ctx
,
159 GLsizei width
, GLsizei height
,
160 GLenum type
, GLvoid
*pixels
,
161 const struct gl_pixelstore_attrib
*packing
)
163 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
164 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
172 /* width should never be > MAX_WIDTH since we did clipping earlier */
173 ASSERT(width
<= MAX_WIDTH
);
175 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
178 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
182 /* process image row by row */
183 for (j
= 0; j
< height
; j
++) {
185 GLubyte stencil
[MAX_WIDTH
];
187 _mesa_unpack_ubyte_stencil_row(rb
->Format
, width
, map
, stencil
);
188 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
189 GL_STENCIL_INDEX
, type
, j
, 0);
191 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, stencil
, packing
);
196 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
201 * Try to do glReadPixels of RGBA data using a simple memcpy or swizzle.
202 * \return GL_TRUE if successful, GL_FALSE otherwise (use the slow path)
205 fast_read_rgba_pixels_memcpy( struct gl_context
*ctx
,
207 GLsizei width
, GLsizei height
,
208 GLenum format
, GLenum type
,
210 const struct gl_pixelstore_attrib
*packing
,
211 GLbitfield transferOps
)
213 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
215 int dstStride
, stride
, j
, texelBytes
;
216 GLboolean swizzle_rb
= GL_FALSE
, copy_xrgb
= GL_FALSE
;
218 /* XXX we could check for other swizzle/special cases here as needed */
219 if (rb
->Format
== MESA_FORMAT_RGBA8888_REV
&&
221 type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
222 swizzle_rb
= GL_TRUE
;
224 else if (rb
->Format
== MESA_FORMAT_XRGB8888
&&
226 type
== GL_UNSIGNED_INT_8_8_8_8_REV
) {
229 else if (!_mesa_format_matches_format_and_type(rb
->Format
, format
, type
))
232 /* check for things we can't handle here */
233 if (packing
->SwapBytes
) {
237 /* If the format is unsigned normalized then we can ignore clamping
238 * because the values are already in the range [0,1] so it won't
239 * have any effect anyway.
241 if (_mesa_get_format_datatype(rb
->Format
) == GL_UNSIGNED_NORMALIZED
)
242 transferOps
&= ~IMAGE_CLAMP_BIT
;
247 dstStride
= _mesa_image_row_stride(packing
, width
, format
, type
);
248 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
251 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
254 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
255 return GL_TRUE
; /* don't bother trying the slow path */
258 texelBytes
= _mesa_get_format_bytes(rb
->Format
);
262 for (j
= 0; j
< height
; j
++) {
264 for (i
= 0; i
< width
; i
++) {
265 GLuint
*dst4
= (GLuint
*) dst
, *map4
= (GLuint
*) map
;
266 GLuint pixel
= map4
[i
];
267 dst4
[i
] = (pixel
& 0xff00ff00)
268 | ((pixel
& 0x00ff0000) >> 16)
269 | ((pixel
& 0x000000ff) << 16);
274 } else if (copy_xrgb
) {
275 /* convert xrgb -> argb */
276 for (j
= 0; j
< height
; j
++) {
277 GLuint
*dst4
= (GLuint
*) dst
, *map4
= (GLuint
*) map
;
279 for (i
= 0; i
< width
; i
++) {
280 dst4
[i
] = map4
[i
] | 0xff000000; /* set A=0xff */
287 for (j
= 0; j
< height
; j
++) {
288 memcpy(dst
, map
, width
* texelBytes
);
294 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
300 slow_read_rgba_pixels( struct gl_context
*ctx
,
302 GLsizei width
, GLsizei height
,
303 GLenum format
, GLenum type
,
305 const struct gl_pixelstore_attrib
*packing
,
306 GLbitfield transferOps
)
308 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
309 const gl_format rbFormat
= rb
->Format
;
312 int dstStride
, stride
, j
;
314 dstStride
= _mesa_image_row_stride(packing
, width
, format
, type
);
315 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
318 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
321 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
325 rgba
= malloc(width
* MAX_PIXEL_BYTES
);
329 for (j
= 0; j
< height
; j
++) {
330 if (_mesa_is_integer_format(format
)) {
331 _mesa_unpack_uint_rgba_row(rbFormat
, width
, map
, (GLuint (*)[4]) rgba
);
332 _mesa_rebase_rgba_uint(width
, (GLuint (*)[4]) rgba
,
334 _mesa_pack_rgba_span_int(ctx
, width
, (GLuint (*)[4]) rgba
, format
,
337 _mesa_unpack_rgba_row(rbFormat
, width
, map
, (GLfloat (*)[4]) rgba
);
338 _mesa_rebase_rgba_float(width
, (GLfloat (*)[4]) rgba
,
340 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
341 type
, dst
, packing
, transferOps
);
350 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
354 * Read R, G, B, A, RGB, L, or LA pixels.
357 read_rgba_pixels( struct gl_context
*ctx
,
359 GLsizei width
, GLsizei height
,
360 GLenum format
, GLenum type
, GLvoid
*pixels
,
361 const struct gl_pixelstore_attrib
*packing
)
363 GLbitfield transferOps
= ctx
->_ImageTransferState
;
364 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
365 struct gl_renderbuffer
*rb
= fb
->_ColorReadBuffer
;
370 if (!_mesa_is_integer_format(format
)) {
371 transferOps
|= IMAGE_CLAMP_BIT
;
374 /* Try the optimized paths first. */
375 if (fast_read_rgba_pixels_memcpy(ctx
, x
, y
, width
, height
,
376 format
, type
, pixels
, packing
,
381 slow_read_rgba_pixels(ctx
, x
, y
, width
, height
,
382 format
, type
, pixels
, packing
, transferOps
);
386 * Software fallback routine for ctx->Driver.ReadPixels().
387 * By time we get here, all error checking will have been done.
390 _mesa_readpixels(struct gl_context
*ctx
,
391 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
392 GLenum format
, GLenum type
,
393 const struct gl_pixelstore_attrib
*packing
,
396 struct gl_pixelstore_attrib clippedPacking
= *packing
;
399 _mesa_update_state(ctx
);
401 /* Do all needed clipping here, so that we can forget about it later */
402 if (_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
404 pixels
= _mesa_map_pbo_dest(ctx
, &clippedPacking
, pixels
);
408 case GL_STENCIL_INDEX
:
409 read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
412 case GL_DEPTH_COMPONENT
:
413 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
417 /* all other formats should be color formats */
418 read_rgba_pixels(ctx
, x
, y
, width
, height
, format
, type
, pixels
,
422 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
429 * Do error checking of the format/type parameters to glReadPixels and
431 * \param drawing if GL_TRUE do checking for DrawPixels, else do checking
433 * \return GL_TRUE if error detected, GL_FALSE if no errors
436 _mesa_error_check_format_type(struct gl_context
*ctx
, GLenum format
,
437 GLenum type
, GLboolean drawing
)
439 const char *readDraw
= drawing
? "Draw" : "Read";
440 const GLboolean reading
= !drawing
;
443 /* state validation should have already been done */
444 ASSERT(ctx
->NewState
== 0x0);
446 /* basic combinations test */
447 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
448 if (err
!= GL_NO_ERROR
) {
449 _mesa_error(ctx
, err
, "gl%sPixels(format or type)", readDraw
);
453 /* additional checks */
461 case GL_LUMINANCE_ALPHA
:
467 case GL_RED_INTEGER_EXT
:
468 case GL_GREEN_INTEGER_EXT
:
469 case GL_BLUE_INTEGER_EXT
:
470 case GL_ALPHA_INTEGER_EXT
:
471 case GL_RGB_INTEGER_EXT
:
472 case GL_RGBA_INTEGER_EXT
:
473 case GL_BGR_INTEGER_EXT
:
474 case GL_BGRA_INTEGER_EXT
:
475 case GL_LUMINANCE_INTEGER_EXT
:
476 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
479 if (!_mesa_source_buffer_exists(ctx
, GL_COLOR
)) {
480 _mesa_error(ctx
, GL_INVALID_OPERATION
,
481 "glReadPixels(no color buffer)");
488 if (ctx
->PixelMaps
.ItoR
.Size
== 0 ||
489 ctx
->PixelMaps
.ItoG
.Size
== 0 ||
490 ctx
->PixelMaps
.ItoB
.Size
== 0) {
491 _mesa_error(ctx
, GL_INVALID_OPERATION
,
492 "glDrawPixels(drawing color index pixels into RGB buffer)");
498 if (!_mesa_source_buffer_exists(ctx
, GL_COLOR
)) {
499 _mesa_error(ctx
, GL_INVALID_OPERATION
,
500 "glReadPixels(no color buffer)");
503 /* We no longer support CI-mode color buffers so trying to read
504 * GL_COLOR_INDEX pixels is always an error.
506 _mesa_error(ctx
, GL_INVALID_OPERATION
,
507 "glReadPixels(color buffer is RGB)");
511 case GL_STENCIL_INDEX
:
512 if ((drawing
&& !_mesa_dest_buffer_exists(ctx
, format
)) ||
513 (reading
&& !_mesa_source_buffer_exists(ctx
, format
))) {
514 _mesa_error(ctx
, GL_INVALID_OPERATION
,
515 "gl%sPixels(no stencil buffer)", readDraw
);
519 case GL_DEPTH_COMPONENT
:
520 if ((drawing
&& !_mesa_dest_buffer_exists(ctx
, format
))) {
521 _mesa_error(ctx
, GL_INVALID_OPERATION
,
522 "gl%sPixels(no depth buffer)", readDraw
);
527 /* this should have been caught in _mesa_error_check_format_type() */
528 _mesa_problem(ctx
, "unexpected format in _mesa_%sPixels", readDraw
);
539 _mesa_ReadnPixelsARB( GLint x
, GLint y
, GLsizei width
, GLsizei height
,
540 GLenum format
, GLenum type
, GLsizei bufSize
,
543 GET_CURRENT_CONTEXT(ctx
);
544 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
546 FLUSH_CURRENT(ctx
, 0);
548 if (MESA_VERBOSE
& VERBOSE_API
)
549 _mesa_debug(ctx
, "glReadPixels(%d, %d, %s, %s, %p)\n",
551 _mesa_lookup_enum_by_nr(format
),
552 _mesa_lookup_enum_by_nr(type
),
555 if (width
< 0 || height
< 0) {
556 _mesa_error( ctx
, GL_INVALID_VALUE
,
557 "glReadPixels(width=%d height=%d)", width
, height
);
562 _mesa_update_state(ctx
);
564 if (_mesa_error_check_format_type(ctx
, format
, type
, GL_FALSE
)) {
569 /* Check that the destination format and source buffer are both
570 * integer-valued or both non-integer-valued.
572 if (ctx
->Extensions
.EXT_texture_integer
&& _mesa_is_color_format(format
)) {
573 const struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
574 const GLboolean srcInteger
= _mesa_is_format_integer_color(rb
->Format
);
575 const GLboolean dstInteger
= _mesa_is_integer_format(format
);
576 if (dstInteger
!= srcInteger
) {
577 _mesa_error(ctx
, GL_INVALID_OPERATION
,
578 "glReadPixels(integer / non-integer format mismatch");
583 if (!_mesa_source_buffer_exists(ctx
, format
)) {
584 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(no readbuffer)");
588 if (width
== 0 || height
== 0)
589 return; /* nothing to do */
591 if (!_mesa_validate_pbo_access(2, &ctx
->Pack
, width
, height
, 1,
592 format
, type
, bufSize
, pixels
)) {
593 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
594 _mesa_error(ctx
, GL_INVALID_OPERATION
,
595 "glReadPixels(out of bounds PBO access)");
597 _mesa_error(ctx
, GL_INVALID_OPERATION
,
598 "glReadnPixelsARB(out of bounds access:"
599 " bufSize (%d) is too small)", bufSize
);
604 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) &&
605 _mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
606 /* buffer is mapped - that's an error */
607 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(PBO is mapped)");
611 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
612 format
, type
, &ctx
->Pack
, pixels
);
616 _mesa_ReadPixels( GLint x
, GLint y
, GLsizei width
, GLsizei height
,
617 GLenum format
, GLenum type
, GLvoid
*pixels
)
619 _mesa_ReadnPixelsARB(x
, y
, width
, height
, format
, type
, INT_MAX
, pixels
);