diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs index 1453de43..1f86db7d 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs @@ -26,7 +26,7 @@ partial class IShaderGenerator writer.WriteLine("hlslSource ="); writer.IncreaseIndent(); writer.WriteLine("\"\"\""); - writer.Write(info.HlslSource, isMultiline: true); + writer.Write(info.HlslInfoKey.HlslSource, isMultiline: true); writer.WriteLine("\"\"\";"); writer.DecreaseIndent(); } diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs index dd4774c0..6a72ca51 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs @@ -11,6 +11,7 @@ using ComputeSharp.Shaders.Translation; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; +using ComputeSharp.SourceGenerators.Models; using Microsoft.CodeAnalysis; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; @@ -41,12 +42,14 @@ partial class IShaderGenerator /// The thread ids value for the X axis. /// The thread ids value for the Y axis. /// The thread ids value for the Z axis. + /// Whether compilation should be attempted for the current info. public static void GetInfo( ImmutableArrayBuilder diagnostics, INamedTypeSymbol structDeclarationSymbol, out int threadsX, out int threadsY, - out int threadsZ) + out int threadsZ, + out bool isCompilationEnabled) { // Try to get the attribute that controls shader precompilation (has to be present when ComputeSharp.Dynamic is not referenced) if (!structDeclarationSymbol.TryGetAttributeWithFullyQualifiedMetadataName(typeof(EmbeddedBytecodeAttribute).FullName, out AttributeData? attribute)) @@ -55,6 +58,7 @@ partial class IShaderGenerator diagnostics.Add(MissingEmbeddedBytecodeAttributeWhenDynamicShaderCompilationIsNotSupported, structDeclarationSymbol, structDeclarationSymbol); threadsX = threadsY = threadsZ = 0; + isCompilationEnabled = false; return; } @@ -82,9 +86,12 @@ partial class IShaderGenerator diagnostics.Add(InvalidEmbeddedBytecodeDispatchAxis, structDeclarationSymbol, structDeclarationSymbol); threadsX = threadsY = threadsZ = 0; + isCompilationEnabled = false; return; } + + isCompilationEnabled = true; } else if ( attribute.ConstructorArguments.Length != 3 || @@ -99,6 +106,7 @@ partial class IShaderGenerator diagnostics.Add(InvalidEmbeddedBytecodeThreadIds, structDeclarationSymbol, structDeclarationSymbol); threadsX = threadsY = threadsZ = 0; + isCompilationEnabled = false; return; } @@ -107,20 +115,20 @@ partial class IShaderGenerator threadsX = explicitThreadsX; threadsY = explicitThreadsY; threadsZ = explicitThreadsZ; + isCompilationEnabled = true; } } /// /// Gets the instance for the input shader info. /// - /// The generated HLSL source code (ignoring captured delegates, if present). - /// Whether compilation is enabled. + /// 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(string hlslSource, bool isCompilationEnabled, CancellationToken token) + public static unsafe HlslBytecodeInfo GetBytecode(HlslBytecodeInfoKey key, CancellationToken token) { // Skip even attempting to compile if compilation is disabled (see comments in D2D1 generator) - if (!isCompilationEnabled) + if (!key.IsCompilationEnabled) { return HlslBytecodeInfo.Missing.Instance; } @@ -133,7 +141,7 @@ partial class IShaderGenerator token.ThrowIfCancellationRequested(); // Compile the shader bytecode - using ComPtr dxcBlobBytecode = ShaderCompiler.Instance.Compile(hlslSource.AsSpan()); + using ComPtr dxcBlobBytecode = ShaderCompiler.Instance.Compile(key.HlslSource.AsSpan()); token.ThrowIfCancellationRequested(); diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs index acc3d723..cf3a562a 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs @@ -70,7 +70,8 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator typeSymbol, out int threadsX, out int threadsY, - out int threadsZ); + out int threadsZ, + out bool isCompilationEnabled); token.ThrowIfCancellationRequested(); @@ -98,10 +99,11 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator token.ThrowIfCancellationRequested(); - // Like for D2D1 shaders, disable compilation if any errors are present - bool isCompilationEnabled = diagnostics.WrittenSpan.IsEmpty; + // Prepare the lookup key for the HLSL shader bytecode + HlslBytecodeInfoKey hlslInfoKey = new(hlslSource, isCompilationEnabled); - HlslBytecodeInfo hlslInfo = LoadBytecode.GetBytecode(hlslSource, isCompilationEnabled, token); + // Try to get the HLSL bytecode + HlslBytecodeInfo hlslInfo = LoadBytecode.GetBytecode(hlslInfoKey, token); token.ThrowIfCancellationRequested(); @@ -124,7 +126,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator Fields: fieldInfos, ResourceDescriptors: resourceDescriptors, Root32BitConstantCount: root32BitConstantCount, - HlslSource: hlslSource, + HlslInfoKey: hlslInfoKey, HlslInfo: hlslInfo, Diagnostcs: diagnostics.ToImmutable()); }) diff --git a/src/ComputeSharp.SourceGenerators/Models/HlslBytecodeInfoKey.cs b/src/ComputeSharp.SourceGenerators/Models/HlslBytecodeInfoKey.cs new file mode 100644 index 00000000..78bc27a6 --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/Models/HlslBytecodeInfoKey.cs @@ -0,0 +1,10 @@ +using ComputeSharp.SourceGeneration.Models; + +namespace ComputeSharp.SourceGenerators.Models; + +/// +/// A model with info to be a unique key for instances. +/// +/// The input HLSL source code. +/// Whether compilation should be attempted for the current info. +internal sealed record HlslBytecodeInfoKey(string HlslSource, bool IsCompilationEnabled); \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs b/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs index 6f593b31..c9cd430a 100644 --- a/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs +++ b/src/ComputeSharp.SourceGenerators/Models/ShaderInfo.cs @@ -15,7 +15,7 @@ namespace ComputeSharp.SourceGenerators.Models; /// The description on shader instance fields. /// The sequence of resource descriptors for the shader. /// The size of the shader root signature, in 32 bit constants. -/// The HLSL source for the shader. +/// The key with processed info on the shader. /// The value with processed info on the shader. /// The discovered diagnostics, if any. internal sealed record ShaderInfo( @@ -28,6 +28,6 @@ internal sealed record ShaderInfo( EquatableArray Fields, EquatableArray ResourceDescriptors, int Root32BitConstantCount, - string HlslSource, + HlslBytecodeInfoKey HlslInfoKey, HlslBytecodeInfo HlslInfo, EquatableArray Diagnostcs); \ No newline at end of file