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_bitmap.h"
47 #include "st_cb_readpixels.h"
48 #include "st_cb_fbo.h"
49 #include "st_format.h"
50 #include "st_public.h"
54 * Special case for reading stencil buffer.
55 * For color/depth we use get_tile(). For stencil, map the stencil buffer.
58 st_read_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
59 GLsizei width
, GLsizei height
, GLenum type
,
60 const struct gl_pixelstore_attrib
*packing
,
63 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
64 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
65 struct st_renderbuffer
*strb
= st_renderbuffer(fb
->_StencilBuffer
);
66 struct pipe_surface
*ps
;
70 /* Create a CPU-READ surface/view into the renderbuffer's texture */
71 ps
= screen
->get_tex_surface(screen
, strb
->texture
, 0, 0, 0,
72 PIPE_BUFFER_USAGE_CPU_READ
);
74 /* map the stencil buffer */
75 stmap
= screen
->surface_map(screen
, ps
, PIPE_BUFFER_USAGE_CPU_READ
);
77 /* width should never be > MAX_WIDTH since we did clipping earlier */
78 ASSERT(width
<= MAX_WIDTH
);
80 /* process image row by row */
81 for (j
= 0; j
< height
; j
++, y
++) {
83 GLstencil values
[MAX_WIDTH
];
86 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
87 srcY
= ctx
->DrawBuffer
->Height
- y
- 1;
93 /* get stencil values */
95 case PIPE_FORMAT_U_S8
:
97 const ubyte
*src
= stmap
+ srcY
* ps
->pitch
+ x
;
98 memcpy(values
, src
, width
);
101 case PIPE_FORMAT_S8Z24_UNORM
:
103 const uint
*src
= (uint
*) stmap
+ srcY
* ps
->pitch
+ x
;
105 for (k
= 0; k
< width
; k
++) {
106 values
[k
] = src
[k
] >> 24;
110 case PIPE_FORMAT_Z24S8_UNORM
:
112 const uint
*src
= (uint
*) stmap
+ srcY
* ps
->pitch
+ x
;
114 for (k
= 0; k
< width
; k
++) {
115 values
[k
] = src
[k
] & 0xff;
124 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
125 GL_STENCIL_INDEX
, type
, j
, 0);
127 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, values
, packing
);
131 /* unmap the stencil buffer */
132 screen
->surface_unmap(screen
, ps
);
133 pipe_surface_reference(&ps
, NULL
);
138 * Return renderbuffer to use for reading color pixels for glRead/CopyPixel
140 * Special care is needed for the front buffer.
142 struct st_renderbuffer
*
143 st_get_color_read_renderbuffer(GLcontext
*ctx
)
145 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
146 struct st_renderbuffer
*strb
=
147 st_renderbuffer(fb
->_ColorReadBuffer
);
148 struct st_renderbuffer
*front
=
149 st_renderbuffer(fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
152 && ctx
->st
->frontbuffer_status
== FRONT_STATUS_COPY_OF_BACK
) {
153 /* reading from front color buffer, which is a logical copy of the
156 struct st_renderbuffer
*back
=
157 st_renderbuffer(fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
166 * Do glReadPixels by getting rows from the framebuffer surface with
167 * get_tile(). Convert to requested format/type with Mesa image routines.
168 * Image transfer ops are done in software too.
171 st_readpixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
172 GLenum format
, GLenum type
,
173 const struct gl_pixelstore_attrib
*pack
,
176 struct pipe_context
*pipe
= ctx
->st
->pipe
;
177 struct pipe_screen
*screen
= pipe
->screen
;
178 GLfloat temp
[MAX_WIDTH
][4];
179 const GLbitfield transferOps
= ctx
->_ImageTransferState
;
180 GLint i
, yStep
, dfStride
;
182 struct st_renderbuffer
*strb
;
183 struct gl_pixelstore_attrib clippedPacking
= *pack
;
184 struct pipe_surface
*surf
;
186 assert(ctx
->ReadBuffer
->Width
> 0);
188 /* XXX convolution not done yet */
189 assert((transferOps
& IMAGE_CONVOLUTION_BIT
) == 0);
191 /* Do all needed clipping here, so that we can forget about it later */
192 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
193 /* The ReadPixels surface is totally outside the window bounds */
197 dest
= _mesa_map_readpix_pbo(ctx
, &clippedPacking
, dest
);
201 st_flush_bitmap_cache(ctx
->st
);
203 /* make sure rendering has completed */
204 pipe
->flush(pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
206 if (format
== GL_STENCIL_INDEX
) {
207 st_read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pack
, dest
);
210 else if (format
== GL_DEPTH_COMPONENT
) {
211 strb
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
214 /* Read color buffer */
215 strb
= st_get_color_read_renderbuffer(ctx
);
221 if (format
== GL_RGBA
&& type
== GL_FLOAT
) {
222 /* write tile(row) directly into user's buffer */
223 df
= (GLfloat
*) _mesa_image_address2d(&clippedPacking
, dest
, width
,
224 height
, format
, type
, 0, 0);
225 dfStride
= width
* 4;
228 /* write tile(row) into temp row buffer */
229 df
= (GLfloat
*) temp
;
233 /* determine bottom-to-top vs. top-to-bottom order */
234 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
235 y
= strb
->Base
.Height
- 1 - y
;
242 /* Create a CPU-READ surface/view into the renderbuffer's texture */
243 surf
= screen
->get_tex_surface(screen
, strb
->texture
, 0, 0, 0,
244 PIPE_BUFFER_USAGE_CPU_READ
);
247 * Copy pixels from pipe_surface to user memory
250 /* dest of first pixel in client memory */
251 GLubyte
*dst
= _mesa_image_address2d(&clippedPacking
, dest
, width
,
252 height
, format
, type
, 0, 0);
253 /* dest row stride */
254 const GLint dstStride
= _mesa_image_row_stride(&clippedPacking
, width
,
257 if (surf
->format
== PIPE_FORMAT_S8Z24_UNORM
||
258 surf
->format
== PIPE_FORMAT_X8Z24_UNORM
) {
259 if (format
== GL_DEPTH_COMPONENT
) {
260 for (i
= 0; i
< height
; i
++) {
261 GLuint ztemp
[MAX_WIDTH
], j
;
262 GLfloat zfloat
[MAX_WIDTH
];
263 const double scale
= 1.0 / ((1 << 24) - 1);
264 pipe_get_tile_raw(pipe
, surf
, x
, y
, width
, 1, ztemp
, 0);
266 for (j
= 0; j
< width
; j
++) {
267 zfloat
[j
] = (float) (scale
* (ztemp
[j
] & 0xffffff));
269 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
270 zfloat
, &clippedPacking
);
275 /* untested, but simple: */
276 assert(format
== GL_DEPTH_STENCIL_EXT
);
277 for (i
= 0; i
< height
; i
++) {
278 pipe_get_tile_raw(pipe
, surf
, x
, y
, width
, 1, dst
, 0);
284 else if (surf
->format
== PIPE_FORMAT_Z16_UNORM
) {
285 for (i
= 0; i
< height
; i
++) {
286 GLushort ztemp
[MAX_WIDTH
], j
;
287 GLfloat zfloat
[MAX_WIDTH
];
288 const double scale
= 1.0 / 0xffff;
289 pipe_get_tile_raw(pipe
, surf
, x
, y
, width
, 1, ztemp
, 0);
291 for (j
= 0; j
< width
; j
++) {
292 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
294 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
295 zfloat
, &clippedPacking
);
299 else if (surf
->format
== PIPE_FORMAT_Z32_UNORM
) {
300 for (i
= 0; i
< height
; i
++) {
301 GLuint ztemp
[MAX_WIDTH
], j
;
302 GLfloat zfloat
[MAX_WIDTH
];
303 const double scale
= 1.0 / 0xffffffff;
304 pipe_get_tile_raw(pipe
, surf
, x
, y
, width
, 1, ztemp
, 0);
306 for (j
= 0; j
< width
; j
++) {
307 zfloat
[j
] = (float) (scale
* ztemp
[j
]);
309 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
310 zfloat
, &clippedPacking
);
316 /* Do a row at a time to flip image data vertically */
317 for (i
= 0; i
< height
; i
++) {
318 pipe_get_tile_rgba(pipe
, surf
, x
, y
, width
, 1, df
);
322 _mesa_pack_rgba_span_float(ctx
, width
, temp
, format
, type
, dst
,
323 &clippedPacking
, transferOps
);
330 pipe_surface_reference(&surf
, NULL
);
332 _mesa_unmap_readpix_pbo(ctx
, &clippedPacking
);
336 void st_init_readpixels_functions(struct dd_function_table
*functions
)
338 functions
->ReadPixels
= st_readpixels
;