2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
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/pixel.h"
37 #include "main/state.h"
39 #include "s_context.h"
42 #include "s_stencil.h"
46 * Read a block of color index pixels.
49 read_index_pixels( GLcontext
*ctx
,
51 GLsizei width
, GLsizei height
,
52 GLenum type
, GLvoid
*pixels
,
53 const struct gl_pixelstore_attrib
*packing
)
55 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
61 /* width should never be > MAX_WIDTH since we did clipping earlier */
62 ASSERT(width
<= MAX_WIDTH
);
64 /* process image row by row */
65 for (i
= 0; i
< height
; i
++) {
66 GLuint index
[MAX_WIDTH
];
68 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
69 rb
->GetRow(ctx
, rb
, width
, x
, y
+ i
, index
);
71 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
72 GL_COLOR_INDEX
, type
, i
, 0);
74 _mesa_pack_index_span(ctx
, width
, type
, dest
, index
,
75 &ctx
->Pack
, ctx
->_ImageTransferState
);
82 * Read pixels for format=GL_DEPTH_COMPONENT.
85 read_depth_pixels( GLcontext
*ctx
,
87 GLsizei width
, GLsizei height
,
88 GLenum type
, GLvoid
*pixels
,
89 const struct gl_pixelstore_attrib
*packing
)
91 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
92 struct gl_renderbuffer
*rb
= fb
->_DepthBuffer
;
93 const GLboolean biasOrScale
94 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
99 /* clipping should have been done already */
102 ASSERT(x
+ width
<= (GLint
) rb
->Width
);
103 ASSERT(y
+ height
<= (GLint
) rb
->Height
);
104 /* width should never be > MAX_WIDTH since we did clipping earlier */
105 ASSERT(width
<= MAX_WIDTH
);
107 if (type
== GL_UNSIGNED_SHORT
&& fb
->Visual
.depthBits
== 16
108 && !biasOrScale
&& !packing
->SwapBytes
) {
109 /* Special case: directly read 16-bit unsigned depth values. */
111 ASSERT(rb
->Format
== MESA_FORMAT_Z16
);
112 ASSERT(rb
->DataType
== GL_UNSIGNED_SHORT
);
113 for (j
= 0; j
< height
; j
++, y
++) {
114 void *dest
=_mesa_image_address2d(packing
, pixels
, width
, height
,
115 GL_DEPTH_COMPONENT
, type
, j
, 0);
116 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
119 else if (type
== GL_UNSIGNED_INT
&& fb
->Visual
.depthBits
== 24
120 && !biasOrScale
&& !packing
->SwapBytes
) {
121 /* Special case: directly read 24-bit unsigned depth values. */
123 ASSERT(rb
->Format
== MESA_FORMAT_X8_Z24
);
124 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
125 for (j
= 0; j
< height
; j
++, y
++) {
126 GLuint
*dest
= (GLuint
*)
127 _mesa_image_address2d(packing
, pixels
, width
, height
,
128 GL_DEPTH_COMPONENT
, type
, j
, 0);
130 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
131 /* convert range from 24-bit to 32-bit */
132 for (k
= 0; k
< width
; k
++) {
133 /* Note: put MSByte of 24-bit value into LSByte */
134 dest
[k
] = (dest
[k
] << 8) | ((dest
[k
] >> 16) & 0xff);
138 else if (type
== GL_UNSIGNED_INT
&& fb
->Visual
.depthBits
== 32
139 && !biasOrScale
&& !packing
->SwapBytes
) {
140 /* Special case: directly read 32-bit unsigned depth values. */
142 ASSERT(rb
->Format
== MESA_FORMAT_Z32
);
143 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
144 for (j
= 0; j
< height
; j
++, y
++) {
145 void *dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
146 GL_DEPTH_COMPONENT
, type
, j
, 0);
147 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
151 /* General case (slower) */
153 for (j
= 0; j
< height
; j
++, y
++) {
154 GLfloat depthValues
[MAX_WIDTH
];
155 GLvoid
*dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
156 GL_DEPTH_COMPONENT
, type
, j
, 0);
157 _swrast_read_depth_span_float(ctx
, rb
, width
, x
, y
, depthValues
);
158 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthValues
, packing
);
165 * Read pixels for format=GL_STENCIL_INDEX.
168 read_stencil_pixels( GLcontext
*ctx
,
170 GLsizei width
, GLsizei height
,
171 GLenum type
, GLvoid
*pixels
,
172 const struct gl_pixelstore_attrib
*packing
)
174 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
175 struct gl_renderbuffer
*rb
= fb
->_StencilBuffer
;
181 /* width should never be > MAX_WIDTH since we did clipping earlier */
182 ASSERT(width
<= MAX_WIDTH
);
184 /* process image row by row */
185 for (j
=0;j
<height
;j
++,y
++) {
187 GLstencil stencil
[MAX_WIDTH
];
189 _swrast_read_stencil_span(ctx
, rb
, width
, x
, y
, stencil
);
191 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
192 GL_STENCIL_INDEX
, type
, j
, 0);
194 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, stencil
, packing
);
201 * Optimized glReadPixels for particular pixel formats when pixel
202 * scaling, biasing, mapping, etc. are disabled.
203 * \return GL_TRUE if success, GL_FALSE if unable to do the readpixels
206 fast_read_rgba_pixels( GLcontext
*ctx
,
208 GLsizei width
, GLsizei height
,
209 GLenum format
, GLenum type
,
211 const struct gl_pixelstore_attrib
*packing
,
212 GLbitfield transferOps
)
214 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
219 ASSERT(rb
->_BaseFormat
== GL_RGBA
|| rb
->_BaseFormat
== GL_RGB
);
221 /* clipping should have already been done */
222 ASSERT(x
+ width
<= (GLint
) rb
->Width
);
223 ASSERT(y
+ height
<= (GLint
) rb
->Height
);
225 /* check for things we can't handle here */
227 packing
->SwapBytes
||
232 if (format
== GL_RGBA
&& rb
->DataType
== type
) {
233 const GLint dstStride
= _mesa_image_row_stride(packing
, width
,
236 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
240 for (row
= 0; row
< height
; row
++) {
241 rb
->GetRow(ctx
, rb
, width
, x
, y
+ row
, dest
);
247 if (format
== GL_RGB
&&
248 rb
->DataType
== GL_UNSIGNED_BYTE
&&
249 type
== GL_UNSIGNED_BYTE
) {
250 const GLint dstStride
= _mesa_image_row_stride(packing
, width
,
253 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
257 for (row
= 0; row
< height
; row
++) {
258 GLubyte tempRow
[MAX_WIDTH
][4];
260 rb
->GetRow(ctx
, rb
, width
, x
, y
+ row
, tempRow
);
261 /* convert RGBA to RGB */
262 for (col
= 0; col
< width
; col
++) {
263 dest
[col
* 3 + 0] = tempRow
[col
][0];
264 dest
[col
* 3 + 1] = tempRow
[col
][1];
265 dest
[col
* 3 + 2] = tempRow
[col
][2];
278 * When we're using a low-precision color buffer (like 16-bit 5/6/5)
279 * we have to adjust our color values a bit to pass conformance.
280 * The problem is when a 5 or 6-bit color value is convert to an 8-bit
281 * value and then a floating point value, the floating point values don't
282 * increment uniformly as the 5 or 6-bit value is incremented.
284 * This function adjusts floating point values to compensate.
287 adjust_colors(GLcontext
*ctx
, GLuint n
, GLfloat rgba
[][4])
289 const GLuint rShift
= 8 - ctx
->Visual
.redBits
;
290 const GLuint gShift
= 8 - ctx
->Visual
.greenBits
;
291 const GLuint bShift
= 8 - ctx
->Visual
.blueBits
;
292 const GLfloat rScale
= 1.0F
/ (GLfloat
) ((1 << ctx
->Visual
.redBits
) - 1);
293 const GLfloat gScale
= 1.0F
/ (GLfloat
) ((1 << ctx
->Visual
.greenBits
) - 1);
294 const GLfloat bScale
= 1.0F
/ (GLfloat
) ((1 << ctx
->Visual
.blueBits
) - 1);
296 for (i
= 0; i
< n
; i
++) {
298 /* convert float back to ubyte */
299 CLAMPED_FLOAT_TO_UBYTE(r
, rgba
[i
][RCOMP
]);
300 CLAMPED_FLOAT_TO_UBYTE(g
, rgba
[i
][GCOMP
]);
301 CLAMPED_FLOAT_TO_UBYTE(b
, rgba
[i
][BCOMP
]);
302 /* using only the N most significant bits of the ubyte value, convert to
305 rgba
[i
][RCOMP
] = (GLfloat
) (r
>> rShift
) * rScale
;
306 rgba
[i
][GCOMP
] = (GLfloat
) (g
>> gShift
) * gScale
;
307 rgba
[i
][BCOMP
] = (GLfloat
) (b
>> bShift
) * bScale
;
314 * Read R, G, B, A, RGB, L, or LA pixels.
317 read_rgba_pixels( GLcontext
*ctx
,
319 GLsizei width
, GLsizei height
,
320 GLenum format
, GLenum type
, GLvoid
*pixels
,
321 const struct gl_pixelstore_attrib
*packing
)
323 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
324 GLbitfield transferOps
= ctx
->_ImageTransferState
;
325 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
326 struct gl_renderbuffer
*rb
= fb
->_ColorReadBuffer
;
331 if (type
== GL_FLOAT
&& ((ctx
->Color
.ClampReadColor
== GL_TRUE
) ||
332 (ctx
->Color
.ClampReadColor
== GL_FIXED_ONLY_ARB
&&
333 rb
->DataType
!= GL_FLOAT
)))
334 transferOps
|= IMAGE_CLAMP_BIT
;
336 /* Try optimized path first */
337 if (fast_read_rgba_pixels(ctx
, x
, y
, width
, height
,
338 format
, type
, pixels
, packing
, transferOps
)) {
342 /* width should never be > MAX_WIDTH since we did clipping earlier */
343 ASSERT(width
<= MAX_WIDTH
);
345 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
346 GLfloat
*dest
, *src
, *tmpImage
, *convImage
;
349 tmpImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
351 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
354 convImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
356 _mesa_free(tmpImage
);
357 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
361 /* read full RGBA, FLOAT image */
363 for (row
= 0; row
< height
; row
++, y
++) {
364 if (fb
->Visual
.rgbMode
) {
365 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, GL_FLOAT
, dest
);
368 GLuint index
[MAX_WIDTH
];
369 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
370 rb
->GetRow(ctx
, rb
, width
, x
, y
, index
);
371 _mesa_apply_ci_transfer_ops(ctx
,
372 transferOps
& IMAGE_SHIFT_OFFSET_BIT
,
374 _mesa_map_ci_to_rgba(ctx
, width
, index
, (GLfloat (*)[4]) dest
);
376 _mesa_apply_rgba_transfer_ops(ctx
,
377 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
,
378 width
, (GLfloat (*)[4]) dest
);
383 if (ctx
->Pixel
.Convolution2DEnabled
) {
384 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
387 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
388 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
390 _mesa_free(tmpImage
);
392 /* finish transfer ops and pack the resulting image */
394 for (row
= 0; row
< height
; row
++) {
396 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
397 format
, type
, row
, 0);
398 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) src
,
399 format
, type
, dest
, packing
,
400 transferOps
& IMAGE_POST_CONVOLUTION_BITS
);
403 _mesa_free(convImage
);
407 const GLint dstStride
408 = _mesa_image_row_stride(packing
, width
, format
, type
);
409 GLfloat (*rgba
)[4] = swrast
->SpanArrays
->attribs
[FRAG_ATTRIB_COL0
];
412 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
415 /* make sure we don't apply 1D convolution */
416 transferOps
&= ~(IMAGE_CONVOLUTION_BIT
|
417 IMAGE_POST_CONVOLUTION_SCALE_BIAS
);
419 for (row
= 0; row
< height
; row
++, y
++) {
421 /* Get float rgba pixels */
422 if (fb
->Visual
.rgbMode
) {
423 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, GL_FLOAT
, rgba
);
426 /* read CI and convert to RGBA */
427 GLuint index
[MAX_WIDTH
];
428 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
429 rb
->GetRow(ctx
, rb
, width
, x
, y
, index
);
430 _mesa_apply_ci_transfer_ops(ctx
,
431 transferOps
& IMAGE_SHIFT_OFFSET_BIT
,
433 _mesa_map_ci_to_rgba(ctx
, width
, index
, rgba
);
436 /* apply fudge factor for shallow color buffers */
437 if (fb
->Visual
.redBits
< 8 ||
438 fb
->Visual
.greenBits
< 8 ||
439 fb
->Visual
.blueBits
< 8) {
440 adjust_colors(ctx
, width
, rgba
);
443 /* pack the row of RGBA pixels into user's buffer */
444 _mesa_pack_rgba_span_float(ctx
, width
, rgba
, format
, type
, dst
,
445 packing
, transferOps
);
454 * Read combined depth/stencil values.
455 * We'll have already done error checking to be sure the expected
456 * depth and stencil buffers really exist.
459 read_depth_stencil_pixels(GLcontext
*ctx
,
461 GLsizei width
, GLsizei height
,
462 GLenum type
, GLvoid
*pixels
,
463 const struct gl_pixelstore_attrib
*packing
)
465 const GLboolean scaleOrBias
466 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
467 const GLboolean stencilTransfer
= ctx
->Pixel
.IndexShift
468 || ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapStencilFlag
;
469 struct gl_renderbuffer
*depthRb
, *stencilRb
;
471 depthRb
= ctx
->ReadBuffer
->_DepthBuffer
;
472 stencilRb
= ctx
->ReadBuffer
->_StencilBuffer
;
474 if (!depthRb
|| !stencilRb
)
477 depthRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
478 stencilRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
480 if (depthRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
481 stencilRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
482 depthRb
== stencilRb
&&
485 /* This is the ideal case.
486 * Reading GL_DEPTH_STENCIL pixels from combined depth/stencil buffer.
487 * Plus, no pixel transfer ops to worry about!
490 GLint dstStride
= _mesa_image_row_stride(packing
, width
,
491 GL_DEPTH_STENCIL_EXT
, type
);
492 GLubyte
*dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
,
494 GL_DEPTH_STENCIL_EXT
,
496 for (i
= 0; i
< height
; i
++) {
497 depthRb
->GetRow(ctx
, depthRb
, width
, x
, y
+ i
, dst
);
502 /* Reading GL_DEPTH_STENCIL pixels from separate depth/stencil buffers,
503 * or we need pixel transfer.
506 depthRb
= ctx
->ReadBuffer
->_DepthBuffer
;
507 stencilRb
= ctx
->ReadBuffer
->_StencilBuffer
;
509 for (i
= 0; i
< height
; i
++) {
510 GLstencil stencilVals
[MAX_WIDTH
];
512 GLuint
*depthStencilDst
= (GLuint
*)
513 _mesa_image_address2d(packing
, pixels
, width
, height
,
514 GL_DEPTH_STENCIL_EXT
, type
, i
, 0);
516 _swrast_read_stencil_span(ctx
, stencilRb
, width
,
517 x
, y
+ i
, stencilVals
);
519 if (!scaleOrBias
&& !stencilTransfer
520 && ctx
->ReadBuffer
->Visual
.depthBits
== 24) {
522 GLuint zVals
[MAX_WIDTH
]; /* 24-bit values! */
524 ASSERT(depthRb
->DataType
== GL_UNSIGNED_INT
);
525 /* note, we've already been clipped */
526 depthRb
->GetRow(ctx
, depthRb
, width
, x
, y
+ i
, zVals
);
527 for (j
= 0; j
< width
; j
++) {
528 depthStencilDst
[j
] = (zVals
[j
] << 8) | (stencilVals
[j
] & 0xff);
533 GLfloat depthVals
[MAX_WIDTH
];
534 _swrast_read_depth_span_float(ctx
, depthRb
, width
, x
, y
+ i
,
536 _mesa_pack_depth_stencil_span(ctx
, width
, depthStencilDst
,
537 depthVals
, stencilVals
, packing
);
546 * Software fallback routine for ctx->Driver.ReadPixels().
547 * By time we get here, all error checking will have been done.
550 _swrast_ReadPixels( GLcontext
*ctx
,
551 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
552 GLenum format
, GLenum type
,
553 const struct gl_pixelstore_attrib
*packing
,
556 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
557 struct gl_pixelstore_attrib clippedPacking
= *packing
;
560 _mesa_update_state(ctx
);
562 /* Need to do swrast_render_start() before clipping or anything else
563 * since this is where a driver may grab the hw lock and get an updated
566 swrast_render_start(ctx
);
568 if (swrast
->NewState
)
569 _swrast_validate_derived( ctx
);
571 /* Do all needed clipping here, so that we can forget about it later */
572 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
573 /* The ReadPixels region is totally outside the window bounds */
574 swrast_render_finish(ctx
);
578 pixels
= _mesa_map_pbo_dest(ctx
, &clippedPacking
, pixels
);
584 read_index_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
587 case GL_STENCIL_INDEX
:
588 read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
591 case GL_DEPTH_COMPONENT
:
592 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
601 case GL_LUMINANCE_ALPHA
:
606 read_rgba_pixels(ctx
, x
, y
, width
, height
,
607 format
, type
, pixels
, &clippedPacking
);
609 case GL_DEPTH_STENCIL_EXT
:
610 read_depth_stencil_pixels(ctx
, x
, y
, width
, height
,
611 type
, pixels
, &clippedPacking
);
614 _mesa_problem(ctx
, "unexpected format in _swrast_ReadPixels");
615 /* don't return yet, clean-up */
618 swrast_render_finish(ctx
);
620 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);