d3d12: upload and readback buffers live in the GPU address arena (RT-UPLOAD-ARENA) #444

Open
github-bot wants to merge 2 commits from github-bot/gh-444 into main AGit
Owner

@⁠Kokonotcu on GitHub · 2026-11-24 05:28 UTC · original
Kokonotcu:pr/m8-upload-arena → main · +466 −15 · 9 files · 2 commits · open

RT-UPLOAD-ARENA
Claimed in the coordination issue as: RT-UPLOAD-ARENA (upload and readback buffers live in the GPU address arena, so a V# can hold their VA)

What and why

Committed upload and readback buffers had memory of their own, and their console VA was their device address minus
the arena's (heap_base): negative on the test GPUs. Root descriptors and vertex/index views resolve such a VA, but
a V# keeps 48 bits of it, so a shader that reads an upload buffer through a descriptor (per-frame constants in an
upload heap behind a table, the usual case) goes to a wild address and the submission is lost. This is the failure
docs/d3d12_vulkan.md measured as D3D-53; the 32-bit descriptor-table pointers most shaders use and the BVH
builder's 2^40 rule cannot hold such a VA either.

Translating the VA at every use cannot fix the V# case: the descriptor bytes are written by the title (or the layer)
into heap memory the GPU reads, where no translation follows them. So this moves the buffers instead:

  • create_buffer places every CPU-visible committed buffer (UPLOAD, READBACK, CPU-visible CUSTOM) in the arena,
    which is host-visible and coherent: console VA = arena offset, Map returns the arena's mapping at that offset,
    the bytes start zeroed there, and the range is registered with the arena buffer like a default-heap buffer and
    returned with the resource. Every VA consumer (root descriptors, VBV/IBV, indirect arguments, copies, BVH inputs,
    host_at) already resolves arena ranges. Placed buffers in upload/readback heaps were arena ranges already.
  • Budget: these buffers now share LEONIDA_ARENA_MB (256 MiB by default) and the arena's memory type, which prefers
    DEVICE_LOCAL | HOST_VISIBLE (Resizable BAR), so a CPU read of a readback buffer there is uncached. A buffer the
    arena has no room for keeps its own memory with the old VA, logged once; LEONIDA_UPLOAD_SYSMEM=1 keeps upload
    buffers out of the arena that way.
  • rt::d3d12::vulkan_buffer_range (for tests) gives the VkBuffer and offset that hold a buffer;
    d3d12_indirect_rt_test uses it for its upload/readback buffers, which no longer have a VkBuffer of their own.
  • Docs: docs/d3d12_vulkan.md section 3.3 and the measured D3D-53 paragraph.

Tested

New d3d12_upload_arena (translator-output cs_upload_read.asm): a compute shader reads an UPLOAD buffer through a
raw SRV V# in a descriptor table and writes a default-heap UAV, the result is copied to a READBACK buffer and read
through its Map pointer: VAs 0x20000 and 0x40000, 256 of 256 words match, clean under LEONIDA_VK_VALIDATION=1.
With the upload buffer kept out of the arena (LEONIDA_UPLOAD_SYSMEM=1, the old placement) its VA is
0xfffffffd905f0000 and the test fails before dispatching.

Full ctest -C RelWithDebInfo -j 4 on this branch (then based on d2cd6c0d9; rebased onto e21080465 afterwards,
whose new commits touch none of these files): 296 of 304 pass, including d3d12_upload_arena, d3d12_indirect_rt,
d3d12_compute and d3d12_vulkan_device. The 8 that fail do so for reasons outside this change on this PC:
d3d12_subgroup (SegFault) and d3d12_set_descriptor_heaps (line 341: CHECK failed: !blob.empty()) fail the same
way on main e21080465 without this commit; root_signature_oracle, d3d12_close_parity, d3d12_pso_validation
and d3d12_root_args_validation compare with the host D3D12 runtime / WARP of this Windows 10 machine and fail on rows
that do not involve buffer placement; d3d12_texture_tiles fails on this NVIDIA driver; xdxr_build_check timed out
under the parallel load.

