Enabled types import for captured delegates

このコミットが含まれているのは:
Sergio Pedri committed 2021-07-14 16:09:24 +02:00
1 親 da786aaf0d
コミット 4db77f72b0
3ファイル変更
+79

検索条件に一致するファイルはありません

+8
ファイルの表示
@@ -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)");
@@ -22,6 +22,22 @@ namespace ComputeSharp.__Internals
}
}
/// <summary>
/// Appends the discovered types for the current method.
/// </summary>
/// <param name="builder">The target <see cref="ArrayPoolStringBuilder"/> instance to write to.</param>
/// <param name="mapping">The mapping of already discovered type names.</param>
public void AppendTypes(ref ArrayPoolStringBuilder builder, HashSet<string> mapping)
{
foreach (KeyValuePair<string, string> type in this.types)
{
if (mapping.Add(type.Key))
{
builder.Append(type.Value);
}
}
}
/// <summary>
/// Appends the mapped source code for the current method.
/// </summary>
+55
ファイルの表示
@@ -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<Position> buffer;
public readonly Func<int, Position> activation;
public void Execute()
{
buffer[ThreadIds.X] = activation(ThreadIds.X);
}
}
[CombinatorialTestMethod]
[AllDevices]
public unsafe void DelegateWithTypes(Device device)
{
using ReadWriteBuffer<Position> buffer = device.Get().AllocateReadWriteBuffer<Position>(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);
}
}
}
}