Merge pull request #878 from Sergio0694/dev/cross-plat-conversions
Optimize pixel conversion methods with cross-plat intrinsics
このコミットが含まれているのは:
4ファイル変更
+112
-56
検索条件に一致するファイルはありません
@@ -1,9 +1,7 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Intrinsics;
|
||||
using System.Runtime.Intrinsics.X86;
|
||||
|
||||
namespace ComputeSharp;
|
||||
|
||||
@@ -86,25 +84,16 @@ public struct Bgra32 : IEquatable<Bgra32>, IPixel<Bgra32, Float4>, ISpanFormatta
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly Float4 ToPixel()
|
||||
{
|
||||
if (Sse2.IsSupported)
|
||||
{
|
||||
int pack = Unsafe.As<Bgra32, int>(ref Unsafe.AsRef(in this));
|
||||
Vector128<byte> vZero = Vector128<byte>.Zero;
|
||||
Vector128<byte> vByte = Vector128.CreateScalarUnsafe(pack).AsByte();
|
||||
Vector128<ushort> vUShort = Sse2.UnpackLow(vByte, vZero).AsUInt16();
|
||||
Vector128<int> vInt = Sse2.UnpackLow(vUShort, vZero.AsUInt16()).AsInt32();
|
||||
Vector128<int> vShuffle = Sse2.Shuffle(vInt, 0b11_00_01_10);
|
||||
Vector128<float> vFloat = Sse2.ConvertToVector128Single(vShuffle);
|
||||
Vector128<float> vMax = Vector128.Create((float)byte.MaxValue);
|
||||
Vector128<float> vNorm = Sse.Divide(vFloat, vMax);
|
||||
int pack = Unsafe.As<Bgra32, int>(ref Unsafe.AsRef(in this));
|
||||
Vector128<byte> vByte = Vector128.CreateScalarUnsafe(pack).AsByte();
|
||||
Vector128<ushort> vUShort = Vector128.WidenLower(vByte);
|
||||
Vector128<int> vInt = Vector128.WidenLower(vUShort).AsInt32();
|
||||
Vector128<int> vShuffle = Vector128.Shuffle(vInt, Vector128.Create(2, 1, 0, 3));
|
||||
Vector128<float> vFloat = Vector128.ConvertToSingle(vShuffle);
|
||||
Vector128<float> vMax = Vector128.Create((float)byte.MaxValue);
|
||||
Vector128<float> vNorm = Vector128.Divide(vFloat, vMax);
|
||||
|
||||
return vNorm.AsVector4();
|
||||
}
|
||||
|
||||
Vector4 linear = new(this.R, this.G, this.B, this.A);
|
||||
Vector4 normalized = linear / byte.MaxValue;
|
||||
|
||||
return normalized;
|
||||
return vNorm.AsVector4();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Intrinsics;
|
||||
using System.Runtime.Intrinsics.X86;
|
||||
|
||||
namespace ComputeSharp;
|
||||
|
||||
@@ -85,24 +83,15 @@ public struct Rgba32 : IEquatable<Rgba32>, IPixel<Rgba32, Float4>, ISpanFormatta
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly Float4 ToPixel()
|
||||
{
|
||||
if (Sse2.IsSupported)
|
||||
{
|
||||
int pack = Unsafe.As<Rgba32, int>(ref Unsafe.AsRef(in this));
|
||||
Vector128<byte> vZero = Vector128<byte>.Zero;
|
||||
Vector128<byte> vByte = Vector128.CreateScalarUnsafe(pack).AsByte();
|
||||
Vector128<ushort> vUShort = Sse2.UnpackLow(vByte, vZero).AsUInt16();
|
||||
Vector128<int> vInt = Sse2.UnpackLow(vUShort, vZero.AsUInt16()).AsInt32();
|
||||
Vector128<float> vFloat = Sse2.ConvertToVector128Single(vInt);
|
||||
Vector128<float> vMax = Vector128.Create((float)byte.MaxValue);
|
||||
Vector128<float> vNorm = Sse.Divide(vFloat, vMax);
|
||||
int pack = Unsafe.As<Rgba32, int>(ref Unsafe.AsRef(in this));
|
||||
Vector128<byte> vByte = Vector128.CreateScalarUnsafe(pack).AsByte();
|
||||
Vector128<ushort> vUShort = Vector128.WidenLower(vByte);
|
||||
Vector128<int> vInt = Vector128.WidenLower(vUShort).AsInt32();
|
||||
Vector128<float> vFloat = Vector128.ConvertToSingle(vInt);
|
||||
Vector128<float> vMax = Vector128.Create((float)byte.MaxValue);
|
||||
Vector128<float> vNorm = Vector128.Divide(vFloat, vMax);
|
||||
|
||||
return vNorm.AsVector4();
|
||||
}
|
||||
|
||||
Vector4 linear = new(this.R, this.G, this.B, this.A);
|
||||
Vector4 normalized = linear / byte.MaxValue;
|
||||
|
||||
return normalized;
|
||||
return vNorm.AsVector4();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Intrinsics;
|
||||
using System.Runtime.Intrinsics.X86;
|
||||
|
||||
namespace ComputeSharp;
|
||||
|
||||
@@ -85,22 +83,14 @@ public struct Rgba64 : IEquatable<Rgba64>, IPixel<Rgba64, Float4>, ISpanFormatta
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly Float4 ToPixel()
|
||||
{
|
||||
if (Sse2.IsSupported)
|
||||
{
|
||||
long pack = Unsafe.As<Rgba64, long>(ref Unsafe.AsRef(in this));
|
||||
Vector128<ushort> vUShort = Vector128.CreateScalarUnsafe(pack).AsUInt16();
|
||||
Vector128<int> vInt = Sse2.UnpackLow(vUShort, Vector128<ushort>.Zero).AsInt32();
|
||||
Vector128<float> vFloat = Sse2.ConvertToVector128Single(vInt);
|
||||
Vector128<float> vMax = Vector128.Create((float)ushort.MaxValue);
|
||||
Vector128<float> vNorm = Sse.Divide(vFloat, vMax);
|
||||
long pack = Unsafe.As<Rgba64, long>(ref Unsafe.AsRef(in this));
|
||||
Vector128<ushort> vUShort = Vector128.CreateScalarUnsafe(pack).AsUInt16();
|
||||
Vector128<int> vInt = Vector128.WidenLower(vUShort).AsInt32();
|
||||
Vector128<float> vFloat = Vector128.ConvertToSingle(vInt);
|
||||
Vector128<float> vMax = Vector128.Create((float)ushort.MaxValue);
|
||||
Vector128<float> vNorm = Vector128.Divide(vFloat, vMax);
|
||||
|
||||
return vNorm.AsVector4();
|
||||
}
|
||||
|
||||
Vector4 linear = new(this.R, this.G, this.B, this.A);
|
||||
Vector4 normalized = linear / ushort.MaxValue;
|
||||
|
||||
return normalized;
|
||||
return vNorm.AsVector4();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace ComputeSharp.Tests;
|
||||
|
||||
[TestClass]
|
||||
public partial class PixelTypeTests
|
||||
{
|
||||
[TestMethod]
|
||||
public unsafe void Bgra32_ToPixel()
|
||||
{
|
||||
Bgra32 color = new(0x20, 0x4A, 0x7C, 0xE4);
|
||||
|
||||
float4 pixel = color.ToPixel();
|
||||
|
||||
Assert.AreEqual(0.1254902f, pixel.R, delta: 0.0001f);
|
||||
Assert.AreEqual(0.2901961f, pixel.G, delta: 0.0001f);
|
||||
Assert.AreEqual(0.4862745f, pixel.B, delta: 0.0001f);
|
||||
Assert.AreEqual(0.89411765f, pixel.A, delta: 0.0001f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public unsafe void R16_ToPixel()
|
||||
{
|
||||
R16 color = new(0x2020);
|
||||
|
||||
float pixel = color.ToPixel();
|
||||
|
||||
Assert.AreEqual(0.1254902f, pixel, delta: 0.0001f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public unsafe void R8_ToPixel()
|
||||
{
|
||||
R8 color = new(0x20);
|
||||
|
||||
float pixel = color.ToPixel();
|
||||
|
||||
Assert.AreEqual(0.1254902f, pixel, delta: 0.0001f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public unsafe void Rg16_ToPixel()
|
||||
{
|
||||
Rg16 color = new(0x20, 0x4A);
|
||||
|
||||
float2 pixel = color.ToPixel();
|
||||
|
||||
Assert.AreEqual(0.1254902f, pixel.R, delta: 0.0001f);
|
||||
Assert.AreEqual(0.2901961f, pixel.G, delta: 0.0001f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public unsafe void Rg32_ToPixel()
|
||||
{
|
||||
Rg32 color = new(0x2020, 0x4A4A);
|
||||
|
||||
float2 pixel = color.ToPixel();
|
||||
|
||||
Assert.AreEqual(0.1254902f, pixel.R, delta: 0.0001f);
|
||||
Assert.AreEqual(0.2901961f, pixel.G, delta: 0.0001f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public unsafe void Rgba32_ToPixel()
|
||||
{
|
||||
Rgba32 color = new(0x20, 0x4A, 0x7C, 0xE4);
|
||||
|
||||
float4 pixel = color.ToPixel();
|
||||
|
||||
Assert.AreEqual(0.1254902f, pixel.R, delta: 0.0001f);
|
||||
Assert.AreEqual(0.2901961f, pixel.G, delta: 0.0001f);
|
||||
Assert.AreEqual(0.4862745f, pixel.B, delta: 0.0001f);
|
||||
Assert.AreEqual(0.89411765f, pixel.A, delta: 0.0001f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public unsafe void Rgba64_ToPixel()
|
||||
{
|
||||
Rgba64 color = new(0x2020, 0x4A4A, 0x7C7C, 0xE4E4);
|
||||
|
||||
float4 pixel = color.ToPixel();
|
||||
|
||||
Assert.AreEqual(0.1254902f, pixel.R, delta: 0.0001f);
|
||||
Assert.AreEqual(0.2901961f, pixel.G, delta: 0.0001f);
|
||||
Assert.AreEqual(0.4862745f, pixel.B, delta: 0.0001f);
|
||||
Assert.AreEqual(0.89411765f, pixel.A, delta: 0.0001f);
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする