Added ThrowHelper type
このコミットが含まれているのは:
1 親
0c3f8f92c3
コミット
3d48a6fb0c
11ファイル変更
+125
-27
検索条件に一致するファイルはありません
@@ -1,6 +1,7 @@
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Core.Helpers;
|
||||
using HRESULT = System.Int32;
|
||||
|
||||
namespace ComputeSharp.Graphics.Helpers
|
||||
@@ -21,9 +22,7 @@ namespace ComputeSharp.Graphics.Helpers
|
||||
{
|
||||
if (result < 0)
|
||||
{
|
||||
static void Throw(HRESULT result) => throw new Win32Exception(result);
|
||||
|
||||
Throw(result);
|
||||
ThrowHelper.ThrowWin32Exception(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace ComputeSharp.Core.Helpers
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods to efficiently throw exceptions.
|
||||
/// </summary>
|
||||
internal static partial class ThrowHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Throws a new <see cref="ArgumentException"/>.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentException">Thrown with no parameters.</exception>
|
||||
[DoesNotReturn]
|
||||
public static void ThrowArgumentException()
|
||||
{
|
||||
throw new ArgumentException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws a new <see cref="ArgumentException"/>.
|
||||
/// </summary>
|
||||
/// <param name="message">The message to include in the exception.</param>
|
||||
/// <exception cref="ArgumentException">Thrown with the specified parameter.</exception>
|
||||
[DoesNotReturn]
|
||||
public static void ThrowArgumentException(string message)
|
||||
{
|
||||
throw new ArgumentException(message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws a new <see cref="InvalidOperationException"/>.
|
||||
/// </summary>
|
||||
/// <param name="message">The message to include in the exception.</param>
|
||||
/// <exception cref="InvalidOperationException">Thrown with the specified parameter.</exception>
|
||||
[DoesNotReturn]
|
||||
public static void ThrowInvalidOperationException(string message)
|
||||
{
|
||||
throw new InvalidOperationException(message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws a new <see cref="Win32Exception"/>.
|
||||
/// </summary>
|
||||
/// <param name="error">The Win32 error code associated with this exception.</param>
|
||||
/// <exception cref="Win32Exception">Thrown with the specified parameter.</exception>
|
||||
[DoesNotReturn]
|
||||
public static void ThrowWin32Exception(int error)
|
||||
{
|
||||
throw new Win32Exception(error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws a new <see cref="ArgumentException"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of expected result.</typeparam>
|
||||
/// <exception cref="ArgumentException">Thrown with no parameters.</exception>
|
||||
/// <returns>This method always throws, so it actually never returns a value.</returns>
|
||||
[DoesNotReturn]
|
||||
public static T ThrowArgumentException<T>()
|
||||
{
|
||||
throw new ArgumentException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws a new <see cref="ArgumentException"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of expected result.</typeparam>
|
||||
/// <param name="message">The message to include in the exception.</param>
|
||||
/// <exception cref="ArgumentException">Thrown with the specified parameter.</exception>
|
||||
/// <returns>This method always throws, so it actually never returns a value.</returns>
|
||||
[DoesNotReturn]
|
||||
public static T ThrowArgumentException<T>(string message)
|
||||
{
|
||||
throw new ArgumentException(message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws a new <see cref="NotSupportedException"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of expected result.</typeparam>
|
||||
/// <param name="message">The message to include in the exception.</param>
|
||||
/// <exception cref="NotSupportedException">Thrown with the specified parameter.</exception>
|
||||
/// <returns>This method always throws, so it actually never returns a value.</returns>
|
||||
[DoesNotReturn]
|
||||
public static T ThrowNotSupportedException<T>(string message)
|
||||
{
|
||||
throw new NotSupportedException(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ using static TerraFX.Interop.D3D12_HEAP_TYPE;
|
||||
using static TerraFX.Interop.D3D12_RESOURCE_FLAGS;
|
||||
using static TerraFX.Interop.D3D12_RESOURCE_STATES;
|
||||
using FX = TerraFX.Interop.Windows;
|
||||
using ComputeSharp.Core.Helpers;
|
||||
|
||||
namespace ComputeSharp.Graphics.Extensions
|
||||
{
|
||||
@@ -116,7 +117,7 @@ namespace ComputeSharp.Graphics.Extensions
|
||||
BufferType.ReadWrite => (D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COMMON),
|
||||
BufferType.ReadBack => (D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST),
|
||||
BufferType.Upload => (D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_GENERIC_READ),
|
||||
_ => throw null!
|
||||
_ => ThrowHelper.ThrowArgumentException<(D3D12_HEAP_TYPE, D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>()
|
||||
};
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = default;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using ComputeSharp.Core.Interop;
|
||||
using ComputeSharp.Core.Helpers;
|
||||
using ComputeSharp.Core.Interop;
|
||||
using ComputeSharp.Graphics.Commands;
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
@@ -137,7 +138,7 @@ namespace ComputeSharp.Graphics
|
||||
{
|
||||
D3D12_COMMAND_LIST_TYPE_COMPUTE => this.computeCommandAllocatorPool.GetCommandAllocator(this.d3D12Device, this.d3D12ComputeFence),
|
||||
D3D12_COMMAND_LIST_TYPE_COPY => this.copyCommandAllocatorPool.GetCommandAllocator(this.d3D12Device, this.d3D12CopyFence),
|
||||
_ => throw null!
|
||||
_ => ThrowHelper.ThrowArgumentException<ComPtr<ID3D12CommandAllocator>>()
|
||||
};
|
||||
}
|
||||
|
||||
@@ -178,7 +179,7 @@ namespace ComputeSharp.Graphics
|
||||
d3D12Fence = this.d3D12CopyFence;
|
||||
d3D12FenceValue = this.nextD3D12CopyFenceValue++;
|
||||
break;
|
||||
default: throw null!;
|
||||
default: ThrowHelper.ThrowArgumentException(); return;
|
||||
}
|
||||
|
||||
// Execute the command list and signal to the target fence
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.Contracts;
|
||||
using ComputeSharp.Core.Helpers;
|
||||
using TerraFX.Interop;
|
||||
using FX = TerraFX.Interop.Windows;
|
||||
using HRESULT = System.Int32;
|
||||
@@ -40,12 +41,7 @@ namespace ComputeSharp.Graphics.Helpers
|
||||
return new GraphicsDevice(d3d12device.Move(), &dxgiDescription1);
|
||||
}
|
||||
|
||||
static GraphicsDevice Throw()
|
||||
{
|
||||
throw new NotSupportedException("There isn't a supported GPU device on the current machine");
|
||||
}
|
||||
|
||||
return Throw();
|
||||
return ThrowHelper.ThrowNotSupportedException<GraphicsDevice>("There isn't a supported GPU device on the current machine");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.Core.Helpers;
|
||||
|
||||
namespace System.Reflection.Emit
|
||||
{
|
||||
@@ -23,7 +24,7 @@ namespace System.Reflection.Emit
|
||||
case PropertyInfo property when property.CanRead:
|
||||
il.EmitCall(property.GetMethod!.IsStatic ? OpCodes.Call : OpCodes.Callvirt, property.GetMethod, null);
|
||||
break;
|
||||
default: throw new ArgumentException($"The input {member.GetType()} instance can't be read");
|
||||
default: ThrowHelper.ThrowArgumentException("The input MemberInfo instance can't be read"); return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +59,7 @@ namespace System.Reflection.Emit
|
||||
6 => OpCodes.Ldc_I4_6,
|
||||
7 => OpCodes.Ldc_I4_7,
|
||||
8 => OpCodes.Ldc_I4_8,
|
||||
_ => throw new InvalidOperationException($"Invalid offset value [{offset}]")
|
||||
_ => ThrowHelper.ThrowArgumentException<OpCode>("Invalid offset value")
|
||||
});
|
||||
}
|
||||
else if (offset <= 127) il.Emit(OpCodes.Ldc_I4_S, (byte)offset);
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using ComputeSharp;
|
||||
using ComputeSharp.Core.Helpers;
|
||||
using ComputeSharp.Shaders.Mappings;
|
||||
using ComputeSharp.Shaders.Translation.Models;
|
||||
|
||||
@@ -97,7 +98,7 @@ namespace Microsoft.CodeAnalysis.CSharp.Syntax
|
||||
string methodName = $"{methodInfo.DeclaringType!.Name}_{methodInfo.Name}";
|
||||
method = (methodName, methodInfo);
|
||||
return SyntaxFactory.IdentifierName(methodName).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
|
||||
default: throw new InvalidOperationException($"Invalid symbol kind: {memberInfos.First().GetType()}");
|
||||
default: ThrowHelper.ThrowArgumentException("Invalid symbol kind"); return null!;
|
||||
}
|
||||
|
||||
// Handle the loaded info
|
||||
@@ -148,7 +149,7 @@ namespace Microsoft.CodeAnalysis.CSharp.Syntax
|
||||
isReadonly = !propertyInfo.CanWrite;
|
||||
memberInfo = propertyInfo;
|
||||
break;
|
||||
default: throw new InvalidOperationException($"Invalid symbol kind: {memberSymbol.Kind}");
|
||||
default: ThrowHelper.ThrowArgumentException("Invalid symbol kind"); return null!;
|
||||
}
|
||||
|
||||
// Handle the loaded info
|
||||
@@ -197,7 +198,7 @@ namespace Microsoft.CodeAnalysis.CSharp.Syntax
|
||||
case MethodInfo methodInfo:
|
||||
method = (name, methodInfo);
|
||||
return SyntaxFactory.InvocationExpression(SyntaxFactory.ParseExpression(name), node.ArgumentList).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
|
||||
default: throw new InvalidOperationException($"Invalid symbol kind: {result.Value.MemberInfo.GetType()}");
|
||||
default: ThrowHelper.ThrowArgumentException("Invalid symbol kind"); return null!;
|
||||
}
|
||||
|
||||
// Return the mapped expression
|
||||
@@ -263,7 +264,7 @@ namespace Microsoft.CodeAnalysis.CSharp.Syntax
|
||||
Bool b when b => SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression, SyntaxFactory.Token(SyntaxKind.TrueKeyword)),
|
||||
Bool b when !b => SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression, SyntaxFactory.Token(SyntaxKind.FalseKeyword)),
|
||||
IFormattable scalar => SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.ParseToken(scalar.ToString(null, CultureInfo.InvariantCulture))),
|
||||
_ => throw new InvalidOperationException($"Invalid field of type {memberInfo.MemberType}")
|
||||
_ => ThrowHelper.ThrowArgumentException<LiteralExpressionSyntax>("Invalid field type")
|
||||
};
|
||||
return expression.WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Metadata;
|
||||
using System.Text.RegularExpressions;
|
||||
using ComputeSharp.Core.Helpers;
|
||||
using ComputeSharp.Shaders.Mappings;
|
||||
using ComputeSharp.Shaders.Translation.Enums;
|
||||
using ICSharpCode.Decompiler;
|
||||
@@ -139,7 +140,7 @@ namespace ComputeSharp.Shaders.Translation
|
||||
{
|
||||
MethodType.Execute => GetSyntaxTreeForExecuteMethod(methodInfo),
|
||||
MethodType.Static => GetSyntaxTreeForStaticMethod(methodInfo),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(methodType), $"Invalid method type: {methodType}")
|
||||
_ => ThrowHelper.ThrowArgumentException<string>("Invalid method type")
|
||||
};
|
||||
|
||||
// Load the type syntax tree
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Reflection;
|
||||
using ComputeSharp.Core.Helpers;
|
||||
|
||||
namespace ComputeSharp.Shaders.Translation.Models
|
||||
{
|
||||
@@ -39,7 +40,7 @@ namespace ComputeSharp.Shaders.Translation.Models
|
||||
IsStatic = propertyInfo.GetMethod!.IsStatic;
|
||||
MemberType = propertyInfo.PropertyType;
|
||||
}
|
||||
else throw new InvalidOperationException("Field and property can't both be null at the same time");
|
||||
else ThrowHelper.ThrowArgumentException("Field and property can't both be null at the same time");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -78,7 +79,7 @@ namespace ComputeSharp.Shaders.Translation.Models
|
||||
{
|
||||
FieldInfo field => field.GetValue(instance)!,
|
||||
PropertyInfo property => property.GetValue(instance)!,
|
||||
_ => throw new InvalidOperationException("Field and property can't both be null at the same time")
|
||||
_ => ThrowHelper.ThrowArgumentException<object>("Field and property can't both be null at the same time")
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.Core.Helpers;
|
||||
using ComputeSharp.Graphics.Buffers.Abstract;
|
||||
using ComputeSharp.Shaders.Mappings;
|
||||
using ComputeSharp.Shaders.Translation.Models;
|
||||
@@ -129,7 +129,7 @@ namespace ComputeSharp.Shaders.Translation
|
||||
|
||||
this.totalVariablesByteSize += size;
|
||||
}
|
||||
else throw new InvalidOperationException($"Invalid captured member of type {member.MemberType}");
|
||||
else ThrowHelper.ThrowArgumentException("Invalid captured member type");
|
||||
}
|
||||
|
||||
il.Emit(OpCodes.Ret);
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text.RegularExpressions;
|
||||
using ComputeSharp.Core.Helpers;
|
||||
using ComputeSharp.Graphics.Extensions;
|
||||
using ComputeSharp.Shaders.Mappings;
|
||||
using ComputeSharp.Shaders.Renderer.Models.Fields;
|
||||
@@ -148,7 +149,10 @@ namespace ComputeSharp.Shaders.Translation
|
||||
BindingFlags.Public |
|
||||
BindingFlags.NonPublic).ToArray();
|
||||
|
||||
if (shaderFields.Count == 0) throw new InvalidOperationException("Empty shader body");
|
||||
if (shaderFields.Count == 0)
|
||||
{
|
||||
ThrowHelper.ThrowInvalidOperationException("The shader body must contain at least one field");
|
||||
}
|
||||
|
||||
// Descriptor for the buffer for captured scalar/vector variables
|
||||
D3D12_DESCRIPTOR_RANGE1 d3D12DescriptorRange1 = new(D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1, this.constantBuffersCount++);
|
||||
@@ -230,7 +234,7 @@ namespace ComputeSharp.Shaders.Translation
|
||||
// Captured static delegates with a return type
|
||||
LoadStaticMethodSource(shader, fieldName, func.Method);
|
||||
}
|
||||
else throw new ArgumentException($"Invalid captured variable of type {fieldType} with name \"{memberInfo.Name}\"");
|
||||
else ThrowHelper.ThrowArgumentException("Invalid captured variable");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする