blob: e10f921d289641f677151310758dcbc11bfe261f [file] [log] [blame]
Brian Paul28876dd2008-06-09 14:22:15 -06001/*
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"
Brian Paul6364d752011-02-18 09:53:29 -070029#include "enums.h"
Brian Paul28876dd2008-06-09 14:22:15 -060030#include "readpix.h"
31#include "framebuffer.h"
Brian Paul751e10f2010-10-25 19:07:33 -060032#include "formats.h"
Brian Pauld2a23d42011-11-12 11:50:31 -070033#include "format_unpack.h"
Brian Paul28876dd2008-06-09 14:22:15 -060034#include "image.h"
Vinson Lee0117da42011-01-05 23:11:54 -080035#include "mtypes.h"
Brian Pauld2a23d42011-11-12 11:50:31 -070036#include "pack.h"
Brian Paulb70610b2011-02-28 18:24:35 -070037#include "pbo.h"
Brian Paul28876dd2008-06-09 14:22:15 -060038#include "state.h"
Jordan Justen9ad8f432012-06-25 10:52:39 -070039#include "glformats.h"
Paul Berry284ad9c2012-07-18 12:54:48 -070040#include "fbobject.h"
Ian Romanickb7c7e5e2012-07-27 12:24:24 -070041#include "teximage.h"
Brian Paul28876dd2008-06-09 14:22:15 -060042
Ian Romanickb7c7e5e2012-07-27 12:24:24 -070043/* Inexplicably, GL_HALF_FLOAT_OES has a different value than GL_HALF_FLOAT.
44 */
45#ifndef GL_HALF_FLOAT_OES
46#define GL_HALF_FLOAT_OES 0x8D61
47#endif
Brian Paul28876dd2008-06-09 14:22:15 -060048
49/**
Brian Pauld2a23d42011-11-12 11:50:31 -070050 * Tries to implement glReadPixels() of GL_DEPTH_COMPONENT using memcpy of the
51 * mapping.
52 */
53static GLboolean
54fast_read_depth_pixels( struct gl_context *ctx,
55 GLint x, GLint y,
56 GLsizei width, GLsizei height,
57 GLenum type, GLvoid *pixels,
58 const struct gl_pixelstore_attrib *packing )
59{
60 struct gl_framebuffer *fb = ctx->ReadBuffer;
61 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
62 GLubyte *map, *dst;
63 int stride, dstStride, j;
64
65 if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0)
66 return GL_FALSE;
67
68 if (packing->SwapBytes)
69 return GL_FALSE;
70
Eric Anholt755f0a02011-11-17 13:56:30 -080071 if (_mesa_get_format_datatype(rb->Format) != GL_UNSIGNED_NORMALIZED)
Brian Pauld2a23d42011-11-12 11:50:31 -070072 return GL_FALSE;
73
74 if (!((type == GL_UNSIGNED_SHORT && rb->Format == MESA_FORMAT_Z16) ||
75 type == GL_UNSIGNED_INT))
76 return GL_FALSE;
77
78 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
79 &map, &stride);
80
Brian Paul038a7df2011-11-17 17:20:05 -070081 if (!map) {
82 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
83 return GL_TRUE; /* don't bother trying the slow path */
84 }
85
Brian Pauld2a23d42011-11-12 11:50:31 -070086 dstStride = _mesa_image_row_stride(packing, width, GL_DEPTH_COMPONENT, type);
87 dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
88 GL_DEPTH_COMPONENT, type, 0, 0);
89
90 for (j = 0; j < height; j++) {
91 if (type == GL_UNSIGNED_INT) {
92 _mesa_unpack_uint_z_row(rb->Format, width, map, (GLuint *)dst);
93 } else {
94 ASSERT(type == GL_UNSIGNED_SHORT && rb->Format == MESA_FORMAT_Z16);
95 memcpy(dst, map, width * 2);
96 }
97
98 map += stride;
99 dst += dstStride;
100 }
101 ctx->Driver.UnmapRenderbuffer(ctx, rb);
102
103 return GL_TRUE;
104}
105
106/**
107 * Read pixels for format=GL_DEPTH_COMPONENT.
108 */
109static void
110read_depth_pixels( struct gl_context *ctx,
111 GLint x, GLint y,
112 GLsizei width, GLsizei height,
113 GLenum type, GLvoid *pixels,
114 const struct gl_pixelstore_attrib *packing )
115{
116 struct gl_framebuffer *fb = ctx->ReadBuffer;
117 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
118 GLint j;
119 GLubyte *dst, *map;
120 int dstStride, stride;
Brian Paul531eaca2012-02-19 20:08:51 -0700121 GLfloat *depthValues;
Brian Pauld2a23d42011-11-12 11:50:31 -0700122
123 if (!rb)
124 return;
125
126 /* clipping should have been done already */
127 ASSERT(x >= 0);
128 ASSERT(y >= 0);
129 ASSERT(x + width <= (GLint) rb->Width);
130 ASSERT(y + height <= (GLint) rb->Height);
Brian Pauld2a23d42011-11-12 11:50:31 -0700131
132 if (fast_read_depth_pixels(ctx, x, y, width, height, type, pixels, packing))
133 return;
134
135 dstStride = _mesa_image_row_stride(packing, width, GL_DEPTH_COMPONENT, type);
136 dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
137 GL_DEPTH_COMPONENT, type, 0, 0);
138
139 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
140 &map, &stride);
Brian Paul038a7df2011-11-17 17:20:05 -0700141 if (!map) {
142 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
143 return;
144 }
Brian Pauld2a23d42011-11-12 11:50:31 -0700145
Matt Turner2b7a9722012-09-03 19:44:00 -0700146 depthValues = malloc(width * sizeof(GLfloat));
Brian Pauld2a23d42011-11-12 11:50:31 -0700147
Brian Paul531eaca2012-02-19 20:08:51 -0700148 if (depthValues) {
149 /* General case (slower) */
150 for (j = 0; j < height; j++, y++) {
151 _mesa_unpack_float_z_row(rb->Format, width, map, depthValues);
152 _mesa_pack_depth_span(ctx, width, dst, type, depthValues, packing);
153
154 dst += dstStride;
155 map += stride;
156 }
Brian Pauld2a23d42011-11-12 11:50:31 -0700157 }
Brian Paul531eaca2012-02-19 20:08:51 -0700158 else {
159 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
160 }
161
162 free(depthValues);
Brian Pauld2a23d42011-11-12 11:50:31 -0700163
164 ctx->Driver.UnmapRenderbuffer(ctx, rb);
165}
166
167
168/**
169 * Read pixels for format=GL_STENCIL_INDEX.
170 */
171static void
172read_stencil_pixels( struct gl_context *ctx,
173 GLint x, GLint y,
174 GLsizei width, GLsizei height,
175 GLenum type, GLvoid *pixels,
176 const struct gl_pixelstore_attrib *packing )
177{
178 struct gl_framebuffer *fb = ctx->ReadBuffer;
179 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
180 GLint j;
Brian Paul531eaca2012-02-19 20:08:51 -0700181 GLubyte *map, *stencil;
Brian Pauld2a23d42011-11-12 11:50:31 -0700182 GLint stride;
183
184 if (!rb)
185 return;
186
Brian Pauld2a23d42011-11-12 11:50:31 -0700187 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
188 &map, &stride);
Brian Paul038a7df2011-11-17 17:20:05 -0700189 if (!map) {
190 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
191 return;
192 }
Brian Pauld2a23d42011-11-12 11:50:31 -0700193
Matt Turner2b7a9722012-09-03 19:44:00 -0700194 stencil = malloc(width * sizeof(GLubyte));
Brian Pauld2a23d42011-11-12 11:50:31 -0700195
Brian Paul531eaca2012-02-19 20:08:51 -0700196 if (stencil) {
197 /* process image row by row */
198 for (j = 0; j < height; j++) {
199 GLvoid *dest;
Brian Pauld2a23d42011-11-12 11:50:31 -0700200
Brian Paul531eaca2012-02-19 20:08:51 -0700201 _mesa_unpack_ubyte_stencil_row(rb->Format, width, map, stencil);
202 dest = _mesa_image_address2d(packing, pixels, width, height,
203 GL_STENCIL_INDEX, type, j, 0);
Brian Pauld2a23d42011-11-12 11:50:31 -0700204
Brian Paul531eaca2012-02-19 20:08:51 -0700205 _mesa_pack_stencil_span(ctx, width, type, dest, stencil, packing);
206
207 map += stride;
208 }
Brian Pauld2a23d42011-11-12 11:50:31 -0700209 }
Brian Paul531eaca2012-02-19 20:08:51 -0700210 else {
211 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
212 }
213
214 free(stencil);
Brian Pauld2a23d42011-11-12 11:50:31 -0700215
216 ctx->Driver.UnmapRenderbuffer(ctx, rb);
217}
218
Brian Paula5e95a42012-04-17 10:49:16 -0600219
220/**
221 * Try to do glReadPixels of RGBA data using a simple memcpy or swizzle.
222 * \return GL_TRUE if successful, GL_FALSE otherwise (use the slow path)
223 */
Brian Pauld2a23d42011-11-12 11:50:31 -0700224static GLboolean
225fast_read_rgba_pixels_memcpy( struct gl_context *ctx,
226 GLint x, GLint y,
227 GLsizei width, GLsizei height,
228 GLenum format, GLenum type,
229 GLvoid *pixels,
230 const struct gl_pixelstore_attrib *packing,
231 GLbitfield transferOps )
232{
233 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
234 GLubyte *dst, *map;
235 int dstStride, stride, j, texelBytes;
Brian Paula5e95a42012-04-17 10:49:16 -0600236 GLboolean swizzle_rb = GL_FALSE, copy_xrgb = GL_FALSE;
Brian Pauld2a23d42011-11-12 11:50:31 -0700237
Brian Paula5e95a42012-04-17 10:49:16 -0600238 /* XXX we could check for other swizzle/special cases here as needed */
239 if (rb->Format == MESA_FORMAT_RGBA8888_REV &&
240 format == GL_BGRA &&
241 type == GL_UNSIGNED_INT_8_8_8_8_REV &&
242 !ctx->Pack.SwapBytes) {
243 swizzle_rb = GL_TRUE;
244 }
245 else if (rb->Format == MESA_FORMAT_XRGB8888 &&
246 format == GL_BGRA &&
247 type == GL_UNSIGNED_INT_8_8_8_8_REV &&
248 !ctx->Pack.SwapBytes) {
249 copy_xrgb = GL_TRUE;
250 }
251 else if (!_mesa_format_matches_format_and_type(rb->Format, format, type,
252 ctx->Pack.SwapBytes))
Brian Pauld2a23d42011-11-12 11:50:31 -0700253 return GL_FALSE;
254
Neil Robertsd9c420972012-02-26 01:33:40 +0000255 /* If the format is unsigned normalized then we can ignore clamping
256 * because the values are already in the range [0,1] so it won't
257 * have any effect anyway.
258 */
259 if (_mesa_get_format_datatype(rb->Format) == GL_UNSIGNED_NORMALIZED)
260 transferOps &= ~IMAGE_CLAMP_BIT;
261
262 if (transferOps)
263 return GL_FALSE;
264
Brian Pauld2a23d42011-11-12 11:50:31 -0700265 dstStride = _mesa_image_row_stride(packing, width, format, type);
266 dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
267 format, type, 0, 0);
268
269 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
270 &map, &stride);
Brian Paul038a7df2011-11-17 17:20:05 -0700271 if (!map) {
272 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
273 return GL_TRUE; /* don't bother trying the slow path */
274 }
Brian Pauld2a23d42011-11-12 11:50:31 -0700275
276 texelBytes = _mesa_get_format_bytes(rb->Format);
Brian Paula5e95a42012-04-17 10:49:16 -0600277
278 if (swizzle_rb) {
279 /* swap R/B */
280 for (j = 0; j < height; j++) {
281 int i;
282 for (i = 0; i < width; i++) {
283 GLuint *dst4 = (GLuint *) dst, *map4 = (GLuint *) map;
284 GLuint pixel = map4[i];
285 dst4[i] = (pixel & 0xff00ff00)
286 | ((pixel & 0x00ff0000) >> 16)
287 | ((pixel & 0x000000ff) << 16);
288 }
289 dst += dstStride;
290 map += stride;
291 }
292 } else if (copy_xrgb) {
293 /* convert xrgb -> argb */
294 for (j = 0; j < height; j++) {
295 GLuint *dst4 = (GLuint *) dst, *map4 = (GLuint *) map;
296 int i;
297 for (i = 0; i < width; i++) {
298 dst4[i] = map4[i] | 0xff000000; /* set A=0xff */
299 }
300 dst += dstStride;
301 map += stride;
302 }
303 } else {
304 /* just memcpy */
305 for (j = 0; j < height; j++) {
306 memcpy(dst, map, width * texelBytes);
307 dst += dstStride;
308 map += stride;
309 }
Brian Pauld2a23d42011-11-12 11:50:31 -0700310 }
311
312 ctx->Driver.UnmapRenderbuffer(ctx, rb);
313
314 return GL_TRUE;
315}
316
Brian Paul72fb25c2011-11-18 17:39:01 -0700317static void
Brian Pauld2a23d42011-11-12 11:50:31 -0700318slow_read_rgba_pixels( struct gl_context *ctx,
319 GLint x, GLint y,
320 GLsizei width, GLsizei height,
321 GLenum format, GLenum type,
322 GLvoid *pixels,
323 const struct gl_pixelstore_attrib *packing,
324 GLbitfield transferOps )
325{
326 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
327 const gl_format rbFormat = _mesa_get_srgb_format_linear(rb->Format);
Brian Paulbf6aac22011-12-05 20:40:48 -0700328 void *rgba;
Brian Pauld2a23d42011-11-12 11:50:31 -0700329 GLubyte *dst, *map;
330 int dstStride, stride, j;
Jordan Justen6671d0d2012-06-25 14:14:25 -0700331 GLboolean dst_is_integer = _mesa_is_enum_format_integer(format);
332 GLboolean dst_is_uint = _mesa_is_format_unsigned(rbFormat);
Brian Pauld2a23d42011-11-12 11:50:31 -0700333
334 dstStride = _mesa_image_row_stride(packing, width, format, type);
335 dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
336 format, type, 0, 0);
337
338 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
339 &map, &stride);
Brian Paul038a7df2011-11-17 17:20:05 -0700340 if (!map) {
341 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
342 return;
343 }
Brian Pauld2a23d42011-11-12 11:50:31 -0700344
Brian Paulbf6aac22011-12-05 20:40:48 -0700345 rgba = malloc(width * MAX_PIXEL_BYTES);
346 if (!rgba)
347 goto done;
348
Brian Pauld2a23d42011-11-12 11:50:31 -0700349 for (j = 0; j < height; j++) {
Jordan Justen6671d0d2012-06-25 14:14:25 -0700350 if (dst_is_integer) {
Brian Paul301fba52012-01-09 08:11:33 -0700351 _mesa_unpack_uint_rgba_row(rbFormat, width, map, (GLuint (*)[4]) rgba);
Brian Paulad897ff2012-02-29 20:55:50 -0700352 _mesa_rebase_rgba_uint(width, (GLuint (*)[4]) rgba,
353 rb->_BaseFormat);
Jordan Justen6671d0d2012-06-25 14:14:25 -0700354 if (dst_is_uint) {
355 _mesa_pack_rgba_span_from_uints(ctx, width, (GLuint (*)[4]) rgba, format,
356 type, dst);
357 } else {
358 _mesa_pack_rgba_span_from_ints(ctx, width, (GLint (*)[4]) rgba, format,
359 type, dst);
360 }
Brian Pauld2a23d42011-11-12 11:50:31 -0700361 } else {
Brian Paulbf6aac22011-12-05 20:40:48 -0700362 _mesa_unpack_rgba_row(rbFormat, width, map, (GLfloat (*)[4]) rgba);
Brian Paulad897ff2012-02-29 20:55:50 -0700363 _mesa_rebase_rgba_float(width, (GLfloat (*)[4]) rgba,
364 rb->_BaseFormat);
Brian Paulbf6aac22011-12-05 20:40:48 -0700365 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
366 type, dst, packing, transferOps);
Brian Pauld2a23d42011-11-12 11:50:31 -0700367 }
368 dst += dstStride;
369 map += stride;
370 }
371
Brian Paulbf6aac22011-12-05 20:40:48 -0700372 free(rgba);
373
374done:
Brian Pauld2a23d42011-11-12 11:50:31 -0700375 ctx->Driver.UnmapRenderbuffer(ctx, rb);
Brian Pauld2a23d42011-11-12 11:50:31 -0700376}
377
378/*
379 * Read R, G, B, A, RGB, L, or LA pixels.
380 */
381static void
382read_rgba_pixels( struct gl_context *ctx,
383 GLint x, GLint y,
384 GLsizei width, GLsizei height,
385 GLenum format, GLenum type, GLvoid *pixels,
386 const struct gl_pixelstore_attrib *packing )
387{
388 GLbitfield transferOps = ctx->_ImageTransferState;
389 struct gl_framebuffer *fb = ctx->ReadBuffer;
390 struct gl_renderbuffer *rb = fb->_ColorReadBuffer;
391
392 if (!rb)
393 return;
394
395 if ((ctx->Color._ClampReadColor == GL_TRUE || type != GL_FLOAT) &&
Jordan Justen9ad8f432012-06-25 10:52:39 -0700396 !_mesa_is_enum_format_integer(format)) {
Brian Pauld2a23d42011-11-12 11:50:31 -0700397 transferOps |= IMAGE_CLAMP_BIT;
398 }
399
Neil Robertsd9c420972012-02-26 01:33:40 +0000400 /* Try the optimized paths first. */
401 if (fast_read_rgba_pixels_memcpy(ctx, x, y, width, height,
402 format, type, pixels, packing,
403 transferOps)) {
404 return;
Brian Pauld2a23d42011-11-12 11:50:31 -0700405 }
406
407 slow_read_rgba_pixels(ctx, x, y, width, height,
408 format, type, pixels, packing, transferOps);
409}
410
411/**
412 * For a packed depth/stencil buffer being read as depth/stencil, just memcpy the
413 * data (possibly swapping 8/24 vs 24/8 as we go).
414 */
415static GLboolean
416fast_read_depth_stencil_pixels(struct gl_context *ctx,
417 GLint x, GLint y,
418 GLsizei width, GLsizei height,
419 GLubyte *dst, int dstStride)
420{
421 struct gl_framebuffer *fb = ctx->ReadBuffer;
422 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
423 struct gl_renderbuffer *stencilRb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
424 GLubyte *map;
425 int stride, i;
426
427 if (rb != stencilRb)
428 return GL_FALSE;
429
430 if (rb->Format != MESA_FORMAT_Z24_S8 &&
431 rb->Format != MESA_FORMAT_S8_Z24)
432 return GL_FALSE;
433
434 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
435 &map, &stride);
Brian Paul038a7df2011-11-17 17:20:05 -0700436 if (!map) {
437 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
438 return GL_TRUE; /* don't bother trying the slow path */
439 }
Brian Pauld2a23d42011-11-12 11:50:31 -0700440
441 for (i = 0; i < height; i++) {
442 _mesa_unpack_uint_24_8_depth_stencil_row(rb->Format, width,
443 map, (GLuint *)dst);
444 map += stride;
445 dst += dstStride;
446 }
447
448 ctx->Driver.UnmapRenderbuffer(ctx, rb);
449
450 return GL_TRUE;
451}
452
453
454/**
455 * For non-float-depth and stencil buffers being read as 24/8 depth/stencil,
456 * copy the integer data directly instead of converting depth to float and
457 * re-packing.
458 */
459static GLboolean
460fast_read_depth_stencil_pixels_separate(struct gl_context *ctx,
461 GLint x, GLint y,
462 GLsizei width, GLsizei height,
463 uint32_t *dst, int dstStride)
464{
465 struct gl_framebuffer *fb = ctx->ReadBuffer;
466 struct gl_renderbuffer *depthRb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
467 struct gl_renderbuffer *stencilRb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
Brian Paul531eaca2012-02-19 20:08:51 -0700468 GLubyte *depthMap, *stencilMap, *stencilVals;
Brian Pauld2a23d42011-11-12 11:50:31 -0700469 int depthStride, stencilStride, i, j;
470
Eric Anholt755f0a02011-11-17 13:56:30 -0800471 if (_mesa_get_format_datatype(depthRb->Format) != GL_UNSIGNED_NORMALIZED)
Brian Pauld2a23d42011-11-12 11:50:31 -0700472 return GL_FALSE;
473
474 ctx->Driver.MapRenderbuffer(ctx, depthRb, x, y, width, height,
475 GL_MAP_READ_BIT, &depthMap, &depthStride);
Brian Paul038a7df2011-11-17 17:20:05 -0700476 if (!depthMap) {
477 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
478 return GL_TRUE; /* don't bother trying the slow path */
479 }
480
Brian Pauld2a23d42011-11-12 11:50:31 -0700481 ctx->Driver.MapRenderbuffer(ctx, stencilRb, x, y, width, height,
482 GL_MAP_READ_BIT, &stencilMap, &stencilStride);
Brian Paul038a7df2011-11-17 17:20:05 -0700483 if (!stencilMap) {
484 ctx->Driver.UnmapRenderbuffer(ctx, depthRb);
485 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
486 return GL_TRUE; /* don't bother trying the slow path */
487 }
Brian Pauld2a23d42011-11-12 11:50:31 -0700488
Matt Turner2b7a9722012-09-03 19:44:00 -0700489 stencilVals = malloc(width * sizeof(GLubyte));
Brian Pauld2a23d42011-11-12 11:50:31 -0700490
Brian Paul531eaca2012-02-19 20:08:51 -0700491 if (stencilVals) {
492 for (j = 0; j < height; j++) {
493 _mesa_unpack_uint_z_row(depthRb->Format, width, depthMap, dst);
494 _mesa_unpack_ubyte_stencil_row(stencilRb->Format, width,
495 stencilMap, stencilVals);
Brian Pauld2a23d42011-11-12 11:50:31 -0700496
Brian Paul531eaca2012-02-19 20:08:51 -0700497 for (i = 0; i < width; i++) {
498 dst[i] = (dst[i] & 0xffffff00) | stencilVals[i];
499 }
500
501 depthMap += depthStride;
502 stencilMap += stencilStride;
503 dst += dstStride / 4;
Brian Pauld2a23d42011-11-12 11:50:31 -0700504 }
Brian Pauld2a23d42011-11-12 11:50:31 -0700505 }
Brian Paul531eaca2012-02-19 20:08:51 -0700506 else {
507 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
508 }
509
510 free(stencilVals);
Brian Pauld2a23d42011-11-12 11:50:31 -0700511
512 ctx->Driver.UnmapRenderbuffer(ctx, depthRb);
513 ctx->Driver.UnmapRenderbuffer(ctx, stencilRb);
514
515 return GL_TRUE;
516}
517
518static void
519slow_read_depth_stencil_pixels_separate(struct gl_context *ctx,
520 GLint x, GLint y,
521 GLsizei width, GLsizei height,
522 GLenum type,
523 const struct gl_pixelstore_attrib *packing,
524 GLubyte *dst, int dstStride)
525{
526 struct gl_framebuffer *fb = ctx->ReadBuffer;
527 struct gl_renderbuffer *depthRb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
528 struct gl_renderbuffer *stencilRb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
529 GLubyte *depthMap, *stencilMap;
530 int depthStride, stencilStride, j;
Brian Paul531eaca2012-02-19 20:08:51 -0700531 GLubyte *stencilVals;
532 GLfloat *depthVals;
533
Brian Pauld2a23d42011-11-12 11:50:31 -0700534
Brian Paulf6a50c02011-11-16 07:47:51 -0700535 /* The depth and stencil buffers might be separate, or a single buffer.
536 * If one buffer, only map it once.
537 */
Brian Pauld2a23d42011-11-12 11:50:31 -0700538 ctx->Driver.MapRenderbuffer(ctx, depthRb, x, y, width, height,
539 GL_MAP_READ_BIT, &depthMap, &depthStride);
Brian Paul038a7df2011-11-17 17:20:05 -0700540 if (!depthMap) {
541 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
542 return;
543 }
544
Brian Paulf6a50c02011-11-16 07:47:51 -0700545 if (stencilRb != depthRb) {
546 ctx->Driver.MapRenderbuffer(ctx, stencilRb, x, y, width, height,
547 GL_MAP_READ_BIT, &stencilMap,
548 &stencilStride);
Brian Paul038a7df2011-11-17 17:20:05 -0700549 if (!stencilMap) {
550 ctx->Driver.UnmapRenderbuffer(ctx, depthRb);
551 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
552 return;
553 }
Brian Paulf6a50c02011-11-16 07:47:51 -0700554 }
Brian Paulf4c03da2011-11-16 09:58:45 -0700555 else {
556 stencilMap = depthMap;
557 stencilStride = depthStride;
558 }
Brian Pauld2a23d42011-11-12 11:50:31 -0700559
Matt Turner2b7a9722012-09-03 19:44:00 -0700560 stencilVals = malloc(width * sizeof(GLubyte));
561 depthVals = malloc(width * sizeof(GLfloat));
Brian Pauld2a23d42011-11-12 11:50:31 -0700562
Brian Paul531eaca2012-02-19 20:08:51 -0700563 if (stencilVals && depthVals) {
564 for (j = 0; j < height; j++) {
565 _mesa_unpack_float_z_row(depthRb->Format, width, depthMap, depthVals);
566 _mesa_unpack_ubyte_stencil_row(stencilRb->Format, width,
567 stencilMap, stencilVals);
Brian Pauld2a23d42011-11-12 11:50:31 -0700568
Brian Paul531eaca2012-02-19 20:08:51 -0700569 _mesa_pack_depth_stencil_span(ctx, width, type, (GLuint *)dst,
570 depthVals, stencilVals, packing);
Brian Pauld2a23d42011-11-12 11:50:31 -0700571
Brian Paul531eaca2012-02-19 20:08:51 -0700572 depthMap += depthStride;
573 stencilMap += stencilStride;
574 dst += dstStride;
575 }
Brian Pauld2a23d42011-11-12 11:50:31 -0700576 }
Brian Paul531eaca2012-02-19 20:08:51 -0700577 else {
578 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
579 }
580
581 free(stencilVals);
582 free(depthVals);
Brian Pauld2a23d42011-11-12 11:50:31 -0700583
584 ctx->Driver.UnmapRenderbuffer(ctx, depthRb);
Brian Paulf6a50c02011-11-16 07:47:51 -0700585 if (stencilRb != depthRb) {
586 ctx->Driver.UnmapRenderbuffer(ctx, stencilRb);
587 }
Brian Pauld2a23d42011-11-12 11:50:31 -0700588}
589
590
591/**
592 * Read combined depth/stencil values.
593 * We'll have already done error checking to be sure the expected
594 * depth and stencil buffers really exist.
595 */
596static void
597read_depth_stencil_pixels(struct gl_context *ctx,
598 GLint x, GLint y,
599 GLsizei width, GLsizei height,
600 GLenum type, GLvoid *pixels,
601 const struct gl_pixelstore_attrib *packing )
602{
603 const GLboolean scaleOrBias
604 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
605 const GLboolean stencilTransfer = ctx->Pixel.IndexShift
606 || ctx->Pixel.IndexOffset || ctx->Pixel.MapStencilFlag;
607 GLubyte *dst;
608 int dstStride;
609
610 dst = (GLubyte *) _mesa_image_address2d(packing, pixels,
611 width, height,
612 GL_DEPTH_STENCIL_EXT,
613 type, 0, 0);
614 dstStride = _mesa_image_row_stride(packing, width,
615 GL_DEPTH_STENCIL_EXT, type);
616
617 /* Fast 24/8 reads. */
618 if (type == GL_UNSIGNED_INT_24_8 &&
619 !scaleOrBias && !stencilTransfer && !packing->SwapBytes) {
620 if (fast_read_depth_stencil_pixels(ctx, x, y, width, height,
621 dst, dstStride))
622 return;
623
624 if (fast_read_depth_stencil_pixels_separate(ctx, x, y, width, height,
625 (uint32_t *)dst, dstStride))
626 return;
627 }
628
629 slow_read_depth_stencil_pixels_separate(ctx, x, y, width, height,
630 type, packing,
631 dst, dstStride);
632}
633
634
635
636/**
637 * Software fallback routine for ctx->Driver.ReadPixels().
638 * By time we get here, all error checking will have been done.
639 */
640void
641_mesa_readpixels(struct gl_context *ctx,
642 GLint x, GLint y, GLsizei width, GLsizei height,
643 GLenum format, GLenum type,
644 const struct gl_pixelstore_attrib *packing,
645 GLvoid *pixels)
646{
647 struct gl_pixelstore_attrib clippedPacking = *packing;
648
649 if (ctx->NewState)
650 _mesa_update_state(ctx);
651
652 /* Do all needed clipping here, so that we can forget about it later */
653 if (_mesa_clip_readpixels(ctx, &x, &y, &width, &height, &clippedPacking)) {
654
655 pixels = _mesa_map_pbo_dest(ctx, &clippedPacking, pixels);
656
657 if (pixels) {
658 switch (format) {
659 case GL_STENCIL_INDEX:
660 read_stencil_pixels(ctx, x, y, width, height, type, pixels,
661 &clippedPacking);
662 break;
663 case GL_DEPTH_COMPONENT:
664 read_depth_pixels(ctx, x, y, width, height, type, pixels,
665 &clippedPacking);
666 break;
667 case GL_DEPTH_STENCIL_EXT:
668 read_depth_stencil_pixels(ctx, x, y, width, height, type, pixels,
669 &clippedPacking);
670 break;
671 default:
672 /* all other formats should be color formats */
673 read_rgba_pixels(ctx, x, y, width, height, format, type, pixels,
674 &clippedPacking);
675 }
676
677 _mesa_unmap_pbo_dest(ctx, &clippedPacking);
678 }
679 }
680}
681
682
Brian Paul28876dd2008-06-09 14:22:15 -0600683void GLAPIENTRY
Brian Paul6b329b92011-04-26 14:54:41 -0600684_mesa_ReadnPixelsARB( GLint x, GLint y, GLsizei width, GLsizei height,
685 GLenum format, GLenum type, GLsizei bufSize,
686 GLvoid *pixels )
Brian Paul28876dd2008-06-09 14:22:15 -0600687{
Brian Paulb8f7eef2012-02-07 07:42:33 -0700688 GLenum err;
689
Brian Paul28876dd2008-06-09 14:22:15 -0600690 GET_CURRENT_CONTEXT(ctx);
691 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
692
693 FLUSH_CURRENT(ctx, 0);
694
Brian Paul6364d752011-02-18 09:53:29 -0700695 if (MESA_VERBOSE & VERBOSE_API)
696 _mesa_debug(ctx, "glReadPixels(%d, %d, %s, %s, %p)\n",
697 width, height,
698 _mesa_lookup_enum_by_nr(format),
699 _mesa_lookup_enum_by_nr(type),
700 pixels);
701
Brian Paul28876dd2008-06-09 14:22:15 -0600702 if (width < 0 || height < 0) {
703 _mesa_error( ctx, GL_INVALID_VALUE,
704 "glReadPixels(width=%d height=%d)", width, height );
705 return;
706 }
707
Ian Romanickb7c7e5e2012-07-27 12:24:24 -0700708 /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
709 * combinations of format and type that can be used.
710 *
711 * Technically, only two combinations are actually allowed:
712 * GL_RGBA/GL_UNSIGNED_BYTE, and some implementation-specific internal
713 * preferred combination. This code doesn't know what that preferred
714 * combination is, and Mesa can handle anything valid. Just work instead.
715 */
716 if (_mesa_is_gles(ctx) && ctx->Version < 30) {
717 err = _mesa_es_error_check_format_and_type(format, type, 2);
718 if (err == GL_NO_ERROR) {
719 if (type == GL_FLOAT || type == GL_HALF_FLOAT_OES) {
720 err = GL_INVALID_OPERATION;
721 } else if (format == GL_DEPTH_COMPONENT
722 || format == GL_DEPTH_STENCIL) {
723 err = GL_INVALID_ENUM;
724 }
725 }
726
727 if (err != GL_NO_ERROR) {
728 _mesa_error(ctx, err, "glReadPixels(invalid format %s and/or type %s)",
729 _mesa_lookup_enum_by_nr(format),
730 _mesa_lookup_enum_by_nr(type));
731 return;
732 }
733 }
734
Brian Paul28876dd2008-06-09 14:22:15 -0600735 if (ctx->NewState)
736 _mesa_update_state(ctx);
737
Brian Paulb8f7eef2012-02-07 07:42:33 -0700738 err = _mesa_error_check_format_and_type(ctx, format, type);
739 if (err != GL_NO_ERROR) {
740 _mesa_error(ctx, err, "glReadPixels(invalid format %s and/or type %s)",
741 _mesa_lookup_enum_by_nr(format),
742 _mesa_lookup_enum_by_nr(type));
Brian Paul28876dd2008-06-09 14:22:15 -0600743 return;
744 }
745
Eric Anholt5a827d92012-05-14 10:18:23 -0700746 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
747 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
748 "glReadPixels(incomplete framebuffer)" );
749 return;
750 }
751
Brian Paul751e10f2010-10-25 19:07:33 -0600752 /* Check that the destination format and source buffer are both
753 * integer-valued or both non-integer-valued.
754 */
755 if (ctx->Extensions.EXT_texture_integer && _mesa_is_color_format(format)) {
756 const struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
Brian Paul412b9602010-10-26 20:30:40 -0600757 const GLboolean srcInteger = _mesa_is_format_integer_color(rb->Format);
Jordan Justen9ad8f432012-06-25 10:52:39 -0700758 const GLboolean dstInteger = _mesa_is_enum_format_integer(format);
Brian Paul751e10f2010-10-25 19:07:33 -0600759 if (dstInteger != srcInteger) {
760 _mesa_error(ctx, GL_INVALID_OPERATION,
761 "glReadPixels(integer / non-integer format mismatch");
762 return;
763 }
764 }
765
Paul Berry284ad9c2012-07-18 12:54:48 -0700766 if (_mesa_is_user_fbo(ctx->ReadBuffer) &&
767 ctx->ReadBuffer->Visual.samples > 0) {
Eric Anholt86b7c672012-01-11 13:54:53 -0800768 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(multisample FBO)");
769 return;
770 }
771
Brian Paul28876dd2008-06-09 14:22:15 -0600772 if (!_mesa_source_buffer_exists(ctx, format)) {
773 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(no readbuffer)");
774 return;
775 }
776
Brian Paul7b9bf392009-04-02 13:05:55 -0600777 if (width == 0 || height == 0)
778 return; /* nothing to do */
779
Brian Paul6b329b92011-04-26 14:54:41 -0600780 if (!_mesa_validate_pbo_access(2, &ctx->Pack, width, height, 1,
781 format, type, bufSize, pixels)) {
782 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
Brian Paul28876dd2008-06-09 14:22:15 -0600783 _mesa_error(ctx, GL_INVALID_OPERATION,
Brian Paul6b329b92011-04-26 14:54:41 -0600784 "glReadPixels(out of bounds PBO access)");
785 } else {
786 _mesa_error(ctx, GL_INVALID_OPERATION,
787 "glReadnPixelsARB(out of bounds access:"
788 " bufSize (%d) is too small)", bufSize);
Brian Paul28876dd2008-06-09 14:22:15 -0600789 }
Brian Paul6b329b92011-04-26 14:54:41 -0600790 return;
791 }
Brian Paul28876dd2008-06-09 14:22:15 -0600792
Brian Paul6b329b92011-04-26 14:54:41 -0600793 if (_mesa_is_bufferobj(ctx->Pack.BufferObj) &&
794 _mesa_bufferobj_mapped(ctx->Pack.BufferObj)) {
795 /* buffer is mapped - that's an error */
796 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(PBO is mapped)");
797 return;
Brian Paul28876dd2008-06-09 14:22:15 -0600798 }
799
800 ctx->Driver.ReadPixels(ctx, x, y, width, height,
801 format, type, &ctx->Pack, pixels);
802}
Brian Paul6b329b92011-04-26 14:54:41 -0600803
804void GLAPIENTRY
805_mesa_ReadPixels( GLint x, GLint y, GLsizei width, GLsizei height,
806 GLenum format, GLenum type, GLvoid *pixels )
807{
808 _mesa_ReadnPixelsARB(x, y, width, height, format, type, INT_MAX, pixels);
809}