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.
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
;
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
->InternalFormat
== GL_DEPTH_COMPONENT16
);
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
->InternalFormat
== GL_DEPTH_COMPONENT24
);
123 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
124 for (j
= 0; j
< height
; j
++, y
++) {
125 GLuint
*dest
= (GLuint
*)
126 _mesa_image_address2d(packing
, pixels
, width
, height
,
127 GL_DEPTH_COMPONENT
, type
, j
, 0);
129 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
130 /* convert range from 24-bit to 32-bit */
131 for (k
= 0; k
< width
; k
++) {
132 dest
[k
] = (dest
[k
] << 8) | (dest
[k
] >> 24);
136 else if (type
== GL_UNSIGNED_INT
&& fb
->Visual
.depthBits
== 32
137 && !biasOrScale
&& !packing
->SwapBytes
) {
138 /* Special case: directly read 32-bit unsigned depth values. */
140 ASSERT(rb
->InternalFormat
== GL_DEPTH_COMPONENT32
);
141 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
142 for (j
= 0; j
< height
; j
++, y
++) {
143 void *dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
144 GL_DEPTH_COMPONENT
, type
, j
, 0);
145 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
149 /* General case (slower) */
151 for (j
= 0; j
< height
; j
++, y
++) {
152 GLfloat depthValues
[MAX_WIDTH
];
153 GLvoid
*dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
154 GL_DEPTH_COMPONENT
, type
, j
, 0);
155 _swrast_read_depth_span_float(ctx
, rb
, width
, x
, y
, depthValues
);
156 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthValues
, packing
);
163 * Read pixels for format=GL_STENCIL_INDEX.
166 read_stencil_pixels( GLcontext
*ctx
,
168 GLsizei width
, GLsizei height
,
169 GLenum type
, GLvoid
*pixels
,
170 const struct gl_pixelstore_attrib
*packing
)
172 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
173 struct gl_renderbuffer
*rb
= fb
->_StencilBuffer
;
179 /* width should never be > MAX_WIDTH since we did clipping earlier */
180 ASSERT(width
<= MAX_WIDTH
);
182 /* process image row by row */
183 for (j
=0;j
<height
;j
++,y
++) {
185 GLstencil stencil
[MAX_WIDTH
];
187 _swrast_read_stencil_span(ctx
, rb
, width
, x
, y
, stencil
);
189 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
190 GL_STENCIL_INDEX
, type
, j
, 0);
192 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, stencil
, packing
);
199 * Optimized glReadPixels for particular pixel formats when pixel
200 * scaling, biasing, mapping, etc. are disabled.
201 * \return GL_TRUE if success, GL_FALSE if unable to do the readpixels
204 fast_read_rgba_pixels( GLcontext
*ctx
,
206 GLsizei width
, GLsizei height
,
207 GLenum format
, GLenum type
,
209 const struct gl_pixelstore_attrib
*packing
,
210 GLbitfield transferOps
)
212 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
217 ASSERT(rb
->_BaseFormat
== GL_RGBA
|| rb
->_BaseFormat
== GL_RGB
);
219 /* clipping should have already been done */
220 ASSERT(x
+ width
<= (GLint
) rb
->Width
);
221 ASSERT(y
+ height
<= (GLint
) rb
->Height
);
223 /* check for things we can't handle here */
225 packing
->SwapBytes
||
230 if (format
== GL_RGBA
&& rb
->DataType
== type
) {
231 const GLint dstStride
= _mesa_image_row_stride(packing
, width
,
234 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
238 for (row
= 0; row
< height
; row
++) {
239 rb
->GetRow(ctx
, rb
, width
, x
, y
+ row
, dest
);
245 if (format
== GL_RGB
&&
246 rb
->DataType
== GL_UNSIGNED_BYTE
&&
247 type
== GL_UNSIGNED_BYTE
) {
248 const GLint dstStride
= _mesa_image_row_stride(packing
, width
,
251 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
255 for (row
= 0; row
< height
; row
++) {
256 GLubyte tempRow
[MAX_WIDTH
][4];
258 rb
->GetRow(ctx
, rb
, width
, x
, y
+ row
, tempRow
);
259 /* convert RGBA to RGB */
260 for (col
= 0; col
< width
; col
++) {
261 dest
[col
* 3 + 0] = tempRow
[col
][0];
262 dest
[col
* 3 + 1] = tempRow
[col
][1];
263 dest
[col
* 3 + 2] = tempRow
[col
][2];
276 * When we're using a low-precision color buffer (like 16-bit 5/6/5)
277 * we have to adjust our color values a bit to pass conformance.
278 * The problem is when a 5 or 6-bit color value is convert to an 8-bit
279 * value and then a floating point value, the floating point values don't
280 * increment uniformly as the 5 or 6-bit value is incremented.
282 * This function adjusts floating point values to compensate.
285 adjust_colors(GLcontext
*ctx
, GLuint n
, GLfloat rgba
[][4])
287 const GLuint rShift
= 8 - ctx
->Visual
.redBits
;
288 const GLuint gShift
= 8 - ctx
->Visual
.greenBits
;
289 const GLuint bShift
= 8 - ctx
->Visual
.blueBits
;
290 const GLfloat rScale
= 1.0F
/ (GLfloat
) ((1 << ctx
->Visual
.redBits
) - 1);
291 const GLfloat gScale
= 1.0F
/ (GLfloat
) ((1 << ctx
->Visual
.greenBits
) - 1);
292 const GLfloat bScale
= 1.0F
/ (GLfloat
) ((1 << ctx
->Visual
.blueBits
) - 1);
294 for (i
= 0; i
< n
; i
++) {
296 /* convert float back to ubyte */
297 CLAMPED_FLOAT_TO_UBYTE(r
, rgba
[i
][RCOMP
]);
298 CLAMPED_FLOAT_TO_UBYTE(g
, rgba
[i
][GCOMP
]);
299 CLAMPED_FLOAT_TO_UBYTE(b
, rgba
[i
][BCOMP
]);
300 /* using only the N most significant bits of the ubyte value, convert to
303 rgba
[i
][RCOMP
] = (GLfloat
) (r
>> rShift
) * rScale
;
304 rgba
[i
][GCOMP
] = (GLfloat
) (g
>> gShift
) * gScale
;
305 rgba
[i
][BCOMP
] = (GLfloat
) (b
>> bShift
) * bScale
;
312 * Read R, G, B, A, RGB, L, or LA pixels.
315 read_rgba_pixels( GLcontext
*ctx
,
317 GLsizei width
, GLsizei height
,
318 GLenum format
, GLenum type
, GLvoid
*pixels
,
319 const struct gl_pixelstore_attrib
*packing
)
321 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
322 GLbitfield transferOps
= ctx
->_ImageTransferState
;
323 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
324 struct gl_renderbuffer
*rb
= fb
->_ColorReadBuffer
;
329 if (type
== GL_FLOAT
&& ((ctx
->Color
.ClampReadColor
== GL_TRUE
) ||
330 (ctx
->Color
.ClampReadColor
== GL_FIXED_ONLY_ARB
&&
331 rb
->DataType
!= GL_FLOAT
)))
332 transferOps
|= IMAGE_CLAMP_BIT
;
334 /* Try optimized path first */
335 if (fast_read_rgba_pixels(ctx
, x
, y
, width
, height
,
336 format
, type
, pixels
, packing
, transferOps
)) {
340 /* width should never be > MAX_WIDTH since we did clipping earlier */
341 ASSERT(width
<= MAX_WIDTH
);
343 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
344 GLfloat
*dest
, *src
, *tmpImage
, *convImage
;
347 tmpImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
349 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
352 convImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
354 _mesa_free(tmpImage
);
355 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
359 /* read full RGBA, FLOAT image */
361 for (row
= 0; row
< height
; row
++, y
++) {
362 if (fb
->Visual
.rgbMode
) {
363 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, GL_FLOAT
, dest
);
366 GLuint index
[MAX_WIDTH
];
367 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
368 rb
->GetRow(ctx
, rb
, width
, x
, y
, index
);
369 _mesa_apply_ci_transfer_ops(ctx
,
370 transferOps
& IMAGE_SHIFT_OFFSET_BIT
,
372 _mesa_map_ci_to_rgba(ctx
, width
, index
, (GLfloat (*)[4]) dest
);
374 _mesa_apply_rgba_transfer_ops(ctx
,
375 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
,
376 width
, (GLfloat (*)[4]) dest
);
381 if (ctx
->Pixel
.Convolution2DEnabled
) {
382 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
385 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
386 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
388 _mesa_free(tmpImage
);
390 /* finish transfer ops and pack the resulting image */
392 for (row
= 0; row
< height
; row
++) {
394 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
395 format
, type
, row
, 0);
396 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) src
,
397 format
, type
, dest
, packing
,
398 transferOps
& IMAGE_POST_CONVOLUTION_BITS
);
401 _mesa_free(convImage
);
405 const GLint dstStride
406 = _mesa_image_row_stride(packing
, width
, format
, type
);
407 GLfloat (*rgba
)[4] = swrast
->SpanArrays
->color
.sz4
.rgba
;
410 = (GLubyte
*) _mesa_image_address2d(packing
, pixels
, width
, height
,
413 /* make sure we don't apply 1D convolution */
414 transferOps
&= ~(IMAGE_CONVOLUTION_BIT
|
415 IMAGE_POST_CONVOLUTION_SCALE_BIAS
);
417 for (row
= 0; row
< height
; row
++, y
++) {
419 /* Get float rgba pixels */
420 if (fb
->Visual
.rgbMode
) {
421 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, GL_FLOAT
, rgba
);
424 /* read CI and convert to RGBA */
425 GLuint index
[MAX_WIDTH
];
426 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
427 rb
->GetRow(ctx
, rb
, width
, x
, y
, index
);
428 _mesa_apply_ci_transfer_ops(ctx
,
429 transferOps
& IMAGE_SHIFT_OFFSET_BIT
,
431 _mesa_map_ci_to_rgba(ctx
, width
, index
, rgba
);
434 /* apply fudge factor for shallow color buffers */
435 if (fb
->Visual
.redBits
< 8 ||
436 fb
->Visual
.greenBits
< 8 ||
437 fb
->Visual
.blueBits
< 8) {
438 adjust_colors(ctx
, width
, rgba
);
441 /* pack the row of RGBA pixels into user's buffer */
442 _mesa_pack_rgba_span_float(ctx
, width
, rgba
, format
, type
, dst
,
443 packing
, transferOps
);
452 * Read combined depth/stencil values.
453 * We'll have already done error checking to be sure the expected
454 * depth and stencil buffers really exist.
457 read_depth_stencil_pixels(GLcontext
*ctx
,
459 GLsizei width
, GLsizei height
,
460 GLenum type
, GLvoid
*pixels
,
461 const struct gl_pixelstore_attrib
*packing
)
463 const GLboolean scaleOrBias
464 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
465 const GLboolean stencilTransfer
= ctx
->Pixel
.IndexShift
466 || ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapStencilFlag
;
467 struct gl_renderbuffer
*depthRb
, *stencilRb
;
469 depthRb
= ctx
->ReadBuffer
->_DepthBuffer
;
470 stencilRb
= ctx
->ReadBuffer
->_StencilBuffer
;
472 if (!depthRb
|| !stencilRb
)
475 depthRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
476 stencilRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
478 if (depthRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
479 stencilRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
480 depthRb
== stencilRb
&&
483 /* This is the ideal case.
484 * Reading GL_DEPTH_STENCIL pixels from combined depth/stencil buffer.
485 * Plus, no pixel transfer ops to worry about!
488 GLint dstStride
= _mesa_image_row_stride(packing
, width
,
489 GL_DEPTH_STENCIL_EXT
, type
);
490 GLubyte
*dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
,
492 GL_DEPTH_STENCIL_EXT
,
494 for (i
= 0; i
< height
; i
++) {
495 depthRb
->GetRow(ctx
, depthRb
, width
, x
, y
+ i
, dst
);
500 /* Reading GL_DEPTH_STENCIL pixels from separate depth/stencil buffers,
501 * or we need pixel transfer.
504 depthRb
= ctx
->ReadBuffer
->_DepthBuffer
;
505 stencilRb
= ctx
->ReadBuffer
->_StencilBuffer
;
507 for (i
= 0; i
< height
; i
++) {
508 GLstencil stencilVals
[MAX_WIDTH
];
510 GLuint
*depthStencilDst
= (GLuint
*)
511 _mesa_image_address2d(packing
, pixels
, width
, height
,
512 GL_DEPTH_STENCIL_EXT
, type
, i
, 0);
514 _swrast_read_stencil_span(ctx
, stencilRb
, width
,
515 x
, y
+ i
, stencilVals
);
517 if (!scaleOrBias
&& !stencilTransfer
518 && ctx
->ReadBuffer
->Visual
.depthBits
== 24) {
520 GLuint zVals
[MAX_WIDTH
]; /* 24-bit values! */
522 ASSERT(depthRb
->DataType
== GL_UNSIGNED_INT
);
523 /* note, we've already been clipped */
524 depthRb
->GetRow(ctx
, depthRb
, width
, x
, y
+ i
, zVals
);
525 for (j
= 0; j
< width
; j
++) {
526 depthStencilDst
[j
] = (zVals
[j
] << 8) | (stencilVals
[j
] & 0xff);
531 GLfloat depthVals
[MAX_WIDTH
];
532 _swrast_read_depth_span_float(ctx
, depthRb
, width
, x
, y
+ i
,
534 _mesa_pack_depth_stencil_span(ctx
, width
, depthStencilDst
,
535 depthVals
, stencilVals
, packing
);
544 * Software fallback routine for ctx->Driver.ReadPixels().
545 * By time we get here, all error checking will have been done.
548 _swrast_ReadPixels( GLcontext
*ctx
,
549 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
550 GLenum format
, GLenum type
,
551 const struct gl_pixelstore_attrib
*packing
,
554 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
555 struct gl_pixelstore_attrib clippedPacking
= *packing
;
557 /* Need to do RENDER_START before clipping or anything else since this
558 * is where a driver may grab the hw lock and get an updated window
561 RENDER_START(swrast
, ctx
);
564 _mesa_update_state(ctx
);
566 if (swrast
->NewState
)
567 _swrast_validate_derived( ctx
);
569 /* Do all needed clipping here, so that we can forget about it later */
570 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
571 /* The ReadPixels region is totally outside the window bounds */
575 if (clippedPacking
.BufferObj
->Name
) {
578 if (!_mesa_validate_pbo_access(2, &clippedPacking
, width
, height
, 1,
579 format
, type
, pixels
)) {
580 _mesa_error(ctx
, GL_INVALID_OPERATION
,
581 "glReadPixels(invalid PBO access)");
584 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
586 clippedPacking
.BufferObj
);
588 /* buffer is already mapped - that's an error */
589 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(PBO is mapped)");
592 pixels
= ADD_POINTERS(buf
, 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 */
633 RENDER_FINISH(swrast
, ctx
);
635 if (clippedPacking
.BufferObj
->Name
) {
636 /* done with PBO so unmap it now */
637 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
638 clippedPacking
.BufferObj
);