gallium: unused var silence warning
[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/context.h"
38 #include "main/image.h"
39
40 #include "pipe/p_context.h"
41 #include "pipe/p_defines.h"
42 #include "pipe/p_inlines.h"
43 #include "util/p_tile.h"
44 #include "st_context.h"
45 #include "st_cb_readpixels.h"
46 #include "st_cb_fbo.h"
47 #include "st_format.h"
48 #include "st_public.h"
49
50
51 /**
52 * Special case for reading stencil buffer.
53 * For color/depth we use get_tile(). For stencil, map the stencil buffer.
54 */
55 void
56 st_read_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
57 GLsizei width, GLsizei height, GLenum type,
58 const struct gl_pixelstore_attrib *packing,
59 GLvoid *pixels)
60 {
61 struct gl_framebuffer *fb = ctx->ReadBuffer;
62 struct st_renderbuffer *strb = st_renderbuffer(fb->_StencilBuffer);
63 struct pipe_surface *ps = strb->surface;
64 ubyte *stmap;
65 GLint j;
66
67 /* map the stencil buffer */
68 stmap = pipe_surface_map(ps);
69
70 /* width should never be > MAX_WIDTH since we did clipping earlier */
71 ASSERT(width <= MAX_WIDTH);
72
73 /* process image row by row */
74 for (j = 0; j < height; j++, y++) {
75 GLvoid *dest;
76 GLstencil values[MAX_WIDTH];
77 GLint srcY;
78
79 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
80 srcY = ctx->DrawBuffer->Height - y - 1;
81 }
82 else {
83 srcY = y;
84 }
85
86 /* get stencil values */
87 switch (ps->format) {
88 case PIPE_FORMAT_U_S8:
89 {
90 const ubyte *src = stmap + srcY * ps->pitch + x;
91 memcpy(values, src, width);
92 }
93 break;
94 case PIPE_FORMAT_S8Z24_UNORM:
95 {
96 const uint *src = (uint *) stmap + srcY * ps->pitch + x;
97 GLint k;
98 for (k = 0; k < width; k++) {
99 values[k] = src[k] >> 24;
100 }
101 }
102 break;
103 case PIPE_FORMAT_Z24S8_UNORM:
104 {
105 const uint *src = (uint *) stmap + srcY * ps->pitch + x;
106 GLint k;
107 for (k = 0; k < width; k++) {
108 values[k] = src[k] & 0xff;
109 }
110 }
111 break;
112 default:
113 assert(0);
114 }
115
116 /* store */
117 dest = _mesa_image_address2d(packing, pixels, width, height,
118 GL_STENCIL_INDEX, type, j, 0);
119
120 _mesa_pack_stencil_span(ctx, width, type, dest, values, packing);
121 }
122
123
124 /* unmap the stencil buffer */
125 pipe_surface_unmap(ps);
126 }
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 /* make sure rendering has completed */
159 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE);
160
161 if (pack->BufferObj && pack->BufferObj->Name) {
162 /* reading into a PBO */
163
164 }
165 else {
166 /* reading into user memory/buffer */
167
168 }
169
170 if (format == GL_STENCIL_INDEX) {
171 st_read_stencil_pixels(ctx, x, y, width, height, type, pack, dest);
172 return;
173 }
174 else if (format == GL_DEPTH_COMPONENT) {
175 strb = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
176 }
177 else {
178 strb = st_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
179 }
180 if (!strb)
181 return;
182
183
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;
189 }
190 else {
191 /* write tile(row) into temp row buffer */
192 df = (GLfloat *) temp;
193 dfStride = 0;
194 }
195
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;
199 yStep = -1;
200 }
201 else {
202 yStep = 1;
203 }
204
205 /*
206 * Copy pixels from pipe_surface to user memory
207 */
208 {
209 /* dest of first pixel in client memory */
210 GLubyte *dst = _mesa_image_address2d(&clippedPacking, dest, width,
211 height, format, type, 0, 0);
212 /* dest row stride */
213 const GLint dstStride = _mesa_image_row_stride(&clippedPacking, width,
214 format, type);
215
216 if (strb->surface->format == PIPE_FORMAT_S8Z24_UNORM) {
217 if (format == GL_DEPTH_COMPONENT) {
218 for (i = 0; i < height; i++) {
219 GLuint ztemp[MAX_WIDTH], j;
220 GLfloat zfloat[MAX_WIDTH];
221 const double scale = 1.0 / ((1 << 24) - 1);
222 pipe_get_tile_raw(pipe, strb->surface, x, y,
223 width, 1, ztemp, 0);
224 y += yStep;
225 for (j = 0; j < width; j++) {
226 zfloat[j] = (float) (scale * (ztemp[j] & 0xffffff));
227 }
228 _mesa_pack_depth_span(ctx, width, dst, type,
229 zfloat, &clippedPacking);
230 dst += dstStride;
231 }
232 }
233 else {
234 /* untested, but simple: */
235 assert(format == GL_DEPTH_STENCIL_EXT);
236 for (i = 0; i < height; i++) {
237 pipe_get_tile_raw(pipe, strb->surface, x, y, width, 1, dst, 0);
238 y += yStep;
239 dst += dstStride;
240 }
241 }
242 }
243 else if (strb->surface->format == PIPE_FORMAT_Z16_UNORM) {
244 for (i = 0; i < height; i++) {
245 GLshort ztemp[MAX_WIDTH], j;
246 GLfloat zfloat[MAX_WIDTH];
247 const double scale = 1.0 / 0xffff;
248 pipe_get_tile_raw(pipe, strb->surface, x, y, width, 1, ztemp, 0);
249 y += yStep;
250 for (j = 0; j < width; j++) {
251 zfloat[j] = (float) (scale * ztemp[j]);
252 }
253 _mesa_pack_depth_span(ctx, width, dst, type,
254 zfloat, &clippedPacking);
255 dst += dstStride;
256 }
257 }
258 else if (strb->surface->format == PIPE_FORMAT_Z32_UNORM) {
259 for (i = 0; i < height; i++) {
260 GLuint ztemp[MAX_WIDTH], j;
261 GLfloat zfloat[MAX_WIDTH];
262 const double scale = 1.0 / 0xffffffff;
263 pipe_get_tile_raw(pipe, strb->surface, x, y, width, 1, ztemp, 0);
264 y += yStep;
265 for (j = 0; j < width; j++) {
266 zfloat[j] = (float) (scale * ztemp[j]);
267 }
268 _mesa_pack_depth_span(ctx, width, dst, type,
269 zfloat, &clippedPacking);
270 dst += dstStride;
271 }
272 }
273 else {
274 /* RGBA format */
275 /* Do a row at a time to flip image data vertically */
276 for (i = 0; i < height; i++) {
277 pipe_get_tile_rgba(pipe, strb->surface, x, y, width, 1, df);
278 y += yStep;
279 df += dfStride;
280 if (!dfStride) {
281 _mesa_pack_rgba_span_float(ctx, width, temp, format, type, dst,
282 &clippedPacking, transferOps);
283 dst += dstStride;
284 }
285 }
286 }
287 }
288 }
289
290
291 void st_init_readpixels_functions(struct dd_function_table *functions)
292 {
293 functions->ReadPixels = st_readpixels;
294 }