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); + } +}