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