From 50f3b74d3c54b3e2a6cfd779d160aae9b0d8cd00 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 28 Sep 2023 23:08:20 +0200 Subject: [PATCH] Update HLSL compilation method in the generator --- ...haderGenerator.CreateLoadBytecodeMethod.cs | 102 ++++++++++-------- .../ID2D1ShaderGenerator.cs | 12 ++- .../Models/HlslBytecodeInfo.cs | 20 +++- 3 files changed, 81 insertions(+), 53 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs index cc9a22f1..733e10e6 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadBytecodeMethod.cs @@ -27,11 +27,11 @@ partial class ID2D1ShaderGenerator internal static partial class LoadBytecode { /// - /// Extracts the shader profile for the current shader. + /// Extracts the requested shader profile for the current shader. /// /// The input instance to process. - /// The shader profile to use to compile the shader, if present. - public static D2D1ShaderProfile? GetShaderProfile(INamedTypeSymbol structDeclarationSymbol) + /// The requested shader profile to use to compile the shader, if present. + public static D2D1ShaderProfile? GetRequestedShaderProfile(INamedTypeSymbol structDeclarationSymbol) { if (structDeclarationSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DShaderProfileAttribute", out AttributeData? attributeData)) { @@ -47,12 +47,12 @@ partial class ID2D1ShaderGenerator } /// - /// Extracts the compile options for the current shader. + /// Extracts the requested compile options for the current shader. /// /// The collection of produced instances. /// The input instance to process. - /// The compile options to use to compile the shader, if present. - public static D2D1CompileOptions? GetCompileOptions(ImmutableArrayBuilder diagnostics, INamedTypeSymbol structDeclarationSymbol) + /// The requested compile options to use to compile the shader, if present. + public static D2D1CompileOptions? GetRequestedCompileOptions(ImmutableArrayBuilder diagnostics, INamedTypeSymbol structDeclarationSymbol) { if (structDeclarationSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.D2D1.D2DCompileOptionsAttribute", out AttributeData? attributeData)) { @@ -79,6 +79,30 @@ partial class ID2D1ShaderGenerator return null; } + /// + /// Gets the effective shader profile to use. + /// + /// The requested shader profile. + /// The effective shader profile. + public static D2D1ShaderProfile GetEffectiveShaderProfile(D2D1ShaderProfile? shaderProfile) + { + // The effective shader profile is either be the requested one, or the default value (which maps to PS5.0) + return shaderProfile ?? D2D1ShaderProfile.PixelShader50; + } + + /// + /// Gets the effective compile options to use. + /// + /// The requested compile options. + /// Whether the input shader supports linking. + /// The effective compile options. + public static D2D1CompileOptions GetEffectiveCompileOptions(D2D1CompileOptions? compileOptions, bool isLinkingSupported) + { + // If an explicit set of compile options is provided, use that directly. Otherwise, use the default + // options plus the option to enable linking only if the shader can potentially support it. + return compileOptions ?? (D2D1CompileOptions.Default | (isLinkingSupported ? D2D1CompileOptions.EnableLinking : 0)); + } + /// /// Extracts the metadata definition for the current shader. /// @@ -113,49 +137,43 @@ partial class ID2D1ShaderGenerator } /// - /// Gets an instance with the compiled bytecode for the current shader. + /// Gets the instance for the input shader info. /// - /// The source info for the shader to compile. + /// The input HLSL source code. + /// The requested shader profile, if available. + /// The requested compile options, if available. + /// The effective shader profile. + /// The effective compile options. + /// Whether any errors were produced when analyzing the shader. /// The used to cancel the operation, if needed. - /// The effective shader profile used to create the shader bytecode, or the default profile to use. - /// The effective compile options used to create the shader bytecode, or the default options to use. - /// The resulting diagnostic from the processing operation, if any. - /// The instance with the compiled shader bytecode. - public static unsafe ImmutableArray GetBytecode( - HlslShaderSourceInfo sourceInfo, - CancellationToken token, - out D2D1ShaderProfile shaderProfile, - out D2D1CompileOptions compileOptions, - out DeferredDiagnosticInfo? diagnostic) + /// + public static unsafe HlslBytecodeInfo GetInfo( + string hlslSource, + D2D1ShaderProfile? requestedShaderProfile, + D2D1CompileOptions? requestedCompileOptions, + D2D1ShaderProfile effectiveShaderProfile, + D2D1CompileOptions effectiveCompileOptions, + bool hasErrors, + CancellationToken token) { - // Set the shader profile to either be the requested one, or the default value (which maps to PS5.0) - shaderProfile = sourceInfo.ShaderProfile ?? D2D1ShaderProfile.PixelShader50; - - // If an explicit set of compile options is provided, use that directly. Otherwise, use the default - // options plus the option to enable linking only if the shader can potentially support it. - compileOptions = sourceInfo.CompileOptions ?? (D2D1CompileOptions.Default | (sourceInfo.IsLinkingSupported ? D2D1CompileOptions.EnableLinking : 0)); - - ImmutableArray bytecode = ImmutableArray.Empty; - // No embedded shader was requested, or there were some errors earlier in the pipeline. // In this case, skip the compilation, as diagnostic will be emitted for those anyway. // Compiling would just add overhead and result in more errors, as the HLSL would be invalid. - if (sourceInfo is { HasErrors: true } or { ShaderProfile: null }) + // We also skip compilation if no shader profile has been requested (we never just assume one). + if (hasErrors || requestedShaderProfile is null) { - diagnostic = null; - - goto End; + return HlslBytecodeInfo.Missing.Instance; } try { token.ThrowIfCancellationRequested(); - // Compile the shader bytecode using the requested parameters + // Compile the shader bytecode using the effective parameters using ComPtr dxcBlobBytecode = D3DCompiler.Compile( - sourceInfo.HlslSource.AsSpan(), - sourceInfo.ShaderProfile.Value, - compileOptions); + hlslSource.AsSpan(), + effectiveShaderProfile, + effectiveCompileOptions); token.ThrowIfCancellationRequested(); @@ -164,22 +182,18 @@ partial class ID2D1ShaderGenerator 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) { - compileOptions = default; - diagnostic = DeferredDiagnosticInfo.Create(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, FixupExceptionMessage(e.Message)); + return new HlslBytecodeInfo.Win32Error(FixupExceptionMessage(e.Message)); } catch (FxcCompilationException e) { - compileOptions = default; - diagnostic = DeferredDiagnosticInfo.Create(EmbeddedBytecodeFailedWithFxcCompilationException, FixupExceptionMessage(e.Message)); + return new HlslBytecodeInfo.FxcError(FixupExceptionMessage(e.Message)); } - - End: - return bytecode; } /// diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs index eacecc0e..8351f5b1 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs @@ -113,9 +113,11 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator token.ThrowIfCancellationRequested(); // Get the shader profile and linking info for LoadBytecode() - D2D1ShaderProfile? shaderProfile = LoadBytecode.GetShaderProfile(typeSymbol); - D2D1CompileOptions? compileOptions = LoadBytecode.GetCompileOptions(diagnostics, typeSymbol); bool isLinkingSupported = LoadBytecode.IsSimpleInputShader(typeSymbol, inputCount); + D2D1ShaderProfile? requestedShaderProfile = LoadBytecode.GetRequestedShaderProfile(typeSymbol); + D2D1CompileOptions? requestedCompileOptions = LoadBytecode.GetRequestedCompileOptions(diagnostics, typeSymbol); + D2D1ShaderProfile effectiveShaderProfile = LoadBytecode.GetEffectiveShaderProfile(requestedShaderProfile); + D2D1CompileOptions effectiveCompileOptions = LoadBytecode.GetEffectiveCompileOptions(requestedCompileOptions, isLinkingSupported); token.ThrowIfCancellationRequested(); @@ -150,8 +152,8 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator ResourceTextureDescriptions: resourceTextureDescriptions, HlslShaderSource: new HlslShaderSourceInfo( hlslSource, - shaderProfile, - compileOptions, + requestedShaderProfile, + requestedCompileOptions, isLinkingSupported, HasErrors: diagnostics.Count > 0), BufferPrecision: bufferPrecision, @@ -322,7 +324,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator shaderBytecodeInfo .Select(static (item, token) => { - ImmutableArray bytecode = LoadBytecode.GetBytecode( + ImmutableArray bytecode = LoadBytecode.GetInfo( item.Source, token, out D2D1ShaderProfile shaderProfile, diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslBytecodeInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslBytecodeInfo.cs index 4640c5b7..0116c5e3 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslBytecodeInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslBytecodeInfo.cs @@ -14,13 +14,25 @@ internal abstract record HlslBytecodeInfo public sealed record Success(EquatableArray Bytecode) : HlslBytecodeInfo; /// - /// An HLSL shader that failed to compile. + /// An HLSL shader that failed to compile due to a Win32 error. /// /// The error message from compiling the shader. - public sealed record Error(string Message) : HlslBytecodeInfo; + public sealed record Win32Error(string Message) : HlslBytecodeInfo; /// - /// No HLSL bytecode is available (ie. compilation was not requested). + /// An HLSL shader that failed to compile due to an FXC compiler error. /// - public sealed record Missing : HlslBytecodeInfo; + /// The error message from compiling the shader. + public sealed record FxcError(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