virgl: only blit if resource is read
[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 assert(src_box->width == dst_box->width);
42 assert(src_box->height == dst_box->height);
43 assert(src_box->depth == dst_box->depth);
44
45 memset(&blit, 0, sizeof(blit));
46 blit.src.resource = src;
47 blit.src.format = src->format;
48 blit.src.level = src_level;
49 blit.src.box = *src_box;
50 blit.dst.resource = dst;
51 blit.dst.format = dst->format;
52 blit.dst.level = dst_level;
53 blit.dst.box.x = dst_box->x;
54 blit.dst.box.y = dst_box->y;
55 blit.dst.box.z = dst_box->z;
56 blit.dst.box.width = src_box->width;
57 blit.dst.box.height = src_box->height;
58 blit.dst.box.depth = src_box->depth;
59 blit.mask = util_format_get_mask(src->format) &
60 util_format_get_mask(dst->format);
61 blit.filter = PIPE_TEX_FILTER_NEAREST;
62
63 if (blit.mask) {
64 pipe->blit(pipe, &blit);
65 }
66 }
67
68 static unsigned temp_bind(unsigned orig)
69 {
70 unsigned warn = ~(PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL |
71 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DISPLAY_TARGET);
72 if (orig & warn)
73 debug_printf("VIRGL: Warning, possibly unhandled bind: %x\n",
74 orig & warn);
75
76 return orig & (PIPE_BIND_DEPTH_STENCIL | PIPE_BIND_RENDER_TARGET);
77 }
78
79 static void virgl_init_temp_resource_from_box(struct pipe_resource *res,
80 struct pipe_resource *orig,
81 const struct pipe_box *box,
82 unsigned level, unsigned flags)
83 {
84 memset(res, 0, sizeof(*res));
85 res->bind = temp_bind(orig->bind);
86 res->format = orig->format;
87 res->width0 = box->width;
88 res->height0 = box->height;
89 res->depth0 = 1;
90 res->array_size = 1;
91 res->usage = PIPE_USAGE_STAGING;
92 res->flags = flags;
93
94 /* We must set the correct texture target and dimensions for a 3D box. */
95 if (box->depth > 1 && util_max_layer(orig, level) > 0)
96 res->target = orig->target;
97 else
98 res->target = PIPE_TEXTURE_2D;
99
100 if (res->target != PIPE_BUFFER)
101 res->bind = PIPE_BIND_RENDER_TARGET;
102
103 switch (res->target) {
104 case PIPE_TEXTURE_1D_ARRAY:
105 case PIPE_TEXTURE_2D_ARRAY:
106 case PIPE_TEXTURE_CUBE_ARRAY:
107 res->array_size = box->depth;
108 break;
109 case PIPE_TEXTURE_3D:
110 res->depth0 = box->depth;
111 break;
112 default:
113 break;
114 }
115 }
116
117 static void *texture_transfer_map_plain(struct pipe_context *ctx,
118 struct pipe_resource *resource,
119 unsigned level,
120 unsigned usage,
121 const struct pipe_box *box,
122 struct pipe_transfer **transfer)
123 {
124 struct virgl_context *vctx = virgl_context(ctx);
125 struct virgl_winsys *vws = virgl_screen(ctx->screen)->vws;
126 struct virgl_resource *vtex = virgl_resource(resource);
127 struct virgl_transfer *trans;
128
129 trans = virgl_resource_create_transfer(&vctx->transfer_pool, resource,
130 &vtex->metadata, level, usage, box);
131 trans->resolve_transfer = NULL;
132
133 assert(resource->nr_samples <= 1);
134
135 if (virgl_res_needs_flush(vctx, trans))
136 ctx->flush(ctx, NULL, 0);
137
138 if (virgl_res_needs_readback(vctx, vtex, usage, level)) {
139 vws->transfer_get(vws, vtex->hw_res, box, trans->base.stride,
140 trans->l_stride, trans->offset, level);
141
142 vws->resource_wait(vws, vtex->hw_res);
143 }
144
145 void *ptr = vws->resource_map(vws, vtex->hw_res);
146 if (!ptr) {
147 virgl_resource_destroy_transfer(&vctx->transfer_pool, trans);
148 return NULL;
149 }
150
151 *transfer = &trans->base;
152 return ptr + trans->offset;
153 }
154
155 static void *texture_transfer_map_resolve(struct pipe_context *ctx,
156 struct pipe_resource *resource,
157 unsigned level,
158 unsigned usage,
159 const struct pipe_box *box,
160 struct pipe_transfer **transfer)
161 {
162 struct virgl_context *vctx = virgl_context(ctx);
163 struct pipe_resource templ, *resolve_tmp;
164 struct virgl_transfer *trans;
165
166 trans = virgl_resource_create_transfer(&vctx->transfer_pool, resource,
167 &virgl_resource(resource)->metadata,
168 level, usage, box);
169 if (!trans)
170 return NULL;
171
172 virgl_init_temp_resource_from_box(&templ, resource, box, level, 0);
173
174 resolve_tmp = ctx->screen->resource_create(ctx->screen, &templ);
175 if (!resolve_tmp)
176 return NULL;
177
178 struct pipe_box dst_box = *box;
179 dst_box.x = dst_box.y = dst_box.z = 0;
180
181 if (usage & PIPE_TRANSFER_READ) {
182 virgl_copy_region_with_blit(ctx, resolve_tmp, 0, &dst_box, resource,
183 level, box);
184 ctx->flush(ctx, NULL, 0);
185 }
186
187 void *ptr = texture_transfer_map_plain(ctx, resolve_tmp, 0, usage, &dst_box,
188 &trans->resolve_transfer);
189 if (!ptr)
190 goto fail;
191
192 *transfer = &trans->base;
193 trans->base.stride = trans->resolve_transfer->stride;
194 trans->base.layer_stride = trans->resolve_transfer->layer_stride;
195 return ptr;
196
197 fail:
198 pipe_resource_reference(&resolve_tmp, NULL);
199 virgl_resource_destroy_transfer(&vctx->transfer_pool, trans);
200 return NULL;
201 }
202
203 static void *virgl_texture_transfer_map(struct pipe_context *ctx,
204 struct pipe_resource *resource,
205 unsigned level,
206 unsigned usage,
207 const struct pipe_box *box,
208 struct pipe_transfer **transfer)
209 {
210 if (resource->nr_samples > 1)
211 return texture_transfer_map_resolve(ctx, resource, level, usage, box,
212 transfer);
213
214 return texture_transfer_map_plain(ctx, resource, level, usage, box, transfer);
215 }
216
217 static void flush_data(struct pipe_context *ctx,
218 struct virgl_transfer *trans,
219 const struct pipe_box *box)
220 {
221 struct virgl_winsys *vws = virgl_screen(ctx->screen)->vws;
222 vws->transfer_put(vws, virgl_resource(trans->base.resource)->hw_res, box,
223 trans->base.stride, trans->l_stride, trans->offset,
224 trans->base.level);
225 }
226
227 static void virgl_texture_transfer_unmap(struct pipe_context *ctx,
228 struct pipe_transfer *transfer)
229 {
230 struct virgl_context *vctx = virgl_context(ctx);
231 struct virgl_transfer *trans = virgl_transfer(transfer);
232 bool queue_unmap = false;
233
234 if (transfer->usage & PIPE_TRANSFER_WRITE &&
235 (transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT) == 0) {
236
237 if (trans->resolve_transfer) {
238 flush_data(ctx, virgl_transfer(trans->resolve_transfer),
239 &trans->resolve_transfer->box);
240
241 virgl_copy_region_with_blit(ctx,
242 trans->base.resource, trans->base.level,
243 &transfer->box,
244 trans->resolve_transfer->resource, 0,
245 &trans->resolve_transfer->box);
246 ctx->flush(ctx, NULL, 0);
247 } else
248 queue_unmap = true;
249 }
250
251 if (trans->resolve_transfer) {
252 pipe_resource_reference(&trans->resolve_transfer->resource, NULL);
253 virgl_resource_destroy_transfer(&vctx->transfer_pool,
254 virgl_transfer(trans->resolve_transfer));
255 }
256
257 if (queue_unmap)
258 virgl_transfer_queue_unmap(&vctx->queue, trans);
259 else
260 virgl_resource_destroy_transfer(&vctx->transfer_pool, trans);
261 }
262
263 static const struct u_resource_vtbl virgl_texture_vtbl =
264 {
265 virgl_resource_get_handle, /* get_handle */
266 virgl_resource_destroy, /* resource_destroy */
267 virgl_texture_transfer_map, /* transfer_map */
268 NULL, /* transfer_flush_region */
269 virgl_texture_transfer_unmap, /* transfer_unmap */
270 };
271
272 void virgl_texture_init(struct virgl_resource *res)
273 {
274 res->u.vtbl = &virgl_texture_vtbl;
275 }