Skip to content

xmem: write DirectStorage page destinations one view per run - #61

Merged
Marvisak merged 1 commit into
Marvisak:mainfrom
TG0000:hle/physical-page-runs
Nov 24, 2026
Merged

Marvisak merged 1 commit into
Marvisak:mainfrom
TG0000:hle/physical-page-runs

Conversation

@TG0000

@TG0000 TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

The README lists loading as "limited by writes into title memory". One part of that is DirectStorage requests that write physical pages (PAGE_DESTINATION):

  • xmem::write_physical mapped, committed and unmapped a separate view for every 64 KiB page. Each unmap flushes every core's TLB, so with all the DirectStorage workers writing at once, this cost was paid serially, once per page.
  • dstorage::into_destination first decoded each such request into a zeroed Vec, then copied it into the pages.

Change

  • xmem::runs looks the pages up under one lock and groups them into runs of consecutive slices of one section. That is the usual case, since XMemAllocatePhysicalPages hands out one section per call. XMemMapPhysicalPages already groups pages the same way.
  • write_physical writes each run through a single view (an RAII View that maps, commits, and unmaps on drop).
  • New xmem::page_window: when a request's pages form one run, DirectStorage inflates or unpacks straight into that view, with no buffer and no copy. Pages from several allocations, or out of order, still go through the buffer and are written run by run. Errors are reported the same way as before (E_INVALIDARG for an unknown page).

Behaviour is unchanged apart from cost. As before, only the bytes the decoder produced are written, and the GPU side's guards are unaffected: these writes go through our own view, as they did before (the shortcut note in victory-gpu/src/watch.rs).

This fits with #53 (Wine: sections committed whole, so commit returns early): View::map goes through the same commit.

Numbers

Synthetic benchmark: a throwaway --release test, not included. It writes 16 MiB destinations, 256 consecutive pages, 20 times each.

before after
write_physical, 1 thread 1.92 GiB/s 2.19 GiB/s
write_physical, 8 threads 3.47 GiB/s 7.79 GiB/s
decode path: zeroed buffer + copy vs in place 0.96 GiB/s 2.13 GiB/s

I haven't measured the effect on in-game load time. I didn't run the title for this change.

Tests

New unit tests, all synthetic (our own data):

  • xmem::runs_group_consecutive_pages_of_one_allocation: run grouping, offsets that reach only some pages, out-of-order pages, pages from two allocations, too few pages, unknown pages, an overflowing offset
  • xmem::writes_cross_runs_and_allocations: a write crossing a run boundary between two sections, with the bytes around it left untouched
  • xmem::page_windows_cover_one_run_in_place
  • dstorage::zlib_into_pages_in_place_and_across_allocations: zlib into physical pages on both paths, checked through the title's view of them

Run:

  • cargo +stable-x86_64-pc-windows-msvc test --workspace: 228 passed, 0 failed, 3 ignored (224 before this change)
  • cargo clippy -p victory-hle --all-targets: no new warnings. The only warnings are the existing fetch_update deprecations on a newer stable toolchain.
  • cargo xtask policy: 148 files OK
  • Changed hunks formatted with rustfmt (main itself is not rustfmt-clean yet, see CI: check the formatting with cargo fmt on all PRs #17)

Machine: Windows 11 Pro, AMD Ryzen 9 5900X, AMD Radeon RX 6800. These tests are CPU-only, so no GPU tests were needed.

This PR was AI-assisted (Claude Code). I reviewed the change and the tests.

🤖 Generated with Claude Code

write_physical mapped, committed and unmapped a view for every 64 KiB
page it wrote, and DirectStorage decoded each page-destination request
into a zeroed buffer first, then copied it. Every unmap flushes every
core's TLB, so with all the workers loading at once these writes were a
serial cost per page.

Pages are now looked up once and grouped into runs of consecutive slices
of one section (how titles allocate them), and each run is written
through one view. When a request's pages form a single run, DirectStorage
inflates or unpacks straight into that view instead of a buffer; pages of
several allocations still go through the buffer, written run by run.

Synthetic release benchmark (16 MiB destinations, Ryzen 9 5900X):
8 threads 3.5 -> 7.8 GiB/s; one thread, buffered then copied 0.96 GiB/s
vs in place 2.1 GiB/s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DaviTostes

Copy link
Copy Markdown

Tested 4e94c2b stacked on #39 on Arch Linux x86_64, rustc 1.98.1: pass (no regression only).

  • cargo test --workspace: 155 passed, 0 failed, 3 ignored; merge with Build: make workspace tests run on Linux #39 clean.
  • Scope note: the diff is xmem.rs + dstorage.rs (+273/−41) and adds no test, so this run shows the portable HLE suite still passes rather than covering the one-view-per-run destination writes. A synthetic DirectStorage destination layout with several runs in one page would pin it.
  • No game data used.

AI-assisted (DeepSeek V4.1 Flash coding agent).

@Marvisak
Marvisak merged commit 97464ba into Marvisak:main Nov 24, 2026
mpDom pushed a commit to mpDom/VIctory that referenced this pull request Nov 24, 2026
Each title resource got its own committed host texture, so resources the
title places over the same memory (several objects at one heap offset or
address, or the same physical pages mapped at several addresses) did not
see each other's GPU writes, and a sampled-only alias of a render target
uploaded the title's (stale) memory instead of what was rendered.

Host textures are now keyed by memory identity (physical allocation and
offset via xmem's new physical_at resolver, else the address) and shape
(size, mips, samples, format family). An alias shares a GPU-written host
texture when it needs no use that one lacks; title-written ones are shared
by identical descriptions. Aliases of another shape still get their own
and are counted. A log line reports live host textures, their MB and the
aliases found. VICTORY_GPU_SHARE_ALIASES=0 turns sharing off to compare.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
suirentakamura referenced this pull request in suirentakamura/victory-suiren Nov 24, 2026
xmem: write DirectStorage page destinations one view per run
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.

3 participants