GPU: draws find the views their tables hold without the view registry's lock #3273

Open
KainanP wants to merge 2 commits from upstream-pr/3275 into main
KainanP commented 2026-11-26 15:26:40 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by KainanP on 2026-11-26T15:26:40Z.


Part of #2586 (the cost of list methods on the title's threads). Local change, same behaviour. Second of two lock fixes for draws recorded on several threads at once. The first is #3274, which removes the most contended lock. This one is smaller and pays off mostly together with #2821.

The problem

Every slot a draw binds looks up its view in the view registry descriptor::VIEWS: HostHeap::mirror → view_at for the slots a table names, and descriptor::view on the table paths. Each lookup takes the registry's lock. On main that's a mutex. With #2821 it becomes an RwLock read, but readers still write the lock word's shared count, so the five recording threads bounce that cache line between cores. With main + #1978 + #2821 + #3258, a per-call-site lock counter put it second, after TEXTURES_AT (#3274): six takes per draw, 157 ns on one thread and 439 ns each on five (ranking table in #3274).

The change

A registry entry keeps its view for as long as its generation is current. An entry gets another view only after forget has freed it under a new generation. So:

  • each entry's live generation (0 while it is free) is kept in chunks of 4,096 AtomicU32. The chunks are made as the list grows and never freed. They are written only under the registry's exclusive lock: when an entry gets a view in record, and when forget frees it;
  • each thread keeps the views it looked up: 1,024 slots of (entry, generation, view), indexed by entry;
  • a lookup whose entry is still live at the asked generation and in the thread's slots takes the thread's copy without the lock. Every other lookup goes through the registry as before. A view found under the lock is kept only if its entry is live at that generation, under the same lock. A freed entry still holds its last view under the generation the next view will get, so it is never kept.

Same answers as the locked lookup. A view released right after the check is the same race as a view released right after the locked lookup returns. Entries past 4,096 × 1,024 are never kept, so they are always looked up under the lock.

Tests

New: descriptor::tests::threads_that_looked_views_up_see_them_rewritten_and_released. A second thread looks views up (and keeps them) while the main thread:

  • rewrites a slot: the second thread must see the new view;
  • releases a resource: its view must be gone;
  • gives the freed entry to another view: the old record still finds nothing, and the new one finds the new view.

I checked that the test catches the bug it's meant for: without the set_live(i, 0) in forget, it fails at "released".

That test passes on main too, as oni-swr found. It guards behaviour, not the change. descriptor::tests::a_view_looked_up_before_does_not_wait_for_the_view_registry (commit 9e07df21) shows the change itself. A thread looks a view up once, then looks it up again while another thread holds VIEWS exclusively. With this PR the second lookup returns within 1 s. On main it blocks until the lock is dropped, so the test fails on main (RTX and WARP) and passes here.

