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()); Assert.Equal(80, Marshal.SizeOf()); } [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 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 lineVerb = stackalloc byte[] { (byte)HbGpuPathVerb.Line }; Span 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 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 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(); } } }