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 pipe_screen
*screen
= pipe
->screen
;
83 struct cso_context
*cso
= st
->cso_context
;
84 struct pipe_surface
*surface
= strb
->surface
;
85 struct pipe_resource
*texture
= strb
->texture
;
86 const struct util_format_description
*desc
;
87 struct st_pbo_addresses addr
;
88 struct pipe_framebuffer_state fb
;
89 enum pipe_texture_target view_target
;
92 if (texture
->nr_samples
> 1)
95 if (!screen
->is_format_supported(screen
, dst_format
, PIPE_TEXTURE_2D
,
97 PIPE_BIND_SHADER_IMAGE
))
100 desc
= util_format_description(dst_format
);
102 /* Compute PBO addresses */
103 addr
.bytes_per_pixel
= desc
->block
.bits
/ 8;
107 addr
.height
= height
;
109 if (!st_pbo_addresses_pixelstore(st
, GL_TEXTURE_2D
, false, pack
, pixels
, &addr
))
112 cso_save_state(cso
, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
113 CSO_BIT_FRAGMENT_SAMPLERS
|
114 CSO_BIT_FRAGMENT_IMAGE0
|
115 CSO_BIT_VERTEX_ELEMENTS
|
116 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
117 CSO_BIT_FRAMEBUFFER
|
120 CSO_BIT_DEPTH_STENCIL_ALPHA
|
121 CSO_BIT_STREAM_OUTPUTS
|
122 CSO_BIT_PAUSE_QUERIES
|
123 CSO_BITS_ALL_SHADERS
));
124 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
126 /* Set up the sampler_view */
128 struct pipe_sampler_view templ
;
129 struct pipe_sampler_view
*sampler_view
;
130 struct pipe_sampler_state sampler
= {0};
131 const struct pipe_sampler_state
*samplers
[1] = {&sampler
};
133 u_sampler_view_default_template(&templ
, texture
, src_format
);
135 switch (texture
->target
) {
136 case PIPE_TEXTURE_CUBE
:
137 case PIPE_TEXTURE_CUBE_ARRAY
:
138 view_target
= PIPE_TEXTURE_2D_ARRAY
;
141 view_target
= texture
->target
;
145 templ
.target
= view_target
;
146 templ
.u
.tex
.first_level
= surface
->u
.tex
.level
;
147 templ
.u
.tex
.last_level
= templ
.u
.tex
.first_level
;
149 if (view_target
!= PIPE_TEXTURE_3D
) {
150 templ
.u
.tex
.first_layer
= surface
->u
.tex
.first_layer
;
151 templ
.u
.tex
.last_layer
= templ
.u
.tex
.last_layer
;
153 addr
.constants
.layer_offset
= surface
->u
.tex
.first_layer
;
156 sampler_view
= pipe
->create_sampler_view(pipe
, texture
, &templ
);
157 if (sampler_view
== NULL
)
160 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
162 pipe_sampler_view_reference(&sampler_view
, NULL
);
164 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, 1, samplers
);
167 /* Set up destination image */
169 struct pipe_image_view image
;
171 memset(&image
, 0, sizeof(image
));
172 image
.resource
= addr
.buffer
;
173 image
.format
= dst_format
;
174 image
.access
= PIPE_IMAGE_ACCESS_WRITE
;
175 image
.u
.buf
.first_element
= addr
.first_element
;
176 image
.u
.buf
.last_element
= addr
.last_element
;
178 cso_set_shader_images(cso
, PIPE_SHADER_FRAGMENT
, 0, 1, &image
);
181 /* Set up no-attachment framebuffer */
182 memset(&fb
, 0, sizeof(fb
));
183 fb
.width
= surface
->width
;
184 fb
.height
= surface
->height
;
187 cso_set_framebuffer(cso
, &fb
);
189 cso_set_viewport_dims(cso
, fb
.width
, fb
.height
, invert_y
);
192 st_pbo_addresses_invert_y(&addr
, fb
.height
);
195 struct pipe_depth_stencil_alpha_state dsa
;
196 memset(&dsa
, 0, sizeof(dsa
));
197 cso_set_depth_stencil_alpha(cso
, &dsa
);
200 /* Set up the fragment shader */
202 void *fs
= st_pbo_get_download_fs(st
, view_target
);
206 cso_set_fragment_shader_handle(cso
, fs
);
209 success
= st_pbo_draw(st
, &addr
, fb
.width
, fb
.height
);
211 /* Buffer written via shader images needs explicit synchronization. */
212 pipe
->memory_barrier(pipe
, PIPE_BARRIER_ALL
);
215 cso_restore_state(cso
);
216 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
222 * This uses a blit to copy the read buffer to a texture format which matches
223 * the format and type combo and then a fast read-back is done using memcpy.
224 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
225 * a format which matches the swizzling.
227 * If such a format isn't available, we fall back to _mesa_readpixels.
229 * NOTE: Some drivers use a blit to convert between tiled and linear
230 * texture layouts during texture uploads/downloads, so the blit
231 * we do here should be free in such cases.
234 st_ReadPixels(struct gl_context
*ctx
, GLint x
, GLint y
,
235 GLsizei width
, GLsizei height
,
236 GLenum format
, GLenum type
,
237 const struct gl_pixelstore_attrib
*pack
,
240 struct st_context
*st
= st_context(ctx
);
241 struct gl_renderbuffer
*rb
=
242 _mesa_get_read_renderbuffer_for_format(ctx
, format
);
243 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
244 struct pipe_context
*pipe
= st
->pipe
;
245 struct pipe_screen
*screen
= pipe
->screen
;
246 struct pipe_resource
*src
;
247 struct pipe_resource
*dst
= NULL
;
248 struct pipe_resource dst_templ
;
249 enum pipe_format dst_format
, src_format
;
250 struct pipe_blit_info blit
;
251 unsigned bind
= PIPE_BIND_TRANSFER_READ
;
252 struct pipe_transfer
*tex_xfer
;
255 /* Validate state (to be sure we have up-to-date framebuffer surfaces)
256 * and flush the bitmap cache prior to reading. */
257 st_validate_state(st
, ST_PIPELINE_RENDER
);
258 st_flush_bitmap_cache(st
);
260 if (!st
->prefer_blit_based_texture_transfer
) {
264 /* This must be done after state validation. */
267 /* XXX Fallback for depth-stencil formats due to an incomplete
268 * stencil blit implementation in some drivers. */
269 if (format
== GL_DEPTH_STENCIL
) {
273 /* If the base internal format and the texture format don't match, we have
274 * to use the slow path. */
275 if (rb
->_BaseFormat
!=
276 _mesa_get_format_base_format(rb
->Format
)) {
280 if (_mesa_readpixels_needs_slow_path(ctx
, format
, type
, GL_TRUE
)) {
284 /* Convert the source format to what is expected by ReadPixels
285 * and see if it's supported. */
286 src_format
= util_format_linear(src
->format
);
287 src_format
= util_format_luminance_to_red(src_format
);
288 src_format
= util_format_intensity_to_red(src_format
);
291 !screen
->is_format_supported(screen
, src_format
, src
->target
,
293 PIPE_BIND_SAMPLER_VIEW
)) {
297 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
298 bind
|= PIPE_BIND_DEPTH_STENCIL
;
300 bind
|= PIPE_BIND_RENDER_TARGET
;
302 /* Choose the destination format by finding the best match
303 * for the format+type combo. */
304 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
306 if (dst_format
== PIPE_FORMAT_NONE
) {
310 if (st
->pbo
.download_enabled
&& _mesa_is_bufferobj(pack
->BufferObj
)) {
311 if (try_pbo_readpixels(st
, strb
,
312 st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
,
314 src_format
, dst_format
,
319 /* We are creating a texture of the size of the region being read back.
320 * Need to check for NPOT texture support. */
321 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
322 (!util_is_power_of_two(width
) ||
323 !util_is_power_of_two(height
))) {
327 /* See if the texture format already matches the format and type,
328 * in which case the memcpy-based fast path will likely be used and
329 * we don't have to blit. */
330 if (_mesa_format_matches_format_and_type(rb
->Format
, format
,
331 type
, pack
->SwapBytes
, NULL
)) {
335 if (needs_integer_signed_unsigned_conversion(ctx
, format
, type
)) {
339 /* create the destination texture */
340 memset(&dst_templ
, 0, sizeof(dst_templ
));
341 dst_templ
.target
= PIPE_TEXTURE_2D
;
342 dst_templ
.format
= dst_format
;
343 dst_templ
.bind
= bind
;
344 dst_templ
.usage
= PIPE_USAGE_STAGING
;
346 st_gl_texture_dims_to_pipe_dims(GL_TEXTURE_2D
, width
, height
, 1,
347 &dst_templ
.width0
, &dst_templ
.height0
,
348 &dst_templ
.depth0
, &dst_templ
.array_size
);
350 dst
= screen
->resource_create(screen
, &dst_templ
);
355 memset(&blit
, 0, sizeof(blit
));
356 blit
.src
.resource
= src
;
357 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
358 blit
.src
.format
= src_format
;
359 blit
.dst
.resource
= dst
;
361 blit
.dst
.format
= dst
->format
;
366 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
368 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
369 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
370 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= 1;
371 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, format
);
372 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
373 blit
.scissor_enable
= FALSE
;
375 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
376 blit
.src
.box
.y
= rb
->Height
- blit
.src
.box
.y
;
377 blit
.src
.box
.height
= -blit
.src
.box
.height
;
381 st
->pipe
->blit(st
->pipe
, &blit
);
384 pixels
= _mesa_map_pbo_dest(ctx
, pack
, pixels
);
386 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
387 0, 0, 0, width
, height
, 1, &tex_xfer
);
389 _mesa_unmap_pbo_dest(ctx
, pack
);
390 pipe_resource_reference(&dst
, NULL
);
394 /* memcpy data into a user buffer */
396 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
399 for (row
= 0; row
< (unsigned) height
; row
++) {
400 void *dest
= _mesa_image_address2d(pack
, pixels
,
401 width
, height
, format
,
403 memcpy(dest
, map
, bytesPerRow
);
404 map
+= tex_xfer
->stride
;
408 pipe_transfer_unmap(pipe
, tex_xfer
);
409 _mesa_unmap_pbo_dest(ctx
, pack
);
410 pipe_resource_reference(&dst
, NULL
);
414 _mesa_readpixels(ctx
, x
, y
, width
, height
, format
, type
, pack
, pixels
);
417 void st_init_readpixels_functions(struct dd_function_table
*functions
)
419 functions
->ReadPixels
= st_ReadPixels
;