From ae23529485242b3cf4f1207b315b46d464f13bec Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 9 Aug 2022 15:33:51 +0200 Subject: [PATCH] Remove some HLSL rewriting duplicated code --- .../SyntaxRewriters/ShaderSourceRewriter.cs | 13 +++---------- .../SyntaxRewriters/ShaderSourceRewriter.cs | 15 +++++++++++---- .../SyntaxRewriters/ShaderSourceRewriter.cs | 13 +++---------- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 567c7489..f472a4c2 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -15,15 +15,8 @@ partial class ShaderSourceRewriter public bool NeedsD2DRequiresScenePositionAttribute { get; private set; } /// - private partial SyntaxNode RewriteSampledTextureAccess(IInvocationOperation operation, InvocationExpressionSyntax node, string? mappedType) + private partial SyntaxNode RewriteSampledTextureAccess(IInvocationOperation operation, ExpressionSyntax expression, ArgumentSyntax arguments) { - // Get the syntax for the argument syntax transformation (to pack arguments if needed, just like in the DX12 rewriter) - ArgumentSyntax coordinateSyntax = mappedType switch - { - not null => Argument(InvocationExpression(IdentifierName(mappedType!), ArgumentList(node.ArgumentList.Arguments))), - null => node.ArgumentList.Arguments[0] - }; - string fieldName = (operation.Instance as IFieldReferenceOperation)?.Member.Name ?? ""; _ = HlslKnownKeywords.TryGetMappedName(fieldName, out string? mapped); @@ -31,8 +24,8 @@ partial class ShaderSourceRewriter // Transform an indexer syntax into a sampling call with the implicit static linear sampler. // For instance: texture.Sample(uv) will be rewritten as texture.Sample(__sampler__texture, uv). return - InvocationExpression(node.Expression) - .AddArgumentListArguments(Argument(IdentifierName($"__sampler__{mapped ?? fieldName}")), coordinateSyntax); + InvocationExpression(expression) + .AddArgumentListArguments(Argument(IdentifierName($"__sampler__{mapped ?? fieldName}")), arguments); } /// diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs index d4bfbc6d..68063d42 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs @@ -398,8 +398,15 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter } else 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, mapping); + return RewriteSampledTextureAccess(operation, updatedNode.Expression, coordinateSyntax); } } @@ -435,10 +442,10 @@ internal sealed partial class ShaderSourceRewriter : HlslSourceRewriter /// Rewrites a sampled texture access. /// /// The instance for the sampled texture access. - /// The input instance for the node to rewrite. - /// The mapped type name to use for the sampled access, if any. + /// The input instance for the node to rewrite. + /// The input with the updated arguments for the expression to rewrite. /// A representing the rewritten sampled texture access. - private partial SyntaxNode RewriteSampledTextureAccess(IInvocationOperation operation, InvocationExpressionSyntax node, string? mappedType); + private partial SyntaxNode RewriteSampledTextureAccess(IInvocationOperation operation, ExpressionSyntax expression, ArgumentSyntax arguments); /// /// Tracks a property access to a known HLSL property. diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 9f48c11b..43ab1642 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -33,24 +33,17 @@ partial class ShaderSourceRewriter public bool IsSamplerUsed { get; private set; } /// - private partial SyntaxNode RewriteSampledTextureAccess(IInvocationOperation operation, InvocationExpressionSyntax node, string? mappedType) + private partial SyntaxNode RewriteSampledTextureAccess(IInvocationOperation operation, ExpressionSyntax expression, ArgumentSyntax arguments) { IsSamplerUsed = true; - // Get the syntax for the argument syntax transformation (adding the vector type constructor if needed) - ArgumentSyntax coordinateSyntax = mappedType switch - { - not null => Argument(InvocationExpression(IdentifierName(mappedType!), ArgumentList(node.ArgumentList.Arguments))), - null => node.ArgumentList.Arguments[0] - }; - // Transform a method invocation syntax into a sampling call with the implicit static linear sampler. // For instance: texture.Sample(uv) will be rewritten as texture.SampleLevel(__sampler, uv, 0). return - InvocationExpression(((MemberAccessExpressionSyntax)node.Expression).WithName(IdentifierName("SampleLevel"))) + InvocationExpression(((MemberAccessExpressionSyntax)expression).WithName(IdentifierName("SampleLevel"))) .AddArgumentListArguments( Argument(IdentifierName("__sampler")), - coordinateSyntax, + arguments, Argument(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0)))); }