From 35bc8ce6494dc6d4e2e1bce3b97be80064faf3dc Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 17 Dec 2023 02:47:54 +0100 Subject: [PATCH] Minor code tweaks to DX12 generator --- ...lShaderDescriptorGenerator.HlslBytecode.cs | 8 +++--- ...eShaderDescriptorGenerator.HlslBytecode.cs | 16 ++++++++++-- .../Dxc/DxcShaderCompiler.cs | 26 +++++++++---------- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs index bbe3d69c..334bb349 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs @@ -202,7 +202,7 @@ partial class D2DPixelShaderDescriptorGenerator token.ThrowIfCancellationRequested(); // Compile the shader bytecode using the effective parameters - using ComPtr dxcBlobBytecode = D3DCompiler.Compile( + using ComPtr d3DBlob = D3DCompiler.Compile( key.HlslSource.AsSpan(), key.ShaderProfile, key.CompileOptions); @@ -210,12 +210,12 @@ partial class D2DPixelShaderDescriptorGenerator token.ThrowIfCancellationRequested(); // Check whether double precision operations are required - bool requiresDoublePrecisionSupport = D3DCompiler.IsDoublePrecisionSupportRequired(dxcBlobBytecode.Get()); + bool requiresDoublePrecisionSupport = D3DCompiler.IsDoublePrecisionSupportRequired(d3DBlob.Get()); token.ThrowIfCancellationRequested(); - byte* buffer = (byte*)dxcBlobBytecode.Get()->GetBufferPointer(); - int length = checked((int)dxcBlobBytecode.Get()->GetBufferSize()); + byte* buffer = (byte*)d3DBlob.Get()->GetBufferPointer(); + int length = checked((int)d3DBlob.Get()->GetBufferSize()); byte[] array = new ReadOnlySpan(buffer, length).ToArray(); diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs index a5fd49c7..25652d20 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs @@ -10,6 +10,8 @@ using ComputeSharp.SourceGeneration.Models; using ComputeSharp.SourceGenerators.Dxc; using ComputeSharp.SourceGenerators.Models; using Microsoft.CodeAnalysis; +using Windows.Win32; +using Windows.Win32.Graphics.Direct3D.Dxc; using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; namespace ComputeSharp.SourceGenerators; @@ -53,11 +55,21 @@ partial class ComputeShaderDescriptorGenerator token.ThrowIfCancellationRequested(); // Compile the shader bytecode - byte[] bytecode = DxcShaderCompiler.Instance.Compile(key.HlslSource.AsSpan(), key.CompileOptions, token); + using ComPtr dxcBlob = DxcShaderCompiler.Instance.Compile( + key.HlslSource.AsSpan(), + key.CompileOptions, + token); token.ThrowIfCancellationRequested(); - return new HlslBytecodeInfo.Success(Unsafe.As>(ref bytecode)); + byte* buffer = (byte*)dxcBlob.Get()->GetBufferPointer(); + int length = checked((int)dxcBlob.Get()->GetBufferSize()); + + byte[] array = new ReadOnlySpan(buffer, length).ToArray(); + + ImmutableArray bytecode = Unsafe.As>(ref array); + + return new HlslBytecodeInfo.Success(bytecode); } catch (Win32Exception e) { diff --git a/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs b/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs index 9e2edff9..fef2d180 100644 --- a/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs +++ b/src/ComputeSharp.SourceGenerators/Dxc/DxcShaderCompiler.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Text.RegularExpressions; using System.Threading; @@ -73,7 +74,7 @@ internal sealed unsafe class DxcShaderCompiler /// The compile options to use. /// The used to cancel the operation, if needed. /// The bytecode for the compiled shader. - public byte[] Compile(ReadOnlySpan source, CompileOptions compileOptions, CancellationToken token) + public ComPtr Compile(ReadOnlySpan source, CompileOptions compileOptions, CancellationToken token) { using ComPtr dxcResult = default; @@ -232,20 +233,18 @@ internal sealed unsafe class DxcShaderCompiler dxcResult.Get()->GetStatus(&status).Assert(); - // The compilation was successful, so we can extract the shader bytecode - if (status == 0) + // Throw an exception with the input messages, if the compilation fails + if (status != 0) { - using ComPtr dxcBlobBytecode = default; - - dxcResult.Get()->GetResult(dxcBlobBytecode.GetAddressOf()).Assert(); - - byte* buffer = (byte*)dxcBlobBytecode.Get()->GetBufferPointer(); - int length = checked((int)dxcBlobBytecode.Get()->GetBufferSize()); - - return new ReadOnlySpan(buffer, length).ToArray(); + ThrowHslsCompilationException(dxcResult.Get()); } - return ThrowHslsCompilationException(dxcResult.Get()); + using ComPtr dxcBlobBytecode = default; + + // The compilation was successful, so we can extract the shader bytecode + dxcResult.Get()->GetResult(dxcBlobBytecode.GetAddressOf()).Assert(); + + return dxcBlobBytecode.Move(); } /// @@ -253,8 +252,9 @@ internal sealed unsafe class DxcShaderCompiler /// /// The input (faulting) operation. /// This method always throws and never actually returs. + [DoesNotReturn] [MethodImpl(MethodImplOptions.NoInlining)] - private static byte[] ThrowHslsCompilationException(IDxcResult* dxcResult) + private static void ThrowHslsCompilationException(IDxcResult* dxcResult) { using ComPtr dxcBlobEncodingError = default;