84b5224c8583ba2d8124a3e7a2a48fe67531b119
[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_NORMALIZED)
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 if (!map) {
74 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
75 return GL_TRUE; /* don't bother trying the slow path */
76 }
77
78 dstStride = _mesa_image_row_stride(packing, width, GL_DEPTH_COMPONENT, type);
79 dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
80 GL_DEPTH_COMPONENT, type, 0, 0);
81
82 for (j = 0; j < height; j++) {
83 if (type == GL_UNSIGNED_INT) {
84 _mesa_unpack_uint_z_row(rb->Format, width, map, (GLuint *)dst);
85 } else {
86 ASSERT(type == GL_UNSIGNED_SHORT && rb->Format == MESA_FORMAT_Z16);
87 memcpy(dst, map, width * 2);
88 }
89
90 map += stride;
91 dst += dstStride;
92 }
93 ctx->Driver.UnmapRenderbuffer(ctx, rb);
94
95 return GL_TRUE;
96 }
97
98 /**
99 * Read pixels for format=GL_DEPTH_COMPONENT.
100 */
101 static void
102 read_depth_pixels( struct gl_context *ctx,
103 GLint x, GLint y,
104 GLsizei width, GLsizei height,
105 GLenum type, GLvoid *pixels,
106 const struct gl_pixelstore_attrib *packing )
107 {
108 struct gl_framebuffer *fb = ctx->ReadBuffer;
109 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
110 GLint j;
111 GLubyte *dst, *map;
112 int dstStride, stride;
113
114 if (!rb)
115 return;
116
117 /* clipping should have been done already */
118 ASSERT(x >= 0);
119 ASSERT(y >= 0);
120 ASSERT(x + width <= (GLint) rb->Width);
121 ASSERT(y + height <= (GLint) rb->Height);
122 /* width should never be > MAX_WIDTH since we did clipping earlier */
123 ASSERT(width <= MAX_WIDTH);
124
125 if (fast_read_depth_pixels(ctx, x, y, width, height, type, pixels, packing))
126 return;
127
128 dstStride = _mesa_image_row_stride(packing, width, GL_DEPTH_COMPONENT, type);
129 dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
130 GL_DEPTH_COMPONENT, type, 0, 0);
131
132 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
133 &map, &stride);
134 if (!map) {
135 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
136 return;
137 }
138
139 /* General case (slower) */
140 for (j = 0; j < height; j++, y++) {
141 GLfloat depthValues[MAX_WIDTH];
142 _mesa_unpack_float_z_row(rb->Format, width, map, depthValues);
143 _mesa_pack_depth_span(ctx, width, dst, type, depthValues, packing);
144
145 dst += dstStride;
146 map += stride;
147 }
148
149 ctx->Driver.UnmapRenderbuffer(ctx, rb);
150 }
151
152
153 /**
154 * Read pixels for format=GL_STENCIL_INDEX.
155 */
156 static void
157 read_stencil_pixels( struct gl_context *ctx,
158 GLint x, GLint y,
159 GLsizei width, GLsizei height,
160 GLenum type, GLvoid *pixels,
161 const struct gl_pixelstore_attrib *packing )
162 {
163 struct gl_framebuffer *fb = ctx->ReadBuffer;
164 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
165 GLint j;
166 GLubyte *map;
167 GLint stride;
168
169 if (!rb)
170 return;
171
172 /* width should never be > MAX_WIDTH since we did clipping earlier */
173 ASSERT(width <= MAX_WIDTH);
174
175 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
176 &map, &stride);
177 if (!map) {
178 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
179 return;
180 }
181
182 /* process image row by row */
183 for (j = 0; j < height; j++) {
184 GLvoid *dest;
185 GLubyte stencil[MAX_WIDTH];
186
187 _mesa_unpack_ubyte_stencil_row(rb->Format, width, map, stencil);
188 dest = _mesa_image_address2d(packing, pixels, width, height,
189 GL_STENCIL_INDEX, type, j, 0);
190
191 _mesa_pack_stencil_span(ctx, width, type, dest, stencil, packing);
192
193 map += stride;
194 }
195
196 ctx->Driver.UnmapRenderbuffer(ctx, rb);
197 }
198
199 static GLboolean
200 fast_read_rgba_pixels_memcpy( struct gl_context *ctx,
201 GLint x, GLint y,
202 GLsizei width, GLsizei height,
203 GLenum format, GLenum type,
204 GLvoid *pixels,
205 const struct gl_pixelstore_attrib *packing,
206 GLbitfield transferOps )
207 {
208 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
209 GLubyte *dst, *map;
210 int dstStride, stride, j, texelBytes;
211
212 if (!_mesa_format_matches_format_and_type(rb->Format, format, type))
213 return GL_FALSE;
214
215 /* check for things we can't handle here */
216 if (packing->SwapBytes) {
217 return GL_FALSE;
218 }
219
220 dstStride = _mesa_image_row_stride(packing, width, format, type);
221 dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
222 format, type, 0, 0);
223
224 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
225 &map, &stride);
226 if (!map) {
227 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
228 return GL_TRUE; /* don't bother trying the slow path */
229 }
230
231 texelBytes = _mesa_get_format_bytes(rb->Format);
232 for (j = 0; j < height; j++) {
233 memcpy(dst, map, width * texelBytes);
234 dst += dstStride;
235 map += stride;
236 }
237
238 ctx->Driver.UnmapRenderbuffer(ctx, rb);
239
240 return GL_TRUE;
241 }
242
243 static void
244 slow_read_rgba_pixels( struct gl_context *ctx,
245 GLint x, GLint y,
246 GLsizei width, GLsizei height,
247 GLenum format, GLenum type,
248 GLvoid *pixels,
249 const struct gl_pixelstore_attrib *packing,
250 GLbitfield transferOps )
251 {
252 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
253 const gl_format rbFormat = _mesa_get_srgb_format_linear(rb->Format);
254 void *rgba;
255 GLubyte *dst, *map;
256 int dstStride, stride, j;
257
258 dstStride = _mesa_image_row_stride(packing, width, format, type);
259 dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
260 format, type, 0, 0);
261
262 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
263 &map, &stride);
264 if (!map) {
265 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
266 return;
267 }
268
269 rgba = malloc(width * MAX_PIXEL_BYTES);
270 if (!rgba)
271 goto done;
272
273 for (j = 0; j < height; j++) {
274 if (_mesa_is_integer_format(format)) {
275 _mesa_unpack_uint_rgba_row(rbFormat, width, map, (GLuint (*)[4]) rgba);
276 _mesa_pack_rgba_span_int(ctx, width, (GLuint (*)[4]) rgba, format,
277 type, dst);
278 } else {
279 _mesa_unpack_rgba_row(rbFormat, width, map, (GLfloat (*)[4]) rgba);
280 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
281 type, dst, packing, transferOps);
282 }
283 dst += dstStride;
284 map += stride;
285 }
286
287 free(rgba);
288
289 done:
290 ctx->Driver.UnmapRenderbuffer(ctx, rb);
291 }
292
293 /*
294 * Read R, G, B, A, RGB, L, or LA pixels.
295 */
296 static void
297 read_rgba_pixels( struct gl_context *ctx,
298 GLint x, GLint y,
299 GLsizei width, GLsizei height,
300 GLenum format, GLenum type, GLvoid *pixels,
301 const struct gl_pixelstore_attrib *packing )
302 {
303 GLbitfield transferOps = ctx->_ImageTransferState;
304 struct gl_framebuffer *fb = ctx->ReadBuffer;
305 struct gl_renderbuffer *rb = fb->_ColorReadBuffer;
306
307 if (!rb)
308 return;
309
310 if ((ctx->Color._ClampReadColor == GL_TRUE || type != GL_FLOAT) &&
311 !_mesa_is_integer_format(format)) {
312 transferOps |= IMAGE_CLAMP_BIT;
313 }
314
315 if (!transferOps) {
316 /* Try the optimized paths first. */
317 if (fast_read_rgba_pixels_memcpy(ctx, x, y, width, height,
318 format, type, pixels, packing,
319 transferOps)) {
320 return;
321 }
322 }
323
324 slow_read_rgba_pixels(ctx, x, y, width, height,
325 format, type, pixels, packing, transferOps);
326 }
327
328 /**
329 * For a packed depth/stencil buffer being read as depth/stencil, just memcpy the
330 * data (possibly swapping 8/24 vs 24/8 as we go).
331 */
332 static GLboolean
333 fast_read_depth_stencil_pixels(struct gl_context *ctx,
334 GLint x, GLint y,
335 GLsizei width, GLsizei height,
336 GLubyte *dst, int dstStride)
337 {
338 struct gl_framebuffer *fb = ctx->ReadBuffer;
339 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
340 struct gl_renderbuffer *stencilRb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
341 GLubyte *map;
342 int stride, i;
343
344 if (rb != stencilRb)
345 return GL_FALSE;
346
347 if (rb->Format != MESA_FORMAT_Z24_S8 &&
348 rb->Format != MESA_FORMAT_S8_Z24)
349 return GL_FALSE;
350
351 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
352 &map, &stride);
353 if (!map) {
354 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
355 return GL_TRUE; /* don't bother trying the slow path */
356 }
357
358 for (i = 0; i < height; i++) {
359 _mesa_unpack_uint_24_8_depth_stencil_row(rb->Format, width,
360 map, (GLuint *)dst);
361 map += stride;
362 dst += dstStride;
363 }
364
365 ctx->Driver.UnmapRenderbuffer(ctx, rb);
366
367 return GL_TRUE;
368 }
369
370
371 /**
372 * For non-float-depth and stencil buffers being read as 24/8 depth/stencil,
373 * copy the integer data directly instead of converting depth to float and
374 * re-packing.
375 */
376 static GLboolean
377 fast_read_depth_stencil_pixels_separate(struct gl_context *ctx,
378 GLint x, GLint y,
379 GLsizei width, GLsizei height,
380 uint32_t *dst, int dstStride)
381 {
382 struct gl_framebuffer *fb = ctx->ReadBuffer;
383 struct gl_renderbuffer *depthRb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
384 struct gl_renderbuffer *stencilRb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
385 GLubyte *depthMap, *stencilMap;
386 int depthStride, stencilStride, i, j;
387
388 if (_mesa_get_format_datatype(depthRb->Format) != GL_UNSIGNED_NORMALIZED)
389 return GL_FALSE;
390
391 ctx->Driver.MapRenderbuffer(ctx, depthRb, x, y, width, height,
392 GL_MAP_READ_BIT, &depthMap, &depthStride);
393 if (!depthMap) {
394 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
395 return GL_TRUE; /* don't bother trying the slow path */
396 }
397
398 ctx->Driver.MapRenderbuffer(ctx, stencilRb, x, y, width, height,
399 GL_MAP_READ_BIT, &stencilMap, &stencilStride);
400 if (!stencilMap) {
401 ctx->Driver.UnmapRenderbuffer(ctx, depthRb);
402 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
403 return GL_TRUE; /* don't bother trying the slow path */
404 }
405
406 for (j = 0; j < height; j++) {
407 GLubyte stencilVals[MAX_WIDTH];
408
409 _mesa_unpack_uint_z_row(depthRb->Format, width, depthMap, dst);
410 _mesa_unpack_ubyte_stencil_row(stencilRb->Format, width,
411 stencilMap, stencilVals);
412
413 for (i = 0; i < width; i++) {
414 dst[i] = (dst[i] & 0xffffff00) | stencilVals[i];
415 }
416
417 depthMap += depthStride;
418 stencilMap += stencilStride;
419 dst += dstStride / 4;
420 }
421
422 ctx->Driver.UnmapRenderbuffer(ctx, depthRb);
423 ctx->Driver.UnmapRenderbuffer(ctx, stencilRb);
424
425 return GL_TRUE;
426 }
427
428 static void
429 slow_read_depth_stencil_pixels_separate(struct gl_context *ctx,
430 GLint x, GLint y,
431 GLsizei width, GLsizei height,
432 GLenum type,
433 const struct gl_pixelstore_attrib *packing,
434 GLubyte *dst, int dstStride)
435 {
436 struct gl_framebuffer *fb = ctx->ReadBuffer;
437 struct gl_renderbuffer *depthRb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
438 struct gl_renderbuffer *stencilRb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
439 GLubyte *depthMap, *stencilMap;
440 int depthStride, stencilStride, j;
441
442 /* The depth and stencil buffers might be separate, or a single buffer.
443 * If one buffer, only map it once.
444 */
445 ctx->Driver.MapRenderbuffer(ctx, depthRb, x, y, width, height,
446 GL_MAP_READ_BIT, &depthMap, &depthStride);
447 if (!depthMap) {
448 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
449 return;
450 }
451
452 if (stencilRb != depthRb) {
453 ctx->Driver.MapRenderbuffer(ctx, stencilRb, x, y, width, height,
454 GL_MAP_READ_BIT, &stencilMap,
455 &stencilStride);
456 if (!stencilMap) {
457 ctx->Driver.UnmapRenderbuffer(ctx, depthRb);
458 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
459 return;
460 }
461 }
462 else {
463 stencilMap = depthMap;
464 stencilStride = depthStride;
465 }
466
467 for (j = 0; j < height; j++) {
468 GLubyte stencilVals[MAX_WIDTH];
469 GLfloat depthVals[MAX_WIDTH];
470
471 _mesa_unpack_float_z_row(depthRb->Format, width, depthMap, depthVals);
472 _mesa_unpack_ubyte_stencil_row(stencilRb->Format, width,
473 stencilMap, stencilVals);
474
475 _mesa_pack_depth_stencil_span(ctx, width, type, (GLuint *)dst,
476 depthVals, stencilVals, packing);
477
478 depthMap += depthStride;
479 stencilMap += stencilStride;
480 dst += dstStride;
481 }
482
483 ctx->Driver.UnmapRenderbuffer(ctx, depthRb);
484 if (stencilRb != depthRb) {
485 ctx->Driver.UnmapRenderbuffer(ctx, stencilRb);
486 }
487 }
488
489
490 /**
491 * Read combined depth/stencil values.
492 * We'll have already done error checking to be sure the expected
493 * depth and stencil buffers really exist.
494 */
495 static void
496 read_depth_stencil_pixels(struct gl_context *ctx,
497 GLint x, GLint y,
498 GLsizei width, GLsizei height,
499 GLenum type, GLvoid *pixels,
500 const struct gl_pixelstore_attrib *packing )
501 {
502 const GLboolean scaleOrBias
503 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
504 const GLboolean stencilTransfer = ctx->Pixel.IndexShift
505 || ctx->Pixel.IndexOffset || ctx->Pixel.MapStencilFlag;
506 GLubyte *dst;
507 int dstStride;
508
509 dst = (GLubyte *) _mesa_image_address2d(packing, pixels,
510 width, height,
511 GL_DEPTH_STENCIL_EXT,
512 type, 0, 0);
513 dstStride = _mesa_image_row_stride(packing, width,
514 GL_DEPTH_STENCIL_EXT, type);
515
516 /* Fast 24/8 reads. */
517 if (type == GL_UNSIGNED_INT_24_8 &&
518 !scaleOrBias && !stencilTransfer && !packing->SwapBytes) {
519 if (fast_read_depth_stencil_pixels(ctx, x, y, width, height,
520 dst, dstStride))
521 return;
522
523 if (fast_read_depth_stencil_pixels_separate(ctx, x, y, width, height,
524 (uint32_t *)dst, dstStride))
525 return;
526 }
527
528 slow_read_depth_stencil_pixels_separate(ctx, x, y, width, height,
529 type, packing,
530 dst, dstStride);
531 }
532
533
534
535 /**
536 * Software fallback routine for ctx->Driver.ReadPixels().
537 * By time we get here, all error checking will have been done.
538 */
539 void
540 _mesa_readpixels(struct gl_context *ctx,
541 GLint x, GLint y, GLsizei width, GLsizei height,
542 GLenum format, GLenum type,
543 const struct gl_pixelstore_attrib *packing,
544 GLvoid *pixels)
545 {
546 struct gl_pixelstore_attrib clippedPacking = *packing;
547
548 if (ctx->NewState)
549 _mesa_update_state(ctx);
550
551 /* Do all needed clipping here, so that we can forget about it later */
552 if (_mesa_clip_readpixels(ctx, &x, &y, &width, &height, &clippedPacking)) {
553
554 pixels = _mesa_map_pbo_dest(ctx, &clippedPacking, pixels);
555
556 if (pixels) {
557 switch (format) {
558 case GL_STENCIL_INDEX:
559 read_stencil_pixels(ctx, x, y, width, height, type, pixels,
560 &clippedPacking);
561 break;
562 case GL_DEPTH_COMPONENT:
563 read_depth_pixels(ctx, x, y, width, height, type, pixels,
564 &clippedPacking);
565 break;
566 case GL_DEPTH_STENCIL_EXT:
567 read_depth_stencil_pixels(ctx, x, y, width, height, type, pixels,
568 &clippedPacking);
569 break;
570 default:
571 /* all other formats should be color formats */
572 read_rgba_pixels(ctx, x, y, width, height, format, type, pixels,
573 &clippedPacking);
574 }
575
576 _mesa_unmap_pbo_dest(ctx, &clippedPacking);
577 }
578 }
579 }
580
581
582 /**
583 * Do error checking of the format/type parameters to glReadPixels and
584 * glDrawPixels.
585 * \param drawing if GL_TRUE do checking for DrawPixels, else do checking
586 * for ReadPixels.
587 * \return GL_TRUE if error detected, GL_FALSE if no errors
588 */
589 GLboolean
590 _mesa_error_check_format_type(struct gl_context *ctx, GLenum format,
591 GLenum type, GLboolean drawing)
592 {
593 const char *readDraw = drawing ? "Draw" : "Read";
594 const GLboolean reading = !drawing;
595
596 /* state validation should have already been done */
597 ASSERT(ctx->NewState == 0x0);
598
599 if (ctx->Extensions.EXT_packed_depth_stencil
600 && type == GL_UNSIGNED_INT_24_8_EXT
601 && format != GL_DEPTH_STENCIL_EXT) {
602 _mesa_error(ctx, GL_INVALID_OPERATION,
603 "gl%sPixels(format is not GL_DEPTH_STENCIL_EXT)", readDraw);
604 return GL_TRUE;
605 }
606
607 if (ctx->Extensions.ARB_depth_buffer_float
608 && type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV
609 && format != GL_DEPTH_STENCIL_EXT) {
610 _mesa_error(ctx, GL_INVALID_OPERATION,
611 "gl%sPixels(format is not GL_DEPTH_STENCIL_EXT)", readDraw);
612 return GL_TRUE;
613 }
614
615 /* basic combinations test */
616 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
617 _mesa_error(ctx, GL_INVALID_ENUM,
618 "gl%sPixels(format or type)", readDraw);
619 return GL_TRUE;
620 }
621
622 /* additional checks */
623 switch (format) {
624 case GL_RG:
625 case GL_RED:
626 case GL_GREEN:
627 case GL_BLUE:
628 case GL_ALPHA:
629 case GL_LUMINANCE:
630 case GL_LUMINANCE_ALPHA:
631 case GL_RGB:
632 case GL_BGR:
633 case GL_RGBA:
634 case GL_BGRA:
635 case GL_ABGR_EXT:
636 case GL_RED_INTEGER_EXT:
637 case GL_GREEN_INTEGER_EXT:
638 case GL_BLUE_INTEGER_EXT:
639 case GL_ALPHA_INTEGER_EXT:
640 case GL_RGB_INTEGER_EXT:
641 case GL_RGBA_INTEGER_EXT:
642 case GL_BGR_INTEGER_EXT:
643 case GL_BGRA_INTEGER_EXT:
644 case GL_LUMINANCE_INTEGER_EXT:
645 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
646 if (!drawing) {
647 /* reading */
648 if (!_mesa_source_buffer_exists(ctx, GL_COLOR)) {
649 _mesa_error(ctx, GL_INVALID_OPERATION,
650 "glReadPixels(no color buffer)");
651 return GL_TRUE;
652 }
653 }
654 break;
655 case GL_COLOR_INDEX:
656 if (drawing) {
657 if (ctx->PixelMaps.ItoR.Size == 0 ||
658 ctx->PixelMaps.ItoG.Size == 0 ||
659 ctx->PixelMaps.ItoB.Size == 0) {
660 _mesa_error(ctx, GL_INVALID_OPERATION,
661 "glDrawPixels(drawing color index pixels into RGB buffer)");
662 return GL_TRUE;
663 }
664 }
665 else {
666 /* reading */
667 if (!_mesa_source_buffer_exists(ctx, GL_COLOR)) {
668 _mesa_error(ctx, GL_INVALID_OPERATION,
669 "glReadPixels(no color buffer)");
670 return GL_TRUE;
671 }
672 /* We no longer support CI-mode color buffers so trying to read
673 * GL_COLOR_INDEX pixels is always an error.
674 */
675 _mesa_error(ctx, GL_INVALID_OPERATION,
676 "glReadPixels(color buffer is RGB)");
677 return GL_TRUE;
678 }
679 break;
680 case GL_STENCIL_INDEX:
681 if ((drawing && !_mesa_dest_buffer_exists(ctx, format)) ||
682 (reading && !_mesa_source_buffer_exists(ctx, format))) {
683 _mesa_error(ctx, GL_INVALID_OPERATION,
684 "gl%sPixels(no stencil buffer)", readDraw);
685 return GL_TRUE;
686 }
687 break;
688 case GL_DEPTH_COMPONENT:
689 if ((drawing && !_mesa_dest_buffer_exists(ctx, format))) {
690 _mesa_error(ctx, GL_INVALID_OPERATION,
691 "gl%sPixels(no depth buffer)", readDraw);
692 return GL_TRUE;
693 }
694 break;
695 case GL_DEPTH_STENCIL_EXT:
696 /* Check validity of the type first. */
697 switch (type) {
698 case GL_UNSIGNED_INT_24_8_EXT:
699 if (!ctx->Extensions.EXT_packed_depth_stencil) {
700 _mesa_error(ctx, GL_INVALID_ENUM, "gl%sPixels(type)", readDraw);
701 return GL_TRUE;
702 }
703 break;
704 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
705 if (!ctx->Extensions.ARB_depth_buffer_float) {
706 _mesa_error(ctx, GL_INVALID_ENUM, "gl%sPixels(type)", readDraw);
707 return GL_TRUE;
708 }
709 break;
710 default:
711 _mesa_error(ctx, GL_INVALID_ENUM, "gl%sPixels(type)", readDraw);
712 return GL_TRUE;
713 }
714 if ((drawing && !_mesa_dest_buffer_exists(ctx, format)) ||
715 (reading && !_mesa_source_buffer_exists(ctx, format))) {
716 _mesa_error(ctx, GL_INVALID_OPERATION,
717 "gl%sPixels(no depth or stencil buffer)", readDraw);
718 return GL_TRUE;
719 }
720 break;
721 default:
722 /* this should have been caught in _mesa_is_legal_format_type() */
723 _mesa_problem(ctx, "unexpected format in _mesa_%sPixels", readDraw);
724 return GL_TRUE;
725 }
726
727 /* no errors */
728 return GL_FALSE;
729 }
730
731
732
733 void GLAPIENTRY
734 _mesa_ReadnPixelsARB( GLint x, GLint y, GLsizei width, GLsizei height,
735 GLenum format, GLenum type, GLsizei bufSize,
736 GLvoid *pixels )
737 {
738 GET_CURRENT_CONTEXT(ctx);
739 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
740
741 FLUSH_CURRENT(ctx, 0);
742
743 if (MESA_VERBOSE & VERBOSE_API)
744 _mesa_debug(ctx, "glReadPixels(%d, %d, %s, %s, %p)\n",
745 width, height,
746 _mesa_lookup_enum_by_nr(format),
747 _mesa_lookup_enum_by_nr(type),
748 pixels);
749
750 if (width < 0 || height < 0) {
751 _mesa_error( ctx, GL_INVALID_VALUE,
752 "glReadPixels(width=%d height=%d)", width, height );
753 return;
754 }
755
756 if (ctx->NewState)
757 _mesa_update_state(ctx);
758
759 if (_mesa_error_check_format_type(ctx, format, type, GL_FALSE)) {
760 /* found an error */
761 return;
762 }
763
764 /* Check that the destination format and source buffer are both
765 * integer-valued or both non-integer-valued.
766 */
767 if (ctx->Extensions.EXT_texture_integer && _mesa_is_color_format(format)) {
768 const struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
769 const GLboolean srcInteger = _mesa_is_format_integer_color(rb->Format);
770 const GLboolean dstInteger = _mesa_is_integer_format(format);
771 if (dstInteger != srcInteger) {
772 _mesa_error(ctx, GL_INVALID_OPERATION,
773 "glReadPixels(integer / non-integer format mismatch");
774 return;
775 }
776 }
777
778 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
779 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
780 "glReadPixels(incomplete framebuffer)" );
781 return;
782 }
783
784 if (ctx->ReadBuffer->Name != 0 && ctx->ReadBuffer->Visual.samples > 0) {
785 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(multisample FBO)");
786 return;
787 }
788
789 if (!_mesa_source_buffer_exists(ctx, format)) {
790 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(no readbuffer)");
791 return;
792 }
793
794 if (width == 0 || height == 0)
795 return; /* nothing to do */
796
797 if (!_mesa_validate_pbo_access(2, &ctx->Pack, width, height, 1,
798 format, type, bufSize, pixels)) {
799 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
800 _mesa_error(ctx, GL_INVALID_OPERATION,
801 "glReadPixels(out of bounds PBO access)");
802 } else {
803 _mesa_error(ctx, GL_INVALID_OPERATION,
804 "glReadnPixelsARB(out of bounds access:"
805 " bufSize (%d) is too small)", bufSize);
806 }
807 return;
808 }
809
810 if (_mesa_is_bufferobj(ctx->Pack.BufferObj) &&
811 _mesa_bufferobj_mapped(ctx->Pack.BufferObj)) {
812 /* buffer is mapped - that's an error */
813 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(PBO is mapped)");
814 return;
815 }
816
817 ctx->Driver.ReadPixels(ctx, x, y, width, height,
818 format, type, &ctx->Pack, pixels);
819 }
820
821 void GLAPIENTRY
822 _mesa_ReadPixels( GLint x, GLint y, GLsizei width, GLsizei height,
823 GLenum format, GLenum type, GLvoid *pixels )
824 {
825 _mesa_ReadnPixelsARB(x, y, width, height, format, type, INT_MAX, pixels);
826 }