From 57a43c48b83c838f8be69366cddf581800d568b7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 4 Dec 2020 19:03:02 +0100 Subject: [PATCH] Added base Texture3D class (with no APIs) --- .../Buffers/Abstract/Texture3D{T}.cs | 122 ++++++++++++++++++ .../GraphicsDeviceMismatchException.cs | 13 ++ .../Extensions/ID3D12DeviceExtensions.cs | 51 ++++++++ 3 files changed, 186 insertions(+) create mode 100644 src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs new file mode 100644 index 00000000..f7ed889c --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -0,0 +1,122 @@ +using System.Runtime.CompilerServices; +using ComputeSharp.Core.Interop; +using ComputeSharp.Exceptions; +using ComputeSharp.Graphics.Buffers.Enums; +using ComputeSharp.Graphics.Extensions; +using ComputeSharp.Graphics.Helpers; +using Microsoft.Toolkit.Diagnostics; +using TerraFX.Interop; +using static TerraFX.Interop.D3D12_SRV_DIMENSION; +using static TerraFX.Interop.D3D12_UAV_DIMENSION; + +namespace ComputeSharp.Graphics.Buffers.Abstract +{ + /// + /// A representing a typed 3D texture stored on GPU memory. + /// + /// The type of items stored on the texture. + public unsafe abstract class Texture3D : NativeObject + where T : unmanaged + { + /// + /// The instance currently mapped. + /// + private ComPtr d3D12Resource; + + /// + /// The instance for the current resource. + /// + /// This field is dynamically accessed when loading shader dispatch data. + internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle; + + /// + /// The default value for the current resource. + /// + private readonly D3D12_RESOURCE_STATES D3D12ResourceState; + + /// + /// 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. + private protected Texture3D(GraphicsDevice device, int width, int height, int depth, ResourceType resourceType) + { + device.ThrowIfDisposed(); + + Guard.IsGreaterThanOrEqualTo(width, 0, nameof(width)); + Guard.IsGreaterThanOrEqualTo(height, 0, nameof(height)); + Guard.IsBetweenOrEqualTo(depth, 0, ushort.MaxValue, nameof(depth)); + + GraphicsDevice = device; + Width = width; + Height = height; + Depth = depth; + + this.d3D12Resource = device.D3D12Device->CreateCommittedResource( + resourceType, + DXGIFormatHelper.GetForType(), + (uint)width, + (uint)height, + (ushort)depth, + out D3D12ResourceState); + + device.AllocateShaderResourceViewDescriptorHandles(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); + + switch (resourceType) + { + case ResourceType.ReadOnly: + device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE3D, d3D12CpuDescriptorHandle); + break; + case ResourceType.ReadWrite: + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource, DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, d3D12CpuDescriptorHandle); + break; + } + } + + /// + /// 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; + + /// + protected override void OnDispose() + { + this.d3D12Resource.Dispose(); + } + + /// + /// 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/Exceptions/GraphicsDeviceMismatchException.cs b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs index 8c7eb70c..4abb6981 100644 --- a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs +++ b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs @@ -71,5 +71,18 @@ 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. + [Pure] + internal static void Throw(Texture3D texture, GraphicsDevice device) + where T : unmanaged + { + throw Create(texture, texture.GraphicsDevice, device); + } } } \ No newline at end of file diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index 6e171a2b..d0efc1d7 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -190,6 +190,57 @@ namespace ComputeSharp.Graphics.Extensions return d3D12Resource.Move(); } + /// + /// Creates a committed resource for a given 3D texture type. + /// + /// The instance in use. + /// The resource type currently in use. + /// The value to use. + /// The width of the texture resource. + /// The height of the texture resource. + /// The depth of the texture resource. + /// The default value for the resource. + /// An reference for the current texture. + public static ComPtr CreateCommittedResource( + this ref ID3D12Device d3D12Device, + ResourceType resourceType, + DXGI_FORMAT dxgiFormat, + uint width, + uint height, + ushort depth, + out D3D12_RESOURCE_STATES d3D12ResourceStates) + { + D3D12_RESOURCE_FLAGS d3D12ResourceFlags; + + (d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch + { + ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE), + ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS), + _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() + }; + + using ComPtr d3D12Resource = default; + + D3D12_HEAP_PROPERTIES d3D12HeapProperties; + d3D12HeapProperties.Type = D3D12_HEAP_TYPE_DEFAULT; + d3D12HeapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; + d3D12HeapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; + d3D12HeapProperties.CreationNodeMask = 1; + d3D12HeapProperties.VisibleNodeMask = 1; + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(dxgiFormat, width, height, depth, flags: d3D12ResourceFlags); + + d3D12Device.CreateCommittedResource( + &d3D12HeapProperties, + D3D12_HEAP_FLAG_NONE, + &d3D12ResourceDescription, + d3D12ResourceStates, + null, + FX.__uuidof(), + d3D12Resource.GetVoidAddressOf()).Assert(); + + return d3D12Resource.Move(); + } + /// /// Creates a view for a constant buffer. ///