feat(render): put the retained UI and debug lines on both backends

Campaign V slice V6d, commit 2 of 3. TextRenderer and DebugLineRenderer were the only two renderers speaking the RHI, and both refused any device that was not a GlGpuDevice. They now refuse nothing: this is the first production rendering acdream can do on Vulkan.

Three things had to go.

The loose uniforms. debug_line declared uView and uProjection separately and DebugLineRenderer set them straight against the compiled GL program, because the pinned push-constant block carries one combined matrix and IGpuPassEncoder has no verb for arbitrary named uniforms. That was never portable — Vulkan has no default uniform block at all — so the shader converged on uViewProjection and Flush multiplies on the CPU. System.Numerics is row-vector convention while GLSL reads the floats column-major, which transposes, so the CPU equivalent of the old per-vertex uProjection * uView is view * projection. The product now rounds once per frame rather than once per vertex; these lines only draw when collision wireframes are switched on, so the offline gate sees nothing of it. ui_text's uScreenSize became the block's two spare scalars, uParamA and uParamB, with the same two divisions and the same NDC mapping around them.

The sampling mode. uUseTexture selected between font coverage, RGBA modulate and flat colour, and no field of the 96-byte block means that. It did not need one: which of the two texture-table slots is assigned IS the mode. uTextureIndexB assigned means a single-channel coverage source, uTextureIndexA assigned means an RGBA colour source, neither assigned means the vertex colour alone. GpuTextureSlot.Unassigned is already a loud sentinel for exactly this kind of question, and both branches guard so it never reaches a sampler. That also retired the 1x1 white fill texture: DrawFill routed solid quads through the sprite bucket relying on white times colour, and the untextured branch produces the same value with no texture at all. Multiplying by 1.0 changes no bits, and the gate agrees.

The texture binding. The classic glActiveTexture/glBindTexture path survived V4a because DrawSprite takes an arbitrary texture from sixty-odd widget call sites. But TextureCache had already registered every one of those into the device's table — the classic path was consuming the raw GL name that registration also produced. The UI's currency is now UiTextureTableHandle, a one-based table index whose zero is the same "no texture" every widget already guards on; a raw slot index would have turned all of those guards into silent false negatives, since slot 0 is perfectly valid. One-based rather than the slot itself because GpuTextureSlot is internal to the pinned contract while UiRenderContext.DrawSprite, TextureCache.GetOrUploadRenderSurface and a dozen widget properties are public, and neither publishing a contract type nor converting the retained UI to internal belongs in this slice.

Two consequences worth stating. The two backends disagree about what a 2-D table entry is — GL reconstructs a sampler2D from the bindless handle, Vulkan reads layer 0 of its sampler2DArray descriptor array — and ACDREAM_SAMPLE_2D is the one place that lives. Keeping GL on sampler2D is what leaves the UI's textures exactly as they are, including the paperdoll/appraisal FBO colour texture, which is an externally-owned GL_TEXTURE_2D from the §7.1 transitional seam and cannot become an array before V4g. On the Vulkan side, sampled views are now always layered, which also removes a latent invalid usage V6c shipped: it registered a Type2D offscreen view into a descriptor array whose element type is sampler2DArray.

And one real fix. Sampling through the table means a bound sampler object overrides the texture's own parameters. Nearest-requested UI art used to get its point filtering from a glTexParameter applied before the bindless handle went resident, so registering it with the stock WorldRepeat sampler would have made every retail icon and dat-font glyph silently bilinear. Those now register with a nearest-and-repeat sampler.

Supporting moves: GlGpuDevice.CreatePipeline splices common.glsl the same way Shader does, since an RHI shader that reads the table needs the table declared; GlGpuPassEncoder binds the device's table with the pipeline, which is the GL analogue of Vulkan binding descriptor set 2 per draw, and has to be per-bind because every raw-GL world renderer puts its own privately-numbered table at that binding; and the encoder derives GL_MULTISAMPLE from the pass's SampleCount, which is where the retained UI's hand-rolled glDisable belonged all along. TextRenderGlStateScope is deleted — the encoder's ambient capture restored a strict superset of it — and its failure-safety test follows the guarantee to GlAmbientCapabilityState, which gains a fakeable seam and, with it, the multisample-dimension coverage #249 recorded as missing.

App tests 4,057 passed / 3 skipped, unchanged from commit 1. Offline pixel gate against 871c406b: differing fraction 2.31e-05, 13 pixels of 563,200 compared — below the documented 15-23 pixel same-commit noise band, on a change that redraws every pixel of the retained UI through a different sampling path. The capture was inspected: vitals, spell bar, radar, toolbar icons and slot digits, chat window and Send button all present and correctly placed. Both new .spv pairs compile; the manifest records ui_text and debug_line as Vulkan-ready, leaving six pairs blocked on the world-renderer slices.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Erik 2026-07-28 08:51:32 +02:00
parent 871c406b99
commit f6f58a12db
26 changed files with 763 additions and 615 deletions

View file

