diff --git a/nuget.config b/nuget.config index 4f62278c..4f8aceec 100644 --- a/nuget.config +++ b/nuget.config @@ -5,6 +5,6 @@ - + diff --git a/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj b/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj index 203d2f59..41957938 100644 --- a/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj +++ b/src/ComputeSharp.Graphics/ComputeSharp.Graphics.csproj @@ -8,7 +8,8 @@ - + + diff --git a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs index 07b1a8a7..613a15c2 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(TransferTexture2D texture, GraphicsDevice device) + where T : unmanaged + { + throw Create(texture, texture.GraphicsDevice, device); + } } } \ No newline at end of file diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs new file mode 100644 index 00000000..e74931a9 --- /dev/null +++ b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -0,0 +1,194 @@ +using System; +using System.Buffers; +using System.Runtime.CompilerServices; +using ComputeSharp.Exceptions; +using ComputeSharp.Graphics.Extensions; +using ComputeSharp.Graphics.Helpers; +using ComputeSharp.Graphics.Resources.Enums; +using ComputeSharp.Interop; +using Microsoft.Toolkit.Diagnostics; +using Microsoft.Toolkit.HighPerformance.Memory; +using TerraFX.Interop; +using FX = TerraFX.Interop.Windows; +using static TerraFX.Interop.D3D12_FORMAT_SUPPORT1; + +namespace ComputeSharp.Resources +{ + /// + /// A representing a typed 2D texture stored on on CPU memory, that can be used to transfer data to/from the GPU. + /// + /// The type of items stored on the texture. + public unsafe abstract class TransferTexture2D : NativeObject + where T : unmanaged + { + /// + /// The instance currently mapped. + /// + private ComPtr d3D12Resource; + + /// + /// The pointer to the start of the mapped buffer data. + /// + private readonly T* mappedData; + + /// + /// The pitch size for each row within . + /// + private readonly int rowPitch; + + /// + /// Creates a new instance with the specified parameters. + /// + /// The associated with the current instance. + /// The height of the texture. + /// The width of the texture. + /// The resource type for the current texture. + /// The allocation mode to use for the new resource. + private protected TransferTexture2D(GraphicsDevice device, int width, int height, ResourceType resourceType, AllocationMode allocationMode) + { + device.ThrowIfDisposed(); + + Guard.IsBetweenOrEqualTo(width, 1, FX.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION, nameof(width)); + Guard.IsBetweenOrEqualTo(height, 1, FX.D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION, nameof(height)); + + if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE2D)) + { + UnsupportedTextureTypeException.ThrowForTexture2D(); + } + + GraphicsDevice = device; + Width = width; + Height = height; + + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex2D(DXGIFormatHelper.GetForType(), (ulong)width, (uint)height); + + GraphicsDevice.D3D12Device->GetCopyableFootprint( + &d3D12ResourceDescription, + out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint, + out ulong rowSizeInBytes, + out ulong totalSizeInBytes); + + this.d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(resourceType, allocationMode, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); + this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; + this.rowPitch = (int)((d3D12PlacedSubresourceFootprint.Footprint.RowPitch - rowSizeInBytes) / (uint)sizeof(T)); + } + + /// + /// Gets the associated with the current instance. + /// + public GraphicsDevice GraphicsDevice { get; } + + /// + /// Gets the width of the current texture. + /// + public int Width { get; } + + /// + /// Gets the height of the current texture. + /// + public int Height { get; } + + /// + /// Gets the instance currently mapped. + /// + internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + + /// + public Memory2D Memory + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + ThrowIfDisposed(); + + MemoryManager memoryManager = new(this); + + return new(memoryManager, 0, Height, Width, this.rowPitch); + } + } + + /// + public Span2D Span + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + ThrowIfDisposed(); + + return new(this.mappedData, Height, Width, this.rowPitch); + } + } + + /// + 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 TransferTexture2D texture; + + /// + /// Creates a new instance for a given texture. + /// + /// The in use. + public MemoryManager(TransferTexture2D texture) + { + this.texture = texture; + } + + /// + public override Memory Memory + { + get => ThrowHelper.ThrowNotSupportedException>(); + } + + /// + public override Span GetSpan() + { + return new(this.texture.mappedData, this.texture.Height * this.texture.rowPitch); + } + + /// + public override MemoryHandle Pin(int elementIndex = 0) + { + Guard.IsEqualTo(elementIndex, 0, nameof(elementIndex)); + + this.texture.ThrowIfDisposed(); + + return new(this.texture.mappedData); + } + + /// + public override void Unpin() + { + } + + /// + protected override void Dispose(bool disposing) + { + } + } + } +}