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

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

@⁠adriaespii on GitHub · 2026-11-22 12:08 UTC · original
adriaespii:contrib/terraria-texture-sync-upload → main · +141 −7 · 6 files · 104 commits · closed without merging

Summary

Resolves the first-frame initialization hang in titles like Terraria (Xbox One \d3d12_x) where Unity waits indefinitely for async texture upload completion (\WaitOnAddress\ loop until \ exture+0xe9\ is set) before any frame is presented (\PresentX).

Root Cause & Changes

  1. Xbox One Texture VA Detection (\crates/compat/d3d12_xs/src/gpu_va.rs, \xone_tex.rs):
    • \expose_texture_va()\ previously cached the result of \crate::xone_tex::is_xbox_one()\ in a \OnceLock. If queried early during initialization before \d3d12_x.dll\ was loaded into the process, it permanently cached \ alse. As a result, subsequent \get_gpu_va_x\ calls returned 0 for committed textures unless manually overridden with \TOMBOX_TEXTURE_VA=1.
    • Changed \expose_texture_va()\ to only cache the env var override and evaluate \is_xbox_one()\ dynamically until detected, caching \ rue\ atomically once active.
  2. ExecuteCommandLists Texture Upload Pumping (\crates/compat/d3d12_xs/src/present.rs, \upload.rs):
    • \upload::pump\ was previously only invoked inside \PresentX. When games perform initialization texture loads (e.g. \ContentManager::LoadTexture2D\ -> \Texture2D::get_UnityTexture\ in Unity) and wait on texture uploads before presenting the first frame, \PresentX\ is never called, leaving \upload.rs's backlog completely unserviced.
    • Added \crate::upload::pump_if_needed(queue)\ called in \exec_lists_x\ before submitting command lists. If new placed textures or memory modification notes are pending, the uploader adopts them, detiles texel data, and executes the upload on a Direct command queue before the title's command lists execute.
  3. Queue Type Tracking (\crates/compat/d3d12_xs/src/device_wrap.rs, \upload.rs):
    • Track Direct queues (\ ype == 0) created in \CreateCommandQueueX\ and on \PresentX\ via
      egister_direct_queue, ensuring \upload::pump\ commands are always submitted to a valid Direct command queue even if \exec_lists_x\ is triggered on a non-direct (e.g. Copy/Compute) queue.
  4. Xbox One Polling Mode (\crates/compat/d3d12_xs/src/upload.rs):
    • Automatically activate \poll_mode\ for Xbox One titles (\is_xbox_one()) where titles write directly into unified memory backings without emulated IO note events.
  5. Placed Resource Pending State (\crates/compat/d3d12_xs/src/placed.rs):
    • Expose \has_new() -> bool\ to check if \NEW\ has unconsumed placed/committed resources without draining them.

Verification

  • Added synthetic unit tests in \crates/compat/d3d12_xs/src/placed.rs\ and \upload.rs:
    • \placed::tests::placed_new_tracking: verifies \has_new()\ and \drain_new()\ transitions.
    • \upload::direct_queue_and_pending_tests::direct_queue_registration_and_detection: verifies Direct queue registration and tracking.
    • \upload::direct_queue_and_pending_tests::pending_work_detects_notes_and_placed: verifies pending work detection.
    • \upload::direct_queue_and_pending_tests::pump_if_needed_on_null_queue_is_safe: verifies null-safety.
  • Run tests:
    • \cargo test -p d3d12_xs --lib -- --skip console_fixed_tests: 455 passed, 0 failed.
