Skip to content

runtime: an Xbox page mapper on Vulkan sparse binding (not wired yet) - #5378

Open
maxxgabriel wants to merge 9 commits into
ItsNotRexed:mainfrom
maxxgabriel:d3d-546-page-mapper
Open

maxxgabriel wants to merge 9 commits into
ItsNotRexed:mainfrom
maxxgabriel:d3d-546-page-mapper

Conversation

@maxxgabriel

Copy link
Copy Markdown

Claimed in the coordination issue as: D3D-546 (#313)

What and why

runtime/src/xbox_paging.h models RegisterPagePoolX, CopyPageMappingsX, CopyPageMappingsBatchX and ClearPageMappingsX on Vulkan sparse binding:

  • Arena: one sparse, device-addressable VkBuffer. A console VA is an offset into it and its device address never changes; a remap only changes what backs a page.
  • Physical pages: 64 KiB, from 64 MiB device-local chunks (PagePool). On NVIDIA only device-local memory can back a sparse buffer page.
  • Calls: each is validated in full and becomes one vkQueueBindSparse. Runs contiguous in both the arena and a chunk merge into one bind.
  • Ordering: VulkanQueue::bind_sparse_ordered puts the bind on the queue timeline as bind_tiles does. Both now share one submit helper; bind_tiles behaves as before.

Only range type 0 is documented publicly (Xbox-GDK-Samples AdvancedESRAM), so other range types are refused. Nothing calls this yet, since the vtable slots aren't measured. docs/xbox_paging.md has the model, the costs and what's left.

Measured on a GTX 1660 SUPER: a call costs about 0.25 ms, plus 1.5–2 µs per contiguous page or 8–10 µs per scattered page. A remap between two dispatches added about 0.7 ms of GPU gap, and the call returned on the CPU in 0.1 ms.

Tested

  • vk_xbox_paging_test: PASS, and 0 errors under LEONIDA_VK_VALIDATION=1. The zero-read check runs only where residencyNonResidentStrict is set; with less than 64 GiB of sparse address space the test skips (77).
  • vk_queue_test, d3d12_sparse_queue_family_test and d3d12_vulkan_device_test (covers the refactored bind_tiles): all PASS.
  • No /W4 warnings in the changed files.

NVIDIA GeForce GTX 1660 SUPER, driver 610.88, Windows 11, MSVC 14.51, Vulkan SDK 1.4.363.1.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (No translator change.)

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Destroying a mapped PagedRegion leaves 64 MiB reserved in a live PagePool. I reproduced this on head 620413c0 on Windows 11 with a GeForce RTX 4090 (32.0.16.2101) and AMD Radeon Graphics (32.0.21043.5001).

I mapped one 64 KiB page, waited for the bind to complete, destroyed the region, released the page, and called trim(). Both GPUs gave these results:

Before region destruction Pages in use after release Bytes freed by trim Bytes still reserved
Explicitly clear the mapping 0 67,108,864 0
Leave the page mapped 0 0 67,108,864

Expected: both paths free the chunk. The region destructor destroys the sparse buffer. The pool still counts its mapping. This prevents the pool from returning the chunk to Vulkan during its lifetime. Please account for outstanding mappings when the region closes. Keep the completed-ticket check for GPU work.

To reproduce, insert this block immediately before const auto stats = mapper.stats(); in runtime/test/vk_xbox_paging_test.cpp.

    for (bool clear : {true, false}) {
        std::vector<PageId> one;
        EXPECT(pool.allocate(1, one));
        {
            PagedRegion temporary;
            EXPECT(temporary.create(d.device, d.device_proc, kPageSize));
            PageMapper m(temporary, pool);
            const auto handle1 = m.register_page_pool(one);
            const PageMappingRange range{PageRangeType::incrementing, 1, 0};
            const PageMappingBatch batch{0, &range, 1};
            EXPECT(m.copy_page_mappings_batch(handle1, {&batch, 1}, submit));
            if (clear) EXPECT(m.clear_page_mappings(0, 1, submit));
            gpu.wait_queue(queue.submitted_ticket());
            EXPECT(m.unregister_page_pool(handle1));
        }
        pool.release(one);
        const auto freed1 = pool.trim(queue.completed_ticket());
        std::printf("region destroyed: clear=%d used=%llu freed=%llu reserved=%llu\n",
                    int(clear), (unsigned long long)pool.pages_in_use(), (unsigned long long)freed1,
                    (unsigned long long)pool.bytes_reserved());
        EXPECT(freed1 == kChunkSize);
    }

Build with cmake --build E:/Leonida-review-work-build --config RelWithDebInfo --target vk_xbox_paging_test. Run vk_xbox_paging_test.exe with LEONIDA_VK_VALIDATION=1. Only the second trim assertion fails. Vulkan reports zero validation errors. The parent has no page mapper.

The unchanged paging, queue, sparse queue family, and Vulkan device tests pass with each adapter selected. AI-assisted.

@maxxgabriel

maxxgabriel commented Nov 23, 2026 •

Copy link
Copy Markdown
Author

Fixed in 04ef583. @shdann Can you run tests again ?

@milas6669

Copy link
Copy Markdown

Tested 6571ec55 (includes the fix 04ef5834) on Windows 11 with an NVIDIA GeForce RTX 3070 (Ampere, GA104; driver 621.24, Vulkan 1.4.357), MSVC 14.44, Ninja — a third GPU family after @shdann's 4090 and AMD iGPU.

cmake --build runtime/build --target vk_xbox_paging_test
vk_xbox_paging_test.exe
INFO pool: 2 chunk(s), 128 MiB reserved, 1040 pages in use
INFO trim freed 128 MiB; 0 chunk(s) left
INFO region destroyed (cleared first): trim freed 64 MiB, 0 bytes still reserved
INFO region destroyed (page still mapped): trim freed 64 MiB, 0 bytes still reserved
INFO mapper: 181 calls, 181 submits, 20144 binds, 71082 pages
PASS

The path that reproduced the leak for @shdann — destroy the region while the page is still mapped, release, trim() — frees the 64 MiB and ends at 0 bytes still reserved here too, as does the cleared-first path.

With LEONIDA_VK_VALIDATION=1: same results, and the harness reports validation errors: 0.

For the record, the bind timings on this card (median of 15): 1024 pages contiguous 1 bind 2.761 ms (2.70 us/page), 1024 scattered 1024 binds 6.928 ms (6.77 us/page); the queued-work gap is 0.007 ms without a remap and 1.445 ms with a 64-page scattered remap, with the remap call returning on the CPU in 0.085 ms while W1 is still running.

I did not run the pre-fix 620413c0; the before state is @shdann's on two machines, this is the after state on a third.

AI-assisted. No game data.

@timbot103

Copy link
Copy Markdown

Tested 6571ec5 natively on Windows 11 Pro 26200 (MSVC 14.44.35207, Vulkan SDK 1.4.363.0), merged onto main 3faac55, on two GPUs that aren't a GTX 1660: an NVIDIA RTX 5080 (616.64) and an AMD Radeon Graphics RDNA2 iGPU (AMD 25.10.30.02). Pass on both.

  • vk_xbox_paging_test: PASS, with no skip. On both GPUs the arena was 64 GiB, all 5 remaps read back in order, the aliasing check read 0xc0ffee00, and the cleared page read 0x00000000/0x00000000. With LEONIDA_VK_VALIDATION=1: PASS, validation errors 0.
  • vk_queue_test, d3d12_sparse_queue_family_test and d3d12_vulkan_device_test: all PASS on both.
  • I also ran the earlier head 04ef583, with the same results.

No game files. AI-assisted (Claude Code).

@maxxgabriel

maxxgabriel commented Nov 23, 2026 •

Copy link
Copy Markdown
Author

@timbot103 thanks. can you also comment the bind timings of your cards ?

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

Reviewed head 6571ec5533.

Build (vcvars64 + Ninja + Vulkan 1.4.363, RelWithDebInfo):
cmake -S runtime -B build ... then cmake --build build --target vk_xbox_paging_test d3d12_buffer_tiles_test

  • .\build\vk_xbox_paging_test.exe -> PASS. 181 calls / 181 submits / 20144 binds / 71082 pages; 1024-page contiguous bind 1.06 ms, scattered 4.31 ms; remap gap 0.005 ms without / 0.61 ms with; trim frees 128 MiB; both region-destroy cases free 64 MiB, 0 reserved.
  • .\build\d3d12_buffer_tiles_test.exe -> PASS (12 tiles across aliased/skipped/reused/remapped; rejected update intact), so the refactored bind_tiles path still works.

Inertness confirmed: xbox_paging.h is included only by its test; runtime/CMakeLists.txt adds only a test executable; bind_sparse_ordered has no production caller (only xbox_paging.h and the test). bind_sparse_ordered/submitted_ticket take state_mutex_; completed_ticket calls the semaphore counter, no shared state.

Verdict: LGTM. #5381 builds on this branch.

@Codejustcode12

Copy link
Copy Markdown

Tested 6571ec5 (base 3faac55, no other PRs) on NVIDIA GeForce GTX 1050 Ti (Pascal, 4 GB), driver 560.94, Windows 10 Enterprise LTSC 21H2 (19044), MSVC 14.44, Windows SDK 10.0.26100, Vulkan SDK 1.4.363, RelWithDebInfo + Ninja: pass. A Pascal / Windows 10 data point next to the 1660 SUPER, 3070, 4090, 5080 and the AMD iGPUs. Synthetic only, no game files.

vk_xbox_paging_test: PASS with no skip (sparseAddressSpaceSize 1 TiB, residencyNonResidentStrict true), twice without and twice with LEONIDA_VK_VALIDATION=1 ("validation errors: 0", no VUIDs). Arena 64 GiB, page memory type 7 (device-local), 5 remaps read back in order, aliasing 0xc0ffee00, cleared page 0x00000000/0x00000000, both "region destroyed" paths end with 0 bytes still reserved. Timings: 1 page 0.33 ms, 256 contiguous 0.19 ms; W1→W2 gap 0.011 ms without a remap, 0.695 ms with a 64-page scattered remap.

vk_queue_test and d3d12_sparse_queue_family_test: exit 0, the same as main 3faac55. d3d12_vulkan_device_test exits 1 at lines 172 and 6790 both here and on main 3faac55 on this GPU (TEST-995, fixed by #4682), so not from this PR.

Negative control for the leak @shdann reported: 04ef583's test against 620413c's runtime/src/xbox_paging.h (vulkan_backend.h is identical in the two) fails at vk_xbox_paging_test.cpp:456 freed_after == kChunkSize: "region destroyed (page still mapped): trim freed 0 MiB, 67108864 bytes still reserved". The same test against 04ef583's xbox_paging.h passes with 0 bytes still reserved. So the leak reproduces on Pascal too, and 04ef583 fixes it.

How: test executables run directly (not ctest).

AI-assisted (Claude Code).

@timbot103

Copy link
Copy Markdown

Bind timings from vk_xbox_paging_test, re-run at your latest head 4098f8f merged onto main 3faac55 (PASS on both GPUs). Windows 11 Pro 26200, Vulkan SDK 1.4.363.0. Each figure is the median of 15 bind round trips, CPU time from the call to the bind's timeline signal.

pages layout binds RTX 5080 (616.64) Radeon RDNA2 iGPU (AMD 25.10.30.02)
1 contiguous 1 0.158 ms (158.1 µs/page) 0.105 ms (104.6 µs/page)
1 scattered 1 0.136 ms (136.0 µs/page) 0.120 ms (120.3 µs/page)
16 contiguous 1 0.181 ms (11.32 µs/page) 0.102 ms (6.39 µs/page)
16 scattered 16 0.240 ms (15.01 µs/page) 0.340 ms (21.24 µs/page)
256 contiguous 1 0.251 ms (0.98 µs/page) 0.376 ms (1.47 µs/page)
256 scattered 256 1.242 ms (4.85 µs/page) 3.832 ms (14.97 µs/page)
1024 contiguous 1 1.172 ms (1.14 µs/page) 0.864 ms (0.84 µs/page)
1024 scattered 1024 3.767 ms (3.68 µs/page) 19.867 ms (19.40 µs/page)

Queued-work test (median of 9):

RTX 5080 Radeon iGPU
W1 on the GPU 2.33 ms 30.38 ms
gap W1 end → W2 start, no remap 0.005 ms 0.032 ms
gap with a 64-page scattered remap 0.555 ms 1.810 ms
remap call returns on the CPU 0.048 ms (no stall) 0.062 ms (no stall)

Contiguous binds cost about the same on both cards. Scattered binds cost about 4x more per page on AMD's driver at 256 pages, and about 5x more at 1,024. The Radeon is the Ryzen 9000's 2-CU iGPU, so its W1 figure says nothing about a desktop RDNA2 card, but the bind costs are the driver's. The 6571ec5 run gave the same picture (1,024 scattered: 3.64 ms vs 20.27 ms).

No game files. AI-assisted (Claude Code).

@caltr98

caltr98 commented Nov 23, 2026

Copy link
Copy Markdown

Tested 4098f8f on NVIDIA GeForce RTX 5080 Laptop GPU, driver 621.14, Windows 11 (build 26300), MSVC 14.44, RelWithDebInfo: pass. vk_xbox_paging passes (1.1 s). AI-assisted.

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 4098f8f on NVIDIA GeForce RTX 4080, driver 617.42: vk_xbox_paging_test exits 0 and ends with "PASS" ("aliasing: write via VA+8 pages, read via VA+9 pages -> 0xc0ffee00", "cleared page reads 0x00000000/0x00000000"), vk_queue_test prints "vk_queue_test: OK", d3d12_vulkan_device_test exits 0, and d3d12_sparse_queue_family_test exits 0 with "LAYER_FINAL PASS". The LEONIDA_VK_VALIDATION=1 run was not reproduced here, and the log does not show whether the zero-read check ran or was skipped.

AI-assisted.

@Jadiac5

Jadiac5 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 4098f8f on Windows 11, AMD Radeon RX 9070 XT (RDNA4), driver 32.0.32029.2010: pass

  • Built vk_xbox_paging_test, vk_queue_test, d3d12_sparse_queue_family_test, d3d12_vulkan_device_test at the head (RelWithDebInfo, Ninja, MSVC 19.44).
  • ctest -R "vk_xbox_paging|d3d12_vulkan_device|d3d12_sparse" -j1: 4 of 4 pass (vk_xbox_paging 14 s, d3d12_vulkan_device 7 s, d3d12_sparse_queue_family 13 s, d3d12_sparse_queue_family_contract 1 s). vk_queue_test.exe passes.
  • vk_xbox_paging_test.exe run 6 times: PASS every time. With LEONIDA_VK_VALIDATION=1: PASS, "validation errors: 0". Arena 64 GiB; the 5 remaps read back in order with a fixed device address; aliasing and cleared-page reads correct.
  • The leak fix in 04ef583 holds here: "region destroyed (cleared first): trim freed 64 MiB, 0 bytes still reserved" and the same with the page still mapped.
  • Bind round trip, median of 15: 1 page 0.19 ms; 256 pages contiguous 0.32 ms (1 bind), scattered 11.2 ms (256 binds); 1024 pages contiguous 0.64 ms, scattered 18.1 ms. Gap between two queued GPU jobs (3.6 ms each): 0.015 ms without a remap, 1.43 ms with a 64-page scattered remap. The remap call returned in 0.044 ms while the first job was still running.
  • python -I tests/test_docs_links.py and tests/test_text_encoding.py (docs changed): pass.
  • The vulkan_backend.h change (submit_sparse_binds split out of bind_tiles) reads as a pure move: same ticket, same wait list, same cleanup on failure. bind_tiles still applies changes only after a nonzero ticket.
  • Finding, from reading xbox_paging.h, not run: a PageId has no generation. PagePool::trim resets a freed chunk to Chunk{}, and new_chunk reuses the first empty slot, so after release + trim + a new allocate the same PageIds name new pages. A pool registered earlier with the released ids passes allocated(id) in copy_page_mappings_batch (xbox_paging.h, the pool_.allocated(id) check) and maps pages that now belong to another allocation. The check added in this PR only refuses ids whose chunk is gone at that moment. Only matters if a title maps from a pool after releasing its pages.
  • Not done: the same run on the merge base (this PR adds the test, nothing to compare). Wine and the game-side wiring (the header says the device proxy wires it later).

AI-assisted (Claude Code).

# Conflicts:
#	runtime/src/vulkan_backend.h
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.

9 participants