Use write posting in the kickoff function too.
[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 "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"
47
48
49 /**
50 * Special case for reading stencil buffer.
51 * For color/depth we use get_tile(). For stencil, map the stencil buffer.
52 */
53 void
54 st_read_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
55 GLsizei width, GLsizei height, GLenum type,
56 const struct gl_pixelstore_attrib *packing,
57 GLvoid *pixels)
58 {
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;
64 ubyte *stmap;
65 GLint j;
66
67 /* map the stencil buffer */
68 stmap = pipe->region_map(pipe, ps->region);
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->region->pitch + x;
91 memcpy(values, src, width);
92 }
93 break;
94 case PIPE_FORMAT_S8_Z24:
95 {
96 const uint *src = (uint *) stmap + srcY * ps->region->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_Z24_S8:
104 {
105 const uint *src = (uint *) stmap + srcY * ps->region->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->region_unmap(pipe, ps->region);
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 region is totally outside the window bounds */
155 return;
156 }
157
158 /* make sure rendering has completed */
159 pipe->flush(pipe, 0x0);
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 pipe->region_map(pipe, strb->surface->region);
184
185 if (format == GL_RGBA && type == GL_FLOAT) {
186 /* write tile(row) directly into user's buffer */
187 df = (GLfloat *) _mesa_image_address2d(&clippedPacking, dest, width,
188 height, format, type, 0, 0);
189 dfStride = width * 4;
190 }
191 #if 0
192 else if (format == GL_DEPTH_COMPONENT && type == GL_FLOAT) {
193 /* write tile(row) directly into user's buffer */
194 df = (GLfloat *) _mesa_image_address2d(&clippedPacking, dest, width,
195 height, format, type, 0, 0);
196 dfStride = width;
197 }
198 #endif
199 else {
200 /* write tile(row) into temp row buffer */
201 df = (GLfloat *) temp;
202 dfStride = 0;
203 }
204
205 /* determine bottom-to-top vs. top-to-bottom order */
206 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
207 y = strb->Base.Height - 1 - y;
208 yStep = -1;
209 }
210 else {
211 yStep = 1;
212 }
213
214 /* Do a row at a time to flip image data vertically */
215 for (i = 0; i < height; i++) {
216 pipe->get_tile_rgba(pipe, strb->surface, x, y, width, 1, df);
217 y += yStep;
218 df += dfStride;
219 if (!dfStride) {
220 /* convert GLfloat to user's format/type */
221 GLvoid *dst = _mesa_image_address2d(&clippedPacking, dest, width,
222 height, format, type, i, 0);
223 if (format == GL_DEPTH_COMPONENT) {
224 _mesa_pack_depth_span(ctx, width, dst, type,
225 (GLfloat *) temp, &clippedPacking);
226 }
227 else {
228 _mesa_pack_rgba_span_float(ctx, width, temp, format, type, dst,
229 &clippedPacking, transferOps);
230 }
231 }
232 }
233
234 pipe->region_unmap(pipe, strb->surface->region);
235 }
236
237
238 void st_init_readpixels_functions(struct dd_function_table *functions)
239 {
240 functions->ReadPixels = st_readpixels;
241 }