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