diff --git a/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs index dbec13e3..526074da 100644 --- a/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs +++ b/src/ComputeSharp.Shaders/Extensions/SyntaxNodeExtensions.cs @@ -168,6 +168,33 @@ namespace ComputeSharp.Shaders.Extensions return node; } + /// + /// Checks a instance and replaces it to be HLSL compatible, if needed + /// + /// The input to check and modify if needed + /// The info on parsed static methods, if any + /// A instance that is compatible with HLSL + [Pure] + public static InvocationExpressionSyntax ReplaceInvocation(this InvocationExpressionSyntax node, Type declaringType, out (string Name, MethodInfo MethodInfo)? method) + { + // Set the method to null + method = null; + + string cSharpName = node.Expression.ToString(); + while ((declaringType = declaringType.DeclaringType) != null) + { + MethodInfo methodInfo = declaringType.GetMethod(cSharpName, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); + if (methodInfo == null) return node; + + string name = $"{methodInfo.DeclaringType.Name}_{methodInfo.Name}"; + method = (name, methodInfo); + + return SyntaxFactory.InvocationExpression(SyntaxFactory.ParseExpression(name), node.ArgumentList).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); + } + + return node; + } + /// /// Processes a loaded static member, either a field or a property /// diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index d9010301..79606206 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -299,7 +299,7 @@ namespace ComputeSharp.Shaders.Translation MethodDecompiler.Instance.GetSyntaxTree(Action.Method, MethodType.Closure, out MethodDeclarationSyntax root, out SemanticModel semanticModel); // Rewrite the shader method (eg. to fix the type declarations) - ShaderSyntaxRewriter syntaxRewriter = new ShaderSyntaxRewriter(semanticModel); + ShaderSyntaxRewriter syntaxRewriter = new ShaderSyntaxRewriter(semanticModel, ShaderType); root = (MethodDeclarationSyntax)syntaxRewriter.Visit(root); // Extract the implicit local functions @@ -340,7 +340,7 @@ namespace ComputeSharp.Shaders.Translation MethodDecompiler.Instance.GetSyntaxTree(methodInfo, MethodType.Static, out MethodDeclarationSyntax root, out SemanticModel semanticModel); // Rewrite the method - ShaderSyntaxRewriter syntaxRewriter = new ShaderSyntaxRewriter(semanticModel); + ShaderSyntaxRewriter syntaxRewriter = new ShaderSyntaxRewriter(semanticModel, methodInfo.DeclaringType); root = (MethodDeclarationSyntax)syntaxRewriter.Visit(root); // Register the captured static members diff --git a/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs b/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs index c92b2e37..704fcbb6 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderSyntaxRewriter.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Reflection; using ComputeSharp.Shaders.Extensions; using ComputeSharp.Shaders.Translation.Models; @@ -18,11 +19,20 @@ namespace ComputeSharp.Shaders.Translation /// private readonly SemanticModel SemanticModel; + /// + /// The declaring type that hosts the root from which the current syntax tree is inspected + /// + private readonly Type DeclaringType; + /// /// Creates a new instance with the specified parameters /// /// - public ShaderSyntaxRewriter(SemanticModel semanticModel) => SemanticModel = semanticModel; + public ShaderSyntaxRewriter(SemanticModel semanticModel, Type declaringType) + { + SemanticModel = semanticModel; + DeclaringType = declaringType; + } private readonly Dictionary _StaticMembers = new Dictionary(); @@ -119,6 +129,21 @@ namespace ComputeSharp.Shaders.Translation return syntaxNode; } + /// + public override SyntaxNode VisitInvocationExpression(InvocationExpressionSyntax node) + { + node = (InvocationExpressionSyntax)base.VisitInvocationExpression(node); + node = node.ReplaceInvocation(DeclaringType, out var method); + + // Register the captured member, if present + if (method.HasValue && !_StaticMethods.ContainsKey(method.Value.Name)) + { + _StaticMethods.Add(method.Value.Name, method.Value.MethodInfo); + } + + return node; + } + /// public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node) { diff --git a/tests/ComputeSharp.Tests/StaticFunctionsTests.cs b/tests/ComputeSharp.Tests/StaticFunctionsTests.cs index fbcd1c53..f1bea042 100644 --- a/tests/ComputeSharp.Tests/StaticFunctionsTests.cs +++ b/tests/ComputeSharp.Tests/StaticFunctionsTests.cs @@ -135,5 +135,19 @@ namespace ComputeSharp.Tests Assert.IsTrue(MathF.Abs(result[0] - 9) < 0.0001f); } + + public static float Square(float x) => x * x; + + [TestMethod] + public void StaticMethodInSameClass() + { + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(1); + + Gpu.Default.For(1, id => buffer[0] = Square(3)); + + float[] result = buffer.GetData(); + + Assert.IsTrue(MathF.Abs(result[0] - 9) < 0.0001f); + } } }