2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 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"
37 #include "s_context.h"
40 #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
;
58 if (ctx
->Visual
.rgbMode
) {
59 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
63 if (type
!= GL_BYTE
&&
64 type
!= GL_UNSIGNED_BYTE
&&
66 type
!= GL_UNSIGNED_SHORT
&&
68 type
!= GL_UNSIGNED_INT
&&
70 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(index type)");
75 return; /* no readbuffer OK */
78 /* width should never be > MAX_WIDTH since we did clipping earlier */
79 ASSERT(width
<= MAX_WIDTH
);
81 /* process image row by row */
82 for (i
= 0; i
< height
; i
++) {
83 GLuint index
[MAX_WIDTH
];
85 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
86 rb
->GetRow(ctx
, rb
, width
, x
, y
+ i
, index
);
88 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
89 GL_COLOR_INDEX
, type
, i
, 0);
91 _mesa_pack_index_span(ctx
, width
, type
, dest
, index
,
92 &ctx
->Pack
, ctx
->_ImageTransferState
);
99 * Read pixels for format=GL_DEPTH_COMPONENT.
102 read_depth_pixels( GLcontext
*ctx
,
104 GLsizei width
, GLsizei height
,
105 GLenum type
, GLvoid
*pixels
,
106 const struct gl_pixelstore_attrib
*packing
)
108 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
109 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
110 GLboolean bias_or_scale
;
112 /* clipping should have been done already */
115 ASSERT(x
+ width
<= rb
->Width
);
116 ASSERT(y
+ height
<= rb
->Height
);
117 /* width should never be > MAX_WIDTH since we did clipping earlier */
118 ASSERT(width
<= MAX_WIDTH
);
121 if (fb
->Visual
.depthBits
<= 0) {
122 /* No depth buffer */
123 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
127 if (type
!= GL_BYTE
&&
128 type
!= GL_UNSIGNED_BYTE
&&
130 type
!= GL_UNSIGNED_SHORT
&&
132 type
!= GL_UNSIGNED_INT
&&
134 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(depth type)");
139 return; /* no readbuffer OK */
142 bias_or_scale
= ctx
->Pixel
.DepthBias
!= 0.0 || ctx
->Pixel
.DepthScale
!= 1.0;
144 if (type
== GL_UNSIGNED_SHORT
&& fb
->Visual
.depthBits
== 16
145 && !bias_or_scale
&& !packing
->SwapBytes
) {
146 /* Special case: directly read 16-bit unsigned depth values. */
148 ASSERT(rb
->InternalFormat
== GL_DEPTH_COMPONENT16
);
149 ASSERT(rb
->DataType
== GL_UNSIGNED_SHORT
);
150 for (j
= 0; j
< height
; j
++, y
++) {
151 void *dest
=_mesa_image_address2d(packing
, pixels
, width
, height
,
152 GL_DEPTH_COMPONENT
, type
, j
, 0);
153 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
156 else if (type
== GL_UNSIGNED_INT
&& fb
->Visual
.depthBits
== 32
157 && !bias_or_scale
&& !packing
->SwapBytes
) {
158 /* Special case: directly read 32-bit unsigned depth values. */
160 ASSERT(rb
->InternalFormat
== GL_DEPTH_COMPONENT32
);
161 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
162 for (j
= 0; j
< height
; j
++, y
++) {
163 void *dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
164 GL_DEPTH_COMPONENT
, type
, j
, 0);
165 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
169 /* General case (slower) */
171 for (j
= 0; j
< height
; j
++, y
++) {
172 GLfloat depthValues
[MAX_WIDTH
];
173 GLvoid
*dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
174 GL_DEPTH_COMPONENT
, type
, j
, 0);
175 _swrast_read_depth_span_float(ctx
, rb
, width
, x
, y
, depthValues
);
176 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthValues
, packing
);
183 * Read pixels for format=GL_STENCIL_INDEX.
186 read_stencil_pixels( GLcontext
*ctx
,
188 GLsizei width
, GLsizei height
,
189 GLenum type
, GLvoid
*pixels
,
190 const struct gl_pixelstore_attrib
*packing
)
192 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
193 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
197 /* no readbuffer - OK */
201 if (type
!= GL_BYTE
&&
202 type
!= GL_UNSIGNED_BYTE
&&
204 type
!= GL_UNSIGNED_SHORT
&&
206 type
!= GL_UNSIGNED_INT
&&
209 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(stencil type)");
213 if (fb
->Visual
.stencilBits
<= 0) {
214 /* No stencil buffer */
215 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
219 /* width should never be > MAX_WIDTH since we did clipping earlier */
220 ASSERT(width
<= MAX_WIDTH
);
222 /* process image row by row */
223 for (j
=0;j
<height
;j
++,y
++) {
225 GLstencil stencil
[MAX_WIDTH
];
227 _swrast_read_stencil_span(ctx
, rb
, width
, x
, y
, stencil
);
229 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
230 GL_STENCIL_INDEX
, type
, j
, 0);
232 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, stencil
, packing
);
239 * Optimized glReadPixels for particular pixel formats:
240 * GL_UNSIGNED_BYTE, GL_RGBA
241 * when pixel scaling, biasing and mapping are disabled.
244 read_fast_rgba_pixels( GLcontext
*ctx
,
246 GLsizei width
, GLsizei height
,
247 GLenum format
, GLenum type
,
249 const struct gl_pixelstore_attrib
*packing
)
251 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
253 /* can't do scale, bias, mapping, etc */
254 if (ctx
->_ImageTransferState
)
257 /* can't do fancy pixel packing */
258 if (packing
->Alignment
!= 1 || packing
->SwapBytes
|| packing
->LsbFirst
)
264 GLint readWidth
= width
; /* actual width read */
265 GLint readHeight
= height
; /* actual height read */
266 GLint skipPixels
= packing
->SkipPixels
;
267 GLint skipRows
= packing
->SkipRows
;
270 if (packing
->RowLength
> 0)
271 rowLength
= packing
->RowLength
;
277 * The window region at (destX, destY) of size (readWidth, readHeight)
279 * We'll write pixel data to buffer pointed to by "pixels" but we'll
280 * skip "skipRows" rows and skip "skipPixels" pixels/row.
283 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
284 #elif CHAN_BITS == 16
285 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_SHORT
) {
289 GLchan
*dest
= (GLchan
*) pixels
290 + (skipRows
* rowLength
+ skipPixels
) * 4;
293 if (packing
->Invert
) {
294 /* start at top and go down */
295 dest
+= (readHeight
- 1) * rowLength
* 4;
296 rowLength
= -rowLength
;
300 for (row
=0; row
<readHeight
; row
++) {
301 rb
->GetRow(ctx
, rb
, readWidth
, srcX
, srcY
, dest
);
302 dest
+= rowLength
* 4;
308 /* can't do this format/type combination */
317 * Read R, G, B, A, RGB, L, or LA pixels.
320 read_rgba_pixels( GLcontext
*ctx
,
322 GLsizei width
, GLsizei height
,
323 GLenum format
, GLenum type
, GLvoid
*pixels
,
324 const struct gl_pixelstore_attrib
*packing
)
326 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
327 struct gl_renderbuffer
*rb
= fb
->_ColorReadBuffer
;
330 /* No readbuffer is OK with GL_EXT_framebuffer_object */
334 /* do error checking on pixel type, format was already checked by caller */
336 case GL_UNSIGNED_BYTE
:
338 case GL_UNSIGNED_SHORT
:
340 case GL_UNSIGNED_INT
:
343 case GL_UNSIGNED_BYTE_3_3_2
:
344 case GL_UNSIGNED_BYTE_2_3_3_REV
:
345 case GL_UNSIGNED_SHORT_5_6_5
:
346 case GL_UNSIGNED_SHORT_5_6_5_REV
:
347 case GL_UNSIGNED_SHORT_4_4_4_4
:
348 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
349 case GL_UNSIGNED_SHORT_5_5_5_1
:
350 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
351 case GL_UNSIGNED_INT_8_8_8_8
:
352 case GL_UNSIGNED_INT_8_8_8_8_REV
:
353 case GL_UNSIGNED_INT_10_10_10_2
:
354 case GL_UNSIGNED_INT_2_10_10_10_REV
:
355 /* valid pixel type */
357 case GL_HALF_FLOAT_ARB
:
358 if (!ctx
->Extensions
.ARB_half_float_pixel
) {
359 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(type)" );
364 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(type)" );
368 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
) ||
369 format
== GL_INTENSITY
) {
370 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(format or type)");
374 /* Try optimized path first */
375 if (read_fast_rgba_pixels( ctx
, x
, y
, width
, height
,
376 format
, type
, pixels
, packing
)) {
380 /* width should never be > MAX_WIDTH since we did clipping earlier */
381 ASSERT(width
<= MAX_WIDTH
);
383 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
384 const GLuint transferOps
= ctx
->_ImageTransferState
;
385 GLfloat
*dest
, *src
, *tmpImage
, *convImage
;
388 tmpImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
390 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
393 convImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
395 _mesa_free(tmpImage
);
396 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
400 /* read full RGBA, FLOAT image */
402 for (row
= 0; row
< height
; row
++, y
++) {
403 GLchan rgba
[MAX_WIDTH
][4];
404 if (fb
->Visual
.rgbMode
) {
405 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, rgba
);
408 GLuint index
[MAX_WIDTH
];
409 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
410 rb
->GetRow(ctx
, rb
, width
, x
, y
, index
);
411 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!=0 ) {
412 _mesa_map_ci(ctx
, width
, index
);
414 _mesa_map_ci_to_rgba_chan(ctx
, width
, index
, rgba
);
416 _mesa_pack_rgba_span_chan(ctx
, width
, (const GLchan (*)[4]) rgba
,
417 GL_RGBA
, GL_FLOAT
, dest
, &ctx
->DefaultPacking
,
418 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
);
423 if (ctx
->Pixel
.Convolution2DEnabled
) {
424 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
427 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
428 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
430 _mesa_free(tmpImage
);
432 /* finish transfer ops and pack the resulting image */
434 for (row
= 0; row
< height
; row
++) {
436 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
437 format
, type
, row
, 0);
438 _mesa_pack_rgba_span_float(ctx
, width
,
439 (const GLfloat (*)[4]) src
,
440 format
, type
, dest
, packing
,
441 transferOps
& IMAGE_POST_CONVOLUTION_BITS
);
448 for (row
= 0; row
< height
; row
++, y
++) {
449 GLchan rgba
[MAX_WIDTH
][4];
451 if (fb
->Visual
.rgbMode
) {
452 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, rgba
);
455 GLuint index
[MAX_WIDTH
];
456 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
457 rb
->GetRow(ctx
, rb
, width
, x
, y
, index
);
458 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!= 0) {
459 _mesa_map_ci(ctx
, width
, index
);
461 _mesa_map_ci_to_rgba_chan(ctx
, width
, index
, rgba
);
463 dst
= _mesa_image_address2d(packing
, pixels
, width
, height
,
464 format
, type
, row
, 0);
465 if (fb
->Visual
.redBits
< CHAN_BITS
||
466 fb
->Visual
.greenBits
< CHAN_BITS
||
467 fb
->Visual
.blueBits
< CHAN_BITS
) {
468 /* Requantize the color values into floating point and go from
469 * there. This fixes conformance failures with 16-bit color
470 * buffers, for example.
472 GLfloat rgbaf
[MAX_WIDTH
][4];
473 _mesa_chan_to_float_span(ctx
, width
,
474 (CONST
GLchan (*)[4]) rgba
, rgbaf
);
475 _mesa_pack_rgba_span_float(ctx
, width
,
476 (CONST
GLfloat (*)[4]) rgbaf
,
477 format
, type
, dst
, packing
,
478 ctx
->_ImageTransferState
);
481 /* GLubytes are fine */
482 _mesa_pack_rgba_span_chan(ctx
, width
, (CONST
GLchan (*)[4]) rgba
,
483 format
, type
, dst
, packing
,
484 ctx
->_ImageTransferState
);
492 * Software fallback routine for ctx->Driver.ReadPixels().
493 * We wind up using the swrast->ReadSpan() routines to do the job.
496 _swrast_ReadPixels( GLcontext
*ctx
,
497 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
498 GLenum format
, GLenum type
,
499 const struct gl_pixelstore_attrib
*packing
,
502 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
503 struct gl_pixelstore_attrib clippedPacking
;
505 if (swrast
->NewState
)
506 _swrast_validate_derived( ctx
);
508 /* Do all needed clipping here, so that we can forget about it later */
509 clippedPacking
= *packing
;
510 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
,
511 &clippedPacking
.SkipPixels
,
512 &clippedPacking
.SkipRows
)) {
513 /* The ReadPixels region is totally outside the window bounds */
517 if (clippedPacking
.BufferObj
->Name
) {
520 if (!_mesa_validate_pbo_access(2, &clippedPacking
, width
, height
, 1,
521 format
, type
, pixels
)) {
522 _mesa_error(ctx
, GL_INVALID_OPERATION
,
523 "glReadPixels(invalid PBO access)");
526 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
528 clippedPacking
.BufferObj
);
530 /* buffer is already mapped - that's an error */
531 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(PBO is mapped)");
534 pixels
= ADD_POINTERS(buf
, pixels
);
537 RENDER_START(swrast
, ctx
);
541 read_index_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
544 case GL_STENCIL_INDEX
:
545 read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
548 case GL_DEPTH_COMPONENT
:
549 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
558 case GL_LUMINANCE_ALPHA
:
563 read_rgba_pixels(ctx
, x
, y
, width
, height
,
564 format
, type
, pixels
, &clippedPacking
);
567 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(format)" );
568 /* don't return yet, clean-up */
571 RENDER_FINISH(swrast
, ctx
);
573 if (clippedPacking
.BufferObj
->Name
) {
574 /* done with PBO so unmap it now */
575 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
576 clippedPacking
.BufferObj
);