GPU: a list lends the host copy it read to its next reads of the texture (no shared lock or refcount per texture read; 5-thread draw −12 %, needs #3258) #3405

Open
KainanP wants to merge 2 commits from upstream-pr/3405 into main
KainanP commented 2026-11-26 22:32:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by KainanP on 2026-11-26T22:32:24Z.


Stacked on #3258 — merge #3258 first; this PR's own change is commit 0d7876f3 (to apply it as a patch on top of #3258, take only that commit: github.com/KainanP/VIctory@0d7876f3be.patch). Part of the list-call scaling work in #2586.

What it costs today

Each texture a draw reads (draw::readable, shader-resource views) calls resource::host_copy: the title resource's host mutex, then an Arc clone and drop of the host copy. When several title threads record lists reading the same textures (the world's render threads do), every one of those writes the same two cache lines. On the list bench (6 table textures per draw) with the recommended stack (main + #1978 + #2821 + #3258 + #3274 + #3275 + rebased #1356), a per-call-site lock profile at 5 threads puts that mutex first: 6 takes per draw, 65 % of them contended, ~740 ns per draw spent there; every other lock on the path is list-local and uncontended.

What changes

  • Resource gets host_generation: AtomicU64. Every write of Resource::host (made in host(), given up in resource::give_up) now goes through Resource::set_host, which stores a generation no write ever stored before (a global counter, under the host lock). It is 0 before the first write, and nothing at generation 0 is ever kept.
  • HostList keeps read_copies: per title texture, the generation and Arc<HostTexture> it read. HostList::with_host_copy(resource, f) compares the resource's generation now (one Acquire load of a word only those writes change) with the kept one; if equal it lends the list's own Arc to f, with no shared lock and no refcount change. Otherwise it reads copy and generation together under the host lock (resource::host_copy_at) and keeps them.
  • draw::readable uses it for the upload check and the state transition. Nothing else changes.

Why it is safe (no use-after-free, no stale copy)

  • Who keeps the object alive: the list. Every copy it lends is a strong Arc in read_copies. A copy replaced by a newer generation moves to read_copies.before, so it is not dropped. Both are cleared only in HostList::reset, which runs after the list's last submission finished (the same point states drops the copies it holds). A lent &Arc is borrowed under the list's own mutex, so it cannot be dropped while f runs. A nested call from f uses try_lock and falls back to the shared path, so it never deadlocks.
  • Never the wrong copy: a texture given up (evict) or made again, or a title texture released with another one made at the same object address, always has a generation different from any kept one. Generations come from one global counter and are written under the host lock together with the value. A list therefore lends a copy only while it is still the texture's copy. Before this change, host_copy could also return a copy that evict gave up a moment later (documented as a shortcut), so the race window is no wider.
  • Reading the generation dereferences the title resource just like the host_copy it replaces. A draw that names a released resource is undefined on both paths.

Test

draw::tests::a_list_lends_a_read_copy_only_while_it_is_the_textures:

  1. A list reads a texture twice and gets the same copy.
  2. The texture is given up, and a render target at the same memory replaces the memory's host texture. The next read in the same open list is the new copy, which equals made_copy (without the generation check the old one would be returned). The first copy stays alive while the list is open and is let go at reset.
  3. A texture read through readable by a recorded, closed list is released by the title (Release) before execute. The list still runs and reads its texels (64/255).
  4. Another title texture made afterwards (often at the released object's address) gets its own copy.

Numbers

RTX 4060, release, VICTORY_LIST_BENCH_SCALING=1 (fork branch perf/list-call-bench). Median of 7 interleaved runs, stack + rebased #1356, with and without this commit:

per draw stack + #1356 + this
1 thread 1759 ns 1788 ns (noise)
5 threads at once 3102 ns 2731 ns (−12 %)

Caveat: a game was running in the foreground during these runs (CPU 46–69 % busy). Both columns are inflated by the same load: on a quiet machine the same stack measured ~1.4 µs / ~2.5 µs earlier. The ranges were 2943–3350 vs 2519–3143. The lock profile is the cleaner evidence: the Resource::host mutex goes from 6 takes and ~740 ns per draw (65 % contended) to ~0 takes per draw. No lock left on the 5-thread path is contended.

Checks

  • cargo test -p victory-gpu --release --lib
    • RTX 4060: 232 passed. 2 failed, same as #3258's head on the same run (dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views: heap NumDescriptors assertion). heap_direct_texture_reads_mark_their_heap_index skipped (access violation on main too).
    • Intel UHD (a78b): 231 passed. 3 failed, same set as #3258's head.
    • WARP (--features diagnostics, VICTORY_D3D12_WARP=1, serial): 249 passed, 0 failed.
  • New test passes on all three.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings, with and without --all-features: clean. cargo test -p xtask (policy): ok. Changed lines rustfmt-clean.

Related: #3249 (targets: clones a texture only when a barrier is owed) touches HostList::transition, not this path. #3288 would add a write of Resource::host; with this PR it should go through set_host.

AI use: written with Claude Code (Claude Opus 5.5), including the change, the safety argument and the measurements.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:5518042809 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3405). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-26T22:32:24Z. --- Stacked on #3258 — merge #3258 first; this PR's own change is commit 0d7876f3 (to apply it as a patch on top of #3258, take only that commit: https://github.com/KainanP/VIctory/commit/0d7876f3be11f4b105e05195e8ff9111679a4e39.patch). Part of the list-call scaling work in #2586. ## What it costs today Each texture a draw reads (`draw::readable`, shader-resource views) calls `resource::host_copy`: the title resource's `host` mutex, then an `Arc` clone and drop of the host copy. When several title threads record lists reading the same textures (the world's render threads do), every one of those writes the same two cache lines. On the list bench (6 table textures per draw) with the recommended stack (main + #1978 + #2821 + #3258 + #3274 + #3275 + rebased #1356), a per-call-site lock profile at 5 threads puts that mutex first: **6 takes per draw, 65 % of them contended, ~740 ns per draw** spent there; every other lock on the path is list-local and uncontended. ## What changes - `Resource` gets `host_generation: AtomicU64`. Every write of `Resource::host` (made in `host()`, given up in `resource::give_up`) now goes through `Resource::set_host`, which stores a generation no write ever stored before (a global counter, under the `host` lock). It is 0 before the first write, and nothing at generation 0 is ever kept. - `HostList` keeps `read_copies`: per title texture, the generation and `Arc<HostTexture>` it read. `HostList::with_host_copy(resource, f)` compares the resource's generation now (one `Acquire` load of a word only those writes change) with the kept one; if equal it lends the list's own `Arc` to `f`, with no shared lock and no refcount change. Otherwise it reads copy and generation together under the `host` lock (`resource::host_copy_at`) and keeps them. - `draw::readable` uses it for the upload check and the state transition. Nothing else changes. ## Why it is safe (no use-after-free, no stale copy) - **Who keeps the object alive:** the list. Every copy it lends is a strong `Arc` in `read_copies`. A copy replaced by a newer generation moves to `read_copies.before`, so it is not dropped. Both are cleared only in `HostList::reset`, which runs after the list's last submission finished (the same point `states` drops the copies it holds). A lent `&Arc` is borrowed under the list's own mutex, so it cannot be dropped while `f` runs. A nested call from `f` uses `try_lock` and falls back to the shared path, so it never deadlocks. - **Never the wrong copy:** a texture given up (`evict`) or made again, or a title texture released with another one made at the same object address, always has a generation different from any kept one. Generations come from one global counter and are written under the `host` lock together with the value. A list therefore lends a copy only while it is still the texture's copy. Before this change, `host_copy` could also return a copy that `evict` gave up a moment later (documented as a shortcut), so the race window is no wider. - Reading the generation dereferences the title resource just like the `host_copy` it replaces. A draw that names a released resource is undefined on both paths. ## Test `draw::tests::a_list_lends_a_read_copy_only_while_it_is_the_textures`: 1. A list reads a texture twice and gets the same copy. 2. The texture is given up, and a render target at the same memory replaces the memory's host texture. The next read in the same open list is the **new** copy, which equals `made_copy` (without the generation check the old one would be returned). The first copy stays alive while the list is open and is let go at `reset`. 3. A texture read through `readable` by a recorded, closed list is released by the title (`Release`) before `execute`. The list still runs and reads its texels (64/255). 4. Another title texture made afterwards (often at the released object's address) gets its own copy. ## Numbers RTX 4060, release, `VICTORY_LIST_BENCH_SCALING=1` (fork branch perf/list-call-bench). Median of 7 interleaved runs, stack + rebased #1356, with and without this commit: | per draw | stack + #1356 | + this | |---|---|---| | 1 thread | 1759 ns | 1788 ns (noise) | | 5 threads at once | 3102 ns | **2731 ns (−12 %)** | Caveat: a game was running in the foreground during these runs (CPU 46–69 % busy). Both columns are inflated by the same load: on a quiet machine the same stack measured ~1.4 µs / ~2.5 µs earlier. The ranges were 2943–3350 vs 2519–3143. The lock profile is the cleaner evidence: the `Resource::host` mutex goes from 6 takes and ~740 ns per draw (65 % contended) to ~0 takes per draw. No lock left on the 5-thread path is contended. ## Checks - `cargo test -p victory-gpu --release --lib` - RTX 4060: 232 passed. 2 failed, same as #3258's head on the same run (`dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views`: heap NumDescriptors assertion). `heap_direct_texture_reads_mark_their_heap_index` skipped (access violation on main too). - Intel UHD (a78b): 231 passed. 3 failed, same set as #3258's head. - WARP (`--features diagnostics`, `VICTORY_D3D12_WARP=1`, serial): 249 passed, 0 failed. - New test passes on all three. - `cargo clippy -p victory-gpu --all-targets -- -D warnings`, with and without `--all-features`: clean. `cargo test -p xtask` (policy): ok. Changed lines rustfmt-clean. Related: #3249 (targets: clones a texture only when a barrier is owed) touches `HostList::transition`, not this path. #3288 would add a write of `Resource::host`; with this PR it should go through `set_host`. _AI use: written with Claude Code (Claude Opus 5.5), including the change, the safety argument and the measurements._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Dukestar69 commented 2026-11-26 22:48:03 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T22:48:03Z.

Review: APPROVED


LGTM — verified on RTX 5090 (Windows / D3D12) against origin/main (63bcca4).

  • Lending the host copy a command list already read to subsequent reads of the same texture (without a shared lock or refcount per read, and only while it remains the texture's current copy) is well-tested and passes cleanly on discrete NVIDIA hardware.
  • cargo test -p victory-gpu --lib draw:: -- --test-threads=1: 23 passed, 0 failed (a_list_lends_a_read_copy_only_while_it_is_the_textures, a_host_textures_kept_flags_give_what_its_description_does, a_texture_read_uploads_once_and_moves_as_a_read_by_resource).
  • cargo test -p victory-gpu --lib resource:: -- --test-threads=1: 18 passed, 0 failed.
  • 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:6081241178 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3405#pullrequestreview-6081241178). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T22:48:03Z. **Review:** APPROVED --- LGTM — verified on RTX 5090 (Windows / D3D12) against `origin/main` (`63bcca4`). - Lending the host copy a command list already read to subsequent reads of the same texture (without a shared lock or refcount per read, and only while it remains the texture's current copy) is well-tested and passes cleanly on discrete NVIDIA hardware. - `cargo test -p victory-gpu --lib draw:: -- --test-threads=1`: 23 passed, 0 failed (`a_list_lends_a_read_copy_only_while_it_is_the_textures`, `a_host_textures_kept_flags_give_what_its_description_does`, `a_texture_read_uploads_once_and_moves_as_a_read_by_resource`). - `cargo test -p victory-gpu --lib resource:: -- --test-threads=1`: 18 passed, 0 failed. - `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).
fuzeofficlal commented 2026-11-26 23:02:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T23:02:43Z.

Review: APPROVED


Reviewed 0d7876f3 (which cleanly stacks on #3258 / 723d6d5c and merges onto main 63bcca43 with zero conflicts).

Verification

  • cargo xtask policy: 191 files ok; cargo clippy --locked -p victory-gpu --all-targets -- -D warnings (both default and --features diagnostics): 0 warnings.
  • draw::tests::, resource::tests::, and texture::tests:: passed on both a discrete NVIDIA RTX 5090 and WARP with --features diagnostics and VICTORY_D3D12_DEBUG=1.
  • Audited every write to Resource::host in crates/victory-gpu/src/resource.rs: both Resource::host_at (resource.rs:1536) and give_up (resource.rs:1787) go through Resource::set_host (resource.rs:1560), bumping the global monotonic HOST_GENERATIONS (starting at 1) under self.host's lock with Release/Acquire ordering, while newly allocated Resource instances start at 0 (resource.rs:1837) so same-address reallocation cannot ABA-match a cached generation.
  • Live adversarial stress on RTX 5090 and WARP (VICTORY_D3D12_DEBUG=1):
    • Nested with_host_copy re-entrancy inside its own closure cleanly takes the WouldBlock fallback without deadlocking.
    • Real Resource release (via vtable slot 0x10 -> resource_release) followed by confirmed same-address Resource reallocation (aba_same_addr_reused = true) on the same open HostList: both draws read exact texels (0.1255 and 0.5020) with 0 Debug Layer errors, and list.reset() drops the list's by_texture/before references cleanly.
    • 8 concurrent recording threads (8,000 readable calls) racing a dedicated eviction thread (1,096 concurrent evictions in 32 ms on RTX 5090; 964 in 25 ms on WARP): 0 panics, 0 deadlocks, and 0 Debug Layer errors.

Minor non-blocking note

  • In the new test a_list_lends_a_read_copy_only_while_it_is_the_textures (crates/victory-gpu/src/draw.rs:3026-3133), crate::object::release(...) only decrements the base Object refcount without invoking resource_release (release_boxed::<Resource>), so the Box<Resource> is not actually freed in that test. Calling the resource's vtable Release slot (crate::object::slot::<extern "system" fn(This) -> u32>(obj, 0x10)(obj), as in draw.rs:4160) will genuinely exercise release and same-address reallocation.

LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:review:6081324552 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3405#pullrequestreview-6081324552). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T23:02:43Z. **Review:** APPROVED --- Reviewed `0d7876f3` (which cleanly stacks on `#3258` / `723d6d5c` and merges onto `main` `63bcca43` with zero conflicts). ### Verification - `cargo xtask policy`: 191 files ok; `cargo clippy --locked -p victory-gpu --all-targets -- -D warnings` (both default and `--features diagnostics`): 0 warnings. - `draw::tests::`, `resource::tests::`, and `texture::tests::` passed on both a discrete NVIDIA RTX 5090 and WARP with `--features diagnostics` and `VICTORY_D3D12_DEBUG=1`. - Audited every write to `Resource::host` in `crates/victory-gpu/src/resource.rs`: both `Resource::host_at` (`resource.rs:1536`) and `give_up` (`resource.rs:1787`) go through `Resource::set_host` (`resource.rs:1560`), bumping the global monotonic `HOST_GENERATIONS` (starting at `1`) under `self.host`'s lock with `Release`/`Acquire` ordering, while newly allocated `Resource` instances start at `0` (`resource.rs:1837`) so same-address reallocation cannot ABA-match a cached generation. - **Live adversarial stress on RTX 5090 and WARP (`VICTORY_D3D12_DEBUG=1`)**: - Nested `with_host_copy` re-entrancy inside its own closure cleanly takes the `WouldBlock` fallback without deadlocking. - Real `Resource` release (via vtable slot `0x10` -> `resource_release`) followed by confirmed same-address `Resource` reallocation (`aba_same_addr_reused = true`) on the same open `HostList`: both draws read exact texels (`0.1255` and `0.5020`) with 0 Debug Layer errors, and `list.reset()` drops the list's `by_texture`/`before` references cleanly. - 8 concurrent recording threads (8,000 `readable` calls) racing a dedicated eviction thread (`1,096` concurrent evictions in 32 ms on RTX 5090; `964` in 25 ms on WARP): 0 panics, 0 deadlocks, and 0 Debug Layer errors. ### Minor non-blocking note - In the new test `a_list_lends_a_read_copy_only_while_it_is_the_textures` (`crates/victory-gpu/src/draw.rs:3026-3133`), `crate::object::release(...)` only decrements the base `Object` refcount without invoking `resource_release` (`release_boxed::<Resource>`), so the `Box<Resource>` is not actually freed in that test. Calling the resource's vtable `Release` slot (`crate::object::slot::<extern "system" fn(This) -> u32>(obj, 0x10)(obj)`, as in `draw.rs:4160`) will genuinely exercise release and same-address reallocation. LGTM.
Jackster commented 2026-11-26 23:10:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Jackster on 2026-11-26T23:10:52Z.


Windows 11, 63bcca4 + this PR, Radeon RX 7900 XTX:

  • Applies to 63bcca4 alone (it carries the #3258 changes it says it needs): cargo test -p victory-gpu --lib gives 234 passed / 0 failed / 1 ignored (unpatched main: 231; +3), clippy clean.
  • Applying #3258 and then this one fails — the two patches overlap in draw.rs, host.rs, list.rs, resource.rs and texture.rs. So this is stacked on #3258, not additive to it; whoever lands second needs a rebase.
  • The new test is draw::tests::a_list_lends_a_read_copy_only_while_it_is_the_textures; the other two names it adds come from #3258.

Synthetic only. AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7205322495 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3405#issuecomment-7205322495). Originally posted by [Jackster](https://github.com/Jackster) on 2026-11-26T23:10:52Z. --- Windows 11, `63bcca4` + this PR, Radeon RX 7900 XTX: - Applies to `63bcca4` **alone** (it carries the #3258 changes it says it needs): `cargo test -p victory-gpu --lib` gives 234 passed / 0 failed / 1 ignored (unpatched main: 231; +3), clippy clean. - Applying #3258 and then this one **fails** — the two patches overlap in `draw.rs`, `host.rs`, `list.rs`, `resource.rs` and `texture.rs`. So this is stacked on #3258, not additive to it; whoever lands second needs a rebase. - The new test is `draw::tests::a_list_lends_a_read_copy_only_while_it_is_the_textures`; the other two names it adds come from #3258. Synthetic only. AI-assisted.
KainanP commented 2026-11-27 13:04:32 +00:00 (Migrated from github.com)

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


Thanks @Jackster, you're right. This branch is #3258's head (723d6d5c, unchanged and still on 63bcca4) plus one commit of its own, 0d7876f3. The PR's diff against main therefore includes #3258, and applying that full diff after #3258 overlaps everywhere #3258 changed. The branches themselves don't conflict: once #3258 is merged, this PR's diff shrinks to 0d7876f3.

I've made the stacking explicit in the first line of the description. If you test it as a patch, apply #3258 and then only the top commit (github.com/KainanP/VIctory@0d7876f3be.patch). I checked that it applies cleanly on 723d6d5c and gives exactly this branch's tree. As you said, the only test this PR adds is draw::tests::a_list_lends_a_read_copy_only_while_it_is_the_textures.

AI use: Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7219025283 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3405#issuecomment-7219025283). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-27T13:04:32Z. --- Thanks @Jackster, you're right. This branch is #3258's head (`723d6d5c`, unchanged and still on `63bcca4`) plus one commit of its own, `0d7876f3`. The PR's diff against main therefore includes #3258, and applying that full diff after #3258 overlaps everywhere #3258 changed. The branches themselves don't conflict: once #3258 is merged, this PR's diff shrinks to `0d7876f3`. I've made the stacking explicit in the first line of the description. If you test it as a patch, apply #3258 and then only the top commit (https://github.com/KainanP/VIctory/commit/0d7876f3be11f4b105e05195e8ff9111679a4e39.patch). I checked that it applies cleanly on `723d6d5c` and gives exactly this branch's tree. As you said, the only test this PR adds is `draw::tests::a_list_lends_a_read_copy_only_while_it_is_the_textures`. _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/3405:upstream-pr/3405'
git switch 'upstream-pr/3405'

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/3405'
git switch 'upstream-pr/3405'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3405'
git switch 'upstream-pr/3405'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3405'
git switch 'main'
git merge --squash 'upstream-pr/3405'
git switch 'main'
git merge --ff-only 'upstream-pr/3405'
git switch 'main'
git merge 'upstream-pr/3405'
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!3405
No description provided.