From 7629dee8c00c6b390a991e2bac1eba6f806cab2e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 15 Jan 2021 20:16:48 +0100 Subject: [PATCH] Added more texture type validation APIs --- .../UnsupportedTextureTypeException.cs | 6 +-- .../Extensions/ID3D12DeviceExtensions.cs | 2 +- src/ComputeSharp.Graphics/GraphicsDevice.cs | 50 ++++++++++++++++--- .../Resources/Abstract/Texture2D{T}.cs | 6 +-- .../Resources/Abstract/Texture3D{T}.cs | 6 +-- .../Resources/ReadOnlyTexture2D{T,TPixel}.cs | 3 +- .../Resources/ReadOnlyTexture2D{T}.cs | 3 +- .../Resources/ReadOnlyTexture3D{T,TPixel}.cs | 3 +- .../Resources/ReadOnlyTexture3D{T}.cs | 3 +- .../Resources/ReadWriteTexture2D{T,TPixel}.cs | 3 +- .../Resources/ReadWriteTexture2D{T}.cs | 3 +- .../Resources/ReadWriteTexture3D{T,TPixel}.cs | 3 +- .../Resources/ReadWriteTexture3D{T}.cs | 3 +- 13 files changed, 68 insertions(+), 26 deletions(-) diff --git a/src/ComputeSharp.Graphics/Exceptions/UnsupportedTextureTypeException.cs b/src/ComputeSharp.Graphics/Exceptions/UnsupportedTextureTypeException.cs index 511ce147..1b100ced 100644 --- a/src/ComputeSharp.Graphics/Exceptions/UnsupportedTextureTypeException.cs +++ b/src/ComputeSharp.Graphics/Exceptions/UnsupportedTextureTypeException.cs @@ -35,12 +35,12 @@ namespace ComputeSharp.Exceptions builder.AppendLine(rank switch { - 2 => $"{nameof(GraphicsDevice.IsTexture2DSupportedForType)}().", - 3 => $"{nameof(GraphicsDevice.IsTexture3DSupportedForType)}().", + 2 => $"{nameof(GraphicsDevice.IsReadOnlyTexture2DSupportedForType)}() or {nameof(GraphicsDevice.IsReadWriteTexture2DSupportedForType)}().", + 3 => $"{nameof(GraphicsDevice.IsReadOnlyTexture3DSupportedForType)}() or {nameof(GraphicsDevice.IsReadWriteTexture3DSupportedForType)}().", _ => ThrowHelper.ThrowArgumentException("Invalid texture rank") }); - builder.AppendLine($"As a possible workaround on older devices, consider using a texture type of lower rank, or a linear buffer."); + builder.AppendLine("As a possible workaround on older devices, consider using a texture type of lower rank, or a linear buffer."); builder.ToString(); return new(builder.ToString()); diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index 971f3db3..0b621f4a 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -505,7 +505,7 @@ namespace ComputeSharp.Graphics.Extensions d3D12Device.CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &d3D12FeatureDataFormatSupport, (uint)sizeof(D3D12_FEATURE_DATA_FORMAT_SUPPORT)).Assert(); - return (d3D12FeatureDataFormatSupport.Support1 & d3D12FormatSupport1) != 0; + return (d3D12FeatureDataFormatSupport.Support1 & d3D12FormatSupport1) == d3D12FormatSupport1; } } } diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs index 600ded21..b2c12d7e 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs @@ -152,13 +152,13 @@ namespace ComputeSharp internal bool IsCacheCoherentUMA { get; } /// - /// Checks whether the current device supports the creation of - /// resources for a specified type . + /// 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. + /// Whether instances can be created by the current device. [Pure] - public bool IsTexture2DSupportedForType() + public bool IsReadOnlyTexture2DSupportedForType() where T : unmanaged { ThrowIfDisposed(); @@ -167,13 +167,30 @@ namespace ComputeSharp } /// - /// Checks whether the current device supports the creation of - /// resources for a specified type . + /// 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. + /// Whether instances can be created by the current device. [Pure] - public bool IsTexture3DSupportedForType() + public bool IsReadWriteTexture2DSupportedForType() + where T : unmanaged + { + ThrowIfDisposed(); + + return this.d3D12Device.Get()->IsDxgiFormatSupported( + DXGIFormatHelper.GetForType(), + D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW); + } + + /// + /// 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. + [Pure] + public bool IsReadOnlyTexture3DSupportedForType() where T : unmanaged { ThrowIfDisposed(); @@ -181,6 +198,23 @@ namespace ComputeSharp return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE3D); } + /// + /// 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. + [Pure] + public bool IsReadWriteTexture3DSupportedForType() + where T : unmanaged + { + ThrowIfDisposed(); + + return this.d3D12Device.Get()->IsDxgiFormatSupported( + DXGIFormatHelper.GetForType(), + D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW); + } + /// internal void RentShaderResourceViewDescriptorHandles( out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs index 6ea4d30e..8780a646 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/Texture2D{T}.cs @@ -10,7 +10,6 @@ using ComputeSharp.Interop; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; -using static TerraFX.Interop.D3D12_FORMAT_SUPPORT1; using static TerraFX.Interop.D3D12_RESOURCE_STATES; using static TerraFX.Interop.D3D12_SRV_DIMENSION; using static TerraFX.Interop.D3D12_UAV_DIMENSION; @@ -60,14 +59,15 @@ namespace ComputeSharp.Resources /// The height of the texture. /// The width of the texture. /// The resource type for the current texture. - private protected Texture2D(GraphicsDevice device, int width, int height, ResourceType resourceType) + /// The format support for the current texture type. + private protected Texture2D(GraphicsDevice device, int width, int height, ResourceType resourceType, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport) { 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)) + if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), d3D12FormatSupport)) { UnsupportedTextureTypeException.ThrowForTexture2D(); } diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs index 207afc44..fa5a08c7 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs @@ -10,7 +10,6 @@ using ComputeSharp.Interop; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; -using static TerraFX.Interop.D3D12_FORMAT_SUPPORT1; using static TerraFX.Interop.D3D12_RESOURCE_STATES; using static TerraFX.Interop.D3D12_SRV_DIMENSION; using static TerraFX.Interop.D3D12_UAV_DIMENSION; @@ -61,7 +60,8 @@ namespace ComputeSharp.Resources /// 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) + /// The format support for the current texture type. + private protected Texture3D(GraphicsDevice device, int width, int height, int depth, ResourceType resourceType, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport) { device.ThrowIfDisposed(); @@ -69,7 +69,7 @@ namespace ComputeSharp.Resources 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_TEXTURE2D)) + if (!device.D3D12Device->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), d3D12FormatSupport)) { UnsupportedTextureTypeException.ThrowForTexture2D(); } diff --git a/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture2D{T,TPixel}.cs b/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture2D{T,TPixel}.cs index e2a1ab08..e4709634 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture2D{T,TPixel}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture2D{T,TPixel}.cs @@ -4,6 +4,7 @@ using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Resources.Enums; using ComputeSharp.Resources; using ComputeSharp.Resources.Views; +using static TerraFX.Interop.D3D12_FORMAT_SUPPORT1; #pragma warning disable CS0618 @@ -27,7 +28,7 @@ namespace ComputeSharp /// The width of the texture. /// The height of the texture. internal ReadOnlyTexture2D(GraphicsDevice device, int width, int height) - : base(device, width, height, ResourceType.ReadOnly) + : base(device, width, height, ResourceType.ReadOnly, D3D12_FORMAT_SUPPORT1_TEXTURE2D) { } diff --git a/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture2D{T}.cs index 3d94f2f0..564d6453 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture2D{T}.cs @@ -3,6 +3,7 @@ using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Resources.Enums; using ComputeSharp.Resources; using ComputeSharp.Resources.Views; +using static TerraFX.Interop.D3D12_FORMAT_SUPPORT1; namespace ComputeSharp { @@ -22,7 +23,7 @@ namespace ComputeSharp /// The width of the texture. /// The height of the texture. internal ReadOnlyTexture2D(GraphicsDevice device, int width, int height) - : base(device, width, height, ResourceType.ReadOnly) + : base(device, width, height, ResourceType.ReadOnly, D3D12_FORMAT_SUPPORT1_TEXTURE2D) { } diff --git a/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture3D{T,TPixel}.cs b/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture3D{T,TPixel}.cs index 07d4d98a..6b632835 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture3D{T,TPixel}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture3D{T,TPixel}.cs @@ -4,6 +4,7 @@ using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Resources.Enums; using ComputeSharp.Resources; using ComputeSharp.Resources.Views; +using static TerraFX.Interop.D3D12_FORMAT_SUPPORT1; #pragma warning disable CS0618 @@ -28,7 +29,7 @@ namespace ComputeSharp /// The height of the texture. /// The depth of the texture. internal ReadOnlyTexture3D(GraphicsDevice device, int width, int height, int depth) - : base(device, width, height, depth, ResourceType.ReadOnly) + : base(device, width, height, depth, ResourceType.ReadOnly, D3D12_FORMAT_SUPPORT1_TEXTURE3D) { } diff --git a/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture3D{T}.cs b/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture3D{T}.cs index 7dc1995e..089a0f5d 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadOnlyTexture3D{T}.cs @@ -3,6 +3,7 @@ using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Resources.Enums; using ComputeSharp.Resources; using ComputeSharp.Resources.Views; +using static TerraFX.Interop.D3D12_FORMAT_SUPPORT1; namespace ComputeSharp { @@ -23,7 +24,7 @@ namespace ComputeSharp /// The height of the texture. /// The depth of the texture. internal ReadOnlyTexture3D(GraphicsDevice device, int width, int height, int depth) - : base(device, width, height, depth, ResourceType.ReadOnly) + : base(device, width, height, depth, ResourceType.ReadOnly, D3D12_FORMAT_SUPPORT1_TEXTURE3D) { } diff --git a/src/ComputeSharp.Graphics/Resources/ReadWriteTexture2D{T,TPixel}.cs b/src/ComputeSharp.Graphics/Resources/ReadWriteTexture2D{T,TPixel}.cs index 28366eb3..d120a371 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadWriteTexture2D{T,TPixel}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadWriteTexture2D{T,TPixel}.cs @@ -4,6 +4,7 @@ using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Resources.Enums; using ComputeSharp.Resources; using ComputeSharp.Resources.Views; +using static TerraFX.Interop.D3D12_FORMAT_SUPPORT1; #pragma warning disable CS0618 @@ -27,7 +28,7 @@ namespace ComputeSharp /// The width of the texture. /// The height of the texture. internal ReadWriteTexture2D(GraphicsDevice device, int width, int height) - : base(device, width, height, ResourceType.ReadWrite) + : base(device, width, height, ResourceType.ReadWrite, D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW) { } diff --git a/src/ComputeSharp.Graphics/Resources/ReadWriteTexture2D{T}.cs b/src/ComputeSharp.Graphics/Resources/ReadWriteTexture2D{T}.cs index bbf43edc..b8e83351 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadWriteTexture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadWriteTexture2D{T}.cs @@ -3,6 +3,7 @@ using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Resources.Enums; using ComputeSharp.Resources; using ComputeSharp.Resources.Views; +using static TerraFX.Interop.D3D12_FORMAT_SUPPORT1; namespace ComputeSharp { @@ -22,7 +23,7 @@ namespace ComputeSharp /// The width of the texture. /// The height of the texture. internal ReadWriteTexture2D(GraphicsDevice device, int width, int height) - : base(device, width, height, ResourceType.ReadWrite) + : base(device, width, height, ResourceType.ReadWrite, D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW) { } diff --git a/src/ComputeSharp.Graphics/Resources/ReadWriteTexture3D{T,TPixel}.cs b/src/ComputeSharp.Graphics/Resources/ReadWriteTexture3D{T,TPixel}.cs index 7a0caaa2..ce4026bd 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadWriteTexture3D{T,TPixel}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadWriteTexture3D{T,TPixel}.cs @@ -4,6 +4,7 @@ using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Resources.Enums; using ComputeSharp.Resources; using ComputeSharp.Resources.Views; +using static TerraFX.Interop.D3D12_FORMAT_SUPPORT1; #pragma warning disable CS0618 @@ -28,7 +29,7 @@ namespace ComputeSharp /// The height of the texture. /// The depth of the texture. internal ReadWriteTexture3D(GraphicsDevice device, int width, int height, int depth) - : base(device, width, height, depth, ResourceType.ReadWrite) + : base(device, width, height, depth, ResourceType.ReadWrite, D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW) { } diff --git a/src/ComputeSharp.Graphics/Resources/ReadWriteTexture3D{T}.cs b/src/ComputeSharp.Graphics/Resources/ReadWriteTexture3D{T}.cs index 631d72bf..1a4b88e1 100644 --- a/src/ComputeSharp.Graphics/Resources/ReadWriteTexture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/ReadWriteTexture3D{T}.cs @@ -3,6 +3,7 @@ using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Resources.Enums; using ComputeSharp.Resources; using ComputeSharp.Resources.Views; +using static TerraFX.Interop.D3D12_FORMAT_SUPPORT1; namespace ComputeSharp { @@ -23,7 +24,7 @@ namespace ComputeSharp /// The height of the texture. /// The depth of the texture. internal ReadWriteTexture3D(GraphicsDevice device, int width, int height, int depth) - : base(device, width, height, depth, ResourceType.ReadWrite) + : base(device, width, height, depth, ResourceType.ReadWrite, D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW) { }