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"
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"
39 #include "st_cb_fbo.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"
50 needs_integer_signed_unsigned_conversion(const struct gl_context
*ctx
,
51 GLenum format
, GLenum type
)
53 struct gl_renderbuffer
*rb
=
54 _mesa_get_read_renderbuffer_for_format(ctx
, format
);
58 GLenum srcType
= _mesa_get_format_datatype(rb
->Format
);
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
&&
75 try_pbo_readpixels(struct st_context
*st
, struct st_renderbuffer
*strb
,
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
)
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
;
91 if (texture
->nr_samples
> 1)
94 desc
= util_format_description(dst_format
);
96 /* Compute PBO addresses */
97 addr
.bytes_per_pixel
= desc
->block
.bits
/ 8;
101 addr
.height
= height
;
103 if (!st_pbo_addresses_pixelstore(st
, GL_TEXTURE_2D
, false, pack
, pixels
, &addr
))
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
|
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
);
120 /* Set up the sampler_view */
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
};
127 u_sampler_view_default_template(&templ
, texture
, src_format
);
129 switch (texture
->target
) {
130 case PIPE_TEXTURE_CUBE
:
131 case PIPE_TEXTURE_CUBE_ARRAY
:
132 view_target
= PIPE_TEXTURE_2D_ARRAY
;
135 view_target
= texture
->target
;
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
;
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
;
147 addr
.constants
.layer_offset
= surface
->u
.tex
.first_layer
;
150 sampler_view
= pipe
->create_sampler_view(pipe
, texture
, &templ
);
151 if (sampler_view
== NULL
)
154 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
156 pipe_sampler_view_reference(&sampler_view
, NULL
);
158 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, 1, samplers
);
161 /* Set up destination image */
163 struct pipe_image_view image
;
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
;
172 cso_set_shader_images(cso
, PIPE_SHADER_FRAGMENT
, 0, 1, &image
);
175 /* Set up no-attachment framebuffer */
176 memset(&fb
, 0, sizeof(fb
));
177 fb
.width
= surface
->width
;
178 fb
.height
= surface
->height
;
181 cso_set_framebuffer(cso
, &fb
);
183 cso_set_viewport_dims(cso
, fb
.width
, fb
.height
, invert_y
);
186 st_pbo_addresses_invert_y(&addr
, fb
.height
);
189 struct pipe_depth_stencil_alpha_state dsa
;
190 memset(&dsa
, 0, sizeof(dsa
));
191 cso_set_depth_stencil_alpha(cso
, &dsa
);
194 /* Set up the fragment shader */
196 void *fs
= st_pbo_get_download_fs(st
, view_target
);
200 cso_set_fragment_shader_handle(cso
, fs
);
203 success
= st_pbo_draw(st
, &addr
, fb
.width
, fb
.height
);
205 /* Buffer written via shader images needs explicit synchronization. */
206 pipe
->memory_barrier(pipe
, PIPE_BARRIER_ALL
);
209 cso_restore_state(cso
);
210 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
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.
221 * If such a format isn't available, we fall back to _mesa_readpixels.
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.
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
,
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
;
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
);
254 if (!st
->prefer_blit_based_texture_transfer
) {
258 /* This must be done after state validation. */
261 /* XXX Fallback for depth-stencil formats due to an incomplete
262 * stencil blit implementation in some drivers. */
263 if (format
== GL_DEPTH_STENCIL
) {
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
)) {
274 if (_mesa_readpixels_needs_slow_path(ctx
, format
, type
, GL_TRUE
)) {
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
);
285 !screen
->is_format_supported(screen
, src_format
, src
->target
,
287 PIPE_BIND_SAMPLER_VIEW
)) {
291 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
292 bind
|= PIPE_BIND_DEPTH_STENCIL
;
294 bind
|= PIPE_BIND_RENDER_TARGET
;
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
,
300 if (dst_format
== PIPE_FORMAT_NONE
) {
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
,
308 src_format
, dst_format
,
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
))) {
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
)) {
329 if (needs_integer_signed_unsigned_conversion(ctx
, format
, type
)) {
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
;
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
);
344 dst
= screen
->resource_create(screen
, &dst_templ
);
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
;
355 blit
.dst
.format
= dst
->format
;
360 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
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
;
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
;
375 st
->pipe
->blit(st
->pipe
, &blit
);
378 pixels
= _mesa_map_pbo_dest(ctx
, pack
, pixels
);
380 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
381 0, 0, 0, width
, height
, 1, &tex_xfer
);
383 _mesa_unmap_pbo_dest(ctx
, pack
);
384 pipe_resource_reference(&dst
, NULL
);
388 /* memcpy data into a user buffer */
390 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
393 for (row
= 0; row
< (unsigned) height
; row
++) {
394 void *dest
= _mesa_image_address2d(pack
, pixels
,
395 width
, height
, format
,
397 memcpy(dest
, map
, bytesPerRow
);
398 map
+= tex_xfer
->stride
;
402 pipe_transfer_unmap(pipe
, tex_xfer
);
403 _mesa_unmap_pbo_dest(ctx
, pack
);
404 pipe_resource_reference(&dst
, NULL
);
408 _mesa_readpixels(ctx
, x
, y
, width
, height
, format
, type
, pack
, pixels
);
411 void st_init_readpixels_functions(struct dd_function_table
*functions
)
413 functions
->ReadPixels
= st_ReadPixels
;