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"
44 * Return true if the conversion L=R+G+B is needed.
47 need_rgb_to_luminance_conversion(gl_format texFormat
, GLenum format
)
49 GLenum baseTexFormat
= _mesa_get_format_base_format(texFormat
);
51 return (baseTexFormat
== GL_RG
||
52 baseTexFormat
== GL_RGB
||
53 baseTexFormat
== GL_RGBA
) &&
54 (format
== GL_LUMINANCE
|| format
== GL_LUMINANCE_ALPHA
);
59 * Return transfer op flags for this ReadPixels operation.
62 get_readpixels_transfer_ops(const struct gl_context
*ctx
, gl_format texFormat
,
63 GLenum format
, GLenum type
, GLboolean uses_blit
)
65 GLbitfield transferOps
= ctx
->_ImageTransferState
;
67 if (format
== GL_DEPTH_COMPONENT
||
68 format
== GL_DEPTH_STENCIL
||
69 format
== GL_STENCIL_INDEX
) {
73 /* Pixel transfer ops (scale, bias, table lookup) do not apply
76 if (_mesa_is_enum_format_integer(format
)) {
81 /* For blit-based ReadPixels packing, the clamping is done automatically
82 * unless the type is float. */
83 if (ctx
->Color
._ClampReadColor
== GL_TRUE
&&
84 (type
== GL_FLOAT
|| type
== GL_HALF_FLOAT
)) {
85 transferOps
|= IMAGE_CLAMP_BIT
;
89 /* For CPU-based ReadPixels packing, the clamping must always be done
90 * for non-float types, */
91 if (ctx
->Color
._ClampReadColor
== GL_TRUE
||
92 (type
!= GL_FLOAT
&& type
!= GL_HALF_FLOAT
)) {
93 transferOps
|= IMAGE_CLAMP_BIT
;
97 /* If the format is unsigned normalized, we can ignore clamping
98 * because the values are already in the range [0,1] so it won't
99 * have any effect anyway.
101 if (_mesa_get_format_datatype(texFormat
) == GL_UNSIGNED_NORMALIZED
&&
102 !need_rgb_to_luminance_conversion(texFormat
, format
)) {
103 transferOps
&= ~IMAGE_CLAMP_BIT
;
111 * Return true if memcpy cannot be used for ReadPixels.
113 * If uses_blit is true, the function returns true if a simple 3D engine blit
114 * cannot be used for ReadPixels packing.
116 * NOTE: This doesn't take swizzling and format conversions between
117 * the readbuffer and the pixel pack buffer into account.
120 _mesa_readpixels_needs_slow_path(const struct gl_context
*ctx
, GLenum format
,
121 GLenum type
, GLboolean uses_blit
)
123 struct gl_renderbuffer
*rb
=
124 _mesa_get_read_renderbuffer_for_format(ctx
, format
);
129 /* There are different rules depending on the base format. */
131 case GL_DEPTH_STENCIL
:
132 return !_mesa_has_depthstencil_combined(ctx
->ReadBuffer
) ||
133 ctx
->Pixel
.DepthScale
!= 1.0f
|| ctx
->Pixel
.DepthBias
!= 0.0f
||
134 ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
||
135 ctx
->Pixel
.MapStencilFlag
;
137 case GL_DEPTH_COMPONENT
:
138 return ctx
->Pixel
.DepthScale
!= 1.0f
|| ctx
->Pixel
.DepthBias
!= 0.0f
;
140 case GL_STENCIL_INDEX
:
141 return ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
||
142 ctx
->Pixel
.MapStencilFlag
;
146 if (need_rgb_to_luminance_conversion(rb
->Format
, format
)) {
150 /* Conversion between signed and unsigned integers needs masking
151 * (it isn't just memcpy). */
152 srcType
= _mesa_get_format_datatype(rb
->Format
);
154 if ((srcType
== GL_INT
&&
155 (type
== GL_UNSIGNED_INT
||
156 type
== GL_UNSIGNED_SHORT
||
157 type
== GL_UNSIGNED_BYTE
)) ||
158 (srcType
== GL_UNSIGNED_INT
&&
165 /* And finally, see if there are any transfer ops. */
166 return get_readpixels_transfer_ops(ctx
, rb
->Format
, format
, type
,
174 readpixels_can_use_memcpy(const struct gl_context
*ctx
, GLenum format
, GLenum type
,
175 const struct gl_pixelstore_attrib
*packing
)
177 struct gl_renderbuffer
*rb
=
178 _mesa_get_read_renderbuffer_for_format(ctx
, format
);
182 if (_mesa_readpixels_needs_slow_path(ctx
, format
, type
, GL_FALSE
)) {
186 /* The base internal format and the base Mesa format must match. */
187 if (rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
)) {
191 /* The Mesa format must match the input format and type. */
192 if (!_mesa_format_matches_format_and_type(rb
->Format
, format
, type
,
193 packing
->SwapBytes
)) {
202 readpixels_memcpy(struct gl_context
*ctx
,
204 GLsizei width
, GLsizei height
,
205 GLenum format
, GLenum type
,
207 const struct gl_pixelstore_attrib
*packing
)
209 struct gl_renderbuffer
*rb
=
210 _mesa_get_read_renderbuffer_for_format(ctx
, format
);
212 int dstStride
, stride
, j
, texelBytes
;
214 /* Fail if memcpy cannot be used. */
215 if (!readpixels_can_use_memcpy(ctx
, format
, type
, packing
)) {
219 dstStride
= _mesa_image_row_stride(packing
, width
, format
, type
);
220 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
223 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
226 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
227 return GL_TRUE
; /* don't bother trying the slow path */
230 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 * Optimized path for conversion of depth values to GL_DEPTH_COMPONENT,
249 read_uint_depth_pixels( struct gl_context
*ctx
,
251 GLsizei width
, GLsizei height
,
252 GLenum type
, GLvoid
*pixels
,
253 const struct gl_pixelstore_attrib
*packing
)
255 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
256 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
258 int stride
, dstStride
, j
;
260 if (ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0)
263 if (packing
->SwapBytes
)
266 if (_mesa_get_format_datatype(rb
->Format
) != GL_UNSIGNED_NORMALIZED
)
269 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
273 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
274 return GL_TRUE
; /* don't bother trying the slow path */
277 dstStride
= _mesa_image_row_stride(packing
, width
, GL_DEPTH_COMPONENT
, type
);
278 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
279 GL_DEPTH_COMPONENT
, type
, 0, 0);
281 for (j
= 0; j
< height
; j
++) {
282 _mesa_unpack_uint_z_row(rb
->Format
, width
, map
, (GLuint
*)dst
);
287 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
293 * Read pixels for format=GL_DEPTH_COMPONENT.
296 read_depth_pixels( struct gl_context
*ctx
,
298 GLsizei width
, GLsizei height
,
299 GLenum type
, GLvoid
*pixels
,
300 const struct gl_pixelstore_attrib
*packing
)
302 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
303 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
306 int dstStride
, stride
;
307 GLfloat
*depthValues
;
312 /* clipping should have been done already */
315 ASSERT(x
+ width
<= (GLint
) rb
->Width
);
316 ASSERT(y
+ height
<= (GLint
) rb
->Height
);
318 if (type
== GL_UNSIGNED_INT
&&
319 read_uint_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
, packing
)) {
323 dstStride
= _mesa_image_row_stride(packing
, width
, GL_DEPTH_COMPONENT
, type
);
324 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
325 GL_DEPTH_COMPONENT
, type
, 0, 0);
327 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
330 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
334 depthValues
= malloc(width
* sizeof(GLfloat
));
337 /* General case (slower) */
338 for (j
= 0; j
< height
; j
++, y
++) {
339 _mesa_unpack_float_z_row(rb
->Format
, width
, map
, depthValues
);
340 _mesa_pack_depth_span(ctx
, width
, dst
, type
, depthValues
, packing
);
347 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
352 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
357 * Read pixels for format=GL_STENCIL_INDEX.
360 read_stencil_pixels( struct gl_context
*ctx
,
362 GLsizei width
, GLsizei height
,
363 GLenum type
, GLvoid
*pixels
,
364 const struct gl_pixelstore_attrib
*packing
)
366 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
367 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
369 GLubyte
*map
, *stencil
;
375 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
378 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
382 stencil
= malloc(width
* sizeof(GLubyte
));
385 /* process image row by row */
386 for (j
= 0; j
< height
; j
++) {
389 _mesa_unpack_ubyte_stencil_row(rb
->Format
, width
, map
, stencil
);
390 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
391 GL_STENCIL_INDEX
, type
, j
, 0);
393 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, stencil
, packing
);
399 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
404 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
409 * Try to do glReadPixels of RGBA data using swizzle.
410 * \return GL_TRUE if successful, GL_FALSE otherwise (use the slow path)
413 read_rgba_pixels_swizzle(struct gl_context
*ctx
,
415 GLsizei width
, GLsizei height
,
416 GLenum format
, GLenum type
,
418 const struct gl_pixelstore_attrib
*packing
)
420 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
422 int dstStride
, stride
, j
;
423 GLboolean swizzle_rb
= GL_FALSE
, copy_xrgb
= GL_FALSE
;
425 /* XXX we could check for other swizzle/special cases here as needed */
426 if (rb
->Format
== MESA_FORMAT_RGBA8888_REV
&&
428 type
== GL_UNSIGNED_INT_8_8_8_8_REV
&&
429 !ctx
->Pack
.SwapBytes
) {
430 swizzle_rb
= GL_TRUE
;
432 else if (rb
->Format
== MESA_FORMAT_XRGB8888
&&
434 type
== GL_UNSIGNED_INT_8_8_8_8_REV
&&
435 !ctx
->Pack
.SwapBytes
) {
442 dstStride
= _mesa_image_row_stride(packing
, width
, format
, type
);
443 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
446 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
449 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
450 return GL_TRUE
; /* don't bother trying the slow path */
455 for (j
= 0; j
< height
; j
++) {
457 for (i
= 0; i
< width
; i
++) {
458 GLuint
*dst4
= (GLuint
*) dst
, *map4
= (GLuint
*) map
;
459 GLuint pixel
= map4
[i
];
460 dst4
[i
] = (pixel
& 0xff00ff00)
461 | ((pixel
& 0x00ff0000) >> 16)
462 | ((pixel
& 0x000000ff) << 16);
467 } else if (copy_xrgb
) {
468 /* convert xrgb -> argb */
469 for (j
= 0; j
< height
; j
++) {
470 GLuint
*dst4
= (GLuint
*) dst
, *map4
= (GLuint
*) map
;
472 for (i
= 0; i
< width
; i
++) {
473 dst4
[i
] = map4
[i
] | 0xff000000; /* set A=0xff */
480 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
486 slow_read_rgba_pixels( struct gl_context
*ctx
,
488 GLsizei width
, GLsizei height
,
489 GLenum format
, GLenum type
,
491 const struct gl_pixelstore_attrib
*packing
,
492 GLbitfield transferOps
)
494 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
495 const gl_format rbFormat
= _mesa_get_srgb_format_linear(rb
->Format
);
498 int dstStride
, stride
, j
;
499 GLboolean dst_is_integer
= _mesa_is_enum_format_integer(format
);
500 GLboolean dst_is_uint
= _mesa_is_format_unsigned(rbFormat
);
502 dstStride
= _mesa_image_row_stride(packing
, width
, format
, type
);
503 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
506 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
509 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
513 rgba
= malloc(width
* MAX_PIXEL_BYTES
);
517 for (j
= 0; j
< height
; j
++) {
518 if (dst_is_integer
) {
519 _mesa_unpack_uint_rgba_row(rbFormat
, width
, map
, (GLuint (*)[4]) rgba
);
520 _mesa_rebase_rgba_uint(width
, (GLuint (*)[4]) rgba
,
523 _mesa_pack_rgba_span_from_uints(ctx
, width
, (GLuint (*)[4]) rgba
, format
,
526 _mesa_pack_rgba_span_from_ints(ctx
, width
, (GLint (*)[4]) rgba
, format
,
530 _mesa_unpack_rgba_row(rbFormat
, width
, map
, (GLfloat (*)[4]) rgba
);
531 _mesa_rebase_rgba_float(width
, (GLfloat (*)[4]) rgba
,
533 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
534 type
, dst
, packing
, transferOps
);
543 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
547 * Read R, G, B, A, RGB, L, or LA pixels.
550 read_rgba_pixels( struct gl_context
*ctx
,
552 GLsizei width
, GLsizei height
,
553 GLenum format
, GLenum type
, GLvoid
*pixels
,
554 const struct gl_pixelstore_attrib
*packing
)
556 GLbitfield transferOps
;
557 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
558 struct gl_renderbuffer
*rb
= fb
->_ColorReadBuffer
;
563 transferOps
= get_readpixels_transfer_ops(ctx
, rb
->Format
, format
, type
,
566 /* Try the optimized paths first. */
568 read_rgba_pixels_swizzle(ctx
, x
, y
, width
, height
,
569 format
, type
, pixels
, packing
)) {
573 slow_read_rgba_pixels(ctx
, x
, y
, width
, height
,
574 format
, type
, pixels
, packing
, transferOps
);
578 * For a packed depth/stencil buffer being read as depth/stencil, just memcpy the
579 * data (possibly swapping 8/24 vs 24/8 as we go).
582 fast_read_depth_stencil_pixels(struct gl_context
*ctx
,
584 GLsizei width
, GLsizei height
,
585 GLubyte
*dst
, int dstStride
)
587 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
588 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
589 struct gl_renderbuffer
*stencilRb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
596 if (rb
->Format
!= MESA_FORMAT_Z24_S8
&&
597 rb
->Format
!= MESA_FORMAT_S8_Z24
)
600 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
603 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
604 return GL_TRUE
; /* don't bother trying the slow path */
607 for (i
= 0; i
< height
; i
++) {
608 _mesa_unpack_uint_24_8_depth_stencil_row(rb
->Format
, width
,
614 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
621 * For non-float-depth and stencil buffers being read as 24/8 depth/stencil,
622 * copy the integer data directly instead of converting depth to float and
626 fast_read_depth_stencil_pixels_separate(struct gl_context
*ctx
,
628 GLsizei width
, GLsizei height
,
629 uint32_t *dst
, int dstStride
)
631 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
632 struct gl_renderbuffer
*depthRb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
633 struct gl_renderbuffer
*stencilRb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
634 GLubyte
*depthMap
, *stencilMap
, *stencilVals
;
635 int depthStride
, stencilStride
, i
, j
;
637 if (_mesa_get_format_datatype(depthRb
->Format
) != GL_UNSIGNED_NORMALIZED
)
640 ctx
->Driver
.MapRenderbuffer(ctx
, depthRb
, x
, y
, width
, height
,
641 GL_MAP_READ_BIT
, &depthMap
, &depthStride
);
643 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
644 return GL_TRUE
; /* don't bother trying the slow path */
647 ctx
->Driver
.MapRenderbuffer(ctx
, stencilRb
, x
, y
, width
, height
,
648 GL_MAP_READ_BIT
, &stencilMap
, &stencilStride
);
650 ctx
->Driver
.UnmapRenderbuffer(ctx
, depthRb
);
651 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
652 return GL_TRUE
; /* don't bother trying the slow path */
655 stencilVals
= malloc(width
* sizeof(GLubyte
));
658 for (j
= 0; j
< height
; j
++) {
659 _mesa_unpack_uint_z_row(depthRb
->Format
, width
, depthMap
, dst
);
660 _mesa_unpack_ubyte_stencil_row(stencilRb
->Format
, width
,
661 stencilMap
, stencilVals
);
663 for (i
= 0; i
< width
; i
++) {
664 dst
[i
] = (dst
[i
] & 0xffffff00) | stencilVals
[i
];
667 depthMap
+= depthStride
;
668 stencilMap
+= stencilStride
;
669 dst
+= dstStride
/ 4;
673 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
678 ctx
->Driver
.UnmapRenderbuffer(ctx
, depthRb
);
679 ctx
->Driver
.UnmapRenderbuffer(ctx
, stencilRb
);
685 slow_read_depth_stencil_pixels_separate(struct gl_context
*ctx
,
687 GLsizei width
, GLsizei height
,
689 const struct gl_pixelstore_attrib
*packing
,
690 GLubyte
*dst
, int dstStride
)
692 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
693 struct gl_renderbuffer
*depthRb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
694 struct gl_renderbuffer
*stencilRb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
695 GLubyte
*depthMap
, *stencilMap
;
696 int depthStride
, stencilStride
, j
;
697 GLubyte
*stencilVals
;
701 /* The depth and stencil buffers might be separate, or a single buffer.
702 * If one buffer, only map it once.
704 ctx
->Driver
.MapRenderbuffer(ctx
, depthRb
, x
, y
, width
, height
,
705 GL_MAP_READ_BIT
, &depthMap
, &depthStride
);
707 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
711 if (stencilRb
!= depthRb
) {
712 ctx
->Driver
.MapRenderbuffer(ctx
, stencilRb
, x
, y
, width
, height
,
713 GL_MAP_READ_BIT
, &stencilMap
,
716 ctx
->Driver
.UnmapRenderbuffer(ctx
, depthRb
);
717 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
722 stencilMap
= depthMap
;
723 stencilStride
= depthStride
;
726 stencilVals
= malloc(width
* sizeof(GLubyte
));
727 depthVals
= malloc(width
* sizeof(GLfloat
));
729 if (stencilVals
&& depthVals
) {
730 for (j
= 0; j
< height
; j
++) {
731 _mesa_unpack_float_z_row(depthRb
->Format
, width
, depthMap
, depthVals
);
732 _mesa_unpack_ubyte_stencil_row(stencilRb
->Format
, width
,
733 stencilMap
, stencilVals
);
735 _mesa_pack_depth_stencil_span(ctx
, width
, type
, (GLuint
*)dst
,
736 depthVals
, stencilVals
, packing
);
738 depthMap
+= depthStride
;
739 stencilMap
+= stencilStride
;
744 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
750 ctx
->Driver
.UnmapRenderbuffer(ctx
, depthRb
);
751 if (stencilRb
!= depthRb
) {
752 ctx
->Driver
.UnmapRenderbuffer(ctx
, stencilRb
);
758 * Read combined depth/stencil values.
759 * We'll have already done error checking to be sure the expected
760 * depth and stencil buffers really exist.
763 read_depth_stencil_pixels(struct gl_context
*ctx
,
765 GLsizei width
, GLsizei height
,
766 GLenum type
, GLvoid
*pixels
,
767 const struct gl_pixelstore_attrib
*packing
)
769 const GLboolean scaleOrBias
770 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
771 const GLboolean stencilTransfer
= ctx
->Pixel
.IndexShift
772 || ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapStencilFlag
;
776 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
,
778 GL_DEPTH_STENCIL_EXT
,
780 dstStride
= _mesa_image_row_stride(packing
, width
,
781 GL_DEPTH_STENCIL_EXT
, type
);
783 /* Fast 24/8 reads. */
784 if (type
== GL_UNSIGNED_INT_24_8
&&
785 !scaleOrBias
&& !stencilTransfer
&& !packing
->SwapBytes
) {
786 if (fast_read_depth_stencil_pixels(ctx
, x
, y
, width
, height
,
790 if (fast_read_depth_stencil_pixels_separate(ctx
, x
, y
, width
, height
,
791 (uint32_t *)dst
, dstStride
))
795 slow_read_depth_stencil_pixels_separate(ctx
, x
, y
, width
, height
,
803 * Software fallback routine for ctx->Driver.ReadPixels().
804 * By time we get here, all error checking will have been done.
807 _mesa_readpixels(struct gl_context
*ctx
,
808 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
809 GLenum format
, GLenum type
,
810 const struct gl_pixelstore_attrib
*packing
,
813 struct gl_pixelstore_attrib clippedPacking
= *packing
;
816 _mesa_update_state(ctx
);
818 /* Do all needed clipping here, so that we can forget about it later */
819 if (_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
821 pixels
= _mesa_map_pbo_dest(ctx
, &clippedPacking
, pixels
);
824 /* Try memcpy first. */
825 if (readpixels_memcpy(ctx
, x
, y
, width
, height
, format
, type
,
827 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
831 /* Otherwise take the slow path. */
833 case GL_STENCIL_INDEX
:
834 read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
837 case GL_DEPTH_COMPONENT
:
838 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
841 case GL_DEPTH_STENCIL_EXT
:
842 read_depth_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
846 /* all other formats should be color formats */
847 read_rgba_pixels(ctx
, x
, y
, width
, height
, format
, type
, pixels
,
851 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
858 read_pixels_es3_error_check(GLenum format
, GLenum type
,
859 const struct gl_renderbuffer
*rb
)
861 const GLenum internalFormat
= rb
->InternalFormat
;
862 const GLenum data_type
= _mesa_get_format_datatype(rb
->Format
);
863 GLboolean is_unsigned_int
= GL_FALSE
;
864 GLboolean is_signed_int
= GL_FALSE
;
866 if (!_mesa_is_color_format(internalFormat
)) {
867 return GL_INVALID_OPERATION
;
870 is_unsigned_int
= _mesa_is_enum_format_unsigned_int(internalFormat
);
871 if (!is_unsigned_int
) {
872 is_signed_int
= _mesa_is_enum_format_signed_int(internalFormat
);
877 if (type
== GL_FLOAT
&& data_type
== GL_FLOAT
)
878 return GL_NO_ERROR
; /* EXT_color_buffer_float */
879 if (type
== GL_UNSIGNED_BYTE
&& data_type
== GL_UNSIGNED_NORMALIZED
)
881 if (internalFormat
== GL_RGB10_A2
&&
882 type
== GL_UNSIGNED_INT_2_10_10_10_REV
)
884 if (internalFormat
== GL_RGB10_A2UI
&& type
== GL_UNSIGNED_BYTE
)
888 /* GL_EXT_read_format_bgra */
889 if (type
== GL_UNSIGNED_BYTE
||
890 type
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
891 type
== GL_UNSIGNED_SHORT_1_5_5_5_REV
)
894 case GL_RGBA_INTEGER
:
895 if ((is_signed_int
&& type
== GL_INT
) ||
896 (is_unsigned_int
&& type
== GL_UNSIGNED_INT
))
901 return GL_INVALID_OPERATION
;
906 _mesa_ReadnPixelsARB( GLint x
, GLint y
, GLsizei width
, GLsizei height
,
907 GLenum format
, GLenum type
, GLsizei bufSize
,
910 GLenum err
= GL_NO_ERROR
;
911 struct gl_renderbuffer
*rb
;
913 GET_CURRENT_CONTEXT(ctx
);
915 FLUSH_VERTICES(ctx
, 0);
916 FLUSH_CURRENT(ctx
, 0);
918 if (MESA_VERBOSE
& VERBOSE_API
)
919 _mesa_debug(ctx
, "glReadPixels(%d, %d, %s, %s, %p)\n",
921 _mesa_lookup_enum_by_nr(format
),
922 _mesa_lookup_enum_by_nr(type
),
925 if (width
< 0 || height
< 0) {
926 _mesa_error( ctx
, GL_INVALID_VALUE
,
927 "glReadPixels(width=%d height=%d)", width
, height
);
932 _mesa_update_state(ctx
);
934 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
935 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
936 "glReadPixels(incomplete framebuffer)" );
940 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, format
);
942 _mesa_error(ctx
, GL_INVALID_OPERATION
,
943 "glReadPixels(read buffer)");
947 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
948 * combinations of format and type that can be used.
950 * Technically, only two combinations are actually allowed:
951 * GL_RGBA/GL_UNSIGNED_BYTE, and some implementation-specific internal
952 * preferred combination. This code doesn't know what that preferred
953 * combination is, and Mesa can handle anything valid. Just work instead.
955 if (_mesa_is_gles(ctx
)) {
956 if (ctx
->API
== API_OPENGLES2
&&
957 _mesa_is_color_format(format
) &&
958 _mesa_get_color_read_format(ctx
) == format
&&
959 _mesa_get_color_read_type(ctx
) == type
) {
961 } else if (ctx
->Version
< 30) {
962 err
= _mesa_es_error_check_format_and_type(format
, type
, 2);
963 if (err
== GL_NO_ERROR
) {
964 if (type
== GL_FLOAT
|| type
== GL_HALF_FLOAT_OES
) {
965 err
= GL_INVALID_OPERATION
;
969 err
= read_pixels_es3_error_check(format
, type
, rb
);
972 if (err
== GL_NO_ERROR
&& (format
== GL_DEPTH_COMPONENT
973 || format
== GL_DEPTH_STENCIL
)) {
974 err
= GL_INVALID_ENUM
;
977 if (err
!= GL_NO_ERROR
) {
978 _mesa_error(ctx
, err
, "glReadPixels(invalid format %s and/or type %s)",
979 _mesa_lookup_enum_by_nr(format
),
980 _mesa_lookup_enum_by_nr(type
));
985 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
986 if (err
!= GL_NO_ERROR
) {
987 _mesa_error(ctx
, err
, "glReadPixels(invalid format %s and/or type %s)",
988 _mesa_lookup_enum_by_nr(format
),
989 _mesa_lookup_enum_by_nr(type
));
993 if (_mesa_is_user_fbo(ctx
->ReadBuffer
) &&
994 ctx
->ReadBuffer
->Visual
.samples
> 0) {
995 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(multisample FBO)");
999 if (!_mesa_source_buffer_exists(ctx
, format
)) {
1000 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(no readbuffer)");
1004 /* Check that the destination format and source buffer are both
1005 * integer-valued or both non-integer-valued.
1007 if (ctx
->Extensions
.EXT_texture_integer
&& _mesa_is_color_format(format
)) {
1008 const struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
1009 const GLboolean srcInteger
= _mesa_is_format_integer_color(rb
->Format
);
1010 const GLboolean dstInteger
= _mesa_is_enum_format_integer(format
);
1011 if (dstInteger
!= srcInteger
) {
1012 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1013 "glReadPixels(integer / non-integer format mismatch");
1018 if (width
== 0 || height
== 0)
1019 return; /* nothing to do */
1021 if (!_mesa_validate_pbo_access(2, &ctx
->Pack
, width
, height
, 1,
1022 format
, type
, bufSize
, pixels
)) {
1023 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1024 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1025 "glReadPixels(out of bounds PBO access)");
1027 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1028 "glReadnPixelsARB(out of bounds access:"
1029 " bufSize (%d) is too small)", bufSize
);
1034 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) &&
1035 _mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
1036 /* buffer is mapped - that's an error */
1037 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(PBO is mapped)");
1041 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
1042 format
, type
, &ctx
->Pack
, pixels
);
1046 _mesa_ReadPixels( GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1047 GLenum format
, GLenum type
, GLvoid
*pixels
)
1049 _mesa_ReadnPixelsARB(x
, y
, width
, height
, format
, type
, INT_MAX
, pixels
);