RustToDust/client/tests/HbGpuProbe.cs

233 lines
8.7 KiB
C#

using System;
using System.IO;
using System.Threading.Tasks;
using Godot;
using RustToDust.HbGpu;
namespace RustToDust.Client.Tests;
/// <summary>
/// Cut 14a visual & headless verification probe for HbGpuCanvas (1 Vulkan draw call).
/// Run headless: Godot --headless --audio-driver Dummy --path client res://tests/HbGpuProbe.tscn
/// Run Vulkan: Godot --audio-driver Dummy --windowed --path client res://tests/HbGpuProbe.tscn -- --capture-proof=path.png
/// </summary>
public partial class HbGpuProbe : Control
{
private HbGpuCanvas _canvas = null!;
private bool _done;
private int _frameCount;
private string? _capturePath;
public override void _Ready()
{
foreach (string arg in OS.GetCmdlineUserArgs())
{
if (arg.StartsWith("--capture-proof=", StringComparison.Ordinal))
{
_capturePath = arg["--capture-proof=".Length..];
}
}
_capturePath ??= System.Environment.GetEnvironmentVariable("RTD_HBGPU_PROOF_PNG");
var bg = new ColorRect
{
Color = new Color(0.05f, 0.065f, 0.085f, 1.0f),
MouseFilter = MouseFilterEnum.Ignore
};
AddChild(bg);
bg.SetAnchorsAndOffsetsPreset(LayoutPreset.FullRect);
_canvas = new HbGpuCanvas
{
Name = "HbGpuCanvas",
ProfilingEnabled = true
};
AddChild(_canvas);
_canvas.SetAnchorsAndOffsetsPreset(LayoutPreset.FullRect);
_ = RunVerificationAsync();
}
private async Task RunVerificationAsync()
{
try
{
// Render enough frames to warm Vulkan pipeline & collect RD GPU timestamps
int targetFrames = _canvas.IsVulkanActive ? 24 : 3;
for (int i = 0; i < targetFrames; i++)
{
SubmitShowcaseFrame(i);
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
_frameCount++;
}
if (_canvas.LastQuadCount <= 0)
{
Fail("Expected LastQuadCount > 0 after submitting HbGpu showcase frame.");
return;
}
if (!_canvas.IsVulkanActive)
{
GD.Print($"HBGPU_PROBE PASS (headless null-RD): quads={_canvas.LastQuadCount}, frames={_frameCount}");
Finish(0);
return;
}
if (_canvas.LastDrawCallCount != 1)
{
Fail($"Expected LastDrawCallCount == 1 on Vulkan, got {_canvas.LastDrawCallCount}.");
return;
}
if (!_canvas.TryReadbackRgba8(out byte[] rgba, out int w, out int h))
{
Fail("HbGpuCanvas.TryReadbackRgba8 returned false on active Vulkan device.");
return;
}
int nonZero = 0;
int premulViolations = 0;
for (int i = 0; i + 3 < rgba.Length; i += 4)
{
byte r = rgba[i];
byte g = rgba[i + 1];
byte b = rgba[i + 2];
byte a = rgba[i + 3];
if (a > 0)
{
nonZero++;
}
if (r > a || g > a || b > a)
{
premulViolations++;
}
}
if (nonZero < 1500)
{
Fail($"Expected >= 1500 rendered pixels in HbGpu Texture2Drd, got {nonZero} ({w}x{h}).");
return;
}
if (premulViolations != 0)
{
Fail($"Premultiplied alpha invariant violated on {premulViolations} pixels.");
return;
}
if (!string.IsNullOrWhiteSpace(_capturePath))
{
SubmitShowcaseFrame(_frameCount);
await ToSignal(RenderingServer.Singleton, RenderingServer.SignalName.FramePostDraw);
Image? vpImg = GetViewport().GetTexture()?.GetImage();
if (vpImg is null)
{
Fail("Viewport capture returned null Image.");
return;
}
string? dir = Path.GetDirectoryName(_capturePath);
if (!string.IsNullOrEmpty(dir))
{
Directory.CreateDirectory(dir);
}
Error saveErr = vpImg.SavePng(_capturePath);
if (saveErr != Error.Ok)
{
Fail($"Failed to save proof PNG '{_capturePath}': {saveErr}");
return;
}
}
GD.Print(
$"HBGPU_PROBE PASS (Vulkan): size={w}x{h}, quads={_canvas.LastQuadCount}, " +
$"draw_calls={_canvas.LastDrawCallCount}, gpu_ms={_canvas.LastHbGpuMs:F4}, " +
$"non_zero_px={nonZero}, premul_violations={premulViolations}");
Finish(0);
}
catch (Exception ex)
{
Fail(ex.ToString());
}
}
private void SubmitShowcaseFrame(int frameIndex)
{
Color amber = new(0.95f, 0.72f, 0.22f, 1.0f);
Color cyan = new(0.32f, 0.86f, 0.96f, 1.0f);
Color slate = new(0.78f, 0.84f, 0.90f, 1.0f);
Color panelBg = new(0.06f, 0.09f, 0.12f, 0.78f);
Color alert = new(0.96f, 0.28f, 0.22f, 1.0f);
Color emerald = new(0.28f, 0.90f, 0.56f, 1.0f);
_canvas.BeginFrame();
// Top-left panel backdrop + crisp Krypton typography across weights/slants/sizes
_canvas.DrawRectFilled(40f, 36f, 680f, 224f, panelBg);
_canvas.DrawLineSegment(40f, 36f, 720f, 36f, 2f, amber);
_canvas.DrawLineSegment(40f, 260f, 720f, 260f, 1.5f, new Color(amber, 0.5f));
_canvas.DrawText("RUST TO DUST // HB-GPU VULKAN", 60f, 84f, 32f, amber, wght: 700f);
_canvas.DrawText("CHASSIS: RAPTOR-7 | ALT: 142.8m | SPD: 28.4m/s", 60f, 124f, 20f, slate, wght: 550f);
_canvas.DrawText("CALT / LIGA: -> ==> != <= >= // 0x4F2A", 60f, 158f, 18f, cyan, wght: 350f);
_canvas.DrawText("SLANT -9 DEG // STEM DARKENED ANALYTIC SLUG", 60f, 192f, 15f, emerald, wght: 400f, slnt: -9f);
_canvas.DrawText("11PX MICRO TELEMETRY: 0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ", 60f, 226f, 11f, slate, wght: 250f);
// Center crosshair reticle + dynamic cubic Bezier thermal heat arc
float cx = 640f;
float cy = 400f;
_canvas.DrawCircleFilled(cx, cy, 3.5f, amber);
_canvas.DrawLineSegment(cx - 28f, cy, cx - 10f, cy, 2.0f, cyan);
_canvas.DrawLineSegment(cx + 10f, cy, cx + 28f, cy, 2.0f, cyan);
_canvas.DrawLineSegment(cx, cy - 28f, cx, cy - 10f, 2.0f, cyan);
_canvas.DrawLineSegment(cx, cy + 10f, cx, cy + 28f, 2.0f, cyan);
float heatSweep = -2.356f + (0.65f + 0.1f * MathF.Sin(frameIndex * 0.1f)) * 4.712f;
_canvas.DrawArcStrokeDynamic(cx, cy, 62f, 5.5f, -2.356f, heatSweep, amber);
_canvas.DrawArcStrokeDynamic(cx, cy, 82f, 4.0f, -0.34f, 0.34f, alert);
// Target lock corner brackets (all 4 quadrants)
float bx0 = 860f, by0 = 280f, bx1 = 1040f, by1 = 460f;
_canvas.DrawTargetBracketCorner(bx0, by0, 26f, 3.5f, 0, alert);
_canvas.DrawTargetBracketCorner(bx1, by0, 26f, 3.5f, 1, alert);
_canvas.DrawTargetBracketCorner(bx1, by1, 26f, 3.5f, 2, alert);
_canvas.DrawTargetBracketCorner(bx0, by1, 26f, 3.5f, 3, alert);
_canvas.DrawText("LOCK: SPECTATOR-02 [318m]", bx0, by0 - 12f, 14f, alert, wght: 600f);
// Offscreen directional arrows
_canvas.DrawDirectionalArrow(1160f, 560f, 28f, 0.55f, amber);
_canvas.DrawDirectionalArrow(120f, 560f, 28f, -2.45f, alert);
// Radar circle, safe ring, LOS lock line, diamond blip & chevron
float rx = 1080f, ry = 160f, rr = 96f;
_canvas.DrawCircleFilled(rx, ry, rr, new Color(0.04f, 0.07f, 0.09f, 0.85f));
_canvas.DrawCircleRing(rx, ry, rr, 2.5f, cyan);
_canvas.DrawCircleRing(rx, ry, rr * 0.58f, 1.5f, new Color(emerald, 0.75f));
_canvas.DrawLineSegment(rx, ry, rx + 44f, ry - 52f, 1.5f, new Color(alert, 0.85f));
_canvas.Writer.DrawBlob(
HbGpuVectorShapes.GetDiamondBlip(_canvas.Blobs),
rx + 44f, ry - 52f,
0.012f, 0f, 0f, -0.012f, 0f, 0f,
alert.R, alert.G, alert.B, alert.A);
_canvas.Writer.DrawBlob(
HbGpuVectorShapes.GetChevronTriangle(_canvas.Blobs),
rx, ry,
0.014f, 0f, 0f, -0.014f, 0f, 0f,
emerald.R, emerald.G, emerald.B, emerald.A);
_canvas.EndFrameAndSubmit();
}
private void Fail(string msg)
{
GD.PrintErr("HBGPU_PROBE FAIL: " + msg);
Finish(1);
}
private void Finish(int code)
{
if (_done) return;
_done = true;
GetTree().Quit(code);
}
}