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
;
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
);
244 for (j
= 0; j
< height
; j
++) {
245 memcpy(dst
, map
, width
* texelBytes
);
250 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
256 * Optimized path for conversion of depth values to GL_DEPTH_COMPONENT,
260 read_uint_depth_pixels( struct gl_context
*ctx
,
262 GLsizei width
, GLsizei height
,
263 GLenum type
, GLvoid
*pixels
,
264 const struct gl_pixelstore_attrib
*packing
)
266 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
267 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
269 int stride
, dstStride
, j
;
271 if (ctx
->Pixel
.DepthScale
!= 1.0F
|| ctx
->Pixel
.DepthBias
!= 0.0F
)
274 if (packing
->SwapBytes
)
277 if (_mesa_get_format_datatype(rb
->Format
) != GL_UNSIGNED_NORMALIZED
)
280 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
284 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
285 return GL_TRUE
; /* don't bother trying the slow path */
288 dstStride
= _mesa_image_row_stride(packing
, width
, GL_DEPTH_COMPONENT
, type
);
289 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
290 GL_DEPTH_COMPONENT
, type
, 0, 0);
292 for (j
= 0; j
< height
; j
++) {
293 _mesa_unpack_uint_z_row(rb
->Format
, width
, map
, (GLuint
*)dst
);
298 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
304 * Read pixels for format=GL_DEPTH_COMPONENT.
307 read_depth_pixels( struct gl_context
*ctx
,
309 GLsizei width
, GLsizei height
,
310 GLenum type
, GLvoid
*pixels
,
311 const struct gl_pixelstore_attrib
*packing
)
313 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
314 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
317 int dstStride
, stride
;
318 GLfloat
*depthValues
;
323 /* clipping should have been done already */
326 assert(x
+ width
<= (GLint
) rb
->Width
);
327 assert(y
+ height
<= (GLint
) rb
->Height
);
329 if (type
== GL_UNSIGNED_INT
&&
330 read_uint_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
, packing
)) {
334 dstStride
= _mesa_image_row_stride(packing
, width
, GL_DEPTH_COMPONENT
, type
);
335 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
336 GL_DEPTH_COMPONENT
, type
, 0, 0);
338 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
341 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
345 depthValues
= malloc(width
* sizeof(GLfloat
));
348 /* General case (slower) */
349 for (j
= 0; j
< height
; j
++, y
++) {
350 _mesa_unpack_float_z_row(rb
->Format
, width
, map
, depthValues
);
351 _mesa_pack_depth_span(ctx
, width
, dst
, type
, depthValues
, packing
);
358 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
363 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
368 * Read pixels for format=GL_STENCIL_INDEX.
371 read_stencil_pixels( struct gl_context
*ctx
,
373 GLsizei width
, GLsizei height
,
374 GLenum type
, GLvoid
*pixels
,
375 const struct gl_pixelstore_attrib
*packing
)
377 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
378 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
380 GLubyte
*map
, *stencil
;
386 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
389 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
393 stencil
= malloc(width
* sizeof(GLubyte
));
396 /* process image row by row */
397 for (j
= 0; j
< height
; j
++) {
400 _mesa_unpack_ubyte_stencil_row(rb
->Format
, width
, map
, stencil
);
401 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
402 GL_STENCIL_INDEX
, type
, j
, 0);
404 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, stencil
, packing
);
410 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
415 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
419 * Read R, G, B, A, RGB, L, or LA pixels.
422 read_rgba_pixels( struct gl_context
*ctx
,
424 GLsizei width
, GLsizei height
,
425 GLenum format
, GLenum type
, GLvoid
*pixels
,
426 const struct gl_pixelstore_attrib
*packing
)
428 GLbitfield transferOps
;
429 bool dst_is_integer
, convert_rgb_to_lum
, needs_rebase
;
430 int dst_stride
, src_stride
, rb_stride
;
431 uint32_t dst_format
, src_format
;
433 mesa_format rb_format
;
436 bool src_is_uint
= false;
437 uint8_t rebase_swizzle
[4];
438 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
439 struct gl_renderbuffer
*rb
= fb
->_ColorReadBuffer
;
440 GLenum dstBaseFormat
= _mesa_unpack_format_to_base_format(format
);
445 transferOps
= _mesa_get_readpixels_transfer_ops(ctx
, rb
->Format
, format
,
447 /* Describe the dst format */
448 dst_is_integer
= _mesa_is_enum_format_integer(format
);
449 dst_stride
= _mesa_image_row_stride(packing
, width
, format
, type
);
450 dst_format
= _mesa_format_from_format_and_type(format
, type
);
452 _mesa_need_rgb_to_luminance_conversion(rb
->_BaseFormat
, dstBaseFormat
);
453 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
456 /* Map the source render buffer */
457 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
460 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
463 rb_format
= _mesa_get_srgb_format_linear(rb
->Format
);
466 * Depending on the base formats involved in the conversion we might need to
467 * rebase some values, so for these formats we compute a rebase swizzle.
469 if (rb
->_BaseFormat
== GL_LUMINANCE
|| rb
->_BaseFormat
== GL_INTENSITY
) {
471 rebase_swizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
472 rebase_swizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
473 rebase_swizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
474 rebase_swizzle
[3] = MESA_FORMAT_SWIZZLE_ONE
;
475 } else if (rb
->_BaseFormat
== GL_LUMINANCE_ALPHA
) {
477 rebase_swizzle
[0] = MESA_FORMAT_SWIZZLE_X
;
478 rebase_swizzle
[1] = MESA_FORMAT_SWIZZLE_ZERO
;
479 rebase_swizzle
[2] = MESA_FORMAT_SWIZZLE_ZERO
;
480 rebase_swizzle
[3] = MESA_FORMAT_SWIZZLE_W
;
481 } else if (_mesa_get_format_base_format(rb_format
) != rb
->_BaseFormat
) {
483 _mesa_compute_rgba2base2rgba_component_mapping(rb
->_BaseFormat
,
486 needs_rebase
= false;
489 /* Since _mesa_format_convert does not handle transferOps we need to handle
490 * them before we call the function. This requires to convert to RGBA float
491 * first so we can call _mesa_apply_rgba_transfer_ops. If the dst format is
492 * integer transferOps do not apply.
494 * Converting to luminance also requires converting to RGBA first, so we can
495 * then compute luminance values as L=R+G+B. Notice that this is different
496 * from GetTexImage, where we compute L=R.
498 assert(!transferOps
|| (transferOps
&& !dst_is_integer
));
500 needs_rgba
= transferOps
|| convert_rgb_to_lum
;
503 uint32_t rgba_format
;
507 /* Convert to RGBA float or int/uint depending on the type of the src */
508 if (dst_is_integer
) {
509 src_is_uint
= _mesa_is_format_unsigned(rb_format
);
511 rgba_format
= RGBA32_UINT
;
512 rgba_stride
= width
* 4 * sizeof(GLuint
);
514 rgba_format
= RGBA32_INT
;
515 rgba_stride
= width
* 4 * sizeof(GLint
);
518 rgba_format
= RGBA32_FLOAT
;
519 rgba_stride
= width
* 4 * sizeof(GLfloat
);
522 /* If we are lucky and the dst format matches the RGBA format we need to
523 * convert to, then we can convert directly into the dst buffer and avoid
524 * the final conversion/copy from the rgba buffer to the dst buffer.
526 if (dst_format
== rgba_format
&&
527 dst_stride
== rgba_stride
) {
528 need_convert
= false;
532 rgba
= malloc(height
* rgba_stride
);
534 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
539 /* Convert to RGBA now */
540 _mesa_format_convert(rgba
, rgba_format
, rgba_stride
,
541 map
, rb_format
, rb_stride
,
543 needs_rebase
? rebase_swizzle
: NULL
);
545 /* Handle transfer ops if necessary */
547 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, width
* height
, rgba
);
549 /* If we had to rebase, we have already taken care of that */
550 needs_rebase
= false;
552 /* If we were lucky and our RGBA conversion matches the dst format, then
558 /* Otherwise, we need to convert from RGBA to dst next */
560 src_format
= rgba_format
;
561 src_stride
= rgba_stride
;
563 /* No RGBA conversion needed, convert directly to dst */
565 src_format
= rb_format
;
566 src_stride
= rb_stride
;
569 /* Do the conversion.
571 * If the dst format is Luminance, we need to do the conversion by computing
574 if (!convert_rgb_to_lum
) {
575 _mesa_format_convert(dst
, dst_format
, dst_stride
,
576 src
, src_format
, src_stride
,
578 needs_rebase
? rebase_swizzle
: NULL
);
579 } else if (!dst_is_integer
) {
580 /* Compute float Luminance values from RGBA float */
581 int luminance_stride
, luminance_bytes
;
583 uint32_t luminance_format
;
585 luminance_stride
= width
* sizeof(GL_FLOAT
);
586 if (format
== GL_LUMINANCE_ALPHA
)
587 luminance_stride
*= 2;
588 luminance_bytes
= height
* luminance_stride
;
589 luminance
= malloc(luminance_bytes
);
591 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
595 _mesa_pack_luminance_from_rgba_float(width
* height
, src
,
596 luminance
, format
, transferOps
);
598 /* Convert from Luminance float to dst (this will hadle type conversion
599 * from float to the type of dst if necessary)
601 luminance_format
= _mesa_format_from_format_and_type(format
, GL_FLOAT
);
602 _mesa_format_convert(dst
, dst_format
, dst_stride
,
603 luminance
, luminance_format
, luminance_stride
,
604 width
, height
, NULL
);
607 _mesa_pack_luminance_from_rgba_integer(width
* height
, src
, !src_is_uint
,
615 /* Handle byte swapping if required */
616 if (packing
->SwapBytes
) {
617 _mesa_swap_bytes_2d_image(format
, type
, packing
,
618 width
, height
, dst
, dst
);
622 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
626 * For a packed depth/stencil buffer being read as depth/stencil, just memcpy the
627 * data (possibly swapping 8/24 vs 24/8 as we go).
630 fast_read_depth_stencil_pixels(struct gl_context
*ctx
,
632 GLsizei width
, GLsizei height
,
633 GLubyte
*dst
, int dstStride
)
635 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
636 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
637 struct gl_renderbuffer
*stencilRb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
644 if (rb
->Format
!= MESA_FORMAT_S8_UINT_Z24_UNORM
&&
645 rb
->Format
!= MESA_FORMAT_Z24_UNORM_S8_UINT
)
648 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
651 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
652 return GL_TRUE
; /* don't bother trying the slow path */
655 for (i
= 0; i
< height
; i
++) {
656 _mesa_unpack_uint_24_8_depth_stencil_row(rb
->Format
, width
,
662 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
669 * For non-float-depth and stencil buffers being read as 24/8 depth/stencil,
670 * copy the integer data directly instead of converting depth to float and
674 fast_read_depth_stencil_pixels_separate(struct gl_context
*ctx
,
676 GLsizei width
, GLsizei height
,
677 uint32_t *dst
, int dstStride
)
679 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
680 struct gl_renderbuffer
*depthRb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
681 struct gl_renderbuffer
*stencilRb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
682 GLubyte
*depthMap
, *stencilMap
, *stencilVals
;
683 int depthStride
, stencilStride
, i
, j
;
685 if (_mesa_get_format_datatype(depthRb
->Format
) != GL_UNSIGNED_NORMALIZED
)
688 ctx
->Driver
.MapRenderbuffer(ctx
, depthRb
, x
, y
, width
, height
,
689 GL_MAP_READ_BIT
, &depthMap
, &depthStride
);
691 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
692 return GL_TRUE
; /* don't bother trying the slow path */
695 ctx
->Driver
.MapRenderbuffer(ctx
, stencilRb
, x
, y
, width
, height
,
696 GL_MAP_READ_BIT
, &stencilMap
, &stencilStride
);
698 ctx
->Driver
.UnmapRenderbuffer(ctx
, depthRb
);
699 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
700 return GL_TRUE
; /* don't bother trying the slow path */
703 stencilVals
= malloc(width
* sizeof(GLubyte
));
706 for (j
= 0; j
< height
; j
++) {
707 _mesa_unpack_uint_z_row(depthRb
->Format
, width
, depthMap
, dst
);
708 _mesa_unpack_ubyte_stencil_row(stencilRb
->Format
, width
,
709 stencilMap
, stencilVals
);
711 for (i
= 0; i
< width
; i
++) {
712 dst
[i
] = (dst
[i
] & 0xffffff00) | stencilVals
[i
];
715 depthMap
+= depthStride
;
716 stencilMap
+= stencilStride
;
717 dst
+= dstStride
/ 4;
721 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
726 ctx
->Driver
.UnmapRenderbuffer(ctx
, depthRb
);
727 ctx
->Driver
.UnmapRenderbuffer(ctx
, stencilRb
);
733 slow_read_depth_stencil_pixels_separate(struct gl_context
*ctx
,
735 GLsizei width
, GLsizei height
,
737 const struct gl_pixelstore_attrib
*packing
,
738 GLubyte
*dst
, int dstStride
)
740 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
741 struct gl_renderbuffer
*depthRb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
742 struct gl_renderbuffer
*stencilRb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
743 GLubyte
*depthMap
, *stencilMap
;
744 int depthStride
, stencilStride
, j
;
745 GLubyte
*stencilVals
;
749 /* The depth and stencil buffers might be separate, or a single buffer.
750 * If one buffer, only map it once.
752 ctx
->Driver
.MapRenderbuffer(ctx
, depthRb
, x
, y
, width
, height
,
753 GL_MAP_READ_BIT
, &depthMap
, &depthStride
);
755 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
759 if (stencilRb
!= depthRb
) {
760 ctx
->Driver
.MapRenderbuffer(ctx
, stencilRb
, x
, y
, width
, height
,
761 GL_MAP_READ_BIT
, &stencilMap
,
764 ctx
->Driver
.UnmapRenderbuffer(ctx
, depthRb
);
765 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
770 stencilMap
= depthMap
;
771 stencilStride
= depthStride
;
774 stencilVals
= malloc(width
* sizeof(GLubyte
));
775 depthVals
= malloc(width
* sizeof(GLfloat
));
777 if (stencilVals
&& depthVals
) {
778 for (j
= 0; j
< height
; j
++) {
779 _mesa_unpack_float_z_row(depthRb
->Format
, width
, depthMap
, depthVals
);
780 _mesa_unpack_ubyte_stencil_row(stencilRb
->Format
, width
,
781 stencilMap
, stencilVals
);
783 _mesa_pack_depth_stencil_span(ctx
, width
, type
, (GLuint
*)dst
,
784 depthVals
, stencilVals
, packing
);
786 depthMap
+= depthStride
;
787 stencilMap
+= stencilStride
;
792 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
798 ctx
->Driver
.UnmapRenderbuffer(ctx
, depthRb
);
799 if (stencilRb
!= depthRb
) {
800 ctx
->Driver
.UnmapRenderbuffer(ctx
, stencilRb
);
806 * Read combined depth/stencil values.
807 * We'll have already done error checking to be sure the expected
808 * depth and stencil buffers really exist.
811 read_depth_stencil_pixels(struct gl_context
*ctx
,
813 GLsizei width
, GLsizei height
,
814 GLenum type
, GLvoid
*pixels
,
815 const struct gl_pixelstore_attrib
*packing
)
817 const GLboolean scaleOrBias
818 = ctx
->Pixel
.DepthScale
!= 1.0F
|| ctx
->Pixel
.DepthBias
!= 0.0F
;
819 const GLboolean stencilTransfer
= ctx
->Pixel
.IndexShift
820 || ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapStencilFlag
;
824 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
,
826 GL_DEPTH_STENCIL_EXT
,
828 dstStride
= _mesa_image_row_stride(packing
, width
,
829 GL_DEPTH_STENCIL_EXT
, type
);
831 /* Fast 24/8 reads. */
832 if (type
== GL_UNSIGNED_INT_24_8
&&
833 !scaleOrBias
&& !stencilTransfer
&& !packing
->SwapBytes
) {
834 if (fast_read_depth_stencil_pixels(ctx
, x
, y
, width
, height
,
838 if (fast_read_depth_stencil_pixels_separate(ctx
, x
, y
, width
, height
,
839 (uint32_t *)dst
, dstStride
))
843 slow_read_depth_stencil_pixels_separate(ctx
, x
, y
, width
, height
,
851 * Software fallback routine for ctx->Driver.ReadPixels().
852 * By time we get here, all error checking will have been done.
855 _mesa_readpixels(struct gl_context
*ctx
,
856 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
857 GLenum format
, GLenum type
,
858 const struct gl_pixelstore_attrib
*packing
,
861 struct gl_pixelstore_attrib clippedPacking
= *packing
;
864 _mesa_update_state(ctx
);
866 /* Do all needed clipping here, so that we can forget about it later */
867 if (_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
869 pixels
= _mesa_map_pbo_dest(ctx
, &clippedPacking
, pixels
);
872 /* Try memcpy first. */
873 if (readpixels_memcpy(ctx
, x
, y
, width
, height
, format
, type
,
875 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
879 /* Otherwise take the slow path. */
881 case GL_STENCIL_INDEX
:
882 read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
885 case GL_DEPTH_COMPONENT
:
886 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
889 case GL_DEPTH_STENCIL_EXT
:
890 read_depth_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
894 /* all other formats should be color formats */
895 read_rgba_pixels(ctx
, x
, y
, width
, height
, format
, type
, pixels
,
899 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
906 read_pixels_es3_error_check(GLenum format
, GLenum type
,
907 const struct gl_renderbuffer
*rb
)
909 const GLenum internalFormat
= rb
->InternalFormat
;
910 const GLenum data_type
= _mesa_get_format_datatype(rb
->Format
);
911 GLboolean is_unsigned_int
= GL_FALSE
;
912 GLboolean is_signed_int
= GL_FALSE
;
913 GLboolean is_float_depth
= (internalFormat
== GL_DEPTH_COMPONENT32F
) ||
914 (internalFormat
== GL_DEPTH32F_STENCIL8
);
916 is_unsigned_int
= _mesa_is_enum_format_unsigned_int(internalFormat
);
917 if (!is_unsigned_int
) {
918 is_signed_int
= _mesa_is_enum_format_signed_int(internalFormat
);
923 if (type
== GL_FLOAT
&& data_type
== GL_FLOAT
)
924 return GL_NO_ERROR
; /* EXT_color_buffer_float */
925 if (type
== GL_UNSIGNED_BYTE
&& data_type
== GL_UNSIGNED_NORMALIZED
)
927 if (internalFormat
== GL_RGB10_A2
&&
928 type
== GL_UNSIGNED_INT_2_10_10_10_REV
)
930 if (internalFormat
== GL_RGB10_A2UI
&& type
== GL_UNSIGNED_BYTE
)
934 /* GL_EXT_read_format_bgra */
935 if (type
== GL_UNSIGNED_BYTE
||
936 type
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
937 type
== GL_UNSIGNED_SHORT_1_5_5_5_REV
)
940 case GL_RGBA_INTEGER
:
941 if ((is_signed_int
&& type
== GL_INT
) ||
942 (is_unsigned_int
&& type
== GL_UNSIGNED_INT
))
945 case GL_DEPTH_STENCIL
:
947 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
951 case GL_UNSIGNED_INT_24_8
:
956 return GL_INVALID_ENUM
;
959 case GL_DEPTH_COMPONENT
:
965 case GL_UNSIGNED_SHORT
:
966 case GL_UNSIGNED_INT
:
967 case GL_UNSIGNED_INT_24_8
:
972 return GL_INVALID_ENUM
;
975 case GL_STENCIL_INDEX
:
977 case GL_UNSIGNED_BYTE
:
980 return GL_INVALID_ENUM
;
985 return GL_INVALID_OPERATION
;
990 _mesa_ReadnPixelsARB( GLint x
, GLint y
, GLsizei width
, GLsizei height
,
991 GLenum format
, GLenum type
, GLsizei bufSize
,
994 GLenum err
= GL_NO_ERROR
;
995 struct gl_renderbuffer
*rb
;
997 GET_CURRENT_CONTEXT(ctx
);
999 FLUSH_VERTICES(ctx
, 0);
1000 FLUSH_CURRENT(ctx
, 0);
1002 if (MESA_VERBOSE
& VERBOSE_API
)
1003 _mesa_debug(ctx
, "glReadPixels(%d, %d, %s, %s, %p)\n",
1005 _mesa_enum_to_string(format
),
1006 _mesa_enum_to_string(type
),
1009 if (width
< 0 || height
< 0) {
1010 _mesa_error( ctx
, GL_INVALID_VALUE
,
1011 "glReadPixels(width=%d height=%d)", width
, height
);
1016 _mesa_update_state(ctx
);
1018 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
1019 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
1020 "glReadPixels(incomplete framebuffer)" );
1024 rb
= _mesa_get_read_renderbuffer_for_format(ctx
, format
);
1026 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1027 "glReadPixels(read buffer)");
1031 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
1032 * combinations of format and type that can be used.
1034 * Technically, only two combinations are actually allowed:
1035 * GL_RGBA/GL_UNSIGNED_BYTE, and some implementation-specific internal
1036 * preferred combination. This code doesn't know what that preferred
1037 * combination is, and Mesa can handle anything valid. Just work instead.
1039 if (_mesa_is_gles(ctx
)) {
1040 if (ctx
->API
== API_OPENGLES2
&&
1041 _mesa_is_color_format(format
) &&
1042 _mesa_get_color_read_format(ctx
) == format
&&
1043 _mesa_get_color_read_type(ctx
) == type
) {
1045 } else if (ctx
->Version
< 30) {
1046 err
= _mesa_es_error_check_format_and_type(format
, type
, 2);
1047 if (err
== GL_NO_ERROR
) {
1048 if (type
== GL_FLOAT
|| type
== GL_HALF_FLOAT_OES
) {
1049 err
= GL_INVALID_OPERATION
;
1053 err
= read_pixels_es3_error_check(format
, type
, rb
);
1056 if (err
!= GL_NO_ERROR
) {
1057 _mesa_error(ctx
, err
, "glReadPixels(invalid format %s and/or type %s)",
1058 _mesa_enum_to_string(format
),
1059 _mesa_enum_to_string(type
));
1064 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
1065 if (err
!= GL_NO_ERROR
) {
1066 _mesa_error(ctx
, err
, "glReadPixels(invalid format %s and/or type %s)",
1067 _mesa_enum_to_string(format
),
1068 _mesa_enum_to_string(type
));
1072 if (_mesa_is_user_fbo(ctx
->ReadBuffer
) &&
1073 ctx
->ReadBuffer
->Visual
.samples
> 0) {
1074 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(multisample FBO)");
1078 if (!_mesa_source_buffer_exists(ctx
, format
)) {
1079 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(no readbuffer)");
1083 /* Check that the destination format and source buffer are both
1084 * integer-valued or both non-integer-valued.
1086 if (ctx
->Extensions
.EXT_texture_integer
&& _mesa_is_color_format(format
)) {
1087 const struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
1088 const GLboolean srcInteger
= _mesa_is_format_integer_color(rb
->Format
);
1089 const GLboolean dstInteger
= _mesa_is_enum_format_integer(format
);
1090 if (dstInteger
!= srcInteger
) {
1091 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1092 "glReadPixels(integer / non-integer format mismatch");
1097 if (width
== 0 || height
== 0)
1098 return; /* nothing to do */
1100 if (!_mesa_validate_pbo_access(2, &ctx
->Pack
, width
, height
, 1,
1101 format
, type
, bufSize
, pixels
)) {
1102 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
1103 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1104 "glReadPixels(out of bounds PBO access)");
1106 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1107 "glReadnPixelsARB(out of bounds access:"
1108 " bufSize (%d) is too small)", bufSize
);
1113 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) &&
1114 _mesa_check_disallowed_mapping(ctx
->Pack
.BufferObj
)) {
1115 /* buffer is mapped - that's an error */
1116 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(PBO is mapped)");
1120 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
1121 format
, type
, &ctx
->Pack
, pixels
);
1125 _mesa_ReadPixels( GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1126 GLenum format
, GLenum type
, GLvoid
*pixels
)
1128 _mesa_ReadnPixelsARB(x
, y
, width
, height
, format
, type
, INT_MAX
, pixels
);