Skip to content

Runtime: give a released buffer's arena range back only after the GPU is done with it (D3D-475) - #4607

Open
requiemsnkrp wants to merge 2 commits into
ItsNotRexed:mainfrom
requiemsnkrp:d3d-475-arena-retire
Open

requiemsnkrp wants to merge 2 commits into
ItsNotRexed:mainfrom
requiemsnkrp:d3d-475-arena-retire

Conversation

@requiemsnkrp

Copy link
Copy Markdown

What and why

D3D-475 (claim, my finding). ~Resource handed a default-heap buffer's address-arena range back at once while its VkBuffer went through defer_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 (new d3d12_arena_retire_test): queue C (COPY) runs ~300 ms of 64 MiB host-buffer copies then Signal(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:

in-flight read: released buffer at VA 0x10000; a buffer of the same size made while the gate was closed got VA 0x10000; the gated read found 262144 of 262144 words wrong
16 MiB arena: a 12 MiB buffer made while a released one waited for a gated read took 1.4 ms (gate still closed when it returned), got VA 0x10000 (released: 0x10000); the read found 3145728 of 3145728 words wrong

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: new DeferredDeletionQueue::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; wait first waits for the device to go idle.
  • d3d12_vulkan_device.cpp: ~Resource calls arena_retire (through defer_destroy, like the buffer's other Vulkan objects); the range comes back in arena_retired. arena_allocate is unchanged when the buffer fits; with no room and ranges still retiring it reclaims, then reclaims after waiting for the device, and only then returns E_OUTOFMEMORY as before. Callback registered in create_arena, cleared in ~Device after the final drain.
  • Docs: docs/d3d12_vulkan.md §3.2 (GPU-address-arena item, the E_OUTOFMEMORY line).

Tested (NVIDIA GeForce RTX 3060, Windows 11, MSVC 19.51, Vulkan SDK 1.4.363.0, LEONIDA_VK_VALIDATION=1 + sync validation, 0 errors)

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 and released 4 times with nothing submitted: VA 0x10000 each time
16 MiB arena: a 12 MiB buffer made while a released one waited for a gated read took 294.7 ms (gate open when it returned), got VA 0x10000 (released: 0x10000); the read found 0 of 3145728 words wrong

Open (not in this PR)

  • CreateHeap ranges (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).
  • Postponed CPU Fence::Signal, reproduced: a queue Signal(F,1) still gated, the CPU calls F->Signal(2) (postponed), another queue Wait(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 applies vkSignalSemaphore from the fence-dispatcher callback but never calls holds().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 from cancel() in ~Fence. Worth its own item.

No game files. AI-assisted (Claude).

… 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 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 wrong
  • after the read completed: the next 1 MiB buffer got VA 0x10000
  • 16 MiB arena: a 12 MiB buffer made and released 4 times with nothing submitted: VA 0x10000 each time
  • 16 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 wrong
  • arena 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 slopradish left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@iurysf

iurysf commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 71e83d7 on NVIDIA GeForce RTX 4070, driver 621.01, Windows 11, MSVC 14.44.35207: pass.

Built and ran d3d12_arena_retire_test (Ninja, RelWithDebInfo). Exit 0. An in-flight 1 MiB read kept the released VA at 0x10000; a buffer made while the gate was closed got 0x110000 and the read found 0 of 262144 words wrong. After the read, the next 1 MiB buffer got 0x10000. On a 16 MiB arena, four 12 MiB create/release cycles with nothing submitted reused 0x10000, and a 12 MiB buffer made while a released one waited took 300.0 ms (gate open when it returned), got that VA, and the read found 0 of 3145728 words wrong.

Negative control: the buffer destructor called arena_release immediately again. The test then exited 1: the new buffer got 0x10000 while the gate was still closed and the read found 262144 of 262144 words wrong; the 12 MiB allocation returned in 0.8 ms with the gate still closed and 3145728 of 3145728 words wrong. Restored the source; the test passed again.

AI-assisted (OpenBot team).

@justr4mzi

Copy link
Copy Markdown
Contributor

Verified PR #4607 on Windows 11 (25H2 26200.9974) / AMD Radeon RX 6700 XT (gfx1031, Adrenalin 26.11.2, Vulkan 1.4.315, MSVC 19.44.35230.0, Vulkan SDK 1.4.363.0) at head 71e83d7e:

  • Build: configured Ninja RelWithDebInfo cleanly and built d3d12_arena_retire_test, d3d12_queue_handover_test, and retire_tickets_test ([15/15], exit 0).
  • CTest (standard & LEONIDA_VK_VALIDATION=1):
    • Standard: 3/3 passed (9.46 s): retire_tickets (0.03 s), d3d12_queue_handover (7.84 s), d3d12_arena_retire (1.41 s).
    • LEONIDA_VK_VALIDATION=1: 3/3 passed (8.62 s): retire_tickets (0.01 s), d3d12_queue_handover (7.46 s), d3d12_arena_retire (1.14 s).
  • Direct AMD RX 6700 XT runs under LEONIDA_VK_VALIDATION=1 (0 Vulkan validation errors, exit 0):
    • d3d12_arena_retire_test.exe:
      • 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 and released 4 times with nothing submitted: VA 0x10000 each time
      • 16 MiB arena: a 12 MiB buffer made while a released one waited for a gated read took 258.9 ms (gate open when it returned), got VA 0x10000 (released: 0x10000); the read found 0 of 3145728 words wrong
    • d3d12_queue_handover_test.exe: Vulkan layer: 371 cases passed, every one exact; Host D3D12 (AMD Radeon RX 6700 XT): 369 cases passed, every one exact.
  • CTest registration, docs & hygiene: tests/test_ctest_registration.py 4/4 OK (0.435s), python -I -m unittest discover -s tests -p "test_docs*.py" 20/20 OK (17.475s), scripts/check_no_game_data.py & git diff --check upstream/main...HEAD clean (0).

Code review of DeferredDeletionQueue::Kind::arena_range, VulkanContext::reclaim_retired, and DeviceState::arena_allocate/arena_retire/arena_retired looks solid: (offset << 32) | bytes fits cleanly below 4 GiB, callbacks execute outside deletion_queue_ mutex, set_arena_release(nullptr, nullptr) runs after teardown drain_deleted_resources(UINT64_MAX), and locking order (resource_mutex_ -> tracker_mutex_ -> submit_mutex_) prevents premature reuse without deadlocking.

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.
@requiemsnkrp

Copy link
Copy Markdown
Author

@ligmaSec thanks — done in 10993a6: Rig::init() creates the DIRECT reader first, then tries a COPY queue for the gate; if it can't, gate_queue = reader and submit_gate() records DIRECT lists. The test prints which mode it used. RTX 3060, validation + sync validation, 0 errors:

  • two queues: gate on a COPY queue, the read on a DIRECT one … 0 of 262144 words wrong / 0 of 3145728 words wrong
  • one queue (COPY creation skipped by a local override): gate on the reader's DIRECT queue (no second queue) … 0 of 262144 / 0 of 3145728 words wrong
  • one queue + main's immediate range release: 262144 of 262144 words wrong, 3145728 of 3145728 words wrong, exit 1.

AI-assisted (Claude).

@Exopus-0x65f7ad0

Copy link
Copy Markdown

Tested 10993a6f on Windows 11 (26300), RTX 5060 Ti / 621.14, MSVC 14.51 (VS 2026), Vulkan SDK 1.4.363.0, RelWithDebInfo, clean build with -DLEONIDA_LLVM_MC (translated-shader D3D12 gate tests included), full LEONIDA_VK_VALIDATION=1 ctest -j4: 146/146 passed, 0 VUIDs, 0 build errors. AI-assisted (Claude Code).

@Vonsek

Vonsek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

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 ctest -R "^(d3d12_arena_retire)$" (the tests this PR adds/changes): pass on both GPUs, 1/1 each. No game files.

AI-assisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants