Skip to content

fix(gpu): sync shader-visible heaps holding the heap lock one page at a time - #199

Merged
pragyaangaur merged 2 commits into
pragyaangaur:mainfrom
MarshallStunt:fix/sync-heaps-page-lock
Nov 26, 2026
Merged

pragyaangaur merged 2 commits into
pragyaangaur:mainfrom
MarshallStunt:fix/sync-heaps-page-lock

Conversation

@MarshallStunt

Copy link
Copy Markdown
Contributor

What

descriptor::sync_heaps runs before every submission and held SHADER_HEAPS across every written page of every shader-visible heap. Title threads writing views (descriptor::mirror) wait on the same lock.

Now the written ranges are taken in one short hold. Each page is then made under its own hold, after checking under the lock that the heap is still listed. Heap::drop takes the same lock to leave the list, so a heap that is still listed is still alive. As before, a heap's ranges are taken only once it has its host heap.

Why

A diagnostics run in the world (Series S) showed sync_heaps checking about 350,000 slots a frame, over 29 submissions, much more than the 1,300-14,400 the doc comment expects. Title threads waited about 106 ms a frame on SHADER_HEAPS (MirrorWaitUs). With the lock held across the whole sync, a title thread that wrote a view stalled for the entire sync.

Measured

Apple M3 Pro, 36 GB, macOS 27, D3DMetal 4.0b3, --console series-s, VICTORY_PATCH_MASKED=all VICTORY_FAULT_TRACKING=1. Diagnostics build, driven into the world with VICTORY_KEYS. The world lines are those with more than 800 draws per frame, about 28 per run:

Run Median frame Average fps
Before, first run 1306 ms 0.77
Before, warm rerun 1275 ms 0.85
After 722 ms 1.34

The summed MirrorWaitUs barely moves (106 → 103 ms). The gain comes from title threads no longer stalling for a whole sync at once.

In a release build, preparing time per world frame fell from about 400 ms to about 100-300 ms in comparable scenes, and light-to-medium scenes went from about 1.6 to 1.8-3.5 fps. One release run stayed in the world for 1 h 35 min. It ended in the title's own crash handler after a 5.3 s frame, while I was running the GPU test suite on the same Mac at the same time, so I don't attribute that to this change.

Caveat: these runs were measured on a local branch that also carries #148, #152 and three upstream VIctory commits (#1865, #2054, #2147). The before/after rows differ only in this change.

Tests

  • cargo test --workspace: 472 passed, 0 failed
  • WINEDLLOVERRIDES="d3d12,d3d12core,dxgi=b" tools/macos/test-wine.sh --workspace --no-fail-fast: 874 passed, 0 failed, 12 ignored
  • cargo clippy -p victory-gpu --all-targets -- -D warnings (gnullvm target): clean
  • The existing records_copied_as_bytes_are_made_at_the_next_sync test covers the sync behaviour

No game data, offsets or file names here.

AI-assisted (Claude Code). The runs above were run on my Mac.

🤖 Generated with Claude Code

… a time

sync_heaps runs before every submission (29 a frame in the world) and held
SHADER_HEAPS across every written page of every heap, some 350,000 slots a
frame, while title threads writing views (mirror) wait on the same lock.
The written ranges are now taken in one short hold, then each page is made
under its own hold, after checking that the heap is still listed:
Heap::drop takes the lock to leave the list, so a listed heap is alive.
Ranges are still taken only once a heap has its host heap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pragyaangaur pragyaangaur added area:gpu Host GPU runtime: resources, copies, presenting area:shader Shader translator bug Something isn't working d3dmetal A difference in Apple's D3DMetal labels Nov 26, 2026
@pragyaangaur

Copy link
Copy Markdown
Owner

Reviewed and merging. The change takes the written ranges in one short hold and then makes each page under its own hold, checking that the heap is still listed first. That is sound, because Heap::drop takes the write lock to leave the list. This branch conflicted with main after the RwLock change in #169, so I merged main in and made the per-page holds read locks as well. Clippy on the Windows target and the native tests pass, and CI is green.

@pragyaangaur
pragyaangaur merged commit 9c552eb into pragyaangaur:main Nov 26, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:gpu Host GPU runtime: resources, copies, presenting area:shader Shader translator bug Something isn't working d3dmetal A difference in Apple's D3DMetal

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants