This reverts ceec3bc4. Two independent reasons, either sufficient.
The rendering regression. The slice deleted TextRenderGlStateScope, which
saved GL_MULTISAMPLE and GL_SAMPLE_ALPHA_TO_COVERAGE on entry, disabled them
for the text pass, and restored them on exit (TextRenderGlStateScope.cs:111-112
and 153-154 at the parent commit). Its replacement bakes that state into the
text pipeline but nothing restores it, and GlGpuPassEncoder.Dispose does not
either. Every world renderer is still raw GL at this point in the campaign, so
from the first UI frame onward the world drew with multisampling disabled.
The offline pixel gate caught it: 1,791 of 563,200 compared pixels differed,
0.318% against a 0.001 threshold. The commit message attributed this to
wall-clock-driven ambient animation shifting phase, and committed through the
failure. That explanation does not survive its own control: capturing twice at
the reverted-to commit differs by 19 pixels and twice at the slice's own commit
by 8, while base-versus-head differs by 1,791 - a 224x gap that no shared-noise
source explains. An amplified difference image settles it visually: the changed
pixels are the silhouette edges of every tree, building and rock, with terrain
interiors, water and the entire UI untouched. That is the signature of losing
edge antialiasing, not of animated sprites.
This is the exact failure mode two existing memory notes already warn about -
a mid-frame renderer must set every GL state it uses rather than inherit it,
and issue #52's lesson that a rendering migration must audit per-pass GL state
before declaring itself done.
The scope. The brief was three small leaf renderers plus additive frame-
lifecycle wiring, roughly ten files. The commit changed 334 files with 3,665
insertions and 3,845 deletions, including 323 public-to-internal visibility
conversions across the App assembly, 55 test files, two retired conformance
tests, and a self-described temporary escape hatch for bridging raw-GL viewport
textures. Even without the regression, that is not separable into the part
worth keeping and the part worth dropping.
Reverting rather than patching because the good work here - the RHI frame
lifecycle wiring and a genuine render-state-cache staleness fix - is small
enough to redo cleanly against a tightened spec, while untangling it from 300+
files of unrelated churn is not.
Post-revert: Release build clean, App suite back to 3,843 passed / 3 skipped,
offline pixel gate passing at 19 differing pixels.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
174 lines
7.5 KiB
C#
174 lines
7.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Numerics;
|
|
using AcDream.App.Rendering;
|
|
using AcDream.Content;
|
|
using DatReaderWriter;
|
|
using DatReaderWriter.DBObjs;
|
|
using DatReaderWriter.Types;
|
|
|
|
namespace AcDream.App.UI;
|
|
|
|
/// <summary>
|
|
/// A retail dat-font (DB_TYPE_FONT, id range 0x40000000-0x40000FFF) ready for
|
|
/// 2D drawing. Holds the two GL atlas textures (foreground glyph pixels +
|
|
/// background outline/shadow), the per-glyph descriptor table, and the line
|
|
/// metrics, so <see cref="UiRenderContext.DrawStringDat"/> can blit each glyph
|
|
/// as two textured quads exactly the way the retail client does.
|
|
///
|
|
/// <para>
|
|
/// Retail render model — <c>SurfaceWindow::DrawCharacter</c>
|
|
/// (acclient 0x00442bd0, Font::GetCharDesc + the two SurfaceWindow blits): for
|
|
/// each glyph it copies the BACKGROUND atlas sub-rect first, tinted with the
|
|
/// outline color (black), then the FOREGROUND atlas sub-rect, tinted with the
|
|
/// requested text color. The pen advances by
|
|
/// <c>HorizontalOffsetBefore + Width + HorizontalOffsetAfter</c> (the function's
|
|
/// return value, accumulated by the string loop at 0x00467ed4
|
|
/// <c>edi_3 += var_98</c>), and each glyph is drawn starting at
|
|
/// <c>penX + HorizontalOffsetBefore</c>.
|
|
/// </para>
|
|
///
|
|
/// <para>
|
|
/// Atlas format: the foreground atlas (0x06005EE5 for Font 0x40000000) is
|
|
/// PFID_A8 — alpha-only. Our <c>SurfaceDecoder</c> expands A8 to RGBA as
|
|
/// (255,255,255, alpha). The UI sprite shader path (ui_text.frag,
|
|
/// <c>uUseTexture==2</c>) MULTIPLIES the sampled texel by the per-vertex tint
|
|
/// (<c>texture(uTex,vUv) * vColor</c>), so tinting a white+alpha glyph by a
|
|
/// color gives that color with the glyph's alpha — black for the outline pass,
|
|
/// text color for the fill pass. No shader change was needed.
|
|
/// </para>
|
|
/// </summary>
|
|
public sealed class UiDatFont
|
|
{
|
|
/// <summary>Retail UI font id (Latin-1, 16x16 max, with outline atlas).</summary>
|
|
public const uint DefaultFontId = 0x40000000u;
|
|
|
|
/// <summary>Foreground (glyph pixels) GL texture handle + atlas pixel size.</summary>
|
|
public uint ForegroundTexture { get; }
|
|
public int ForegroundWidth { get; }
|
|
public int ForegroundHeight { get; }
|
|
|
|
/// <summary>Background (outline/shadow) GL texture handle + atlas pixel size.
|
|
/// 0 when the font has no background atlas (then the outline pass is skipped).</summary>
|
|
public uint BackgroundTexture { get; }
|
|
public int BackgroundWidth { get; }
|
|
public int BackgroundHeight { get; }
|
|
|
|
/// <summary>Vertical advance between lines (retail MaxCharHeight).</summary>
|
|
public float LineHeight { get; }
|
|
|
|
/// <summary>Distance from a line's top to its baseline (retail BaselineOffset).</summary>
|
|
public float BaselineOffset { get; }
|
|
|
|
private readonly Dictionary<char, FontCharDesc> _glyphs;
|
|
|
|
internal UiDatFont(
|
|
uint fgTex, int fgW, int fgH,
|
|
uint bgTex, int bgW, int bgH,
|
|
float lineHeight, float baselineOffset,
|
|
Dictionary<char, FontCharDesc> glyphs)
|
|
{
|
|
ForegroundTexture = fgTex; ForegroundWidth = fgW; ForegroundHeight = fgH;
|
|
BackgroundTexture = bgTex; BackgroundWidth = bgW; BackgroundHeight = bgH;
|
|
LineHeight = lineHeight;
|
|
BaselineOffset = baselineOffset;
|
|
_glyphs = glyphs;
|
|
}
|
|
|
|
/// <summary>True if this font carries a separate outline/shadow atlas
|
|
/// (retail's <c>m_pBackgroundSurface</c>). When false the outline pass is
|
|
/// skipped and only the foreground (fill) glyphs are drawn.</summary>
|
|
public bool HasBackground => BackgroundTexture != 0;
|
|
|
|
/// <summary>Look up a glyph descriptor for a character. Returns false for
|
|
/// characters not present in the font's table (callers skip them).</summary>
|
|
public bool TryGetGlyph(char c, out FontCharDesc glyph) => _glyphs.TryGetValue(c, out glyph!);
|
|
|
|
/// <summary>
|
|
/// Load Font <paramref name="fontId"/> from the dat collection and upload
|
|
/// both atlases through the texture cache (the same direct-RenderSurface
|
|
/// path the D.2b chrome sprites use). Returns null if the Font DBObj is
|
|
/// missing — callers fall back to the debug bitmap font.
|
|
/// </summary>
|
|
public static UiDatFont? Load(IDatReaderWriter dats, TextureCache cache, uint fontId = DefaultFontId)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(dats);
|
|
ArgumentNullException.ThrowIfNull(cache);
|
|
|
|
if (!dats.TryGet<Font>(fontId, out var font) || font is null)
|
|
return null;
|
|
|
|
// Foreground atlas is required; without it there are no glyph pixels.
|
|
if (font.ForegroundSurfaceDataId == 0)
|
|
return null;
|
|
|
|
// Point-sample the glyph atlases (nearest) so small UI text stays pixel-crisp;
|
|
// bilinear softens the dat font noticeably (the chat menu/button text "blur").
|
|
uint fgTex = cache.GetOrUploadRenderSurface(font.ForegroundSurfaceDataId, out int fgW, out int fgH, nearest: true);
|
|
|
|
uint bgTex = 0; int bgW = 0, bgH = 0;
|
|
if (font.BackgroundSurfaceDataId != 0)
|
|
bgTex = cache.GetOrUploadRenderSurface(font.BackgroundSurfaceDataId, out bgW, out bgH, nearest: true);
|
|
|
|
// Build the char->descriptor lookup. FontCharDesc.Unicode is the code
|
|
// point; for Latin-1 fonts this is a direct char cast. Last write wins
|
|
// on the rare duplicate (retail's Font::GetCharDesc does a linear scan
|
|
// and returns the first match, but the dat tables have no duplicates).
|
|
var glyphs = new Dictionary<char, FontCharDesc>(font.CharDescs.Count);
|
|
foreach (var cd in font.CharDescs)
|
|
glyphs[(char)cd.Unicode] = cd;
|
|
|
|
return new UiDatFont(
|
|
fgTex, fgW, fgH,
|
|
bgTex, bgW, bgH,
|
|
lineHeight: font.MaxCharHeight,
|
|
baselineOffset: font.BaselineOffset,
|
|
glyphs);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Total pen advance (in pixels) for <paramref name="text"/>, summing each
|
|
/// glyph's retail advance. Characters not in the font contribute nothing.
|
|
/// </summary>
|
|
public float MeasureWidth(string text)
|
|
{
|
|
if (string.IsNullOrEmpty(text)) return 0f;
|
|
|
|
float width = 0f;
|
|
for (int index = 0; index < text.Length; index++)
|
|
{
|
|
if (_glyphs.TryGetValue(text[index], out FontCharDesc? glyph))
|
|
width += GlyphAdvance(glyph);
|
|
}
|
|
|
|
return width;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pure pen-advance summation seam: total width of <paramref name="text"/>
|
|
/// given a <paramref name="lookup"/> that maps each char to its descriptor
|
|
/// (null = not in the font → contributes nothing). Lets the advance math be
|
|
/// unit-tested with synthetic glyphs, with no GL or dat dependency.
|
|
/// </summary>
|
|
public static float MeasureWidth(string? text, Func<char, FontCharDesc?> lookup)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(lookup);
|
|
if (string.IsNullOrEmpty(text)) return 0f;
|
|
float w = 0f;
|
|
for (int i = 0; i < text.Length; i++)
|
|
if (lookup(text[i]) is { } g)
|
|
w += GlyphAdvance(g);
|
|
return w;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The retail per-glyph horizontal advance:
|
|
/// <c>HorizontalOffsetBefore + Width + HorizontalOffsetAfter</c>. This is the
|
|
/// value <c>SurfaceWindow::DrawCharacter</c> returns for proportional text
|
|
/// (flag bit 0x10 set, acclient 0x00442c3a) and the string loop accumulates
|
|
/// into the pen. Pulled out as a pure static so the math is unit-testable
|
|
/// without GL or the dat.
|
|
/// </summary>
|
|
public static float GlyphAdvance(FontCharDesc g)
|
|
=> g.HorizontalOffsetBefore + g.Width + g.HorizontalOffsetAfter;
|
|
}
|