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
;
56 SWcontext *swrast = SWRAST_CONTEXT(ctx);
61 if (ctx
->Visual
.rgbMode
) {
62 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
66 if (type
!= GL_BYTE
&&
67 type
!= GL_UNSIGNED_BYTE
&&
69 type
!= GL_UNSIGNED_SHORT
&&
71 type
!= GL_UNSIGNED_INT
&&
73 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(index type)");
77 /* XXX: width should never be > MAX_WIDTH since we did clipping earlier */
78 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
80 /* process image row by row */
81 for (i
= 0; i
< height
; i
++) {
82 GLuint index
[MAX_WIDTH
];
84 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
85 rb
->GetRow(ctx
, rb
, readWidth
, x
, y
+ i
, index
);
87 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
88 GL_COLOR_INDEX
, type
, i
, 0);
90 _mesa_pack_index_span(ctx
, readWidth
, type
, dest
, index
,
91 &ctx
->Pack
, ctx
->_ImageTransferState
);
98 * Read pixels for format=GL_DEPTH_COMPONENT.
101 read_depth_pixels( GLcontext
*ctx
,
103 GLsizei width
, GLsizei height
,
104 GLenum type
, GLvoid
*pixels
,
105 const struct gl_pixelstore_attrib
*packing
)
107 struct gl_renderbuffer
*rb
108 = ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
110 GLboolean bias_or_scale
;
113 /* no readbuffer - OK */
118 if (ctx
->Visual
.depthBits
<= 0) {
119 /* No depth buffer */
120 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
124 if (type
!= GL_BYTE
&&
125 type
!= GL_UNSIGNED_BYTE
&&
127 type
!= GL_UNSIGNED_SHORT
&&
129 type
!= GL_UNSIGNED_INT
&&
131 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(depth type)");
135 /* XXX: width should never be > MAX_WIDTH since we did clipping earlier */
136 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
138 bias_or_scale
= ctx
->Pixel
.DepthBias
!=0.0 || ctx
->Pixel
.DepthScale
!=1.0;
140 if (type
==GL_UNSIGNED_SHORT
&& ctx
->Visual
.depthBits
== 16
141 && !bias_or_scale
&& !packing
->SwapBytes
) {
142 /* Special case: directly read 16-bit unsigned depth values. */
144 for (j
=0;j
<height
;j
++,y
++) {
145 GLdepth depth
[MAX_WIDTH
];
146 GLushort
*dst
= (GLushort
*) _mesa_image_address2d(packing
, pixels
,
147 width
, height
, GL_DEPTH_COMPONENT
, type
, j
, 0);
149 _swrast_read_depth_span(ctx
, rb
, width
, x
, y
, depth
);
150 for (i
= 0; i
< width
; i
++)
154 else if (type
==GL_UNSIGNED_INT
&& ctx
->Visual
.depthBits
== 32
155 && !bias_or_scale
&& !packing
->SwapBytes
) {
156 /* Special case: directly read 32-bit unsigned depth values. */
158 for (j
=0;j
<height
;j
++,y
++) {
159 GLdepth
*dst
= (GLdepth
*) _mesa_image_address2d(packing
, pixels
,
160 width
, height
, GL_DEPTH_COMPONENT
, type
, j
, 0);
161 _swrast_read_depth_span(ctx
, rb
, width
, x
, y
, dst
);
165 /* General case (slower) */
167 for (j
=0;j
<height
;j
++,y
++) {
168 GLfloat depth
[MAX_WIDTH
];
171 _swrast_read_depth_span_float(ctx
, rb
, readWidth
, x
, y
, depth
);
173 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
174 GL_DEPTH_COMPONENT
, type
, j
, 0);
176 _mesa_pack_depth_span(ctx
, readWidth
, (GLdepth
*) dest
, type
,
184 * Read pixels for format=GL_STENCIL_INDEX.
187 read_stencil_pixels( GLcontext
*ctx
,
189 GLsizei width
, GLsizei height
,
190 GLenum type
, GLvoid
*pixels
,
191 const struct gl_pixelstore_attrib
*packing
)
193 struct gl_renderbuffer
*rb
194 = ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
198 /* no readbuffer - OK */
202 if (type
!= GL_BYTE
&&
203 type
!= GL_UNSIGNED_BYTE
&&
205 type
!= GL_UNSIGNED_SHORT
&&
207 type
!= GL_UNSIGNED_INT
&&
210 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(stencil type)");
214 if (ctx
->Visual
.stencilBits
<= 0) {
215 /* No stencil buffer */
216 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
220 /* XXX: width should never be > MAX_WIDTH since we did clipping earlier */
221 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
223 /* process image row by row */
224 for (j
=0;j
<height
;j
++,y
++) {
226 GLstencil stencil
[MAX_WIDTH
];
228 _swrast_read_stencil_span(ctx
, rb
, readWidth
, x
, y
, stencil
);
230 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
231 GL_STENCIL_INDEX
, type
, j
, 0);
233 _mesa_pack_stencil_span(ctx
, readWidth
, type
, dest
, stencil
, packing
);
240 * Optimized glReadPixels for particular pixel formats:
241 * GL_UNSIGNED_BYTE, GL_RGBA
242 * when pixel scaling, biasing and mapping are disabled.
245 read_fast_rgba_pixels( GLcontext
*ctx
,
247 GLsizei width
, GLsizei height
,
248 GLenum format
, GLenum type
,
250 const struct gl_pixelstore_attrib
*packing
)
252 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_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
328 SWcontext *swrast = SWRAST_CONTEXT(ctx);
333 /* No readbuffer is OK with GL_EXT_framebuffer_object */
337 /* do error checking on pixel type, format was already checked by caller */
339 case GL_UNSIGNED_BYTE
:
341 case GL_UNSIGNED_SHORT
:
343 case GL_UNSIGNED_INT
:
346 case GL_UNSIGNED_BYTE_3_3_2
:
347 case GL_UNSIGNED_BYTE_2_3_3_REV
:
348 case GL_UNSIGNED_SHORT_5_6_5
:
349 case GL_UNSIGNED_SHORT_5_6_5_REV
:
350 case GL_UNSIGNED_SHORT_4_4_4_4
:
351 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
352 case GL_UNSIGNED_SHORT_5_5_5_1
:
353 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
354 case GL_UNSIGNED_INT_8_8_8_8
:
355 case GL_UNSIGNED_INT_8_8_8_8_REV
:
356 case GL_UNSIGNED_INT_10_10_10_2
:
357 case GL_UNSIGNED_INT_2_10_10_10_REV
:
358 /* valid pixel type */
360 case GL_HALF_FLOAT_ARB
:
361 if (!ctx
->Extensions
.ARB_half_float_pixel
) {
362 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(type)" );
367 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(type)" );
371 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
) ||
372 format
== GL_INTENSITY
) {
373 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(format or type)");
377 /* Try optimized path first */
378 if (read_fast_rgba_pixels( ctx
, x
, y
, width
, height
,
379 format
, type
, pixels
, packing
)) {
383 /* XXX: width should never be > MAX_WIDTH since we did clipping earlier */
384 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
387 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
388 const GLuint transferOps
= ctx
->_ImageTransferState
;
389 GLfloat
*dest
, *src
, *tmpImage
, *convImage
;
392 tmpImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
394 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
397 convImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
399 _mesa_free(tmpImage
);
400 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
404 /* read full RGBA, FLOAT image */
406 for (row
= 0; row
< height
; row
++, y
++) {
407 GLchan rgba
[MAX_WIDTH
][4];
408 if (ctx
->Visual
.rgbMode
) {
409 _swrast_read_rgba_span(ctx
, rb
, readWidth
, x
, y
, rgba
);
412 GLuint index
[MAX_WIDTH
];
413 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
414 rb
->GetRow(ctx
, rb
, readWidth
, x
, y
, index
);
415 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!=0 ) {
416 _mesa_map_ci(ctx
, readWidth
, index
);
418 _mesa_map_ci_to_rgba_chan(ctx
, readWidth
, index
, rgba
);
420 _mesa_pack_rgba_span_chan(ctx
, readWidth
, (const GLchan (*)[4]) rgba
,
421 GL_RGBA
, GL_FLOAT
, dest
, &ctx
->DefaultPacking
,
422 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
);
427 if (ctx
->Pixel
.Convolution2DEnabled
) {
428 _mesa_convolve_2d_image(ctx
, &readWidth
, &height
, tmpImage
, convImage
);
431 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
432 _mesa_convolve_sep_image(ctx
, &readWidth
, &height
, tmpImage
, convImage
);
434 _mesa_free(tmpImage
);
436 /* finish transfer ops and pack the resulting image */
438 for (row
= 0; row
< height
; row
++) {
440 dest
= _mesa_image_address2d(packing
, pixels
, readWidth
, height
,
441 format
, type
, row
, 0);
442 _mesa_pack_rgba_span_float(ctx
, readWidth
,
443 (const GLfloat (*)[4]) src
,
444 format
, type
, dest
, packing
,
445 transferOps
& IMAGE_POST_CONVOLUTION_BITS
);
446 src
+= readWidth
* 4;
452 for (row
= 0; row
< height
; row
++, y
++) {
453 GLchan rgba
[MAX_WIDTH
][4];
455 if (ctx
->Visual
.rgbMode
) {
456 _swrast_read_rgba_span(ctx
, rb
, readWidth
, x
, y
, rgba
);
459 GLuint index
[MAX_WIDTH
];
460 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
461 rb
->GetRow(ctx
, rb
, readWidth
, x
, y
, index
);
462 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!= 0) {
463 _mesa_map_ci(ctx
, readWidth
, index
);
465 _mesa_map_ci_to_rgba_chan(ctx
, readWidth
, index
, rgba
);
467 dst
= _mesa_image_address2d(packing
, pixels
, width
, height
,
468 format
, type
, row
, 0);
469 if (ctx
->Visual
.redBits
< CHAN_BITS
||
470 ctx
->Visual
.greenBits
< CHAN_BITS
||
471 ctx
->Visual
.blueBits
< CHAN_BITS
) {
472 /* Requantize the color values into floating point and go from
473 * there. This fixes conformance failures with 16-bit color
474 * buffers, for example.
476 GLfloat rgbaf
[MAX_WIDTH
][4];
477 _mesa_chan_to_float_span(ctx
, readWidth
,
478 (CONST
GLchan (*)[4]) rgba
, rgbaf
);
479 _mesa_pack_rgba_span_float(ctx
, readWidth
,
480 (CONST
GLfloat (*)[4]) rgbaf
,
481 format
, type
, dst
, packing
,
482 ctx
->_ImageTransferState
);
485 /* GLubytes are fine */
486 _mesa_pack_rgba_span_chan(ctx
, readWidth
, (CONST
GLchan (*)[4]) rgba
,
487 format
, type
, dst
, packing
,
488 ctx
->_ImageTransferState
);
496 * Software fallback routine for ctx->Driver.ReadPixels().
497 * We wind up using the swrast->ReadSpan() routines to do the job.
500 _swrast_ReadPixels( GLcontext
*ctx
,
501 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
502 GLenum format
, GLenum type
,
503 const struct gl_pixelstore_attrib
*packing
,
506 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
507 struct gl_pixelstore_attrib clippedPacking
;
509 if (swrast
->NewState
)
510 _swrast_validate_derived( ctx
);
512 /* Do all needed clipping here, so that we can forget about it later */
513 clippedPacking
= *packing
;
514 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
,
515 &clippedPacking
.SkipPixels
,
516 &clippedPacking
.SkipRows
)) {
517 /* The ReadPixels region is totally outside the window bounds */
521 if (clippedPacking
.BufferObj
->Name
) {
524 if (!_mesa_validate_pbo_access(2, &clippedPacking
, width
, height
, 1,
525 format
, type
, pixels
)) {
526 _mesa_error(ctx
, GL_INVALID_OPERATION
,
527 "glReadPixels(invalid PBO access)");
530 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
532 clippedPacking
.BufferObj
);
534 /* buffer is already mapped - that's an error */
535 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(PBO is mapped)");
538 pixels
= ADD_POINTERS(buf
, pixels
);
541 RENDER_START(swrast
, ctx
);
545 read_index_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
548 case GL_STENCIL_INDEX
:
549 read_stencil_pixels(ctx
, x
,y
, width
,height
, type
, pixels
,
552 case GL_DEPTH_COMPONENT
:
553 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
562 case GL_LUMINANCE_ALPHA
:
567 read_rgba_pixels(ctx
, x
, y
, width
, height
,
568 format
, type
, pixels
, &clippedPacking
);
571 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(format)" );
572 /* don't return yet, clean-up */
575 RENDER_FINISH(swrast
, ctx
);
577 if (clippedPacking
.BufferObj
->Name
) {
578 /* done with PBO so unmap it now */
579 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
580 clippedPacking
.BufferObj
);