diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs index fe1ba94d..19bf6d1f 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslBytecode.cs @@ -1,16 +1,8 @@ using System; -using System.Collections.Immutable; -using System.ComponentModel; -using System.Runtime.CompilerServices; -using System.Threading; -using ComputeSharp.D2D1.Shaders.Translation; -using ComputeSharp.D2D1.SourceGenerators.Models; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; -using Windows.Win32; -using Windows.Win32.Graphics.Direct3D; using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors; namespace ComputeSharp.D2D1.SourceGenerators; @@ -23,11 +15,6 @@ partial class D2DPixelShaderDescriptorGenerator /// internal static partial class HlslBytecode { - /// - /// The shared cache of values. - /// - private static readonly DynamicCache HlslBytecodeCache = new(); - /// /// Extracts the requested shader profile for the current shader. /// @@ -178,143 +165,5 @@ partial class D2DPixelShaderDescriptorGenerator return isSimpleInputShader; } - - /// - /// Gets the instance for the input shader info. - /// - /// 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) - { - // Check if the compilation is not enabled (eg. if there's been 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 (!key.IsCompilationEnabled) - { - return HlslBytecodeInfo.Missing.Instance; - } - - try - { - token.ThrowIfCancellationRequested(); - - // Compile the shader bytecode using the effective parameters - using ComPtr d3DBlob = D3DCompiler.Compile( - key.HlslSource.AsSpan(), - key.ShaderProfile, - key.CompileOptions, - token); - - token.ThrowIfCancellationRequested(); - - // Check whether double precision operations are required - bool requiresDoublePrecisionSupport = D3DCompiler.IsDoublePrecisionSupportRequired(d3DBlob.Get()); - - token.ThrowIfCancellationRequested(); - - byte* buffer = (byte*)d3DBlob.Get()->GetBufferPointer(); - int length = checked((int)d3DBlob.Get()->GetBufferSize()); - - byte[] array = new ReadOnlySpan(buffer, length).ToArray(); - - ImmutableArray bytecode = Unsafe.As>(ref array); - - return new HlslBytecodeInfo.Success(bytecode, requiresDoublePrecisionSupport); - } - catch (Win32Exception e) - { - return new HlslBytecodeInfo.Win32Error(e.NativeErrorCode, D3DCompiler.PrettifyFxcErrorMessage(e.Message)); - } - catch (FxcCompilationException e) - { - return new HlslBytecodeInfo.CompilerError(D3DCompiler.PrettifyFxcErrorMessage(e.Message)); - } - } - - // Get or create the HLSL bytecode compilation result for the input key. The dynamic cache - // will take care of retrieving an existing cached value if the same shader has been compiled - // already with the same parameters. After this call, callers must use the updated key value. - return HlslBytecodeCache.GetOrCreate(ref key, GetInfo, token); - } - - /// - /// 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( - HlslBytecodeFailedWithWin32Exception, - structDeclarationSymbol, - structDeclarationSymbol, - win32Error.HResult, - win32Error.Message); - } - else if (info is HlslBytecodeInfo.CompilerError fxcError) - { - diagnostic = DiagnosticInfo.Create( - HlslBytecodeFailedWithCompilationException, - structDeclarationSymbol, - structDeclarationSymbol, - fxcError.Message); - } - - if (diagnostic is not null) - { - diagnostics.Add(diagnostic); - } - } - - /// - /// Gets the diagnostics for when double precision support is configured incorrectly. - /// - /// The input instance to process. - /// The source instance. - /// The collection of produced instances. - public static void GetDoublePrecisionSupportDiagnostics( - INamedTypeSymbol structDeclarationSymbol, - HlslBytecodeInfo info, - ImmutableArrayBuilder diagnostics) - { - // If we have no compiled HLSL bytecode, there is nothing more to do - if (info is not HlslBytecodeInfo.Success success) - { - return; - } - - bool hasD2DRequiresDoublePrecisionSupportAttribute = structDeclarationSymbol.TryGetAttributeWithFullyQualifiedMetadataName( - "ComputeSharp.D2D1.D2DRequiresDoublePrecisionSupportAttribute", - out AttributeData? attributeData); - - // Check the two cases where diagnostics are necessary: - // - The shader does not have [D2DRequiresDoublePrecisionSupport], but it needs it - // - The shader has [D2DRequiresDoublePrecisionSupport], but it does not need it - if (!hasD2DRequiresDoublePrecisionSupportAttribute && success.RequiresDoublePrecisionSupport) - { - diagnostics.Add(DiagnosticInfo.Create( - MissingRequiresDoublePrecisionSupportAttribute, - structDeclarationSymbol, - structDeclarationSymbol)); - } - else if (hasD2DRequiresDoublePrecisionSupportAttribute && !success.RequiresDoublePrecisionSupport) - { - diagnostics.Add(DiagnosticInfo.Create( - UnnecessaryRequiresDoublePrecisionSupportAttribute, - attributeData!.GetLocation(), - structDeclarationSymbol)); - } - } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs index d46dc2c0..4266885c 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs @@ -163,13 +163,13 @@ public sealed partial class D2DPixelShaderDescriptorGenerator : IIncrementalGene isCompilationEnabled); // Get the existing compiled shader, or compile the processed HLSL code - HlslBytecodeInfo hlslInfo = HlslBytecode.GetInfo(ref hlslInfoKey, token); + HlslBytecodeInfo hlslInfo = HlslBytecodeSyntaxProcessor.GetInfo(ref hlslInfoKey, token); token.ThrowIfCancellationRequested(); // Append any diagnostic for the shader compilation - HlslBytecode.GetInfoDiagnostics(typeSymbol, hlslInfo, diagnostics); - HlslBytecode.GetDoublePrecisionSupportDiagnostics(typeSymbol, hlslInfo, diagnostics); + HlslBytecodeSyntaxProcessor.GetInfoDiagnostics(typeSymbol, hlslInfo, diagnostics); + HlslBytecodeSyntaxProcessor.GetDoublePrecisionSupportDiagnostics(typeSymbol, hlslInfo, diagnostics); token.ThrowIfCancellationRequested(); diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs index f861bea0..ed19e18e 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs @@ -4,10 +4,10 @@ using ComputeSharp.D2D1.SourceGenerators.Models; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; +using ComputeSharp.SourceGeneration.SyntaxProcessors; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; -using static ComputeSharp.D2D1.SourceGenerators.D2DPixelShaderDescriptorGenerator; namespace ComputeSharp.D2D1.SourceGenerators; @@ -55,7 +55,7 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato isCompilationEnabled); // Get the existing compiled shader, or compile the processed HLSL code - HlslBytecodeInfo hlslInfo = HlslBytecode.GetInfo(ref hlslInfoKey, token); + HlslBytecodeInfo hlslInfo = HlslBytecodeSyntaxProcessor.GetInfo(ref hlslInfoKey, token); token.ThrowIfCancellationRequested(); diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs index 437f7506..9e10d0b5 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslBytecode.cs @@ -1,17 +1,7 @@ -using System; -using System.Collections.Immutable; -using System.ComponentModel; -using System.Runtime.CompilerServices; -using System.Threading; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; 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; @@ -23,71 +13,6 @@ partial class ComputeShaderDescriptorGenerator /// private static partial class HlslBytecode { - /// - /// The shared cache of values. - /// - private static readonly DynamicCache HlslBytecodeCache = new(); - - /// - /// Gets the instance for the input shader info. - /// - /// The instance for the shader to compile. - /// The used to cancel the operation, if needed. - /// The instance for the current shader. - public static unsafe HlslBytecodeInfo GetBytecode(ref HlslBytecodeInfoKey key, CancellationToken token) - { - static unsafe HlslBytecodeInfo GetInfo(HlslBytecodeInfoKey key, CancellationToken token) - { - // Skip even attempting to compile if compilation is disabled (see comments in D2D1 generator) - if (!key.IsCompilationEnabled) - { - return HlslBytecodeInfo.Missing.Instance; - } - - try - { - token.ThrowIfCancellationRequested(); - - // Try to load dxcompiler.dll and dxil.dll - DxcLibraryLoader.LoadNativeDxcLibraries(); - - token.ThrowIfCancellationRequested(); - - // Compile the shader bytecode - using ComPtr dxcBlob = DxcShaderCompiler.Instance.Compile( - key.HlslSource.AsSpan(), - key.CompileOptions, - token); - - token.ThrowIfCancellationRequested(); - - // Check whether double precision operations are required - bool requiresDoublePrecisionSupport = DxcShaderCompiler.Instance.IsDoublePrecisionSupportRequired(dxcBlob.Get()); - - token.ThrowIfCancellationRequested(); - - 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, requiresDoublePrecisionSupport); - } - catch (Win32Exception e) - { - return new HlslBytecodeInfo.Win32Error(e.NativeErrorCode, DxcShaderCompiler.FixupExceptionMessage(e.Message)); - } - catch (DxcCompilationException e) - { - return new HlslBytecodeInfo.CompilerError(DxcShaderCompiler.FixupExceptionMessage(e.Message)); - } - } - - return HlslBytecodeCache.GetOrCreate(ref key, GetInfo, token); - } - /// /// Extracts the compile options for the current shader. /// @@ -105,80 +30,5 @@ partial class ComputeShaderDescriptorGenerator return CompileOptions.Default; } - - /// - /// 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( - HlslBytecodeFailedWithWin32Exception, - structDeclarationSymbol, - structDeclarationSymbol, - win32Error.HResult, - win32Error.Message); - } - else if (info is HlslBytecodeInfo.CompilerError dxcError) - { - diagnostic = DiagnosticInfo.Create( - HlslBytecodeFailedWithCompilationException, - structDeclarationSymbol, - structDeclarationSymbol, - dxcError.Message); - } - - if (diagnostic is not null) - { - diagnostics.Add(diagnostic); - } - } - - /// - /// Gets the diagnostics for when double precision support is configured incorrectly. - /// - /// The input instance to process. - /// The source instance. - /// The collection of produced instances. - public static void GetDoublePrecisionSupportDiagnostics( - INamedTypeSymbol structDeclarationSymbol, - HlslBytecodeInfo info, - ImmutableArrayBuilder diagnostics) - { - // If we have no compiled HLSL bytecode, there is nothing more to do - if (info is not HlslBytecodeInfo.Success success) - { - return; - } - - bool hasD2DRequiresDoublePrecisionSupportAttribute = structDeclarationSymbol.TryGetAttributeWithFullyQualifiedMetadataName( - "ComputeSharp.RequiresDoublePrecisionSupportAttribute", - out AttributeData? attributeData); - - // Check the two cases where diagnostics are necessary (same as the D2D generator) - if (!hasD2DRequiresDoublePrecisionSupportAttribute && success.RequiresDoublePrecisionSupport) - { - diagnostics.Add(DiagnosticInfo.Create( - MissingRequiresDoublePrecisionSupportAttribute, - structDeclarationSymbol, - structDeclarationSymbol)); - } - else if (hasD2DRequiresDoublePrecisionSupportAttribute && !success.RequiresDoublePrecisionSupport) - { - diagnostics.Add(DiagnosticInfo.Create( - UnnecessaryRequiresDoublePrecisionSupportAttribute, - attributeData!.GetLocation(), - structDeclarationSymbol)); - } - } } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs index c218dd6e..4d3e6cbd 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.cs @@ -117,12 +117,12 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener HlslBytecodeInfoKey hlslInfoKey = new(hlslSource, compileOptions, isCompilationEnabled); // Try to get the HLSL bytecode - HlslBytecodeInfo hlslInfo = HlslBytecode.GetBytecode(ref hlslInfoKey, token); + HlslBytecodeInfo hlslInfo = HlslBytecodeSyntaxProcessor.GetInfo(ref hlslInfoKey, token); token.ThrowIfCancellationRequested(); - HlslBytecode.GetInfoDiagnostics(typeSymbol, hlslInfo, diagnostics); - HlslBytecode.GetDoublePrecisionSupportDiagnostics(typeSymbol, hlslInfo, diagnostics); + HlslBytecodeSyntaxProcessor.GetInfoDiagnostics(typeSymbol, hlslInfo, diagnostics); + HlslBytecodeSyntaxProcessor.GetDoublePrecisionSupportDiagnostics(typeSymbol, hlslInfo, diagnostics); token.ThrowIfCancellationRequested(); diff --git a/src/ComputeSharp.SourceGenerators/SyntaxProcessors/HlslBytecodeSyntaxProcessor.D3D12.cs b/src/ComputeSharp.SourceGenerators/SyntaxProcessors/HlslBytecodeSyntaxProcessor.D3D12.cs index 123b10ad..6d49101d 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxProcessors/HlslBytecodeSyntaxProcessor.D3D12.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxProcessors/HlslBytecodeSyntaxProcessor.D3D12.cs @@ -18,6 +18,12 @@ partial class HlslBytecodeSyntaxProcessor /// private static partial ComPtr Compile(HlslBytecodeInfoKey key, CancellationToken token) { + // Try to load dxcompiler.dll and dxil.dll + DxcLibraryLoader.LoadNativeDxcLibraries(); + + token.ThrowIfCancellationRequested(); + + // Compile the shader bytecode using DXC return DxcShaderCompiler.Instance.Compile( key.HlslSource.AsSpan(), key.CompileOptions,