From 1372d14a90ae3cb4eaf7f348b34a6adfe4232963 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 5 Dec 2023 22:41:25 +0100 Subject: [PATCH] Enable tracking external static fields --- ...xelShaderDescriptorGenerator.HlslSource.cs | 6 +++ .../SyntaxRewriters/ShaderSourceRewriter.cs | 39 +++++++++++++++++++ ...uteShaderDescriptorGenerator.HlslSource.cs | 6 +++ 3 files changed, 51 insertions(+) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs index 2e00cabd..150cc289 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs @@ -286,6 +286,12 @@ partial class D2DPixelShaderDescriptorGenerator } } + // Also gather the external static fields (same as in the DX12 generator) + foreach ((string, string, string?) externalField in staticFieldDefinitions.Values) + { + builder.Add(externalField); + } + return builder.ToImmutable(); } diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs index f771423e..b36a9aba 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs @@ -7,6 +7,7 @@ using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Mappings; using ComputeSharp.SourceGeneration.Models; +using ComputeSharp.SourceGeneration.SyntaxProcessors; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -366,6 +367,44 @@ internal sealed partial class ShaderSourceRewriter( { return updatedNode.Name; } + + // Handle static fields, but only if they're not in the shader type itself. If that is the case, they will already + // be discovered separately, when crawling all members of the shader type. Here we only gather external fields. + if (fieldOperation.Field.IsStatic && + !SymbolEqualityComparer.Default.Equals(fieldOperation.Field.ContainingType, this.shaderType)) + { + if (!StaticFieldDefinitions.TryGetValue(fieldOperation.Field, out (string Name, string, string?) info)) + { + // Execute the same logic as for shader static fields, to process them and extract the relevant info. + // In this case, we use the fully qualified name of the field as identifier, not just the field name. + if (HlslDefinitionsSyntaxProcessor.TryGetStaticField( + this.shaderType, + fieldOperation.Field, + SemanticModel, + DiscoveredTypes, + ConstantDefinitions, + StaticFieldDefinitions, + Diagnostics, + CancellationToken, + out _, + out string? typeDeclaration, + out string? assignmentExpression, + out _)) + { + info.Name = fieldOperation.Field.GetFullyQualifiedMetadataName().ToHlslIdentifierName(); + + StaticFieldDefinitions.Add(fieldOperation.Field, (info.Name, typeDeclaration, assignmentExpression)); + } + else + { + // We failed to process the field for whatever reason. Just stop rewriting it and return + // the current updated node. We'll have some diagnostic for this case emitted previously. + return updatedNode; + } + } + + return IdentifierName(info.Name); + } } // If the current member access is a field or property access, check the lookup table diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs index c67dcfbe..6c60560f 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs @@ -300,6 +300,12 @@ partial class ComputeShaderDescriptorGenerator } } + // Also gather the external static fields + foreach ((string, string, string?) externalField in staticFieldDefinitions.Values) + { + builder.Add(externalField); + } + return builder.ToImmutable(); }