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/image.h"
33 #include "main/macros.h"
34 #include "main/imports.h"
35 #include "main/pixel.h"
36 #include "main/state.h"
38 #include "s_context.h"
41 #include "s_stencil.h"
45 * Read a block of color index pixels.
48 read_index_pixels( GLcontext
*ctx
,
50 GLsizei width
, GLsizei height
,
51 GLenum type
, GLvoid
*pixels
,
52 const struct gl_pixelstore_attrib
*packing
)
54 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
60 /* width should never be > MAX_WIDTH since we did clipping earlier */
61 ASSERT(width
<= MAX_WIDTH
);
63 /* process image row by row */
64 for (i
= 0; i
< height
; i
++) {
65 GLuint index
[MAX_WIDTH
];
67 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
68 rb
->GetRow(ctx
, rb
, width
, x
, y
+ i
, index
);
70 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
71 GL_COLOR_INDEX
, type
, i
, 0);
73 _mesa_pack_index_span(ctx
, width
, type
, dest
, index
,
74 &ctx
->Pack
, ctx
->_ImageTransferState
);
81 * Read pixels for format=GL_DEPTH_COMPONENT.
84 read_depth_pixels( GLcontext
*ctx
,
86 GLsizei width
, GLsizei height
,
87 GLenum type
, GLvoid
*pixels
,
88 const struct gl_pixelstore_attrib
*packing
)
90 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
91 struct gl_renderbuffer
*rb
= fb
->_DepthBuffer
;
92 const GLboolean biasOrScale
93 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
98 /* clipping should have been done already */
101 ASSERT(x
+ width
<= (GLint
) rb
->Width
);
102 ASSERT(y
+ height
<= (GLint
) rb
->Height
);
103 /* width should never be > MAX_WIDTH since we did clipping earlier */
104 ASSERT(width
<= MAX_WIDTH
);
106 if (type
== GL_UNSIGNED_SHORT
&& fb
->Visual
.depthBits
== 16
107 && !biasOrScale
&& !packing
->SwapBytes
) {
108 /* Special case: directly read 16-bit unsigned depth values. */
110 ASSERT(rb
->InternalFormat
== GL_DEPTH_COMPONENT16
);
111 ASSERT(rb
->DataType
== GL_UNSIGNED_SHORT
);
112 for (j
= 0; j
< height
; j
++, y
++) {
113 void *dest
=_mesa_image_address2d(packing
, pixels
, width
, height
,
114 GL_DEPTH_COMPONENT
, type
, j
, 0);
115 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
118 else if (type
== GL_UNSIGNED_INT
&& fb
->Visual
.depthBits
== 24
119 && !biasOrScale
&& !packing
->SwapBytes
) {
120 /* Special case: directly read 24-bit unsigned depth values. */
122 ASSERT(rb
->InternalFormat
== GL_DEPTH_COMPONENT24
);
123 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
124 for (j
= 0; j
< height
; j
++, y
++) {
125 GLuint
*dest
= (GLuint
*)
126 _mesa_image_address2d(packing
, pixels
, width
, height
,
127 GL_DEPTH_COMPONENT
, type
, j
, 0);
129 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
130 /* convert range from 24-bit to 32-bit */
131 for (k
= 0; k
< width
; k
++) {
132 /* Note: put MSByte of 24-bit value into LSByte */
133 dest
[k
] = (dest
[k
] << 8) | ((dest
[k
] >> 16) & 0xff);
137 else if (type
== GL_UNSIGNED_INT
&& fb
->Visual
.depthBits
== 32
138 && !biasOrScale
&& !packing
->SwapBytes
) {
139 /* Special case: directly read 32-bit unsigned depth values. */
141 ASSERT(rb
->InternalFormat
== GL_DEPTH_COMPONENT32
);
142 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
143 for (j
= 0; j
< height
; j
++, y
++) {
144 void *dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
145 GL_DEPTH_COMPONENT
, type
, j
, 0);
146 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
150 /* General case (slower) */
152 for (j
= 0; j
< height
; j
++, y
++) {
153 GLfloat depthValues
[MAX_WIDTH
];
154 GLvoid
*dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
155 GL_DEPTH_COMPONENT
, type
, j
, 0);
156 _swrast_read_depth_span_float(ctx
, rb
, width
, x
, y
, depthValues
);
157 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthValues
, packing
);
164 * Read pixels for format=GL_STENCIL_INDEX.
167 read_stencil_pixels( GLcontext
*ctx
,
169 GLsizei width
, GLsizei height
,
170 GLenum type
, GLvoid
*pixels
,
171 const struct gl_pixelstore_attrib
*packing
)
173 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
174 struct gl_renderbuffer
*rb
= fb
->_StencilBuffer
;
180 /* width should never be > MAX_WIDTH since we did clipping earlier */
181 ASSERT(width
<= MAX_WIDTH
);
183 /* process image row by row */
184 for (j
=0;j
<height
;j
++,y
++) {
186 GLstencil stencil
[MAX_WIDTH
];
188 _swrast_read_stencil_span(ctx
, rb
, width
, x
, y
, stencil
);
190 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
191 GL_STENCIL_INDEX
, type
, j
, 0);
193 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, stencil
, packing
);
200 * Optimized glReadPixels for particular pixel formats when pixel
201 * scaling, biasing, mapping, etc. are disabled.
202 * \return GL_TRUE if success, GL_FALSE if unable to do the readpixels
205 fast_read_rgba_pixels( GLcontext
*ctx
,
207 GLsizei width
, GLsizei height
,
208 GLenum format
, GLenum type
,
210 const struct gl_pixelstore_attrib
*packing
,
211 GLbitfield transferOps
)
213 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
218 ASSERT(rb
->_BaseFormat
== GL_RGBA
|| rb
->_BaseFormat
== GL_RGB
);
220 /* clipping should have already been done */
221 ASSERT(x
+ width
<= (GLint
) rb
->Width
);
222 ASSERT(y
+ height
<= (GLint
) rb
->Height
);
224 /* check for things we can't handle here */
226 packing
->SwapBytes
||
231 if (format
== GL_RGBA
&& rb
->DataType
== type
) {
232 const GLint dstStride
= _mesa_image_row_stride(packing
, width
,
235 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
239 for (row
= 0; row
< height
; row
++) {
240 rb
->GetRow(ctx
, rb
, width
, x
, y
+ row
, dest
);
246 if (format
== GL_RGB
&&
247 rb
->DataType
== GL_UNSIGNED_BYTE
&&
248 type
== GL_UNSIGNED_BYTE
) {
249 const GLint dstStride
= _mesa_image_row_stride(packing
, width
,
252 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
256 for (row
= 0; row
< height
; row
++) {
257 GLubyte tempRow
[MAX_WIDTH
][4];
259 rb
->GetRow(ctx
, rb
, width
, x
, y
+ row
, tempRow
);
260 /* convert RGBA to RGB */
261 for (col
= 0; col
< width
; col
++) {
262 dest
[col
* 3 + 0] = tempRow
[col
][0];
263 dest
[col
* 3 + 1] = tempRow
[col
][1];
264 dest
[col
* 3 + 2] = tempRow
[col
][2];
277 * When we're using a low-precision color buffer (like 16-bit 5/6/5)
278 * we have to adjust our color values a bit to pass conformance.
279 * The problem is when a 5 or 6-bit color value is convert to an 8-bit
280 * value and then a floating point value, the floating point values don't
281 * increment uniformly as the 5 or 6-bit value is incremented.
283 * This function adjusts floating point values to compensate.
286 adjust_colors(GLcontext
*ctx
, GLuint n
, GLfloat rgba
[][4])
288 const GLuint rShift
= 8 - ctx
->Visual
.redBits
;
289 const GLuint gShift
= 8 - ctx
->Visual
.greenBits
;
290 const GLuint bShift
= 8 - ctx
->Visual
.blueBits
;
291 const GLfloat rScale
= 1.0F
/ (GLfloat
) ((1 << ctx
->Visual
.redBits
) - 1);
292 const GLfloat gScale
= 1.0F
/ (GLfloat
) ((1 << ctx
->Visual
.greenBits
) - 1);
293 const GLfloat bScale
= 1.0F
/ (GLfloat
) ((1 << ctx
->Visual
.blueBits
) - 1);
295 for (i
= 0; i
< n
; i
++) {
297 /* convert float back to ubyte */
298 CLAMPED_FLOAT_TO_UBYTE(r
, rgba
[i
][RCOMP
]);
299 CLAMPED_FLOAT_TO_UBYTE(g
, rgba
[i
][GCOMP
]);
300 CLAMPED_FLOAT_TO_UBYTE(b
, rgba
[i
][BCOMP
]);
301 /* using only the N most significant bits of the ubyte value, convert to
304 rgba
[i
][RCOMP
] = (GLfloat
) (r
>> rShift
) * rScale
;
305 rgba
[i
][GCOMP
] = (GLfloat
) (g
>> gShift
) * gScale
;
306 rgba
[i
][BCOMP
] = (GLfloat
) (b
>> bShift
) * bScale
;
313 * Read R, G, B, A, RGB, L, or LA pixels.
316 read_rgba_pixels( GLcontext
*ctx
,
318 GLsizei width
, GLsizei height
,
319 GLenum format
, GLenum type
, GLvoid
*pixels
,
320 const struct gl_pixelstore_attrib
*packing
)
322 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
323 GLbitfield transferOps
= ctx
->_ImageTransferState
;
324 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
325 struct gl_renderbuffer
*rb
= fb
->_ColorReadBuffer
;
330 if (type
== GL_FLOAT
&& ((ctx
->Color
.ClampReadColor
== GL_TRUE
) ||
331 (ctx
->Color
.ClampReadColor
== GL_FIXED_ONLY_ARB
&&
332 rb
->DataType
!= GL_FLOAT
)))
333 transferOps
|= IMAGE_CLAMP_BIT
;
335 /* Try optimized path first */
336 if (fast_read_rgba_pixels(ctx
, x
, y
, width
, height
,
337 format
, type
, pixels
, packing
, transferOps
)) {
341 /* width should never be > MAX_WIDTH since we did clipping earlier */
342 ASSERT(width
<= MAX_WIDTH
);
344 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
345 GLfloat
*dest
, *src
, *tmpImage
, *convImage
;
348 tmpImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
350 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
353 convImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
355 _mesa_free(tmpImage
);
356 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
360 /* read full RGBA, FLOAT image */
362 for (row
= 0; row
< height
; row
++, y
++) {
363 if (fb
->Visual
.rgbMode
) {
364 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, GL_FLOAT
, dest
);
367 GLuint index
[MAX_WIDTH
];
368 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
369 rb
->GetRow(ctx
, rb
, width
, x
, y
, index
);
370 _mesa_apply_ci_transfer_ops(ctx
,
371 transferOps
& IMAGE_SHIFT_OFFSET_BIT
,
373 _mesa_map_ci_to_rgba(ctx
, width
, index
, (GLfloat (*)[4]) dest
);
375 _mesa_apply_rgba_transfer_ops(ctx
,
376 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
,
377 width
, (GLfloat (*)[4]) dest
);
382 if (ctx
->Pixel
.Convolution2DEnabled
) {
383 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
386 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
387 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
389 _mesa_free(tmpImage
);
391 /* finish transfer ops and pack the resulting image */
393 for (row
= 0; row
< height
; row
++) {
395 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
396 format
, type
, row
, 0);
397 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) src
,
398 format
, type
, dest
, packing
,
399 transferOps
& IMAGE_POST_CONVOLUTION_BITS
);
402 _mesa_free(convImage
);
406 const GLint dstStride
407 = _mesa_image_row_stride(packing
, width
, format
, type
);
408 GLfloat (*rgba
)[4] = swrast
->SpanArrays
->attribs
[FRAG_ATTRIB_COL0
];
411 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
414 /* make sure we don't apply 1D convolution */
415 transferOps
&= ~(IMAGE_CONVOLUTION_BIT
|
416 IMAGE_POST_CONVOLUTION_SCALE_BIAS
);
418 for (row
= 0; row
< height
; row
++, y
++) {
420 /* Get float rgba pixels */
421 if (fb
->Visual
.rgbMode
) {
422 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, GL_FLOAT
, rgba
);
425 /* read CI and convert to RGBA */
426 GLuint index
[MAX_WIDTH
];
427 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
428 rb
->GetRow(ctx
, rb
, width
, x
, y
, index
);
429 _mesa_apply_ci_transfer_ops(ctx
,
430 transferOps
& IMAGE_SHIFT_OFFSET_BIT
,
432 _mesa_map_ci_to_rgba(ctx
, width
, index
, rgba
);
435 /* apply fudge factor for shallow color buffers */
436 if (fb
->Visual
.redBits
< 8 ||
437 fb
->Visual
.greenBits
< 8 ||
438 fb
->Visual
.blueBits
< 8) {
439 adjust_colors(ctx
, width
, rgba
);
442 /* pack the row of RGBA pixels into user's buffer */
443 _mesa_pack_rgba_span_float(ctx
, width
, rgba
, format
, type
, dst
,
444 packing
, transferOps
);
453 * Read combined depth/stencil values.
454 * We'll have already done error checking to be sure the expected
455 * depth and stencil buffers really exist.
458 read_depth_stencil_pixels(GLcontext
*ctx
,
460 GLsizei width
, GLsizei height
,
461 GLenum type
, GLvoid
*pixels
,
462 const struct gl_pixelstore_attrib
*packing
)
464 const GLboolean scaleOrBias
465 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
466 const GLboolean stencilTransfer
= ctx
->Pixel
.IndexShift
467 || ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapStencilFlag
;
468 struct gl_renderbuffer
*depthRb
, *stencilRb
;
470 depthRb
= ctx
->ReadBuffer
->_DepthBuffer
;
471 stencilRb
= ctx
->ReadBuffer
->_StencilBuffer
;
473 if (!depthRb
|| !stencilRb
)
476 depthRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
477 stencilRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
479 if (depthRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
480 stencilRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
481 depthRb
== stencilRb
&&
484 /* This is the ideal case.
485 * Reading GL_DEPTH_STENCIL pixels from combined depth/stencil buffer.
486 * Plus, no pixel transfer ops to worry about!
489 GLint dstStride
= _mesa_image_row_stride(packing
, width
,
490 GL_DEPTH_STENCIL_EXT
, type
);
491 GLubyte
*dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
,
493 GL_DEPTH_STENCIL_EXT
,
495 for (i
= 0; i
< height
; i
++) {
496 depthRb
->GetRow(ctx
, depthRb
, width
, x
, y
+ i
, dst
);
501 /* Reading GL_DEPTH_STENCIL pixels from separate depth/stencil buffers,
502 * or we need pixel transfer.
505 depthRb
= ctx
->ReadBuffer
->_DepthBuffer
;
506 stencilRb
= ctx
->ReadBuffer
->_StencilBuffer
;
508 for (i
= 0; i
< height
; i
++) {
509 GLstencil stencilVals
[MAX_WIDTH
];
511 GLuint
*depthStencilDst
= (GLuint
*)
512 _mesa_image_address2d(packing
, pixels
, width
, height
,
513 GL_DEPTH_STENCIL_EXT
, type
, i
, 0);
515 _swrast_read_stencil_span(ctx
, stencilRb
, width
,
516 x
, y
+ i
, stencilVals
);
518 if (!scaleOrBias
&& !stencilTransfer
519 && ctx
->ReadBuffer
->Visual
.depthBits
== 24) {
521 GLuint zVals
[MAX_WIDTH
]; /* 24-bit values! */
523 ASSERT(depthRb
->DataType
== GL_UNSIGNED_INT
);
524 /* note, we've already been clipped */
525 depthRb
->GetRow(ctx
, depthRb
, width
, x
, y
+ i
, zVals
);
526 for (j
= 0; j
< width
; j
++) {
527 depthStencilDst
[j
] = (zVals
[j
] << 8) | (stencilVals
[j
] & 0xff);
532 GLfloat depthVals
[MAX_WIDTH
];
533 _swrast_read_depth_span_float(ctx
, depthRb
, width
, x
, y
+ i
,
535 _mesa_pack_depth_stencil_span(ctx
, width
, depthStencilDst
,
536 depthVals
, stencilVals
, packing
);
545 * Software fallback routine for ctx->Driver.ReadPixels().
546 * By time we get here, all error checking will have been done.
549 _swrast_ReadPixels( GLcontext
*ctx
,
550 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
551 GLenum format
, GLenum type
,
552 const struct gl_pixelstore_attrib
*packing
,
555 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
556 struct gl_pixelstore_attrib clippedPacking
= *packing
;
558 /* Need to do RENDER_START before clipping or anything else since this
559 * is where a driver may grab the hw lock and get an updated window
562 RENDER_START(swrast
, ctx
);
565 _mesa_update_state(ctx
);
567 if (swrast
->NewState
)
568 _swrast_validate_derived( ctx
);
570 /* Do all needed clipping here, so that we can forget about it later */
571 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
572 /* The ReadPixels region is totally outside the window bounds */
573 RENDER_FINISH(swrast
, ctx
);
577 pixels
= _mesa_map_readpix_pbo(ctx
, &clippedPacking
, pixels
);
583 read_index_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
586 case GL_STENCIL_INDEX
:
587 read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
590 case GL_DEPTH_COMPONENT
:
591 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
600 case GL_LUMINANCE_ALPHA
:
605 read_rgba_pixels(ctx
, x
, y
, width
, height
,
606 format
, type
, pixels
, &clippedPacking
);
608 case GL_DEPTH_STENCIL_EXT
:
609 read_depth_stencil_pixels(ctx
, x
, y
, width
, height
,
610 type
, pixels
, &clippedPacking
);
613 _mesa_problem(ctx
, "unexpected format in _swrast_ReadPixels");
614 /* don't return yet, clean-up */
617 RENDER_FINISH(swrast
, ctx
);
619 _mesa_unmap_readpix_pbo(ctx
, &clippedPacking
);