From 61945b3e26c680fb62f2e4a6cb96dd86c47d0edc Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 31 Jan 2021 19:57:13 +0100 Subject: [PATCH] Code refactoring, added TransferBuffer base type --- .../GraphicsDeviceMismatchException.cs | 16 +- .../Resources/Abstract/TransferBuffer{T}.cs | 168 ++++++++++++++++++ .../Resources/ReadBackBuffer{T}.cs | 150 +--------------- .../Resources/UploadBuffer{T}.cs | 150 +--------------- 4 files changed, 176 insertions(+), 308 deletions(-) create mode 100644 src/ComputeSharp.Graphics/Resources/Abstract/TransferBuffer{T}.cs diff --git a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs index 8b96c826..07b1a8a7 100644 --- a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs +++ b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs @@ -85,21 +85,9 @@ namespace ComputeSharp.Exceptions /// Throws a new instance from the specified parameters. /// /// The type of values in the input buffer. - /// The input that was used. + /// The input that was used. /// The target instance that was used. - internal static void Throw(UploadBuffer buffer, GraphicsDevice device) - where T : unmanaged - { - 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) + internal static void Throw(TransferBuffer buffer, GraphicsDevice device) where T : unmanaged { throw Create(buffer, buffer.GraphicsDevice, device); diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/TransferBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/TransferBuffer{T}.cs new file mode 100644 index 00000000..0c631c76 --- /dev/null +++ b/src/ComputeSharp.Graphics/Resources/Abstract/TransferBuffer{T}.cs @@ -0,0 +1,168 @@ +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 data to/from the GPU. + /// + /// The type of items stored on the buffer. + public abstract unsafe class TransferBuffer : NativeObject, IMemoryOwner + where T : unmanaged + { + /// + /// The instance currently mapped. + /// + 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 resource type for the current buffer. + /// The allocation mode to use for the new resource. + private protected TransferBuffer(GraphicsDevice device, int length, ResourceType resourceType, 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, 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 a instance. + /// + private sealed class MemoryManager : MemoryManager + { + /// + /// The in use. + /// + private readonly TransferBuffer buffer; + + /// + /// Creates a new instance for a given buffer. + /// + /// The in use. + public MemoryManager(TransferBuffer 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) + { + } + } + } +} diff --git a/src/ComputeSharp.Graphics/Resources/ReadBackBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/ReadBackBuffer{T}.cs index f564b056..cdb231ac 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadBackBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadBackBuffer{T}.cs @@ -1,12 +1,4 @@ -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; +using ComputeSharp.Graphics.Resources.Enums; namespace ComputeSharp { @@ -14,24 +6,9 @@ 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 + public sealed class ReadBackBuffer : TransferBuffer 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. /// @@ -39,129 +16,8 @@ namespace ComputeSharp /// 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) + : base(device, length, ResourceType.ReadBack, 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) - { - } } } } diff --git a/src/ComputeSharp.Graphics/Resources/UploadBuffer{T}.cs b/src/ComputeSharp.Graphics/Resources/UploadBuffer{T}.cs index 481b3917..7ab3c125 100644 --- a/src/ComputeSharp.Graphics/Resources/UploadBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/UploadBuffer{T}.cs @@ -1,12 +1,4 @@ -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; +using ComputeSharp.Graphics.Resources.Enums; namespace ComputeSharp { @@ -14,24 +6,9 @@ namespace ComputeSharp /// A representing a typed buffer stored on CPU memory, that can be used to transfer data to the GPU. /// /// The type of items stored on the buffer. - public sealed unsafe class UploadBuffer : NativeObject, IMemoryOwner + public sealed class UploadBuffer : TransferBuffer 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. /// @@ -39,129 +16,8 @@ namespace ComputeSharp /// The number of items to store in the current buffer. /// The allocation mode to use for the new resource. public UploadBuffer(GraphicsDevice device, int length, AllocationMode allocationMode) + : base(device, length, ResourceType.Upload, 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.Upload, 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 UploadBuffer buffer; - - /// - /// Creates a new instance for a given buffer. - /// - /// The in use. - public MemoryManager(UploadBuffer 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) - { - } } } }