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 "st_context.h"
43 #include "st_cb_readpixels.h"
44 #include "st_cb_fbo.h"
45 #include "st_format.h"
46 #include "st_public.h"
50 * Special case for reading stencil buffer.
51 * For color/depth we use get_tile(). For stencil, map the stencil buffer.
54 read_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
55 GLsizei width
, GLsizei height
, GLenum type
,
56 const struct gl_pixelstore_attrib
*packing
,
59 struct st_context
*st
= ctx
->st
;
60 struct pipe_context
*pipe
= st
->pipe
;
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
->region_map(pipe
, ps
->region
);
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
+ y
* ps
->region
->pitch
+ x
;
91 memcpy(values
, src
, width
);
94 case PIPE_FORMAT_S8_Z24
:
96 const uint
*src
= (const uint
*) stmap
+ y
* ps
->region
->pitch
+ x
;
98 for (k
= 0; k
< width
; k
++) {
99 values
[k
] = src
[k
] >> 24;
108 dest
= _mesa_image_address2d(packing
, pixels
, width
, height
,
109 GL_STENCIL_INDEX
, type
, j
, 0);
111 _mesa_pack_stencil_span(ctx
, width
, type
, dest
, values
, packing
);
115 /* unmap the stencil buffer */
116 pipe
->region_unmap(pipe
, ps
->region
);
122 * Do glReadPixels by getting rows from the framebuffer surface with
123 * get_tile(). Convert to requested format/type with Mesa image routines.
124 * Image transfer ops are done in software too.
127 st_readpixels(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
,
128 GLenum format
, GLenum type
,
129 const struct gl_pixelstore_attrib
*pack
,
132 struct pipe_context
*pipe
= ctx
->st
->pipe
;
133 GLfloat temp
[MAX_WIDTH
][4];
134 const GLbitfield transferOps
= ctx
->_ImageTransferState
;
135 GLint i
, yStep
, dfStride
;
137 struct st_renderbuffer
*strb
;
138 struct gl_pixelstore_attrib clippedPacking
= *pack
;
140 /* XXX convolution not done yet */
141 assert((transferOps
& IMAGE_CONVOLUTION_BIT
) == 0);
143 /* Do all needed clipping here, so that we can forget about it later */
144 if (!_mesa_clip_readpixels(ctx
, &x
, &y
, &width
, &height
, &clippedPacking
)) {
145 /* The ReadPixels region is totally outside the window bounds */
149 /* make sure rendering has completed */
150 pipe
->flush(pipe
, 0x0);
152 if (pack
->BufferObj
&& pack
->BufferObj
->Name
) {
153 /* reading into a PBO */
157 /* reading into user memory/buffer */
161 if (format
== GL_STENCIL_INDEX
) {
162 read_stencil_pixels(ctx
, x
, y
, width
, height
, type
, pack
, dest
);
165 else if (format
== GL_DEPTH_COMPONENT
) {
166 strb
= st_renderbuffer(ctx
->ReadBuffer
->_DepthBuffer
);
169 strb
= st_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
174 pipe
->region_map(pipe
, strb
->surface
->region
);
176 if (format
== GL_RGBA
&& type
== GL_FLOAT
) {
177 /* write tile(row) directly into user's buffer */
178 df
= (GLfloat
*) _mesa_image_address2d(&clippedPacking
, dest
, width
,
179 height
, format
, type
, 0, 0);
180 dfStride
= width
* 4;
183 else if (format
== GL_DEPTH_COMPONENT
&& type
== GL_FLOAT
) {
184 /* write tile(row) directly into user's buffer */
185 df
= (GLfloat
*) _mesa_image_address2d(&clippedPacking
, dest
, width
,
186 height
, format
, type
, 0, 0);
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
;
205 /* Do a row at a time to flip image data vertically */
206 for (i
= 0; i
< height
; i
++) {
207 strb
->surface
->get_tile(strb
->surface
, x
, y
, width
, 1, df
);
211 /* convert GLfloat to user's format/type */
212 GLvoid
*dst
= _mesa_image_address2d(&clippedPacking
, dest
, width
,
213 height
, format
, type
, i
, 0);
214 if (format
== GL_DEPTH_COMPONENT
) {
215 _mesa_pack_depth_span(ctx
, width
, dst
, type
,
216 (GLfloat
*) temp
, &clippedPacking
);
219 _mesa_pack_rgba_span_float(ctx
, width
, temp
, format
, type
, dst
,
220 &clippedPacking
, transferOps
);
225 pipe
->region_unmap(pipe
, strb
->surface
->region
);
229 void st_init_readpixels_functions(struct dd_function_table
*functions
)
231 functions
->ReadPixels
= st_readpixels
;