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/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
->Format
== MESA_FORMAT_Z16
);
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
->Format
== MESA_FORMAT_X8_Z24
||
123 rb
->Format
== MESA_FORMAT_S8_Z24
||
124 rb
->Format
== MESA_FORMAT_Z24_X8
||
125 rb
->Format
== MESA_FORMAT_Z24_S8
);
126 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
||
127 rb
->DataType
== GL_UNSIGNED_INT_24_8
);
128 for (j
= 0; j
< height
; j
++, y
++) {
129 GLuint
*dest
= (GLuint
*)
130 _mesa_image_address2d(packing
, pixels
, width
, height
,
131 GL_DEPTH_COMPONENT
, type
, j
, 0);
133 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
134 /* convert range from 24-bit to 32-bit */
135 if (rb
->Format
== MESA_FORMAT_X8_Z24
||
136 rb
->Format
== MESA_FORMAT_S8_Z24
) {
137 for (k
= 0; k
< width
; k
++) {
138 /* Note: put MSByte of 24-bit value into LSByte */
139 dest
[k
] = (dest
[k
] << 8) | ((dest
[k
] >> 16) & 0xff);
143 for (k
= 0; k
< width
; k
++) {
144 /* Note: fill in LSByte by replication */
145 dest
[k
] = dest
[k
] | ((dest
[k
] >> 8) & 0xff);
150 else if (type
== GL_UNSIGNED_INT
&& fb
->Visual
.depthBits
== 32
151 && !biasOrScale
&& !packing
->SwapBytes
) {
152 /* Special case: directly read 32-bit unsigned depth values. */
154 ASSERT(rb
->Format
== MESA_FORMAT_Z32
);
155 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
156 for (j
= 0; j
< height
; j
++, y
++) {
157 void *dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
158 GL_DEPTH_COMPONENT
, type
, j
, 0);
159 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
163 /* General case (slower) */
165 for (j
= 0; j
< height
; j
++, y
++) {
166 GLfloat depthValues
[MAX_WIDTH
];
167 GLvoid
*dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
168 GL_DEPTH_COMPONENT
, type
, j
, 0);
169 _swrast_read_depth_span_float(ctx
, rb
, width
, x
, y
, depthValues
);
170 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthValues
, packing
);
177 * Read pixels for format=GL_STENCIL_INDEX.
180 read_stencil_pixels( GLcontext
*ctx
,
182 GLsizei width
, GLsizei height
,
183 GLenum type
, GLvoid
*pixels
,
184 const struct gl_pixelstore_attrib
*packing
)
186 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
187 struct gl_renderbuffer
*rb
= fb
->_StencilBuffer
;
193 /* width should never be > MAX_WIDTH since we did clipping earlier */
194 ASSERT(width
<= MAX_WIDTH
);
196 /* process image row by row */
197 for (j
=0;j
<height
;j
++,y
++) {
199 GLstencil stencil
[MAX_WIDTH
];
201 _swrast_read_stencil_span(ctx
, rb
, width
, x
, y
, stencil
);
203 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
204 GL_STENCIL_INDEX
, type
, j
, 0);
206 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, stencil
, packing
);
213 * Optimized glReadPixels for particular pixel formats when pixel
214 * scaling, biasing, mapping, etc. are disabled.
215 * \return GL_TRUE if success, GL_FALSE if unable to do the readpixels
218 fast_read_rgba_pixels( GLcontext
*ctx
,
220 GLsizei width
, GLsizei height
,
221 GLenum format
, GLenum type
,
223 const struct gl_pixelstore_attrib
*packing
,
224 GLbitfield transferOps
)
226 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
231 ASSERT(rb
->_BaseFormat
== GL_RGBA
|| rb
->_BaseFormat
== GL_RGB
);
233 /* clipping should have already been done */
234 ASSERT(x
+ width
<= (GLint
) rb
->Width
);
235 ASSERT(y
+ height
<= (GLint
) rb
->Height
);
237 /* check for things we can't handle here */
239 packing
->SwapBytes
||
244 if (format
== GL_RGBA
&& rb
->DataType
== type
) {
245 const GLint dstStride
= _mesa_image_row_stride(packing
, width
,
248 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
252 for (row
= 0; row
< height
; row
++) {
253 rb
->GetRow(ctx
, rb
, width
, x
, y
+ row
, dest
);
259 if (format
== GL_RGB
&&
260 rb
->DataType
== GL_UNSIGNED_BYTE
&&
261 type
== GL_UNSIGNED_BYTE
) {
262 const GLint dstStride
= _mesa_image_row_stride(packing
, width
,
265 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
269 for (row
= 0; row
< height
; row
++) {
270 GLubyte tempRow
[MAX_WIDTH
][4];
272 rb
->GetRow(ctx
, rb
, width
, x
, y
+ row
, tempRow
);
273 /* convert RGBA to RGB */
274 for (col
= 0; col
< width
; col
++) {
275 dest
[col
* 3 + 0] = tempRow
[col
][0];
276 dest
[col
* 3 + 1] = tempRow
[col
][1];
277 dest
[col
* 3 + 2] = tempRow
[col
][2];
290 * When we're using a low-precision color buffer (like 16-bit 5/6/5)
291 * we have to adjust our color values a bit to pass conformance.
292 * The problem is when a 5 or 6-bit color value is convert to an 8-bit
293 * value and then a floating point value, the floating point values don't
294 * increment uniformly as the 5 or 6-bit value is incremented.
296 * This function adjusts floating point values to compensate.
299 adjust_colors(GLcontext
*ctx
, GLuint n
, GLfloat rgba
[][4])
301 const GLuint rShift
= 8 - ctx
->Visual
.redBits
;
302 const GLuint gShift
= 8 - ctx
->Visual
.greenBits
;
303 const GLuint bShift
= 8 - ctx
->Visual
.blueBits
;
304 const GLfloat rScale
= 1.0F
/ (GLfloat
) ((1 << ctx
->Visual
.redBits
) - 1);
305 const GLfloat gScale
= 1.0F
/ (GLfloat
) ((1 << ctx
->Visual
.greenBits
) - 1);
306 const GLfloat bScale
= 1.0F
/ (GLfloat
) ((1 << ctx
->Visual
.blueBits
) - 1);
308 for (i
= 0; i
< n
; i
++) {
310 /* convert float back to ubyte */
311 CLAMPED_FLOAT_TO_UBYTE(r
, rgba
[i
][RCOMP
]);
312 CLAMPED_FLOAT_TO_UBYTE(g
, rgba
[i
][GCOMP
]);
313 CLAMPED_FLOAT_TO_UBYTE(b
, rgba
[i
][BCOMP
]);
314 /* using only the N most significant bits of the ubyte value, convert to
317 rgba
[i
][RCOMP
] = (GLfloat
) (r
>> rShift
) * rScale
;
318 rgba
[i
][GCOMP
] = (GLfloat
) (g
>> gShift
) * gScale
;
319 rgba
[i
][BCOMP
] = (GLfloat
) (b
>> bShift
) * bScale
;
326 * Read R, G, B, A, RGB, L, or LA pixels.
329 read_rgba_pixels( GLcontext
*ctx
,
331 GLsizei width
, GLsizei height
,
332 GLenum format
, GLenum type
, GLvoid
*pixels
,
333 const struct gl_pixelstore_attrib
*packing
)
335 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
336 GLbitfield transferOps
= ctx
->_ImageTransferState
;
337 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
338 struct gl_renderbuffer
*rb
= fb
->_ColorReadBuffer
;
343 if (type
== GL_FLOAT
&& ((ctx
->Color
.ClampReadColor
== GL_TRUE
) ||
344 (ctx
->Color
.ClampReadColor
== GL_FIXED_ONLY_ARB
&&
345 rb
->DataType
!= GL_FLOAT
)))
346 transferOps
|= IMAGE_CLAMP_BIT
;
348 /* Try optimized path first */
349 if (fast_read_rgba_pixels(ctx
, x
, y
, width
, height
,
350 format
, type
, pixels
, packing
, transferOps
)) {
354 /* width should never be > MAX_WIDTH since we did clipping earlier */
355 ASSERT(width
<= MAX_WIDTH
);
357 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
358 GLfloat
*dest
, *src
, *tmpImage
, *convImage
;
361 tmpImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
363 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
366 convImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
368 _mesa_free(tmpImage
);
369 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
373 /* read full RGBA, FLOAT image */
375 for (row
= 0; row
< height
; row
++, y
++) {
376 if (fb
->Visual
.rgbMode
) {
377 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, GL_FLOAT
, dest
);
380 GLuint index
[MAX_WIDTH
];
381 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
382 rb
->GetRow(ctx
, rb
, width
, x
, y
, index
);
383 _mesa_apply_ci_transfer_ops(ctx
,
384 transferOps
& IMAGE_SHIFT_OFFSET_BIT
,
386 _mesa_map_ci_to_rgba(ctx
, width
, index
, (GLfloat (*)[4]) dest
);
388 _mesa_apply_rgba_transfer_ops(ctx
,
389 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
,
390 width
, (GLfloat (*)[4]) dest
);
395 if (ctx
->Pixel
.Convolution2DEnabled
) {
396 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
399 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
400 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
402 _mesa_free(tmpImage
);
404 /* finish transfer ops and pack the resulting image */
406 for (row
= 0; row
< height
; row
++) {
408 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
409 format
, type
, row
, 0);
410 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) src
,
411 format
, type
, dest
, packing
,
412 transferOps
& IMAGE_POST_CONVOLUTION_BITS
);
415 _mesa_free(convImage
);
419 const GLint dstStride
420 = _mesa_image_row_stride(packing
, width
, format
, type
);
421 GLfloat (*rgba
)[4] = swrast
->SpanArrays
->attribs
[FRAG_ATTRIB_COL0
];
424 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
427 /* make sure we don't apply 1D convolution */
428 transferOps
&= ~(IMAGE_CONVOLUTION_BIT
|
429 IMAGE_POST_CONVOLUTION_SCALE_BIAS
);
431 for (row
= 0; row
< height
; row
++, y
++) {
433 /* Get float rgba pixels */
434 if (fb
->Visual
.rgbMode
) {
435 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, GL_FLOAT
, rgba
);
438 /* read CI and convert to RGBA */
439 GLuint index
[MAX_WIDTH
];
440 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
441 rb
->GetRow(ctx
, rb
, width
, x
, y
, index
);
442 _mesa_apply_ci_transfer_ops(ctx
,
443 transferOps
& IMAGE_SHIFT_OFFSET_BIT
,
445 _mesa_map_ci_to_rgba(ctx
, width
, index
, rgba
);
448 /* apply fudge factor for shallow color buffers */
449 if (fb
->Visual
.redBits
< 8 ||
450 fb
->Visual
.greenBits
< 8 ||
451 fb
->Visual
.blueBits
< 8) {
452 adjust_colors(ctx
, width
, rgba
);
455 /* pack the row of RGBA pixels into user's buffer */
456 _mesa_pack_rgba_span_float(ctx
, width
, rgba
, format
, type
, dst
,
457 packing
, transferOps
);
466 * Read combined depth/stencil values.
467 * We'll have already done error checking to be sure the expected
468 * depth and stencil buffers really exist.
471 read_depth_stencil_pixels(GLcontext
*ctx
,
473 GLsizei width
, GLsizei height
,
474 GLenum type
, GLvoid
*pixels
,
475 const struct gl_pixelstore_attrib
*packing
)
477 const GLboolean scaleOrBias
478 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
479 const GLboolean stencilTransfer
= ctx
->Pixel
.IndexShift
480 || ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapStencilFlag
;
481 struct gl_renderbuffer
*depthRb
, *stencilRb
;
483 depthRb
= ctx
->ReadBuffer
->_DepthBuffer
;
484 stencilRb
= ctx
->ReadBuffer
->_StencilBuffer
;
486 if (!depthRb
|| !stencilRb
)
489 depthRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
490 stencilRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
492 if (depthRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
493 stencilRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
494 depthRb
== stencilRb
&&
497 /* This is the ideal case.
498 * Reading GL_DEPTH_STENCIL pixels from combined depth/stencil buffer.
499 * Plus, no pixel transfer ops to worry about!
502 GLint dstStride
= _mesa_image_row_stride(packing
, width
,
503 GL_DEPTH_STENCIL_EXT
, type
);
504 GLubyte
*dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
,
506 GL_DEPTH_STENCIL_EXT
,
508 for (i
= 0; i
< height
; i
++) {
509 depthRb
->GetRow(ctx
, depthRb
, width
, x
, y
+ i
, dst
);
514 /* Reading GL_DEPTH_STENCIL pixels from separate depth/stencil buffers,
515 * or we need pixel transfer.
518 depthRb
= ctx
->ReadBuffer
->_DepthBuffer
;
519 stencilRb
= ctx
->ReadBuffer
->_StencilBuffer
;
521 for (i
= 0; i
< height
; i
++) {
522 GLstencil stencilVals
[MAX_WIDTH
];
524 GLuint
*depthStencilDst
= (GLuint
*)
525 _mesa_image_address2d(packing
, pixels
, width
, height
,
526 GL_DEPTH_STENCIL_EXT
, type
, i
, 0);
528 _swrast_read_stencil_span(ctx
, stencilRb
, width
,
529 x
, y
+ i
, stencilVals
);
531 if (!scaleOrBias
&& !stencilTransfer
532 && ctx
->ReadBuffer
->Visual
.depthBits
== 24) {
534 GLuint zVals
[MAX_WIDTH
]; /* 24-bit values! */
536 ASSERT(depthRb
->DataType
== GL_UNSIGNED_INT
);
537 /* note, we've already been clipped */
538 depthRb
->GetRow(ctx
, depthRb
, width
, x
, y
+ i
, zVals
);
539 for (j
= 0; j
< width
; j
++) {
540 depthStencilDst
[j
] = (zVals
[j
] << 8) | (stencilVals
[j
] & 0xff);
545 GLfloat depthVals
[MAX_WIDTH
];
546 _swrast_read_depth_span_float(ctx
, depthRb
, width
, x
, y
+ i
,
548 _mesa_pack_depth_stencil_span(ctx
, width
, depthStencilDst
,
549 depthVals
, stencilVals
, packing
);
558 * Software fallback routine for ctx->Driver.ReadPixels().
559 * By time we get here, all error checking will have been done.
562 _swrast_ReadPixels( GLcontext
*ctx
,
563 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
564 GLenum format
, GLenum type
,
565 const struct gl_pixelstore_attrib
*packing
,
568 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
569 struct gl_pixelstore_attrib clippedPacking
= *packing
;
572 _mesa_update_state(ctx
);
574 /* Need to do swrast_render_start() before clipping or anything else
575 * since this is where a driver may grab the hw lock and get an updated
578 swrast_render_start(ctx
);
580 if (swrast
->NewState
)
581 _swrast_validate_derived( ctx
);
583 /* Do all needed clipping here, so that we can forget about it later */
584 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
585 /* The ReadPixels region is totally outside the window bounds */
586 swrast_render_finish(ctx
);
590 pixels
= _mesa_map_pbo_dest(ctx
, &clippedPacking
, pixels
);
596 read_index_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
599 case GL_STENCIL_INDEX
:
600 read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
603 case GL_DEPTH_COMPONENT
:
604 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
613 case GL_LUMINANCE_ALPHA
:
618 read_rgba_pixels(ctx
, x
, y
, width
, height
,
619 format
, type
, pixels
, &clippedPacking
);
621 case GL_DEPTH_STENCIL_EXT
:
622 read_depth_stencil_pixels(ctx
, x
, y
, width
, height
,
623 type
, pixels
, &clippedPacking
);
626 _mesa_problem(ctx
, "unexpected format in _swrast_ReadPixels");
627 /* don't return yet, clean-up */
630 swrast_render_finish(ctx
);
632 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);