virgl: make sure bind is set for non-buffers
[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 virgl_copy_region_with_blit(ctx, resolve_tmp, 0, &dst_box, resource, level, box);
182 ctx->flush(ctx, NULL, 0);
183
184 void *ptr = texture_transfer_map_plain(ctx, resolve_tmp, 0, usage, &dst_box,
185 &trans->resolve_transfer);
186 if (!ptr)
187 goto fail;
188
189 *transfer = &trans->base;
190 trans->base.stride = trans->resolve_transfer->stride;
191 trans->base.layer_stride = trans->resolve_transfer->layer_stride;
192 return ptr;
193
194 fail:
195 pipe_resource_reference(&resolve_tmp, NULL);
196 virgl_resource_destroy_transfer(&vctx->transfer_pool, trans);
197 return NULL;
198 }
199
200 static void *virgl_texture_transfer_map(struct pipe_context *ctx,
201 struct pipe_resource *resource,
202 unsigned level,
203 unsigned usage,
204 const struct pipe_box *box,
205 struct pipe_transfer **transfer)
206 {
207 if (resource->nr_samples > 1)
208 return texture_transfer_map_resolve(ctx, resource, level, usage, box,
209 transfer);
210
211 return texture_transfer_map_plain(ctx, resource, level, usage, box, transfer);
212 }
213
214 static void flush_data(struct pipe_context *ctx,
215 struct virgl_transfer *trans,
216 const struct pipe_box *box)
217 {
218 struct virgl_winsys *vws = virgl_screen(ctx->screen)->vws;
219 vws->transfer_put(vws, virgl_resource(trans->base.resource)->hw_res, box,
220 trans->base.stride, trans->l_stride, trans->offset,
221 trans->base.level);
222 }
223
224 static void virgl_texture_transfer_unmap(struct pipe_context *ctx,
225 struct pipe_transfer *transfer)
226 {
227 struct virgl_context *vctx = virgl_context(ctx);
228 struct virgl_transfer *trans = virgl_transfer(transfer);
229 bool queue_unmap = false;
230
231 if (transfer->usage & PIPE_TRANSFER_WRITE &&
232 (transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT) == 0) {
233
234 if (trans->resolve_transfer) {
235 flush_data(ctx, virgl_transfer(trans->resolve_transfer),
236 &trans->resolve_transfer->box);
237
238 virgl_copy_region_with_blit(ctx,
239 trans->base.resource, trans->base.level,
240 &transfer->box,
241 trans->resolve_transfer->resource, 0,
242 &trans->resolve_transfer->box);
243 ctx->flush(ctx, NULL, 0);
244 } else
245 queue_unmap = true;
246 }
247
248 if (trans->resolve_transfer) {
249 pipe_resource_reference(&trans->resolve_transfer->resource, NULL);
250 virgl_resource_destroy_transfer(&vctx->transfer_pool,
251 virgl_transfer(trans->resolve_transfer));
252 }
253
254 if (queue_unmap)
255 virgl_transfer_queue_unmap(&vctx->queue, trans);
256 else
257 virgl_resource_destroy_transfer(&vctx->transfer_pool, trans);
258 }
259
260 static const struct u_resource_vtbl virgl_texture_vtbl =
261 {
262 virgl_resource_get_handle, /* get_handle */
263 virgl_resource_destroy, /* resource_destroy */
264 virgl_texture_transfer_map, /* transfer_map */
265 NULL, /* transfer_flush_region */
266 virgl_texture_transfer_unmap, /* transfer_unmap */
267 };
268
269 void virgl_texture_init(struct virgl_resource *res)
270 {
271 res->u.vtbl = &virgl_texture_vtbl;
272 }