From 4c8fadac6f020c8de11861a2171cc2a8c80d0bd6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 2 Dec 2020 20:17:10 +0100 Subject: [PATCH] Added initial Texture2D base type --- .../Buffers/Abstract/Buffer{T}.cs | 4 +- .../Buffers/Abstract/StructuredBuffer{T}.cs | 2 +- .../Buffers/Abstract/Texture2D{T}.cs | 88 +++++++++++++++++++ .../GraphicsDeviceMismatchException.cs | 33 +++++-- .../Helpers/DXGIFormatHelper.cs | 10 +-- 5 files changed, 121 insertions(+), 16 deletions(-) create mode 100644 src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs index 3b70da0c..b936fb89 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Buffer{T}.cs @@ -40,7 +40,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The number of items to store in the current buffer. /// The size in bytes of each buffer item (including padding, if any). /// The buffer type for the current buffer. - internal Buffer(GraphicsDevice device, int length, uint elementSizeInBytes, BufferType bufferType) + private protected Buffer(GraphicsDevice device, int length, uint elementSizeInBytes, BufferType bufferType) { device.ThrowIfDisposed(); @@ -69,7 +69,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract } /// - /// Gets the associated with the current instance. + /// Gets the associated with the current instance. /// public GraphicsDevice GraphicsDevice { get; } diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs index d03c019c..9f8b01f5 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/StructuredBuffer{T}.cs @@ -24,7 +24,7 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// The associated with the current instance. /// The number of items to store in the current buffer. /// The buffer type for the current buffer. - internal StructuredBuffer(GraphicsDevice device, int length, BufferType bufferType) + private protected StructuredBuffer(GraphicsDevice device, int length, BufferType bufferType) : base(device, length, (uint)Unsafe.SizeOf(), bufferType) { } diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs new file mode 100644 index 00000000..7313a9a7 --- /dev/null +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -0,0 +1,88 @@ +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; + +namespace ComputeSharp.Graphics.Buffers.Abstract +{ + /// + /// A representing a typed 2D texture stored on GPU memory. + /// + /// The type of items stored on the texture. + public unsafe abstract class Texture2D : NativeObject + where T : unmanaged + { + /// + /// The instance currently mapped. + /// + private ComPtr d3D12Resource; + + /// + /// The instance for the current resource. + /// + internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle; + + /// + /// 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 size in bytes of each buffer item (including padding, if any). + /// The buffer type for the current buffer. + private protected Texture2D(GraphicsDevice device, int height, int width, TextureType textureType) + { + device.ThrowIfDisposed(); + + Guard.IsGreaterThanOrEqualTo(height, 0, nameof(height)); + Guard.IsGreaterThanOrEqualTo(width, 0, nameof(width)); + + GraphicsDevice = device; + + this.d3D12Resource = device.D3D12Device->CreateCommittedResource(textureType, DXGIFormatHelper.GetForType(), (uint)height, (uint)width); + + device.AllocateShaderResourceViewDescriptorHandles(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); + + switch (textureType) + { + case TextureType.ReadOnly: + device.D3D12Device->CreateShaderResourceView(this.d3D12Resource, DXGIFormatHelper.GetForType(), d3D12CpuDescriptorHandle); + break; + case TextureType.ReadWrite: + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource, DXGIFormatHelper.GetForType(), d3D12CpuDescriptorHandle); + break; + } + } + + /// + /// Gets the associated with the current instance. + /// + public GraphicsDevice GraphicsDevice { 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 fe2c4490..8197b10f 100644 --- a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs +++ b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics.Contracts; using System.Text; +using ComputeSharp.Core.Interop; using ComputeSharp.Graphics; using ComputeSharp.Graphics.Buffers.Abstract; @@ -23,19 +24,22 @@ namespace ComputeSharp.Exceptions /// /// Creates 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. + /// The input that was used. + /// The source instance tied to . + /// The target instance that was used. /// A new instance with a formatted error message. + /// + /// This method only takes a instance and the associated instance as + /// will still be available, but without the unnecessary generic type specializations for the method. + /// [Pure] - private static GraphicsDeviceMismatchException Create(Buffer buffer, GraphicsDevice device) - where T : unmanaged + private static GraphicsDeviceMismatchException Create(NativeObject resource, GraphicsDevice sourceDevice, GraphicsDevice destinationDevice) { StringBuilder builder = new(512); builder.AppendLine("Invalid pairing of graphics devices used to run a compute shader and allocate memory buffers."); - builder.AppendLine($"The target device to run the compute shader is \"{device}\"."); - builder.AppendLine($"The buffer of type {buffer.GetType()} was allocated on device \"{buffer.GraphicsDevice}\"."); + builder.AppendLine($"The target device to run the compute shader is \"{destinationDevice}\"."); + builder.AppendLine($"The buffer of type {resource.GetType()} was allocated on device \"{sourceDevice}\"."); builder.Append("Make sure to always allocate buffers on the same device used to actually run the code that accesses them."); builder.ToString(); @@ -52,7 +56,20 @@ namespace ComputeSharp.Exceptions internal static void Throw(Buffer buffer, GraphicsDevice device) where T : unmanaged { - throw Create(buffer, device); + 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. + [Pure] + internal static void Throw(Texture2D texture, GraphicsDevice device) + where T : unmanaged + { + throw Create(texture, texture.GraphicsDevice, device); } } } \ No newline at end of file diff --git a/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs b/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs index fd49b602..be8d5d21 100644 --- a/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs @@ -1,9 +1,9 @@ -using static TerraFX.Interop.DXGI_FORMAT; -using TerraFX.Interop; -using ABI.System.Numerics; -using Microsoft.Toolkit.Diagnostics; -using System.Diagnostics.Contracts; +using System.Diagnostics.Contracts; +using System.Numerics; using System.Runtime.CompilerServices; +using Microsoft.Toolkit.Diagnostics; +using static TerraFX.Interop.DXGI_FORMAT; +using TerraFX.Interop; namespace ComputeSharp.Graphics.Helpers {