From ab747f4b8044c22212ec8e5a3c7628422858cd43 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 27 Aug 2022 01:53:11 +0200 Subject: [PATCH] Add unit tests for bool intrinsics --- .../ComputeSharp.Tests/ShaderRewriterTests.cs | 157 ++++++++++++++++++ 1 file changed, 157 insertions(+) diff --git a/tests/ComputeSharp.Tests/ShaderRewriterTests.cs b/tests/ComputeSharp.Tests/ShaderRewriterTests.cs index 9f2dc298..8b9cb27c 100644 --- a/tests/ComputeSharp.Tests/ShaderRewriterTests.cs +++ b/tests/ComputeSharp.Tests/ShaderRewriterTests.cs @@ -237,6 +237,163 @@ public partial class ShaderRewriterTests } } + [CombinatorialTestMethod] + [AllDevices] + public void ToBooleanConversionHlslIntrinsics(Device device) + { + float[] data1 = new[] { 3.14f, 0, 1.44f, 0, 0, 0.4445f }; + int[] data2 = new[] { 3, 0, 1, 0, 0, 156 }; + + using ReadOnlyBuffer buffer1 = device.Get().AllocateReadOnlyBuffer(data1); + using ReadOnlyBuffer buffer2 = device.Get().AllocateReadOnlyBuffer(data2); + using ReadWriteBuffer buffer3 = device.Get().AllocateReadWriteBuffer(18, AllocationMode.Clear); + + device.Get().For(1, new ToBooleanConversionIntrinsicsShader(buffer1, buffer2, buffer3)); + + int[] results = buffer3.ToArray(); + + CollectionAssert.AreEqual( + expected: new[] { 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1 }, + actual: results); + } + + [AutoConstructor] + internal readonly partial struct ToBooleanConversionIntrinsicsShader : IComputeShader + { + public readonly ReadOnlyBuffer buffer1; + public readonly ReadOnlyBuffer buffer2; + public readonly ReadWriteBuffer result; + + public void Execute() + { + float f1 = buffer1[0]; + float f2 = buffer1[1]; + float4 f4 = new(buffer1[2], buffer1[3], buffer1[4], buffer1[5]); + + int i1 = buffer2[0]; + int i2 = buffer2[1]; + int4 i4 = new(buffer2[2], buffer2[3], buffer2[4], buffer2[5]); + + uint u1 = (uint)buffer2[0]; + uint u2 = (uint)buffer2[1]; + uint4 u4 = new((uint)buffer2[2], (uint)buffer2[3], (uint)buffer2[4], (uint)buffer2[5]); + + result[0] = Hlsl.FloatToBool(f1) ? 1 : 0; + result[1] = Hlsl.FloatToBool(f2) ? 1 : 0; + + bool4 f4b = Hlsl.FloatToBool(f4); + + result[2] = f4b[0] ? 1 : 0; + result[3] = f4b[1] ? 1 : 0; + result[4] = f4b[2] ? 1 : 0; + result[5] = f4b[3] ? 1 : 0; + + result[6] = Hlsl.IntToBool(i1) ? 1 : 0; + result[7] = Hlsl.IntToBool(i2) ? 1 : 0; + + bool4 i4b = Hlsl.IntToBool(i4); + + result[8] = i4b[0] ? 1 : 0; + result[9] = i4b[1] ? 1 : 0; + result[10] = i4b[2] ? 1 : 0; + result[11] = i4b[3] ? 1 : 0; + + result[12] = Hlsl.UIntToBool(u1) ? 1 : 0; + result[13] = Hlsl.UIntToBool(u2) ? 1 : 0; + + bool4 u4b = Hlsl.UIntToBool(u4); + + result[14] = u4b[0] ? 1 : 0; + result[15] = u4b[1] ? 1 : 0; + result[16] = u4b[2] ? 1 : 0; + result[17] = u4b[3] ? 1 : 0; + } + } + + [CombinatorialTestMethod] + [AllDevices] + public void FromBooleanConversionHlslIntrinsics(Device device) + { + int[] data = new[] { 1, 0, 1, 0, 0, 1 }; + + using ReadOnlyBuffer buffer1 = device.Get().AllocateReadOnlyBuffer(data); + using ReadWriteBuffer buffer2 = device.Get().AllocateReadWriteBuffer(6, AllocationMode.Clear); + using ReadWriteBuffer buffer3 = device.Get().AllocateReadWriteBuffer(6, AllocationMode.Clear); + using ReadWriteBuffer buffer4 = device.Get().AllocateReadWriteBuffer(6, AllocationMode.Clear); + + device.Get().For(1, new FromBooleanConversionIntrinsicsShader(buffer1, buffer2, buffer3, buffer4)); + + float[] results1 = buffer2.ToArray(); + int[] results2 = buffer3.ToArray(); + uint[] results3 = buffer4.ToArray(); + + CollectionAssert.AreEqual( + expected: new float[] { 1, 0, 1, 0, 0, 1 }, + actual: results1); + + CollectionAssert.AreEqual( + expected: new int[] { 1, 0, 1, 0, 0, 1 }, + actual: results2); + + CollectionAssert.AreEqual( + expected: new uint[] { 1, 0, 1, 0, 0, 1 }, + actual: results3); + } + + [AutoConstructor] + internal readonly partial struct FromBooleanConversionIntrinsicsShader : IComputeShader + { + public readonly ReadOnlyBuffer source; + public readonly ReadWriteBuffer destination1; + public readonly ReadWriteBuffer destination2; + public readonly ReadWriteBuffer destination3; + + public void Execute() + { + bool b1 = source[0] != 0; + bool b2 = source[1] != 0; + bool4 b4 = new(source[2] != 0, source[3] != 0, source[4] != 0, source[5] != 0); + + bool b11 = source[0] != 0; + bool b22 = source[1] != 0; + bool4 b44 = new(source[2] != 0, source[3] != 0, source[4] != 0, source[5] != 0); + + bool b111 = source[0] != 0; + bool b222 = source[1] != 0; + bool4 b444 = new(source[2] != 0, source[3] != 0, source[4] != 0, source[5] != 0); + + destination1[0] = Hlsl.BoolToFloat(b1); + destination1[1] = Hlsl.BoolToFloat(b2); + + float4 f4 = Hlsl.BoolToFloat(b4); + + destination1[2] = f4.X; + destination1[3] = f4.Y; + destination1[4] = f4.Z; + destination1[5] = f4.W; + + destination2[0] = Hlsl.BoolToInt(b11); + destination2[1] = Hlsl.BoolToInt(b22); + + int4 i4 = Hlsl.BoolToInt(b44); + + destination2[2] = i4.X; + destination2[3] = i4.Y; + destination2[4] = i4.Z; + destination2[5] = i4.W; + + destination3[0] = Hlsl.BoolToUInt(b111); + destination3[1] = Hlsl.BoolToUInt(b222); + + uint4 u4 = Hlsl.BoolToUInt(b444); + + destination3[2] = u4.X; + destination3[3] = u4.Y; + destination3[4] = u4.Z; + destination3[5] = u4.W; + } + } + // See: https://github.com/Sergio0694/ComputeSharp/issues/259 [CombinatorialTestMethod] [AllDevices]