From 7b46a7d5278b37679e504991cbe55574354df4f0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 9 Aug 2022 16:03:22 +0200 Subject: [PATCH] For matching of known sampler methods --- .../SyntaxRewriters/ShaderSourceRewriter.cs | 27 +++++++++++-------- .../Extensions/ISymbolExtensions.cs | 15 ++--------- 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs index 68063d42..38d46a49 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs @@ -346,10 +346,10 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter if (SemanticModel.For(node).GetOperation(node) is IInvocationOperation { TargetMethod: IMethodSymbol method } operation) { - string metadataName = method.GetFullMetadataName(); - if (method.IsStatic) { + string metadataName = method.GetFullMetadataName(); + // If the invocation consists of invoking a static method that has a direct // mapping to HLSL, rewrite the expression in the current invocation node. // For instance: Math.Abs(expr) => abs(expr). @@ -396,17 +396,22 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter return updatedNode.WithExpression(ParseExpression(methodIdentifier)); } } - else if (HlslKnownMethods.TryGetMappedResourceSamplerAccessType(metadataName, out string? mapping)) + else { - // Get the syntax for the argument syntax transformation (adding the vector type constructor if needed) - ArgumentSyntax coordinateSyntax = mapping switch - { - not null => Argument(InvocationExpression(IdentifierName(mapping!), ArgumentList(updatedNode.ArgumentList.Arguments))), - null => updatedNode.ArgumentList.Arguments[0] - }; + string metadataName = method.GetFullMetadataName(includeParameters: true); - // Rewrite texture resource sampled accesses, if needed - return RewriteSampledTextureAccess(operation, updatedNode.Expression, coordinateSyntax); + if (HlslKnownMethods.TryGetMappedResourceSamplerAccessType(metadataName, out string? mapping)) + { + // Get the syntax for the argument syntax transformation (adding the vector type constructor if needed) + ArgumentSyntax coordinateSyntax = mapping switch + { + not null => Argument(InvocationExpression(IdentifierName(mapping!), ArgumentList(updatedNode.ArgumentList.Arguments))), + null => updatedNode.ArgumentList.Arguments[0] + }; + + // Rewrite texture resource sampled accesses, if needed + return RewriteSampledTextureAccess(operation, updatedNode.Expression, coordinateSyntax); + } } } diff --git a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs index 914adbd8..170b648d 100644 --- a/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGeneration/Extensions/ISymbolExtensions.cs @@ -19,17 +19,6 @@ internal static class ISymbolExtensions genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters, miscellaneousOptions: SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers); - /// - /// A custom instance with fully qualified style, without global:: and parameters. - /// - private static readonly SymbolDisplayFormat FullyQualifiedWithoutGlobalAndParametersFormat = new( - globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.Omitted, - typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces, - genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters, - miscellaneousOptions: SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers, - memberOptions: SymbolDisplayMemberOptions.IncludeContainingType, - parameterOptions: SymbolDisplayParameterOptions.None); - /// /// Gets the fully qualified name for a given symbol. /// @@ -89,10 +78,10 @@ internal static class ISymbolExtensions { var parameters = string.Join(", ", symbol.Parameters.Select(static p => ((INamedTypeSymbol)p.Type).GetFullMetadataName())); - return $"{symbol.ToDisplayString(FullyQualifiedWithoutGlobalAndParametersFormat)}({parameters})"; + return $"{symbol.ContainingType.GetFullMetadataName()}.{symbol.Name}({parameters})"; } - return symbol.ToDisplayString(FullyQualifiedWithoutGlobalAndParametersFormat); + return $"{symbol.ContainingType.GetFullMetadataName()}.{symbol.Name}"; } ///