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