(MAJOR) Improvied sampling performance by removing the need to copy region data to a buffer first

This commit is contained in:
DarthAffe committed 2023-04-23 17:19:51 +02:00
1 parent af3989aa73
commit 586734b44a
12 files changed
+176 -169

No files matched your search

@@ -1,4 +1,6 @@
using System;
// ReSharper disable InconsistentNaming
using System;
namespace RGB.NET.Core.Tests.Helper;
@@ -342,13 +344,4 @@ public static class SimplexNoise
float v = h < 4 ? y : (h == 12) || (h == 14) ? x : z; // Fix repeats at h = 12 to 15
return ((h & 1) != 0 ? -u : u) + ((h & 2) != 0 ? -v : v);
}
private static float Grad(int hash, float x, float y, float z, float t)
{
int h = hash & 31; // Convert low 5 bits of hash code into 32 simple
float u = h < 24 ? x : y; // gradient directions, and compute dot product.
float v = h < 16 ? y : z;
float w = h < 8 ? z : t;
return ((h & 1) != 0 ? -u : u) + ((h & 2) != 0 ? -v : v) + ((h & 4) != 0 ? -w : w);
}
}
@@ -15,11 +15,11 @@ public class AverageColorSamplerTest
data.Fill(new Core.Color(1f, 1f, 1f, 1f));
Core.Color[] result = new Core.Color[1];
SamplerInfo<Core.Color> info = new(2, 3, data[..6]);
SamplerInfo<Core.Color> info = new(0, 0, 2, 3, 16, 1, data);
new AverageColorSampler().Sample(info, result);
Assert.AreEqual(new Core.Color(1f, 1f, 1f, 1f), result[0]);
info = new SamplerInfo<Core.Color>(16, 16, data);
info = new SamplerInfo<Core.Color>(0, 0, 16, 16, 16, 1, data);
new AverageColorSampler().Sample(info, result);
Assert.AreEqual(new Core.Color(1f, 1f, 1f, 1f), result[0]);
}
@@ -31,11 +31,11 @@ public class AverageColorSamplerTest
data.Fill(new Core.Color(1f, 0f, 0f, 0f));
Core.Color[] result = new Core.Color[1];
SamplerInfo<Core.Color> info = new(2, 3, data[..6]);
SamplerInfo<Core.Color> info = new(0, 0, 2, 3, 16, 1, data);
new AverageColorSampler().Sample(info, result);
Assert.AreEqual(new Core.Color(1f, 0f, 0f, 0f), result[0]);
info = new SamplerInfo<Core.Color>(16, 16, data);
info = new SamplerInfo<Core.Color>(0, 0, 16, 16, 16, 1, data);
new AverageColorSampler().Sample(info, result);
Assert.AreEqual(new Core.Color(1f, 0f, 0f, 0f), result[0]);
}
@@ -48,11 +48,11 @@ public class AverageColorSamplerTest
data[i] = (i % 2) == 0 ? new Core.Color(1f, 0f, 0f, 0f) : new Core.Color(1f, 1f, 1f, 1f);
Core.Color[] result = new Core.Color[1];
SamplerInfo<Core.Color> info = new(2, 3, data[..6]);
SamplerInfo<Core.Color> info = new(0, 0, 2, 3, 16, 1, data);
new AverageColorSampler().Sample(info, result);
Assert.AreEqual(new Core.Color(1f, 0.5f, 0.5f, 0.5f), result[0]);
info = new SamplerInfo<Core.Color>(16, 16, data);
info = new SamplerInfo<Core.Color>(0, 0, 16, 16, 16, 1, data);
new AverageColorSampler().Sample(info, result);
Assert.AreEqual(new Core.Color(1f, 0.5f, 0.5f, 0.5f), result[0]);
}
@@ -72,11 +72,11 @@ public class AverageColorSamplerTest
};
Core.Color[] result = new Core.Color[1];
SamplerInfo<Core.Color> info = new(2, 3, data[..6]);
SamplerInfo<Core.Color> info = new(0, 0, 2, 3, 2, 1, data[..6]);
new AverageColorSampler().Sample(info, result);
Assert.AreEqual(new Core.Color(0.5833333f, 0.5f, 0.291666657f, 0.25f), result[0]);
info = new SamplerInfo<Core.Color>(16, 16, data);
info = new SamplerInfo<Core.Color>(0, 0, 16, 16, 16, 1, data);
new AverageColorSampler().Sample(info, result);
Assert.AreEqual(new Core.Color(0.5019531f, 0.40234375f, 0.3486328f, 0.298828125f), result[0]);
}
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RGB.NET.Core.Tests.Helper;
@@ -42,7 +41,6 @@ public class PixelTextureTest
Core.Color[] data = new Core.Color[SIZE * SIZE];
SimplexNoise.Seed = 1872;
Random random = new(1872);
for (int y = 0; y < SIZE; y++)
for (int x = 0; x < SIZE; x++)
data[(y * SIZE) + x] = HSVColor.Create(SimplexNoise.CalcPixel2D(x, y, 1f / SIZE) * 360, 1, 1);
@@ -27,11 +27,15 @@ public class AverageByteSamplerTest
data[index++] = colorData[i].GetB();
}
SamplerInfo<byte> info = new(2, 3, data[..(6 * 4)]);
SamplerInfo<byte> info = new(0, 0, 2, 3, 16, 4, data);
new AverageByteSampler().Sample(info, result);
Assert.AreEqual(new Color(1f, 1f, 1f, 1f), new Color(result[0], result[1], result[2], result[3]));
info = new SamplerInfo<byte>(16, 16, data);
info = new SamplerInfo<byte>(0, 0, 13, 13, 16, 4, data);
new AverageByteSampler().Sample(info, result);
Assert.AreEqual(new Color(1f, 1f, 1f, 1f), new Color(result[0], result[1], result[2], result[3]));
info = new SamplerInfo<byte>(0, 0, 16, 16, 16, 4, data);
new AverageByteSampler().Sample(info, result);
Assert.AreEqual(new Color(1f, 1f, 1f, 1f), new Color(result[0], result[1], result[2], result[3]));
}
@@ -53,11 +57,15 @@ public class AverageByteSamplerTest
data[index++] = colorData[i].GetB();
}
SamplerInfo<byte> info = new(2, 3, data[..(6 * 4)]);
SamplerInfo<byte> info = new(0, 0, 2, 3, 16, 4, data);
new AverageByteSampler().Sample(info, result);
Assert.AreEqual(new Color(1f, 0f, 0f, 0f), new Color(result[0], result[1], result[2], result[3]));
info = new SamplerInfo<byte>(16, 16, data);
info = new SamplerInfo<byte>(0, 0, 13, 13, 16, 4, data);
new AverageByteSampler().Sample(info, result);
Assert.AreEqual(new Color(1f, 0f, 0f, 0f), new Color(result[0], result[1], result[2], result[3]));
info = new SamplerInfo<byte>(0, 0, 16, 16, 16, 4, data);
new AverageByteSampler().Sample(info, result);
Assert.AreEqual(new Color(1f, 0f, 0f, 0f), new Color(result[0], result[1], result[2], result[3]));
}
@@ -80,11 +88,15 @@ public class AverageByteSamplerTest
data[index++] = colorData[i].GetB();
}
SamplerInfo<byte> info = new(2, 3, data[..(6 * 4)]);
SamplerInfo<byte> info = new(0, 0, 2, 3, 16, 4, data);
new AverageByteSampler().Sample(info, result);
Assert.AreEqual(new Color(1f, 0.5f, 0.5f, 0.5f), new Color(result[0], result[1], result[2], result[3]));
info = new SamplerInfo<byte>(16, 16, data);
info = new SamplerInfo<byte>(0, 0, 13, 13, 16, 4, data);
new AverageByteSampler().Sample(info, result);
Assert.AreEqual(new Color(1f, (6f / 13f).GetByteValueFromPercentage(), (6f / 13f).GetByteValueFromPercentage(), (6f / 13f).GetByteValueFromPercentage()), new Color(result[0], result[1], result[2], result[3]));
info = new SamplerInfo<byte>(0, 0, 16, 16, 16, 4, data);
new AverageByteSampler().Sample(info, result);
Assert.AreEqual(new Color(1f, 0.5f, 0.5f, 0.5f), new Color(result[0], result[1], result[2], result[3]));
}
@@ -114,11 +126,11 @@ public class AverageByteSamplerTest
data[index++] = colorData[i].GetB();
}
SamplerInfo<byte> info = new(2, 3, data[..(6 * 4)]);
SamplerInfo<byte> info = new(0, 0, 2, 3, 2, 4, data[..(6 * 4)]);
new AverageByteSampler().Sample(info, result);
Assert.AreEqual(new Color(149, 128, 74, 64), new Color(result[0], result[1], result[2], result[3]));
info = new SamplerInfo<byte>(16, 16, data);
info = new SamplerInfo<byte>(0, 0, 16, 16, 16, 4, data);
new AverageByteSampler().Sample(info, result);
Assert.AreEqual(new Color(128, 103, 89, 76), new Color(result[0], result[1], result[2], result[3]));
}
@@ -27,11 +27,15 @@ public class AverageFloatSamplerTest
data[index++] = colorData[i].B;
}
SamplerInfo<float> info = new(2, 3, data[..(6 * 4)]);
SamplerInfo<float> info = new(0, 0, 2, 3, 16, 4, data);
new AverageFloatSampler().Sample(info, result);
Assert.AreEqual(new Color(1f, 1f, 1f, 1f), new Color(result[0], result[1], result[2], result[3]));
info = new SamplerInfo<float>(16, 16, data);
info = new SamplerInfo<float>(0, 0, 13, 13, 16, 4, data);
new AverageFloatSampler().Sample(info, result);
Assert.AreEqual(new Color(1f, 1f, 1f, 1f), new Color(result[0], result[1], result[2], result[3]));
info = new SamplerInfo<float>(0, 0, 16, 16, 16, 4, data);
new AverageFloatSampler().Sample(info, result);
Assert.AreEqual(new Color(1f, 1f, 1f, 1f), new Color(result[0], result[1], result[2], result[3]));
}
@@ -53,11 +57,15 @@ public class AverageFloatSamplerTest
data[index++] = colorData[i].B;
}
SamplerInfo<float> info = new(2, 3, data[..(6 * 4)]);
SamplerInfo<float> info = new(0, 0, 2, 3, 16, 4, data);
new AverageFloatSampler().Sample(info, result);
Assert.AreEqual(new Color(1f, 0f, 0f, 0f), new Color(result[0], result[1], result[2], result[3]));
info = new SamplerInfo<float>(16, 16, data);
info = new SamplerInfo<float>(0, 0, 13, 13, 16, 4, data);
new AverageFloatSampler().Sample(info, result);
Assert.AreEqual(new Color(1f, 0f, 0f, 0f), new Color(result[0], result[1], result[2], result[3]));
info = new SamplerInfo<float>(0, 0, 16, 16, 16, 4, data);
new AverageFloatSampler().Sample(info, result);
Assert.AreEqual(new Color(1f, 0f, 0f, 0f), new Color(result[0], result[1], result[2], result[3]));
}
@@ -80,11 +88,15 @@ public class AverageFloatSamplerTest
data[index++] = colorData[i].B;
}
SamplerInfo<float> info = new(2, 3, data[..(6 * 4)]);
SamplerInfo<float> info = new(0, 0, 2, 3, 16, 4, data);
new AverageFloatSampler().Sample(info, result);
Assert.AreEqual(new Color(1f, 0.5f, 0.5f, 0.5f), new Color(result[0], result[1], result[2], result[3]));
info = new SamplerInfo<float>(16, 16, data);
info = new SamplerInfo<float>(0, 0, 13, 13, 16, 4, data);
new AverageFloatSampler().Sample(info, result);
Assert.AreEqual(new Color(1f, 6f / 13f, 6f / 13f, 6f / 13f), new Color(result[0], result[1], result[2], result[3]));
info = new SamplerInfo<float>(0, 0, 16, 16, 16, 4, data);
new AverageFloatSampler().Sample(info, result);
Assert.AreEqual(new Color(1f, 0.5f, 0.5f, 0.5f), new Color(result[0], result[1], result[2], result[3]));
}
@@ -114,11 +126,11 @@ public class AverageFloatSamplerTest
data[index++] = colorData[i].B;
}
SamplerInfo<float> info = new(2, 3, data[..(6 * 4)]);
SamplerInfo<float> info = new(0, 0, 2, 3, 2, 4, data[..(6 * 4)]);
new AverageFloatSampler().Sample(info, result);
Assert.AreEqual(new Color(0.5833333f, 0.5f, 0.291666657f, 0.25f), new Color(result[0], result[1], result[2], result[3]));
info = new SamplerInfo<float>(16, 16, data);
info = new SamplerInfo<float>(0, 0, 16, 16, 16, 4, data);
new AverageFloatSampler().Sample(info, result);
Assert.AreEqual(new Color(0.5019531f, 0.40234375f, 0.3486328f, 0.298828125f), new Color(result[0], result[1], result[2], result[3]));
}