acdream/tests/AcDream.App.Tests/Rendering/Gpu/Vk/VulkanMemoryModelTests.cs
Erik fb9c6693dc feat(render): Campaign V slice V6a - Vulkan memory, buffers, rings and the frame timeline
The first of V6's three commits, and the half of the Vulkan backend that has
nothing to do with drawing: where memory comes from, how per-frame data reaches
the GPU, and what makes it safe to reuse either.

Plan sections: 4.2 (bindings layer and the no-VMA decision), 4.3 (memory:
arena, staging ring, per-frame data), 4.8 (sync and the frame).

The allocator is hand-rolled, roughly as 4.2 sizes it. Silk ships no VMA, and a
third-party binding would be a native binary to carry across win-x64, linux-x64
and CI lavapipe for an allocation profile that is genuinely tame: two mesh arena
buffers, one staging ring, a per-flight ring buffer each, a few render targets
and a texture pool. What a custom allocator buys instead is exact accounting -
every byte is attributable to a memory type and a block - which is what
GpuMemoryTracker will want and what VMA would obscure.

Placement, block policy and heap choice are pure types with no Vulkan handle in
sight: VulkanMemoryBlockFreeList is first-fit with coalescing on release,
VulkanMemoryTypePool decides when a request is large enough to warrant a block
of its own, and VulkanMemoryTypeSelection maps each GpuMemoryResidency onto a
preference order of property masks. VulkanDeviceMemoryAllocator turns their
answers into vkAllocateMemory and one persistent vkMapMemory per host-visible
block. That split is deliberate: an allocator's real failure modes are
arithmetic - a mis-coalesced neighbour, an alignment that eats a block's tail, a
double release that quietly corrupts the used-byte count - and arithmetic does
not need a GPU to be wrong. Twenty-two tests cover exactly those.

The HostWritable row of the selection table is the campaign's CPU win stated as
data. It prefers a memory type that is both DEVICE_LOCAL and HOST_VISIBLE -
resizable BAR, present on the RX 9070 XT - so per-frame data is written once,
straight into memory the GPU reads, and falls back to ordinary host-visible
coherent memory when no such type exists. GpuCapabilityRecord's
SupportsPersistentlyMappedRings is the first capability that is true on this
backend and false on GL.

Mapping is per block, never per allocation, because Vulkan permits a memory
object to be mapped once - mapping per buffer would need one VkDeviceMemory per
buffer, which is precisely the allocation-count explosion the design exists to
avoid.

VulkanRingBufferState is markedly simpler than its GL sibling, and the
difference IS the point. GlRingBufferState has to track a dirty watermark and
prove its upload never overlaps an in-flight read, because a ring allocation
there writes into a managed array that is later copied into a GL buffer. Here
the allocation hands back memory the GPU reads directly: there is no upload step
to track. What is left is a cursor.

VulkanUploadQueue accumulates transfers rather than issuing them, for two
reasons that both come from Vulkan rather than from taste: copies must be
recorded into a command buffer, and they must be recorded outside a
dynamic-rendering block. So requests queue and drain at the one moment both hold
- immediately before a pass begins - which is the direct analogue of the GL
backend's flush-before-every-draw discipline at the granularity Vulkan needs.
The drain emits one batched buffer barrier for the whole batch, one of the four
to six 4.8 budgets per frame.

Staging exhaustion falls back to a temporary dedicated buffer retired through
the ledger. Section 4.3 already specifies that for oversized uploads; extending
it to "the ring is full of unretired frames" is the same shape and is a policy
rather than a workaround - the transfer stays correct and ordered, it just costs
one allocation.

VulkanFrameFlightController is the mechanical port 4.8 promised. GL's array of
fences becomes one timeline semaphore whose value is the frame serial, "has this
slot retired?" becomes "is the counter at least serial minus two?", and the
SortedDictionary retirement ledger keeps its keys because those keys were
already frame serials. One subtlety is worth stating: a release is filed against
the frame currently being RECORDED, not the last one completed, because commands
already recorded into the open frame may still read the resource. A test pins
that, since getting it wrong frees memory a pending command buffer reads and the
symptom would appear somewhere else entirely.

