asahi: Don't shadow idle resources
authorAlyssa Rosenzweig <alyssa@rosenzweig.io>
Tue, 29 Nov 2022 01:35:38 +0000 (20:35 -0500)
committerAlyssa Rosenzweig <alyssa@rosenzweig.io>
Sun, 11 Dec 2022 02:51:04 +0000 (21:51 -0500)
Pointless allocation+memcpy.

Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/20123>

src/gallium/drivers/asahi/agx_batch.c
src/gallium/drivers/asahi/agx_pipe.c
src/gallium/drivers/asahi/agx_state.h

index d1bfd30..cd25c08 100644 (file)
@@ -220,6 +220,20 @@ agx_flush_writer_except(struct agx_context *ctx,
    }
 }
 
+bool
+agx_any_batch_uses_resource(struct agx_context *ctx, struct agx_resource *rsrc)
+{
+   unsigned idx;
+   foreach_batch(ctx, idx) {
+      struct agx_batch *batch = &ctx->batches.slots[idx];
+
+      if (agx_batch_uses_bo(batch, rsrc->bo))
+         return true;
+   }
+
+   return false;
+}
+
 void
 agx_flush_readers(struct agx_context *ctx, struct agx_resource *rsrc, const char *reason)
 {
index 684464a..50fc31c 100644 (file)
@@ -679,16 +679,25 @@ agx_prepare_for_map(struct agx_context *ctx,
    if (usage & PIPE_MAP_UNSYNCHRONIZED)
       return;
 
+   /* Both writing and reading need writers flushed */
    agx_flush_writer(ctx, rsrc, "Unsynchronized transfer");
 
-   if (usage & PIPE_MAP_WRITE) {
-      /* Try to shadow the resource to avoid a flush */
-      if ((usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE) && agx_shadow(ctx, rsrc))
-         return;
+   /* Additionally, writing needs readers flushed */
+   if (!(usage & PIPE_MAP_WRITE))
+      return;
 
-      /* Otherwise, we need to flush */
-      agx_flush_readers(ctx, rsrc, "Unsynchronized write");
-   }
+   /* If there are no readers, we're done. We check at the start to
+    * avoid expensive shadowing paths or duplicated checks in this hapyp path.
+    */
+   if (!agx_any_batch_uses_resource(ctx, rsrc))
+      return;
+
+   /* There are readers. Try to shadow the resource to avoid a flush */
+   if ((usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE) && agx_shadow(ctx, rsrc))
+      return;
+
+   /* Otherwise, we need to flush */
+   agx_flush_readers(ctx, rsrc, "Unsynchronized write");
 }
 
 
index 1432ea8..165ef68 100644 (file)
@@ -441,6 +441,8 @@ void agx_flush_writer(struct agx_context *ctx, struct agx_resource *rsrc, const
 void agx_batch_reads(struct agx_batch *batch, struct agx_resource *rsrc);
 void agx_batch_writes(struct agx_batch *batch, struct agx_resource *rsrc);
 
+bool agx_any_batch_uses_resource(struct agx_context *ctx, struct agx_resource *rsrc);
+
 struct agx_batch *agx_get_batch(struct agx_context *ctx);
 void agx_batch_cleanup(struct agx_context *ctx, struct agx_batch *batch);