[Mesa-dev] [PATCH 2/2] radeon: Use transfer manager for buffer downloads

Niels Ole Salscheider niels_ole at salscheider-online.de
Mon Mar 3 12:29:33 PST 2014


Using DMA for reads is much faster.

Signed-off-by: Niels Ole Salscheider <niels_ole at salscheider-online.de>
---
 src/gallium/drivers/radeon/r600_buffer_common.c | 78 +++++++++++++++++++------
 src/gallium/drivers/radeon/r600_pipe_common.c   | 11 ++++
 src/gallium/drivers/radeon/r600_pipe_common.h   |  1 +
 3 files changed, 72 insertions(+), 18 deletions(-)

diff --git a/src/gallium/drivers/radeon/r600_buffer_common.c b/src/gallium/drivers/radeon/r600_buffer_common.c
index 340ebb2..c910107 100644
--- a/src/gallium/drivers/radeon/r600_buffer_common.c
+++ b/src/gallium/drivers/radeon/r600_buffer_common.c
@@ -260,6 +260,46 @@ static void *r600_buffer_transfer_map(struct pipe_context *ctx,
 		/* At this point, the buffer is always idle (we checked it above). */
 		usage |= PIPE_TRANSFER_UNSYNCHRONIZED;
 	}
+	/* Using DMA for larger reads is much faster */
+	else if ((usage & PIPE_TRANSFER_READ) &&
+		 !(usage & PIPE_TRANSFER_WRITE) &&
+		 (rbuffer->domains == RADEON_DOMAIN_VRAM) &&
+		 (rscreen->has_cp_dma ||
+		  (rscreen->has_streamout &&
+		   /* The buffer range must be aligned to 4 with streamout. */
+		   box->x % 4 == 0 && box->width % 4 == 0))) {
+		unsigned offset;
+		struct r600_resource *staging = NULL;
+
+		u_upload_alloc(rctx->downloader, 0,
+			       box->width + (box->x % R600_MAP_BUFFER_ALIGNMENT),
+			       &offset, (struct pipe_resource**)&staging, (void**)&data);
+
+		if (staging) {
+			data += box->x % R600_MAP_BUFFER_ALIGNMENT;
+
+			/* Copy the staging buffer into the original one. */
+			if (rctx->dma_copy(ctx, (struct pipe_resource*)staging, 0,
+						 box->x % R600_MAP_BUFFER_ALIGNMENT,
+						 0, 0, resource, level, box)) {
+				rctx->rings.gfx.flush(rctx, 0);
+				if (rctx->rings.dma.cs)
+					rctx->rings.dma.flush(rctx, 0);
+
+				/* Wait for any offloaded CS flush to complete
+				 * to avoid busy-waiting in the winsys. */
+				rctx->ws->cs_sync_flush(rctx->rings.gfx.cs);
+				if (rctx->rings.dma.cs)
+					rctx->ws->cs_sync_flush(rctx->rings.dma.cs);
+
+				rctx->ws->buffer_wait(staging->buf, RADEON_USAGE_READWRITE);
+				return r600_buffer_get_transfer(ctx, resource, level, usage, box,
+								ptransfer, data, staging, offset);
+			} else {
+				pipe_resource_reference((struct pipe_resource**)&staging, NULL);
+			}
+		}
+	}
 
 	data = r600_buffer_map_sync_with_rings(rctx, rbuffer, usage);
 	if (!data) {
@@ -279,24 +319,26 @@ static void r600_buffer_transfer_unmap(struct pipe_context *ctx,
 	struct r600_resource *rbuffer = r600_resource(transfer->resource);
 
 	if (rtransfer->staging) {
-		struct pipe_resource *dst, *src;
-		unsigned soffset, doffset, size;
-		struct pipe_box box;
-
-		dst = transfer->resource;
-		src = &rtransfer->staging->b.b;
-		size = transfer->box.width;
-		doffset = transfer->box.x;
-		soffset = rtransfer->offset + transfer->box.x % R600_MAP_BUFFER_ALIGNMENT;
-
-		u_box_1d(soffset, size, &box);
-
-		/* Copy the staging buffer into the original one. */
-		if (!(size % 4) && !(doffset % 4) && !(soffset % 4) &&
-		    rctx->dma_copy(ctx, dst, 0, doffset, 0, 0, src, 0, &box)) {
-			/* DONE. */
-		} else {
-			ctx->resource_copy_region(ctx, dst, 0, doffset, 0, 0, src, 0, &box);
+		if (rtransfer->transfer.usage & PIPE_TRANSFER_WRITE) {
+			struct pipe_resource *dst, *src;
+			unsigned soffset, doffset, size;
+			struct pipe_box box;
+
+			dst = transfer->resource;
+			src = &rtransfer->staging->b.b;
+			size = transfer->box.width;
+			doffset = transfer->box.x;
+			soffset = rtransfer->offset + transfer->box.x % R600_MAP_BUFFER_ALIGNMENT;
+
+			u_box_1d(soffset, size, &box);
+
+			/* Copy the staging buffer into the original one. */
+			if (!(size % 4) && !(doffset % 4) && !(soffset % 4) &&
+			    rctx->dma_copy(ctx, dst, 0, doffset, 0, 0, src, 0, &box)) {
+				/* DONE. */
+			} else {
+				ctx->resource_copy_region(ctx, dst, 0, doffset, 0, 0, src, 0, &box);
+			}
 		}
 		pipe_resource_reference((struct pipe_resource**)&rtransfer->staging, NULL);
 	}
diff --git a/src/gallium/drivers/radeon/r600_pipe_common.c b/src/gallium/drivers/radeon/r600_pipe_common.c
index fc81dcf..47e811f 100644
--- a/src/gallium/drivers/radeon/r600_pipe_common.c
+++ b/src/gallium/drivers/radeon/r600_pipe_common.c
@@ -78,6 +78,13 @@ bool r600_common_context_init(struct r600_common_context *rctx,
 	if (!rctx->uploader)
 		return false;
 
+	rctx->downloader = u_upload_create(&rctx->b, 1024 * 1024, 256,
+					   PIPE_BIND_INDEX_BUFFER |
+					   PIPE_BIND_CONSTANT_BUFFER,
+					   UPLOAD_MGR_DOWNLOAD);
+	if (!rctx->downloader)
+		return false;
+
 	return true;
 }
 
@@ -94,6 +101,10 @@ void r600_common_context_cleanup(struct r600_common_context *rctx)
 		u_upload_destroy(rctx->uploader);
 	}
 
+	if (rctx->downloader) {
+		u_upload_destroy(rctx->downloader);
+	}
+
 	util_slab_destroy(&rctx->pool_transfers);
 
 	if (rctx->allocator_so_filled_size) {
diff --git a/src/gallium/drivers/radeon/r600_pipe_common.h b/src/gallium/drivers/radeon/r600_pipe_common.h
index 692de5e..4af76e9 100644
--- a/src/gallium/drivers/radeon/r600_pipe_common.h
+++ b/src/gallium/drivers/radeon/r600_pipe_common.h
@@ -287,6 +287,7 @@ struct r600_common_context {
 	unsigned			initial_gfx_cs_size;
 
 	struct u_upload_mgr		*uploader;
+	struct u_upload_mgr		*downloader;
 	struct u_suballocator		*allocator_so_filled_size;
 	struct util_slab_mempool	pool_transfers;
 
-- 
1.9.0



More information about the mesa-dev mailing list