st/mesa: fix resource leak in try_pbo_readpixels
[mesa.git] / src / mesa / state_tracker / st_cb_readpixels.c
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 #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"
38
39 #include "st_cb_fbo.h"
40 #include "st_atom.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"
48
49 static boolean
50 needs_integer_signed_unsigned_conversion(const struct gl_context *ctx,
51 GLenum format, GLenum type)
52 {
53 struct gl_renderbuffer *rb =
54 _mesa_get_read_renderbuffer_for_format(ctx, format);
55
56 assert(rb);
57
58 GLenum srcType = _mesa_get_format_datatype(rb->Format);
59
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 &&
65 (type == GL_INT ||
66 type == GL_SHORT ||
67 type == GL_BYTE))) {
68 return TRUE;
69 }
70
71 return FALSE;
72 }
73
74 static bool
75 try_pbo_readpixels(struct st_context *st, struct st_renderbuffer *strb,
76 bool invert_y,
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)
80 {
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;
90 bool success = false;
91
92 if (texture->nr_samples > 1)
93 return false;
94
95 if (!screen->is_format_supported(screen, dst_format, PIPE_TEXTURE_2D,
96 texture->nr_samples,
97 PIPE_BIND_SHADER_IMAGE))
98 return false;
99
100 desc = util_format_description(dst_format);
101
102 /* Compute PBO addresses */
103 addr.bytes_per_pixel = desc->block.bits / 8;
104 addr.xoffset = x;
105 addr.yoffset = y;
106 addr.width = width;
107 addr.height = height;
108 addr.depth = 1;
109 if (!st_pbo_addresses_pixelstore(st, GL_TEXTURE_2D, false, pack, pixels, &addr))
110 return false;
111
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 |
118 CSO_BIT_VIEWPORT |
119 CSO_BIT_RASTERIZER |
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);
125
126 /* Set up the sampler_view */
127 {
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};
132
133 u_sampler_view_default_template(&templ, texture, src_format);
134
135 switch (texture->target) {
136 case PIPE_TEXTURE_CUBE:
137 case PIPE_TEXTURE_CUBE_ARRAY:
138 view_target = PIPE_TEXTURE_2D_ARRAY;
139 break;
140 default:
141 view_target = texture->target;
142 break;
143 }
144
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;
148
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;
152 } else {
153 addr.constants.layer_offset = surface->u.tex.first_layer;
154 }
155
156 sampler_view = pipe->create_sampler_view(pipe, texture, &templ);
157 if (sampler_view == NULL)
158 goto fail;
159
160 cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, 1, &sampler_view);
161
162 pipe_sampler_view_reference(&sampler_view, NULL);
163
164 cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, 1, samplers);
165 }
166
167 /* Set up destination image */
168 {
169 struct pipe_image_view image;
170
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;
177
178 cso_set_shader_images(cso, PIPE_SHADER_FRAGMENT, 0, 1, &image);
179 }
180
181 /* Set up no-attachment framebuffer */
182 memset(&fb, 0, sizeof(fb));
183 fb.width = surface->width;
184 fb.height = surface->height;
185 fb.samples = 1;
186 fb.layers = 1;
187 cso_set_framebuffer(cso, &fb);
188
189 cso_set_viewport_dims(cso, fb.width, fb.height, invert_y);
190
191 if (invert_y)
192 st_pbo_addresses_invert_y(&addr, fb.height);
193
194 {
195 struct pipe_depth_stencil_alpha_state dsa;
196 memset(&dsa, 0, sizeof(dsa));
197 cso_set_depth_stencil_alpha(cso, &dsa);
198 }
199
200 /* Set up the fragment shader */
201 {
202 void *fs = st_pbo_get_download_fs(st, view_target);
203 if (!fs)
204 goto fail;
205
206 cso_set_fragment_shader_handle(cso, fs);
207 }
208
209 success = st_pbo_draw(st, &addr, fb.width, fb.height);
210
211 /* Buffer written via shader images needs explicit synchronization. */
212 pipe->memory_barrier(pipe, PIPE_BARRIER_ALL);
213
214 fail:
215 cso_restore_state(cso);
216 cso_restore_constant_buffer_slot0(cso, PIPE_SHADER_FRAGMENT);
217
218 return success;
219 }
220
221 /**
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.
226 *
227 * If such a format isn't available, we fall back to _mesa_readpixels.
228 *
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.
232 */
233 static void
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,
238 void *pixels)
239 {
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;
253 ubyte *map = NULL;
254
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);
259
260 if (!st->prefer_blit_based_texture_transfer) {
261 goto fallback;
262 }
263
264 /* This must be done after state validation. */
265 src = strb->texture;
266
267 /* XXX Fallback for depth-stencil formats due to an incomplete
268 * stencil blit implementation in some drivers. */
269 if (format == GL_DEPTH_STENCIL) {
270 goto fallback;
271 }
272
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)) {
277 goto fallback;
278 }
279
280 if (_mesa_readpixels_needs_slow_path(ctx, format, type, GL_TRUE)) {
281 goto fallback;
282 }
283
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);
289
290 if (!src_format ||
291 !screen->is_format_supported(screen, src_format, src->target,
292 src->nr_samples,
293 PIPE_BIND_SAMPLER_VIEW)) {
294 goto fallback;
295 }
296
297 if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
298 bind |= PIPE_BIND_DEPTH_STENCIL;
299 else
300 bind |= PIPE_BIND_RENDER_TARGET;
301
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,
305 pack->SwapBytes);
306 if (dst_format == PIPE_FORMAT_NONE) {
307 goto fallback;
308 }
309
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,
313 x, y, width, height,
314 src_format, dst_format,
315 pack, pixels))
316 return;
317 }
318
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))) {
324 goto fallback;
325 }
326
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)) {
332 goto fallback;
333 }
334
335 if (needs_integer_signed_unsigned_conversion(ctx, format, type)) {
336 goto fallback;
337 }
338
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;
345
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);
349
350 dst = screen->resource_create(screen, &dst_templ);
351 if (!dst) {
352 goto fallback;
353 }
354
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;
360 blit.dst.level = 0;
361 blit.dst.format = dst->format;
362 blit.src.box.x = x;
363 blit.dst.box.x = 0;
364 blit.src.box.y = y;
365 blit.dst.box.y = 0;
366 blit.src.box.z = strb->surface->u.tex.first_layer;
367 blit.dst.box.z = 0;
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;
374
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;
378 }
379
380 /* blit */
381 st->pipe->blit(st->pipe, &blit);
382
383 /* map resources */
384 pixels = _mesa_map_pbo_dest(ctx, pack, pixels);
385
386 map = pipe_transfer_map_3d(pipe, dst, 0, PIPE_TRANSFER_READ,
387 0, 0, 0, width, height, 1, &tex_xfer);
388 if (!map) {
389 _mesa_unmap_pbo_dest(ctx, pack);
390 pipe_resource_reference(&dst, NULL);
391 goto fallback;
392 }
393
394 /* memcpy data into a user buffer */
395 {
396 const uint bytesPerRow = width * util_format_get_blocksize(dst_format);
397 GLuint row;
398
399 for (row = 0; row < (unsigned) height; row++) {
400 void *dest = _mesa_image_address2d(pack, pixels,
401 width, height, format,
402 type, row, 0);
403 memcpy(dest, map, bytesPerRow);
404 map += tex_xfer->stride;
405 }
406 }
407
408 pipe_transfer_unmap(pipe, tex_xfer);
409 _mesa_unmap_pbo_dest(ctx, pack);
410 pipe_resource_reference(&dst, NULL);
411 return;
412
413 fallback:
414 _mesa_readpixels(ctx, x, y, width, height, format, type, pack, pixels);
415 }
416
417 void st_init_readpixels_functions(struct dd_function_table *functions)
418 {
419 functions->ReadPixels = st_ReadPixels;
420 }