From 52ed2a4ac4824481233e660401340613b2dcceef Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 20 Sep 2019 02:47:22 +0200 Subject: [PATCH 1/5] Added Bool struct to map the bool HLSL type --- src/ComputeSharp.Core/Primitives/Bool/Bool.cs | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/ComputeSharp.Core/Primitives/Bool/Bool.cs diff --git a/src/ComputeSharp.Core/Primitives/Bool/Bool.cs b/src/ComputeSharp.Core/Primitives/Bool/Bool.cs new file mode 100644 index 00000000..f83e8041 --- /dev/null +++ b/src/ComputeSharp.Core/Primitives/Bool/Bool.cs @@ -0,0 +1,63 @@ +using System.Diagnostics; +using System.Runtime.InteropServices; + +namespace ComputeSharp +{ + /// + /// A that can be used in place of the type in HLSL shaders + /// + [DebuggerDisplay("{Value}")] + [StructLayout(LayoutKind.Explicit, Size = sizeof(int))] + public readonly struct Bool + { + /// + /// The wrapped value for the current instance + /// + [FieldOffset(0)] + private readonly bool Value; + + /// + /// Creates a new instance for a given value + /// + /// + private Bool(bool value) => Value = value; + + /// + public override bool Equals(object obj) => obj is Bool x && Value == x.Value; + + /// + public override int GetHashCode() => Value.GetHashCode(); + + /// + /// Checks whether or not two instances represent the same value + /// + /// The left instance + /// The right instance + public static bool operator ==(Bool left, Bool right) => left.Value == right.Value; + + /// + /// Checks whether or not two instances represent a different value + /// + /// The left instance + /// The right instance + public static bool operator !=(Bool left, Bool right) => left.Value != right.Value; + + /// + /// Inverts the value represented by a given instance + /// + /// The input instance + public static Bool operator !(Bool x) => new Bool(!x.Value); + + /// + /// Converts a given instance to its corresponding value + /// + /// The input instance + public static implicit operator bool(Bool x) => x.Value; + + /// + /// Converts a value to a corresponding instance + /// + /// The input value + public static implicit operator Bool(bool x) => new Bool(x); + } +} From 200d91a539d7955d846ef3930f1868a3ba84712f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 20 Sep 2019 02:49:48 +0200 Subject: [PATCH 2/5] Tweaked some XML comments --- src/ComputeSharp.Core/Primitives/Bool/Bool2.cs | 2 +- src/ComputeSharp.Core/Primitives/Bool/Bool3.cs | 2 +- src/ComputeSharp.Core/Primitives/Bool/Bool4.cs | 2 +- src/ComputeSharp.Core/Primitives/Double/Double2.cs | 2 +- src/ComputeSharp.Core/Primitives/Double/Double3.cs | 2 +- src/ComputeSharp.Core/Primitives/Double/Double4.cs | 2 +- src/ComputeSharp.Core/Primitives/Float/Float2.cs | 2 +- src/ComputeSharp.Core/Primitives/Float/Float3.cs | 2 +- src/ComputeSharp.Core/Primitives/Float/Float4.cs | 2 +- src/ComputeSharp.Core/Primitives/Int/Int2.cs | 2 +- src/ComputeSharp.Core/Primitives/Int/Int3.cs | 2 +- src/ComputeSharp.Core/Primitives/Int/Int4.cs | 2 +- src/ComputeSharp.Core/Primitives/UInt/UInt2.cs | 2 +- src/ComputeSharp.Core/Primitives/UInt/UInt3.cs | 2 +- src/ComputeSharp.Core/Primitives/UInt/UInt4.cs | 2 +- 15 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/ComputeSharp.Core/Primitives/Bool/Bool2.cs b/src/ComputeSharp.Core/Primitives/Bool/Bool2.cs index 9d123f70..bbdb8fc6 100644 --- a/src/ComputeSharp.Core/Primitives/Bool/Bool2.cs +++ b/src/ComputeSharp.Core/Primitives/Bool/Bool2.cs @@ -5,7 +5,7 @@ using ComputeSharp.Exceptions; namespace ComputeSharp { /// - /// A that maps the bool2 HLSL type + /// A that maps the HLSL type /// [DebuggerDisplay("({X}, {Y})")] [StructLayout(LayoutKind.Explicit, Size = sizeof(int) * 2)] diff --git a/src/ComputeSharp.Core/Primitives/Bool/Bool3.cs b/src/ComputeSharp.Core/Primitives/Bool/Bool3.cs index aaa36147..5766ffe4 100644 --- a/src/ComputeSharp.Core/Primitives/Bool/Bool3.cs +++ b/src/ComputeSharp.Core/Primitives/Bool/Bool3.cs @@ -5,7 +5,7 @@ using ComputeSharp.Exceptions; namespace ComputeSharp { /// - /// A that maps the bool3 HLSL type + /// A that maps the HLSL type /// [DebuggerDisplay("({X}, {Y}, {Z})")] [StructLayout(LayoutKind.Explicit, Size = sizeof(int) * 3)] diff --git a/src/ComputeSharp.Core/Primitives/Bool/Bool4.cs b/src/ComputeSharp.Core/Primitives/Bool/Bool4.cs index 557868ca..c731f5c7 100644 --- a/src/ComputeSharp.Core/Primitives/Bool/Bool4.cs +++ b/src/ComputeSharp.Core/Primitives/Bool/Bool4.cs @@ -5,7 +5,7 @@ using ComputeSharp.Exceptions; namespace ComputeSharp { /// - /// A that maps the bool4 HLSL type + /// A that maps the HLSL type /// [DebuggerDisplay("({X}, {Y}, {Z}, {W})")] [StructLayout(LayoutKind.Explicit, Size = sizeof(int) * 4)] diff --git a/src/ComputeSharp.Core/Primitives/Double/Double2.cs b/src/ComputeSharp.Core/Primitives/Double/Double2.cs index 0c377e4b..d7974f93 100644 --- a/src/ComputeSharp.Core/Primitives/Double/Double2.cs +++ b/src/ComputeSharp.Core/Primitives/Double/Double2.cs @@ -5,7 +5,7 @@ using ComputeSharp.Exceptions; namespace ComputeSharp { /// - /// A that maps the double2 HLSL type + /// A that maps the HLSL type /// [DebuggerDisplay("({X}, {Y})")] [StructLayout(LayoutKind.Sequential, Size = sizeof(double) * 2)] diff --git a/src/ComputeSharp.Core/Primitives/Double/Double3.cs b/src/ComputeSharp.Core/Primitives/Double/Double3.cs index 3a56db61..627f27db 100644 --- a/src/ComputeSharp.Core/Primitives/Double/Double3.cs +++ b/src/ComputeSharp.Core/Primitives/Double/Double3.cs @@ -5,7 +5,7 @@ using ComputeSharp.Exceptions; namespace ComputeSharp { /// - /// A that maps the double3 HLSL type + /// A that maps the HLSL type /// [DebuggerDisplay("({X}, {Y}, {Z})")] [StructLayout(LayoutKind.Sequential, Size = sizeof(double) * 3)] diff --git a/src/ComputeSharp.Core/Primitives/Double/Double4.cs b/src/ComputeSharp.Core/Primitives/Double/Double4.cs index 2a3e57c6..3bec6557 100644 --- a/src/ComputeSharp.Core/Primitives/Double/Double4.cs +++ b/src/ComputeSharp.Core/Primitives/Double/Double4.cs @@ -5,7 +5,7 @@ using ComputeSharp.Exceptions; namespace ComputeSharp { /// - /// A that maps the double4 HLSL type + /// A that maps the HLSL type /// [DebuggerDisplay("({X}, {Y}, {Z}, {W})")] [StructLayout(LayoutKind.Sequential, Size = sizeof(double) * 4)] diff --git a/src/ComputeSharp.Core/Primitives/Float/Float2.cs b/src/ComputeSharp.Core/Primitives/Float/Float2.cs index a45fcf41..0b9e1167 100644 --- a/src/ComputeSharp.Core/Primitives/Float/Float2.cs +++ b/src/ComputeSharp.Core/Primitives/Float/Float2.cs @@ -5,7 +5,7 @@ using ComputeSharp.Exceptions; namespace ComputeSharp { /// - /// A that maps the float2 HLSL type + /// A that maps the HLSL type /// [DebuggerDisplay("({X}, {Y})")] [StructLayout(LayoutKind.Sequential, Size = sizeof(float) * 2)] diff --git a/src/ComputeSharp.Core/Primitives/Float/Float3.cs b/src/ComputeSharp.Core/Primitives/Float/Float3.cs index 568f4eda..00d1323b 100644 --- a/src/ComputeSharp.Core/Primitives/Float/Float3.cs +++ b/src/ComputeSharp.Core/Primitives/Float/Float3.cs @@ -5,7 +5,7 @@ using ComputeSharp.Exceptions; namespace ComputeSharp { /// - /// A that maps the float3 HLSL type + /// A that maps the HLSL type /// [DebuggerDisplay("({X}, {Y}, {Z})")] [StructLayout(LayoutKind.Sequential, Size = sizeof(float) * 3)] diff --git a/src/ComputeSharp.Core/Primitives/Float/Float4.cs b/src/ComputeSharp.Core/Primitives/Float/Float4.cs index c8727ffb..70f2c4b2 100644 --- a/src/ComputeSharp.Core/Primitives/Float/Float4.cs +++ b/src/ComputeSharp.Core/Primitives/Float/Float4.cs @@ -5,7 +5,7 @@ using ComputeSharp.Exceptions; namespace ComputeSharp { /// - /// A that maps the float4 HLSL type + /// A that maps the HLSL type /// [DebuggerDisplay("({X}, {Y}, {Z}, {W})")] [StructLayout(LayoutKind.Sequential, Size = sizeof(float) * 4)] diff --git a/src/ComputeSharp.Core/Primitives/Int/Int2.cs b/src/ComputeSharp.Core/Primitives/Int/Int2.cs index 06252c89..012170f5 100644 --- a/src/ComputeSharp.Core/Primitives/Int/Int2.cs +++ b/src/ComputeSharp.Core/Primitives/Int/Int2.cs @@ -5,7 +5,7 @@ using ComputeSharp.Exceptions; namespace ComputeSharp { /// - /// A that maps the int2 HLSL type + /// A that maps the HLSL type /// [DebuggerDisplay("({X}, {Y})")] [StructLayout(LayoutKind.Sequential, Size = sizeof(int) * 2)] diff --git a/src/ComputeSharp.Core/Primitives/Int/Int3.cs b/src/ComputeSharp.Core/Primitives/Int/Int3.cs index f0b8f57b..9052701c 100644 --- a/src/ComputeSharp.Core/Primitives/Int/Int3.cs +++ b/src/ComputeSharp.Core/Primitives/Int/Int3.cs @@ -5,7 +5,7 @@ using ComputeSharp.Exceptions; namespace ComputeSharp { /// - /// A that maps the int3 HLSL type + /// A that maps the HLSL type /// [DebuggerDisplay("({X}, {Y}, {Z})")] [StructLayout(LayoutKind.Sequential, Size = sizeof(int) * 3)] diff --git a/src/ComputeSharp.Core/Primitives/Int/Int4.cs b/src/ComputeSharp.Core/Primitives/Int/Int4.cs index 71cc010f..ae191475 100644 --- a/src/ComputeSharp.Core/Primitives/Int/Int4.cs +++ b/src/ComputeSharp.Core/Primitives/Int/Int4.cs @@ -5,7 +5,7 @@ using ComputeSharp.Exceptions; namespace ComputeSharp { /// - /// A that maps the int4 HLSL type + /// A that maps the HLSL type /// [DebuggerDisplay("({X}, {Y}, {Z}, {W})")] [StructLayout(LayoutKind.Sequential, Size = sizeof(int) * 4)] diff --git a/src/ComputeSharp.Core/Primitives/UInt/UInt2.cs b/src/ComputeSharp.Core/Primitives/UInt/UInt2.cs index 5be415d9..df1ced13 100644 --- a/src/ComputeSharp.Core/Primitives/UInt/UInt2.cs +++ b/src/ComputeSharp.Core/Primitives/UInt/UInt2.cs @@ -5,7 +5,7 @@ using ComputeSharp.Exceptions; namespace ComputeSharp { /// - /// A that maps the uint2 HLSL type + /// A that maps the HLSL type /// [DebuggerDisplay("({X}, {Y})")] [StructLayout(LayoutKind.Sequential, Size = sizeof(uint) * 2)] diff --git a/src/ComputeSharp.Core/Primitives/UInt/UInt3.cs b/src/ComputeSharp.Core/Primitives/UInt/UInt3.cs index 4917a475..b5eee9b7 100644 --- a/src/ComputeSharp.Core/Primitives/UInt/UInt3.cs +++ b/src/ComputeSharp.Core/Primitives/UInt/UInt3.cs @@ -5,7 +5,7 @@ using ComputeSharp.Exceptions; namespace ComputeSharp { /// - /// A that maps the uint3 HLSL type + /// A that maps the HLSL type /// [DebuggerDisplay("({X}, {Y}, {Z})")] [StructLayout(LayoutKind.Sequential, Size = sizeof(uint) * 3)] diff --git a/src/ComputeSharp.Core/Primitives/UInt/UInt4.cs b/src/ComputeSharp.Core/Primitives/UInt/UInt4.cs index a3d8c623..aeb1c202 100644 --- a/src/ComputeSharp.Core/Primitives/UInt/UInt4.cs +++ b/src/ComputeSharp.Core/Primitives/UInt/UInt4.cs @@ -5,7 +5,7 @@ using ComputeSharp.Exceptions; namespace ComputeSharp { /// - /// A that maps the uuint4 HLSL type + /// A that maps the HLSL type /// [DebuggerDisplay("({X}, {Y}, {Z}, {W})")] [StructLayout(LayoutKind.Sequential, Size = sizeof(uint) * 4)] From fcc41ae91613821e8d2ab899c6635fa3ab97a7e7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 20 Sep 2019 03:13:04 +0200 Subject: [PATCH 3/5] Added mappings for the Bool type --- src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs index de6c9a36..170953b0 100644 --- a/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.Shaders/Mappings/HlslKnownTypes.cs @@ -17,6 +17,7 @@ namespace ComputeSharp.Shaders.Mappings private static readonly IReadOnlyDictionary KnownTypes = new Dictionary { [typeof(bool).FullName] = "bool", + [typeof(Bool).FullName] = "bool", [typeof(Bool2).FullName] = "bool2", [typeof(Bool3).FullName] = "bool3", [typeof(Bool4).FullName] = "bool4", @@ -77,6 +78,7 @@ namespace ComputeSharp.Shaders.Mappings /// A indicating whether the input is in fact a known HLSL scalar type [Pure] public static bool IsKnownScalarType(Type type) => type == typeof(bool) || + type == typeof(Bool) || type == typeof(int) || type == typeof(uint) || type == typeof(float) || From 562a4674745286d33e1c5e42e6ad8be58d113e30 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 20 Sep 2019 03:13:18 +0200 Subject: [PATCH 4/5] Improved tests for scalar types in buffers --- .../ComputeSharp.Tests/ShaderClosureTests.cs | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/tests/ComputeSharp.Tests/ShaderClosureTests.cs b/tests/ComputeSharp.Tests/ShaderClosureTests.cs index 7815e739..53219e6c 100644 --- a/tests/ComputeSharp.Tests/ShaderClosureTests.cs +++ b/tests/ComputeSharp.Tests/ShaderClosureTests.cs @@ -10,19 +10,35 @@ namespace ComputeSharp.Tests [TestCategory("ShaderClosure")] public class ShaderClosureTests { + private static void LocalScalarAssignToBuffer(T value, Func equals) where T : unmanaged + { + using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(4); + + T zero = default; + + Action action = id => + { + if (id.X % 2 == 0) buffer[id.X] = value; + else buffer[id.X] = zero; + }; + + Gpu.Default.For(4, action); + + T[] result = buffer.GetData(); + + Assert.IsTrue(equals(result[0], value)); + Assert.IsFalse(equals(result[1], value)); + Assert.IsTrue(equals(result[2], value)); + Assert.IsFalse(equals(result[3], value)); + } + [TestMethod] public void LocalScalarAssignToBuffer() { - float value = 10; - using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(1); - - Action action = id => buffer[0] = value; - - Gpu.Default.For(1, action); - - float[] result = buffer.GetData(); - - Assert.IsTrue(MathF.Abs(result[0] - value) < 0.0001f); + LocalScalarAssignToBuffer(10, (a, b) => a == b); + LocalScalarAssignToBuffer(3.14f, (a, b) => MathF.Abs(a - b) < 0.001f); + LocalScalarAssignToBuffer(6.28, (a, b) => Math.Abs(a - b) < 0.001f); + LocalScalarAssignToBuffer(true, (a, b) => a == b); } [TestMethod] From 1a16b7df316e29c37ed376161563fdeb4ee68ef0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 20 Sep 2019 13:19:59 +0200 Subject: [PATCH 5/5] Added test for the new Bool type --- src/ComputeSharp.Core/Primitives/Bool/Bool.cs | 3 ++ .../ComputeSharp.Tests/ShaderClosureTests.cs | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/ComputeSharp.Core/Primitives/Bool/Bool.cs b/src/ComputeSharp.Core/Primitives/Bool/Bool.cs index f83e8041..597d7695 100644 --- a/src/ComputeSharp.Core/Primitives/Bool/Bool.cs +++ b/src/ComputeSharp.Core/Primitives/Bool/Bool.cs @@ -28,6 +28,9 @@ namespace ComputeSharp /// public override int GetHashCode() => Value.GetHashCode(); + /// + public override string ToString() => Value.ToString(); + /// /// Checks whether or not two instances represent the same value /// diff --git a/tests/ComputeSharp.Tests/ShaderClosureTests.cs b/tests/ComputeSharp.Tests/ShaderClosureTests.cs index 53219e6c..19efe7b1 100644 --- a/tests/ComputeSharp.Tests/ShaderClosureTests.cs +++ b/tests/ComputeSharp.Tests/ShaderClosureTests.cs @@ -41,6 +41,35 @@ namespace ComputeSharp.Tests LocalScalarAssignToBuffer(true, (a, b) => a == b); } + [TestMethod] + public void ShaderWithBoolType() + { + using ReadWriteBuffer bools = Gpu.Default.AllocateReadWriteBuffer(new Bool[] { true, false, true, true }); + using ReadWriteBuffer ints = Gpu.Default.AllocateReadWriteBuffer(4); + + void Foo(ThreadIds id) + { + if (bools[id.X]) ints[id.X] = id.X + 1; + bools[id.X] = (id.X % 2) == 0; + } + + Gpu.Default.For(4, Foo); + + Bool[] boolResults = bools.GetData(); + + Assert.IsTrue(boolResults[0]); + Assert.IsFalse(boolResults[1]); + Assert.IsTrue(boolResults[2]); + Assert.IsFalse(boolResults[3]); + + int[] intResults = ints.GetData(); + + Assert.IsTrue(intResults[0] == 1); + Assert.IsTrue(intResults[1] == 0); + Assert.IsTrue(intResults[2] == 3); + Assert.IsTrue(intResults[3] == 4); + } + [TestMethod] public void LocalScalarAssignToBufferWithinLocalMethod() {