From eefd0de9f062e55e4008087d10200382417aeae3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 5 Dec 2023 15:51:46 +0100 Subject: [PATCH] Add mapping of static fields to HLSL rewriters --- .../D2DPixelShaderDescriptorGenerator.HlslSource.cs | 9 +++++++++ .../HlslDefinitionsSyntaxProcessor.cs | 3 +++ .../SyntaxRewriters/HlslSourceRewriter.cs | 8 ++++++++ .../SyntaxRewriters/ShaderSourceRewriter.cs | 11 +++++++++-- .../SyntaxRewriters/StaticFieldRewriter.cs | 4 +++- .../ComputeShaderDescriptorGenerator.HlslSource.cs | 9 +++++++++ 6 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs index abef3b7d..2e00cabd 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs @@ -53,6 +53,7 @@ partial class D2DPixelShaderDescriptorGenerator Dictionary instanceMethods = new(SymbolEqualityComparer.Default); Dictionary constructors = new(SymbolEqualityComparer.Default); Dictionary constantDefinitions = new(SymbolEqualityComparer.Default); + Dictionary staticFieldDefinitions = new(SymbolEqualityComparer.Default); token.ThrowIfCancellationRequested(); @@ -78,6 +79,7 @@ partial class D2DPixelShaderDescriptorGenerator instanceMethods, constructors, constantDefinitions, + staticFieldDefinitions, token, out bool methodsNeedD2D1RequiresScenePosition); @@ -89,6 +91,7 @@ partial class D2DPixelShaderDescriptorGenerator structDeclarationSymbol, discoveredTypes, constantDefinitions, + staticFieldDefinitions, token, out bool fieldsNeedD2D1RequiresScenePosition); @@ -238,6 +241,7 @@ partial class D2DPixelShaderDescriptorGenerator /// The type symbol for the shader type. /// The collection of currently discovered types. /// The collection of discovered constant definitions. + /// The collection of discovered static field definitions. /// The used to cancel the operation, if needed. /// Whether or not the shader needs the [D2DRequiresScenePosition] annotation. /// A sequence of static constant fields in . @@ -247,6 +251,7 @@ partial class D2DPixelShaderDescriptorGenerator INamedTypeSymbol structDeclarationSymbol, ICollection discoveredTypes, IDictionary constantDefinitions, + IDictionary staticFieldDefinitions, CancellationToken token, out bool needsD2D1RequiresScenePosition) { @@ -267,6 +272,7 @@ partial class D2DPixelShaderDescriptorGenerator semanticModel, discoveredTypes, constantDefinitions, + staticFieldDefinitions, diagnostics, token, out string? name, @@ -294,6 +300,7 @@ partial class D2DPixelShaderDescriptorGenerator /// The collection of discovered instance methods for custom struct types. /// The collection of discovered constructors for custom struct types. /// The collection of discovered constant definitions. + /// The collection of discovered static field definitions. /// Whether or not the shader needs the [D2DRequiresScenePosition] annotation. /// The used to cancel the operation, if needed. /// A sequence of processed methods in , and the entry point. @@ -306,6 +313,7 @@ partial class D2DPixelShaderDescriptorGenerator IDictionary instanceMethods, IDictionary constructors, IDictionary constantDefinitions, + IDictionary staticFieldDefinitions, CancellationToken token, out bool needsD2D1RequiresScenePosition) { @@ -350,6 +358,7 @@ partial class D2DPixelShaderDescriptorGenerator instanceMethods, constructors, constantDefinitions, + staticFieldDefinitions, diagnostics, token, isShaderEntryPoint); diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslDefinitionsSyntaxProcessor.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslDefinitionsSyntaxProcessor.cs index 97f6d3d6..cbff40dd 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslDefinitionsSyntaxProcessor.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslDefinitionsSyntaxProcessor.cs @@ -49,6 +49,7 @@ internal static class HlslDefinitionsSyntaxProcessor /// The instance for the type to process. /// The collection of currently discovered types. /// The collection of discovered constant definitions. + /// The collection of discovered static field definitions. /// The collection of produced instances. /// The used to cancel the operation, if needed. /// The mapped name for the field. @@ -62,6 +63,7 @@ internal static class HlslDefinitionsSyntaxProcessor SemanticModelProvider semanticModel, ICollection discoveredTypes, IDictionary constantDefinitions, + IDictionary staticFieldDefinitions, ImmutableArrayBuilder diagnostics, CancellationToken token, [NotNullWhen(true)] out string? name, @@ -112,6 +114,7 @@ internal static class HlslDefinitionsSyntaxProcessor semanticModel, discoveredTypes, constantDefinitions, + staticFieldDefinitions, diagnostics, token); diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs index 320805ef..8f43912a 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs @@ -33,18 +33,21 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter /// The instance for the target syntax tree. /// The set of discovered custom types. /// The collection of discovered constant definitions. + /// The collection of discovered static field definitions. /// The collection of produced instances. /// The value for the current operation. protected HlslSourceRewriter( SemanticModelProvider semanticModel, ICollection discoveredTypes, IDictionary constantDefinitions, + IDictionary staticFieldDefinitions, ImmutableArrayBuilder diagnostics, CancellationToken token) { SemanticModel = semanticModel; DiscoveredTypes = discoveredTypes; ConstantDefinitions = constantDefinitions; + StaticFieldDefinitions = staticFieldDefinitions; Diagnostics = diagnostics; CancellationToken = token; } @@ -64,6 +67,11 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter /// protected IDictionary ConstantDefinitions { get; } + /// + /// Gets the collection of discovered static field definitions. + /// + protected IDictionary StaticFieldDefinitions { get; } + /// /// Gets the collection of produced instances. /// diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs index 26e677a6..da4705af 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs @@ -77,6 +77,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter /// The collection of discovered instance methods for custom struct types. /// The collection of discovered constructors for custom struct types. /// The collection of discovered constant definitions. + /// The collection of discovered static field definitions. /// The collection of produced instances. /// The value for the current operation. /// Whether or not the current instance is processing a shader entry point. @@ -88,10 +89,11 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter IDictionary instanceMethods, IDictionary constructors, IDictionary constantDefinitions, + IDictionary staticFieldDefinitions, ImmutableArrayBuilder diagnostics, CancellationToken token, bool isEntryPoint) - : base(semanticModel, discoveredTypes, constantDefinitions, diagnostics, token) + : base(semanticModel, discoveredTypes, constantDefinitions, staticFieldDefinitions, diagnostics, token) { this.shaderType = shaderType; this.staticMethods = staticMethods; @@ -111,6 +113,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter /// The collection of discovered instance methods for custom struct types. /// The collection of discovered constructors for custom struct types. /// The collection of discovered constant definitions. + /// The collection of discovered static field definitions. /// The collection of produced instances. /// The value for the current operation. private ShaderSourceRewriter( @@ -120,9 +123,10 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter IDictionary instanceMethods, IDictionary constructors, IDictionary constantDefinitions, + IDictionary staticFieldDefinitions, ImmutableArrayBuilder diagnostics, CancellationToken token) - : base(semanticModel, discoveredTypes, constantDefinitions, diagnostics, token) + : base(semanticModel, discoveredTypes, constantDefinitions, staticFieldDefinitions, diagnostics, token) { this.staticMethods = staticMethods; this.instanceMethods = instanceMethods; @@ -545,6 +549,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter this.instanceMethods, this.constructors, ConstantDefinitions, + StaticFieldDefinitions, Diagnostics, CancellationToken); @@ -598,6 +603,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter this.instanceMethods, this.constructors, ConstantDefinitions, + StaticFieldDefinitions, Diagnostics, CancellationToken); @@ -685,6 +691,7 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter this.instanceMethods, this.constructors, ConstantDefinitions, + StaticFieldDefinitions, Diagnostics, CancellationToken); diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs index 2d8fddcf..11b3cc55 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs @@ -23,15 +23,17 @@ namespace ComputeSharp.SourceGeneration.SyntaxRewriters; /// The instance for the target syntax tree. /// The set of discovered custom types. /// The collection of discovered constant definitions. +/// The collection of discovered static field definitions. /// The collection of produced instances. /// The value for the current operation. internal sealed partial class StaticFieldRewriter( SemanticModelProvider semanticModel, ICollection discoveredTypes, IDictionary constantDefinitions, + IDictionary staticFieldDefinitions, ImmutableArrayBuilder diagnostics, CancellationToken token) - : HlslSourceRewriter(semanticModel, discoveredTypes, constantDefinitions, diagnostics, token) + : HlslSourceRewriter(semanticModel, discoveredTypes, constantDefinitions, staticFieldDefinitions, diagnostics, token) { /// public ExpressionSyntax? Visit(VariableDeclaratorSyntax? node) diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs index 27f31746..c67dcfbe 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs @@ -59,6 +59,7 @@ partial class ComputeShaderDescriptorGenerator Dictionary instanceMethods = new(SymbolEqualityComparer.Default); Dictionary constructors = new(SymbolEqualityComparer.Default); Dictionary constantDefinitions = new(SymbolEqualityComparer.Default); + Dictionary staticFieldDefinitions = new(SymbolEqualityComparer.Default); // Setup the semantic model and basic properties INamedTypeSymbol? pixelShaderSymbol = structDeclarationSymbol.AllInterfaces.FirstOrDefault(static interfaceSymbol => interfaceSymbol is { IsGenericType: true, Name: "IComputeShader" }); @@ -93,6 +94,7 @@ partial class ComputeShaderDescriptorGenerator instanceMethods, constructors, constantDefinitions, + staticFieldDefinitions, isComputeShader, token); @@ -105,6 +107,7 @@ partial class ComputeShaderDescriptorGenerator structDeclarationSymbol, discoveredTypes, constantDefinitions, + staticFieldDefinitions, token); token.ThrowIfCancellationRequested(); @@ -251,6 +254,7 @@ partial class ComputeShaderDescriptorGenerator /// The type symbol for the shader type. /// The collection of currently discovered types. /// The collection of discovered constant definitions. + /// The collection of discovered static field definitions. /// The used to cancel the operation, if needed. /// A sequence of static constant fields in . private static ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> GetStaticFields( @@ -259,6 +263,7 @@ partial class ComputeShaderDescriptorGenerator INamedTypeSymbol structDeclarationSymbol, ICollection discoveredTypes, IDictionary constantDefinitions, + IDictionary staticFieldDefinitions, CancellationToken token) { using ImmutableArrayBuilder<(string, string, string?)> builder = new(); @@ -283,6 +288,7 @@ partial class ComputeShaderDescriptorGenerator semanticModel, discoveredTypes, constantDefinitions, + staticFieldDefinitions, diagnostics, token, out string? name, @@ -349,6 +355,7 @@ partial class ComputeShaderDescriptorGenerator /// The collection of discovered instance methods for custom struct types. /// The collection of discovered constructors for custom struct types. /// The collection of discovered constant definitions. + /// The collection of discovered static field definitions. /// Indicates whether or not represents a compute shader. /// The used to cancel the operation, if needed. /// A sequence of processed methods in , and the entry point. @@ -361,6 +368,7 @@ partial class ComputeShaderDescriptorGenerator IDictionary instanceMethods, IDictionary constructors, IDictionary constantDefinitions, + IDictionary staticFieldDefinitions, bool isComputeShader, CancellationToken token) { @@ -411,6 +419,7 @@ partial class ComputeShaderDescriptorGenerator instanceMethods, constructors, constantDefinitions, + staticFieldDefinitions, diagnostics, token, isShaderEntryPoint);