Add unit tests for struct instance methods rewriting

このコミットが含まれているのは:
Sergio Pedri committed 2022-10-02 17:40:32 +02:00
1 親 2a62f00019
コミット c40ca177a6
2ファイル変更
+97

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

+49
ファイルの表示
@@ -416,6 +416,55 @@ namespace ComputeSharp.Tests
return default;
}
}
[TestMethod]
public void StructInstanceMethods()
{
_ = ReflectionServices.GetShaderInfo<StructInstanceMethodsShader>();
}
public struct MyStructTypeA
{
public int A;
public float B;
public float Sum()
{
return A + Bar();
}
public float Bar() => this.B;
}
public struct MyStructTypeB
{
public MyStructTypeA A;
public float B;
public float Combine()
{
return A.Sum() + this.B;
}
}
[AutoConstructor]
internal readonly partial struct StructInstanceMethodsShader : IComputeShader
{
public readonly MyStructTypeA a;
public readonly MyStructTypeB b;
public readonly ReadWriteBuffer<MyStructTypeA> bufferA;
public readonly ReadWriteBuffer<MyStructTypeB> bufferB;
public readonly ReadWriteBuffer<float> results;
/// <inheritdoc/>
public void Execute()
{
float result1 = a.Sum() + a.Bar();
float result2 = b.Combine();
results[ThreadIds.X] = result1 + result2 + bufferA[ThreadIds.X].Sum() + bufferB[0].Combine();
}
}
}
}
+48
ファイルの表示
@@ -1,4 +1,6 @@
using System;
using System.Linq;
using ComputeSharp.Interop;
using ComputeSharp.Tests.Attributes;
using ComputeSharp.Tests.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -722,4 +724,50 @@ public partial class ShaderRewriterTests
destination2[21] = i2_copy2.Y;
}
}
// See https://github.com/Sergio0694/ComputeSharp/issues/390
[CombinatorialTestMethod]
[AllDevices]
public void CustomStructWypeWithInstanceMethod(Device device)
{
CustomStructType[] data = Enumerable.Range(0, 1024).Select(static i => new CustomStructType() { value = i }).ToArray();
using ReadWriteBuffer<CustomStructType> source = device.Get().AllocateReadWriteBuffer(data);
using ReadWriteBuffer<int> destination = device.Get().AllocateReadWriteBuffer<int>(data.Length, AllocationMode.Clear);
device.Get().For(source.Length, new CustomStructTypeShader(source, destination));
CustomStructType[] results1 = source.ToArray();
int[] results2 = destination.ToArray();
for (int i = 0; i < source.Length; i++)
{
Assert.AreEqual(i * 2 + 1, results1[i].value);
Assert.AreEqual(results1[i].value, results2[i]);
}
}
public struct CustomStructType
{
public int value;
public int AddAndGetSum(int x)
{
this.value += x;
return this.value;
}
}
[AutoConstructor]
internal readonly partial struct CustomStructTypeShader : IComputeShader
{
public readonly ReadWriteBuffer<CustomStructType> source;
public readonly ReadWriteBuffer<int> destination;
public void Execute()
{
destination[ThreadIds.X] = source[ThreadIds.X].AddAndGetSum(ThreadIds.X + 1);
}
}
}