diff --git a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs index 613a15c2..39987597 100644 --- a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs +++ b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs @@ -104,5 +104,17 @@ namespace ComputeSharp.Exceptions { throw Create(texture, texture.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(TransferTexture3D 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/TransferTexture3D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture3D{T}.cs new file mode 100644 index 00000000..aebbbede --- /dev/null +++ b/src/ComputeSharp.Graphics/Resources/Abstract/TransferTexture3D{T}.cs @@ -0,0 +1,132 @@ +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 TerraFX.Interop; +using FX = TerraFX.Interop.Windows; +using static TerraFX.Interop.D3D12_FORMAT_SUPPORT1; + +namespace ComputeSharp.Resources +{ + /// + /// A representing a typed 3D 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 TransferTexture3D : 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 stride in bytes for each row within . + /// + private readonly int strideInBytes; + + /// + /// 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 depth of the texture. + /// The resource type for the current texture. + /// The allocation mode to use for the new resource. + private protected TransferTexture3D(GraphicsDevice device, int width, int height, int depth, ResourceType resourceType, AllocationMode allocationMode) + { + device.ThrowIfDisposed(); + + Guard.IsBetweenOrEqualTo(width, 1, FX.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION, nameof(width)); + Guard.IsBetweenOrEqualTo(height, 1, FX.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION, nameof(height)); + Guard.IsBetweenOrEqualTo(depth, 1, FX.D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION, nameof(depth)); + + if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE3D)) + { + UnsupportedTextureTypeException.ThrowForTexture2D(); + } + + GraphicsDevice = device; + Width = width; + Height = height; + Depth = depth; + + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(DXGIFormatHelper.GetForType(), (ulong)width, (uint)height, (ushort)depth); + + GraphicsDevice.D3D12Device->GetCopyableFootprint( + &d3D12ResourceDescription, + out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint, + out ulong rowSizeInBytes, + out ulong totalSizeInBytes); + + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(resourceType, allocationMode, totalSizeInBytes, GraphicsDevice.IsCacheCoherentUMA); + this.mappedData = (T*)this.d3D12Resource.Get()->Map().Pointer; + this.strideInBytes = (int)d3D12PlacedSubresourceFootprint.Footprint.RowPitch; + } + + /// + /// 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 depth of the current texture. + /// + public int Depth { get; } + + /// + /// Gets the instance currently mapped. + /// + internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + + /// + public TextureView3D View + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + ThrowIfDisposed(); + + return new(this.mappedData, Height, Width, Depth, this.strideInBytes); + } + } + + /// + 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); + } + } + } +} diff --git a/src/ComputeSharp.Graphics/Resources/Views/ThrowHelper.cs b/src/ComputeSharp.Graphics/Resources/Views/ThrowHelper.cs index aa011264..9c7d9a94 100644 --- a/src/ComputeSharp.Graphics/Resources/Views/ThrowHelper.cs +++ b/src/ComputeSharp.Graphics/Resources/Views/ThrowHelper.cs @@ -51,6 +51,14 @@ namespace ComputeSharp.Resources.Views throw new ArgumentOutOfRangeException("column"); } + /// + /// Throws an when the "z" parameter is invalid. + /// + public static void ThrowArgumentOutOfRangeExceptionForZ() + { + throw new ArgumentOutOfRangeException("z"); + } + /// /// Throws an when the "height" parameter is invalid. ///