Frame acquire ordering is the other subtlety. TryBeginFrame waits on the flight
slot BEFORE acquiring its swapchain image, so the slot's acquire semaphore is
provably idle - signalling a semaphore a pending submit still waits on is the
classic Vulkan deadlock. When the acquire fails the serial is still signalled
through an empty submit, because a serial that never completes makes every later
frame wait forever.

The device is a partial class split along the V6 commit boundary: everything
here is memory and frames, while textures and the descriptor table (V6b) and
pipelines, passes and readback (V6c) throw with the slice named rather than
returning something that fails later and further away. Nothing constructs this
device yet - VulkanBringUpHost still presents its clear colour - so the GL path
executes not one new statement.

VK_EXT_debug_utils naming arrives with the allocator rather than at V6c, because
every resource wants a name from birth and the campaign has already spent days
on defects only visible from outside the API. It stays optional: absent
extension means every call is a no-op and no call site checks.

Gates: Release build clean, App suite 4014 passed / 3 skipped (3981 baseline
plus 33 new). One Issue181WallPressEquilibriumTests failure in the full run is
the known #250 zero-allocation flake and passes on a single run. Offline pixel
gate against the parent is a tripwire here - the backend is dark and no GL code
path changed - and is reported with the slice.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-28 07:36:15 +02:00

