From d1e91d71abd0bff929d88be0b0a9624a66afe497 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 1 Dec 2020 18:16:44 +0100 Subject: [PATCH] Added dedicated exception for missing method source --- .../Attributes/ShaderMethodSourceAttribute.cs | 16 ++++-- .../InvalidExecutionContextException.cs | 5 +- .../MissingMethodSourceException.cs | 52 +++++++++++++++++++ .../Extensions/MethodInfoExtensions.cs | 7 +++ .../GraphicsDeviceMismatchException.cs | 2 +- 5 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 src/ComputeSharp.Core/Exceptions/MissingMethodSourceException.cs diff --git a/src/ComputeSharp.Core/Attributes/ShaderMethodSourceAttribute.cs b/src/ComputeSharp.Core/Attributes/ShaderMethodSourceAttribute.cs index 508758f1..9fd1fbd1 100644 --- a/src/ComputeSharp.Core/Attributes/ShaderMethodSourceAttribute.cs +++ b/src/ComputeSharp.Core/Attributes/ShaderMethodSourceAttribute.cs @@ -2,9 +2,9 @@ using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics.Contracts; -using System.Linq; using System.Reflection; using ComputeSharp.Core.Extensions; +using ComputeSharp.Exceptions; using Microsoft.Toolkit.Diagnostics; namespace ComputeSharp @@ -81,12 +81,18 @@ namespace ComputeSharp { Guard.IsTrue(function.Method.IsStatic, "Captured delegates need to wrap static methods"); + var attributes = function.Method.DeclaringType.Assembly.GetCustomAttributes(); string methodName = function.Method.GetFullName(); - return ( - from attribute in function.Method.DeclaringType.Assembly.GetCustomAttributes() - where attribute.MethodName == methodName - select attribute).Single(); + foreach (var attribute in attributes) + { + if (attribute.MethodName.Equals(methodName)) + { + return attribute; + } + } + + return MissingMethodSourceException.Throw(function); } } } \ No newline at end of file diff --git a/src/ComputeSharp.Core/Exceptions/InvalidExecutionContextException.cs b/src/ComputeSharp.Core/Exceptions/InvalidExecutionContextException.cs index 00a70c3e..d7401c19 100644 --- a/src/ComputeSharp.Core/Exceptions/InvalidExecutionContextException.cs +++ b/src/ComputeSharp.Core/Exceptions/InvalidExecutionContextException.cs @@ -11,6 +11,9 @@ namespace ComputeSharp.Exceptions /// Creates a new instance /// /// The name of the caller API - internal InvalidExecutionContextException(string name) : base($"The API {name} can only be used from within an HLSL shader") { } + internal InvalidExecutionContextException(string name) + : base($"The API {name} can only be used from within an HLSL shader.") + { + } } } diff --git a/src/ComputeSharp.Core/Exceptions/MissingMethodSourceException.cs b/src/ComputeSharp.Core/Exceptions/MissingMethodSourceException.cs new file mode 100644 index 00000000..ddb6232e --- /dev/null +++ b/src/ComputeSharp.Core/Exceptions/MissingMethodSourceException.cs @@ -0,0 +1,52 @@ +using System; +using System.Diagnostics.Contracts; +using System.Text; +using ComputeSharp.Core.Extensions; + +#pragma warning disable CS0618 + +namespace ComputeSharp.Exceptions +{ + /// + /// A custom that indicates when an HLSL-only API is called from C# + /// + public sealed class MissingMethodSourceException : InvalidOperationException + { + /// + /// Creates a new instance. + /// + /// The exception message to display. + private MissingMethodSourceException(string message) + : base(message) + { + } + + /// + /// Creates a new instance from the specified parameters. + /// + /// The target that couldn't be processed. + /// A new instance with a formatted error message. + [Pure] + private static MissingMethodSourceException Create(Delegate method) + { + StringBuilder builder = new(512); + + builder.AppendLine("The processed source for a method captured as a delegate in a shader was not found."); + builder.AppendLine($"The delegate was of type {method.GetType()} and it was wrapping the method {method.Method.GetFullName()}."); + builder.Append($"Make sure that the target method is annotated with [{nameof(ShaderMethodAttribute)}]."); + builder.ToString(); + + return new(builder.ToString()); + } + + /// + /// Throws a new for a given delegate. + /// + /// The target that couldn't be processed. + /// This method always throws and never actually returns anything. + internal static ShaderMethodSourceAttribute Throw(Delegate method) + { + throw Create(method); + } + } +} diff --git a/src/ComputeSharp.Core/Extensions/MethodInfoExtensions.cs b/src/ComputeSharp.Core/Extensions/MethodInfoExtensions.cs index 1a09359b..b413e1b3 100644 --- a/src/ComputeSharp.Core/Extensions/MethodInfoExtensions.cs +++ b/src/ComputeSharp.Core/Extensions/MethodInfoExtensions.cs @@ -19,6 +19,13 @@ namespace ComputeSharp.Core.Extensions { var parameters = string.Join(", ", method.GetParameters().Select(static p => p.ParameterType.FullName)); + if (method.IsGenericMethod) + { + var arguments = string.Join(", ", method.GetGenericArguments().Select(static t => t.FullName)); + + return $"{method.DeclaringType!.FullName}.{method.Name}<{arguments}>({parameters})"; + } + return $"{method.DeclaringType!.FullName}.{method.Name}({parameters})"; } } diff --git a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs index 9fb31b09..edf3b8fe 100644 --- a/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs +++ b/src/ComputeSharp.Graphics/Exceptions/GraphicsDeviceMismatchException.cs @@ -36,7 +36,7 @@ namespace ComputeSharp.Exceptions builder.AppendLine("Invalid pairing of graphics devices used to run a compute shader and allocate memory buffers."); builder.AppendLine($"The target device to run the compute shader is \"{device}\"."); builder.AppendLine($"The buffer of type {buffer.GetType()} was allocated on device \"{buffer.GraphicsDevice}\"."); - builder.AppendLine("Make sure to always allocate buffers on the same device used to actually run the code that accesses them."); + builder.Append("Make sure to always allocate buffers on the same device used to actually run the code that accesses them."); builder.ToString(); return new(builder.ToString());