The second of V6's three commits: everything the fragment stage samples. Plan sections 4.3 (textures and mip generation) and 4.4 (descriptors). The descriptor table is the piece that retires GL_ARB_bindless_texture. One update-after-bind, partially-bound, variable-count combined-image-sampler array of 16384; registration appends exactly one vkUpdateDescriptorSets and nothing is written at draw time, so steady state is zero descriptor writes per frame. A slot is a (view, sampler) pair, exactly like a bindless handle, which is why the CPU data model needs no change at all - GpuTextureSlot already carries the index and V2 already moved every batch onto it. Eviction is retirement-gated and the slot is scrubbed on the way out. Returning a slot the moment a texture is deleted would let the LRU alias a live draw onto a new texture, so the release is filed through the ledger; and when it runs the slot is first overwritten with the default 1x1 white. A stale view descriptor sitting in a partially-bound array is legal right up until something reads it, at which point it is a use-after-free with no error attached. Writing the dummy makes that impossible rather than unlikely. The CPU block-compression codec is the slice's other substantial piece, and it exists because Vulkan cannot blit into a compressed image. DAT surfaces arrive as DXT1/3/5 with no mips, so the chain has to be decoded, box filtered and re-encoded here. That is not merely a substitute for the missing blit: the GL path calls glGenerateMipmap on compressed array textures, whose result is explicitly implementation-defined, so this is the first time that part of the pipeline has had a defined answer. Two properties matter more than quality, and both are tested. It is deterministic - integer arithmetic end to end, endpoints from the block's bounding box, nearest-palette selection, no dithering and no iterative fit - because the offline pixel gate compares captures from separate processes and a chain that varied run to run would make every textured surface look like a regression. And it preserves BC1's one-bit cut-out: a block containing any texel below the alpha threshold is encoded in three-colour mode, because retail's foliage and grates ARE that mode and quantising those texels to an opaque colour would fill in every leaf. Plan 4.3's escape hatch stands if quality ever trips a gate: store the affected textures as RGBA8 and blit their mips. Uncompressed images do take the blit chain, added to the upload queue. Each source level moves to TRANSFER_SRC for its blit and back to TRANSFER_DST afterwards; leaving the chain in mixed layouts would be one barrier cheaper and would then force the batch's final shader-read transition to name a different old layout per level, so ending every level the same way is what keeps that transition one barrier per image. The upload queue now records the layout each image is in on ENTRY to a batch rather than always naming UNDEFINED. UNDEFINED lets the driver discard existing contents, which is right for a fresh image and wrong for the incremental array-layer fills that mirror ManagedGLTextureArray - discarding there would erase every layer uploaded earlier. Render targets are single-sampled per the contract and carry SAMPLED usage alongside COLOR_ATTACHMENT, so a paperdoll or appraisal view can be registered into the table and drawn by the retained UI the moment its pass ends. VulkanBackbufferAttachments owns the two attachments the swapchain does not: the multisampled colour scratch that resolves into the swapchain image, and the transient depth/stencil. Both are TRANSIENT_ATTACHMENT because nothing reads either after the frame. Stencil is not optional - issue #117's portal punch needs the aspect, which is why the V5 gate prefers D32_SFLOAT_S8_UINT over a depth-only format. Every format stays UNORM, and that is the V3 audit's finding rather than a default. The plan previously specified an sRGB swapchain "matching the GL FramebufferSrgb contract"; that contract does not exist, the renderer is plain UNORM end to end, and shipping _SRGB would have brightened every frame and passed silently until V7. VulkanPipelineLayouts is extracted from V5's capability probe rather than written beside it, and the probe now calls it. The probe's whole value is proving the layouts the live backend builds can be built on this device; two similar-looking definitions would have quietly ended that the first time one of them changed. Gates: Release build clean, App suite 4037 passed / 3 skipped (4014 at V6a plus 23 new), offline pixel gate PASS against the parent baseline at a differing fraction of 4.26e-05 - 24 pixels of 563,200, one above the campaign's recorded 15-23 same-commit noise band and about 23x under the 0.001 threshold, on a commit that changes no GL code path. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
128 lines
5.5 KiB
C#
128 lines
5.5 KiB
C#
namespace AcDream.App.Rendering.Gpu.Vk;
|
|
|
|
/// <summary>
|
|
/// Campaign V slice V6b, plan §4.3: build a full mip chain on the CPU.
|
|
///
|
|
/// <para>DAT surfaces arrive with level 0 only. Uncompressed formats can have
|
|
/// their chain generated on the GPU with <c>vkCmdBlitImage</c>; block-compressed
|
|
/// ones cannot, because a compressed image is not a legal blit destination. So
|
|
/// BC chains are built here, and the result is uploaded level by level exactly
|
|
/// as level 0 was.</para>
|
|
///
|
|
/// <para>The filter is a plain 2x2 box average, computed in 8-bit space with
|
|
/// round-half-up. It is not a Gaussian or a Kaiser, and the reason is the same
|
|
/// reason the encoder does not fit a principal axis: this has to produce the
|
|
/// same bytes every run, on every machine, so the pixel gate compares renders
|
|
/// rather than the output of a floating-point reduction that reassociated
|
|
/// differently. Integer arithmetic throughout means it does.</para>
|
|
///
|
|
/// <para><b>Alpha is averaged, not premultiplied.</b> Retail's cut-out foliage
|
|
/// uses BC1's one-bit alpha, and the encoder's three-colour mode preserves the
|
|
/// cut at every level; premultiplying would darken the fringe of every leaf as
|
|
/// the chain descended.</para>
|
|
/// </summary>
|
|
internal static class BlockCompressionMipChain
|
|
{
|
|
/// <summary>One level of a generated chain: its extent and its encoded bytes.</summary>
|
|
internal readonly record struct Level(int MipLevel, int Width, int Height, byte[] Data);
|
|
|
|
/// <summary>
|
|
/// Builds levels 1..<paramref name="mipLevelCount"/>-1 from a compressed
|
|
/// level 0. Level 0 itself is not returned — it was uploaded verbatim and
|
|
/// re-encoding it would be a lossy round trip of data that is already right.
|
|
/// </summary>
|
|
internal static IReadOnlyList<Level> BuildCompressed(
|
|
GpuTextureFormat format,
|
|
ReadOnlySpan<byte> level0,
|
|
int width,
|
|
int height,
|
|
int mipLevelCount)
|
|
{
|
|
if (!BlockCompressionCodec.IsBlockCompressed(format))
|
|
throw new ArgumentOutOfRangeException(nameof(format), format, "This chain builder is for BC formats.");
|
|
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(mipLevelCount);
|
|
if (mipLevelCount == 1)
|
|
return [];
|
|
|
|
byte[] rgba = BlockCompressionCodec.DecodeLevel(format, level0, width, height);
|
|
return BuildFromRgba(format, rgba, width, height, mipLevelCount);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Builds levels 1..N-1 from a tightly packed RGBA8 level 0, encoding each
|
|
/// into <paramref name="format"/>. Exposed separately because a caller that
|
|
/// already has uncompressed source (a composited UI surface, a test) should
|
|
/// not pay a decode to get here.
|
|
/// </summary>
|
|
internal static IReadOnlyList<Level> BuildFromRgba(
|
|
GpuTextureFormat format,
|
|
ReadOnlySpan<byte> rgba,
|
|
int width,
|
|
int height,
|
|
int mipLevelCount)
|
|
{
|
|
var levels = new List<Level>(Math.Max(0, mipLevelCount - 1));
|
|
byte[] current = rgba.ToArray();
|
|
int currentWidth = width;
|
|
int currentHeight = height;
|
|
|
|
for (int level = 1; level < mipLevelCount; level++)
|
|
{
|
|
(byte[] next, int nextWidth, int nextHeight) = Downsample(current, currentWidth, currentHeight);
|
|
byte[] encoded = BlockCompressionCodec.IsBlockCompressed(format)
|
|
? BlockCompressionCodec.EncodeLevel(format, next, nextWidth, nextHeight)
|
|
: next;
|
|
levels.Add(new Level(level, nextWidth, nextHeight, encoded));
|
|
current = next;
|
|
currentWidth = nextWidth;
|
|
currentHeight = nextHeight;
|
|
}
|
|
|
|
return levels;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Halves an RGBA8 image with a 2x2 box average. Odd extents clamp the
|
|
/// second sample to the last row/column instead of wrapping or reading out
|
|
/// of bounds — a non-power-of-two DAT surface is unusual but not impossible,
|
|
/// and the alternative is a buffer overrun rather than a slightly soft mip.
|
|
/// </summary>
|
|
internal static (byte[] Rgba, int Width, int Height) Downsample(
|
|
ReadOnlySpan<byte> rgba,
|
|
int width,
|
|
int height)
|
|
{
|
|
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(width);
|
|
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(height);
|
|
int nextWidth = Math.Max(1, width / 2);
|
|
int nextHeight = Math.Max(1, height / 2);
|
|
var output = new byte[nextWidth * nextHeight * 4];
|
|
|
|
for (int y = 0; y < nextHeight; y++)
|
|
{
|
|
int y0 = Math.Min((y * 2) + 0, height - 1);
|
|
int y1 = Math.Min((y * 2) + 1, height - 1);
|
|
for (int x = 0; x < nextWidth; x++)
|
|
{
|
|
int x0 = Math.Min((x * 2) + 0, width - 1);
|
|
int x1 = Math.Min((x * 2) + 1, width - 1);
|
|
|
|
int a = ((y0 * width) + x0) * 4;
|
|
int b = ((y0 * width) + x1) * 4;
|
|
int c = ((y1 * width) + x0) * 4;
|
|
int d = ((y1 * width) + x1) * 4;
|
|
int destination = ((y * nextWidth) + x) * 4;
|
|
|
|
for (int channel = 0; channel < 4; channel++)
|
|
{
|
|
int sum = rgba[a + channel] + rgba[b + channel] + rgba[c + channel] + rgba[d + channel];
|
|
// Round half up in integers so the result never depends on
|
|
// a floating-point rounding mode.
|
|
output[destination + channel] = (byte)((sum + 2) / 4);
|
|
}
|
|
}
|
|
}
|
|
|
|
return (output, nextWidth, nextHeight);
|
|
}
|
|
}
|