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