2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 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 /* no readbuffer - OK */
122 if (ctx
->Visual
.depthBits
<= 0) {
123 /* No depth buffer */
124 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
128 if (type
!= GL_BYTE
&&
129 type
!= GL_UNSIGNED_BYTE
&&
131 type
!= GL_UNSIGNED_SHORT
&&
133 type
!= GL_UNSIGNED_INT
&&
135 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(depth type)");
139 /* XXX: width should never be > MAX_WIDTH since we did clipping earlier */
140 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
142 bias_or_scale
= ctx
->Pixel
.DepthBias
!=0.0 || ctx
->Pixel
.DepthScale
!=1.0;
144 if (type
==GL_UNSIGNED_SHORT
&& ctx
->Visual
.depthBits
== 16
145 && !bias_or_scale
&& !packing
->SwapBytes
) {
146 /* Special case: directly read 16-bit unsigned depth values. */
148 for (j
=0;j
<height
;j
++,y
++) {
149 GLdepth depth
[MAX_WIDTH
];
150 GLushort
*dst
= (GLushort
*) _mesa_image_address2d(packing
, pixels
,
151 width
, height
, GL_DEPTH_COMPONENT
, type
, j
, 0);
153 _swrast_read_depth_span(ctx
, rb
, width
, x
, y
, depth
);
154 for (i
= 0; i
< width
; i
++)
158 else if (type
==GL_UNSIGNED_INT
&& ctx
->Visual
.depthBits
== 32
159 && !bias_or_scale
&& !packing
->SwapBytes
) {
160 /* Special case: directly read 32-bit unsigned depth values. */
162 for (j
=0;j
<height
;j
++,y
++) {
163 GLdepth
*dst
= (GLdepth
*) _mesa_image_address2d(packing
, pixels
,
164 width
, height
, GL_DEPTH_COMPONENT
, type
, j
, 0);
165 _swrast_read_depth_span(ctx
, rb
, width
, x
, y
, dst
);
169 /* General case (slower) */
171 for (j
=0;j
<height
;j
++,y
++) {
172 GLfloat depth
[MAX_WIDTH
];
175 _swrast_read_depth_span_float(ctx
, rb
, readWidth
, x
, y
, depth
);
177 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
178 GL_DEPTH_COMPONENT
, type
, j
, 0);
180 _mesa_pack_depth_span(ctx
, readWidth
, (GLdepth
*) dest
, type
,
188 * Read pixels for format=GL_STENCIL_INDEX.
191 read_stencil_pixels( GLcontext
*ctx
,
193 GLsizei width
, GLsizei height
,
194 GLenum type
, GLvoid
*pixels
,
195 const struct gl_pixelstore_attrib
*packing
)
197 struct gl_renderbuffer
*rb
198 = ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
202 /* no readbuffer - OK */
206 if (type
!= GL_BYTE
&&
207 type
!= GL_UNSIGNED_BYTE
&&
209 type
!= GL_UNSIGNED_SHORT
&&
211 type
!= GL_UNSIGNED_INT
&&
214 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels(stencil type)");
218 if (ctx
->Visual
.stencilBits
<= 0) {
219 /* No stencil buffer */
220 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glReadPixels" );
224 /* XXX: width should never be > MAX_WIDTH since we did clipping earlier */
225 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
227 /* process image row by row */
228 for (j
=0;j
<height
;j
++,y
++) {
230 GLstencil stencil
[MAX_WIDTH
];
232 _swrast_read_stencil_span(ctx
, rb
, readWidth
, x
, y
, stencil
);
234 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
235 GL_STENCIL_INDEX
, type
, j
, 0);
237 _mesa_pack_stencil_span(ctx
, readWidth
, type
, dest
, stencil
, packing
);
244 * Optimized glReadPixels for particular pixel formats:
245 * GL_UNSIGNED_BYTE, GL_RGBA
246 * when pixel scaling, biasing and mapping are disabled.
249 read_fast_rgba_pixels( GLcontext
*ctx
,
251 GLsizei width
, GLsizei height
,
252 GLenum format
, GLenum type
,
254 const struct gl_pixelstore_attrib
*packing
)
256 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
257 /* can't do scale, bias, mapping, etc */
258 if (ctx
->_ImageTransferState
)
261 /* can't do fancy pixel packing */
262 if (packing
->Alignment
!= 1 || packing
->SwapBytes
|| packing
->LsbFirst
)
268 GLint readWidth
= width
; /* actual width read */
269 GLint readHeight
= height
; /* actual height read */
270 GLint skipPixels
= packing
->SkipPixels
;
271 GLint skipRows
= packing
->SkipRows
;
274 if (packing
->RowLength
> 0)
275 rowLength
= packing
->RowLength
;
281 * The window region at (destX, destY) of size (readWidth, readHeight)
283 * We'll write pixel data to buffer pointed to by "pixels" but we'll
284 * skip "skipRows" rows and skip "skipPixels" pixels/row.
287 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
288 #elif CHAN_BITS == 16
289 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_SHORT
) {
293 GLchan
*dest
= (GLchan
*) pixels
294 + (skipRows
* rowLength
+ skipPixels
) * 4;
297 if (packing
->Invert
) {
298 /* start at top and go down */
299 dest
+= (readHeight
- 1) * rowLength
* 4;
300 rowLength
= -rowLength
;
304 for (row
=0; row
<readHeight
; row
++) {
305 rb
->GetRow(ctx
, rb
, readWidth
, srcX
, srcY
, dest
);
306 dest
+= rowLength
* 4;
312 /* can't do this format/type combination */
321 * Read R, G, B, A, RGB, L, or LA pixels.
324 read_rgba_pixels( GLcontext
*ctx
,
326 GLsizei width
, GLsizei height
,
327 GLenum format
, GLenum type
, GLvoid
*pixels
,
328 const struct gl_pixelstore_attrib
*packing
)
330 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
332 SWcontext *swrast = SWRAST_CONTEXT(ctx);
337 /* No readbuffer is OK with GL_EXT_framebuffer_object */
341 /* do error checking on pixel type, format was already checked by caller */
343 case GL_UNSIGNED_BYTE
:
345 case GL_UNSIGNED_SHORT
:
347 case GL_UNSIGNED_INT
:
350 case GL_UNSIGNED_BYTE_3_3_2
:
351 case GL_UNSIGNED_BYTE_2_3_3_REV
:
352 case GL_UNSIGNED_SHORT_5_6_5
:
353 case GL_UNSIGNED_SHORT_5_6_5_REV
:
354 case GL_UNSIGNED_SHORT_4_4_4_4
:
355 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
356 case GL_UNSIGNED_SHORT_5_5_5_1
:
357 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
358 case GL_UNSIGNED_INT_8_8_8_8
:
359 case GL_UNSIGNED_INT_8_8_8_8_REV
:
360 case GL_UNSIGNED_INT_10_10_10_2
:
361 case GL_UNSIGNED_INT_2_10_10_10_REV
:
362 /* valid pixel type */
364 case GL_HALF_FLOAT_ARB
:
365 if (!ctx
->Extensions
.ARB_half_float_pixel
) {
366 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(type)" );
371 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(type)" );
375 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
) ||
376 format
== GL_INTENSITY
) {
377 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(format or type)");
381 _swrast_use_read_buffer(ctx
);
383 /* Try optimized path first */
384 if (read_fast_rgba_pixels( ctx
, x
, y
, width
, height
,
385 format
, type
, pixels
, packing
)) {
387 _swrast_use_draw_buffer(ctx
);
391 /* XXX: width should never be > MAX_WIDTH since we did clipping earlier */
392 readWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
395 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
396 const GLuint transferOps
= ctx
->_ImageTransferState
;
397 GLfloat
*dest
, *src
, *tmpImage
, *convImage
;
400 tmpImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
402 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
405 convImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
407 _mesa_free(tmpImage
);
408 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
412 /* read full RGBA, FLOAT image */
414 for (row
= 0; row
< height
; row
++, y
++) {
415 GLchan rgba
[MAX_WIDTH
][4];
416 if (ctx
->Visual
.rgbMode
) {
417 _swrast_read_rgba_span(ctx
, rb
, readWidth
, x
, y
, rgba
);
420 GLuint index
[MAX_WIDTH
];
421 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
422 rb
->GetRow(ctx
, rb
, readWidth
, x
, y
, index
);
423 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!=0 ) {
424 _mesa_map_ci(ctx
, readWidth
, index
);
426 _mesa_map_ci_to_rgba_chan(ctx
, readWidth
, index
, rgba
);
428 _mesa_pack_rgba_span_chan(ctx
, readWidth
, (const GLchan (*)[4]) rgba
,
429 GL_RGBA
, GL_FLOAT
, dest
, &ctx
->DefaultPacking
,
430 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
);
435 if (ctx
->Pixel
.Convolution2DEnabled
) {
436 _mesa_convolve_2d_image(ctx
, &readWidth
, &height
, tmpImage
, convImage
);
439 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
440 _mesa_convolve_sep_image(ctx
, &readWidth
, &height
, tmpImage
, convImage
);
442 _mesa_free(tmpImage
);
444 /* finish transfer ops and pack the resulting image */
446 for (row
= 0; row
< height
; row
++) {
448 dest
= _mesa_image_address2d(packing
, pixels
, readWidth
, height
,
449 format
, type
, row
, 0);
450 _mesa_pack_rgba_span_float(ctx
, readWidth
,
451 (const GLfloat (*)[4]) src
,
452 format
, type
, dest
, packing
,
453 transferOps
& IMAGE_POST_CONVOLUTION_BITS
);
454 src
+= readWidth
* 4;
460 for (row
= 0; row
< height
; row
++, y
++) {
461 GLchan rgba
[MAX_WIDTH
][4];
463 if (ctx
->Visual
.rgbMode
) {
464 _swrast_read_rgba_span(ctx
, rb
, readWidth
, x
, y
, rgba
);
467 GLuint index
[MAX_WIDTH
];
468 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
469 rb
->GetRow(ctx
, rb
, readWidth
, x
, y
, index
);
470 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!= 0) {
471 _mesa_map_ci(ctx
, readWidth
, index
);
473 _mesa_map_ci_to_rgba_chan(ctx
, readWidth
, index
, rgba
);
475 dst
= _mesa_image_address2d(packing
, pixels
, width
, height
,
476 format
, type
, row
, 0);
477 if (ctx
->Visual
.redBits
< CHAN_BITS
||
478 ctx
->Visual
.greenBits
< CHAN_BITS
||
479 ctx
->Visual
.blueBits
< CHAN_BITS
) {
480 /* Requantize the color values into floating point and go from
481 * there. This fixes conformance failures with 16-bit color
482 * buffers, for example.
484 DEFMARRAY(GLfloat
, rgbaf
, MAX_WIDTH
, 4); /* mac 32k limitation */
485 CHECKARRAY(rgbaf
, return); /* mac 32k limitation */
486 _mesa_chan_to_float_span(ctx
, readWidth
,
487 (CONST
GLchan (*)[4]) rgba
, rgbaf
);
488 _mesa_pack_rgba_span_float(ctx
, readWidth
,
489 (CONST
GLfloat (*)[4]) rgbaf
,
490 format
, type
, dst
, packing
,
491 ctx
->_ImageTransferState
);
492 UNDEFARRAY(rgbaf
); /* mac 32k limitation */
495 /* GLubytes are fine */
496 _mesa_pack_rgba_span_chan(ctx
, readWidth
, (CONST
GLchan (*)[4]) rgba
,
497 format
, type
, dst
, packing
,
498 ctx
->_ImageTransferState
);
503 _swrast_use_draw_buffer(ctx
);
508 * Software fallback routine for ctx->Driver.ReadPixels().
509 * We wind up using the swrast->ReadSpan() routines to do the job.
512 _swrast_ReadPixels( GLcontext
*ctx
,
513 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
514 GLenum format
, GLenum type
,
515 const struct gl_pixelstore_attrib
*packing
,
518 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
519 struct gl_pixelstore_attrib clippedPacking
;
521 if (swrast
->NewState
)
522 _swrast_validate_derived( ctx
);
524 /* Do all needed clipping here, so that we can forget about it later */
525 clippedPacking
= *packing
;
526 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
,
527 &clippedPacking
.SkipPixels
,
528 &clippedPacking
.SkipRows
)) {
529 /* The ReadPixels region is totally outside the window bounds */
533 if (clippedPacking
.BufferObj
->Name
) {
536 if (!_mesa_validate_pbo_access(2, &clippedPacking
, width
, height
, 1,
537 format
, type
, pixels
)) {
538 _mesa_error(ctx
, GL_INVALID_OPERATION
,
539 "glReadPixels(invalid PBO access)");
542 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
544 clippedPacking
.BufferObj
);
546 /* buffer is already mapped - that's an error */
547 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(PBO is mapped)");
550 pixels
= ADD_POINTERS(buf
, pixels
);
553 RENDER_START(swrast
, ctx
);
557 read_index_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
560 case GL_STENCIL_INDEX
:
561 read_stencil_pixels(ctx
, x
,y
, width
,height
, type
, pixels
,
564 case GL_DEPTH_COMPONENT
:
565 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
574 case GL_LUMINANCE_ALPHA
:
579 read_rgba_pixels(ctx
, x
, y
, width
, height
,
580 format
, type
, pixels
, &clippedPacking
);
583 _mesa_error( ctx
, GL_INVALID_ENUM
, "glReadPixels(format)" );
584 /* don't return yet, clean-up */
587 RENDER_FINISH(swrast
, ctx
);
589 if (clippedPacking
.BufferObj
->Name
) {
590 /* done with PBO so unmap it now */
591 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
592 clippedPacking
.BufferObj
);