Skip to content

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
Marvisak:mainfrom
KainanP:perf/read-copy-lent
Open

KainanP wants to merge 2 commits into
Marvisak:mainfrom
KainanP:perf/read-copy-lent

Conversation

@KainanP

@KainanP KainanP commented Nov 26, 2026

Copy link
Copy Markdown

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) 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

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); I reviewed the change, the safety argument and the measurements.

🤖 Generated with Claude Code

KainanP and others added 2 commits November 26, 2026 11:41
…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 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown

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

Synthetic only. AI-assisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants