gallium: make state tracker explictly ask for rendercache flushes
[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 "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"
48
49
50 /**
51 * Special case for reading stencil buffer.
52 * For color/depth we use get_tile(). For stencil, map the stencil buffer.
53 */
54 void
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,
58 GLvoid *pixels)
59 {
60 struct gl_framebuffer *fb = ctx->ReadBuffer;
61 struct st_renderbuffer *strb = st_renderbuffer(fb->_StencilBuffer);
62 struct pipe_surface *ps = strb->surface;
63 ubyte *stmap;
64 GLint j;
65
66 /* map the stencil buffer */
67 stmap = pipe_surface_map(ps);
68
69 /* width should never be > MAX_WIDTH since we did clipping earlier */
70 ASSERT(width <= MAX_WIDTH);
71
72 /* process image row by row */
73 for (j = 0; j < height; j++, y++) {
74 GLvoid *dest;
75 GLstencil values[MAX_WIDTH];
76 GLint srcY;
77
78 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
79 srcY = ctx->DrawBuffer->Height - y - 1;
80 }
81 else {
82 srcY = y;
83 }
84
85 /* get stencil values */
86 switch (ps->format) {
87 case PIPE_FORMAT_U_S8:
88 {
89 const ubyte *src = stmap + srcY * ps->pitch + x;
90 memcpy(values, src, width);
91 }
92 break;
93 case PIPE_FORMAT_S8Z24_UNORM:
94 {
95 const uint *src = (uint *) stmap + srcY * ps->pitch + x;
96 GLint k;
97 for (k = 0; k < width; k++) {
98 values[k] = src[k] >> 24;
99 }
100 }
101 break;
102 case PIPE_FORMAT_Z24S8_UNORM:
103 {
104 const uint *src = (uint *) stmap + srcY * ps->pitch + x;
105 GLint k;
106 for (k = 0; k < width; k++) {
107 values[k] = src[k] & 0xff;
108 }
109 }
110 break;
111 default:
112 assert(0);
113 }
114
115 /* store */
116 dest = _mesa_image_address2d(packing, pixels, width, height,
117 GL_STENCIL_INDEX, type, j, 0);
118
119 _mesa_pack_stencil_span(ctx, width, type, dest, values, packing);
120 }
121
122
123 /* unmap the stencil buffer */
124 pipe_surface_unmap(ps);
125 }
126
127
128
129 /**
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.
133 */
134 static void
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,
138 GLvoid *dest)
139 {
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;
144 GLfloat *df;
145 struct st_renderbuffer *strb;
146 struct gl_pixelstore_attrib clippedPacking = *pack;
147
148 /* XXX convolution not done yet */
149 assert((transferOps & IMAGE_CONVOLUTION_BIT) == 0);
150
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 */
154 return;
155 }
156
157 /* make sure rendering has completed */
158 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE);
159
160 if (pack->BufferObj && pack->BufferObj->Name) {
161 /* reading into a PBO */
162
163 }
164 else {
165 /* reading into user memory/buffer */
166
167 }
168
169 if (format == GL_STENCIL_INDEX) {
170 st_read_stencil_pixels(ctx, x, y, width, height, type, pack, dest);
171 return;
172 }
173 else if (format == GL_DEPTH_COMPONENT) {
174 strb = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
175 }
176 else {
177 strb = st_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
178 }
179 if (!strb)
180 return;
181
182 pipe_surface_map(strb->surface);
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 #if 0
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);
195 dfStride = width;
196 }
197 #endif
198 else {
199 /* write tile(row) into temp row buffer */
200 df = (GLfloat *) temp;
201 dfStride = 0;
202 }
203
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;
207 yStep = -1;
208 }
209 else {
210 yStep = 1;
211 }
212
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);
216 y += yStep;
217 df += dfStride;
218 if (!dfStride) {
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);
225 }
226 else {
227 _mesa_pack_rgba_span_float(ctx, width, temp, format, type, dst,
228 &clippedPacking, transferOps);
229 }
230 }
231 }
232
233 pipe_surface_unmap(strb->surface);
234 }
235
236
237 void st_init_readpixels_functions(struct dd_function_table *functions)
238 {
239 functions->ReadPixels = st_readpixels;
240 }