virgl: Use copy transfers for textures
[mesa.git] / src / gallium / drivers / virgl / virgl_texture.c
index 19ae6ff1d3aeafe7c3db192499eb8bf31f19ce33..0d9c6a03e3517ab416017d97679cc068e484f2bf 100644 (file)
@@ -25,6 +25,7 @@
 #include "util/u_memory.h"
 
 #include "virgl_context.h"
+#include "virgl_encode.h"
 #include "virgl_resource.h"
 #include "virgl_screen.h"
 
@@ -38,10 +39,6 @@ static void virgl_copy_region_with_blit(struct pipe_context *pipe,
 {
    struct pipe_blit_info blit;
 
-   assert(src_box->width == dst_box->width);
-   assert(src_box->height == dst_box->height);
-   assert(src_box->depth == dst_box->depth);
-
    memset(&blit, 0, sizeof(blit));
    blit.src.resource = src;
    blit.src.format = src->format;
@@ -53,9 +50,9 @@ static void virgl_copy_region_with_blit(struct pipe_context *pipe,
    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 = src_box->width;
-   blit.dst.box.height = src_box->height;
-   blit.dst.box.depth = src_box->depth;
+   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;
@@ -126,7 +123,8 @@ static void *texture_transfer_map_plain(struct pipe_context *ctx,
    struct virgl_winsys *vws = virgl_screen(ctx->screen)->vws;
    struct virgl_resource *vtex = virgl_resource(resource);
    struct virgl_transfer *trans;
-   bool flush, readback;
+   enum virgl_transfer_map_type map_type;
+   void *map_addr;
 
    trans = virgl_resource_create_transfer(&vctx->transfer_pool, resource,
                                           &vtex->metadata, level, usage, box);
@@ -134,26 +132,34 @@ static void *texture_transfer_map_plain(struct pipe_context *ctx,
 
    assert(resource->nr_samples <= 1);
 
-   flush = virgl_res_needs_flush(vctx, trans);
-   if (flush)
-      ctx->flush(ctx, NULL, 0);
-
-   readback = virgl_res_needs_readback(vctx, vtex, usage, level);
-   if (readback)
-      vws->transfer_get(vws, vtex->hw_res, box, trans->base.stride,
-                        trans->l_stride, trans->offset, level);
-
-   if (readback || flush)
-      vws->resource_wait(vws, vtex->hw_res);
+   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;
+   }
 
-   trans->hw_res_map = vws->resource_map(vws, vtex->hw_res);
-   if (!trans->hw_res_map) {
+   if (!map_addr) {
       virgl_resource_destroy_transfer(&vctx->transfer_pool, trans);
       return NULL;
    }
 
    *transfer = &trans->base;
-   return trans->hw_res_map + trans->offset;
+   return map_addr;
 }
 
 static void *texture_transfer_map_resolve(struct pipe_context *ctx,
@@ -186,15 +192,22 @@ static void *texture_transfer_map_resolve(struct pipe_context *ctx,
       assert(virgl_has_readback_format(ctx->screen, fmt));
    }
 
-   virgl_init_temp_resource_from_box(&templ, resource, box, level, 0, fmt);
+   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));
+   }
+
+   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;
 
-   struct pipe_box dst_box = *box;
-   dst_box.x = dst_box.y = dst_box.z = 0;
-
    if (usage & PIPE_TRANSFER_READ) {
       virgl_copy_region_with_blit(ctx, resolve_tmp, 0, &dst_box, resource,
                                   level, box);
@@ -219,8 +232,8 @@ static void *texture_transfer_map_resolve(struct pipe_context *ctx,
          if (!ptr)
             goto fail;
 
-          if (!util_format_translate_3d(resource->format,
-                                       ptr,
+         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,
@@ -229,7 +242,9 @@ static void *texture_transfer_map_resolve(struct pipe_context *ctx,
                                        trans->resolve_transfer->stride,
                                        trans->resolve_transfer->layer_stride,
                                        0, 0, 0,
-                                       box->width, box->height, box->depth)) {
+                                       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));
@@ -291,8 +306,17 @@ 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_screen *vs = virgl_screen(ctx->screen);
+   struct pipe_resource *res = transfer->resource;
    bool queue_unmap = false;
 
+   /* 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);
+      return;
+   }
+
    if (transfer->usage & PIPE_TRANSFER_WRITE &&
        (transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT) == 0) {
 
@@ -323,10 +347,19 @@ static void virgl_texture_transfer_unmap(struct pipe_context *ctx,
                                       virgl_transfer(trans->resolve_transfer));
    }
 
-   if (queue_unmap)
-      virgl_transfer_queue_unmap(&vctx->queue, trans);
-   else
+   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 =