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/image.h"
31 #include "main/imports.h"
32 #include "main/macros.h"
33 #include "main/pack.h"
35 #include "main/state.h"
37 #include "s_context.h"
40 #include "s_stencil.h"
44 * Read pixels for format=GL_DEPTH_COMPONENT.
47 read_depth_pixels( struct gl_context
*ctx
,
49 GLsizei width
, GLsizei height
,
50 GLenum type
, GLvoid
*pixels
,
51 const struct gl_pixelstore_attrib
*packing
)
53 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
54 struct gl_renderbuffer
*rb
= fb
->_DepthBuffer
;
55 const GLboolean biasOrScale
56 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
61 /* clipping should have been done already */
64 ASSERT(x
+ width
<= (GLint
) rb
->Width
);
65 ASSERT(y
+ height
<= (GLint
) rb
->Height
);
66 /* width should never be > MAX_WIDTH since we did clipping earlier */
67 ASSERT(width
<= MAX_WIDTH
);
69 if (type
== GL_UNSIGNED_SHORT
&& fb
->Visual
.depthBits
== 16
70 && !biasOrScale
&& !packing
->SwapBytes
) {
71 /* Special case: directly read 16-bit unsigned depth values. */
73 ASSERT(rb
->Format
== MESA_FORMAT_Z16
);
74 ASSERT(rb
->DataType
== GL_UNSIGNED_SHORT
);
75 for (j
= 0; j
< height
; j
++, y
++) {
76 void *dest
=_mesa_image_address2d(packing
, pixels
, width
, height
,
77 GL_DEPTH_COMPONENT
, type
, j
, 0);
78 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
81 else if (type
== GL_UNSIGNED_INT
&& fb
->Visual
.depthBits
== 24
82 && !biasOrScale
&& !packing
->SwapBytes
) {
83 /* Special case: directly read 24-bit unsigned depth values. */
85 ASSERT(rb
->Format
== MESA_FORMAT_X8_Z24
||
86 rb
->Format
== MESA_FORMAT_S8_Z24
||
87 rb
->Format
== MESA_FORMAT_Z24_X8
||
88 rb
->Format
== MESA_FORMAT_Z24_S8
);
89 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
||
90 rb
->DataType
== GL_UNSIGNED_INT_24_8
);
91 for (j
= 0; j
< height
; j
++, y
++) {
92 GLuint
*dest
= (GLuint
*)
93 _mesa_image_address2d(packing
, pixels
, width
, height
,
94 GL_DEPTH_COMPONENT
, type
, j
, 0);
96 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
97 /* convert range from 24-bit to 32-bit */
98 if (rb
->Format
== MESA_FORMAT_X8_Z24
||
99 rb
->Format
== MESA_FORMAT_S8_Z24
) {
100 for (k
= 0; k
< width
; k
++) {
101 /* Note: put MSByte of 24-bit value into LSByte */
102 dest
[k
] = (dest
[k
] << 8) | ((dest
[k
] >> 16) & 0xff);
106 for (k
= 0; k
< width
; k
++) {
107 /* Note: fill in LSByte by replication */
108 dest
[k
] = dest
[k
] | ((dest
[k
] >> 8) & 0xff);
113 else if (type
== GL_UNSIGNED_INT
&& fb
->Visual
.depthBits
== 32
114 && !biasOrScale
&& !packing
->SwapBytes
) {
115 /* Special case: directly read 32-bit unsigned depth values. */
117 ASSERT(rb
->Format
== MESA_FORMAT_Z32
);
118 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
119 for (j
= 0; j
< height
; j
++, y
++) {
120 void *dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
121 GL_DEPTH_COMPONENT
, type
, j
, 0);
122 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
126 /* General case (slower) */
128 for (j
= 0; j
< height
; j
++, y
++) {
129 GLfloat depthValues
[MAX_WIDTH
];
130 GLvoid
*dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
131 GL_DEPTH_COMPONENT
, type
, j
, 0);
132 _swrast_read_depth_span_float(ctx
, rb
, width
, x
, y
, depthValues
);
133 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthValues
, packing
);
140 * Read pixels for format=GL_STENCIL_INDEX.
143 read_stencil_pixels( struct gl_context
*ctx
,
145 GLsizei width
, GLsizei height
,
146 GLenum type
, GLvoid
*pixels
,
147 const struct gl_pixelstore_attrib
*packing
)
149 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
150 struct gl_renderbuffer
*rb
= fb
->_StencilBuffer
;
156 /* width should never be > MAX_WIDTH since we did clipping earlier */
157 ASSERT(width
<= MAX_WIDTH
);
159 /* process image row by row */
160 for (j
=0;j
<height
;j
++,y
++) {
162 GLstencil stencil
[MAX_WIDTH
];
164 _swrast_read_stencil_span(ctx
, rb
, width
, x
, y
, stencil
);
166 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
167 GL_STENCIL_INDEX
, type
, j
, 0);
169 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, stencil
, packing
);
176 * Optimized glReadPixels for particular pixel formats when pixel
177 * scaling, biasing, mapping, etc. are disabled.
178 * \return GL_TRUE if success, GL_FALSE if unable to do the readpixels
181 fast_read_rgba_pixels( struct gl_context
*ctx
,
183 GLsizei width
, GLsizei height
,
184 GLenum format
, GLenum type
,
186 const struct gl_pixelstore_attrib
*packing
,
187 GLbitfield transferOps
)
189 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
194 ASSERT(rb
->_BaseFormat
== GL_RGBA
||
195 rb
->_BaseFormat
== GL_RGB
||
196 rb
->_BaseFormat
== GL_RG
||
197 rb
->_BaseFormat
== GL_RED
||
198 rb
->_BaseFormat
== GL_ALPHA
);
200 /* clipping should have already been done */
201 ASSERT(x
+ width
<= (GLint
) rb
->Width
);
202 ASSERT(y
+ height
<= (GLint
) rb
->Height
);
204 /* check for things we can't handle here */
206 packing
->SwapBytes
||
211 if (format
== GL_RGBA
&& rb
->DataType
== type
) {
212 const GLint dstStride
= _mesa_image_row_stride(packing
, width
,
215 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
219 for (row
= 0; row
< height
; row
++) {
220 rb
->GetRow(ctx
, rb
, width
, x
, y
+ row
, dest
);
226 if (format
== GL_RGB
&&
227 rb
->DataType
== GL_UNSIGNED_BYTE
&&
228 type
== GL_UNSIGNED_BYTE
) {
229 const GLint dstStride
= _mesa_image_row_stride(packing
, width
,
232 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
236 for (row
= 0; row
< height
; row
++) {
237 GLubyte tempRow
[MAX_WIDTH
][4];
239 rb
->GetRow(ctx
, rb
, width
, x
, y
+ row
, tempRow
);
240 /* convert RGBA to RGB */
241 for (col
= 0; col
< width
; col
++) {
242 dest
[col
* 3 + 0] = tempRow
[col
][0];
243 dest
[col
* 3 + 1] = tempRow
[col
][1];
244 dest
[col
* 3 + 2] = tempRow
[col
][2];
257 * When we're using a low-precision color buffer (like 16-bit 5/6/5)
258 * we have to adjust our color values a bit to pass conformance.
259 * The problem is when a 5 or 6-bit color value is converted to an 8-bit
260 * value and then a floating point value, the floating point values don't
261 * increment uniformly as the 5 or 6-bit value is incremented.
263 * This function adjusts floating point values to compensate.
266 adjust_colors(const struct gl_framebuffer
*fb
, GLuint n
, GLfloat rgba
[][4])
268 const GLuint rShift
= 8 - fb
->Visual
.redBits
;
269 const GLuint gShift
= 8 - fb
->Visual
.greenBits
;
270 const GLuint bShift
= 8 - fb
->Visual
.blueBits
;
271 GLfloat rScale
= 1.0F
/ (GLfloat
) ((1 << fb
->Visual
.redBits
) - 1);
272 GLfloat gScale
= 1.0F
/ (GLfloat
) ((1 << fb
->Visual
.greenBits
) - 1);
273 GLfloat bScale
= 1.0F
/ (GLfloat
) ((1 << fb
->Visual
.blueBits
) - 1);
276 if (fb
->Visual
.redBits
== 0)
278 if (fb
->Visual
.greenBits
== 0)
280 if (fb
->Visual
.blueBits
== 0)
283 for (i
= 0; i
< n
; i
++) {
285 /* convert float back to ubyte */
286 CLAMPED_FLOAT_TO_UBYTE(r
, rgba
[i
][RCOMP
]);
287 CLAMPED_FLOAT_TO_UBYTE(g
, rgba
[i
][GCOMP
]);
288 CLAMPED_FLOAT_TO_UBYTE(b
, rgba
[i
][BCOMP
]);
289 /* using only the N most significant bits of the ubyte value, convert to
292 rgba
[i
][RCOMP
] = (GLfloat
) (r
>> rShift
) * rScale
;
293 rgba
[i
][GCOMP
] = (GLfloat
) (g
>> gShift
) * gScale
;
294 rgba
[i
][BCOMP
] = (GLfloat
) (b
>> bShift
) * bScale
;
301 * Read R, G, B, A, RGB, L, or LA pixels.
304 read_rgba_pixels( struct gl_context
*ctx
,
306 GLsizei width
, GLsizei height
,
307 GLenum format
, GLenum type
, GLvoid
*pixels
,
308 const struct gl_pixelstore_attrib
*packing
)
310 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
311 GLbitfield transferOps
= ctx
->_ImageTransferState
;
312 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
313 struct gl_renderbuffer
*rb
= fb
->_ColorReadBuffer
;
318 if (type
== GL_FLOAT
&& ((ctx
->Color
.ClampReadColor
== GL_TRUE
) ||
319 (ctx
->Color
.ClampReadColor
== GL_FIXED_ONLY_ARB
&&
320 rb
->DataType
!= GL_FLOAT
)))
321 transferOps
|= IMAGE_CLAMP_BIT
;
323 /* Try optimized path first */
324 if (fast_read_rgba_pixels(ctx
, x
, y
, width
, height
,
325 format
, type
, pixels
, packing
, transferOps
)) {
329 /* width should never be > MAX_WIDTH since we did clipping earlier */
330 ASSERT(width
<= MAX_WIDTH
);
333 const GLint dstStride
334 = _mesa_image_row_stride(packing
, width
, format
, type
);
335 GLfloat (*rgba
)[4] = swrast
->SpanArrays
->attribs
[FRAG_ATTRIB_COL0
];
338 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
341 for (row
= 0; row
< height
; row
++, y
++) {
343 /* Get float rgba pixels */
344 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, GL_FLOAT
, rgba
);
346 /* apply fudge factor for shallow color buffers */
347 if (fb
->Visual
.redBits
< 8 ||
348 fb
->Visual
.greenBits
< 8 ||
349 fb
->Visual
.blueBits
< 8) {
350 adjust_colors(fb
, width
, rgba
);
353 /* pack the row of RGBA pixels into user's buffer */
354 _mesa_pack_rgba_span_float(ctx
, width
, rgba
, format
, type
, dst
,
355 packing
, transferOps
);
364 * Read combined depth/stencil values.
365 * We'll have already done error checking to be sure the expected
366 * depth and stencil buffers really exist.
369 read_depth_stencil_pixels(struct gl_context
*ctx
,
371 GLsizei width
, GLsizei height
,
372 GLenum type
, GLvoid
*pixels
,
373 const struct gl_pixelstore_attrib
*packing
)
375 const GLboolean scaleOrBias
376 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
377 const GLboolean stencilTransfer
= ctx
->Pixel
.IndexShift
378 || ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapStencilFlag
;
379 struct gl_renderbuffer
*depthRb
, *stencilRb
;
381 depthRb
= ctx
->ReadBuffer
->_DepthBuffer
;
382 stencilRb
= ctx
->ReadBuffer
->_StencilBuffer
;
384 if (!depthRb
|| !stencilRb
)
387 depthRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
388 stencilRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
390 if (depthRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
391 stencilRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
392 depthRb
== stencilRb
&&
395 /* This is the ideal case.
396 * Reading GL_DEPTH_STENCIL pixels from combined depth/stencil buffer.
397 * Plus, no pixel transfer ops to worry about!
400 GLint dstStride
= _mesa_image_row_stride(packing
, width
,
401 GL_DEPTH_STENCIL_EXT
, type
);
402 GLubyte
*dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
,
404 GL_DEPTH_STENCIL_EXT
,
406 for (i
= 0; i
< height
; i
++) {
407 depthRb
->GetRow(ctx
, depthRb
, width
, x
, y
+ i
, dst
);
412 /* Reading GL_DEPTH_STENCIL pixels from separate depth/stencil buffers,
413 * or we need pixel transfer.
416 depthRb
= ctx
->ReadBuffer
->_DepthBuffer
;
417 stencilRb
= ctx
->ReadBuffer
->_StencilBuffer
;
419 for (i
= 0; i
< height
; i
++) {
420 GLstencil stencilVals
[MAX_WIDTH
];
422 GLuint
*depthStencilDst
= (GLuint
*)
423 _mesa_image_address2d(packing
, pixels
, width
, height
,
424 GL_DEPTH_STENCIL_EXT
, type
, i
, 0);
426 _swrast_read_stencil_span(ctx
, stencilRb
, width
,
427 x
, y
+ i
, stencilVals
);
429 if (!scaleOrBias
&& !stencilTransfer
430 && ctx
->ReadBuffer
->Visual
.depthBits
== 24) {
432 GLuint zVals
[MAX_WIDTH
]; /* 24-bit values! */
434 ASSERT(depthRb
->DataType
== GL_UNSIGNED_INT
);
435 /* note, we've already been clipped */
436 depthRb
->GetRow(ctx
, depthRb
, width
, x
, y
+ i
, zVals
);
437 for (j
= 0; j
< width
; j
++) {
438 depthStencilDst
[j
] = (zVals
[j
] << 8) | (stencilVals
[j
] & 0xff);
443 GLfloat depthVals
[MAX_WIDTH
];
444 _swrast_read_depth_span_float(ctx
, depthRb
, width
, x
, y
+ i
,
446 _mesa_pack_depth_stencil_span(ctx
, width
, depthStencilDst
,
447 depthVals
, stencilVals
, packing
);
456 * Software fallback routine for ctx->Driver.ReadPixels().
457 * By time we get here, all error checking will have been done.
460 _swrast_ReadPixels( struct gl_context
*ctx
,
461 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
462 GLenum format
, GLenum type
,
463 const struct gl_pixelstore_attrib
*packing
,
466 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
467 struct gl_pixelstore_attrib clippedPacking
= *packing
;
470 _mesa_update_state(ctx
);
472 /* Need to do swrast_render_start() before clipping or anything else
473 * since this is where a driver may grab the hw lock and get an updated
476 swrast_render_start(ctx
);
478 if (swrast
->NewState
)
479 _swrast_validate_derived( ctx
);
481 /* Do all needed clipping here, so that we can forget about it later */
482 if (_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
484 pixels
= _mesa_map_pbo_dest(ctx
, &clippedPacking
, pixels
);
488 case GL_STENCIL_INDEX
:
489 read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
492 case GL_DEPTH_COMPONENT
:
493 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
496 case GL_DEPTH_STENCIL_EXT
:
497 read_depth_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
501 /* all other formats should be color formats */
502 read_rgba_pixels(ctx
, x
, y
, width
, height
, format
, type
, pixels
,
506 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);
510 swrast_render_finish(ctx
);