@ -250,13 +250,20 @@ public sealed unsafe class TextureCache
/// <c>DefaultPaletteId</c> (same starting palette <see cref="DecodeFromDats"/>
/// uses); non-paletted formats have DefaultPaletteId==0 → palette null. Returns
/// a 1x1 magenta handle on miss.
///
/// <para>Campaign V slice V6d: the returned value is a
/// <see cref="UiTextureTableHandle"/> — a one-based index into the device's
/// global texture table — not a raw GL texture name. Every caller passes it
/// straight to <see cref="TextRenderer.DrawSprite"/>, which samples the
/// table; nothing reads it as a GL name, and on Vulkan there is no GL name.
/// Zero still means "no texture", which is what every widget guards on.</para>
/// </summary>
public uint GetOrUploadRenderSurface(uint renderSurfaceId, out int width, out int height, bool nearest = false)
{
if (_renderSurfaceGpuTextures.TryGetValue(renderSurfaceId, out GpuUiTextureEntry existing))
{
width = existing.Width; height = existing.Height;
return existing.GlName;
return UiTextureTableHandle.FromSlot(existing.Slot);
}
DecodedTexture decoded;
@ -280,7 +287,7 @@ public sealed unsafe class TextureCache
GpuUiTextureEntry entry = UploadUiTexture(decoded, nearest, $"ui-rendersurface-0x{renderSurfaceId:X8}");
_renderSurfaceGpuTextures[renderSurfaceId] = entry;
width = decoded.Width; height = decoded.Height;
return entry.GlName;
return UiTextureTableHandle.FromSlot(entry.Slot);
}
/// <summary>
@ -288,13 +295,16 @@ public sealed unsafe class TextureCache
/// decoded UI sprite/atlas and registers it into the device's global
/// texture table. Every UI-path texture uses REPEAT addressing (existing
/// behaviour — panel fills and tiled chrome sample UVs greater than 1) and
/// a single mip level (UI sprites never mip). The classic texture-unit
/// binding path <see cref="TextRenderer.DrawSprite"/> uses for these
/// (see that class's remarks) samples the texture object directly rather
/// than through a bound sampler object, so filtering must live on the
/// texture itself — <c>GlGpuTexture</c>'s constructor always sets Linear,
/// which is wrong for <paramref name="nearest"/>-requested (pixel-exact)
/// sprites, so it is overridden here exactly as the old raw-GL path did.
/// a single mip level (UI sprites never mip).
///
/// <para>Campaign V slice V6d: the sampler is now what filtering actually
/// comes from. Before this slice the draw bound the texture object directly,
/// so filtering lived on the texture and <paramref name="nearest"/> was
/// applied with a raw <c>glTexParameter</c> before the bindless handle was
/// made resident. Sampling through the table means a bound sampler object
/// overrides those parameters, so a nearest-requested sprite has to be
/// registered with a nearest SAMPLER or every retail icon and dat-font
/// glyph would silently become bilinear.</para>
/// </summary>
private GpuUiTextureEntry UploadUiTexture(DecodedTexture decoded, bool nearest, string debugName)
{
@ -312,19 +322,7 @@ public sealed unsafe class TextureCache
uint glName = ((GlGpuTexture)texture).GlName;
TrackUploadedTexture(glName, decoded.Width, decoded.Height);
// MUST happen BEFORE RegisterTexture below: ARB_bindless_texture
// forbids glTexParameter on a texture once its bindless handle has
// been made resident (GL_INVALID_OPERATION). See BitmapFont's
// constructor for the same fix and full explanation.
if (nearest)
{
_gl.BindTexture(TextureTarget.Texture2D, glName);
_gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
_gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
_gl.BindTexture(TextureTarget.Texture2D, 0);
}
IGpuSampler sampler = _device.CreateSampler(GpuSamplerDescription.WorldRepeat);
IGpuSampler sampler = _device.CreateSampler(nearest ? UiNearestRepeat : GpuSamplerDescription.WorldRepeat);
GpuTextureSlot slot = _device.RegisterTexture(texture, sampler);
return new GpuUiTextureEntry(texture, slot, glName, decoded.Width, decoded.Height);
}
@ -335,6 +333,19 @@ public sealed unsafe class TextureCache
}
}
/// <summary>
/// Point sampling with REPEAT addressing — pixel-exact retail UI art that is
/// still tiled by nine-slice chrome and meter tracks. Neither stock preset
/// fits: <c>UiNearest</c> clamps, <c>WorldRepeat</c> filters.
/// </summary>
private static readonly GpuSamplerDescription UiNearestRepeat = new(
GpuFilter.Nearest,
GpuFilter.Nearest,
GpuMipFilter.None,
GpuAddressMode.Repeat,
GpuAddressMode.Repeat,
MaxAnisotropy: 1f);
/// <summary>
/// Alpha-channel histogram for one decoded texture. Used to diagnose
/// "why are clouds not transparent" — if cloud textures come out with
@ -887,15 +898,20 @@ public sealed unsafe class TextureCache
/// <summary>Uploads a raw RGBA8 byte array as a Texture2D. Used by
/// <see cref="AcDream.App.UI.IconComposer"/> to upload CPU-composited icon layers.
/// The returned handle is tracked in <see cref="_adhocGpuTextures"/> and deleted by
/// <see cref="Dispose"/>. Callers must NOT also store the handle in any of the
/// keyed caches — that would cause a double-delete on Dispose.</summary>
/// The texture is tracked in <see cref="_adhocGpuTextures"/> and deleted by
/// <see cref="Dispose"/>. Callers must NOT also store the returned handle in any
/// of the keyed caches — that would cause a double-delete on Dispose.
///
/// <para>Campaign V slice V6d: returns a <see cref="UiTextureTableHandle"/>
/// rather than a GL texture name, for the reason given on
/// <see cref="GetOrUploadRenderSurface"/>.</para>
/// </summary>
public uint UploadRgba8(byte[] rgba, int width, int height, bool nearest = false)
{
GpuUiTextureEntry entry = UploadUiTexture(
new DecodedTexture(rgba, width, height), nearest, "ui-adhoc-rgba8");
_adhocGpuTextures.Add(entry);
return entry.GlName;
return UiTextureTableHandle.FromSlot(entry.Slot);
}
private uint UploadRgba8(DecodedTexture decoded, bool nearest = false)