diff --git a/src/ComputeSharp.Core/Polyfills/ArgumentExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/ArgumentExceptionExtensions.cs index 3f7a7699..e2782394 100644 --- a/src/ComputeSharp.Core/Polyfills/ArgumentExceptionExtensions.cs +++ b/src/ComputeSharp.Core/Polyfills/ArgumentExceptionExtensions.cs @@ -15,7 +15,7 @@ internal static class ArgumentExceptionExtensions /// The condition to decide whether to throw the exception. /// The name of the parameter with which corresponds. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void ThrowIf(this ArgumentException? _, bool condition, string? parameterName = null) + public static void ThrowIf(this ArgumentException? _, [DoesNotReturnIf(true)] bool condition, string? parameterName = null) { if (condition) { diff --git a/src/ComputeSharp.Core/Polyfills/InvalidOperationExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/InvalidOperationExceptionExtensions.cs new file mode 100644 index 00000000..9028f05b --- /dev/null +++ b/src/ComputeSharp.Core/Polyfills/InvalidOperationExceptionExtensions.cs @@ -0,0 +1,34 @@ +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +namespace System; + +/// +/// Throw helper extensions for . +/// +internal static class InvalidOperationExceptionExtensions +{ + /// + /// Throws an if is . + /// + /// Dummy value to invoke the extension upon (always pass . + /// The condition to decide whether to throw the exception. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIf(this InvalidOperationException? _, [DoesNotReturnIf(true)] bool condition) + { + if (condition) + { + Throw(); + } + } + + /// + /// Throws an for a given parameter name. + /// + /// Always thrown with no parameters. + [DoesNotReturn] + private static void Throw() + { + throw new InvalidOperationException(); + } +} diff --git a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs index a4cb2df8..11a4e2cf 100644 --- a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs +++ b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs @@ -1,5 +1,4 @@ using System; -using CommunityToolkit.Diagnostics; using ComputeSharp.Graphics.Extensions; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -94,10 +93,7 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable { lock (this.d3D12DescriptorHandlePairs) { - if (this.size <= 0) - { - ThrowHelper.ThrowInvalidOperationException("There are no descriptor handles left in the pool."); - } + default(InvalidOperationException).ThrowIf(this.size <= 0); d3D12ResourceDescriptorHandles = this.d3D12DescriptorHandlePairs[this.head++]; @@ -118,10 +114,7 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable { lock (this.d3D12DescriptorHandlePairs) { - if (this.size >= DescriptorsPerHeap) - { - ThrowHelper.ThrowInvalidOperationException("The pool of descriptor heaps is already full."); - } + default(InvalidOperationException).ThrowIf(this.size >= DescriptorsPerHeap); this.d3D12DescriptorHandlePairs[this.tail++] = d3D12ResourceDescriptorHandles; diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs index 3debaf70..4a551e48 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs @@ -105,10 +105,7 @@ internal static partial class DeviceHelper d3D12DeviceCandidate.GetVoidAddressOf()); // Check and throw if the device is a device lost state and not disposed properly - if (createDeviceResult.IsDeviceLostReason()) - { - ThrowHelper.ThrowInvalidOperationException("The default device is in device lost state and has not been disposed properly."); - } + default(InvalidOperationException).ThrowIf(createDeviceResult.IsDeviceLostReason()); // Check for success, and then also filter out devices that support SM6.0. This can // only be checked on a concrete ID3D12Device instance, so it can't be done earlier. @@ -151,10 +148,7 @@ internal static partial class DeviceHelper Windows.__uuidof(), d3D12DeviceCandidate.GetVoidAddressOf()); - if (createDeviceResult.IsDeviceLostReason()) - { - ThrowHelper.ThrowInvalidOperationException("The default device is in device lost state and has not been disposed properly."); - } + default(InvalidOperationException).ThrowIf(createDeviceResult.IsDeviceLostReason()); // There is no need to check for SM6.0, as it's guaranteed to be supported by Windows if (Windows.SUCCEEDED(createDeviceResult)) diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index ad82f2db..b7be311f 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -2,7 +2,6 @@ using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Threading.Tasks; -using CommunityToolkit.Diagnostics; using ComputeSharp.__Internals; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; @@ -60,7 +59,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable { get { - ThrowInvalidOperationExceptionIfDeviceIsNull(); + default(InvalidOperationException).ThrowIf(this.device is null); return this.device; } @@ -72,7 +71,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable /// The to insert the barrier for. internal readonly unsafe void Barrier(ID3D12Resource* d3D12Resource) { - ThrowInvalidOperationExceptionIfDeviceIsNull(); + default(InvalidOperationException).ThrowIf(this.device is null); ref CommandList commandList = ref GetCommandList(); @@ -92,7 +91,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, bool isNormalized) { - ThrowInvalidOperationExceptionIfDeviceIsNull(); + default(InvalidOperationException).ThrowIf(this.device is null); ref CommandList commandList = ref GetCommandList(pipelineState: null); @@ -112,7 +111,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, Float4 value) { - ThrowInvalidOperationExceptionIfDeviceIsNull(); + default(InvalidOperationException).ThrowIf(this.device is null); ref CommandList commandList = ref GetCommandList(pipelineState: null); @@ -179,8 +178,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable ref T shader) where T : struct, IComputeShader { - ThrowInvalidOperationExceptionIfDeviceIsNull(); - + default(InvalidOperationException).ThrowIf(this.device is null); default(ArgumentOutOfRangeException).ThrowIfNegativeOrZero(x); default(ArgumentOutOfRangeException).ThrowIfNegativeOrZero(y); default(ArgumentOutOfRangeException).ThrowIfNegativeOrZero(z); @@ -221,7 +219,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable where T : struct, IPixelShader where TPixel : unmanaged { - ThrowInvalidOperationExceptionIfDeviceIsNull(); + default(InvalidOperationException).ThrowIf(this.device is null); int x = texture.Width; int y = texture.Height; @@ -263,7 +261,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable D3D12_RESOURCE_STATES d3D12ResourceStatesBefore, D3D12_RESOURCE_STATES d3D12ResourceStatesAfter) { - ThrowInvalidOperationExceptionIfDeviceIsNull(); + default(InvalidOperationException).ThrowIf(this.device is null); ref CommandList commandList = ref GetCommandList(pipelineState: null); @@ -274,7 +272,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Dispose() { - ThrowInvalidOperationExceptionIfDeviceIsNull(); + default(InvalidOperationException).ThrowIf(this.device is null); GraphicsDevice device = this.device; @@ -301,7 +299,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable [MethodImpl(MethodImplOptions.AggressiveInlining)] public ValueTask DisposeAsync() { - ThrowInvalidOperationExceptionIfDeviceIsNull(); + default(InvalidOperationException).ThrowIf(this.device is null); GraphicsDevice device = this.device; @@ -342,10 +340,7 @@ public struct ComputeContext : IDisposable, IAsyncDisposable // original context would not see the changes done by the following queued dispatches. ref CommandList commandList = ref Unsafe.AsRef(in this.commandList); - if (!commandList.IsAllocated) - { - ThrowHelper.ThrowInvalidOperationException("The current compute context has not yet been initialized."); - } + default(InvalidOperationException).ThrowIf(!commandList.IsAllocated); return ref commandList; } @@ -377,18 +372,4 @@ public struct ComputeContext : IDisposable, IAsyncDisposable return ref commandList; } - - /// - /// Throws an if is . - /// - /// Thrown if is . - [MemberNotNull(nameof(device))] - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private readonly void ThrowInvalidOperationExceptionIfDeviceIsNull() - { - if (this.device is null) - { - ThrowHelper.ThrowInvalidOperationException("The current compute context is not in a valid state."); - } - } } \ No newline at end of file diff --git a/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs b/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs index 4d72f814..c4c02754 100644 --- a/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs +++ b/src/ComputeSharp/Shaders/Dispatching/ShaderBytecodeLoader.cs @@ -38,10 +38,7 @@ internal struct ShaderBytecodeLoader : IBytecodeLoader /// public unsafe void LoadDynamicBytecode(IntPtr handle) { - if (this.cachedShader is not null) - { - ThrowHelper.ThrowInvalidOperationException("The shader has already been initialized."); - } + default(InvalidOperationException).ThrowIf(this.cachedShader is not null); if (handle == IntPtr.Zero) { @@ -55,10 +52,7 @@ internal struct ShaderBytecodeLoader : IBytecodeLoader [MethodImpl(MethodImplOptions.AggressiveInlining)] public void LoadEmbeddedBytecode(ReadOnlySpan bytecode) { - if (this.cachedShader is not null) - { - ThrowHelper.ThrowInvalidOperationException("The shader has already been initialized."); - } + default(InvalidOperationException).ThrowIf(this.cachedShader is not null); this.cachedShader = new ICachedShader.Embedded(bytecode); }