RTX 4060 Laptop Intel UHD (a78b, local adapter-selection hook) WARP (--features diagnostics, VICTORY_D3D12_WARP=1)
main 63bcca4 228–229 pass, 2 fail (+1 timing flake) 228 pass, 3 fail 246 pass, 0 fail
this PR 230 pass, same 2 fail 230 pass, same 3 fail (with #3274) 248 pass, 0 fail (with #3274)

The failures are main's (see #3274). Clippy passes with -D warnings for the workspace, all targets, with and without --all-features. repository_passes_policy passes, and rustfmt is clean on the changed lines.

Measured

Same method as #3274: pipeline::list_bench's draw sequence on 1 and 5 threads, 7 runs per build interleaved round-robin, medians (ranges). RTX 4060 Laptop, i7-14650HX, release. Seven single-threaded CPU-bound processes I don't control ran throughout, which interleaving spreads evenly over the builds. ns per draw, per thread:

tree 1 thread 5 threads
main 3144 (3015–3322) 12119 (10965–14624)
main + this PR 3102 (2995–3325) 12020 (11476–13522)
main + #1978 + #2821 + #3258 1559 (1419–1893) 5100 (4188–5889)
the same + this PR 1524 (1459–1561) 4650 (3264–5077)
the same + #3274 1430 (1356–1569) 2895 (2692–3066)
the same + #3274 + this PR 1426 (1388–1578) 2824 (2755–2997)

Two more interleaved rounds gave, on five threads: with #3274 2893 → 2609 (5 runs) and 2803 → 2691 (7 runs); without it 4818 → 3784 (7 runs).

On main alone it changes nothing measurable: the other shared locks (TEXTURES_AT, main's mutexes) dominate. Together with #2821 it saves about 0.5–1 µs per draw on five threads, and on top of #2821 + #3274 about 0.1–0.3 µs (−2 to −10%).

Conflicts

The text conflicts with #2821, #2099, #3234 and #2848 (lines next to view_at/view, or the registry's lock calls). With #2821: take this PR's bodies of view_at and view, and in view_of change VIEWS.lock() to VIEWS.read(). Without that change the merge doesn't compile, so nothing breaks silently. That is the measured tree.

What this does not show

  • No in-game numbers: I didn't run this in a game. Please A/B it in a title that records on several threads, together with #2821 and #3274.
  • Memory: up to about 140 KB per thread that looks views up (1,024 slots), plus 16 KB per 4,096 registry entries.
  • What's left on five threads after both fixes, by the same counter: each texture's own host mutex plus its Arc count (six per draw), and shadow::BUFFERS (four per draw). Removing the mutex alone saved about 80 ns, so most of the remaining cost is the shared Arc counts. That one belongs with the host-copy lookup in #3258.

AI use: Claude Code (Claude Opus 5.5) profiled the locks, wrote the change and the test, and ran the benchmarks and test suites listed above.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:5512769271 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3275). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-26T15:26:40Z. --- Part of #2586 (the cost of list methods on the title's threads). Local change, same behaviour. Second of two lock fixes for draws recorded on several threads at once. The first is #3274, which removes the most contended lock. This one is smaller and pays off mostly together with #2821. ## The problem Every slot a draw binds looks up its view in the view registry `descriptor::VIEWS`: `HostHeap::mirror` → `view_at` for the slots a table names, and `descriptor::view` on the table paths. Each lookup takes the registry's lock. On main that's a mutex. With #2821 it becomes an `RwLock` read, but readers still write the lock word's shared count, so the five recording threads bounce that cache line between cores. With main + #1978 + #2821 + #3258, a per-call-site lock counter put it second, after `TEXTURES_AT` (#3274): six takes per draw, 157 ns on one thread and 439 ns each on five (ranking table in #3274). ## The change A registry entry keeps its view for as long as its generation is current. An entry gets another view only after `forget` has freed it under a new generation. So: - each entry's live generation (0 while it is free) is kept in chunks of 4,096 `AtomicU32`. The chunks are made as the list grows and never freed. They are written only under the registry's exclusive lock: when an entry gets a view in `record`, and when `forget` frees it; - each thread keeps the views it looked up: 1,024 slots of (entry, generation, view), indexed by entry; - a lookup whose entry is still live at the asked generation and in the thread's slots takes the thread's copy without the lock. Every other lookup goes through the registry as before. A view found under the lock is kept only if its entry is live at that generation, under the same lock. A freed entry still holds its last view under the generation the next view will get, so it is never kept. Same answers as the locked lookup. A view released right after the check is the same race as a view released right after the locked lookup returns. Entries past 4,096 × 1,024 are never kept, so they are always looked up under the lock. ## Tests New: `descriptor::tests::threads_that_looked_views_up_see_them_rewritten_and_released`. A second thread looks views up (and keeps them) while the main thread: - rewrites a slot: the second thread must see the new view; - releases a resource: its view must be gone; - gives the freed entry to another view: the old record still finds nothing, and the new one finds the new view. I checked that the test catches the bug it's meant for: without the `set_live(i, 0)` in `forget`, it fails at "released". That test passes on main too, as oni-swr found. It guards behaviour, not the change. `descriptor::tests::a_view_looked_up_before_does_not_wait_for_the_view_registry` (commit 9e07df21) shows the change itself. A thread looks a view up once, then looks it up again while another thread holds `VIEWS` exclusively. With this PR the second lookup returns within 1 s. On main it blocks until the lock is dropped, so the test **fails on main** (RTX and WARP) and passes here. | | RTX 4060 Laptop | Intel UHD (a78b, local adapter-selection hook) | WARP (`--features diagnostics`, `VICTORY_D3D12_WARP=1`) | |---|---|---|---| | main 63bcca4 | 228–229 pass, 2 fail (+1 timing flake) | 228 pass, 3 fail | 246 pass, 0 fail | | this PR | 230 pass, same 2 fail | 230 pass, same 3 fail (with #3274) | 248 pass, 0 fail (with #3274) | The failures are main's (see #3274). Clippy passes with `-D warnings` for the workspace, all targets, with and without `--all-features`. `repository_passes_policy` passes, and rustfmt is clean on the changed lines. ## Measured Same method as #3274: `pipeline::list_bench`'s draw sequence on 1 and 5 threads, 7 runs per build interleaved round-robin, medians (ranges). RTX 4060 Laptop, i7-14650HX, release. Seven single-threaded CPU-bound processes I don't control ran throughout, which interleaving spreads evenly over the builds. ns per draw, per thread: | tree | 1 thread | 5 threads | |---|---:|---:| | main | 3144 (3015–3322) | 12119 (10965–14624) | | main + this PR | 3102 (2995–3325) | 12020 (11476–13522) | | main + #1978 + #2821 + #3258 | 1559 (1419–1893) | 5100 (4188–5889) | | the same + this PR | 1524 (1459–1561) | **4650** (3264–5077) | | the same + #3274 | 1430 (1356–1569) | 2895 (2692–3066) | | the same + #3274 + this PR | 1426 (1388–1578) | **2824** (2755–2997) | Two more interleaved rounds gave, on five threads: with #3274 2893 → 2609 (5 runs) and 2803 → 2691 (7 runs); without it 4818 → 3784 (7 runs). On main alone it changes nothing measurable: the other shared locks (`TEXTURES_AT`, main's mutexes) dominate. Together with #2821 it saves about 0.5–1 µs per draw on five threads, and on top of #2821 + #3274 about 0.1–0.3 µs (−2 to −10%). ## Conflicts The text conflicts with #2821, #2099, #3234 and #2848 (lines next to `view_at`/`view`, or the registry's lock calls). With #2821: take this PR's bodies of `view_at` and `view`, and in `view_of` change `VIEWS.lock()` to `VIEWS.read()`. Without that change the merge doesn't compile, so nothing breaks silently. That is the measured tree. ## What this does not show - No in-game numbers: I didn't run this in a game. Please A/B it in a title that records on several threads, together with #2821 and #3274. - Memory: up to about 140 KB per thread that looks views up (1,024 slots), plus 16 KB per 4,096 registry entries. - What's left on five threads after both fixes, by the same counter: each texture's own `host` mutex plus its `Arc` count (six per draw), and `shadow::BUFFERS` (four per draw). Removing the mutex alone saved about 80 ns, so most of the remaining cost is the shared `Arc` counts. That one belongs with the host-copy lookup in #3258. _AI use: Claude Code (Claude Opus 5.5) profiled the locks, wrote the change and the test, and ran the benchmarks and test suites listed above._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Dukestar69 commented 2026-11-26 15:50:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T15:50:06Z.

Review: APPROVED


Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090):

  • Caching (entry, generation, View) in thread-local SEEN validated against the lock-free Acquire/Release generation table LIVE (updated under VIEWS in record and cleared to 0 in forget) cleanly avoids VIEWS mutex contention on hot draw paths while preserving invalidation on release/reuse.
  • cargo test -p victory-gpu --lib descriptor:: -- --test-threads=1 --skip dynamic_slots_are_held_until_released (RTX 5090): 22 passed, 0 failed (including threads_that_looked_views_up_see_them_rewritten_and_released).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6077043028 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3275#pullrequestreview-6077043028). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T15:50:06Z. **Review:** APPROVED --- Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090): - Caching `(entry, generation, View)` in thread-local `SEEN` validated against the lock-free `Acquire`/`Release` generation table `LIVE` (updated under `VIEWS` in `record` and cleared to `0` in `forget`) cleanly avoids `VIEWS` mutex contention on hot draw paths while preserving invalidation on release/reuse. - `cargo test -p victory-gpu --lib descriptor:: -- --test-threads=1 --skip dynamic_slots_are_held_until_released` (RTX 5090): 22 passed, 0 failed (including `threads_that_looked_views_up_see_them_rewritten_and_released`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
