feat(render): Campaign V slice V4t-2 — the world texture stack crosses to GpuTextureSlot

The rest of V4t. The composite, particle and shared-atlas texture paths now
hand out the device's GpuTextureSlot instead of a raw 64-bit
ARB_bindless_texture handle, and GroupKey, CachedBatch and ObjectRenderBatch
carry that slot. WbDrawDispatcher, EnvCellRenderer and ParticleRenderer retire
their interim GlBindlessHandleTable instances and share the device's one
table, exactly as V4t-1 did for terrain. Nothing about world submission
changes otherwise: these three renderers are still raw GL, still bind binding
9 themselves, and still draw the same geometry in the same order.

**What produces a slot now.** CompositeTextureArrayCache's GL backend interns
each array's handle when it makes it resident and retires the entry when it
makes it non-resident, so the pair is created and destroyed together and the
cache above it never learns a device exists — the fake backend its tests use
mints a stand-in slot. TextureCache.AcquireParticleTexture does the same for
the one-layer particle arrays it owns, including on its rollback path.
ObjectMeshManager registers each shared atlas's wrap/clamp handles at batch
upload; registration is idempotent by handle, so the many batches sharing an
atlas share its entry.

**Slot release is stricter than what it replaces, not looser.** The interim
tables never released anything — the class comment said so — and they grew
without bound. The device's table has a fixed 16,384-slot capacity, so an
unreleased entry is now a leak with an end. Every producer therefore retires
its entry: the composite backend at MakeNonResident, the particle backend at
MakeNonResident, and ObjectMeshManager when a retiring atlas's PHYSICAL
retirement completes — the point at which its handles are already non-resident
and its texture already deleted. That last one needs the handles snapshotted
at eviction, because ManagedGLTextureArray.Dispose zeroes its own copies as
its first act. Teardown deliberately does not release: the device is being torn
down alongside its callers, so there is nothing left to recycle a slot into,
and deferring work through a possibly-disposed retirement queue would turn a
clean shutdown into a throw.

**The default value became load-bearing, and that is the one real hazard here.**
BindlessTextureLocation could say "not resolved" with handle 0, because no
texture has handle 0. A slot index has no spare value — default(GpuTextureSlot)
is real slot 0 — so a positional record would have turned every
budget-rejected or still-uploading composite into a silent read of whichever
texture registered first. That is the magenta-placeholder failure shape one
layer down. The type is now a struct storing the slot one-based, so default IS
Unresolved, with a test pinning both halves: default is unresolved, and a
location naming slot 0 is resolved and distinguishable from it. Elsewhere the
sentinel is already exact — GpuTextureSlot.Unassigned is 0xFFFFFFFF, which is
common.glsl's ACDREAM_TEXTURE_NONE — so the classify path's "no texture yet"
test and the particle billboard's untextured branch are unchanged in meaning.

**GroupKey ordering is preserved because the key never ordered anything.**
Handle→slot is a bijection (the device interns one slot per resident handle),
so the same (entity, batch) pairs bucket together as before. The key reaches
equality, hashing and the scene-digest fingerprints — never a comparator:
opaque and translucent groups sort by cull mode then camera distance, the
delayed-alpha path by viewer distance then submission ordinal, and group
enumeration follows the persistent dictionary's insertion order, which a
changed hash does not disturb. The digests hash the slot index where they
hashed the handle; both sides of the render-shadow comparison compute them the
same way, so the value changing is invisible to it. Read
CompareOpaqueSubmissionOrder, CompareTransparentSubmissionOrder and
AlphaFingerprintComparer before doubting this — sort-order drift is a
pixel-visible regression class this project has hit, and it is why the check
was made before the retype rather than after.

**One visibility change, forced rather than chosen.** BindlessTextureLocation
was public and now holds an internal contract type, so it is internal;
ObjectRenderBatch.TextureSlot is internal on an otherwise public class for the
same reason. Nothing outside this assembly and its InternalsVisibleTo test
assemblies named either.

**SkyRenderer keeps its interim table**, and the report should say why: the
sky's textures are minted by SkyRenderer itself from TextureCache's raw GL
texture names, which this slice does not retype, so it would be the one
consumer registering handles it produced — a different shape from the world
stack. The offline gate also masks the sky band, so the one automated
instrument here cannot see a sky regression. V4f owns that renderer.

