diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs index 269b2adc..ab8901df 100644 --- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs @@ -416,6 +416,55 @@ namespace ComputeSharp.Tests return default; } } + + [TestMethod] + public void StructInstanceMethods() + { + _ = ReflectionServices.GetShaderInfo(); + } + + 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 bufferA; + public readonly ReadWriteBuffer bufferB; + public readonly ReadWriteBuffer results; + + /// + 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(); + } + } } } diff --git a/tests/ComputeSharp.Tests/ShaderRewriterTests.cs b/tests/ComputeSharp.Tests/ShaderRewriterTests.cs index 4ca56f7e..ad8033c2 100644 --- a/tests/ComputeSharp.Tests/ShaderRewriterTests.cs +++ b/tests/ComputeSharp.Tests/ShaderRewriterTests.cs @@ -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 source = device.Get().AllocateReadWriteBuffer(data); + using ReadWriteBuffer destination = device.Get().AllocateReadWriteBuffer(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 source; + public readonly ReadWriteBuffer destination; + + public void Execute() + { + destination[ThreadIds.X] = source[ThreadIds.X].AddAndGetSum(ThreadIds.X + 1); + } + } }