**@⁠adriaespii** on GitHub · 2026-11-22 12:08 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/118) `adriaespii:contrib/terraria-texture-sync-upload` → `main` · +141 −7 · 6 files · 104 commits · closed without merging ### Summary Resolves the first-frame initialization hang in titles like Terraria (Xbox One \d3d12_x\) where Unity waits indefinitely for async texture upload completion (\WaitOnAddress\ loop until \ exture+0xe9\ is set) before any frame is presented (\PresentX\). ### Root Cause & Changes 1. **Xbox One Texture VA Detection (\crates/compat/d3d12_xs/src/gpu_va.rs\, \xone_tex.rs\)**: - \expose_texture_va()\ previously cached the result of \crate::xone_tex::is_xbox_one()\ in a \OnceLock<bool>\. If queried early during initialization before \d3d12_x.dll\ was loaded into the process, it permanently cached \ alse\. As a result, subsequent \get_gpu_va_x\ calls returned 0 for committed textures unless manually overridden with \TOMBOX_TEXTURE_VA=1\. - Changed \expose_texture_va()\ to only cache the env var override and evaluate \is_xbox_one()\ dynamically until detected, caching \ rue\ atomically once active. 2. **ExecuteCommandLists Texture Upload Pumping (\crates/compat/d3d12_xs/src/present.rs\, \upload.rs\)**: - \upload::pump\ was previously only invoked inside \PresentX\. When games perform initialization texture loads (e.g. \ContentManager::LoadTexture2D\ -> \Texture2D::get_UnityTexture\ in Unity) and wait on texture uploads before presenting the first frame, \PresentX\ is never called, leaving \upload.rs\'s backlog completely unserviced. - Added \crate::upload::pump_if_needed(queue)\ called in \exec_lists_x\ before submitting command lists. If new placed textures or memory modification notes are pending, the uploader adopts them, detiles texel data, and executes the upload on a Direct command queue before the title's command lists execute. 3. **Queue Type Tracking (\crates/compat/d3d12_xs/src/device_wrap.rs\, \upload.rs\)**: - Track Direct queues (\ ype == 0\) created in \CreateCommandQueueX\ and on \PresentX\ via \ egister_direct_queue\, ensuring \upload::pump\ commands are always submitted to a valid Direct command queue even if \exec_lists_x\ is triggered on a non-direct (e.g. Copy/Compute) queue. 4. **Xbox One Polling Mode (\crates/compat/d3d12_xs/src/upload.rs\)**: - Automatically activate \poll_mode\ for Xbox One titles (\is_xbox_one()\) where titles write directly into unified memory backings without emulated IO note events. 5. **Placed Resource Pending State (\crates/compat/d3d12_xs/src/placed.rs\)**: - Expose \has_new() -> bool\ to check if \NEW\ has unconsumed placed/committed resources without draining them. ### Verification - Added synthetic unit tests in \crates/compat/d3d12_xs/src/placed.rs\ and \upload.rs\: - \placed::tests::placed_new_tracking\: verifies \has_new()\ and \drain_new()\ transitions. - \upload::direct_queue_and_pending_tests::direct_queue_registration_and_detection\: verifies Direct queue registration and tracking. - \upload::direct_queue_and_pending_tests::pending_work_detects_notes_and_placed\: verifies pending work detection. - \upload::direct_queue_and_pending_tests::pump_if_needed_on_null_queue_is_safe\: verifies null-safety. - Run tests: - \cargo test -p d3d12_xs --lib -- --skip console_fixed_tests\: 455 passed, 0 failed.
Fixes the first-frame initialization hang in titles like Terraria (Xbox One d3d12_x) where Unity waits on async texture upload completion before presenting any frames.

- Fix expose_texture_va() so it does not permanently lock in false via OnceLock when queried before d3d12_x.dll is loaded into the process.
- Automatically enable poll_mode() for Xbox One titles where textures live in unified memory without title IO memory events.
- Track Direct command queues via register_direct_queue / is_direct_queue / direct_queue.
- Wire upload::pump_if_needed on ExecuteCommandLists so pending and placed textures are adopted, detiled, and uploaded to the PC GPU before command lists execute.
- Add unit tests in placed.rs and upload.rs for new placed tracking and direct queue detection.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:10:38 +00:00
github-bot closed this pull request 2026-11-26 06:10:38 +00:00
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-22 13:12 UTC · original

Main's history was rewritten, which auto-closed this PR (sorry for the noise). A maintainer reviewed it. The safe part (texture VA / is_xbox_one no longer caching false before d3d12_x.dll loads, plus Direct queue registration) will be applied with your authorship. The per-ExecuteCommandLists pump needs changes. pump() is presenting-thread-only, and it adds a cost to every ExecuteCommandLists call. Acceptable shape: an atomic pending check that costs nothing with no backlog, gated to Xbox One, and either pump() made safe from any thread under a lock or ExecuteCommandLists only signalling the presenting/upload thread. Add a test showing no double submit when two threads race. Forcing poll_mode on for Xbox One is fine together with that. To resubmit: git fetch this repo, branch from the current main, cherry-pick only your own commits (the old branch is based on the previous history and cannot be merged), and open a new PR.

**@⁠omonkeyz** on GitHub · 2026-11-22 13:12 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/118#issuecomment-7079648771) Main's history was rewritten, which auto-closed this PR (sorry for the noise). A maintainer reviewed it. The safe part (texture VA / is_xbox_one no longer caching false before d3d12_x.dll loads, plus Direct queue registration) will be applied with your authorship. The per-ExecuteCommandLists pump needs changes. pump() is presenting-thread-only, and it adds a cost to every ExecuteCommandLists call. Acceptable shape: an atomic pending check that costs nothing with no backlog, gated to Xbox One, and either pump() made safe from any thread under a lock or ExecuteCommandLists only signalling the presenting/upload thread. Add a test showing no double submit when two threads race. Forcing poll_mode on for Xbox One is fine together with that. To resubmit: `git fetch` this repo, branch from the current `main`, cherry-pick only your own commits (the old branch is based on the previous history and cannot be merged), and open a new PR.
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-22 13:18 UTC · original

Update: the safe part is applied in d693e69 (merged in 99f48e3) with your authorship kept, thanks! The ExecuteCommandLists pump and forced poll mode are still open as described above. Please resubmit those as a new PR based on the current main.

**@⁠omonkeyz** on GitHub · 2026-11-22 13:18 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/118#issuecomment-7079717982) Update: the safe part is applied in d693e69 (merged in 99f48e3) with your authorship kept, thanks! The ExecuteCommandLists pump and forced poll mode are still open as described above. Please resubmit those as a new PR based on the current main.

Pull request closed

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