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/pixel.h"
37 #include "main/state.h"
39 #include "s_context.h"
42 #include "s_stencil.h"
46 * Read a block of color index pixels.
49 read_index_pixels( GLcontext
*ctx
,
51 GLsizei width
, GLsizei height
,
52 GLenum type
, GLvoid
*pixels
,
53 const struct gl_pixelstore_attrib
*packing
)
55 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
61 /* width should never be > MAX_WIDTH since we did clipping earlier */
62 ASSERT(width
<= MAX_WIDTH
);
64 /* process image row by row */
65 for (i
= 0; i
< height
; i
++) {
66 GLuint index
[MAX_WIDTH
];
68 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
69 rb
->GetRow(ctx
, rb
, width
, x
, y
+ i
, index
);
71 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
72 GL_COLOR_INDEX
, type
, i
, 0);
74 _mesa_pack_index_span(ctx
, width
, type
, dest
, index
,
75 &ctx
->Pack
, ctx
->_ImageTransferState
);
82 * Read pixels for format=GL_DEPTH_COMPONENT.
85 read_depth_pixels( GLcontext
*ctx
,
87 GLsizei width
, GLsizei height
,
88 GLenum type
, GLvoid
*pixels
,
89 const struct gl_pixelstore_attrib
*packing
)
91 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
92 struct gl_renderbuffer
*rb
= fb
->_DepthBuffer
;
93 const GLboolean biasOrScale
94 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
99 /* clipping should have been done already */
102 ASSERT(x
+ width
<= (GLint
) rb
->Width
);
103 ASSERT(y
+ height
<= (GLint
) rb
->Height
);
104 /* width should never be > MAX_WIDTH since we did clipping earlier */
105 ASSERT(width
<= MAX_WIDTH
);
107 if (type
== GL_UNSIGNED_SHORT
&& fb
->Visual
.depthBits
== 16
108 && !biasOrScale
&& !packing
->SwapBytes
) {
109 /* Special case: directly read 16-bit unsigned depth values. */
111 ASSERT(rb
->Format
== MESA_FORMAT_Z16
);
112 ASSERT(rb
->DataType
== GL_UNSIGNED_SHORT
);
113 for (j
= 0; j
< height
; j
++, y
++) {
114 void *dest
=_mesa_image_address2d(packing
, pixels
, width
, height
,
115 GL_DEPTH_COMPONENT
, type
, j
, 0);
116 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
119 else if (type
== GL_UNSIGNED_INT
&& fb
->Visual
.depthBits
== 24
120 && !biasOrScale
&& !packing
->SwapBytes
) {
121 /* Special case: directly read 24-bit unsigned depth values. */
123 ASSERT(rb
->Format
== MESA_FORMAT_X8_Z24
||
124 rb
->Format
== MESA_FORMAT_S8_Z24
||
125 rb
->Format
== MESA_FORMAT_Z24_X8
||
126 rb
->Format
== MESA_FORMAT_Z24_S8
);
127 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
||
128 rb
->DataType
== GL_UNSIGNED_INT_24_8
);
129 for (j
= 0; j
< height
; j
++, y
++) {
130 GLuint
*dest
= (GLuint
*)
131 _mesa_image_address2d(packing
, pixels
, width
, height
,
132 GL_DEPTH_COMPONENT
, type
, j
, 0);
134 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
135 /* convert range from 24-bit to 32-bit */
136 if (rb
->Format
== MESA_FORMAT_X8_Z24
||
137 rb
->Format
== MESA_FORMAT_S8_Z24
) {
138 for (k
= 0; k
< width
; k
++) {
139 /* Note: put MSByte of 24-bit value into LSByte */
140 dest
[k
] = (dest
[k
] << 8) | ((dest
[k
] >> 16) & 0xff);
144 for (k
= 0; k
< width
; k
++) {
145 /* Note: fill in LSByte by replication */
146 dest
[k
] = dest
[k
] | ((dest
[k
] >> 8) & 0xff);
151 else if (type
== GL_UNSIGNED_INT
&& fb
->Visual
.depthBits
== 32
152 && !biasOrScale
&& !packing
->SwapBytes
) {
153 /* Special case: directly read 32-bit unsigned depth values. */
155 ASSERT(rb
->Format
== MESA_FORMAT_Z32
);
156 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
157 for (j
= 0; j
< height
; j
++, y
++) {
158 void *dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
159 GL_DEPTH_COMPONENT
, type
, j
, 0);
160 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
164 /* General case (slower) */
166 for (j
= 0; j
< height
; j
++, y
++) {
167 GLfloat depthValues
[MAX_WIDTH
];
168 GLvoid
*dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
169 GL_DEPTH_COMPONENT
, type
, j
, 0);
170 _swrast_read_depth_span_float(ctx
, rb
, width
, x
, y
, depthValues
);
171 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthValues
, packing
);
178 * Read pixels for format=GL_STENCIL_INDEX.
181 read_stencil_pixels( GLcontext
*ctx
,
183 GLsizei width
, GLsizei height
,
184 GLenum type
, GLvoid
*pixels
,
185 const struct gl_pixelstore_attrib
*packing
)
187 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
188 struct gl_renderbuffer
*rb
= fb
->_StencilBuffer
;
194 /* width should never be > MAX_WIDTH since we did clipping earlier */
195 ASSERT(width
<= MAX_WIDTH
);
197 /* process image row by row */
198 for (j
=0;j
<height
;j
++,y
++) {
200 GLstencil stencil
[MAX_WIDTH
];
202 _swrast_read_stencil_span(ctx
, rb
, width
, x
, y
, stencil
);
204 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
205 GL_STENCIL_INDEX
, type
, j
, 0);
207 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, stencil
, packing
);
214 * Optimized glReadPixels for particular pixel formats when pixel
215 * scaling, biasing, mapping, etc. are disabled.
216 * \return GL_TRUE if success, GL_FALSE if unable to do the readpixels
219 fast_read_rgba_pixels( GLcontext
*ctx
,
221 GLsizei width
, GLsizei height
,
222 GLenum format
, GLenum type
,
224 const struct gl_pixelstore_attrib
*packing
,
225 GLbitfield transferOps
)
227 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
232 ASSERT(rb
->_BaseFormat
== GL_RGBA
|| rb
->_BaseFormat
== GL_RGB
);
234 /* clipping should have already been done */
235 ASSERT(x
+ width
<= (GLint
) rb
->Width
);
236 ASSERT(y
+ height
<= (GLint
) rb
->Height
);
238 /* check for things we can't handle here */
240 packing
->SwapBytes
||
245 if (format
== GL_RGBA
&& rb
->DataType
== type
) {
246 const GLint dstStride
= _mesa_image_row_stride(packing
, width
,
249 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
253 for (row
= 0; row
< height
; row
++) {
254 rb
->GetRow(ctx
, rb
, width
, x
, y
+ row
, dest
);
260 if (format
== GL_RGB
&&
261 rb
->DataType
== GL_UNSIGNED_BYTE
&&
262 type
== GL_UNSIGNED_BYTE
) {
263 const GLint dstStride
= _mesa_image_row_stride(packing
, width
,
266 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
270 for (row
= 0; row
< height
; row
++) {
271 GLubyte tempRow
[MAX_WIDTH
][4];
273 rb
->GetRow(ctx
, rb
, width
, x
, y
+ row
, tempRow
);
274 /* convert RGBA to RGB */
275 for (col
= 0; col
< width
; col
++) {
276 dest
[col
* 3 + 0] = tempRow
[col
][0];
277 dest
[col
* 3 + 1] = tempRow
[col
][1];
278 dest
[col
* 3 + 2] = tempRow
[col
][2];
291 * When we're using a low-precision color buffer (like 16-bit 5/6/5)
292 * we have to adjust our color values a bit to pass conformance.
293 * The problem is when a 5 or 6-bit color value is convert to an 8-bit
294 * value and then a floating point value, the floating point values don't
295 * increment uniformly as the 5 or 6-bit value is incremented.
297 * This function adjusts floating point values to compensate.
300 adjust_colors(GLcontext
*ctx
, GLuint n
, GLfloat rgba
[][4])
302 const GLuint rShift
= 8 - ctx
->Visual
.redBits
;
303 const GLuint gShift
= 8 - ctx
->Visual
.greenBits
;
304 const GLuint bShift
= 8 - ctx
->Visual
.blueBits
;
305 const GLfloat rScale
= 1.0F
/ (GLfloat
) ((1 << ctx
->Visual
.redBits
) - 1);
306 const GLfloat gScale
= 1.0F
/ (GLfloat
) ((1 << ctx
->Visual
.greenBits
) - 1);
307 const GLfloat bScale
= 1.0F
/ (GLfloat
) ((1 << ctx
->Visual
.blueBits
) - 1);
309 for (i
= 0; i
< n
; i
++) {
311 /* convert float back to ubyte */
312 CLAMPED_FLOAT_TO_UBYTE(r
, rgba
[i
][RCOMP
]);
313 CLAMPED_FLOAT_TO_UBYTE(g
, rgba
[i
][GCOMP
]);
314 CLAMPED_FLOAT_TO_UBYTE(b
, rgba
[i
][BCOMP
]);
315 /* using only the N most significant bits of the ubyte value, convert to
318 rgba
[i
][RCOMP
] = (GLfloat
) (r
>> rShift
) * rScale
;
319 rgba
[i
][GCOMP
] = (GLfloat
) (g
>> gShift
) * gScale
;
320 rgba
[i
][BCOMP
] = (GLfloat
) (b
>> bShift
) * bScale
;
327 * Read R, G, B, A, RGB, L, or LA pixels.
330 read_rgba_pixels( GLcontext
*ctx
,
332 GLsizei width
, GLsizei height
,
333 GLenum format
, GLenum type
, GLvoid
*pixels
,
334 const struct gl_pixelstore_attrib
*packing
)
336 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
337 GLbitfield transferOps
= ctx
->_ImageTransferState
;
338 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
339 struct gl_renderbuffer
*rb
= fb
->_ColorReadBuffer
;
344 if (type
== GL_FLOAT
&& ((ctx
->Color
.ClampReadColor
== GL_TRUE
) ||
345 (ctx
->Color
.ClampReadColor
== GL_FIXED_ONLY_ARB
&&
346 rb
->DataType
!= GL_FLOAT
)))
347 transferOps
|= IMAGE_CLAMP_BIT
;
349 /* Try optimized path first */
350 if (fast_read_rgba_pixels(ctx
, x
, y
, width
, height
,
351 format
, type
, pixels
, packing
, transferOps
)) {
355 /* width should never be > MAX_WIDTH since we did clipping earlier */
356 ASSERT(width
<= MAX_WIDTH
);
358 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
359 GLfloat
*dest
, *src
, *tmpImage
, *convImage
;
362 tmpImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
364 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
367 convImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
369 _mesa_free(tmpImage
);
370 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
374 /* read full RGBA, FLOAT image */
376 for (row
= 0; row
< height
; row
++, y
++) {
377 if (fb
->Visual
.rgbMode
) {
378 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, GL_FLOAT
, dest
);
381 GLuint index
[MAX_WIDTH
];
382 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
383 rb
->GetRow(ctx
, rb
, width
, x
, y
, index
);
384 _mesa_apply_ci_transfer_ops(ctx
,
385 transferOps
& IMAGE_SHIFT_OFFSET_BIT
,
387 _mesa_map_ci_to_rgba(ctx
, width
, index
, (GLfloat (*)[4]) dest
);
389 _mesa_apply_rgba_transfer_ops(ctx
,
390 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
,
391 width
, (GLfloat (*)[4]) dest
);
396 if (ctx
->Pixel
.Convolution2DEnabled
) {
397 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
400 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
401 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
403 _mesa_free(tmpImage
);
405 /* finish transfer ops and pack the resulting image */
407 for (row
= 0; row
< height
; row
++) {
409 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
410 format
, type
, row
, 0);
411 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) src
,
412 format
, type
, dest
, packing
,
413 transferOps
& IMAGE_POST_CONVOLUTION_BITS
);
416 _mesa_free(convImage
);
420 const GLint dstStride
421 = _mesa_image_row_stride(packing
, width
, format
, type
);
422 GLfloat (*rgba
)[4] = swrast
->SpanArrays
->attribs
[FRAG_ATTRIB_COL0
];
425 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
428 /* make sure we don't apply 1D convolution */
429 transferOps
&= ~(IMAGE_CONVOLUTION_BIT
|
430 IMAGE_POST_CONVOLUTION_SCALE_BIAS
);
432 for (row
= 0; row
< height
; row
++, y
++) {
434 /* Get float rgba pixels */
435 if (fb
->Visual
.rgbMode
) {
436 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, GL_FLOAT
, rgba
);
439 /* read CI and convert to RGBA */
440 GLuint index
[MAX_WIDTH
];
441 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
442 rb
->GetRow(ctx
, rb
, width
, x
, y
, index
);
443 _mesa_apply_ci_transfer_ops(ctx
,
444 transferOps
& IMAGE_SHIFT_OFFSET_BIT
,
446 _mesa_map_ci_to_rgba(ctx
, width
, index
, rgba
);
449 /* apply fudge factor for shallow color buffers */
450 if (fb
->Visual
.redBits
< 8 ||
451 fb
->Visual
.greenBits
< 8 ||
452 fb
->Visual
.blueBits
< 8) {
453 adjust_colors(ctx
, width
, rgba
);
456 /* pack the row of RGBA pixels into user's buffer */
457 _mesa_pack_rgba_span_float(ctx
, width
, rgba
, format
, type
, dst
,
458 packing
, transferOps
);
467 * Read combined depth/stencil values.
468 * We'll have already done error checking to be sure the expected
469 * depth and stencil buffers really exist.
472 read_depth_stencil_pixels(GLcontext
*ctx
,
474 GLsizei width
, GLsizei height
,
475 GLenum type
, GLvoid
*pixels
,
476 const struct gl_pixelstore_attrib
*packing
)
478 const GLboolean scaleOrBias
479 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
480 const GLboolean stencilTransfer
= ctx
->Pixel
.IndexShift
481 || ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapStencilFlag
;
482 struct gl_renderbuffer
*depthRb
, *stencilRb
;
484 depthRb
= ctx
->ReadBuffer
->_DepthBuffer
;
485 stencilRb
= ctx
->ReadBuffer
->_StencilBuffer
;
487 if (!depthRb
|| !stencilRb
)
490 depthRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
491 stencilRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
493 if (depthRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
494 stencilRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
495 depthRb
== stencilRb
&&
498 /* This is the ideal case.
499 * Reading GL_DEPTH_STENCIL pixels from combined depth/stencil buffer.
500 * Plus, no pixel transfer ops to worry about!
503 GLint dstStride
= _mesa_image_row_stride(packing
, width
,
504 GL_DEPTH_STENCIL_EXT
, type
);
505 GLubyte
*dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
,
507 GL_DEPTH_STENCIL_EXT
,
509 for (i
= 0; i
< height
; i
++) {
510 depthRb
->GetRow(ctx
, depthRb
, width
, x
, y
+ i
, dst
);
515 /* Reading GL_DEPTH_STENCIL pixels from separate depth/stencil buffers,
516 * or we need pixel transfer.
519 depthRb
= ctx
->ReadBuffer
->_DepthBuffer
;
520 stencilRb
= ctx
->ReadBuffer
->_StencilBuffer
;
522 for (i
= 0; i
< height
; i
++) {
523 GLstencil stencilVals
[MAX_WIDTH
];
525 GLuint
*depthStencilDst
= (GLuint
*)
526 _mesa_image_address2d(packing
, pixels
, width
, height
,
527 GL_DEPTH_STENCIL_EXT
, type
, i
, 0);
529 _swrast_read_stencil_span(ctx
, stencilRb
, width
,
530 x
, y
+ i
, stencilVals
);
532 if (!scaleOrBias
&& !stencilTransfer
533 && ctx
->ReadBuffer
->Visual
.depthBits
== 24) {
535 GLuint zVals
[MAX_WIDTH
]; /* 24-bit values! */
537 ASSERT(depthRb
->DataType
== GL_UNSIGNED_INT
);
538 /* note, we've already been clipped */
539 depthRb
->GetRow(ctx
, depthRb
, width
, x
, y
+ i
, zVals
);
540 for (j
= 0; j
< width
; j
++) {
541 depthStencilDst
[j
] = (zVals
[j
] << 8) | (stencilVals
[j
] & 0xff);
546 GLfloat depthVals
[MAX_WIDTH
];
547 _swrast_read_depth_span_float(ctx
, depthRb
, width
, x
, y
+ i
,
549 _mesa_pack_depth_stencil_span(ctx
, width
, depthStencilDst
,
550 depthVals
, stencilVals
, packing
);
559 * Software fallback routine for ctx->Driver.ReadPixels().
560 * By time we get here, all error checking will have been done.
563 _swrast_ReadPixels( GLcontext
*ctx
,
564 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
565 GLenum format
, GLenum type
,
566 const struct gl_pixelstore_attrib
*packing
,
569 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
570 struct gl_pixelstore_attrib clippedPacking
= *packing
;
573 _mesa_update_state(ctx
);
575 /* Need to do swrast_render_start() before clipping or anything else
576 * since this is where a driver may grab the hw lock and get an updated
579 swrast_render_start(ctx
);
581 if (swrast
->NewState
)
582 _swrast_validate_derived( ctx
);
584 /* Do all needed clipping here, so that we can forget about it later */
585 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
586 /* The ReadPixels region is totally outside the window bounds */
587 swrast_render_finish(ctx
);
591 pixels
= _mesa_map_pbo_dest(ctx
, &clippedPacking
, pixels
);
597 read_index_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
600 case GL_STENCIL_INDEX
:
601 read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
604 case GL_DEPTH_COMPONENT
:
605 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
614 case GL_LUMINANCE_ALPHA
:
619 read_rgba_pixels(ctx
, x
, y
, width
, height
,
620 format
, type
, pixels
, &clippedPacking
);
622 case GL_DEPTH_STENCIL_EXT
:
623 read_depth_stencil_pixels(ctx
, x
, y
, width
, height
,
624 type
, pixels
, &clippedPacking
);
627 _mesa_problem(ctx
, "unexpected format in _swrast_ReadPixels");
628 /* don't return yet, clean-up */
631 swrast_render_finish(ctx
);
633 _mesa_unmap_pbo_dest(ctx
, &clippedPacking
);