diff --git a/src/ComputeSharp.Core/Polyfills/ArgumentNullExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/ArgumentNullExceptionExtensions.cs index ab256f00..9255dda0 100644 --- a/src/ComputeSharp.Core/Polyfills/ArgumentNullExceptionExtensions.cs +++ b/src/ComputeSharp.Core/Polyfills/ArgumentNullExceptionExtensions.cs @@ -18,7 +18,7 @@ internal static class ArgumentNullExceptionExtensions [DoesNotReturn] public static void Throw(this ArgumentNullException? _, string? parameterName) { - Throw(parameterName); + throw new ArgumentNullException(parameterName); } /// diff --git a/src/ComputeSharp.Core/Polyfills/Win32ExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/Win32ExceptionExtensions.cs new file mode 100644 index 00000000..f24bc389 --- /dev/null +++ b/src/ComputeSharp.Core/Polyfills/Win32ExceptionExtensions.cs @@ -0,0 +1,61 @@ +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +namespace System.ComponentModel; + +/// +/// Throw helper extensions for . +/// +internal static class Win32ExceptionExtensions +{ + /// + /// Throws an for a given error code. + /// + /// Dummy value to invoke the extension upon (always pass . + /// The error code to report. + [DoesNotReturn] + public static void Throw(this Win32Exception? _, int hresult) + { + throw new Win32Exception(hresult); + } + + /// + /// Throws an if is . + /// + /// Dummy value to invoke the extension upon (always pass . + /// The condition to decide whether to throw the exception. + /// The error code to report. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIf(this Win32Exception? _, [DoesNotReturnIf(true)] bool condition, int hresult) + { + if (condition) + { + Throw(hresult); + } + } + + /// + /// Throws an if a given error code represents an error. + /// + /// Dummy value to invoke the extension upon (always pass . + /// The error code to check. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfFailed(this Win32Exception? _, int hresult) + { + if (hresult < 0) + { + Throw(hresult); + } + } + + /// + /// Throws an for a given error code. + /// + /// The error code to report. + /// Thrown with the given error code. + [DoesNotReturn] + private static void Throw(int hresult) + { + throw new Win32Exception(hresult); + } +} diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/HRESULTExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/HRESULTExtensions.cs index 77710d00..9dce4803 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/HRESULTExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/HRESULTExtensions.cs @@ -1,7 +1,6 @@ using System.ComponentModel; using System.Diagnostics; using System.Runtime.CompilerServices; -using CommunityToolkit.Diagnostics; using ComputeSharp.Graphics.Helpers; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -38,10 +37,7 @@ internal static class HRESULTExtensions // single branch can then be inlined without bloating the code size in the caller. if (!Configuration.IsDebugOutputEnabled) { - if (result < 0) - { - ThrowHelper.ThrowWin32Exception(result); - } + default(Win32Exception).ThrowIfFailed(result); } else { @@ -51,15 +47,11 @@ internal static class HRESULTExtensions { bool hasErrorsOrWarnings = DeviceHelper.FlushAllID3D12InfoQueueMessagesAndCheckForErrorsOrWarnings(); - if (result < 0) - { - ThrowHelper.ThrowWin32Exception(result); - } + // First, check whether the current call has failed, and report that + default(Win32Exception).ThrowIfFailed(result); - if (hasErrorsOrWarnings) - { - ThrowHelper.ThrowWin32Exception("Warning or error detected by ID3D12InfoQueue."); - } + // If that's not the case, then throw if any errors are present in the info queue + default(Win32Exception).ThrowIf(hasErrorsOrWarnings, E.E_FAIL); } AssertWithDebugInfo(result); diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.Execute.cs b/src/ComputeSharp/Graphics/GraphicsDevice.Execute.cs index 957ff01a..897a7ce3 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.Execute.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.Execute.cs @@ -1,11 +1,11 @@ using System; using System.Collections.Concurrent; +using System.ComponentModel; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using System.Threading.Tasks.Sources; -using CommunityToolkit.Diagnostics; using ComputeSharp.Core.Extensions; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Commands.Interop; @@ -215,7 +215,7 @@ unsafe partial class GraphicsDevice NativeMemory.Free(callbackContext); - ThrowHelper.ThrowWin32Exception("Failed to register the compute context completion callback."); + default(Win32Exception).Throw(E.E_FAIL); } return waitForFenceValueTaskSource; diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs index a51063ae..6d51f371 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs @@ -1,8 +1,8 @@ using System.Collections.Generic; +using System.ComponentModel; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; -using CommunityToolkit.Diagnostics; using ComputeSharp.Core.Extensions; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -107,20 +107,14 @@ partial class DeviceHelper riid: Windows.__uuidof(), ppvObject: d3D12DeviceRemovedExtendedData.GetVoidAddressOf()); - if (hresult != S.S_OK) - { - ThrowHelper.ThrowWin32Exception(hresult); - } + default(Win32Exception).ThrowIfFailed(hresult); D3D12_DRED_AUTO_BREADCRUMBS_OUTPUT1 d3D12DredAutoBreadcrumbsOutput; // Get the auto breadcrumbs for the current event hresult = d3D12DeviceRemovedExtendedData.Get()->GetAutoBreadcrumbsOutput1(&d3D12DredAutoBreadcrumbsOutput); - if (hresult != S.S_OK) - { - ThrowHelper.ThrowWin32Exception(hresult); - } + default(Win32Exception).ThrowIfFailed(hresult); _ = builder.AppendLine("[DRED breadcrumbs START] ===============================");