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

Closed
adriaespii wants to merge 1 commit from upstream-pr/121 into main
adriaespii commented 2026-11-22 13:00:22 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by adriaespii on 2026-11-22T13:00:22Z.


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\ (\
<!-- greyfinch-sync:omonkeyz/xbox-emulator:issue:6542685281 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/121). Originally posted by [adriaespii](https://github.com/adriaespii) on 2026-11-22T13:00:22Z. --- ### 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\ (\
omonkeyz commented 2026-11-22 13:23:31 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by omonkeyz on 2026-11-22T13:23:31Z.

Review: CHANGES_REQUESTED


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.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:review:6012472345 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/121#pullrequestreview-6012472345). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-22T13:23:31Z. **Review:** CHANGES_REQUESTED --- 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.
adriaespii commented 2026-11-22 13:52:42 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by adriaespii on 2026-11-22T13:52:42Z.


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!

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7080127223 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/121#issuecomment-7080127223). Originally posted by [adriaespii](https://github.com/adriaespii) on 2026-11-22T13:52:42Z. --- 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!
omonkeyz commented 2026-11-22 17:31:50 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by omonkeyz on 2026-11-22T17:31:50Z.


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.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7082907927 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/121#issuecomment-7082907927). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-22T17:31:50Z. --- 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 commented 2026-11-23 13:31:19 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by omonkeyz on 2026-11-23T13:31:19Z.


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.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7102654917 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/121#issuecomment-7102654917). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-23T13:31:19Z. --- 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.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
omonkeyz/xbox-emulator!121
No description provided.