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
);
200 fast_read_rgba_pixels_memcpy( struct gl_context
*ctx
,
202 GLsizei width
, GLsizei height
,
203 GLenum format
, GLenum type
,
205 const struct gl_pixelstore_attrib
*packing
,
206 GLbitfield transferOps
)
208 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
210 int dstStride
, stride
, j
, texelBytes
;
212 if (!_mesa_format_matches_format_and_type(rb
->Format
, format
, type
))
215 /* check for things we can't handle here */
216 if (packing
->SwapBytes
||
221 dstStride
= _mesa_image_row_stride(packing
, width
, format
, type
);
222 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
225 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
228 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
229 return GL_TRUE
; /* don't bother trying the slow path */
232 texelBytes
= _mesa_get_format_bytes(rb
->Format
);
233 for (j
= 0; j
< height
; j
++) {
234 memcpy(dst
, map
, width
* texelBytes
);
239 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
245 slow_read_rgba_pixels( struct gl_context
*ctx
,
247 GLsizei width
, GLsizei height
,
248 GLenum format
, GLenum type
,
250 const struct gl_pixelstore_attrib
*packing
,
251 GLbitfield transferOps
)
253 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
254 const gl_format rbFormat
= _mesa_get_srgb_format_linear(rb
->Format
);
257 int dstStride
, stride
, j
;
259 dstStride
= _mesa_image_row_stride(packing
, width
, format
, type
);
260 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
263 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
266 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
270 rgba
= malloc(width
* MAX_PIXEL_BYTES
);
274 for (j
= 0; j
< height
; j
++) {
275 if (_mesa_is_integer_format(format
)) {
276 _mesa_unpack_uint_rgba_row(rbFormat
, width
, map
, (GLuint (*)[4]) rgba
);
277 _mesa_pack_rgba_span_int(ctx
, width
, (GLuint (*)[4]) rgba
, format
,
280 _mesa_unpack_rgba_row(rbFormat
, width
, map
, (GLfloat (*)[4]) rgba
);
281 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
282 type
, dst
, packing
, transferOps
);
291 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
295 * Read R, G, B, A, RGB, L, or LA pixels.
298 read_rgba_pixels( struct gl_context
*ctx
,
300 GLsizei width
, GLsizei height
,
301 GLenum format
, GLenum type
, GLvoid
*pixels
,
302 const struct gl_pixelstore_attrib
*packing
)
304 GLbitfield transferOps
= ctx
->_ImageTransferState
;
305 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
306 struct gl_renderbuffer
*rb
= fb
->_ColorReadBuffer
;
311 if ((ctx
->Color
._ClampReadColor
== GL_TRUE
|| type
!= GL_FLOAT
) &&
312 !_mesa_is_integer_format(format
)) {
313 transferOps
|= IMAGE_CLAMP_BIT
;
317 /* Try the optimized paths first. */
318 if (fast_read_rgba_pixels_memcpy(ctx
, x
, y
, width
, height
,
319 format
, type
, pixels
, packing
,
325 slow_read_rgba_pixels(ctx
, x
, y
, width
, height
,
326 format
, type
, pixels
, packing
, transferOps
);
330 * For a packed depth/stencil buffer being read as depth/stencil, just memcpy the
331 * data (possibly swapping 8/24 vs 24/8 as we go).
334 fast_read_depth_stencil_pixels(struct gl_context
*ctx
,
336 GLsizei width
, GLsizei height
,
337 GLubyte
*dst
, int dstStride
)
339 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
340 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
341 struct gl_renderbuffer
*stencilRb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
348 if (rb
->Format
!= MESA_FORMAT_Z24_S8
&&
349 rb
->Format
!= MESA_FORMAT_S8_Z24
)
352 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
355 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
356 return GL_TRUE
; /* don't bother trying the slow path */
359 for (i
= 0; i
< height
; i
++) {
360 _mesa_unpack_uint_24_8_depth_stencil_row(rb
->Format
, width
,
366 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
373 * For non-float-depth and stencil buffers being read as 24/8 depth/stencil,
374 * copy the integer data directly instead of converting depth to float and
378 fast_read_depth_stencil_pixels_separate(struct gl_context
*ctx
,
380 GLsizei width
, GLsizei height
,
381 uint32_t *dst
, int dstStride
)
383 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
384 struct gl_renderbuffer
*depthRb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
385 struct gl_renderbuffer
*stencilRb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
386 GLubyte
*depthMap
, *stencilMap
;
387 int depthStride
, stencilStride
, i
, j
;
389 if (_mesa_get_format_datatype(depthRb
->Format
) != GL_UNSIGNED_NORMALIZED
)
392 ctx
->Driver
.MapRenderbuffer(ctx
, depthRb
, x
, y
, width
, height
,
393 GL_MAP_READ_BIT
, &depthMap
, &depthStride
);
395 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
396 return GL_TRUE
; /* don't bother trying the slow path */
399 ctx
->Driver
.MapRenderbuffer(ctx
, stencilRb
, x
, y
, width
, height
,
400 GL_MAP_READ_BIT
, &stencilMap
, &stencilStride
);
402 ctx
->Driver
.UnmapRenderbuffer(ctx
, depthRb
);
403 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
404 return GL_TRUE
; /* don't bother trying the slow path */
407 for (j
= 0; j
< height
; j
++) {
408 GLubyte stencilVals
[MAX_WIDTH
];
410 _mesa_unpack_uint_z_row(depthRb
->Format
, width
, depthMap
, dst
);
411 _mesa_unpack_ubyte_stencil_row(stencilRb
->Format
, width
,
412 stencilMap
, stencilVals
);
414 for (i
= 0; i
< width
; i
++) {
415 dst
[i
] = (dst
[i
] & 0xffffff00) | stencilVals
[i
];
418 depthMap
+= depthStride
;
419 stencilMap
+= stencilStride
;
420 dst
+= dstStride
/ 4;
423 ctx
->Driver
.UnmapRenderbuffer(ctx
, depthRb
);
424 ctx
->Driver
.UnmapRenderbuffer(ctx
, stencilRb
);
430 slow_read_depth_stencil_pixels_separate(struct gl_context
*ctx
,
432 GLsizei width
, GLsizei height
,
434 const struct gl_pixelstore_attrib
*packing
,
435 GLubyte
*dst
, int dstStride
)
437 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
438 struct gl_renderbuffer
*depthRb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
439 struct gl_renderbuffer
*stencilRb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
440 GLubyte
*depthMap
, *stencilMap
;
441 int depthStride
, stencilStride
, j
;
443 /* The depth and stencil buffers might be separate, or a single buffer.
444 * If one buffer, only map it once.
446 ctx
->Driver
.MapRenderbuffer(ctx
, depthRb
, x
, y
, width
, height
,
447 GL_MAP_READ_BIT
, &depthMap
, &depthStride
);
449 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
453 if (stencilRb
!= depthRb
) {
454 ctx
->Driver
.MapRenderbuffer(ctx
, stencilRb
, x
, y
, width
, height
,
455 GL_MAP_READ_BIT
, &stencilMap
,
458 ctx
->Driver
.UnmapRenderbuffer(ctx
, depthRb
);
459 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
464 stencilMap
= depthMap
;
465 stencilStride
= depthStride
;
468 for (j
= 0; j
< height
; j
++) {
469 GLubyte stencilVals
[MAX_WIDTH
];
470 GLfloat depthVals
[MAX_WIDTH
];
472 _mesa_unpack_float_z_row(depthRb
->Format
, width
, depthMap
, depthVals
);
473 _mesa_unpack_ubyte_stencil_row(stencilRb
->Format
, width
,
474 stencilMap
, stencilVals
);
476 _mesa_pack_depth_stencil_span(ctx
, width
, type
, (GLuint
*)dst
,
477 depthVals
, stencilVals
, packing
);
479 depthMap
+= depthStride
;
480 stencilMap
+= stencilStride
;
484 ctx
->Driver
.UnmapRenderbuffer(ctx
, depthRb
);
485 if (stencilRb
!= depthRb
) {
486 ctx
->Driver
.UnmapRenderbuffer(ctx
, stencilRb
);
492 * Read combined depth/stencil values.
493 * We'll have already done error checking to be sure the expected
494 * depth and stencil buffers really exist.
497 read_depth_stencil_pixels(struct gl_context
*ctx
,
499 GLsizei width
, GLsizei height
,
500 GLenum type
, GLvoid
*pixels
,
501 const struct gl_pixelstore_attrib
*packing
)
503 const GLboolean scaleOrBias
504 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
505 const GLboolean stencilTransfer
= ctx
->Pixel
.IndexShift
506 || ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapStencilFlag
;
510 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
,
512 GL_DEPTH_STENCIL_EXT
,
514 dstStride
= _mesa_image_row_stride(packing
, width
,
515 GL_DEPTH_STENCIL_EXT
, type
);
517 /* Fast 24/8 reads. */
518 if (type
== GL_UNSIGNED_INT_24_8
&&
519 !scaleOrBias
&& !stencilTransfer
&& !packing
->SwapBytes
) {
520 if (fast_read_depth_stencil_pixels(ctx
, x
, y
, width
, height
,
524 if (fast_read_depth_stencil_pixels_separate(ctx
, x
, y
, width
, height
,
525 (uint32_t *)dst
, dstStride
))
529 slow_read_depth_stencil_pixels_separate(ctx
, x
, y
, width
, height
,
537 * Software fallback routine for ctx->Driver.ReadPixels().
538 * By time we get here, all error checking will have been done.
541 _mesa_readpixels(struct gl_context
*ctx
,
542 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
543 GLenum format
, GLenum type
,
544 const struct gl_pixelstore_attrib
*packing
,
547 struct gl_pixelstore_attrib clippedPacking
= *packing
;
550 _mesa_update_state(ctx
);
552 /* Do all needed clipping here, so that we can forget about it later */
553 if (_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
555 pixels
= _mesa_map_pbo_dest(ctx
, &clippedPacking
, pixels
);
559 case GL_STENCIL_INDEX
:
560 read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
563 case GL_DEPTH_COMPONENT
:
564 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
567 case GL_DEPTH_STENCIL_EXT
:
568 read_depth_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
572 /* all other formats should be color formats */
573 read_rgba_pixels(ctx
, x
, y
, width
, height
, format
, type
, pixels
,
577 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
584 * Do error checking of the format/type parameters to glReadPixels and
586 * \param drawing if GL_TRUE do checking for DrawPixels, else do checking
588 * \return GL_TRUE if error detected, GL_FALSE if no errors
591 _mesa_error_check_format_type(struct gl_context
*ctx
, GLenum format
,
592 GLenum type
, GLboolean drawing
)
594 const char *readDraw
= drawing
? "Draw" : "Read";
595 const GLboolean reading
= !drawing
;
597 /* state validation should have already been done */
598 ASSERT(ctx
->NewState
== 0x0);
600 if (ctx
->Extensions
.EXT_packed_depth_stencil
601 && type
== GL_UNSIGNED_INT_24_8_EXT
602 && format
!= GL_DEPTH_STENCIL_EXT
) {
603 _mesa_error(ctx
, GL_INVALID_OPERATION
,
604 "gl%sPixels(format is not GL_DEPTH_STENCIL_EXT)", readDraw
);
608 if (ctx
->Extensions
.ARB_depth_buffer_float
609 && type
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
610 && format
!= GL_DEPTH_STENCIL_EXT
) {
611 _mesa_error(ctx
, GL_INVALID_OPERATION
,
612 "gl%sPixels(format is not GL_DEPTH_STENCIL_EXT)", readDraw
);
616 /* basic combinations test */
617 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
618 _mesa_error(ctx
, GL_INVALID_ENUM
,
619 "gl%sPixels(format or type)", readDraw
);
623 /* additional checks */
631 case GL_LUMINANCE_ALPHA
:
637 case GL_RED_INTEGER_EXT
:
638 case GL_GREEN_INTEGER_EXT
:
639 case GL_BLUE_INTEGER_EXT
:
640 case GL_ALPHA_INTEGER_EXT
:
641 case GL_RGB_INTEGER_EXT
:
642 case GL_RGBA_INTEGER_EXT
:
643 case GL_BGR_INTEGER_EXT
:
644 case GL_BGRA_INTEGER_EXT
:
645 case GL_LUMINANCE_INTEGER_EXT
:
646 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
649 if (!_mesa_source_buffer_exists(ctx
, GL_COLOR
)) {
650 _mesa_error(ctx
, GL_INVALID_OPERATION
,
651 "glReadPixels(no color buffer)");
658 if (ctx
->PixelMaps
.ItoR
.Size
== 0 ||
659 ctx
->PixelMaps
.ItoG
.Size
== 0 ||
660 ctx
->PixelMaps
.ItoB
.Size
== 0) {
661 _mesa_error(ctx
, GL_INVALID_OPERATION
,
662 "glDrawPixels(drawing color index pixels into RGB buffer)");
668 if (!_mesa_source_buffer_exists(ctx
, GL_COLOR
)) {
669 _mesa_error(ctx
, GL_INVALID_OPERATION
,
670 "glReadPixels(no color buffer)");
673 /* We no longer support CI-mode color buffers so trying to read
674 * GL_COLOR_INDEX pixels is always an error.
676 _mesa_error(ctx
, GL_INVALID_OPERATION
,
677 "glReadPixels(color buffer is RGB)");
681 case GL_STENCIL_INDEX
:
682 if ((drawing
&& !_mesa_dest_buffer_exists(ctx
, format
)) ||
683 (reading
&& !_mesa_source_buffer_exists(ctx
, format
))) {
684 _mesa_error(ctx
, GL_INVALID_OPERATION
,
685 "gl%sPixels(no stencil buffer)", readDraw
);
689 case GL_DEPTH_COMPONENT
:
690 if ((drawing
&& !_mesa_dest_buffer_exists(ctx
, format
))) {
691 _mesa_error(ctx
, GL_INVALID_OPERATION
,
692 "gl%sPixels(no depth buffer)", readDraw
);
696 case GL_DEPTH_STENCIL_EXT
:
697 /* Check validity of the type first. */
699 case GL_UNSIGNED_INT_24_8_EXT
:
700 if (!ctx
->Extensions
.EXT_packed_depth_stencil
) {
701 _mesa_error(ctx
, GL_INVALID_ENUM
, "gl%sPixels(type)", readDraw
);
705 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
706 if (!ctx
->Extensions
.ARB_depth_buffer_float
) {
707 _mesa_error(ctx
, GL_INVALID_ENUM
, "gl%sPixels(type)", readDraw
);
712 _mesa_error(ctx
, GL_INVALID_ENUM
, "gl%sPixels(type)", readDraw
);
715 if ((drawing
&& !_mesa_dest_buffer_exists(ctx
, format
)) ||
716 (reading
&& !_mesa_source_buffer_exists(ctx
, format
))) {
717 _mesa_error(ctx
, GL_INVALID_OPERATION
,
718 "gl%sPixels(no depth or stencil buffer)", readDraw
);
723 /* this should have been caught in _mesa_is_legal_format_type() */
724 _mesa_problem(ctx
, "unexpected format in _mesa_%sPixels", readDraw
);
735 _mesa_ReadnPixelsARB( GLint x
, GLint y
, GLsizei width
, GLsizei height
,
736 GLenum format
, GLenum type
, GLsizei bufSize
,
739 GET_CURRENT_CONTEXT(ctx
);
740 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
742 FLUSH_CURRENT(ctx
, 0);
744 if (MESA_VERBOSE
& VERBOSE_API
)
745 _mesa_debug(ctx
, "glReadPixels(%d, %d, %s, %s, %p)\n",
747 _mesa_lookup_enum_by_nr(format
),
748 _mesa_lookup_enum_by_nr(type
),
751 if (width
< 0 || height
< 0) {
752 _mesa_error( ctx
, GL_INVALID_VALUE
,
753 "glReadPixels(width=%d height=%d)", width
, height
);
758 _mesa_update_state(ctx
);
760 if (_mesa_error_check_format_type(ctx
, format
, type
, GL_FALSE
)) {
765 /* Check that the destination format and source buffer are both
766 * integer-valued or both non-integer-valued.
768 if (ctx
->Extensions
.EXT_texture_integer
&& _mesa_is_color_format(format
)) {
769 const struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
770 const GLboolean srcInteger
= _mesa_is_format_integer_color(rb
->Format
);
771 const GLboolean dstInteger
= _mesa_is_integer_format(format
);
772 if (dstInteger
!= srcInteger
) {
773 _mesa_error(ctx
, GL_INVALID_OPERATION
,
774 "glReadPixels(integer / non-integer format mismatch");
779 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
780 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
781 "glReadPixels(incomplete framebuffer)" );
785 if (ctx
->ReadBuffer
->Name
!= 0 && ctx
->ReadBuffer
->Visual
.samples
> 0) {
786 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(multisample FBO)");
790 if (!_mesa_source_buffer_exists(ctx
, format
)) {
791 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(no readbuffer)");
795 if (width
== 0 || height
== 0)
796 return; /* nothing to do */
798 if (!_mesa_validate_pbo_access(2, &ctx
->Pack
, width
, height
, 1,
799 format
, type
, bufSize
, pixels
)) {
800 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
801 _mesa_error(ctx
, GL_INVALID_OPERATION
,
802 "glReadPixels(out of bounds PBO access)");
804 _mesa_error(ctx
, GL_INVALID_OPERATION
,
805 "glReadnPixelsARB(out of bounds access:"
806 " bufSize (%d) is too small)", bufSize
);
811 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) &&
812 _mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
813 /* buffer is mapped - that's an error */
814 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(PBO is mapped)");
818 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
819 format
, type
, &ctx
->Pack
, pixels
);
823 _mesa_ReadPixels( GLint x
, GLint y
, GLsizei width
, GLsizei height
,
824 GLenum format
, GLenum type
, GLvoid
*pixels
)
826 _mesa_ReadnPixelsARB(x
, y
, width
, height
, format
, type
, INT_MAX
, pixels
);