1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "main/bufferobj.h"
29 #include "main/image.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/format/u_format.h"
37 #include "cso_cache/cso_context.h"
39 #include "st_cb_fbo.h"
41 #include "st_context.h"
42 #include "st_cb_bitmap.h"
43 #include "st_cb_readpixels.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 #include "state_tracker/st_util.h"
52 /* The readpixels cache caches a blitted staging texture so that back-to-back
53 * calls to glReadPixels with user pointers require less CPU-GPU synchronization.
57 * (1) Blits have high synchronization overheads, and it is beneficial to
58 * use a single blit of the entire framebuffer instead of many smaller
59 * blits (because the smaller blits cannot be batched, and we have to wait
60 * for the GPU after each one).
62 * (2) transfer_map implicitly involves a blit as well (for de-tiling, copy
63 * from VRAM, etc.), so that it is beneficial to replace the
64 * _mesa_readpixels path as well when possible.
66 * Change this #define to true to fill and use the cache whenever possible
67 * (this is inefficient and only meant for testing / debugging).
69 #define ALWAYS_READPIXELS_CACHE false
72 needs_integer_signed_unsigned_conversion(const struct gl_context
*ctx
,
73 GLenum format
, GLenum type
)
75 struct gl_renderbuffer
*rb
=
76 _mesa_get_read_renderbuffer_for_format(ctx
, format
);
80 GLenum srcType
= _mesa_get_format_datatype(rb
->Format
);
82 if ((srcType
== GL_INT
&&
83 (type
== GL_UNSIGNED_INT
||
84 type
== GL_UNSIGNED_SHORT
||
85 type
== GL_UNSIGNED_BYTE
)) ||
86 (srcType
== GL_UNSIGNED_INT
&&
97 try_pbo_readpixels(struct st_context
*st
, struct st_renderbuffer
*strb
,
99 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
100 enum pipe_format src_format
, enum pipe_format dst_format
,
101 const struct gl_pixelstore_attrib
*pack
, void *pixels
)
103 struct pipe_context
*pipe
= st
->pipe
;
104 struct pipe_screen
*screen
= pipe
->screen
;
105 struct cso_context
*cso
= st
->cso_context
;
106 struct pipe_surface
*surface
= strb
->surface
;
107 struct pipe_resource
*texture
= strb
->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;
114 if (texture
->nr_samples
> 1)
117 if (!screen
->is_format_supported(screen
, dst_format
, PIPE_BUFFER
, 0, 0,
118 PIPE_BIND_SHADER_IMAGE
))
121 desc
= util_format_description(dst_format
);
123 /* Compute PBO addresses */
124 addr
.bytes_per_pixel
= desc
->block
.bits
/ 8;
128 addr
.height
= height
;
130 if (!st_pbo_addresses_pixelstore(st
, GL_TEXTURE_2D
, false, pack
, pixels
, &addr
))
133 cso_save_state(cso
, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
134 CSO_BIT_FRAGMENT_SAMPLERS
|
135 CSO_BIT_FRAGMENT_IMAGE0
|
137 CSO_BIT_VERTEX_ELEMENTS
|
138 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
139 CSO_BIT_FRAMEBUFFER
|
142 CSO_BIT_DEPTH_STENCIL_ALPHA
|
143 CSO_BIT_STREAM_OUTPUTS
|
144 (st
->active_queries
? CSO_BIT_PAUSE_QUERIES
: 0) |
145 CSO_BIT_SAMPLE_MASK
|
146 CSO_BIT_MIN_SAMPLES
|
147 CSO_BIT_RENDER_CONDITION
|
148 CSO_BITS_ALL_SHADERS
));
149 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
151 cso_set_sample_mask(cso
, ~0);
152 cso_set_min_samples(cso
, 1);
153 cso_set_render_condition(cso
, NULL
, FALSE
, 0);
155 /* Set up the sampler_view */
157 struct pipe_sampler_view templ
;
158 struct pipe_sampler_view
*sampler_view
;
159 struct pipe_sampler_state sampler
= {0};
160 const struct pipe_sampler_state
*samplers
[1] = {&sampler
};
162 u_sampler_view_default_template(&templ
, texture
, src_format
);
164 switch (texture
->target
) {
165 case PIPE_TEXTURE_CUBE
:
166 case PIPE_TEXTURE_CUBE_ARRAY
:
167 view_target
= PIPE_TEXTURE_2D_ARRAY
;
170 view_target
= texture
->target
;
174 templ
.target
= view_target
;
175 templ
.u
.tex
.first_level
= surface
->u
.tex
.level
;
176 templ
.u
.tex
.last_level
= templ
.u
.tex
.first_level
;
178 if (view_target
!= PIPE_TEXTURE_3D
) {
179 templ
.u
.tex
.first_layer
= surface
->u
.tex
.first_layer
;
180 templ
.u
.tex
.last_layer
= templ
.u
.tex
.first_layer
;
182 addr
.constants
.layer_offset
= surface
->u
.tex
.first_layer
;
185 sampler_view
= pipe
->create_sampler_view(pipe
, texture
, &templ
);
186 if (sampler_view
== NULL
)
189 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
191 pipe_sampler_view_reference(&sampler_view
, NULL
);
193 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, 1, samplers
);
196 /* Set up destination image */
198 struct pipe_image_view image
;
200 memset(&image
, 0, sizeof(image
));
201 image
.resource
= addr
.buffer
;
202 image
.format
= dst_format
;
203 image
.access
= PIPE_IMAGE_ACCESS_WRITE
;
204 image
.shader_access
= PIPE_IMAGE_ACCESS_WRITE
;
205 image
.u
.buf
.offset
= addr
.first_element
* addr
.bytes_per_pixel
;
206 image
.u
.buf
.size
= (addr
.last_element
- addr
.first_element
+ 1) *
207 addr
.bytes_per_pixel
;
209 cso_set_shader_images(cso
, PIPE_SHADER_FRAGMENT
, 0, 1, &image
);
212 /* Set up no-attachment framebuffer */
213 memset(&fb
, 0, sizeof(fb
));
214 fb
.width
= surface
->width
;
215 fb
.height
= surface
->height
;
218 cso_set_framebuffer(cso
, &fb
);
220 /* Any blend state would do. Set this just to prevent drivers having
223 cso_set_blend(cso
, &st
->pbo
.upload_blend
);
225 cso_set_viewport_dims(cso
, fb
.width
, fb
.height
, invert_y
);
228 st_pbo_addresses_invert_y(&addr
, fb
.height
);
231 struct pipe_depth_stencil_alpha_state dsa
;
232 memset(&dsa
, 0, sizeof(dsa
));
233 cso_set_depth_stencil_alpha(cso
, &dsa
);
236 /* Set up the fragment shader */
238 void *fs
= st_pbo_get_download_fs(st
, view_target
, src_format
, dst_format
);
242 cso_set_fragment_shader_handle(cso
, fs
);
245 success
= st_pbo_draw(st
, &addr
, fb
.width
, fb
.height
);
247 /* Buffer written via shader images needs explicit synchronization. */
248 pipe
->memory_barrier(pipe
, PIPE_BARRIER_ALL
);
251 cso_restore_state(cso
);
252 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
258 * Create a staging texture and blit the requested region to it.
260 static struct pipe_resource
*
261 blit_to_staging(struct st_context
*st
, struct st_renderbuffer
*strb
,
263 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
265 enum pipe_format src_format
, enum pipe_format dst_format
)
267 struct pipe_context
*pipe
= st
->pipe
;
268 struct pipe_screen
*screen
= pipe
->screen
;
269 struct pipe_resource dst_templ
;
270 struct pipe_resource
*dst
;
271 struct pipe_blit_info blit
;
273 /* We are creating a texture of the size of the region being read back.
274 * Need to check for NPOT texture support. */
275 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
276 (!util_is_power_of_two_or_zero(width
) ||
277 !util_is_power_of_two_or_zero(height
)))
280 /* create the destination texture */
281 memset(&dst_templ
, 0, sizeof(dst_templ
));
282 dst_templ
.target
= PIPE_TEXTURE_2D
;
283 dst_templ
.format
= dst_format
;
284 if (util_format_is_depth_or_stencil(dst_format
))
285 dst_templ
.bind
|= PIPE_BIND_DEPTH_STENCIL
;
287 dst_templ
.bind
|= PIPE_BIND_RENDER_TARGET
;
288 dst_templ
.usage
= PIPE_USAGE_STAGING
;
290 st_gl_texture_dims_to_pipe_dims(GL_TEXTURE_2D
, width
, height
, 1,
291 &dst_templ
.width0
, &dst_templ
.height0
,
292 &dst_templ
.depth0
, &dst_templ
.array_size
);
294 dst
= screen
->resource_create(screen
, &dst_templ
);
298 memset(&blit
, 0, sizeof(blit
));
299 blit
.src
.resource
= strb
->texture
;
300 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
301 blit
.src
.format
= src_format
;
302 blit
.dst
.resource
= dst
;
304 blit
.dst
.format
= dst
->format
;
309 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
311 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
312 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
313 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= 1;
314 blit
.mask
= st_get_blit_mask(strb
->Base
._BaseFormat
, format
);
315 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
316 blit
.scissor_enable
= FALSE
;
319 blit
.src
.box
.y
= strb
->Base
.Height
- blit
.src
.box
.y
;
320 blit
.src
.box
.height
= -blit
.src
.box
.height
;
324 st
->pipe
->blit(st
->pipe
, &blit
);
329 static struct pipe_resource
*
330 try_cached_readpixels(struct st_context
*st
, struct st_renderbuffer
*strb
,
332 GLsizei width
, GLsizei height
,
334 enum pipe_format src_format
, enum pipe_format dst_format
)
336 struct pipe_resource
*src
= strb
->texture
;
337 struct pipe_resource
*dst
= NULL
;
339 if (ST_DEBUG
& DEBUG_NOREADPIXCACHE
)
342 /* Reset cache after invalidation or switch of parameters. */
343 if (st
->readpix_cache
.src
!= src
||
344 st
->readpix_cache
.dst_format
!= dst_format
||
345 st
->readpix_cache
.level
!= strb
->surface
->u
.tex
.level
||
346 st
->readpix_cache
.layer
!= strb
->surface
->u
.tex
.first_layer
) {
347 pipe_resource_reference(&st
->readpix_cache
.src
, src
);
348 pipe_resource_reference(&st
->readpix_cache
.cache
, NULL
);
349 st
->readpix_cache
.dst_format
= dst_format
;
350 st
->readpix_cache
.level
= strb
->surface
->u
.tex
.level
;
351 st
->readpix_cache
.layer
= strb
->surface
->u
.tex
.first_layer
;
352 st
->readpix_cache
.hits
= 0;
355 /* Decide whether to trigger the cache. */
356 if (!st
->readpix_cache
.cache
) {
357 if (!strb
->use_readpix_cache
&& !ALWAYS_READPIXELS_CACHE
) {
358 /* Heuristic: If previous successive calls read at least a fraction
359 * of the surface _and_ we read again, trigger the cache.
361 unsigned threshold
= MAX2(1, strb
->Base
.Width
* strb
->Base
.Height
/ 8);
363 if (st
->readpix_cache
.hits
< threshold
) {
364 st
->readpix_cache
.hits
+= width
* height
;
368 strb
->use_readpix_cache
= true;
372 st
->readpix_cache
.cache
= blit_to_staging(st
, strb
, invert_y
,
375 strb
->Base
.Height
, format
,
376 src_format
, dst_format
);
379 /* Return an owning reference to stay consistent with the non-cached path */
380 pipe_resource_reference(&dst
, st
->readpix_cache
.cache
);
386 * This uses a blit to copy the read buffer to a texture format which matches
387 * the format and type combo and then a fast read-back is done using memcpy.
388 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
389 * a format which matches the swizzling.
391 * If such a format isn't available, we fall back to _mesa_readpixels.
393 * NOTE: Some drivers use a blit to convert between tiled and linear
394 * texture layouts during texture uploads/downloads, so the blit
395 * we do here should be free in such cases.
398 st_ReadPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
399 GLsizei width
, GLsizei height
,
400 GLenum format
, GLenum type
,
401 const struct gl_pixelstore_attrib
*pack
,
404 struct st_context
*st
= st_context(ctx
);
405 struct gl_renderbuffer
*rb
=
406 _mesa_get_read_renderbuffer_for_format(ctx
, format
);
407 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
408 struct pipe_context
*pipe
= st
->pipe
;
409 struct pipe_screen
*screen
= pipe
->screen
;
410 struct pipe_resource
*src
;
411 struct pipe_resource
*dst
= NULL
;
412 enum pipe_format dst_format
, src_format
;
414 struct pipe_transfer
*tex_xfer
;
418 /* Validate state (to be sure we have up-to-date framebuffer surfaces)
419 * and flush the bitmap cache prior to reading. */
420 st_validate_state(st
, ST_PIPELINE_UPDATE_FRAMEBUFFER
);
421 st_flush_bitmap_cache(st
);
423 if (!st
->prefer_blit_based_texture_transfer
) {
427 /* This must be done after state validation. */
430 /* XXX Fallback for depth-stencil formats due to an incomplete
431 * stencil blit implementation in some drivers. */
432 if (format
== GL_DEPTH_STENCIL
) {
436 /* If the base internal format and the texture format don't match, we have
437 * to use the slow path. */
438 if (rb
->_BaseFormat
!=
439 _mesa_get_format_base_format(rb
->Format
)) {
443 if (_mesa_readpixels_needs_slow_path(ctx
, format
, type
, GL_TRUE
)) {
447 /* Convert the source format to what is expected by ReadPixels
448 * and see if it's supported. */
449 src_format
= util_format_linear(src
->format
);
450 src_format
= util_format_luminance_to_red(src_format
);
451 src_format
= util_format_intensity_to_red(src_format
);
454 !screen
->is_format_supported(screen
, src_format
, src
->target
,
455 src
->nr_samples
, src
->nr_storage_samples
,
456 PIPE_BIND_SAMPLER_VIEW
)) {
460 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
461 bind
= PIPE_BIND_DEPTH_STENCIL
;
463 bind
= PIPE_BIND_RENDER_TARGET
;
465 /* Choose the destination format by finding the best match
466 * for the format+type combo. */
467 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
469 if (dst_format
== PIPE_FORMAT_NONE
) {
473 if (st
->pbo
.download_enabled
&& pack
->BufferObj
) {
474 if (try_pbo_readpixels(st
, strb
,
475 st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
,
477 src_format
, dst_format
,
482 if (needs_integer_signed_unsigned_conversion(ctx
, format
, type
)) {
486 /* Cache a staging texture for back-to-back ReadPixels, to avoid CPU-GPU
487 * synchronization overhead.
489 dst
= try_cached_readpixels(st
, strb
,
490 st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
,
491 width
, height
, format
, src_format
, dst_format
);
496 /* See if the texture format already matches the format and type,
497 * in which case the memcpy-based fast path will likely be used and
498 * we don't have to blit. */
499 if (_mesa_format_matches_format_and_type(rb
->Format
, format
,
500 type
, pack
->SwapBytes
, NULL
)) {
504 dst
= blit_to_staging(st
, strb
,
505 st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
,
506 x
, y
, width
, height
, format
,
507 src_format
, dst_format
);
516 pixels
= _mesa_map_pbo_dest(ctx
, pack
, pixels
);
518 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
519 dst_x
, dst_y
, 0, width
, height
, 1, &tex_xfer
);
521 _mesa_unmap_pbo_dest(ctx
, pack
);
522 pipe_resource_reference(&dst
, NULL
);
526 /* memcpy data into a user buffer */
528 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
529 const int destStride
= _mesa_image_row_stride(pack
, width
, format
, type
);
530 char *dest
= _mesa_image_address2d(pack
, pixels
,
531 width
, height
, format
,
534 if (tex_xfer
->stride
== bytesPerRow
&& destStride
== bytesPerRow
) {
535 memcpy(dest
, map
, bytesPerRow
* height
);
539 for (row
= 0; row
< (unsigned) height
; row
++) {
540 memcpy(dest
, map
, bytesPerRow
);
541 map
+= tex_xfer
->stride
;
547 pipe_transfer_unmap(pipe
, tex_xfer
);
548 _mesa_unmap_pbo_dest(ctx
, pack
);
549 pipe_resource_reference(&dst
, NULL
);
553 _mesa_readpixels(ctx
, x
, y
, width
, height
, format
, type
, pack
, pixels
);
556 void st_init_readpixels_functions(struct dd_function_table
*functions
)
558 functions
->ReadPixels
= st_ReadPixels
;