From fa8d9446cd28643458df26c01c13bc43693a8dd0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 13 Oct 2023 23:24:04 +0200 Subject: [PATCH] Adopt HlslBytecodeInfo pattern in D3D12 generator --- ...lShaderDescriptorGenerator.HlslBytecode.cs | 2 +- ...nerator.CreateLoadBytecodeMethod.Syntax.cs | 4 +- ...haderGenerator.CreateLoadBytecodeMethod.cs | 80 ++++++++++++------- .../IShaderGenerator.cs | 19 +++-- .../Models/EmbeddedBytecodeInfo.cs | 12 --- .../Models/HlslBytecodeInfo.cs | 39 +++++++++ .../Models/ShaderInfo.cs | 4 +- 7 files changed, 104 insertions(+), 56 deletions(-) delete mode 100644 src/ComputeSharp.SourceGenerators/Models/EmbeddedBytecodeInfo.cs create mode 100644 src/ComputeSharp.SourceGenerators/Models/HlslBytecodeInfo.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs index 087ef987..cdefc28c 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs @@ -148,7 +148,7 @@ partial class D2DPixelShaderDescriptorGenerator /// /// The instance for the shader to compile. /// The used to cancel the operation, if needed. - /// + /// The instance for the current shader. public static HlslBytecodeInfo GetInfo(ref HlslBytecodeInfoKey key, CancellationToken token) { static unsafe HlslBytecodeInfo GetInfo(HlslBytecodeInfoKey key, CancellationToken token) diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs index 57873bd3..3ad8bb90 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.Syntax.cs @@ -24,7 +24,7 @@ partial class IShaderGenerator using (writer.WriteBlock()) { // If there is no HLSL bytecode, there's nothing left to do - if (info.EmbeddedBytecode.Bytecode.IsEmpty) + if (info.HlslInfo is not HlslBytecodeInfo.Success success) { return; } @@ -36,7 +36,7 @@ partial class IShaderGenerator { writer.Write("global::System.ReadOnlySpan bytecode = new byte[] { "); - SyntaxFormattingHelper.WriteByteArrayInitializationExpressions(info.EmbeddedBytecode.Bytecode.AsSpan(), writer); + SyntaxFormattingHelper.WriteByteArrayInitializationExpressions(success.Bytecode.AsSpan(), writer); writer.WriteLine(" };"); writer.WriteLine(); diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs index 1a29c02b..3c178945 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Immutable; using System.ComponentModel; -using System.Globalization; using System.IO; using System.Reflection; using System.Runtime.CompilerServices; @@ -14,7 +13,6 @@ using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; using ComputeSharp.SourceGenerators.Models; using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp.Syntax; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; @@ -31,6 +29,11 @@ partial class IShaderGenerator /// private static partial class LoadBytecode { + /// + /// Indicates whether the required dxcompiler.dll and dxil.dll libraries have been loaded. + /// + private static volatile bool areDxcLibrariesLoaded; + /// /// Gets the thread ids values for a given shader type, if available. /// @@ -113,32 +116,21 @@ partial class IShaderGenerator } /// - /// Indicates whether the required dxcompiler.dll and dxil.dll libraries have been loaded. - /// - private static volatile bool areDxcLibrariesLoaded; - - /// - /// Gets a instance with the logic to try to get a compiled shader bytecode. + /// Gets the instance for the input shader info. /// /// The input instance to process. /// The generated HLSL source code (ignoring captured delegates, if present). /// The used to cancel the operation, if needed. - /// The resulting diagnostic from the processing operation, if any. - /// The instance with the compiled shader bytecode. - public static unsafe ImmutableArray GetBytecode( + /// The instance for the current shader. + public static unsafe HlslBytecodeInfo GetBytecode( ThreadIdsInfo threadIds, string hlslSource, - CancellationToken token, - out DeferredDiagnosticInfo? diagnostic) + CancellationToken token) { - ImmutableArray bytecode = ImmutableArray.Empty; - - // No embedded shader was requested + // Skip even attempting to compile if compilation is disabled (see comments in D2D1 generator) if (threadIds.IsDefault) { - diagnostic = null; - - goto End; + return HlslBytecodeInfo.Missing.Instance; } try @@ -148,12 +140,6 @@ partial class IShaderGenerator token.ThrowIfCancellationRequested(); - // Replace the actual thread num values - hlslSource = hlslSource - .Replace("", threadIds.X.ToString(CultureInfo.InvariantCulture)) - .Replace("", threadIds.Y.ToString(CultureInfo.InvariantCulture)) - .Replace("", threadIds.Z.ToString(CultureInfo.InvariantCulture)); - // Compile the shader bytecode using ComPtr dxcBlobBytecode = ShaderCompiler.Instance.Compile(hlslSource.AsSpan()); @@ -164,20 +150,52 @@ partial class IShaderGenerator byte[] array = new ReadOnlySpan(buffer, length).ToArray(); - bytecode = Unsafe.As>(ref array); - diagnostic = null; + ImmutableArray bytecode = Unsafe.As>(ref array); + + return new HlslBytecodeInfo.Success(bytecode); } catch (Win32Exception e) { - diagnostic = DeferredDiagnosticInfo.Create(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, FixupExceptionMessage(e.Message)); + return new HlslBytecodeInfo.Win32Error(e.NativeErrorCode, FixupExceptionMessage(e.Message)); } catch (DxcCompilationException e) { - diagnostic = DeferredDiagnosticInfo.Create(EmbeddedBytecodeFailedWithDxcCompilationException, FixupExceptionMessage(e.Message)); + return new HlslBytecodeInfo.DxcError(FixupExceptionMessage(e.Message)); + } + } + + /// + /// Gets any diagnostics from a processed instance. + /// + /// The input instance to process. + /// The source instance. + /// The collection of produced instances. + public static void GetInfoDiagnostics( + INamedTypeSymbol structDeclarationSymbol, + HlslBytecodeInfo info, + ImmutableArrayBuilder diagnostics) + { + DiagnosticInfo? diagnostic = null; + + if (info is HlslBytecodeInfo.Win32Error win32Error) + { + diagnostic = DiagnosticInfo.Create( + EmbeddedBytecodeFailedWithWin32Exception, + structDeclarationSymbol, + new object[] { structDeclarationSymbol, win32Error.HResult, win32Error.Message }); + } + else if (info is HlslBytecodeInfo.DxcError dxcError) + { + diagnostic = DiagnosticInfo.Create( + EmbeddedBytecodeFailedWithDxcCompilationException, + structDeclarationSymbol, + new object[] { structDeclarationSymbol, dxcError.Message }); } - End: - return bytecode; + if (diagnostic is not null) + { + diagnostics.Add(diagnostic); + } } /// diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs index 7acef9d5..92a4c5f0 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs @@ -92,18 +92,21 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator token.ThrowIfCancellationRequested(); - ImmutableArray bytecode = LoadBytecode.GetBytecode(threadIds, hlslSourceInfo.HlslSource, token, out DeferredDiagnosticInfo? diagnostic); + HlslBytecodeInfo hlslInfo = LoadBytecode.GetBytecode(threadIds, hlslSourceInfo.HlslSource, token); token.ThrowIfCancellationRequested(); - EmbeddedBytecodeInfo bytecodeInfo = new( - threadIds.X, - threadIds.Y, - threadIds.Z, - bytecode); + LoadBytecode.GetInfoDiagnostics(typeSymbol, hlslInfo, diagnostics); + + token.ThrowIfCancellationRequested(); + + // Also get the hierarchy too + HierarchyInfo hierarchyInfo = HierarchyInfo.From(typeSymbol); + + token.ThrowIfCancellationRequested(); return new ShaderInfo( - Hierarchy: HierarchyInfo.From(typeSymbol), + Hierarchy: hierarchyInfo, DispatchData: new DispatchDataInfo( isPixelShaderLike, fieldInfos, @@ -111,7 +114,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator root32BitConstantCount), DispatchMetadata: dispatchMetadataInfo, HlslShaderSource: hlslSourceInfo, - EmbeddedBytecode: bytecodeInfo, + HlslInfo: hlslInfo, ThreadIds: threadIds, Diagnostcs: diagnostics.ToImmutable()); }) diff --git a/src/ComputeSharp.SourceGenerators/Models/EmbeddedBytecodeInfo.cs b/src/ComputeSharp.SourceGenerators/Models/EmbeddedBytecodeInfo.cs deleted file mode 100644 index f6fec7a6..00000000 --- a/src/ComputeSharp.SourceGenerators/Models/EmbeddedBytecodeInfo.cs +++ /dev/null @@ -1,12 +0,0 @@ -using ComputeSharp.SourceGeneration.Helpers; - -namespace ComputeSharp.SourceGenerators.Models; - -/// -/// A model representing a compiled shader bytecode, or none. -/// -/// The thread ids value for the X axis. -/// The thread ids value for the Y axis. -/// The thread ids value for the Z axis. -/// The compiled shader bytecode, if available. -internal sealed record EmbeddedBytecodeInfo(int X, int Y, int Z, EquatableArray Bytecode); \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Models/HlslBytecodeInfo.cs b/src/ComputeSharp.SourceGenerators/Models/HlslBytecodeInfo.cs new file mode 100644 index 00000000..63fe4e9d --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/Models/HlslBytecodeInfo.cs @@ -0,0 +1,39 @@ +using ComputeSharp.SourceGeneration.Helpers; + +namespace ComputeSharp.SourceGenerators.Models; + +/// +/// A model representing info on compiled HLSL bytecode, if available. +/// +internal abstract record HlslBytecodeInfo +{ + /// + /// A successfully compiled HLSL shader. + /// + /// The resulting HLSL bytecode. + public sealed record Success(EquatableArray Bytecode) : HlslBytecodeInfo; + + /// + /// An HLSL shader that failed to compile due to a Win32 error. + /// + /// The HRESULT for the error. + /// The error message from compiling the shader. + public sealed record Win32Error(int HResult, string Message) : HlslBytecodeInfo; + + /// + /// An HLSL shader that failed to compile due to an DXC compiler error. + /// + /// The error message from compiling the shader. + public sealed record DxcError(string Message) : HlslBytecodeInfo; + + /// + /// No HLSL bytecode is available (ie. compilation was not requested or some other error was detected). + /// + public sealed record Missing : HlslBytecodeInfo + { + /// + /// Gets the shared instance. + /// + public static Missing Instance { get; } = new(); + } +} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs b/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs index a94bd8d1..65c72ef5 100644 --- a/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs +++ b/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs @@ -10,7 +10,7 @@ namespace ComputeSharp.SourceGenerators.Models; /// The gathered shader dispatch data. /// The gathered shader dispatch metadata. /// The processed HLSL source for the shader. -/// The info on the compiled bytecode, if available. +/// The value with processed info on the shader. /// The thread ids for the shader, if compilation is requested. /// The discovered diagnostics, if any. internal sealed record ShaderInfo( @@ -18,6 +18,6 @@ internal sealed record ShaderInfo( DispatchDataInfo DispatchData, DispatchMetadataInfo DispatchMetadata, HlslShaderSourceInfo HlslShaderSource, - EmbeddedBytecodeInfo EmbeddedBytecode, + HlslBytecodeInfo HlslInfo, ThreadIdsInfo ThreadIds, EquatableArray Diagnostcs); \ No newline at end of file