diff --git a/src/ComputeSharp.Core/Primitives/Bool/Bool.cs b/src/ComputeSharp.Core/Primitives/Bool/Bool.cs
new file mode 100644
index 00000000..597d7695
--- /dev/null
+++ b/src/ComputeSharp.Core/Primitives/Bool/Bool.cs
@@ -0,0 +1,66 @@
+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();
+
+ ///
+ public override string ToString() => Value.ToString();
+
+ ///
+ /// 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);
+ }
+}
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)]
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) ||
diff --git a/tests/ComputeSharp.Tests/ShaderClosureTests.cs b/tests/ComputeSharp.Tests/ShaderClosureTests.cs
index 7815e739..19efe7b1 100644
--- a/tests/ComputeSharp.Tests/ShaderClosureTests.cs
+++ b/tests/ComputeSharp.Tests/ShaderClosureTests.cs
@@ -10,19 +10,64 @@ 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);
+ 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);
+ }
- Action action = id => buffer[0] = value;
+ [TestMethod]
+ public void ShaderWithBoolType()
+ {
+ using ReadWriteBuffer bools = Gpu.Default.AllocateReadWriteBuffer(new Bool[] { true, false, true, true });
+ using ReadWriteBuffer ints = Gpu.Default.AllocateReadWriteBuffer(4);
- Gpu.Default.For(1, action);
+ void Foo(ThreadIds id)
+ {
+ if (bools[id.X]) ints[id.X] = id.X + 1;
+ bools[id.X] = (id.X % 2) == 0;
+ }
- float[] result = buffer.GetData();
+ Gpu.Default.For(4, Foo);
- Assert.IsTrue(MathF.Abs(result[0] - value) < 0.0001f);
+ 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]