diff --git a/src/ComputeSharp.Core/PixelTypes/Bgra32.cs b/src/ComputeSharp.Core/PixelTypes/Bgra32.cs index faa0f907..1b3f7ea8 100644 --- a/src/ComputeSharp.Core/PixelTypes/Bgra32.cs +++ b/src/ComputeSharp.Core/PixelTypes/Bgra32.cs @@ -16,6 +16,7 @@ namespace ComputeSharp /// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form. /// /// + /// This struct is fully mutable. [StructLayout(LayoutKind.Sequential)] public struct Bgra32 : IEquatable, IUnorm, IUnorm, IUnorm { @@ -108,9 +109,11 @@ namespace ComputeSharp public override readonly bool Equals(object? obj) => obj is Bgra32 other && Equals(other); /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public readonly bool Equals(Bgra32 other) => PackedValue.Equals(other.PackedValue); /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public override readonly int GetHashCode() => PackedValue.GetHashCode(); /// diff --git a/src/ComputeSharp.Core/PixelTypes/Rgba32.cs b/src/ComputeSharp.Core/PixelTypes/Rgba32.cs new file mode 100644 index 00000000..a288d440 --- /dev/null +++ b/src/ComputeSharp.Core/PixelTypes/Rgba32.cs @@ -0,0 +1,121 @@ +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 four 8-bit unsigned normalized values ranging from 0 to 255. + /// The color components are stored in red, green, blue, and alpha order (least significant to most significant byte). + /// + /// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form. + /// + /// + /// This struct is fully mutable. + [StructLayout(LayoutKind.Sequential)] + public struct Rgba32 : IEquatable, IUnorm, IUnorm, IUnorm + { + /// + /// The red component. + /// + public byte R; + + /// + /// The green component. + /// + public byte G; + + /// + /// The blue component. + /// + public byte B; + + /// + /// The alpha component. + /// + public byte A; + + /// + /// Initializes a new instance of the struct. + /// + /// The red component. + /// The green component. + /// The blue component. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Rgba32(byte r, byte g, byte b) + { + this.R = r; + this.G = g; + this.B = b; + this.A = byte.MaxValue; + } + + /// + /// Initializes a new instance of the struct. + /// + /// The red component. + /// The green component. + /// The blue component. + /// The alpha component. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Rgba32(byte r, byte g, byte b, byte a) + { + this.R = r; + this.G = g; + this.B = b; + this.A = a; + } + + /// + /// Gets or sets the packed representation of the Rgba32 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 ==(Rgba32 left, Rgba32 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 !=(Rgba32 left, Rgba32 right) => !left.Equals(right); + + /// + public override readonly bool Equals(object obj) => obj is Rgba32 rgba32 && Equals(rgba32); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly bool Equals(Rgba32 other) => PackedValue.Equals(other.PackedValue); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override readonly int GetHashCode() => PackedValue.GetHashCode(); + + /// + public override readonly string ToString() => $"{nameof(Rgba32)}({this.R}, {this.G}, {this.B}, {this.A})"; + } +} diff --git a/src/ComputeSharp.Core/PixelTypes/Rgba64.cs b/src/ComputeSharp.Core/PixelTypes/Rgba64.cs new file mode 100644 index 00000000..794b85da --- /dev/null +++ b/src/ComputeSharp.Core/PixelTypes/Rgba64.cs @@ -0,0 +1,121 @@ +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 four 16-bit unsigned normalized values ranging from 0 to 255. + /// The color components are stored in red, green, blue, and alpha order (least significant to most significant byte). + /// + /// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form. + /// + /// + /// This struct is fully mutable. + [StructLayout(LayoutKind.Sequential)] + public struct Rgba64 : IEquatable, IUnorm, IUnorm, IUnorm + { + /// + /// The red component. + /// + public ushort R; + + /// + /// The green component. + /// + public ushort G; + + /// + /// The blue component. + /// + public ushort B; + + /// + /// The alpha component. + /// + public ushort A; + + /// + /// Initializes a new instance of the struct. + /// + /// The red component. + /// The green component. + /// The blue component. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Rgba64(ushort r, ushort g, ushort b) + { + this.R = r; + this.G = g; + this.B = b; + this.A = ushort.MaxValue; + } + + /// + /// Initializes a new instance of the struct. + /// + /// The red component. + /// The green component. + /// The blue component. + /// The alpha component. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Rgba64(ushort r, ushort g, ushort b, ushort a) + { + this.R = r; + this.G = g; + this.B = b; + this.A = a; + } + + /// + /// Gets or sets the packed representation of the Rgba64 struct. + /// + public ulong 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 ==(Rgba64 left, Rgba64 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 !=(Rgba64 left, Rgba64 right) => !left.Equals(right); + + /// + public override readonly bool Equals(object obj) => obj is Rgba64 rgba32 && Equals(rgba32); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly bool Equals(Rgba64 other) => PackedValue.Equals(other.PackedValue); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override readonly int GetHashCode() => PackedValue.GetHashCode(); + + /// + public override readonly string ToString() => $"{nameof(Rgba64)}({this.R}, {this.G}, {this.B}, {this.A})"; + } +} diff --git a/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs b/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs index 6260d27e..87596774 100644 --- a/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs @@ -39,6 +39,8 @@ namespace ComputeSharp.Graphics.Helpers else if (typeof(T) == typeof(Vector3)) return DXGI_FORMAT_R32G32B32_FLOAT; else if (typeof(T) == typeof(Vector4)) return DXGI_FORMAT_R32G32B32A32_FLOAT; 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 return ThrowHelper.ThrowArgumentException("Invalid texture type"); } }