feat(render): Campaign V slice V6j commit 2 - Dereth draws on Vulkan

The three world renderers' submission arms, both pass executors, and the
composition that reaches them. This is the unit three predecessors stopped at.

What it produces. ACDREAM_RENDER_BACKEND=vulkan on the offline scene renders
terrain with blended textures and road overlays, the water edge, static world
meshes, procedural scenery, and the complete retained UI - the same frame the GL
pixel gate captures, from the same camera, minus the sky. artifacts/v6j-vk2.

The shape, and why it is not V4c's. Section 5.5.6 chose option (B) after NVIDIA
rendered the V4c binary 10/10 where AMD's GL stack did not: GL keeps its raw
world path through to V10 as a documented fork confined to the submission seam,
and the RHI world path ships on Vulkan. So V4c's and V4d-2's content returns as a
SECOND arm rather than a replacement. The GL arm issues the same GL statements in
the same order against the same objects; the encoder arm lives in three .Rhi.cs
partials and is entered by one branch per submission site.

Three differences from V4c, each because the tree moved under it. There is no
binding-9 texture table - V4t put the slot on the device and Vulkan binds set 2,
so the arm that used to intern bindless handles simply has nothing to do. The
pipelines carry the device's sample count rather than 1, because Vulkan requires
rasterizationSamples to match the pass and alpha-to-coverage is a no-op at one
sample. And no renderer opens a pass.

That last one is structural, not tidiness. Under MSAA the frame's one backbuffer
pass resolves into the swapchain image and stores DONT_CARE into the multisampled
scratch, so a second pass declaring Load would load undefined contents; the
backend also permits one open pass per frame. VulkanWorldScenePhase therefore
opens the pass, publishes the encoder on VulkanWorldPassScope for exactly the
span of the inner WorldSceneRenderer, and every renderer borrows it.

Three sections are frame-global on GL and cannot be on Vulkan: the SceneLighting
UBO, the per-cell clip regions, and the terrain clip block. GL binds each to a
global binding point and every consumer inherits it. Vulkan binds a descriptor
set per draw, and a renderer's own binds are what select the scope those sections
must land in - so their writers PUBLISH into WorldFrameSections and each renderer
binds them inside the pass, after its own binds. SceneLightingUboBinding's
per-flight-slot buffer pool disappears with it: a ring allocation is already
distinct memory that lives until the frame retires, which is the property the
pool existed to provide.

Both pass executors became backend-neutral rather than gaining twins. Everything
they do is delegation to a renderer except four concerns - the clip-frame
publication, the doorway scissor, gl_ClipDistance enablement, and retail's
interior depth clear - so those four move behind IWorldPassSurface and retail's
ordering, which is what these classes are actually for, is written once. The GL
implementation issues the statements the executors used to issue inline.

Clip distances are no-ops on the Vulkan arm, and that is safe rather than a
divergence: Vulkan activates every element the shader declares, and all three
world vertex shaders already write 1.0 into every slot past the active count.
The interior depth clear becomes vkCmdClearAttachments, reached through the scope
so the pinned contract stays frozen and the backend-only verb stays in the
backend. The hook for it was already committed at V6i-3 with a cref to a type
that did not exist yet; it exists now.

The collision-wireframe DebugLineRenderer is composed as null on the Vulkan arm.
DrawAndPublish flushes it INSIDE the world phase and it opens its own pass, which
the one-pass rule forbids. The toggle is DevTools-only and DevTools is not
composed there, so nothing is lost - composing it would throw on the first
wireframe frame rather than silently misdraw.

Two seams widened rather than invented. GameWindowGraphics answers whether the
backend has a world-pass seam, because the three composition phases that need it
already borrow that handle and "does this backend work that way" is what the type
exists to answer. And MeshSourceReady replaces the anyVao != 0 gate with the same
question in backend-neutral form - V6i-3 published HasStores for exactly this -
so the predicate evaluates identically on GL.

What is NOT here, and is expected. Sky and weather are still raw GL (V4f), so the
Vulkan frame's sky is the atmosphere fog clear. Particles (V4e), the paperdoll and
appraisal viewports and the portal depth mask (V4g) likewise. The executors
already accepted all of them as absent.

