diff --git a/src/ComputeSharp.Core/Extensions/HResultExtensions.cs b/src/ComputeSharp.Core/Extensions/HResultExtensions.cs
new file mode 100644
index 00000000..2a816833
--- /dev/null
+++ b/src/ComputeSharp.Core/Extensions/HResultExtensions.cs
@@ -0,0 +1,30 @@
+using System.ComponentModel;
+using System.Diagnostics;
+using System.Runtime.CompilerServices;
+using HRESULT = System.Int32;
+
+namespace ComputeSharp.Graphics.Helpers
+{
+ ///
+ /// Helper methods to efficiently throw exceptions.
+ ///
+ [DebuggerStepThrough]
+ internal static class HResultExtensions
+ {
+ ///
+ /// Throws a if represents an error.
+ ///
+ /// The input to check.
+ /// Thrown if represents an error.
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static void Assert(this HRESULT result)
+ {
+ if (result < 0)
+ {
+ static void Throw(HRESULT result) => throw new Win32Exception(result);
+
+ Throw(result);
+ }
+ }
+ }
+}
diff --git a/src/ComputeSharp.Graphics/Commands/Interop/ID3D12CommandAllocatorPool.cs b/src/ComputeSharp.Graphics/Commands/Interop/ID3D12CommandAllocatorPool.cs
index 18281468..6491839a 100644
--- a/src/ComputeSharp.Graphics/Commands/Interop/ID3D12CommandAllocatorPool.cs
+++ b/src/ComputeSharp.Graphics/Commands/Interop/ID3D12CommandAllocatorPool.cs
@@ -55,9 +55,7 @@ namespace ComputeSharp.Graphics.Commands
{
if (this.d3d12CommandAllocatorQueue.TryDequeue(out *&d3D12CommandAllocator))
{
- int result = d3D12CommandAllocator.Get()->Reset();
-
- ThrowHelper.ThrowIfFailed(result);
+ d3D12CommandAllocator.Get()->Reset().Assert();
return d3D12CommandAllocator.Move();
}
diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs
index b126b97f..49e1af71 100644
--- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs
+++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs
@@ -35,12 +35,10 @@ namespace ComputeSharp.Graphics.Extensions
d3d12CommandQueueDesc.NodeMask = 0;
Guid d3d12CommandQueueDescGuid = FX.IID_ID3D12CommandQueue;
- int result = d3d12device.CreateCommandQueue(
+ d3d12device.CreateCommandQueue(
&d3d12CommandQueueDesc,
&d3d12CommandQueueDescGuid,
- d3D12CommandQueue.GetVoidAddressOf());
-
- ThrowHelper.ThrowIfFailed(result);
+ d3D12CommandQueue.GetVoidAddressOf()).Assert();
return d3D12CommandQueue.Move();
}
@@ -57,13 +55,11 @@ namespace ComputeSharp.Graphics.Extensions
Guid d3D12FenceGuid = FX.IID_ID3D12Fence;
- int result = d3d12device.CreateFence(
+ d3d12device.CreateFence(
0,
D3D12_FENCE_FLAG_NONE,
&d3D12FenceGuid,
- d3d12Fence.GetVoidAddressOf());
-
- ThrowHelper.ThrowIfFailed(result);
+ d3d12Fence.GetVoidAddressOf()).Assert();
return d3d12Fence.Move();
}
@@ -86,12 +82,10 @@ namespace ComputeSharp.Graphics.Extensions
d3d12DescriptorHeapDesc.NodeMask = 0;
Guid d3d12DescriptorHeapDescGuid = FX.IID_ID3D12DescriptorHeap;
- int result = d3d12device.CreateDescriptorHeap(
+ d3d12device.CreateDescriptorHeap(
&d3d12DescriptorHeapDesc,
&d3d12DescriptorHeapDescGuid,
- d3d12DescriptorHeap.GetVoidAddressOf());
-
- ThrowHelper.ThrowIfFailed(result);
+ d3d12DescriptorHeap.GetVoidAddressOf()).Assert();
return d3d12DescriptorHeap.Move();
}
@@ -121,16 +115,14 @@ namespace ComputeSharp.Graphics.Extensions
D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Buffer(width, d3D12ResourceFlags);
Guid d3D12ResourceGuid = FX.IID_ID3D12Resource;
- int result = d3d12device.CreateCommittedResource(
+ d3d12device.CreateCommittedResource(
&d3D12HeapProperties,
D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_NONE,
&d3D12ResourceDescription,
d3D12ResourceStates,
null,
&d3D12ResourceGuid,
- d3D12Resource.GetVoidAddressOf());
-
- ThrowHelper.ThrowIfFailed(result);
+ d3D12Resource.GetVoidAddressOf()).Assert();
return d3D12Resource.Move();
}
@@ -150,12 +142,10 @@ namespace ComputeSharp.Graphics.Extensions
Guid d3D12CommandAllocatorGuid = FX.IID_ID3D12CommandAllocator;
- int result = d3d12device.CreateCommandAllocator(
+ d3d12device.CreateCommandAllocator(
d3d12CommandListType,
&d3D12CommandAllocatorGuid,
- d3D12CommandAllocator.GetVoidAddressOf());
-
- ThrowHelper.ThrowIfFailed(result);
+ d3D12CommandAllocator.GetVoidAddressOf()).Assert();
return d3D12CommandAllocator.Move();
}
@@ -176,15 +166,13 @@ namespace ComputeSharp.Graphics.Extensions
Guid d3D12GraphicsCommandListGuid = FX.IID_ID3D12GraphicsCommandList;
- int result = d3d12device.CreateCommandList(
+ d3d12device.CreateCommandList(
0,
d3d12CommandListType,
d3D12CommandAllocator,
null,
&d3D12GraphicsCommandListGuid,
- d3d12GraphicsCommandList.GetVoidAddressOf());
-
- ThrowHelper.ThrowIfFailed(result);
+ d3d12GraphicsCommandList.GetVoidAddressOf()).Assert();
return d3d12GraphicsCommandList.Move();
}
@@ -202,9 +190,7 @@ namespace ComputeSharp.Graphics.Extensions
{
TFeature feature;
- int result = d3d12device.CheckFeatureSupport(d3d12feature, &feature, (uint)sizeof(TFeature));
-
- ThrowHelper.ThrowIfFailed(result);
+ d3d12device.CheckFeatureSupport(d3d12feature, &feature, (uint)sizeof(TFeature)).Assert();
return feature;
}
diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12ResourceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12ResourceExtensions.cs
index a1887c28..a2011a6a 100644
--- a/src/ComputeSharp.Graphics/Extensions/ID3D12ResourceExtensions.cs
+++ b/src/ComputeSharp.Graphics/Extensions/ID3D12ResourceExtensions.cs
@@ -19,9 +19,7 @@ namespace ComputeSharp.Graphics.Extensions
{
IntPtr ptr;
- int result = d3d12resource.Map(0, null, (void**)&ptr);
-
- ThrowHelper.ThrowIfFailed(result);
+ d3d12resource.Map(0, null, (void**)&ptr).Assert();
return ptr;
}
diff --git a/src/ComputeSharp.Graphics/GraphicsDevice2.cs b/src/ComputeSharp.Graphics/GraphicsDevice2.cs
index 6819a40f..989a8a16 100644
--- a/src/ComputeSharp.Graphics/GraphicsDevice2.cs
+++ b/src/ComputeSharp.Graphics/GraphicsDevice2.cs
@@ -178,16 +178,12 @@ namespace ComputeSharp.Graphics
// Execute the command list and signal to the target fence
d3D12CommandQueue->ExecuteCommandLists(1, commandList.GetD3D12CommandListAddressOf());
- int result = d3D12CommandQueue->Signal(d3D12Fence, d3D12FenceValue);
-
- ThrowHelper.ThrowIfFailed(result);
+ d3D12CommandQueue->Signal(d3D12Fence, d3D12FenceValue).Assert();
// If the fence value hasn't been reached, wait until the operation completes
if (d3D12FenceValue > d3D12Fence->GetCompletedValue())
{
- result = d3D12Fence->SetEventOnCompletion(d3D12FenceValue, default);
-
- ThrowHelper.ThrowIfFailed(result);
+ d3D12Fence->SetEventOnCompletion(d3D12FenceValue, default).Assert();
}
// Enqueue the command allocator pool so that it can be reused later
diff --git a/src/ComputeSharp.Graphics/Helpers/ThrowHelper.cs b/src/ComputeSharp.Graphics/Helpers/ThrowHelper.cs
deleted file mode 100644
index 43444b20..00000000
--- a/src/ComputeSharp.Graphics/Helpers/ThrowHelper.cs
+++ /dev/null
@@ -1,74 +0,0 @@
-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/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Shaders/Extensions/ID3D12DeviceExtensions.cs
index 6bb97ae7..929980a0 100644
--- a/src/ComputeSharp.Shaders/Extensions/ID3D12DeviceExtensions.cs
+++ b/src/ComputeSharp.Shaders/Extensions/ID3D12DeviceExtensions.cs
@@ -28,8 +28,6 @@ namespace ComputeSharp.Shaders.Extensions
using ComPtr d3D3BlobError = default;
using ComPtr d3D12RootSignature = default;
- int result;
-
fixed (D3D12_DESCRIPTOR_RANGE1* d3D12DescriptorRange1 = d3D12DescriptorRanges1)
{
D3D12_ROOT_PARAMETER1* d3D12RootParameters1 = stackalloc D3D12_ROOT_PARAMETER1[d3D12DescriptorRanges1.Length];
@@ -52,24 +50,20 @@ namespace ComputeSharp.Shaders.Extensions
// Serialize the root signature from the data just computed. When this is done
// we just work with the resulting blobs, so the input data can be unpinned.
- result = FX.D3D12SerializeVersionedRootSignature(
+ FX.D3D12SerializeVersionedRootSignature(
&d3d12VersionedRootSignatureDescription,
d3D3Blob.GetAddressOf(),
- d3D3BlobError.GetAddressOf());
-
- ThrowHelper.ThrowIfFailed(result, d3D3BlobError);
+ d3D3BlobError.GetAddressOf()).Assert();
}
Guid d3D12RootSignatureGuid = FX.IID_ID3D12RootSignature;
- result = d3d12device.CreateRootSignature(
+ d3d12device.CreateRootSignature(
0,
d3D3Blob.Get()->GetBufferPointer(),
d3D3Blob.Get()->GetBufferSize(),
&d3D12RootSignatureGuid,
- d3D12RootSignature.GetVoidAddressOf());
-
- ThrowHelper.ThrowIfFailed(result);
+ d3D12RootSignature.GetVoidAddressOf()).Assert();
return d3D12RootSignature.Move();
}
@@ -97,12 +91,10 @@ namespace ComputeSharp.Shaders.Extensions
d3d12ComputePipelineStateDescription.Flags = D3D12_PIPELINE_STATE_FLAG_NONE;
Guid d3d12ComputePipelineStateGuid = FX.IID_ID3D12PipelineState;
- int result = d3d12device.CreateComputePipelineState(
+ d3d12device.CreateComputePipelineState(
&d3d12ComputePipelineStateDescription,
&d3d12ComputePipelineStateGuid,
- d3D12PipelineState.GetVoidAddressOf());
-
- ThrowHelper.ThrowIfFailed(result);
+ d3D12PipelineState.GetVoidAddressOf()).Assert();
return d3D12PipelineState.Move();
}
diff --git a/src/ComputeSharp.Shaders/Translation/ShaderCompiler2.cs b/src/ComputeSharp.Shaders/Translation/ShaderCompiler2.cs
index 5dab1701..7e741b92 100644
--- a/src/ComputeSharp.Shaders/Translation/ShaderCompiler2.cs
+++ b/src/ComputeSharp.Shaders/Translation/ShaderCompiler2.cs
@@ -43,17 +43,10 @@ namespace ComputeSharp.Shaders.Translation
Guid dxcLibraryCLGuid = FX.CLSID_DxcLibrary;
Guid dxcLibraryGuid = FX.IID_IDxcLibrary;
- int result = FX.DxcCreateInstance(&dxcCompilerCLGuid, &dxcCompilerGuid, dxcCompiler.GetVoidAddressOf());
+ FX.DxcCreateInstance(&dxcCompilerCLGuid, &dxcCompilerGuid, dxcCompiler.GetVoidAddressOf()).Assert();
+ FX.DxcCreateInstance(&dxcLibraryCLGuid, &dxcLibraryGuid, dxcLibrary.GetVoidAddressOf()).Assert();
- ThrowHelper.ThrowIfFailed(result);
-
- result = FX.DxcCreateInstance(&dxcLibraryCLGuid, &dxcLibraryGuid, dxcLibrary.GetVoidAddressOf());
-
- ThrowHelper.ThrowIfFailed(result);
-
- result = dxcLibrary.Get()->CreateIncludeHandler(dxcIncludeHandler.GetAddressOf());
-
- ThrowHelper.ThrowIfFailed(result);
+ dxcLibrary.Get()->CreateIncludeHandler(dxcIncludeHandler.GetAddressOf()).Assert();
DxcCompiler = dxcCompiler;
DxcLibrary = dxcLibrary;
@@ -72,14 +65,14 @@ namespace ComputeSharp.Shaders.Translation
using ComPtr dxcOperationResult = default;
using ComPtr dxcBlobBytecode = default;
- int result;
-
// Get the encoded blob from the source code
fixed (char* p = source)
{
- result = DxcLibrary.Get()->CreateBlobWithEncodingOnHeapCopy(p, (uint)source.Length * 2, 1200, dxcBlobEncoding.GetAddressOf());
-
- ThrowHelper.ThrowIfFailed(result);
+ DxcLibrary.Get()->CreateBlobWithEncodingOnHeapCopy(
+ p,
+ (uint)source.Length * 2,
+ 1200,
+ dxcBlobEncoding.GetAddressOf()).Assert();
}
// Try to compile the new compute shader
@@ -89,7 +82,7 @@ namespace ComputeSharp.Shaders.Translation
{
char nullTerminator = '\0';
- result = DxcCompiler.Get()->Compile(
+ DxcCompiler.Get()->Compile(
dxcBlobEncoding.Upcast().Get(),
(ushort*)&nullTerminator,
(ushort*)entryPoint,
@@ -99,23 +92,17 @@ namespace ComputeSharp.Shaders.Translation
null,
0,
DxcIncludeHandler.Get(),
- dxcOperationResult.GetAddressOf());
-
- ThrowHelper.ThrowIfFailed(result);
+ dxcOperationResult.GetAddressOf()).Assert();
}
int status;
- result = dxcOperationResult.Get()->GetStatus(&status);
-
- ThrowHelper.ThrowIfFailed(result);
+ dxcOperationResult.Get()->GetStatus(&status).Assert();
// The compilation was successful, so we can extract the shader bytecode
if (status == 0)
{
- result = dxcOperationResult.Get()->GetResult(dxcBlobBytecode.GetAddressOf());
-
- ThrowHelper.ThrowIfFailed(result);
+ dxcOperationResult.Get()->GetResult(dxcBlobBytecode.GetAddressOf()).Assert();
return dxcBlobBytecode.Move();
}
@@ -134,17 +121,13 @@ namespace ComputeSharp.Shaders.Translation
{
using ComPtr dxcBlobEncodingError = default;
- int result = dxcOperationResult.Get()->GetErrorBuffer(dxcBlobEncodingError.GetAddressOf());
-
- ThrowHelper.ThrowIfFailed(result);
+ dxcOperationResult.Get()->GetErrorBuffer(dxcBlobEncodingError.GetAddressOf()).Assert();
using ComPtr dxcBlobUtf16Error = default;
- result = DxcLibrary.Get()->GetBlobAsUtf16(
+ DxcLibrary.Get()->GetBlobAsUtf16(
dxcBlobEncodingError.Upcast().Get(),
- dxcBlobUtf16Error.Upcast().GetAddressOf());
-
- ThrowHelper.ThrowIfFailed(result);
+ dxcBlobUtf16Error.Upcast().GetAddressOf()).Assert();
string message = new string(
(char*)dxcBlobUtf16Error.Get()->GetStringPointer(),