From e512c5839e8d70bf2cbc0fcd26a12c789ff6f40a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 5 Sep 2019 17:33:41 +0200 Subject: [PATCH] Added support for captured local functions cast to delegates --- .../Translation/MethodDecompiler.cs | 2 +- .../StaticFunctionsTests.cs | 22 +++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs index a0bd0703..0a543835 100644 --- a/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/MethodDecompiler.cs @@ -208,7 +208,7 @@ namespace ComputeSharp.Shaders.Translation { string sourceCode = DecompileMethodOrDeclaringType(methodInfo, true), - methodFixedCode = sourceCode.Replace(methodInfo.Name, methodInfo.Name.Replace("<", string.Empty).Replace(">", "_")), + methodFixedCode = sourceCode.Replace(methodInfo.Name, Regex.Replace(methodInfo.Name, "[<>|]", "_")), prototype = methodFixedCode.Split(Environment.NewLine)[0], commentedSourceCode = $"// {methodInfo.Name}{Environment.NewLine}{methodFixedCode}", inFixedSourceCode = Regex.Replace(commentedSourceCode, @"(? f = x => x * x; - Func invert = x => -x; - - Gpu.Default.For(1, id => buffer[0] = f(3) + invert(8)); + Gpu.Default.For(1, id => buffer[0] = f(3)); float[] result = buffer.GetData(); - Assert.IsTrue(MathF.Abs(result[0] - 1) < 0.0001f); + Assert.IsTrue(MathF.Abs(result[0] - 9) < 0.0001f); + } + + [TestMethod] + public void StatelessLocalFunctionToDelegate() + { + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(1); + + float Func(float x) => x * x; + + Func f = Func; + + Gpu.Default.For(1, id => buffer[0] = f(3)); + + float[] result = buffer.GetData(); + + Assert.IsTrue(MathF.Abs(result[0] - 9) < 0.0001f); } } }