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