2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
28 #include "bufferobj.h"
32 #include "framebuffer.h"
34 #include "format_unpack.h"
40 #include "glformats.h"
42 #include "format_utils.h"
43 #include "pixeltransfer.h"
47 * Return true if the conversion L=R+G+B is needed.
50 _mesa_need_rgb_to_luminance_conversion(GLenum srcBaseFormat
,
53 return (srcBaseFormat
== GL_RG
||
54 srcBaseFormat
== GL_RGB
||
55 srcBaseFormat
== GL_RGBA
) &&
56 (dstBaseFormat
== GL_LUMINANCE
||
57 dstBaseFormat
== GL_LUMINANCE_ALPHA
);
61 * Return true if the conversion L,I to RGB conversion is needed.
64 _mesa_need_luminance_to_rgb_conversion(GLenum srcBaseFormat
,
67 return (srcBaseFormat
== GL_LUMINANCE
||
68 srcBaseFormat
== GL_LUMINANCE_ALPHA
||
69 srcBaseFormat
== GL_INTENSITY
) &&
70 (dstBaseFormat
== GL_GREEN
||
71 dstBaseFormat
== GL_BLUE
||
72 dstBaseFormat
== GL_RG
||
73 dstBaseFormat
== GL_RGB
||
74 dstBaseFormat
== GL_BGR
||
75 dstBaseFormat
== GL_RGBA
||
76 dstBaseFormat
== GL_BGRA
);
80 * Return transfer op flags for this ReadPixels operation.
83 _mesa_get_readpixels_transfer_ops(const struct gl_context
*ctx
,
84 mesa_format texFormat
,
85 GLenum format
, GLenum type
,
88 GLbitfield transferOps
= ctx
->_ImageTransferState
;
89 GLenum srcBaseFormat
= _mesa_get_format_base_format(texFormat
);
90 GLenum dstBaseFormat
= _mesa_unpack_format_to_base_format(format
);
92 if (format
== GL_DEPTH_COMPONENT
||
93 format
== GL_DEPTH_STENCIL
||
94 format
== GL_STENCIL_INDEX
) {
98 /* Pixel transfer ops (scale, bias, table lookup) do not apply
101 if (_mesa_is_enum_format_integer(format
)) {
106 /* For blit-based ReadPixels packing, the clamping is done automatically
107 * unless the type is float. */
108 if (_mesa_get_clamp_read_color(ctx
, ctx
->ReadBuffer
) &&
109 (type
== GL_FLOAT
|| type
== GL_HALF_FLOAT
)) {
110 transferOps
|= IMAGE_CLAMP_BIT
;
114 /* For CPU-based ReadPixels packing, the clamping must always be done
115 * for non-float types, */
116 if (_mesa_get_clamp_read_color(ctx
, ctx
->ReadBuffer
) ||
117 (type
!= GL_FLOAT
&& type
!= GL_HALF_FLOAT
)) {
118 transferOps
|= IMAGE_CLAMP_BIT
;
122 /* If the format is unsigned normalized, we can ignore clamping
123 * because the values are already in the range [0,1] so it won't
124 * have any effect anyway.
126 if (_mesa_get_format_datatype(texFormat
) == GL_UNSIGNED_NORMALIZED
&&
127 !_mesa_need_rgb_to_luminance_conversion(srcBaseFormat
, dstBaseFormat
)) {
128 transferOps
&= ~IMAGE_CLAMP_BIT
;
136 * Return true if memcpy cannot be used for ReadPixels.
138 * If uses_blit is true, the function returns true if a simple 3D engine blit
139 * cannot be used for ReadPixels packing.
141 * NOTE: This doesn't take swizzling and format conversions between
142 * the readbuffer and the pixel pack buffer into account.
145 _mesa_readpixels_needs_slow_path(const struct gl_context
*ctx
, GLenum format
,
146 GLenum type
, GLboolean uses_blit
)
148 struct gl_renderbuffer
*rb
=
149 _mesa_get_read_renderbuffer_for_format(ctx
, format
);
150 GLenum dstBaseFormat
= _mesa_unpack_format_to_base_format(format
);
154 /* There are different rules depending on the base format. */
156 case GL_DEPTH_STENCIL
:
157 return !_mesa_has_depthstencil_combined(ctx
->ReadBuffer
) ||
158 ctx
->Pixel
.DepthScale
!= 1.0f
|| ctx
->Pixel
.DepthBias
!= 0.0f
||
159 ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
||
160 ctx
->Pixel
.MapStencilFlag
;
162 case GL_DEPTH_COMPONENT
:
163 return ctx
->Pixel
.DepthScale
!= 1.0f
|| ctx
->Pixel
.DepthBias
!= 0.0f
;
165 case GL_STENCIL_INDEX
:
166 return ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
||
167 ctx
->Pixel
.MapStencilFlag
;
171 if (_mesa_need_rgb_to_luminance_conversion(rb
->_BaseFormat
,
176 /* And finally, see if there are any transfer ops. */
177 return _mesa_get_readpixels_transfer_ops(ctx
, rb
->Format
, format
, type
,
185 readpixels_can_use_memcpy(const struct gl_context
*ctx
, GLenum format
, GLenum type
,
186 const struct gl_pixelstore_attrib
*packing
)
188 struct gl_renderbuffer
*rb
=
189 _mesa_get_read_renderbuffer_for_format(ctx
, format
);
193 if (_mesa_readpixels_needs_slow_path(ctx
, format
, type
, GL_FALSE
)) {
197 /* The base internal format and the base Mesa format must match. */
198 if (rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
)) {
202 /* The Mesa format must match the input format and type. */
203 if (!_mesa_format_matches_format_and_type(rb
->Format
, format
, type
,
204 packing
->SwapBytes
, NULL
)) {
213 readpixels_memcpy(struct gl_context
*ctx
,
215 GLsizei width
, GLsizei height
,
216 GLenum format
, GLenum type
,
218 const struct gl_pixelstore_attrib
*packing
)
220 struct gl_renderbuffer
*rb
=
221 _mesa_get_read_renderbuffer_for_format(ctx
, format
);
223 int dstStride
, stride
, j
, texelBytes
, bytesPerRow
;
225 /* Fail if memcpy cannot be used. */
226 if (!readpixels_can_use_memcpy(ctx
, format
, type
, packing
)) {
230 dstStride
= _mesa_image_row_stride(packing
, width
, format
, type
);
231 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
234 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
237 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
238 return GL_TRUE
; /* don't bother trying the slow path */
241 texelBytes
= _mesa_get_format_bytes(rb
->Format
);
242 bytesPerRow
= texelBytes
* width
;
245 if (dstStride
== stride
&& dstStride
== bytesPerRow
) {
246 memcpy(dst
, map
, bytesPerRow
* height
);
248 for (j
= 0; j
< height
; j
++) {
249 memcpy(dst
, map
, bytesPerRow
);
255 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
261 * Optimized path for conversion of depth values to GL_DEPTH_COMPONENT,
265 read_uint_depth_pixels( struct gl_context
*ctx
,
267 GLsizei width
, GLsizei height
,
268 GLenum type
, GLvoid
*pixels
,
269 const struct gl_pixelstore_attrib
*packing
)
271 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
272 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
274 int stride
, dstStride
, j
;
276 if (ctx
->Pixel
.DepthScale
!= 1.0F
|| ctx
->Pixel
.DepthBias
!= 0.0F
)
279 if (packing
->SwapBytes
)
282 if (_mesa_get_format_datatype(rb
->Format
) != GL_UNSIGNED_NORMALIZED
)
285 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
289 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
290 return GL_TRUE
; /* don't bother trying the slow path */
293 dstStride
= _mesa_image_row_stride(packing
, width
, GL_DEPTH_COMPONENT
, type
);
294 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
295 GL_DEPTH_COMPONENT
, type
, 0, 0);
297 for (j
= 0; j
< height
; j
++) {
298 _mesa_unpack_uint_z_row(rb
->Format
, width
, map
, (GLuint
*)dst
);
303 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
309 * Read pixels for format=GL_DEPTH_COMPONENT.
312 read_depth_pixels( struct gl_context
*ctx
,
314 GLsizei width
, GLsizei height
,
315 GLenum type
, GLvoid
*pixels
,
316 const struct gl_pixelstore_attrib
*packing
)
318 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
319 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
322 int dstStride
, stride
;
323 GLfloat
*depthValues
;
328 /* clipping should have been done already */
331 assert(x
+ width
<= (GLint
) rb
->Width
);
332 assert(y
+ height
<= (GLint
) rb
->Height
);
334 if (type
== GL_UNSIGNED_INT
&&
335 read_uint_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
, packing
)) {
339 dstStride
= _mesa_image_row_stride(packing
, width
, GL_DEPTH_COMPONENT
, type
);
340 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
341 GL_DEPTH_COMPONENT
, type
, 0, 0);
343 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
346 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
350 depthValues
= malloc(width
* sizeof(GLfloat
));
353 /* General case (slower) */
354 for (j
= 0; j
< height
; j
++, y
++) {
355 _mesa_unpack_float_z_row(rb
->Format
, width
, map
, depthValues
);
356 _mesa_pack_depth_span(ctx
, width
, dst
, type
, depthValues
, packing
);
363 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
368 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
373 * Read pixels for format=GL_STENCIL_INDEX.
376 read_stencil_pixels( struct gl_context
*ctx
,
378 GLsizei width
, GLsizei height
,
379 GLenum type
, GLvoid
*pixels
,
380 const struct gl_pixelstore_attrib
*packing
)
382 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
383 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
385 GLubyte
*map
, *stencil
;
391 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
394 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
398 stencil
= malloc(width
* sizeof(GLubyte
));
401 /* process image row by row */
402 for (j
= 0; j
< height
; j
++) {
405 _mesa_unpack_ubyte_stencil_row(rb
->Format
, width
, map
, stencil
);
406 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
407 GL_STENCIL_INDEX
, type
, j
, 0);
409 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, stencil
, packing
);
415 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
420 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
424 * Read R, G, B, A, RGB, L, or LA pixels.
427 read_rgba_pixels( struct gl_context
*ctx
,
429 GLsizei width
, GLsizei height
,
430 GLenum format
, GLenum type
, GLvoid
*pixels
,
431 const struct gl_pixelstore_attrib
*packing
)
433 GLbitfield transferOps
;
434 bool dst_is_integer
, convert_rgb_to_lum
, needs_rebase
;
435 int dst_stride
, src_stride
, rb_stride
;
436 uint32_t dst_format
, src_format
;
438 mesa_format rb_format
;
441 bool src_is_uint
= false;
442 uint8_t rebase_swizzle
[4];
443 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
444 struct gl_renderbuffer
*rb
= fb
->_ColorReadBuffer
;
445 GLenum dstBaseFormat
= _mesa_unpack_format_to_base_format(format
);
450 transferOps
= _mesa_get_readpixels_transfer_ops(ctx
, rb
->Format
, format
,
452 /* Describe the dst format */
453 dst_is_integer
= _mesa_is_enum_format_integer(format
);
454 dst_stride
= _mesa_image_row_stride(packing
, width
, format
, type
);
455 dst_format
= _mesa_format_from_format_and_type(format
, type
);
457 _mesa_need_rgb_to_luminance_conversion(rb
->_BaseFormat
, dstBaseFormat
);
458 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
461 /* Map the source render buffer */
462 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
465 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
468 rb_format
= _mesa_get_srgb_format_linear(rb
->Format
);
471 * Depending on the base formats involved in the conversion we might need to
472 * rebase some values, so for these formats we compute a rebase swizzle.
474 if (rb
->_BaseFormat
== GL_LUMINANCE
|| rb
->_BaseFormat
== GL_INTENSITY
) {
476 rebase_swizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
477 rebase_swizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
478 rebase_swizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
479 rebase_swizzle
[3] = MESA_FORMAT_SWIZZLE_ONE
;
480 } else if (rb
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
482 rebase_swizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
483 rebase_swizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
484 rebase_swizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
485 rebase_swizzle
[3] = MESA_FORMAT_SWIZZLE_W
;
486 } else if (_mesa_get_format_base_format(rb_format
) != rb
->_BaseFormat
) {
488 _mesa_compute_rgba2base2rgba_component_mapping(rb
->_BaseFormat
,
491 needs_rebase
= false;
494 /* Since _mesa_format_convert does not handle transferOps we need to handle
495 * them before we call the function. This requires to convert to RGBA float
496 * first so we can call _mesa_apply_rgba_transfer_ops. If the dst format is
497 * integer transferOps do not apply.
499 * Converting to luminance also requires converting to RGBA first, so we can
500 * then compute luminance values as L=R+G+B. Notice that this is different
501 * from GetTexImage, where we compute L=R.
503 assert(!transferOps
|| (transferOps
&& !dst_is_integer
));
505 needs_rgba
= transferOps
|| convert_rgb_to_lum
;
508 uint32_t rgba_format
;
512 /* Convert to RGBA float or int/uint depending on the type of the src */
513 if (dst_is_integer
) {
514 src_is_uint
= _mesa_is_format_unsigned(rb_format
);
516 rgba_format
= RGBA32_UINT
;
517 rgba_stride
= width
* 4 * sizeof(GLuint
);
519 rgba_format
= RGBA32_INT
;
520 rgba_stride
= width
* 4 * sizeof(GLint
);
523 rgba_format
= RGBA32_FLOAT
;
524 rgba_stride
= width
* 4 * sizeof(GLfloat
);
527 /* If we are lucky and the dst format matches the RGBA format we need to
528 * convert to, then we can convert directly into the dst buffer and avoid
529 * the final conversion/copy from the rgba buffer to the dst buffer.
531 if (dst_format
== rgba_format
&&
532 dst_stride
== rgba_stride
) {
533 need_convert
= false;
537 rgba
= malloc(height
* rgba_stride
);
539 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
544 /* Convert to RGBA now */
545 _mesa_format_convert(rgba
, rgba_format
, rgba_stride
,
546 map
, rb_format
, rb_stride
,
548 needs_rebase
? rebase_swizzle
: NULL
);
550 /* Handle transfer ops if necessary */
552 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, width
* height
, rgba
);
554 /* If we had to rebase, we have already taken care of that */
555 needs_rebase
= false;
557 /* If we were lucky and our RGBA conversion matches the dst format, then
563 /* Otherwise, we need to convert from RGBA to dst next */
565 src_format
= rgba_format
;
566 src_stride
= rgba_stride
;
568 /* No RGBA conversion needed, convert directly to dst */
570 src_format
= rb_format
;
571 src_stride
= rb_stride
;
574 /* Do the conversion.
576 * If the dst format is Luminance, we need to do the conversion by computing
579 if (!convert_rgb_to_lum
) {
580 _mesa_format_convert(dst
, dst_format
, dst_stride
,
581 src
, src_format
, src_stride
,
583 needs_rebase
? rebase_swizzle
: NULL
);
584 } else if (!dst_is_integer
) {
585 /* Compute float Luminance values from RGBA float */
586 int luminance_stride
, luminance_bytes
;
588 uint32_t luminance_format
;
590 luminance_stride
= width
* sizeof(GLfloat
);
591 if (format
== GL_LUMINANCE_ALPHA
)
592 luminance_stride
*= 2;
593 luminance_bytes
= height
* luminance_stride
;
594 luminance
= malloc(luminance_bytes
);
596 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
600 _mesa_pack_luminance_from_rgba_float(width
* height
, src
,
601 luminance
, format
, transferOps
);
603 /* Convert from Luminance float to dst (this will hadle type conversion
604 * from float to the type of dst if necessary)
606 luminance_format
= _mesa_format_from_format_and_type(format
, GL_FLOAT
);
607 _mesa_format_convert(dst
, dst_format
, dst_stride
,
608 luminance
, luminance_format
, luminance_stride
,
609 width
, height
, NULL
);
612 _mesa_pack_luminance_from_rgba_integer(width
* height
, src
, !src_is_uint
,
619 /* Handle byte swapping if required */
620 if (packing
->SwapBytes
) {
621 _mesa_swap_bytes_2d_image(format
, type
, packing
,
622 width
, height
, dst
, dst
);
626 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
630 * For a packed depth/stencil buffer being read as depth/stencil, just memcpy the
631 * data (possibly swapping 8/24 vs 24/8 as we go).
634 fast_read_depth_stencil_pixels(struct gl_context
*ctx
,
636 GLsizei width
, GLsizei height
,
637 GLubyte
*dst
, int dstStride
)
639 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
640 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
641 struct gl_renderbuffer
*stencilRb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
648 if (rb
->Format
!= MESA_FORMAT_S8_UINT_Z24_UNORM
&&
649 rb
->Format
!= MESA_FORMAT_Z24_UNORM_S8_UINT
)
652 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
655 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
656 return GL_TRUE
; /* don't bother trying the slow path */
659 for (i
= 0; i
< height
; i
++) {
660 _mesa_unpack_uint_24_8_depth_stencil_row(rb
->Format
, width
,
666 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
673 * For non-float-depth and stencil buffers being read as 24/8 depth/stencil,
674 * copy the integer data directly instead of converting depth to float and
678 fast_read_depth_stencil_pixels_separate(struct gl_context
*ctx
,
680 GLsizei width
, GLsizei height
,
681 uint32_t *dst
, int dstStride
)
683 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
684 struct gl_renderbuffer
*depthRb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
685 struct gl_renderbuffer
*stencilRb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
686 GLubyte
*depthMap
, *stencilMap
, *stencilVals
;
687 int depthStride
, stencilStride
, i
, j
;
689 if (_mesa_get_format_datatype(depthRb
->Format
) != GL_UNSIGNED_NORMALIZED
)
692 ctx
->Driver
.MapRenderbuffer(ctx
, depthRb
, x
, y
, width
, height
,
693 GL_MAP_READ_BIT
, &depthMap
, &depthStride
);
695 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
696 return GL_TRUE
; /* don't bother trying the slow path */
699 ctx
->Driver
.MapRenderbuffer(ctx
, stencilRb
, x
, y
, width
, height
,
700 GL_MAP_READ_BIT
, &stencilMap
, &stencilStride
);
702 ctx
->Driver
.UnmapRenderbuffer(ctx
, depthRb
);
703 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
704 return GL_TRUE
; /* don't bother trying the slow path */
707 stencilVals
= malloc(width
* sizeof(GLubyte
));
710 for (j
= 0; j
< height
; j
++) {
711 _mesa_unpack_uint_z_row(depthRb
->Format
, width
, depthMap
, dst
);
712 _mesa_unpack_ubyte_stencil_row(stencilRb
->Format
, width
,
713 stencilMap
, stencilVals
);
715 for (i
= 0; i
< width
; i
++) {
716 dst
[i
] = (dst
[i
] & 0xffffff00) | stencilVals
[i
];
719 depthMap
+= depthStride
;
720 stencilMap
+= stencilStride
;
721 dst
+= dstStride
/ 4;
725 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
730 ctx
->Driver
.UnmapRenderbuffer(ctx
, depthRb
);
731 ctx
->Driver
.UnmapRenderbuffer(ctx
, stencilRb
);
737 slow_read_depth_stencil_pixels_separate(struct gl_context
*ctx
,
739 GLsizei width
, GLsizei height
,
741 const struct gl_pixelstore_attrib
*packing
,
742 GLubyte
*dst
, int dstStride
)
744 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
745 struct gl_renderbuffer
*depthRb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
746 struct gl_renderbuffer
*stencilRb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
747 GLubyte
*depthMap
, *stencilMap
;
748 int depthStride
, stencilStride
, j
;
749 GLubyte
*stencilVals
;
753 /* The depth and stencil buffers might be separate, or a single buffer.
754 * If one buffer, only map it once.
756 ctx
->Driver
.MapRenderbuffer(ctx
, depthRb
, x
, y
, width
, height
,
757 GL_MAP_READ_BIT
, &depthMap
, &depthStride
);
759 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
763 if (stencilRb
!= depthRb
) {
764 ctx
->Driver
.MapRenderbuffer(ctx
, stencilRb
, x
, y
, width
, height
,
765 GL_MAP_READ_BIT
, &stencilMap
,
768 ctx
->Driver
.UnmapRenderbuffer(ctx
, depthRb
);
769 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
774 stencilMap
= depthMap
;
775 stencilStride
= depthStride
;
778 stencilVals
= malloc(width
* sizeof(GLubyte
));
779 depthVals
= malloc(width
* sizeof(GLfloat
));
781 if (stencilVals
&& depthVals
) {
782 for (j
= 0; j
< height
; j
++) {
783 _mesa_unpack_float_z_row(depthRb
->Format
, width
, depthMap
, depthVals
);
784 _mesa_unpack_ubyte_stencil_row(stencilRb
->Format
, width
,
785 stencilMap
, stencilVals
);
787 _mesa_pack_depth_stencil_span(ctx
, width
, type
, (GLuint
*)dst
,
788 depthVals
, stencilVals
, packing
);
790 depthMap
+= depthStride
;
791 stencilMap
+= stencilStride
;
796 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
802 ctx
->Driver
.UnmapRenderbuffer(ctx
, depthRb
);
803 if (stencilRb
!= depthRb
) {
804 ctx
->Driver
.UnmapRenderbuffer(ctx
, stencilRb
);
810 * Read combined depth/stencil values.
811 * We'll have already done error checking to be sure the expected
812 * depth and stencil buffers really exist.
815 read_depth_stencil_pixels(struct gl_context
*ctx
,
817 GLsizei width
, GLsizei height
,
818 GLenum type
, GLvoid
*pixels
,
819 const struct gl_pixelstore_attrib
*packing
)
821 const GLboolean scaleOrBias
822 = ctx
->Pixel
.DepthScale
!= 1.0F
|| ctx
->Pixel
.DepthBias
!= 0.0F
;
823 const GLboolean stencilTransfer
= ctx
->Pixel
.IndexShift
824 || ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapStencilFlag
;
828 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
,
830 GL_DEPTH_STENCIL_EXT
,
832 dstStride
= _mesa_image_row_stride(packing
, width
,
833 GL_DEPTH_STENCIL_EXT
, type
);
835 /* Fast 24/8 reads. */
836 if (type
== GL_UNSIGNED_INT_24_8
&&
837 !scaleOrBias
&& !stencilTransfer
&& !packing
->SwapBytes
) {
838 if (fast_read_depth_stencil_pixels(ctx
, x
, y
, width
, height
,
842 if (fast_read_depth_stencil_pixels_separate(ctx
, x
, y
, width
, height
,
843 (uint32_t *)dst
, dstStride
))
847 slow_read_depth_stencil_pixels_separate(ctx
, x
, y
, width
, height
,
855 * Software fallback routine for ctx->Driver.ReadPixels().
856 * By time we get here, all error checking will have been done.
859 _mesa_readpixels(struct gl_context
*ctx
,
860 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
861 GLenum format
, GLenum type
,
862 const struct gl_pixelstore_attrib
*packing
,
866 _mesa_update_state(ctx
);
868 pixels
= _mesa_map_pbo_dest(ctx
, packing
, pixels
);
871 /* Try memcpy first. */
872 if (readpixels_memcpy(ctx
, x
, y
, width
, height
, format
, type
,
874 _mesa_unmap_pbo_dest(ctx
, packing
);
878 /* Otherwise take the slow path. */
880 case GL_STENCIL_INDEX
:
881 read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
884 case GL_DEPTH_COMPONENT
:
885 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
888 case GL_DEPTH_STENCIL_EXT
:
889 read_depth_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
893 /* all other formats should be color formats */
894 read_rgba_pixels(ctx
, x
, y
, width
, height
, format
, type
, pixels
,
898 _mesa_unmap_pbo_dest(ctx
, packing
);
904 read_pixels_es3_error_check(GLenum format
, GLenum type
,
905 const struct gl_renderbuffer
*rb
)
907 const GLenum internalFormat
= rb
->InternalFormat
;
908 const GLenum data_type
= _mesa_get_format_datatype(rb
->Format
);
909 GLboolean is_unsigned_int
= GL_FALSE
;
910 GLboolean is_signed_int
= GL_FALSE
;
911 GLboolean is_float_depth
= (internalFormat
== GL_DEPTH_COMPONENT32F
) ||
912 (internalFormat
== GL_DEPTH32F_STENCIL8
);
914 is_unsigned_int
= _mesa_is_enum_format_unsigned_int(internalFormat
);
915 if (!is_unsigned_int
) {
916 is_signed_int
= _mesa_is_enum_format_signed_int(internalFormat
);
921 if (type
== GL_FLOAT
&& data_type
== GL_FLOAT
)
922 return GL_NO_ERROR
; /* EXT_color_buffer_float */
923 if (type
== GL_UNSIGNED_BYTE
&& data_type
== GL_UNSIGNED_NORMALIZED
)
925 if (internalFormat
== GL_RGB10_A2
&&
926 type
== GL_UNSIGNED_INT_2_10_10_10_REV
)
928 if (internalFormat
== GL_RGB10_A2UI
&& type
== GL_UNSIGNED_BYTE
)
932 /* GL_EXT_read_format_bgra */
933 if (type
== GL_UNSIGNED_BYTE
||
934 type
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
935 type
== GL_UNSIGNED_SHORT_1_5_5_5_REV
)
938 case GL_RGBA_INTEGER
:
939 if ((is_signed_int
&& type
== GL_INT
) ||
940 (is_unsigned_int
&& type
== GL_UNSIGNED_INT
))
943 case GL_DEPTH_STENCIL
:
945 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
949 case GL_UNSIGNED_INT_24_8
:
954 return GL_INVALID_ENUM
;
957 case GL_DEPTH_COMPONENT
:
963 case GL_UNSIGNED_SHORT
:
964 case GL_UNSIGNED_INT
:
965 case GL_UNSIGNED_INT_24_8
:
970 return GL_INVALID_ENUM
;
973 case GL_STENCIL_INDEX
:
975 case GL_UNSIGNED_BYTE
:
978 return GL_INVALID_ENUM
;
983 return GL_INVALID_OPERATION
;
987 static ALWAYS_INLINE
void
988 read_pixels(GLint x
, GLint y
, GLsizei width
, GLsizei height
, GLenum format
,
989 GLenum type
, GLsizei bufSize
, GLvoid
*pixels
, bool no_error
)
991 GLenum err
= GL_NO_ERROR
;
992 struct gl_renderbuffer
*rb
;
993 struct gl_pixelstore_attrib clippedPacking
;
995 GET_CURRENT_CONTEXT(ctx
);
997 FLUSH_VERTICES(ctx
, 0);
998 FLUSH_CURRENT(ctx
, 0);
1000 if (MESA_VERBOSE
& VERBOSE_API
)
1001 _mesa_debug(ctx
, "glReadPixels(%d, %d, %s, %s, %p)\n",
1003 _mesa_enum_to_string(format
),
1004 _mesa_enum_to_string(type
),
1007 if (!no_error
&& (width
< 0 || height
< 0)) {
1008 _mesa_error( ctx
, GL_INVALID_VALUE
,
1009 "glReadPixels(width=%d height=%d)", width
, height
);
1014 _mesa_update_state(ctx
);
1016 if (!no_error
&& ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1017 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
1018 "glReadPixels(incomplete framebuffer)" );
1022 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, format
);
1025 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1026 "glReadPixels(read buffer)");
1030 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1031 * combinations of format and type that can be used.
1033 * Technically, only two combinations are actually allowed:
1034 * GL_RGBA/GL_UNSIGNED_BYTE, and some implementation-specific internal
1035 * preferred combination. This code doesn't know what that preferred
1036 * combination is, and Mesa can handle anything valid. Just work instead.
1038 if (_mesa_is_gles(ctx
)) {
1039 if (ctx
->API
== API_OPENGLES2
&&
1040 _mesa_is_color_format(format
) &&
1041 _mesa_get_color_read_format(ctx
, NULL
, "glReadPixels") == format
&&
1042 _mesa_get_color_read_type(ctx
, NULL
, "glReadPixels") == type
) {
1044 } else if (ctx
->Version
< 30) {
1045 err
= _mesa_es_error_check_format_and_type(ctx
, format
, type
, 2);
1046 if (err
== GL_NO_ERROR
) {
1047 if (type
== GL_FLOAT
|| type
== GL_HALF_FLOAT_OES
) {
1048 err
= GL_INVALID_OPERATION
;
1052 err
= read_pixels_es3_error_check(format
, type
, rb
);
1055 if (err
!= GL_NO_ERROR
) {
1056 _mesa_error(ctx
, err
, "glReadPixels(invalid format %s and/or type %s)",
1057 _mesa_enum_to_string(format
),
1058 _mesa_enum_to_string(type
));
1063 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1064 if (err
!= GL_NO_ERROR
) {
1065 _mesa_error(ctx
, err
, "glReadPixels(invalid format %s and/or type %s)",
1066 _mesa_enum_to_string(format
),
1067 _mesa_enum_to_string(type
));
1071 if (_mesa_is_user_fbo(ctx
->ReadBuffer
) &&
1072 ctx
->ReadBuffer
->Visual
.samples
> 0) {
1073 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(multisample FBO)");
1077 if (!_mesa_source_buffer_exists(ctx
, format
)) {
1078 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(no readbuffer)");
1082 /* Check that the destination format and source buffer are both
1083 * integer-valued or both non-integer-valued.
1085 if (ctx
->Extensions
.EXT_texture_integer
&& _mesa_is_color_format(format
)) {
1086 const struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
1087 const GLboolean srcInteger
= _mesa_is_format_integer_color(rb
->Format
);
1088 const GLboolean dstInteger
= _mesa_is_enum_format_integer(format
);
1089 if (dstInteger
!= srcInteger
) {
1090 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1091 "glReadPixels(integer / non-integer format mismatch");
1097 /* Do all needed clipping here, so that we can forget about it later */
1098 clippedPacking
= ctx
->Pack
;
1099 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
))
1100 return; /* nothing to do */
1103 if (!_mesa_validate_pbo_access(2, &ctx
->Pack
, width
, height
, 1,
1104 format
, type
, bufSize
, pixels
)) {
1105 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1106 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1107 "glReadPixels(out of bounds PBO access)");
1109 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1110 "glReadnPixelsARB(out of bounds access:"
1111 " bufSize (%d) is too small)", bufSize
);
1116 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) &&
1117 _mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
1118 /* buffer is mapped - that's an error */
1119 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(PBO is mapped)");
1124 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
1125 format
, type
, &clippedPacking
, pixels
);
1129 _mesa_ReadnPixelsARB_no_error(GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1130 GLenum format
, GLenum type
, GLsizei bufSize
,
1133 read_pixels(x
, y
, width
, height
, format
, type
, bufSize
, pixels
, true);
1137 _mesa_ReadnPixelsARB(GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1138 GLenum format
, GLenum type
, GLsizei bufSize
,
1141 read_pixels(x
, y
, width
, height
, format
, type
, bufSize
, pixels
, false);
1145 _mesa_ReadPixels_no_error(GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1146 GLenum format
, GLenum type
, GLvoid
*pixels
)
1148 _mesa_ReadnPixelsARB_no_error(x
, y
, width
, height
, format
, type
, INT_MAX
,
1153 _mesa_ReadPixels(GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1154 GLenum format
, GLenum type
, GLvoid
*pixels
)
1156 _mesa_ReadnPixelsARB(x
, y
, width
, height
, format
, type
, INT_MAX
, pixels
);