From 974a9aed9a915ba03f1501be55168e3879d4ef2b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 16 Feb 2021 13:23:03 +0100 Subject: [PATCH] Fixed transfer buffer copies on UMA devices --- .../Resources/Abstract/StructuredBuffer{T}.cs | 56 ++++++++++++++----- .../Resources/Abstract/TransferBuffer{T}.cs | 5 ++ 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs index cd4c3629..5c25645e 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -101,15 +101,29 @@ namespace ComputeSharp.Resources Guard.IsInRange(destinationOffset, 0, destination.Length, nameof(destinationOffset)); Guard.IsLessThanOrEqualTo(destinationOffset + length, destination.Length, nameof(length)); - ulong - byteDestinationOffset = (uint)destinationOffset * (uint)sizeof(T), - byteOffset = (uint)offset * (uint)sizeof(T), - byteLength = (uint)length * (uint)sizeof(T); + if (GraphicsDevice.IsCacheCoherentUMA) + { + using ID3D12ResourceMap resource = D3D12Resource->Map(); - using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); + MemoryHelper.Copy( + resource.Pointer, + (uint)offset, + (uint)length, + (uint)sizeof(T), + destination.MappedData + destinationOffset); + } + else + { + ulong + byteDestinationOffset = (uint)destinationOffset * (uint)sizeof(T), + byteOffset = (uint)offset * (uint)sizeof(T), + byteLength = (uint)length * (uint)sizeof(T); - copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(destination.D3D12Resource, byteDestinationOffset, D3D12Resource, byteOffset, byteLength); - copyCommandList.ExecuteAndWaitForCompletion(); + using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); + + copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(destination.D3D12Resource, byteDestinationOffset, D3D12Resource, byteOffset, byteLength); + copyCommandList.ExecuteAndWaitForCompletion(); + } } /// @@ -183,15 +197,29 @@ namespace ComputeSharp.Resources Guard.IsInRange(sourceOffset, 0, source.Length, nameof(sourceOffset)); Guard.IsLessThanOrEqualTo(sourceOffset + length, source.Length, nameof(length)); - ulong - byteSourceOffset = (uint)sourceOffset * (uint)sizeof(T), - byteOffset = (uint)offset * (uint)sizeof(T), - byteLength = (uint)length * (uint)sizeof(T); + if (GraphicsDevice.IsCacheCoherentUMA) + { + using ID3D12ResourceMap resource = D3D12Resource->Map(); - using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); + MemoryHelper.Copy( + source.MappedData, + (uint)sourceOffset, + (uint)length, + (uint)sizeof(T), + (T*)resource.Pointer + offset); + } + else + { + ulong + byteSourceOffset = (uint)sourceOffset * (uint)sizeof(T), + byteOffset = (uint)offset * (uint)sizeof(T), + byteLength = (uint)length * (uint)sizeof(T); - copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, byteOffset, source.D3D12Resource, byteSourceOffset, byteLength); - copyCommandList.ExecuteAndWaitForCompletion(); + using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); + + copyCommandList.D3D12GraphicsCommandList->CopyBufferRegion(D3D12Resource, byteOffset, source.D3D12Resource, byteSourceOffset, byteLength); + copyCommandList.ExecuteAndWaitForCompletion(); + } } /// diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/TransferBuffer{T}.cs index d2a3a2fc..9c52d2a3 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/TransferBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -67,6 +67,11 @@ namespace ComputeSharp.Resources /// internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + /// + /// Gets the pointer to the start of the mapped buffer data. + /// + internal T* MappedData => this.mappedData; + /// public Memory Memory {