oni-swr commented 2026-11-26 16:05:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-26T15:44:43Z.


WARP verification report (test report only, no code changes)

  • PR head: b90ab5f9
  • main: 63bcca43 (control); WARP software adapter forced with VICTORY_WARP=1 VICTORY_D3D12_WARP=1, --features diagnostics, d3d10warp.dll 10.0.26100.9818; dxil.dll 1.8.2502.11 (Windows Kit 10.0.26100.0, via VICTORY_DXIL).
  • Each test run in its own process (--exact), -j 4, synthetic tests only, no game data.
  • New test descriptor::tests::threads_that_looked_views_up_see_them_rewritten_and_released: passes on the PR (1 passed).
  • Control on main: the test only uses existing APIs, so I ran it on main with only the test added. It also passes on main. It guards the behaviour kept while the lookup moves off the views lock (rewritten and released views seen from another thread), not a bug on main, so it does not distinguish the two.
  • victory-gpu lib suite: PR 247 passed, 0 failed, 1 ignored; main 246 passed, 0 failed, 1 ignored. No failures on either.
  • Not tested: physical GPU. Not measured: the contention the change removes.

AI use: Claude Code (Claude Sonnet 5.5)

<!-- greyfinch-sync:Marvisak/VIctory:comment:7196690945 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3275#issuecomment-7196690945). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-26T15:44:43Z. --- WARP verification report (test report only, no code changes) - PR head: `b90ab5f9` - main: `63bcca43` (control); WARP software adapter forced with `VICTORY_WARP=1 VICTORY_D3D12_WARP=1`, `--features diagnostics`, d3d10warp.dll 10.0.26100.9818; dxil.dll 1.8.2502.11 (Windows Kit 10.0.26100.0, via `VICTORY_DXIL`). - Each test run in its own process (`--exact`), `-j 4`, synthetic tests only, no game data. - New test `descriptor::tests::threads_that_looked_views_up_see_them_rewritten_and_released`: **passes on the PR** (1 passed). - Control on main: the test only uses existing APIs, so I ran it on main with only the test added. It **also passes on main**. It guards the behaviour kept while the lookup moves off the views lock (rewritten and released views seen from another thread), not a bug on main, so it does not distinguish the two. - `victory-gpu` lib suite: PR 247 passed, 0 failed, 1 ignored; main 246 passed, 0 failed, 1 ignored. No failures on either. - Not tested: physical GPU. Not measured: the contention the change removes. AI use: Claude Code (Claude Sonnet 5.5)
KainanP commented 2026-11-27 13:04:29 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by KainanP on 2026-11-27T13:04:29Z.


