D3D12.X: serialize tile unmapping with texture uploads #799

Open
github-bot wants to merge 1 commit from github-bot/gh-799 into main AGit
Owner

@⁠wayward-tail on GitHub · 2026-11-24 05:36 UTC · original
wayward-tail:pr/upload-unmap-sync → main · +87 −6 · 2 files · 1 commits · draft

Superseded placed textures can be unmapped on the mapping queue while an internal texture upload is still using them. Serialize those unmaps with REFRESH, acquired after releasing TEXTURES, so active uploads finish first. Related: #594 orders against title queues; this change addresses the separate internal upload queue.

What is in it

  • title_textures.rs: synchronize both superseded-unmap call sites and add a GPU regression test that holds REFRESH while a texture is retired.
  • README note describing the behavior.

Tested

  • Commit c9633c64d11761564b92357e4c06f6cf4d39c686; Windows 11 Home build 26200, AMD Ryzen 7 8845HS, NVIDIA RTX 4060 Laptop GPU (driver 32.0.15.9282), Rust 1.100.0 MSVC, DXC 1.9.2609.5. Tests debug; title smoke release with symbols. XCL_ADAPTER=rtx 4060.
  • cargo build --workspace and cargo build --release -p xcl: succeeded.
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 701 passed, 1 failed, 41 ignored. The two wave64 skips below are included in Cargo's passed count.
  • Baseline 64b3f35: 700 passed, 1 failed, 41 ignored. gpu_clamp_and_omod_per_instruction fails at v_pk_add_i16 clamp on unmodified main on this GPU.
  • Branch failures: gpu::recomp::tests::gpu_clamp_and_omod_per_instruction.
  • GPU skips: SKIPPED gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask: no wave64; SKIPPED gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware: no wave64.

Regression run
Main:

xcl-smoke v1
commit: 64b3f35599bb7102f55d640e1a791e7d6dd3b175
gpu: NVIDIA GeForce RTX 4060 Laptop GPU
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 989 (PresentX 990)
presents per 2 s (last 3): 50 49 53
device removals: 0
crashes: 0
peak commit: 15068 MB

This branch:

xcl-smoke v1
commit: c9633c64d11761564b92357e4c06f6cf4d39c686
gpu: NVIDIA GeForce RTX 4060 Laptop GPU
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 801 (PresentX 801)
presents per 2 s (last 3): 12 60 61
device removals: 0
crashes: 0
peak commit: 10590 MB

Not tested

  • AMD / Intel GPU behavior, wave64, gameplay and sustained streaming. The smoke runs exercise startup without menu input.
  • Tests requiring XCL_SAMPLE_* game-data variables: unset, so those bodies were not exercised.
  • Not merge-ready while the full suite has failures; retained as a draft with the upstream baseline failure disclosed.

AI use

  • OpenAI Codex (GPT-6) reconciled the existing local fix with upstream, adjusted the regression test where needed, ran validation, and prepared this description. The existing local commit's original model provenance is unknown.
