Enhance noise generation and memory management

Added new noise generation methods in `ParallelNoiseBenchmark`, including `Frac`, `Lerp`, and `GradientNoiseDirect`, and updated the `GradientNoise` method to utilize them. Changed constants to use `_LENGTH` for consistency.

Changed `Arena` and `DynamicArena` classes to use `uint` instead of `ulong` for size fields, improving memory usage. Updated memory allocation to use `NativeMemory` for better performance and safety.

Updated `UnsafeArray<T>` and `UnsafeList<T>` classes to replace `Marshal` methods with `NativeMemory`, enhancing performance and safety. Modified `Dispose` methods to use `NativeMemory.AlignedFree`.

Added `MemoryUtilities` class with new methods for memory management, including `MemClear`, `MemSet`, `MemCpy`, `SizeOf`, and `AlignOf`, utilizing `NativeMemory`.

Fixed minor cleanup in the `ObjectPool` class's `Dispose` method.
This commit is contained in:
2025-03-25 09:49:49 +09:00
parent aa1e9e6b1d
commit 7bcd699eb9
7 changed files with 120 additions and 73 deletions

View File

@@ -14,6 +14,43 @@ public class ParallelNoiseBenchmark
public UnsafeArray<float> buffers;
public int width;
public int height;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static float Frac(float x)
{
return x - MathF.Truncate(x);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static float Lerp(float a, float b, float t)
{
return a + t * (b - a);
}
private static Vector2 GradientNoiseDirect(Vector2 uv)
{
uv.X %= 289;
uv.Y %= 289;
var x = (34 * uv.X + 1) * uv.X % 289 + uv.Y;
x = (34 * x + 1) * x % 289;
x = Frac(x / 41) * 2 - 1;
return Vector2.Normalize(new Vector2(x - MathF.Floor(x + 0.5f), MathF.Abs(x) - 0.5f));
}
public static float GradientNoise(Vector2 uv)
{
var ip = new Vector2(MathF.Floor(uv.X), MathF.Floor(uv.Y));
var fp = new Vector2(Frac(uv.X), Frac(uv.Y));
var d00 = Vector2.Dot(GradientNoiseDirect(ip), fp);
var d01 = Vector2.Dot(GradientNoiseDirect(ip + new Vector2(0, 1)), fp - new Vector2(0, 1));
var d10 = Vector2.Dot(GradientNoiseDirect(ip + new Vector2(1, 0)), fp - new Vector2(1, 0));
var d11 = Vector2.Dot(GradientNoiseDirect(ip + new Vector2(1, 1)), fp - new Vector2(1, 1));
fp = fp * fp * fp * (fp * (fp * new Vector2(6.0f) - new Vector2(15.0f)) + new Vector2(10.0f));
return Lerp(Lerp(d00, d10, fp.Y), Lerp(d01, d11, fp.Y), fp.X);
}
public void Execute(int index)
{
var x = index % width;
@@ -27,46 +64,10 @@ public class ParallelNoiseBenchmark
private const int _HEIGHT = 512;
private const int _LENGTH = _WIDTH * _HEIGHT;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static float Frac(float x)
{
return x - MathF.Truncate(x);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static float Lerp(float a, float b, float t)
{
return a + t * (b - a);
}
private static Vector2 GradientNoiseDirect(Vector2 uv)
{
uv.X %= 289;
uv.Y %= 289;
var x = (34 * uv.X + 1) * uv.X % 289 + uv.Y;
x = (34 * x + 1) * x % 289;
x = Frac(x / 41) * 2 - 1;
return Vector2.Normalize(new Vector2(x - MathF.Floor(x + 0.5f), MathF.Abs(x) - 0.5f));
}
private static float GradientNoise(Vector2 uv)
{
var ip = new Vector2(MathF.Floor(uv.X), MathF.Floor(uv.Y));
var fp = new Vector2(Frac(uv.X), Frac(uv.Y));
var d00 = Vector2.Dot(GradientNoiseDirect(ip), fp);
var d01 = Vector2.Dot(GradientNoiseDirect(ip + new Vector2(0, 1)), fp - new Vector2(0, 1));
var d10 = Vector2.Dot(GradientNoiseDirect(ip + new Vector2(1, 0)), fp - new Vector2(1, 0));
var d11 = Vector2.Dot(GradientNoiseDirect(ip + new Vector2(1, 1)), fp - new Vector2(1, 1));
fp = fp * fp * fp * (fp * (fp * new Vector2(6.0f) - new Vector2(15.0f)) + new Vector2(10.0f));
return Lerp(Lerp(d00, d10, fp.Y), Lerp(d01, d11, fp.Y), fp.X);
}
[Benchmark]
public void JobSystem()
{
using var buffers = new UnsafeArray<float>(_WIDTH * _HEIGHT, AllocationType.UnInitialized);
using var buffers = new UnsafeArray<float>(_LENGTH, AllocationType.UnInitialized);
var job = new NoiseJob()
{
buffers = buffers,
@@ -81,14 +82,14 @@ public class ParallelNoiseBenchmark
[Benchmark]
public void ParallelFor()
{
using var buffers = new UnsafeArray<float>(_WIDTH * _HEIGHT, AllocationType.UnInitialized);
using var buffers = new UnsafeArray<float>(_LENGTH, AllocationType.UnInitialized);
Parallel.For(0, _LENGTH, i =>
{
var x = i % _WIDTH;
var y = i / _HEIGHT;
var uv = new Vector2(x, y);
buffers[i] = GradientNoise(uv);
buffers[i] = NoiseJob.GradientNoise(uv);
});
}
}