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 "st_context.h"
44 #include "st_cb_readpixels.h"
45 #include "st_cb_fbo.h"
46 #include "st_format.h"
47 #include "st_public.h"
51 * Special case for reading stencil buffer.
52 * For color/depth we use get_tile(). For stencil, map the stencil buffer.
55 st_read_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
56 GLsizei width
, GLsizei height
, GLenum type
,
57 const struct gl_pixelstore_attrib
*packing
,
60 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
61 struct st_renderbuffer
*strb
= st_renderbuffer(fb
->_StencilBuffer
);
62 struct pipe_surface
*ps
= strb
->surface
;
66 /* map the stencil buffer */
67 stmap
= pipe_surface_map(ps
);
69 /* width should never be > MAX_WIDTH since we did clipping earlier */
70 ASSERT(width
<= MAX_WIDTH
);
72 /* process image row by row */
73 for (j
= 0; j
< height
; j
++, y
++) {
75 GLstencil values
[MAX_WIDTH
];
78 if (st_fb_orientation(ctx
->DrawBuffer
) == Y_0_TOP
) {
79 srcY
= ctx
->DrawBuffer
->Height
- y
- 1;
85 /* get stencil values */
87 case PIPE_FORMAT_U_S8
:
89 const ubyte
*src
= stmap
+ srcY
* ps
->pitch
+ x
;
90 memcpy(values
, src
, width
);
93 case PIPE_FORMAT_S8Z24_UNORM
:
95 const uint
*src
= (uint
*) stmap
+ srcY
* ps
->pitch
+ x
;
97 for (k
= 0; k
< width
; k
++) {
98 values
[k
] = src
[k
] >> 24;
102 case PIPE_FORMAT_Z24S8_UNORM
:
104 const uint
*src
= (uint
*) stmap
+ srcY
* ps
->pitch
+ x
;
106 for (k
= 0; k
< width
; k
++) {
107 values
[k
] = src
[k
] & 0xff;
116 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
117 GL_STENCIL_INDEX
, type
, j
, 0);
119 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, values
, packing
);
123 /* unmap the stencil buffer */
124 pipe_surface_unmap(ps
);
130 * Do glReadPixels by getting rows from the framebuffer surface with
131 * get_tile(). Convert to requested format/type with Mesa image routines.
132 * Image transfer ops are done in software too.
135 st_readpixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
136 GLenum format
, GLenum type
,
137 const struct gl_pixelstore_attrib
*pack
,
140 struct pipe_context
*pipe
= ctx
->st
->pipe
;
141 GLfloat temp
[MAX_WIDTH
][4];
142 const GLbitfield transferOps
= ctx
->_ImageTransferState
;
143 GLint i
, yStep
, dfStride
;
145 struct st_renderbuffer
*strb
;
146 struct gl_pixelstore_attrib clippedPacking
= *pack
;
148 /* XXX convolution not done yet */
149 assert((transferOps
& IMAGE_CONVOLUTION_BIT
) == 0);
151 /* Do all needed clipping here, so that we can forget about it later */
152 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
153 /* The ReadPixels surface is totally outside the window bounds */
157 /* make sure rendering has completed */
158 pipe
->flush(pipe
, 0x0);
160 if (pack
->BufferObj
&& pack
->BufferObj
->Name
) {
161 /* reading into a PBO */
165 /* reading into user memory/buffer */
169 if (format
== GL_STENCIL_INDEX
) {
170 st_read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pack
, dest
);
173 else if (format
== GL_DEPTH_COMPONENT
) {
174 strb
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
177 strb
= st_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
182 pipe_surface_map(strb
->surface
);
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 else if (format
== GL_DEPTH_COMPONENT
&& type
== GL_FLOAT
) {
192 /* write tile(row) directly into user's buffer */
193 df
= (GLfloat
*) _mesa_image_address2d(&clippedPacking
, dest
, width
,
194 height
, format
, type
, 0, 0);
199 /* write tile(row) into temp row buffer */
200 df
= (GLfloat
*) temp
;
204 /* determine bottom-to-top vs. top-to-bottom order */
205 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
206 y
= strb
->Base
.Height
- 1 - y
;
213 /* Do a row at a time to flip image data vertically */
214 for (i
= 0; i
< height
; i
++) {
215 pipe
->get_tile_rgba(pipe
, strb
->surface
, x
, y
, width
, 1, df
);
219 /* convert GLfloat to user's format/type */
220 GLvoid
*dst
= _mesa_image_address2d(&clippedPacking
, dest
, width
,
221 height
, format
, type
, i
, 0);
222 if (format
== GL_DEPTH_COMPONENT
) {
223 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
224 (GLfloat
*) temp
, &clippedPacking
);
227 _mesa_pack_rgba_span_float(ctx
, width
, temp
, format
, type
, dst
,
228 &clippedPacking
, transferOps
);
233 pipe_surface_unmap(strb
->surface
);
237 void st_init_readpixels_functions(struct dd_function_table
*functions
)
239 functions
->ReadPixels
= st_readpixels
;