feat(render): Campaign V slice V4a - port TextRenderer/BitmapFont/DebugLineRenderer/TextureCache UI path onto IGpuDevice
Second attempt at V4a afterceec3bc4was reverted at9aaf97e7for losing world multisampling and a 334-file scope explosion. This lands the same functional slice with a much smaller footprint and the two structural fixes the revert postmortem (docs/plans/2026-07-27-vulkan-campaign.md SS7.1) called for. What moved onto the RHI: - TextRenderer: the ui_text shader now compiles through IGpuDevice.CreatePipeline (one IGpuPipeline, replacing the old hand-rolled Shader class); its three fence-buffered per-flight VBOs are gone in favour of a per-IGpuFrame ring allocation per draw bucket; its 1x1 white fill texture is created via IGpuDevice.CreateTexture and registered into the device's texture table. Flush keeps TextRenderGlStateScope and the manual GL disable block verbatim (TextRendererFailureSafetyTests pins their literal presence) alongside the new pipeline bind - both target the identical final GL state, so this is redundant, not contradictory. Sprite/font texture binding stays classic (glActiveTexture/glBindTexture) because DrawSprite receives arbitrary externally-owned GL texture names from dozens of UI call sites outside this slice's scope; IGpuPassEncoder has no verb for that, by design (every other RHI consumer samples through the bindless texture table). - BitmapFont: the stb-baked R8 atlas is created/uploaded through IGpuDevice.CreateTexture; TextureId stays a raw GL name extracted from the IGpuTexture, since its only consumer is TextRenderer's classic path above. - DebugLineRenderer: the debug_line shader compiles through IGpuDevice.CreatePipeline (LineList topology, depth disabled); Flush ring- allocates its vertex data and draws through IGpuPassEncoder. uView/uProjection don't fit the shared GpuPushConstants block (one combined VP matrix) so they are set directly on the pipeline's compiled program, mirroring TextRenderer. - TextureCache: GetOrUploadRenderSurface and the public UploadRgba8(byte[],...) wrapper now create IGpuTexture+GpuTextureSlot internally, extracting the raw GL name for their unchanged uint return type - DrawSprite's signature and its 16 call sites across the UI are untouched. The world-material path (GetOrUpload, the raw layer-array upload) is untouched. - UiViewport: TextureHandle (uint) -> TextureSlot (GpuTextureSlot), resolved back to a raw GL name via TextRenderer.ResolveExternalTextureSlot at draw time. Its texture is produced by PaperdollViewportRenderer/ PrivateEntityViewportRenderer, both still raw GL until V4g, so RetailPaperdollFrameView/RetailCreatureAppraisalFrameView register it through the pre-approved GlGpuDevice.RegisterExternalColorTexture transitional seam (campaign doc SS7.1's final paragraph) instead of inventing anything broader. The two revert-postmortem fixes, both in Gpu/Gl (never in the pinned Gpu/ contract): - GlGpuDevice.BeginPass now resets the render-state cache unconditionally on every pass, not only a clearing one. The first attempt's crash came from exactly this gap: a raw-GL renderer running between two RHI passes changes GL program/blend/depth/cull state the cache never observes, so a later BindPipeline skipped re-issuing glUseProgram and the following push-constant upload threw GL_INVALID_OPERATION. - GlGpuPassEncoder now captures ambient GL capability state (program, VAO, array buffer, texture0 binding, depth test/write/func, blend enable+func, cull enable+mode, front face, alpha-to-coverage, multisample) on construction and restores it on Dispose, generalizing what TextRenderGlStateScope already did for TextRenderer specifically to every RHI pass - this is what stops DebugLineRenderer's pipeline bind (which has no scope of its own) from leaking state into the next raw-GL renderer. Both are marked transitional, deleted at V4h once nothing raw-GL remains. Frame lifecycle (additive, per the task's own description of this piece): new GpuDeviceFrameLifetime wraps IGpuDevice.BeginFrame()/IGpuFrame.End() and exposes the open frame via ICurrentGpuFrameSource. RenderFrameOrchestrator's IRenderFrameLifetime now routes through this wrapper instead of calling GpuFrameFlightController directly - GlGpuDevice.BeginFrame already calls straight through to that same controller, so the fence/slot-rotation contract is unchanged; the wrapper only additionally yields the IGpuFrame ported renderers need. No clears moved, no framebuffer binding changed, frame-graph phase order is untouched. The two now-dead per-slot TextRenderer.BeginFrame(int) calls in RuntimeRenderFrameBeginResources are removed. The UI Studio (RenderBootstrap/StudioWindow) gets its own independent RHI device+lifetime, mirroring the production composition. Real bug found and fixed while exercising this for the first time: both BitmapFont and TextureCache's nearest-filter override called TexParameter AFTER RegisterTexture, which made the bindless handle resident - GL_ARB_ bindless_texture forbids modifying a texture's parameters once its handle is resident, so this threw GL_INVALID_OPERATION building the retained UI's own TextRenderer. Fixed by moving both TexParameter blocks before RegisterTexture. Scope note: touches 25 files (24 modified + this commit's one new file), not the ~10 the brief estimated, because the frame-lifecycle wiring and the viewport escape hatch (both explicitly asked for) ripple through five composition files and two frame presenters that thread IGpuDevice/ ICurrentGpuFrameSource to construction sites. No file outside that necessary set was touched: no visibility sweep beyond the specific constructors/ properties whose new parameter types are internal (TextRenderer/BitmapFont/ DebugLineRenderer/UiHost's constructors, TextureCache's otherwise-orphaned convenience overload, UiViewport.TextureSlot), no world-mesh/terrain/particle/ sky file touched, no test deleted or weakened - three source-text conformance tests (TextRendererPublishesEveryConstructorResourceBeforeLaterGlWork, GlTextureOwnershipTests' TextRenderer.cs check, and RenderFrameResourceControllerTests' frame-order check) were replaced with equivalent assertions against the new construction/wiring shape, since their pinned invariant was specifically the old raw-GL shape this slice legitimately replaces. Gates: - dotnet build -c Release: 0 warnings, 0 errors. - dotnet test tests/AcDream.App.Tests -c Release: 3,843 passed / 3 skipped - exactly the baseline. Complete solution: 8,906 passed / 5 skipped across all nine test projects. - Offline pixel gate (tools/run-offline-pixel-gate.ps1, parenta97e04aevs this commit): 26 differing pixels of 563,200 compared (fraction 4.62e-05), pass against the 0.001/563-pixel threshold. Verified against a same-commit control (two captures at this commit differ by 20 pixels) rather than accepted at face value - the two numbers are in the same band, confirming this is normal animated-content/frame-pacing noise and not the systematic silhouette-edge loss (1,791 pixels, 224x higher) the first attempt's revert diagnosed. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
a97e04ae3d
commit
096dd203fa
26 changed files with 954 additions and 467 deletions
|
|
@ -1,5 +1,7 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using AcDream.App.Rendering.Gpu;
|
||||
using AcDream.App.Rendering.Gpu.Gl;
|
||||
using Silk.NET.OpenGL;
|
||||
using StbTrueTypeSharp;
|
||||
|
||||
|
|
@ -7,9 +9,17 @@ namespace AcDream.App.Rendering;
|
|||
|
||||
/// <summary>
|
||||
/// A pixel-font atlas rasterized from a TTF at load time using stb_truetype.
|
||||
/// Glyphs are packed into a single-channel (R8) GL texture. Call
|
||||
/// Glyphs are packed into a single-channel (R8) atlas. Call
|
||||
/// <see cref="TryGetGlyph"/> to resolve an ASCII codepoint to UV + metrics.
|
||||
///
|
||||
/// Campaign V slice V4a: the atlas is created and uploaded through
|
||||
/// <see cref="IGpuDevice.CreateTexture"/> instead of raw GL, and registered
|
||||
/// into the device's texture table. <see cref="TextureId"/> stays a raw GL
|
||||
/// name — extracted from the created <see cref="IGpuTexture"/> — because its
|
||||
/// only consumer (<see cref="TextRenderer"/>'s classic sprite/font texture-unit
|
||||
/// binding path; see that class's remarks) is not itself slot-based this
|
||||
/// slice.
|
||||
///
|
||||
/// Only printable ASCII (32..127) is supported for the debug overlay.
|
||||
/// </summary>
|
||||
public sealed unsafe class BitmapFont : IDisposable
|
||||
|
|
@ -34,11 +44,10 @@ public sealed unsafe class BitmapFont : IDisposable
|
|||
}
|
||||
}
|
||||
|
||||
private readonly GL _gl;
|
||||
private readonly Glyph[] _glyphs;
|
||||
private readonly int _firstChar;
|
||||
private readonly int _numChars;
|
||||
private readonly ResourceCleanupGroup _resources;
|
||||
private readonly IGpuTexture _texture;
|
||||
|
||||
public uint TextureId { get; }
|
||||
public float PixelHeight { get; }
|
||||
|
|
@ -47,10 +56,14 @@ public sealed unsafe class BitmapFont : IDisposable
|
|||
public int AtlasWidth { get; }
|
||||
public int AtlasHeight { get; }
|
||||
|
||||
public BitmapFont(GL gl, byte[] ttfBytes, float pixelHeight,
|
||||
// internal, not public: IGpuDevice is an internal type (the pinned RHI
|
||||
// contract). BitmapFont stays public — only construction is restricted —
|
||||
// so existing public members that hold or return a BitmapFont need no
|
||||
// visibility change of their own.
|
||||
internal BitmapFont(IGpuDevice device, byte[] ttfBytes, float pixelHeight,
|
||||
int atlasSize = 512, int firstChar = 32, int numChars = 96)
|
||||
{
|
||||
_gl = gl;
|
||||
ArgumentNullException.ThrowIfNull(device);
|
||||
PixelHeight = pixelHeight;
|
||||
AtlasWidth = atlasSize;
|
||||
AtlasHeight = atlasSize;
|
||||
|
|
@ -96,65 +109,52 @@ public sealed unsafe class BitmapFont : IDisposable
|
|||
adv: bc.xadvance);
|
||||
}
|
||||
|
||||
// Upload atlas as a single-channel GL texture (R8). Publish the GL
|
||||
// name into the construction ledger before any later upload/state
|
||||
// command can fail.
|
||||
var resources = new ResourceCleanupGroup();
|
||||
uint texture = 0;
|
||||
// Upload atlas as a single-channel texture (R8) through the device.
|
||||
IGpuTexture texture = device.CreateTexture(new GpuTextureDescription(
|
||||
"bitmap-font-atlas",
|
||||
GpuTextureKind.Texture2D,
|
||||
GpuTextureFormat.R8Unorm,
|
||||
Width: AtlasWidth,
|
||||
Height: AtlasHeight,
|
||||
LayerCount: 1,
|
||||
MipLevelCount: 1));
|
||||
try
|
||||
{
|
||||
texture = GlResourceCommand.CreateTexture(_gl, "BitmapFont atlas");
|
||||
uint ownedTexture = texture;
|
||||
resources.Add(
|
||||
"bitmap-font atlas",
|
||||
() => GlResourceCommand.DeleteTexture(
|
||||
_gl,
|
||||
ownedTexture,
|
||||
$"delete BitmapFont atlas {ownedTexture}"));
|
||||
_gl.GetInteger(GetPName.TextureBinding2D, out int previousTexture);
|
||||
_gl.GetInteger(GetPName.UnpackAlignment, out int previousAlignment);
|
||||
GlResourceCommand.Execute(_gl, "initialize BitmapFont atlas", () =>
|
||||
fixed (byte* ptr = pixels)
|
||||
texture.Upload(0, 0, new ReadOnlySpan<byte>(ptr, AtlasWidth * AtlasHeight));
|
||||
uint glNameForWrapFixup = ((GlGpuTexture)texture).GlName;
|
||||
|
||||
// The classic texture-unit binding path TextRenderer's font draws
|
||||
// use (see its class remarks) samples this texture object
|
||||
// directly rather than through a bound GL sampler object, so wrap
|
||||
// addressing must live on the texture itself — GlGpuTexture's
|
||||
// constructor only sets the filter, not wrap mode (GL 4.3
|
||||
// defaults textures to GL_REPEAT). Retail's dat font glyph UVs
|
||||
// never leave their own tight atlas sub-rect, but pin ClampToEdge
|
||||
// explicitly to preserve the exact sampling behaviour the raw-GL
|
||||
// constructor had. MUST happen BEFORE RegisterTexture below:
|
||||
// ARB_bindless_texture forbids glTexParameter on a texture once
|
||||
// its bindless handle has been made resident (GL_INVALID_OPERATION).
|
||||
if (device is GlGpuDevice glDeviceForWrapFixup)
|
||||
{
|
||||
try
|
||||
{
|
||||
_gl.BindTexture(TextureTarget.Texture2D, texture);
|
||||
_gl.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
|
||||
fixed (byte* ptr = pixels)
|
||||
{
|
||||
_gl.TexImage2D(TextureTarget.Texture2D, 0,
|
||||
(int)InternalFormat.R8,
|
||||
(uint)AtlasWidth, (uint)AtlasHeight, 0,
|
||||
PixelFormat.Red, PixelType.UnsignedByte, ptr);
|
||||
}
|
||||
_gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter,
|
||||
(int)TextureMinFilter.Linear);
|
||||
_gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter,
|
||||
(int)TextureMagFilter.Linear);
|
||||
_gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS,
|
||||
(int)TextureWrapMode.ClampToEdge);
|
||||
_gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT,
|
||||
(int)TextureWrapMode.ClampToEdge);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_gl.PixelStore(
|
||||
PixelStoreParameter.UnpackAlignment,
|
||||
previousAlignment);
|
||||
_gl.BindTexture(
|
||||
TextureTarget.Texture2D,
|
||||
unchecked((uint)previousTexture));
|
||||
}
|
||||
});
|
||||
GL gl = glDeviceForWrapFixup.Gl;
|
||||
gl.BindTexture(TextureTarget.Texture2D, glNameForWrapFixup);
|
||||
gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
|
||||
gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
|
||||
gl.BindTexture(TextureTarget.Texture2D, 0);
|
||||
}
|
||||
|
||||
IGpuSampler sampler = device.CreateSampler(GpuSamplerDescription.WorldClamp);
|
||||
device.RegisterTexture(texture, sampler);
|
||||
}
|
||||
catch (Exception constructionFailure)
|
||||
catch
|
||||
{
|
||||
resources.RollbackConstructionAndThrow(
|
||||
"BitmapFont construction failed and its GL atlas did not cleanly roll back.",
|
||||
constructionFailure);
|
||||
texture.Dispose();
|
||||
throw;
|
||||
}
|
||||
|
||||
TextureId = texture;
|
||||
_resources = resources;
|
||||
_texture = texture;
|
||||
TextureId = ((GlGpuTexture)texture).GlName;
|
||||
}
|
||||
|
||||
public bool TryGetGlyph(char c, out Glyph g)
|
||||
|
|
@ -183,7 +183,7 @@ public sealed unsafe class BitmapFont : IDisposable
|
|||
|
||||
public void Dispose()
|
||||
{
|
||||
_resources.RetryCleanup();
|
||||
_texture.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue