diff --git a/client/HbGpuCanvas.cs b/client/HbGpuCanvas.cs index 0e2f67d..5813b8f 100644 --- a/client/HbGpuCanvas.cs +++ b/client/HbGpuCanvas.cs @@ -181,7 +181,8 @@ public partial class HbGpuCanvas : Control w * 0.001f, 0f, 0f, -h * 0.001f, 0f, 0f, color.R, color.G, color.B, color.A, - stemDarken: false, clipMinPacked, clipMaxPacked); + stemDarken: false, clipMinPacked, clipMaxPacked, + extraFlags: HbGpuQuadInstance.FlagSolidRect); } public void DrawLineSegment(float x0, float y0, float x1, float y1, float thicknessPx, Color color, @@ -209,7 +210,8 @@ public partial class HbGpuCanvas : Control sin * sx, cos * sy, 0f, 0f, color.R, color.G, color.B, color.A, - stemDarken: false, clipMinPacked, clipMaxPacked); + stemDarken: false, clipMinPacked, clipMaxPacked, + extraFlags: HbGpuQuadInstance.FlagSolidRect); } public void DrawCircleFilled(float cx, float cy, float radiusPx, Color color, @@ -347,6 +349,49 @@ public partial class HbGpuCanvas : Control DrawTargetBracketCorner(x, y + h, arm, cThick, 3, accentCol); } + public void DrawArcStrokeStatic( + float cx, + float cy, + float radiusPx, + float strokePx, + float startRadScreen, + float endRadScreen, + float rotationRadScreen, + VectorBlobKey key, + Color color) + { + if (radiusPx <= 0f || strokePx <= 0f) + { + return; + } + + float halfThickDesign = Math.Clamp(strokePx / radiusPx * 250f, 2f, 200f); + CachedBlob blob = HbGpuVectorShapes.GetStaticArcStroke( + _blobCache, key, -endRadScreen, -startRadScreen, 500f, halfThickDesign); + float s = radiusPx * 0.002f; + if (MathF.Abs(rotationRadScreen) < 1e-5f) + { + _writer.DrawBlob( + blob, cx, cy, + s, 0f, 0f, -s, + 0f, 0f, + color.R, color.G, color.B, color.A); + } + else + { + // Rotate Y-up design space clockwise by rotationRadScreen in Y-down screen space: + // Screen X = s * (cos*ex + sin*ey), Screen Y = s * (sin*ex - cos*ey) + float cos = MathF.Cos(rotationRadScreen); + float sin = MathF.Sin(rotationRadScreen); + _writer.DrawBlob( + blob, cx, cy, + cos * s, sin * s, + sin * s, -cos * s, + 0f, 0f, + color.R, color.G, color.B, color.A); + } + } + public void DrawArcStrokeDynamic(float cx, float cy, float radiusPx, float strokePx, float startRadScreen, float endRadScreen, Color color) { if (radiusPx <= 0f || strokePx <= 0f) @@ -1016,12 +1061,22 @@ layout(location = 4) flat in uvec2 v_clip_packed; layout(location = 0) out vec4 frag_color; void main() {{ - float cov = hb_gpu_draw(v_em, v_blob_loc); - float ppem = hb_gpu_ppem(v_em, v_blob_loc); + vec2 em_px = fwidth(v_em); + vec2 pixelsPerEm = 1.0 / max(em_px, vec2(1e-6)); + float cov; - if ((v_flags & 1u) != 0u && cov > 0.0 && cov < 1.0) {{ - float brightness = dot(v_color.rgb, vec3(1.0 / 3.0)); - cov = hb_gpu_stem_darken(cov, brightness, ppem); + if ((v_flags & 2u) != 0u) {{ + vec2 edgeDist = (500.0 - abs(v_em)) * pixelsPerEm; + cov = clamp(min(edgeDist.x, edgeDist.y) + 0.5, 0.0, 1.0); + }} else {{ + cov = _hb_gpu_slug(v_em, pixelsPerEm, v_blob_loc); + if ((v_flags & 1u) != 0u) {{ + float ppem = hb_gpu_ppem(v_em, v_blob_loc); + if (cov > 0.0 && cov < 1.0) {{ + float brightness = dot(v_color.rgb, vec3(1.0 / 3.0)); + cov = hb_gpu_stem_darken(cov, brightness, ppem); + }} + }} }} if (v_clip_packed.y != 0xFFFFFFFFu) {{ diff --git a/client/Hud.cs b/client/Hud.cs index c34b339..be2c4e1 100644 --- a/client/Hud.cs +++ b/client/Hud.cs @@ -151,8 +151,12 @@ public partial class Hud : CanvasLayer private long _lastChyronVersion = -1; private readonly string[] _chyronDisplayLines = new string[3]; private readonly bool[] _chyronIsLatest = new bool[3]; + private readonly List _chyronWrapScratch = new(8); private int _chyronLineCount; + private readonly List _hintLines = new(2); + private int _lastHintMode = -1; + private IClientArenaSession? _latestSession; private string? _cachedChassisName; private string _cachedChassisUpper = "CHASSIS"; @@ -593,6 +597,7 @@ public partial class Hud : CanvasLayer } UpdatePromptCache(state, player, localPod); + UpdateHintCache(player); _schematic.SetGrid(player.Grid, _selectedCargoIndex); _lastCargoGrid = player.Grid; @@ -650,8 +655,8 @@ public partial class Hud : CanvasLayer float vh = MathF.Max(240f, vpSize.Y); // Top-Left panel - float tlW = 368f; - float tlH = 152f; + float tlW = 396f; + float tlH = 138f + Math.Max(1, _hintLines.Count) * 14f; if (session is not null && session.ConnectionStatus.Mode != NetworkMode.Solo) { tlH += 17f; @@ -983,13 +988,33 @@ public partial class Hud : CanvasLayer } } - // Controls hint - ReadOnlySpan hint = player.IsInDropPod - ? "WASD Steer Pod | Mouse Look | Retro-Burn Auto | Wheel Zoom" - : (player.Grid.Spec.IsVtol - ? "WASD Fly | Space/C Alt | Mouse Aim | LMB Fire | E Bolt | Tab Garage | M Map | F9 Net" - : "WASD Drive | Mouse Aim | LMB Fire | E Bolt | Tab Garage | M Map | F9 Net | Wheel Zoom"); - _hbCanvas.DrawText(hint, x, _topLeftRect.End.Y - 10f, 10.2f, TextMuted, wght: 350f); + // Controls hint (wrapped & clipped cleanly inside panel width) + HbGpuCommandWriter.PackClipRect( + _topLeftRect.Position.X + 8f, + _topLeftRect.Position.Y + 6f, + _topLeftRect.Size.X - 16f, + _topLeftRect.Size.Y - 12f, + out uint tlClipMin, + out uint tlClipMax); + + if (_hintLines.Count == 0) + { + UpdateHintCache(player); + } + float hintY = _topLeftRect.End.Y - 8f - Math.Max(0, _hintLines.Count - 1) * 13.5f; + for (int i = 0; i < _hintLines.Count; i++) + { + _hbCanvas.DrawText( + _hintLines[i], + x, + hintY, + 10.0f, + TextMuted, + wght: 350f, + clipMinPacked: tlClipMin, + clipMaxPacked: tlClipMax); + hintY += 13.5f; + } } // ------------------------------------------------------------------------- @@ -1346,21 +1371,28 @@ public partial class Hud : CanvasLayer const float arcRadius = 21f; const float startAngle = -MathF.PI * 0.75f; const float maxSweep = MathF.PI * 1.5f; - _hbCanvas.DrawArcStrokeDynamic( + _hbCanvas.DrawArcStrokeStatic( center.X, center.Y, arcRadius, 2.0f, startAngle, startAngle + maxSweep, + rotationRadScreen: 0f, + new VectorBlobKey(VectorShapeKind.ThermalHeatArc, 0, 0), new Color(BorderCol.R, BorderCol.G, BorderCol.B, 0.40f)); if (heatRatio > 0.01f) { + const int heatSteps = 135; // 2-degree increments across 270 deg + int step = Math.Clamp((int)MathF.Round(heatRatio * heatSteps), 1, heatSteps); + float sweep = maxSweep * (step / (float)heatSteps); Color heatCol = overPlateau ? RedCol : (heatRatio > 0.65f ? OrangeCol : OliveCol); - _hbCanvas.DrawArcStrokeDynamic( + _hbCanvas.DrawArcStrokeStatic( center.X, center.Y, arcRadius, 2.5f, - startAngle, startAngle + maxSweep * heatRatio, + startAngle, startAngle + sweep, + rotationRadScreen: 0f, + new VectorBlobKey(VectorShapeKind.ThermalHeatArc, (ushort)step, 1), heatCol); } - // Directional incoming-hit indicator arcs + // Directional incoming-hit indicator arcs (single cached static arc rotated per angle -> 0 dynamic atlas uploads) if (_incomingArcCount > 0) { float cameraYaw = _getCameraYaw(); @@ -1391,12 +1423,19 @@ public partial class Hud : CanvasLayer const float hitR = 39f; const float halfSpan = 0.34f; - _hbCanvas.DrawArcStrokeDynamic(center.X, center.Y, hitR, 4.2f, angle - halfSpan, angle + halfSpan, arcCol); + _hbCanvas.DrawArcStrokeStatic( + center.X, center.Y, hitR, 4.2f, + -halfSpan, halfSpan, + rotationRadScreen: angle, + new VectorBlobKey(VectorShapeKind.IncomingHitArc, 0, 0), + arcCol); if (arc.WeaponClass is WeaponVisualClass.ThermalBeam or WeaponVisualClass.GaussRail or WeaponVisualClass.PlasmaPpc) { - _hbCanvas.DrawArcStrokeDynamic( + _hbCanvas.DrawArcStrokeStatic( center.X, center.Y, hitR - 3.5f, 2.0f, - angle - halfSpan * 0.75f, angle + halfSpan * 0.75f, + -halfSpan * 0.75f, halfSpan * 0.75f, + rotationRadScreen: angle, + new VectorBlobKey(VectorShapeKind.IncomingHitArc, 1, 0), new Color(GoldCol.R, GoldCol.G, GoldCol.B, alpha * 0.90f)); } @@ -2126,6 +2165,24 @@ public partial class Hud : CanvasLayer return string.Empty; } + private void UpdateHintCache(ContenderState player) + { + int mode = player.IsInDropPod ? 0 : (player.Grid.Spec.IsVtol ? 1 : 2); + if (mode == _lastHintMode && _hintLines.Count > 0) + { + return; + } + _lastHintMode = mode; + + string hint = mode switch + { + 0 => "WASD Steer Pod | Mouse Look | Retro-Burn Auto | Wheel Zoom", + 1 => "WASD Fly | Space/C Alt | Mouse Aim | LMB Fire | E Bolt | Tab Garage | M Map | F9 Net", + _ => "WASD Drive | Mouse Aim | LMB Fire | E Bolt | Tab Garage | M Map | F9 Net | Wheel Zoom" + }; + WrapTextInto(hint, 376f, 10.0f, 350f, _hintLines); + } + private void SyncBroadcastChyron(BroadcastFeed broadcast) { var lines = broadcast.Lines; @@ -2137,16 +2194,36 @@ public partial class Hud : CanvasLayer _lastBroadcastFeed = broadcast; _lastChyronVersion = broadcast.Version; - int start = Math.Max(0, lines.Count - _chyronDisplayLines.Length); - int visibleCount = lines.Count - start; - _chyronLineCount = visibleCount; - for (int slot = 0; slot < visibleCount; slot++) + // Wrap recent messages cleanly to 548px so nothing overflows or clips mid-word + int startMsg = Math.Max(0, lines.Count - _chyronDisplayLines.Length); + int outIdx = 0; + for (int m = startMsg; m < lines.Count; m++) { - int lineIdx = start + slot; - var line = lines[lineIdx]; - _chyronIsLatest[slot] = lineIdx == lines.Count - 1; - _chyronDisplayLines[slot] = $"[{line.Timestamp:00.0}s] {line.Text}"; + var line = lines[m]; + bool isLatestMsg = m == lines.Count - 1; + string formatted = $"[{line.Timestamp:00.0}s] {line.Text}"; + WrapTextInto(formatted, 548f, isLatestMsg ? 11.5f : 10.8f, isLatestMsg ? 500f : 380f, _chyronWrapScratch); + for (int w = 0; w < _chyronWrapScratch.Count; w++) + { + if (outIdx < _chyronDisplayLines.Length) + { + _chyronDisplayLines[outIdx] = _chyronWrapScratch[w]; + _chyronIsLatest[outIdx] = isLatestMsg; + outIdx++; + } + else + { + for (int s = 1; s < _chyronDisplayLines.Length; s++) + { + _chyronDisplayLines[s - 1] = _chyronDisplayLines[s]; + _chyronIsLatest[s - 1] = _chyronIsLatest[s]; + } + _chyronDisplayLines[^1] = _chyronWrapScratch[w]; + _chyronIsLatest[^1] = isLatestMsg; + } + } } + _chyronLineCount = outIdx; } private void OnHardpointClicked(string hardpointId) diff --git a/src/RustToDust.HbGpu/HbGpuCommandWriter.cs b/src/RustToDust.HbGpu/HbGpuCommandWriter.cs index 47c0b4c..77fa213 100644 --- a/src/RustToDust.HbGpu/HbGpuCommandWriter.cs +++ b/src/RustToDust.HbGpu/HbGpuCommandWriter.cs @@ -71,7 +71,8 @@ public sealed unsafe class HbGpuCommandWriter float a, bool stemDarken = false, uint clipMinPacked = 0, - uint clipMaxPacked = HbGpuQuadInstance.Unclipped) + uint clipMaxPacked = HbGpuQuadInstance.Unclipped, + uint extraFlags = 0u) { if (!blob.IsValid || QuadCount >= MaxQuads) { @@ -97,7 +98,7 @@ public sealed unsafe class HbGpuCommandWriter ColorB = b, ColorA = a, AtlasTexelOffset = blob.AtlasOffset, - Flags = stemDarken ? HbGpuQuadInstance.FlagStemDarken : 0u, + Flags = (stemDarken ? HbGpuQuadInstance.FlagStemDarken : 0u) | extraFlags, ClipMinPacked = clipMinPacked, ClipMaxPacked = clipMaxPacked }; diff --git a/src/RustToDust.HbGpu/HbGpuTypes.cs b/src/RustToDust.HbGpu/HbGpuTypes.cs index d747c59..07fc309 100644 --- a/src/RustToDust.HbGpu/HbGpuTypes.cs +++ b/src/RustToDust.HbGpu/HbGpuTypes.cs @@ -64,6 +64,7 @@ public struct HbGpuQuadInstance public uint ClipMaxPacked; public const uint FlagStemDarken = 1u; + public const uint FlagSolidRect = 2u; public const uint Unclipped = 0xFFFFFFFFu; } diff --git a/src/RustToDust.HbGpu/HbGpuVectorShapes.cs b/src/RustToDust.HbGpu/HbGpuVectorShapes.cs index e04115f..6ec2ebe 100644 --- a/src/RustToDust.HbGpu/HbGpuVectorShapes.cs +++ b/src/RustToDust.HbGpu/HbGpuVectorShapes.cs @@ -134,6 +134,35 @@ public static class HbGpuVectorShapes ChevronVerbs, DiamondArgs); + /// + /// Returns a permanently cached static stroked circular arc in design space (radius=500, Y-up). + /// Eliminates per-frame dynamic atlas uploads for fixed/quantized HUD arcs. + /// + public static CachedBlob GetStaticArcStroke( + HbGpuBlobCache cache, + VectorBlobKey key, + float startRad, + float endRad, + float radius = 500f, + float halfThickness = 32f) + { + if (cache.TryGetStaticPath(key, out CachedBlob cached)) + { + return cached; + } + + float sweep = endRad - startRad; + if (MathF.Abs(sweep) < 1e-4f) + { + return default; + } + + Span verbs = stackalloc byte[19]; // max 1 + 8 + 1 + 8 + 1 + Span args = stackalloc float[100]; // max 2 + 8*6 + 2 + 8*6 + BuildArcPath(startRad, endRad, radius, halfThickness, verbs, args, out int vCount, out int aCount); + return cache.GetOrEncodeStaticPath(key, DesignUpem, verbs[..vCount], args[..aCount]); + } + /// /// Encodes a stroked circular arc in design space (radius=500, Y-up) using cubic Bezier segments. /// Useful for thermal heat arcs and directional incoming-hit arcs. @@ -152,20 +181,31 @@ public static class HbGpuVectorShapes return default; } + Span verbs = stackalloc byte[19]; + Span args = stackalloc float[100]; + BuildArcPath(startRad, endRad, radius, halfThickness, verbs, args, out int vCount, out int aCount); + return cache.EncodeDynamicPath(DesignUpem, verbs[..vCount], args[..aCount]); + } + + private static void BuildArcPath( + float startRad, + float endRad, + float radius, + float halfThickness, + Span verbs, + Span args, + out int v, + out int a) + { + float sweep = endRad - startRad; int segs = Math.Clamp((int)MathF.Ceiling(MathF.Abs(sweep) / (MathF.PI * 0.4f)), 1, 8); float rOut = radius + halfThickness; float rIn = MathF.Max(1f, radius - halfThickness); float dt = sweep / segs; float k = (4f / 3f) * MathF.Tan(dt * 0.25f); - // 1 Move + segs CubicTo + 1 LineTo + segs CubicTo + 1 Close - int nVerbs = 1 + segs + 1 + segs + 1; - int nArgs = 2 + segs * 6 + 2 + segs * 6; - Span verbs = stackalloc byte[nVerbs]; - Span args = stackalloc float[nArgs]; - - int v = 0; - int a = 0; + v = 0; + a = 0; float ca0 = MathF.Cos(startRad); float sa0 = MathF.Sin(startRad); @@ -210,6 +250,5 @@ public static class HbGpuVectorShapes } verbs[v++] = (byte)HbGpuPathVerb.Close; - return cache.EncodeDynamicPath(DesignUpem, verbs[..v], args[..a]); } } diff --git a/tests/RustToDust.Tests/HbGpuNativeTests.cs b/tests/RustToDust.Tests/HbGpuNativeTests.cs index 915b1a2..7809f58 100644 --- a/tests/RustToDust.Tests/HbGpuNativeTests.cs +++ b/tests/RustToDust.Tests/HbGpuNativeTests.cs @@ -166,6 +166,23 @@ public sealed class HbGpuNativeTests 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(); + + long arcGcBefore = GC.GetAllocatedBytesForCurrentThread(); + for (int i = 0; i < 64; i++) + { + CachedBlob hit = HbGpuVectorShapes.GetStaticArcStroke(blobs, staticArcKey, -2.356f, 0.785f, 500f, 30f); + Assert.Equal(staticArc.AtlasOffset, hit.AtlasOffset); + } + long arcGcAfter = GC.GetAllocatedBytesForCurrentThread(); + Assert.Equal(0, arcGcAfter - arcGcBefore); + Assert.Equal(0, atlas.PendingChunkCount); + // Verify dynamic ring wrap-around for (int i = 0; i < 600; i++) {