gallium: free bitmap fragment shaders, misc clean-up
[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_readpixels.h"
47 #include "st_cb_fbo.h"
48 #include "st_format.h"
49 #include "st_public.h"
50
51
52 /**
53 * Special case for reading stencil buffer.
54 * For color/depth we use get_tile(). For stencil, map the stencil buffer.
55 */
56 void
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,
60 GLvoid *pixels)
61 {
62 struct gl_framebuffer *fb = ctx->ReadBuffer;
63 struct st_renderbuffer *strb = st_renderbuffer(fb->_StencilBuffer);
64 struct pipe_surface *ps = strb->surface;
65 ubyte *stmap;
66 GLint j;
67
68 /* map the stencil buffer */
69 stmap = pipe_surface_map(ps);
70
71 /* width should never be > MAX_WIDTH since we did clipping earlier */
72 ASSERT(width <= MAX_WIDTH);
73
74 /* process image row by row */
75 for (j = 0; j < height; j++, y++) {
76 GLvoid *dest;
77 GLstencil values[MAX_WIDTH];
78 GLint srcY;
79
80 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
81 srcY = ctx->DrawBuffer->Height - y - 1;
82 }
83 else {
84 srcY = y;
85 }
86
87 /* get stencil values */
88 switch (ps->format) {
89 case PIPE_FORMAT_U_S8:
90 {
91 const ubyte *src = stmap + srcY * ps->pitch + x;
92 memcpy(values, src, width);
93 }
94 break;
95 case PIPE_FORMAT_S8Z24_UNORM:
96 {
97 const uint *src = (uint *) stmap + srcY * ps->pitch + x;
98 GLint k;
99 for (k = 0; k < width; k++) {
100 values[k] = src[k] >> 24;
101 }
102 }
103 break;
104 case PIPE_FORMAT_Z24S8_UNORM:
105 {
106 const uint *src = (uint *) stmap + srcY * ps->pitch + x;
107 GLint k;
108 for (k = 0; k < width; k++) {
109 values[k] = src[k] & 0xff;
110 }
111 }
112 break;
113 default:
114 assert(0);
115 }
116
117 /* store */
118 dest = _mesa_image_address2d(packing, pixels, width, height,
119 GL_STENCIL_INDEX, type, j, 0);
120
121 _mesa_pack_stencil_span(ctx, width, type, dest, values, packing);
122 }
123
124
125 /* unmap the stencil buffer */
126 pipe_surface_unmap(ps);
127 }
128
129
130 /**
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.
134 */
135 static void
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,
139 GLvoid *dest)
140 {
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;
145 GLfloat *df;
146 struct st_renderbuffer *strb;
147 struct gl_pixelstore_attrib clippedPacking = *pack;
148
149 /* XXX convolution not done yet */
150 assert((transferOps & IMAGE_CONVOLUTION_BIT) == 0);
151
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 */
155 return;
156 }
157
158 dest = _mesa_map_readpix_pbo(ctx, &clippedPacking, dest);
159 if (!dest)
160 return;
161
162 /* make sure rendering has completed */
163 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE);
164
165 if (format == GL_STENCIL_INDEX) {
166 st_read_stencil_pixels(ctx, x, y, width, height, type, pack, dest);
167 return;
168 }
169 else if (format == GL_DEPTH_COMPONENT) {
170 strb = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
171 }
172 else {
173 strb = st_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
174 }
175 if (!strb)
176 return;
177
178
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;
184 }
185 else {
186 /* write tile(row) into temp row buffer */
187 df = (GLfloat *) temp;
188 dfStride = 0;
189 }
190
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;
194 yStep = -1;
195 }
196 else {
197 yStep = 1;
198 }
199
200 /*
201 * Copy pixels from pipe_surface to user memory
202 */
203 {
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,
209 format, type);
210
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,
218 width, 1, ztemp, 0);
219 y += yStep;
220 for (j = 0; j < width; j++) {
221 zfloat[j] = (float) (scale * (ztemp[j] & 0xffffff));
222 }
223 _mesa_pack_depth_span(ctx, width, dst, type,
224 zfloat, &clippedPacking);
225 dst += dstStride;
226 }
227 }
228 else {
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);
233 y += yStep;
234 dst += dstStride;
235 }
236 }
237 }
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);
244 y += yStep;
245 for (j = 0; j < width; j++) {
246 zfloat[j] = (float) (scale * ztemp[j]);
247 }
248 _mesa_pack_depth_span(ctx, width, dst, type,
249 zfloat, &clippedPacking);
250 dst += dstStride;
251 }
252 }
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);
259 y += yStep;
260 for (j = 0; j < width; j++) {
261 zfloat[j] = (float) (scale * ztemp[j]);
262 }
263 _mesa_pack_depth_span(ctx, width, dst, type,
264 zfloat, &clippedPacking);
265 dst += dstStride;
266 }
267 }
268 else {
269 /* RGBA format */
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);
273 y += yStep;
274 df += dfStride;
275 if (!dfStride) {
276 _mesa_pack_rgba_span_float(ctx, width, temp, format, type, dst,
277 &clippedPacking, transferOps);
278 dst += dstStride;
279 }
280 }
281 }
282 }
283
284 _mesa_unmap_readpix_pbo(ctx, &clippedPacking);
285 }
286
287
288 void st_init_readpixels_functions(struct dd_function_table *functions)
289 {
290 functions->ReadPixels = st_readpixels;
291 }