d3d12_xs: sync unified memory texture uploads on ExecuteCommandLists and fix Xbox One texture VA caching #121

Closed
github-bot wants to merge 1 commit from gh-pr/121/head into gh-pr/121/base
Owner

@⁠adriaespii on GitHub · 2026-11-22 13:00 UTC · original
adriaespii:contrib/terraria-texture-sync-upload → main · +140 −2 · 3 files · 1 commits · closed without merging

Summary

This PR addresses the first-frame initialization hang on Xbox One titles such as Terraria (\d3d12_x), where Unity writes textures into CPU-visible unified memory and waits indefinitely on async texture upload completion before presenting frames.

Context & Problem

In \docs/BLOCKERS.md:

Texture VA on by default, with CPU-visible memory per texture that is synced into the real texture before the GPU uses it.
Unity writes the texels there in the tiled layout that \xg_x\ reported for the texture (mode 13), so the sync must detile with that layout. It cannot copy the bytes as linear.
Today \ack_committed_texture\ memory never reaches the real texture.

Key issues identified:

  1. \expose_texture_va()\ was using \std::sync::OnceLock, caching \ alse\ if queried prior to \d3d12_x.dll\ being loaded into the process.
  2. In Xbox One titles, textures live in unified memory without title IO memory events, requiring \poll_mode()\ to be active.
  3. Placed and committed textures in unified memory were registered in \placed::track, but \upload::pump_if_needed\ was never triggered on \ExecuteCommandLists, leaving the textures empty/uninitialized on the PC GPU when the command lists executed.

Key Changes

  1. Dynamic Texture VA detection (\crates/compat/d3d12_xs/src/gpu_va.rs):
    • Replaced static \OnceLock\ with dynamic resolution of \d3d12_x.dll\ presence, ensuring \expose_texture_va()\ returns \ rue\ as soon as the Xbox One runtime is active.
    • Automatically activates \poll_mode()\ when \expose_texture_va()\ is enabled.
  2. Direct Command Queue Tracking & Syncing (\crates/compat/d3d12_xs/src/upload.rs):
    • Added
      egister_direct_queue, \is_direct_queue, and \direct_queue\ helpers.
    • Wired \upload::pump_if_needed\ inside \device_wrap.rs\ on \ID3D12CommandQueue::ExecuteCommandLists\ for Direct queues, ensuring pending and placed unified memory textures are adopted, detiled, and uploaded to the PC GPU before execution.
  3. Committed Resource Translation (\crates/compat/d3d12_xs/src/xone_tex.rs):
    • Initialized upload heaps in \GENERIC_READ\ (\
**@⁠adriaespii** on GitHub · 2026-11-22 13:00 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/121) `adriaespii:contrib/terraria-texture-sync-upload` → `main` · +140 −2 · 3 files · 1 commits · closed without merging ### Summary This PR addresses the first-frame initialization hang on Xbox One titles such as Terraria (\d3d12_x\), where Unity writes textures into CPU-visible unified memory and waits indefinitely on async texture upload completion before presenting frames. ### Context & Problem In \docs/BLOCKERS.md\: > Texture VA on by default, with CPU-visible memory per texture that is synced into the real texture before the GPU uses it. > Unity writes the texels there in the tiled layout that \xg_x\ reported for the texture (mode 13), so the sync must detile with that layout. It cannot copy the bytes as linear. > Today \ack_committed_texture\ memory never reaches the real texture. Key issues identified: 1. \expose_texture_va()\ was using \std::sync::OnceLock\, caching \ alse\ if queried prior to \d3d12_x.dll\ being loaded into the process. 2. In Xbox One titles, textures live in unified memory without title IO memory events, requiring \poll_mode()\ to be active. 3. Placed and committed textures in unified memory were registered in \placed::track\, but \upload::pump_if_needed\ was never triggered on \ExecuteCommandLists\, leaving the textures empty/uninitialized on the PC GPU when the command lists executed. ### Key Changes 1. **Dynamic Texture VA detection (\crates/compat/d3d12_xs/src/gpu_va.rs\)**: - Replaced static \OnceLock\ with dynamic resolution of \d3d12_x.dll\ presence, ensuring \expose_texture_va()\ returns \ rue\ as soon as the Xbox One runtime is active. - Automatically activates \poll_mode()\ when \expose_texture_va()\ is enabled. 2. **Direct Command Queue Tracking & Syncing (\crates/compat/d3d12_xs/src/upload.rs\)**: - Added \ egister_direct_queue\, \is_direct_queue\, and \direct_queue\ helpers. - Wired \upload::pump_if_needed\ inside \device_wrap.rs\ on \ID3D12CommandQueue::ExecuteCommandLists\ for Direct queues, ensuring pending and placed unified memory textures are adopted, detiled, and uploaded to the PC GPU before execution. 3. **Committed Resource Translation (\crates/compat/d3d12_xs/src/xone_tex.rs\)**: - Initialized upload heaps in \GENERIC_READ\ (\
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:10:44 +00:00
github-bot closed this pull request 2026-11-26 06:10:44 +00:00
Author
Owner

@⁠adriaespii on GitHub · 2026-11-22 13:52 UTC · original

Hi @⁠omonkeyz, thanks for the guidance!

