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"
44 * Read a block of color index pixels.
47 read_index_pixels( GLcontext
*ctx
,
49 GLsizei width
, GLsizei height
,
50 GLenum type
, GLvoid
*pixels
,
51 const struct gl_pixelstore_attrib
*packing
)
53 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
57 return; /* no readbuffer OK */
60 /* width should never be > MAX_WIDTH since we did clipping earlier */
61 ASSERT(width
<= MAX_WIDTH
);
63 /* process image row by row */
64 for (i
= 0; i
< height
; i
++) {
65 GLuint index
[MAX_WIDTH
];
67 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
68 rb
->GetRow(ctx
, rb
, width
, x
, y
+ i
, index
);
70 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
71 GL_COLOR_INDEX
, type
, i
, 0);
73 _mesa_pack_index_span(ctx
, width
, type
, dest
, index
,
74 &ctx
->Pack
, ctx
->_ImageTransferState
);
81 * Read pixels for format=GL_DEPTH_COMPONENT.
84 read_depth_pixels( GLcontext
*ctx
,
86 GLsizei width
, GLsizei height
,
87 GLenum type
, GLvoid
*pixels
,
88 const struct gl_pixelstore_attrib
*packing
)
90 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
91 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
92 GLboolean bias_or_scale
;
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
);
103 return; /* no readbuffer OK */
106 bias_or_scale
= ctx
->Pixel
.DepthBias
!= 0.0 || ctx
->Pixel
.DepthScale
!= 1.0;
108 if (type
== GL_UNSIGNED_SHORT
&& fb
->Visual
.depthBits
== 16
109 && !bias_or_scale
&& !packing
->SwapBytes
) {
110 /* Special case: directly read 16-bit unsigned depth values. */
112 ASSERT(rb
->InternalFormat
== GL_DEPTH_COMPONENT16
);
113 ASSERT(rb
->DataType
== GL_UNSIGNED_SHORT
);
114 for (j
= 0; j
< height
; j
++, y
++) {
115 void *dest
=_mesa_image_address2d(packing
, pixels
, width
, height
,
116 GL_DEPTH_COMPONENT
, type
, j
, 0);
117 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
120 else if (type
== GL_UNSIGNED_INT
&& fb
->Visual
.depthBits
== 32
121 && !bias_or_scale
&& !packing
->SwapBytes
) {
122 /* Special case: directly read 32-bit unsigned depth values. */
124 ASSERT(rb
->InternalFormat
== GL_DEPTH_COMPONENT32
);
125 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
126 for (j
= 0; j
< height
; j
++, y
++) {
127 void *dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
128 GL_DEPTH_COMPONENT
, type
, j
, 0);
129 rb
->GetRow(ctx
, rb
, width
, x
, y
, dest
);
133 /* General case (slower) */
135 for (j
= 0; j
< height
; j
++, y
++) {
136 GLfloat depthValues
[MAX_WIDTH
];
137 GLvoid
*dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
138 GL_DEPTH_COMPONENT
, type
, j
, 0);
139 _swrast_read_depth_span_float(ctx
, rb
, width
, x
, y
, depthValues
);
140 _mesa_pack_depth_span(ctx
, width
, dest
, type
, depthValues
, packing
);
147 * Read pixels for format=GL_STENCIL_INDEX.
150 read_stencil_pixels( GLcontext
*ctx
,
152 GLsizei width
, GLsizei height
,
153 GLenum type
, GLvoid
*pixels
,
154 const struct gl_pixelstore_attrib
*packing
)
156 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
157 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
161 /* no readbuffer - OK */
165 /* width should never be > MAX_WIDTH since we did clipping earlier */
166 ASSERT(width
<= MAX_WIDTH
);
168 /* process image row by row */
169 for (j
=0;j
<height
;j
++,y
++) {
171 GLstencil stencil
[MAX_WIDTH
];
173 _swrast_read_stencil_span(ctx
, rb
, width
, x
, y
, stencil
);
175 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
176 GL_STENCIL_INDEX
, type
, j
, 0);
178 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, stencil
, packing
);
185 * Optimized glReadPixels for particular pixel formats:
186 * GL_UNSIGNED_BYTE, GL_RGBA
187 * when pixel scaling, biasing and mapping are disabled.
190 read_fast_rgba_pixels( GLcontext
*ctx
,
192 GLsizei width
, GLsizei height
,
193 GLenum format
, GLenum type
,
195 const struct gl_pixelstore_attrib
*packing
)
197 struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
199 /* can't do scale, bias, mapping, etc */
200 if (ctx
->_ImageTransferState
)
203 /* can't do fancy pixel packing */
204 if (packing
->Alignment
!= 1 || packing
->SwapBytes
|| packing
->LsbFirst
)
210 GLint readWidth
= width
; /* actual width read */
211 GLint readHeight
= height
; /* actual height read */
212 GLint skipPixels
= packing
->SkipPixels
;
213 GLint skipRows
= packing
->SkipRows
;
216 if (packing
->RowLength
> 0)
217 rowLength
= packing
->RowLength
;
223 * The window region at (destX, destY) of size (readWidth, readHeight)
225 * We'll write pixel data to buffer pointed to by "pixels" but we'll
226 * skip "skipRows" rows and skip "skipPixels" pixels/row.
229 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
)
230 #elif CHAN_BITS == 16
231 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_SHORT
)
236 GLchan
*dest
= (GLchan
*) pixels
237 + (skipRows
* rowLength
+ skipPixels
) * 4;
240 if (packing
->Invert
) {
241 /* start at top and go down */
242 dest
+= (readHeight
- 1) * rowLength
* 4;
243 rowLength
= -rowLength
;
247 for (row
=0; row
<readHeight
; row
++) {
248 rb
->GetRow(ctx
, rb
, readWidth
, srcX
, srcY
, dest
);
249 dest
+= rowLength
* 4;
255 /* can't do this format/type combination */
264 * Read R, G, B, A, RGB, L, or LA pixels.
267 read_rgba_pixels( GLcontext
*ctx
,
269 GLsizei width
, GLsizei height
,
270 GLenum format
, GLenum type
, GLvoid
*pixels
,
271 const struct gl_pixelstore_attrib
*packing
)
273 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
274 struct gl_renderbuffer
*rb
= fb
->_ColorReadBuffer
;
277 /* No readbuffer is OK with GL_EXT_framebuffer_object */
281 /* Try optimized path first */
282 if (read_fast_rgba_pixels( ctx
, x
, y
, width
, height
,
283 format
, type
, pixels
, packing
)) {
287 /* width should never be > MAX_WIDTH since we did clipping earlier */
288 ASSERT(width
<= MAX_WIDTH
);
290 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
291 const GLuint transferOps
= ctx
->_ImageTransferState
;
292 GLfloat
*dest
, *src
, *tmpImage
, *convImage
;
295 tmpImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
297 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
300 convImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
302 _mesa_free(tmpImage
);
303 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glReadPixels");
307 /* read full RGBA, FLOAT image */
309 for (row
= 0; row
< height
; row
++, y
++) {
310 GLchan rgba
[MAX_WIDTH
][4];
311 if (fb
->Visual
.rgbMode
) {
312 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, rgba
);
315 GLuint index
[MAX_WIDTH
];
316 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
317 rb
->GetRow(ctx
, rb
, width
, x
, y
, index
);
318 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!=0 ) {
319 _mesa_map_ci(ctx
, width
, index
);
321 _mesa_map_ci_to_rgba_chan(ctx
, width
, index
, rgba
);
323 _mesa_pack_rgba_span_chan(ctx
, width
, (const GLchan (*)[4]) rgba
,
324 GL_RGBA
, GL_FLOAT
, dest
, &ctx
->DefaultPacking
,
325 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
);
330 if (ctx
->Pixel
.Convolution2DEnabled
) {
331 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
334 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
335 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
337 _mesa_free(tmpImage
);
339 /* finish transfer ops and pack the resulting image */
341 for (row
= 0; row
< height
; row
++) {
343 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
344 format
, type
, row
, 0);
345 _mesa_pack_rgba_span_float(ctx
, width
,
346 (const GLfloat (*)[4]) src
,
347 format
, type
, dest
, packing
,
348 transferOps
& IMAGE_POST_CONVOLUTION_BITS
);
355 for (row
= 0; row
< height
; row
++, y
++) {
356 GLchan rgba
[MAX_WIDTH
][4];
358 if (fb
->Visual
.rgbMode
) {
359 _swrast_read_rgba_span(ctx
, rb
, width
, x
, y
, rgba
);
362 GLuint index
[MAX_WIDTH
];
363 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
364 rb
->GetRow(ctx
, rb
, width
, x
, y
, index
);
365 if (ctx
->Pixel
.IndexShift
!= 0 || ctx
->Pixel
.IndexOffset
!= 0) {
366 _mesa_map_ci(ctx
, width
, index
);
368 _mesa_map_ci_to_rgba_chan(ctx
, width
, index
, rgba
);
370 dst
= _mesa_image_address2d(packing
, pixels
, width
, height
,
371 format
, type
, row
, 0);
372 if (fb
->Visual
.redBits
< CHAN_BITS
||
373 fb
->Visual
.greenBits
< CHAN_BITS
||
374 fb
->Visual
.blueBits
< CHAN_BITS
) {
375 /* Requantize the color values into floating point and go from
376 * there. This fixes conformance failures with 16-bit color
377 * buffers, for example.
379 GLfloat rgbaf
[MAX_WIDTH
][4];
380 _mesa_chan_to_float_span(ctx
, width
,
381 (CONST
GLchan (*)[4]) rgba
, rgbaf
);
382 _mesa_pack_rgba_span_float(ctx
, width
,
383 (CONST
GLfloat (*)[4]) rgbaf
,
384 format
, type
, dst
, packing
,
385 ctx
->_ImageTransferState
);
388 /* GLubytes are fine */
389 _mesa_pack_rgba_span_chan(ctx
, width
, (CONST
GLchan (*)[4]) rgba
,
390 format
, type
, dst
, packing
,
391 ctx
->_ImageTransferState
);
399 * Software fallback routine for ctx->Driver.ReadPixels().
400 * By time we get here, all error checking will have been done.
403 _swrast_ReadPixels( GLcontext
*ctx
,
404 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
405 GLenum format
, GLenum type
,
406 const struct gl_pixelstore_attrib
*packing
,
409 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
410 struct gl_pixelstore_attrib clippedPacking
;
412 if (swrast
->NewState
)
413 _swrast_validate_derived( ctx
);
415 /* Do all needed clipping here, so that we can forget about it later */
416 clippedPacking
= *packing
;
417 if (clippedPacking
.RowLength
== 0) {
418 clippedPacking
.RowLength
= width
;
420 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
,
421 &clippedPacking
.SkipPixels
,
422 &clippedPacking
.SkipRows
)) {
423 /* The ReadPixels region is totally outside the window bounds */
427 if (clippedPacking
.BufferObj
->Name
) {
430 if (!_mesa_validate_pbo_access(2, &clippedPacking
, width
, height
, 1,
431 format
, type
, pixels
)) {
432 _mesa_error(ctx
, GL_INVALID_OPERATION
,
433 "glReadPixels(invalid PBO access)");
436 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
438 clippedPacking
.BufferObj
);
440 /* buffer is already mapped - that's an error */
441 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(PBO is mapped)");
444 pixels
= ADD_POINTERS(buf
, pixels
);
447 RENDER_START(swrast
, ctx
);
451 read_index_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
454 case GL_STENCIL_INDEX
:
455 read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
458 case GL_DEPTH_COMPONENT
:
459 read_depth_pixels(ctx
, x
, y
, width
, height
, type
, pixels
,
468 case GL_LUMINANCE_ALPHA
:
473 read_rgba_pixels(ctx
, x
, y
, width
, height
,
474 format
, type
, pixels
, &clippedPacking
);
477 _mesa_problem(ctx
, "unexpected format in _swrast_ReadPixels");
478 /* don't return yet, clean-up */
481 RENDER_FINISH(swrast
, ctx
);
483 if (clippedPacking
.BufferObj
->Name
) {
484 /* done with PBO so unmap it now */
485 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
486 clippedPacking
.BufferObj
);