virgl: unsigned int -> unsigned
[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 if (orig & ~(PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL |
67 PIPE_BIND_SAMPLER_VIEW))
68 fprintf(stderr, "Waring, possibly unhandled bind: %x\n", orig);
69 return orig & (PIPE_BIND_DEPTH_STENCIL | PIPE_BIND_RENDER_TARGET);
70 }
71
72 static void virgl_init_temp_resource_from_box(struct pipe_resource *res,
73 struct pipe_resource *orig,
74 const struct pipe_box *box,
75 unsigned level, unsigned flags)
76 {
77 memset(res, 0, sizeof(*res));
78 res->bind = temp_bind(orig->bind);
79 res->format = orig->format;
80 res->width0 = box->width;
81 res->height0 = box->height;
82 res->depth0 = 1;
83 res->array_size = 1;
84 res->usage = PIPE_USAGE_STAGING;
85 res->flags = flags;
86
87 /* We must set the correct texture target and dimensions for a 3D box. */
88 if (box->depth > 1 && util_max_layer(orig, level) > 0)
89 res->target = orig->target;
90 else
91 res->target = PIPE_TEXTURE_2D;
92
93 switch (res->target) {
94 case PIPE_TEXTURE_1D_ARRAY:
95 case PIPE_TEXTURE_2D_ARRAY:
96 case PIPE_TEXTURE_CUBE_ARRAY:
97 res->array_size = box->depth;
98 break;
99 case PIPE_TEXTURE_3D:
100 res->depth0 = box->depth;
101 break;
102 default:
103 break;
104 }
105 }
106
107 static void *virgl_texture_transfer_map(struct pipe_context *ctx,
108 struct pipe_resource *resource,
109 unsigned level,
110 unsigned usage,
111 const struct pipe_box *box,
112 struct pipe_transfer **transfer)
113 {
114 struct virgl_context *vctx = virgl_context(ctx);
115 struct virgl_screen *vs = virgl_screen(ctx->screen);
116 struct virgl_resource *vtex = virgl_resource(resource);
117 struct virgl_transfer *trans;
118 void *ptr;
119 boolean readback = TRUE;
120 struct virgl_hw_res *hw_res;
121 bool flush;
122
123 trans = virgl_resource_create_transfer(&vctx->transfer_pool, resource,
124 &vtex->metadata, level, usage, box);
125
126 flush = virgl_res_needs_flush(vctx, trans);
127 if (flush)
128 ctx->flush(ctx, NULL, 0);
129
130 if (resource->nr_samples > 1) {
131 struct pipe_resource tmp_resource;
132 virgl_init_temp_resource_from_box(&tmp_resource, resource, box,
133 level, 0);
134
135 trans->resolve_tmp = (struct virgl_resource *)ctx->screen->resource_create(ctx->screen, &tmp_resource);
136
137 virgl_copy_region_with_blit(ctx, &trans->resolve_tmp->u.b, 0, 0, 0, 0, resource, level, box);
138 ctx->flush(ctx, NULL, 0);
139 /* we want to do a resolve blit into the temporary */
140 hw_res = trans->resolve_tmp->hw_res;
141 struct virgl_resource_metadata *data = &trans->resolve_tmp->metadata;
142 trans->base.stride = data->stride[level];
143 trans->base.layer_stride = data->layer_stride[level];
144 trans->offset = 0;
145 } else {
146 hw_res = vtex->hw_res;
147 trans->resolve_tmp = NULL;
148 }
149
150 readback = virgl_res_needs_readback(vctx, vtex, usage, level);
151 if (readback) {
152 vs->vws->transfer_get(vs->vws, hw_res, box, trans->base.stride,
153 trans->l_stride, trans->offset, level);
154
155 vs->vws->resource_wait(vs->vws, vtex->hw_res);
156 }
157
158 ptr = vs->vws->resource_map(vs->vws, hw_res);
159 if (!ptr) {
160 virgl_resource_destroy_transfer(&vctx->transfer_pool, trans);
161 return NULL;
162 }
163
164 *transfer = &trans->base;
165 return ptr + trans->offset;
166 }
167
168 static void virgl_texture_transfer_unmap(struct pipe_context *ctx,
169 struct pipe_transfer *transfer)
170 {
171 struct virgl_context *vctx = virgl_context(ctx);
172 struct virgl_transfer *trans = virgl_transfer(transfer);
173 struct virgl_resource *vtex = virgl_resource(transfer->resource);
174
175 if (trans->base.usage & PIPE_TRANSFER_WRITE) {
176 if (!(transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT)) {
177 struct virgl_screen *vs = virgl_screen(ctx->screen);
178 vctx->num_transfers++;
179
180 if (trans->resolve_tmp) {
181 vs->vws->transfer_put(vs->vws, vtex->hw_res,
182 &transfer->box, trans->base.stride,
183 trans->l_stride, trans->offset,
184 transfer->level);
185 } else {
186 virgl_transfer_queue_unmap(&vctx->queue, trans);
187 }
188 }
189 }
190
191 if (trans->resolve_tmp) {
192 pipe_resource_reference((struct pipe_resource **)&trans->resolve_tmp, NULL);
193 virgl_resource_destroy_transfer(&vctx->transfer_pool, trans);
194 } else if (!(trans->base.usage & PIPE_TRANSFER_WRITE))
195 virgl_resource_destroy_transfer(&vctx->transfer_pool, trans);
196 }
197
198 static const struct u_resource_vtbl virgl_texture_vtbl =
199 {
200 virgl_resource_get_handle, /* get_handle */
201 virgl_resource_destroy, /* resource_destroy */
202 virgl_texture_transfer_map, /* transfer_map */
203 NULL, /* transfer_flush_region */
204 virgl_texture_transfer_unmap, /* transfer_unmap */
205 };
206
207 void virgl_texture_init(struct virgl_resource *res)
208 {
209 res->u.vtbl = &virgl_texture_vtbl;
210 }