diff --git a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs index efe0b5f4..8b96c826 100644 --- a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs +++ b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs @@ -92,5 +92,17 @@ namespace ComputeSharp.Exceptions { throw Create(buffer, buffer.GraphicsDevice, device); } + + /// + /// Throws a new instance from the specified parameters. + /// + /// The type of values in the input buffer. + /// The input that was used. + /// The target instance that was used. + internal static void Throw(ReadBackBuffer buffer, GraphicsDevice device) + where T : unmanaged + { + throw Create(buffer, buffer.GraphicsDevice, device); + } } } \ No newline at end of file diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 1e479da4..a2ce007c 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -80,6 +80,32 @@ namespace ComputeSharp.Resources } } + /// + public unsafe void GetData(ReadBackBuffer destination, int destinationOffset, int length, int offset) + { + GraphicsDevice.ThrowIfDisposed(); + + ThrowIfDisposed(); + + destination.ThrowIfDeviceMismatch(GraphicsDevice); + destination.ThrowIfDisposed(); + + Guard.IsInRange(offset, 0, Length, nameof(offset)); + Guard.IsLessThanOrEqualTo(offset + length, Length, nameof(length)); + 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); + + using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COPY); + + copyCommandList.CopyBufferRegion(destination.D3D12Resource, byteDestinationOffset, D3D12Resource, byteOffset, byteLength); + copyCommandList.ExecuteAndWaitForCompletion(); + } + /// internal override unsafe void SetData(ref T source, int length, int offset) { diff --git a/src/ComputeSharp.Graphics/Resources/ReadBackBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/ReadBackBuffer{T}.cs new file mode 100644 index 00000000..f564b056 --- /dev/null +++ b/src/ComputeSharp.Graphics/Resources/ReadBackBuffer{T}.cs @@ -0,0 +1,167 @@ +using System; +using System.Buffers; +using System.Runtime.CompilerServices; +using ComputeSharp.Exceptions; +using ComputeSharp.Graphics.Extensions; +using ComputeSharp.Graphics.Resources.Enums; +using ComputeSharp.Interop; +using Microsoft.Toolkit.Diagnostics; +using TerraFX.Interop; + +namespace ComputeSharp +{ + /// + /// A representing a typed buffer stored on CPU memory, that can be used to transfer back from the GPU. + /// + /// The type of items stored on the buffer. + public sealed unsafe class ReadBackBuffer : NativeObject, IMemoryOwner + where T : unmanaged + { + /// + /// The instance currently mapped, if any. + /// + private ComPtr d3D12Resource; + + /// + /// The size in bytes of the current buffer (this value is never negative). + /// + private readonly nint sizeInBytes; + + /// + /// The pointer to the start of the mapped buffer data. + /// + private readonly T* mappedData; + + /// + /// Creates a new instance with the specified parameters. + /// + /// The associated with the current instance. + /// The number of items to store in the current buffer. + /// The allocation mode to use for the new resource. + public ReadBackBuffer(GraphicsDevice device, int length, AllocationMode allocationMode) + { + device.ThrowIfDisposed(); + + // The maximum length is set such that the aligned buffer size can't exceed uint.MaxValue + Guard.IsBetweenOrEqualTo(length, 1, (uint.MaxValue / (uint)sizeof(T)) & ~255, nameof(length)); + + GraphicsDevice = device; + Length = length; + + this.sizeInBytes = checked((nint)(length * (uint)sizeof(T))); + this.d3D12Resource = device.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, allocationMode, (ulong)this.sizeInBytes, device.IsCacheCoherentUMA); + this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; + } + + /// + /// Gets the associated with the current instance. + /// + public GraphicsDevice GraphicsDevice { get; } + + /// + /// Gets the length of the current buffer. + /// + public int Length { get; } + + /// + /// Gets the instance currently mapped. + /// + internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + + /// + public Memory Memory + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + ThrowIfDisposed(); + + return new MemoryManager(this).Memory; + } + } + + /// + public Span Span + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + ThrowIfDisposed(); + + return new(this.mappedData, Length); + } + } + + /// + protected override bool OnDispose() + { + this.d3D12Resource.Dispose(); + + return true; + } + + /// + /// Throws a if the target device doesn't match the current one. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal void ThrowIfDeviceMismatch(GraphicsDevice device) + { + if (GraphicsDevice != device) + { + GraphicsDeviceMismatchException.Throw(this, device); + } + } + + /// + /// A implementation wrapping an instance. + /// + private sealed class MemoryManager : MemoryManager + { + /// + /// The in use. + /// + private readonly ReadBackBuffer buffer; + + /// + /// Creates a new instance for a given buffer. + /// + /// The in use. + public MemoryManager(ReadBackBuffer buffer) + { + this.buffer = buffer; + } + + /// + public override Memory Memory + { + get => CreateMemory(this.buffer.Length); + } + + /// + public override Span GetSpan() + { + return this.buffer.Span; + } + + /// + public override MemoryHandle Pin(int elementIndex = 0) + { + Guard.IsInRange(elementIndex, 0, this.buffer.Length, nameof(elementIndex)); + + this.buffer.ThrowIfDisposed(); + + return new(this.buffer.mappedData + elementIndex); + } + + /// + public override void Unpin() + { + } + + /// + protected override void Dispose(bool disposing) + { + } + } + } +}