mesa: rename RGBA8888_* format constants to something appropriate.
[mesa.git] / src / mesa / main / readpix.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
23 */
24
25 #include "glheader.h"
26 #include "imports.h"
27 #include "blend.h"
28 #include "bufferobj.h"
29 #include "context.h"
30 #include "enums.h"
31 #include "readpix.h"
32 #include "framebuffer.h"
33 #include "formats.h"
34 #include "format_unpack.h"
35 #include "image.h"
36 #include "mtypes.h"
37 #include "pack.h"
38 #include "pbo.h"
39 #include "state.h"
40 #include "glformats.h"
41 #include "fbobject.h"
42 #include "format_utils.h"
43 #include "pixeltransfer.h"
44
45
46 /**
47 * Return true if the conversion L=R+G+B is needed.
48 */
49 static GLboolean
50 need_rgb_to_luminance_conversion(mesa_format texFormat, GLenum format)
51 {
52 GLenum baseTexFormat = _mesa_get_format_base_format(texFormat);
53
54 return (baseTexFormat == GL_RG ||
55 baseTexFormat == GL_RGB ||
56 baseTexFormat == GL_RGBA) &&
57 (format == GL_LUMINANCE || format == GL_LUMINANCE_ALPHA);
58 }
59
60
61 /**
62 * Return transfer op flags for this ReadPixels operation.
63 */
64 static GLbitfield
65 get_readpixels_transfer_ops(const struct gl_context *ctx, mesa_format texFormat,
66 GLenum format, GLenum type, GLboolean uses_blit)
67 {
68 GLbitfield transferOps = ctx->_ImageTransferState;
69
70 if (format == GL_DEPTH_COMPONENT ||
71 format == GL_DEPTH_STENCIL ||
72 format == GL_STENCIL_INDEX) {
73 return 0;
74 }
75
76 /* Pixel transfer ops (scale, bias, table lookup) do not apply
77 * to integer formats.
78 */
79 if (_mesa_is_enum_format_integer(format)) {
80 return 0;
81 }
82
83 if (uses_blit) {
84 /* For blit-based ReadPixels packing, the clamping is done automatically
85 * unless the type is float. */
86 if (_mesa_get_clamp_read_color(ctx) &&
87 (type == GL_FLOAT || type == GL_HALF_FLOAT)) {
88 transferOps |= IMAGE_CLAMP_BIT;
89 }
90 }
91 else {
92 /* For CPU-based ReadPixels packing, the clamping must always be done
93 * for non-float types, */
94 if (_mesa_get_clamp_read_color(ctx) ||
95 (type != GL_FLOAT && type != GL_HALF_FLOAT)) {
96 transferOps |= IMAGE_CLAMP_BIT;
97 }
98 }
99
100 /* If the format is unsigned normalized, we can ignore clamping
101 * because the values are already in the range [0,1] so it won't
102 * have any effect anyway.
103 */
104 if (_mesa_get_format_datatype(texFormat) == GL_UNSIGNED_NORMALIZED &&
105 !need_rgb_to_luminance_conversion(texFormat, format)) {
106 transferOps &= ~IMAGE_CLAMP_BIT;
107 }
108
109 return transferOps;
110 }
111
112
113 /**
114 * Return true if memcpy cannot be used for ReadPixels.
115 *
116 * If uses_blit is true, the function returns true if a simple 3D engine blit
117 * cannot be used for ReadPixels packing.
118 *
119 * NOTE: This doesn't take swizzling and format conversions between
120 * the readbuffer and the pixel pack buffer into account.
121 */
122 GLboolean
123 _mesa_readpixels_needs_slow_path(const struct gl_context *ctx, GLenum format,
124 GLenum type, GLboolean uses_blit)
125 {
126 struct gl_renderbuffer *rb =
127 _mesa_get_read_renderbuffer_for_format(ctx, format);
128 GLenum srcType;
129
130 ASSERT(rb);
131
132 /* There are different rules depending on the base format. */
133 switch (format) {
134 case GL_DEPTH_STENCIL:
135 return !_mesa_has_depthstencil_combined(ctx->ReadBuffer) ||
136 ctx->Pixel.DepthScale != 1.0f || ctx->Pixel.DepthBias != 0.0f ||
137 ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset ||
138 ctx->Pixel.MapStencilFlag;
139
140 case GL_DEPTH_COMPONENT:
141 return ctx->Pixel.DepthScale != 1.0f || ctx->Pixel.DepthBias != 0.0f;
142
143 case GL_STENCIL_INDEX:
144 return ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset ||
145 ctx->Pixel.MapStencilFlag;
146
147 default:
148 /* Color formats. */
149 if (need_rgb_to_luminance_conversion(rb->Format, format)) {
150 return GL_TRUE;
151 }
152
153 /* Conversion between signed and unsigned integers needs masking
154 * (it isn't just memcpy). */
155 srcType = _mesa_get_format_datatype(rb->Format);
156
157 if ((srcType == GL_INT &&
158 (type == GL_UNSIGNED_INT ||
159 type == GL_UNSIGNED_SHORT ||
160 type == GL_UNSIGNED_BYTE)) ||
161 (srcType == GL_UNSIGNED_INT &&
162 (type == GL_INT ||
163 type == GL_SHORT ||
164 type == GL_BYTE))) {
165 return GL_TRUE;
166 }
167
168 /* And finally, see if there are any transfer ops. */
169 return get_readpixels_transfer_ops(ctx, rb->Format, format, type,
170 uses_blit) != 0;
171 }
172 return GL_FALSE;
173 }
174
175
176 static GLboolean
177 readpixels_can_use_memcpy(const struct gl_context *ctx, GLenum format, GLenum type,
178 const struct gl_pixelstore_attrib *packing)
179 {
180 struct gl_renderbuffer *rb =
181 _mesa_get_read_renderbuffer_for_format(ctx, format);
182
183 ASSERT(rb);
184
185 if (_mesa_readpixels_needs_slow_path(ctx, format, type, GL_FALSE)) {
186 return GL_FALSE;
187 }
188
189 /* The base internal format and the base Mesa format must match. */
190 if (rb->_BaseFormat != _mesa_get_format_base_format(rb->Format)) {
191 return GL_FALSE;
192 }
193
194 /* The Mesa format must match the input format and type. */
195 if (!_mesa_format_matches_format_and_type(rb->Format, format, type,
196 packing->SwapBytes)) {
197 return GL_FALSE;
198 }
199
200 return GL_TRUE;
201 }
202
203
204 static GLboolean
205 readpixels_memcpy(struct gl_context *ctx,
206 GLint x, GLint y,
207 GLsizei width, GLsizei height,
208 GLenum format, GLenum type,
209 GLvoid *pixels,
210 const struct gl_pixelstore_attrib *packing)
211 {
212 struct gl_renderbuffer *rb =
213 _mesa_get_read_renderbuffer_for_format(ctx, format);
214 GLubyte *dst, *map;
215 int dstStride, stride, j, texelBytes;
216
217 /* Fail if memcpy cannot be used. */
218 if (!readpixels_can_use_memcpy(ctx, format, type, packing)) {
219 return GL_FALSE;
220 }
221
222 dstStride = _mesa_image_row_stride(packing, width, format, type);
223 dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
224 format, type, 0, 0);
225
226 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
227 &map, &stride);
228 if (!map) {
229 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
230 return GL_TRUE; /* don't bother trying the slow path */
231 }
232
233 texelBytes = _mesa_get_format_bytes(rb->Format);
234
235 /* memcpy*/
236 for (j = 0; j < height; j++) {
237 memcpy(dst, map, width * texelBytes);
238 dst += dstStride;
239 map += stride;
240 }
241
242 ctx->Driver.UnmapRenderbuffer(ctx, rb);
243 return GL_TRUE;
244 }
245
246
247 /**
248 * Optimized path for conversion of depth values to GL_DEPTH_COMPONENT,
249 * GL_UNSIGNED_INT.
250 */
251 static GLboolean
252 read_uint_depth_pixels( struct gl_context *ctx,
253 GLint x, GLint y,
254 GLsizei width, GLsizei height,
255 GLenum type, GLvoid *pixels,
256 const struct gl_pixelstore_attrib *packing )
257 {
258 struct gl_framebuffer *fb = ctx->ReadBuffer;
259 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
260 GLubyte *map, *dst;
261 int stride, dstStride, j;
262
263 if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0)
264 return GL_FALSE;
265
266 if (packing->SwapBytes)
267 return GL_FALSE;
268
269 if (_mesa_get_format_datatype(rb->Format) != GL_UNSIGNED_NORMALIZED)
270 return GL_FALSE;
271
272 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
273 &map, &stride);
274
275 if (!map) {
276 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
277 return GL_TRUE; /* don't bother trying the slow path */
278 }
279
280 dstStride = _mesa_image_row_stride(packing, width, GL_DEPTH_COMPONENT, type);
281 dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
282 GL_DEPTH_COMPONENT, type, 0, 0);
283
284 for (j = 0; j < height; j++) {
285 _mesa_unpack_uint_z_row(rb->Format, width, map, (GLuint *)dst);
286
287 map += stride;
288 dst += dstStride;
289 }
290 ctx->Driver.UnmapRenderbuffer(ctx, rb);
291
292 return GL_TRUE;
293 }
294
295 /**
296 * Read pixels for format=GL_DEPTH_COMPONENT.
297 */
298 static void
299 read_depth_pixels( struct gl_context *ctx,
300 GLint x, GLint y,
301 GLsizei width, GLsizei height,
302 GLenum type, GLvoid *pixels,
303 const struct gl_pixelstore_attrib *packing )
304 {
305 struct gl_framebuffer *fb = ctx->ReadBuffer;
306 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
307 GLint j;
308 GLubyte *dst, *map;
309 int dstStride, stride;
310 GLfloat *depthValues;
311
312 if (!rb)
313 return;
314
315 /* clipping should have been done already */
316 ASSERT(x >= 0);
317 ASSERT(y >= 0);
318 ASSERT(x + width <= (GLint) rb->Width);
319 ASSERT(y + height <= (GLint) rb->Height);
320
321 if (type == GL_UNSIGNED_INT &&
322 read_uint_depth_pixels(ctx, x, y, width, height, type, pixels, packing)) {
323 return;
324 }
325
326 dstStride = _mesa_image_row_stride(packing, width, GL_DEPTH_COMPONENT, type);
327 dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
328 GL_DEPTH_COMPONENT, type, 0, 0);
329
330 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
331 &map, &stride);
332 if (!map) {
333 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
334 return;
335 }
336
337 depthValues = malloc(width * sizeof(GLfloat));
338
339 if (depthValues) {
340 /* General case (slower) */
341 for (j = 0; j < height; j++, y++) {
342 _mesa_unpack_float_z_row(rb->Format, width, map, depthValues);
343 _mesa_pack_depth_span(ctx, width, dst, type, depthValues, packing);
344
345 dst += dstStride;
346 map += stride;
347 }
348 }
349 else {
350 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
351 }
352
353 free(depthValues);
354
355 ctx->Driver.UnmapRenderbuffer(ctx, rb);
356 }
357
358
359 /**
360 * Read pixels for format=GL_STENCIL_INDEX.
361 */
362 static void
363 read_stencil_pixels( struct gl_context *ctx,
364 GLint x, GLint y,
365 GLsizei width, GLsizei height,
366 GLenum type, GLvoid *pixels,
367 const struct gl_pixelstore_attrib *packing )
368 {
369 struct gl_framebuffer *fb = ctx->ReadBuffer;
370 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
371 GLint j;
372 GLubyte *map, *stencil;
373 GLint stride;
374
375 if (!rb)
376 return;
377
378 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
379 &map, &stride);
380 if (!map) {
381 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
382 return;
383 }
384
385 stencil = malloc(width * sizeof(GLubyte));
386
387 if (stencil) {
388 /* process image row by row */
389 for (j = 0; j < height; j++) {
390 GLvoid *dest;
391
392 _mesa_unpack_ubyte_stencil_row(rb->Format, width, map, stencil);
393 dest = _mesa_image_address2d(packing, pixels, width, height,
394 GL_STENCIL_INDEX, type, j, 0);
395
396 _mesa_pack_stencil_span(ctx, width, type, dest, stencil, packing);
397
398 map += stride;
399 }
400 }
401 else {
402 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
403 }
404
405 free(stencil);
406
407 ctx->Driver.UnmapRenderbuffer(ctx, rb);
408 }
409
410 /*
411 * Read R, G, B, A, RGB, L, or LA pixels.
412 */
413 static void
414 read_rgba_pixels( struct gl_context *ctx,
415 GLint x, GLint y,
416 GLsizei width, GLsizei height,
417 GLenum format, GLenum type, GLvoid *pixels,
418 const struct gl_pixelstore_attrib *packing )
419 {
420 GLbitfield transferOps;
421 bool dst_is_integer, dst_is_luminance, needs_rebase;
422 int dst_stride, src_stride, rb_stride;
423 uint32_t dst_format, src_format;
424 GLubyte *dst, *map;
425 mesa_format rb_format;
426 bool needs_rgba;
427 void *rgba, *src;
428 bool src_is_uint = false;
429 uint8_t rebase_swizzle[4];
430 struct gl_framebuffer *fb = ctx->ReadBuffer;
431 struct gl_renderbuffer *rb = fb->_ColorReadBuffer;
432
433 if (!rb)
434 return;
435
436 transferOps = get_readpixels_transfer_ops(ctx, rb->Format, format, type,
437 GL_FALSE);
438 /* Describe the dst format */
439 dst_is_integer = _mesa_is_enum_format_integer(format);
440 dst_stride = _mesa_image_row_stride(packing, width, format, type);
441 dst_format = _mesa_format_from_format_and_type(format, type);
442 dst_is_luminance = format == GL_LUMINANCE ||
443 format == GL_LUMINANCE_ALPHA ||
444 format == GL_LUMINANCE_INTEGER_EXT ||
445 format == GL_LUMINANCE_ALPHA_INTEGER_EXT;
446 dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
447 format, type, 0, 0);
448
449 /* Map the source render buffer */
450 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
451 &map, &rb_stride);
452 if (!map) {
453 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
454 return;
455 }
456 rb_format = _mesa_get_srgb_format_linear(rb->Format);
457
458 /*
459 * Depending on the base formats involved in the conversion we might need to
460 * rebase some values, so for these formats we compute a rebase swizzle.
461 */
462 if (rb->_BaseFormat == GL_LUMINANCE || rb->_BaseFormat == GL_INTENSITY) {
463 needs_rebase = true;
464 rebase_swizzle[0] = MESA_FORMAT_SWIZZLE_X;
465 rebase_swizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
466 rebase_swizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
467 rebase_swizzle[3] = MESA_FORMAT_SWIZZLE_ONE;
468 } else if (rb->_BaseFormat == GL_LUMINANCE_ALPHA) {
469 needs_rebase = true;
470 rebase_swizzle[0] = MESA_FORMAT_SWIZZLE_X;
471 rebase_swizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
472 rebase_swizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
473 rebase_swizzle[3] = MESA_FORMAT_SWIZZLE_W;
474 } else if (_mesa_get_format_base_format(rb_format) != rb->_BaseFormat) {
475 needs_rebase =
476 _mesa_compute_rgba2base2rgba_component_mapping(rb->_BaseFormat,
477 rebase_swizzle);
478 } else {
479 needs_rebase = false;
480 }
481
482 /* Since _mesa_format_convert does not handle transferOps we need to handle
483 * them before we call the function. This requires to convert to RGBA float
484 * first so we can call _mesa_apply_rgba_transfer_ops. If the dst format is
485 * integer transferOps do not apply.
486 *
487 * Converting to luminance also requires converting to RGBA first, so we can
488 * then compute luminance values as L=R+G+B. Notice that this is different
489 * from GetTexImage, where we compute L=R.
490 */
491 assert(!transferOps || (transferOps && !dst_is_integer));
492
493 needs_rgba = transferOps || dst_is_luminance;
494 rgba = NULL;
495 if (needs_rgba) {
496 uint32_t rgba_format;
497 int rgba_stride;
498 bool need_convert;
499
500 /* Convert to RGBA float or int/uint depending on the type of the src */
501 if (dst_is_integer) {
502 src_is_uint = _mesa_is_format_unsigned(rb_format);
503 if (src_is_uint) {
504 rgba_format = RGBA32_UINT;
505 rgba_stride = width * 4 * sizeof(GLuint);
506 } else {
507 rgba_format = RGBA32_INT;
508 rgba_stride = width * 4 * sizeof(GLint);
509 }
510 } else {
511 rgba_format = RGBA32_FLOAT;
512 rgba_stride = width * 4 * sizeof(GLfloat);
513 }
514
515 /* If we are lucky and the dst format matches the RGBA format we need to
516 * convert to, then we can convert directly into the dst buffer and avoid
517 * the final conversion/copy from the rgba buffer to the dst buffer.
518 */
519 if (dst_format == rgba_format) {
520 need_convert = false;
521 rgba = dst;
522 } else {
523 need_convert = true;
524 rgba = malloc(height * rgba_stride);
525 if (!rgba) {
526 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
527 goto done_unmap;
528 }
529 }
530
531 /* Convert to RGBA now */
532 _mesa_format_convert(rgba, rgba_format, rgba_stride,
533 map, rb_format, rb_stride,
534 width, height,
535 needs_rebase ? rebase_swizzle : NULL);
536
537 /* Handle transfer ops if necessary */
538 if (transferOps)
539 _mesa_apply_rgba_transfer_ops(ctx, transferOps, width * height, rgba);
540
541 /* If we had to rebase, we have already taken care of that */
542 needs_rebase = false;
543
544 /* If we were lucky and our RGBA conversion matches the dst format, then
545 * we are done.
546 */
547 if (!need_convert)
548 goto done_swap;
549
550 /* Otherwise, we need to convert from RGBA to dst next */
551 src = rgba;
552 src_format = rgba_format;
553 src_stride = rgba_stride;
554 } else {
555 /* No RGBA conversion needed, convert directly to dst */
556 src = map;
557 src_format = rb_format;
558 src_stride = rb_stride;
559 }
560
561 /* Do the conversion.
562 *
563 * If the dst format is Luminance, we need to do the conversion by computing
564 * L=R+G+B values.
565 */
566 if (!dst_is_luminance) {
567 _mesa_format_convert(dst, dst_format, dst_stride,
568 src, src_format, src_stride,
569 width, height,
570 needs_rebase ? rebase_swizzle : NULL);
571 } else if (!dst_is_integer) {
572 /* Compute float Luminance values from RGBA float */
573 int luminance_stride, luminance_bytes;
574 void *luminance;
575 uint32_t luminance_format;
576
577 luminance_stride = width * sizeof(GL_FLOAT);
578 if (format == GL_LUMINANCE_ALPHA)
579 luminance_stride *= 2;
580 luminance_bytes = height * luminance_stride;
581 luminance = malloc(luminance_bytes);
582 if (!luminance) {
583 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
584 free(rgba);
585 goto done_unmap;
586 }
587 _mesa_pack_luminance_from_rgba_float(width * height, src,
588 luminance, format, transferOps);
589
590 /* Convert from Luminance float to dst (this will hadle type conversion
591 * from float to the type of dst if necessary)
592 */
593 luminance_format = _mesa_format_from_format_and_type(format, GL_FLOAT);
594 _mesa_format_convert(dst, dst_format, dst_stride,
595 luminance, luminance_format, luminance_stride,
596 width, height, NULL);
597 } else {
598 _mesa_pack_luminance_from_rgba_integer(width * height, src, !src_is_uint,
599 dst, format, type);
600 }
601
602 if (rgba)
603 free(rgba);
604
605 done_swap:
606 /* Handle byte swapping if required */
607 if (packing->SwapBytes) {
608 int components = _mesa_components_in_format(format);
609 GLint swapSize = _mesa_sizeof_packed_type(type);
610 if (swapSize == 2)
611 _mesa_swap2((GLushort *) dst, width * height * components);
612 else if (swapSize == 4)
613 _mesa_swap4((GLuint *) dst, width * height * components);
614 }
615
616 done_unmap:
617 ctx->Driver.UnmapRenderbuffer(ctx, rb);
618 }
619
620 /**
621 * For a packed depth/stencil buffer being read as depth/stencil, just memcpy the
622 * data (possibly swapping 8/24 vs 24/8 as we go).
623 */
624 static GLboolean
625 fast_read_depth_stencil_pixels(struct gl_context *ctx,
626 GLint x, GLint y,
627 GLsizei width, GLsizei height,
628 GLubyte *dst, int dstStride)
629 {
630 struct gl_framebuffer *fb = ctx->ReadBuffer;
631 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
632 struct gl_renderbuffer *stencilRb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
633 GLubyte *map;
634 int stride, i;
635
636 if (rb != stencilRb)
637 return GL_FALSE;
638
639 if (rb->Format != MESA_FORMAT_S8_UINT_Z24_UNORM &&
640 rb->Format != MESA_FORMAT_Z24_UNORM_S8_UINT)
641 return GL_FALSE;
642
643 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
644 &map, &stride);
645 if (!map) {
646 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
647 return GL_TRUE; /* don't bother trying the slow path */
648 }
649
650 for (i = 0; i < height; i++) {
651 _mesa_unpack_uint_24_8_depth_stencil_row(rb->Format, width,
652 map, (GLuint *)dst);
653 map += stride;
654 dst += dstStride;
655 }
656
657 ctx->Driver.UnmapRenderbuffer(ctx, rb);
658
659 return GL_TRUE;
660 }
661
662
663 /**
664 * For non-float-depth and stencil buffers being read as 24/8 depth/stencil,
665 * copy the integer data directly instead of converting depth to float and
666 * re-packing.
667 */
668 static GLboolean
669 fast_read_depth_stencil_pixels_separate(struct gl_context *ctx,
670 GLint x, GLint y,
671 GLsizei width, GLsizei height,
672 uint32_t *dst, int dstStride)
673 {
674 struct gl_framebuffer *fb = ctx->ReadBuffer;
675 struct gl_renderbuffer *depthRb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
676 struct gl_renderbuffer *stencilRb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
677 GLubyte *depthMap, *stencilMap, *stencilVals;
678 int depthStride, stencilStride, i, j;
679
680 if (_mesa_get_format_datatype(depthRb->Format) != GL_UNSIGNED_NORMALIZED)
681 return GL_FALSE;
682
683 ctx->Driver.MapRenderbuffer(ctx, depthRb, x, y, width, height,
684 GL_MAP_READ_BIT, &depthMap, &depthStride);
685 if (!depthMap) {
686 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
687 return GL_TRUE; /* don't bother trying the slow path */
688 }
689
690 ctx->Driver.MapRenderbuffer(ctx, stencilRb, x, y, width, height,
691 GL_MAP_READ_BIT, &stencilMap, &stencilStride);
692 if (!stencilMap) {
693 ctx->Driver.UnmapRenderbuffer(ctx, depthRb);
694 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
695 return GL_TRUE; /* don't bother trying the slow path */
696 }
697
698 stencilVals = malloc(width * sizeof(GLubyte));
699
700 if (stencilVals) {
701 for (j = 0; j < height; j++) {
702 _mesa_unpack_uint_z_row(depthRb->Format, width, depthMap, dst);
703 _mesa_unpack_ubyte_stencil_row(stencilRb->Format, width,
704 stencilMap, stencilVals);
705
706 for (i = 0; i < width; i++) {
707 dst[i] = (dst[i] & 0xffffff00) | stencilVals[i];
708 }
709
710 depthMap += depthStride;
711 stencilMap += stencilStride;
712 dst += dstStride / 4;
713 }
714 }
715 else {
716 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
717 }
718
719 free(stencilVals);
720
721 ctx->Driver.UnmapRenderbuffer(ctx, depthRb);
722 ctx->Driver.UnmapRenderbuffer(ctx, stencilRb);
723
724 return GL_TRUE;
725 }
726
727 static void
728 slow_read_depth_stencil_pixels_separate(struct gl_context *ctx,
729 GLint x, GLint y,
730 GLsizei width, GLsizei height,
731 GLenum type,
732 const struct gl_pixelstore_attrib *packing,
733 GLubyte *dst, int dstStride)
734 {
735 struct gl_framebuffer *fb = ctx->ReadBuffer;
736 struct gl_renderbuffer *depthRb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
737 struct gl_renderbuffer *stencilRb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
738 GLubyte *depthMap, *stencilMap;
739 int depthStride, stencilStride, j;
740 GLubyte *stencilVals;
741 GLfloat *depthVals;
742
743
744 /* The depth and stencil buffers might be separate, or a single buffer.
745 * If one buffer, only map it once.
746 */
747 ctx->Driver.MapRenderbuffer(ctx, depthRb, x, y, width, height,
748 GL_MAP_READ_BIT, &depthMap, &depthStride);
749 if (!depthMap) {
750 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
751 return;
752 }
753
754 if (stencilRb != depthRb) {
755 ctx->Driver.MapRenderbuffer(ctx, stencilRb, x, y, width, height,
756 GL_MAP_READ_BIT, &stencilMap,
757 &stencilStride);
758 if (!stencilMap) {
759 ctx->Driver.UnmapRenderbuffer(ctx, depthRb);
760 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
761 return;
762 }
763 }
764 else {
765 stencilMap = depthMap;
766 stencilStride = depthStride;
767 }
768
769 stencilVals = malloc(width * sizeof(GLubyte));
770 depthVals = malloc(width * sizeof(GLfloat));
771
772 if (stencilVals && depthVals) {
773 for (j = 0; j < height; j++) {
774 _mesa_unpack_float_z_row(depthRb->Format, width, depthMap, depthVals);
775 _mesa_unpack_ubyte_stencil_row(stencilRb->Format, width,
776 stencilMap, stencilVals);
777
778 _mesa_pack_depth_stencil_span(ctx, width, type, (GLuint *)dst,
779 depthVals, stencilVals, packing);
780
781 depthMap += depthStride;
782 stencilMap += stencilStride;
783 dst += dstStride;
784 }
785 }
786 else {
787 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
788 }
789
790 free(stencilVals);
791 free(depthVals);
792
793 ctx->Driver.UnmapRenderbuffer(ctx, depthRb);
794 if (stencilRb != depthRb) {
795 ctx->Driver.UnmapRenderbuffer(ctx, stencilRb);
796 }
797 }
798
799
800 /**
801 * Read combined depth/stencil values.
802 * We'll have already done error checking to be sure the expected
803 * depth and stencil buffers really exist.
804 */
805 static void
806 read_depth_stencil_pixels(struct gl_context *ctx,
807 GLint x, GLint y,
808 GLsizei width, GLsizei height,
809 GLenum type, GLvoid *pixels,
810 const struct gl_pixelstore_attrib *packing )
811 {
812 const GLboolean scaleOrBias
813 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
814 const GLboolean stencilTransfer = ctx->Pixel.IndexShift
815 || ctx->Pixel.IndexOffset || ctx->Pixel.MapStencilFlag;
816 GLubyte *dst;
817 int dstStride;
818
819 dst = (GLubyte *) _mesa_image_address2d(packing, pixels,
820 width, height,
821 GL_DEPTH_STENCIL_EXT,
822 type, 0, 0);
823 dstStride = _mesa_image_row_stride(packing, width,
824 GL_DEPTH_STENCIL_EXT, type);
825
826 /* Fast 24/8 reads. */
827 if (type == GL_UNSIGNED_INT_24_8 &&
828 !scaleOrBias && !stencilTransfer && !packing->SwapBytes) {
829 if (fast_read_depth_stencil_pixels(ctx, x, y, width, height,
830 dst, dstStride))
831 return;
832
833 if (fast_read_depth_stencil_pixels_separate(ctx, x, y, width, height,
834 (uint32_t *)dst, dstStride))
835 return;
836 }
837
838 slow_read_depth_stencil_pixels_separate(ctx, x, y, width, height,
839 type, packing,
840 dst, dstStride);
841 }
842
843
844
845 /**
846 * Software fallback routine for ctx->Driver.ReadPixels().
847 * By time we get here, all error checking will have been done.
848 */
849 void
850 _mesa_readpixels(struct gl_context *ctx,
851 GLint x, GLint y, GLsizei width, GLsizei height,
852 GLenum format, GLenum type,
853 const struct gl_pixelstore_attrib *packing,
854 GLvoid *pixels)
855 {
856 struct gl_pixelstore_attrib clippedPacking = *packing;
857
858 if (ctx->NewState)
859 _mesa_update_state(ctx);
860
861 /* Do all needed clipping here, so that we can forget about it later */
862 if (_mesa_clip_readpixels(ctx, &x, &y, &width, &height, &clippedPacking)) {
863
864 pixels = _mesa_map_pbo_dest(ctx, &clippedPacking, pixels);
865
866 if (pixels) {
867 /* Try memcpy first. */
868 if (readpixels_memcpy(ctx, x, y, width, height, format, type,
869 pixels, packing)) {
870 _mesa_unmap_pbo_dest(ctx, &clippedPacking);
871 return;
872 }
873
874 /* Otherwise take the slow path. */
875 switch (format) {
876 case GL_STENCIL_INDEX:
877 read_stencil_pixels(ctx, x, y, width, height, type, pixels,
878 &clippedPacking);
879 break;
880 case GL_DEPTH_COMPONENT:
881 read_depth_pixels(ctx, x, y, width, height, type, pixels,
882 &clippedPacking);
883 break;
884 case GL_DEPTH_STENCIL_EXT:
885 read_depth_stencil_pixels(ctx, x, y, width, height, type, pixels,
886 &clippedPacking);
887 break;
888 default:
889 /* all other formats should be color formats */
890 read_rgba_pixels(ctx, x, y, width, height, format, type, pixels,
891 &clippedPacking);
892 }
893
894 _mesa_unmap_pbo_dest(ctx, &clippedPacking);
895 }
896 }
897 }
898
899
900 static GLenum
901 read_pixels_es3_error_check(GLenum format, GLenum type,
902 const struct gl_renderbuffer *rb)
903 {
904 const GLenum internalFormat = rb->InternalFormat;
905 const GLenum data_type = _mesa_get_format_datatype(rb->Format);
906 GLboolean is_unsigned_int = GL_FALSE;
907 GLboolean is_signed_int = GL_FALSE;
908
909 if (!_mesa_is_color_format(internalFormat)) {
910 return GL_INVALID_OPERATION;
911 }
912
913 is_unsigned_int = _mesa_is_enum_format_unsigned_int(internalFormat);
914 if (!is_unsigned_int) {
915 is_signed_int = _mesa_is_enum_format_signed_int(internalFormat);
916 }
917
918 switch (format) {
919 case GL_RGBA:
920 if (type == GL_FLOAT && data_type == GL_FLOAT)
921 return GL_NO_ERROR; /* EXT_color_buffer_float */
922 if (type == GL_UNSIGNED_BYTE && data_type == GL_UNSIGNED_NORMALIZED)
923 return GL_NO_ERROR;
924 if (internalFormat == GL_RGB10_A2 &&
925 type == GL_UNSIGNED_INT_2_10_10_10_REV)
926 return GL_NO_ERROR;
927 if (internalFormat == GL_RGB10_A2UI && type == GL_UNSIGNED_BYTE)
928 return GL_NO_ERROR;
929 break;
930 case GL_BGRA:
931 /* GL_EXT_read_format_bgra */
932 if (type == GL_UNSIGNED_BYTE ||
933 type == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
934 type == GL_UNSIGNED_SHORT_1_5_5_5_REV)
935 return GL_NO_ERROR;
936 break;
937 case GL_RGBA_INTEGER:
938 if ((is_signed_int && type == GL_INT) ||
939 (is_unsigned_int && type == GL_UNSIGNED_INT))
940 return GL_NO_ERROR;
941 break;
942 }
943
944 return GL_INVALID_OPERATION;
945 }
946
947
948 void GLAPIENTRY
949 _mesa_ReadnPixelsARB( GLint x, GLint y, GLsizei width, GLsizei height,
950 GLenum format, GLenum type, GLsizei bufSize,
951 GLvoid *pixels )
952 {
953 GLenum err = GL_NO_ERROR;
954 struct gl_renderbuffer *rb;
955
956 GET_CURRENT_CONTEXT(ctx);
957
958 FLUSH_VERTICES(ctx, 0);
959 FLUSH_CURRENT(ctx, 0);
960
961 if (MESA_VERBOSE & VERBOSE_API)
962 _mesa_debug(ctx, "glReadPixels(%d, %d, %s, %s, %p)\n",
963 width, height,
964 _mesa_lookup_enum_by_nr(format),
965 _mesa_lookup_enum_by_nr(type),
966 pixels);
967
968 if (width < 0 || height < 0) {
969 _mesa_error( ctx, GL_INVALID_VALUE,
970 "glReadPixels(width=%d height=%d)", width, height );
971 return;
972 }
973
974 if (ctx->NewState)
975 _mesa_update_state(ctx);
976
977 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
978 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
979 "glReadPixels(incomplete framebuffer)" );
980 return;
981 }
982
983 rb = _mesa_get_read_renderbuffer_for_format(ctx, format);
984 if (rb == NULL) {
985 _mesa_error(ctx, GL_INVALID_OPERATION,
986 "glReadPixels(read buffer)");
987 return;
988 }
989
990 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
991 * combinations of format and type that can be used.
992 *
993 * Technically, only two combinations are actually allowed:
994 * GL_RGBA/GL_UNSIGNED_BYTE, and some implementation-specific internal
995 * preferred combination. This code doesn't know what that preferred
996 * combination is, and Mesa can handle anything valid. Just work instead.
997 */
998 if (_mesa_is_gles(ctx)) {
999 if (ctx->API == API_OPENGLES2 &&
1000 _mesa_is_color_format(format) &&
1001 _mesa_get_color_read_format(ctx) == format &&
1002 _mesa_get_color_read_type(ctx) == type) {
1003 err = GL_NO_ERROR;
1004 } else if (ctx->Version < 30) {
1005 err = _mesa_es_error_check_format_and_type(format, type, 2);
1006 if (err == GL_NO_ERROR) {
1007 if (type == GL_FLOAT || type == GL_HALF_FLOAT_OES) {
1008 err = GL_INVALID_OPERATION;
1009 }
1010 }
1011 } else {
1012 err = read_pixels_es3_error_check(format, type, rb);
1013 }
1014
1015 if (err == GL_NO_ERROR && (format == GL_DEPTH_COMPONENT
1016 || format == GL_DEPTH_STENCIL)) {
1017 err = GL_INVALID_ENUM;
1018 }
1019
1020 if (err != GL_NO_ERROR) {
1021 _mesa_error(ctx, err, "glReadPixels(invalid format %s and/or type %s)",
1022 _mesa_lookup_enum_by_nr(format),
1023 _mesa_lookup_enum_by_nr(type));
1024 return;
1025 }
1026 }
1027
1028 err = _mesa_error_check_format_and_type(ctx, format, type);
1029 if (err != GL_NO_ERROR) {
1030 _mesa_error(ctx, err, "glReadPixels(invalid format %s and/or type %s)",
1031 _mesa_lookup_enum_by_nr(format),
1032 _mesa_lookup_enum_by_nr(type));
1033 return;
1034 }
1035
1036 if (_mesa_is_user_fbo(ctx->ReadBuffer) &&
1037 ctx->ReadBuffer->Visual.samples > 0) {
1038 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(multisample FBO)");
1039 return;
1040 }
1041
1042 if (!_mesa_source_buffer_exists(ctx, format)) {
1043 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(no readbuffer)");
1044 return;
1045 }
1046
1047 /* Check that the destination format and source buffer are both
1048 * integer-valued or both non-integer-valued.
1049 */
1050 if (ctx->Extensions.EXT_texture_integer && _mesa_is_color_format(format)) {
1051 const struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
1052 const GLboolean srcInteger = _mesa_is_format_integer_color(rb->Format);
1053 const GLboolean dstInteger = _mesa_is_enum_format_integer(format);
1054 if (dstInteger != srcInteger) {
1055 _mesa_error(ctx, GL_INVALID_OPERATION,
1056 "glReadPixels(integer / non-integer format mismatch");
1057 return;
1058 }
1059 }
1060
1061 if (width == 0 || height == 0)
1062 return; /* nothing to do */
1063
1064 if (!_mesa_validate_pbo_access(2, &ctx->Pack, width, height, 1,
1065 format, type, bufSize, pixels)) {
1066 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
1067 _mesa_error(ctx, GL_INVALID_OPERATION,
1068 "glReadPixels(out of bounds PBO access)");
1069 } else {
1070 _mesa_error(ctx, GL_INVALID_OPERATION,
1071 "glReadnPixelsARB(out of bounds access:"
1072 " bufSize (%d) is too small)", bufSize);
1073 }
1074 return;
1075 }
1076
1077 if (_mesa_is_bufferobj(ctx->Pack.BufferObj) &&
1078 _mesa_check_disallowed_mapping(ctx->Pack.BufferObj)) {
1079 /* buffer is mapped - that's an error */
1080 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(PBO is mapped)");
1081 return;
1082 }
1083
1084 ctx->Driver.ReadPixels(ctx, x, y, width, height,
1085 format, type, &ctx->Pack, pixels);
1086 }
1087
1088 void GLAPIENTRY
1089 _mesa_ReadPixels( GLint x, GLint y, GLsizei width, GLsizei height,
1090 GLenum format, GLenum type, GLvoid *pixels )
1091 {
1092 _mesa_ReadnPixelsARB(x, y, width, height, format, type, INT_MAX, pixels);
1093 }