Sparse tile sweep drains in-flight GPU work before unmapping and retires swept tiles behind a fence #194

Closed
github-bot wants to merge 1 commit from github-bot/gh-194 into main AGit
Owner

@⁠eIBart0 on GitHub · 2026-11-25 20:34 UTC · original
sweep-fix → main · +278 −54 · 2 files · 1 commits · closed without merging

Problem

With VICTORY_SWEEP_TILES=2, the host GPU is removed with DXGI_ERROR_DEVICE_HUNG (0x887A0006, "host GPU device removed after a submission of N lists"). It happens 64-90 s after the world starts drawing, when the first cinematic hands over to the cell.

On a Radeon RX 9070 XT (Windows 11 26220, Ryzen 7 5800X3D), standing in the cell:

Build VICTORY_SWEEP_TILES=2 Runs that hung
main and local integration branches yes 5 of 7
same builds no 0 of 4
this branch yes 0 of 3 (19, 27 and 27 sweeps returning up to 338 tiles each, 90 s standing in the cell; 5.4, 5.6 and 5.2 fps)

Cause

shadow::sweep runs from queue::show. Timeline::step drains in-flight work before Op::Work, but releases host.submitted before show() runs. That leaves three gaps:

  1. The unmap is not ordered against submissions still in flight. Another queue can submit lists and return while the GPU still runs them, since submission is asynchronous by default. sweep() then queues UpdateTileMappings(..., D3D12_TILE_RANGE_FLAG_NULL) without holding host.submitted or draining in_flight.
  2. Physical tiles are reused at once. Swept Tiles went straight back to TILES.free with no fence. The next new_tile() could map them elsewhere and write them from the CPU while the GPU was still using the old mapping.
  3. The tile can be remapped between removal and unmap. sweep() removed tiles from s.tiles, dropped the lock, and only then unmapped them. A concurrent Shadow::tile could map the same address in between. The unmap then landed after that map, so the GPU read zeros while s.tiles still listed the tile as mapped. Shadow::sparse_on_element had the same window around map_tiles.

Changes

  • host.rs: added Host::completed_fence() and Host::drain_and_signal(). drain_and_signal() takes submitted, drains in-flight work, runs the callback, and signals the fence when it queued work.
  • shadow.rs:
    • Unmap: sweep() still looks for candidates outside the locks, so a frame with nothing to sweep costs nothing extra. When it finds some, it does the removal, the unmap of the buffer and its aliases, and the cleanup of looks, copied and unwatched inside drain_and_signal().
    • Removal and unmap together: a new function, take_and_unmap, holds s.tiles across both steps. sparse_on_element now holds s.tiles across map_tiles.
    • Tile reuse: swept tiles retire through host::retire into a fence-gated free list (TileFreeList, RetiredTiles). A tile is reused only once completed_fence >= unmap_fence.
  • Lock order: host.submitted → s.tiles → s.kept/s.aliases → TILES. Shadow::tile, map_tiles, new_tile and sparse_on_element never take host.submitted, so the in_parallel workers inside a submission can't deadlock on it.

Tests

  • Three unit tests that need no GPU, in shadow::tests:
    • swept_tiles_wait_for_their_unmap_fence_before_reuse
    • reinserted_tile_is_not_unmapped_by_sweep
    • concurrent_reinsert_waits_for_sweep_unmap_before_mapping
  • On Windows: cargo test -p victory-gpu --lib shadow::tests passes (8 tests). cargo check -p victory, with and without diagnostics, adds no new warning.
  • The branch is rebased on 000ffdd.
**@⁠eIBart0** on GitHub · 2026-11-25 20:34 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/194) `sweep-fix` → `main` · +278 −54 · 2 files · 1 commits · closed without merging ### Problem With `VICTORY_SWEEP_TILES=2`, the host GPU is removed with `DXGI_ERROR_DEVICE_HUNG` (`0x887A0006`, "host GPU device removed after a submission of N lists"). It happens 64-90 s after the world starts drawing, when the first cinematic hands over to the cell. On a Radeon RX 9070 XT (Windows 11 26220, Ryzen 7 5800X3D), standing in the cell: | Build | `VICTORY_SWEEP_TILES=2` | Runs that hung | |---|---|---| | main and local integration branches | yes | 5 of 7 | | same builds | no | 0 of 4 | | this branch | yes | 0 of 3 (19, 27 and 27 sweeps returning up to 338 tiles each, 90 s standing in the cell; 5.4, 5.6 and 5.2 fps) | ### Cause `shadow::sweep` runs from `queue::show`. `Timeline::step` drains in-flight work before `Op::Work`, but releases `host.submitted` before `show()` runs. That leaves three gaps: 1. **The unmap is not ordered against submissions still in flight.** Another queue can submit lists and return while the GPU still runs them, since submission is asynchronous by default. `sweep()` then queues `UpdateTileMappings(..., D3D12_TILE_RANGE_FLAG_NULL)` without holding `host.submitted` or draining `in_flight`. 2. **Physical tiles are reused at once.** Swept `Tile`s went straight back to `TILES.free` with no fence. The next `new_tile()` could map them elsewhere and write them from the CPU while the GPU was still using the old mapping. 3. **The tile can be remapped between removal and unmap.** `sweep()` removed tiles from `s.tiles`, dropped the lock, and only then unmapped them. A concurrent `Shadow::tile` could map the same address in between. The unmap then landed after that map, so the GPU read zeros while `s.tiles` still listed the tile as mapped. `Shadow::sparse_on_element` had the same window around `map_tiles`. ### Changes - `host.rs`: added `Host::completed_fence()` and `Host::drain_and_signal()`. `drain_and_signal()` takes `submitted`, drains in-flight work, runs the callback, and signals the fence when it queued work. - `shadow.rs`: - **Unmap:** `sweep()` still looks for candidates outside the locks, so a frame with nothing to sweep costs nothing extra. When it finds some, it does the removal, the unmap of the buffer and its aliases, and the cleanup of `looks`, `copied` and `unwatched` inside `drain_and_signal()`. - **Removal and unmap together:** a new function, `take_and_unmap`, holds `s.tiles` across both steps. `sparse_on_element` now holds `s.tiles` across `map_tiles`. - **Tile reuse:** swept tiles retire through `host::retire` into a fence-gated free list (`TileFreeList`, `RetiredTiles`). A tile is reused only once `completed_fence >= unmap_fence`. - **Lock order:** `host.submitted` → `s.tiles` → `s.kept`/`s.aliases` → `TILES`. `Shadow::tile`, `map_tiles`, `new_tile` and `sparse_on_element` never take `host.submitted`, so the `in_parallel` workers inside a submission can't deadlock on it. ### Tests - Three unit tests that need no GPU, in `shadow::tests`: - `swept_tiles_wait_for_their_unmap_fence_before_reuse` - `reinserted_tile_is_not_unmapped_by_sweep` - `concurrent_reinsert_waits_for_sweep_unmap_before_mapping` - On Windows: `cargo test -p victory-gpu --lib shadow::tests` passes (8 tests). `cargo check -p victory`, with and without `diagnostics`, adds no new warning. - The branch is rebased on `000ffdd`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:37:42 +00:00
Author
Owner

@⁠MamaLeVrai on GitHub · 2026-11-26 00:18 UTC · original

