0b41de622241e28a83203655056feea66fe2d597
[mesa.git] / src / mesa / main / readpix.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "glheader.h"
26 #include "imports.h"
27 #include "bufferobj.h"
28 #include "context.h"
29 #include "enums.h"
30 #include "readpix.h"
31 #include "framebuffer.h"
32 #include "formats.h"
33 #include "format_unpack.h"
34 #include "image.h"
35 #include "mtypes.h"
36 #include "pack.h"
37 #include "pbo.h"
38 #include "state.h"
39
40
41 /**
42 * Tries to implement glReadPixels() of GL_DEPTH_COMPONENT using memcpy of the
43 * mapping.
44 */
45 static GLboolean
46 fast_read_depth_pixels( struct gl_context *ctx,
47 GLint x, GLint y,
48 GLsizei width, GLsizei height,
49 GLenum type, GLvoid *pixels,
50 const struct gl_pixelstore_attrib *packing )
51 {
52 struct gl_framebuffer *fb = ctx->ReadBuffer;
53 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
54 GLubyte *map, *dst;
55 int stride, dstStride, j;
56
57 if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0)
58 return GL_FALSE;
59
60 if (packing->SwapBytes)
61 return GL_FALSE;
62
63 if (_mesa_get_format_datatype(rb->Format) != GL_UNSIGNED_INT)
64 return GL_FALSE;
65
66 if (!((type == GL_UNSIGNED_SHORT && rb->Format == MESA_FORMAT_Z16) ||
67 type == GL_UNSIGNED_INT))
68 return GL_FALSE;
69
70 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
71 &map, &stride);
72
73 dstStride = _mesa_image_row_stride(packing, width, GL_DEPTH_COMPONENT, type);
74 dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
75 GL_DEPTH_COMPONENT, type, 0, 0);
76
77 for (j = 0; j < height; j++) {
78 if (type == GL_UNSIGNED_INT) {
79 _mesa_unpack_uint_z_row(rb->Format, width, map, (GLuint *)dst);
80 } else {
81 ASSERT(type == GL_UNSIGNED_SHORT && rb->Format == MESA_FORMAT_Z16);
82 memcpy(dst, map, width * 2);
83 }
84
85 map += stride;
86 dst += dstStride;
87 }
88 ctx->Driver.UnmapRenderbuffer(ctx, rb);
89
90 return GL_TRUE;
91 }
92
93 /**
94 * Read pixels for format=GL_DEPTH_COMPONENT.
95 */
96 static void
97 read_depth_pixels( struct gl_context *ctx,
98 GLint x, GLint y,
99 GLsizei width, GLsizei height,
100 GLenum type, GLvoid *pixels,
101 const struct gl_pixelstore_attrib *packing )
102 {
103 struct gl_framebuffer *fb = ctx->ReadBuffer;
104 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
105 GLint j;
106 GLubyte *dst, *map;
107 int dstStride, stride;
108
109 if (!rb)
110 return;
111
112 /* clipping should have been done already */
113 ASSERT(x >= 0);
114 ASSERT(y >= 0);
115 ASSERT(x + width <= (GLint) rb->Width);
116 ASSERT(y + height <= (GLint) rb->Height);
117 /* width should never be > MAX_WIDTH since we did clipping earlier */
118 ASSERT(width <= MAX_WIDTH);
119
120 if (fast_read_depth_pixels(ctx, x, y, width, height, type, pixels, packing))
121 return;
122
123 dstStride = _mesa_image_row_stride(packing, width, GL_DEPTH_COMPONENT, type);
124 dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
125 GL_DEPTH_COMPONENT, type, 0, 0);
126
127 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
128 &map, &stride);
129
130 /* General case (slower) */
131 for (j = 0; j < height; j++, y++) {
132 GLfloat depthValues[MAX_WIDTH];
133 _mesa_unpack_float_z_row(rb->Format, width, map, depthValues);
134 _mesa_pack_depth_span(ctx, width, dst, type, depthValues, packing);
135
136 dst += dstStride;
137 map += stride;
138 }
139
140 ctx->Driver.UnmapRenderbuffer(ctx, rb);
141 }
142
143
144 /**
145 * Read pixels for format=GL_STENCIL_INDEX.
146 */
147 static void
148 read_stencil_pixels( struct gl_context *ctx,
149 GLint x, GLint y,
150 GLsizei width, GLsizei height,
151 GLenum type, GLvoid *pixels,
152 const struct gl_pixelstore_attrib *packing )
153 {
154 struct gl_framebuffer *fb = ctx->ReadBuffer;
155 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
156 GLint j;
157 GLubyte *map;
158 GLint stride;
159
160 if (!rb)
161 return;
162
163 /* width should never be > MAX_WIDTH since we did clipping earlier */
164 ASSERT(width <= MAX_WIDTH);
165
166 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
167 &map, &stride);
168
169 /* process image row by row */
170 for (j = 0; j < height; j++) {
171 GLvoid *dest;
172 GLubyte stencil[MAX_WIDTH];
173
174 _mesa_unpack_ubyte_stencil_row(rb->Format, width, map, stencil);
175 dest = _mesa_image_address2d(packing, pixels, width, height,
176 GL_STENCIL_INDEX, type, j, 0);
177
178 _mesa_pack_stencil_span(ctx, width, type, dest, stencil, packing);
179
180 map += stride;
181 }
182
183 ctx->Driver.UnmapRenderbuffer(ctx, rb);
184 }
185
186 static GLboolean
187 fast_read_rgba_pixels_memcpy( struct gl_context *ctx,
188 GLint x, GLint y,
189 GLsizei width, GLsizei height,
190 GLenum format, GLenum type,
191 GLvoid *pixels,
192 const struct gl_pixelstore_attrib *packing,
193 GLbitfield transferOps )
194 {
195 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
196 GLubyte *dst, *map;
197 int dstStride, stride, j, texelBytes;
198
199 if (!_mesa_format_matches_format_and_type(rb->Format, format, type))
200 return GL_FALSE;
201
202 /* check for things we can't handle here */
203 if (packing->SwapBytes ||
204 packing->LsbFirst) {
205 return GL_FALSE;
206 }
207
208 dstStride = _mesa_image_row_stride(packing, width, format, type);
209 dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
210 format, type, 0, 0);
211
212 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
213 &map, &stride);
214
215 texelBytes = _mesa_get_format_bytes(rb->Format);
216 for (j = 0; j < height; j++) {
217 memcpy(dst, map, width * texelBytes);
218 dst += dstStride;
219 map += stride;
220 }
221
222 ctx->Driver.UnmapRenderbuffer(ctx, rb);
223
224 return GL_TRUE;
225 }
226
227 static GLboolean
228 slow_read_rgba_pixels( struct gl_context *ctx,
229 GLint x, GLint y,
230 GLsizei width, GLsizei height,
231 GLenum format, GLenum type,
232 GLvoid *pixels,
233 const struct gl_pixelstore_attrib *packing,
234 GLbitfield transferOps )
235 {
236 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
237 const gl_format rbFormat = _mesa_get_srgb_format_linear(rb->Format);
238 union {
239 float f[MAX_WIDTH][4];
240 unsigned int i[MAX_WIDTH][4];
241 } rgba;
242 GLubyte *dst, *map;
243 int dstStride, stride, j;
244
245 dstStride = _mesa_image_row_stride(packing, width, format, type);
246 dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
247 format, type, 0, 0);
248
249 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
250 &map, &stride);
251
252 for (j = 0; j < height; j++) {
253 if (_mesa_is_integer_format(format)) {
254 _mesa_unpack_int_rgba_row(rbFormat, width, map, rgba.i);
255 _mesa_pack_rgba_span_int(ctx, width, rgba.i, format, type, dst);
256 } else {
257 _mesa_unpack_rgba_row(rbFormat, width, map, rgba.f);
258 _mesa_pack_rgba_span_float(ctx, width, rgba.f, format, type, dst,
259 packing, transferOps);
260 }
261 dst += dstStride;
262 map += stride;
263 }
264
265 ctx->Driver.UnmapRenderbuffer(ctx, rb);
266
267 return GL_TRUE;
268 }
269
270 /*
271 * Read R, G, B, A, RGB, L, or LA pixels.
272 */
273 static void
274 read_rgba_pixels( struct gl_context *ctx,
275 GLint x, GLint y,
276 GLsizei width, GLsizei height,
277 GLenum format, GLenum type, GLvoid *pixels,
278 const struct gl_pixelstore_attrib *packing )
279 {
280 GLbitfield transferOps = ctx->_ImageTransferState;
281 struct gl_framebuffer *fb = ctx->ReadBuffer;
282 struct gl_renderbuffer *rb = fb->_ColorReadBuffer;
283
284 if (!rb)
285 return;
286
287 if ((ctx->Color._ClampReadColor == GL_TRUE || type != GL_FLOAT) &&
288 !_mesa_is_integer_format(format)) {
289 transferOps |= IMAGE_CLAMP_BIT;
290 }
291
292 if (!transferOps) {
293 /* Try the optimized paths first. */
294 if (fast_read_rgba_pixels_memcpy(ctx, x, y, width, height,
295 format, type, pixels, packing,
296 transferOps)) {
297 return;
298 }
299 }
300
301 slow_read_rgba_pixels(ctx, x, y, width, height,
302 format, type, pixels, packing, transferOps);
303 }
304
305 /**
306 * For a packed depth/stencil buffer being read as depth/stencil, just memcpy the
307 * data (possibly swapping 8/24 vs 24/8 as we go).
308 */
309 static GLboolean
310 fast_read_depth_stencil_pixels(struct gl_context *ctx,
311 GLint x, GLint y,
312 GLsizei width, GLsizei height,
313 GLubyte *dst, int dstStride)
314 {
315 struct gl_framebuffer *fb = ctx->ReadBuffer;
316 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
317 struct gl_renderbuffer *stencilRb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
318 GLubyte *map;
319 int stride, i;
320
321 if (rb != stencilRb)
322 return GL_FALSE;
323
324 if (rb->Format != MESA_FORMAT_Z24_S8 &&
325 rb->Format != MESA_FORMAT_S8_Z24)
326 return GL_FALSE;
327
328 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
329 &map, &stride);
330
331 for (i = 0; i < height; i++) {
332 _mesa_unpack_uint_24_8_depth_stencil_row(rb->Format, width,
333 map, (GLuint *)dst);
334 map += stride;
335 dst += dstStride;
336 }
337
338 ctx->Driver.UnmapRenderbuffer(ctx, rb);
339
340 return GL_TRUE;
341 }
342
343
344 /**
345 * For non-float-depth and stencil buffers being read as 24/8 depth/stencil,
346 * copy the integer data directly instead of converting depth to float and
347 * re-packing.
348 */
349 static GLboolean
350 fast_read_depth_stencil_pixels_separate(struct gl_context *ctx,
351 GLint x, GLint y,
352 GLsizei width, GLsizei height,
353 uint32_t *dst, int dstStride)
354 {
355 struct gl_framebuffer *fb = ctx->ReadBuffer;
356 struct gl_renderbuffer *depthRb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
357 struct gl_renderbuffer *stencilRb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
358 GLubyte *depthMap, *stencilMap;
359 int depthStride, stencilStride, i, j;
360
361 if (_mesa_get_format_datatype(depthRb->Format) != GL_UNSIGNED_INT)
362 return GL_FALSE;
363
364 ctx->Driver.MapRenderbuffer(ctx, depthRb, x, y, width, height,
365 GL_MAP_READ_BIT, &depthMap, &depthStride);
366 ctx->Driver.MapRenderbuffer(ctx, stencilRb, x, y, width, height,
367 GL_MAP_READ_BIT, &stencilMap, &stencilStride);
368
369 for (j = 0; j < height; j++) {
370 GLubyte stencilVals[MAX_WIDTH];
371
372 _mesa_unpack_uint_z_row(depthRb->Format, width, depthMap, dst);
373 _mesa_unpack_ubyte_stencil_row(stencilRb->Format, width,
374 stencilMap, stencilVals);
375
376 for (i = 0; i < width; i++) {
377 dst[i] = (dst[i] & 0xffffff00) | stencilVals[i];
378 }
379
380 depthMap += depthStride;
381 stencilMap += stencilStride;
382 dst += dstStride / 4;
383 }
384
385 ctx->Driver.UnmapRenderbuffer(ctx, depthRb);
386 ctx->Driver.UnmapRenderbuffer(ctx, stencilRb);
387
388 return GL_TRUE;
389 }
390
391 static void
392 slow_read_depth_stencil_pixels_separate(struct gl_context *ctx,
393 GLint x, GLint y,
394 GLsizei width, GLsizei height,
395 GLenum type,
396 const struct gl_pixelstore_attrib *packing,
397 GLubyte *dst, int dstStride)
398 {
399 struct gl_framebuffer *fb = ctx->ReadBuffer;
400 struct gl_renderbuffer *depthRb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
401 struct gl_renderbuffer *stencilRb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
402 GLubyte *depthMap, *stencilMap;
403 int depthStride, stencilStride, j;
404
405 /* The depth and stencil buffers might be separate, or a single buffer.
406 * If one buffer, only map it once.
407 */
408 ctx->Driver.MapRenderbuffer(ctx, depthRb, x, y, width, height,
409 GL_MAP_READ_BIT, &depthMap, &depthStride);
410 if (stencilRb != depthRb) {
411 ctx->Driver.MapRenderbuffer(ctx, stencilRb, x, y, width, height,
412 GL_MAP_READ_BIT, &stencilMap,
413 &stencilStride);
414 }
415
416 for (j = 0; j < height; j++) {
417 GLubyte stencilVals[MAX_WIDTH];
418 GLfloat depthVals[MAX_WIDTH];
419
420 _mesa_unpack_float_z_row(depthRb->Format, width, depthMap, depthVals);
421 _mesa_unpack_ubyte_stencil_row(stencilRb->Format, width,
422 stencilMap, stencilVals);
423
424 _mesa_pack_depth_stencil_span(ctx, width, type, (GLuint *)dst,
425 depthVals, stencilVals, packing);
426
427 depthMap += depthStride;
428 stencilMap += stencilStride;
429 dst += dstStride;
430 }
431
432 ctx->Driver.UnmapRenderbuffer(ctx, depthRb);
433 if (stencilRb != depthRb) {
434 ctx->Driver.UnmapRenderbuffer(ctx, stencilRb);
435 }
436 }
437
438
439 /**
440 * Read combined depth/stencil values.
441 * We'll have already done error checking to be sure the expected
442 * depth and stencil buffers really exist.
443 */
444 static void
445 read_depth_stencil_pixels(struct gl_context *ctx,
446 GLint x, GLint y,
447 GLsizei width, GLsizei height,
448 GLenum type, GLvoid *pixels,
449 const struct gl_pixelstore_attrib *packing )
450 {
451 const GLboolean scaleOrBias
452 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
453 const GLboolean stencilTransfer = ctx->Pixel.IndexShift
454 || ctx->Pixel.IndexOffset || ctx->Pixel.MapStencilFlag;
455 GLubyte *dst;
456 int dstStride;
457
458 dst = (GLubyte *) _mesa_image_address2d(packing, pixels,
459 width, height,
460 GL_DEPTH_STENCIL_EXT,
461 type, 0, 0);
462 dstStride = _mesa_image_row_stride(packing, width,
463 GL_DEPTH_STENCIL_EXT, type);
464
465 /* Fast 24/8 reads. */
466 if (type == GL_UNSIGNED_INT_24_8 &&
467 !scaleOrBias && !stencilTransfer && !packing->SwapBytes) {
468 if (fast_read_depth_stencil_pixels(ctx, x, y, width, height,
469 dst, dstStride))
470 return;
471
472 if (fast_read_depth_stencil_pixels_separate(ctx, x, y, width, height,
473 (uint32_t *)dst, dstStride))
474 return;
475 }
476
477 slow_read_depth_stencil_pixels_separate(ctx, x, y, width, height,
478 type, packing,
479 dst, dstStride);
480 }
481
482
483
484 /**
485 * Software fallback routine for ctx->Driver.ReadPixels().
486 * By time we get here, all error checking will have been done.
487 */
488 void
489 _mesa_readpixels(struct gl_context *ctx,
490 GLint x, GLint y, GLsizei width, GLsizei height,
491 GLenum format, GLenum type,
492 const struct gl_pixelstore_attrib *packing,
493 GLvoid *pixels)
494 {
495 struct gl_pixelstore_attrib clippedPacking = *packing;
496
497 if (ctx->NewState)
498 _mesa_update_state(ctx);
499
500 /* Do all needed clipping here, so that we can forget about it later */
501 if (_mesa_clip_readpixels(ctx, &x, &y, &width, &height, &clippedPacking)) {
502
503 pixels = _mesa_map_pbo_dest(ctx, &clippedPacking, pixels);
504
505 if (pixels) {
506 switch (format) {
507 case GL_STENCIL_INDEX:
508 read_stencil_pixels(ctx, x, y, width, height, type, pixels,
509 &clippedPacking);
510 break;
511 case GL_DEPTH_COMPONENT:
512 read_depth_pixels(ctx, x, y, width, height, type, pixels,
513 &clippedPacking);
514 break;
515 case GL_DEPTH_STENCIL_EXT:
516 read_depth_stencil_pixels(ctx, x, y, width, height, type, pixels,
517 &clippedPacking);
518 break;
519 default:
520 /* all other formats should be color formats */
521 read_rgba_pixels(ctx, x, y, width, height, format, type, pixels,
522 &clippedPacking);
523 }
524
525 _mesa_unmap_pbo_dest(ctx, &clippedPacking);
526 }
527 }
528 }
529
530
531 /**
532 * Do error checking of the format/type parameters to glReadPixels and
533 * glDrawPixels.
534 * \param drawing if GL_TRUE do checking for DrawPixels, else do checking
535 * for ReadPixels.
536 * \return GL_TRUE if error detected, GL_FALSE if no errors
537 */
538 GLboolean
539 _mesa_error_check_format_type(struct gl_context *ctx, GLenum format,
540 GLenum type, GLboolean drawing)
541 {
542 const char *readDraw = drawing ? "Draw" : "Read";
543 const GLboolean reading = !drawing;
544
545 /* state validation should have already been done */
546 ASSERT(ctx->NewState == 0x0);
547
548 if (ctx->Extensions.EXT_packed_depth_stencil
549 && type == GL_UNSIGNED_INT_24_8_EXT
550 && format != GL_DEPTH_STENCIL_EXT) {
551 _mesa_error(ctx, GL_INVALID_OPERATION,
552 "gl%sPixels(format is not GL_DEPTH_STENCIL_EXT)", readDraw);
553 return GL_TRUE;
554 }
555
556 if (ctx->Extensions.ARB_depth_buffer_float
557 && type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV
558 && format != GL_DEPTH_STENCIL_EXT) {
559 _mesa_error(ctx, GL_INVALID_OPERATION,
560 "gl%sPixels(format is not GL_DEPTH_STENCIL_EXT)", readDraw);
561 return GL_TRUE;
562 }
563
564 /* basic combinations test */
565 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
566 _mesa_error(ctx, GL_INVALID_ENUM,
567 "gl%sPixels(format or type)", readDraw);
568 return GL_TRUE;
569 }
570
571 /* additional checks */
572 switch (format) {
573 case GL_RG:
574 case GL_RED:
575 case GL_GREEN:
576 case GL_BLUE:
577 case GL_ALPHA:
578 case GL_LUMINANCE:
579 case GL_LUMINANCE_ALPHA:
580 case GL_RGB:
581 case GL_BGR:
582 case GL_RGBA:
583 case GL_BGRA:
584 case GL_ABGR_EXT:
585 case GL_RED_INTEGER_EXT:
586 case GL_GREEN_INTEGER_EXT:
587 case GL_BLUE_INTEGER_EXT:
588 case GL_ALPHA_INTEGER_EXT:
589 case GL_RGB_INTEGER_EXT:
590 case GL_RGBA_INTEGER_EXT:
591 case GL_BGR_INTEGER_EXT:
592 case GL_BGRA_INTEGER_EXT:
593 case GL_LUMINANCE_INTEGER_EXT:
594 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
595 if (!drawing) {
596 /* reading */
597 if (!_mesa_source_buffer_exists(ctx, GL_COLOR)) {
598 _mesa_error(ctx, GL_INVALID_OPERATION,
599 "glReadPixels(no color buffer)");
600 return GL_TRUE;
601 }
602 }
603 break;
604 case GL_COLOR_INDEX:
605 if (drawing) {
606 if (ctx->PixelMaps.ItoR.Size == 0 ||
607 ctx->PixelMaps.ItoG.Size == 0 ||
608 ctx->PixelMaps.ItoB.Size == 0) {
609 _mesa_error(ctx, GL_INVALID_OPERATION,
610 "glDrawPixels(drawing color index pixels into RGB buffer)");
611 return GL_TRUE;
612 }
613 }
614 else {
615 /* reading */
616 if (!_mesa_source_buffer_exists(ctx, GL_COLOR)) {
617 _mesa_error(ctx, GL_INVALID_OPERATION,
618 "glReadPixels(no color buffer)");
619 return GL_TRUE;
620 }
621 /* We no longer support CI-mode color buffers so trying to read
622 * GL_COLOR_INDEX pixels is always an error.
623 */
624 _mesa_error(ctx, GL_INVALID_OPERATION,
625 "glReadPixels(color buffer is RGB)");
626 return GL_TRUE;
627 }
628 break;
629 case GL_STENCIL_INDEX:
630 if ((drawing && !_mesa_dest_buffer_exists(ctx, format)) ||
631 (reading && !_mesa_source_buffer_exists(ctx, format))) {
632 _mesa_error(ctx, GL_INVALID_OPERATION,
633 "gl%sPixels(no stencil buffer)", readDraw);
634 return GL_TRUE;
635 }
636 break;
637 case GL_DEPTH_COMPONENT:
638 if ((drawing && !_mesa_dest_buffer_exists(ctx, format))) {
639 _mesa_error(ctx, GL_INVALID_OPERATION,
640 "gl%sPixels(no depth buffer)", readDraw);
641 return GL_TRUE;
642 }
643 break;
644 case GL_DEPTH_STENCIL_EXT:
645 /* Check validity of the type first. */
646 switch (type) {
647 case GL_UNSIGNED_INT_24_8_EXT:
648 if (!ctx->Extensions.EXT_packed_depth_stencil) {
649 _mesa_error(ctx, GL_INVALID_ENUM, "gl%sPixels(type)", readDraw);
650 return GL_TRUE;
651 }
652 break;
653 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
654 if (!ctx->Extensions.ARB_depth_buffer_float) {
655 _mesa_error(ctx, GL_INVALID_ENUM, "gl%sPixels(type)", readDraw);
656 return GL_TRUE;
657 }
658 break;
659 default:
660 _mesa_error(ctx, GL_INVALID_ENUM, "gl%sPixels(type)", readDraw);
661 return GL_TRUE;
662 }
663 if ((drawing && !_mesa_dest_buffer_exists(ctx, format)) ||
664 (reading && !_mesa_source_buffer_exists(ctx, format))) {
665 _mesa_error(ctx, GL_INVALID_OPERATION,
666 "gl%sPixels(no depth or stencil buffer)", readDraw);
667 return GL_TRUE;
668 }
669 break;
670 default:
671 /* this should have been caught in _mesa_is_legal_format_type() */
672 _mesa_problem(ctx, "unexpected format in _mesa_%sPixels", readDraw);
673 return GL_TRUE;
674 }
675
676 /* no errors */
677 return GL_FALSE;
678 }
679
680
681
682 void GLAPIENTRY
683 _mesa_ReadnPixelsARB( GLint x, GLint y, GLsizei width, GLsizei height,
684 GLenum format, GLenum type, GLsizei bufSize,
685 GLvoid *pixels )
686 {
687 GET_CURRENT_CONTEXT(ctx);
688 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
689
690 FLUSH_CURRENT(ctx, 0);
691
692 if (MESA_VERBOSE & VERBOSE_API)
693 _mesa_debug(ctx, "glReadPixels(%d, %d, %s, %s, %p)\n",
694 width, height,
695 _mesa_lookup_enum_by_nr(format),
696 _mesa_lookup_enum_by_nr(type),
697 pixels);
698
699 if (width < 0 || height < 0) {
700 _mesa_error( ctx, GL_INVALID_VALUE,
701 "glReadPixels(width=%d height=%d)", width, height );
702 return;
703 }
704
705 if (ctx->NewState)
706 _mesa_update_state(ctx);
707
708 if (_mesa_error_check_format_type(ctx, format, type, GL_FALSE)) {
709 /* found an error */
710 return;
711 }
712
713 /* Check that the destination format and source buffer are both
714 * integer-valued or both non-integer-valued.
715 */
716 if (ctx->Extensions.EXT_texture_integer && _mesa_is_color_format(format)) {
717 const struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
718 const GLboolean srcInteger = _mesa_is_format_integer_color(rb->Format);
719 const GLboolean dstInteger = _mesa_is_integer_format(format);
720 if (dstInteger != srcInteger) {
721 _mesa_error(ctx, GL_INVALID_OPERATION,
722 "glReadPixels(integer / non-integer format mismatch");
723 return;
724 }
725 }
726
727 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
728 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
729 "glReadPixels(incomplete framebuffer)" );
730 return;
731 }
732
733 if (!_mesa_source_buffer_exists(ctx, format)) {
734 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(no readbuffer)");
735 return;
736 }
737
738 if (width == 0 || height == 0)
739 return; /* nothing to do */
740
741 if (!_mesa_validate_pbo_access(2, &ctx->Pack, width, height, 1,
742 format, type, bufSize, pixels)) {
743 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
744 _mesa_error(ctx, GL_INVALID_OPERATION,
745 "glReadPixels(out of bounds PBO access)");
746 } else {
747 _mesa_error(ctx, GL_INVALID_OPERATION,
748 "glReadnPixelsARB(out of bounds access:"
749 " bufSize (%d) is too small)", bufSize);
750 }
751 return;
752 }
753
754 if (_mesa_is_bufferobj(ctx->Pack.BufferObj) &&
755 _mesa_bufferobj_mapped(ctx->Pack.BufferObj)) {
756 /* buffer is mapped - that's an error */
757 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(PBO is mapped)");
758 return;
759 }
760
761 ctx->Driver.ReadPixels(ctx, x, y, width, height,
762 format, type, &ctx->Pack, pixels);
763 }
764
765 void GLAPIENTRY
766 _mesa_ReadPixels( GLint x, GLint y, GLsizei width, GLsizei height,
767 GLenum format, GLenum type, GLvoid *pixels )
768 {
769 _mesa_ReadnPixelsARB(x, y, width, height, format, type, INT_MAX, pixels);
770 }