diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
index eebe052b..5c2b85ae 100644
--- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
@@ -377,7 +377,11 @@ namespace ComputeSharp.SourceGenerators
// If the method is the shader entry point, do additional processing
if (isShaderEntryPoint)
{
- processedMethod = new ExecuteMethodRewriter(shaderSourceRewriter, isComputeShader).Visit(processedMethod)!;
+ processedMethod = isComputeShader switch
+ {
+ true => new ExecuteMethodRewriter.Compute(shaderSourceRewriter).Visit(processedMethod)!,
+ false => new ExecuteMethodRewriter.Pixel(shaderSourceRewriter).Visit(processedMethod)!
+ };
entryPoint = processedMethod.NormalizeWhitespace().ToFullString();
}
diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs
index d363b9ca..3624a87e 100644
--- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs
+++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs
@@ -8,29 +8,22 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
/// A custom type that does postprocessing fixups on the shader main method.
///
- internal sealed class ExecuteMethodRewriter : CSharpSyntaxRewriter
+ internal abstract class ExecuteMethodRewriter : CSharpSyntaxRewriter
{
///
- /// The instance used to process the input tree.
- ///
- private readonly ShaderSourceRewriter shaderSourceRewriter;
-
- ///
- /// Indicates whether or not the method belongs to a compute shader.
- ///
- private readonly bool isComputeShader;
-
- ///
- /// Creates a new instance with the specified parameters.
+ /// Creates a new instance with the specified parameters.
///
/// The instance used to process the input tree.
- /// Indicates whether or not the method belongs to a compute shader.
- public ExecuteMethodRewriter(ShaderSourceRewriter shaderSourceRewriter, bool isComputeShader)
+ public ExecuteMethodRewriter(ShaderSourceRewriter shaderSourceRewriter)
{
- this.shaderSourceRewriter = shaderSourceRewriter;
- this.isComputeShader = isComputeShader;
+ ShaderSourceRewriter = shaderSourceRewriter;
}
+ ///
+ /// Gets the instance used to process the input tree.
+ ///
+ protected ShaderSourceRewriter ShaderSourceRewriter { get; }
+
///
public TNode? Visit(TNode? node)
where TNode : SyntaxNode
@@ -45,17 +38,17 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
updatedNode = updatedNode.AddParameters(Parameter(Identifier($"uint3 {nameof(ThreadIds)} : SV_DispatchThreadID")));
- if (this.shaderSourceRewriter.IsGroupIdsUsed)
+ if (ShaderSourceRewriter.IsGroupIdsUsed)
{
updatedNode = updatedNode.AddParameters(Parameter(Identifier($"uint3 {nameof(GroupIds)} : SV_GroupThreadID")));
}
- if (this.shaderSourceRewriter.IsGroupIdsIndexUsed)
+ if (ShaderSourceRewriter.IsGroupIdsIndexUsed)
{
updatedNode = updatedNode.AddParameters(Parameter(Identifier($"uint __{nameof(GroupIds)}__get_Index : SV_GroupIndex")));
}
- if (this.shaderSourceRewriter.IsGridIdsUsed)
+ if (ShaderSourceRewriter.IsGridIdsUsed)
{
updatedNode = updatedNode.AddParameters(Parameter(Identifier($"uint3 {nameof(GridIds)} : SV_GroupID")));
}
@@ -63,54 +56,106 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
return updatedNode;
}
- ///
- public override SyntaxNode? VisitReturnStatement(ReturnStatementSyntax node)
+ ///
+ /// A custom type specialized for compute shaders.
+ ///
+ public sealed class Compute : ExecuteMethodRewriter
{
- var updatedNode = (ReturnStatementSyntax)base.VisitReturnStatement(node)!;
-
- if (this.isComputeShader)
+ ///
+ /// Creates a new instance with the specified parameters.
+ ///
+ /// The instance used to process the input tree.
+ public Compute(ShaderSourceRewriter shaderSourceRewriter)
+ : base(shaderSourceRewriter)
{
- return updatedNode;
}
- // __outputTexture[ThreadIds.xy] = ;
- return
- ExpressionStatement(
- AssignmentExpression(
- SyntaxKind.SimpleAssignmentExpression,
- ElementAccessExpression(IdentifierName("__outputTexture"))
- .AddArgumentListArguments(
- Argument(
+ ///
+ 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 (ThreadIds.x < __x &&
+ // ThreadIds.y < __y &&
+ // ThreadIds.z < __z)
+ // {
+ //
+ // }
+ BinaryExpressionSyntax rangeCheckExpression =
+ BinaryExpression(SyntaxKind.LogicalAndExpression,
+ BinaryExpression(SyntaxKind.LogicalAndExpression,
+ BinaryExpression(SyntaxKind.LessThanExpression,
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
- IdentifierName("ThreadIds"),
- IdentifierName("xy")))),
- updatedNode.Expression!));
+ IdentifierName(nameof(ThreadIds)),
+ IdentifierName("x")),
+ IdentifierName("__x")),
+ BinaryExpression(SyntaxKind.LessThanExpression,
+ MemberAccessExpression(
+ SyntaxKind.SimpleMemberAccessExpression,
+ IdentifierName(nameof(ThreadIds)),
+ IdentifierName("y")),
+ IdentifierName("__y"))),
+ BinaryExpression(SyntaxKind.LessThanExpression,
+ MemberAccessExpression(
+ SyntaxKind.SimpleMemberAccessExpression,
+ IdentifierName(nameof(ThreadIds)),
+ IdentifierName("z")),
+ IdentifierName("__z")));
+
+ return updatedNode.WithBody(Block(IfStatement(rangeCheckExpression, updatedNode.Body!)));
+ }
}
- ///
- public override SyntaxNode? VisitMethodDeclaration(MethodDeclarationSyntax node)
+ ///
+ /// A custom type specialized for compute shaders.
+ ///
+ public sealed class Pixel : ExecuteMethodRewriter
{
- var updatedNode = ((MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!).WithModifiers(TokenList());
-
- // Change the return type if the current shader is a pixel shader
- if (!this.isComputeShader)
+ ///
+ /// Creates a new instance with the specified parameters.
+ ///
+ /// The instance used to process the input tree.
+ public Pixel(ShaderSourceRewriter shaderSourceRewriter)
+ : base(shaderSourceRewriter)
{
- updatedNode = updatedNode.WithReturnType(PredefinedType(Token(SyntaxKind.VoidKeyword)));
}
- // 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 (ThreadIds.x < __x &&
- // ThreadIds.y < __y &&
- // ThreadIds.z < __z)
- // {
- //
- // }
- var rangeCheckExpression =
- BinaryExpression(SyntaxKind.LogicalAndExpression,
+ ///
+ public override SyntaxNode? VisitReturnStatement(ReturnStatementSyntax node)
+ {
+ var updatedNode = (ReturnStatementSyntax)base.VisitReturnStatement(node)!;
+
+ // __outputTexture[ThreadIds.xy] = ;
+ return
+ ExpressionStatement(
+ AssignmentExpression(
+ SyntaxKind.SimpleAssignmentExpression,
+ ElementAccessExpression(IdentifierName("__outputTexture"))
+ .AddArgumentListArguments(
+ Argument(
+ MemberAccessExpression(
+ SyntaxKind.SimpleMemberAccessExpression,
+ IdentifierName("ThreadIds"),
+ IdentifierName("xy")))),
+ updatedNode.Expression!));
+ }
+
+ ///
+ public override SyntaxNode? VisitMethodDeclaration(MethodDeclarationSyntax node)
+ {
+ var updatedNode = ((MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!).WithModifiers(TokenList());
+
+ // Change the return type to void
+ updatedNode = updatedNode.WithReturnType(PredefinedType(Token(SyntaxKind.VoidKeyword)));
+
+ // Same as above, but skipping the third check for the Z axis.
+ // This is because pixel shaders only ever run on 2D textures.
+ BinaryExpressionSyntax rangeCheckExpression =
BinaryExpression(SyntaxKind.LogicalAndExpression,
BinaryExpression(SyntaxKind.LessThanExpression,
MemberAccessExpression(
@@ -123,15 +168,10 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName(nameof(ThreadIds)),
IdentifierName("y")),
- IdentifierName("__y"))),
- BinaryExpression(SyntaxKind.LessThanExpression,
- MemberAccessExpression(
- SyntaxKind.SimpleMemberAccessExpression,
- IdentifierName(nameof(ThreadIds)),
- IdentifierName("z")),
- IdentifierName("__z")));
+ IdentifierName("__y")));
- return updatedNode.WithBody(Block(IfStatement(rangeCheckExpression, updatedNode.Body!)));
+ return updatedNode.WithBody(Block(IfStatement(rangeCheckExpression, updatedNode.Body!)));
+ }
}
}
}