Added better exception for missing shader metadata

このコミットが含まれているのは:
Sergio Pedri committed 2021-03-18 13:49:52 +01:00
1 親 804d64e81e
コミット d967fb480a
4ファイル変更
+57 -18

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

+1
ファイルの表示
@@ -25,6 +25,7 @@
<Compile Include="..\ComputeSharp\Core\Dispatch\ThreadIds.Normalized.cs" Link="Core\Dispatch\ThreadIds.Normalized.cs" />
<Compile Include="..\ComputeSharp\Core\Exceptions\InvalidExecutionContextException.cs" Link="Core\Exceptions\InvalidExecutionContextException.cs" />
<Compile Include="..\ComputeSharp\Core\Exceptions\MissingMethodSourceException.cs" Link="Core\Exceptions\MissingMethodSourceException.cs" />
<Compile Include="..\ComputeSharp\Core\Exceptions\MissingShaderMetadataException.cs" Link="Core\Exceptions\MissingShaderMetadataException.cs" />
<Compile Include="..\ComputeSharp\Core\Extensions\MethodInfoExtensions.cs" Link="Core\Extensions\MethodInfoExtensions.cs" />
<Compile Include="..\ComputeSharp\Core\Helpers\AlignmentHelper.cs" Link="Core\Helpers\AlignmentHelper.cs" />
<Compile Include="..\ComputeSharp\Core\Interfaces\IComputeShader.cs" Link="Core\Interfaces\IComputeShader.cs" />
+9 -2
ファイルの表示
@@ -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<T>()
{
Type shaderType = typeof(T);
IEnumerable<IComputeShaderSourceAttribute> attributes = typeof(T).Assembly.GetCustomAttributes<IComputeShaderSourceAttribute>();
IComputeShaderSourceAttribute? attribute = attributes.SingleOrDefault(static attribute => attribute.ShaderType == typeof(T));
return shaderType.Assembly.GetCustomAttributes<IComputeShaderSourceAttribute>().Single(attribute => attribute.ShaderType == shaderType);
if (attribute is null)
{
MissingShaderMetadataException.Throw<T>();
}
return attribute!;
}
}
}
+47
ファイルの表示
@@ -0,0 +1,47 @@
using System;
using System.Diagnostics.Contracts;
using System.Text;
namespace ComputeSharp.Exceptions
{
/// <summary>
/// A custom <see cref="Exception"/> type that indicates when metadata is missing for a given shader.
/// </summary>
public sealed class MissingShaderMetadataException : Exception
{
/// <summary>
/// Creates a new <see cref="MissingShaderMetadataException"/> instance.
/// </summary>
/// <param name="error">The error message to include in the exception..</param>
private MissingShaderMetadataException(string error)
: base(error)
{
}
/// <summary>
/// Gets a formatted exception message for a given error.
/// </summary>
/// <param name="type">The type of shader that was failed to be loaded.</param>
/// <returns>A new <see cref="MissingShaderMetadataException"/> instance with a formatted error message.</returns>
[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());
}
/// <summary>
/// Throws a new <see cref="MissingShaderMetadataException"/> for a given delegate.
/// </summary>
internal static void Throw<T>()
{
throw Create(typeof(T));
}
}
}
-16
ファイルの表示
@@ -58,21 +58,5 @@ namespace ComputeSharp.Resources.Views
{
throw new ArgumentOutOfRangeException("z");
}
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> when the "height" parameter is invalid.
/// </summary>
public static void ThrowArgumentOutOfRangeExceptionForHeight()
{
throw new ArgumentOutOfRangeException("height");
}
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> when the "width" parameter is invalid.
/// </summary>
public static void ThrowArgumentOutOfRangeExceptionForWidth()
{
throw new ArgumentOutOfRangeException("width");
}
}
}