From 4f60d704113a543eff000e639baa81ab14696629 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 5 Dec 2023 13:06:22 +0100 Subject: [PATCH] Use shared static field rewriter in DX12/D2D generators --- ...xelShaderDescriptorGenerator.HlslSource.cs | 50 +++++-------------- ...uteShaderDescriptorGenerator.HlslSource.cs | 48 +++++------------- 2 files changed, 26 insertions(+), 72 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs index d070d38e..abef3b7d 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs @@ -256,52 +256,28 @@ partial class D2DPixelShaderDescriptorGenerator foreach (ISymbol memberSymbol in structDeclarationSymbol.GetMembers()) { - // Find all declared static fields in the type - if (memberSymbol is not IFieldSymbol { IsImplicitlyDeclared: false, IsStatic: true, IsConst: false, } fieldSymbol) + if (memberSymbol is not IFieldSymbol fieldSymbol) { continue; } - if (!fieldSymbol.TryGetSyntaxNode(token, out VariableDeclaratorSyntax? variableDeclarator)) - { - continue; - } - - // Constant properties must be of a primitive, vector or matrix type - if (fieldSymbol.Type is not INamedTypeSymbol typeSymbol || - !HlslKnownTypes.IsKnownHlslType(typeSymbol.GetFullyQualifiedMetadataName())) - { - diagnostics.Add(InvalidShaderStaticFieldType, variableDeclarator, structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type); - - continue; - } - - _ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping); - - string typeDeclaration = fieldSymbol.IsReadOnly switch - { - true => $"static const {HlslKnownTypes.GetMappedName(typeSymbol)}", - false => $"static {HlslKnownTypes.GetMappedName(typeSymbol)}" - }; - - token.ThrowIfCancellationRequested(); - - StaticFieldRewriter staticFieldRewriter = new( + if (HlslDefinitionsSyntaxProcessor.TryGetStaticField( + structDeclarationSymbol, + fieldSymbol, semanticModel, discoveredTypes, constantDefinitions, diagnostics, - token); + token, + out string? name, + out string? typeDeclaration, + out string? assignmentExpression, + out StaticFieldRewriter? staticFieldRewriter)) + { + needsD2D1RequiresScenePosition |= staticFieldRewriter.NeedsD2DRequiresScenePositionAttribute; - ExpressionSyntax? processedDeclaration = staticFieldRewriter.Visit(variableDeclarator); - - token.ThrowIfCancellationRequested(); - - string? assignment = processedDeclaration?.NormalizeWhitespace(eol: "\n").ToFullString(); - - needsD2D1RequiresScenePosition |= staticFieldRewriter.NeedsD2DRequiresScenePositionAttribute; - - builder.Add((mapping ?? fieldSymbol.Name, typeDeclaration, assignment)); + builder.Add((name, typeDeclaration, assignmentExpression)); + } } return builder.ToImmutable(); diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs index c2e3c8d9..27f31746 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs @@ -265,55 +265,33 @@ partial class ComputeShaderDescriptorGenerator foreach (ISymbol memberSymbol in structDeclarationSymbol.GetMembers()) { - // Find all declared static fields in the type + // Pre-filter fields that cannot be valid, to reduce the calls to check the [GroupShared] attribute if (memberSymbol is not IFieldSymbol { IsImplicitlyDeclared: false, IsStatic: true, IsConst: false, } fieldSymbol) { continue; } - if (!fieldSymbol.TryGetSyntaxNode(token, out VariableDeclaratorSyntax? variableDeclarator)) - { - continue; - } - + // Ignore [GroupShared] fields, they'll be gathered in a separate rewriting step if (fieldSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.GroupSharedAttribute", out _)) { continue; } - // Constant properties must be of a primitive, vector or matrix type - if (fieldSymbol.Type is not INamedTypeSymbol typeSymbol || - !HlslKnownTypes.IsKnownHlslType(typeSymbol.GetFullyQualifiedMetadataName())) - { - diagnostics.Add(InvalidShaderStaticFieldType, variableDeclarator, structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type); - - continue; - } - - _ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping); - - string typeDeclaration = fieldSymbol.IsReadOnly switch - { - true => $"static const {HlslKnownTypes.GetMappedName(typeSymbol)}", - false => $"static {HlslKnownTypes.GetMappedName(typeSymbol)}" - }; - - token.ThrowIfCancellationRequested(); - - StaticFieldRewriter staticFieldRewriter = new( + if (HlslDefinitionsSyntaxProcessor.TryGetStaticField( + structDeclarationSymbol, + fieldSymbol, semanticModel, discoveredTypes, constantDefinitions, diagnostics, - token); - - ExpressionSyntax? processedDeclaration = staticFieldRewriter.Visit(variableDeclarator); - - token.ThrowIfCancellationRequested(); - - string? assignment = processedDeclaration?.NormalizeWhitespace(eol: "\n").ToFullString(); - - builder.Add((mapping ?? fieldSymbol.Name, typeDeclaration, assignment)); + token, + out string? name, + out string? typeDeclaration, + out string? assignmentExpression, + out _)) + { + builder.Add((name, typeDeclaration, assignmentExpression)); + } } return builder.ToImmutable();