Added dedicated exception for missing method source

このコミットが含まれているのは:
Sergio Pedri committed 2020-12-01 18:16:44 +01:00
1 親 f4bd3f87b9
コミット d1e91d71ab
5ファイル変更
+75 -7

検索条件に一致するファイルはありません

+11 -5
ファイルの表示
@@ -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<ShaderMethodSourceAttribute>();
string methodName = function.Method.GetFullName();
return (
from attribute in function.Method.DeclaringType.Assembly.GetCustomAttributes<ShaderMethodSourceAttribute>()
where attribute.MethodName == methodName
select attribute).Single();
foreach (var attribute in attributes)
{
if (attribute.MethodName.Equals(methodName))
{
return attribute;
}
}
return MissingMethodSourceException.Throw(function);
}
}
}
+4 -1
ファイルの表示
@@ -11,6 +11,9 @@ namespace ComputeSharp.Exceptions
/// Creates a new <see cref="InvalidExecutionContextException"/> instance
/// </summary>
/// <param name="name">The name of the caller API</param>
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.")
{
}
}
}
+52
ファイルの表示
@@ -0,0 +1,52 @@
using System;
using System.Diagnostics.Contracts;
using System.Text;
using ComputeSharp.Core.Extensions;
#pragma warning disable CS0618
namespace ComputeSharp.Exceptions
{
/// <summary>
/// A custom <see cref="InvalidOperationException"/> that indicates when an HLSL-only API is called from C#
/// </summary>
public sealed class MissingMethodSourceException : InvalidOperationException
{
/// <summary>
/// Creates a new <see cref="MissingMethodSourceException"/> instance.
/// </summary>
/// <param name="message">The exception message to display.</param>
private MissingMethodSourceException(string message)
: base(message)
{
}
/// <summary>
/// Creates a new <see cref="MissingMethodSourceException"/> instance from the specified parameters.
/// </summary>
/// <param name="method">The target <see cref="Delegate"/> that couldn't be processed.</param>
/// <returns>A new <see cref="MissingMethodSourceException"/> instance with a formatted error message.</returns>
[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());
}
/// <summary>
/// Throws a new <see cref="MissingMethodSourceException"/> for a given delegate.
/// </summary>
/// <param name="method">The target <see cref="Delegate"/> that couldn't be processed.</param>
/// <returns>This method always throws and never actually returns anything.</returns>
internal static ShaderMethodSourceAttribute Throw(Delegate method)
{
throw Create(method);
}
}
}
+7
ファイルの表示
@@ -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})";
}
}
+1 -1
ファイルの表示
@@ -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());