From 4d0bab3890fe915d3a66e9552e51948be33ca712 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 29 Aug 2022 00:52:04 +0200 Subject: [PATCH] Add TransferTexture1D type --- .../UnsupportedTextureTypeException.cs | 11 + src/ComputeSharp/Graphics/GraphicsDevice.cs | 34 +++ .../Abstract/TransferTexture1D{T}.cs | 207 ++++++++++++++++++ .../Abstract/TransferTexture2D{T}.cs | 2 +- .../Abstract/TransferTexture3D{T}.cs | 2 +- 5 files changed, 254 insertions(+), 2 deletions(-) create mode 100644 src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture1D{T}.cs diff --git a/src/ComputeSharp/Graphics/Exceptions/UnsupportedTextureTypeException.cs b/src/ComputeSharp/Graphics/Exceptions/UnsupportedTextureTypeException.cs index 0558e6f8..bc4f9353 100644 --- a/src/ComputeSharp/Graphics/Exceptions/UnsupportedTextureTypeException.cs +++ b/src/ComputeSharp/Graphics/Exceptions/UnsupportedTextureTypeException.cs @@ -33,6 +33,7 @@ public sealed class UnsupportedTextureTypeException : NotSupportedException builder.AppendLine(rank switch { + 1 => $"{nameof(GraphicsDevice.IsReadOnlyTexture1DSupportedForType)}() or {nameof(GraphicsDevice)}.{nameof(GraphicsDevice.IsReadWriteTexture1DSupportedForType)}().", 2 => $"{nameof(GraphicsDevice.IsReadOnlyTexture2DSupportedForType)}() or {nameof(GraphicsDevice)}.{nameof(GraphicsDevice.IsReadWriteTexture2DSupportedForType)}().", 3 => $"{nameof(GraphicsDevice.IsReadOnlyTexture3DSupportedForType)}() or {nameof(GraphicsDevice)}.{nameof(GraphicsDevice.IsReadWriteTexture3DSupportedForType)}().", _ => ThrowHelper.ThrowArgumentException("Invalid texture rank.") @@ -43,6 +44,16 @@ public sealed class UnsupportedTextureTypeException : NotSupportedException return new(builder.ToString()); } + /// + /// Throws a new instance from the specified parameters. + /// + /// The type of values in the texture that couldn't be created. + internal static void ThrowForTexture1D() + where T : unmanaged + { + throw Create(1, typeof(T)); + } + /// /// Throws a new instance from the specified parameters. /// diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index ea9edbc0..71895039 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -261,6 +261,40 @@ public sealed unsafe partial class GraphicsDevice : NativeObject return d3D12OptionsData.DoublePrecisionFloatShaderOps; } + /// + /// Checks whether the current device supports the creation of + /// resources for a specified type . + /// + /// The type of values to check support for. + /// Whether instances can be created by the current device. + public bool IsReadOnlyTexture1DSupportedForType() + where T : unmanaged + { + using var _0 = GetReferenceTrackingLease(); + + ThrowIfDeviceLost(); + + return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE1D); + } + + /// + /// Checks whether the current device supports the creation of + /// resources for a specified type . + /// + /// The type of values to check support for. + /// Whether instances can be created by the current device. + public bool IsReadWriteTexture1DSupportedForType() + where T : unmanaged + { + using var _0 = GetReferenceTrackingLease(); + + ThrowIfDeviceLost(); + + return this.d3D12Device.Get()->IsDxgiFormatSupported( + DXGIFormatHelper.GetForType(), + D3D12_FORMAT_SUPPORT1_TEXTURE1D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW); + } + /// /// Checks whether the current device supports the creation of /// resources for a specified type . diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture1D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture1D{T}.cs new file mode 100644 index 00000000..63058466 --- /dev/null +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture1D{T}.cs @@ -0,0 +1,207 @@ +using System; +using System.Buffers; +using System.Runtime.CompilerServices; +using CommunityToolkit.Diagnostics; +using ComputeSharp.Exceptions; +using ComputeSharp.Graphics.Extensions; +using ComputeSharp.Graphics.Helpers; +using ComputeSharp.Interop; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; +using static TerraFX.Interop.DirectX.D3D12_FORMAT_SUPPORT1; +using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType; + +namespace ComputeSharp.Resources; + +/// +/// A representing a typed 1D 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 TransferTexture1D : NativeObject, IGraphicsResource, IMemoryOwner + where T : unmanaged +{ +#if NET6_0_OR_GREATER + /// + /// The instance used to retrieve . + /// + private ComPtr allocation; +#endif + + /// + /// The instance currently mapped. + /// + private ComPtr d3D12Resource; + + /// + /// The pointer to the start of the mapped buffer data. + /// + private readonly T* mappedData; + + /// + /// The description for the current resource. + /// + private readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; + + /// + /// Creates a new instance with the specified parameters. + /// + /// The associated with the current instance. + /// The width of the texture. + /// The resource type for the current texture. + /// The allocation mode to use for the new resource. + private protected TransferTexture1D(GraphicsDevice device, int width, ResourceType resourceType, AllocationMode allocationMode) + { + Guard.IsBetweenOrEqualTo(width, 1, D3D12.D3D12_REQ_TEXTURE1D_U_DIMENSION); + + using var _0 = device.GetReferenceTrackingLease(); + + device.ThrowIfDeviceLost(); + + if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE1D)) + { + UnsupportedTextureTypeException.ThrowForTexture1D(); + } + + GraphicsDevice = device; + + device.D3D12Device->GetCopyableFootprint( + DXGIFormatHelper.GetForType(), + (uint)width, + out this.d3D12PlacedSubresourceFootprint, + out _, + out ulong totalSizeInBytes); + +#if NET6_0_OR_GREATER + this.allocation = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, totalSizeInBytes); + this.d3D12Resource = new ComPtr(this.allocation.Get()->GetResource()); +#else + this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, totalSizeInBytes, device.IsCacheCoherentUMA); +#endif + + device.RegisterAllocatedResource(); + + this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; + + this.d3D12Resource.Get()->SetName(this); + } + + /// + public GraphicsDevice GraphicsDevice { get; } + + /// + /// Gets the width of the current texture. + /// + public int Width => (int)this.d3D12PlacedSubresourceFootprint.Footprint.Width; + + /// + /// Gets the instance currently mapped. + /// + internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + + /// + /// Gets the value for the current resource. + /// + internal ref readonly D3D12_PLACED_SUBRESOURCE_FOOTPRINT D3D12PlacedSubresourceFootprint => ref this.d3D12PlacedSubresourceFootprint; + + /// + public Memory Memory + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + using var _0 = GetReferenceTrackingLease(); + + return new MemoryManager(this).Memory; + } + } + + /// + public Span Span + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + using var _0 = GetReferenceTrackingLease(); + + return new(this.mappedData, Width); + } + } + + /// + private protected sealed override void OnDispose() + { + this.d3D12Resource.Dispose(); +#if NET6_0_OR_GREATER + this.allocation.Dispose(); +#endif + + if (GraphicsDevice is GraphicsDevice device) + { + device.UnregisterAllocatedResource(); + } + } + + /// + /// 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 TransferTexture1D buffer; + + /// + /// Creates a new instance for a given buffer. + /// + /// The in use. + public MemoryManager(TransferTexture1D buffer) + { + this.buffer = buffer; + } + + /// + public override Memory Memory + { + get => CreateMemory(this.buffer.Width); + } + + /// + public override Span GetSpan() + { + return this.buffer.Span; + } + + /// + public override MemoryHandle Pin(int elementIndex = 0) + { + Guard.IsEqualTo(elementIndex, 0); + + using var _0 = this.buffer.GetReferenceTrackingLease(); + + return new(this.buffer.mappedData); + } + + /// + public override void Unpin() + { + } + + /// + protected override void Dispose(bool disposing) + { + } + } +} diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs index 61c0c892..05bc33e4 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture2D{T}.cs @@ -44,8 +44,8 @@ public unsafe abstract class TransferTexture2D : NativeObject, IGraphicsResou /// 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 height 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) diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs index 5f310119..701399b1 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/TransferTexture3D{T}.cs @@ -44,8 +44,8 @@ public unsafe abstract class TransferTexture3D : NativeObject, IGraphicsResou /// 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 height of the texture. /// The depth of the texture. /// The resource type for the current texture. /// The allocation mode to use for the new resource.