Skip to content

fix(kyty): back the device address page table only where buffers are registered - #313

Open
Ryongen wants to merge 2 commits into
simnJS:AnyKytyfrom
Ryongen:fix/sparse-bda-page-table
Open

Ryongen wants to merge 2 commits into
simnJS:AnyKytyfrom
Ryongen:fix/sparse-bda-page-table

Conversation

@Ryongen

@Ryongen Ryongen commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What

m_bda_pagetable_buffer = 8 B per 16 KiB page of the whole guest range = 768 MiB of VRAM, almost all zero.

  • Buffer(…, Buffer::SparseResident, flags, size): sparse binding + residency buffer without memory when graphics.partial_buffers_enabled, else a normal buffer. partial_buffers_enabled needs sparseBinding, sparseResidencyBuffer, residencyNonResidentStrict and a sparse-binding queue; it enables only sparseBinding and sparseResidencyBuffer.
  • Buffer::Commit(offset, size): for each missing 64 KiB block, allocates memory (one VMA allocation each), binds it to a plain transfer buffer and zero-fills it in its own submit; vkQueueBindSparse waits on that submit's semaphore, then on a fence. A block is never visible before it holds zeros. A failed allocation logs each heap's usage and budget.
  • BufferCache commits before writing page table entries and commits the RT context entry at start. Unbound reads return 0 = not mapped (as before); fills of unbound ranges are discarded.

Tested

