Added internal ThrowHelper, code refactoring

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-15 18:26:58 +01:00
1 親 c8138c465c
コミット 4c36209d77
5ファイル変更
+92 -27

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

+9 -9
ファイルの表示
@@ -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;
}
+3 -2
ファイルの表示
@@ -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;
}
+74
ファイルの表示
@@ -0,0 +1,74 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using TerraFX.Interop;
namespace ComputeSharp.Graphics.Helpers
{
/// <summary>
/// Helper methods to efficiently throw exceptions.
/// </summary>
[DebuggerStepThrough]
internal static class ThrowHelper
{
/// <summary>
/// Throws a <see cref="Win32Exception"/> if <paramref name="result"/> represents an error.
/// </summary>
/// <param name="result">The input <see cref="HRESULT"/> to check.</param>
/// <exception cref="Win32Exception">Thrown if <paramref name="result"/> represents an error.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfFailed(int result)
{
if (result < 0)
{
static void Throw(int result) => throw new Win32Exception(result);
Throw(result);
}
}
/// <summary>
/// Throws a <see cref="Win32Exception"/> if <paramref name="result"/> represents an error.
/// </summary>
/// <param name="result">The input <see cref="HRESULT"/> to check.</param>
/// <param name="d3d3blobError">A <see cref="ID3DBlob"/> pointer representing an error message.</param>
/// <exception cref="Win32Exception">Thrown if <paramref name="result"/> represents an error.</exception>
[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);
}
}
/// <summary>
/// Throws a <see cref="Win32Exception"/> if <paramref name="result"/> represents an error.
/// </summary>
/// <param name="result">The input <see cref="HRESULT"/> to check.</param>
/// <param name="d3d3blobError">A <see cref="IDxcBlob"/> pointer representing an error message.</param>
/// <exception cref="Win32Exception">Thrown if <paramref name="result"/> represents an error.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void ThrowIfFailed(int result, IDxcBlob* d3d3blobError)
{
ThrowIfFailed(result, (ID3DBlob*)d3d3blobError);
}
}
}
+1 -1
ファイルの表示
@@ -2,8 +2,8 @@
<PropertyGroup>
<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
+5 -15
ファイルの表示
@@ -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;
}