248 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using AcDream.App.Rendering.Gpu;
using AcDream.App.Rendering.Gpu.Vk;
using Silk.NET.Vulkan;
namespace AcDream.App.Tests.Rendering.Gpu.Vk;
/// <summary>
/// Campaign V slice V6a — the allocator's arithmetic (plan §4.2/§4.3).
///
/// Every decision the Vulkan allocator makes is reachable from here: where a
/// suballocation lands inside a block, whether releasing one lets the next reuse
/// its bytes, when a request gets a block of its own, and which heap a residency
/// class picks. That is the whole reason the free list and the pool carry no
/// Vulkan handles — an allocator's real failure modes are arithmetic, and
/// arithmetic does not need a GPU to be wrong.
/// </summary>
public sealed class VulkanMemoryModelTests
{
// ── free list ────────────────────────────────────────────────────────────
[Fact]
public void FreeList_AllocatesFromTheFrontAndTracksUse()
{
var block = new VulkanMemoryBlockFreeList(1024);
Assert.True(block.TryAllocate(256, 1, out ulong first));
Assert.True(block.TryAllocate(256, 1, out ulong second));
Assert.Equal(0ul, first);
Assert.Equal(256ul, second);
Assert.Equal(512ul, block.UsedBytes);
Assert.Equal(512ul, block.FreeBytes);
}
[Fact]
public void FreeList_HonoursAlignmentAndChargesThePaddingToTheAllocation()
{
var block = new VulkanMemoryBlockFreeList(1024);
Assert.True(block.TryAllocate(1, 1, out _));
Assert.True(block.TryAllocate(16, 256, out ulong aligned));
Assert.Equal(256ul, aligned);
// 1 byte of payload, then 255 bytes of padding plus 16 of payload.
Assert.Equal(272ul, block.UsedBytes);
}
[Fact]
public void FreeList_ReleasingAnAlignedAllocationReturnsItsPaddingToo()
{
var block = new VulkanMemoryBlockFreeList(1024);
Assert.True(block.TryAllocate(1, 1, out _));
Assert.True(block.TryAllocate(16, 256, out ulong aligned));
block.Free(aligned, 16);
// Only the leading 1-byte allocation is still out.
Assert.Equal(1ul, block.UsedBytes);
Assert.Equal(1023ul, block.FreeBytes);
}
[Fact]
public void FreeList_CoalescesNeighboursSoTheBlockDoesNotFragmentAway()
{
var block = new VulkanMemoryBlockFreeList(1024);
Assert.True(block.TryAllocate(256, 1, out ulong a));
Assert.True(block.TryAllocate(256, 1, out ulong b));
Assert.True(block.TryAllocate(256, 1, out ulong c));
block.Free(a, 256);
block.Free(c, 256);
// Two ranges, not three: releasing c already merged with the block's
// untouched tail.
Assert.Equal(2, block.FreeRangeCount);
block.Free(b, 256);
// Everything is back and it is ONE range, not three: a block that
// coalesced badly would still report the right free byte count while
// being unable to satisfy a large allocation ever again.
Assert.Equal(1, block.FreeRangeCount);
Assert.Equal(1024ul, block.LargestFreeBytes);
Assert.Equal(0ul, block.UsedBytes);
}
[Fact]
public void FreeList_RefusesAnAllocationLargerThanTheLargestHole()
{
var block = new VulkanMemoryBlockFreeList(1024);
Assert.True(block.TryAllocate(600, 1, out _));
Assert.False(block.TryAllocate(600, 1, out _));
Assert.True(block.TryAllocate(400, 1, out _));
}
[Fact]
public void FreeList_DoubleReleaseIsRejectedRatherThanCorruptingTheAccounting()
{
var block = new VulkanMemoryBlockFreeList(1024);
Assert.True(block.TryAllocate(256, 1, out ulong offset));
block.Free(offset, 256);
Assert.Throws<InvalidOperationException>(() => block.Free(offset, 256));
}
// ── pool policy ──────────────────────────────────────────────────────────
[Fact]
public void Pool_AsksForANewBlockBeforeItHasOne()
{
var pool = new VulkanMemoryTypePool(memoryTypeIndex: 0, blockSizeBytes: 4096, dedicatedThresholdBytes: 2048);
Assert.False(pool.TryAllocate(64, 1, out _));
int index = pool.AddBlock(pool.BlockCapacityFor(64), dedicated: false);
Assert.Equal(0, index);
Assert.True(pool.TryAllocate(64, 1, out VulkanMemoryRange range));
Assert.Equal(0, range.BlockIndex);
Assert.False(range.IsDedicated);
}
[Fact]
public void Pool_GivesALargeRequestAWholeBlockOfItsOwn()
{
var pool = new VulkanMemoryTypePool(memoryTypeIndex: 0, blockSizeBytes: 4096, dedicatedThresholdBytes: 2048);
int shared = pool.AddBlock(4096, dedicated: false);
Assert.Equal(0, shared);
// 3000 >= the 2048 threshold, so it must not be placed in the shared
// block even though the shared block would fit it — taking that space
// would strand the remaining kilobyte.
Assert.False(pool.TryAllocate(3000, 1, out _));
Assert.Equal(3000ul, pool.BlockCapacityFor(3000));
int dedicated = pool.AddBlock(3000, dedicated: true);
VulkanMemoryRange range = pool.AllocateWholeBlock(dedicated, 3000);
Assert.True(range.IsDedicated);
Assert.Equal(0ul, range.OffsetBytes);
Assert.Equal(2, pool.LiveBlockCount);
}
[Fact]
public void Pool_FreeingADedicatedRangeRetiresItsBlockButKeepsSharedOnes()
{
var pool = new VulkanMemoryTypePool(memoryTypeIndex: 0, blockSizeBytes: 4096, dedicatedThresholdBytes: 2048);
int shared = pool.AddBlock(4096, dedicated: false);
Assert.True(pool.TryAllocate(64, 1, out VulkanMemoryRange small));
int dedicatedBlock = pool.AddBlock(3000, dedicated: true);
VulkanMemoryRange large = pool.AllocateWholeBlock(dedicatedBlock, 3000);
Assert.True(pool.Free(large)); // dedicated block is now free device memory
Assert.False(pool.Free(small)); // shared block is kept for reuse
Assert.Equal(0, shared);
Assert.Equal(1, pool.LiveBlockCount);
}
[Fact]
public void Pool_ReusesASharedBlockAfterItsAllocationsAreReleased()
{
var pool = new VulkanMemoryTypePool(memoryTypeIndex: 3, blockSizeBytes: 1024, dedicatedThresholdBytes: 4096);
pool.AddBlock(1024, dedicated: false);
Assert.True(pool.TryAllocate(1024, 1, out VulkanMemoryRange whole));
Assert.False(pool.TryAllocate(16, 1, out _));
pool.Free(whole);
Assert.True(pool.TryAllocate(16, 1, out VulkanMemoryRange reused));
Assert.Equal(0, reused.BlockIndex);
Assert.Equal(1, pool.LiveBlockCount);
}
// ── heap selection ───────────────────────────────────────────────────────
private static readonly MemoryPropertyFlags DeviceLocal = MemoryPropertyFlags.DeviceLocalBit;
private static readonly MemoryPropertyFlags HostCoherent =
MemoryPropertyFlags.HostVisibleBit | MemoryPropertyFlags.HostCoherentBit;
private static readonly MemoryPropertyFlags ReBar =
MemoryPropertyFlags.DeviceLocalBit | MemoryPropertyFlags.HostVisibleBit | MemoryPropertyFlags.HostCoherentBit;
private static readonly MemoryPropertyFlags HostCached =
MemoryPropertyFlags.HostVisibleBit | MemoryPropertyFlags.HostCoherentBit | MemoryPropertyFlags.HostCachedBit;
[Fact]
public void Selection_HostWritablePrefersResizableBarOverPlainHostMemory()
{
List<MemoryPropertyFlags> types = [DeviceLocal, HostCoherent, ReBar];
uint? chosen = VulkanMemoryTypeSelection.Choose(types, 0b111, GpuMemoryResidency.HostWritable);
// Index 2 is the device-local AND host-visible type: writing per-frame
// data straight into memory the GPU reads is the campaign's CPU win.
Assert.Equal(2u, chosen);
}
[Fact]
public void Selection_HostWritableFallsBackWhenNoResizableBarTypeExists()
{
List<MemoryPropertyFlags> types = [DeviceLocal, HostCoherent];
Assert.Equal(1u, VulkanMemoryTypeSelection.Choose(types, 0b11, GpuMemoryResidency.HostWritable));
}
[Fact]
public void Selection_RespectsTheResourcesAllowedTypeMask()
{
List<MemoryPropertyFlags> types = [ReBar, HostCoherent];
// Bit 0 is masked out even though it is the preferred type.
Assert.Equal(1u, VulkanMemoryTypeSelection.Choose(types, 0b10, GpuMemoryResidency.HostWritable));
}
[Fact]
public void Selection_DeviceLocalPrefersDeviceLocalButAcceptsAnythingRatherThanFailing()
{
List<MemoryPropertyFlags> deviceLocalPresent = [HostCoherent, DeviceLocal];
Assert.Equal(1u, VulkanMemoryTypeSelection.Choose(deviceLocalPresent, 0b11, GpuMemoryResidency.DeviceLocal));
List<MemoryPropertyFlags> unifiedMemory = [HostCoherent];
Assert.Equal(0u, VulkanMemoryTypeSelection.Choose(unifiedMemory, 0b1, GpuMemoryResidency.DeviceLocal));
}
[Fact]
public void Selection_HostReadablePrefersCachedMemoryForTheReadbackItExistsFor()
{
List<MemoryPropertyFlags> types = [HostCoherent, HostCached];
Assert.Equal(1u, VulkanMemoryTypeSelection.Choose(types, 0b11, GpuMemoryResidency.HostReadable));
}
[Fact]
public void Selection_AnswersNullWhenNothingIsUsableRatherThanGuessing()
{
List<MemoryPropertyFlags> types = [DeviceLocal];
Assert.Null(VulkanMemoryTypeSelection.Choose(types, 0b1, GpuMemoryResidency.HostWritable));
}
[Fact]
public void Selection_NamesWhichResidenciesMustBeMappable()
{
Assert.False(VulkanMemoryTypeSelection.RequiresMapping(GpuMemoryResidency.DeviceLocal));
Assert.True(VulkanMemoryTypeSelection.RequiresMapping(GpuMemoryResidency.HostWritable));
Assert.True(VulkanMemoryTypeSelection.RequiresMapping(GpuMemoryResidency.HostReadable));
}
}