gallium: change pipe_image_view::first_element/last_element -> offset/size
[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 "st_debug.h"
45 #include "state_tracker/st_cb_texture.h"
46 #include "state_tracker/st_format.h"
47 #include "state_tracker/st_pbo.h"
48 #include "state_tracker/st_texture.h"
49
50 /* The readpixels cache caches a blitted staging texture so that back-to-back
51 * calls to glReadPixels with user pointers require less CPU-GPU synchronization.
52 *
53 * Assumptions:
54 *
55 * (1) Blits have high synchronization overheads, and it is beneficial to
56 * use a single blit of the entire framebuffer instead of many smaller
57 * blits (because the smaller blits cannot be batched, and we have to wait
58 * for the GPU after each one).
59 *
60 * (2) transfer_map implicitly involves a blit as well (for de-tiling, copy
61 * from VRAM, etc.), so that it is beneficial to replace the
62 * _mesa_readpixels path as well when possible.
63 *
64 * Change this #define to true to fill and use the cache whenever possible
65 * (this is inefficient and only meant for testing / debugging).
66 */
67 #define ALWAYS_READPIXELS_CACHE false
68
69 static boolean
70 needs_integer_signed_unsigned_conversion(const struct gl_context *ctx,
71 GLenum format, GLenum type)
72 {
73 struct gl_renderbuffer *rb =
74 _mesa_get_read_renderbuffer_for_format(ctx, format);
75
76 assert(rb);
77
78 GLenum srcType = _mesa_get_format_datatype(rb->Format);
79
80 if ((srcType == GL_INT &&
81 (type == GL_UNSIGNED_INT ||
82 type == GL_UNSIGNED_SHORT ||
83 type == GL_UNSIGNED_BYTE)) ||
84 (srcType == GL_UNSIGNED_INT &&
85 (type == GL_INT ||
86 type == GL_SHORT ||
87 type == GL_BYTE))) {
88 return TRUE;
89 }
90
91 return FALSE;
92 }
93
94 static bool
95 try_pbo_readpixels(struct st_context *st, struct st_renderbuffer *strb,
96 bool invert_y,
97 GLint x, GLint y, GLsizei width, GLsizei height,
98 enum pipe_format src_format, enum pipe_format dst_format,
99 const struct gl_pixelstore_attrib *pack, void *pixels)
100 {
101 struct pipe_context *pipe = st->pipe;
102 struct pipe_screen *screen = pipe->screen;
103 struct cso_context *cso = st->cso_context;
104 struct pipe_surface *surface = strb->surface;
105 struct pipe_resource *texture = strb->texture;
106 const struct util_format_description *desc;
107 struct st_pbo_addresses addr;
108 struct pipe_framebuffer_state fb;
109 enum pipe_texture_target view_target;
110 bool success = false;
111
112 if (texture->nr_samples > 1)
113 return false;
114
115 if (!screen->is_format_supported(screen, dst_format, PIPE_TEXTURE_2D,
116 texture->nr_samples,
117 PIPE_BIND_SHADER_IMAGE))
118 return false;
119
120 desc = util_format_description(dst_format);
121
122 /* Compute PBO addresses */
123 addr.bytes_per_pixel = desc->block.bits / 8;
124 addr.xoffset = x;
125 addr.yoffset = y;
126 addr.width = width;
127 addr.height = height;
128 addr.depth = 1;
129 if (!st_pbo_addresses_pixelstore(st, GL_TEXTURE_2D, false, pack, pixels, &addr))
130 return false;
131
132 cso_save_state(cso, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS |
133 CSO_BIT_FRAGMENT_SAMPLERS |
134 CSO_BIT_FRAGMENT_IMAGE0 |
135 CSO_BIT_VERTEX_ELEMENTS |
136 CSO_BIT_AUX_VERTEX_BUFFER_SLOT |
137 CSO_BIT_FRAMEBUFFER |
138 CSO_BIT_VIEWPORT |
139 CSO_BIT_RASTERIZER |
140 CSO_BIT_DEPTH_STENCIL_ALPHA |
141 CSO_BIT_STREAM_OUTPUTS |
142 CSO_BIT_PAUSE_QUERIES |
143 CSO_BITS_ALL_SHADERS));
144 cso_save_constant_buffer_slot0(cso, PIPE_SHADER_FRAGMENT);
145
146 /* Set up the sampler_view */
147 {
148 struct pipe_sampler_view templ;
149 struct pipe_sampler_view *sampler_view;
150 struct pipe_sampler_state sampler = {0};
151 const struct pipe_sampler_state *samplers[1] = {&sampler};
152
153 u_sampler_view_default_template(&templ, texture, src_format);
154
155 switch (texture->target) {
156 case PIPE_TEXTURE_CUBE:
157 case PIPE_TEXTURE_CUBE_ARRAY:
158 view_target = PIPE_TEXTURE_2D_ARRAY;
159 break;
160 default:
161 view_target = texture->target;
162 break;
163 }
164
165 templ.target = view_target;
166 templ.u.tex.first_level = surface->u.tex.level;
167 templ.u.tex.last_level = templ.u.tex.first_level;
168
169 if (view_target != PIPE_TEXTURE_3D) {
170 templ.u.tex.first_layer = surface->u.tex.first_layer;
171 templ.u.tex.last_layer = templ.u.tex.last_layer;
172 } else {
173 addr.constants.layer_offset = surface->u.tex.first_layer;
174 }
175
176 sampler_view = pipe->create_sampler_view(pipe, texture, &templ);
177 if (sampler_view == NULL)
178 goto fail;
179
180 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, 1, &sampler_view);
181
182 pipe_sampler_view_reference(&sampler_view, NULL);
183
184 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, 1, samplers);
185 }
186
187 /* Set up destination image */
188 {
189 struct pipe_image_view image;
190
191 memset(&image, 0, sizeof(image));
192 image.resource = addr.buffer;
193 image.format = dst_format;
194 image.access = PIPE_IMAGE_ACCESS_WRITE;
195 image.u.buf.offset = addr.first_element * addr.bytes_per_pixel;
196 image.u.buf.size = (addr.last_element - addr.first_element + 1) *
197 addr.bytes_per_pixel;
198
199 cso_set_shader_images(cso, PIPE_SHADER_FRAGMENT, 0, 1, &image);
200 }
201
202 /* Set up no-attachment framebuffer */
203 memset(&fb, 0, sizeof(fb));
204 fb.width = surface->width;
205 fb.height = surface->height;
206 fb.samples = 1;
207 fb.layers = 1;
208 cso_set_framebuffer(cso, &fb);
209
210 cso_set_viewport_dims(cso, fb.width, fb.height, invert_y);
211
212 if (invert_y)
213 st_pbo_addresses_invert_y(&addr, fb.height);
214
215 {
216 struct pipe_depth_stencil_alpha_state dsa;
217 memset(&dsa, 0, sizeof(dsa));
218 cso_set_depth_stencil_alpha(cso, &dsa);
219 }
220
221 /* Set up the fragment shader */
222 {
223 void *fs = st_pbo_get_download_fs(st, view_target);
224 if (!fs)
225 goto fail;
226
227 cso_set_fragment_shader_handle(cso, fs);
228 }
229
230 success = st_pbo_draw(st, &addr, fb.width, fb.height);
231
232 /* Buffer written via shader images needs explicit synchronization. */
233 pipe->memory_barrier(pipe, PIPE_BARRIER_ALL);
234
235 fail:
236 cso_restore_state(cso);
237 cso_restore_constant_buffer_slot0(cso, PIPE_SHADER_FRAGMENT);
238
239 return success;
240 }
241
242 /* Invalidate the readpixels cache to ensure we don't read stale data.
243 */
244 void st_invalidate_readpix_cache(struct st_context *st)
245 {
246 pipe_resource_reference(&st->readpix_cache.src, NULL);
247 pipe_resource_reference(&st->readpix_cache.cache, NULL);
248 }
249
250 /**
251 * Create a staging texture and blit the requested region to it.
252 */
253 static struct pipe_resource *
254 blit_to_staging(struct st_context *st, struct st_renderbuffer *strb,
255 bool invert_y,
256 GLint x, GLint y, GLsizei width, GLsizei height,
257 GLenum format,
258 enum pipe_format src_format, enum pipe_format dst_format)
259 {
260 struct pipe_context *pipe = st->pipe;
261 struct pipe_screen *screen = pipe->screen;
262 struct pipe_resource dst_templ;
263 struct pipe_resource *dst;
264 struct pipe_blit_info blit;
265
266 /* We are creating a texture of the size of the region being read back.
267 * Need to check for NPOT texture support. */
268 if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES) &&
269 (!util_is_power_of_two(width) ||
270 !util_is_power_of_two(height)))
271 return NULL;
272
273 /* create the destination texture */
274 memset(&dst_templ, 0, sizeof(dst_templ));
275 dst_templ.target = PIPE_TEXTURE_2D;
276 dst_templ.format = dst_format;
277 dst_templ.bind = PIPE_BIND_TRANSFER_READ;
278 if (util_format_is_depth_or_stencil(dst_format))
279 dst_templ.bind |= PIPE_BIND_DEPTH_STENCIL;
280 else
281 dst_templ.bind |= PIPE_BIND_RENDER_TARGET;
282 dst_templ.usage = PIPE_USAGE_STAGING;
283
284 st_gl_texture_dims_to_pipe_dims(GL_TEXTURE_2D, width, height, 1,
285 &dst_templ.width0, &dst_templ.height0,
286 &dst_templ.depth0, &dst_templ.array_size);
287
288 dst = screen->resource_create(screen, &dst_templ);
289 if (!dst)
290 return NULL;
291
292 memset(&blit, 0, sizeof(blit));
293 blit.src.resource = strb->texture;
294 blit.src.level = strb->surface->u.tex.level;
295 blit.src.format = src_format;
296 blit.dst.resource = dst;
297 blit.dst.level = 0;
298 blit.dst.format = dst->format;
299 blit.src.box.x = x;
300 blit.dst.box.x = 0;
301 blit.src.box.y = y;
302 blit.dst.box.y = 0;
303 blit.src.box.z = strb->surface->u.tex.first_layer;
304 blit.dst.box.z = 0;
305 blit.src.box.width = blit.dst.box.width = width;
306 blit.src.box.height = blit.dst.box.height = height;
307 blit.src.box.depth = blit.dst.box.depth = 1;
308 blit.mask = st_get_blit_mask(strb->Base._BaseFormat, format);
309 blit.filter = PIPE_TEX_FILTER_NEAREST;
310 blit.scissor_enable = FALSE;
311
312 if (invert_y) {
313 blit.src.box.y = strb->Base.Height - blit.src.box.y;
314 blit.src.box.height = -blit.src.box.height;
315 }
316
317 /* blit */
318 st->pipe->blit(st->pipe, &blit);
319
320 return dst;
321 }
322
323 static struct pipe_resource *
324 try_cached_readpixels(struct st_context *st, struct st_renderbuffer *strb,
325 bool invert_y,
326 GLsizei width, GLsizei height,
327 GLenum format,
328 enum pipe_format src_format, enum pipe_format dst_format)
329 {
330 struct pipe_resource *src = strb->texture;
331 struct pipe_resource *dst = NULL;
332
333 if (ST_DEBUG & DEBUG_NOREADPIXCACHE)
334 return NULL;
335
336 /* Reset cache after invalidation or switch of parameters. */
337 if (st->readpix_cache.src != src ||
338 st->readpix_cache.dst_format != dst_format ||
339 st->readpix_cache.level != strb->surface->u.tex.level ||
340 st->readpix_cache.layer != strb->surface->u.tex.first_layer) {
341 pipe_resource_reference(&st->readpix_cache.src, src);
342 pipe_resource_reference(&st->readpix_cache.cache, NULL);
343 st->readpix_cache.dst_format = dst_format;
344 st->readpix_cache.level = strb->surface->u.tex.level;
345 st->readpix_cache.layer = strb->surface->u.tex.first_layer;
346 st->readpix_cache.hits = 0;
347 }
348
349 /* Decide whether to trigger the cache. */
350 if (!st->readpix_cache.cache) {
351 if (!strb->use_readpix_cache && !ALWAYS_READPIXELS_CACHE) {
352 /* Heuristic: If previous successive calls read at least a fraction
353 * of the surface _and_ we read again, trigger the cache.
354 */
355 unsigned threshold = MAX2(1, strb->Base.Width * strb->Base.Height / 8);
356
357 if (st->readpix_cache.hits < threshold) {
358 st->readpix_cache.hits += width * height;
359 return NULL;
360 }
361
362 strb->use_readpix_cache = true;
363 }
364
365 /* Fill the cache */
366 st->readpix_cache.cache = blit_to_staging(st, strb, invert_y,
367 0, 0,
368 strb->Base.Width,
369 strb->Base.Height, format,
370 src_format, dst_format);
371 }
372
373 /* Return an owning reference to stay consistent with the non-cached path */
374 pipe_resource_reference(&dst, st->readpix_cache.cache);
375
376 return dst;
377 }
378
379 /**
380 * This uses a blit to copy the read buffer to a texture format which matches
381 * the format and type combo and then a fast read-back is done using memcpy.
382 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
383 * a format which matches the swizzling.
384 *
385 * If such a format isn't available, we fall back to _mesa_readpixels.
386 *
387 * NOTE: Some drivers use a blit to convert between tiled and linear
388 * texture layouts during texture uploads/downloads, so the blit
389 * we do here should be free in such cases.
390 */
391 static void
392 st_ReadPixels(struct gl_context *ctx, GLint x, GLint y,
393 GLsizei width, GLsizei height,
394 GLenum format, GLenum type,
395 const struct gl_pixelstore_attrib *pack,
396 void *pixels)
397 {
398 struct st_context *st = st_context(ctx);
399 struct gl_renderbuffer *rb =
400 _mesa_get_read_renderbuffer_for_format(ctx, format);
401 struct st_renderbuffer *strb = st_renderbuffer(rb);
402 struct pipe_context *pipe = st->pipe;
403 struct pipe_screen *screen = pipe->screen;
404 struct pipe_resource *src;
405 struct pipe_resource *dst = NULL;
406 enum pipe_format dst_format, src_format;
407 unsigned bind = PIPE_BIND_TRANSFER_READ;
408 struct pipe_transfer *tex_xfer;
409 ubyte *map = NULL;
410 int dst_x, dst_y;
411
412 /* Validate state (to be sure we have up-to-date framebuffer surfaces)
413 * and flush the bitmap cache prior to reading. */
414 st_validate_state(st, ST_PIPELINE_RENDER);
415 st_flush_bitmap_cache(st);
416
417 if (!st->prefer_blit_based_texture_transfer) {
418 goto fallback;
419 }
420
421 /* This must be done after state validation. */
422 src = strb->texture;
423
424 /* XXX Fallback for depth-stencil formats due to an incomplete
425 * stencil blit implementation in some drivers. */
426 if (format == GL_DEPTH_STENCIL) {
427 goto fallback;
428 }
429
430 /* If the base internal format and the texture format don't match, we have
431 * to use the slow path. */
432 if (rb->_BaseFormat !=
433 _mesa_get_format_base_format(rb->Format)) {
434 goto fallback;
435 }
436
437 if (_mesa_readpixels_needs_slow_path(ctx, format, type, GL_TRUE)) {
438 goto fallback;
439 }
440
441 /* Convert the source format to what is expected by ReadPixels
442 * and see if it's supported. */
443 src_format = util_format_linear(src->format);
444 src_format = util_format_luminance_to_red(src_format);
445 src_format = util_format_intensity_to_red(src_format);
446
447 if (!src_format ||
448 !screen->is_format_supported(screen, src_format, src->target,
449 src->nr_samples,
450 PIPE_BIND_SAMPLER_VIEW)) {
451 goto fallback;
452 }
453
454 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
455 bind |= PIPE_BIND_DEPTH_STENCIL;
456 else
457 bind |= PIPE_BIND_RENDER_TARGET;
458
459 /* Choose the destination format by finding the best match
460 * for the format+type combo. */
461 dst_format = st_choose_matching_format(st, bind, format, type,
462 pack->SwapBytes);
463 if (dst_format == PIPE_FORMAT_NONE) {
464 goto fallback;
465 }
466
467 if (st->pbo.download_enabled && _mesa_is_bufferobj(pack->BufferObj)) {
468 if (try_pbo_readpixels(st, strb,
469 st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP,
470 x, y, width, height,
471 src_format, dst_format,
472 pack, pixels))
473 return;
474 }
475
476 if (needs_integer_signed_unsigned_conversion(ctx, format, type)) {
477 goto fallback;
478 }
479
480 /* Cache a staging texture for back-to-back ReadPixels, to avoid CPU-GPU
481 * synchronization overhead.
482 */
483 dst = try_cached_readpixels(st, strb,
484 st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP,
485 width, height, format, src_format, dst_format);
486 if (dst) {
487 dst_x = x;
488 dst_y = y;
489 } else {
490 /* See if the texture format already matches the format and type,
491 * in which case the memcpy-based fast path will likely be used and
492 * we don't have to blit. */
493 if (_mesa_format_matches_format_and_type(rb->Format, format,
494 type, pack->SwapBytes, NULL)) {
495 goto fallback;
496 }
497
498 dst = blit_to_staging(st, strb,
499 st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP,
500 x, y, width, height, format,
501 src_format, dst_format);
502 if (!dst)
503 goto fallback;
504
505 dst_x = 0;
506 dst_y = 0;
507 }
508
509 /* map resources */
510 pixels = _mesa_map_pbo_dest(ctx, pack, pixels);
511
512 map = pipe_transfer_map_3d(pipe, dst, 0, PIPE_TRANSFER_READ,
513 dst_x, dst_y, 0, width, height, 1, &tex_xfer);
514 if (!map) {
515 _mesa_unmap_pbo_dest(ctx, pack);
516 pipe_resource_reference(&dst, NULL);
517 goto fallback;
518 }
519
520 /* memcpy data into a user buffer */
521 {
522 const uint bytesPerRow = width * util_format_get_blocksize(dst_format);
523 const int destStride = _mesa_image_row_stride(pack, width, format, type);
524 char *dest = _mesa_image_address2d(pack, pixels,
525 width, height, format,
526 type, 0, 0);
527
528 if (tex_xfer->stride == bytesPerRow && destStride == bytesPerRow) {
529 memcpy(dest, map, bytesPerRow * height);
530 } else {
531 GLuint row;
532
533 for (row = 0; row < (unsigned) height; row++) {
534 memcpy(dest, map, bytesPerRow);
535 map += tex_xfer->stride;
536 dest += destStride;
537 }
538 }
539 }
540
541 pipe_transfer_unmap(pipe, tex_xfer);
542 _mesa_unmap_pbo_dest(ctx, pack);
543 pipe_resource_reference(&dst, NULL);
544 return;
545
546 fallback:
547 _mesa_readpixels(ctx, x, y, width, height, format, type, pack, pixels);
548 }
549
550 void st_init_readpixels_functions(struct dd_function_table *functions)
551 {
552 functions->ReadPixels = st_ReadPixels;
553 }