Add IPixel<T, TPixel>.ToPixel() API, minor code refactoring

このコミットが含まれているのは:
Sergio Pedri committed 2022-01-08 19:56:45 +01:00
1 親 c72b0c7fd2
コミット e0cae90832
10ファイル変更
+97 -109

検索条件に一致するファイルはありません

+20 -9
ファイルの表示
@@ -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<Bgra32>, IPixel<Bgra32, Float4>
[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;
}
/// <summary>
@@ -64,10 +65,10 @@ public struct Bgra32 : IEquatable<Bgra32>, IPixel<Bgra32, Float4>
[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;
}
/// <summary>
@@ -76,12 +77,22 @@ public struct Bgra32 : IEquatable<Bgra32>, IPixel<Bgra32, Float4>
public uint PackedValue
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get => Unsafe.As<Bgra32, uint>(ref Unsafe.AsRef(this));
readonly get => Unsafe.As<Bgra32, uint>(ref Unsafe.AsRef(in this));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Unsafe.As<Bgra32, uint>(ref this) = value;
}
/// <inheritdoc/>
[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;
}
/// <summary>
/// Compares two <see cref="Bgra32"/> objects for equality.
/// </summary>
+10
ファイルの表示
@@ -9,4 +9,14 @@ public interface IPixel<T, TPixel>
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged
{
/// <summary>
/// Converts the current <typeparamref name="T"/> value into its normalized <typeparamref name="TPixel"/> representation.
/// </summary>
/// <returns>The <typeparamref name="TPixel"/> representation for the current value.</returns>
/// <remarks>
/// This method is primarily meant to be used to support
/// <see cref="ComputeContextExtensions.Fill{TPixel}(in ComputeContext, IReadWriteTexture2D{TPixel}, TPixel)"/>
/// and <see cref="ComputeContextExtensions.Fill{TPixel}(in ComputeContext, IReadWriteTexture3D{TPixel}, TPixel)"/>.
/// </remarks>
TPixel ToPixel();
}
+9 -2
ファイルの表示
@@ -27,7 +27,7 @@ public struct R16 : IEquatable<R16>, IPixel<R16, float>
/// <param name="r">The red component.</param>
public R16(ushort r)
{
R = r;
this.R = r;
}
/// <summary>
@@ -36,12 +36,19 @@ public struct R16 : IEquatable<R16>, IPixel<R16, float>
public ushort PackedValue
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get => Unsafe.As<R16, ushort>(ref Unsafe.AsRef(this));
readonly get => Unsafe.As<R16, ushort>(ref Unsafe.AsRef(in this));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Unsafe.As<R16, ushort>(ref this) = value;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly float ToPixel()
{
return this.R / (float)ushort.MaxValue;
}
/// <summary>
/// Compares two <see cref="R16"/> objects for equality.
/// </summary>
+9 -2
ファイルの表示
@@ -27,7 +27,7 @@ public struct R8 : IEquatable<R8>, IPixel<R8, float>
/// <param name="r">The red component.</param>
public R8(byte r)
{
R = r;
this.R = r;
}
/// <summary>
@@ -36,12 +36,19 @@ public struct R8 : IEquatable<R8>, IPixel<R8, float>
public byte PackedValue
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get => Unsafe.As<R8, byte>(ref Unsafe.AsRef(this));
readonly get => Unsafe.As<R8, byte>(ref Unsafe.AsRef(in this));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Unsafe.As<R8, byte>(ref this) = value;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly float ToPixel()
{
return this.R / (float)byte.MaxValue;
}
/// <summary>
/// Compares two <see cref="R8"/> objects for equality.
/// </summary>
+10 -3
ファイルの表示
@@ -33,8 +33,8 @@ public struct Rg16 : IEquatable<Rg16>, IPixel<Rg16, Float2>
/// <param name="g">The green component.</param>
public Rg16(byte r, byte g)
{
R = r;
G = g;
this.R = r;
this.G = g;
}
/// <summary>
@@ -43,12 +43,19 @@ public struct Rg16 : IEquatable<Rg16>, IPixel<Rg16, Float2>
public ushort PackedValue
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get => Unsafe.As<Rg16, ushort>(ref Unsafe.AsRef(this));
readonly get => Unsafe.As<Rg16, ushort>(ref Unsafe.AsRef(in this));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Unsafe.As<Rg16, ushort>(ref this) = value;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Float2 ToPixel()
{
return new(this.R / (float)byte.MaxValue, this.G / (float)byte.MaxValue);
}
/// <summary>
/// Compares two <see cref="Rg16"/> objects for equality.
/// </summary>
+10 -3
ファイルの表示
@@ -33,8 +33,8 @@ public struct Rg32 : IEquatable<Rg32>, IPixel<Rg32, Float2>
/// <param name="g">The green component.</param>
public Rg32(ushort r, ushort g)
{
R = r;
G = g;
this.R = r;
this.G = g;
}
/// <summary>
@@ -43,12 +43,19 @@ public struct Rg32 : IEquatable<Rg32>, IPixel<Rg32, Float2>
public uint PackedValue
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get => Unsafe.As<Rg32, uint>(ref Unsafe.AsRef(this));
readonly get => Unsafe.As<Rg32, uint>(ref Unsafe.AsRef(in this));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Unsafe.As<Rg32, uint>(ref this) = value;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Float2 ToPixel()
{
return new(this.R / (float)ushort.MaxValue, this.G / (float)ushort.MaxValue);
}
/// <summary>
/// Compares two <see cref="Rg32"/> objects for equality.
/// </summary>
+12 -1
ファイルの表示
@@ -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<Rgba32>, IPixel<Rgba32, Float4>
public uint PackedValue
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get => Unsafe.As<Rgba32, uint>(ref Unsafe.AsRef(this));
readonly get => Unsafe.As<Rgba32, uint>(ref Unsafe.AsRef(in this));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Unsafe.As<Rgba32, uint>(ref this) = value;
}
/// <inheritdoc/>
[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;
}
/// <summary>
/// Compares two <see cref="Rgba32"/> objects for equality.
/// </summary>
+12 -1
ファイルの表示
@@ -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<Rgba64>, IPixel<Rgba64, Float4>
public ulong PackedValue
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get => Unsafe.As<Rgba64, ulong>(ref Unsafe.AsRef(this));
readonly get => Unsafe.As<Rgba64, ulong>(ref Unsafe.AsRef(in this));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => Unsafe.As<Rgba64, ulong>(ref this) = value;
}
/// <inheritdoc/>
[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;
}
/// <summary>
/// Compares two <see cref="Rgba64"/> objects for equality.
/// </summary>
+1 -84
ファイルの表示
@@ -95,7 +95,7 @@ internal static class DXGIFormatHelper
/// <typeparam name="T">The input pixel value to convert.</typeparam>
/// <returns>The <see cref="Float4"/> equivalent value for <paramref name="value"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Float4 ExtendToNormalizedValue<T>(ref T value)
public static Float4 ExtendToNormalizedValue<T>(T value)
where T : unmanaged
{
if (typeof(T) == typeof(Float4))
@@ -109,87 +109,4 @@ internal static class DXGIFormatHelper
return result;
}
/// <summary>
/// Converts a given value to a normalized value.
/// </summary>
/// <typeparam name="T">The input value to convert.</typeparam>
/// <returns>The normalized value for <paramref name="value"/>.</returns>
/// <exception cref="System.ArgumentException">Thrown when the input type <typeparamref name="T"/> is not supported.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Float4 ConvertToNormalizedValue<T>(ref T value)
where T : unmanaged
{
if (typeof(T) == typeof(Bgra32))
{
ref Bgra32 bgra = ref Unsafe.As<T, Bgra32>(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<T, Rgba32>(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<T, Rgba64>(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<T, R8>(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<T, R16>(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<T, Rg16>(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<T, Rg32>(ref value);
Vector4 linear = new(rg.R, rg.G, 0, 0);
Vector4 normalized = linear / ushort.MaxValue;
return normalized;
}
return ThrowHelper.ThrowArgumentException<Float4>("Invalid pixel type");
}
}
+4 -4
ファイルの表示
@@ -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()));
}
/// <summary>
@@ -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()));
}
/// <summary>
@@ -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));
}
/// <summary>
@@ -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));
}
/// <summary>