**@⁠wayward-tail** on GitHub · 2026-11-24 05:36 UTC · [original](https://github.com/xcl-dev/xcl/pull/799) `wayward-tail:pr/upload-unmap-sync` → `main` · +87 −6 · 2 files · 1 commits · draft Superseded placed textures can be unmapped on the mapping queue while an internal texture upload is still using them. Serialize those unmaps with REFRESH, acquired after releasing TEXTURES, so active uploads finish first. Related: #594 orders against title queues; this change addresses the separate internal upload queue. **What is in it** - title_textures.rs: synchronize both superseded-unmap call sites and add a GPU regression test that holds REFRESH while a texture is retired. - README note describing the behavior. **Tested** - Commit `c9633c64d11761564b92357e4c06f6cf4d39c686`; Windows 11 Home build 26200, AMD Ryzen 7 8845HS, NVIDIA RTX 4060 Laptop GPU (driver 32.0.15.9282), Rust 1.100.0 MSVC, DXC 1.9.2609.5. Tests debug; title smoke release with symbols. `XCL_ADAPTER=rtx 4060`. - `cargo build --workspace` and `cargo build --release -p xcl`: succeeded. - `cargo test --workspace --no-fail-fast -- --test-threads=4`: 701 passed, 1 failed, 41 ignored. The two wave64 skips below are included in Cargo's passed count. - Baseline `64b3f35`: 700 passed, 1 failed, 41 ignored. `gpu_clamp_and_omod_per_instruction` fails at `v_pk_add_i16 clamp` on unmodified main on this GPU. - Branch failures: gpu::recomp::tests::gpu_clamp_and_omod_per_instruction. - GPU skips: SKIPPED gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask: no wave64; SKIPPED gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware: no wave64. **Regression run** Main: ``` xcl-smoke v1 commit: 64b3f35599bb7102f55d640e1a791e7d6dd3b175 gpu: NVIDIA GeForce RTX 4060 Laptop GPU title: 3c290c16 seconds: 60 exit: 0x0 frames shown: 989 (PresentX 990) presents per 2 s (last 3): 50 49 53 device removals: 0 crashes: 0 peak commit: 15068 MB ``` This branch: ``` xcl-smoke v1 commit: c9633c64d11761564b92357e4c06f6cf4d39c686 gpu: NVIDIA GeForce RTX 4060 Laptop GPU title: 3c290c16 seconds: 60 exit: 0x0 frames shown: 801 (PresentX 801) presents per 2 s (last 3): 12 60 61 device removals: 0 crashes: 0 peak commit: 10590 MB ``` **Not tested** - AMD / Intel GPU behavior, wave64, gameplay and sustained streaming. The smoke runs exercise startup without menu input. - Tests requiring `XCL_SAMPLE_*` game-data variables: unset, so those bodies were not exercised. - Not merge-ready while the full suite has failures; retained as a draft with the upstream baseline failure disclosed. **AI use** - OpenAI Codex (GPT-6) reconciled the existing local fix with upstream, adjusted the regression test where needed, ran validation, and prepared this description. The existing local commit's original model provenance is unknown.
When a new placement supersedes an older placed texture, register() and retire_overlapping() previously called vram::unmap_superseded() without synchronizing with refresh(). If refresh() was concurrently executing upload_batch() on upload_queue for that texture, Pages.queue could issue UpdateTileMappings(NULL) while CopyTextureRegion was still in flight on upload_queue.

Acquire REFRESH (strictly after dropping TEXTURES, matching refresh()'s lock order) before calling vram::unmap_superseded() so any active upload batch completes and waits on its fence before the superseded tiles are unmapped.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:33:03 +00:00
Author
Owner

@⁠futurepro62 on GitHub · 2026-11-24 07:06 UTC · original

Full-suite run, since this PR has no test report yet. Tested exact head c9633c6 (on its base 64b3f35).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4: 702 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: superseded_unmap_waits_for_in_flight_refresh.

Not tested

  • AMD, a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
**@⁠futurepro62** on GitHub · 2026-11-24 07:06 UTC · [original](https://github.com/xcl-dev/xcl/pull/799#issuecomment-7124616004) Full-suite run, since this PR has no test report yet. Tested exact head `c9633c6` (on its base `64b3f35`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed. - `cargo build --workspace`, then `XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4`: **702 passed, 0 failed, 41 ignored**. SKIPPED (no wave64): `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`. - The new tests ran and passed: `superseded_unmap_waits_for_in_flight_refresh`. **Not tested** - AMD, a release build, a game (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
Author
Owner

@⁠iggyvilla on GitHub · 2026-11-24 07:34 UTC · original

Code read of head c9633c6 (static read only; nothing built or run). The locking itself holds up: refresh keeps REFRESH across upload_batch, which waits on its own fence (gpu/texture.rs), and TEXTURES is dropped before REFRESH is taken (title_textures.rs:399 unmap_superseded_all), so I see no lock inversion with the REFRESH → PAGES order in refresh_once. Two things to consider:

  1. The creating thread now waits for a whole refresh. register and retire_overlapping run on the title's CreatePlacedResourceX path (device_x.rs:892, :911). unmap_superseded_all makes that thread wait for the dirty-page scan, the hashes and upload_batch's fence wait (up to 10 s). The unmap has no deadline of its own (the tiles only go back to the pool), so it could instead be queued and drained at the end of refresh_once while REFRESH is held, which keeps the ordering and does not stall resource creation. Is the stall measured or ruled out on the retail title?

  2. Other unmaps are not covered. #640 and #782 add evict_lru, which calls self.unmap(res, total) under PAGES and not under REFRESH (#782 vram.rs:477, #640 vram.rs:471). An upload into a texture being evicted can still be in flight on upload_queue when its tiles are NULLed, which is the race this PR closes for superseded textures. Taking REFRESH inside take_tile would deadlock (refresh_once holds REFRESH and then takes PAGES in vram::before_execute), so the queue-and-drain shape from point 1 would also be the way to cover eviction.

Minor: superseded_unmap_waits_for_in_flight_refresh decides that the spawned thread is blocked by sleeping 30 ms; if the thread is not scheduled in that time the first assert passes without testing the lock. A channel signalled just before retire_overlapping and a try_recv timeout would make it deterministic.

AI use: Claude Code, Claude Sonnet 5.5, read the diff and the code of #640 / #782 at the heads named above and drafted this comment; I checked each line reference.

**@⁠iggyvilla** on GitHub · 2026-11-24 07:34 UTC · [original](https://github.com/xcl-dev/xcl/pull/799#issuecomment-7125139883) Code read of head `c9633c6` (static read only; nothing built or run). The locking itself holds up: `refresh` keeps `REFRESH` across `upload_batch`, which waits on its own fence (`gpu/texture.rs`), and `TEXTURES` is dropped before `REFRESH` is taken (`title_textures.rs:399` `unmap_superseded_all`), so I see no lock inversion with the `REFRESH` → `PAGES` order in `refresh_once`. Two things to consider: 1. **The creating thread now waits for a whole refresh.** `register` and `retire_overlapping` run on the title's `CreatePlacedResourceX` path (`device_x.rs:892`, `:911`). `unmap_superseded_all` makes that thread wait for the dirty-page scan, the hashes and `upload_batch`'s fence wait (up to 10 s). The unmap has no deadline of its own (the tiles only go back to the pool), so it could instead be queued and drained at the end of `refresh_once` while `REFRESH` is held, which keeps the ordering and does not stall resource creation. Is the stall measured or ruled out on the retail title? 2. **Other unmaps are not covered.** #640 and #782 add `evict_lru`, which calls `self.unmap(res, total)` under `PAGES` and not under `REFRESH` (#782 `vram.rs:477`, #640 `vram.rs:471`). An upload into a texture being evicted can still be in flight on `upload_queue` when its tiles are NULLed, which is the race this PR closes for superseded textures. Taking `REFRESH` inside `take_tile` would deadlock (`refresh_once` holds `REFRESH` and then takes `PAGES` in `vram::before_execute`), so the queue-and-drain shape from point 1 would also be the way to cover eviction. Minor: `superseded_unmap_waits_for_in_flight_refresh` decides that the spawned thread is blocked by sleeping 30 ms; if the thread is not scheduled in that time the first assert passes without testing the lock. A channel signalled just before `retire_overlapping` and a `try_recv` timeout would make it deterministic. AI use: Claude Code, Claude Sonnet 5.5, read the diff and the code of #640 / #782 at the heads named above and drafted this comment; I checked each line reference.
Author
Owner

@⁠Anon64 on GitHub · 2026-11-24 23:58 UTC · original

A gap next to this change, reported as the second point of #1897 and not covered by #594 or here. I checked it by reading main fc14fab; I have not reproduced it in a run.

track() / retire_overlapping() decide under TEXTURES to unmap A, then drop TEXTURES and call vram::unmap_superseded(A). forget(B) likewise decides under TEXTURES to bring A back, drops it, and calls vram::remap_if_unmapped(A). The two vram calls only see t.mapped under PAGES, so this order is possible:

  1. Thread 1: track(B) moves A to shadowed, returns to_unmap = [A], drops TEXTURES.
  2. Thread 2: forget(B) restores A, returns to_remap = [A], and calls remap_if_unmapped(A). t.mapped is still true, so it does nothing.
  3. Thread 1: unmap_superseded(A) sees t.mapped == true and unmaps A, which is active again. Nothing maps it back.

With this PR, the unmap sites take REFRESH after dropping TEXTURES. If forget() also held REFRESH from before its TEXTURES section until after remap_if_unmapped, and the unmap sites took it before their TEXTURES section rather than after, each decision and its vram call would happen as one step, which would close this order too. That keeps the lock order REFRESH → TEXTURES / PAGES. I'm posting it here rather than as a separate PR because it touches the same call sites.

Written with Claude Code (Claude Opus 5.5).

**@⁠Anon64** on GitHub · 2026-11-24 23:58 UTC · [original](https://github.com/xcl-dev/xcl/pull/799#issuecomment-7148001869) A gap next to this change, reported as the second point of #1897 and not covered by #594 or here. I checked it by reading `main` `fc14fab`; I have not reproduced it in a run. `track()` / `retire_overlapping()` decide under `TEXTURES` to unmap A, then drop `TEXTURES` and call `vram::unmap_superseded(A)`. `forget(B)` likewise decides under `TEXTURES` to bring A back, drops it, and calls `vram::remap_if_unmapped(A)`. The two vram calls only see `t.mapped` under `PAGES`, so this order is possible: 1. Thread 1: `track(B)` moves A to shadowed, returns `to_unmap = [A]`, drops `TEXTURES`. 2. Thread 2: `forget(B)` restores A, returns `to_remap = [A]`, and calls `remap_if_unmapped(A)`. `t.mapped` is still `true`, so it does nothing. 3. Thread 1: `unmap_superseded(A)` sees `t.mapped == true` and unmaps A, which is active again. Nothing maps it back. With this PR, the unmap sites take `REFRESH` after dropping `TEXTURES`. If `forget()` also held `REFRESH` from before its `TEXTURES` section until after `remap_if_unmapped`, and the unmap sites took it before their `TEXTURES` section rather than after, each decision and its vram call would happen as one step, which would close this order too. That keeps the lock order `REFRESH` → `TEXTURES` / `PAGES`. I'm posting it here rather than as a separate PR because it touches the same call sites. Written with Claude Code (Claude Opus 5.5).
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-25 10:58 UTC · original

Verification on RTX 3060 Ti (Windows 11): merge conflict with current main — needs a rebase before it can be tested.

**@⁠DavidM1911** on GitHub · 2026-11-25 10:58 UTC · [original](https://github.com/xcl-dev/xcl/pull/799#issuecomment-7160423957) Verification on RTX 3060 Ti (Windows 11): **merge conflict with current `main`** — needs a rebase before it can be tested.
This pull request has changes conflicting with the target branch.
  • README.md
  • crates/xcl/src/hle/d3d12x/title_textures.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/799/head:github-bot/gh-799'
git switch 'github-bot/gh-799'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-799'
git switch 'github-bot/gh-799'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-799'
git switch 'github-bot/gh-799'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-799'
git switch 'main'
git merge --squash 'github-bot/gh-799'
git switch 'main'
git merge --ff-only 'github-bot/gh-799'
git switch 'main'
git merge 'github-bot/gh-799'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.