diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index 7a7549b4..3419d429 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -1,6 +1,6 @@ -using System; +using ComputeSharp.Graphics.Helpers; +using System; using System.Diagnostics.Contracts; -using System.Runtime.InteropServices; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; using static TerraFX.Interop.D3D12_COMMAND_QUEUE_FLAGS; @@ -40,7 +40,7 @@ namespace ComputeSharp.Graphics.Extensions &d3d12CommandQueueDescGuid, (void**)&commandQueue); - if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); + ThrowHelper.ThrowIfFailed(result); return commandQueue; } @@ -62,7 +62,7 @@ namespace ComputeSharp.Graphics.Extensions &d3D12FenceGuid, (void**)&d3d12Fence); - if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); + ThrowHelper.ThrowIfFailed(result); return d3d12Fence; } @@ -89,7 +89,7 @@ namespace ComputeSharp.Graphics.Extensions &d3d12DescriptorHeapDescGuid, (void**)&d3d12DescriptorHeap); - if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); + ThrowHelper.ThrowIfFailed(result); return d3d12DescriptorHeap; } @@ -127,7 +127,7 @@ namespace ComputeSharp.Graphics.Extensions &d3D12ResourceGuid, (void**)&d3D12Resource); - if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); + ThrowHelper.ThrowIfFailed(result); return d3D12Resource; } @@ -151,7 +151,7 @@ namespace ComputeSharp.Graphics.Extensions &d3D12CommandAllocatorGuid, (void**)&d3D12CommandAllocator); - if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); + ThrowHelper.ThrowIfFailed(result); return d3D12CommandAllocator; } @@ -179,7 +179,7 @@ namespace ComputeSharp.Graphics.Extensions &d3D12GraphicsCommandListGuid, (void**)&d3d12GraphicsCommandList); - if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); + ThrowHelper.ThrowIfFailed(result); return d3d12GraphicsCommandList; } @@ -199,7 +199,7 @@ namespace ComputeSharp.Graphics.Extensions int result = d3d12device.CheckFeatureSupport(d3d12feature, &feature, (uint)sizeof(TFeature)); - if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); + ThrowHelper.ThrowIfFailed(result); return feature; } diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12ResourceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12ResourceExtensions.cs index 6b32626d..c104c66c 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12ResourceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12ResourceExtensions.cs @@ -1,4 +1,5 @@ -using System; +using ComputeSharp.Graphics.Helpers; +using System; using System.Runtime.InteropServices; using TerraFX.Interop; using FX = TerraFX.Interop.Windows; @@ -22,7 +23,7 @@ namespace ComputeSharp.Graphics.Extensions int result = d3d12resource.Map(0, null, (void**)&ptr); - if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); + ThrowHelper.ThrowIfFailed(result); return ptr; } diff --git a/src/ComputeSharp.Graphics/Helpers/ThrowHelper.cs b/src/ComputeSharp.Graphics/Helpers/ThrowHelper.cs new file mode 100644 index 00000000..43444b20 --- /dev/null +++ b/src/ComputeSharp.Graphics/Helpers/ThrowHelper.cs @@ -0,0 +1,74 @@ +using System.ComponentModel; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using TerraFX.Interop; + +namespace ComputeSharp.Graphics.Helpers +{ + /// + /// Helper methods to efficiently throw exceptions. + /// + [DebuggerStepThrough] + internal static class ThrowHelper + { + /// + /// Throws a if represents an error. + /// + /// The input to check. + /// Thrown if represents an error. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfFailed(int result) + { + if (result < 0) + { + static void Throw(int result) => throw new Win32Exception(result); + + Throw(result); + } + } + + /// + /// Throws a if represents an error. + /// + /// The input to check. + /// A pointer representing an error message. + /// Thrown if represents an error. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe void ThrowIfFailed(int result, ID3DBlob* d3d3blobError) + { + if (result < 0) + { + // Gets an exception with an optional message with more info. + // The input buffer transfers ownership and is disposed on return. + static Win32Exception GetException(int result, ID3DBlob* d3d3blobError) + { + if (d3d3blobError is null) return new Win32Exception(result); + + char* p = (char*)d3d3blobError->GetBufferPointer(); + int length = checked((int)unchecked(d3d3blobError->GetBufferSize() / sizeof(char))); + string message = new(p, 0, length); + + d3d3blobError->Release(); + + return new Win32Exception(result, message); + } + + static void Throw(int result, ID3DBlob* d3d3blobError) => throw GetException(result, d3d3blobError); + + Throw(result, d3d3blobError); + } + } + + /// + /// Throws a if represents an error. + /// + /// The input to check. + /// A pointer representing an error message. + /// Thrown if represents an error. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe void ThrowIfFailed(int result, IDxcBlob* d3d3blobError) + { + ThrowIfFailed(result, (ID3DBlob*)d3d3blobError); + } + } +} diff --git a/src/ComputeSharp.Shaders/ComputeSharp.Shaders.csproj b/src/ComputeSharp.Shaders/ComputeSharp.Shaders.csproj index ea6bff87..dd53abda 100644 --- a/src/ComputeSharp.Shaders/ComputeSharp.Shaders.csproj +++ b/src/ComputeSharp.Shaders/ComputeSharp.Shaders.csproj @@ -2,8 +2,8 @@ net5.0-windows10.0.19041.0 - 8.0 enable + true diff --git a/src/ComputeSharp.Shaders/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Shaders/Extensions/ID3D12DeviceExtensions.cs index 720c82e9..3d930257 100644 --- a/src/ComputeSharp.Shaders/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Shaders/Extensions/ID3D12DeviceExtensions.cs @@ -1,5 +1,5 @@ -using System; -using System.Runtime.InteropServices; +using ComputeSharp.Graphics.Helpers; +using System; using TerraFX.Interop; using static TerraFX.Interop.D3D12_PIPELINE_STATE_FLAGS; using static TerraFX.Interop.D3D12_ROOT_PARAMETER_TYPE; @@ -54,12 +54,7 @@ namespace ComputeSharp.Shaders.Extensions &d3D3Blob, &d3D3BlobError); - if (FX.FAILED(result)) - { - if (d3D3BlobError != null) d3D3BlobError->Release(); - - Marshal.ThrowExceptionForHR(result); - } + ThrowHelper.ThrowIfFailed(result, d3D3BlobError); } Guid d3D12RootSignatureGuid = FX.IID_ID3D12RootSignature; @@ -72,12 +67,7 @@ namespace ComputeSharp.Shaders.Extensions &d3D12RootSignatureGuid, (void**)&d3D12RootSignature); - if (FX.FAILED(result)) - { - if (d3D3Blob != null) d3D3Blob->Release(); - - Marshal.ThrowExceptionForHR(result); - } + ThrowHelper.ThrowIfFailed(result); return d3D12RootSignature; } @@ -109,7 +99,7 @@ namespace ComputeSharp.Shaders.Extensions &d3d12ComputePipelineStateGuid, (void**)&d3D12PipelineState); - if (FX.FAILED(result)) Marshal.ThrowExceptionForHR(result); + ThrowHelper.ThrowIfFailed(result); return d3D12PipelineState; }