Performance tweaks
No files matched your search
@@ -30,8 +30,8 @@ public static class Program
|
|||||||
morph.SetDensity(2); // Higher than 1 sets fluid to 0.
|
morph.SetDensity(2); // Higher than 1 sets fluid to 0.
|
||||||
morph.SetMotion(InterpolationMethod.Spline);
|
morph.SetMotion(InterpolationMethod.Spline);
|
||||||
morph.SetFading(InterpolationMethod.Perlin);
|
morph.SetFading(InterpolationMethod.Perlin);
|
||||||
morph.SetThreads(8);
|
morph.SetThreads(80);
|
||||||
morph.SetCycleLength(100000);
|
morph.SetCycleLength(10000);
|
||||||
morph.SetFeather(0);
|
morph.SetFeather(0);
|
||||||
morph.SetKeepBackground(false);
|
morph.SetKeepBackground(false);
|
||||||
morph.SetFinite(true);
|
morph.SetFinite(true);
|
||||||
@@ -52,7 +52,6 @@ public static class Program
|
|||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
// Add your main logic here
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool LoadFiles(Morph morph, Options options)
|
private static bool LoadFiles(Morph morph, Options options)
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 27 KiB After Width: | Height: | Size: 27 KiB |
|
Before Width: | Height: | Size: 31 KiB After Width: | Height: | Size: 31 KiB |
|
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 33 KiB |
|
Before Width: | Height: | Size: 35 KiB After Width: | Height: | Size: 34 KiB |
|
Before Width: | Height: | Size: 36 KiB After Width: | Height: | Size: 36 KiB |
|
Before Width: | Height: | Size: 36 KiB After Width: | Height: | Size: 36 KiB |
|
Before Width: | Height: | Size: 35 KiB After Width: | Height: | Size: 35 KiB |
|
Before Width: | Height: | Size: 35 KiB After Width: | Height: | Size: 34 KiB |
|
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 33 KiB |
|
Before Width: | Height: | Size: 31 KiB After Width: | Height: | Size: 31 KiB |
|
Before Width: | Height: | Size: 29 KiB After Width: | Height: | Size: 28 KiB |
|
Before Width: | Height: | Size: 25 KiB After Width: | Height: | Size: 24 KiB |
|
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 19 KiB |
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 14 KiB |
|
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 21 KiB |
|
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 25 KiB |
|
Before Width: | Height: | Size: 27 KiB After Width: | Height: | Size: 27 KiB |
|
Before Width: | Height: | Size: 28 KiB After Width: | Height: | Size: 31 KiB |
|
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 30 KiB |
|
Before Width: | Height: | Size: 32 KiB After Width: | Height: | Size: 26 KiB |
|
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 22 KiB |
|
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 18 KiB |
|
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 15 KiB |
|
Before Width: | Height: | Size: 32 KiB After Width: | Height: | Size: 18 KiB |
|
Before Width: | Height: | Size: 31 KiB After Width: | Height: | Size: 23 KiB |
|
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 27 KiB |
|
Before Width: | Height: | Size: 29 KiB After Width: | Height: | Size: 29 KiB |
|
Before Width: | Height: | Size: 31 KiB After Width: | Height: | Size: 31 KiB |
|
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 28 KiB |
|
Before Width: | Height: | Size: 35 KiB After Width: | Height: | Size: 23 KiB |
|
Before Width: | Height: | Size: 37 KiB After Width: | Height: | Size: 18 KiB |
|
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 15 KiB |
|
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 17 KiB |
|
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 23 KiB |
|
Before Width: | Height: | Size: 37 KiB After Width: | Height: | Size: 28 KiB |
|
Before Width: | Height: | Size: 35 KiB After Width: | Height: | Size: 33 KiB |
|
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 35 KiB |
|
Before Width: | Height: | Size: 29 KiB After Width: | Height: | Size: 37 KiB |
|
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 39 KiB |
|
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 39 KiB |
|
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 39 KiB |
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 38 KiB |
|
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 36 KiB |
|
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 33 KiB |
|
Before Width: | Height: | Size: 26 KiB After Width: | Height: | Size: 31 KiB |
|
Before Width: | Height: | Size: 26 KiB After Width: | Height: | Size: 26 KiB |
@@ -268,7 +268,11 @@ public class AtomorphThread : IDisposable
|
|||||||
public void Start()
|
public void Start()
|
||||||
{
|
{
|
||||||
_running = true;
|
_running = true;
|
||||||
_stepThread = new Thread(Run) { Name = "AtomorphThread", };
|
_stepThread = new Thread(Run)
|
||||||
|
{
|
||||||
|
Name = "AtomorphThread",
|
||||||
|
IsBackground = true,
|
||||||
|
};
|
||||||
_stepThread.Start();
|
_stepThread.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -530,23 +534,24 @@ public class AtomorphThread : IDisposable
|
|||||||
#else
|
#else
|
||||||
private static object morph_mutex = new object();
|
private static object morph_mutex = new object();
|
||||||
|
|
||||||
private static void MorphAsync(Point[][] map, ulong width, ulong height, ulong repeat, uint rngSeed,
|
private class GainWrapper
|
||||||
ref double gain)
|
|
||||||
{
|
{
|
||||||
var random = new PcgRandom((int)rngSeed);
|
public double Gain { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
ulong y = (ulong)random.Next(0, (int)(height - 1));
|
private static async Task MorphAsync(Point[][] map, ulong width, ulong height, ulong repeat, Pcg32Single random,
|
||||||
|
GainWrapper gain)
|
||||||
|
{
|
||||||
|
ulong y = random.GenerateNext((uint)(height - 1));
|
||||||
ulong y_next = (y + 1) % (height);
|
ulong y_next = (y + 1) % (height);
|
||||||
ulong y_prev = (y > 0 ? y - 1 : height - 1);
|
ulong y_prev = (y > 0 ? y - 1 : height - 1);
|
||||||
|
|
||||||
for (ulong i = 0; i < repeat; ++i)
|
for (ulong i = 0; i < repeat; ++i)
|
||||||
{
|
{
|
||||||
ulong x1, x2;
|
ulong x1 = random.GenerateNext((uint)(width - 1));
|
||||||
x1 = (ulong)random.Next(0, (int)(width - 1));
|
ulong x2 = random.GenerateNext((uint)(width - 2));
|
||||||
do
|
if (x2 >= x1)
|
||||||
{
|
x2++;
|
||||||
x2 = (ulong)random.Next(0, (int)(width - 1));
|
|
||||||
} while (x1 == x2);
|
|
||||||
|
|
||||||
var x1_y_prev = map[x1][y_prev];
|
var x1_y_prev = map[x1][y_prev];
|
||||||
var x2_y_prev = map[x2][y_prev];
|
var x2_y_prev = map[x2][y_prev];
|
||||||
@@ -573,45 +578,52 @@ public class AtomorphThread : IDisposable
|
|||||||
{
|
{
|
||||||
if (map[x1][y].Word != x1_y.Word || map[x2][y].Word != x2_y.Word)
|
if (map[x1][y].Word != x1_y.Word || map[x2][y].Word != x2_y.Word)
|
||||||
continue;
|
continue;
|
||||||
var buf = map[x2][y];
|
(map[x2][y], map[x1][y]) = (map[x1][y], map[x2][y]);
|
||||||
map[x2][y] = map[x1][y];
|
gain.Gain += c1 - c2;
|
||||||
map[x1][y] = buf;
|
|
||||||
gain += c1 - c2;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Task[]? _morphWorkers = null;
|
||||||
|
private Pcg32Single[]? _morphRandoms = null;
|
||||||
private bool Morph()
|
private bool Morph()
|
||||||
{
|
{
|
||||||
var ch = _chains[_counter % _blobMapW];
|
var ch = _chains[_counter % _blobMapW];
|
||||||
if (ch.Width <= 1) return false;
|
if (ch.Width <= 1) return false;
|
||||||
|
|
||||||
var map = ch.Points;
|
var map = ch.Points!;
|
||||||
|
|
||||||
double gain = 0.0;
|
double gain = 0.0;
|
||||||
|
var gainWrapper = new GainWrapper { Gain = 0.0 };
|
||||||
if (_threadCount == 0)
|
if (_threadCount == 0)
|
||||||
{
|
{
|
||||||
MorphAsync(map, ch.Width, ch.Height, _cycleLength, (uint)_random.Next(), ref gain);
|
var random = new Pcg32Single((uint)_random.Next());
|
||||||
|
MorphAsync(map, ch.Width, ch.Height, _cycleLength, random, gainWrapper).ConfigureAwait(false).GetAwaiter()
|
||||||
|
.GetResult();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
List<Thread> workers = [];
|
if (_morphWorkers is null || (ulong)_morphWorkers.Length != _threadCount)
|
||||||
for (ulong i = 0; i < _threadCount; ++i)
|
_morphWorkers = new Task[_threadCount];
|
||||||
|
if (_morphRandoms is null || (ulong)_morphRandoms.Length != _threadCount)
|
||||||
{
|
{
|
||||||
var thread = new Thread(() =>
|
_morphRandoms = new Pcg32Single[_threadCount];
|
||||||
MorphAsync(map, ch.Width, ch.Height, _cycleLength, (uint)_random.Next(), ref gain));
|
for (ulong i = 0; i < _threadCount; ++i)
|
||||||
thread.Name = $"MorphThread-{i}";
|
{
|
||||||
thread.Start();
|
_morphRandoms[i] = new Pcg32Single((uint)_random.Next());
|
||||||
workers.Add(thread);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while (workers.Any())
|
for (ulong i = 0; i < _threadCount; ++i)
|
||||||
{
|
{
|
||||||
workers.Last().Join();
|
var random = _morphRandoms[i];
|
||||||
workers.RemoveAt(workers.Count - 1);
|
var workerTask = MorphAsync(map, ch.Width, ch.Height, _cycleLength, random, gainWrapper);
|
||||||
|
_morphWorkers[i] = workerTask;
|
||||||
}
|
}
|
||||||
|
Task.WaitAll(_morphWorkers);
|
||||||
}
|
}
|
||||||
|
gain = gainWrapper.Gain;
|
||||||
_chainMapE -= gain;
|
_chainMapE -= gain;
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@@ -717,8 +729,8 @@ public class AtomorphThread : IDisposable
|
|||||||
{
|
{
|
||||||
pos = it;
|
pos = it;
|
||||||
var ownerPos = f.Owners[pos];
|
var ownerPos = f.Owners[pos];
|
||||||
if (bl.Surface.Count(v => v == pos) != 0 || f.Owners.Count(v => v.Key == pos) == 0 ||
|
if (!HasPixel(frameKey, pos) || bl.Surface.Contains(pos) || !f.Owners.ContainsKey(pos) ||
|
||||||
checkedBlobs.Count(v => v == ownerPos) != 0 || !HasPixel(frameKey, pos))
|
checkedBlobs.Contains(ownerPos))
|
||||||
{
|
{
|
||||||
// Don't expand into itself and empty positions.
|
// Don't expand into itself and empty positions.
|
||||||
if (checkThreshold && (ulong)bl.Surface.Count >= _blobMinSize)
|
if (checkThreshold && (ulong)bl.Surface.Count >= _blobMinSize)
|
||||||
@@ -759,7 +771,6 @@ public class AtomorphThread : IDisposable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MainHandling:
|
|
||||||
if (bestFound)
|
if (bestFound)
|
||||||
{
|
{
|
||||||
// Merge with this blob.
|
// Merge with this blob.
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ public static class AtomorphUtilities
|
|||||||
y = pt.Y + pt.YFract/(float)(byte.MaxValue+1);
|
y = pt.Y + pt.YFract/(float)(byte.MaxValue+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static ulong PointDistance(Point p1, Point p2)
|
public static ulong PointDistance(Point p1, Point p2)
|
||||||
{
|
{
|
||||||
var xd = (ulong)Math.Abs(((byte.MaxValue+1)*p1.X+p1.XFract) - ((byte.MaxValue+1)*p2.X+p2.XFract));
|
var xd = (ulong)Math.Abs(((byte.MaxValue+1)*p1.X+p1.XFract) - ((byte.MaxValue+1)*p2.X+p2.XFract));
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace Atomorph.Models;
|
namespace Atomorph.Models;
|
||||||
|
|
||||||
public record struct Point
|
public record struct Point
|
||||||
@@ -14,41 +16,53 @@ public record struct Point
|
|||||||
// } point;
|
// } point;
|
||||||
//
|
//
|
||||||
|
|
||||||
private ulong _word;
|
private ushort _x;
|
||||||
|
private ushort _y;
|
||||||
|
private byte _xFract;
|
||||||
|
private byte _yFract;
|
||||||
|
private PointFlags _flags;
|
||||||
|
|
||||||
public ulong Word
|
public ulong Word
|
||||||
{
|
{
|
||||||
get => _word;
|
[method: MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
set => _word = value;
|
get => _x | ((ulong)_y << 16) | ((ulong)_xFract << 32) | ((ulong)_yFract << 40) | ((ulong)_flags << 48);
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_x = (ushort)(value & 0xFFFF);
|
||||||
|
_y = (ushort)((value >> 16) & 0xFFFF);
|
||||||
|
_xFract = (byte)((value >> 32) & 0xFF);
|
||||||
|
_yFract = (byte)((value >> 40) & 0xFF);
|
||||||
|
_flags = (PointFlags)((value >> 48) & 0xFF);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ushort X
|
public ushort X
|
||||||
{
|
{
|
||||||
get => (ushort)(_word & 0xFFFF);
|
get => _x;
|
||||||
set => _word = (_word & ~0xFFFFUL) | value;
|
set => _x = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ushort Y
|
public ushort Y
|
||||||
{
|
{
|
||||||
get => (ushort)((_word >> 16) & 0xFFFF);
|
get => _y;
|
||||||
set => _word = (_word & ~(0xFFFFUL << 16)) | ((ulong)value << 16);
|
set => _y = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte XFract
|
public byte XFract
|
||||||
{
|
{
|
||||||
get => (byte)((_word >> 32) & 0xFF);
|
get => _xFract;
|
||||||
set => _word = (_word & ~(0xFFUL << 32)) | ((ulong)value << 32);
|
set => _xFract = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte YFract
|
public byte YFract
|
||||||
{
|
{
|
||||||
get => (byte)((_word >> 40) & 0xFF);
|
get => _yFract;
|
||||||
set => _word = (_word & ~(0xFFUL << 40)) | ((ulong)value << 40);
|
set => _yFract = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PointFlags Flags
|
public PointFlags Flags
|
||||||
{
|
{
|
||||||
get => (PointFlags)((_word >> 48) & 0xFF);
|
get => _flags;
|
||||||
set => _word = (_word & ~(0xFFUL << 48)) | ((ulong)value << 48);
|
set => _flags = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||