diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/HResultExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/HResultExtensions.cs index 403dc0ba..85b7f135 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/HResultExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/HResultExtensions.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Runtime.CompilerServices; using CommunityToolkit.Diagnostics; using ComputeSharp.Graphics.Helpers; +using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; namespace ComputeSharp.Core.Extensions; @@ -64,4 +65,31 @@ internal static class HResultExtensions AssertWithDebugInfo(result); } } + + /// + /// Checks whether or not a given represents a device lost reason. + /// + /// The input to check. + /// Whether or not a given represents a device lost reason. + /// + /// An is a device lost reason when it has one of the following values: + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static bool IsDeviceLostReason(this HRESULT result) + { + return (int)result is + DXGI.DXGI_ERROR_DEVICE_HUNG or + DXGI.DXGI_ERROR_DEVICE_REMOVED or + DXGI.DXGI_ERROR_DEVICE_RESET or + DXGI.DXGI_ERROR_DRIVER_INTERNAL_ERROR or + DXGI.DXGI_ERROR_INVALID_CALL or + DXGI.DXGI_ERROR_ACCESS_DENIED; + } } diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs index 256e4eab..54ae9364 100644 --- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.cs @@ -105,13 +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 ((int)createDeviceResult is - DXGI.DXGI_ERROR_DEVICE_HUNG or - DXGI.DXGI_ERROR_DEVICE_REMOVED or - DXGI.DXGI_ERROR_DEVICE_RESET or - DXGI.DXGI_ERROR_DRIVER_INTERNAL_ERROR or - DXGI.DXGI_ERROR_INVALID_CALL or - DXGI.DXGI_ERROR_ACCESS_DENIED) + if (createDeviceResult.IsDeviceLostReason()) { ThrowHelper.ThrowInvalidOperationException("The default device is in device lost state and has not been disposed properly."); } @@ -157,13 +151,7 @@ internal static partial class DeviceHelper Windows.__uuidof(), d3D12DeviceCandidate.GetVoidAddressOf()); - if ((int)createDeviceResult is - DXGI.DXGI_ERROR_DEVICE_HUNG or - DXGI.DXGI_ERROR_DEVICE_REMOVED or - DXGI.DXGI_ERROR_DEVICE_RESET or - DXGI.DXGI_ERROR_DRIVER_INTERNAL_ERROR or - DXGI.DXGI_ERROR_INVALID_CALL or - DXGI.DXGI_ERROR_ACCESS_DENIED) + if (createDeviceResult.IsDeviceLostReason()) { ThrowHelper.ThrowInvalidOperationException("The default device is in device lost state and has not been disposed properly."); }