Gates. Release build green. App tests 4,112 passed / 3 skipped, the unchanged
baseline; complete Release suite 9,175 / 5. Strict GL offline pixel gate against
847f14ae: 5.50e-05, 31 differing pixels of 563,200, inside the documented 9-31
band and 18x under the threshold. Characterised rather than accepted, because 31
is the band's top: cross-commit pairs measured 21, 29 and 31 while same-commit
controls measured 12 and 20, and maximumChannelDelta is 46-52 in every comparison
INCLUDING the pure controls - so the few large-delta pixels are a property of the
capture, and a cross-commit pair at 21 against a same-commit pair at 20 is not
what a systematic shift looks like. GL connected repeat gate at 3 runs: 3/3
RENDERED on the desktop witness and 3/3 on the client capture. One offline Vulkan
run with VK_LAYER_KHRONOS_validation proven inserted by the loader: zero
validation errors, zero warnings, a captured world frame, and a graceful close.

Coverage gap, stated rather than assumed. The offline scene is a fixed outdoor
view, so EnvCellRenderer's Vulkan arm draws nothing in it - dungeon interiors are
half of this slice and are unproven by anything automated, exactly as they were
for V4c. The deferred-alpha path and the doorway scissor are likewise untouched
by this scene. They join the accumulated user-gate debt in plan section 5.1.

No divergence-register row: no retail-facing behaviour changes.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Erik 2026-07-28 15:46:54 +02:00
parent 81fe5e1b63
commit f84eef3256
22 changed files with 2566 additions and 264 deletions

View file

