2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 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.
27 #include "bufferobj.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
;
59 /* width should never be > MAX_WIDTH since we did clipping earlier */
60 ASSERT(width
<= MAX_WIDTH
);
62 /* process image row by row */
63 for (i
= 0; i
< height
; i
++) {
64 GLuint index
[MAX_WIDTH
];
66 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
67 rb
->GetRow(ctx
, rb
, width
, x
, y
+ i
, index
);
69 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
70 GL_COLOR_INDEX
, type
, i
, 0);
72 _mesa_pack_index_span(ctx
, width
, type
, dest
, index
,
73 &ctx
->Pack
, ctx
->_ImageTransferState
);
80 * Read pixels for format=GL_DEPTH_COMPONENT.
83 read_depth_pixels( GLcontext
*ctx
,
85 GLsizei width
, GLsizei height
,
86 GLenum type
, GLvoid
*pixels
,
87 const struct gl_pixelstore_attrib
*packing
)
89 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
90 struct gl_renderbuffer
*rb
= fb
->_DepthBuffer
;
91 const GLboolean biasOrScale
92 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
94 /* clipping should have been done already */
97 ASSERT(x
+ width
<= rb
->Width
);
98 ASSERT(y
+ height
<= rb
->Height
);
99 /* width should never be > MAX_WIDTH since we did clipping earlier */
100 ASSERT(width
<= MAX_WIDTH
);
104 if (type
== GL_UNSIGNED_SHORT
&& fb
->Visual
.depthBits
== 16
105 && !biasOrScale
&& !packing
->SwapBytes
) {
106 /* Special case: directly read 16-bit unsigned depth values. */
108 ASSERT(rb
->InternalFormat
== GL_DEPTH_COMPONENT16
);
109 ASSERT(rb
->DataType
== GL_UNSIGNED_SHORT
);
110 for (j
= 0; j
< height
; j
++, y
++) {
111 void *dest
=_mesa_image_address2d(packing
, pixels
, width
, height
,
112 GL_DEPTH_COMPONENT
, type
, j
, 0);
113 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
116 else if (type
== GL_UNSIGNED_INT
&& fb
->Visual
.depthBits
== 24
117 && !biasOrScale
&& !packing
->SwapBytes
) {
118 /* Special case: directly read 24-bit unsigned depth values. */
120 ASSERT(rb
->InternalFormat
== GL_DEPTH_COMPONENT32
);
121 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
122 for (j
= 0; j
< height
; j
++, y
++) {
123 GLuint
*dest
= (GLuint
*)
124 _mesa_image_address2d(packing
, pixels
, width
, height
,
125 GL_DEPTH_COMPONENT
, type
, j
, 0);
127 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
128 /* convert range from 24-bit to 32-bit */
129 for (k
= 0; k
< width
; k
++) {
130 dest
[k
] = (dest
[k
] << 8) | (dest
[k
] >> 24);
134 else if (type
== GL_UNSIGNED_INT
&& fb
->Visual
.depthBits
== 32
135 && !biasOrScale
&& !packing
->SwapBytes
) {
136 /* Special case: directly read 32-bit unsigned depth values. */
138 ASSERT(rb
->InternalFormat
== GL_DEPTH_COMPONENT32
);
139 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
140 for (j
= 0; j
< height
; j
++, y
++) {
141 void *dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
142 GL_DEPTH_COMPONENT
, type
, j
, 0);
143 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
147 /* General case (slower) */
149 for (j
= 0; j
< height
; j
++, y
++) {
150 GLfloat depthValues
[MAX_WIDTH
];
151 GLvoid
*dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
152 GL_DEPTH_COMPONENT
, type
, j
, 0);
153 _swrast_read_depth_span_float(ctx
, rb
, width
, x
, y
, depthValues
);
154 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthValues
, packing
);
161 * Read pixels for format=GL_STENCIL_INDEX.
164 read_stencil_pixels( GLcontext
*ctx
,
166 GLsizei width
, GLsizei height
,
167 GLenum type
, GLvoid
*pixels
,
168 const struct gl_pixelstore_attrib
*packing
)
170 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
171 struct gl_renderbuffer
*rb
= fb
->_StencilBuffer
;
176 /* width should never be > MAX_WIDTH since we did clipping earlier */
177 ASSERT(width
<= MAX_WIDTH
);
179 /* process image row by row */
180 for (j
=0;j
<height
;j
++,y
++) {
182 GLstencil stencil
[MAX_WIDTH
];
184 _swrast_read_stencil_span(ctx
, rb
, width
, x
, y
, stencil
);
186 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
187 GL_STENCIL_INDEX
, type
, j
, 0);
189 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, stencil
, packing
);
196 * Optimized glReadPixels for particular pixel formats when pixel
197 * scaling, biasing, mapping, etc. are disabled.
200 fast_read_rgba_pixels( GLcontext
*ctx
,
202 GLsizei width
, GLsizei height
,
203 GLenum format
, GLenum type
,
205 const struct gl_pixelstore_attrib
*packing
)
207 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
209 ASSERT(rb
->_BaseFormat
== GL_RGBA
|| rb
->_BaseFormat
== GL_RGB
);
211 /* clipping should have already been done */
212 ASSERT(x
+ width
<= rb
->Width
);
213 ASSERT(y
+ height
<= rb
->Height
);
215 /* check for things we can't handle here */
216 if (ctx
->_ImageTransferState
||
217 packing
->SwapBytes
||
222 if (format
== GL_RGBA
&& rb
->DataType
== type
) {
223 const GLint dstStride
= _mesa_image_row_stride(packing
, width
,
225 GLubyte
*dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
229 for (row
= 0; row
< height
; row
++) {
230 rb
->GetRow(ctx
, rb
, width
, x
, y
+ row
, dest
);
236 if (format
== GL_RGB
&&
237 rb
->DataType
== GL_UNSIGNED_BYTE
&&
238 type
== GL_UNSIGNED_BYTE
) {
239 const GLint dstStride
= _mesa_image_row_stride(packing
, width
,
241 GLubyte
*dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
245 for (row
= 0; row
< height
; row
++) {
246 GLubyte tempRow
[MAX_WIDTH
][4];
248 rb
->GetRow(ctx
, rb
, width
, x
, y
+ row
, tempRow
);
249 /* convert RGBA to RGB */
250 for (col
= 0; col
< width
; col
++) {
251 dest
[col
* 3 + 0] = tempRow
[col
][0];
252 dest
[col
* 3 + 1] = tempRow
[col
][1];
253 dest
[col
* 3 + 2] = tempRow
[col
][2];
266 * When we're using a low-precision color buffer (like 16-bit 5/6/5)
267 * we have to adjust our color values a bit to pass conformance.
268 * The problem is when a 5 or 6-bit color value is convert to an 8-bit
269 * value and then a floating point value, the floating point values don't
270 * increment uniformly as the 5 or 6-bit value is incremented.
272 * This function adjusts floating point values to compensate.
275 adjust_colors(GLcontext
*ctx
, GLuint n
, GLfloat rgba
[][4])
277 const GLuint rShift
= 8 - ctx
->Visual
.redBits
;
278 const GLuint gShift
= 8 - ctx
->Visual
.greenBits
;
279 const GLuint bShift
= 8 - ctx
->Visual
.blueBits
;
280 const GLfloat rScale
= 1.0F
/ (GLfloat
) ((1 << ctx
->Visual
.redBits
) - 1);
281 const GLfloat gScale
= 1.0F
/ (GLfloat
) ((1 << ctx
->Visual
.greenBits
) - 1);
282 const GLfloat bScale
= 1.0F
/ (GLfloat
) ((1 << ctx
->Visual
.blueBits
) - 1);
284 for (i
= 0; i
< n
; i
++) {
286 /* convert float back to ubyte */
287 CLAMPED_FLOAT_TO_UBYTE(r
, rgba
[i
][RCOMP
]);
288 CLAMPED_FLOAT_TO_UBYTE(g
, rgba
[i
][GCOMP
]);
289 CLAMPED_FLOAT_TO_UBYTE(b
, rgba
[i
][BCOMP
]);
290 /* using only the N most significant bits of the ubyte value, convert to
293 rgba
[i
][RCOMP
] = (GLfloat
) (r
>> rShift
) * rScale
;
294 rgba
[i
][GCOMP
] = (GLfloat
) (g
>> gShift
) * gScale
;
295 rgba
[i
][BCOMP
] = (GLfloat
) (b
>> bShift
) * bScale
;
302 * Read R, G, B, A, RGB, L, or LA pixels.
305 read_rgba_pixels( GLcontext
*ctx
,
307 GLsizei width
, GLsizei height
,
308 GLenum format
, GLenum type
, GLvoid
*pixels
,
309 const struct gl_pixelstore_attrib
*packing
)
311 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
312 const GLbitfield transferOps
= ctx
->_ImageTransferState
;
313 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
314 struct gl_renderbuffer
*rb
= fb
->_ColorReadBuffer
;
318 /* Try optimized path first */
319 if (fast_read_rgba_pixels(ctx
, x
, y
, width
, height
,
320 format
, type
, pixels
, packing
)) {
324 /* width should never be > MAX_WIDTH since we did clipping earlier */
325 ASSERT(width
<= MAX_WIDTH
);
327 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
328 GLfloat
*dest
, *src
, *tmpImage
, *convImage
;
331 tmpImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
333 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
336 convImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
338 _mesa_free(tmpImage
);
339 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
343 /* read full RGBA, FLOAT image */
345 for (row
= 0; row
< height
; row
++, y
++) {
346 if (fb
->Visual
.rgbMode
) {
347 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, GL_FLOAT
, dest
);
350 GLuint index
[MAX_WIDTH
];
351 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
352 rb
->GetRow(ctx
, rb
, width
, x
, y
, index
);
353 _mesa_apply_ci_transfer_ops(ctx
,
354 transferOps
& IMAGE_SHIFT_OFFSET_BIT
,
356 _mesa_map_ci_to_rgba(ctx
, width
, index
, (GLfloat (*)[4]) dest
);
358 _mesa_apply_rgba_transfer_ops(ctx
,
359 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
,
360 width
, (GLfloat (*)[4]) dest
);
365 if (ctx
->Pixel
.Convolution2DEnabled
) {
366 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
369 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
370 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
372 _mesa_free(tmpImage
);
374 /* finish transfer ops and pack the resulting image */
376 for (row
= 0; row
< height
; row
++) {
378 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
379 format
, type
, row
, 0);
380 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) src
,
381 format
, type
, dest
, packing
,
382 transferOps
& IMAGE_POST_CONVOLUTION_BITS
);
385 _mesa_free(convImage
);
389 const GLint dstStride
390 = _mesa_image_row_stride(packing
, width
, format
, type
);
391 GLfloat (*rgba
)[4] = swrast
->SpanArrays
->color
.sz4
.rgba
;
393 GLubyte
*dst
= _mesa_image_address2d(packing
, pixels
, width
, height
,
396 for (row
= 0; row
< height
; row
++, y
++) {
398 /* Get float rgba pixels */
399 if (fb
->Visual
.rgbMode
) {
400 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, GL_FLOAT
, rgba
);
403 /* read CI and convert to RGBA */
404 GLuint index
[MAX_WIDTH
];
405 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
406 rb
->GetRow(ctx
, rb
, width
, x
, y
, index
);
407 _mesa_apply_ci_transfer_ops(ctx
,
408 transferOps
& IMAGE_SHIFT_OFFSET_BIT
,
410 _mesa_map_ci_to_rgba(ctx
, width
, index
, rgba
);
413 /* apply fudge factor for shallow color buffers */
414 if (fb
->Visual
.redBits
< 8 ||
415 fb
->Visual
.greenBits
< 8 ||
416 fb
->Visual
.blueBits
< 8) {
417 adjust_colors(ctx
, width
, rgba
);
420 /* pack the row of RGBA pixels into user's buffer */
421 _mesa_pack_rgba_span_float(ctx
, width
, rgba
, format
, type
, dst
,
422 packing
, ctx
->_ImageTransferState
);
431 * Read combined depth/stencil values.
432 * We'll have already done error checking to be sure the expected
433 * depth and stencil buffers really exist.
436 read_depth_stencil_pixels(GLcontext
*ctx
,
438 GLsizei width
, GLsizei height
,
439 GLenum type
, GLvoid
*pixels
,
440 const struct gl_pixelstore_attrib
*packing
)
442 const GLboolean scaleOrBias
443 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
444 const GLboolean stencilTransfer
= ctx
->Pixel
.IndexShift
445 || ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapStencilFlag
;
446 struct gl_renderbuffer
*depthRb
, *stencilRb
;
448 depthRb
= ctx
->ReadBuffer
->_DepthBuffer
;
449 stencilRb
= ctx
->ReadBuffer
->_StencilBuffer
;
454 depthRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
455 stencilRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
457 if (depthRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
458 stencilRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
459 depthRb
== stencilRb
&&
462 /* This is the ideal case.
463 * Reading GL_DEPTH_STENCIL pixels from combined depth/stencil buffer.
464 * Plus, no pixel transfer ops to worry about!
467 GLint dstStride
= _mesa_image_row_stride(packing
, width
,
468 GL_DEPTH_STENCIL_EXT
, type
);
469 GLubyte
*dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
,
471 GL_DEPTH_STENCIL_EXT
,
473 for (i
= 0; i
< height
; i
++) {
474 depthRb
->GetRow(ctx
, depthRb
, width
, x
, y
+ i
, dst
);
479 /* Reading GL_DEPTH_STENCIL pixels from separate depth/stencil buffers,
480 * or we need pixel transfer.
483 depthRb
= ctx
->ReadBuffer
->_DepthBuffer
;
484 stencilRb
= ctx
->ReadBuffer
->_StencilBuffer
;
486 for (i
= 0; i
< height
; i
++) {
487 GLstencil stencilVals
[MAX_WIDTH
];
489 GLuint
*depthStencilDst
= (GLuint
*)
490 _mesa_image_address2d(packing
, pixels
, width
, height
,
491 GL_DEPTH_STENCIL_EXT
, type
, i
, 0);
493 _swrast_read_stencil_span(ctx
, stencilRb
, width
,
494 x
, y
+ i
, stencilVals
);
496 if (!scaleOrBias
&& !stencilTransfer
497 && ctx
->ReadBuffer
->Visual
.depthBits
== 24) {
499 GLuint zVals
[MAX_WIDTH
]; /* 24-bit values! */
501 ASSERT(depthRb
->DataType
== GL_UNSIGNED_INT
);
502 /* note, we've already been clipped */
503 depthRb
->GetRow(ctx
, depthRb
, width
, x
, y
+ i
, zVals
);
504 for (j
= 0; j
< width
; j
++) {
505 depthStencilDst
[j
] = (zVals
[j
] << 8) | (stencilVals
[j
] & 0xff);
510 GLfloat depthVals
[MAX_WIDTH
];
511 _swrast_read_depth_span_float(ctx
, depthRb
, width
, x
, y
+ i
,
513 _mesa_pack_depth_stencil_span(ctx
, width
, depthStencilDst
,
514 depthVals
, stencilVals
, packing
);
523 * Software fallback routine for ctx->Driver.ReadPixels().
524 * By time we get here, all error checking will have been done.
527 _swrast_ReadPixels( GLcontext
*ctx
,
528 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
529 GLenum format
, GLenum type
,
530 const struct gl_pixelstore_attrib
*packing
,
533 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
534 struct gl_pixelstore_attrib clippedPacking
= *packing
;
536 /* Need to do RENDER_START before clipping or anything else since this
537 * is where a driver may grab the hw lock and get an updated window
540 RENDER_START(swrast
, ctx
);
543 _mesa_update_state(ctx
);
545 if (swrast
->NewState
)
546 _swrast_validate_derived( ctx
);
548 /* Do all needed clipping here, so that we can forget about it later */
549 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
550 /* The ReadPixels region is totally outside the window bounds */
554 if (clippedPacking
.BufferObj
->Name
) {
557 if (!_mesa_validate_pbo_access(2, &clippedPacking
, width
, height
, 1,
558 format
, type
, pixels
)) {
559 _mesa_error(ctx
, GL_INVALID_OPERATION
,
560 "glReadPixels(invalid PBO access)");
563 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
565 clippedPacking
.BufferObj
);
567 /* buffer is already mapped - that's an error */
568 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(PBO is mapped)");
571 pixels
= ADD_POINTERS(buf
, pixels
);
576 read_index_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
579 case GL_STENCIL_INDEX
:
580 read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
583 case GL_DEPTH_COMPONENT
:
584 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
593 case GL_LUMINANCE_ALPHA
:
598 read_rgba_pixels(ctx
, x
, y
, width
, height
,
599 format
, type
, pixels
, &clippedPacking
);
601 case GL_DEPTH_STENCIL_EXT
:
602 read_depth_stencil_pixels(ctx
, x
, y
, width
, height
,
603 type
, pixels
, &clippedPacking
);
606 _mesa_problem(ctx
, "unexpected format in _swrast_ReadPixels");
607 /* don't return yet, clean-up */
612 RENDER_FINISH(swrast
, ctx
);
614 if (clippedPacking
.BufferObj
->Name
) {
615 /* done with PBO so unmap it now */
616 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
617 clippedPacking
.BufferObj
);