From d176042528ca32a97d502d8f9258cd5058aed816 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 25 Nov 2020 01:59:28 +0100 Subject: [PATCH] Code refactoring --- .../ComputeSharp.SourceGenerators.csproj | 2 +- .../IComputeShaderSourceAttributeGenerator.cs | 195 +----------------- .../SyntaxRewriters/ExecuteMethodRewriter.cs | 84 ++++++++ .../SyntaxRewriters/ShaderSourceRewriter.cs | 125 +++++++++++ 4 files changed, 211 insertions(+), 195 deletions(-) create mode 100644 src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs create mode 100644 src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs diff --git a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj index 82d9546c..1c9c7e2d 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj +++ b/src/ComputeSharp.SourceGenerators/ComputeSharp.SourceGenerators.csproj @@ -1,4 +1,4 @@ - + netstandard2.0 diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs index 36ed489f..b6c14153 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Text; using ComputeSharp.SourceGenerators.Extensions; +using ComputeSharp.SourceGenerators.SyntaxRewriters; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -87,200 +88,6 @@ namespace ComputeSharp.SourceGenerators } } } - - /// - /// A custom type that processes C# methods to convert to HLSL compliant code. - /// - private sealed class ShaderSourceRewriter : CSharpSyntaxRewriter - { - /// - /// The instance with semantic info on the target syntax tree. - /// - private readonly SemanticModel semanticModel; - - /// - /// Creates a new instance with the specified parameters. - /// - /// The instance for the target syntax tree. - public ShaderSourceRewriter(SemanticModel semanticModel) - { - this.semanticModel = semanticModel; - } - - /// - public TNode? Visit(TNode? node) - where TNode : SyntaxNode - { - return (TNode?)base.Visit(node); - } - - /// - public override SyntaxNode VisitParameter(ParameterSyntax node) - { - var updatedNode = (ParameterSyntax)base.VisitParameter(node)!; - - return updatedNode - .WithAttributeLists(default) - .ReplaceType(updatedNode.Type!, node.Type!, this.semanticModel); - } - - /// - public override SyntaxNode VisitCastExpression(CastExpressionSyntax node) - { - var updatedNode = (CastExpressionSyntax)base.VisitCastExpression(node)!; - - return updatedNode.ReplaceType(updatedNode.Type, node.Type, this.semanticModel); - } - - /// - public override SyntaxNode VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node) - { - var updatedNode = ((LocalDeclarationStatementSyntax)base.VisitLocalDeclarationStatement(node)!); - - return updatedNode.ReplaceType(updatedNode.Declaration.Type, node.Declaration.Type, this.semanticModel); - } - - /// - public override SyntaxNode VisitObjectCreationExpression(ObjectCreationExpressionSyntax node) - { - var updatedNode = (ObjectCreationExpressionSyntax)base.VisitObjectCreationExpression(node)!; - - updatedNode = updatedNode.ReplaceType(updatedNode.Type, node.Type, this.semanticModel); - - // New objects use the default HLSL cast syntax, eg. (float4)0 - if (updatedNode.ArgumentList!.Arguments.Count == 0) - { - return CastExpression(updatedNode.Type, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0))); - } - - return InvocationExpression(updatedNode.Type, updatedNode.ArgumentList); - } - - /// - public override SyntaxNode VisitDefaultExpression(DefaultExpressionSyntax node) - { - var updatedNode = (DefaultExpressionSyntax)base.VisitDefaultExpression(node)!; - - updatedNode = updatedNode.ReplaceType(updatedNode.Type, node.Type, this.semanticModel); - - // A default expression becomes (T)0 in HLSL - return CastExpression(updatedNode.Type, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0))); - } - - /// - public override SyntaxNode? VisitLiteralExpression(LiteralExpressionSyntax node) - { - node = ((LiteralExpressionSyntax)base.VisitLiteralExpression(node)!); - - if (node.IsKind(SyntaxKind.DefaultLiteralExpression)) - { - // Same HLSL-style expression in the form (T)0 - return CastExpression(node.ReplaceType(this.semanticModel), LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0))); - } - - return node; - } - - /// - public override SyntaxNode? VisitMethodDeclaration(MethodDeclarationSyntax node) - { - return ((MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!).WithBlockBody(); - } - - /// - public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyntax node) - { - var updatedNode = (MemberAccessExpressionSyntax)base.VisitMemberAccessExpression(node)!; - - if (node.IsKind(SyntaxKind.SimpleMemberAccessExpression) && - this.semanticModel.GetSymbolInfo(node).Symbol is ISymbol nodeSymbol && - nodeSymbol.ContainingType.ToDisplayString() == typeof(ThreadIds).FullName) - { - // When accessing ThreadIds members, they are in lowercase in HLSL. This is - // because the type is mapped to uint3, which has the xyz members. - return updatedNode.WithName(IdentifierName(updatedNode.Name.ToString().ToLower())); - } - - return updatedNode; - } - } - - /// - /// A custom type that does postprocessing fixups on the shader main method. - /// - private sealed class ExecuteMethodRewriter : CSharpSyntaxRewriter - { - /// - /// The identifier name for the input parameter. - /// - private readonly string threadIdsIdentifier; - - /// - /// Creates a new instance with the specified parameters. - /// - /// The identifier name for the input parameter. - public ExecuteMethodRewriter(string threadIdsIdentifier) - { - this.threadIdsIdentifier = threadIdsIdentifier; - } - - /// - public TNode? Visit(TNode? node) - where TNode : SyntaxNode - { - return (TNode?)base.Visit(node); - } - - /// - public override SyntaxNode VisitParameter(ParameterSyntax node) - { - var updatedNode = (ParameterSyntax)base.VisitParameter(node)!; - - return updatedNode.WithIdentifier(Identifier($"{updatedNode.Identifier.Text} : SV_DispatchThreadId")); - } - - /// - public override SyntaxNode? VisitMethodDeclaration(MethodDeclarationSyntax node) - { - var updatedNode = ((MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!).WithModifiers(TokenList()); - - // When we're rewriting the main compute shader method, we need to insert a range - // check to ensure that invocation outside of the requested range are discarded. - // The following snippet creates this prologue before the user provided body: - // - // if (ids.X < __x && - // ids.Y < __y && - // ids.Z < __z) - // { - // - // } - var rangeCheckExpression = - BinaryExpression(SyntaxKind.LogicalAndExpression, - BinaryExpression(SyntaxKind.LogicalAndExpression, - BinaryExpression(SyntaxKind.LessThanExpression, - MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - IdentifierName(this.threadIdsIdentifier), - IdentifierName("x")), - IdentifierName("__x")), - BinaryExpression(SyntaxKind.LessThanExpression, - MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - IdentifierName(this.threadIdsIdentifier), - IdentifierName("y")), - IdentifierName("__y"))), - BinaryExpression(SyntaxKind.LessThanExpression, - MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - IdentifierName(this.threadIdsIdentifier), - IdentifierName("z")), - IdentifierName("__z"))); - - return updatedNode - .WithIdentifier(Identifier("CSMain")) - .WithBody(Block(IfStatement(rangeCheckExpression, updatedNode.Body!))); - } - } } } diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs new file mode 100644 index 00000000..07e0d06b --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs @@ -0,0 +1,84 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace ComputeSharp.SourceGenerators.SyntaxRewriters +{ + /// + /// A custom type that does postprocessing fixups on the shader main method. + /// + internal sealed class ExecuteMethodRewriter : CSharpSyntaxRewriter + { + /// + /// The identifier name for the input parameter. + /// + private readonly string threadIdsIdentifier; + + /// + /// Creates a new instance with the specified parameters. + /// + /// The identifier name for the input parameter. + public ExecuteMethodRewriter(string threadIdsIdentifier) + { + this.threadIdsIdentifier = threadIdsIdentifier; + } + + /// + public TNode? Visit(TNode? node) + where TNode : SyntaxNode + { + return (TNode?)base.Visit(node); + } + + /// + public override SyntaxNode VisitParameter(ParameterSyntax node) + { + var updatedNode = (ParameterSyntax)base.VisitParameter(node)!; + + return updatedNode.WithIdentifier(Identifier($"{updatedNode.Identifier.Text} : SV_DispatchThreadId")); + } + + /// + public override SyntaxNode? VisitMethodDeclaration(MethodDeclarationSyntax node) + { + var updatedNode = ((MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!).WithModifiers(TokenList()); + + // When we're rewriting the main compute shader method, we need to insert a range + // check to ensure that invocation outside of the requested range are discarded. + // The following snippet creates this prologue before the user provided body: + // + // if (ids.X < __x && + // ids.Y < __y && + // ids.Z < __z) + // { + // + // } + var rangeCheckExpression = + BinaryExpression(SyntaxKind.LogicalAndExpression, + BinaryExpression(SyntaxKind.LogicalAndExpression, + BinaryExpression(SyntaxKind.LessThanExpression, + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName(this.threadIdsIdentifier), + IdentifierName("x")), + IdentifierName("__x")), + BinaryExpression(SyntaxKind.LessThanExpression, + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName(this.threadIdsIdentifier), + IdentifierName("y")), + IdentifierName("__y"))), + BinaryExpression(SyntaxKind.LessThanExpression, + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName(this.threadIdsIdentifier), + IdentifierName("z")), + IdentifierName("__z"))); + + return updatedNode + .WithIdentifier(Identifier("CSMain")) + .WithBody(Block(IfStatement(rangeCheckExpression, updatedNode.Body!))); + } + } +} diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs new file mode 100644 index 00000000..69a5a1a2 --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -0,0 +1,125 @@ +using ComputeSharp.SourceGenerators.Extensions; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; + +namespace ComputeSharp.SourceGenerators.SyntaxRewriters +{ + /// + /// A custom type that processes C# methods to convert to HLSL compliant code. + /// + internal sealed class ShaderSourceRewriter : CSharpSyntaxRewriter + { + /// + /// The instance with semantic info on the target syntax tree. + /// + private readonly SemanticModel semanticModel; + + /// + /// Creates a new instance with the specified parameters. + /// + /// The instance for the target syntax tree. + public ShaderSourceRewriter(SemanticModel semanticModel) + { + this.semanticModel = semanticModel; + } + + /// + public TNode? Visit(TNode? node) + where TNode : SyntaxNode + { + return (TNode?)base.Visit(node); + } + + /// + public override SyntaxNode VisitParameter(ParameterSyntax node) + { + var updatedNode = (ParameterSyntax)base.VisitParameter(node)!; + + return updatedNode + .WithAttributeLists(default) + .ReplaceType(updatedNode.Type!, node.Type!, this.semanticModel); + } + + /// + public override SyntaxNode VisitCastExpression(CastExpressionSyntax node) + { + var updatedNode = (CastExpressionSyntax)base.VisitCastExpression(node)!; + + return updatedNode.ReplaceType(updatedNode.Type, node.Type, this.semanticModel); + } + + /// + public override SyntaxNode VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node) + { + var updatedNode = ((LocalDeclarationStatementSyntax)base.VisitLocalDeclarationStatement(node)!); + + return updatedNode.ReplaceType(updatedNode.Declaration.Type, node.Declaration.Type, this.semanticModel); + } + + /// + public override SyntaxNode VisitObjectCreationExpression(ObjectCreationExpressionSyntax node) + { + var updatedNode = (ObjectCreationExpressionSyntax)base.VisitObjectCreationExpression(node)!; + + updatedNode = updatedNode.ReplaceType(updatedNode.Type, node.Type, this.semanticModel); + + // New objects use the default HLSL cast syntax, eg. (float4)0 + if (updatedNode.ArgumentList!.Arguments.Count == 0) + { + return CastExpression(updatedNode.Type, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0))); + } + + return InvocationExpression(updatedNode.Type, updatedNode.ArgumentList); + } + + /// + public override SyntaxNode VisitDefaultExpression(DefaultExpressionSyntax node) + { + var updatedNode = (DefaultExpressionSyntax)base.VisitDefaultExpression(node)!; + + updatedNode = updatedNode.ReplaceType(updatedNode.Type, node.Type, this.semanticModel); + + // A default expression becomes (T)0 in HLSL + return CastExpression(updatedNode.Type, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0))); + } + + /// + public override SyntaxNode? VisitLiteralExpression(LiteralExpressionSyntax node) + { + node = ((LiteralExpressionSyntax)base.VisitLiteralExpression(node)!); + + if (node.IsKind(SyntaxKind.DefaultLiteralExpression)) + { + // Same HLSL-style expression in the form (T)0 + return CastExpression(node.ReplaceType(this.semanticModel), LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(0))); + } + + return node; + } + + /// + public override SyntaxNode? VisitMethodDeclaration(MethodDeclarationSyntax node) + { + return ((MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!).WithBlockBody(); + } + + /// + public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyntax node) + { + var updatedNode = (MemberAccessExpressionSyntax)base.VisitMemberAccessExpression(node)!; + + if (node.IsKind(SyntaxKind.SimpleMemberAccessExpression) && + this.semanticModel.GetSymbolInfo(node).Symbol is ISymbol nodeSymbol && + nodeSymbol.ContainingType.ToDisplayString() == typeof(ThreadIds).FullName) + { + // When accessing ThreadIds members, they are in lowercase in HLSL. This is + // because the type is mapped to uint3, which has the xyz members. + return updatedNode.WithName(IdentifierName(updatedNode.Name.ToString().ToLower())); + } + + return updatedNode; + } + } +}