Repository navigation
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 into
Conversation
…thout asking the driver Every texture a draw reads through a table went through `readable`, which looked the title texture's host copy up three times (the upload check, the upload, the state move), each through the resource's lock, and asked the driver for the texture's description three times (`title_written`, then `legal_state` in `HostList::transition` and again in `list::ensure_state`) for flags that never change. - `HostTexture` keeps its resource flags, read once when it is made; `HostTexture::legal` and `HostTexture::title_written` answer from them what `host::legal_state` and `texture::title_written` ask GetDesc for. - `HostList::transition_held` and `list::ensure_held_state` move a host copy the caller holds; `transition` and `ensure_state` look it up and use them, so their answers are the same. - `texture::upload_copy` is `upload` for a copy the caller holds. - `readable` takes the copy once for the upload and the state move. Same moves, same uploads: two tests, one comparing the kept flags' answers with the driver's for five kinds of texture and every state bit, one checking the moves and the single upload a list records for reads of a title-written texture, and that a texture the host wrote is not uploaded. Measured with an #[ignore]d per-call benchmark (Marvisak#2586; RTX 4060 Laptop, release, median of 7 interleaved runs): an indexed draw reading six textures through a table 2713 -> 2047 ns; `list::ensure_state` with the state unchanged 70 -> 35 ns. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each texture a draw reads took the title resource's host lock and cloned the host copy's Arc (an atomic increment, then a decrement) on every call: a lock and a count every thread reading the same texture writes. A list now keeps the copies it read, by title texture, with the generation of the texture's host copy they were read at. Every write of a resource's host copy (made, given up) goes through Resource::set_host, which gives it a generation no write stored before (0 before the first, never kept). While the generation is the same the list lends its own copy: one Acquire load of a word only those writes change. A texture given up or replaced, or released and another made at its address, has a new generation, so a list never lends a copy that is not the texture's now. The list holds every copy it read until reset (after its last submission finished), as `states` already does, so nothing lent is freed while recorded commands name it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Dukestar69
approved these changes
Nov 26, 2026
Dukestar69
left a comment
There was a problem hiding this comment.
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
approved these changes
Nov 26, 2026
fuzeofficlal
left a comment
Contributor
There was a problem hiding this comment.
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::, andtexture::tests::passed on both a discrete NVIDIA RTX 5090 and WARP with--features diagnosticsandVICTORY_D3D12_DEBUG=1.- Audited every write to
Resource::hostincrates/victory-gpu/src/resource.rs: bothResource::host_at(resource.rs:1536) andgive_up(resource.rs:1787) go throughResource::set_host(resource.rs:1560), bumping the global monotonicHOST_GENERATIONS(starting at1) underself.host's lock withRelease/Acquireordering, while newly allocatedResourceinstances start at0(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_copyre-entrancy inside its own closure cleanly takes theWouldBlockfallback without deadlocking. - Real
Resourcerelease (via vtable slot0x10->resource_release) followed by confirmed same-addressResourcereallocation (aba_same_addr_reused = true) on the same openHostList: both draws read exact texels (0.1255and0.5020) with 0 Debug Layer errors, andlist.reset()drops the list'sby_texture/beforereferences cleanly. - 8 concurrent recording threads (8,000
readablecalls) racing a dedicated eviction thread (1,096concurrent evictions in 32 ms on RTX 5090;964in 25 ms on WARP): 0 panics, 0 deadlocks, and 0 Debug Layer errors.
- Nested
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 baseObjectrefcount without invokingresource_release(release_boxed::<Resource>), so theBox<Resource>is not actually freed in that test. Calling the resource's vtableReleaseslot (crate::object::slot::<extern "system" fn(This) -> u32>(obj, 0x10)(obj), as indraw.rs:4160) will genuinely exercise release and same-address reallocation.
LGTM.
|
Windows 11,
Synthetic only. AI-assisted. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Depends on #3258 (this branch contains its commit 723d6d5; review only the top commit 0d7876f). Part of the list-call scaling work in #2586.
What it costs today
Each texture a draw reads (
draw::readable, shader-resource views) callsresource::host_copy: the title resource'shostmutex, then anArcclone 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
Resourcegetshost_generation: AtomicU64. Every write ofResource::host(made inhost(), given up inresource::give_up) now goes throughResource::set_host, which stores a generation no write ever stored before (a global counter, under thehostlock). It is 0 before the first write, and nothing at generation 0 is ever kept.HostListkeepsread_copies: per title texture, the generation andArc<HostTexture>it read.HostList::with_host_copy(resource, f)compares the resource's generation now (oneAcquireload of a word only those writes change) with the kept one; if equal it lends the list's ownArctof, with no shared lock and no refcount change. Otherwise it reads copy and generation together under thehostlock (resource::host_copy_at) and keeps them.draw::readableuses it for the upload check and the state transition. Nothing else changes.Why it is safe (no use-after-free, no stale copy)
Arcinread_copies. A copy replaced by a newer generation moves toread_copies.before, so it is not dropped. Both are cleared only inHostList::reset, which runs after the list's last submission finished (the same pointstatesdrops the copies it holds). A lent&Arcis borrowed under the list's own mutex, so it cannot be dropped whilefruns. A nested call fromfusestry_lockand falls back to the shared path, so it never deadlocks.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 thehostlock together with the value. A list therefore lends a copy only while it is still the texture's copy. Before this change,host_copycould also return a copy thatevictgave up a moment later (documented as a shortcut), so the race window is no wider.host_copyit 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: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 atreset.readableby a recorded, closed list is released by the title (Release) beforeexecute. The list still runs and reads its texels (64/255).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: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::hostmutex 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 --libdynamic_slots_are_held_until_released,lists_share_a_table_and_copies_keep_their_views: heap NumDescriptors assertion).heap_direct_texture_reads_mark_their_heap_indexskipped (access violation on main too).--features diagnostics,VICTORY_D3D12_WARP=1, serial): 249 passed, 0 failed.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 ofResource::host; with this PR it should go throughset_host.AI use: written with Claude Code (Claude Opus 5.5); I reviewed the change, the safety argument and the measurements.
🤖 Generated with Claude Code