diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
index 7e5787bc..05ec04c3 100644
--- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
@@ -179,7 +179,7 @@ namespace ComputeSharp.SourceGenerators
methodDeclarationSymbol.TypeParameters.Length == 0 &&
methodDeclarationSymbol.Parameters.Length == 0)
{
- processedMethod = new ExecuteMethodRewriter().Visit(processedMethod)!;
+ processedMethod = new ExecuteMethodRewriter(shaderSourceRewriter).Visit(processedMethod)!;
entryPoint = processedMethod.NormalizeWhitespace().ToFullString();
}
diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs
index bdfce306..ad67c0b3 100644
--- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs
+++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ExecuteMethodRewriter.cs
@@ -10,6 +10,20 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
internal sealed class ExecuteMethodRewriter : CSharpSyntaxRewriter
{
+ ///
+ /// The instance used to process the input tree.
+ ///
+ private readonly ShaderSourceRewriter shaderSourceRewriter;
+
+ ///
+ /// Creates a new instance with the specified parameters.
+ ///
+ /// The instance used to process the input tree.
+ public ExecuteMethodRewriter(ShaderSourceRewriter shaderSourceRewriter)
+ {
+ this.shaderSourceRewriter = shaderSourceRewriter;
+ }
+
///
public TNode? Visit(TNode? node)
where TNode : SyntaxNode
@@ -22,11 +36,24 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
{
var updatedNode = (ParameterListSyntax)base.VisitParameterList(node)!;
- return updatedNode.AddParameters(
- Parameter(Identifier($"uint3 {nameof(ThreadIds)} : SV_DispatchThreadID")),
- Parameter(Identifier($"uint3 {nameof(GroupIds)} : SV_GroupThreadID")),
- Parameter(Identifier($"uint __{nameof(GroupIds)}__get_Index : SV_GroupIndex")),
- Parameter(Identifier($"uint3 {nameof(WarpIds)} : SV_GroupID")));
+ updatedNode = updatedNode.AddParameters(Parameter(Identifier($"uint3 {nameof(ThreadIds)} : SV_DispatchThreadID")));
+
+ if (this.shaderSourceRewriter.IsGroupIdsUsed)
+ {
+ updatedNode = updatedNode.AddParameters(Parameter(Identifier($"uint3 {nameof(GroupIds)} : SV_GroupThreadID")));
+ }
+
+ if (this.shaderSourceRewriter.IsGroupIdsIndexUsed)
+ {
+ updatedNode = updatedNode.AddParameters(Parameter(Identifier($"uint __{nameof(GroupIds)}__get_Index : SV_GroupIndex")));
+ }
+
+ if (this.shaderSourceRewriter.IsWarpIdsUsed)
+ {
+ updatedNode = updatedNode.AddParameters(Parameter(Identifier($"uint3 {nameof(WarpIds)} : SV_GroupID")));
+ }
+
+ return updatedNode;
}
///
diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
index fe593c2a..289f14ac 100644
--- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
+++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs
@@ -106,6 +106,21 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
///
public IReadOnlyDictionary LocalFunctions => this.localFunctions;
+ ///
+ /// Gets whether or not the shader uses at least once (except ).
+ ///
+ public bool IsGroupIdsUsed { get; private set; }
+
+ ///
+ /// Gets whether or not the shader uses at least once.
+ ///
+ public bool IsWarpIdsUsed { get; set; }
+
///
public MethodDeclarationSyntax? Visit(MethodDeclarationSyntax? node)
{
@@ -307,11 +322,22 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
// to see if the member should be rewritten for HLSL compliance, and rewrite if needed.
if (HlslKnownMembers.TryGetMappedName(operation.Member.ToDisplayString(), out string? mapping))
{
+ // Mark which dispatch properties have been used, to optimize the declaration afterwards
+ if (operation.Member.IsStatic)
+ {
+ string typeName = operation.Member.ContainingType.GetFullMetadataName();
+
+ if (mapping == $"__{nameof(GroupIds)}__get_Index") IsGroupIdsIndexUsed = true;
+ else if (typeName == typeof(GroupIds).FullName) IsGroupIdsUsed = true;
+ else if (typeName == typeof(WarpIds).FullName) IsWarpIdsUsed = true;
+ }
+
// Static and instance members are handled differently, with static members being
// converted into a literal expression, and instance getting an updated identifier.
- // For instance, consider these two cases:
+ // For instance, consider these three cases:
// - Vector3.One (static) => float3(1.0f, 1.0f, 1.0f)
- // - ThredIds.X (instance) => [arg].x
+ // - ThreadIds.X (custom) => ThreadIds.x
+ // - uint3.X (instance) => [arg].x
return operation.Member.IsStatic switch
{
true => ParseExpression(mapping!),