From 4db77f72b0688f7581afbf484f69ff590d24ec93 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 14 Jul 2021 16:09:24 +0200 Subject: [PATCH] Enabled types import for captured delegates --- ...erGenerator.CreateBuildHlslStringMethod.cs | 8 +++ ...derMethodSourceAttribute.HlslGeneration.cs | 16 ++++++ .../MetaprogrammingTests.cs | 55 +++++++++++++++++++ 3 files changed, 79 insertions(+) diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs index f0ff8b7a..cbda6b4d 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs @@ -657,6 +657,14 @@ namespace ComputeSharp.SourceGenerators AppendLineAndLF(typeDefinition); } + // Declared types from captured delegates + foreach (var (_, fieldName, _) in instanceFields.Where(static t => t.Symbol.Type.TypeKind == TypeKind.Delegate)) + { + AppendLF(); + AppendParsedStatement($"__{fieldName}Attribute.AppendTypes(ref builder, __typeNames);"); + AppendLF(); + } + // Captured variables AppendLF(); AppendLineAndLF("cbuffer _ : register(b0)"); diff --git a/src/ComputeSharp/Core/Attributes/__Internals/ShaderMethodSourceAttribute.HlslGeneration.cs b/src/ComputeSharp/Core/Attributes/__Internals/ShaderMethodSourceAttribute.HlslGeneration.cs index eabb96b8..9e79aa98 100644 --- a/src/ComputeSharp/Core/Attributes/__Internals/ShaderMethodSourceAttribute.HlslGeneration.cs +++ b/src/ComputeSharp/Core/Attributes/__Internals/ShaderMethodSourceAttribute.HlslGeneration.cs @@ -22,6 +22,22 @@ namespace ComputeSharp.__Internals } } + /// + /// Appends the discovered types for the current method. + /// + /// The target instance to write to. + /// The mapping of already discovered type names. + public void AppendTypes(ref ArrayPoolStringBuilder builder, HashSet mapping) + { + foreach (KeyValuePair type in this.types) + { + if (mapping.Add(type.Key)) + { + builder.Append(type.Value); + } + } + } + /// /// Appends the mapped source code for the current method. /// diff --git a/tests/ComputeSharp.Tests/MetaprogrammingTests.cs b/tests/ComputeSharp.Tests/MetaprogrammingTests.cs index 1a7f434a..9d5bf688 100644 --- a/tests/ComputeSharp.Tests/MetaprogrammingTests.cs +++ b/tests/ComputeSharp.Tests/MetaprogrammingTests.cs @@ -36,6 +36,26 @@ namespace ComputeSharp.Tests [ShaderMethod] public static int FunctionWithConstants(int x) => (int)(x + FourAndAHalf); + + [ShaderMethod] + public static Position FunctionWithTypes(int x) + { + Point point = default; + point.A = x; + point.B = x * x; + + Position position = default; + position.X = point.A; + position.Y = point.B; + + return position; + } + + public struct Point + { + public int A; + public int B; + } } [CombinatorialTestMethod] @@ -145,5 +165,40 @@ namespace ComputeSharp.Tests Assert.AreEqual(result[i], MagicNumber + Activations.FunctionWithConstants(i)); } } + + public struct Position + { + public int X; + public int Y; + } + + [AutoConstructor] + internal readonly partial struct DelegateWithTypesShader : IComputeShader + { + public readonly ReadWriteBuffer buffer; + public readonly Func activation; + + public void Execute() + { + buffer[ThreadIds.X] = activation(ThreadIds.X); + } + } + + [CombinatorialTestMethod] + [AllDevices] + public unsafe void DelegateWithTypes(Device device) + { + using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(100); + + device.Get().For(100, new DelegateWithTypesShader(buffer, Activations.FunctionWithTypes)); + + Position[] result = buffer.ToArray(); + + for (int i = 0; i < 100; i++) + { + Assert.AreEqual(result[i].X, Activations.FunctionWithTypes(i).X); + Assert.AreEqual(result[i].Y, Activations.FunctionWithTypes(i).Y); + } + } } }