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/bufferobj.h"
38 #include "main/context.h"
39 #include "main/image.h"
41 #include "pipe/p_context.h"
42 #include "pipe/p_defines.h"
43 #include "pipe/p_inlines.h"
44 #include "util/p_tile.h"
45 #include "st_context.h"
46 #include "st_cb_readpixels.h"
47 #include "st_cb_fbo.h"
48 #include "st_format.h"
49 #include "st_public.h"
53 * Special case for reading stencil buffer.
54 * For color/depth we use get_tile(). For stencil, map the stencil buffer.
57 st_read_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
58 GLsizei width
, GLsizei height
, GLenum type
,
59 const struct gl_pixelstore_attrib
*packing
,
62 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
63 struct st_renderbuffer
*strb
= st_renderbuffer(fb
->_StencilBuffer
);
64 struct pipe_surface
*ps
= strb
->surface
;
68 /* map the stencil buffer */
69 stmap
= pipe_surface_map(ps
);
71 /* width should never be > MAX_WIDTH since we did clipping earlier */
72 ASSERT(width
<= MAX_WIDTH
);
74 /* process image row by row */
75 for (j
= 0; j
< height
; j
++, y
++) {
77 GLstencil values
[MAX_WIDTH
];
80 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
81 srcY
= ctx
->DrawBuffer
->Height
- y
- 1;
87 /* get stencil values */
89 case PIPE_FORMAT_U_S8
:
91 const ubyte
*src
= stmap
+ srcY
* ps
->pitch
+ x
;
92 memcpy(values
, src
, width
);
95 case PIPE_FORMAT_S8Z24_UNORM
:
97 const uint
*src
= (uint
*) stmap
+ srcY
* ps
->pitch
+ x
;
99 for (k
= 0; k
< width
; k
++) {
100 values
[k
] = src
[k
] >> 24;
104 case PIPE_FORMAT_Z24S8_UNORM
:
106 const uint
*src
= (uint
*) stmap
+ srcY
* ps
->pitch
+ x
;
108 for (k
= 0; k
< width
; k
++) {
109 values
[k
] = src
[k
] & 0xff;
118 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
119 GL_STENCIL_INDEX
, type
, j
, 0);
121 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, values
, packing
);
125 /* unmap the stencil buffer */
126 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 dest
= _mesa_map_readpix_pbo(ctx
, &clippedPacking
, dest
);
162 /* make sure rendering has completed */
163 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
);
165 if (format
== GL_STENCIL_INDEX
) {
166 st_read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pack
, dest
);
169 else if (format
== GL_DEPTH_COMPONENT
) {
170 strb
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
173 strb
= st_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
179 if (format
== GL_RGBA
&& type
== GL_FLOAT
) {
180 /* write tile(row) directly into user's buffer */
181 df
= (GLfloat
*) _mesa_image_address2d(&clippedPacking
, dest
, width
,
182 height
, format
, type
, 0, 0);
183 dfStride
= width
* 4;
186 /* write tile(row) into temp row buffer */
187 df
= (GLfloat
*) temp
;
191 /* determine bottom-to-top vs. top-to-bottom order */
192 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
193 y
= strb
->Base
.Height
- 1 - y
;
201 * Copy pixels from pipe_surface to user memory
204 /* dest of first pixel in client memory */
205 GLubyte
*dst
= _mesa_image_address2d(&clippedPacking
, dest
, width
,
206 height
, format
, type
, 0, 0);
207 /* dest row stride */
208 const GLint dstStride
= _mesa_image_row_stride(&clippedPacking
, width
,
211 if (strb
->surface
->format
== PIPE_FORMAT_S8Z24_UNORM
) {
212 if (format
== GL_DEPTH_COMPONENT
) {
213 for (i
= 0; i
< height
; i
++) {
214 GLuint ztemp
[MAX_WIDTH
], j
;
215 GLfloat zfloat
[MAX_WIDTH
];
216 const double scale
= 1.0 / ((1 << 24) - 1);
217 pipe_get_tile_raw(pipe
, strb
->surface
, x
, y
,
220 for (j
= 0; j
< width
; j
++) {
221 zfloat
[j
] = (float) (scale
* (ztemp
[j
] & 0xffffff));
223 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
224 zfloat
, &clippedPacking
);
229 /* untested, but simple: */
230 assert(format
== GL_DEPTH_STENCIL_EXT
);
231 for (i
= 0; i
< height
; i
++) {
232 pipe_get_tile_raw(pipe
, strb
->surface
, x
, y
, width
, 1, dst
, 0);
238 else if (strb
->surface
->format
== PIPE_FORMAT_Z16_UNORM
) {
239 for (i
= 0; i
< height
; i
++) {
240 GLshort ztemp
[MAX_WIDTH
], j
;
241 GLfloat zfloat
[MAX_WIDTH
];
242 const double scale
= 1.0 / 0xffff;
243 pipe_get_tile_raw(pipe
, strb
->surface
, x
, y
, width
, 1, ztemp
, 0);
245 for (j
= 0; j
< width
; j
++) {
246 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
248 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
249 zfloat
, &clippedPacking
);
253 else if (strb
->surface
->format
== PIPE_FORMAT_Z32_UNORM
) {
254 for (i
= 0; i
< height
; i
++) {
255 GLuint ztemp
[MAX_WIDTH
], j
;
256 GLfloat zfloat
[MAX_WIDTH
];
257 const double scale
= 1.0 / 0xffffffff;
258 pipe_get_tile_raw(pipe
, strb
->surface
, x
, y
, width
, 1, ztemp
, 0);
260 for (j
= 0; j
< width
; j
++) {
261 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
263 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
264 zfloat
, &clippedPacking
);
270 /* Do a row at a time to flip image data vertically */
271 for (i
= 0; i
< height
; i
++) {
272 pipe_get_tile_rgba(pipe
, strb
->surface
, x
, y
, width
, 1, df
);
276 _mesa_pack_rgba_span_float(ctx
, width
, temp
, format
, type
, dst
,
277 &clippedPacking
, transferOps
);
284 _mesa_unmap_readpix_pbo(ctx
, &clippedPacking
);
288 void st_init_readpixels_functions(struct dd_function_table
*functions
)
290 functions
->ReadPixels
= st_readpixels
;