diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.Syntax.cs index bff66911..6dcdb293 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.Syntax.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Text; using ComputeSharp.D2D1.SourceGenerators.Models; using Microsoft.CodeAnalysis; @@ -21,7 +22,7 @@ partial class D2DPixelShaderSourceGenerator /// The input bytecode info. /// An opaque instance to transform the final tree into text. /// The resulting instance for the target method. - public static MethodDeclarationSyntax GetSyntax(EmbeddedBytecodeInfo bytecodeInfo, out Func fixup) + public static MethodDeclarationSyntax GetSyntax(EmbeddedBytecodeMethodInfo bytecodeInfo, out Func fixup) { // If there is no precompiled bytecode, just emit a span with a 0 value string bytecodeLiterals = bytecodeInfo.Bytecode.IsDefaultOrEmpty @@ -47,7 +48,8 @@ partial class D2DPixelShaderSourceGenerator MethodDeclaration( GenericName(Identifier("global::System.ReadOnlySpan")) .AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword))), - Identifier("TheMethodName")).AddModifiers(Token(SyntaxKind.PartialKeyword)) + Identifier(bytecodeInfo.MethodName)) + .AddModifiers(bytecodeInfo.Modifiers.Select(Token).ToArray()) .AddAttributeLists( AttributeList(SingletonSeparatedList( Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode")) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs index 9bd70fab..297e8903 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.Execute.cs @@ -99,7 +99,7 @@ partial class D2DPixelShaderSourceGenerator /// 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(HlslShaderSourceInfo sourceInfo, CancellationToken token, out DiagnosticInfo? diagnostic) + public static unsafe ImmutableArray GetBytecode(HlslShaderMethodSourceInfo sourceInfo, CancellationToken token, out DiagnosticInfo? diagnostic) { ImmutableArray bytecode = ImmutableArray.Empty; diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs index cf32c82f..81da5d56 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderSourceGenerator.cs @@ -22,33 +22,38 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato public void Initialize(IncrementalGeneratorInitializationContext context) { // Get all method declarations with the [D2DPixelShaderSource] attribute - IncrementalValuesProvider methodSymbols = + IncrementalValuesProvider<(MethodDeclarationSyntax Syntax, IMethodSymbol Symbol)> methodSymbols = context.SyntaxProvider .CreateSyntaxProvider( static (node, _) => node is MethodDeclarationSyntax { Parent: ClassDeclarationSyntax, AttributeLists.Count: > 0 }, - static (context, _) => (IMethodSymbol)context.SemanticModel.GetDeclaredSymbol(context.Node)!) - .Where(static symbol => symbol.HasAttributeWithFullyQualifiedName("ComputeSharp.D2D1.D2DPixelShaderSourceAttribute")); + static (context, _) => ( + (MethodDeclarationSyntax)context.Node, + Symbol: (IMethodSymbol)context.SemanticModel.GetDeclaredSymbol(context.Node)!)) + .Where(static pair => pair.Symbol.HasAttributeWithFullyQualifiedName("ComputeSharp.D2D1.D2DPixelShaderSourceAttribute")); // Gather info for all annotated methods - IncrementalValuesProvider<(HierarchyInfo Hierarchy, HlslShaderSourceInfo Source, ImmutableArray Diagnostics)> shaderInfoWithErrors = + IncrementalValuesProvider<(HierarchyInfo Hierarchy, HlslShaderMethodSourceInfo Source, ImmutableArray Diagnostics)> shaderInfoWithErrors = methodSymbols .Select(static (item, _) => { ImmutableArray.Builder diagnostics = ImmutableArray.CreateBuilder(); // Get all necessary info for the current shader - string? hlslSource = Execute.GetHlslSource(diagnostics, item) ?? ""; - D2D1ShaderProfile? shaderProfile = Execute.GetShaderProfile(diagnostics, item); - D2D1CompileOptions? compileOptions = Execute.GetCompileOptions(diagnostics, item); + ImmutableArray modifiers = item.Syntax.Modifiers.Select(token => token.Kind()).ToImmutableArray(); + string methodName = item.Symbol.Name; + string? hlslSource = Execute.GetHlslSource(diagnostics, item.Symbol) ?? ""; + D2D1ShaderProfile? shaderProfile = Execute.GetShaderProfile(diagnostics, item.Symbol); + D2D1CompileOptions? compileOptions = Execute.GetCompileOptions(diagnostics, item.Symbol); - HlslShaderSourceInfo sourceInfo = new( + HlslShaderMethodSourceInfo sourceInfo = new( + modifiers, + methodName, hlslSource, shaderProfile, compileOptions, - IsLinkingSupported: false, HasErrors: diagnostics.Count > 0); - HierarchyInfo hierarchy = HierarchyInfo.From(item.ContainingType!); + HierarchyInfo hierarchy = HierarchyInfo.From(item.Symbol.ContainingType!); return (hierarchy, sourceInfo, diagnostics.ToImmutable()); }); @@ -57,13 +62,13 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, _) => item.Diagnostics)); // Filter all items to enable caching at a coarse level, and remove diagnostics - IncrementalValuesProvider<(HierarchyInfo Hierarchy, HlslShaderSourceInfo Source)> shaderInfo = + IncrementalValuesProvider<(HierarchyInfo Hierarchy, HlslShaderMethodSourceInfo Source)> shaderInfo = shaderInfoWithErrors .Select(static (item, token) => (item.Hierarchy, item.Source)) - .WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer.Default); + .WithComparers(HierarchyInfo.Comparer.Default, HlslShaderMethodSourceInfo.Comparer.Default); // Compile the requested shader bytecodes - IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo, DiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors = + IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeMethodInfo BytecodeInfo, DiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors = shaderInfo .Select(static (item, token) => { @@ -74,10 +79,10 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato token.ThrowIfCancellationRequested(); - EmbeddedBytecodeInfo bytecodeInfo = new( + EmbeddedBytecodeMethodInfo bytecodeInfo = new( + item.Source.Modifiers, + item.Source.MethodName, item.Source.HlslSource, - item.Source.ShaderProfile, - item.Source.CompileOptions, bytecode); return (item.Hierarchy, bytecodeInfo, diagnostic); @@ -103,10 +108,10 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato context.ReportDiagnostics(embeddedBytecodeDiagnostics); // Get the filtered sequence to enable caching - IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo)> embeddedBytecode = + IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeMethodInfo BytecodeInfo)> embeddedBytecode = embeddedBytecodeWithErrors .Select(static (item, token) => (item.Hierarchy, item.BytecodeInfo)) - .WithComparers(HierarchyInfo.Comparer.Default, EmbeddedBytecodeInfo.Comparer.Default); + .WithComparers(HierarchyInfo.Comparer.Default, EmbeddedBytecodeMethodInfo.Comparer.Default); // Generate the shader bytecode methods context.RegisterSourceOutput(embeddedBytecode, static (context, item) => diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/EmbeddedBytecodeInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/EmbeddedBytecodeInfo.cs index 532a839e..cf51ee76 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/Models/EmbeddedBytecodeInfo.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/EmbeddedBytecodeInfo.cs @@ -7,7 +7,7 @@ using ComputeSharp.SourceGeneration.Helpers; namespace ComputeSharp.D2D1.SourceGenerators.Models; /// -/// A model representing a shared and its compiled bytecode, if available. +/// A model representing info on a shader that has requested to be precompiled at build time. /// /// The HLSL source for the shader. /// The shader profile to use to compile the shader, if requested. diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/EmbeddedBytecodeMethodInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/EmbeddedBytecodeMethodInfo.cs new file mode 100644 index 00000000..4e74740c --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/EmbeddedBytecodeMethodInfo.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Runtime.InteropServices; +using ComputeSharp.SourceGeneration.Extensions; +using ComputeSharp.SourceGeneration.Helpers; +using Microsoft.CodeAnalysis.CSharp; + +namespace ComputeSharp.D2D1.SourceGenerators.Models; + +/// +/// A model representing info on a shader method that has requested to be precompiled at build time. +/// +///The modifiers for the annotated method. +/// The name of the annotated method. +/// The HLSL source. +/// The compiled shader bytecode, if available. +internal sealed record EmbeddedBytecodeMethodInfo( + ImmutableArray Modifiers, + string MethodName, + string HlslSource, + ImmutableArray Bytecode) +{ + /// + /// An implementation for . + /// + public sealed class Comparer : Comparer + { + /// + protected override void AddToHashCode(ref HashCode hashCode, EmbeddedBytecodeMethodInfo obj) + { + hashCode.AddRange(obj.Modifiers); + hashCode.Add(obj.MethodName); + hashCode.Add(obj.HlslSource); + hashCode.AddBytes(obj.Bytecode.AsSpan()); + } + + /// + protected override bool AreEqual(EmbeddedBytecodeMethodInfo x, EmbeddedBytecodeMethodInfo y) + { + return + MemoryMarshal.Cast(x.Modifiers.AsSpan()).SequenceEqual(MemoryMarshal.Cast(y.Modifiers.AsSpan())) && + x.MethodName == y.MethodName && + x.HlslSource == y.HlslSource && + x.Bytecode.SequenceEqual(y.Bytecode); + } + } +} diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderMethodSourceInfo.cs b/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderMethodSourceInfo.cs new file mode 100644 index 00000000..d6662952 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/Models/HlslShaderMethodSourceInfo.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Immutable; +using System.Runtime.InteropServices; +using ComputeSharp.SourceGeneration.Extensions; +using ComputeSharp.SourceGeneration.Helpers; +using Microsoft.CodeAnalysis.CSharp; + +namespace ComputeSharp.D2D1.SourceGenerators.Models; + +/// +/// A model for extracted info on a processed HLSL shader method. +/// +/// The modifiers for the annotated method. +/// The name of the annotated method. +/// The HLSL source. +/// The shader profile to use to compile the shader, if requested. +/// The compile options to use to compile the shader. +/// Whether any errors have been detected, and therefore the shader compilation should be skipped. +internal sealed record HlslShaderMethodSourceInfo( + ImmutableArray Modifiers, + string MethodName, + string HlslSource, + D2D1ShaderProfile? ShaderProfile, + D2D1CompileOptions? CompileOptions, + bool HasErrors) +{ + /// + /// An implementation for . + /// + public sealed class Comparer : Comparer + { + /// + protected override void AddToHashCode(ref HashCode hashCode, HlslShaderMethodSourceInfo obj) + { + hashCode.AddRange(obj.Modifiers); + hashCode.Add(obj.MethodName); + hashCode.Add(obj.HlslSource); + hashCode.Add(obj.ShaderProfile); + hashCode.Add(obj.CompileOptions); + hashCode.Add(obj.HasErrors); + } + + /// + protected override bool AreEqual(HlslShaderMethodSourceInfo x, HlslShaderMethodSourceInfo y) + { + return + MemoryMarshal.Cast(x.Modifiers.AsSpan()).SequenceEqual(MemoryMarshal.Cast(y.Modifiers.AsSpan())) && + x.MethodName == y.MethodName && + x.HlslSource == y.HlslSource && + x.ShaderProfile == y.ShaderProfile && + x.CompileOptions == y.CompileOptions && + x.HasErrors == y.HasErrors; + } + } +}