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 _swrast_use_read_buffer(ctx
);
79 /* XXX: width should never be > MAX_WIDTH since we did clipping earlier */
80 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
82 /* process image row by row */
83 for (i
= 0; i
< height
; i
++) {
84 GLuint index
[MAX_WIDTH
];
86 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
87 rb
->GetRow(ctx
, rb
, readWidth
, x
, y
+ i
, index
);
89 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
90 GL_COLOR_INDEX
, type
, i
, 0);
92 _mesa_pack_index_span(ctx
, readWidth
, type
, dest
, index
,
93 &ctx
->Pack
, ctx
->_ImageTransferState
);
96 _swrast_use_draw_buffer(ctx
);
102 * Read pixels for format=GL_DEPTH_COMPONENT.
105 read_depth_pixels( GLcontext
*ctx
,
107 GLsizei width
, GLsizei height
,
108 GLenum type
, GLvoid
*pixels
,
109 const struct gl_pixelstore_attrib
*packing
)
111 struct gl_renderbuffer
*rb
112 = ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
114 GLboolean bias_or_scale
;
117 if (ctx
->Visual
.depthBits
<= 0 || !rb
) {
118 /* No depth buffer */
119 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
123 if (type
!= GL_BYTE
&&
124 type
!= GL_UNSIGNED_BYTE
&&
126 type
!= GL_UNSIGNED_SHORT
&&
128 type
!= GL_UNSIGNED_INT
&&
130 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(depth type)");
134 /* XXX: width should never be > MAX_WIDTH since we did clipping earlier */
135 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
137 bias_or_scale
= ctx
->Pixel
.DepthBias
!=0.0 || ctx
->Pixel
.DepthScale
!=1.0;
139 if (type
==GL_UNSIGNED_SHORT
&& ctx
->Visual
.depthBits
== 16
140 && !bias_or_scale
&& !packing
->SwapBytes
) {
141 /* Special case: directly read 16-bit unsigned depth values. */
143 for (j
=0;j
<height
;j
++,y
++) {
144 GLdepth depth
[MAX_WIDTH
];
145 GLushort
*dst
= (GLushort
*) _mesa_image_address2d(packing
, pixels
,
146 width
, height
, GL_DEPTH_COMPONENT
, type
, j
, 0);
148 _swrast_read_depth_span(ctx
, rb
, width
, x
, y
, depth
);
149 for (i
= 0; i
< width
; i
++)
153 else if (type
==GL_UNSIGNED_INT
&& ctx
->Visual
.depthBits
== 32
154 && !bias_or_scale
&& !packing
->SwapBytes
) {
155 /* Special case: directly read 32-bit unsigned depth values. */
157 for (j
=0;j
<height
;j
++,y
++) {
158 GLdepth
*dst
= (GLdepth
*) _mesa_image_address2d(packing
, pixels
,
159 width
, height
, GL_DEPTH_COMPONENT
, type
, j
, 0);
160 _swrast_read_depth_span(ctx
, rb
, width
, x
, y
, dst
);
164 /* General case (slower) */
166 for (j
=0;j
<height
;j
++,y
++) {
167 GLfloat depth
[MAX_WIDTH
];
170 _swrast_read_depth_span_float(ctx
, rb
, readWidth
, x
, y
, depth
);
172 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
173 GL_DEPTH_COMPONENT
, type
, j
, 0);
175 _mesa_pack_depth_span(ctx
, readWidth
, (GLdepth
*) dest
, type
,
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_renderbuffer
*rb
193 = ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
196 if (type
!= GL_BYTE
&&
197 type
!= GL_UNSIGNED_BYTE
&&
199 type
!= GL_UNSIGNED_SHORT
&&
201 type
!= GL_UNSIGNED_INT
&&
204 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(stencil type)");
208 if (ctx
->Visual
.stencilBits
<= 0 || !rb
) {
209 /* No stencil buffer */
210 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
214 /* XXX: width should never be > MAX_WIDTH since we did clipping earlier */
215 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
217 /* process image row by row */
218 for (j
=0;j
<height
;j
++,y
++) {
220 GLstencil stencil
[MAX_WIDTH
];
222 _swrast_read_stencil_span(ctx
, rb
, readWidth
, x
, y
, stencil
);
224 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
225 GL_STENCIL_INDEX
, type
, j
, 0);
227 _mesa_pack_stencil_span(ctx
, readWidth
, type
, dest
, stencil
, packing
);
234 * Optimized glReadPixels for particular pixel formats:
235 * GL_UNSIGNED_BYTE, GL_RGBA
236 * when pixel scaling, biasing and mapping are disabled.
239 read_fast_rgba_pixels( GLcontext
*ctx
,
241 GLsizei width
, GLsizei height
,
242 GLenum format
, GLenum type
,
244 const struct gl_pixelstore_attrib
*packing
)
246 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
247 /* can't do scale, bias, mapping, etc */
248 if (ctx
->_ImageTransferState
)
251 /* can't do fancy pixel packing */
252 if (packing
->Alignment
!= 1 || packing
->SwapBytes
|| packing
->LsbFirst
)
258 GLint readWidth
= width
; /* actual width read */
259 GLint readHeight
= height
; /* actual height read */
260 GLint skipPixels
= packing
->SkipPixels
;
261 GLint skipRows
= packing
->SkipRows
;
264 if (packing
->RowLength
> 0)
265 rowLength
= packing
->RowLength
;
271 * The window region at (destX, destY) of size (readWidth, readHeight)
273 * We'll write pixel data to buffer pointed to by "pixels" but we'll
274 * skip "skipRows" rows and skip "skipPixels" pixels/row.
277 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
278 #elif CHAN_BITS == 16
279 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_SHORT
) {
283 GLchan
*dest
= (GLchan
*) pixels
284 + (skipRows
* rowLength
+ skipPixels
) * 4;
287 if (packing
->Invert
) {
288 /* start at top and go down */
289 dest
+= (readHeight
- 1) * rowLength
* 4;
290 rowLength
= -rowLength
;
294 for (row
=0; row
<readHeight
; row
++) {
295 rb
->GetRow(ctx
, rb
, readWidth
, srcX
, srcY
, dest
);
296 dest
+= rowLength
* 4;
302 /* can't do this format/type combination */
311 * Read R, G, B, A, RGB, L, or LA pixels.
314 read_rgba_pixels( GLcontext
*ctx
,
316 GLsizei width
, GLsizei height
,
317 GLenum format
, GLenum type
, GLvoid
*pixels
,
318 const struct gl_pixelstore_attrib
*packing
)
320 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
324 /* No readbuffer is OK with GL_EXT_framebuffer_object */
328 /* do error checking on pixel type, format was already checked by caller */
330 case GL_UNSIGNED_BYTE
:
332 case GL_UNSIGNED_SHORT
:
334 case GL_UNSIGNED_INT
:
337 case GL_UNSIGNED_BYTE_3_3_2
:
338 case GL_UNSIGNED_BYTE_2_3_3_REV
:
339 case GL_UNSIGNED_SHORT_5_6_5
:
340 case GL_UNSIGNED_SHORT_5_6_5_REV
:
341 case GL_UNSIGNED_SHORT_4_4_4_4
:
342 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
343 case GL_UNSIGNED_SHORT_5_5_5_1
:
344 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
345 case GL_UNSIGNED_INT_8_8_8_8
:
346 case GL_UNSIGNED_INT_8_8_8_8_REV
:
347 case GL_UNSIGNED_INT_10_10_10_2
:
348 case GL_UNSIGNED_INT_2_10_10_10_REV
:
349 /* valid pixel type */
351 case GL_HALF_FLOAT_ARB
:
352 if (!ctx
->Extensions
.ARB_half_float_pixel
) {
353 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(type)" );
358 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(type)" );
362 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
) ||
363 format
== GL_INTENSITY
) {
364 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(format or type)");
368 _swrast_use_read_buffer(ctx
);
370 /* Try optimized path first */
371 if (read_fast_rgba_pixels( ctx
, x
, y
, width
, height
,
372 format
, type
, pixels
, packing
)) {
374 _swrast_use_draw_buffer(ctx
);
378 /* XXX: width should never be > MAX_WIDTH since we did clipping earlier */
379 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
382 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
383 const GLuint transferOps
= ctx
->_ImageTransferState
;
384 GLfloat
*dest
, *src
, *tmpImage
, *convImage
;
387 tmpImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
389 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
392 convImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
394 _mesa_free(tmpImage
);
395 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
399 /* read full RGBA, FLOAT image */
401 for (row
= 0; row
< height
; row
++, y
++) {
402 GLchan rgba
[MAX_WIDTH
][4];
403 if (ctx
->Visual
.rgbMode
) {
404 _swrast_read_rgba_span(ctx
, rb
, readWidth
, x
, y
, rgba
);
407 GLuint index
[MAX_WIDTH
];
408 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
409 rb
->GetRow(ctx
, rb
, readWidth
, x
, y
, index
);
410 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!=0 ) {
411 _mesa_map_ci(ctx
, readWidth
, index
);
413 _mesa_map_ci_to_rgba_chan(ctx
, readWidth
, index
, rgba
);
415 _mesa_pack_rgba_span_chan(ctx
, readWidth
, (const GLchan (*)[4]) rgba
,
416 GL_RGBA
, GL_FLOAT
, dest
, &ctx
->DefaultPacking
,
417 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
);
422 if (ctx
->Pixel
.Convolution2DEnabled
) {
423 _mesa_convolve_2d_image(ctx
, &readWidth
, &height
, tmpImage
, convImage
);
426 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
427 _mesa_convolve_sep_image(ctx
, &readWidth
, &height
, tmpImage
, convImage
);
429 _mesa_free(tmpImage
);
431 /* finish transfer ops and pack the resulting image */
433 for (row
= 0; row
< height
; row
++) {
435 dest
= _mesa_image_address2d(packing
, pixels
, readWidth
, height
,
436 format
, type
, row
, 0);
437 _mesa_pack_rgba_span_float(ctx
, readWidth
,
438 (const GLfloat (*)[4]) src
,
439 format
, type
, dest
, packing
,
440 transferOps
& IMAGE_POST_CONVOLUTION_BITS
);
441 src
+= readWidth
* 4;
447 for (row
= 0; row
< height
; row
++, y
++) {
448 GLchan rgba
[MAX_WIDTH
][4];
450 if (ctx
->Visual
.rgbMode
) {
451 _swrast_read_rgba_span(ctx
, rb
, readWidth
, x
, y
, rgba
);
454 GLuint index
[MAX_WIDTH
];
455 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
456 rb
->GetRow(ctx
, rb
, readWidth
, x
, y
, index
);
457 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!= 0) {
458 _mesa_map_ci(ctx
, readWidth
, index
);
460 _mesa_map_ci_to_rgba_chan(ctx
, readWidth
, index
, rgba
);
462 dst
= _mesa_image_address2d(packing
, pixels
, width
, height
,
463 format
, type
, row
, 0);
464 if (ctx
->Visual
.redBits
< CHAN_BITS
||
465 ctx
->Visual
.greenBits
< CHAN_BITS
||
466 ctx
->Visual
.blueBits
< CHAN_BITS
) {
467 /* Requantize the color values into floating point and go from
468 * there. This fixes conformance failures with 16-bit color
469 * buffers, for example.
471 DEFMARRAY(GLfloat
, rgbaf
, MAX_WIDTH
, 4); /* mac 32k limitation */
472 CHECKARRAY(rgbaf
, return); /* mac 32k limitation */
473 _mesa_chan_to_float_span(ctx
, readWidth
,
474 (CONST
GLchan (*)[4]) rgba
, rgbaf
);
475 _mesa_pack_rgba_span_float(ctx
, readWidth
,
476 (CONST
GLfloat (*)[4]) rgbaf
,
477 format
, type
, dst
, packing
,
478 ctx
->_ImageTransferState
);
479 UNDEFARRAY(rgbaf
); /* mac 32k limitation */
482 /* GLubytes are fine */
483 _mesa_pack_rgba_span_chan(ctx
, readWidth
, (CONST
GLchan (*)[4]) rgba
,
484 format
, type
, dst
, packing
,
485 ctx
->_ImageTransferState
);
490 _swrast_use_draw_buffer(ctx
);
495 * Software fallback routine for ctx->Driver.ReadPixels().
496 * We wind up using the swrast->ReadSpan() routines to do the job.
499 _swrast_ReadPixels( GLcontext
*ctx
,
500 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
501 GLenum format
, GLenum type
,
502 const struct gl_pixelstore_attrib
*packing
,
505 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
506 struct gl_pixelstore_attrib clippedPacking
;
508 if (swrast
->NewState
)
509 _swrast_validate_derived( ctx
);
511 /* Do all needed clipping here, so that we can forget about it later */
512 clippedPacking
= *packing
;
513 if (clippedPacking
.RowLength
== 0) {
514 clippedPacking
.RowLength
= width
;
516 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
,
517 &clippedPacking
.SkipPixels
,
518 &clippedPacking
.SkipRows
)) {
519 /* The ReadPixels region is totally outside the window bounds */
523 if (clippedPacking
.BufferObj
->Name
) {
526 if (!_mesa_validate_pbo_access(2, &clippedPacking
, width
, height
, 1,
527 format
, type
, pixels
)) {
528 _mesa_error(ctx
, GL_INVALID_OPERATION
,
529 "glReadPixels(invalid PBO access)");
532 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
534 clippedPacking
.BufferObj
);
536 /* buffer is already mapped - that's an error */
537 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(PBO is mapped)");
540 pixels
= ADD_POINTERS(buf
, pixels
);
543 RENDER_START(swrast
, ctx
);
547 read_index_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
550 case GL_STENCIL_INDEX
:
551 read_stencil_pixels(ctx
, x
,y
, width
,height
, type
, pixels
,
554 case GL_DEPTH_COMPONENT
:
555 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
564 case GL_LUMINANCE_ALPHA
:
569 read_rgba_pixels(ctx
, x
, y
, width
, height
,
570 format
, type
, pixels
, &clippedPacking
);
573 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(format)" );
574 /* don't return yet, clean-up */
577 RENDER_FINISH(swrast
, ctx
);
579 if (clippedPacking
.BufferObj
->Name
) {
580 /* done with PBO so unmap it now */
581 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
582 clippedPacking
.BufferObj
);