xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/virgl/virgl_texture.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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/format/u_format.h"
24 #include "util/u_inlines.h"
25 #include "util/u_memory.h"
26 
27 #include "virgl_context.h"
28 #include "virgl_encode.h"
29 #include "virgl_resource.h"
30 #include "virgl_screen.h"
31 
virgl_copy_region_with_blit(struct pipe_context * pipe,struct pipe_resource * dst,unsigned dst_level,const struct pipe_box * dst_box,struct pipe_resource * src,unsigned src_level,const struct pipe_box * src_box)32 static void virgl_copy_region_with_blit(struct pipe_context *pipe,
33                                         struct pipe_resource *dst,
34                                         unsigned dst_level,
35                                         const struct pipe_box *dst_box,
36                                         struct pipe_resource *src,
37                                         unsigned src_level,
38                                         const struct pipe_box *src_box)
39 {
40    struct pipe_blit_info blit;
41 
42    memset(&blit, 0, sizeof(blit));
43    blit.src.resource = src;
44    blit.src.format = src->format;
45    blit.src.level = src_level;
46    blit.src.box = *src_box;
47    blit.dst.resource = dst;
48    blit.dst.format = dst->format;
49    blit.dst.level = dst_level;
50    blit.dst.box.x = dst_box->x;
51    blit.dst.box.y = dst_box->y;
52    blit.dst.box.z = dst_box->z;
53    blit.dst.box.width = dst_box->width;
54    blit.dst.box.height = dst_box->height;
55    blit.dst.box.depth = dst_box->depth;
56    blit.mask = util_format_get_mask(src->format) &
57       util_format_get_mask(dst->format);
58    blit.filter = PIPE_TEX_FILTER_NEAREST;
59 
60    if (blit.mask) {
61       pipe->blit(pipe, &blit);
62    }
63 }
64 
temp_bind(unsigned orig)65 static unsigned temp_bind(unsigned orig)
66 {
67    unsigned warn = ~(PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL |
68                      PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DISPLAY_TARGET);
69    if (orig & warn)
70       debug_printf("VIRGL: Warning, possibly unhandled bind: %x\n",
71                    orig & warn);
72 
73    return orig & (PIPE_BIND_DEPTH_STENCIL | PIPE_BIND_RENDER_TARGET);
74 }
75 
virgl_init_temp_resource_from_box(struct pipe_resource * res,struct pipe_resource * orig,const struct pipe_box * box,unsigned level,unsigned flags,enum pipe_format fmt)76 static void virgl_init_temp_resource_from_box(struct pipe_resource *res,
77                                               struct pipe_resource *orig,
78                                               const struct pipe_box *box,
79                                               unsigned level, unsigned flags,
80                                               enum pipe_format fmt)
81 {
82    memset(res, 0, sizeof(*res));
83    res->bind = temp_bind(orig->bind);
84    res->format = fmt;
85    res->width0 = box->width;
86    res->height0 = box->height;
87    res->depth0 = 1;
88    res->array_size = 1;
89    res->usage = PIPE_USAGE_STAGING;
90    res->flags = flags;
91 
92    /* We must set the correct texture target and dimensions for a 3D box. */
93    if (box->depth > 1 && util_max_layer(orig, level) > 0)
94       res->target = orig->target;
95    else
96       res->target = PIPE_TEXTURE_2D;
97 
98    if (res->target != PIPE_BUFFER)
99       res->bind = PIPE_BIND_RENDER_TARGET;
100 
101    switch (res->target) {
102    case PIPE_TEXTURE_CUBE:
103    case PIPE_TEXTURE_1D_ARRAY:
104    case PIPE_TEXTURE_2D_ARRAY:
105    case PIPE_TEXTURE_CUBE_ARRAY:
106       res->array_size = box->depth;
107       break;
108    case PIPE_TEXTURE_3D:
109       res->depth0 = box->depth;
110       break;
111    default:
112       break;
113    }
114 }
115 
texture_transfer_map_resolve(struct pipe_context * ctx,struct pipe_resource * resource,unsigned level,unsigned usage,const struct pipe_box * box,struct pipe_transfer ** transfer)116 static void *texture_transfer_map_resolve(struct pipe_context *ctx,
117                                           struct pipe_resource *resource,
118                                           unsigned level,
119                                           unsigned usage,
120                                           const struct pipe_box *box,
121                                           struct pipe_transfer **transfer)
122 {
123    struct virgl_context *vctx = virgl_context(ctx);
124    struct virgl_resource *vtex = virgl_resource(resource);
125    struct pipe_resource templ, *resolve_tmp;
126    struct virgl_transfer *trans;
127 
128    trans = virgl_resource_create_transfer(vctx, resource,
129                                           &vtex->metadata, level, usage, box);
130    if (!trans)
131       return NULL;
132 
133    enum pipe_format fmt = resource->format;
134    if (!virgl_has_readback_format(ctx->screen, pipe_to_virgl_format(fmt), true)) {
135       if (util_format_fits_8unorm(util_format_description(fmt)))
136          fmt = PIPE_FORMAT_R8G8B8A8_UNORM;
137       else if (util_format_is_pure_sint(fmt))
138          fmt = PIPE_FORMAT_R32G32B32A32_SINT;
139       else if (util_format_is_pure_uint(fmt))
140          fmt = PIPE_FORMAT_R32G32B32A32_UINT;
141       else
142          fmt = PIPE_FORMAT_R32G32B32A32_FLOAT;
143       assert(virgl_has_readback_format(ctx->screen, pipe_to_virgl_format(fmt), true));
144    }
145 
146    struct pipe_box dst_box = *box;
147    dst_box.x = dst_box.y = dst_box.z = 0;
148    if (usage & PIPE_MAP_READ) {
149       /* readback should scale to the block size */
150       dst_box.width = align(dst_box.width,
151             util_format_get_blockwidth(resource->format));
152       dst_box.height = align(dst_box.height,
153             util_format_get_blockheight(resource->format));
154       if (resource->target == PIPE_TEXTURE_3D)
155          dst_box.depth = align(dst_box.depth,
156                util_format_get_blockdepth(resource->format));
157    }
158 
159    virgl_init_temp_resource_from_box(&templ, resource, &dst_box, level, 0, fmt);
160 
161    resolve_tmp = ctx->screen->resource_create(ctx->screen, &templ);
162    if (!resolve_tmp)
163       return NULL;
164 
165    if (usage & PIPE_MAP_READ) {
166       virgl_copy_region_with_blit(ctx, resolve_tmp, 0, &dst_box, resource,
167                                   level, box);
168       ctx->flush(ctx, NULL, 0);
169    }
170 
171    void *ptr = virgl_resource_transfer_map(ctx, resolve_tmp, 0, usage, &dst_box,
172                                            &trans->resolve_transfer);
173    if (!ptr)
174       goto fail;
175 
176    /* trans->resolve_transfer owns resolve_tmp now */
177    pipe_resource_reference(&resolve_tmp, NULL);
178 
179    *transfer = &trans->base;
180    if (fmt == resource->format) {
181       trans->base.stride = trans->resolve_transfer->stride;
182       trans->base.layer_stride = trans->resolve_transfer->layer_stride;
183       return ptr;
184    } else {
185       if (usage & PIPE_MAP_READ) {
186          struct virgl_winsys *vws = virgl_screen(ctx->screen)->vws;
187          void *src = ptr;
188          ptr = vws->resource_map(vws, vtex->hw_res);
189          if (!ptr)
190             goto fail;
191 
192          if (!util_format_translate_3d(resource->format,
193                                        (uint8_t *)ptr + vtex->metadata.level_offset[level],
194                                        trans->base.stride,
195                                        trans->base.layer_stride,
196                                        box->x, box->y, box->z,
197                                        fmt,
198                                        src,
199                                        trans->resolve_transfer->stride,
200                                        trans->resolve_transfer->layer_stride,
201                                        0, 0, 0,
202                                        dst_box.width,
203                                        dst_box.height,
204                                        dst_box.depth)) {
205             debug_printf("failed to translate format %s to %s\n",
206                          util_format_short_name(fmt),
207                          util_format_short_name(resource->format));
208             goto fail;
209          }
210       }
211 
212       if ((usage & PIPE_MAP_WRITE) == 0)
213          pipe_resource_reference(&trans->resolve_transfer->resource, NULL);
214 
215       return (uint8_t *)ptr + trans->offset;
216    }
217 
218 fail:
219    pipe_resource_reference(&resolve_tmp, NULL);
220    virgl_resource_destroy_transfer(vctx, trans);
221    return NULL;
222 }
223 
needs_resolve(struct pipe_screen * screen,struct pipe_resource * resource,unsigned usage)224 static bool needs_resolve(struct pipe_screen *screen,
225                           struct pipe_resource *resource, unsigned usage)
226 {
227    if (resource->nr_samples > 1)
228       return true;
229 
230    if (usage & PIPE_MAP_READ)
231       return !util_format_is_depth_or_stencil(resource->format) &&
232              !virgl_has_readback_format(screen, pipe_to_virgl_format(resource->format), true);
233 
234    return false;
235 }
236 
virgl_texture_transfer_map(struct pipe_context * ctx,struct pipe_resource * resource,unsigned level,unsigned usage,const struct pipe_box * box,struct pipe_transfer ** transfer)237 void *virgl_texture_transfer_map(struct pipe_context *ctx,
238                                  struct pipe_resource *resource,
239                                  unsigned level,
240                                  unsigned usage,
241                                  const struct pipe_box *box,
242                                  struct pipe_transfer **transfer)
243 {
244    if (needs_resolve(ctx->screen, resource, usage))
245       return texture_transfer_map_resolve(ctx, resource, level, usage, box,
246                                           transfer);
247 
248    return virgl_resource_transfer_map(ctx, resource, level, usage, box, transfer);
249 }
250 
flush_data(struct pipe_context * ctx,struct virgl_transfer * trans,const struct pipe_box * box)251 static void flush_data(struct pipe_context *ctx,
252                        struct virgl_transfer *trans,
253                        const struct pipe_box *box)
254 {
255    struct virgl_winsys *vws = virgl_screen(ctx->screen)->vws;
256    vws->transfer_put(vws, trans->hw_res, box,
257                      trans->base.stride, trans->l_stride, trans->offset,
258                      trans->base.level);
259 }
260 
virgl_texture_transfer_unmap(struct pipe_context * ctx,struct pipe_transfer * transfer)261 void virgl_texture_transfer_unmap(struct pipe_context *ctx,
262                                   struct pipe_transfer *transfer)
263 {
264    struct virgl_context *vctx = virgl_context(ctx);
265    struct virgl_transfer *trans = virgl_transfer(transfer);
266    bool queue_unmap = false;
267 
268    if (transfer->usage & PIPE_MAP_WRITE &&
269        (transfer->usage & PIPE_MAP_FLUSH_EXPLICIT) == 0) {
270 
271       if (trans->resolve_transfer && (trans->base.resource->format ==
272           trans->resolve_transfer->resource->format)) {
273          flush_data(ctx, virgl_transfer(trans->resolve_transfer),
274                     &trans->resolve_transfer->box);
275 
276          /* FINISHME: In case the destination format isn't renderable here, the
277           * blit here will currently fail. This could for instance happen if the
278           * mapped resource is of a compressed format, and it's mapped with both
279           * read and write usage.
280           */
281 
282          virgl_copy_region_with_blit(ctx,
283                                      trans->base.resource, trans->base.level,
284                                      &transfer->box,
285                                      trans->resolve_transfer->resource, 0,
286                                      &trans->resolve_transfer->box);
287          ctx->flush(ctx, NULL, 0);
288       } else
289          queue_unmap = true;
290    }
291 
292    if (trans->resolve_transfer) {
293       virgl_resource_destroy_transfer(vctx,
294                                       virgl_transfer(trans->resolve_transfer));
295    }
296 
297    if (queue_unmap) {
298       if (trans->copy_src_hw_res && trans->direction == VIRGL_TRANSFER_TO_HOST) {
299          virgl_encode_copy_transfer(vctx, trans);
300          virgl_resource_destroy_transfer(vctx, trans);
301       } else if (trans->copy_src_hw_res && trans->direction == VIRGL_TRANSFER_FROM_HOST) {
302          // if it is readback, then we have already encoded transfer
303          virgl_resource_destroy_transfer(vctx, trans);
304       } else {
305          virgl_transfer_queue_unmap(&vctx->queue, trans);
306       }
307    } else {
308       virgl_resource_destroy_transfer(vctx, trans);
309    }
310 }
311 
virgl_texture_init(struct virgl_resource * res)312 void virgl_texture_init(struct virgl_resource *res)
313 {
314 }
315