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/colormac.h"
28 #include "main/feedback.h"
29 #include "main/formats.h"
30 #include "main/format_unpack.h"
31 #include "main/image.h"
32 #include "main/imports.h"
33 #include "main/macros.h"
34 #include "main/pack.h"
36 #include "main/state.h"
38 #include "s_context.h"
41 #include "s_stencil.h"
44 * Tries to implement glReadPixels() of GL_DEPTH_COMPONENT using memcpy of the
48 fast_read_depth_pixels( struct gl_context
*ctx
,
50 GLsizei width
, GLsizei height
,
51 GLenum type
, GLvoid
*pixels
,
52 const struct gl_pixelstore_attrib
*packing
)
54 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
55 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
57 int stride
, dstStride
, j
;
59 if (ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0)
62 if (packing
->SwapBytes
)
65 if (_mesa_get_format_datatype(rb
->Format
) != GL_UNSIGNED_INT
)
68 if (!((type
== GL_UNSIGNED_SHORT
&& rb
->Format
== MESA_FORMAT_Z16
) ||
69 type
== GL_UNSIGNED_INT
))
72 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
75 dstStride
= _mesa_image_row_stride(packing
, width
, GL_DEPTH_COMPONENT
, type
);
76 dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
77 GL_DEPTH_COMPONENT
, type
, 0, 0);
79 for (j
= 0; j
< height
; j
++) {
80 if (type
== GL_UNSIGNED_INT
) {
81 _mesa_unpack_uint_z_row(rb
->Format
, width
, map
, (GLuint
*)dst
);
83 ASSERT(type
== GL_UNSIGNED_SHORT
&& rb
->Format
== MESA_FORMAT_Z16
);
84 memcpy(dst
, map
, width
* 2);
90 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
96 * Read pixels for format=GL_DEPTH_COMPONENT.
99 read_depth_pixels( struct gl_context
*ctx
,
101 GLsizei width
, GLsizei height
,
102 GLenum type
, GLvoid
*pixels
,
103 const struct gl_pixelstore_attrib
*packing
)
105 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
106 struct gl_renderbuffer
*rb
= fb
->_DepthBuffer
;
112 /* clipping should have been done already */
115 ASSERT(x
+ width
<= (GLint
) rb
->Width
);
116 ASSERT(y
+ height
<= (GLint
) rb
->Height
);
117 /* width should never be > MAX_WIDTH since we did clipping earlier */
118 ASSERT(width
<= MAX_WIDTH
);
120 if (fast_read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
, packing
))
123 /* General case (slower) */
124 for (j
= 0; j
< height
; j
++, y
++) {
125 GLfloat depthValues
[MAX_WIDTH
];
126 GLvoid
*dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
127 GL_DEPTH_COMPONENT
, type
, j
, 0);
128 _swrast_read_depth_span_float(ctx
, rb
, width
, x
, y
, depthValues
);
129 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthValues
, packing
);
135 * Read pixels for format=GL_STENCIL_INDEX.
138 read_stencil_pixels( struct gl_context
*ctx
,
140 GLsizei width
, GLsizei height
,
141 GLenum type
, GLvoid
*pixels
,
142 const struct gl_pixelstore_attrib
*packing
)
144 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
145 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
153 /* width should never be > MAX_WIDTH since we did clipping earlier */
154 ASSERT(width
<= MAX_WIDTH
);
156 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
, GL_MAP_READ_BIT
,
159 /* process image row by row */
160 for (j
= 0; j
< height
; j
++) {
162 GLstencil stencil
[MAX_WIDTH
];
164 _mesa_unpack_ubyte_stencil_row(rb
->Format
, width
, map
, stencil
);
165 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
166 GL_STENCIL_INDEX
, type
, j
, 0);
168 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, stencil
, packing
);
173 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
179 * Optimized glReadPixels for particular pixel formats when pixel
180 * scaling, biasing, mapping, etc. are disabled.
181 * \return GL_TRUE if success, GL_FALSE if unable to do the readpixels
184 fast_read_rgba_pixels( struct gl_context
*ctx
,
186 GLsizei width
, GLsizei height
,
187 GLenum format
, GLenum type
,
189 const struct gl_pixelstore_attrib
*packing
,
190 GLbitfield transferOps
)
192 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
197 ASSERT(rb
->_BaseFormat
== GL_RGBA
||
198 rb
->_BaseFormat
== GL_RGB
||
199 rb
->_BaseFormat
== GL_RG
||
200 rb
->_BaseFormat
== GL_RED
||
201 rb
->_BaseFormat
== GL_LUMINANCE
||
202 rb
->_BaseFormat
== GL_INTENSITY
||
203 rb
->_BaseFormat
== GL_LUMINANCE_ALPHA
||
204 rb
->_BaseFormat
== GL_ALPHA
);
206 /* clipping should have already been done */
207 ASSERT(x
+ width
<= (GLint
) rb
->Width
);
208 ASSERT(y
+ height
<= (GLint
) rb
->Height
);
210 /* check for things we can't handle here */
212 packing
->SwapBytes
||
217 if (format
== GL_RGBA
&& rb
->DataType
== type
) {
218 const GLint dstStride
= _mesa_image_row_stride(packing
, width
,
221 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
225 for (row
= 0; row
< height
; row
++) {
226 rb
->GetRow(ctx
, rb
, width
, x
, y
+ row
, dest
);
232 if (format
== GL_RGB
&&
233 rb
->DataType
== GL_UNSIGNED_BYTE
&&
234 type
== GL_UNSIGNED_BYTE
) {
235 const GLint dstStride
= _mesa_image_row_stride(packing
, width
,
238 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
242 for (row
= 0; row
< height
; row
++) {
243 GLubyte tempRow
[MAX_WIDTH
][4];
245 rb
->GetRow(ctx
, rb
, width
, x
, y
+ row
, tempRow
);
246 /* convert RGBA to RGB */
247 for (col
= 0; col
< width
; col
++) {
248 dest
[col
* 3 + 0] = tempRow
[col
][0];
249 dest
[col
* 3 + 1] = tempRow
[col
][1];
250 dest
[col
* 3 + 2] = tempRow
[col
][2];
263 * When we're using a low-precision color buffer (like 16-bit 5/6/5)
264 * we have to adjust our color values a bit to pass conformance.
265 * The problem is when a 5 or 6-bit color value is converted to an 8-bit
266 * value and then a floating point value, the floating point values don't
267 * increment uniformly as the 5 or 6-bit value is incremented.
269 * This function adjusts floating point values to compensate.
272 adjust_colors(const struct gl_framebuffer
*fb
, GLuint n
, GLfloat rgba
[][4])
274 const GLuint rShift
= 8 - fb
->Visual
.redBits
;
275 const GLuint gShift
= 8 - fb
->Visual
.greenBits
;
276 const GLuint bShift
= 8 - fb
->Visual
.blueBits
;
277 GLfloat rScale
= 1.0F
/ (GLfloat
) ((1 << fb
->Visual
.redBits
) - 1);
278 GLfloat gScale
= 1.0F
/ (GLfloat
) ((1 << fb
->Visual
.greenBits
) - 1);
279 GLfloat bScale
= 1.0F
/ (GLfloat
) ((1 << fb
->Visual
.blueBits
) - 1);
282 if (fb
->Visual
.redBits
== 0)
284 if (fb
->Visual
.greenBits
== 0)
286 if (fb
->Visual
.blueBits
== 0)
289 for (i
= 0; i
< n
; i
++) {
291 /* convert float back to ubyte */
292 CLAMPED_FLOAT_TO_UBYTE(r
, rgba
[i
][RCOMP
]);
293 CLAMPED_FLOAT_TO_UBYTE(g
, rgba
[i
][GCOMP
]);
294 CLAMPED_FLOAT_TO_UBYTE(b
, rgba
[i
][BCOMP
]);
295 /* using only the N most significant bits of the ubyte value, convert to
298 rgba
[i
][RCOMP
] = (GLfloat
) (r
>> rShift
) * rScale
;
299 rgba
[i
][GCOMP
] = (GLfloat
) (g
>> gShift
) * gScale
;
300 rgba
[i
][BCOMP
] = (GLfloat
) (b
>> bShift
) * bScale
;
307 * Read R, G, B, A, RGB, L, or LA pixels.
310 read_rgba_pixels( struct gl_context
*ctx
,
312 GLsizei width
, GLsizei height
,
313 GLenum format
, GLenum type
, GLvoid
*pixels
,
314 const struct gl_pixelstore_attrib
*packing
)
316 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
317 GLbitfield transferOps
= ctx
->_ImageTransferState
;
318 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
319 struct gl_renderbuffer
*rb
= fb
->_ColorReadBuffer
;
324 if ((ctx
->Color
._ClampReadColor
== GL_TRUE
|| type
!= GL_FLOAT
) &&
325 !_mesa_is_integer_format(format
)) {
326 transferOps
|= IMAGE_CLAMP_BIT
;
329 /* Try the optimized path first. */
330 if (fast_read_rgba_pixels(ctx
, x
, y
, width
, height
,
331 format
, type
, pixels
, packing
, transferOps
)) {
335 /* width should never be > MAX_WIDTH since we did clipping earlier */
336 ASSERT(width
<= MAX_WIDTH
);
339 const GLint dstStride
340 = _mesa_image_row_stride(packing
, width
, format
, type
);
341 GLfloat (*rgba
)[4] = swrast
->SpanArrays
->attribs
[FRAG_ATTRIB_COL0
];
344 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
347 for (row
= 0; row
< height
; row
++, y
++) {
349 /* Get float rgba pixels */
350 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, GL_FLOAT
, rgba
);
352 /* apply fudge factor for shallow color buffers */
353 if ((fb
->Visual
.redBits
< 8 && fb
->Visual
.redBits
!= 0) ||
354 (fb
->Visual
.greenBits
< 8 && fb
->Visual
.greenBits
!= 0) ||
355 (fb
->Visual
.blueBits
< 8 && fb
->Visual
.blueBits
!= 0)) {
356 adjust_colors(fb
, width
, rgba
);
359 /* pack the row of RGBA pixels into user's buffer */
360 _mesa_pack_rgba_span_float(ctx
, width
, rgba
, format
, type
, dst
,
361 packing
, transferOps
);
370 * Read combined depth/stencil values.
371 * We'll have already done error checking to be sure the expected
372 * depth and stencil buffers really exist.
375 read_depth_stencil_pixels(struct gl_context
*ctx
,
377 GLsizei width
, GLsizei height
,
378 GLenum type
, GLvoid
*pixels
,
379 const struct gl_pixelstore_attrib
*packing
)
381 const GLboolean scaleOrBias
382 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
383 const GLboolean stencilTransfer
= ctx
->Pixel
.IndexShift
384 || ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapStencilFlag
;
385 struct gl_renderbuffer
*depthRb
, *stencilRb
;
387 depthRb
= ctx
->ReadBuffer
->_DepthBuffer
;
388 stencilRb
= ctx
->ReadBuffer
->_StencilBuffer
;
390 if (!depthRb
|| !stencilRb
)
393 depthRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
394 stencilRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
396 if (depthRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
397 depthRb
->Format
== MESA_FORMAT_Z24_S8
&&
398 type
== GL_UNSIGNED_INT_24_8
&&
399 depthRb
== stencilRb
&&
400 depthRb
->GetRow
&& /* May be null if depthRb is a wrapper around
401 * separate depth and stencil buffers. */
404 /* This is the ideal case.
405 * Reading GL_DEPTH_STENCIL pixels from combined depth/stencil buffer.
406 * Plus, no pixel transfer ops to worry about!
409 GLint dstStride
= _mesa_image_row_stride(packing
, width
,
410 GL_DEPTH_STENCIL_EXT
, type
);
411 GLubyte
*dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
,
413 GL_DEPTH_STENCIL_EXT
,
415 for (i
= 0; i
< height
; i
++) {
416 depthRb
->GetRow(ctx
, depthRb
, width
, x
, y
+ i
, dst
);
421 /* Reading GL_DEPTH_STENCIL pixels from separate depth/stencil buffers,
422 * or we need pixel transfer.
425 depthRb
= ctx
->ReadBuffer
->_DepthBuffer
;
426 stencilRb
= ctx
->ReadBuffer
->_StencilBuffer
;
428 for (i
= 0; i
< height
; i
++) {
429 GLstencil stencilVals
[MAX_WIDTH
];
431 GLuint
*depthStencilDst
= (GLuint
*)
432 _mesa_image_address2d(packing
, pixels
, width
, height
,
433 GL_DEPTH_STENCIL_EXT
, type
, i
, 0);
435 _swrast_read_stencil_span(ctx
, stencilRb
, width
,
436 x
, y
+ i
, stencilVals
);
438 if (!scaleOrBias
&& !stencilTransfer
439 && ctx
->ReadBuffer
->Visual
.depthBits
== 24) {
441 GLuint zVals
[MAX_WIDTH
]; /* 24-bit values! */
443 ASSERT(depthRb
->DataType
== GL_UNSIGNED_INT
);
444 /* note, we've already been clipped */
445 depthRb
->GetRow(ctx
, depthRb
, width
, x
, y
+ i
, zVals
);
446 for (j
= 0; j
< width
; j
++) {
447 depthStencilDst
[j
] = (zVals
[j
] << 8) | (stencilVals
[j
] & 0xff);
452 GLfloat depthVals
[MAX_WIDTH
];
453 _swrast_read_depth_span_float(ctx
, depthRb
, width
, x
, y
+ i
,
455 _mesa_pack_depth_stencil_span(ctx
, width
, type
, depthStencilDst
,
456 depthVals
, stencilVals
, packing
);
465 * Software fallback routine for ctx->Driver.ReadPixels().
466 * By time we get here, all error checking will have been done.
469 _swrast_ReadPixels( struct gl_context
*ctx
,
470 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
471 GLenum format
, GLenum type
,
472 const struct gl_pixelstore_attrib
*packing
,
475 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
476 struct gl_pixelstore_attrib clippedPacking
= *packing
;
479 _mesa_update_state(ctx
);
481 /* Need to do swrast_render_start() before clipping or anything else
482 * since this is where a driver may grab the hw lock and get an updated
485 swrast_render_start(ctx
);
487 if (swrast
->NewState
)
488 _swrast_validate_derived( ctx
);
490 /* Do all needed clipping here, so that we can forget about it later */
491 if (_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
493 pixels
= _mesa_map_pbo_dest(ctx
, &clippedPacking
, pixels
);
497 case GL_STENCIL_INDEX
:
498 read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
501 case GL_DEPTH_COMPONENT
:
502 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
505 case GL_DEPTH_STENCIL_EXT
:
506 read_depth_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
510 /* all other formats should be color formats */
511 read_rgba_pixels(ctx
, x
, y
, width
, height
, format
, type
, pixels
,
515 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
519 swrast_render_finish(ctx
);