@ -29,12 +29,16 @@ using Silk.NET.OpenGL;
namespace AcDream.App.Rendering.Wb;
public sealed unsafe class EnvCellRenderer :
public sealed unsafe partial class EnvCellRenderer :
IDisposable,
IEnvCellLandblockPublisher
{
private readonly object _publicationOwner = new();
private readonly GL _gl;
// Campaign V slice V6j: null on the RHI arm. Every GL statement below is
// reached only when this is non-null; the encoder arm lives in
// EnvCellRenderer.Rhi.cs and the GL arm is unchanged.
private readonly GL? _gl;
private readonly ObjectMeshManager _meshManager;
private readonly WbFrustum _frustum;
@ -972,13 +976,17 @@ public sealed unsafe class EnvCellRenderer :
IReadOnlyList<uint>? orderedCellIds)
{
// WB EnvCellRenderManager.cs:400:
if (!_initialized || _shader is null || _shader.Program == 0) return;
if (!_initialized) return;
// Campaign V slice V6j: the RHI arm has no linked program to check — the
// pipeline it draws with was built at construction and the same readiness
// question is answered by _initialized alone.
if (_gl is not null && (_shader is null || _shader.Program == 0)) return;
lock (_renderLock)
{
var snapshot = _activeSnapshot;
// WB EnvCellRenderManager.cs:403-404:
_shader.Use();
_shader?.Use();
// FIX 2026-05-28 (pool aliasing root cause): mirror WB
// EnvCellRenderManager.cs:405 — restore the pool cursor to the
// high-water mark Prepare's merge phase reached, so any
@ -1010,11 +1018,11 @@ public sealed unsafe class EnvCellRenderer :
_currentCullMode = null;
// WB EnvCellRenderManager.cs:406-409: uniform state setup.
_shader.SetInt("uRenderPass", (int)renderPass);
_shader.SetInt("uFilterByCell", 0);
_shader.SetInt("uLightingMode", 1); // A7 Fix D D-3/D-4: EnvCell bake (wrap points, no sun)
_shader?.SetInt("uRenderPass", (int)renderPass);
_shader?.SetInt("uFilterByCell", 0);
_shader?.SetInt("uLightingMode", 1); // A7 Fix D D-3/D-4: EnvCell bake (wrap points, no sun)
// #176 stripe-hunt isolation (ACDREAM_LIGHT_DEBUG) — throwaway diagnostic.
_shader.SetInt("uLightDebug", AcDream.Core.Rendering.RenderingDiagnostics.LightDebugMode);
_shader?.SetInt("uLightDebug", AcDream.Core.Rendering.RenderingDiagnostics.LightDebugMode);
// Phase U.4 ROOT-CAUSE FIX (cell-shell flicker / "transparent walls when
// moving"): upload uViewProjection HERE rather than inheriting it from
@ -1024,7 +1032,7 @@ public sealed unsafe class EnvCellRenderer :
// gl_Position against this frame's clip planes → pose-dependent clipping,
// worst while moving. Same self-contained-GL-state precedent as the
// 2026-05-28 cull-state cache fix above.
_shader.SetMatrix4("uViewProjection", _lastViewProjection);
_shader?.SetMatrix4("uViewProjection", _lastViewProjection);
List<InstanceData> allInstances = _renderInstances;
List<(ObjectRenderData renderData, ulong gfxObjId, int count, int offset)> drawCalls =
@ -1148,9 +1156,9 @@ public sealed unsafe class EnvCellRenderer :
// WB EnvCellRenderManager.cs:486-510: selection/hover highlights — DROPPED (no editor state).
// WB EnvCellRenderManager.cs:506-509: cleanup.
_shader.SetVec4("uHighlightColor", new System.Numerics.Vector4(0, 0, 0, 0));
_shader.SetInt("uRenderPass", (int)renderPass);
_gl.BindVertexArray(0);
_shader?.SetVec4("uHighlightColor", new System.Numerics.Vector4(0, 0, 0, 0));
_shader?.SetInt("uRenderPass", (int)renderPass);
_gl?.BindVertexArray(0);
_currentVao = 0;
// No cull restore at exit, matching WB's manager pattern: the
@ -1307,12 +1315,12 @@ public sealed unsafe class EnvCellRenderer :
var set = new DynamicBufferSet();
try
{
set.MdiCommandBuffer = TrackedGlResource.CreateBuffer(_gl, "creating EnvCell MDI buffer");
set.ModernInstanceBuffer = TrackedGlResource.CreateBuffer(_gl, "creating EnvCell instance SSBO");
set.ModernBatchBuffer = TrackedGlResource.CreateBuffer(_gl, "creating EnvCell batch SSBO");
set.ClipSlotBuffer = TrackedGlResource.CreateBuffer(_gl, "creating EnvCell clip-slot SSBO");
set.GlobalLightsSsbo = TrackedGlResource.CreateBuffer(_gl, "creating EnvCell global-light SSBO");
set.InstanceLightSetSsbo = TrackedGlResource.CreateBuffer(_gl, "creating EnvCell light-set SSBO");
set.MdiCommandBuffer = TrackedGlResource.CreateBuffer(_gl!, "creating EnvCell MDI buffer");
set.ModernInstanceBuffer = TrackedGlResource.CreateBuffer(_gl!, "creating EnvCell instance SSBO");
set.ModernBatchBuffer = TrackedGlResource.CreateBuffer(_gl!, "creating EnvCell batch SSBO");
set.ClipSlotBuffer = TrackedGlResource.CreateBuffer(_gl!, "creating EnvCell clip-slot SSBO");
set.GlobalLightsSsbo = TrackedGlResource.CreateBuffer(_gl!, "creating EnvCell global-light SSBO");
set.InstanceLightSetSsbo = TrackedGlResource.CreateBuffer(_gl!, "creating EnvCell light-set SSBO");
return set;
}
catch (Exception creationFailure)
@ -1356,7 +1364,7 @@ public sealed unsafe class EnvCellRenderer :
List<Exception>? failures = null;
void Attempt(uint buffer, long bytes, string name)
{
try { TrackedGlResource.DeleteBuffer(_gl, buffer, bytes, $"deleting {name}"); }
try { TrackedGlResource.DeleteBuffer(_gl!, buffer, bytes, $"deleting {name}"); }
catch (Exception ex) { (failures ??= []).Add(ex); }
}
@ -1403,7 +1411,7 @@ public sealed unsafe class EnvCellRenderer :
}
private void RenderModernMDIInternal(
AcDream.App.Rendering.Shader shader,
AcDream.App.Rendering.Shader? shader,
List<(ObjectRenderData renderData, ulong gfxObjId, int count, int offset)> drawCalls,
List<InstanceData> allInstances,
IReadOnlyList<DrawCallRange> drawCallRanges,
@ -1419,12 +1427,22 @@ public sealed unsafe class EnvCellRenderer :
// Without the global VAO nothing can draw, and returning AFTER the pass state
// was established leaked it (same early-out shape as the totalDraws==0 leak —
// see the comment on the state-establish block below).
// Campaign V slice V6j: the RHI arm has no vertex array — the pipeline
// owns one shaped by GpuVertexLayout.WorldMesh — so its readiness test is
// the backend-neutral HasStores the arena publishes (V6i-3).
var globalVao = _meshManager.GlobalBuffer?.VAO ?? 0u;
if (globalVao == 0) return;
if (_gl is not null)
{
if (globalVao == 0) return;
}
else if (_meshManager.GlobalBuffer is not { HasStores: true })
{
return;
}
// WB BaseObjectRenderManager.cs:715-716:
shader.Use();
shader.SetInt("uFilterByCell", 0);
shader?.Use();
shader?.SetInt("uFilterByCell", 0);
// WB BaseObjectRenderManager.cs:718-740: count the pass-filtered batches.
// A normal render has one range. The ordered transparent-shell path has
@ -1460,6 +1478,13 @@ public sealed unsafe class EnvCellRenderer :
// WB BaseObjectRenderManager.cs:743:
if (totalDraws == 0) return;
int uniqueInstanceCount = allInstances.Count;
// Campaign V slice V6j: the encoder arm owns no buffer pool and no
// imperative state bracket. Every per-frame section is a ring slice, so
// there is nothing to activate or grow, and blend plus depth-write are
// baked into the three shell pipelines rather than set here.
if (_gl is not null)
{
ActivateNextDynamicBufferSet();
// Phase U.4 ROOT-CAUSE FIX (cell-shell "transparent walls / only bluish
@ -1517,7 +1542,6 @@ public sealed unsafe class EnvCellRenderer :
_modernBatchCapacity = grownBatchCapacity;
}
int uniqueInstanceCount = allInstances.Count;
if (uniqueInstanceCount > _modernInstanceCapacity)
{
int grownInstanceCapacity = Math.Max(_modernInstanceCapacity * 2, uniqueInstanceCount);
@ -1568,6 +1592,7 @@ public sealed unsafe class EnvCellRenderer :
$"growing EnvCell light-set SSBO to {grownLightSetCapacity} instances");
_instLightSetCapacity = grownLightSetCapacity;
}
}
// WB BaseObjectRenderManager.cs:761-762: grow scratch arrays.
if (_commands.Length < totalDraws)
@ -1659,6 +1684,12 @@ public sealed unsafe class EnvCellRenderer :
}
}
if (_gl is null)
{
SubmitRhi(allInstances, renderPass, totalDraws, uniqueInstanceCount);
return;
}
// WB BaseObjectRenderManager.cs:784-805 upload. Retain capacity and
// update the active prefix so portal frames cannot enqueue an unbounded
// chain of retired driver allocations.
@ -1810,15 +1841,15 @@ public sealed unsafe class EnvCellRenderer :
if (isAdditive)
{
_gl.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.One);
shader.SetInt("uRenderPass", (int)renderPass | 0x100);
shader!.SetInt("uRenderPass", (int)renderPass | 0x100);
}
else
{
_gl.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
shader.SetInt("uRenderPass", (int)renderPass);
shader!.SetInt("uRenderPass", (int)renderPass);
}
shader.SetInt("uDrawIDOffset", drawRange.FirstCommand);
shader!.SetInt("uDrawIDOffset", drawRange.FirstCommand);
_gl.MultiDrawElementsIndirect(
PrimitiveType.Triangles,
DrawElementsType.UnsignedShort,
@ -1834,7 +1865,7 @@ public sealed unsafe class EnvCellRenderer :
_gl.DepthMask(true);
// WB BaseObjectRenderManager.cs:845-847:
shader.SetInt("uDrawIDOffset", 0);
shader!.SetInt("uDrawIDOffset", 0);
_gl.BindBuffer(GLEnum.DrawIndirectBuffer, 0);
}
@ -1974,15 +2005,15 @@ public sealed unsafe class EnvCellRenderer :
switch (mode)
{
case CullMode.None:
_gl.Disable(EnableCap.CullFace);
_gl!.Disable(EnableCap.CullFace);
break;
case CullMode.Clockwise:
_gl.Enable(EnableCap.CullFace);
_gl!.Enable(EnableCap.CullFace);
_gl.CullFace(TriangleFace.Front);
break;
case CullMode.CounterClockwise:
case CullMode.Landblock:
_gl.Enable(EnableCap.CullFace);
_gl!.Enable(EnableCap.CullFace);
_gl.CullFace(TriangleFace.Back);
break;
}
@ -2005,7 +2036,7 @@ public sealed unsafe class EnvCellRenderer :
{
AcDream.App.Rendering.Gpu.Gl.GlGpuDevice device = WorldTextureTable;
device.FlushTextureTable();
_gl.BindBufferBase(
_gl!.BindBufferBase(
GLEnum.ShaderStorageBuffer,
AcDream.App.Rendering.Gpu.GpuBindingModel.StorageTextureTable,
device.TextureTableGlName);
@ -2025,19 +2056,19 @@ public sealed unsafe class EnvCellRenderer :
{
if (_sharedClipRegionSsbo != 0)
{
_gl.BindBufferBase(GLEnum.ShaderStorageBuffer,
_gl!.BindBufferBase(GLEnum.ShaderStorageBuffer,
AcDream.App.Rendering.ClipFrame.MeshClipSsboBinding, _sharedClipRegionSsbo);
return;
}
if (_fallbackClipRegionSsbo == 0)
{
uint fallback = TrackedGlResource.CreateBuffer(_gl, "creating EnvCell fallback clip SSBO");
uint fallback = TrackedGlResource.CreateBuffer(_gl!, "creating EnvCell fallback clip SSBO");
bool allocated = false;
try
{
TrackedGlResource.AllocateBufferStorage(
_gl,
_gl!,
GLEnum.ShaderStorageBuffer,
fallback,
0,
@ -2050,7 +2081,7 @@ public sealed unsafe class EnvCellRenderer :
zero.Clear();
fixed (byte* p = zero)
{
_gl.BufferSubData(
_gl!.BufferSubData(
GLEnum.ShaderStorageBuffer,
0,
(nuint)zero.Length,
@ -2062,14 +2093,14 @@ public sealed unsafe class EnvCellRenderer :
catch
{
TrackedGlResource.DeleteBuffer(
_gl,
_gl!,
fallback,
allocated ? AcDream.App.Rendering.ClipFrame.CellClipStrideBytes : 0,
"rolling back EnvCell fallback clip SSBO");
throw;
}
}
_gl.BindBufferBase(GLEnum.ShaderStorageBuffer,
_gl!.BindBufferBase(GLEnum.ShaderStorageBuffer,
AcDream.App.Rendering.ClipFrame.MeshClipSsboBinding, _fallbackClipRegionSsbo);
}
@ -2123,6 +2154,11 @@ public sealed unsafe class EnvCellRenderer :
{
("prepare-scratch", _prepareScratch.Dispose),
};
// Campaign V slice V6j: the encoder arm owns no GL names — its
// pipelines route their physical free through the device's
// retirement queue — so the ledger below holds only the scratch.
if (_gl is null)
DisposeRhiResources();
for (int frame = 0; frame < _dynamicBufferSetsByFrame.Length; frame++)
{
@ -2226,7 +2262,7 @@ public sealed unsafe class EnvCellRenderer :
return;
RetryableGpuResourceRelease release =
TrackedGlResource.CreateRetryableBufferDeletion(
_gl,
_gl!,
buffer,
capacityBytes,
context);