I have rebased onto current \main\ (dropping the texture VA caching and direct queue registration parts that landed in \d693e69) and updated the \ExecuteCommandLists\ upload pump to match all requested specifications:

  1. Atomic pending check: \has_pending()\ is an \AtomicBool\ load (\HAS_PENDING) set when new placed resources or memory notes arrive and cleared once backlog/notes/unhashed textures are drained. When there is no backlog, the check in \ExecuteCommandLists\ costs nothing.
  2. Gated to Xbox One: In \exec_lists_x, the pump check is gated behind \if crate::xone_tex::is_xbox_one() && crate::upload::has_pending().
  3. Thread-safe pump under a lock: \pump()\ and \pump_if_needed()\ synchronize under \PUMP_LOCK. \pump_if_needed()\ uses double-checked locking so any thread waiting for the lock sees !has_pending()\ and returns immediately if the winning thread already drained and submitted the pending textures.
  4. No double submit test: Added
    acing_threads_do_not_double_submit\ in \direct_queue_and_pending_tests\ testing two concurrent racing threads calling \pump_if_needed\ on a direct queue, verifying that only a single pump occurs and \has_pending\ is safely cleared.
  5. Xbox One poll mode: Retained \SW.on() || crate::xone_tex::is_xbox_one().
  6. Verified with .github/scripts/pr_gate.py --base upstream/main --head HEAD\ (0 problems, comment-free).

Ready for your re-review whenever you have time. Thanks!

**@⁠adriaespii** on GitHub · 2026-11-22 13:52 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/121#issuecomment-7080127223) Hi @⁠omonkeyz, thanks for the guidance! I have rebased onto current \main\ (dropping the texture VA caching and direct queue registration parts that landed in \d693e69\) and updated the \ExecuteCommandLists\ upload pump to match all requested specifications: 1. **Atomic pending check**: \has_pending()\ is an \AtomicBool\ load (\HAS_PENDING\) set when new placed resources or memory notes arrive and cleared once backlog/notes/unhashed textures are drained. When there is no backlog, the check in \ExecuteCommandLists\ costs nothing. 2. **Gated to Xbox One**: In \exec_lists_x\, the pump check is gated behind \if crate::xone_tex::is_xbox_one() && crate::upload::has_pending()\. 3. **Thread-safe pump under a lock**: \pump()\ and \pump_if_needed()\ synchronize under \PUMP_LOCK\. \pump_if_needed()\ uses double-checked locking so any thread waiting for the lock sees \!has_pending()\ and returns immediately if the winning thread already drained and submitted the pending textures. 4. **No double submit test**: Added \ acing_threads_do_not_double_submit\ in \direct_queue_and_pending_tests\ testing two concurrent racing threads calling \pump_if_needed\ on a direct queue, verifying that only a single pump occurs and \has_pending\ is safely cleared. 5. **Xbox One poll mode**: Retained \SW.on() || crate::xone_tex::is_xbox_one()\. 6. Verified with \.github/scripts/pr_gate.py --base upstream/main --head HEAD\ (0 problems, comment-free). Ready for your re-review whenever you have time. Thanks!
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-22 17:31 UTC · original

Thanks for the rework. It now matches the requested design: an atomic pending check, gated to Xbox One, with the pump under a lock. It applies cleanly on current main, and cargo test -p d3d12_xs --lib passes. It's approved in principle. We're holding the merge only because it changes the ExecuteCommandLists path for Xbox One titles while we bisect a GPU hang in one of them, and the next runs have to stay comparable with the earlier ones. It'll be merged once that hang is closed or shown to be unrelated. No change is needed from you. When it lands, it will include one small test fix: racing_threads_do_not_double_submit will restore the global uploader and unregister its fake queue afterwards, so it doesn't leak state into other tests.

**@⁠omonkeyz** on GitHub · 2026-11-22 17:31 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/121#issuecomment-7082907927) Thanks for the rework. It now matches the requested design: an atomic pending check, gated to Xbox One, with the pump under a lock. It applies cleanly on current main, and `cargo test -p d3d12_xs --lib` passes. It's approved in principle. We're holding the merge only because it changes the ExecuteCommandLists path for Xbox One titles while we bisect a GPU hang in one of them, and the next runs have to stay comparable with the earlier ones. It'll be merged once that hang is closed or shown to be unrelated. No change is needed from you. When it lands, it will include one small test fix: `racing_threads_do_not_double_submit` will restore the global uploader and unregister its fake queue afterwards, so it doesn't leak state into other tests.
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-23 13:31 UTC · original

Closing as superseded: main now re-checks Xbox One textures dynamically in expose_texture_va, and syncs title writes at submit time with upload::submit_hook (write-watch, on by default). The upload.rs design this PR changes has been replaced. Thanks.

**@⁠omonkeyz** on GitHub · 2026-11-23 13:31 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/121#issuecomment-7102654917) Closing as superseded: main now re-checks Xbox One textures dynamically in expose_texture_va, and syncs title writes at submit time with upload::submit_hook (write-watch, on by default). The upload.rs design this PR changes has been replaced. Thanks.
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-22 13:23 UTC · original · requested changes

Thanks for resubmitting. The safe part of this commit (the texture VA / is_xbox_one caching fix and Direct queue registration) is already on main as d693e69 with your authorship, so please rebase onto current main and keep only the rest. The per-ExecuteCommandLists pump still needs the shape described on #118: an atomic pending check that costs nothing with no backlog, gated to Xbox One, pump() made safe from any thread under a lock (or ExecuteCommandLists only signalling the presenting/upload thread), and a test showing no double submit when two threads race. Forcing poll_mode on for Xbox One is fine together with that.

**@⁠omonkeyz** on GitHub · 2026-11-22 13:23 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/121#pullrequestreview-6012472345) · **requested changes** Thanks for resubmitting. The safe part of this commit (the texture VA / is_xbox_one caching fix and Direct queue registration) is already on main as d693e69 with your authorship, so please rebase onto current main and keep only the rest. The per-ExecuteCommandLists pump still needs the shape described on #118: an atomic pending check that costs nothing with no backlog, gated to Xbox One, pump() made safe from any thread under a lock (or ExecuteCommandLists only signalling the presenting/upload thread), and a test showing no double submit when two threads race. Forcing poll_mode on for Xbox One is fine together with that.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.