feat(render): Campaign V slice V6i-3 commit 1 — the mesh pipeline's upload bodies cross the seam

V6i-2 cut IMeshPipelineDevice at the measured surface and proved the mesh
pipeline could be CONSTRUCTED without naming a backend. It said plainly what it
did not claim: "the mesh pipeline does not RUN on Vulkan. Its upload bodies are
still raw GL — GlobalMeshBuffer, the VAO/IBO construction, the layer transfers."
This moves them, and gives the interface its second implementation.

GlobalMeshBuffer takes GL?. The two backing stores were already IGpuBuffer
(V4b); what still needed a context was the vertex array and the attribute
pointers, which have no RHI verb because Vulkan bakes vertex input into the
pipeline. So a backend with none builds the stores and nothing else, publishes
0 for VAO/VBO/IBO, and publishes VertexStore/IndexStore — the same buffers,
named the way a pass encoder binds them. HasStores is the backend-neutral form
of the VAO != 0 readiness test the raw-GL draw paths make. Two bodies fork on
the context and nothing else does: InitBuffers skips the vertex array, and
CommitMigration skips the rebind — on the encoder arm the field swap IS the
atomic publication, because the next pass reads whatever the field then holds.
The store deletion likewise splits: GL keeps its immediate DeleteRetired,
because the arena's own flight gate has already proven no submitted frame can
reference the store, while the other arm has no second deferral to skip and
Dispose is its retirement-queued release.

ObjectMeshManager's RequireGl narrowed to the LEGACY per-mesh upload. Its three
call sites were one modern-path constructor argument and two bodies whose every
GL statement sits inside `if (!_useModernRendering)`. The constructor now hands
the arena the nullable context; the two bodies resolve one lazily inside the
legacy branch. That branch is unreachable in every shipping configuration —
missing bindless or draw-parameters throws at startup under the N.5 ship
amendment — so the accessor survives as the guard on dead code rather than as a
blocker, and it is deleted with that code.

VulkanMeshPipelineDevice is the second implementation, and it is four
properties and two no-ops. Two things about it are worth stating rather than
leaving to be inferred. HasBindless and HasOpenGL43 answer TRUE: their names are
GL-shaped because the seam was cut from a GL device, but what they gate is the
MODERN path — one shared arena, table texture indexing, multi-draw indirect —
which Vulkan supplies unconditionally and the capability gate rejects a device
for lacking, so answering false would disable the only path that exists.
HasPendingWork answers false because the GL device's queue exists to defer work
onto the thread holding the context, and Vulkan resource work is recorded into
the frame's command buffer or routed through the retirement queue.

WbMeshAdapter selects between them once, in the one place the mesh pipeline
still names a backend. The GL arm is unchanged, including the queue-drain
guarantee its construction rollback asserts.

So composition builds the mesh pipeline on BOTH arms, and NullWbMeshAdapter is
deleted — it existed for exactly the gap this closes, and the landblock spawn
ledger now registers against the real adapter. Streaming's publication into GPU
state stops being a no-op there: the Vulkan run below builds real render data,
including the [up-null] zero-vertex caching path.

Gates. Release build green. App tests 4,112 passed / 3 skipped, against a 4,109
baseline plus the three added here. Strict GL offline pixel gate against
579e0b7f: 4.44e-05 (25 differing pixels of 563,200), inside the documented 9-31
px control band and 22x under the 0.001 threshold. One offline Vulkan run with
VK_LAYER_KHRONOS_validation proven inserted by the loader (VK_LOADER_DEBUG=layer
reports `Insert instance layer "VK_LAYER_KHRONOS_validation"`): zero validation
errors, zero warnings, a captured frame, and no [shutdown] diagnostic on either
stream.

What this does NOT claim: nothing draws the world on Vulkan yet. The three
world renderers' submission arms, the two pass executors, and the pass-structure
merge are the next commit's.

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 14:31:20 +02:00
parent 579e0b7f60
commit fe8abacfc6
10 changed files with 410 additions and 186 deletions

View file

