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
->_StencilBuffer
;
151 /* width should never be > MAX_WIDTH since we did clipping earlier */
152 ASSERT(width
<= MAX_WIDTH
);
154 /* process image row by row */
155 for (j
=0;j
<height
;j
++,y
++) {
157 GLstencil stencil
[MAX_WIDTH
];
159 _swrast_read_stencil_span(ctx
, rb
, width
, x
, y
, stencil
);
161 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
162 GL_STENCIL_INDEX
, type
, j
, 0);
164 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, stencil
, packing
);
171 * Optimized glReadPixels for particular pixel formats when pixel
172 * scaling, biasing, mapping, etc. are disabled.
173 * \return GL_TRUE if success, GL_FALSE if unable to do the readpixels
176 fast_read_rgba_pixels( struct gl_context
*ctx
,
178 GLsizei width
, GLsizei height
,
179 GLenum format
, GLenum type
,
181 const struct gl_pixelstore_attrib
*packing
,
182 GLbitfield transferOps
)
184 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
189 ASSERT(rb
->_BaseFormat
== GL_RGBA
||
190 rb
->_BaseFormat
== GL_RGB
||
191 rb
->_BaseFormat
== GL_RG
||
192 rb
->_BaseFormat
== GL_RED
||
193 rb
->_BaseFormat
== GL_LUMINANCE
||
194 rb
->_BaseFormat
== GL_INTENSITY
||
195 rb
->_BaseFormat
== GL_LUMINANCE_ALPHA
||
196 rb
->_BaseFormat
== GL_ALPHA
);
198 /* clipping should have already been done */
199 ASSERT(x
+ width
<= (GLint
) rb
->Width
);
200 ASSERT(y
+ height
<= (GLint
) rb
->Height
);
202 /* check for things we can't handle here */
204 packing
->SwapBytes
||
209 if (format
== GL_RGBA
&& rb
->DataType
== type
) {
210 const GLint dstStride
= _mesa_image_row_stride(packing
, width
,
213 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
217 for (row
= 0; row
< height
; row
++) {
218 rb
->GetRow(ctx
, rb
, width
, x
, y
+ row
, dest
);
224 if (format
== GL_RGB
&&
225 rb
->DataType
== GL_UNSIGNED_BYTE
&&
226 type
== GL_UNSIGNED_BYTE
) {
227 const GLint dstStride
= _mesa_image_row_stride(packing
, width
,
230 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
234 for (row
= 0; row
< height
; row
++) {
235 GLubyte tempRow
[MAX_WIDTH
][4];
237 rb
->GetRow(ctx
, rb
, width
, x
, y
+ row
, tempRow
);
238 /* convert RGBA to RGB */
239 for (col
= 0; col
< width
; col
++) {
240 dest
[col
* 3 + 0] = tempRow
[col
][0];
241 dest
[col
* 3 + 1] = tempRow
[col
][1];
242 dest
[col
* 3 + 2] = tempRow
[col
][2];
255 * When we're using a low-precision color buffer (like 16-bit 5/6/5)
256 * we have to adjust our color values a bit to pass conformance.
257 * The problem is when a 5 or 6-bit color value is converted to an 8-bit
258 * value and then a floating point value, the floating point values don't
259 * increment uniformly as the 5 or 6-bit value is incremented.
261 * This function adjusts floating point values to compensate.
264 adjust_colors(const struct gl_framebuffer
*fb
, GLuint n
, GLfloat rgba
[][4])
266 const GLuint rShift
= 8 - fb
->Visual
.redBits
;
267 const GLuint gShift
= 8 - fb
->Visual
.greenBits
;
268 const GLuint bShift
= 8 - fb
->Visual
.blueBits
;
269 GLfloat rScale
= 1.0F
/ (GLfloat
) ((1 << fb
->Visual
.redBits
) - 1);
270 GLfloat gScale
= 1.0F
/ (GLfloat
) ((1 << fb
->Visual
.greenBits
) - 1);
271 GLfloat bScale
= 1.0F
/ (GLfloat
) ((1 << fb
->Visual
.blueBits
) - 1);
274 if (fb
->Visual
.redBits
== 0)
276 if (fb
->Visual
.greenBits
== 0)
278 if (fb
->Visual
.blueBits
== 0)
281 for (i
= 0; i
< n
; i
++) {
283 /* convert float back to ubyte */
284 CLAMPED_FLOAT_TO_UBYTE(r
, rgba
[i
][RCOMP
]);
285 CLAMPED_FLOAT_TO_UBYTE(g
, rgba
[i
][GCOMP
]);
286 CLAMPED_FLOAT_TO_UBYTE(b
, rgba
[i
][BCOMP
]);
287 /* using only the N most significant bits of the ubyte value, convert to
290 rgba
[i
][RCOMP
] = (GLfloat
) (r
>> rShift
) * rScale
;
291 rgba
[i
][GCOMP
] = (GLfloat
) (g
>> gShift
) * gScale
;
292 rgba
[i
][BCOMP
] = (GLfloat
) (b
>> bShift
) * bScale
;
299 * Read R, G, B, A, RGB, L, or LA pixels.
302 read_rgba_pixels( struct gl_context
*ctx
,
304 GLsizei width
, GLsizei height
,
305 GLenum format
, GLenum type
, GLvoid
*pixels
,
306 const struct gl_pixelstore_attrib
*packing
)
308 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
309 GLbitfield transferOps
= ctx
->_ImageTransferState
;
310 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
311 struct gl_renderbuffer
*rb
= fb
->_ColorReadBuffer
;
316 if ((ctx
->Color
._ClampReadColor
== GL_TRUE
|| type
!= GL_FLOAT
) &&
317 !_mesa_is_integer_format(format
)) {
318 transferOps
|= IMAGE_CLAMP_BIT
;
321 /* Try the optimized path first. */
322 if (fast_read_rgba_pixels(ctx
, x
, y
, width
, height
,
323 format
, type
, pixels
, packing
, transferOps
)) {
327 /* width should never be > MAX_WIDTH since we did clipping earlier */
328 ASSERT(width
<= MAX_WIDTH
);
331 const GLint dstStride
332 = _mesa_image_row_stride(packing
, width
, format
, type
);
333 GLfloat (*rgba
)[4] = swrast
->SpanArrays
->attribs
[FRAG_ATTRIB_COL0
];
336 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
339 for (row
= 0; row
< height
; row
++, y
++) {
341 /* Get float rgba pixels */
342 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, GL_FLOAT
, rgba
);
344 /* apply fudge factor for shallow color buffers */
345 if ((fb
->Visual
.redBits
< 8 && fb
->Visual
.redBits
!= 0) ||
346 (fb
->Visual
.greenBits
< 8 && fb
->Visual
.greenBits
!= 0) ||
347 (fb
->Visual
.blueBits
< 8 && fb
->Visual
.blueBits
!= 0)) {
348 adjust_colors(fb
, width
, rgba
);
351 /* pack the row of RGBA pixels into user's buffer */
352 _mesa_pack_rgba_span_float(ctx
, width
, rgba
, format
, type
, dst
,
353 packing
, transferOps
);
362 * Read combined depth/stencil values.
363 * We'll have already done error checking to be sure the expected
364 * depth and stencil buffers really exist.
367 read_depth_stencil_pixels(struct gl_context
*ctx
,
369 GLsizei width
, GLsizei height
,
370 GLenum type
, GLvoid
*pixels
,
371 const struct gl_pixelstore_attrib
*packing
)
373 const GLboolean scaleOrBias
374 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
375 const GLboolean stencilTransfer
= ctx
->Pixel
.IndexShift
376 || ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapStencilFlag
;
377 struct gl_renderbuffer
*depthRb
, *stencilRb
;
379 depthRb
= ctx
->ReadBuffer
->_DepthBuffer
;
380 stencilRb
= ctx
->ReadBuffer
->_StencilBuffer
;
382 if (!depthRb
|| !stencilRb
)
385 depthRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
386 stencilRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
388 if (depthRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
389 depthRb
->Format
== MESA_FORMAT_Z24_S8
&&
390 type
== GL_UNSIGNED_INT_24_8
&&
391 depthRb
== stencilRb
&&
392 depthRb
->GetRow
&& /* May be null if depthRb is a wrapper around
393 * separate depth and stencil buffers. */
396 /* This is the ideal case.
397 * Reading GL_DEPTH_STENCIL pixels from combined depth/stencil buffer.
398 * Plus, no pixel transfer ops to worry about!
401 GLint dstStride
= _mesa_image_row_stride(packing
, width
,
402 GL_DEPTH_STENCIL_EXT
, type
);
403 GLubyte
*dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
,
405 GL_DEPTH_STENCIL_EXT
,
407 for (i
= 0; i
< height
; i
++) {
408 depthRb
->GetRow(ctx
, depthRb
, width
, x
, y
+ i
, dst
);
413 /* Reading GL_DEPTH_STENCIL pixels from separate depth/stencil buffers,
414 * or we need pixel transfer.
417 depthRb
= ctx
->ReadBuffer
->_DepthBuffer
;
418 stencilRb
= ctx
->ReadBuffer
->_StencilBuffer
;
420 for (i
= 0; i
< height
; i
++) {
421 GLstencil stencilVals
[MAX_WIDTH
];
423 GLuint
*depthStencilDst
= (GLuint
*)
424 _mesa_image_address2d(packing
, pixels
, width
, height
,
425 GL_DEPTH_STENCIL_EXT
, type
, i
, 0);
427 _swrast_read_stencil_span(ctx
, stencilRb
, width
,
428 x
, y
+ i
, stencilVals
);
430 if (!scaleOrBias
&& !stencilTransfer
431 && ctx
->ReadBuffer
->Visual
.depthBits
== 24) {
433 GLuint zVals
[MAX_WIDTH
]; /* 24-bit values! */
435 ASSERT(depthRb
->DataType
== GL_UNSIGNED_INT
);
436 /* note, we've already been clipped */
437 depthRb
->GetRow(ctx
, depthRb
, width
, x
, y
+ i
, zVals
);
438 for (j
= 0; j
< width
; j
++) {
439 depthStencilDst
[j
] = (zVals
[j
] << 8) | (stencilVals
[j
] & 0xff);
444 GLfloat depthVals
[MAX_WIDTH
];
445 _swrast_read_depth_span_float(ctx
, depthRb
, width
, x
, y
+ i
,
447 _mesa_pack_depth_stencil_span(ctx
, width
, type
, depthStencilDst
,
448 depthVals
, stencilVals
, packing
);
457 * Software fallback routine for ctx->Driver.ReadPixels().
458 * By time we get here, all error checking will have been done.
461 _swrast_ReadPixels( struct gl_context
*ctx
,
462 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
463 GLenum format
, GLenum type
,
464 const struct gl_pixelstore_attrib
*packing
,
467 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
468 struct gl_pixelstore_attrib clippedPacking
= *packing
;
471 _mesa_update_state(ctx
);
473 /* Need to do swrast_render_start() before clipping or anything else
474 * since this is where a driver may grab the hw lock and get an updated
477 swrast_render_start(ctx
);
479 if (swrast
->NewState
)
480 _swrast_validate_derived( ctx
);
482 /* Do all needed clipping here, so that we can forget about it later */
483 if (_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
485 pixels
= _mesa_map_pbo_dest(ctx
, &clippedPacking
, pixels
);
489 case GL_STENCIL_INDEX
:
490 read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
493 case GL_DEPTH_COMPONENT
:
494 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
497 case GL_DEPTH_STENCIL_EXT
:
498 read_depth_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
502 /* all other formats should be color formats */
503 read_rgba_pixels(ctx
, x
, y
, width
, height
, format
, type
, pixels
,
507 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
511 swrast_render_finish(ctx
);