From 914d56a3a86fb7e9c749c9613e24703b9051b78e Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 15 Dec 2020 17:08:33 +0100 Subject: [PATCH 1/3] Added APIs to check texture ND format support --- .../Extensions/ID3D12DeviceExtensions.cs | 19 +++++++++++ src/ComputeSharp.Graphics/GraphicsDevice.cs | 32 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index 5e3d17f6..8a6f278c 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -9,6 +9,7 @@ using static TerraFX.Interop.D3D12_COMMAND_QUEUE_PRIORITY; using static TerraFX.Interop.D3D12_CPU_PAGE_PROPERTY; using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_FLAGS; using static TerraFX.Interop.D3D12_DESCRIPTOR_HEAP_TYPE; +using static TerraFX.Interop.D3D12_FEATURE; using static TerraFX.Interop.D3D12_FENCE_FLAGS; using static TerraFX.Interop.D3D12_MEMORY_POOL; using static TerraFX.Interop.D3D12_HEAP_FLAGS; @@ -448,5 +449,23 @@ namespace ComputeSharp.Graphics.Extensions return feature; } + + /// + /// Checks whether or not a given DXGI format is supported for the specified resource type. + /// + /// The target to use to check features for. + /// The type to check support for. + /// The resource type to check support for. + /// Whether or not the input device supports the requested format for the specified resource type. + [Pure] + public static unsafe bool IsDxgiFormatSupported(this ref ID3D12Device d3D12Device, DXGI_FORMAT dxgiFormat, D3D12_FORMAT_SUPPORT1 d3D12FormatSupport1) + { + D3D12_FEATURE_DATA_FORMAT_SUPPORT d3D12FeatureDataFormatSupport = default; + d3D12FeatureDataFormatSupport.Format = dxgiFormat; + + d3D12Device.CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &d3D12FeatureDataFormatSupport, (uint)sizeof(D3D12_FEATURE_DATA_FORMAT_SUPPORT)).Assert(); + + return (d3D12FeatureDataFormatSupport.Support1 & d3D12FormatSupport1) != 0; + } } } diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs index a5e18802..92503ee7 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using ComputeSharp.Core.Extensions; using ComputeSharp.Graphics.Commands; @@ -10,6 +11,7 @@ using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; using static TerraFX.Interop.D3D12_FEATURE; +using static TerraFX.Interop.D3D12_FORMAT_SUPPORT1; namespace ComputeSharp.Graphics { @@ -126,6 +128,36 @@ namespace ComputeSharp.Graphics /// internal ID3D12Device* D3D12Device => this.d3D12Device; + /// + /// 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 IsTexture2DSupportedForType() + where T : unmanaged + { + ThrowIfDisposed(); + + return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE2D); + } + + /// + /// 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 IsTexture3DSupportedForType() + where T : unmanaged + { + ThrowIfDisposed(); + + return this.d3D12Device.Get()->IsDxgiFormatSupported(DXGIFormatHelper.GetForType(), D3D12_FORMAT_SUPPORT1_TEXTURE3D); + } + /// internal void RentShaderResourceViewDescriptorHandles( out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, From 1f1ba3532410eda99d367890547d54a97a176594 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 15 Dec 2020 17:09:15 +0100 Subject: [PATCH 2/3] Added format validation to create texture ND types --- .../Buffers/Abstract/Texture2D{T}.cs | 6 ++ .../Buffers/Abstract/Texture3D{T}.cs | 6 ++ .../GraphicsDeviceMismatchException.cs | 3 - .../UnsupportedTextureTypeException.cs | 70 +++++++++++++++++++ 4 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 src/ComputeSharp.Graphics/Exceptions/UnsupportedTextureTypeException.cs diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index 57cf2a07..963c3b5f 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -9,6 +9,7 @@ using ComputeSharp.Graphics.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; @@ -63,6 +64,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract 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)) + { + UnsupportedTextureTypeException.ThrowForTexture2D(); + } + GraphicsDevice = device; Width = width; Height = height; diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index db758741..6539319a 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -9,6 +9,7 @@ using ComputeSharp.Graphics.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; @@ -65,6 +66,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract 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)) + { + UnsupportedTextureTypeException.ThrowForTexture2D(); + } + GraphicsDevice = device; Width = width; Height = height; diff --git a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs index ef4e927d..6790ec38 100644 --- a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs +++ b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs @@ -52,7 +52,6 @@ namespace ComputeSharp.Exceptions /// The type of values in the input buffer. /// The input that was used. /// The target instance that was used. - [Pure] internal static void Throw(Buffer buffer, GraphicsDevice device) where T : unmanaged { @@ -65,7 +64,6 @@ namespace ComputeSharp.Exceptions /// 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 { @@ -78,7 +76,6 @@ namespace ComputeSharp.Exceptions /// 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 { diff --git a/src/ComputeSharp.Graphics/Exceptions/UnsupportedTextureTypeException.cs b/src/ComputeSharp.Graphics/Exceptions/UnsupportedTextureTypeException.cs new file mode 100644 index 00000000..e7f1e242 --- /dev/null +++ b/src/ComputeSharp.Graphics/Exceptions/UnsupportedTextureTypeException.cs @@ -0,0 +1,70 @@ +using System; +using System.Diagnostics.Contracts; +using System.Text; +using ComputeSharp.Graphics; +using Microsoft.Toolkit.Diagnostics; + +namespace ComputeSharp.Exceptions +{ + /// + /// A custom that indicates when a texture was attempted to be created with an unsupported type. + /// + public sealed class UnsupportedTextureTypeException : InvalidOperationException + { + /// + /// Creates a new instance. + /// + /// The message that describes the error. + private UnsupportedTextureTypeException(string message) + : base(message) + { + } + + /// + /// Creates a new instance from the specified parameters. + /// + /// The rank of the texture that couldn't be created. + /// The element type of texture that was being created. + /// A new instance with a formatted error message. + [Pure] + private static UnsupportedTextureTypeException Create(int rank, Type type) + { + StringBuilder builder = new(256); + + builder.AppendLine($"The device in use does not support creating {rank}D textures of type {type}."); + builder.Append($"Make sure to check the support at runtime by using {nameof(GraphicsDevice)}."); + + builder.AppendLine(rank switch + { + 2 => $"{nameof(GraphicsDevice.IsTexture2DSupportedForType)}().", + 3 => $"{nameof(GraphicsDevice.IsTexture3DSupportedForType)}().", + _ => 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.ToString(); + + 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 ThrowForTexture2D() + where T : unmanaged + { + throw Create(2, typeof(T)); + } + + /// + /// Throws a new instance from the specified parameters. + /// + /// The type of values in the texture that couldn't be created. + internal static void ThrowForTexture3D() + where T : unmanaged + { + throw Create(3, typeof(T)); + } + } +} \ No newline at end of file From 10a2a1c28b2bb5c187768ad7732c5111c873ade5 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 15 Dec 2020 17:10:45 +0100 Subject: [PATCH 3/3] Lowered requirement to FL11_0 and SM6_0 --- src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs | 4 ++-- src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs | 2 +- src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs index 0c6b73cb..daefc4ec 100644 --- a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.Enumerator.cs @@ -126,7 +126,7 @@ namespace ComputeSharp.Graphics.Helpers HRESULT createDeviceResult = FX.D3D12CreateDevice( dxgiAdapter1.AsIUnknown().Get(), - D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_12_0, + D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, FX.__uuidof(), null); @@ -137,7 +137,7 @@ namespace ComputeSharp.Graphics.Helpers FX.D3D12CreateDevice( dxgiAdapter1.AsIUnknown().Get(), - D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_12_0, + D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, FX.__uuidof(), d3D12Device.GetVoidAddressOf()).Assert(); diff --git a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs index 86c880b7..4f4e08fe 100644 --- a/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/DeviceHelper.cs @@ -95,7 +95,7 @@ namespace ComputeSharp.Graphics.Helpers HRESULT createDeviceResult = FX.D3D12CreateDevice( dxgiAdapter1.AsIUnknown().Get(), - D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_12_0, + D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0, FX.__uuidof(), (void**)d3D12Device); diff --git a/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs b/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs index d5e62752..38c19d3a 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs @@ -76,7 +76,7 @@ namespace ComputeSharp.Shaders.Translation // Try to compile the new compute shader fixed (char* shaderName = "") fixed (char* entryPoint = nameof(IComputeShader.Execute)) - fixed (char* shaderProfile = "cs_6_1") + fixed (char* shaderProfile = "cs_6_0") fixed (char* optimization = "-O3") { DxcCompiler.Get()->Compile(