Add and use InvalidOperationExceptionExtensions.ThrowIf

このコミットが含まれているのは:
Sergio Pedri committed 2023-02-25 17:42:11 +01:00
1 親 e4c7b5161c
コミット e27fb13a01
6ファイル変更
+51 -55

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

+1 -1
ファイルの表示
@@ -15,7 +15,7 @@ internal static class ArgumentExceptionExtensions
/// <param name="condition">The condition to decide whether to throw the exception.</param>
/// <param name="parameterName">The name of the parameter with which <paramref name="condition"/> corresponds.</param>
[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)
{
+34
ファイルの表示
@@ -0,0 +1,34 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace System;
/// <summary>
/// Throw helper extensions for <see cref="InvalidOperationException"/>.
/// </summary>
internal static class InvalidOperationExceptionExtensions
{
/// <summary>
/// Throws an <see cref="InvalidOperationException"/> if <paramref name="condition"/> is <see langword="true"/>.
/// </summary>
/// <param name="_">Dummy value to invoke the extension upon (always pass <see langword="null"/>.</param>
/// <param name="condition">The condition to decide whether to throw the exception.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIf(this InvalidOperationException? _, [DoesNotReturnIf(true)] bool condition)
{
if (condition)
{
Throw();
}
}
/// <summary>
/// Throws an <see cref="InvalidOperationException"/> for a given parameter name.
/// </summary>
/// <exception cref="InvalidOperationException">Always thrown with no parameters.</exception>
[DoesNotReturn]
private static void Throw()
{
throw new InvalidOperationException();
}
}
+2 -9
ファイルの表示
@@ -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;
+2 -8
ファイルの表示
@@ -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<ID3D12Device>(),
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))
+10 -29
ファイルの表示
@@ -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
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> to insert the barrier for.</param>
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<TPixel>
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;
}
/// <summary>
/// Throws an <see cref="InvalidOperationException"/> if <see cref="device"/> is <see langword="null"/>.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown if <see cref="device"/> is <see langword="null"/>.</exception>
[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.");
}
}
}
+2 -8
ファイルの表示
@@ -38,10 +38,7 @@ internal struct ShaderBytecodeLoader : IBytecodeLoader
/// <inheritdoc/>
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<byte> 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);
}