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 boolean
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 sampler.normalized_coords = true;
163 const struct pipe_sampler_state *samplers[1] = {&sampler};
164
165 u_sampler_view_default_template(&templ, texture, src_format);
166
167 switch (texture->target) {
168 case PIPE_TEXTURE_CUBE:
169 case PIPE_TEXTURE_CUBE_ARRAY:
170 view_target = PIPE_TEXTURE_2D_ARRAY;
171 break;
172 default:
173 view_target = texture->target;
174 break;
175 }
176
177 templ.target = view_target;
178 templ.u.tex.first_level = surface->u.tex.level;
179 templ.u.tex.last_level = templ.u.tex.first_level;
180
181 if (view_target != PIPE_TEXTURE_3D) {
182 templ.u.tex.first_layer = surface->u.tex.first_layer;
183 templ.u.tex.last_layer = templ.u.tex.first_layer;
184 } else {
185 addr.constants.layer_offset = surface->u.tex.first_layer;
186 }
187
188 sampler_view = pipe->create_sampler_view(pipe, texture, &templ);
189 if (sampler_view == NULL)
190 goto fail;
191
192 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, 0,
193 false, &sampler_view);
194 st->state.num_sampler_views[PIPE_SHADER_FRAGMENT] =
195 MAX2(st->state.num_sampler_views[PIPE_SHADER_FRAGMENT], 1);
196
197 pipe_sampler_view_reference(&sampler_view, NULL);
198
199 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, 1, samplers);
200 }
201
202 /* Set up destination image */
203 {
204 struct pipe_image_view image;
205
206 memset(&image, 0, sizeof(image));
207 image.resource = addr.buffer;
208 image.format = dst_format;
209 image.access = PIPE_IMAGE_ACCESS_WRITE;
210 image.shader_access = PIPE_IMAGE_ACCESS_WRITE;
211 image.u.buf.offset = addr.first_element * addr.bytes_per_pixel;
212 image.u.buf.size = (addr.last_element - addr.first_element + 1) *
213 addr.bytes_per_pixel;
214
215 pipe->set_shader_images(pipe, PIPE_SHADER_FRAGMENT, 0, 1, 0, &image);
216 }
217
218 /* Set up no-attachment framebuffer */
219 memset(&fb, 0, sizeof(fb));
220 fb.width = surface->width;
221 fb.height = surface->height;
222 fb.samples = 1;
223 fb.layers = 1;
224 cso_set_framebuffer(cso, &fb);
225
226 /* Any blend state would do. Set this just to prevent drivers having
227 * blend == NULL.
228 */
229 cso_set_blend(cso, &st->pbo.upload_blend);
230
231 cso_set_viewport_dims(cso, fb.width, fb.height, invert_y);
232
233 if (invert_y)
234 st_pbo_addresses_invert_y(&addr, fb.height);
235
236 {
237 struct pipe_depth_stencil_alpha_state dsa;
238 memset(&dsa, 0, sizeof(dsa));
239 cso_set_depth_stencil_alpha(cso, &dsa);
240 }
241
242 /* Set up the fragment shader */
243 {
244 void *fs = st_pbo_get_download_fs(st, view_target, src_format, dst_format, addr.depth != 1);
245 if (!fs)
246 goto fail;
247
248 cso_set_fragment_shader_handle(cso, fs);
249 }
250
251 success = st_pbo_draw(st, &addr, fb.width, fb.height);
252
253 /* Buffer written via shader images needs explicit synchronization. */
254 pipe->memory_barrier(pipe, PIPE_BARRIER_ALL);
255
256 fail:
257 /* Unbind all because st/mesa won't do it if the current shader doesn't
258 * use them.
259 */
260 cso_restore_state(cso, CSO_UNBIND_FS_SAMPLERVIEWS | CSO_UNBIND_FS_IMAGE0);
261 st->state.num_sampler_views[PIPE_SHADER_FRAGMENT] = 0;
262
263 st->ctx->Array.NewVertexElements = true;
264 st->dirty |= ST_NEW_FS_CONSTANTS |
265 ST_NEW_FS_IMAGES |
266 ST_NEW_FS_SAMPLER_VIEWS |
267 ST_NEW_VERTEX_ARRAYS;
268
269 return success;
270 }
271
272 /**
273 * Create a staging texture and blit the requested region to it.
274 */
275 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)276 blit_to_staging(struct st_context *st, struct gl_renderbuffer *rb,
277 bool invert_y,
278 GLint x, GLint y, GLsizei width, GLsizei height,
279 GLenum format,
280 enum pipe_format src_format, enum pipe_format dst_format)
281 {
282 struct pipe_screen *screen = st->screen;
283 struct pipe_resource dst_templ;
284 struct pipe_resource *dst;
285 struct pipe_blit_info blit;
286
287 /* We are creating a texture of the size of the region being read back.
288 * Need to check for NPOT texture support. */
289 if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES) &&
290 (!util_is_power_of_two_or_zero(width) ||
291 !util_is_power_of_two_or_zero(height)))
292 return NULL;
293
294 /* create the destination texture */
295 memset(&dst_templ, 0, sizeof(dst_templ));
296 dst_templ.target = PIPE_TEXTURE_2D;
297 dst_templ.format = dst_format;
298 if (util_format_is_depth_or_stencil(dst_format))
299 dst_templ.bind |= PIPE_BIND_DEPTH_STENCIL;
300 else
301 dst_templ.bind |= PIPE_BIND_RENDER_TARGET;
302 dst_templ.usage = PIPE_USAGE_STAGING;
303
304 st_gl_texture_dims_to_pipe_dims(GL_TEXTURE_2D, width, height, 1,
305 &dst_templ.width0, &dst_templ.height0,
306 &dst_templ.depth0, &dst_templ.array_size);
307
308 dst = screen->resource_create(screen, &dst_templ);
309 if (!dst)
310 return NULL;
311
312 memset(&blit, 0, sizeof(blit));
313 blit.src.resource = rb->texture;
314 blit.src.level = rb->surface->u.tex.level;
315 blit.src.format = src_format;
316 blit.dst.resource = dst;
317 blit.dst.level = 0;
318 blit.dst.format = dst->format;
319 blit.src.box.x = x;
320 blit.dst.box.x = 0;
321 blit.src.box.y = y;
322 blit.dst.box.y = 0;
323 blit.src.box.z = rb->surface->u.tex.first_layer;
324 blit.dst.box.z = 0;
325 blit.src.box.width = blit.dst.box.width = width;
326 blit.src.box.height = blit.dst.box.height = height;
327 blit.src.box.depth = blit.dst.box.depth = 1;
328 blit.mask = st_get_blit_mask(rb->_BaseFormat, format);
329 blit.filter = PIPE_TEX_FILTER_NEAREST;
330 blit.scissor_enable = FALSE;
331
332 if (invert_y) {
333 blit.src.box.y = rb->Height - blit.src.box.y;
334 blit.src.box.height = -blit.src.box.height;
335 }
336
337 /* blit */
338 st->pipe->blit(st->pipe, &blit);
339
340 return dst;
341 }
342
343 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)344 try_cached_readpixels(struct st_context *st, struct gl_renderbuffer *rb,
345 bool invert_y,
346 GLsizei width, GLsizei height,
347 GLenum format,
348 enum pipe_format src_format, enum pipe_format dst_format)
349 {
350 struct pipe_resource *src = rb->texture;
351 struct pipe_resource *dst = NULL;
352
353 if (ST_DEBUG & DEBUG_NOREADPIXCACHE)
354 return NULL;
355
356 /* Reset cache after invalidation or switch of parameters. */
357 if (st->readpix_cache.src != src ||
358 st->readpix_cache.dst_format != dst_format ||
359 st->readpix_cache.level != rb->surface->u.tex.level ||
360 st->readpix_cache.layer != rb->surface->u.tex.first_layer) {
361 pipe_resource_reference(&st->readpix_cache.src, src);
362 pipe_resource_reference(&st->readpix_cache.cache, NULL);
363 st->readpix_cache.dst_format = dst_format;
364 st->readpix_cache.level = rb->surface->u.tex.level;
365 st->readpix_cache.layer = rb->surface->u.tex.first_layer;
366 st->readpix_cache.hits = 0;
367 }
368
369 /* Decide whether to trigger the cache. */
370 if (!st->readpix_cache.cache) {
371 if (!rb->use_readpix_cache && !ALWAYS_READPIXELS_CACHE) {
372 /* Heuristic: If previous successive calls read at least a fraction
373 * of the surface _and_ we read again, trigger the cache.
374 */
375 unsigned threshold = MAX2(1, rb->Width * rb->Height / 8);
376
377 if (st->readpix_cache.hits < threshold) {
378 st->readpix_cache.hits += width * height;
379 return NULL;
380 }
381
382 rb->use_readpix_cache = true;
383 }
384
385 /* Fill the cache */
386 st->readpix_cache.cache = blit_to_staging(st, rb, invert_y,
387 0, 0,
388 rb->Width,
389 rb->Height, format,
390 src_format, dst_format);
391 }
392
393 /* Return an owning reference to stay consistent with the non-cached path */
394 pipe_resource_reference(&dst, st->readpix_cache.cache);
395
396 return dst;
397 }
398
399 /**
400 * This uses a blit to copy the read buffer to a texture format which matches
401 * the format and type combo and then a fast read-back is done using memcpy.
402 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
403 * a format which matches the swizzling.
404 *
405 * If such a format isn't available, we fall back to _mesa_readpixels.
406 *
407 * NOTE: Some drivers use a blit to convert between tiled and linear
408 * texture layouts during texture uploads/downloads, so the blit
409 * we do here should be free in such cases.
410 */
411 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)412 st_ReadPixels(struct gl_context *ctx, GLint x, GLint y,
413 GLsizei width, GLsizei height,
414 GLenum format, GLenum type,
415 const struct gl_pixelstore_attrib *pack,
416 void *pixels)
417 {
418 struct st_context *st = st_context(ctx);
419 struct gl_renderbuffer *rb =
420 _mesa_get_read_renderbuffer_for_format(ctx, format);
421 struct pipe_context *pipe = st->pipe;
422 struct pipe_screen *screen = st->screen;
423 struct pipe_resource *src;
424 struct pipe_resource *dst = NULL;
425 enum pipe_format dst_format, src_format;
426 unsigned bind;
427 struct pipe_transfer *tex_xfer;
428 ubyte *map = NULL;
429 int dst_x, dst_y;
430
431 /* Validate state (to be sure we have up-to-date framebuffer surfaces)
432 * and flush the bitmap cache prior to reading. */
433 st_validate_state(st, ST_PIPELINE_UPDATE_FRAMEBUFFER);
434 st_flush_bitmap_cache(st);
435
436 if (!st->prefer_blit_based_texture_transfer) {
437 goto fallback;
438 }
439
440 /* This must be done after state validation. */
441 src = rb->texture;
442
443 /* XXX Fallback for depth-stencil formats due to an incomplete
444 * stencil blit implementation in some drivers. */
445 if (format == GL_DEPTH_STENCIL) {
446 goto fallback;
447 }
448
449 /* If the base internal format and the texture format don't match, we have
450 * to use the slow path. */
451 if (rb->_BaseFormat !=
452 _mesa_get_format_base_format(rb->Format)) {
453 goto fallback;
454 }
455
456 if (_mesa_readpixels_needs_slow_path(ctx, format, type, GL_TRUE)) {
457 goto fallback;
458 }
459
460 /* Convert the source format to what is expected by ReadPixels
461 * and see if it's supported. */
462 src_format = util_format_linear(rb->Format);
463 src_format = util_format_luminance_to_red(src_format);
464 src_format = util_format_intensity_to_red(src_format);
465
466 if (!src_format ||
467 !screen->is_format_supported(screen, src_format, src->target,
468 src->nr_samples, src->nr_storage_samples,
469 PIPE_BIND_SAMPLER_VIEW)) {
470 goto fallback;
471 }
472
473 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
474 bind = PIPE_BIND_DEPTH_STENCIL;
475 else
476 bind = PIPE_BIND_RENDER_TARGET;
477
478 /* Choose the destination format by finding the best match
479 * for the format+type combo. */
480 dst_format = st_choose_matching_format(st, bind, format, type,
481 pack->SwapBytes);
482 if (dst_format == PIPE_FORMAT_NONE) {
483 goto fallback;
484 }
485
486 if (st->pbo.download_enabled && pack->BufferObj) {
487 if (try_pbo_readpixels(st, rb,
488 _mesa_fb_orientation(ctx->ReadBuffer) == Y_0_TOP,
489 x, y, width, height,
490 format, src_format, dst_format,
491 pack, pixels))
492 return;
493 }
494
495 if (needs_integer_signed_unsigned_conversion(ctx, format, type)) {
496 goto fallback;
497 }
498
499 /* Cache a staging texture for back-to-back ReadPixels, to avoid CPU-GPU
500 * synchronization overhead.
501 */
502 dst = try_cached_readpixels(st, rb,
503 _mesa_fb_orientation(ctx->ReadBuffer) == Y_0_TOP,
504 width, height, format, src_format, dst_format);
505 if (dst) {
506 dst_x = x;
507 dst_y = y;
508 } else {
509 /* See if the texture format already matches the format and type,
510 * in which case the memcpy-based fast path will likely be used and
511 * we don't have to blit. */
512 if (_mesa_format_matches_format_and_type(rb->Format, format,
513 type, pack->SwapBytes, NULL)) {
514 goto fallback;
515 }
516
517 dst = blit_to_staging(st, rb,
518 _mesa_fb_orientation(ctx->ReadBuffer) == Y_0_TOP,
519 x, y, width, height, format,
520 src_format, dst_format);
521 if (!dst)
522 goto fallback;
523
524 dst_x = 0;
525 dst_y = 0;
526 }
527
528 /* map resources */
529 pixels = _mesa_map_pbo_dest(ctx, pack, pixels);
530
531 map = pipe_texture_map_3d(pipe, dst, 0, PIPE_MAP_READ,
532 dst_x, dst_y, 0, width, height, 1, &tex_xfer);
533 if (!map) {
534 _mesa_unmap_pbo_dest(ctx, pack);
535 pipe_resource_reference(&dst, NULL);
536 goto fallback;
537 }
538
539 /* memcpy data into a user buffer */
540 {
541 const uint bytesPerRow = width * util_format_get_blocksize(dst_format);
542 const int destStride = _mesa_image_row_stride(pack, width, format, type);
543 char *dest = _mesa_image_address2d(pack, pixels,
544 width, height, format,
545 type, 0, 0);
546
547 if (tex_xfer->stride == bytesPerRow && destStride == bytesPerRow) {
548 memcpy(dest, map, bytesPerRow * height);
549 } else {
550 GLuint row;
551
552 for (row = 0; row < (unsigned) height; row++) {
553 memcpy(dest, map, bytesPerRow);
554 map += tex_xfer->stride;
555 dest += destStride;
556 }
557 }
558 }
559
560 pipe_texture_unmap(pipe, tex_xfer);
561 _mesa_unmap_pbo_dest(ctx, pack);
562 pipe_resource_reference(&dst, NULL);
563 return;
564
565 fallback:
566 _mesa_readpixels(ctx, x, y, width, height, format, type, pack, pixels);
567 }
568