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

Closed
adriaespii wants to merge 0 commits from upstream-pr/118 into main
adriaespii commented 2026-11-22 12:08:30 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by adriaespii on 2026-11-22T12:08:30Z.


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.
<!-- greyfinch-sync:omonkeyz/xbox-emulator:issue:6542182023 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/118). Originally posted by [adriaespii](https://github.com/adriaespii) on 2026-11-22T12:08:30Z. --- ### 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.
omonkeyz commented 2026-11-22 13:12:54 +00:00 (Migrated from github.com)

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


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.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7079648771 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/118#issuecomment-7079648771). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-22T13:12:54Z. --- 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 commented 2026-11-22 13:18:43 +00:00 (Migrated from github.com)

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


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.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7079717982 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/118#issuecomment-7079717982). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-22T13:18:43Z. --- 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

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!118
No description provided.