diff --git a/src/ComputeSharp/Graphics/Exceptions/UnsupportedDoubleOperationsException.cs b/src/ComputeSharp/Graphics/Exceptions/UnsupportedDoubleOperationsException.cs new file mode 100644 index 00000000..e8bfe41d --- /dev/null +++ b/src/ComputeSharp/Graphics/Exceptions/UnsupportedDoubleOperationsException.cs @@ -0,0 +1,49 @@ +using System; +using System.Diagnostics.Contracts; +using System.Text; + +namespace ComputeSharp.Exceptions +{ + /// + /// A custom that indicates when a resource was attempted to be created with an unsupported type. + /// + public sealed class UnsupportedDoubleOperationsException : InvalidOperationException + { + /// + /// Creates a new instance. + /// + /// The message that describes the error. + public UnsupportedDoubleOperationsException(string message) + : base(message) + { + } + + /// + /// Creates a new instance from the specified parameters. + /// + /// The element type of resource that was being created. + /// A new instance with a formatted error message. + [Pure] + private static UnsupportedDoubleOperationsException Create(Type type) + { + StringBuilder builder = new(256); + + builder.AppendLine($"The device in use does not support creating resources of type {type}, as it does not support using double precision floating point numbers."); + builder.AppendLine("As a possible workaround, consider replacing usage of the double type with single precision floating point numbers."); + builder.AppendLine("Note that double precision operations are only partially supported on GPU devices, and are far slower than single precision ones."); + builder.ToString(); + + return new(builder.ToString()); + } + + /// + /// Throws a new instance from the specified parameters. + /// + /// The type of values in the resource that couldn't be created. + internal static void Throw() + where T : unmanaged + { + throw Create(typeof(T)); + } + } +} \ No newline at end of file diff --git a/src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs b/src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs new file mode 100644 index 00000000..3f1807c6 --- /dev/null +++ b/src/ComputeSharp/Graphics/Helpers/TypeInfo{T}.cs @@ -0,0 +1,42 @@ +using System; +using System.Diagnostics.Contracts; +using System.Reflection; + +namespace ComputeSharp.Graphics.Helpers +{ + /// + /// A class providing info on specific types. + /// + /// The input type to extract info for. + internal static class TypeInfo + where T : unmanaged + { + /// + /// Whether or not type is or contains a field. + /// + public static readonly bool IsDoubleOrContainsDoubles; + + /// + /// Checks the value for for type . + /// + /// The current type to check. + /// Whether or not is or contains a field. + [Pure] + private static bool ChecksIsDoubleOrContainsDoubles(Type type) + { + if (type == typeof(double)) return true; + + if (type.IsPrimitive) return false; + + foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) + { + if (ChecksIsDoubleOrContainsDoubles(fieldInfo.FieldType)) + { + return true; + } + } + + return false; + } + } +} diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index f1cca2ad..57723d53 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -3,9 +3,11 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Extensions; +using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; +using static TerraFX.Interop.D3D12_FEATURE; using FX = TerraFX.Interop.Windows; using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType; @@ -66,6 +68,12 @@ namespace ComputeSharp.Resources Guard.IsBetweenOrEqualTo(length, 1, (uint.MaxValue / elementSizeInBytes) & ~255, nameof(length)); } + if (TypeInfo.IsDoubleOrContainsDoubles && + device.D3D12Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS).DoublePrecisionFloatShaderOps == 0) + { + UnsupportedDoubleOperationsException.Throw(); + } + SizeInBytes = checked((nint)(length * elementSizeInBytes)); GraphicsDevice = device; Length = length;