Tested on an RX 6950 XT (RDNA2), Windows 11 build 26300, Rust 1.98.1, release lib tests with this PR (c1b2efd) merged on main 4f62c2a:

  • victory-gpu: 115 passed, 1 failed. On main: 112 passed, 1 failed. The 3 new shadow::tests pass. The one failure is list::tests::lists_are_cut_at_waits_that_follow_work, which fails in every release run on main (#210).
  • victory-shader and victory-hle: the same as main.

I did not run with VICTORY_SWEEP_TILES=2. Not run in the game.

harness machine: rx6950xt-w11 | AMD Radeon RX 6950 XT (RDNA2), driver 32.0.21045.29003 | 32 GB RAM | Windows 11 build 26300

AI-assisted (Claude Code); no game data.

**@⁠MamaLeVrai** on GitHub · 2026-11-26 00:18 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/194#issuecomment-7178615848) Tested on an RX 6950 XT (RDNA2), Windows 11 build 26300, Rust 1.98.1, release lib tests with this PR (`c1b2efd`) merged on `main` `4f62c2a`: - `victory-gpu`: 115 passed, 1 failed. On `main`: 112 passed, 1 failed. The 3 new `shadow::tests` pass. The one failure is `list::tests::lists_are_cut_at_waits_that_follow_work`, which fails in every release run on `main` (#210). - `victory-shader` and `victory-hle`: the same as `main`. I did not run with `VICTORY_SWEEP_TILES=2`. Not run in the game. harness machine: rx6950xt-w11 | AMD Radeon RX 6950 XT (RDNA2), driver 32.0.21045.29003 | 32 GB RAM | Windows 11 build 26300 AI-assisted (Claude Code); no game data.
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-26 19:45 UTC · original

Tested natively on Windows 11 26300, RX 7900 XT (RDNA3, driver 32.0.32029.2010). Head c1b2efd, base 000ffdd. I also merged it locally into current main 4f62c2a: the merge is clean.

What I ran:

  • cargo test -p victory-gpu --lib, interleaved: 26 runs of the PR, 25 of 000ffdd, 15 of 4f62c2a and 15 of 4f62c2a + this PR. The machine was busy with other jobs.
  • The three new shadow::tests passed in every run that completed.
  • The only other failures in the PR and merged runs were tests that also fail now and then on 000ffdd: frame::tests::waits_end_on_ticks_and_count_frames, the watch::tests group and, once, depth_bounds_control_a_title_draw.
  • One PR run stopped partway with no result line. It did not happen again in 25 more PR runs, so I can't tie it to this change.
  • cargo check -p victory on 4f62c2a + this PR, with and without diagnostics: same warning count as 4f62c2a.

Do the new tests fail without the fix? They don't compile on 000ffdd, so I put back the old behaviour inside the PR tree instead:

  • Releasing s.tiles before the unmap in take_and_unmap, as the old code did: concurrent_reinsert_waits_for_sweep_unmap_before_mapping fails 3 of 3 (order is map, then unmap).
  • Making TileFreeList::defer put tiles straight into free, ignoring the fence: swept_tiles_wait_for_their_unmap_fence_before_reuse fails 3 of 3.
  • reinserted_tile_is_not_unmapped_by_sweep passes with both changes put back. The old code already re-checked used and kept under the lock, so this test protects that re-check, not the new fix.

One small thing in sweep(): if queue.Signal fails, unmap_fence is None and old is dropped. Those tiles never go back to TILES, and SHADOWS is not decremented. Before this PR they always went back to the free list.

Not tested: the in-game hang with VICTORY_SWEEP_TILES=2. I did not run the game, so I can't confirm the fix on RDNA3.

**@⁠adrianmjim** on GitHub · 2026-11-26 19:45 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/194#issuecomment-7201934750) Tested natively on Windows 11 26300, RX 7900 XT (RDNA3, driver 32.0.32029.2010). Head c1b2efd, base 000ffdd. I also merged it locally into current main 4f62c2a: the merge is clean. What I ran: - `cargo test -p victory-gpu --lib`, interleaved: 26 runs of the PR, 25 of 000ffdd, 15 of 4f62c2a and 15 of 4f62c2a + this PR. The machine was busy with other jobs. - The three new `shadow::tests` passed in every run that completed. - The only other failures in the PR and merged runs were tests that also fail now and then on 000ffdd: `frame::tests::waits_end_on_ticks_and_count_frames`, the `watch::tests` group and, once, `depth_bounds_control_a_title_draw`. - One PR run stopped partway with no result line. It did not happen again in 25 more PR runs, so I can't tie it to this change. - `cargo check -p victory` on 4f62c2a + this PR, with and without `diagnostics`: same warning count as 4f62c2a. Do the new tests fail without the fix? They don't compile on 000ffdd, so I put back the old behaviour inside the PR tree instead: - Releasing `s.tiles` before the unmap in `take_and_unmap`, as the old code did: `concurrent_reinsert_waits_for_sweep_unmap_before_mapping` fails 3 of 3 (order is map, then unmap). - Making `TileFreeList::defer` put tiles straight into `free`, ignoring the fence: `swept_tiles_wait_for_their_unmap_fence_before_reuse` fails 3 of 3. - `reinserted_tile_is_not_unmapped_by_sweep` passes with both changes put back. The old code already re-checked `used` and `kept` under the lock, so this test protects that re-check, not the new fix. One small thing in `sweep()`: if `queue.Signal` fails, `unmap_fence` is `None` and `old` is dropped. Those tiles never go back to `TILES`, and `SHADOWS` is not decremented. Before this PR they always went back to the free list. Not tested: the in-game hang with `VICTORY_SWEEP_TILES=2`. I did not run the game, so I can't confirm the fix on RDNA3.
github-bot closed this pull request 2026-11-27 12:31:24 +00:00

Pull request closed

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