feat(render): Campaign V slice V6f-2 - terrain's tiling table becomes a buffer

terrain_modern.frag declared `uniform float uTexTiling[36]` - the per-layer
tiling factors retail passes to TexMerge::CopyAndTile / TexMerge::Merge, one per
terrain atlas layer. Vulkan GLSL has no default uniform block, so a loose array
is unspellable there, and 144 bytes of payload cannot ride the pinned 96-byte
push-constant block. GpuBindingModel reserved UniformTerrainTiling (binding 3)
for exactly this at slice V4d. The array now lives in that block.

The ELEMENT TYPE is deliberately unchanged. std140 pads every array element out
to 16 bytes, so the block is 576 bytes rather than 144, and packing four values
per vec4 would be tighter - but it would also rewrite the accessor and every use
site, and this commit's whole value is that its pixel gate measures the move to
a uniform buffer and nothing else. `uTexTiling[int(layer)]` reads exactly as it
did.

That padding is the hazard the change introduces, so it is pinned twice. The CPU
writer walks TerrainTextureTilingTable.UniformElementStrideBytes and zero-fills
the dead words rather than blitting 36 packed floats, and a new test asserts the
stride is 16, the block is 576, and the two are consistent with LayerCapacity. A
tightly-packed writer would not crash or even look obviously wrong: the shader
would read layer 0's factor for layers 0-3, layer 4's for 4-7, and in a scene
where most layers tile at 1 the error stays invisible until a layer that does
not appears. Nothing else in the suite could see that.

The buffer is allocated once in the constructor, through the same
TrackedGlResource + ResourceCleanupGroup rollback path every other terrain
buffer uses, written on the first bound draw - preserving the upload-once
property the linked program's uniform had for free - and released through the
dispose ledger. It is REBOUND every draw rather than once: GL's uniform-buffer
binding points are global and shared with SceneLighting at 1 and the sky's
params at 4, so a renderer running between two terrain draws can take binding 3
out from under us. Self-contained render state, per the standing rule.

Gates. Release build clean. App tests 4,073 passed / 3 skipped - the baseline
4,072 plus the new layout test. Offline pixel gate against 5e13b45f: 21 differing
pixels of 563,200 compared (fraction 3.73e-05), inside the documented 15-23
pixel band and ~27x under the 0.001 threshold. This gate is a real test of the
layout rather than a formality: terrain blending, road overlays and the water
edge are most of the captured frame, and every one of those samples goes through
terrainTiling(), so a stride mismatch would have shown as a wholesale retexture
rather than as noise. The gate run's client log has zero exceptions and an empty
stderr.

Manifest regenerated in the same commit. terrain_modern's remaining Vulkan error
moved from `'uTexTiling' : undeclared identifier` to the frag's direct
`sampler2DArray(...)` construction, which is the same dialect migration V6e ran
for mesh_modern and which lands next. The pair count is unchanged at 7/9.

No divergence-register row: the tiling values, their source and their use are
unchanged, and no retail-facing behaviour moves.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Erik 2026-07-28 10:11:00 +02:00
parent 5e13b45fae
commit fac0940711
5 changed files with 150 additions and 12 deletions

View file

@ -55,7 +55,14 @@ public sealed class TerrainTextureTilingTableTests
"terrain_modern.frag");
string shader = File.ReadAllText(shaderPath);
Assert.Contains("uniform float uTexTiling[36];", shader);
// Campaign V slice V6f-2 moved the table out of a loose
// `uniform float uTexTiling[36]` and into a std140 block at
// GpuBindingModel.UniformTerrainTiling. The element type and count are
// what every sample site below depends on, so both are still pinned —
// and the binding number is now pinned too, because the shader and
// GpuBindingModel have to agree.
Assert.Contains("binding = 3) uniform TerrainTiling {", shader);
Assert.Contains("float uTexTiling[36];", shader);
Assert.Contains("baseUV * terrainTiling(pOverlay0.z)", shader);
Assert.Contains("baseUV * terrainTiling(pOverlay1.z)", shader);
Assert.Contains("baseUV * terrainTiling(pOverlay2.z)", shader);
@ -63,4 +70,33 @@ public sealed class TerrainTextureTilingTableTests
Assert.Contains("vBaseUV * terrainTiling(vBaseTexIdx)", shader);
Assert.DoesNotContain("const float TILE", shader);
}
/// <summary>
/// Campaign V slice V6f-2: the CPU writer and the std140 block must agree on
/// where each value lands.
///
/// <para>This is the specific hazard the move to a uniform buffer
/// introduced. std140 pads every element of a scalar array to 16 bytes, so a
/// writer that packs 36 floats tightly produces a buffer in which the shader
/// reads layer 0's value for layers 0-3, layer 4's for 4-7, and so on. That
/// renders — it just tiles the wrong textures, in a scene where nine of ten
/// layers use a tiling factor of 1 and the difference is invisible until the
/// tenth appears. Nothing else in the suite would catch it, so the two
/// constants are pinned to the layout rule directly.</para>
/// </summary>
[Fact]
public void UniformBufferMatchesTheStd140LayoutTheShaderDeclares()
{
Assert.Equal(16, TerrainTextureTilingTable.UniformElementStrideBytes);
Assert.Equal(576, TerrainTextureTilingTable.UniformBufferBytes);
Assert.Equal(
TerrainTextureTilingTable.LayerCapacity
* TerrainTextureTilingTable.UniformElementStrideBytes,
TerrainTextureTilingTable.UniformBufferBytes);
// A std140 float array element is padded, never packed. If this ever
// equals sizeof(float) the writer above has been "simplified" into the
// bug this test exists for.
Assert.NotEqual(sizeof(float), TerrainTextureTilingTable.UniformElementStrideBytes);
}
}