From d967fb480a583a8ce9f5f04edb9ede19171cd33a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 18 Mar 2021 13:49:52 +0100 Subject: [PATCH] Added better exception for missing shader metadata --- .../ComputeSharp.SourceGenerators.csproj | 1 + .../IComputeShaderSourceAttribute.cs | 11 ++++- .../MissingShaderMetadataException.cs | 47 +++++++++++++++++++ .../Graphics/Resources/Views/ThrowHelper.cs | 16 ------- 4 files changed, 57 insertions(+), 18 deletions(-) create mode 100644 src/ComputeSharp/Core/Exceptions/MissingShaderMetadataException.cs diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index 6bbcdcc6..0530ae6d 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -25,6 +25,7 @@ + diff --git a/src/ComputeSharp/Core/Attributes/__Internals/IComputeShaderSourceAttribute.cs b/src/ComputeSharp/Core/Attributes/__Internals/IComputeShaderSourceAttribute.cs index 221215a2..27fa5ed8 100644 --- a/src/ComputeSharp/Core/Attributes/__Internals/IComputeShaderSourceAttribute.cs +++ b/src/ComputeSharp/Core/Attributes/__Internals/IComputeShaderSourceAttribute.cs @@ -4,6 +4,7 @@ using System.ComponentModel; using System.Diagnostics.Contracts; using System.Linq; using System.Reflection; +using ComputeSharp.Exceptions; namespace ComputeSharp.__Internals { @@ -95,9 +96,15 @@ namespace ComputeSharp.__Internals [Pure] internal static IComputeShaderSourceAttribute GetForType() { - Type shaderType = typeof(T); + IEnumerable attributes = typeof(T).Assembly.GetCustomAttributes(); + IComputeShaderSourceAttribute? attribute = attributes.SingleOrDefault(static attribute => attribute.ShaderType == typeof(T)); - return shaderType.Assembly.GetCustomAttributes().Single(attribute => attribute.ShaderType == shaderType); + if (attribute is null) + { + MissingShaderMetadataException.Throw(); + } + + return attribute!; } } } \ No newline at end of file diff --git a/src/ComputeSharp/Core/Exceptions/MissingShaderMetadataException.cs b/src/ComputeSharp/Core/Exceptions/MissingShaderMetadataException.cs new file mode 100644 index 00000000..c4b9f7eb --- /dev/null +++ b/src/ComputeSharp/Core/Exceptions/MissingShaderMetadataException.cs @@ -0,0 +1,47 @@ +using System; +using System.Diagnostics.Contracts; +using System.Text; + +namespace ComputeSharp.Exceptions +{ + /// + /// A custom type that indicates when metadata is missing for a given shader. + /// + public sealed class MissingShaderMetadataException : Exception + { + /// + /// Creates a new instance. + /// + /// The error message to include in the exception.. + private MissingShaderMetadataException(string error) + : base(error) + { + } + + /// + /// Gets a formatted exception message for a given error. + /// + /// The type of shader that was failed to be loaded. + /// A new instance with a formatted error message. + [Pure] + private static MissingShaderMetadataException Create(Type type) + { + StringBuilder builder = new(512); + + builder.AppendLine($"Failed to retrieve the shader metadata for type {type}."); + builder.AppendLine("This usually indicates that the necessary source generator failed to run. Make sure to be using a IDE with full support " + + "for source generators, and that there are no build warnings in the errors window upon building your project. If a source " + + "generator fails to run correctly, please open an issue at https://github.com/Sergio0694/ComputeSharp and include a working repro along with it."); + + return new(builder.ToString()); + } + + /// + /// Throws a new for a given delegate. + /// + internal static void Throw() + { + throw Create(typeof(T)); + } + } +} diff --git a/src/ComputeSharp/Graphics/Resources/Views/ThrowHelper.cs b/src/ComputeSharp/Graphics/Resources/Views/ThrowHelper.cs index 9c7d9a94..fb0a67ac 100644 --- a/src/ComputeSharp/Graphics/Resources/Views/ThrowHelper.cs +++ b/src/ComputeSharp/Graphics/Resources/Views/ThrowHelper.cs @@ -58,21 +58,5 @@ namespace ComputeSharp.Resources.Views { throw new ArgumentOutOfRangeException("z"); } - - /// - /// Throws an when the "height" parameter is invalid. - /// - public static void ThrowArgumentOutOfRangeExceptionForHeight() - { - throw new ArgumentOutOfRangeException("height"); - } - - /// - /// Throws an when the "width" parameter is invalid. - /// - public static void ThrowArgumentOutOfRangeExceptionForWidth() - { - throw new ArgumentOutOfRangeException("width"); - } } }