2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 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
;
59 /* width should never be > MAX_WIDTH since we did clipping earlier */
60 ASSERT(width
<= MAX_WIDTH
);
62 /* process image row by row */
63 for (i
= 0; i
< height
; i
++) {
64 GLuint index
[MAX_WIDTH
];
66 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
67 rb
->GetRow(ctx
, rb
, width
, x
, y
+ i
, index
);
69 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
70 GL_COLOR_INDEX
, type
, i
, 0);
72 _mesa_pack_index_span(ctx
, width
, type
, dest
, index
,
73 &ctx
->Pack
, ctx
->_ImageTransferState
);
80 * Read pixels for format=GL_DEPTH_COMPONENT.
83 read_depth_pixels( GLcontext
*ctx
,
85 GLsizei width
, GLsizei height
,
86 GLenum type
, GLvoid
*pixels
,
87 const struct gl_pixelstore_attrib
*packing
)
89 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
90 struct gl_renderbuffer
*rb
= fb
->_DepthBuffer
;
91 const GLboolean biasOrScale
92 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
94 /* clipping should have been done already */
97 ASSERT(x
+ width
<= rb
->Width
);
98 ASSERT(y
+ height
<= rb
->Height
);
99 /* width should never be > MAX_WIDTH since we did clipping earlier */
100 ASSERT(width
<= MAX_WIDTH
);
104 if (type
== GL_UNSIGNED_SHORT
&& fb
->Visual
.depthBits
== 16
105 && !biasOrScale
&& !packing
->SwapBytes
) {
106 /* Special case: directly read 16-bit unsigned depth values. */
108 ASSERT(rb
->InternalFormat
== GL_DEPTH_COMPONENT16
);
109 ASSERT(rb
->DataType
== GL_UNSIGNED_SHORT
);
110 for (j
= 0; j
< height
; j
++, y
++) {
111 void *dest
=_mesa_image_address2d(packing
, pixels
, width
, height
,
112 GL_DEPTH_COMPONENT
, type
, j
, 0);
113 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
116 else if (type
== GL_UNSIGNED_INT
&& fb
->Visual
.depthBits
== 24
117 && !biasOrScale
&& !packing
->SwapBytes
) {
118 /* Special case: directly read 24-bit unsigned depth values. */
120 ASSERT(rb
->InternalFormat
== GL_DEPTH_COMPONENT32
);
121 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
122 for (j
= 0; j
< height
; j
++, y
++) {
123 GLuint
*dest
= (GLuint
*)
124 _mesa_image_address2d(packing
, pixels
, width
, height
,
125 GL_DEPTH_COMPONENT
, type
, j
, 0);
127 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
128 /* convert range from 24-bit to 32-bit */
129 for (k
= 0; k
< width
; k
++) {
130 dest
[k
] = (dest
[k
] << 8) | (dest
[k
] >> 24);
134 else if (type
== GL_UNSIGNED_INT
&& fb
->Visual
.depthBits
== 32
135 && !biasOrScale
&& !packing
->SwapBytes
) {
136 /* Special case: directly read 32-bit unsigned depth values. */
138 ASSERT(rb
->InternalFormat
== GL_DEPTH_COMPONENT32
);
139 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
140 for (j
= 0; j
< height
; j
++, y
++) {
141 void *dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
142 GL_DEPTH_COMPONENT
, type
, j
, 0);
143 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
147 /* General case (slower) */
149 for (j
= 0; j
< height
; j
++, y
++) {
150 GLfloat depthValues
[MAX_WIDTH
];
151 GLvoid
*dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
152 GL_DEPTH_COMPONENT
, type
, j
, 0);
153 _swrast_read_depth_span_float(ctx
, rb
, width
, x
, y
, depthValues
);
154 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthValues
, packing
);
161 * Read pixels for format=GL_STENCIL_INDEX.
164 read_stencil_pixels( GLcontext
*ctx
,
166 GLsizei width
, GLsizei height
,
167 GLenum type
, GLvoid
*pixels
,
168 const struct gl_pixelstore_attrib
*packing
)
170 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
171 struct gl_renderbuffer
*rb
= fb
->_StencilBuffer
;
176 /* width should never be > MAX_WIDTH since we did clipping earlier */
177 ASSERT(width
<= MAX_WIDTH
);
179 /* process image row by row */
180 for (j
=0;j
<height
;j
++,y
++) {
182 GLstencil stencil
[MAX_WIDTH
];
184 _swrast_read_stencil_span(ctx
, rb
, width
, x
, y
, stencil
);
186 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
187 GL_STENCIL_INDEX
, type
, j
, 0);
189 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, stencil
, packing
);
196 * Optimized glReadPixels for particular pixel formats:
197 * GL_UNSIGNED_BYTE, GL_RGBA
198 * when pixel scaling, biasing and mapping are disabled.
201 read_fast_rgba_pixels( GLcontext
*ctx
,
203 GLsizei width
, GLsizei height
,
204 GLenum format
, GLenum type
,
206 const struct gl_pixelstore_attrib
*packing
)
208 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
210 /* can't do scale, bias, mapping, etc */
211 if (ctx
->_ImageTransferState
)
214 /* can't do fancy pixel packing */
215 if (packing
->Alignment
!= 1 || packing
->SwapBytes
|| packing
->LsbFirst
)
221 GLint readWidth
= width
; /* actual width read */
222 GLint readHeight
= height
; /* actual height read */
223 GLint skipPixels
= packing
->SkipPixels
;
224 GLint skipRows
= packing
->SkipRows
;
227 if (packing
->RowLength
> 0)
228 rowLength
= packing
->RowLength
;
234 * The window region at (destX, destY) of size (readWidth, readHeight)
236 * We'll write pixel data to buffer pointed to by "pixels" but we'll
237 * skip "skipRows" rows and skip "skipPixels" pixels/row.
240 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
)
241 #elif CHAN_BITS == 16
242 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_SHORT
)
247 GLchan
*dest
= (GLchan
*) pixels
248 + (skipRows
* rowLength
+ skipPixels
) * 4;
251 if (packing
->Invert
) {
252 /* start at top and go down */
253 dest
+= (readHeight
- 1) * rowLength
* 4;
254 rowLength
= -rowLength
;
258 for (row
=0; row
<readHeight
; row
++) {
259 rb
->GetRow(ctx
, rb
, readWidth
, srcX
, srcY
, dest
);
260 dest
+= rowLength
* 4;
266 /* can't do this format/type combination */
275 * Read R, G, B, A, RGB, L, or LA pixels.
278 read_rgba_pixels( GLcontext
*ctx
,
280 GLsizei width
, GLsizei height
,
281 GLenum format
, GLenum type
, GLvoid
*pixels
,
282 const struct gl_pixelstore_attrib
*packing
)
284 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
285 struct gl_renderbuffer
*rb
= fb
->_ColorReadBuffer
;
289 /* Try optimized path first */
290 if (read_fast_rgba_pixels( ctx
, x
, y
, width
, height
,
291 format
, type
, pixels
, packing
)) {
295 /* width should never be > MAX_WIDTH since we did clipping earlier */
296 ASSERT(width
<= MAX_WIDTH
);
298 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
299 const GLuint transferOps
= ctx
->_ImageTransferState
;
300 GLfloat
*dest
, *src
, *tmpImage
, *convImage
;
303 tmpImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
305 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
308 convImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
310 _mesa_free(tmpImage
);
311 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
315 /* read full RGBA, FLOAT image */
317 for (row
= 0; row
< height
; row
++, y
++) {
318 GLchan rgba
[MAX_WIDTH
][4];
319 if (fb
->Visual
.rgbMode
) {
320 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, rgba
);
323 GLuint index
[MAX_WIDTH
];
324 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
325 rb
->GetRow(ctx
, rb
, width
, x
, y
, index
);
326 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!=0 ) {
327 _mesa_map_ci(ctx
, width
, index
);
329 _mesa_map_ci_to_rgba_chan(ctx
, width
, index
, rgba
);
331 _mesa_pack_rgba_span_chan(ctx
, width
, (const GLchan (*)[4]) rgba
,
332 GL_RGBA
, GL_FLOAT
, dest
, &ctx
->DefaultPacking
,
333 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
);
338 if (ctx
->Pixel
.Convolution2DEnabled
) {
339 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
342 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
343 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
345 _mesa_free(tmpImage
);
347 /* finish transfer ops and pack the resulting image */
349 for (row
= 0; row
< height
; row
++) {
351 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
352 format
, type
, row
, 0);
353 _mesa_pack_rgba_span_float(ctx
, width
,
354 (const GLfloat (*)[4]) src
,
355 format
, type
, dest
, packing
,
356 transferOps
& IMAGE_POST_CONVOLUTION_BITS
);
359 _mesa_free(convImage
);
364 for (row
= 0; row
< height
; row
++, y
++) {
365 GLchan rgba
[MAX_WIDTH
][4];
367 if (fb
->Visual
.rgbMode
) {
368 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, rgba
);
371 GLuint index
[MAX_WIDTH
];
372 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
373 rb
->GetRow(ctx
, rb
, width
, x
, y
, index
);
374 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!= 0) {
375 _mesa_map_ci(ctx
, width
, index
);
377 _mesa_map_ci_to_rgba_chan(ctx
, width
, index
, rgba
);
379 dst
= _mesa_image_address2d(packing
, pixels
, width
, height
,
380 format
, type
, row
, 0);
381 if (fb
->Visual
.redBits
< CHAN_BITS
||
382 fb
->Visual
.greenBits
< CHAN_BITS
||
383 fb
->Visual
.blueBits
< CHAN_BITS
) {
384 /* Requantize the color values into floating point and go from
385 * there. This fixes conformance failures with 5/6/5 color
386 * buffers, for example.
388 GLfloat rgbaf
[MAX_WIDTH
][4];
389 _mesa_chan_to_float_span(ctx
, width
,
390 (CONST
GLchan (*)[4]) rgba
, rgbaf
);
391 _mesa_pack_rgba_span_float(ctx
, width
,
392 (CONST
GLfloat (*)[4]) rgbaf
,
393 format
, type
, dst
, packing
,
394 ctx
->_ImageTransferState
);
397 /* GLubytes are fine */
398 _mesa_pack_rgba_span_chan(ctx
, width
, (CONST
GLchan (*)[4]) rgba
,
399 format
, type
, dst
, packing
,
400 ctx
->_ImageTransferState
);
408 * Read combined depth/stencil values.
409 * We'll have already done error checking to be sure the expected
410 * depth and stencil buffers really exist.
413 read_depth_stencil_pixels(GLcontext
*ctx
,
415 GLsizei width
, GLsizei height
,
416 GLenum type
, GLvoid
*pixels
,
417 const struct gl_pixelstore_attrib
*packing
)
419 const GLboolean scaleOrBias
420 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
421 const GLboolean stencilTransfer
= ctx
->Pixel
.IndexShift
422 || ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapStencilFlag
;
423 struct gl_renderbuffer
*depthRb
, *stencilRb
;
425 depthRb
= ctx
->ReadBuffer
->_DepthBuffer
;
426 stencilRb
= ctx
->ReadBuffer
->_StencilBuffer
;
431 depthRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
432 stencilRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
434 if (depthRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
435 stencilRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
436 depthRb
== stencilRb
&&
439 /* This is the ideal case.
440 * Reading GL_DEPTH_STENCIL pixels from combined depth/stencil buffer.
441 * Plus, no pixel transfer ops to worry about!
444 GLint dstStride
= _mesa_image_row_stride(packing
, width
,
445 GL_DEPTH_STENCIL_EXT
, type
);
446 GLubyte
*dst
= (GLubyte
*) _mesa_image_address2d(packing
, pixels
,
448 GL_DEPTH_STENCIL_EXT
,
450 for (i
= 0; i
< height
; i
++) {
451 depthRb
->GetRow(ctx
, depthRb
, width
, x
, y
+ i
, dst
);
456 /* Reading GL_DEPTH_STENCIL pixels from separate depth/stencil buffers,
457 * or we need pixel transfer.
460 depthRb
= ctx
->ReadBuffer
->_DepthBuffer
;
461 stencilRb
= ctx
->ReadBuffer
->_StencilBuffer
;
463 for (i
= 0; i
< height
; i
++) {
464 GLstencil stencilVals
[MAX_WIDTH
];
466 GLuint
*depthStencilDst
= (GLuint
*)
467 _mesa_image_address2d(packing
, pixels
, width
, height
,
468 GL_DEPTH_STENCIL_EXT
, type
, i
, 0);
470 _swrast_read_stencil_span(ctx
, stencilRb
, width
,
471 x
, y
+ i
, stencilVals
);
473 if (!scaleOrBias
&& !stencilTransfer
474 && ctx
->ReadBuffer
->Visual
.depthBits
== 24) {
476 GLuint zVals
[MAX_WIDTH
]; /* 24-bit values! */
478 ASSERT(depthRb
->DataType
== GL_UNSIGNED_INT
);
479 /* note, we've already been clipped */
480 depthRb
->GetRow(ctx
, depthRb
, width
, x
, y
+ i
, zVals
);
481 for (j
= 0; j
< width
; j
++) {
482 depthStencilDst
[j
] = (zVals
[j
] << 8) | (stencilVals
[j
] & 0xff);
487 GLfloat depthVals
[MAX_WIDTH
];
488 _swrast_read_depth_span_float(ctx
, depthRb
, width
, x
, y
+ i
,
490 _mesa_pack_depth_stencil_span(ctx
, width
, depthStencilDst
,
491 depthVals
, stencilVals
, packing
);
500 * Software fallback routine for ctx->Driver.ReadPixels().
501 * By time we get here, all error checking will have been done.
504 _swrast_ReadPixels( GLcontext
*ctx
,
505 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
506 GLenum format
, GLenum type
,
507 const struct gl_pixelstore_attrib
*packing
,
510 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
511 struct gl_pixelstore_attrib clippedPacking
= *packing
;
513 /* Need to do RENDER_START before clipping or anything else since this
514 * is where a driver may grab the hw lock and get an updated window
517 RENDER_START(swrast
, ctx
);
520 _mesa_update_state(ctx
);
522 if (swrast
->NewState
)
523 _swrast_validate_derived( ctx
);
525 /* Do all needed clipping here, so that we can forget about it later */
526 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
527 /* The ReadPixels region is totally outside the window bounds */
531 if (clippedPacking
.BufferObj
->Name
) {
534 if (!_mesa_validate_pbo_access(2, &clippedPacking
, width
, height
, 1,
535 format
, type
, pixels
)) {
536 _mesa_error(ctx
, GL_INVALID_OPERATION
,
537 "glReadPixels(invalid PBO access)");
540 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
542 clippedPacking
.BufferObj
);
544 /* buffer is already mapped - that's an error */
545 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(PBO is mapped)");
548 pixels
= ADD_POINTERS(buf
, pixels
);
553 read_index_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
556 case GL_STENCIL_INDEX
:
557 read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
560 case GL_DEPTH_COMPONENT
:
561 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
570 case GL_LUMINANCE_ALPHA
:
575 read_rgba_pixels(ctx
, x
, y
, width
, height
,
576 format
, type
, pixels
, &clippedPacking
);
578 case GL_DEPTH_STENCIL_EXT
:
579 read_depth_stencil_pixels(ctx
, x
, y
, width
, height
,
580 type
, pixels
, &clippedPacking
);
583 _mesa_problem(ctx
, "unexpected format in _swrast_ReadPixels");
584 /* don't return yet, clean-up */
589 RENDER_FINISH(swrast
, ctx
);
591 if (clippedPacking
.BufferObj
->Name
) {
592 /* done with PBO so unmap it now */
593 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
594 clippedPacking
.BufferObj
);