From f598cb3ace803a8b23857f9bee19569ce2fd32a2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 24 Nov 2020 16:27:15 +0100 Subject: [PATCH] Added syntax rewriter for main CS method --- .../IComputeShaderSourceAttributeGenerator.cs | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs index 30b7527d..e6971536 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceAttributeGenerator.cs @@ -50,6 +50,17 @@ namespace ComputeSharp.SourceGenerators // Rewrite the method syntax tree var processedMethod = new ShaderSourceRewriter(semanticModel).Visit(methodDeclaration)!.WithoutTrivia(); + if (methodDeclarationSymbol.Name == "Execute" && + methodDeclarationSymbol.ReturnsVoid && + methodDeclarationSymbol.TypeParameters.Length == 0 && + methodDeclarationSymbol.Parameters.Length == 1 && + methodDeclarationSymbol.Parameters[0].Type.ToDisplayString() == "ComputeSharp.ThreadIds") + { + var parameterName = methodDeclarationSymbol.Parameters[0].Name; + + processedMethod = new ExecuteMethodRewriter(parameterName).Visit(processedMethod)!; + } + // Produce the final method source var processedMethodSource = processedMethod.NormalizeWhitespace().ToFullString(); @@ -173,6 +184,83 @@ namespace ComputeSharp.SourceGenerators return ((MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!).WithBlockBody(); } } + + /// + /// 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!))); + } + } } }