diff --git a/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs
index a047f37f..dbfbbb86 100644
--- a/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs
+++ b/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs
@@ -126,6 +126,22 @@ namespace ComputeSharp.SourceGenerators.Extensions
return node;
}
+ ///
+ /// Returns a as a method definition.
+ ///
+ /// The input node.
+ /// A node like the one in input, but just as a definition.
+ [Pure]
+ public static MethodDeclarationSyntax AsDefinition(this MethodDeclarationSyntax node)
+ {
+ if (node.ExpressionBody is ArrowExpressionClauseSyntax)
+ {
+ return node.WithExpressionBody(null).WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
+ }
+
+ return node.WithBody(null).WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
+ }
+
///
/// Returns a with a block body.
///
@@ -155,6 +171,22 @@ namespace ComputeSharp.SourceGenerators.Extensions
return node;
}
+ ///
+ /// Returns a as a method definition.
+ ///
+ /// The input node.
+ /// A node like the one in input, but just as a definition.
+ [Pure]
+ public static LocalFunctionStatementSyntax AsDefinition(this LocalFunctionStatementSyntax node)
+ {
+ if (node.ExpressionBody is ArrowExpressionClauseSyntax)
+ {
+ return node.WithExpressionBody(null).WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
+ }
+
+ return node.WithBody(null).WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
+ }
+
///
/// Returns a instance with no accessibility modifiers.
///
diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
index 16b6c48f..597434d3 100644
--- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
@@ -77,9 +77,8 @@ namespace ComputeSharp.SourceGenerators
// Explore the syntax tree and extract the processed info
var processedMembers = GetProcessedFields(context, structDeclarationSymbol, discoveredTypes).ToArray();
var sharedBuffers = GetGroupSharedMembers(context, structDeclarationSymbol, discoveredTypes).ToArray();
- var (entryPoint, localFunctions) = GetProcessedMethods(context, structDeclaration, structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions);
+ var (entryPoint, processedMethods, forwardDeclarations) = GetProcessedMethods(context, structDeclaration, structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions);
var processedTypes = GetProcessedTypes(discoveredTypes).ToArray();
- var processedMethods = localFunctions.Concat(staticMethods.Values).Select(static method => method.NormalizeWhitespace().ToFullString()).ToArray();
var processedConstants = GetProcessedConstants(constantDefinitions);
var staticFields = GetStaticFields(context, semanticModel, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
@@ -92,6 +91,7 @@ namespace ComputeSharp.SourceGenerators
AttributeArgument(TypeOfExpression(IdentifierName(structDeclarationSymbol.ToDisplayString()))),
AttributeArgument(ArrayExpression(processedTypes)),
AttributeArgument(NestedArrayExpression(processedMembers)),
+ AttributeArgument(ArrayExpression(forwardDeclarations)),
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(entryPoint))),
AttributeArgument(ArrayExpression(processedMethods)),
AttributeArgument(NestedArrayExpression(processedConstants)),
@@ -305,7 +305,7 @@ namespace ComputeSharp.SourceGenerators
/// The collection of discovered constant definitions.
/// A sequence of processed methods in , and the entry point.
[Pure]
- private static (string EntryPoint, IEnumerable Methods) GetProcessedMethods(
+ private static (string EntryPoint, IEnumerable Methods, IEnumerable Declarations) GetProcessedMethods(
GeneratorExecutionContext context,
StructDeclarationSyntax structDeclaration,
INamedTypeSymbol structDeclarationSymbol,
@@ -321,7 +321,8 @@ namespace ComputeSharp.SourceGenerators
select (MethodDeclarationSyntax)syntaxNode).ToImmutableArray();
string? entryPoint = null;
- List methods = new();
+ List methods = new();
+ List declarations = new();
foreach (MethodDeclarationSyntax methodDeclaration in methodDeclarations)
{
@@ -348,7 +349,8 @@ namespace ComputeSharp.SourceGenerators
// Emit the extracted local functions first
foreach (var localFunction in shaderSourceRewriter.LocalFunctions)
{
- methods.Add(localFunction.Value);
+ methods.Add(localFunction.Value.NormalizeWhitespace().ToFullString());
+ declarations.Add(localFunction.Value.AsDefinition().NormalizeWhitespace().ToFullString());
}
// If the method is the shader entry point, do additional processing
@@ -358,10 +360,21 @@ namespace ComputeSharp.SourceGenerators
entryPoint = processedMethod.NormalizeWhitespace().ToFullString();
}
- else methods.Add(processedMethod);
+ else
+ {
+ methods.Add(processedMethod.NormalizeWhitespace().ToFullString());
+ declarations.Add(processedMethod.AsDefinition().NormalizeWhitespace().ToFullString());
+ }
}
- return (entryPoint!, methods);
+ // Process static methods as well
+ foreach (MethodDeclarationSyntax staticMethod in staticMethods.Values)
+ {
+ methods.Add(staticMethod.NormalizeWhitespace().ToFullString());
+ declarations.Add(staticMethod.AsDefinition().NormalizeWhitespace().ToFullString());
+ }
+
+ return (entryPoint!, methods, declarations);
}
///
diff --git a/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs
index 0c28acc0..14351ce6 100644
--- a/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs
@@ -102,9 +102,9 @@ namespace ComputeSharp.SourceGenerators
/// The collection of currently discovered types.
/// The set of discovered and processed static methods.
/// The collection of discovered constant definitions.
- /// A sequence of processed methods in .
+ /// A sequence of processed methods in (main method and local functions).
[Pure]
- private static (string InvokeMethod, IEnumerable Methods) GetProcessedMethods(
+ private static (string TargetMethod, IEnumerable LocalFunctions) GetProcessedMethods(
GeneratorExecutionContext context,
MethodDeclarationSyntax methodDeclaration,
SemanticModelProvider semanticModel,
@@ -115,22 +115,22 @@ namespace ComputeSharp.SourceGenerators
ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods, constantDefinitions, context);
// Rewrite the method syntax tree
- var processedMethod = shaderSourceRewriter
+ var targetMethod = shaderSourceRewriter
.Visit(methodDeclaration)!
.WithIdentifier(Identifier(ShaderMethodSourceAttribute.InvokeMethodIdentifier))
.WithoutTrivia()
.NormalizeWhitespace()
.ToFullString();
- List methods = new(shaderSourceRewriter.LocalFunctions.Count);
+ List localFunctions = new(shaderSourceRewriter.LocalFunctions.Count);
// Emit the extracted local functions
foreach (var localFunction in shaderSourceRewriter.LocalFunctions)
{
- methods.Add(localFunction.Value.NormalizeWhitespace().ToFullString());
+ localFunctions.Add(localFunction.Value.NormalizeWhitespace().ToFullString());
}
- return (processedMethod, methods);
+ return (targetMethod, localFunctions);
}
}
}
diff --git a/src/ComputeSharp/Core/Attributes/__Internals/IComputeShaderSourceAttribute.cs b/src/ComputeSharp/Core/Attributes/__Internals/IComputeShaderSourceAttribute.cs
index 27fa5ed8..22f05618 100644
--- a/src/ComputeSharp/Core/Attributes/__Internals/IComputeShaderSourceAttribute.cs
+++ b/src/ComputeSharp/Core/Attributes/__Internals/IComputeShaderSourceAttribute.cs
@@ -22,7 +22,8 @@ namespace ComputeSharp.__Internals
///
/// The type of the current shader.
/// The collection of custom types.
- /// The mapped collection of shader fields.
+ /// The mapped collection of shader fields.
+ /// The forward declarations for static methods.
/// The source code for the method.
/// The collection of processed methods.
/// The collection of discovered defines.
@@ -31,7 +32,8 @@ namespace ComputeSharp.__Internals
public IComputeShaderSourceAttribute(
Type shaderType,
string[] types,
- object[] args,
+ object[] fields,
+ string[] forwardDeclarations,
string executeMethod,
string[] methods,
object[] defines,
@@ -40,7 +42,8 @@ namespace ComputeSharp.__Internals
{
ShaderType = shaderType;
Types = types;
- Fields = args.Cast().ToDictionary(static arg => arg[0], static arg => (arg[1], arg[2]));
+ Fields = fields.Cast().ToDictionary(static arg => arg[0], static arg => (arg[1], arg[2]));
+ ForwardDeclarations = forwardDeclarations;
ExecuteMethod = executeMethod;
Methods = methods;
Defines = defines.Cast().ToDictionary(static c => c[0], static c => c[1]);
@@ -63,6 +66,11 @@ namespace ComputeSharp.__Internals
///
internal IReadOnlyDictionary Fields { get; }
+ ///
+ /// The forward declarations for static methods.
+ ///
+ internal IReadOnlyCollection ForwardDeclarations { get; }
+
///
/// Gets the source code for the method.
///
diff --git a/src/ComputeSharp/Shaders/Renderer/HlslShaderRenderer.cs b/src/ComputeSharp/Shaders/Renderer/HlslShaderRenderer.cs
index cc2c7c2b..89fead55 100644
--- a/src/ComputeSharp/Shaders/Renderer/HlslShaderRenderer.cs
+++ b/src/ComputeSharp/Shaders/Renderer/HlslShaderRenderer.cs
@@ -155,6 +155,13 @@ namespace ComputeSharp.Shaders.Renderer
builder.AppendLine("];");
}
+ // Forward declarations
+ foreach (var declaration in info.ForwardDeclarations)
+ {
+ builder.AppendLine();
+ builder.AppendLine(declaration);
+ }
+
// Captured methods
foreach (var function in info.MethodsInfo)
{
diff --git a/src/ComputeSharp/Shaders/Translation/IShaderLoader.cs b/src/ComputeSharp/Shaders/Translation/IShaderLoader.cs
index 491e803e..23438eff 100644
--- a/src/ComputeSharp/Shaders/Translation/IShaderLoader.cs
+++ b/src/ComputeSharp/Shaders/Translation/IShaderLoader.cs
@@ -23,6 +23,11 @@ namespace ComputeSharp.Shaders.Translation
///
IReadOnlyList FieldsInfo { get; }
+ ///
+ /// Gets the forward declarations for static methods.
+ ///
+ IReadOnlyCollection ForwardDeclarations { get; }
+
///
/// Gets the collection of methods to be used in the shader.
///
diff --git a/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.cs b/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.cs
index d0f043c8..745f53b0 100644
--- a/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.cs
+++ b/src/ComputeSharp/Shaders/Translation/ShaderLoader{T}.cs
@@ -104,6 +104,9 @@ namespace ComputeSharp.Shaders.Translation
///
public IReadOnlyList FieldsInfo => this.fieldsInfo;
+ ///
+ public IReadOnlyCollection ForwardDeclarations { get; private set; }
+
///
public IReadOnlyCollection MethodsInfo => this.methodsInfo;
@@ -145,6 +148,7 @@ namespace ComputeSharp.Shaders.Translation
///
private void LoadMethodMetadata()
{
+ ForwardDeclarations = Attribute.ForwardDeclarations;
EntryPoint = Attribute.ExecuteMethod;
StaticFields = Attribute.StaticFields;
SharedBuffers = Attribute.SharedBuffers;
diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs
index a51ce292..0bdfd0b4 100644
--- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs
+++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs
@@ -120,5 +120,38 @@ namespace ComputeSharp.Tests
buffer[ThreadIds.X] = ExternalStructType.Sum(type);
}
}
+
+ [TestMethod]
+ public void OutOfOrderMethods_Ok()
+ {
+ ReflectionServices.GetShaderInfo(out var info);
+ }
+
+ [AutoConstructor]
+ public readonly partial struct OutOfOrderMethodsShader : IComputeShader
+ {
+ public readonly ReadWriteBuffer buffer;
+
+ static int A() => B();
+
+ static int B() => 1 + C();
+
+ static int C() => 1;
+
+ int D() => A() + E() + F();
+
+ int E() => 1;
+
+ static int F() => 1;
+
+ ///
+ public void Execute()
+ {
+ float value = buffer[ThreadIds.X];
+ ExternalStructType type = ExternalStructType.New((int)value, Hlsl.Abs(value));
+
+ buffer[ThreadIds.X] = ExternalStructType.Sum(type);
+ }
+ }
}
}