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