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"
39 #include "glformats.h"
45 * Tries to implement glReadPixels() of GL_DEPTH_COMPONENT using memcpy of the
49 fast_read_depth_pixels( struct gl_context
*ctx
,
51 GLsizei width
, GLsizei height
,
52 GLenum type
, GLvoid
*pixels
,
53 const struct gl_pixelstore_attrib
*packing
)
55 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
56 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
58 int stride
, dstStride
, j
;
60 if (ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0)
63 if (packing
->SwapBytes
)
66 if (_mesa_get_format_datatype(rb
->Format
) != GL_UNSIGNED_NORMALIZED
)
69 if (!((type
== GL_UNSIGNED_SHORT
&& rb
->Format
== MESA_FORMAT_Z16
) ||
70 type
== GL_UNSIGNED_INT
))
73 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
77 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
78 return GL_TRUE
; /* don't bother trying the slow path */
81 dstStride
= _mesa_image_row_stride(packing
, width
, GL_DEPTH_COMPONENT
, type
);
82 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
83 GL_DEPTH_COMPONENT
, type
, 0, 0);
85 for (j
= 0; j
< height
; j
++) {
86 if (type
== GL_UNSIGNED_INT
) {
87 _mesa_unpack_uint_z_row(rb
->Format
, width
, map
, (GLuint
*)dst
);
89 ASSERT(type
== GL_UNSIGNED_SHORT
&& rb
->Format
== MESA_FORMAT_Z16
);
90 memcpy(dst
, map
, width
* 2);
96 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
102 * Read pixels for format=GL_DEPTH_COMPONENT.
105 read_depth_pixels( struct gl_context
*ctx
,
107 GLsizei width
, GLsizei height
,
108 GLenum type
, GLvoid
*pixels
,
109 const struct gl_pixelstore_attrib
*packing
)
111 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
112 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
115 int dstStride
, stride
;
116 GLfloat
*depthValues
;
121 /* clipping should have been done already */
124 ASSERT(x
+ width
<= (GLint
) rb
->Width
);
125 ASSERT(y
+ height
<= (GLint
) rb
->Height
);
127 if (fast_read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
, packing
))
130 dstStride
= _mesa_image_row_stride(packing
, width
, GL_DEPTH_COMPONENT
, type
);
131 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
132 GL_DEPTH_COMPONENT
, type
, 0, 0);
134 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
137 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
141 depthValues
= malloc(width
* sizeof(GLfloat
));
144 /* General case (slower) */
145 for (j
= 0; j
< height
; j
++, y
++) {
146 _mesa_unpack_float_z_row(rb
->Format
, width
, map
, depthValues
);
147 _mesa_pack_depth_span(ctx
, width
, dst
, type
, depthValues
, packing
);
154 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
159 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
164 * Read pixels for format=GL_STENCIL_INDEX.
167 read_stencil_pixels( struct gl_context
*ctx
,
169 GLsizei width
, GLsizei height
,
170 GLenum type
, GLvoid
*pixels
,
171 const struct gl_pixelstore_attrib
*packing
)
173 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
174 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
176 GLubyte
*map
, *stencil
;
182 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
185 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
189 stencil
= malloc(width
* sizeof(GLubyte
));
192 /* process image row by row */
193 for (j
= 0; j
< height
; j
++) {
196 _mesa_unpack_ubyte_stencil_row(rb
->Format
, width
, map
, stencil
);
197 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
198 GL_STENCIL_INDEX
, type
, j
, 0);
200 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, stencil
, packing
);
206 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
211 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
216 * Try to do glReadPixels of RGBA data using a simple memcpy or swizzle.
217 * \return GL_TRUE if successful, GL_FALSE otherwise (use the slow path)
220 fast_read_rgba_pixels_memcpy( struct gl_context
*ctx
,
222 GLsizei width
, GLsizei height
,
223 GLenum format
, GLenum type
,
225 const struct gl_pixelstore_attrib
*packing
,
226 GLbitfield transferOps
)
228 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
230 int dstStride
, stride
, j
, texelBytes
;
231 GLboolean swizzle_rb
= GL_FALSE
, copy_xrgb
= GL_FALSE
;
233 /* XXX we could check for other swizzle/special cases here as needed */
234 if (rb
->Format
== MESA_FORMAT_RGBA8888_REV
&&
236 type
== GL_UNSIGNED_INT_8_8_8_8_REV
&&
237 !ctx
->Pack
.SwapBytes
) {
238 swizzle_rb
= GL_TRUE
;
240 else if (rb
->Format
== MESA_FORMAT_XRGB8888
&&
242 type
== GL_UNSIGNED_INT_8_8_8_8_REV
&&
243 !ctx
->Pack
.SwapBytes
) {
246 else if (!_mesa_format_matches_format_and_type(rb
->Format
, format
, type
,
247 ctx
->Pack
.SwapBytes
))
250 /* If the format is unsigned normalized then we can ignore clamping
251 * because the values are already in the range [0,1] so it won't
252 * have any effect anyway.
254 if (_mesa_get_format_datatype(rb
->Format
) == GL_UNSIGNED_NORMALIZED
)
255 transferOps
&= ~IMAGE_CLAMP_BIT
;
260 dstStride
= _mesa_image_row_stride(packing
, width
, format
, type
);
261 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
264 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
267 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
268 return GL_TRUE
; /* don't bother trying the slow path */
271 texelBytes
= _mesa_get_format_bytes(rb
->Format
);
275 for (j
= 0; j
< height
; j
++) {
277 for (i
= 0; i
< width
; i
++) {
278 GLuint
*dst4
= (GLuint
*) dst
, *map4
= (GLuint
*) map
;
279 GLuint pixel
= map4
[i
];
280 dst4
[i
] = (pixel
& 0xff00ff00)
281 | ((pixel
& 0x00ff0000) >> 16)
282 | ((pixel
& 0x000000ff) << 16);
287 } else if (copy_xrgb
) {
288 /* convert xrgb -> argb */
289 for (j
= 0; j
< height
; j
++) {
290 GLuint
*dst4
= (GLuint
*) dst
, *map4
= (GLuint
*) map
;
292 for (i
= 0; i
< width
; i
++) {
293 dst4
[i
] = map4
[i
] | 0xff000000; /* set A=0xff */
300 for (j
= 0; j
< height
; j
++) {
301 memcpy(dst
, map
, width
* texelBytes
);
307 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
313 slow_read_rgba_pixels( struct gl_context
*ctx
,
315 GLsizei width
, GLsizei height
,
316 GLenum format
, GLenum type
,
318 const struct gl_pixelstore_attrib
*packing
,
319 GLbitfield transferOps
)
321 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
322 const gl_format rbFormat
= _mesa_get_srgb_format_linear(rb
->Format
);
325 int dstStride
, stride
, j
;
326 GLboolean dst_is_integer
= _mesa_is_enum_format_integer(format
);
327 GLboolean dst_is_uint
= _mesa_is_format_unsigned(rbFormat
);
329 dstStride
= _mesa_image_row_stride(packing
, width
, format
, type
);
330 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
333 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
336 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
340 rgba
= malloc(width
* MAX_PIXEL_BYTES
);
344 for (j
= 0; j
< height
; j
++) {
345 if (dst_is_integer
) {
346 _mesa_unpack_uint_rgba_row(rbFormat
, width
, map
, (GLuint (*)[4]) rgba
);
347 _mesa_rebase_rgba_uint(width
, (GLuint (*)[4]) rgba
,
350 _mesa_pack_rgba_span_from_uints(ctx
, width
, (GLuint (*)[4]) rgba
, format
,
353 _mesa_pack_rgba_span_from_ints(ctx
, width
, (GLint (*)[4]) rgba
, format
,
357 _mesa_unpack_rgba_row(rbFormat
, width
, map
, (GLfloat (*)[4]) rgba
);
358 _mesa_rebase_rgba_float(width
, (GLfloat (*)[4]) rgba
,
360 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
361 type
, dst
, packing
, transferOps
);
370 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
374 * Read R, G, B, A, RGB, L, or LA pixels.
377 read_rgba_pixels( struct gl_context
*ctx
,
379 GLsizei width
, GLsizei height
,
380 GLenum format
, GLenum type
, GLvoid
*pixels
,
381 const struct gl_pixelstore_attrib
*packing
)
383 GLbitfield transferOps
= ctx
->_ImageTransferState
;
384 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
385 struct gl_renderbuffer
*rb
= fb
->_ColorReadBuffer
;
390 if ((ctx
->Color
._ClampReadColor
== GL_TRUE
|| type
!= GL_FLOAT
) &&
391 !_mesa_is_enum_format_integer(format
)) {
392 transferOps
|= IMAGE_CLAMP_BIT
;
395 /* Try the optimized paths first. */
396 if (fast_read_rgba_pixels_memcpy(ctx
, x
, y
, width
, height
,
397 format
, type
, pixels
, packing
,
402 slow_read_rgba_pixels(ctx
, x
, y
, width
, height
,
403 format
, type
, pixels
, packing
, transferOps
);
407 * For a packed depth/stencil buffer being read as depth/stencil, just memcpy the
408 * data (possibly swapping 8/24 vs 24/8 as we go).
411 fast_read_depth_stencil_pixels(struct gl_context
*ctx
,
413 GLsizei width
, GLsizei height
,
414 GLubyte
*dst
, int dstStride
)
416 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
417 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
418 struct gl_renderbuffer
*stencilRb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
425 if (rb
->Format
!= MESA_FORMAT_Z24_S8
&&
426 rb
->Format
!= MESA_FORMAT_S8_Z24
)
429 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
432 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
433 return GL_TRUE
; /* don't bother trying the slow path */
436 for (i
= 0; i
< height
; i
++) {
437 _mesa_unpack_uint_24_8_depth_stencil_row(rb
->Format
, width
,
443 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
450 * For non-float-depth and stencil buffers being read as 24/8 depth/stencil,
451 * copy the integer data directly instead of converting depth to float and
455 fast_read_depth_stencil_pixels_separate(struct gl_context
*ctx
,
457 GLsizei width
, GLsizei height
,
458 uint32_t *dst
, int dstStride
)
460 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
461 struct gl_renderbuffer
*depthRb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
462 struct gl_renderbuffer
*stencilRb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
463 GLubyte
*depthMap
, *stencilMap
, *stencilVals
;
464 int depthStride
, stencilStride
, i
, j
;
466 if (_mesa_get_format_datatype(depthRb
->Format
) != GL_UNSIGNED_NORMALIZED
)
469 ctx
->Driver
.MapRenderbuffer(ctx
, depthRb
, x
, y
, width
, height
,
470 GL_MAP_READ_BIT
, &depthMap
, &depthStride
);
472 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
473 return GL_TRUE
; /* don't bother trying the slow path */
476 ctx
->Driver
.MapRenderbuffer(ctx
, stencilRb
, x
, y
, width
, height
,
477 GL_MAP_READ_BIT
, &stencilMap
, &stencilStride
);
479 ctx
->Driver
.UnmapRenderbuffer(ctx
, depthRb
);
480 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
481 return GL_TRUE
; /* don't bother trying the slow path */
484 stencilVals
= malloc(width
* sizeof(GLubyte
));
487 for (j
= 0; j
< height
; j
++) {
488 _mesa_unpack_uint_z_row(depthRb
->Format
, width
, depthMap
, dst
);
489 _mesa_unpack_ubyte_stencil_row(stencilRb
->Format
, width
,
490 stencilMap
, stencilVals
);
492 for (i
= 0; i
< width
; i
++) {
493 dst
[i
] = (dst
[i
] & 0xffffff00) | stencilVals
[i
];
496 depthMap
+= depthStride
;
497 stencilMap
+= stencilStride
;
498 dst
+= dstStride
/ 4;
502 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
507 ctx
->Driver
.UnmapRenderbuffer(ctx
, depthRb
);
508 ctx
->Driver
.UnmapRenderbuffer(ctx
, stencilRb
);
514 slow_read_depth_stencil_pixels_separate(struct gl_context
*ctx
,
516 GLsizei width
, GLsizei height
,
518 const struct gl_pixelstore_attrib
*packing
,
519 GLubyte
*dst
, int dstStride
)
521 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
522 struct gl_renderbuffer
*depthRb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
523 struct gl_renderbuffer
*stencilRb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
524 GLubyte
*depthMap
, *stencilMap
;
525 int depthStride
, stencilStride
, j
;
526 GLubyte
*stencilVals
;
530 /* The depth and stencil buffers might be separate, or a single buffer.
531 * If one buffer, only map it once.
533 ctx
->Driver
.MapRenderbuffer(ctx
, depthRb
, x
, y
, width
, height
,
534 GL_MAP_READ_BIT
, &depthMap
, &depthStride
);
536 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
540 if (stencilRb
!= depthRb
) {
541 ctx
->Driver
.MapRenderbuffer(ctx
, stencilRb
, x
, y
, width
, height
,
542 GL_MAP_READ_BIT
, &stencilMap
,
545 ctx
->Driver
.UnmapRenderbuffer(ctx
, depthRb
);
546 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
551 stencilMap
= depthMap
;
552 stencilStride
= depthStride
;
555 stencilVals
= malloc(width
* sizeof(GLubyte
));
556 depthVals
= malloc(width
* sizeof(GLfloat
));
558 if (stencilVals
&& depthVals
) {
559 for (j
= 0; j
< height
; j
++) {
560 _mesa_unpack_float_z_row(depthRb
->Format
, width
, depthMap
, depthVals
);
561 _mesa_unpack_ubyte_stencil_row(stencilRb
->Format
, width
,
562 stencilMap
, stencilVals
);
564 _mesa_pack_depth_stencil_span(ctx
, width
, type
, (GLuint
*)dst
,
565 depthVals
, stencilVals
, packing
);
567 depthMap
+= depthStride
;
568 stencilMap
+= stencilStride
;
573 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
579 ctx
->Driver
.UnmapRenderbuffer(ctx
, depthRb
);
580 if (stencilRb
!= depthRb
) {
581 ctx
->Driver
.UnmapRenderbuffer(ctx
, stencilRb
);
587 * Read combined depth/stencil values.
588 * We'll have already done error checking to be sure the expected
589 * depth and stencil buffers really exist.
592 read_depth_stencil_pixels(struct gl_context
*ctx
,
594 GLsizei width
, GLsizei height
,
595 GLenum type
, GLvoid
*pixels
,
596 const struct gl_pixelstore_attrib
*packing
)
598 const GLboolean scaleOrBias
599 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
600 const GLboolean stencilTransfer
= ctx
->Pixel
.IndexShift
601 || ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapStencilFlag
;
605 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
,
607 GL_DEPTH_STENCIL_EXT
,
609 dstStride
= _mesa_image_row_stride(packing
, width
,
610 GL_DEPTH_STENCIL_EXT
, type
);
612 /* Fast 24/8 reads. */
613 if (type
== GL_UNSIGNED_INT_24_8
&&
614 !scaleOrBias
&& !stencilTransfer
&& !packing
->SwapBytes
) {
615 if (fast_read_depth_stencil_pixels(ctx
, x
, y
, width
, height
,
619 if (fast_read_depth_stencil_pixels_separate(ctx
, x
, y
, width
, height
,
620 (uint32_t *)dst
, dstStride
))
624 slow_read_depth_stencil_pixels_separate(ctx
, x
, y
, width
, height
,
632 * Software fallback routine for ctx->Driver.ReadPixels().
633 * By time we get here, all error checking will have been done.
636 _mesa_readpixels(struct gl_context
*ctx
,
637 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
638 GLenum format
, GLenum type
,
639 const struct gl_pixelstore_attrib
*packing
,
642 struct gl_pixelstore_attrib clippedPacking
= *packing
;
645 _mesa_update_state(ctx
);
647 /* Do all needed clipping here, so that we can forget about it later */
648 if (_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
650 pixels
= _mesa_map_pbo_dest(ctx
, &clippedPacking
, pixels
);
654 case GL_STENCIL_INDEX
:
655 read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
658 case GL_DEPTH_COMPONENT
:
659 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
662 case GL_DEPTH_STENCIL_EXT
:
663 read_depth_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
667 /* all other formats should be color formats */
668 read_rgba_pixels(ctx
, x
, y
, width
, height
, format
, type
, pixels
,
672 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
679 _mesa_ReadnPixelsARB( GLint x
, GLint y
, GLsizei width
, GLsizei height
,
680 GLenum format
, GLenum type
, GLsizei bufSize
,
685 GET_CURRENT_CONTEXT(ctx
);
686 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
688 FLUSH_CURRENT(ctx
, 0);
690 if (MESA_VERBOSE
& VERBOSE_API
)
691 _mesa_debug(ctx
, "glReadPixels(%d, %d, %s, %s, %p)\n",
693 _mesa_lookup_enum_by_nr(format
),
694 _mesa_lookup_enum_by_nr(type
),
697 if (width
< 0 || height
< 0) {
698 _mesa_error( ctx
, GL_INVALID_VALUE
,
699 "glReadPixels(width=%d height=%d)", width
, height
);
703 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
704 * combinations of format and type that can be used.
706 * Technically, only two combinations are actually allowed:
707 * GL_RGBA/GL_UNSIGNED_BYTE, and some implementation-specific internal
708 * preferred combination. This code doesn't know what that preferred
709 * combination is, and Mesa can handle anything valid. Just work instead.
711 if (_mesa_is_gles(ctx
) && ctx
->Version
< 30) {
712 err
= _mesa_es_error_check_format_and_type(format
, type
, 2);
713 if (err
== GL_NO_ERROR
) {
714 if (type
== GL_FLOAT
|| type
== GL_HALF_FLOAT_OES
) {
715 err
= GL_INVALID_OPERATION
;
716 } else if (format
== GL_DEPTH_COMPONENT
717 || format
== GL_DEPTH_STENCIL
) {
718 err
= GL_INVALID_ENUM
;
722 if (err
!= GL_NO_ERROR
) {
723 _mesa_error(ctx
, err
, "glReadPixels(invalid format %s and/or type %s)",
724 _mesa_lookup_enum_by_nr(format
),
725 _mesa_lookup_enum_by_nr(type
));
731 _mesa_update_state(ctx
);
733 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
734 if (err
!= GL_NO_ERROR
) {
735 _mesa_error(ctx
, err
, "glReadPixels(invalid format %s and/or type %s)",
736 _mesa_lookup_enum_by_nr(format
),
737 _mesa_lookup_enum_by_nr(type
));
741 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
742 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
743 "glReadPixels(incomplete framebuffer)" );
747 /* Check that the destination format and source buffer are both
748 * integer-valued or both non-integer-valued.
750 if (ctx
->Extensions
.EXT_texture_integer
&& _mesa_is_color_format(format
)) {
751 const struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
752 const GLboolean srcInteger
= _mesa_is_format_integer_color(rb
->Format
);
753 const GLboolean dstInteger
= _mesa_is_enum_format_integer(format
);
754 if (dstInteger
!= srcInteger
) {
755 _mesa_error(ctx
, GL_INVALID_OPERATION
,
756 "glReadPixels(integer / non-integer format mismatch");
761 if (_mesa_is_user_fbo(ctx
->ReadBuffer
) &&
762 ctx
->ReadBuffer
->Visual
.samples
> 0) {
763 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(multisample FBO)");
767 if (!_mesa_source_buffer_exists(ctx
, format
)) {
768 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(no readbuffer)");
772 if (width
== 0 || height
== 0)
773 return; /* nothing to do */
775 if (!_mesa_validate_pbo_access(2, &ctx
->Pack
, width
, height
, 1,
776 format
, type
, bufSize
, pixels
)) {
777 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
778 _mesa_error(ctx
, GL_INVALID_OPERATION
,
779 "glReadPixels(out of bounds PBO access)");
781 _mesa_error(ctx
, GL_INVALID_OPERATION
,
782 "glReadnPixelsARB(out of bounds access:"
783 " bufSize (%d) is too small)", bufSize
);
788 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) &&
789 _mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
790 /* buffer is mapped - that's an error */
791 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(PBO is mapped)");
795 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
796 format
, type
, &ctx
->Pack
, pixels
);
800 _mesa_ReadPixels( GLint x
, GLint y
, GLsizei width
, GLsizei height
,
801 GLenum format
, GLenum type
, GLvoid
*pixels
)
803 _mesa_ReadnPixelsARB(x
, y
, width
, height
, format
, type
, INT_MAX
, pixels
);