st/mesa: implement PBO downloads for 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 #include "cso_cache/cso_context.h"
38
39 #include "st_cb_fbo.h"
40 #include "st_atom.h"
41 #include "st_context.h"
42 #include "st_cb_bitmap.h"
43 #include "st_cb_readpixels.h"
44 #include "state_tracker/st_cb_texture.h"
45 #include "state_tracker/st_format.h"
46 #include "state_tracker/st_pbo.h"
47 #include "state_tracker/st_texture.h"
48
49 static boolean
50 needs_integer_signed_unsigned_conversion(const struct gl_context *ctx,
51 GLenum format, GLenum type)
52 {
53 struct gl_renderbuffer *rb =
54 _mesa_get_read_renderbuffer_for_format(ctx, format);
55
56 assert(rb);
57
58 GLenum srcType = _mesa_get_format_datatype(rb->Format);
59
60 if ((srcType == GL_INT &&
61 (type == GL_UNSIGNED_INT ||
62 type == GL_UNSIGNED_SHORT ||
63 type == GL_UNSIGNED_BYTE)) ||
64 (srcType == GL_UNSIGNED_INT &&
65 (type == GL_INT ||
66 type == GL_SHORT ||
67 type == GL_BYTE))) {
68 return TRUE;
69 }
70
71 return FALSE;
72 }
73
74 static bool
75 try_pbo_readpixels(struct st_context *st, struct st_renderbuffer *strb,
76 bool invert_y,
77 GLint x, GLint y, GLsizei width, GLsizei height,
78 enum pipe_format src_format, enum pipe_format dst_format,
79 const struct gl_pixelstore_attrib *pack, void *pixels)
80 {
81 struct pipe_context *pipe = st->pipe;
82 struct cso_context *cso = st->cso_context;
83 struct pipe_surface *surface = strb->surface;
84 struct pipe_resource *texture = strb->texture;
85 const struct util_format_description *desc;
86 struct st_pbo_addresses addr;
87 struct pipe_framebuffer_state fb;
88 enum pipe_texture_target view_target;
89 bool success = false;
90
91 if (texture->nr_samples > 1)
92 return false;
93
94 desc = util_format_description(dst_format);
95
96 /* Compute PBO addresses */
97 addr.bytes_per_pixel = desc->block.bits / 8;
98 addr.xoffset = x;
99 addr.yoffset = y;
100 addr.width = width;
101 addr.height = height;
102 addr.depth = 1;
103 if (!st_pbo_addresses_pixelstore(st, GL_TEXTURE_2D, false, pack, pixels, &addr))
104 return false;
105
106 cso_save_state(cso, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS |
107 CSO_BIT_FRAGMENT_SAMPLERS |
108 CSO_BIT_FRAGMENT_IMAGE0 |
109 CSO_BIT_VERTEX_ELEMENTS |
110 CSO_BIT_AUX_VERTEX_BUFFER_SLOT |
111 CSO_BIT_FRAMEBUFFER |
112 CSO_BIT_VIEWPORT |
113 CSO_BIT_RASTERIZER |
114 CSO_BIT_DEPTH_STENCIL_ALPHA |
115 CSO_BIT_STREAM_OUTPUTS |
116 CSO_BIT_PAUSE_QUERIES |
117 CSO_BITS_ALL_SHADERS));
118 cso_save_constant_buffer_slot0(cso, PIPE_SHADER_FRAGMENT);
119
120 /* Set up the sampler_view */
121 {
122 struct pipe_sampler_view templ;
123 struct pipe_sampler_view *sampler_view;
124 struct pipe_sampler_state sampler = {0};
125 const struct pipe_sampler_state *samplers[1] = {&sampler};
126
127 u_sampler_view_default_template(&templ, texture, src_format);
128
129 switch (texture->target) {
130 case PIPE_TEXTURE_CUBE:
131 case PIPE_TEXTURE_CUBE_ARRAY:
132 view_target = PIPE_TEXTURE_2D_ARRAY;
133 break;
134 default:
135 view_target = texture->target;
136 break;
137 }
138
139 templ.target = view_target;
140 templ.u.tex.first_level = surface->u.tex.level;
141 templ.u.tex.last_level = templ.u.tex.first_level;
142
143 if (view_target != PIPE_TEXTURE_3D) {
144 templ.u.tex.first_layer = surface->u.tex.first_layer;
145 templ.u.tex.last_layer = templ.u.tex.last_layer;
146 } else {
147 addr.constants.layer_offset = surface->u.tex.first_layer;
148 }
149
150 sampler_view = pipe->create_sampler_view(pipe, texture, &templ);
151 if (sampler_view == NULL)
152 goto fail;
153
154 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, 1, &sampler_view);
155
156 pipe_sampler_view_reference(&sampler_view, NULL);
157
158 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, 1, samplers);
159 }
160
161 /* Set up destination image */
162 {
163 struct pipe_image_view image;
164
165 memset(&image, 0, sizeof(image));
166 pipe_resource_reference(&image.resource, addr.buffer);
167 image.format = dst_format;
168 image.access = PIPE_IMAGE_ACCESS_WRITE;
169 image.u.buf.first_element = addr.first_element;
170 image.u.buf.last_element = addr.last_element;
171
172 cso_set_shader_images(cso, PIPE_SHADER_FRAGMENT, 0, 1, &image);
173 }
174
175 /* Set up no-attachment framebuffer */
176 memset(&fb, 0, sizeof(fb));
177 fb.width = surface->width;
178 fb.height = surface->height;
179 fb.samples = 1;
180 fb.layers = 1;
181 cso_set_framebuffer(cso, &fb);
182
183 cso_set_viewport_dims(cso, fb.width, fb.height, invert_y);
184
185 if (invert_y)
186 st_pbo_addresses_invert_y(&addr, fb.height);
187
188 {
189 struct pipe_depth_stencil_alpha_state dsa;
190 memset(&dsa, 0, sizeof(dsa));
191 cso_set_depth_stencil_alpha(cso, &dsa);
192 }
193
194 /* Set up the fragment shader */
195 {
196 void *fs = st_pbo_get_download_fs(st, view_target);
197 if (!fs)
198 goto fail;
199
200 cso_set_fragment_shader_handle(cso, fs);
201 }
202
203 success = st_pbo_draw(st, &addr, fb.width, fb.height);
204
205 /* Buffer written via shader images needs explicit synchronization. */
206 pipe->memory_barrier(pipe, PIPE_BARRIER_ALL);
207
208 fail:
209 cso_restore_state(cso);
210 cso_restore_constant_buffer_slot0(cso, PIPE_SHADER_FRAGMENT);
211
212 return success;
213 }
214
215 /**
216 * This uses a blit to copy the read buffer to a texture format which matches
217 * the format and type combo and then a fast read-back is done using memcpy.
218 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
219 * a format which matches the swizzling.
220 *
221 * If such a format isn't available, we fall back to _mesa_readpixels.
222 *
223 * NOTE: Some drivers use a blit to convert between tiled and linear
224 * texture layouts during texture uploads/downloads, so the blit
225 * we do here should be free in such cases.
226 */
227 static void
228 st_ReadPixels(struct gl_context *ctx, GLint x, GLint y,
229 GLsizei width, GLsizei height,
230 GLenum format, GLenum type,
231 const struct gl_pixelstore_attrib *pack,
232 void *pixels)
233 {
234 struct st_context *st = st_context(ctx);
235 struct gl_renderbuffer *rb =
236 _mesa_get_read_renderbuffer_for_format(ctx, format);
237 struct st_renderbuffer *strb = st_renderbuffer(rb);
238 struct pipe_context *pipe = st->pipe;
239 struct pipe_screen *screen = pipe->screen;
240 struct pipe_resource *src;
241 struct pipe_resource *dst = NULL;
242 struct pipe_resource dst_templ;
243 enum pipe_format dst_format, src_format;
244 struct pipe_blit_info blit;
245 unsigned bind = PIPE_BIND_TRANSFER_READ;
246 struct pipe_transfer *tex_xfer;
247 ubyte *map = NULL;
248
249 /* Validate state (to be sure we have up-to-date framebuffer surfaces)
250 * and flush the bitmap cache prior to reading. */
251 st_validate_state(st, ST_PIPELINE_RENDER);
252 st_flush_bitmap_cache(st);
253
254 if (!st->prefer_blit_based_texture_transfer) {
255 goto fallback;
256 }
257
258 /* This must be done after state validation. */
259 src = strb->texture;
260
261 /* XXX Fallback for depth-stencil formats due to an incomplete
262 * stencil blit implementation in some drivers. */
263 if (format == GL_DEPTH_STENCIL) {
264 goto fallback;
265 }
266
267 /* If the base internal format and the texture format don't match, we have
268 * to use the slow path. */
269 if (rb->_BaseFormat !=
270 _mesa_get_format_base_format(rb->Format)) {
271 goto fallback;
272 }
273
274 if (_mesa_readpixels_needs_slow_path(ctx, format, type, GL_TRUE)) {
275 goto fallback;
276 }
277
278 /* Convert the source format to what is expected by ReadPixels
279 * and see if it's supported. */
280 src_format = util_format_linear(src->format);
281 src_format = util_format_luminance_to_red(src_format);
282 src_format = util_format_intensity_to_red(src_format);
283
284 if (!src_format ||
285 !screen->is_format_supported(screen, src_format, src->target,
286 src->nr_samples,
287 PIPE_BIND_SAMPLER_VIEW)) {
288 goto fallback;
289 }
290
291 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
292 bind |= PIPE_BIND_DEPTH_STENCIL;
293 else
294 bind |= PIPE_BIND_RENDER_TARGET;
295
296 /* Choose the destination format by finding the best match
297 * for the format+type combo. */
298 dst_format = st_choose_matching_format(st, bind, format, type,
299 pack->SwapBytes);
300 if (dst_format == PIPE_FORMAT_NONE) {
301 goto fallback;
302 }
303
304 if (st->pbo.download_enabled && _mesa_is_bufferobj(pack->BufferObj)) {
305 if (try_pbo_readpixels(st, strb,
306 st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP,
307 x, y, width, height,
308 src_format, dst_format,
309 pack, pixels))
310 return;
311 }
312
313 /* We are creating a texture of the size of the region being read back.
314 * Need to check for NPOT texture support. */
315 if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES) &&
316 (!util_is_power_of_two(width) ||
317 !util_is_power_of_two(height))) {
318 goto fallback;
319 }
320
321 /* See if the texture format already matches the format and type,
322 * in which case the memcpy-based fast path will likely be used and
323 * we don't have to blit. */
324 if (_mesa_format_matches_format_and_type(rb->Format, format,
325 type, pack->SwapBytes, NULL)) {
326 goto fallback;
327 }
328
329 if (needs_integer_signed_unsigned_conversion(ctx, format, type)) {
330 goto fallback;
331 }
332
333 /* create the destination texture */
334 memset(&dst_templ, 0, sizeof(dst_templ));
335 dst_templ.target = PIPE_TEXTURE_2D;
336 dst_templ.format = dst_format;
337 dst_templ.bind = bind;
338 dst_templ.usage = PIPE_USAGE_STAGING;
339
340 st_gl_texture_dims_to_pipe_dims(GL_TEXTURE_2D, width, height, 1,
341 &dst_templ.width0, &dst_templ.height0,
342 &dst_templ.depth0, &dst_templ.array_size);
343
344 dst = screen->resource_create(screen, &dst_templ);
345 if (!dst) {
346 goto fallback;
347 }
348
349 memset(&blit, 0, sizeof(blit));
350 blit.src.resource = src;
351 blit.src.level = strb->surface->u.tex.level;
352 blit.src.format = src_format;
353 blit.dst.resource = dst;
354 blit.dst.level = 0;
355 blit.dst.format = dst->format;
356 blit.src.box.x = x;
357 blit.dst.box.x = 0;
358 blit.src.box.y = y;
359 blit.dst.box.y = 0;
360 blit.src.box.z = strb->surface->u.tex.first_layer;
361 blit.dst.box.z = 0;
362 blit.src.box.width = blit.dst.box.width = width;
363 blit.src.box.height = blit.dst.box.height = height;
364 blit.src.box.depth = blit.dst.box.depth = 1;
365 blit.mask = st_get_blit_mask(rb->_BaseFormat, format);
366 blit.filter = PIPE_TEX_FILTER_NEAREST;
367 blit.scissor_enable = FALSE;
368
369 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
370 blit.src.box.y = rb->Height - blit.src.box.y;
371 blit.src.box.height = -blit.src.box.height;
372 }
373
374 /* blit */
375 st->pipe->blit(st->pipe, &blit);
376
377 /* map resources */
378 pixels = _mesa_map_pbo_dest(ctx, pack, pixels);
379
380 map = pipe_transfer_map_3d(pipe, dst, 0, PIPE_TRANSFER_READ,
381 0, 0, 0, width, height, 1, &tex_xfer);
382 if (!map) {
383 _mesa_unmap_pbo_dest(ctx, pack);
384 pipe_resource_reference(&dst, NULL);
385 goto fallback;
386 }
387
388 /* memcpy data into a user buffer */
389 {
390 const uint bytesPerRow = width * util_format_get_blocksize(dst_format);
391 GLuint row;
392
393 for (row = 0; row < (unsigned) height; row++) {
394 void *dest = _mesa_image_address2d(pack, pixels,
395 width, height, format,
396 type, row, 0);
397 memcpy(dest, map, bytesPerRow);
398 map += tex_xfer->stride;
399 }
400 }
401
402 pipe_transfer_unmap(pipe, tex_xfer);
403 _mesa_unmap_pbo_dest(ctx, pack);
404 pipe_resource_reference(&dst, NULL);
405 return;
406
407 fallback:
408 _mesa_readpixels(ctx, x, y, width, height, format, type, pack, pixels);
409 }
410
411 void st_init_readpixels_functions(struct dd_function_table *functions)
412 {
413 functions->ReadPixels = st_ReadPixels;
414 }