@ -86,7 +86,15 @@ internal enum GlobalMeshCapacityResult
/// VAO has no RHI equivalent (Vulkan bakes vertex input into the pipeline) and
/// <c>WbDrawDispatcher</c>, <c>EnvCellRenderer</c> and <c>ParticleRenderer</c>
/// still bind <see cref="VAO"/>/<see cref="VBO"/>/<see cref="IBO"/> directly
/// until slice V4c moves them onto the pass encoder.
/// on the GL arm.
///
/// <para>Campaign V slice V6i-3 made the GL context optional. A backend that has
/// none builds no vertex array and publishes no raw names — <see cref="VAO"/>,
/// <see cref="VBO"/> and <see cref="IBO"/> are 0 there — and its consumers bind
/// <see cref="VertexStore"/> and <see cref="IndexStore"/> through the pass
/// encoder instead, which is the same 32-byte position/normal/texcoord layout
/// expressed as pipeline vertex input. Everything above the handle — the
/// allocator, the migration, the ledger — is one body on both arms.</para>
/// </summary>
public sealed class GlobalMeshBuffer : IDisposable
{
@ -106,9 +114,9 @@ public sealed class GlobalMeshBuffer : IDisposable
(int)(MaximumIndexBufferBytes / sizeof(ushort));
// Retained only for the vertex array object and its attribute layout, which
// the RHI has no verb for. Slice V4c retires this field with the raw-GL
// dispatcher.
private readonly GL _gl;
// the RHI has no verb for, and null on a backend with no such object. It is
// retired with the raw-GL dispatcher.
private readonly GL? _gl;
private readonly IGpuDevice _device;
private readonly GpuRetirementLedger _retirementLedger;
private readonly GpuRetiredRangeAllocator _vertices;
@ -162,15 +170,33 @@ public sealed class GlobalMeshBuffer : IDisposable
public uint VAO { get; private set; }
/// <summary>
/// The vertex store's raw GL name. Transitional: the modern draw paths still
/// bind the arena themselves until Campaign V slice V4c hands them the pass
/// encoder, so the arena keeps publishing the backend name of the buffer it
/// now owns as an <see cref="IGpuBuffer"/>.
/// The vertex store's raw GL name, or 0 on a backend with no GL context.
/// Transitional: the GL draw paths still bind the arena themselves, so the
/// arena keeps publishing the backend name of the buffer it now owns as an
/// <see cref="IGpuBuffer"/>.
/// </summary>
public uint VBO => _vertexBuffer is null ? 0u : RequireGlBuffer(_vertexBuffer).GlName;
public uint VBO =>
_gl is null || _vertexBuffer is null ? 0u : RequireGlBuffer(_vertexBuffer).GlName;
/// <summary>The index store's raw GL name. See <see cref="VBO"/>.</summary>
public uint IBO => _indexBuffer is null ? 0u : RequireGlBuffer(_indexBuffer).GlName;
public uint IBO =>
_gl is null || _indexBuffer is null ? 0u : RequireGlBuffer(_indexBuffer).GlName;
/// <summary>
/// The vertex store as the contract's own handle. This is what a pass
/// encoder binds, and it is live on both arms — <see cref="VAO"/> is the
/// GL-only expression of the same thing.
/// </summary>
internal IGpuBuffer? VertexStore => _vertexBuffer;
/// <summary>The index store as the contract's own handle. See <see cref="VertexStore"/>.</summary>
internal IGpuBuffer? IndexStore => _indexBuffer;
/// <summary>
/// True once both backing stores exist. The backend-neutral form of the
/// <c>VAO != 0</c> readiness test the raw-GL draw paths make.
/// </summary>
internal bool HasStores => _vertexBuffer is not null && _indexBuffer is not null;
internal long UploadCount { get; private set; }
internal long UploadedBytes { get; private set; }
internal long CapacityBytes =>
@ -242,9 +268,9 @@ public sealed class GlobalMeshBuffer : IDisposable
newBuffers);
}
internal GlobalMeshBuffer(GL gl, IGpuDevice device, IGpuResourceRetirementQueue retirement)
internal GlobalMeshBuffer(GL? gl, IGpuDevice device, IGpuResourceRetirementQueue retirement)
{
_gl = gl ?? throw new ArgumentNullException(nameof(gl));
_gl = gl;
_device = device ?? throw new ArgumentNullException(nameof(device));
ArgumentNullException.ThrowIfNull(retirement);
_retirementLedger = new GpuRetirementLedger(retirement);
@ -282,23 +308,34 @@ public sealed class GlobalMeshBuffer : IDisposable
try
{
_gl.GenVertexArrays(1, out vao);
if (vao == 0)
throw new InvalidOperationException("OpenGL did not create the global mesh-buffer objects.");
// The vertex array is the one object here with no RHI equivalent —
// Vulkan bakes vertex input into the pipeline — so a backend with no
// GL context builds the two stores and nothing else.
if (_gl is { } gl)
{
gl.GenVertexArrays(1, out vao);
if (vao == 0)
throw new InvalidOperationException("OpenGL did not create the global mesh-buffer objects.");
}
vbo = _device.CreateBuffer(DescribeStore(BufferKind.Vertices, vertexBytes, _storeGeneration));
ibo = _device.CreateBuffer(DescribeStore(BufferKind.Indices, indexBytes, _storeGeneration));
_gl.BindVertexArray(vao);
_gl.BindBuffer(GLEnum.ArrayBuffer, RequireGlBuffer(vbo).GlName);
ConfigureVertexAttributes();
if (_gl is { } glBind)
{
glBind.BindVertexArray(vao);
glBind.BindBuffer(GLEnum.ArrayBuffer, RequireGlBuffer(vbo).GlName);
ConfigureVertexAttributes(glBind);
_gl.BindBuffer(GLEnum.ElementArrayBuffer, RequireGlBuffer(ibo).GlName);
GLHelpers.ThrowOnResourceError(
_gl,
$"creating global mesh buffers ({vertexBytes} vertex bytes, {indexBytes} index bytes)");
glBind.BindBuffer(GLEnum.ElementArrayBuffer, RequireGlBuffer(ibo).GlName);
GLHelpers.ThrowOnResourceError(
glBind,
$"creating global mesh buffers ({vertexBytes} vertex bytes, {indexBytes} index bytes)");
GlobalMeshVaoAccounting.TrackAllocation();
vaoTracked = true;
}
GlobalMeshVaoAccounting.TrackAllocation();
vaoTracked = true;
GpuMemoryTracker.TrackResourceAllocation(GpuResourceType.Buffer);
GpuMemoryTracker.TrackAllocation(vertexBytes, GpuResourceType.Buffer);
vertexTracked = true;
@ -318,9 +355,13 @@ public sealed class GlobalMeshBuffer : IDisposable
// cast failure that masks the original construction exception.
if (ibo is GlGpuBuffer stagedIndexStore)
stagedIndexStore.DeleteRetired("rolling back the global index arena buffer");
else
ibo?.Dispose();
if (vbo is GlGpuBuffer stagedVertexStore)
stagedVertexStore.DeleteRetired("rolling back the global vertex arena buffer");
if (vao != 0) _gl.DeleteVertexArray(vao);
else
vbo?.Dispose();
if (vao != 0) _gl!.DeleteVertexArray(vao);
if (indexTracked)
{
GpuMemoryTracker.TrackDeallocation(indexBytes, GpuResourceType.Buffer);
@ -337,19 +378,19 @@ public sealed class GlobalMeshBuffer : IDisposable
}
finally
{
_gl.BindVertexArray(0);
_gl?.BindVertexArray(0);
}
}
private unsafe void ConfigureVertexAttributes()
private static unsafe void ConfigureVertexAttributes(GL gl)
{
int stride = VertexPositionNormalTexture.Size;
_gl.EnableVertexAttribArray(0);
_gl.VertexAttribPointer(0, 3, GLEnum.Float, false, (uint)stride, (void*)0);
_gl.EnableVertexAttribArray(1);
_gl.VertexAttribPointer(1, 3, GLEnum.Float, false, (uint)stride, (void*)(3 * sizeof(float)));
_gl.EnableVertexAttribArray(2);
_gl.VertexAttribPointer(2, 2, GLEnum.Float, false, (uint)stride, (void*)(6 * sizeof(float)));
gl.EnableVertexAttribArray(0);
gl.VertexAttribPointer(0, 3, GLEnum.Float, false, (uint)stride, (void*)0);
gl.EnableVertexAttribArray(1);
gl.VertexAttribPointer(1, 3, GLEnum.Float, false, (uint)stride, (void*)(3 * sizeof(float)));
gl.EnableVertexAttribArray(2);
gl.VertexAttribPointer(2, 2, GLEnum.Float, false, (uint)stride, (void*)(6 * sizeof(float)));
}
internal GlobalMeshAllocation UploadMesh(
@ -778,38 +819,46 @@ public sealed class GlobalMeshBuffer : IDisposable
private void CommitMigration(BufferMigration migration)
{
try
// The atomic publication step is a VAO rebind on GL and nothing at all
// on a backend whose vertex source is a per-draw encoder bind: the field
// swap below IS the publication there, and the next pass reads the new
// store. The rollback arm exists for the same reason it did — a failed
// rebind must leave the vertex array pointing at the live store.
if (_gl is { } gl)
{
_gl.BindVertexArray(VAO);
if (migration.Kind == BufferKind.Vertices)
try
{
_gl.BindBuffer(GLEnum.ArrayBuffer, RequireGlBuffer(migration.NewBuffer).GlName);
ConfigureVertexAttributes();
gl.BindVertexArray(VAO);
if (migration.Kind == BufferKind.Vertices)
{
gl.BindBuffer(GLEnum.ArrayBuffer, RequireGlBuffer(migration.NewBuffer).GlName);
ConfigureVertexAttributes(gl);
}
else
{
gl.BindBuffer(GLEnum.ElementArrayBuffer, RequireGlBuffer(migration.NewBuffer).GlName);
}
GLHelpers.ThrowOnResourceError(gl, $"publishing staged {migration.Kind} arena buffer");
}
else
catch
{
_gl.BindBuffer(GLEnum.ElementArrayBuffer, RequireGlBuffer(migration.NewBuffer).GlName);
gl.BindVertexArray(VAO);
if (migration.Kind == BufferKind.Vertices)
{
gl.BindBuffer(GLEnum.ArrayBuffer, RequireGlBuffer(migration.OldBuffer).GlName);
ConfigureVertexAttributes(gl);
}
else
{
gl.BindBuffer(GLEnum.ElementArrayBuffer, RequireGlBuffer(migration.OldBuffer).GlName);
}
gl.BindVertexArray(0);
throw;
}
GLHelpers.ThrowOnResourceError(_gl, $"publishing staged {migration.Kind} arena buffer");
}
catch
{
_gl.BindVertexArray(VAO);
if (migration.Kind == BufferKind.Vertices)
finally
{
_gl.BindBuffer(GLEnum.ArrayBuffer, RequireGlBuffer(migration.OldBuffer).GlName);
ConfigureVertexAttributes();
gl.BindVertexArray(0);
}
else
{
_gl.BindBuffer(GLEnum.ElementArrayBuffer, RequireGlBuffer(migration.OldBuffer).GlName);
}
_gl.BindVertexArray(0);
throw;
}
finally
{
_gl.BindVertexArray(0);
}
if (migration.Kind == BufferKind.Vertices)
@ -872,10 +921,24 @@ public sealed class GlobalMeshBuffer : IDisposable
{
ArgumentNullException.ThrowIfNull(buffer);
ArgumentOutOfRangeException.ThrowIfNegative(capacityBytes);
GL gl = _gl;
GL? gl = _gl;
return new RetryableGpuResourceRelease(
() => GLHelpers.ThrowOnResourceError(gl, $"{context} (precondition)"),
() => RequireGlBuffer(buffer).DeleteRetired(context),
() =>
{
if (gl is not null)
GLHelpers.ThrowOnResourceError(gl, $"{context} (precondition)");
},
// On GL the delete runs here rather than through IGpuBuffer.Dispose
// because the arena's own flight gate has already proven no submitted
// frame can reference the store. A backend with no GL context has no
// second deferral to skip: Dispose IS its retirement-queued release.
() =>
{
if (gl is not null)
RequireGlBuffer(buffer).DeleteRetired(context);
else
buffer.Dispose();
},
() =>
{
if (capacityBytes != 0)
@ -957,11 +1020,11 @@ public sealed class GlobalMeshBuffer : IDisposable
releases.Add(("staged-migration-buffer", release.Run));
}
if (VAO != 0)
if (VAO != 0 && _gl is { } vaoGl)
{
RetryableGpuResourceRelease release =
TrackedGlResource.CreateRetryableVertexArrayDeletion(
_gl,
vaoGl,
VAO,
$"deleting global mesh vertex array {VAO}",
GlobalMeshVaoAccounting.TrackDeallocation);