271 lines
11 KiB
C#
271 lines
11 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Runtime.InteropServices;
|
|
using System.Security.Cryptography;
|
|
using RustToDust.HbGpu;
|
|
using Xunit;
|
|
|
|
namespace RustToDust.Tests;
|
|
|
|
public sealed class HbGpuNativeTests
|
|
{
|
|
private static string RepoRoot
|
|
{
|
|
get
|
|
{
|
|
string? dir = AppContext.BaseDirectory;
|
|
while (dir is not null)
|
|
{
|
|
if (File.Exists(Path.Combine(dir, "RustToDust.slnx")))
|
|
{
|
|
return dir;
|
|
}
|
|
dir = Directory.GetParent(dir)?.FullName;
|
|
}
|
|
throw new DirectoryNotFoundException("Could not locate RustToDust.slnx from test output directory.");
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void NativeDllExistsAndMeetsSizeBudget()
|
|
{
|
|
if (OperatingSystem.IsWindows())
|
|
{
|
|
string dllPath = Path.Combine(AppContext.BaseDirectory, "rtd_hbgpu.dll");
|
|
Assert.True(File.Exists(dllPath), $"Expected rtd_hbgpu.dll at '{dllPath}'.");
|
|
long size = new FileInfo(dllPath).Length;
|
|
Assert.InRange(size, 100_000, 2_621_440); // < 2.5 MiB
|
|
}
|
|
|
|
string ver = HbGpuNative.GetVersionString();
|
|
Assert.Equal("15.2.0", ver);
|
|
|
|
string vertShared = HbGpuNative.GetShaderSource(HbGpuShaderStage.Vertex, HbGpuShaderKind.Shared);
|
|
string fragShared = HbGpuNative.GetShaderSource(HbGpuShaderStage.Fragment, HbGpuShaderKind.Shared);
|
|
string fragDraw = HbGpuNative.GetShaderSource(HbGpuShaderStage.Fragment, HbGpuShaderKind.Draw);
|
|
Assert.Contains("hb_gpu_dilate", vertShared);
|
|
Assert.Contains("uniform isamplerBuffer hb_gpu_atlas;", fragShared);
|
|
Assert.Contains("hb_gpu_draw", fragDraw);
|
|
}
|
|
|
|
[Fact]
|
|
public void KryptonFontAndLicensesAreCompliant()
|
|
{
|
|
string root = RepoRoot;
|
|
string fontPath = Path.Combine(root, "assets", "fonts", "Krypton.ttf");
|
|
string oflPath = Path.Combine(root, "assets", "fonts", "OFL-1.1.txt");
|
|
string hbCopyPath = Path.Combine(root, "assets", "fonts", "HARFBUZZ-COPYING.txt");
|
|
string noticesPath = Path.Combine(root, "THIRD_PARTY_NOTICES.txt");
|
|
|
|
Assert.True(File.Exists(fontPath));
|
|
Assert.True(File.Exists(oflPath));
|
|
Assert.True(File.Exists(hbCopyPath));
|
|
Assert.True(File.Exists(noticesPath));
|
|
|
|
byte[] fontBytes = File.ReadAllBytes(fontPath);
|
|
Assert.Equal(1_363_340, fontBytes.Length);
|
|
string sha = Convert.ToHexString(SHA256.HashData(fontBytes)).ToLowerInvariant();
|
|
Assert.Equal("6462cae55b336ae6ffb7c8f1932177960510a4207e65803f527b276d42c8a240", sha);
|
|
|
|
string oflText = File.ReadAllText(oflPath);
|
|
Assert.Contains("Copyright (c) 2023, GitHub", oflText);
|
|
Assert.Contains("Krypton", oflText);
|
|
}
|
|
|
|
[Fact]
|
|
public void QuadInstanceStructIs80Bytes()
|
|
{
|
|
Assert.Equal(80, Unsafe.SizeOf<HbGpuQuadInstance>());
|
|
Assert.Equal(80, Marshal.SizeOf<HbGpuQuadInstance>());
|
|
}
|
|
|
|
[Fact]
|
|
public void ShapingAndGlyphEncodingWorkAcrossKryptonAxes()
|
|
{
|
|
string fontPath = Path.Combine(RepoRoot, "assets", "fonts", "Krypton.ttf");
|
|
using var fonts = new HbGpuFontCache();
|
|
fonts.RegisterFontFromFile(0, fontPath);
|
|
|
|
RtdHbFontMetrics metrics = fonts.GetMetrics(0);
|
|
Assert.Equal(2000, metrics.Upem);
|
|
|
|
var atlas = new HbGpuAtlasAllocator();
|
|
var blobs = new HbGpuBlobCache(atlas);
|
|
var writer = new HbGpuCommandWriter();
|
|
|
|
writer.BeginFrame();
|
|
float wReg = writer.DrawTextUtf16("RAPTOR -> 142.8m", 10f, 40f, 24f, 1f, 1f, 1f, 1f, fonts, blobs, 0, wght: 200f);
|
|
float wBold = writer.DrawTextUtf16("CHASSIS", 10f, 80f, 24f, 1f, 1f, 1f, 1f, fonts, blobs, 0, wght: 700f);
|
|
float wSlnt = writer.DrawTextUtf16("SLANT", 10f, 120f, 24f, 1f, 1f, 1f, 1f, fonts, blobs, 0, wght: 400f, slnt: -9f);
|
|
|
|
Assert.True(wReg > 50f);
|
|
Assert.True(wBold > 20f);
|
|
Assert.True(wSlnt > 20f);
|
|
Assert.True(writer.QuadCount >= 18); // spaces omit quads, visible glyphs emit quads
|
|
Assert.True(blobs.CachedGlyphCount >= 15);
|
|
Assert.True(atlas.PersistentCursor > 0);
|
|
Assert.True(atlas.PendingChunkCount > 0);
|
|
|
|
// Space glyph encodes cleanly as empty
|
|
CachedBlob spaceBlob = blobs.GetOrEncodeGlyph(fonts, 0, 1 /* glyph or verify space */, 200, 1000, 0);
|
|
Assert.True(spaceBlob.TexelCount >= 0);
|
|
|
|
// Hot steady-state loop allocates zero managed bytes once glyphs/styles are cached
|
|
writer.BeginFrame();
|
|
atlas.ClearPending();
|
|
ReadOnlySpan<char> hotText = "RAPTOR -> 142.8m";
|
|
long before = GC.GetAllocatedBytesForCurrentThread();
|
|
for (int i = 0; i < 64; i++)
|
|
{
|
|
writer.BeginFrame();
|
|
writer.DrawTextUtf16(hotText, 10f, 40f, 24f, 1f, 1f, 1f, 1f, fonts, blobs, 0, wght: 200f);
|
|
}
|
|
long after = GC.GetAllocatedBytesForCurrentThread();
|
|
Assert.Equal(0, after - before);
|
|
}
|
|
|
|
[Fact]
|
|
public void VectorPathsAndAtlasBudgetResetWork()
|
|
{
|
|
var atlas = new HbGpuAtlasAllocator();
|
|
var blobs = new HbGpuBlobCache(atlas);
|
|
|
|
CachedBlob rect = HbGpuVectorShapes.GetUnitFilledRect(blobs);
|
|
CachedBlob circle = HbGpuVectorShapes.GetUnitFilledCircle(blobs);
|
|
CachedBlob ring = HbGpuVectorShapes.GetUnitRingStroke(blobs, 24);
|
|
CachedBlob bracket = HbGpuVectorShapes.GetTargetBracketCorner(blobs, 120);
|
|
CachedBlob arrow = HbGpuVectorShapes.GetDirectionalArrow(blobs);
|
|
CachedBlob chevron = HbGpuVectorShapes.GetChevronTriangle(blobs);
|
|
CachedBlob diamond = HbGpuVectorShapes.GetDiamondBlip(blobs);
|
|
CachedBlob arc = HbGpuVectorShapes.EncodeArcStrokeDynamic(blobs, -2.356f, 0.785f);
|
|
|
|
Assert.True(rect.IsValid && rect.TexelCount > 2);
|
|
Assert.True(circle.IsValid && circle.TexelCount > 2);
|
|
Assert.True(ring.IsValid && ring.TexelCount > 4);
|
|
Assert.True(bracket.IsValid && bracket.TexelCount > 2);
|
|
Assert.True(arrow.IsValid && arrow.TexelCount > 2);
|
|
Assert.True(chevron.IsValid && chevron.TexelCount > 2);
|
|
Assert.True(diamond.IsValid && diamond.TexelCount > 2);
|
|
Assert.True(arc.IsValid && arc.TexelCount > 4);
|
|
Assert.True(arc.AtlasOffset >= HbGpuAtlasAllocator.DynamicBase);
|
|
|
|
// Also test tapered line verb directly
|
|
Span<byte> lineVerb = stackalloc byte[] { (byte)HbGpuPathVerb.Line };
|
|
Span<float> lineArgs = stackalloc float[] { -400f, 0f, 20f, 400f, 0f, 60f, 1f };
|
|
CachedBlob lineBlob = blobs.GetOrEncodeStaticPath(
|
|
new VectorBlobKey(VectorShapeKind.Custom, 1, 0), 1000u, lineVerb, lineArgs);
|
|
Assert.True(lineBlob.IsValid);
|
|
|
|
// Drain uploads with zero allocations
|
|
byte[] staging = new byte[HbGpuBlobCache.ScratchShortsCapacity * sizeof(short)];
|
|
int drainedBytes = 0;
|
|
Action<uint, uint> cb = (_, len) => drainedBytes += (int)len;
|
|
int chunks = atlas.DrainChunks(staging, cb);
|
|
Assert.True(chunks >= 9);
|
|
Assert.True(drainedBytes > 0);
|
|
Assert.Equal(0, atlas.PendingChunkCount);
|
|
|
|
// Verify static arc caching (0 pending chunks and 0 B GC allocs on repeat lookup)
|
|
var staticArcKey = new VectorBlobKey(VectorShapeKind.ThermalHeatArc, 45, 1);
|
|
CachedBlob staticArc = HbGpuVectorShapes.GetStaticArcStroke(blobs, staticArcKey, -2.356f, 0.785f, 500f, 30f);
|
|
Assert.True(staticArc.IsValid);
|
|
Assert.True(staticArc.AtlasOffset < HbGpuAtlasAllocator.DynamicBase);
|
|
atlas.ClearPending();
|
|
|
|
// Warm up cache-hit path once before measuring GC allocations
|
|
CachedBlob warmHit = HbGpuVectorShapes.GetStaticArcStroke(blobs, staticArcKey, -2.356f, 0.785f, 500f, 30f);
|
|
Assert.Equal(staticArc.AtlasOffset, warmHit.AtlasOffset);
|
|
|
|
uint expectedOffset = staticArc.AtlasOffset;
|
|
bool allMatched = true;
|
|
long arcGcBefore = GC.GetAllocatedBytesForCurrentThread();
|
|
for (int i = 0; i < 64; i++)
|
|
{
|
|
CachedBlob hit = HbGpuVectorShapes.GetStaticArcStroke(blobs, staticArcKey, -2.356f, 0.785f, 500f, 30f);
|
|
if (hit.AtlasOffset != expectedOffset) allMatched = false;
|
|
}
|
|
long arcGcAfter = GC.GetAllocatedBytesForCurrentThread();
|
|
Assert.True(allMatched);
|
|
Assert.Equal(0, arcGcAfter - arcGcBefore);
|
|
Assert.Equal(0, atlas.PendingChunkCount);
|
|
|
|
// Verify dynamic ring wrap-around
|
|
for (int i = 0; i < 600; i++)
|
|
{
|
|
CachedBlob dyn = HbGpuVectorShapes.EncodeArcStrokeDynamic(blobs, -1.5f, 1.5f);
|
|
Assert.True(dyn.AtlasOffset >= HbGpuAtlasAllocator.DynamicBase);
|
|
Assert.True(dyn.AtlasOffset + dyn.TexelCount <= HbGpuAtlasAllocator.TotalTexels);
|
|
atlas.ClearPending();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void PersistentAtlasOverflowDoesNotResetMidFrame()
|
|
{
|
|
var atlas = new HbGpuAtlasAllocator();
|
|
var blobs = new HbGpuBlobCache(atlas);
|
|
|
|
// Fill persistent zone right up near PersistentCapacity without crossing it
|
|
short[] chunk = new short[4096 * 4]; // 4096 texels
|
|
chunk[0] = 1;
|
|
while (atlas.PersistentCursor + 4096 <= HbGpuAtlasAllocator.PersistentCapacity)
|
|
{
|
|
atlas.AllocatePersistent(chunk);
|
|
atlas.ClearPending();
|
|
}
|
|
|
|
uint epochBefore = atlas.Epoch;
|
|
uint cursorBefore = atlas.PersistentCursor;
|
|
Assert.True(cursorBefore > HbGpuAtlasAllocator.PersistentResetWatermark);
|
|
|
|
// Encode a static vector that exceeds remaining PersistentCapacity: must NOT reset mid-frame!
|
|
Span<byte> bigVerbs = stackalloc byte[64];
|
|
Span<float> bigArgs = stackalloc float[128];
|
|
int v = 0, a = 0;
|
|
bigVerbs[v++] = (byte)HbGpuPathVerb.Move;
|
|
bigArgs[a++] = 0f;
|
|
bigArgs[a++] = 0f;
|
|
for (int i = 1; i <= 30; i++)
|
|
{
|
|
float ang = i * 0.2f;
|
|
bigVerbs[v++] = (byte)HbGpuPathVerb.LineTo;
|
|
bigArgs[a++] = MathF.Cos(ang) * 400f;
|
|
bigArgs[a++] = MathF.Sin(ang) * 400f;
|
|
}
|
|
bigVerbs[v++] = (byte)HbGpuPathVerb.Close;
|
|
|
|
// First force a large remaining fill so the encoded vector overflows remaining space
|
|
uint rem = HbGpuAtlasAllocator.PersistentCapacity - atlas.PersistentCursor;
|
|
if (rem > 2)
|
|
{
|
|
atlas.AllocatePersistent(new short[(rem - 1) * 4]);
|
|
atlas.ClearPending();
|
|
}
|
|
cursorBefore = atlas.PersistentCursor;
|
|
|
|
CachedBlob overflowed = blobs.GetOrEncodeStaticPath(
|
|
new VectorBlobKey(VectorShapeKind.Custom, 999, 1),
|
|
1000u,
|
|
bigVerbs[..v],
|
|
bigArgs[..a]);
|
|
|
|
Assert.False(overflowed.IsValid);
|
|
Assert.Equal(epochBefore, atlas.Epoch);
|
|
Assert.Equal(cursorBefore, atlas.PersistentCursor);
|
|
Assert.True(blobs.IsPersistentOverflowPending);
|
|
|
|
// Next BeginFrame resets cleanly and allows encoding
|
|
Assert.True(blobs.EnsureBudgetOrReset());
|
|
Assert.Equal(epochBefore + 1, atlas.Epoch);
|
|
Assert.Equal(0u, atlas.PersistentCursor);
|
|
Assert.False(blobs.IsPersistentOverflowPending);
|
|
|
|
CachedBlob recovered = blobs.GetOrEncodeStaticPath(
|
|
new VectorBlobKey(VectorShapeKind.Custom, 999, 1),
|
|
1000u,
|
|
bigVerbs[..v],
|
|
bigArgs[..a]);
|
|
Assert.True(recovered.IsValid);
|
|
}
|
|
}
|