diff --git a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs index 20c2a95a..9a5682c3 100644 --- a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs @@ -6,6 +6,7 @@ using System.IO; using System.Linq; using System.Reflection; using System.Reflection.Metadata; +using System.Text.RegularExpressions; using ICSharpCode.Decompiler; using ICSharpCode.Decompiler.CSharp; using ICSharpCode.Decompiler.Metadata; @@ -97,6 +98,11 @@ namespace ComputeSharp.Shaders.Translation } } + /// + /// A uused to preprocess the entry point declaration for both lambda expressions and local methods + /// + private static readonly Regex LambdaMethodDeclarationRegex = new Regex(@"^(private|internal) void <\w+>[\w_|]+(?=\()", RegexOptions.Compiled); + /// /// Decompiles a target method and returns its and info /// @@ -117,10 +123,13 @@ namespace ComputeSharp.Shaders.Translation Decompilers.Add(assemblyPath, decompiler); } - string sourceCode = decompiler.DecompileAsString(methodHandle); + // Decompile the method source and fix the method declaration for local methods converted to lambdas + string + sourceCode = decompiler.DecompileAsString(methodHandle), + fixedCode = LambdaMethodDeclarationRegex.Replace(sourceCode, "internal void Main"); // Load the method syntax tree - SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(sourceCode); + SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(fixedCode); rootNode = syntaxTree.GetRoot(); // Update the incremental compilation and retrieve the syntax tree for the method diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs index 40859c0d..8b9e43eb 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs @@ -157,7 +157,7 @@ namespace ComputeSharp.Shaders.Translation root = syntaxRewriter.Visit(root); // Get the thread ids identifier name and shader method body - ConstructorDeclarationSyntax methodNode = root.ChildNodes().OfType().First(); + MethodDeclarationSyntax methodNode = root.ChildNodes().OfType().First(); ThreadsIdsVariableName = methodNode.ParameterList.Parameters.First().Identifier.Text; MethodBody = methodNode.Body.ToFullString(); diff --git a/tests/ComputeSharp.NetCore.Tests/ShaderClosureTests.cs b/tests/ComputeSharp.NetCore.Tests/ShaderClosureTests.cs index 8cc81f59..18bad3b9 100644 --- a/tests/ComputeSharp.NetCore.Tests/ShaderClosureTests.cs +++ b/tests/ComputeSharp.NetCore.Tests/ShaderClosureTests.cs @@ -26,6 +26,21 @@ namespace ComputeSharp.NetCore.Tests Assert.IsTrue((int)result[0] == (int)value); } + [TestMethod] + public void LocalScalarAssignToBufferWithinLocalMethod() + { + float value = 10; + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(1); + + void Foo(ThreadIds id) => buffer[0] = value; + + Gpu.Default.For(1, Foo); + + float[] result = buffer.GetData(); + + Assert.IsTrue((int)result[0] == (int)value); + } + [TestMethod] public void LocalKnownVectorAssignToBuffer() {