mesa: new BYTE/SHORT_TO_FLOATZ() macros
[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 ctx->Driver.MapRenderbuffer(ctx, depthRb, x, y, width, height,
406 GL_MAP_READ_BIT, &depthMap, &depthStride);
407 ctx->Driver.MapRenderbuffer(ctx, stencilRb, x, y, width, height,
408 GL_MAP_READ_BIT, &stencilMap, &stencilStride);
409
410 for (j = 0; j < height; j++) {
411 GLubyte stencilVals[MAX_WIDTH];
412 GLfloat depthVals[MAX_WIDTH];
413
414 _mesa_unpack_float_z_row(depthRb->Format, width, depthMap, depthVals);
415 _mesa_unpack_ubyte_stencil_row(stencilRb->Format, width,
416 stencilMap, stencilVals);
417
418 _mesa_pack_depth_stencil_span(ctx, width, type, (GLuint *)dst,
419 depthVals, stencilVals, packing);
420
421 depthMap += depthStride;
422 stencilMap += stencilStride;
423 dst += dstStride;
424 }
425
426 ctx->Driver.UnmapRenderbuffer(ctx, depthRb);
427 ctx->Driver.UnmapRenderbuffer(ctx, stencilRb);
428 }
429
430
431 /**
432 * Read combined depth/stencil values.
433 * We'll have already done error checking to be sure the expected
434 * depth and stencil buffers really exist.
435 */
436 static void
437 read_depth_stencil_pixels(struct gl_context *ctx,
438 GLint x, GLint y,
439 GLsizei width, GLsizei height,
440 GLenum type, GLvoid *pixels,
441 const struct gl_pixelstore_attrib *packing )
442 {
443 const GLboolean scaleOrBias
444 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
445 const GLboolean stencilTransfer = ctx->Pixel.IndexShift
446 || ctx->Pixel.IndexOffset || ctx->Pixel.MapStencilFlag;
447 GLubyte *dst;
448 int dstStride;
449
450 dst = (GLubyte *) _mesa_image_address2d(packing, pixels,
451 width, height,
452 GL_DEPTH_STENCIL_EXT,
453 type, 0, 0);
454 dstStride = _mesa_image_row_stride(packing, width,
455 GL_DEPTH_STENCIL_EXT, type);
456
457 /* Fast 24/8 reads. */
458 if (type == GL_UNSIGNED_INT_24_8 &&
459 !scaleOrBias && !stencilTransfer && !packing->SwapBytes) {
460 if (fast_read_depth_stencil_pixels(ctx, x, y, width, height,
461 dst, dstStride))
462 return;
463
464 if (fast_read_depth_stencil_pixels_separate(ctx, x, y, width, height,
465 (uint32_t *)dst, dstStride))
466 return;
467 }
468
469 slow_read_depth_stencil_pixels_separate(ctx, x, y, width, height,
470 type, packing,
471 dst, dstStride);
472 }
473
474
475
476 /**
477 * Software fallback routine for ctx->Driver.ReadPixels().
478 * By time we get here, all error checking will have been done.
479 */
480 void
481 _mesa_readpixels(struct gl_context *ctx,
482 GLint x, GLint y, GLsizei width, GLsizei height,
483 GLenum format, GLenum type,
484 const struct gl_pixelstore_attrib *packing,
485 GLvoid *pixels)
486 {
487 struct gl_pixelstore_attrib clippedPacking = *packing;
488
489 if (ctx->NewState)
490 _mesa_update_state(ctx);
491
492 /* Do all needed clipping here, so that we can forget about it later */
493 if (_mesa_clip_readpixels(ctx, &x, &y, &width, &height, &clippedPacking)) {
494
495 pixels = _mesa_map_pbo_dest(ctx, &clippedPacking, pixels);
496
497 if (pixels) {
498 switch (format) {
499 case GL_STENCIL_INDEX:
500 read_stencil_pixels(ctx, x, y, width, height, type, pixels,
501 &clippedPacking);
502 break;
503 case GL_DEPTH_COMPONENT:
504 read_depth_pixels(ctx, x, y, width, height, type, pixels,
505 &clippedPacking);
506 break;
507 case GL_DEPTH_STENCIL_EXT:
508 read_depth_stencil_pixels(ctx, x, y, width, height, type, pixels,
509 &clippedPacking);
510 break;
511 default:
512 /* all other formats should be color formats */
513 read_rgba_pixels(ctx, x, y, width, height, format, type, pixels,
514 &clippedPacking);
515 }
516
517 _mesa_unmap_pbo_dest(ctx, &clippedPacking);
518 }
519 }
520 }
521
522
523 /**
524 * Do error checking of the format/type parameters to glReadPixels and
525 * glDrawPixels.
526 * \param drawing if GL_TRUE do checking for DrawPixels, else do checking
527 * for ReadPixels.
528 * \return GL_TRUE if error detected, GL_FALSE if no errors
529 */
530 GLboolean
531 _mesa_error_check_format_type(struct gl_context *ctx, GLenum format,
532 GLenum type, GLboolean drawing)
533 {
534 const char *readDraw = drawing ? "Draw" : "Read";
535 const GLboolean reading = !drawing;
536
537 /* state validation should have already been done */
538 ASSERT(ctx->NewState == 0x0);
539
540 if (ctx->Extensions.EXT_packed_depth_stencil
541 && type == GL_UNSIGNED_INT_24_8_EXT
542 && format != GL_DEPTH_STENCIL_EXT) {
543 _mesa_error(ctx, GL_INVALID_OPERATION,
544 "gl%sPixels(format is not GL_DEPTH_STENCIL_EXT)", readDraw);
545 return GL_TRUE;
546 }
547
548 if (ctx->Extensions.ARB_depth_buffer_float
549 && type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV
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 /* basic combinations test */
557 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
558 _mesa_error(ctx, GL_INVALID_ENUM,
559 "gl%sPixels(format or type)", readDraw);
560 return GL_TRUE;
561 }
562
563 /* additional checks */
564 switch (format) {
565 case GL_RG:
566 case GL_RED:
567 case GL_GREEN:
568 case GL_BLUE:
569 case GL_ALPHA:
570 case GL_LUMINANCE:
571 case GL_LUMINANCE_ALPHA:
572 case GL_RGB:
573 case GL_BGR:
574 case GL_RGBA:
575 case GL_BGRA:
576 case GL_ABGR_EXT:
577 case GL_RED_INTEGER_EXT:
578 case GL_GREEN_INTEGER_EXT:
579 case GL_BLUE_INTEGER_EXT:
580 case GL_ALPHA_INTEGER_EXT:
581 case GL_RGB_INTEGER_EXT:
582 case GL_RGBA_INTEGER_EXT:
583 case GL_BGR_INTEGER_EXT:
584 case GL_BGRA_INTEGER_EXT:
585 case GL_LUMINANCE_INTEGER_EXT:
586 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
587 if (!drawing) {
588 /* reading */
589 if (!_mesa_source_buffer_exists(ctx, GL_COLOR)) {
590 _mesa_error(ctx, GL_INVALID_OPERATION,
591 "glReadPixels(no color buffer)");
592 return GL_TRUE;
593 }
594 }
595 break;
596 case GL_COLOR_INDEX:
597 if (drawing) {
598 if (ctx->PixelMaps.ItoR.Size == 0 ||
599 ctx->PixelMaps.ItoG.Size == 0 ||
600 ctx->PixelMaps.ItoB.Size == 0) {
601 _mesa_error(ctx, GL_INVALID_OPERATION,
602 "glDrawPixels(drawing color index pixels into RGB buffer)");
603 return GL_TRUE;
604 }
605 }
606 else {
607 /* reading */
608 if (!_mesa_source_buffer_exists(ctx, GL_COLOR)) {
609 _mesa_error(ctx, GL_INVALID_OPERATION,
610 "glReadPixels(no color buffer)");
611 return GL_TRUE;
612 }
613 /* We no longer support CI-mode color buffers so trying to read
614 * GL_COLOR_INDEX pixels is always an error.
615 */
616 _mesa_error(ctx, GL_INVALID_OPERATION,
617 "glReadPixels(color buffer is RGB)");
618 return GL_TRUE;
619 }
620 break;
621 case GL_STENCIL_INDEX:
622 if ((drawing && !_mesa_dest_buffer_exists(ctx, format)) ||
623 (reading && !_mesa_source_buffer_exists(ctx, format))) {
624 _mesa_error(ctx, GL_INVALID_OPERATION,
625 "gl%sPixels(no stencil buffer)", readDraw);
626 return GL_TRUE;
627 }
628 break;
629 case GL_DEPTH_COMPONENT:
630 if ((drawing && !_mesa_dest_buffer_exists(ctx, format))) {
631 _mesa_error(ctx, GL_INVALID_OPERATION,
632 "gl%sPixels(no depth buffer)", readDraw);
633 return GL_TRUE;
634 }
635 break;
636 case GL_DEPTH_STENCIL_EXT:
637 /* Check validity of the type first. */
638 switch (type) {
639 case GL_UNSIGNED_INT_24_8_EXT:
640 if (!ctx->Extensions.EXT_packed_depth_stencil) {
641 _mesa_error(ctx, GL_INVALID_ENUM, "gl%sPixels(type)", readDraw);
642 return GL_TRUE;
643 }
644 break;
645 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
646 if (!ctx->Extensions.ARB_depth_buffer_float) {
647 _mesa_error(ctx, GL_INVALID_ENUM, "gl%sPixels(type)", readDraw);
648 return GL_TRUE;
649 }
650 break;
651 default:
652 _mesa_error(ctx, GL_INVALID_ENUM, "gl%sPixels(type)", readDraw);
653 return GL_TRUE;
654 }
655 if ((drawing && !_mesa_dest_buffer_exists(ctx, format)) ||
656 (reading && !_mesa_source_buffer_exists(ctx, format))) {
657 _mesa_error(ctx, GL_INVALID_OPERATION,
658 "gl%sPixels(no depth or stencil buffer)", readDraw);
659 return GL_TRUE;
660 }
661 break;
662 default:
663 /* this should have been caught in _mesa_is_legal_format_type() */
664 _mesa_problem(ctx, "unexpected format in _mesa_%sPixels", readDraw);
665 return GL_TRUE;
666 }
667
668 /* no errors */
669 return GL_FALSE;
670 }
671
672
673
674 void GLAPIENTRY
675 _mesa_ReadnPixelsARB( GLint x, GLint y, GLsizei width, GLsizei height,
676 GLenum format, GLenum type, GLsizei bufSize,
677 GLvoid *pixels )
678 {
679 GET_CURRENT_CONTEXT(ctx);
680 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
681
682 FLUSH_CURRENT(ctx, 0);
683
684 if (MESA_VERBOSE & VERBOSE_API)
685 _mesa_debug(ctx, "glReadPixels(%d, %d, %s, %s, %p)\n",
686 width, height,
687 _mesa_lookup_enum_by_nr(format),
688 _mesa_lookup_enum_by_nr(type),
689 pixels);
690
691 if (width < 0 || height < 0) {
692 _mesa_error( ctx, GL_INVALID_VALUE,
693 "glReadPixels(width=%d height=%d)", width, height );
694 return;
695 }
696
697 if (ctx->NewState)
698 _mesa_update_state(ctx);
699
700 if (_mesa_error_check_format_type(ctx, format, type, GL_FALSE)) {
701 /* found an error */
702 return;
703 }
704
705 /* Check that the destination format and source buffer are both
706 * integer-valued or both non-integer-valued.
707 */
708 if (ctx->Extensions.EXT_texture_integer && _mesa_is_color_format(format)) {
709 const struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
710 const GLboolean srcInteger = _mesa_is_format_integer_color(rb->Format);
711 const GLboolean dstInteger = _mesa_is_integer_format(format);
712 if (dstInteger != srcInteger) {
713 _mesa_error(ctx, GL_INVALID_OPERATION,
714 "glReadPixels(integer / non-integer format mismatch");
715 return;
716 }
717 }
718
719 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
720 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
721 "glReadPixels(incomplete framebuffer)" );
722 return;
723 }
724
725 if (!_mesa_source_buffer_exists(ctx, format)) {
726 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(no readbuffer)");
727 return;
728 }
729
730 if (width == 0 || height == 0)
731 return; /* nothing to do */
732
733 if (!_mesa_validate_pbo_access(2, &ctx->Pack, width, height, 1,
734 format, type, bufSize, pixels)) {
735 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
736 _mesa_error(ctx, GL_INVALID_OPERATION,
737 "glReadPixels(out of bounds PBO access)");
738 } else {
739 _mesa_error(ctx, GL_INVALID_OPERATION,
740 "glReadnPixelsARB(out of bounds access:"
741 " bufSize (%d) is too small)", bufSize);
742 }
743 return;
744 }
745
746 if (_mesa_is_bufferobj(ctx->Pack.BufferObj) &&
747 _mesa_bufferobj_mapped(ctx->Pack.BufferObj)) {
748 /* buffer is mapped - that's an error */
749 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(PBO is mapped)");
750 return;
751 }
752
753 ctx->Driver.ReadPixels(ctx, x, y, width, height,
754 format, type, &ctx->Pack, pixels);
755 }
756
757 void GLAPIENTRY
758 _mesa_ReadPixels( GLint x, GLint y, GLsizei width, GLsizei height,
759 GLenum format, GLenum type, GLvoid *pixels )
760 {
761 _mesa_ReadnPixelsARB(x, y, width, height, format, type, INT_MAX, pixels);
762 }