**Gates.** GL offline pixel gate vs cb2a70b8, measured twice: 31 and 22
differing pixels of 563,200 (5.50e-05, 3.91e-05). The first is above the
plan's documented 15-23 px band, so a control was measured rather than
assumed: two same-commit captures at this tree differ by 19 px, and — the
decisive number — a capture at V4t-1 and a capture at this commit differ by
9 px, fewer than the same-commit control. Maximum channel delta is 41-52 in
every pair including the controls, i.e. the differing pixels are drawn from
one flickering population, not from moved geometry. tools/run-repeat-connected-gate.ps1
-Runs 3: 3/3 RENDERED on both the desktop witness and the client capture. One
Vulkan composition-host run with VK_LAYER_KHRONOS_validation proven inserted
by the loader: zero errors, zero warnings, converged ownership ledger. App
tests 4,077 / 3 skips and the complete Release suite 9,140 / 5 — both the
4,075 and 9,138 baselines plus the two tests added here.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Erik 2026-07-28 12:43:50 +02:00
parent b8bcaa3ef2
commit 565c351f93
17 changed files with 463 additions and 281 deletions

View file

@ -46,7 +46,7 @@ public sealed unsafe class ParticleRenderer : IDisposable
public readonly Vector3 AxisX;
public readonly Vector3 AxisY;
public readonly uint ColorArgb;
public readonly ulong TextureHandle;
public readonly AcDream.App.Rendering.Gpu.GpuTextureSlot TextureSlot;
public readonly float DistanceSq;
public ParticleInstance(
@ -54,14 +54,14 @@ public sealed unsafe class ParticleRenderer : IDisposable
Vector3 axisX,
Vector3 axisY,
uint colorArgb,
ulong textureHandle,
AcDream.App.Rendering.Gpu.GpuTextureSlot textureSlot,
float distanceSq)
{
Position = position;
AxisX = axisX;
AxisY = axisY;
ColorArgb = colorArgb;
TextureHandle = textureHandle;
TextureSlot = textureSlot;
DistanceSq = distanceSq;
}
}
@ -124,17 +124,20 @@ public sealed unsafe class ParticleRenderer : IDisposable
private readonly int _meshTextureIndexLoc = -1;
private readonly int _meshTextureLayerLoc = -1;
// Campaign V slice V2c (2026-07-27): GL-only emulation of the eventual
// Vulkan global texture descriptor array (binding=9,
// GpuBindingModel.StorageTextureTable). Owns its own table — see
// GlBindlessHandleTable's doc comment and the campaign doc's §5.2 for why
// particles don't share WbDrawDispatcher's/EnvCellRenderer's/
// TerrainModernRenderer's tables. There is no automated pixel-gate
// coverage for particles (the offline gate's fixed outdoor view has none
// in frame), so this indirection is kept strictly mechanical.
private readonly GlBindlessHandleTable _textureTable = new();
private uint _textureTableSsbo;
private int _textureTableSsboCapacityBytes;
// Campaign V slice V4t (2026-07-28): the interim per-renderer
// GlBindlessHandleTable is retired. Both particle texture sources now hand
// out the device's own GpuTextureSlot — TextureCache.AcquireParticleTexture
// for billboards, ObjectRenderBatch.TextureSlot for mesh particles — so all
// that is left here is flushing and binding that one table before each
// raw-GL draw. There is still no automated pixel-gate coverage for
// particles (the offline gate's fixed outdoor view has none in frame), so
// this change is kept strictly mechanical, exactly as V2c's was.
private AcDream.App.Rendering.Gpu.Gl.GlGpuDevice WorldTextureTable =>
(_meshAdapter
?? throw new InvalidOperationException(
"ParticleRenderer was constructed without a mesh adapter: its texture " +
"slots come from that adapter's GL device table (Campaign V slice V4t)."))
.WorldTextureTable;
private uint _quadVao;
private readonly uint _quadVbo;
@ -275,21 +278,6 @@ public sealed unsafe class ParticleRenderer : IDisposable
_meshTextureLayerLoc = _gl.GetUniformLocation(_meshShader.Program, "uParamA");
}
// Campaign V slice V2c: binding=9 texture-table SSBO (GL-only
// emulation of the eventual Vulkan descriptor array).
_textureTableSsbo = TrackedGlResource.CreateBuffer(
_gl,
"creating particle texture-table SSBO");
RetryableGpuResourceRelease textureTableRelease =
TrackedGlResource.CreateRetryableBufferDeletion(
_gl,
_textureTableSsbo,
() => _textureTableSsboCapacityBytes,
"rolling back particle texture-table SSBO");
constructionResources.Add(
"particle texture-table SSBO",
textureTableRelease.Run);
float[] quadVerts =
{
-0.5f, -0.5f, 0f, 0f,
@ -568,7 +556,7 @@ public sealed unsafe class ParticleRenderer : IDisposable
_gl.ProgramUniform1(
_meshShader.Program,
_meshTextureIndexLoc,
_textureTable.GetOrAdd(batch.BindlessTextureHandle));
batch.TextureSlot.Index);
// Slice V6e: uParamA is a float, so the layer is widened here rather
// than in the shader's float(uTextureLayer). Layers are small
// integers; the sampled value is bit-identical.
@ -763,7 +751,7 @@ public sealed unsafe class ParticleRenderer : IDisposable
_gl.ProgramUniform1(
_meshShader.Program,
_meshTextureIndexLoc,
_textureTable.GetOrAdd(batch.BindlessTextureHandle));
batch.TextureSlot.Index);
// Slice V6e: uParamA is a float, so the layer is widened here rather
// than in the shader's float(uTextureLayer). Layers are small
// integers; the sampled value is bit-identical.
@ -962,7 +950,7 @@ public sealed unsafe class ParticleRenderer : IDisposable
axisX,
axisY,
p.ColorArgb,
gfxInfo.TextureHandle,
gfxInfo.TextureSlot,
distSq)));
_submissionScratch.Add(new ParticleSubmission(
ParticleSubmissionKind.Billboard,
@ -1005,7 +993,7 @@ public sealed unsafe class ParticleRenderer : IDisposable
for (int batchIndex = 0; batchIndex < renderData.Batches.Count; batchIndex++)
{
ObjectRenderBatch batch = renderData.Batches[batchIndex];
if (batch.IndexCount <= 0 || batch.BindlessTextureHandle == 0)
if (batch.IndexCount <= 0 || !batch.TextureSlot.IsAssigned)
continue;
int drawIndex = _meshDrawListScratch.Count;
@ -1084,9 +1072,12 @@ public sealed unsafe class ParticleRenderer : IDisposable
/// </summary>
private const uint NoTextureSlot = 0xFFFFFFFFu;
// Campaign V slice V2c: instance method (not static) because it converts
// the particle's raw bindless handle to a _textureTable slot.
private void WriteBillboardGpuInstance(
// Campaign V slice V4t: static again — the particle already carries the
// device's table slot, so there is no per-renderer interning left to do.
// GpuTextureSlot.Unassigned and NoTextureSlot are the same 0xFFFFFFFF by
// construction (the contract's sentinel IS ACDREAM_TEXTURE_NONE), so the
// untextured branch collapses into the assignment rather than disappearing.
private static void WriteBillboardGpuInstance(
ref BillboardGpuInstance destination,
ParticleInstance particle)
{
@ -1100,9 +1091,9 @@ public sealed unsafe class ParticleRenderer : IDisposable
((particle.ColorArgb >> 8) & 0xFF) / 255f,
(particle.ColorArgb & 0xFF) / 255f,
((particle.ColorArgb >> 24) & 0xFF) / 255f),
TextureIndex = particle.TextureHandle == 0UL
? NoTextureSlot
: _textureTable.GetOrAdd(particle.TextureHandle),
TextureIndex = particle.TextureSlot.IsAssigned
? particle.TextureSlot.Index
: NoTextureSlot,
};
}
@ -1282,45 +1273,22 @@ public sealed unsafe class ParticleRenderer : IDisposable
}
/// <summary>
/// Campaign V slice V2c: uploads <see cref="_textureTable"/>'s handles to
/// <see cref="_textureTableSsbo"/> when a new one was registered since the
/// last flush (by <see cref="WriteBillboardGpuInstance"/> or either mesh
/// draw site's <c>_textureTable.GetOrAdd</c>), then (re)binds it at
/// Campaign V slice V4t: drains the device texture table's dirty runs and
/// (re)binds it at
/// <see cref="AcDream.App.Rendering.Gpu.GpuBindingModel.StorageTextureTable"/>.
/// Called immediately before every draw call rather than once per pipeline
/// switch: a run of consecutive mesh-particle sub-batches can register a
/// new handle partway through, and the table must be current for each one.
/// Still called immediately before every draw call rather than once per
/// pipeline switch, for the reason V2c gave: a run of consecutive
/// mesh-particle sub-batches can pull in a texture whose slot was registered
/// this frame, and the table must be current for each one.
/// </summary>
private void FlushAndBindTextureTable()
{
if (_textureTable.Dirty)
{
ReadOnlySpan<ulong> handles = _textureTable.Handles;
int byteCount = handles.Length * sizeof(ulong);
fixed (ulong* p = handles)
{
_gl.BindBuffer(BufferTargetARB.ShaderStorageBuffer, _textureTableSsbo);
if (_textureTableSsboCapacityBytes < byteCount)
{
int grown = DynamicBufferCapacity.Grow(_textureTableSsboCapacityBytes, byteCount);
TrackedGlResource.AllocateBufferStorage(
_gl,
GLEnum.ShaderStorageBuffer,
_textureTableSsbo,
_textureTableSsboCapacityBytes,
grown,
GLEnum.DynamicDraw,
"growing particle texture-table SSBO");
_textureTableSsboCapacityBytes = grown;
}
_gl.BufferSubData(BufferTargetARB.ShaderStorageBuffer, 0, (nuint)byteCount, p);
}
_textureTable.MarkFlushed();
}
AcDream.App.Rendering.Gpu.Gl.GlGpuDevice device = WorldTextureTable;
device.FlushTextureTable();
_gl.BindBufferBase(
BufferTargetARB.ShaderStorageBuffer,
AcDream.App.Rendering.Gpu.GpuBindingModel.StorageTextureTable,
_textureTableSsbo);
device.TextureTableGlName);
}
private void ApplyMeshCullMode(CullMode mode)
@ -1431,7 +1399,7 @@ public sealed unsafe class ParticleRenderer : IDisposable
? ParticleGfxInfo.Default
: info with
{
TextureHandle = _textures.AcquireParticleTexture(
TextureSlot = _textures.AcquireParticleTexture(
emitter.Handle,
info.SurfaceId),
};
@ -1456,7 +1424,9 @@ public sealed unsafe class ParticleRenderer : IDisposable
}
return AuthoredParticleGfxInfo(
gfx,
texture: 0,
// Shape only: the caller re-resolves the slot per emitter, so
// this record is cached with no texture rather than slot 0.
texture: AcDream.App.Rendering.Gpu.GpuTextureSlot.Unassigned,
additive,
hasMaterial: surfaceId != 0,
surfaceId: surfaceId);
@ -1469,7 +1439,7 @@ public sealed unsafe class ParticleRenderer : IDisposable
private ParticleGfxInfo AuthoredParticleGfxInfo(
GfxObj gfx,
ulong texture,
AcDream.App.Rendering.Gpu.GpuTextureSlot texture,
bool additive,
bool hasMaterial,
uint surfaceId)
@ -1663,13 +1633,6 @@ public sealed unsafe class ParticleRenderer : IDisposable
6L * sizeof(uint),
"quad-ebo",
"deleting particle quad EBO");
AddTrackedBufferRelease(
releases,
_textureTableSsbo,
_textureTableSsboCapacityBytes,
"texture-table",
"deleting particle texture-table SSBO");
for (int frame = 0; frame < _dynamicBufferSetsByFrame.Length; frame++)
{
List<DynamicBufferSet> frameSets = _dynamicBufferSetsByFrame[frame];
@ -1766,8 +1729,6 @@ public sealed unsafe class ParticleRenderer : IDisposable
_meshInstanceVbo = 0;
_instanceVboCapacityBytes = 0;
_meshInstanceVboCapacityBytes = 0;
_textureTableSsbo = 0;
_textureTableSsboCapacityBytes = 0;
_particleGfxInfoByEmitter.Clear();
_particleGfxInfoByGfxObj.Clear();
_geometryKindByGfxObj.Clear();
@ -1776,7 +1737,7 @@ public sealed unsafe class ParticleRenderer : IDisposable
}
private readonly record struct ParticleGfxInfo(
ulong TextureHandle,
AcDream.App.Rendering.Gpu.GpuTextureSlot TextureSlot,
Vector2 Size,
Vector3 AxisX,
Vector3 AxisY,
@ -1788,7 +1749,7 @@ public sealed unsafe class ParticleRenderer : IDisposable
{
public static ParticleGfxInfo Default { get; } =
Billboard(
0ul,
AcDream.App.Rendering.Gpu.GpuTextureSlot.Unassigned,
Vector2.One,
Vector3.Zero,
additive: false,
@ -1796,14 +1757,14 @@ public sealed unsafe class ParticleRenderer : IDisposable
surfaceId: 0);
public static ParticleGfxInfo Billboard(
ulong textureHandle,
AcDream.App.Rendering.Gpu.GpuTextureSlot textureSlot,
Vector2 size,
Vector3 centerOffset,
bool additive,
bool hasMaterial,
uint surfaceId) =>
new(
textureHandle,
textureSlot,
size,
Vector3.UnitX,
Vector3.UnitY,