Thanks @oni-swr. You were right: threads_that_looked_views_up_see_them_rewritten_and_released guards the behaviour, but it passes on main too, so it doesn't show the change.

New commit 9e07df21 adds descriptor::tests::a_view_looked_up_before_does_not_wait_for_the_view_registry. A thread looks a view up once. Then the test thread holds VIEWS exclusively, the way a title thread writing a view does, and the same thread looks the view up again. With this PR it gets the view within 1 s from its per-thread cache. On main it blocks until the lock is dropped. While the lock is held nothing can change the entry's live generation, so the PR path can't fall back to the lock and the test isn't flaky.

  • Fails on main (63bcca43 + only this test): the lookup waited for the view registry's lock on RTX and WARP.
  • Passes on the PR: RTX and WARP (--features diagnostics, VICTORY_D3D12_WARP=1).
  • Full victory-gpu --lib: WARP 248 passed / 0 failed / 1 ignored. RTX has the same 2 failures as main, with heap_direct_texture_reads_mark_their_heap_index skipped. descriptor::host::tests::views_bound_while_another_slot_gets_its_texture_do_not_wait sometimes fails under the full parallel run: 1/10 here, and 2/10 on unpatched main, so it isn't from this PR.
  • Clippy -D warnings (default and --all-features), cargo xtask policy and rustfmt on the new lines are clean.

The earlier behaviour test stays.

AI use: Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7219024108 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3275#issuecomment-7219024108). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-27T13:04:29Z. --- Thanks @oni-swr. You were right: `threads_that_looked_views_up_see_them_rewritten_and_released` guards the behaviour, but it passes on main too, so it doesn't show the change. New commit 9e07df21 adds `descriptor::tests::a_view_looked_up_before_does_not_wait_for_the_view_registry`. A thread looks a view up once. Then the test thread holds `VIEWS` exclusively, the way a title thread writing a view does, and the same thread looks the view up again. With this PR it gets the view within 1 s from its per-thread cache. On main it blocks until the lock is dropped. While the lock is held nothing can change the entry's live generation, so the PR path can't fall back to the lock and the test isn't flaky. - **Fails on main** (`63bcca43` + only this test): `the lookup waited for the view registry's lock` on RTX and WARP. - **Passes on the PR**: RTX and WARP (`--features diagnostics`, `VICTORY_D3D12_WARP=1`). - Full `victory-gpu --lib`: WARP 248 passed / 0 failed / 1 ignored. RTX has the same 2 failures as main, with `heap_direct_texture_reads_mark_their_heap_index` skipped. `descriptor::host::tests::views_bound_while_another_slot_gets_its_texture_do_not_wait` sometimes fails under the full parallel run: 1/10 here, and 2/10 on unpatched main, so it isn't from this PR. - Clippy `-D warnings` (default and `--all-features`), `cargo xtask policy` and rustfmt on the new lines are clean. The earlier behaviour test stays. _AI use: Claude Code (Claude Opus 5.5)._
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3275:upstream-pr/3275'
git switch 'upstream-pr/3275'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3275'
git switch 'upstream-pr/3275'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3275'
git switch 'upstream-pr/3275'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3275'
git switch 'main'
git merge --squash 'upstream-pr/3275'
git switch 'main'
git merge --ff-only 'upstream-pr/3275'
git switch 'main'
git merge 'upstream-pr/3275'
git push origin 'main'
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
Marvisak/VIctory!3273
No description provided.