187c27e4ece7d3fbb9428d1b8fe0bca473be3440
[mesa.git] / src / mesa / swrast / s_readpix.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.0.3
4 *
5 * Copyright (C) 1999-2007 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
26 #include "main/glheader.h"
27 #include "main/colormac.h"
28 #include "main/feedback.h"
29 #include "main/formats.h"
30 #include "main/format_unpack.h"
31 #include "main/image.h"
32 #include "main/imports.h"
33 #include "main/macros.h"
34 #include "main/pack.h"
35 #include "main/pbo.h"
36 #include "main/state.h"
37
38 #include "s_context.h"
39 #include "s_depth.h"
40 #include "s_span.h"
41 #include "s_stencil.h"
42
43 /**
44 * Tries to implement glReadPixels() of GL_DEPTH_COMPONENT using memcpy of the
45 * mapping.
46 */
47 static GLboolean
48 fast_read_depth_pixels( struct gl_context *ctx,
49 GLint x, GLint y,
50 GLsizei width, GLsizei height,
51 GLenum type, GLvoid *pixels,
52 const struct gl_pixelstore_attrib *packing )
53 {
54 struct gl_framebuffer *fb = ctx->ReadBuffer;
55 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
56 GLubyte *map, *dst;
57 int stride, dstStride, j;
58
59 if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0)
60 return GL_FALSE;
61
62 if (packing->SwapBytes)
63 return GL_FALSE;
64
65 if (_mesa_get_format_datatype(rb->Format) != GL_UNSIGNED_INT)
66 return GL_FALSE;
67
68 if (!((type == GL_UNSIGNED_SHORT && rb->Format == MESA_FORMAT_Z16) ||
69 type == GL_UNSIGNED_INT))
70 return GL_FALSE;
71
72 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height, GL_MAP_READ_BIT,
73 &map, &stride);
74
75 dstStride = _mesa_image_row_stride(packing, width, GL_DEPTH_COMPONENT, type);
76 dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
77 GL_DEPTH_COMPONENT, type, 0, 0);
78
79 for (j = 0; j < height; j++) {
80 if (type == GL_UNSIGNED_INT) {
81 _mesa_unpack_uint_z_row(rb->Format, width, map, (GLuint *)dst);
82 } else {
83 ASSERT(type == GL_UNSIGNED_SHORT && rb->Format == MESA_FORMAT_Z16);
84 memcpy(dst, map, width * 2);
85 }
86
87 map += stride;
88 dst += dstStride;
89 }
90 ctx->Driver.UnmapRenderbuffer(ctx, rb);
91
92 return GL_TRUE;
93 }
94
95 /**
96 * Read pixels for format=GL_DEPTH_COMPONENT.
97 */
98 static void
99 read_depth_pixels( struct gl_context *ctx,
100 GLint x, GLint y,
101 GLsizei width, GLsizei height,
102 GLenum type, GLvoid *pixels,
103 const struct gl_pixelstore_attrib *packing )
104 {
105 struct gl_framebuffer *fb = ctx->ReadBuffer;
106 struct gl_renderbuffer *rb = fb->_DepthBuffer;
107 GLint j;
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 /* General case (slower) */
124 for (j = 0; j < height; j++, y++) {
125 GLfloat depthValues[MAX_WIDTH];
126 GLvoid *dest = _mesa_image_address2d(packing, pixels, width, height,
127 GL_DEPTH_COMPONENT, type, j, 0);
128 _swrast_read_depth_span_float(ctx, rb, width, x, y, depthValues);
129 _mesa_pack_depth_span(ctx, width, dest, type, depthValues, packing);
130 }
131 }
132
133
134 /**
135 * Read pixels for format=GL_STENCIL_INDEX.
136 */
137 static void
138 read_stencil_pixels( struct gl_context *ctx,
139 GLint x, GLint y,
140 GLsizei width, GLsizei height,
141 GLenum type, GLvoid *pixels,
142 const struct gl_pixelstore_attrib *packing )
143 {
144 struct gl_framebuffer *fb = ctx->ReadBuffer;
145 struct gl_renderbuffer *rb = fb->_StencilBuffer;
146 GLint j;
147
148 if (!rb)
149 return;
150
151 /* width should never be > MAX_WIDTH since we did clipping earlier */
152 ASSERT(width <= MAX_WIDTH);
153
154 /* process image row by row */
155 for (j=0;j<height;j++,y++) {
156 GLvoid *dest;
157 GLstencil stencil[MAX_WIDTH];
158
159 _swrast_read_stencil_span(ctx, rb, width, x, y, stencil);
160
161 dest = _mesa_image_address2d(packing, pixels, width, height,
162 GL_STENCIL_INDEX, type, j, 0);
163
164 _mesa_pack_stencil_span(ctx, width, type, dest, stencil, packing);
165 }
166 }
167
168
169
170 /**
171 * Optimized glReadPixels for particular pixel formats when pixel
172 * scaling, biasing, mapping, etc. are disabled.
173 * \return GL_TRUE if success, GL_FALSE if unable to do the readpixels
174 */
175 static GLboolean
176 fast_read_rgba_pixels( struct gl_context *ctx,
177 GLint x, GLint y,
178 GLsizei width, GLsizei height,
179 GLenum format, GLenum type,
180 GLvoid *pixels,
181 const struct gl_pixelstore_attrib *packing,
182 GLbitfield transferOps)
183 {
184 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
185
186 if (!rb)
187 return GL_FALSE;
188
189 ASSERT(rb->_BaseFormat == GL_RGBA ||
190 rb->_BaseFormat == GL_RGB ||
191 rb->_BaseFormat == GL_RG ||
192 rb->_BaseFormat == GL_RED ||
193 rb->_BaseFormat == GL_LUMINANCE ||
194 rb->_BaseFormat == GL_INTENSITY ||
195 rb->_BaseFormat == GL_LUMINANCE_ALPHA ||
196 rb->_BaseFormat == GL_ALPHA);
197
198 /* clipping should have already been done */
199 ASSERT(x + width <= (GLint) rb->Width);
200 ASSERT(y + height <= (GLint) rb->Height);
201
202 /* check for things we can't handle here */
203 if (transferOps ||
204 packing->SwapBytes ||
205 packing->LsbFirst) {
206 return GL_FALSE;
207 }
208
209 if (format == GL_RGBA && rb->DataType == type) {
210 const GLint dstStride = _mesa_image_row_stride(packing, width,
211 format, type);
212 GLubyte *dest
213 = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
214 format, type, 0, 0);
215 GLint row;
216 ASSERT(rb->GetRow);
217 for (row = 0; row < height; row++) {
218 rb->GetRow(ctx, rb, width, x, y + row, dest);
219 dest += dstStride;
220 }
221 return GL_TRUE;
222 }
223
224 if (format == GL_RGB &&
225 rb->DataType == GL_UNSIGNED_BYTE &&
226 type == GL_UNSIGNED_BYTE) {
227 const GLint dstStride = _mesa_image_row_stride(packing, width,
228 format, type);
229 GLubyte *dest
230 = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
231 format, type, 0, 0);
232 GLint row;
233 ASSERT(rb->GetRow);
234 for (row = 0; row < height; row++) {
235 GLubyte tempRow[MAX_WIDTH][4];
236 GLint col;
237 rb->GetRow(ctx, rb, width, x, y + row, tempRow);
238 /* convert RGBA to RGB */
239 for (col = 0; col < width; col++) {
240 dest[col * 3 + 0] = tempRow[col][0];
241 dest[col * 3 + 1] = tempRow[col][1];
242 dest[col * 3 + 2] = tempRow[col][2];
243 }
244 dest += dstStride;
245 }
246 return GL_TRUE;
247 }
248
249 /* not handled */
250 return GL_FALSE;
251 }
252
253
254 /**
255 * When we're using a low-precision color buffer (like 16-bit 5/6/5)
256 * we have to adjust our color values a bit to pass conformance.
257 * The problem is when a 5 or 6-bit color value is converted to an 8-bit
258 * value and then a floating point value, the floating point values don't
259 * increment uniformly as the 5 or 6-bit value is incremented.
260 *
261 * This function adjusts floating point values to compensate.
262 */
263 static void
264 adjust_colors(const struct gl_framebuffer *fb, GLuint n, GLfloat rgba[][4])
265 {
266 const GLuint rShift = 8 - fb->Visual.redBits;
267 const GLuint gShift = 8 - fb->Visual.greenBits;
268 const GLuint bShift = 8 - fb->Visual.blueBits;
269 GLfloat rScale = 1.0F / (GLfloat) ((1 << fb->Visual.redBits ) - 1);
270 GLfloat gScale = 1.0F / (GLfloat) ((1 << fb->Visual.greenBits) - 1);
271 GLfloat bScale = 1.0F / (GLfloat) ((1 << fb->Visual.blueBits ) - 1);
272 GLuint i;
273
274 if (fb->Visual.redBits == 0)
275 rScale = 0;
276 if (fb->Visual.greenBits == 0)
277 gScale = 0;
278 if (fb->Visual.blueBits == 0)
279 bScale = 0;
280
281 for (i = 0; i < n; i++) {
282 GLint r, g, b;
283 /* convert float back to ubyte */
284 CLAMPED_FLOAT_TO_UBYTE(r, rgba[i][RCOMP]);
285 CLAMPED_FLOAT_TO_UBYTE(g, rgba[i][GCOMP]);
286 CLAMPED_FLOAT_TO_UBYTE(b, rgba[i][BCOMP]);
287 /* using only the N most significant bits of the ubyte value, convert to
288 * float in [0,1].
289 */
290 rgba[i][RCOMP] = (GLfloat) (r >> rShift) * rScale;
291 rgba[i][GCOMP] = (GLfloat) (g >> gShift) * gScale;
292 rgba[i][BCOMP] = (GLfloat) (b >> bShift) * bScale;
293 }
294 }
295
296
297
298 /*
299 * Read R, G, B, A, RGB, L, or LA pixels.
300 */
301 static void
302 read_rgba_pixels( struct gl_context *ctx,
303 GLint x, GLint y,
304 GLsizei width, GLsizei height,
305 GLenum format, GLenum type, GLvoid *pixels,
306 const struct gl_pixelstore_attrib *packing )
307 {
308 SWcontext *swrast = SWRAST_CONTEXT(ctx);
309 GLbitfield transferOps = ctx->_ImageTransferState;
310 struct gl_framebuffer *fb = ctx->ReadBuffer;
311 struct gl_renderbuffer *rb = fb->_ColorReadBuffer;
312
313 if (!rb)
314 return;
315
316 if ((ctx->Color._ClampReadColor == GL_TRUE || type != GL_FLOAT) &&
317 !_mesa_is_integer_format(format)) {
318 transferOps |= IMAGE_CLAMP_BIT;
319 }
320
321 /* Try the optimized path first. */
322 if (fast_read_rgba_pixels(ctx, x, y, width, height,
323 format, type, pixels, packing, transferOps)) {
324 return; /* done! */
325 }
326
327 /* width should never be > MAX_WIDTH since we did clipping earlier */
328 ASSERT(width <= MAX_WIDTH);
329
330 {
331 const GLint dstStride
332 = _mesa_image_row_stride(packing, width, format, type);
333 GLfloat (*rgba)[4] = swrast->SpanArrays->attribs[FRAG_ATTRIB_COL0];
334 GLint row;
335 GLubyte *dst
336 = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
337 format, type, 0, 0);
338
339 for (row = 0; row < height; row++, y++) {
340
341 /* Get float rgba pixels */
342 _swrast_read_rgba_span(ctx, rb, width, x, y, GL_FLOAT, rgba);
343
344 /* apply fudge factor for shallow color buffers */
345 if ((fb->Visual.redBits < 8 && fb->Visual.redBits != 0) ||
346 (fb->Visual.greenBits < 8 && fb->Visual.greenBits != 0) ||
347 (fb->Visual.blueBits < 8 && fb->Visual.blueBits != 0)) {
348 adjust_colors(fb, width, rgba);
349 }
350
351 /* pack the row of RGBA pixels into user's buffer */
352 _mesa_pack_rgba_span_float(ctx, width, rgba, format, type, dst,
353 packing, transferOps);
354
355 dst += dstStride;
356 }
357 }
358 }
359
360
361 /**
362 * Read combined depth/stencil values.
363 * We'll have already done error checking to be sure the expected
364 * depth and stencil buffers really exist.
365 */
366 static void
367 read_depth_stencil_pixels(struct gl_context *ctx,
368 GLint x, GLint y,
369 GLsizei width, GLsizei height,
370 GLenum type, GLvoid *pixels,
371 const struct gl_pixelstore_attrib *packing )
372 {
373 const GLboolean scaleOrBias
374 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
375 const GLboolean stencilTransfer = ctx->Pixel.IndexShift
376 || ctx->Pixel.IndexOffset || ctx->Pixel.MapStencilFlag;
377 struct gl_renderbuffer *depthRb, *stencilRb;
378
379 depthRb = ctx->ReadBuffer->_DepthBuffer;
380 stencilRb = ctx->ReadBuffer->_StencilBuffer;
381
382 if (!depthRb || !stencilRb)
383 return;
384
385 depthRb = ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
386 stencilRb = ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
387
388 if (depthRb->_BaseFormat == GL_DEPTH_STENCIL_EXT &&
389 depthRb->Format == MESA_FORMAT_Z24_S8 &&
390 type == GL_UNSIGNED_INT_24_8 &&
391 depthRb == stencilRb &&
392 depthRb->GetRow && /* May be null if depthRb is a wrapper around
393 * separate depth and stencil buffers. */
394 !scaleOrBias &&
395 !stencilTransfer) {
396 /* This is the ideal case.
397 * Reading GL_DEPTH_STENCIL pixels from combined depth/stencil buffer.
398 * Plus, no pixel transfer ops to worry about!
399 */
400 GLint i;
401 GLint dstStride = _mesa_image_row_stride(packing, width,
402 GL_DEPTH_STENCIL_EXT, type);
403 GLubyte *dst = (GLubyte *) _mesa_image_address2d(packing, pixels,
404 width, height,
405 GL_DEPTH_STENCIL_EXT,
406 type, 0, 0);
407 for (i = 0; i < height; i++) {
408 depthRb->GetRow(ctx, depthRb, width, x, y + i, dst);
409 dst += dstStride;
410 }
411 }
412 else {
413 /* Reading GL_DEPTH_STENCIL pixels from separate depth/stencil buffers,
414 * or we need pixel transfer.
415 */
416 GLint i;
417 depthRb = ctx->ReadBuffer->_DepthBuffer;
418 stencilRb = ctx->ReadBuffer->_StencilBuffer;
419
420 for (i = 0; i < height; i++) {
421 GLstencil stencilVals[MAX_WIDTH];
422
423 GLuint *depthStencilDst = (GLuint *)
424 _mesa_image_address2d(packing, pixels, width, height,
425 GL_DEPTH_STENCIL_EXT, type, i, 0);
426
427 _swrast_read_stencil_span(ctx, stencilRb, width,
428 x, y + i, stencilVals);
429
430 if (!scaleOrBias && !stencilTransfer
431 && ctx->ReadBuffer->Visual.depthBits == 24) {
432 /* ideal case */
433 GLuint zVals[MAX_WIDTH]; /* 24-bit values! */
434 GLint j;
435 ASSERT(depthRb->DataType == GL_UNSIGNED_INT);
436 /* note, we've already been clipped */
437 depthRb->GetRow(ctx, depthRb, width, x, y + i, zVals);
438 for (j = 0; j < width; j++) {
439 depthStencilDst[j] = (zVals[j] << 8) | (stencilVals[j] & 0xff);
440 }
441 }
442 else {
443 /* general case */
444 GLfloat depthVals[MAX_WIDTH];
445 _swrast_read_depth_span_float(ctx, depthRb, width, x, y + i,
446 depthVals);
447 _mesa_pack_depth_stencil_span(ctx, width, type, depthStencilDst,
448 depthVals, stencilVals, packing);
449 }
450 }
451 }
452 }
453
454
455
456 /**
457 * Software fallback routine for ctx->Driver.ReadPixels().
458 * By time we get here, all error checking will have been done.
459 */
460 void
461 _swrast_ReadPixels( struct gl_context *ctx,
462 GLint x, GLint y, GLsizei width, GLsizei height,
463 GLenum format, GLenum type,
464 const struct gl_pixelstore_attrib *packing,
465 GLvoid *pixels )
466 {
467 SWcontext *swrast = SWRAST_CONTEXT(ctx);
468 struct gl_pixelstore_attrib clippedPacking = *packing;
469
470 if (ctx->NewState)
471 _mesa_update_state(ctx);
472
473 /* Need to do swrast_render_start() before clipping or anything else
474 * since this is where a driver may grab the hw lock and get an updated
475 * window size.
476 */
477 swrast_render_start(ctx);
478
479 if (swrast->NewState)
480 _swrast_validate_derived( ctx );
481
482 /* Do all needed clipping here, so that we can forget about it later */
483 if (_mesa_clip_readpixels(ctx, &x, &y, &width, &height, &clippedPacking)) {
484
485 pixels = _mesa_map_pbo_dest(ctx, &clippedPacking, pixels);
486
487 if (pixels) {
488 switch (format) {
489 case GL_STENCIL_INDEX:
490 read_stencil_pixels(ctx, x, y, width, height, type, pixels,
491 &clippedPacking);
492 break;
493 case GL_DEPTH_COMPONENT:
494 read_depth_pixels(ctx, x, y, width, height, type, pixels,
495 &clippedPacking);
496 break;
497 case GL_DEPTH_STENCIL_EXT:
498 read_depth_stencil_pixels(ctx, x, y, width, height, type, pixels,
499 &clippedPacking);
500 break;
501 default:
502 /* all other formats should be color formats */
503 read_rgba_pixels(ctx, x, y, width, height, format, type, pixels,
504 &clippedPacking);
505 }
506
507 _mesa_unmap_pbo_dest(ctx, &clippedPacking);
508 }
509 }
510
511 swrast_render_finish(ctx);
512 }