diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs index 75cb5643..6883c166 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs @@ -10,8 +10,6 @@ using ComputeSharp.SourceGeneration.Models; using ComputeSharp.SourceGenerators.Dxc; using ComputeSharp.SourceGenerators.Models; using Microsoft.CodeAnalysis; -using TerraFX.Interop.DirectX; -using TerraFX.Interop.Windows; using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; namespace ComputeSharp.SourceGenerators; @@ -47,24 +45,19 @@ partial class ComputeShaderDescriptorGenerator try { + token.ThrowIfCancellationRequested(); + // Try to load dxcompiler.dll and dxil.dll DxcLibraryLoader.LoadNativeDxcLibraries(); token.ThrowIfCancellationRequested(); // Compile the shader bytecode - using ComPtr dxcBlobBytecode = DxcShaderCompiler.Instance.Compile(key.HlslSource.AsSpan()); + byte[] bytecode = DxcShaderCompiler.Instance.Compile(key.HlslSource.AsSpan(), token); token.ThrowIfCancellationRequested(); - byte* buffer = (byte*)dxcBlobBytecode.Get()->GetBufferPointer(); - int length = checked((int)dxcBlobBytecode.Get()->GetBufferSize()); - - byte[] array = new ReadOnlySpan(buffer, length).ToArray(); - - ImmutableArray bytecode = Unsafe.As>(ref array); - - return new HlslBytecodeInfo.Success(bytecode); + return new HlslBytecodeInfo.Success(Unsafe.As>(ref bytecode)); } catch (Win32Exception e) { diff --git a/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs b/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs index 1bad772f..cc8d528a 100644 --- a/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs +++ b/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.CompilerServices; using System.Text.RegularExpressions; +using System.Threading; using ComputeSharp.Core.Extensions; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -79,12 +80,12 @@ internal sealed unsafe class DxcShaderCompiler /// Compiles a new HLSL shader from the input source code. /// /// The HLSL source code to compile. + /// The used to cancel the operation, if needed. /// The bytecode for the compiled shader. - public ComPtr Compile(ReadOnlySpan source) + public byte[] Compile(ReadOnlySpan source, CancellationToken token) { using ComPtr dxcBlobEncoding = default; using ComPtr dxcOperationResult = default; - using ComPtr dxcBlobBytecode = default; // Get the encoded blob from the source code fixed (char* p = source) @@ -96,6 +97,8 @@ internal sealed unsafe class DxcShaderCompiler dxcBlobEncoding.GetAddressOf()).Assert(); } + token.ThrowIfCancellationRequested(); + // Try to compile the new compute shader fixed (char* shaderName = "") fixed (char* entryPoint = nameof(IComputeShader.Execute)) @@ -119,6 +122,8 @@ internal sealed unsafe class DxcShaderCompiler dxcOperationResult.GetAddressOf()).Assert(); } + token.ThrowIfCancellationRequested(); + HRESULT status; dxcOperationResult.Get()->GetStatus(&status).Assert(); @@ -126,9 +131,14 @@ internal sealed unsafe class DxcShaderCompiler // The compilation was successful, so we can extract the shader bytecode if (status == 0) { + using ComPtr dxcBlobBytecode = default; + dxcOperationResult.Get()->GetResult(dxcBlobBytecode.GetAddressOf()).Assert(); - return dxcBlobBytecode.Move(); + byte* buffer = (byte*)dxcBlobBytecode.Get()->GetBufferPointer(); + int length = checked((int)dxcBlobBytecode.Get()->GetBufferSize()); + + return new ReadOnlySpan(buffer, length).ToArray(); } return ThrowHslsCompilationException(dxcOperationResult.Get()); @@ -140,7 +150,7 @@ internal sealed unsafe class DxcShaderCompiler /// The input (faulting) operation. /// This method always throws and never actually returs. [MethodImpl(MethodImplOptions.NoInlining)] - private static ComPtr ThrowHslsCompilationException(IDxcOperationResult* dxcOperationResult) + private static byte[] ThrowHslsCompilationException(IDxcOperationResult* dxcOperationResult) { using ComPtr dxcBlobEncodingError = default;