virgl: Use copy transfers for textures
[mesa.git] / src / gallium / drivers / virgl / virgl_texture.c
index c5226f21421c3ea4e4059c8241a696ed65f681f7..0d9c6a03e3517ab416017d97679cc068e484f2bf 100644 (file)
 #include "util/u_memory.h"
 
 #include "virgl_context.h"
+#include "virgl_encode.h"
 #include "virgl_resource.h"
 #include "virgl_screen.h"
 
 static void virgl_copy_region_with_blit(struct pipe_context *pipe,
                                         struct pipe_resource *dst,
                                         unsigned dst_level,
-                                        unsigned dstx, unsigned dsty, unsigned dstz,
+                                        const struct pipe_box *dst_box,
                                         struct pipe_resource *src,
                                         unsigned src_level,
                                         const struct pipe_box *src_box)
@@ -46,12 +47,12 @@ static void virgl_copy_region_with_blit(struct pipe_context *pipe,
    blit.dst.resource = dst;
    blit.dst.format = dst->format;
    blit.dst.level = dst_level;
-   blit.dst.box.x = dstx;
-   blit.dst.box.y = dsty;
-   blit.dst.box.z = dstz;
-   blit.dst.box.width = src_box->width;
-   blit.dst.box.height = src_box->height;
-   blit.dst.box.depth = src_box->depth;
+   blit.dst.box.x = dst_box->x;
+   blit.dst.box.y = dst_box->y;
+   blit.dst.box.z = dst_box->z;
+   blit.dst.box.width = dst_box->width;
+   blit.dst.box.height = dst_box->height;
+   blit.dst.box.depth = dst_box->depth;
    blit.mask = util_format_get_mask(src->format) &
       util_format_get_mask(dst->format);
    blit.filter = PIPE_TEX_FILTER_NEAREST;
@@ -64,20 +65,23 @@ static void virgl_copy_region_with_blit(struct pipe_context *pipe,
 static unsigned temp_bind(unsigned orig)
 {
    unsigned warn = ~(PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL |
-                     PIPE_BIND_SAMPLER_VIEW);
+                     PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DISPLAY_TARGET);
    if (orig & warn)
-      fprintf(stderr, "Waring, possibly unhandled bind: %x\n", orig & warn);
+      debug_printf("VIRGL: Warning, possibly unhandled bind: %x\n",
+                   orig & warn);
+
    return orig & (PIPE_BIND_DEPTH_STENCIL | PIPE_BIND_RENDER_TARGET);
 }
 
 static void virgl_init_temp_resource_from_box(struct pipe_resource *res,
                                               struct pipe_resource *orig,
                                               const struct pipe_box *box,
-                                              unsigned level, unsigned flags)
+                                              unsigned level, unsigned flags,
+                                              enum pipe_format fmt)
 {
    memset(res, 0, sizeof(*res));
    res->bind = temp_bind(orig->bind);
-   res->format = orig->format;
+   res->format = fmt;
    res->width0 = box->width;
    res->height0 = box->height;
    res->depth0 = 1;
@@ -91,6 +95,9 @@ static void virgl_init_temp_resource_from_box(struct pipe_resource *res,
    else
       res->target = PIPE_TEXTURE_2D;
 
+   if (res->target != PIPE_BUFFER)
+      res->bind = PIPE_BIND_RENDER_TARGET;
+
    switch (res->target) {
    case PIPE_TEXTURE_1D_ARRAY:
    case PIPE_TEXTURE_2D_ARRAY:
@@ -105,7 +112,7 @@ static void virgl_init_temp_resource_from_box(struct pipe_resource *res,
    }
 }
 
-static void *virgl_texture_transfer_map(struct pipe_context *ctx,
+static void *texture_transfer_map_plain(struct pipe_context *ctx,
                                         struct pipe_resource *resource,
                                         unsigned level,
                                         unsigned usage,
@@ -113,57 +120,185 @@ static void *virgl_texture_transfer_map(struct pipe_context *ctx,
                                         struct pipe_transfer **transfer)
 {
    struct virgl_context *vctx = virgl_context(ctx);
-   struct virgl_screen *vs = virgl_screen(ctx->screen);
+   struct virgl_winsys *vws = virgl_screen(ctx->screen)->vws;
    struct virgl_resource *vtex = virgl_resource(resource);
    struct virgl_transfer *trans;
-   void *ptr;
-   boolean readback = TRUE;
-   struct virgl_hw_res *hw_res;
-   bool flush;
+   enum virgl_transfer_map_type map_type;
+   void *map_addr;
 
    trans = virgl_resource_create_transfer(&vctx->transfer_pool, resource,
                                           &vtex->metadata, level, usage, box);
+   trans->resolve_transfer = NULL;
 
-   flush = virgl_res_needs_flush(vctx, trans);
-   if (flush)
-      ctx->flush(ctx, NULL, 0);
-
-   if (resource->nr_samples > 1) {
-      struct pipe_resource tmp_resource;
-      virgl_init_temp_resource_from_box(&tmp_resource, resource, box,
-                                        level, 0);
+   assert(resource->nr_samples <= 1);
 
-      trans->resolve_tmp = (struct virgl_resource *)ctx->screen->resource_create(ctx->screen, &tmp_resource);
+   map_type = virgl_resource_transfer_prepare(vctx, trans);
+   switch (map_type) {
+   case VIRGL_TRANSFER_MAP_HW_RES:
+      trans->hw_res_map = vws->resource_map(vws, vtex->hw_res);
+      if (trans->hw_res_map)
+         map_addr = trans->hw_res_map + trans->offset;
+      else
+         map_addr = NULL;
+      break;
+   case VIRGL_TRANSFER_MAP_STAGING:
+      map_addr = virgl_transfer_uploader_map(vctx, trans);
+      /* Copy transfers don't make use of hw_res_map at the moment. */
+      trans->hw_res_map = NULL;
+      break;
+   case VIRGL_TRANSFER_MAP_ERROR:
+   default:
+      trans->hw_res_map = NULL;
+      map_addr = NULL;
+      break;
+   }
 
-      virgl_copy_region_with_blit(ctx, &trans->resolve_tmp->u.b, 0, 0, 0, 0, resource, level, box);
-      ctx->flush(ctx, NULL, 0);
-      /* we want to do a resolve blit into the temporary */
-      hw_res = trans->resolve_tmp->hw_res;
-      struct virgl_resource_metadata *data = &trans->resolve_tmp->metadata;
-      trans->base.stride = data->stride[level];
-      trans->base.layer_stride = data->layer_stride[level];
-      trans->offset = 0;
-   } else {
-      hw_res = vtex->hw_res;
-      trans->resolve_tmp = NULL;
+   if (!map_addr) {
+      virgl_resource_destroy_transfer(&vctx->transfer_pool, trans);
+      return NULL;
    }
 
-   readback = virgl_res_needs_readback(vctx, vtex, usage, level);
-   if (readback) {
-      vs->vws->transfer_get(vs->vws, hw_res, box, trans->base.stride,
-                            trans->l_stride, trans->offset, level);
+   *transfer = &trans->base;
+   return map_addr;
+}
+
+static void *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)
+{
+   struct virgl_context *vctx = virgl_context(ctx);
+   struct virgl_resource *vtex = virgl_resource(resource);
+   struct pipe_resource templ, *resolve_tmp;
+   struct virgl_transfer *trans;
+
+   trans = virgl_resource_create_transfer(&vctx->transfer_pool, resource,
+                                          &vtex->metadata, level, usage, box);
+   if (!trans)
+      return NULL;
+
+   enum pipe_format fmt = resource->format;
+   if (!virgl_has_readback_format(ctx->screen, fmt)) {
+      if (util_format_fits_8unorm(util_format_description(fmt)))
+         fmt = PIPE_FORMAT_R8G8B8A8_UNORM;
+      else if (util_format_is_pure_sint(fmt))
+         fmt = PIPE_FORMAT_R32G32B32A32_SINT;
+      else if (util_format_is_pure_uint(fmt))
+         fmt = PIPE_FORMAT_R32G32B32A32_UINT;
+      else
+         fmt = PIPE_FORMAT_R32G32B32A32_FLOAT;
+      assert(virgl_has_readback_format(ctx->screen, fmt));
+   }
 
-      vs->vws->resource_wait(vs->vws, vtex->hw_res);
+   struct pipe_box dst_box = *box;
+   dst_box.x = dst_box.y = dst_box.z = 0;
+   if (usage & PIPE_TRANSFER_READ) {
+      /* readback should scale to the block size */
+      dst_box.width = align(dst_box.width,
+            util_format_get_blockwidth(resource->format));
+      dst_box.height = align(dst_box.height,
+            util_format_get_blockheight(resource->format));
    }
 
-   ptr = vs->vws->resource_map(vs->vws, hw_res);
-   if (!ptr) {
-      virgl_resource_destroy_transfer(&vctx->transfer_pool, trans);
+   virgl_init_temp_resource_from_box(&templ, resource, &dst_box, level, 0, fmt);
+
+   resolve_tmp = ctx->screen->resource_create(ctx->screen, &templ);
+   if (!resolve_tmp)
       return NULL;
+
+   if (usage & PIPE_TRANSFER_READ) {
+      virgl_copy_region_with_blit(ctx, resolve_tmp, 0, &dst_box, resource,
+                                  level, box);
+      ctx->flush(ctx, NULL, 0);
    }
 
+   void *ptr = texture_transfer_map_plain(ctx, resolve_tmp, 0, usage, &dst_box,
+                                          &trans->resolve_transfer);
+   if (!ptr)
+      goto fail;
+
    *transfer = &trans->base;
-   return ptr + trans->offset;
+   if (fmt == resource->format) {
+      trans->base.stride = trans->resolve_transfer->stride;
+      trans->base.layer_stride = trans->resolve_transfer->layer_stride;
+      return ptr;
+   } else {
+      if (usage & PIPE_TRANSFER_READ) {
+         struct virgl_winsys *vws = virgl_screen(ctx->screen)->vws;
+         void *src = ptr;
+         ptr = vws->resource_map(vws, vtex->hw_res);
+         if (!ptr)
+            goto fail;
+
+         if (!util_format_translate_3d(resource->format,
+                                       ptr + vtex->metadata.level_offset[level],
+                                       trans->base.stride,
+                                       trans->base.layer_stride,
+                                       box->x, box->y, box->z,
+                                       fmt,
+                                       src,
+                                       trans->resolve_transfer->stride,
+                                       trans->resolve_transfer->layer_stride,
+                                       0, 0, 0,
+                                       dst_box.width,
+                                       dst_box.height,
+                                       dst_box.depth)) {
+            debug_printf("failed to translate format %s to %s\n",
+                         util_format_short_name(fmt),
+                         util_format_short_name(resource->format));
+            goto fail;
+         }
+      }
+
+      if ((usage & PIPE_TRANSFER_WRITE) == 0)
+         pipe_resource_reference(&trans->resolve_transfer->resource, NULL);
+
+      return ptr + trans->offset;
+   }
+
+fail:
+   pipe_resource_reference(&resolve_tmp, NULL);
+   virgl_resource_destroy_transfer(&vctx->transfer_pool, trans);
+   return NULL;
+}
+
+static bool needs_resolve(struct pipe_screen *screen,
+                          struct pipe_resource *resource, unsigned usage)
+{
+   if (resource->nr_samples > 1)
+      return true;
+
+   if (usage & PIPE_TRANSFER_READ)
+      return !util_format_is_depth_or_stencil(resource->format) &&
+             !virgl_has_readback_format(screen, resource->format);
+
+   return false;
+}
+
+static void *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)
+{
+   if (needs_resolve(ctx->screen, resource, usage))
+      return texture_transfer_map_resolve(ctx, resource, level, usage, box,
+                                          transfer);
+
+   return texture_transfer_map_plain(ctx, resource, level, usage, box, transfer);
+}
+
+static void flush_data(struct pipe_context *ctx,
+                       struct virgl_transfer *trans,
+                       const struct pipe_box *box)
+{
+   struct virgl_winsys *vws = virgl_screen(ctx->screen)->vws;
+   vws->transfer_put(vws, virgl_resource(trans->base.resource)->hw_res, box,
+                     trans->base.stride, trans->l_stride, trans->offset,
+                     trans->base.level);
 }
 
 static void virgl_texture_transfer_unmap(struct pipe_context *ctx,
@@ -171,29 +306,60 @@ static void virgl_texture_transfer_unmap(struct pipe_context *ctx,
 {
    struct virgl_context *vctx = virgl_context(ctx);
    struct virgl_transfer *trans = virgl_transfer(transfer);
-   struct virgl_resource *vtex = virgl_resource(transfer->resource);
-
-   if (trans->base.usage & PIPE_TRANSFER_WRITE) {
-      if (!(transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT)) {
-         struct virgl_screen *vs = virgl_screen(ctx->screen);
-         vctx->num_transfers++;
-
-         if (trans->resolve_tmp) {
-            vs->vws->transfer_put(vs->vws, vtex->hw_res,
-                                  &transfer->box, trans->base.stride,
-                                  trans->l_stride, trans->offset,
-                                  transfer->level);
-         } else {
-            virgl_transfer_queue_unmap(&vctx->queue, trans);
-         }
-      }
-   }
+   struct virgl_screen *vs = virgl_screen(ctx->screen);
+   struct pipe_resource *res = transfer->resource;
+   bool queue_unmap = false;
 
-   if (trans->resolve_tmp) {
-      pipe_resource_reference((struct pipe_resource **)&trans->resolve_tmp, NULL);
+   /* We don't need to transfer the contents of staging buffers, since they
+    * don't have any host-side storage. */
+   if (pipe_to_virgl_bind(vs, res->bind, res->flags) == VIRGL_BIND_STAGING) {
       virgl_resource_destroy_transfer(&vctx->transfer_pool, trans);
-   } else if (!(trans->base.usage & PIPE_TRANSFER_WRITE))
+      return;
+   }
+
+   if (transfer->usage & PIPE_TRANSFER_WRITE &&
+       (transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT) == 0) {
+
+      if (trans->resolve_transfer && (trans->base.resource->format ==
+          trans->resolve_transfer->resource->format)) {
+         flush_data(ctx, virgl_transfer(trans->resolve_transfer),
+                    &trans->resolve_transfer->box);
+
+         /* FINISHME: In case the destination format isn't renderable here, the
+          * blit here will currently fail. This could for instance happen if the
+          * mapped resource is of a compressed format, and it's mapped with both
+          * read and write usage.
+          */
+
+         virgl_copy_region_with_blit(ctx,
+                                     trans->base.resource, trans->base.level,
+                                     &transfer->box,
+                                     trans->resolve_transfer->resource, 0,
+                                     &trans->resolve_transfer->box);
+         ctx->flush(ctx, NULL, 0);
+      } else
+         queue_unmap = true;
+   }
+
+   if (trans->resolve_transfer) {
+      pipe_resource_reference(&trans->resolve_transfer->resource, NULL);
+      virgl_resource_destroy_transfer(&vctx->transfer_pool,
+                                      virgl_transfer(trans->resolve_transfer));
+   }
+
+   if (queue_unmap) {
+      if (trans->copy_src_res) {
+         virgl_encode_copy_transfer(vctx, trans);
+         /* It's now safe for other mappings to use the transfer_uploader. */
+         vctx->transfer_uploader_in_use = false;
+         virgl_resource_destroy_transfer(&vctx->transfer_pool, trans);
+      } else {
+         virgl_transfer_queue_unmap(&vctx->queue, trans);
+      }
+   } else {
       virgl_resource_destroy_transfer(&vctx->transfer_pool, trans);
+   }
+
 }
 
 static const struct u_resource_vtbl virgl_texture_vtbl =