Windows 10, RTX 2070 SUPER 617.42, 3D scene of a title that streams textures:

  • This version, 2 runs: in the one with a block counter, 48 blocks of 64 KiB bound (3 MiB of the 768 MiB), each zeroed before its bind. Neither run had a device loss, an unfinished tick or a Buffer: stop. Both ended at ~3 min on my 7800 MiB VRAM guard, from streamed textures (kyty: device memory cap does not bound VRAM on NVIDIA/Windows; heap-pinned textures are never freed #315), not from this buffer.
  • Previous version (fill after bind): 2 MiB bound after 6.7 min, 766 MiB less VRAM.
  • kyty_shim_bda_page_table_sparse and kyty_shim_bda_page_table_dense (unbound entries, untouched entries of a newly backed block, registration, removal) and kyty_shim_buffer_cache_pages pass with VK_LAYER_KHRONOS_validation 1.4.363 inserted, with no VUID- messages. check_conventions.py and check_kyty_settings.py pass.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 2070 SUPER, 617.42, Windows 10 22H2 (game runs and focused ctest with the validation layer)
  • AI-assisted: yes

@simnJS

simnJS commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thanks. Getting 766 MiB back for 2 MiB bound is a clear win, and this is a good fix to have now. (Longer term, the page table goes away: device addresses would be served from a sparse mirror of guest memory. That work is paused, so this PR is worth having.) A few changes first:

  1. Split it from feat(kyty): create large block-compressed textures without their top mips under memory pressure #312: the branch carries the feat(kyty): create large block-compressed textures without their top mips under memory pressure #312 commit, so it fails the same settings check and carries the same commit message problem. Please enable sparseBinding and sparseResidencyBuffer on their own, without making partial_buffers_enabled depend on sparseResidencyImage2D, so this can be merged alone.
  2. Entries become visible before they are zeroed: Commit() binds a fresh block, then records the zero fill in the current command buffer. The bind completes before that command buffer is submitted, so commands recorded earlier in the same command buffer, and work still running from earlier submits, can read the new block before the fill runs. The other 8191 entries of the block then hold whatever the VMA memory held before, and a shader that probes an unregistered page (the normal device-address fault path) gets a garbage device address instead of 0. Please zero the memory before it is bound: for example, fill it through a plain buffer bound to the same allocation, in its own submit, before vkQueueBindSparse, or keep a pool of zeroed blocks.
  3. Unbound reads only return 0 when residencyNonResidentStrict is VK_TRUE. Please fall back to the normal buffer when it is not.
  4. Nit: each block is its own 64 KiB vmaAllocateMemory, and a failure EXITs. That is fine for about 32 blocks, but please log the heap budget in that message, in case it ever happens near the memory cap.

On GPU cost: the table is read on every device-address access, but each lookup is a random 8-byte read, and in our measurements of sparse buffers, random reads cost the same however the 64 KiB blocks were placed. No objection there.

@simnJS simnJS added perf Speed: frames per second, CPU or GPU time, stutters area: gpu-memory Memory tracking, write faults, buffer and texture caches, write-backs medium A few days of work; needs some knowledge of the code needs: AMD Needs an AMD GPU (RADV or AMD's Windows driver) labels Nov 26, 2026

@simnJS simnJS left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please rebase the sparse page table directly onto AnyKyty so it can land independently of #312. Gate it on sparse binding, a sparse-capable queue, sparseResidencyBuffer and residencyNonResidentStrict, with a dense fallback. A bind currently exposes backing before the zero fill is recorded. Order every reader before the bind or after initialization. Please cover unbound lookups, untouched entries in newly bound blocks, registration and removal, and the dense fallback under validation. Include the heap budget in allocation failures.

@Ryongen
Ryongen force-pushed the fix/sparse-bda-page-table branch from e92a88b to a733a97 Compare November 26, 2026 16:23
@Ryongen

Ryongen commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Updated; the branch is now one commit on 688dbf145d:

  1. Split from feat(kyty): create large block-compressed textures without their top mips under memory pressure #312: the branch no longer carries it. sparseBinding and sparseResidencyBuffer are enabled on their own, when the device has both, residencyNonResidentStrict, and a sparse-binding queue. Nothing depends on sparseResidencyImage2D any more.
  2. Zeroed before bound: each new block's memory is first bound to a plain transfer buffer and zero-filled in its own submit. That submit signals a semaphore, and vkQueueBindSparse waits on it. The fill no longer goes into the current command buffer, so no earlier command or submit can see the block before it holds zeros.
  3. residencyNonResidentStrict: required for the sparse path. Without it, the page table is the plain device-local buffer, as before.
  4. Budget in the failure message: a failed block allocation logs each heap's usage and budget before it stops.

Tested on Windows (RTX 2070 SUPER) in two runs of the same scene, with no device loss or stop in either. The run with a block counter bound 48 blocks (3 MiB of 768 MiB), each zeroed before its bind. check_conventions.py and check_kyty_settings.py pass. Validation-layer ctest not run yet.

@Ryongen

Ryongen commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Your review crossed my push by a few seconds: the rebase onto AnyKyty, the gate with its dense fallback, zeroing before the bind and the heap budget are in a733a97ec0 (see my comment above). This adds the tests in 2bcd370522.

kyty_shim_bda_page_table_sparse and kyty_shim_bda_page_table_dense run the same checks on the real BufferCache page table, the first on a device with sparseBinding, sparseResidencyBuffer and residencyNonResidentStrict enabled (it skips without them), the second on the dense fallback:

  • an entry in a table block that no buffer was registered in reads 0;
  • entries read before any registration in the block read 0;
  • after a buffer is registered, its pages hold its device address and every other entry of that newly backed 64 KiB block reads 0;
  • after the buffer is removed, its entries read 0 again.

TestVulkanDevice::Initialize takes an optional sparse_buffers flag for this; other tests are unchanged.

Run on Windows, RTX 2070 SUPER 617.42, with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, VK_LOADER_DEBUG=layer and layer 1.4.363: both pass, as does kyty_shim_buffer_cache_pages. The loader inserted the layer in each, and there were no VUID- messages. With the zero fill on removal disabled, both fail on the removal check.

@simnJS simnJS left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The split from #312, strict-residency gate, dense fallback, zero-before-bind ordering and heap diagnostics are in this revision. The sparse and dense fixture results also cover the requested cases. GitHub now reports a conflict with AnyKyty. Please rebase, retain both test registrations and test friend declarations, and rerun both CI workflows. Please confirm the sparse and dense cases under validation on the rebased head.

…registered

The page table holds an entry for every 16 KiB page of the guest address space and took 768 MiB of VRAM. It is now a sparse-resident buffer whose 64 KiB blocks are zeroed and then bound on first write, on devices with strict non-resident reads; elsewhere it stays a plain buffer.
…se residency

Checks that unbound and untouched entries read 0, registered pages hold their device addresses and removed ones read 0 again, on a sparse-resident table and on the dense fallback.
@Ryongen

Ryongen commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto AnyKyty (90a48455d2) as 729c1f9fef and 6b2320a40c. The two conflicts kept both sides: BufferCache keeps the IndirectDispatchTestAccess and BufferCacheBdaTestAccess friends, and the tests' CMakeLists.txt registers kyty_shim_indirect_dispatch_dimensions and both kyty_shim_bda_page_table_* tests.

On the rebased head, Windows, RTX 2070 SUPER 617.42, with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, VK_LOADER_DEBUG=layer and layer 1.4.363: kyty_shim_bda_page_table_sparse, kyty_shim_bda_page_table_dense, kyty_shim_buffer_cache_pages and kyty_shim_indirect_dispatch_dimensions pass. On AnyKyty the last two pass as well. Every log shows the loader inserting the layer, none has was not found but was requested, and none has a VUID- line. check_conventions.py and check_kyty_settings.py pass; the push reruns both workflows.

mjnhbg3 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

I closed the companion PR I mistakenly opened against the contributor fork. The focused fixture fix remains available as one commit based on this PR's current head 6b2320a40c. It registers the dense test buffer at a page-aligned guest address through BufferCache, avoiding the ASan Buffer: invalid copy range failure. Exact-head CI, including ASan, passed. Please cherry-pick the commit if it fits this PR.

This branch has not been deployed

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

Labels

area: gpu-memory Memory tracking, write faults, buffer and texture caches, write-backs medium A few days of work; needs some knowledge of the code needs: AMD Needs an AMD GPU (RADV or AMD's Windows driver) perf Speed: frames per second, CPU or GPU time, stutters

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants