From 1ac1f6afdd3b792d37e75cf3d032b714c1a47cb7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 4 Jan 2021 19:35:11 +0100 Subject: [PATCH] Added R16 pixel type --- src/ComputeSharp.Core/PixelTypes/R16.cs | 81 +++++++++++++++++++ src/ComputeSharp.Core/PixelTypes/R8.cs | 2 +- src/ComputeSharp.Core/PixelTypes/Rg16.cs | 2 +- src/ComputeSharp.Core/PixelTypes/Rg32.cs | 2 +- src/ComputeSharp.Core/PixelTypes/Rgba32.cs | 2 +- src/ComputeSharp.Core/PixelTypes/Rgba64.cs | 2 +- .../Helpers/DXGIFormatHelper.cs | 1 + 7 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 src/ComputeSharp.Core/PixelTypes/R16.cs diff --git a/src/ComputeSharp.Core/PixelTypes/R16.cs b/src/ComputeSharp.Core/PixelTypes/R16.cs new file mode 100644 index 00000000..a9fccb92 --- /dev/null +++ b/src/ComputeSharp.Core/PixelTypes/R16.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 16-bit unsigned normalized channel value. + /// + /// Ranges from [0, 0, 0, 0] to [1, 0, 0, 0] in vector form. + /// + /// + [StructLayout(LayoutKind.Sequential)] + public struct R16 : IEquatable, IUnorm, IUnorm + { + /// + /// The red component. + /// + public ushort R; + + /// + /// Initializes a new instance of the struct. + /// + /// The red component. + public R16(ushort r) + { + R = r; + } + + /// + /// Gets or sets the packed representation of the 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 ==(R16 left, R16 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 !=(R16 left, R16 right) => !left.Equals(right); + + /// + public override readonly bool Equals(object obj) => obj is R16 R16 && Equals(R16); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly bool Equals(R16 other) => PackedValue.Equals(other.PackedValue); + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public override readonly int GetHashCode() => PackedValue.GetHashCode(); + + /// + public override readonly string ToString() => $"{nameof(R16)}({this.R})"; + } +} diff --git a/src/ComputeSharp.Core/PixelTypes/R8.cs b/src/ComputeSharp.Core/PixelTypes/R8.cs index 7403ab05..044c34f1 100644 --- a/src/ComputeSharp.Core/PixelTypes/R8.cs +++ b/src/ComputeSharp.Core/PixelTypes/R8.cs @@ -31,7 +31,7 @@ namespace ComputeSharp } /// - /// Gets or sets the packed representation of the Rgba32 struct. + /// Gets or sets the packed representation of the struct. /// public byte PackedValue { diff --git a/src/ComputeSharp.Core/PixelTypes/Rg16.cs b/src/ComputeSharp.Core/PixelTypes/Rg16.cs index d81b2313..5d6887f6 100644 --- a/src/ComputeSharp.Core/PixelTypes/Rg16.cs +++ b/src/ComputeSharp.Core/PixelTypes/Rg16.cs @@ -39,7 +39,7 @@ namespace ComputeSharp } /// - /// Gets or sets the packed representation of the Rg16 struct. + /// Gets or sets the packed representation of the struct. /// public ushort PackedValue { diff --git a/src/ComputeSharp.Core/PixelTypes/Rg32.cs b/src/ComputeSharp.Core/PixelTypes/Rg32.cs index 872f07e0..36bba66f 100644 --- a/src/ComputeSharp.Core/PixelTypes/Rg32.cs +++ b/src/ComputeSharp.Core/PixelTypes/Rg32.cs @@ -39,7 +39,7 @@ namespace ComputeSharp } /// - /// Gets or sets the packed representation of the Rg32 struct. + /// Gets or sets the packed representation of the struct. /// public uint PackedValue { diff --git a/src/ComputeSharp.Core/PixelTypes/Rgba32.cs b/src/ComputeSharp.Core/PixelTypes/Rgba32.cs index a288d440..e8787029 100644 --- a/src/ComputeSharp.Core/PixelTypes/Rgba32.cs +++ b/src/ComputeSharp.Core/PixelTypes/Rgba32.cs @@ -71,7 +71,7 @@ namespace ComputeSharp } /// - /// Gets or sets the packed representation of the Rgba32 struct. + /// Gets or sets the packed representation of the struct. /// public uint PackedValue { diff --git a/src/ComputeSharp.Core/PixelTypes/Rgba64.cs b/src/ComputeSharp.Core/PixelTypes/Rgba64.cs index 794b85da..835e8c30 100644 --- a/src/ComputeSharp.Core/PixelTypes/Rgba64.cs +++ b/src/ComputeSharp.Core/PixelTypes/Rgba64.cs @@ -71,7 +71,7 @@ namespace ComputeSharp } /// - /// Gets or sets the packed representation of the Rgba64 struct. + /// Gets or sets the packed representation of the struct. /// public ulong PackedValue { diff --git a/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs b/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs index 6b7319d5..c12698ae 100644 --- a/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/DXGIFormatHelper.cs @@ -42,6 +42,7 @@ namespace ComputeSharp.Graphics.Helpers 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(R16)) return DXGI_FORMAT_R16_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");