TextRenderer, BitmapFont, DebugLineRenderer, and TextureCache's UI-texture
upload path (GetOrUploadRenderSurface/UploadRgba8) now issue every draw and
resource creation through the pinned IGpuDevice/IGpuFrame/IGpuPassEncoder
RHI contract instead of raw GL. This is the RHI's first real production
consumer - V0-V3 only established the contract, GL backend skeleton, and a
shader-dialect migration with no live GL exercise. TextRenderer owns one
IGpuPipeline (ui_text shader, straight-alpha blend, depth disabled) and
allocates a per-bucket ring each Flush; BitmapFont's atlas texture is
created and uploaded via device.CreateTexture/.Upload; DebugLineRenderer
mirrors the same one-pipeline-per-Flush shape for its line-list draws.
World-path TextureCache methods (GetOrUpload, the raw-GL layer-array
upload) are untouched - still legacy GL, still out of scope.
Frame lifecycle: GpuDeviceFrameLifetime (RenderFrameOrchestrator.cs) wraps
IGpuDevice.BeginFrame()/IGpuFrame.End() inside the existing
IRenderFrameLifetime bracket HostInputCameraCompositionPhase already opens
per callback, additively - no frame-graph restructuring. Ported renderers
reach the frame via ICurrentGpuFrameSource, a plain interface (not a
delegate field) so WorldSceneDiagnosticsController keeps passing its
existing "no stored window/delegate" architectural-conformance test.
Two real bugs surfaced by actually exercising the RHI against a live GL
context (nothing here was previously reachable before this slice):
- GlGpuDevice.BeginFrame() now resets the render-state cache every frame.
The cache assumes it is the sole writer of GL program/blend/depth/cull
state, which was true while it had zero real consumers, but every
still-legacy renderer (WbDrawDispatcher, terrain, particles, EnvCells)
mutates that same GL state directly and never informs the cache. Once a
legacy renderer ran between two RHI binds, the cache's belief about the
current GL program went stale, so a later BindPipeline(text shader)
skipped re-issuing glUseProgram and the following push-constant upload
threw GL_INVALID_OPERATION against whatever program was actually bound.
Reset() at the frame boundary is the same defensive move BeginPass
already makes after a forced clear (see its comment); it costs one
redundant state application on the frame's first bind.
- GL_MULTISAMPLE has no representation in the pinned contract. Added a
GL-backend-internal Multisample field to GlRenderStateSnapshot/Changes,
computed from GpuPipelineDescription.SampleCount at BindPipeline time -
mirrors how Vulkan bakes MSAA into the pipeline instead of a separate
toggle.
Collateral, scoped to keep the port real rather than a stub:
- GpuTextureSlot (Unassigned = uint.MaxValue, NOT 0) now flows through
every consumer of TextureCache.GetOrUploadRenderSurface/UploadRgba8 and
TextRenderer.DrawSprite - the entire retained UI layer, since a pervasive
Func<uint,(uint,int,int)> sprite-resolve delegate threads through nearly
every UI element/controller. Every prior `== 0` / `!= 0` "no texture"
check became `.IsAssigned` / `!.IsAssigned`; slot 0 is a real assigned
slot (the device's default white texture), so the old sentinel would
have produced live visual regressions if left in place.
- GpuTextureSlot/IGpuDevice/IGpuFrame are internal, so ~270 previously
public AcDream.App types that touched them (directly or transitively)
are now internal too - safe, since AcDream.App is an exe with no
external project references; only the two test projects consume it, via
InternalsVisibleTo. A handful of unrelated types the sweep caught
(ElementInfo/ImportedLayout's property-bag hierarchy, several enums used
as public [Theory] parameters, CursorFeedbackSnapshot's DragAcceptState)
were reverted back to public where making them internal would have
either cascaded into unrelated files or broken xUnit's public-member
discovery.
- ExternalViewportTextureBridge (new) registers the still-raw-GL FBO
color textures PrivateEntityViewportRenderer/PaperdollViewportRenderer
produce (V4g's scope) into the device's texture table for
UiViewport.TextureHandle, via a temporary
GlGpuDevice.RegisterExternalColorTexture escape hatch (internal, not
part of IGpuDevice) deleted when V4g ports those viewports.
- TextRenderGlStateScope.cs and its test deleted: the pipeline description
now bakes what it used to restore by hand.
- ResourceCleanupGroupTests/GlTextureOwnershipTests: the two source-text
conformance tests keyed to TextRenderer's old multi-resource
construction shape (Shader + per-flight FrameBufferSet array + white
texture + tracked VAO/VBO, all via ResourceCleanupGroup) no longer apply
- that shape is gone, replaced by one IGpuPipeline created through
IGpuDevice. The construction-order test is deleted; the checked-commit
texture-creation check now targets GlGpuTexture (which already used
the same GlResourceCommand.CreateName primitive before this slice).
Gates:
- dotnet build -c Release: 0 warnings, 0 errors (AcDream.App has
TreatWarningsAsErrors).
- dotnet test tests/AcDream.App.Tests -c Release: 3,840 passed / 3
skipped (was 3,843/3 entering this slice - net 3 fewer tests:
TextRendererFailureSafetyTests.cs deleted (2, tested the now-deleted
TextRenderGlStateScope) plus the one retired ResourceCleanupGroupTests
method). Full solution: 8,908 passed / 5 skipped across all nine test
projects.
- Offline pixel gate (tools/run-offline-pixel-gate.ps1, parent ec414d60
vs this commit): differing fraction 0.318% (1,791/563,200 compared
pixels), above the 0.001 threshold. Investigated pixel-by-pixel rather
than waved through: a diff heatmap plus 4x crops at the differing
clusters show zero differences anywhere in the retained UI, terrain,
scenery, or static meshes - every differing pixel sits on continuously-
animated ambient content (flying-insect sprites over the swamp, foliage
sparkle/dew glints) whose exact phase depends on elapsed wall-clock
time, the same category the gate's own sky-masking rationale already
documents and the campaign doc's coverage table explicitly excludes
("Not covered - particles"). Confirming evidence: two same-commit
captures at HEAD compare clean against each other (0.0025%), and two
same-commit captures at the parent compare clean against each other
(0.0044%) - only base-vs-head is consistently elevated, which is what
frame-pacing drift from genuinely new per-frame RHI work (BeginFrame,
ring resets, the render-state reset above) would produce against a
fixed wall-clock capture deadline, not a rendering defect. Recommend a
quick user visual check of this capture pair alongside the automated
result, matching how V2c's particle work was already handled in this
campaign (flagged for user visual confirmation rather than blocked on
an automated gate that cannot cover animated content).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
206 lines
8.9 KiB
C#
206 lines
8.9 KiB
C#
using System;
|
||
using System.Numerics;
|
||
|
||
namespace AcDream.App.UI;
|
||
|
||
/// <summary>
|
||
/// Rectangular container with an optional translucent background and
|
||
/// border. Used as the base of every retail panel (attributes, chat,
|
||
/// inventory, login, etc.).
|
||
///
|
||
/// Retail has panel background art stored as 9-slice sprite assets in
|
||
/// the <c>0x06xxxxxx</c> RenderSurface range, and composed via
|
||
/// <c>LayoutDesc</c> (<c>0x21xxxxxx</c>) trees. Until our
|
||
/// <c>AcFont</c>/<c>UiSpriteBatch</c> consumes those directly, we draw a
|
||
/// simple translucent rectangle so panels are visible during development.
|
||
/// </summary>
|
||
internal class UiPanel : UiElement
|
||
{
|
||
/// <summary>Background fill color. Set <see cref="Vector4.Zero"/> to skip.</summary>
|
||
public Vector4 BackgroundColor { get; set; } = new(0f, 0f, 0f, 0.55f);
|
||
|
||
/// <summary>Border color. Set <see cref="Vector4.Zero"/> to skip.</summary>
|
||
public Vector4 BorderColor { get; set; } = new(0.15f, 0.15f, 0.2f, 0.8f);
|
||
|
||
public float BorderThickness { get; set; } = 1f;
|
||
|
||
/// <summary>Optional dat RenderSurface id for the panel background sprite, drawn
|
||
/// in place of (or alongside) <see cref="BackgroundColor"/>. 0 = none.
|
||
/// When set, the sprite is stretched to fill the panel rect.
|
||
/// Used by the attribute-list selected-row highlight (sprite 0x06001397 = Button state 6).</summary>
|
||
public uint BackgroundSprite { get; set; }
|
||
|
||
/// <summary>Resolves a dat RenderSurface id to (GL tex handle, pixel width, pixel height).
|
||
/// Required when <see cref="BackgroundSprite"/> is non-zero.</summary>
|
||
public Func<uint, (GpuTextureSlot tex, int w, int h)>? SpriteResolve { get; set; }
|
||
|
||
protected override void OnDraw(UiRenderContext ctx)
|
||
{
|
||
if (BackgroundSprite != 0 && SpriteResolve is { } sr)
|
||
{
|
||
var (tex, tw, th) = sr(BackgroundSprite);
|
||
if (tex.IsAssigned && tw != 0 && th != 0)
|
||
ctx.DrawSprite(tex, 0, 0, Width, Height, 0, 0, Width / tw, Height / th, Vector4.One);
|
||
}
|
||
else if (BackgroundColor.W > 0f)
|
||
{
|
||
// Panel fills are backgrounds. Draw them through the sprite/fill
|
||
// bucket so children that render as sprites/dat-font glyphs stay on
|
||
// top in painter order; DrawRect flushes after sprites and would
|
||
// cover text/icons.
|
||
ctx.DrawFill(0, 0, Width, Height, BackgroundColor);
|
||
}
|
||
|
||
if (BorderColor.W > 0f && BorderThickness > 0f)
|
||
ctx.DrawRectOutline(0, 0, Width, Height, BorderColor, BorderThickness);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Static text label. Draws a single line of text using the context's
|
||
/// default font (or an override). Does not consume input.
|
||
///
|
||
/// Equivalent retail primitive: wide-string appended to a CString via
|
||
/// <c>FUN_0040b8f0</c> then drawn by the widget's draw method through
|
||
/// <c>FUN_00698330</c>.
|
||
/// </summary>
|
||
internal class UiLabel : UiElement
|
||
{
|
||
public string Text { get; set; } = string.Empty;
|
||
public Vector4 TextColor { get; set; } = new(1f, 1f, 1f, 1f);
|
||
|
||
public UiLabel() { ClickThrough = true; }
|
||
|
||
protected override void OnDraw(UiRenderContext ctx)
|
||
=> ctx.DrawString(Text, 0, 0, TextColor);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Simple clickable button: panel background + centered label + click
|
||
/// callback. Retail equivalent is Keystone's button widget, driven by
|
||
/// a <c>StateDesc</c> per <c>UIStateId</c> (normal / hot / pressed /
|
||
/// disabled) from the panel layout.
|
||
/// Note: the dat-widget button (Type 1 / UIElement_Button) is <see cref="AcDream.App.UI.UiButton"/>
|
||
/// in <c>UiButton.cs</c> — that is the production widget used by D.2b panels.
|
||
/// This class is the earlier dev-scaffold button (plain rect + text; no dat sprites).
|
||
/// </summary>
|
||
internal class UiSimpleButton : UiPanel
|
||
{
|
||
public string Text { get; set; } = string.Empty;
|
||
public Vector4 TextColor { get; set; } = new(1f, 1f, 1f, 1f);
|
||
public event System.Action? Click;
|
||
|
||
public UiSimpleButton()
|
||
{
|
||
BackgroundColor = new Vector4(0.1f, 0.1f, 0.15f, 0.8f);
|
||
BorderColor = new Vector4(0.45f, 0.45f, 0.55f, 1f);
|
||
}
|
||
|
||
public override bool OnEvent(in UiEvent e)
|
||
{
|
||
if (e.Type == UiEventType.Click && Enabled)
|
||
{
|
||
Click?.Invoke();
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
protected override void OnDraw(UiRenderContext ctx)
|
||
{
|
||
base.OnDraw(ctx);
|
||
if (Text.Length == 0 || ctx.DefaultFont is null) return;
|
||
|
||
float textW = ctx.DefaultFont.MeasureWidth(Text);
|
||
float tx = (Width - textW) * 0.5f;
|
||
float ty = (Height - ctx.DefaultFont.LineHeight) * 0.5f;
|
||
ctx.DrawString(Text, tx, ty, TextColor);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// A <see cref="UiPanel"/> that fires an <see cref="OnClick"/> callback when the user
|
||
/// left-clicks it. Used for the attribute-list rows in the Character window — each row
|
||
/// is a transparent container that needs to respond to pointer hits while its children
|
||
/// (icon, name, value) are ClickThrough decorations.
|
||
///
|
||
/// <para>Retail analog: the <c>AttributeInfoRegion</c> row widget in <c>gmAttributeUI</c>
|
||
/// catches <c>UIEvent_LeftClick</c> (0x01) and calls <c>SetSelectedAttribute</c> on the
|
||
/// parent window. In acdream we wire the equivalent via this action callback instead of
|
||
/// the retail message bus.</para>
|
||
///
|
||
/// <para>When <see cref="UseSelectionBars"/> is true and <see cref="UiPanel.BackgroundSprite"/>
|
||
/// is non-zero, draws the sprite as a thin full-width bar at the TOP and BOTTOM edges of
|
||
/// the row (not stretched to fill). This matches retail's selection highlight which shows
|
||
/// a horizontal dark bar on both the top and bottom edge of the selected attribute row,
|
||
/// with NO left/right end-caps. Bar height is <see cref="SelectionBarHeight"/> pixels
|
||
/// (default 3px).</para>
|
||
/// </summary>
|
||
internal class UiClickablePanel : UiPanel
|
||
{
|
||
/// <summary>Called when the user releases the left mouse button over this panel.</summary>
|
||
public Action? OnClick { get; set; }
|
||
|
||
/// <summary>When true and <see cref="UiPanel.BackgroundSprite"/> is non-zero, draws
|
||
/// the sprite as a thin horizontal bar at the top AND bottom edges of the panel,
|
||
/// NOT as a full-height stretched fill. Matches retail's selected-row highlight
|
||
/// (sprite 0x06001397 — 300×32 px — shown as bars, not a block fill).
|
||
/// Default false (preserves legacy full-stretch behavior).</summary>
|
||
public bool UseSelectionBars { get; set; }
|
||
|
||
/// <summary>Height in pixels of each selection bar (top and bottom). Default 3px.
|
||
/// Ignored when <see cref="UseSelectionBars"/> is false.</summary>
|
||
public float SelectionBarHeight { get; set; } = 3f;
|
||
|
||
public UiClickablePanel()
|
||
{
|
||
// Rows must receive pointer events — override the UiPanel default (ClickThrough=false,
|
||
// which is the UiElement base default). Explicit for clarity.
|
||
ClickThrough = false;
|
||
}
|
||
|
||
/// <summary>HandlesClick = true ensures this row receives its own Click even when
|
||
/// it is nested inside a Draggable ancestor window frame (e.g. the future character
|
||
/// window with a whole-window drag handle). Without this, the press would be consumed
|
||
/// by the ancestor's drag logic and the Click would never fire.</summary>
|
||
public override bool HandlesClick => true;
|
||
|
||
public override bool OnEvent(in UiEvent e)
|
||
{
|
||
if (e.Type == UiEventType.Click && Enabled)
|
||
{
|
||
OnClick?.Invoke();
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
protected override void OnDraw(UiRenderContext ctx)
|
||
{
|
||
if (UseSelectionBars && BackgroundSprite != 0 && SpriteResolve is { } sr)
|
||
{
|
||
// Draw the selection highlight as a thin bar at the TOP and BOTTOM of the row.
|
||
// The sprite (0x06001397) is 300×32 px — we draw it as horizontal strips at
|
||
// native height (SelectionBarHeight), stretched to full panel width (UV tile
|
||
// horizontally). No left/right end-caps: u0=0, u1=Width/nativeW (UV repeat).
|
||
var (tex, tw, th) = sr(BackgroundSprite);
|
||
if (tex.IsAssigned && tw > 0 && th > 0)
|
||
{
|
||
float barH = SelectionBarHeight;
|
||
float uTile = tw > 0 ? Width / tw : 1f;
|
||
// Top bar: shows the top barH px of the sprite (v = 0 → barH/th).
|
||
float vBot = th > 0 ? barH / th : 1f;
|
||
ctx.DrawSprite(tex, 0f, 0f, Width, barH, 0f, 0f, uTile, vBot, Vector4.One);
|
||
// Bottom bar: shows the bottom barH px of the sprite (v = 1−barH/th → 1).
|
||
float vTop2 = th > 0 ? 1f - barH / th : 0f;
|
||
ctx.DrawSprite(tex, 0f, Height - barH, Width, barH, 0f, vTop2, uTile, 1f, Vector4.One);
|
||
}
|
||
// Selection-bar mode draws no border (rows have BorderColor=Zero by design).
|
||
}
|
||
else
|
||
{
|
||
// Default UiPanel draw: handles BackgroundSprite, BackgroundColor, AND border.
|
||
base.OnDraw(ctx);
|
||
}
|
||
}
|
||
}
|