mirror of
https://github.com/DarthAffe/RGB.NET.git
synced 2026-11-10 20:46:24 +00:00
(MAJOR) Improvied sampling performance by removing the need to copy region data to a buffer first
This commit is contained in:
1 parent
af3989aa73
commit
586734b44a
12 files changed
+176
-169
No files matched your search
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using RGB.NET.Core;
|
||||
using RGB.NET.Presets.Textures.Sampler;
|
||||
|
||||
@@ -59,6 +60,7 @@ public sealed class BytePixelTexture : PixelTexture<byte>
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override Color GetColor(in ReadOnlySpan<byte> pixel)
|
||||
{
|
||||
return ColorFormat switch
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using RGB.NET.Core;
|
||||
using RGB.NET.Presets.Textures.Sampler;
|
||||
|
||||
@@ -59,6 +60,7 @@ public sealed class FloatPixelTexture : PixelTexture<float>
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override Color GetColor(in ReadOnlySpan<float> pixel)
|
||||
{
|
||||
return ColorFormat switch
|
||||
|
||||
@@ -24,41 +24,48 @@ public sealed class AverageByteSampler : ISampler<byte>
|
||||
int count = info.Width * info.Height;
|
||||
if (count == 0) return;
|
||||
|
||||
ReadOnlySpan<byte> data = info.Data;
|
||||
|
||||
int dataLength = pixelData.Length;
|
||||
Span<uint> sums = stackalloc uint[dataLength];
|
||||
|
||||
if (Vector.IsHardwareAccelerated && (data.Length >= Vector<byte>.Count) && (dataLength <= Vector<byte>.Count))
|
||||
int elementsPerVector = Vector<byte>.Count / dataLength;
|
||||
int valuesPerVector = elementsPerVector * dataLength;
|
||||
if (Vector.IsHardwareAccelerated && (info.Height > 1) && (info.Width >= valuesPerVector) && (dataLength <= Vector<byte>.Count))
|
||||
{
|
||||
int elementsPerVector = Vector<byte>.Count / dataLength;
|
||||
int valuesPerVector = elementsPerVector * dataLength;
|
||||
|
||||
int chunks = data.Length / valuesPerVector;
|
||||
int missingElements = data.Length - (chunks * valuesPerVector);
|
||||
int chunks = info.Width / elementsPerVector;
|
||||
|
||||
Vector<uint> sum1 = Vector<uint>.Zero;
|
||||
Vector<uint> sum2 = Vector<uint>.Zero;
|
||||
Vector<uint> sum3 = Vector<uint>.Zero;
|
||||
Vector<uint> sum4 = Vector<uint>.Zero;
|
||||
|
||||
fixed (byte* colorPtr = &MemoryMarshal.GetReference(data))
|
||||
for (int y = 0; y < info.Height; y++)
|
||||
{
|
||||
byte* current = colorPtr;
|
||||
for (int i = 0; i < chunks; i++)
|
||||
ReadOnlySpan<byte> data = info[y];
|
||||
|
||||
fixed (byte* colorPtr = &MemoryMarshal.GetReference(data))
|
||||
{
|
||||
Vector<byte> bytes = *(Vector<byte>*)current;
|
||||
Vector.Widen(bytes, out Vector<ushort> short1, out Vector<ushort> short2);
|
||||
Vector.Widen(short1, out Vector<uint> int1, out Vector<uint> int2);
|
||||
Vector.Widen(short2, out Vector<uint> int3, out Vector<uint> int4);
|
||||
byte* current = colorPtr;
|
||||
for (int i = 0; i < chunks; i++)
|
||||
{
|
||||
Vector<byte> bytes = *(Vector<byte>*)current;
|
||||
Vector.Widen(bytes, out Vector<ushort> short1, out Vector<ushort> short2);
|
||||
Vector.Widen(short1, out Vector<uint> int1, out Vector<uint> int2);
|
||||
Vector.Widen(short2, out Vector<uint> int3, out Vector<uint> int4);
|
||||
|
||||
sum1 = Vector.Add(sum1, int1);
|
||||
sum2 = Vector.Add(sum2, int2);
|
||||
sum3 = Vector.Add(sum3, int3);
|
||||
sum4 = Vector.Add(sum4, int4);
|
||||
sum1 = Vector.Add(sum1, int1);
|
||||
sum2 = Vector.Add(sum2, int2);
|
||||
sum3 = Vector.Add(sum3, int3);
|
||||
sum4 = Vector.Add(sum4, int4);
|
||||
|
||||
current += valuesPerVector;
|
||||
current += valuesPerVector;
|
||||
}
|
||||
}
|
||||
|
||||
int missingElements = data.Length - (chunks * valuesPerVector);
|
||||
int offset = chunks * valuesPerVector;
|
||||
for (int i = 0; i < missingElements; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += data[offset + i + j];
|
||||
}
|
||||
|
||||
int value = 0;
|
||||
@@ -102,17 +109,16 @@ public sealed class AverageByteSampler : ISampler<byte>
|
||||
if (sumIndex >= dataLength)
|
||||
sumIndex = 0;
|
||||
}
|
||||
|
||||
int offset = chunks * valuesPerVector;
|
||||
for (int i = 0; i < missingElements; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += data[offset + i + j];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < data.Length; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += data[i + j];
|
||||
for (int y = 0; y < info.Height; y++)
|
||||
{
|
||||
ReadOnlySpan<byte> data = info[y];
|
||||
for (int i = 0; i < data.Length; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += data[i + j];
|
||||
}
|
||||
}
|
||||
|
||||
float divisor = count * byte.MaxValue;
|
||||
|
||||
@@ -18,45 +18,51 @@ public sealed class AverageFloatSampler : ISampler<float>
|
||||
int count = info.Width * info.Height;
|
||||
if (count == 0) return;
|
||||
|
||||
ReadOnlySpan<float> data = info.Data;
|
||||
|
||||
int dataLength = pixelData.Length;
|
||||
Span<float> sums = stackalloc float[dataLength];
|
||||
|
||||
if (Vector.IsHardwareAccelerated && (data.Length >= Vector<float>.Count) && (dataLength <= Vector<float>.Count))
|
||||
{
|
||||
int elementsPerVector = Vector<float>.Count / dataLength;
|
||||
int valuesPerVector = elementsPerVector * dataLength;
|
||||
|
||||
int chunks = data.Length / valuesPerVector;
|
||||
int missingElements = data.Length - (chunks * valuesPerVector);
|
||||
int elementsPerVector = Vector<float>.Count / dataLength;
|
||||
int valuesPerVector = elementsPerVector * dataLength;
|
||||
if (Vector.IsHardwareAccelerated && (info.Height > 1) && (info.Width >= valuesPerVector) && (dataLength <= Vector<float>.Count))
|
||||
{
|
||||
int chunks = info.Width / elementsPerVector;
|
||||
|
||||
Vector<float> sum = Vector<float>.Zero;
|
||||
|
||||
fixed (float* colorPtr = &MemoryMarshal.GetReference(data))
|
||||
for (int y = 0; y < info.Height; y++)
|
||||
{
|
||||
float* current = colorPtr;
|
||||
for (int i = 0; i < chunks; i++)
|
||||
ReadOnlySpan<float> data = info[y];
|
||||
|
||||
fixed (float* colorPtr = &MemoryMarshal.GetReference(data))
|
||||
{
|
||||
sum = Vector.Add(sum, *(Vector<float>*)current);
|
||||
current += valuesPerVector;
|
||||
float* current = colorPtr;
|
||||
for (int i = 0; i < chunks; i++)
|
||||
{
|
||||
sum = Vector.Add(sum, *(Vector<float>*)current);
|
||||
current += valuesPerVector;
|
||||
}
|
||||
}
|
||||
|
||||
int missingElements = data.Length - (chunks * valuesPerVector);
|
||||
int offset = chunks * valuesPerVector;
|
||||
for (int i = 0; i < missingElements; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += data[offset + i + j];
|
||||
}
|
||||
|
||||
for (int i = 0; i < valuesPerVector; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += sum[i + j];
|
||||
|
||||
int offset = chunks * valuesPerVector;
|
||||
for (int i = 0; i < missingElements; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += data[offset + i + j];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < data.Length; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += data[i + j];
|
||||
for (int y = 0; y < info.Height; y++)
|
||||
{
|
||||
ReadOnlySpan<float> data = info[y];
|
||||
for (int i = 0; i < data.Length; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += data[i + j];
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < pixelData.Length; i++)
|
||||
|
||||
Reference in new issue
Block a user