GPUs tested: NVIDIA GeForce RTX 3080 Laptop GPU, driver 617.42, Windows 10 22H2 (19045), Intel Core i7-11800H

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main (d2cd6c0d9) and merges cleanly.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption, including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md (no translator change).

🤖 Generated with Claude Code

**@⁠Kokonotcu** on GitHub · 2026-11-24 05:28 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/444) `Kokonotcu:pr/m8-upload-arena` → `main` · +466 −15 · 9 files · 2 commits · open RT-UPLOAD-ARENA Claimed in the coordination issue as: RT-UPLOAD-ARENA (upload and readback buffers live in the GPU address arena, so a V# can hold their VA) ### What and why Committed upload and readback buffers had memory of their own, and their console VA was their device address minus the arena's (`heap_base`): negative on the test GPUs. Root descriptors and vertex/index views resolve such a VA, but a V# keeps 48 bits of it, so a shader that reads an upload buffer through a descriptor (per-frame constants in an upload heap behind a table, the usual case) goes to a wild address and the submission is lost. This is the failure `docs/d3d12_vulkan.md` measured as D3D-53; the 32-bit descriptor-table pointers most shaders use and the BVH builder's 2^40 rule cannot hold such a VA either. Translating the VA at every use cannot fix the V# case: the descriptor bytes are written by the title (or the layer) into heap memory the GPU reads, where no translation follows them. So this moves the buffers instead: - `create_buffer` places every CPU-visible committed buffer (UPLOAD, READBACK, CPU-visible CUSTOM) in the arena, which is host-visible and coherent: console VA = arena offset, `Map` returns the arena's mapping at that offset, the bytes start zeroed there, and the range is registered with the arena buffer like a default-heap buffer and returned with the resource. Every VA consumer (root descriptors, VBV/IBV, indirect arguments, copies, BVH inputs, `host_at`) already resolves arena ranges. Placed buffers in upload/readback heaps were arena ranges already. - Budget: these buffers now share `LEONIDA_ARENA_MB` (256 MiB by default) and the arena's memory type, which prefers `DEVICE_LOCAL | HOST_VISIBLE` (Resizable BAR), so a CPU read of a readback buffer there is uncached. A buffer the arena has no room for keeps its own memory with the old VA, logged once; `LEONIDA_UPLOAD_SYSMEM=1` keeps upload buffers out of the arena that way. - `rt::d3d12::vulkan_buffer_range` (for tests) gives the `VkBuffer` and offset that hold a buffer; `d3d12_indirect_rt_test` uses it for its upload/readback buffers, which no longer have a `VkBuffer` of their own. - Docs: `docs/d3d12_vulkan.md` section 3.3 and the measured D3D-53 paragraph. ### Tested New `d3d12_upload_arena` (translator-output `cs_upload_read.asm`): a compute shader reads an UPLOAD buffer through a raw SRV V# in a descriptor table and writes a default-heap UAV, the result is copied to a READBACK buffer and read through its `Map` pointer: VAs 0x20000 and 0x40000, 256 of 256 words match, clean under `LEONIDA_VK_VALIDATION=1`. With the upload buffer kept out of the arena (`LEONIDA_UPLOAD_SYSMEM=1`, the old placement) its VA is 0xfffffffd905f0000 and the test fails before dispatching. Full `ctest -C RelWithDebInfo -j 4` on this branch (then based on `d2cd6c0d9`; rebased onto `e21080465` afterwards, whose new commits touch none of these files): 296 of 304 pass, including `d3d12_upload_arena`, `d3d12_indirect_rt`, `d3d12_compute` and `d3d12_vulkan_device`. The 8 that fail do so for reasons outside this change on this PC: `d3d12_subgroup` (SegFault) and `d3d12_set_descriptor_heaps` (`line 341: CHECK failed: !blob.empty()`) fail the same way on `main` `e21080465` without this commit; `root_signature_oracle`, `d3d12_close_parity`, `d3d12_pso_validation` and `d3d12_root_args_validation` compare with the host D3D12 runtime / WARP of this Windows 10 machine and fail on rows that do not involve buffer placement; `d3d12_texture_tiles` fails on this NVIDIA driver; `xdxr_build_check` timed out under the parallel load. GPUs tested: NVIDIA GeForce RTX 3080 Laptop GPU, driver 617.42, Windows 10 22H2 (19045), Intel Core i7-11800H AI-assisted: yes (Claude Code) ### Checklist - [x] Based on current `main` (d2cd6c0d9) and merges cleanly. - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption, including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md` (no translator change). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Committed upload and readback buffers had their own memory, and their
console VA was their device address minus heap_base: negative on the test
GPUs. Root descriptors and views resolved that, but a V# keeps 48 bits of
it, so a shader reading an upload buffer through a descriptor went to a
wild address and the submission was lost (docs "Measured (D3D-53)"); the
32-bit table pointers (97.6% of shaders) and the BVH builder's 2^40 rule
cannot hold such a VA either.

The evidence says move them, not translate the VA everywhere: the V# is
written by the title or the layer into descriptor memory the GPU reads,
where no translation can follow it. So create_buffer now places every
CPU-visible committed buffer (upload, readback, CPU-visible custom heaps)
in the arena, which is host-visible and coherent: VA = arena offset, Map
returns the arena mapping, zeroed there, registered with the arena buffer
and offset like a default-heap buffer, released with the resource. Every VA
consumer (root descriptors, VBV/IBV, indirect arguments, copies, BVH
inputs, host_at) already resolves arena ranges. Placed buffers were arena
ranges already.

Budget: they share LEONIDA_ARENA_MB (256 MiB default) and the arena's
memory type (DEVICE_LOCAL | HOST_VISIBLE where there is Resizable BAR), so
CPU reads of readback buffers are uncached there. A buffer the arena has no
room for keeps its own memory with the old VA, logged once;
LEONIDA_UPLOAD_SYSMEM=1 keeps upload buffers out of it that way.

vulkan_buffer_range (for tests) gives the VkBuffer and offset holding a
buffer; d3d12_indirect_rt_test uses it for its upload/readback buffers.

Test: d3d12_upload_arena (cs_upload_read.asm reads an upload buffer through
a raw SRV V# in a table, the result is read back through a readback
buffer's Map pointer; VAs 0x20000 and 0x40000; clean under validation).
With the upload buffer outside the arena (LEONIDA_UPLOAD_SYSMEM=1, the old
placement) its VA is 0xfffffffd905f0000 and the test fails before
dispatching.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
D3D12 binds no READBACK resource to a shader (no SRV, CBV or UAV), so an arena range only took space a later default-heap buffer needs: with the default 256 MiB arena, a live 160 MiB readback buffer made a following 128 MiB DEFAULT buffer fail with E_OUTOFMEMORY (arena full). The arena's memory is also uncached for the CPU reads a readback buffer exists for. Readback buffers now keep their own HOST_CACHED memory, as before M8; upload buffers (which a V# must reach) stay in the arena.

New d3d12_readback_arena_test (no shaders): 160 MiB READBACK then 128 MiB DEFAULT both succeed, the default buffer at an arena offset, and a copy into the readback buffer's tail reads back. Without this change it fails: DEFAULT 0x8007000e. d3d12_upload_arena_test checks only the upload buffer's VA as an arena offset and has the same pressure check. d3d12_vulkan_device, d3d12_indirect_rt and d3d12_compute pass; 0 validation errors (RTX 3080 Laptop).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:00:30 +00:00
Author
Owner

@⁠moffington on GitHub · 2026-11-24 05:37 UTC · original

I checked arena pressure at 5a52674a against the merge base (e2108046). With the default 256 MiB arena, keeping a 128 MiB committed READBACK buffer live caused a subsequent 128 MiB DEFAULT buffer to hit E_OUTOFMEMORY in the allocator probe. On the merge base, READBACK took the separate allocation path and DEFAULT got its arena range. On the PR, reversing the order or releasing READBACK first let DEFAULT get its range again.

The probe ran the actual allocator and placement branches and stopped before Vulkan calls. Could you try the same sequence through CreateCommittedResource on Windows and check both HRESULTs? The case I’m concerned about is a readback consuming arena space that a later default buffer requires.

**@⁠moffington** on GitHub · 2026-11-24 05:37 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/444#issuecomment-7123127930) I checked arena pressure at 5a52674a against the merge base (e2108046). With the default 256 MiB arena, keeping a 128 MiB committed READBACK buffer live caused a subsequent 128 MiB DEFAULT buffer to hit E_OUTOFMEMORY in the allocator probe. On the merge base, READBACK took the separate allocation path and DEFAULT got its arena range. On the PR, reversing the order or releasing READBACK first let DEFAULT get its range again. The probe ran the actual allocator and placement branches and stopped before Vulkan calls. Could you try the same sequence through CreateCommittedResource on Windows and check both HRESULTs? The case I’m concerned about is a readback consuming arena space that a later default buffer requires.
Author
Owner

@⁠dcastell1 on GitHub · 2026-11-24 06:19 UTC · original

Tested 5a52674a7 on NVIDIA GeForce RTX 5090 (GB202), driver 621.24, Windows 11 10.0.26300: pass.

Built the branch on Windows (MSVC 14.44.35207, Ninja, RelWithDebInfo, Vulkan SDK 1.4.363.1) and ran the four executables the change touches:

test result
d3d12_upload_arena_test pass, exit 0 — VAs 0x20000 and 0x40000; 256 words read through a V# on the upload buffer and read back through the readback buffer's Map pointer match
the same with LEONIDA_VK_VALIDATION=1 pass, exit 0, no message
the same with LEONIDA_UPLOAD_SYSMEM=1 fails where the PR says it should: VA 0xfffffffd043b1000 is not an arena offset, line 102: va && va < (1ull << 32)
d3d12_indirect_rt_test PASS (0 validation errors)
d3d12_compute_test pass: CreateConstantBufferView: 64/64 dwords matched for CBV A, 32 in-bounds + 32 OOB-zero matched at +128, SRV raw/structured/typed and the three dispatches through the arena
d3d12_vulkan_device_test pass, including the placed clears and castable formats on this device

The negative control is the interesting one for Blackwell: with the buffer kept out of the arena its console VA is 0xfffffffd…, which is exactly the 64-bit value a V# cannot hold, so the test stops at the guard instead of dispatching.

Arena budget in these runs: peak end 0.3 of 256.0 MiB, 5 allocations, 0 refused; d3d12_vulkan_device_test logs upload heaps in VRAM (DEVICE_LOCAL | HOST_VISIBLE, Resizable BAR) on this card, so the readback-uncached note in the PR applies here too.

ctest itself was not on PATH in my shell, so I ran the test executables directly; that is the only difference from the ctest -C RelWithDebInfo line in the PR body. AI-assisted (opencode). No game files in any of these runs.

**@⁠dcastell1** on GitHub · 2026-11-24 06:19 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/444#issuecomment-7123820951) Tested `5a52674a7` on **NVIDIA GeForce RTX 5090 (GB202), driver 621.24, Windows 11 10.0.26300**: pass. Built the branch on Windows (MSVC 14.44.35207, Ninja, RelWithDebInfo, Vulkan SDK 1.4.363.1) and ran the four executables the change touches: | test | result | |---|---| | `d3d12_upload_arena_test` | pass, exit 0 — `VAs 0x20000 and 0x40000; 256 words read through a V# on the upload buffer and read back through the readback buffer's Map pointer match` | | the same with `LEONIDA_VK_VALIDATION=1` | pass, exit 0, no message | | the same with `LEONIDA_UPLOAD_SYSMEM=1` | fails where the PR says it should: `VA 0xfffffffd043b1000 is not an arena offset`, `line 102: va && va < (1ull << 32)` | | `d3d12_indirect_rt_test` | PASS (0 validation errors) | | `d3d12_compute_test` | pass: `CreateConstantBufferView: 64/64 dwords matched for CBV A, 32 in-bounds + 32 OOB-zero matched at +128`, SRV raw/structured/typed and the three dispatches through the arena | | `d3d12_vulkan_device_test` | pass, including the placed clears and castable formats on this device | The negative control is the interesting one for Blackwell: with the buffer kept out of the arena its console VA is `0xfffffffd…`, which is exactly the 64-bit value a V# cannot hold, so the test stops at the guard instead of dispatching. Arena budget in these runs: peak end 0.3 of 256.0 MiB, 5 allocations, 0 refused; `d3d12_vulkan_device_test` logs `upload heaps in VRAM (DEVICE_LOCAL | HOST_VISIBLE, Resizable BAR)` on this card, so the readback-uncached note in the PR applies here too. `ctest` itself was not on `PATH` in my shell, so I ran the test executables directly; that is the only difference from the `ctest -C RelWithDebInfo` line in the PR body. AI-assisted (opencode). No game files in any of these runs.
Author
Owner

@⁠Kokonotcu on GitHub · 2026-11-24 07:09 UTC · original

Thanks, confirmed through CreateCommittedResource on Windows (RTX 3080 Laptop): with the default 256 MiB arena, a live 160 MiB READBACK buffer returned S_OK and the following 128 MiB DEFAULT buffer then failed with 0x8007000e ("GPU address arena full").

Fixed in a902c78ab. Readback buffers stay out of the arena and keep their own HOST_CACHED memory, as before this PR. D3D12 binds no READBACK resource to a shader (no SRV, CBV or UAV), so an arena range only cost the space you found, plus uncached CPU reads. Upload buffers, which a V# has to reach, stay in the arena.

New d3d12_readback_arena_test (no shaders, so it builds without llvm-mc):

  • 160 MiB READBACK, then 128 MiB DEFAULT: both S_OK, the default buffer at arena offset 0x10000.
  • A copy into the readback buffer's tail reads back.
  • Without the fix it fails with DEFAULT 0x8007000e.

d3d12_upload_arena_test now checks only the upload buffer's VA as an arena offset and runs the same pressure sequence. d3d12_vulkan_device, d3d12_indirect_rt and d3d12_compute pass, with 0 validation errors under LEONIDA_VK_VALIDATION=1.

🤖 Generated with Claude Code

**@⁠Kokonotcu** on GitHub · 2026-11-24 07:09 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/444#issuecomment-7124682012) Thanks, confirmed through `CreateCommittedResource` on Windows (RTX 3080 Laptop): with the default 256 MiB arena, a live 160 MiB READBACK buffer returned `S_OK` and the following 128 MiB DEFAULT buffer then failed with `0x8007000e` ("GPU address arena full"). Fixed in a902c78ab. Readback buffers stay out of the arena and keep their own HOST_CACHED memory, as before this PR. D3D12 binds no READBACK resource to a shader (no SRV, CBV or UAV), so an arena range only cost the space you found, plus uncached CPU reads. Upload buffers, which a V# has to reach, stay in the arena. New `d3d12_readback_arena_test` (no shaders, so it builds without llvm-mc): - 160 MiB READBACK, then 128 MiB DEFAULT: both `S_OK`, the default buffer at arena offset 0x10000. - A copy into the readback buffer's tail reads back. - Without the fix it fails with DEFAULT `0x8007000e`. `d3d12_upload_arena_test` now checks only the upload buffer's VA as an arena offset and runs the same pressure sequence. `d3d12_vulkan_device`, `d3d12_indirect_rt` and `d3d12_compute` pass, with 0 validation errors under `LEONIDA_VK_VALIDATION=1`. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
This pull request has changes conflicting with the target branch.
  • runtime/shims/d3d12_com.h
  • runtime/shims/d3d12_vulkan_device.cpp
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/444/head:github-bot/gh-444'
git switch 'github-bot/gh-444'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-444'
git switch 'github-bot/gh-444'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-444'
git switch 'github-bot/gh-444'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-444'
git switch 'main'
git merge --squash 'github-bot/gh-444'
git switch 'main'
git merge --ff-only 'github-bot/gh-444'
git switch 'main'
git merge 'github-bot/gh-444'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.