virgl: rewrite core of virgl_texture_transfer_map
[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 unsigned dstx, unsigned dsty, unsigned dstz,
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 = dstx;
50 blit.dst.box.y = dsty;
51 blit.dst.box.z = dstz;
52 blit.dst.box.width = src_box->width;
53 blit.dst.box.height = src_box->height;
54 blit.dst.box.depth = src_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 {
80 memset(res, 0, sizeof(*res));
81 res->bind = temp_bind(orig->bind);
82 res->format = orig->format;
83 res->width0 = box->width;
84 res->height0 = box->height;
85 res->depth0 = 1;
86 res->array_size = 1;
87 res->usage = PIPE_USAGE_STAGING;
88 res->flags = flags;
89
90 /* We must set the correct texture target and dimensions for a 3D box. */
91 if (box->depth > 1 && util_max_layer(orig, level) > 0)
92 res->target = orig->target;
93 else
94 res->target = PIPE_TEXTURE_2D;
95
96 switch (res->target) {
97 case PIPE_TEXTURE_1D_ARRAY:
98 case PIPE_TEXTURE_2D_ARRAY:
99 case PIPE_TEXTURE_CUBE_ARRAY:
100 res->array_size = box->depth;
101 break;
102 case PIPE_TEXTURE_3D:
103 res->depth0 = box->depth;
104 break;
105 default:
106 break;
107 }
108 }
109
110 static void *texture_transfer_map_plain(struct pipe_context *ctx,
111 struct pipe_resource *resource,
112 unsigned level,
113 unsigned usage,
114 const struct pipe_box *box,
115 struct pipe_transfer **transfer)
116 {
117 struct virgl_context *vctx = virgl_context(ctx);
118 struct virgl_winsys *vws = virgl_screen(ctx->screen)->vws;
119 struct virgl_resource *vtex = virgl_resource(resource);
120 struct virgl_transfer *trans;
121
122 trans = virgl_resource_create_transfer(&vctx->transfer_pool, resource,
123 &vtex->metadata, level, usage, box);
124 trans->resolve_tmp = NULL;
125
126 assert(resource->nr_samples <= 1);
127
128 if (virgl_res_needs_flush(vctx, trans))
129 ctx->flush(ctx, NULL, 0);
130
131 if (virgl_res_needs_readback(vctx, vtex, usage, level)) {
132 vws->transfer_get(vws, vtex->hw_res, box, trans->base.stride,
133 trans->l_stride, trans->offset, level);
134
135 vws->resource_wait(vws, vtex->hw_res);
136 }
137
138 void *ptr = vws->resource_map(vws, vtex->hw_res);
139 if (!ptr) {
140 virgl_resource_destroy_transfer(&vctx->transfer_pool, trans);
141 return NULL;
142 }
143
144 *transfer = &trans->base;
145 return ptr + trans->offset;
146 }
147
148 static void *texture_transfer_map_resolve(struct pipe_context *ctx,
149 struct pipe_resource *resource,
150 unsigned level,
151 unsigned usage,
152 const struct pipe_box *box,
153 struct pipe_transfer **transfer)
154 {
155 struct pipe_resource templ, *resolve_tmp;
156 virgl_init_temp_resource_from_box(&templ, resource, box, level, 0);
157
158 resolve_tmp = ctx->screen->resource_create(ctx->screen, &templ);
159 if (!resolve_tmp)
160 return NULL;
161
162 virgl_copy_region_with_blit(ctx, resolve_tmp, 0, 0, 0, 0, resource, level, box);
163 ctx->flush(ctx, NULL, 0);
164
165 void *ptr = texture_transfer_map_plain(ctx, resolve_tmp, 0, usage, box,
166 transfer);
167 if (!ptr) {
168 pipe_resource_reference(&resolve_tmp, NULL);
169 return NULL;
170 }
171
172 virgl_transfer(*transfer)->resolve_tmp = virgl_resource(resolve_tmp);
173
174 struct virgl_resource_metadata *data = &virgl_resource(resolve_tmp)->metadata;
175 assert((*transfer)->stride == data->stride[0]);
176 assert((*transfer)->layer_stride == data->layer_stride[0]);
177 assert(virgl_transfer(*transfer)->offset == 0);
178
179 return ptr;
180 }
181
182 static void *virgl_texture_transfer_map(struct pipe_context *ctx,
183 struct pipe_resource *resource,
184 unsigned level,
185 unsigned usage,
186 const struct pipe_box *box,
187 struct pipe_transfer **transfer)
188 {
189 if (resource->nr_samples > 1)
190 return texture_transfer_map_resolve(ctx, resource, level, usage, box,
191 transfer);
192
193 return texture_transfer_map_plain(ctx, resource, level, usage, box, transfer);
194 }
195
196 static void virgl_texture_transfer_unmap(struct pipe_context *ctx,
197 struct pipe_transfer *transfer)
198 {
199 struct virgl_context *vctx = virgl_context(ctx);
200 struct virgl_transfer *trans = virgl_transfer(transfer);
201 struct virgl_resource *vtex = virgl_resource(transfer->resource);
202 bool queue_unmap = false;
203
204 if (transfer->usage & PIPE_TRANSFER_WRITE &&
205 (transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT) == 0) {
206 if (trans->resolve_tmp) {
207 struct virgl_winsys *vws = virgl_screen(ctx->screen)->vws;
208 vws->transfer_put(vws, vtex->hw_res, &transfer->box,
209 trans->base.stride, trans->l_stride,
210 trans->offset, transfer->level);
211 } else
212 queue_unmap = true;
213 }
214
215 pipe_resource_reference((struct pipe_resource **)&trans->resolve_tmp, NULL);
216
217 if (queue_unmap)
218 virgl_transfer_queue_unmap(&vctx->queue, trans);
219 else
220 virgl_resource_destroy_transfer(&vctx->transfer_pool, trans);
221 }
222
223 static const struct u_resource_vtbl virgl_texture_vtbl =
224 {
225 virgl_resource_get_handle, /* get_handle */
226 virgl_resource_destroy, /* resource_destroy */
227 virgl_texture_transfer_map, /* transfer_map */
228 NULL, /* transfer_flush_region */
229 virgl_texture_transfer_unmap, /* transfer_unmap */
230 };
231
232 void virgl_texture_init(struct virgl_resource *res)
233 {
234 res->u.vtbl = &virgl_texture_vtbl;
235 }