From 36f2512cf6449225e80bafc63836cf6704df6efd Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 23 Feb 2021 02:32:08 +0100 Subject: [PATCH] Fixed an issue with local functions in external methods --- .../Shaders/TwoTiledTruchet.cs | 24 +++++++------- .../IComputeShaderSourceGenerator.cs | 12 +++---- .../ComputeSharp.Tests/ShaderCompilerTests.cs | 32 ++++++++++++++++--- 3 files changed, 46 insertions(+), 22 deletions(-) diff --git a/samples/ComputeSharp.Sample.SwapChain/Shaders/TwoTiledTruchet.cs b/samples/ComputeSharp.Sample.SwapChain/Shaders/TwoTiledTruchet.cs index a9f7781a..14e5213b 100644 --- a/samples/ComputeSharp.Sample.SwapChain/Shaders/TwoTiledTruchet.cs +++ b/samples/ComputeSharp.Sample.SwapChain/Shaders/TwoTiledTruchet.cs @@ -18,18 +18,6 @@ /// public readonly float time; - // Fast hash for a pair of floats - private static float Hash21(Float2 p) - { - return Hlsl.Frac(Hlsl.Sin(Hlsl.Dot(p, new Float2(27.619f, 57.583f))) * 43758.5453f); - } - - // HLSL's port of the GLSL mod intrinsic - private static float Mod(float x, float y) - { - return x - y * Hlsl.Floor(x / y); - } - /// /// Calculates the Truchet distance field. /// @@ -38,6 +26,18 @@ /// The distance field for the input value. private Float2 DistanceField(Float2 p, out Float2 ang) { + // Fast hash for a pair of floats + static float Hash21(Float2 p) + { + return Hlsl.Frac(Hlsl.Sin(Hlsl.Dot(p, new Float2(27.619f, 57.583f))) * 43758.5453f); + } + + // HLSL's port of the GLSL mod intrinsic + static float Mod(float x, float y) + { + return x - y * Hlsl.Floor(x / y); + } + Float2 ip2 = Hlsl.Floor(p / 2.0f); Float2 ip = Hlsl.Floor(p); float rnd = Hash21(ip); diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs index aebad7c3..b6c54421 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs @@ -251,6 +251,12 @@ namespace ComputeSharp.SourceGenerators // Rewrite the method syntax tree MethodDeclarationSyntax? processedMethod = shaderSourceRewriter.Visit(methodDeclaration)!.WithoutTrivia(); + // Emit the extracted local functions first + foreach (var localFunction in shaderSourceRewriter.LocalFunctions) + { + methods.Add(localFunction.Value); + } + // If the method is the shader entry point, do additional processing if (isShaderEntryPoint) { @@ -259,12 +265,6 @@ namespace ComputeSharp.SourceGenerators entryPoint = processedMethod.NormalizeWhitespace().ToFullString(); } else methods.Add(processedMethod); - - // Emit the extracted local functions - foreach (var localFunction in shaderSourceRewriter.LocalFunctions) - { - methods.Add(localFunction.Value); - } } return (entryPoint!, methods); diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs index b490ce77..09faf240 100644 --- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs @@ -11,11 +11,11 @@ namespace ComputeSharp.Tests [TestMethod] public void ReservedKeywords() { - ReflectionServices.GetShaderInfo(out _); + ReflectionServices.GetShaderInfo(out _); } [AutoConstructor] - public readonly partial struct SoftmaxActivation : IComputeShader + public readonly partial struct ReservedKeywords_Kernel : IComputeShader { public readonly ReadWriteBuffer row_major; public readonly float dword; @@ -32,11 +32,11 @@ namespace ComputeSharp.Tests [TestMethod] public void SpecialTypeAsReturnType() { - ReflectionServices.GetShaderInfo(out _); + ReflectionServices.GetShaderInfo(out _); } [AutoConstructor] - public readonly partial struct FloatReturnType : IComputeShader + public readonly partial struct SpecialTypeAsReturnType_Kernel : IComputeShader { public readonly ReadWriteBuffer buffer; @@ -49,5 +49,29 @@ namespace ComputeSharp.Tests buffer[ThreadIds.X] = Foo(ThreadIds.X) + Bar(ThreadIds.X).XY; } } + + [TestMethod] + public void LocalFunctionInExternalMethods() + { + ReflectionServices.GetShaderInfo(out _); + } + + [AutoConstructor] + public readonly partial struct LocalFunctionInExternalMethods_Kernel : IComputeShader + { + public readonly ReadWriteBuffer buffer; + + Float2 Foo(float x) + { + static Float2 Baz(float y) => y; + + return Baz(x); + } + + public void Execute() + { + buffer[ThreadIds.X] = Foo(ThreadIds.X); + } + } } }