Merge commit 'origin/gallium-0.1' into gallium-tex-surfaces
[mesa.git] / src / mesa / state_tracker / st_cb_readpixels.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 /**
30 * glReadPixels interface to pipe
31 *
32 * \author Brian Paul
33 */
34
35
36 #include "main/imports.h"
37 #include "main/bufferobj.h"
38 #include "main/context.h"
39 #include "main/image.h"
40
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"
51
52
53 /**
54 * Special case for reading stencil buffer.
55 * For color/depth we use get_tile(). For stencil, map the stencil buffer.
56 */
57 void
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,
61 GLvoid *pixels)
62 {
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 = strb->surface;
67 ubyte *stmap;
68 GLint j;
69
70 /* map the stencil buffer */
71 stmap = screen->surface_map(screen, ps, PIPE_BUFFER_USAGE_CPU_READ);
72
73 /* width should never be > MAX_WIDTH since we did clipping earlier */
74 ASSERT(width <= MAX_WIDTH);
75
76 /* process image row by row */
77 for (j = 0; j < height; j++, y++) {
78 GLvoid *dest;
79 GLstencil values[MAX_WIDTH];
80 GLint srcY;
81
82 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
83 srcY = ctx->DrawBuffer->Height - y - 1;
84 }
85 else {
86 srcY = y;
87 }
88
89 /* get stencil values */
90 switch (ps->format) {
91 case PIPE_FORMAT_U_S8:
92 {
93 const ubyte *src = stmap + srcY * ps->pitch + x;
94 memcpy(values, src, width);
95 }
96 break;
97 case PIPE_FORMAT_S8Z24_UNORM:
98 {
99 const uint *src = (uint *) stmap + srcY * ps->pitch + x;
100 GLint k;
101 for (k = 0; k < width; k++) {
102 values[k] = src[k] >> 24;
103 }
104 }
105 break;
106 case PIPE_FORMAT_Z24S8_UNORM:
107 {
108 const uint *src = (uint *) stmap + srcY * ps->pitch + x;
109 GLint k;
110 for (k = 0; k < width; k++) {
111 values[k] = src[k] & 0xff;
112 }
113 }
114 break;
115 default:
116 assert(0);
117 }
118
119 /* store */
120 dest = _mesa_image_address2d(packing, pixels, width, height,
121 GL_STENCIL_INDEX, type, j, 0);
122
123 _mesa_pack_stencil_span(ctx, width, type, dest, values, packing);
124 }
125
126
127 /* unmap the stencil buffer */
128 screen->surface_unmap(screen, ps);
129 }
130
131
132 /**
133 * Return renderbuffer to use for reading color pixels for glRead/CopyPixel
134 * commands.
135 * Special care is needed for the front buffer.
136 */
137 struct st_renderbuffer *
138 st_get_color_read_renderbuffer(GLcontext *ctx)
139 {
140 struct gl_framebuffer *fb = ctx->ReadBuffer;
141 struct st_renderbuffer *strb =
142 st_renderbuffer(fb->_ColorReadBuffer);
143 struct st_renderbuffer *front =
144 st_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
145
146 if (strb == front
147 && ctx->st->frontbuffer_status == FRONT_STATUS_COPY_OF_BACK) {
148 /* reading from front color buffer, which is a logical copy of the
149 * back color buffer.
150 */
151 struct st_renderbuffer *back =
152 st_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
153 strb = back;
154 }
155
156 return strb;
157 }
158
159
160 /**
161 * Do glReadPixels by getting rows from the framebuffer surface with
162 * get_tile(). Convert to requested format/type with Mesa image routines.
163 * Image transfer ops are done in software too.
164 */
165 static void
166 st_readpixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
167 GLenum format, GLenum type,
168 const struct gl_pixelstore_attrib *pack,
169 GLvoid *dest)
170 {
171 struct pipe_context *pipe = ctx->st->pipe;
172 GLfloat temp[MAX_WIDTH][4];
173 const GLbitfield transferOps = ctx->_ImageTransferState;
174 GLint i, yStep, dfStride;
175 GLfloat *df;
176 struct st_renderbuffer *strb;
177 struct gl_pixelstore_attrib clippedPacking = *pack;
178
179 /* XXX convolution not done yet */
180 assert((transferOps & IMAGE_CONVOLUTION_BIT) == 0);
181
182 /* Do all needed clipping here, so that we can forget about it later */
183 if (!_mesa_clip_readpixels(ctx, &x, &y, &width, &height, &clippedPacking)) {
184 /* The ReadPixels surface is totally outside the window bounds */
185 return;
186 }
187
188 dest = _mesa_map_readpix_pbo(ctx, &clippedPacking, dest);
189 if (!dest)
190 return;
191
192 st_flush_bitmap_cache(ctx->st);
193
194 /* make sure rendering has completed */
195 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
196
197 if (format == GL_STENCIL_INDEX) {
198 st_read_stencil_pixels(ctx, x, y, width, height, type, pack, dest);
199 return;
200 }
201 else if (format == GL_DEPTH_COMPONENT) {
202 strb = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
203 }
204 else {
205 /* Read color buffer */
206 strb = st_get_color_read_renderbuffer(ctx);
207 }
208
209 if (!strb)
210 return;
211
212 if (format == GL_RGBA && type == GL_FLOAT) {
213 /* write tile(row) directly into user's buffer */
214 df = (GLfloat *) _mesa_image_address2d(&clippedPacking, dest, width,
215 height, format, type, 0, 0);
216 dfStride = width * 4;
217 }
218 else {
219 /* write tile(row) into temp row buffer */
220 df = (GLfloat *) temp;
221 dfStride = 0;
222 }
223
224 /* determine bottom-to-top vs. top-to-bottom order */
225 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
226 y = strb->Base.Height - 1 - y;
227 yStep = -1;
228 }
229 else {
230 yStep = 1;
231 }
232
233 /*
234 * Copy pixels from pipe_surface to user memory
235 */
236 {
237 /* dest of first pixel in client memory */
238 GLubyte *dst = _mesa_image_address2d(&clippedPacking, dest, width,
239 height, format, type, 0, 0);
240 /* dest row stride */
241 const GLint dstStride = _mesa_image_row_stride(&clippedPacking, width,
242 format, type);
243
244 if (strb->surface->format == PIPE_FORMAT_S8Z24_UNORM ||
245 strb->surface->format == PIPE_FORMAT_X8Z24_UNORM) {
246 if (format == GL_DEPTH_COMPONENT) {
247 for (i = 0; i < height; i++) {
248 GLuint ztemp[MAX_WIDTH], j;
249 GLfloat zfloat[MAX_WIDTH];
250 const double scale = 1.0 / ((1 << 24) - 1);
251 pipe_get_tile_raw(pipe, strb->surface, x, y,
252 width, 1, ztemp, 0);
253 y += yStep;
254 for (j = 0; j < width; j++) {
255 zfloat[j] = (float) (scale * (ztemp[j] & 0xffffff));
256 }
257 _mesa_pack_depth_span(ctx, width, dst, type,
258 zfloat, &clippedPacking);
259 dst += dstStride;
260 }
261 }
262 else {
263 /* untested, but simple: */
264 assert(format == GL_DEPTH_STENCIL_EXT);
265 for (i = 0; i < height; i++) {
266 pipe_get_tile_raw(pipe, strb->surface, x, y, width, 1, dst, 0);
267 y += yStep;
268 dst += dstStride;
269 }
270 }
271 }
272 else if (strb->surface->format == PIPE_FORMAT_Z16_UNORM) {
273 for (i = 0; i < height; i++) {
274 GLushort ztemp[MAX_WIDTH], j;
275 GLfloat zfloat[MAX_WIDTH];
276 const double scale = 1.0 / 0xffff;
277 pipe_get_tile_raw(pipe, strb->surface, x, y, width, 1, ztemp, 0);
278 y += yStep;
279 for (j = 0; j < width; j++) {
280 zfloat[j] = (float) (scale * ztemp[j]);
281 }
282 _mesa_pack_depth_span(ctx, width, dst, type,
283 zfloat, &clippedPacking);
284 dst += dstStride;
285 }
286 }
287 else if (strb->surface->format == PIPE_FORMAT_Z32_UNORM) {
288 for (i = 0; i < height; i++) {
289 GLuint ztemp[MAX_WIDTH], j;
290 GLfloat zfloat[MAX_WIDTH];
291 const double scale = 1.0 / 0xffffffff;
292 pipe_get_tile_raw(pipe, strb->surface, x, y, width, 1, ztemp, 0);
293 y += yStep;
294 for (j = 0; j < width; j++) {
295 zfloat[j] = (float) (scale * ztemp[j]);
296 }
297 _mesa_pack_depth_span(ctx, width, dst, type,
298 zfloat, &clippedPacking);
299 dst += dstStride;
300 }
301 }
302 else {
303 /* RGBA format */
304 /* Do a row at a time to flip image data vertically */
305 for (i = 0; i < height; i++) {
306 pipe_get_tile_rgba(pipe, strb->surface, x, y, width, 1, df);
307 y += yStep;
308 df += dfStride;
309 if (!dfStride) {
310 _mesa_pack_rgba_span_float(ctx, width, temp, format, type, dst,
311 &clippedPacking, transferOps);
312 dst += dstStride;
313 }
314 }
315 }
316 }
317
318 _mesa_unmap_readpix_pbo(ctx, &clippedPacking);
319 }
320
321
322 void st_init_readpixels_functions(struct dd_function_table *functions)
323 {
324 functions->ReadPixels = st_readpixels;
325 }