virgl: Use buffer copy transfers to avoid waiting when mapping
[mesa.git] / src / gallium / drivers / virgl / virgl_texture.c
1 /*
2 * Copyright 2014, 2015 Red Hat.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #include "util/u_format.h"
24 #include "util/u_inlines.h"
25 #include "util/u_memory.h"
26
27 #include "virgl_context.h"
28 #include "virgl_resource.h"
29 #include "virgl_screen.h"
30
31 static void virgl_copy_region_with_blit(struct pipe_context *pipe,
32 struct pipe_resource *dst,
33 unsigned dst_level,
34 const struct pipe_box *dst_box,
35 struct pipe_resource *src,
36 unsigned src_level,
37 const struct pipe_box *src_box)
38 {
39 struct pipe_blit_info blit;
40
41 memset(&blit, 0, sizeof(blit));
42 blit.src.resource = src;
43 blit.src.format = src->format;
44 blit.src.level = src_level;
45 blit.src.box = *src_box;
46 blit.dst.resource = dst;
47 blit.dst.format = dst->format;
48 blit.dst.level = dst_level;
49 blit.dst.box.x = dst_box->x;
50 blit.dst.box.y = dst_box->y;
51 blit.dst.box.z = dst_box->z;
52 blit.dst.box.width = dst_box->width;
53 blit.dst.box.height = dst_box->height;
54 blit.dst.box.depth = dst_box->depth;
55 blit.mask = util_format_get_mask(src->format) &
56 util_format_get_mask(dst->format);
57 blit.filter = PIPE_TEX_FILTER_NEAREST;
58
59 if (blit.mask) {
60 pipe->blit(pipe, &blit);
61 }
62 }
63
64 static unsigned temp_bind(unsigned orig)
65 {
66 unsigned warn = ~(PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL |
67 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DISPLAY_TARGET);
68 if (orig & warn)
69 debug_printf("VIRGL: Warning, possibly unhandled bind: %x\n",
70 orig & warn);
71
72 return orig & (PIPE_BIND_DEPTH_STENCIL | PIPE_BIND_RENDER_TARGET);
73 }
74
75 static void virgl_init_temp_resource_from_box(struct pipe_resource *res,
76 struct pipe_resource *orig,
77 const struct pipe_box *box,
78 unsigned level, unsigned flags,
79 enum pipe_format fmt)
80 {
81 memset(res, 0, sizeof(*res));
82 res->bind = temp_bind(orig->bind);
83 res->format = fmt;
84 res->width0 = box->width;
85 res->height0 = box->height;
86 res->depth0 = 1;
87 res->array_size = 1;
88 res->usage = PIPE_USAGE_STAGING;
89 res->flags = flags;
90
91 /* We must set the correct texture target and dimensions for a 3D box. */
92 if (box->depth > 1 && util_max_layer(orig, level) > 0)
93 res->target = orig->target;
94 else
95 res->target = PIPE_TEXTURE_2D;
96
97 if (res->target != PIPE_BUFFER)
98 res->bind = PIPE_BIND_RENDER_TARGET;
99
100 switch (res->target) {
101 case PIPE_TEXTURE_1D_ARRAY:
102 case PIPE_TEXTURE_2D_ARRAY:
103 case PIPE_TEXTURE_CUBE_ARRAY:
104 res->array_size = box->depth;
105 break;
106 case PIPE_TEXTURE_3D:
107 res->depth0 = box->depth;
108 break;
109 default:
110 break;
111 }
112 }
113
114 static void *texture_transfer_map_plain(struct pipe_context *ctx,
115 struct pipe_resource *resource,
116 unsigned level,
117 unsigned usage,
118 const struct pipe_box *box,
119 struct pipe_transfer **transfer)
120 {
121 struct virgl_context *vctx = virgl_context(ctx);
122 struct virgl_winsys *vws = virgl_screen(ctx->screen)->vws;
123 struct virgl_resource *vtex = virgl_resource(resource);
124 struct virgl_transfer *trans;
125 enum virgl_transfer_map_type map_type;
126
127 trans = virgl_resource_create_transfer(&vctx->transfer_pool, resource,
128 &vtex->metadata, level, usage, box);
129 trans->resolve_transfer = NULL;
130
131 assert(resource->nr_samples <= 1);
132
133 map_type = virgl_resource_transfer_prepare(vctx, trans);
134 switch (map_type) {
135 case VIRGL_TRANSFER_MAP_HW_RES:
136 trans->hw_res_map = vws->resource_map(vws, vtex->hw_res);
137 break;
138 case VIRGL_TRANSFER_MAP_ERROR:
139 default:
140 trans->hw_res_map = NULL;
141 break;
142 }
143
144 if (!trans->hw_res_map) {
145 virgl_resource_destroy_transfer(&vctx->transfer_pool, trans);
146 return NULL;
147 }
148
149 *transfer = &trans->base;
150 return trans->hw_res_map + trans->offset;
151 }
152
153 static void *texture_transfer_map_resolve(struct pipe_context *ctx,
154 struct pipe_resource *resource,
155 unsigned level,
156 unsigned usage,
157 const struct pipe_box *box,
158 struct pipe_transfer **transfer)
159 {
160 struct virgl_context *vctx = virgl_context(ctx);
161 struct virgl_resource *vtex = virgl_resource(resource);
162 struct pipe_resource templ, *resolve_tmp;
163 struct virgl_transfer *trans;
164
165 trans = virgl_resource_create_transfer(&vctx->transfer_pool, resource,
166 &vtex->metadata, level, usage, box);
167 if (!trans)
168 return NULL;
169
170 enum pipe_format fmt = resource->format;
171 if (!virgl_has_readback_format(ctx->screen, fmt)) {
172 if (util_format_fits_8unorm(util_format_description(fmt)))
173 fmt = PIPE_FORMAT_R8G8B8A8_UNORM;
174 else if (util_format_is_pure_sint(fmt))
175 fmt = PIPE_FORMAT_R32G32B32A32_SINT;
176 else if (util_format_is_pure_uint(fmt))
177 fmt = PIPE_FORMAT_R32G32B32A32_UINT;
178 else
179 fmt = PIPE_FORMAT_R32G32B32A32_FLOAT;
180 assert(virgl_has_readback_format(ctx->screen, fmt));
181 }
182
183 struct pipe_box dst_box = *box;
184 dst_box.x = dst_box.y = dst_box.z = 0;
185 if (usage & PIPE_TRANSFER_READ) {
186 /* readback should scale to the block size */
187 dst_box.width = align(dst_box.width,
188 util_format_get_blockwidth(resource->format));
189 dst_box.height = align(dst_box.height,
190 util_format_get_blockheight(resource->format));
191 }
192
193 virgl_init_temp_resource_from_box(&templ, resource, &dst_box, level, 0, fmt);
194
195 resolve_tmp = ctx->screen->resource_create(ctx->screen, &templ);
196 if (!resolve_tmp)
197 return NULL;
198
199 if (usage & PIPE_TRANSFER_READ) {
200 virgl_copy_region_with_blit(ctx, resolve_tmp, 0, &dst_box, resource,
201 level, box);
202 ctx->flush(ctx, NULL, 0);
203 }
204
205 void *ptr = texture_transfer_map_plain(ctx, resolve_tmp, 0, usage, &dst_box,
206 &trans->resolve_transfer);
207 if (!ptr)
208 goto fail;
209
210 *transfer = &trans->base;
211 if (fmt == resource->format) {
212 trans->base.stride = trans->resolve_transfer->stride;
213 trans->base.layer_stride = trans->resolve_transfer->layer_stride;
214 return ptr;
215 } else {
216 if (usage & PIPE_TRANSFER_READ) {
217 struct virgl_winsys *vws = virgl_screen(ctx->screen)->vws;
218 void *src = ptr;
219 ptr = vws->resource_map(vws, vtex->hw_res);
220 if (!ptr)
221 goto fail;
222
223 if (!util_format_translate_3d(resource->format,
224 ptr + vtex->metadata.level_offset[level],
225 trans->base.stride,
226 trans->base.layer_stride,
227 box->x, box->y, box->z,
228 fmt,
229 src,
230 trans->resolve_transfer->stride,
231 trans->resolve_transfer->layer_stride,
232 0, 0, 0,
233 dst_box.width,
234 dst_box.height,
235 dst_box.depth)) {
236 debug_printf("failed to translate format %s to %s\n",
237 util_format_short_name(fmt),
238 util_format_short_name(resource->format));
239 goto fail;
240 }
241 }
242
243 if ((usage & PIPE_TRANSFER_WRITE) == 0)
244 pipe_resource_reference(&trans->resolve_transfer->resource, NULL);
245
246 return ptr + trans->offset;
247 }
248
249 fail:
250 pipe_resource_reference(&resolve_tmp, NULL);
251 virgl_resource_destroy_transfer(&vctx->transfer_pool, trans);
252 return NULL;
253 }
254
255 static bool needs_resolve(struct pipe_screen *screen,
256 struct pipe_resource *resource, unsigned usage)
257 {
258 if (resource->nr_samples > 1)
259 return true;
260
261 if (usage & PIPE_TRANSFER_READ)
262 return !util_format_is_depth_or_stencil(resource->format) &&
263 !virgl_has_readback_format(screen, resource->format);
264
265 return false;
266 }
267
268 static void *virgl_texture_transfer_map(struct pipe_context *ctx,
269 struct pipe_resource *resource,
270 unsigned level,
271 unsigned usage,
272 const struct pipe_box *box,
273 struct pipe_transfer **transfer)
274 {
275 if (needs_resolve(ctx->screen, resource, usage))
276 return texture_transfer_map_resolve(ctx, resource, level, usage, box,
277 transfer);
278
279 return texture_transfer_map_plain(ctx, resource, level, usage, box, transfer);
280 }
281
282 static void flush_data(struct pipe_context *ctx,
283 struct virgl_transfer *trans,
284 const struct pipe_box *box)
285 {
286 struct virgl_winsys *vws = virgl_screen(ctx->screen)->vws;
287 vws->transfer_put(vws, virgl_resource(trans->base.resource)->hw_res, box,
288 trans->base.stride, trans->l_stride, trans->offset,
289 trans->base.level);
290 }
291
292 static void virgl_texture_transfer_unmap(struct pipe_context *ctx,
293 struct pipe_transfer *transfer)
294 {
295 struct virgl_context *vctx = virgl_context(ctx);
296 struct virgl_transfer *trans = virgl_transfer(transfer);
297 bool queue_unmap = false;
298
299 if (transfer->usage & PIPE_TRANSFER_WRITE &&
300 (transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT) == 0) {
301
302 if (trans->resolve_transfer && (trans->base.resource->format ==
303 trans->resolve_transfer->resource->format)) {
304 flush_data(ctx, virgl_transfer(trans->resolve_transfer),
305 &trans->resolve_transfer->box);
306
307 /* FINISHME: In case the destination format isn't renderable here, the
308 * blit here will currently fail. This could for instance happen if the
309 * mapped resource is of a compressed format, and it's mapped with both
310 * read and write usage.
311 */
312
313 virgl_copy_region_with_blit(ctx,
314 trans->base.resource, trans->base.level,
315 &transfer->box,
316 trans->resolve_transfer->resource, 0,
317 &trans->resolve_transfer->box);
318 ctx->flush(ctx, NULL, 0);
319 } else
320 queue_unmap = true;
321 }
322
323 if (trans->resolve_transfer) {
324 pipe_resource_reference(&trans->resolve_transfer->resource, NULL);
325 virgl_resource_destroy_transfer(&vctx->transfer_pool,
326 virgl_transfer(trans->resolve_transfer));
327 }
328
329 if (queue_unmap)
330 virgl_transfer_queue_unmap(&vctx->queue, trans);
331 else
332 virgl_resource_destroy_transfer(&vctx->transfer_pool, trans);
333 }
334
335 static const struct u_resource_vtbl virgl_texture_vtbl =
336 {
337 virgl_resource_get_handle, /* get_handle */
338 virgl_resource_destroy, /* resource_destroy */
339 virgl_texture_transfer_map, /* transfer_map */
340 NULL, /* transfer_flush_region */
341 virgl_texture_transfer_unmap, /* transfer_unmap */
342 };
343
344 void virgl_texture_init(struct virgl_resource *res)
345 {
346 res->u.vtbl = &virgl_texture_vtbl;
347 }