1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * glReadPixels interface to pipe
36 #include "main/imports.h"
37 #include "main/context.h"
38 #include "main/image.h"
40 #include "pipe/p_context.h"
41 #include "pipe/p_defines.h"
42 #include "pipe/p_inlines.h"
43 #include "util/p_tile.h"
44 #include "st_context.h"
45 #include "st_cb_readpixels.h"
46 #include "st_cb_fbo.h"
47 #include "st_format.h"
48 #include "st_public.h"
52 * Special case for reading stencil buffer.
53 * For color/depth we use get_tile(). For stencil, map the stencil buffer.
56 st_read_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
57 GLsizei width
, GLsizei height
, GLenum type
,
58 const struct gl_pixelstore_attrib
*packing
,
61 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
62 struct st_renderbuffer
*strb
= st_renderbuffer(fb
->_StencilBuffer
);
63 struct pipe_surface
*ps
= strb
->surface
;
67 /* map the stencil buffer */
68 stmap
= pipe_surface_map(ps
);
70 /* width should never be > MAX_WIDTH since we did clipping earlier */
71 ASSERT(width
<= MAX_WIDTH
);
73 /* process image row by row */
74 for (j
= 0; j
< height
; j
++, y
++) {
76 GLstencil values
[MAX_WIDTH
];
79 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
80 srcY
= ctx
->DrawBuffer
->Height
- y
- 1;
86 /* get stencil values */
88 case PIPE_FORMAT_U_S8
:
90 const ubyte
*src
= stmap
+ srcY
* ps
->pitch
+ x
;
91 memcpy(values
, src
, width
);
94 case PIPE_FORMAT_S8Z24_UNORM
:
96 const uint
*src
= (uint
*) stmap
+ srcY
* ps
->pitch
+ x
;
98 for (k
= 0; k
< width
; k
++) {
99 values
[k
] = src
[k
] >> 24;
103 case PIPE_FORMAT_Z24S8_UNORM
:
105 const uint
*src
= (uint
*) stmap
+ srcY
* ps
->pitch
+ x
;
107 for (k
= 0; k
< width
; k
++) {
108 values
[k
] = src
[k
] & 0xff;
117 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
118 GL_STENCIL_INDEX
, type
, j
, 0);
120 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, values
, packing
);
124 /* unmap the stencil buffer */
125 pipe_surface_unmap(ps
);
131 * Do glReadPixels by getting rows from the framebuffer surface with
132 * get_tile(). Convert to requested format/type with Mesa image routines.
133 * Image transfer ops are done in software too.
136 st_readpixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
137 GLenum format
, GLenum type
,
138 const struct gl_pixelstore_attrib
*pack
,
141 struct pipe_context
*pipe
= ctx
->st
->pipe
;
142 GLfloat temp
[MAX_WIDTH
][4];
143 const GLbitfield transferOps
= ctx
->_ImageTransferState
;
144 GLint i
, yStep
, dfStride
;
146 struct st_renderbuffer
*strb
;
147 struct gl_pixelstore_attrib clippedPacking
= *pack
;
149 /* XXX convolution not done yet */
150 assert((transferOps
& IMAGE_CONVOLUTION_BIT
) == 0);
152 /* Do all needed clipping here, so that we can forget about it later */
153 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
154 /* The ReadPixels surface is totally outside the window bounds */
158 /* make sure rendering has completed */
159 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
);
161 if (pack
->BufferObj
&& pack
->BufferObj
->Name
) {
162 /* reading into a PBO */
166 /* reading into user memory/buffer */
170 if (format
== GL_STENCIL_INDEX
) {
171 st_read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pack
, dest
);
174 else if (format
== GL_DEPTH_COMPONENT
) {
175 strb
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
178 strb
= st_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
184 if (format
== GL_RGBA
&& type
== GL_FLOAT
) {
185 /* write tile(row) directly into user's buffer */
186 df
= (GLfloat
*) _mesa_image_address2d(&clippedPacking
, dest
, width
,
187 height
, format
, type
, 0, 0);
188 dfStride
= width
* 4;
191 /* write tile(row) into temp row buffer */
192 df
= (GLfloat
*) temp
;
196 /* determine bottom-to-top vs. top-to-bottom order */
197 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
198 y
= strb
->Base
.Height
- 1 - y
;
206 * Copy pixels from pipe_surface to user memory
209 /* dest of first pixel in client memory */
210 GLubyte
*dst
= _mesa_image_address2d(&clippedPacking
, dest
, width
,
211 height
, format
, type
, 0, 0);
212 /* dest row stride */
213 const GLint dstStride
= _mesa_image_row_stride(&clippedPacking
, width
,
216 if (strb
->surface
->format
== PIPE_FORMAT_S8Z24_UNORM
) {
217 if (format
== GL_DEPTH_COMPONENT
) {
218 for (i
= 0; i
< height
; i
++) {
219 GLuint ztemp
[MAX_WIDTH
], j
;
220 GLfloat zfloat
[MAX_WIDTH
];
221 const double scale
= 1.0 / ((1 << 24) - 1);
222 pipe_get_tile_raw(pipe
, strb
->surface
, x
, y
,
225 for (j
= 0; j
< width
; j
++) {
226 zfloat
[j
] = (float) (scale
* (ztemp
[j
] & 0xffffff));
228 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
229 zfloat
, &clippedPacking
);
234 /* untested, but simple: */
235 assert(format
== GL_DEPTH_STENCIL_EXT
);
236 for (i
= 0; i
< height
; i
++) {
237 pipe_get_tile_raw(pipe
, strb
->surface
, x
, y
, width
, 1, dst
, 0);
243 else if (strb
->surface
->format
== PIPE_FORMAT_Z16_UNORM
) {
244 for (i
= 0; i
< height
; i
++) {
245 GLshort ztemp
[MAX_WIDTH
], j
;
246 GLfloat zfloat
[MAX_WIDTH
];
247 const double scale
= 1.0 / 0xffff;
248 pipe_get_tile_raw(pipe
, strb
->surface
, x
, y
, width
, 1, ztemp
, 0);
250 for (j
= 0; j
< width
; j
++) {
251 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
253 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
254 zfloat
, &clippedPacking
);
258 else if (strb
->surface
->format
== PIPE_FORMAT_Z32_UNORM
) {
259 for (i
= 0; i
< height
; i
++) {
260 GLuint ztemp
[MAX_WIDTH
], j
;
261 GLfloat zfloat
[MAX_WIDTH
];
262 const double scale
= 1.0 / 0xffffffff;
263 pipe_get_tile_raw(pipe
, strb
->surface
, x
, y
, width
, 1, ztemp
, 0);
265 for (j
= 0; j
< width
; j
++) {
266 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
268 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
269 zfloat
, &clippedPacking
);
275 /* Do a row at a time to flip image data vertically */
276 for (i
= 0; i
< height
; i
++) {
277 pipe_get_tile_rgba(pipe
, strb
->surface
, x
, y
, width
, 1, df
);
281 _mesa_pack_rgba_span_float(ctx
, width
, temp
, format
, type
, dst
,
282 &clippedPacking
, transferOps
);
291 void st_init_readpixels_functions(struct dd_function_table
*functions
)
293 functions
->ReadPixels
= st_readpixels
;