st/mesa: hook up a no-op try_pbo_readpixels
[mesa.git] / src / mesa / state_tracker / st_cb_readpixels.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 #include "main/bufferobj.h"
29 #include "main/image.h"
30 #include "main/pbo.h"
31 #include "main/imports.h"
32 #include "main/readpix.h"
33 #include "main/enums.h"
34 #include "main/framebuffer.h"
35 #include "util/u_inlines.h"
36 #include "util/u_format.h"
37
38 #include "st_cb_fbo.h"
39 #include "st_atom.h"
40 #include "st_context.h"
41 #include "st_cb_bitmap.h"
42 #include "st_cb_readpixels.h"
43 #include "state_tracker/st_cb_texture.h"
44 #include "state_tracker/st_format.h"
45 #include "state_tracker/st_texture.h"
46
47 static boolean
48 needs_integer_signed_unsigned_conversion(const struct gl_context *ctx,
49 GLenum format, GLenum type)
50 {
51 struct gl_renderbuffer *rb =
52 _mesa_get_read_renderbuffer_for_format(ctx, format);
53
54 assert(rb);
55
56 GLenum srcType = _mesa_get_format_datatype(rb->Format);
57
58 if ((srcType == GL_INT &&
59 (type == GL_UNSIGNED_INT ||
60 type == GL_UNSIGNED_SHORT ||
61 type == GL_UNSIGNED_BYTE)) ||
62 (srcType == GL_UNSIGNED_INT &&
63 (type == GL_INT ||
64 type == GL_SHORT ||
65 type == GL_BYTE))) {
66 return TRUE;
67 }
68
69 return FALSE;
70 }
71
72 static bool
73 try_pbo_readpixels(struct st_context *st, struct st_renderbuffer *strb,
74 GLint x, GLint y, GLsizei width, GLsizei height,
75 enum pipe_format src_format, enum pipe_format dst_format,
76 const struct gl_pixelstore_attrib *pack, void *pixels)
77 {
78 return false;
79 }
80
81 /**
82 * This uses a blit to copy the read buffer to a texture format which matches
83 * the format and type combo and then a fast read-back is done using memcpy.
84 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
85 * a format which matches the swizzling.
86 *
87 * If such a format isn't available, we fall back to _mesa_readpixels.
88 *
89 * NOTE: Some drivers use a blit to convert between tiled and linear
90 * texture layouts during texture uploads/downloads, so the blit
91 * we do here should be free in such cases.
92 */
93 static void
94 st_ReadPixels(struct gl_context *ctx, GLint x, GLint y,
95 GLsizei width, GLsizei height,
96 GLenum format, GLenum type,
97 const struct gl_pixelstore_attrib *pack,
98 void *pixels)
99 {
100 struct st_context *st = st_context(ctx);
101 struct gl_renderbuffer *rb =
102 _mesa_get_read_renderbuffer_for_format(ctx, format);
103 struct st_renderbuffer *strb = st_renderbuffer(rb);
104 struct pipe_context *pipe = st->pipe;
105 struct pipe_screen *screen = pipe->screen;
106 struct pipe_resource *src;
107 struct pipe_resource *dst = NULL;
108 struct pipe_resource dst_templ;
109 enum pipe_format dst_format, src_format;
110 struct pipe_blit_info blit;
111 unsigned bind = PIPE_BIND_TRANSFER_READ;
112 struct pipe_transfer *tex_xfer;
113 ubyte *map = NULL;
114
115 /* Validate state (to be sure we have up-to-date framebuffer surfaces)
116 * and flush the bitmap cache prior to reading. */
117 st_validate_state(st, ST_PIPELINE_RENDER);
118 st_flush_bitmap_cache(st);
119
120 if (!st->prefer_blit_based_texture_transfer) {
121 goto fallback;
122 }
123
124 /* This must be done after state validation. */
125 src = strb->texture;
126
127 /* XXX Fallback for depth-stencil formats due to an incomplete
128 * stencil blit implementation in some drivers. */
129 if (format == GL_DEPTH_STENCIL) {
130 goto fallback;
131 }
132
133 /* If the base internal format and the texture format don't match, we have
134 * to use the slow path. */
135 if (rb->_BaseFormat !=
136 _mesa_get_format_base_format(rb->Format)) {
137 goto fallback;
138 }
139
140 if (_mesa_readpixels_needs_slow_path(ctx, format, type, GL_TRUE)) {
141 goto fallback;
142 }
143
144 /* Convert the source format to what is expected by ReadPixels
145 * and see if it's supported. */
146 src_format = util_format_linear(src->format);
147 src_format = util_format_luminance_to_red(src_format);
148 src_format = util_format_intensity_to_red(src_format);
149
150 if (!src_format ||
151 !screen->is_format_supported(screen, src_format, src->target,
152 src->nr_samples,
153 PIPE_BIND_SAMPLER_VIEW)) {
154 goto fallback;
155 }
156
157 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
158 bind |= PIPE_BIND_DEPTH_STENCIL;
159 else
160 bind |= PIPE_BIND_RENDER_TARGET;
161
162 /* Choose the destination format by finding the best match
163 * for the format+type combo. */
164 dst_format = st_choose_matching_format(st, bind, format, type,
165 pack->SwapBytes);
166 if (dst_format == PIPE_FORMAT_NONE) {
167 goto fallback;
168 }
169
170 if (_mesa_is_bufferobj(pack->BufferObj)) {
171 if (try_pbo_readpixels(st, strb, x, y, width, height,
172 src_format, dst_format,
173 pack, pixels))
174 return;
175 }
176
177 /* We are creating a texture of the size of the region being read back.
178 * Need to check for NPOT texture support. */
179 if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES) &&
180 (!util_is_power_of_two(width) ||
181 !util_is_power_of_two(height))) {
182 goto fallback;
183 }
184
185 /* See if the texture format already matches the format and type,
186 * in which case the memcpy-based fast path will likely be used and
187 * we don't have to blit. */
188 if (_mesa_format_matches_format_and_type(rb->Format, format,
189 type, pack->SwapBytes, NULL)) {
190 goto fallback;
191 }
192
193 if (needs_integer_signed_unsigned_conversion(ctx, format, type)) {
194 goto fallback;
195 }
196
197 /* create the destination texture */
198 memset(&dst_templ, 0, sizeof(dst_templ));
199 dst_templ.target = PIPE_TEXTURE_2D;
200 dst_templ.format = dst_format;
201 dst_templ.bind = bind;
202 dst_templ.usage = PIPE_USAGE_STAGING;
203
204 st_gl_texture_dims_to_pipe_dims(GL_TEXTURE_2D, width, height, 1,
205 &dst_templ.width0, &dst_templ.height0,
206 &dst_templ.depth0, &dst_templ.array_size);
207
208 dst = screen->resource_create(screen, &dst_templ);
209 if (!dst) {
210 goto fallback;
211 }
212
213 memset(&blit, 0, sizeof(blit));
214 blit.src.resource = src;
215 blit.src.level = strb->surface->u.tex.level;
216 blit.src.format = src_format;
217 blit.dst.resource = dst;
218 blit.dst.level = 0;
219 blit.dst.format = dst->format;
220 blit.src.box.x = x;
221 blit.dst.box.x = 0;
222 blit.src.box.y = y;
223 blit.dst.box.y = 0;
224 blit.src.box.z = strb->surface->u.tex.first_layer;
225 blit.dst.box.z = 0;
226 blit.src.box.width = blit.dst.box.width = width;
227 blit.src.box.height = blit.dst.box.height = height;
228 blit.src.box.depth = blit.dst.box.depth = 1;
229 blit.mask = st_get_blit_mask(rb->_BaseFormat, format);
230 blit.filter = PIPE_TEX_FILTER_NEAREST;
231 blit.scissor_enable = FALSE;
232
233 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
234 blit.src.box.y = rb->Height - blit.src.box.y;
235 blit.src.box.height = -blit.src.box.height;
236 }
237
238 /* blit */
239 st->pipe->blit(st->pipe, &blit);
240
241 /* map resources */
242 pixels = _mesa_map_pbo_dest(ctx, pack, pixels);
243
244 map = pipe_transfer_map_3d(pipe, dst, 0, PIPE_TRANSFER_READ,
245 0, 0, 0, width, height, 1, &tex_xfer);
246 if (!map) {
247 _mesa_unmap_pbo_dest(ctx, pack);
248 pipe_resource_reference(&dst, NULL);
249 goto fallback;
250 }
251
252 /* memcpy data into a user buffer */
253 {
254 const uint bytesPerRow = width * util_format_get_blocksize(dst_format);
255 GLuint row;
256
257 for (row = 0; row < (unsigned) height; row++) {
258 void *dest = _mesa_image_address2d(pack, pixels,
259 width, height, format,
260 type, row, 0);
261 memcpy(dest, map, bytesPerRow);
262 map += tex_xfer->stride;
263 }
264 }
265
266 pipe_transfer_unmap(pipe, tex_xfer);
267 _mesa_unmap_pbo_dest(ctx, pack);
268 pipe_resource_reference(&dst, NULL);
269 return;
270
271 fallback:
272 _mesa_readpixels(ctx, x, y, width, height, format, type, pack, pixels);
273 }
274
275 void st_init_readpixels_functions(struct dd_function_table *functions)
276 {
277 functions->ReadPixels = st_ReadPixels;
278 }