diff --git a/tests/ComputeSharp.Tests/ShaderMembersTests.cs b/tests/ComputeSharp.Tests/ShaderMembersTests.cs index 9960f410..706aed37 100644 --- a/tests/ComputeSharp.Tests/ShaderMembersTests.cs +++ b/tests/ComputeSharp.Tests/ShaderMembersTests.cs @@ -1,4 +1,5 @@ -using ComputeSharp.Tests.Attributes; +using System.Linq; +using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -83,4 +84,94 @@ public partial class ShaderMembersTests buffer[ThreadIds.X] = total; } } + + // See: https://github.com/Sergio0694/ComputeSharp/issues/193 + [CombinatorialTestMethod] + [AllDevices] + public void BoolInstanceFields(Device device) + { + using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(13, AllocationMode.Clear); + + device.Get().For(1, new BoolInstanceFieldsShader(buffer, true, false, 42, false, true, new int3(1, 2, 3), true, new bool2(false, true), 123, true)); + + int[] results = buffer.ToArray(); + + CollectionAssert.AreEqual( + expected: new[] { 1, 0, 42, 0, 1, 1, 2, 3, 1, 0, 1, 123, 1 }, + actual: results); + } + + [AutoConstructor] + internal readonly partial struct BoolInstanceFieldsShader : IComputeShader + { + public readonly ReadWriteBuffer buffer; + + public readonly bool a; + public readonly bool b; + public readonly int c; + public readonly bool d; + public readonly bool e; + public readonly int3 f; + public readonly bool g; + public readonly bool2 h; + public readonly int i; + public readonly bool j; + + public void Execute() + { + buffer[0] = Hlsl.BoolToInt(a); + buffer[1] = Hlsl.BoolToInt(b); + buffer[2] = c; + buffer[3] = Hlsl.BoolToInt(d); + buffer[4] = Hlsl.BoolToInt(e); + buffer[5] = f.X; + buffer[6] = f.Y; + buffer[7] = f.Z; + buffer[8] = Hlsl.BoolToInt(g); + buffer[9] = Hlsl.BoolToInt(h.X); + buffer[10] = Hlsl.BoolToInt(h.Y); + buffer[11] = i; + buffer[12] = Hlsl.BoolToInt(j); + } + } + + // See: https://github.com/Sergio0694/ComputeSharp/issues/193 + [CombinatorialTestMethod] + [AllDevices] + public void BoolInstanceFieldInCustomStruct(Device device) + { + Int2AndBoolField[] data = Enumerable.Range(1, 100).Select(i => new Int2AndBoolField { P = new Int2(i, i) }).ToArray(); + + using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(data); + + device.Get().For(100, new BoolInstanceFieldInCustomStructShader(buffer)); + + Int2AndBoolField[] results = buffer.ToArray(); + + for (int i = 0; i < results.Length; i++) + { + ref Int2AndBoolField value = ref results[i]; + + Assert.AreEqual(i + 1, value.P.X); + Assert.AreEqual(i + 1, value.P.Y); + Assert.AreEqual(true, (bool)value.B); + } + } + + public struct Int2AndBoolField + { + public Int2 P; + public Bool B; + } + + [AutoConstructor] + internal readonly partial struct BoolInstanceFieldInCustomStructShader : IComputeShader + { + public readonly ReadWriteBuffer buffer; + + public void Execute() + { + buffer[ThreadIds.X].B = true; + } + } }