From 54d11f7c8f6e23fef39a9583c99c7a2231e3f92a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 17 Oct 2023 12:50:27 +0200 Subject: [PATCH] Add HRESULT.Assert() to D3D12 generator --- .../Dxc/DxcShaderCompiler.cs | 24 +++++++++---------- .../Dxc/Windows.Win32/HRESULT.cs | 21 ++++++++++++++++ 2 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 src/ComputeSharp.SourceGenerators/Dxc/Windows.Win32/HRESULT.cs diff --git a/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs b/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs index 5751ffe0..281a8794 100644 --- a/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs +++ b/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs @@ -43,19 +43,19 @@ internal sealed unsafe class DxcShaderCompiler using ComPtr dxcCompiler = default; using ComPtr dxcLibrary = default; - _ = PInvoke.DxcCreateInstance( + PInvoke.DxcCreateInstance( PInvoke.CLSID_DxcCompiler, IDxcCompiler.IID_Guid, - out *(void**)dxcCompiler.GetAddressOf()).ThrowOnFailure(); + out *(void**)dxcCompiler.GetAddressOf()).Assert(); - _ = PInvoke.DxcCreateInstance( + PInvoke.DxcCreateInstance( PInvoke.CLSID_DxcLibrary, IDxcLibrary.IID_Guid, - out *(void**)dxcLibrary.GetAddressOf()).ThrowOnFailure(); + out *(void**)dxcLibrary.GetAddressOf()).Assert(); using ComPtr dxcIncludeHandler = default; - _ = dxcLibrary.Get()->CreateIncludeHandler(dxcIncludeHandler.GetAddressOf()).ThrowOnFailure(); + dxcLibrary.Get()->CreateIncludeHandler(dxcIncludeHandler.GetAddressOf()).Assert(); this.dxcCompiler = new ComPtr(dxcCompiler.Get()); this.dxcLibrary = new ComPtr(dxcLibrary.Get()); @@ -91,11 +91,11 @@ internal sealed unsafe class DxcShaderCompiler // Get the encoded blob from the source code fixed (char* p = source) { - _ = this.dxcLibrary.Get()->CreateBlobWithEncodingOnHeapCopy( + this.dxcLibrary.Get()->CreateBlobWithEncodingOnHeapCopy( pText: p, size: (uint)source.Length * 2, codePage: DXC_CP.DXC_CP_UTF16, - pBlobEncoding: dxcBlobEncoding.GetAddressOf()).ThrowOnFailure(); + pBlobEncoding: dxcBlobEncoding.GetAddressOf()).Assert(); } token.ThrowIfCancellationRequested(); @@ -107,7 +107,7 @@ internal sealed unsafe class DxcShaderCompiler { Span arguments = stackalloc PCWSTR[] { optimization, rowMajor, warningsAsErrors }; - _ = this.dxcCompiler.Get()->Compile( + this.dxcCompiler.Get()->Compile( pSource: (IDxcBlob*)dxcBlobEncoding.Get(), pSourceName: "", pEntryPoint: "Execute", @@ -115,21 +115,21 @@ internal sealed unsafe class DxcShaderCompiler pArguments: arguments, pDefines: ReadOnlySpan.Empty, pIncludeHandler: this.dxcIncludeHandler.Get(), - ppResult: dxcOperationResult.GetAddressOf()).ThrowOnFailure(); + ppResult: dxcOperationResult.GetAddressOf()).Assert(); } token.ThrowIfCancellationRequested(); HRESULT status; - _ = dxcOperationResult.Get()->GetStatus(&status).ThrowOnFailure(); + dxcOperationResult.Get()->GetStatus(&status).Assert(); // The compilation was successful, so we can extract the shader bytecode if (status == 0) { using ComPtr dxcBlobBytecode = default; - _ = dxcOperationResult.Get()->GetResult(dxcBlobBytecode.GetAddressOf()).ThrowOnFailure(); + dxcOperationResult.Get()->GetResult(dxcBlobBytecode.GetAddressOf()).Assert(); byte* buffer = (byte*)dxcBlobBytecode.Get()->GetBufferPointer(); int length = checked((int)dxcBlobBytecode.Get()->GetBufferSize()); @@ -150,7 +150,7 @@ internal sealed unsafe class DxcShaderCompiler { using ComPtr dxcBlobEncodingError = default; - _ = dxcOperationResult->GetErrorBuffer(dxcBlobEncodingError.GetAddressOf()).ThrowOnFailure(); + dxcOperationResult->GetErrorBuffer(dxcBlobEncodingError.GetAddressOf()).Assert(); string message = new((sbyte*)dxcBlobEncodingError.Get()->GetBufferPointer()); diff --git a/src/ComputeSharp.SourceGenerators/Dxc/Windows.Win32/HRESULT.cs b/src/ComputeSharp.SourceGenerators/Dxc/Windows.Win32/HRESULT.cs new file mode 100644 index 00000000..357f1cf0 --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/Dxc/Windows.Win32/HRESULT.cs @@ -0,0 +1,21 @@ +using System.ComponentModel; +using System.Runtime.CompilerServices; + +namespace Windows.Win32.Foundation; + +/// +partial struct HRESULT +{ + /// + /// Throws a if the current value represents an error. + /// + /// Thrown if the current value represents an error. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly void Assert() + { + if (Failed) + { + throw new Win32Exception(this.Value); + } + } +} \ No newline at end of file