diff --git a/src/ComputeSharp.Core/PixelTypes/R8.cs b/src/ComputeSharp.Core/PixelTypes/R8.cs new file mode 100644 index 00000000..7403ab05 --- /dev/null +++ b/src/ComputeSharp.Core/PixelTypes/R8.cs @@ -0,0 +1,81 @@ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ComputeSharp.__Internals; + +#pragma warning disable CS0618 + +namespace ComputeSharp +{ + /// + /// Packed pixel type containing a single 8-bit unsigned normalized channel value. + /// + /// Ranges from [0, 0, 0, 0] to [1, 0, 0, 0] in vector form. + /// + /// + [StructLayout(LayoutKind.Sequential)] + public struct R8 : IEquatable, IUnorm, IUnorm + { + /// + /// The red component. + /// + public byte R; + + /// + /// Initializes a new instance of the struct. + /// + /// The red component. + public R8(byte r) + { + R = r; + } + + /// + /// Gets or sets the packed representation of the Rgba32 struct. + /// + public byte PackedValue + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + set => Unsafe.As(ref this) = value; + } + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(R8 left, R8 right) => left.Equals(right); + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is not equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(R8 left, R8 right) => !left.Equals(right); + + /// + public override readonly bool Equals(object obj) => obj is R8 R8 && Equals(R8); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly bool Equals(R8 other) => PackedValue.Equals(other.PackedValue); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override readonly int GetHashCode() => PackedValue.GetHashCode(); + + /// + public override readonly string ToString() => $"{nameof(R8)}({this.R})"; + } +} diff --git a/src/ComputeSharp.Core/PixelTypes/Rg16.cs b/src/ComputeSharp.Core/PixelTypes/Rg16.cs new file mode 100644 index 00000000..d81b2313 --- /dev/null +++ b/src/ComputeSharp.Core/PixelTypes/Rg16.cs @@ -0,0 +1,89 @@ +using System; +using System.Numerics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ComputeSharp.__Internals; + +#pragma warning disable CS0618 + +namespace ComputeSharp +{ + /// + /// Packed pixel type containing two 8-bit unsigned normalized values ranging from 0 to 255. + /// + /// Ranges from [0, 0, 0, 0] to [1, 1, 0, 0] in vector form. + /// + /// + [StructLayout(LayoutKind.Sequential)] + public struct Rg16 : IEquatable, IUnorm, IUnorm, IUnorm + { + /// + /// The red component. + /// + public byte R; + + /// + /// The green component. + /// + public byte G; + + /// + /// Initializes a new instance of the struct. + /// + /// The x-component + /// The y-component + public Rg16(byte r, byte g) + { + R = r; + G = g; + } + + /// + /// Gets or sets the packed representation of the Rg16 struct. + /// + public ushort PackedValue + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + set => Unsafe.As(ref this) = value; + } + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(Rg16 left, Rg16 right) => left.Equals(right); + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is not equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(Rg16 left, Rg16 right) => !left.Equals(right); + + /// + public override readonly bool Equals(object obj) => obj is Rg16 rgba32 && Equals(rgba32); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly bool Equals(Rg16 other) => PackedValue.Equals(other.PackedValue); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override readonly int GetHashCode() => PackedValue.GetHashCode(); + + /// + public override readonly string ToString() => $"{nameof(Rg16)}({this.R}, {this.G})"; + } +} diff --git a/src/ComputeSharp.Core/PixelTypes/Rg32.cs b/src/ComputeSharp.Core/PixelTypes/Rg32.cs new file mode 100644 index 00000000..872f07e0 --- /dev/null +++ b/src/ComputeSharp.Core/PixelTypes/Rg32.cs @@ -0,0 +1,89 @@ +using System; +using System.Numerics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ComputeSharp.__Internals; + +#pragma warning disable CS0618 + +namespace ComputeSharp +{ + /// + /// Packed pixel type containing two 16-bit unsigned normalized values ranging from 0 to 255. + /// + /// Ranges from [0, 0, 0, 0] to [1, 1, 0, 0] in vector form. + /// + /// + [StructLayout(LayoutKind.Sequential)] + public struct Rg32 : IEquatable, IUnorm, IUnorm, IUnorm + { + /// + /// The red component. + /// + public ushort R; + + /// + /// The green component. + /// + public ushort G; + + /// + /// Initializes a new instance of the struct. + /// + /// The x-component + /// The y-component + public Rg32(ushort r, ushort g) + { + R = r; + G = g; + } + + /// + /// Gets or sets the packed representation of the Rg32 struct. + /// + public uint PackedValue + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + set => Unsafe.As(ref this) = value; + } + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(Rg32 left, Rg32 right) => left.Equals(right); + + /// + /// Compares two objects for equality. + /// + /// The on the left side of the operand. + /// The on the right side of the operand. + /// + /// True if the parameter is not equal to the parameter; otherwise, false. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(Rg32 left, Rg32 right) => !left.Equals(right); + + /// + public override readonly bool Equals(object obj) => obj is Rg32 rgba32 && Equals(rgba32); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly bool Equals(Rg32 other) => PackedValue.Equals(other.PackedValue); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override readonly int GetHashCode() => PackedValue.GetHashCode(); + + /// + public override readonly string ToString() => $"{nameof(Rg32)}({this.R}, {this.G})"; + } +} diff --git a/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs b/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs index 87596774..6b7319d5 100644 --- a/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs @@ -41,6 +41,9 @@ namespace ComputeSharp.Graphics.Helpers else if (typeof(T) == typeof(Bgra32)) return DXGI_FORMAT_B8G8R8A8_UNORM; else if (typeof(T) == typeof(Rgba32)) return DXGI_FORMAT_R8G8B8A8_UNORM; else if (typeof(T) == typeof(Rgba64)) return DXGI_FORMAT_R16G16B16A16_UNORM; + else if (typeof(T) == typeof(R8)) return DXGI_FORMAT_R8_UNORM; + else if (typeof(T) == typeof(Rg16)) return DXGI_FORMAT_R8G8_UNORM; + else if (typeof(T) == typeof(Rg32)) return DXGI_FORMAT_R16G16_UNORM; else return ThrowHelper.ThrowArgumentException("Invalid texture type"); } }