diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index 33cb7f2a..66666ac9 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -8,7 +8,6 @@ - @@ -67,7 +66,6 @@ - diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp/Graphics/Extensions/Interop/HRESULTExtensions.cs b/src/ComputeSharp.SourceGenerators/ComputeSharp/Graphics/Extensions/Interop/HRESULTExtensions.cs deleted file mode 100644 index da6a898f..00000000 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp/Graphics/Extensions/Interop/HRESULTExtensions.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.ComponentModel; -using System.Diagnostics; -using TerraFX.Interop.Windows; - -namespace ComputeSharp.Core.Extensions; - -/// -/// 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. - public static void Assert(this HRESULT result) - { - Assert((int)result); - } - - /// - /// Throws a if represents an error. - /// - /// The input to check. - /// Thrown if represents an error. - public static void Assert(this int result) - { - if (result < 0) - { - throw new Win32Exception(result); - } - } -} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs b/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs index cc8d528a..9360ef72 100644 --- a/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs +++ b/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs @@ -1,8 +1,8 @@ using System; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Text.RegularExpressions; using System.Threading; -using ComputeSharp.Core.Extensions; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -44,21 +44,21 @@ internal sealed unsafe class DxcShaderCompiler using ComPtr dxcLibrary = default; using ComPtr dxcIncludeHandler = default; - DirectX.DxcCreateInstance( + Marshal.ThrowExceptionForHR(DirectX.DxcCreateInstance( (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in CLSID.CLSID_DxcCompiler)), Windows.__uuidof(), - (void**)dxcCompiler.GetAddressOf()).Assert(); + (void**)dxcCompiler.GetAddressOf())); - DirectX.DxcCreateInstance( + Marshal.ThrowExceptionForHR(DirectX.DxcCreateInstance( (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in CLSID.CLSID_DxcLibrary)), Windows.__uuidof(), - (void**)dxcLibrary.GetAddressOf()).Assert(); + (void**)dxcLibrary.GetAddressOf())); - dxcLibrary.Get()->CreateIncludeHandler(dxcIncludeHandler.GetAddressOf()).Assert(); + Marshal.ThrowExceptionForHR(dxcLibrary.Get()->CreateIncludeHandler(dxcIncludeHandler.GetAddressOf())); - this.dxcCompiler = dxcCompiler.Move(); - this.dxcLibrary = dxcLibrary.Move(); - this.dxcIncludeHandler = dxcIncludeHandler.Move(); + this.dxcCompiler = new ComPtr(dxcCompiler.Get()); + this.dxcLibrary = new ComPtr(dxcLibrary.Get()); + this.dxcIncludeHandler = new ComPtr(dxcIncludeHandler.Get()); } /// @@ -90,11 +90,13 @@ internal sealed unsafe class DxcShaderCompiler // Get the encoded blob from the source code fixed (char* p = source) { - this.dxcLibrary.Get()->CreateBlobWithEncodingOnHeapCopy( + int hresult = this.dxcLibrary.Get()->CreateBlobWithEncodingOnHeapCopy( p, (uint)source.Length * 2, 1200, - dxcBlobEncoding.GetAddressOf()).Assert(); + dxcBlobEncoding.GetAddressOf()); + + Marshal.ThrowExceptionForHR(hresult); } token.ThrowIfCancellationRequested(); @@ -109,7 +111,7 @@ internal sealed unsafe class DxcShaderCompiler { char** arguments = stackalloc char*[3] { optimization, rowMajor, warningsAsErrors }; - this.dxcCompiler.Get()->Compile( + int hresult = this.dxcCompiler.Get()->Compile( (IDxcBlob*)dxcBlobEncoding.Get(), (ushort*)shaderName, (ushort*)entryPoint, @@ -119,21 +121,23 @@ internal sealed unsafe class DxcShaderCompiler null, 0, this.dxcIncludeHandler.Get(), - dxcOperationResult.GetAddressOf()).Assert(); + dxcOperationResult.GetAddressOf()); + + Marshal.ThrowExceptionForHR(hresult); } token.ThrowIfCancellationRequested(); HRESULT status; - dxcOperationResult.Get()->GetStatus(&status).Assert(); + Marshal.ThrowExceptionForHR(dxcOperationResult.Get()->GetStatus(&status)); // The compilation was successful, so we can extract the shader bytecode if (status == 0) { using ComPtr dxcBlobBytecode = default; - dxcOperationResult.Get()->GetResult(dxcBlobBytecode.GetAddressOf()).Assert(); + Marshal.ThrowExceptionForHR(dxcOperationResult.Get()->GetResult(dxcBlobBytecode.GetAddressOf())); byte* buffer = (byte*)dxcBlobBytecode.Get()->GetBufferPointer(); int length = checked((int)dxcBlobBytecode.Get()->GetBufferSize()); @@ -154,7 +158,7 @@ internal sealed unsafe class DxcShaderCompiler { using ComPtr dxcBlobEncodingError = default; - dxcOperationResult->GetErrorBuffer(dxcBlobEncodingError.GetAddressOf()).Assert(); + Marshal.ThrowExceptionForHR(dxcOperationResult->GetErrorBuffer(dxcBlobEncodingError.GetAddressOf())); string message = new((sbyte*)dxcBlobEncodingError.Get()->GetBufferPointer());