Repository navigation
Runtime: give a released buffer's arena range back only after the GPU is done with it (D3D-475) - #4607
Runtime: give a released buffer's arena range back only after the GPU is done with it (D3D-475)#4607requiemsnkrp wants to merge 3 commits into
Conversation
… GPU is done with it (D3D-475) A default-heap buffer is a range of the GPU address arena, and ~Resource handed the range back at once. The next buffer placed there is zero-filled through the arena's host mapping, so a read still in flight saw zeros. Repro (d3d12_arena_retire_test, behaviour of origin/main): queue A copies a 1 MiB buffer to a readback buffer behind a GPU Wait for a fence queue C signals after ~300 ms of copies (C's Signal is submitted first, so nothing is held on the CPU); the buffer is released and a 1 MiB buffer made while the gate is closed: it gets the same VA (0x10000) and the read finds 262144 of 262144 words zero. - vulkan_backend.h: DeferredDeletionQueue::Kind::arena_range ((offset << 32) | bytes, the arena is below 4 GiB) handed to an ArenaReleaseFn set by the device; VulkanContext::set_arena_release and reclaim_retired(wait): with resource_mutex_ and tracker_mutex_ held no submission is between resolving and recording its ticket, so after the last recorded submission completed a later release needs no next one; with `wait` it first waits for the device to go idle. - d3d12_vulkan_device.cpp: ~Resource retires the range through defer_destroy (arena_retire, arena.retiring bytes); the deletion queue gives it back (arena_retired). arena_allocate is unchanged when the range fits; out of room while ranges are retiring it reclaims, then reclaims after a device wait, then fails as before. - d3d12_arena_retire_test (new): the gated read keeps its data and the new buffer gets another range (0x110000); after the read and the submission after it, the next buffer gets 0x10000 back; with LEONIDA_ARENA_MB=16, a 12 MiB buffer made and released 4 times with nothing submitted keeps its range, and one made while a released 12 MiB buffer waits for a gated read waits for the GPU (~280 ms) and gets that range with the read intact (before: 1.4 ms, same range, 3145728 words zero). - docs/d3d12_vulkan.md 3.2: arena ranges of released buffers, the full-arena path, what is not covered. RTX 3060 / 617.14, LEONIDA_VK_VALIDATION=1 with sync validation: 0 messages. AI-assisted.
Kam471
left a comment
There was a problem hiding this comment.
Built and ran d3d12_arena_retire_test at 71e83d7e on Windows 11, RTX 5070 Ti: exit 0.
in-flight read: released buffer at VA 0x10000; a buffer of the same size made while the gate was closed got VA 0x110000; the gated read found 0 of 262144 words wrongafter the read completed: the next 1 MiB buffer got VA 0x1000016 MiB arena: a 12 MiB buffer made and released 4 times with nothing submitted: VA 0x10000 each time16 MiB arena: a 12 MiB buffer made while a released one waited for a gated read took 287.9 ms (gate open when it returned), got VA 0x10000 (released: 0x10000); the read found 0 of 3145728 words wrongarena ranges of released buffers came back only after the GPU was done with them
The arena-full line (1 refused) is the test deliberately filling the 16 MiB arena, not a failure.
Merge-order: git merge-tree --write-tree against #4557, #4570 and #4577 is clean (exit 0) for all three, so this one can land in any order with the queue-hold PRs. git diff --check clean. No blocking issue.
slopradish
left a comment
There was a problem hiding this comment.
Verified locally on an RTX 3090: built d3d12_arena_retire_test from PR #4607's head and ran it.
in-flight read: released buffer at VA 0x10000; a buffer of the same size made while the gate was closed got VA 0x110000; the gated read found 0 of 262144 words wrong
after the read completed: the next 1 MiB buffer got VA 0x10000
16 MiB arena: a 12 MiB buffer made while a released one waited for a gated read took 300.4 ms (gate open when it returned) ... the read found 0 of 3145728 words wrong
arena ranges of released buffers came back only after the GPU was done with them
The before-fix behaviour quoted in the PR (262144/262144 words wrong) is exactly what this run now avoids. Holding the arena range in the same deferred-release path as the VkBuffer is the correct fix — splitting lifetime between a host mapping and the GPU object was the root cause. The test uses a real cross-queue GPU gate (submitted first, so nothing is held on the CPU), which makes it a genuine in-flight test rather than a simulated one.
No game data or keys involved.
ligmaSec
left a comment
There was a problem hiding this comment.
I built 71e83d7e with clang-cl 21 and ran d3d12_arena_retire_test under Wine 10.0 on Linux with llvmpipe (Mesa 25.0.7). The unmodified test exits 77 because llvmpipe exposes one Vulkan queue. The lifetime regression can also be exercised on one queue: submit the gate copies and signal their fence on the DIRECT queue, then submit the read on that queue with the same fence wait.
With a local test-only fallback to the DIRECT queue, I got these results with Khronos synchronization validation enabled:
| Scenario | PR implementation | Immediate range release restored in ~Resource |
|---|---|---|
| 1 MiB with free space in the arena | Different VA while the gate was closed, original VA reusable after completion, 0 of 262,144 words wrong | Same VA while the gate was closed, 262,144 of 262,144 words wrong |
| 12 MiB in a 16 MiB arena | Allocation waited 271.8 ms for the gate, reused the VA, 0 of 3,145,728 words wrong | Allocation reused the VA in 1.0 ms with the gate closed, 3,145,728 of 3,145,728 words wrong |
The fixed implementation exited 0, the control exited 1, and the validation layer reported no errors in either run. The four idle allocations also reused the same VA on the fixed implementation.
Would you consider keeping the two-queue case when a COPY queue is available and falling back to the DIRECT queue otherwise? In Rig::init(), create reader first, then try to create gate_queue as COPY. If that fails, set gate_queue = reader and record gate lists as DIRECT in submit_gate(). That would let this test catch the host-write regression in software Vulkan CI.
The postponed CPU Fence::Signal case in the PR description also has an existing fix and a timing test in #4282 (D3D-455).
AI-assisted. Synthetic test data only.
|
Tested 71e83d7 on NVIDIA GeForce RTX 4070, driver 621.01, Windows 11, MSVC 14.44.35207: pass. Built and ran Negative control: the buffer destructor called AI-assisted (OpenBot team). |
|
Verified PR #4607 on Windows 11 (25H2
Code review of AI-assisted (Gemini, Antigravity). |
…vice gives no second queue Review of ItsNotRexed#4607 (@ligmaSec): llvmpipe exposes one Vulkan queue, so creating the COPY gate queue failed and the test exited 77. Rig::init() now creates the DIRECT reader first, then tries a COPY queue for the gate; without one the gate runs on the reader itself, recorded as DIRECT lists in submit_gate(), ahead of the gated read, so the host-write regression is still caught. The test prints which mode it used. RTX 3060 / 617.14, LEONIDA_VK_VALIDATION=1 with sync validation, 0 messages in every run: - two queues: new buffer 0x110000 (released 0x10000), 0 of 262144 words wrong, range back after the read; 16 MiB arena wait 288.6 ms, 0 of 3145728 wrong; exit 0. - one queue (COPY creation skipped by a local, uncommitted override): 0x110000, 0 of 262144 wrong, range back; wait 296.3 ms, 0 of 3145728 wrong; exit 0. With the range released at once in ~Resource (control): 0x10000 under the closed gate, 262144 of 262144 wrong; 1.4 ms, 3145728 of 3145728 wrong; exit 1. AI-assisted.
|
@ligmaSec thanks — done in 10993a6:
AI-assisted (Claude). |
|
Tested |
|
Tested 10993a6 on Windows 11 (MSVC BuildTools 2022, Ninja, RelWithDebInfo, -j 4; no Vulkan validation layer installed), one GPU at a time, device line confirmed: AMD Radeon RDNA2 iGPU (driver 26.10.1) and NVIDIA RTX 5080 (driver 616.92). Ran AI-assisted. |
|
Verified against main 3faac55: merges cleanly, compileall clean, and the full nine-test docs family is green with the new 4.4 text (re-run). The C++ side reads consistently — retiring counter mutex-guarded, the offset/bytes pack valid under the documented arena bound, lock order matching its comment — but the new d3d12_arena_retire GPU test needs a Vulkan device and was not runnable here, and the RTX 3060 numbers are taken on trust; the pending GPU check carries the verdict. Note docs/d3d12_vulkan.md is also edited by long-open #3634 in unrelated regions. Checks pending. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only review). |
|
Tested 10993a6 on NVIDIA GeForce RTX 4080, driver 617.42: d3d12_arena_retire_test exits 0. In the in-flight case a released buffer at VA 0x10000 was not handed out again while the gate was closed (the new buffer got VA 0x110000) and "the gated read found 0 of 262144 words wrong"; after the read completed the next 1 MiB buffer got VA 0x10000. In the 16 MiB arena a 12 MiB buffer made while a released one waited for a gated read got VA 0x10000 and the read found 0 of 3145728 words wrong, and the arena line reports "6 allocations, 1 refused". The test prints "arena ranges of released buffers came back only after the GPU was done with them". I did not run main, so the test failing without the change is not shown here. AI-assisted. |
Three files conflicted, all kept on both sides: - runtime/src/vulkan_backend.h: DeferredDeletionQueue::Kind gets main's framebuffer next to arena_range (both drained); VulkanContext keeps set_arena_release and main's submitted_retire_ticket. - runtime/shims/d3d12_vulkan_device.cpp: ~Resource retires its arena range (arena_retire) and skips main's sparse images; ~Device clears the arena release and main's set_bindless_current. - runtime/CMakeLists.txt: d3d12_arena_retire_test next to main's queue handover modes and d3d12_queue_share_test. AI-assisted.
|
Merged Re-ran on RTX 3060 (617.14), Windows 11, MSVC (VS 18), RelWithDebInfo,
Full ctest ( AI-assisted (Claude). |
What and why
D3D-475 (claim, my finding).
~Resourcehanded a default-heap buffer's address-arena range back at once while itsVkBufferwent throughdefer_destroy, so a buffer created right after got the same range and zero-filled it through the host mapping while queued GPU work still read the old one.Reproduced on
main's behaviour (newd3d12_arena_retire_test): queue C (COPY) runs ~300 ms of 64 MiB host-buffer copies thenSignal(F,1); queue A (DIRECT)Wait(F,1)then copies a 1 MiB default buffer R to readback — a GPU gate (C's Signal is submitted first, so nothing is held on the CPU). With the gate still closed (checked), R is released and a new 1 MiB default buffer is created:Every wrong word is
00000000(the new buffer's zero-fill). Validation was clean there too: sync validation doesn't track host writes.Change
vulkan_backend.h: newDeferredDeletionQueue::Kind::arena_range(offset and size packed in the 64-bit handle; the arena is under 4 GiB); on retirement the device's release function gets the range back (set_arena_release).VulkanContext::reclaim_retired(wait), used only when the arena is full: with the resource and tracker locks held no submission can be half-recorded, so once every recorded submission completed, a range released after the last one needn't wait for the next;waitfirst waits for the device to go idle.d3d12_vulkan_device.cpp:~Resourcecallsarena_retire(throughdefer_destroy, like the buffer's other Vulkan objects); the range comes back inarena_retired.arena_allocateis unchanged when the buffer fits; with no room and ranges still retiring it reclaims, then reclaims after waiting for the device, and only then returnsE_OUTOFMEMORYas before. Callback registered increate_arena, cleared in~Deviceafter the final drain.docs/d3d12_vulkan.md§3.2 (GPU-address-arena item, theE_OUTOFMEMORYline).Tested (NVIDIA GeForce RTX 3060, Windows 11, MSVC 19.51, Vulkan SDK 1.4.363.0,
LEONIDA_VK_VALIDATION=1+ sync validation, 0 errors)LEONIDA_ARENA_MB=16and checks a second live 12 MiB buffer is refused (the small arena is in effect). Create-and-release loops with nothing submitted still reuse the range at once, so that pattern doesn't fill the arena.main(the new test). 68 queue/fence/vk_/d3d12_tests under validation: 0 errors.Open (not in this PR)
CreateHeapranges (and their placed buffers), shader-visible descriptor heaps and shader code still return to the arena when the object is destroyed (D3D12 lets the layer require the app to keep those alive; documented).Fence::Signal, reproduced: a queueSignal(F,1)still gated, the CPU callsF->Signal(2)(postponed), another queueWait(F,2)→ held:after the gate + 3016 ms the fence reads 2 and the batch waiting for 2 is still held(dropped only when the fence is released).Fence::Signal's postponed path appliesvkSignalSemaphorefrom the fence-dispatcher callback but never callsholds().signaled()/pump_ready(). Not fixed here: pumping from that callback would replay held batches on the dispatcher thread, and is unsafe when the callback runs fromcancel()in~Fence. Worth its own item.No game files. AI-assisted (Claude).