diff --git a/src/ComputeSharp/Core/Imaging/Bgra32.cs b/src/ComputeSharp/Core/Imaging/Bgra32.cs index 4a06092b..c1f1ab2a 100644 --- a/src/ComputeSharp/Core/Imaging/Bgra32.cs +++ b/src/ComputeSharp/Core/Imaging/Bgra32.cs @@ -1,4 +1,5 @@ using System; +using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -48,10 +49,10 @@ public struct Bgra32 : IEquatable, IPixel [MethodImpl(MethodImplOptions.AggressiveInlining)] public Bgra32(byte r, byte g, byte b) { - R = r; - G = g; - B = b; - A = byte.MaxValue; + this.R = r; + this.G = g; + this.B = b; + this.A = byte.MaxValue; } /// @@ -64,10 +65,10 @@ public struct Bgra32 : IEquatable, IPixel [MethodImpl(MethodImplOptions.AggressiveInlining)] public Bgra32(byte r, byte g, byte b, byte a) { - R = r; - G = g; - B = b; - A = a; + this.R = r; + this.G = g; + this.B = b; + this.A = a; } /// @@ -76,12 +77,22 @@ public struct Bgra32 : IEquatable, IPixel public uint PackedValue { [MethodImpl(MethodImplOptions.AggressiveInlining)] - readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + readonly get => Unsafe.As(ref Unsafe.AsRef(in this)); [MethodImpl(MethodImplOptions.AggressiveInlining)] set => Unsafe.As(ref this) = value; } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Float4 ToPixel() + { + Vector4 linear = new(this.R, this.G, this.B, this.A); + Vector4 normalized = linear / byte.MaxValue; + + return normalized; + } + /// /// Compares two objects for equality. /// diff --git a/src/ComputeSharp/Core/Imaging/Interfaces/IPixel{T,TPixel}.cs b/src/ComputeSharp/Core/Imaging/Interfaces/IPixel{T,TPixel}.cs index be3f2740..4c32dba5 100644 --- a/src/ComputeSharp/Core/Imaging/Interfaces/IPixel{T,TPixel}.cs +++ b/src/ComputeSharp/Core/Imaging/Interfaces/IPixel{T,TPixel}.cs @@ -9,4 +9,14 @@ public interface IPixel where T : unmanaged, IPixel where TPixel : unmanaged { + /// + /// Converts the current value into its normalized representation. + /// + /// The representation for the current value. + /// + /// This method is primarily meant to be used to support + /// + /// and . + /// + TPixel ToPixel(); } diff --git a/src/ComputeSharp/Core/Imaging/R16.cs b/src/ComputeSharp/Core/Imaging/R16.cs index 850d9222..f096e73b 100644 --- a/src/ComputeSharp/Core/Imaging/R16.cs +++ b/src/ComputeSharp/Core/Imaging/R16.cs @@ -27,7 +27,7 @@ public struct R16 : IEquatable, IPixel /// The red component. public R16(ushort r) { - R = r; + this.R = r; } /// @@ -36,12 +36,19 @@ public struct R16 : IEquatable, IPixel public ushort PackedValue { [MethodImpl(MethodImplOptions.AggressiveInlining)] - readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + readonly get => Unsafe.As(ref Unsafe.AsRef(in this)); [MethodImpl(MethodImplOptions.AggressiveInlining)] set => Unsafe.As(ref this) = value; } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly float ToPixel() + { + return this.R / (float)ushort.MaxValue; + } + /// /// Compares two objects for equality. /// diff --git a/src/ComputeSharp/Core/Imaging/R8.cs b/src/ComputeSharp/Core/Imaging/R8.cs index 7f95b8c4..e438980c 100644 --- a/src/ComputeSharp/Core/Imaging/R8.cs +++ b/src/ComputeSharp/Core/Imaging/R8.cs @@ -27,7 +27,7 @@ public struct R8 : IEquatable, IPixel /// The red component. public R8(byte r) { - R = r; + this.R = r; } /// @@ -36,12 +36,19 @@ public struct R8 : IEquatable, IPixel public byte PackedValue { [MethodImpl(MethodImplOptions.AggressiveInlining)] - readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + readonly get => Unsafe.As(ref Unsafe.AsRef(in this)); [MethodImpl(MethodImplOptions.AggressiveInlining)] set => Unsafe.As(ref this) = value; } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly float ToPixel() + { + return this.R / (float)byte.MaxValue; + } + /// /// Compares two objects for equality. /// diff --git a/src/ComputeSharp/Core/Imaging/Rg16.cs b/src/ComputeSharp/Core/Imaging/Rg16.cs index 0f1e9fc0..97baee2c 100644 --- a/src/ComputeSharp/Core/Imaging/Rg16.cs +++ b/src/ComputeSharp/Core/Imaging/Rg16.cs @@ -33,8 +33,8 @@ public struct Rg16 : IEquatable, IPixel /// The green component. public Rg16(byte r, byte g) { - R = r; - G = g; + this.R = r; + this.G = g; } /// @@ -43,12 +43,19 @@ public struct Rg16 : IEquatable, IPixel public ushort PackedValue { [MethodImpl(MethodImplOptions.AggressiveInlining)] - readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + readonly get => Unsafe.As(ref Unsafe.AsRef(in this)); [MethodImpl(MethodImplOptions.AggressiveInlining)] set => Unsafe.As(ref this) = value; } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Float2 ToPixel() + { + return new(this.R / (float)byte.MaxValue, this.G / (float)byte.MaxValue); + } + /// /// Compares two objects for equality. /// diff --git a/src/ComputeSharp/Core/Imaging/Rg32.cs b/src/ComputeSharp/Core/Imaging/Rg32.cs index f8fc621d..99742d81 100644 --- a/src/ComputeSharp/Core/Imaging/Rg32.cs +++ b/src/ComputeSharp/Core/Imaging/Rg32.cs @@ -33,8 +33,8 @@ public struct Rg32 : IEquatable, IPixel /// The green component. public Rg32(ushort r, ushort g) { - R = r; - G = g; + this.R = r; + this.G = g; } /// @@ -43,12 +43,19 @@ public struct Rg32 : IEquatable, IPixel public uint PackedValue { [MethodImpl(MethodImplOptions.AggressiveInlining)] - readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + readonly get => Unsafe.As(ref Unsafe.AsRef(in this)); [MethodImpl(MethodImplOptions.AggressiveInlining)] set => Unsafe.As(ref this) = value; } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Float2 ToPixel() + { + return new(this.R / (float)ushort.MaxValue, this.G / (float)ushort.MaxValue); + } + /// /// Compares two objects for equality. /// diff --git a/src/ComputeSharp/Core/Imaging/Rgba32.cs b/src/ComputeSharp/Core/Imaging/Rgba32.cs index 4a54a90c..5daab793 100644 --- a/src/ComputeSharp/Core/Imaging/Rgba32.cs +++ b/src/ComputeSharp/Core/Imaging/Rgba32.cs @@ -1,4 +1,5 @@ using System; +using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -75,12 +76,22 @@ public struct Rgba32 : IEquatable, IPixel public uint PackedValue { [MethodImpl(MethodImplOptions.AggressiveInlining)] - readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + readonly get => Unsafe.As(ref Unsafe.AsRef(in this)); [MethodImpl(MethodImplOptions.AggressiveInlining)] set => Unsafe.As(ref this) = value; } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Float4 ToPixel() + { + Vector4 linear = new(this.R, this.G, this.B, this.A); + Vector4 normalized = linear / byte.MaxValue; + + return normalized; + } + /// /// Compares two objects for equality. /// diff --git a/src/ComputeSharp/Core/Imaging/Rgba64.cs b/src/ComputeSharp/Core/Imaging/Rgba64.cs index 6655f2b4..59f6dd1d 100644 --- a/src/ComputeSharp/Core/Imaging/Rgba64.cs +++ b/src/ComputeSharp/Core/Imaging/Rgba64.cs @@ -1,4 +1,5 @@ using System; +using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -75,12 +76,22 @@ public struct Rgba64 : IEquatable, IPixel public ulong PackedValue { [MethodImpl(MethodImplOptions.AggressiveInlining)] - readonly get => Unsafe.As(ref Unsafe.AsRef(this)); + readonly get => Unsafe.As(ref Unsafe.AsRef(in this)); [MethodImpl(MethodImplOptions.AggressiveInlining)] set => Unsafe.As(ref this) = value; } + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Float4 ToPixel() + { + Vector4 linear = new(this.R, this.G, this.B, this.A); + Vector4 normalized = linear / ushort.MaxValue; + + return normalized; + } + /// /// Compares two objects for equality. /// diff --git a/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs b/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs index e14a142a..985f2271 100644 --- a/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs @@ -95,7 +95,7 @@ internal static class DXGIFormatHelper /// The input pixel value to convert. /// The equivalent value for . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Float4 ExtendToNormalizedValue(ref T value) + public static Float4 ExtendToNormalizedValue(T value) where T : unmanaged { if (typeof(T) == typeof(Float4)) @@ -109,87 +109,4 @@ internal static class DXGIFormatHelper return result; } - - /// - /// Converts a given value to a normalized value. - /// - /// The input value to convert. - /// The normalized value for . - /// Thrown when the input type is not supported. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Float4 ConvertToNormalizedValue(ref T value) - where T : unmanaged - { - if (typeof(T) == typeof(Bgra32)) - { - ref Bgra32 bgra = ref Unsafe.As(ref value); - - Vector4 linear = new(bgra.R, bgra.G, bgra.B, bgra.A); - Vector4 normalized = linear / byte.MaxValue; - - return normalized; - } - - if (typeof(T) == typeof(Rgba32)) - { - ref Rgba32 rgba = ref Unsafe.As(ref value); - - Vector4 linear = new(rgba.R, rgba.G, rgba.B, rgba.A); - Vector4 normalized = linear / byte.MaxValue; - - return normalized; - } - - if (typeof(T) == typeof(Rgba64)) - { - ref Rgba64 rgba = ref Unsafe.As(ref value); - - Vector4 linear = new(rgba.R, rgba.G, rgba.B, rgba.A); - Vector4 normalized = linear / ushort.MaxValue; - - return normalized; - } - - if (typeof(T) == typeof(R8)) - { - ref R8 r = ref Unsafe.As(ref value); - - Vector4 linear = new(r.R, 0, 0, 0); - Vector4 normalized = linear / byte.MaxValue; - - return normalized; - } - - if (typeof(T) == typeof(R16)) - { - ref R16 r = ref Unsafe.As(ref value); - - Vector4 linear = new(r.R, 0, 0, 0); - Vector4 normalized = linear / ushort.MaxValue; - - return normalized; - } - - if (typeof(T) == typeof(Rg16)) - { - ref Rg16 rg = ref Unsafe.As(ref value); - - Vector4 linear = new(rg.R, rg.G, 0, 0); - Vector4 normalized = linear / byte.MaxValue; - - return normalized; - } - - if (typeof(T) == typeof(Rg32)) - { - ref Rg32 rg = ref Unsafe.As(ref value); - - Vector4 linear = new(rg.R, rg.G, 0, 0); - Vector4 normalized = linear / ushort.MaxValue; - - return normalized; - } - - return ThrowHelper.ThrowArgumentException("Invalid pixel type"); - } } diff --git a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs index 131c035d..c2f3863f 100644 --- a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs +++ b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs @@ -215,7 +215,7 @@ public static partial class ComputeContextExtensions { var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); - context.Fill(texture.D3D12Resource, handles.Gpu, handles.Cpu, DXGIFormatHelper.ConvertToNormalizedValue(ref value)); + context.Fill(texture.D3D12Resource, handles.Gpu, handles.Cpu, DXGIFormatHelper.ExtendToNormalizedValue(value.ToPixel())); } /// @@ -232,7 +232,7 @@ public static partial class ComputeContextExtensions { var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); - context.Fill(texture.D3D12Resource, handles.Gpu, handles.Cpu, DXGIFormatHelper.ConvertToNormalizedValue(ref value)); + context.Fill(texture.D3D12Resource, handles.Gpu, handles.Cpu, DXGIFormatHelper.ExtendToNormalizedValue(value.ToPixel())); } /// @@ -251,7 +251,7 @@ public static partial class ComputeContextExtensions ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, - DXGIFormatHelper.ExtendToNormalizedValue(ref value)); + DXGIFormatHelper.ExtendToNormalizedValue(value)); } /// @@ -270,7 +270,7 @@ public static partial class ComputeContextExtensions ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, - DXGIFormatHelper.ExtendToNormalizedValue(ref value)); + DXGIFormatHelper.ExtendToNormalizedValue(value)); } ///