Skip to content

GPU: a texture read knows its memory has no writer without the texture map's lock - #3274

Open
KainanP wants to merge 2 commits into
Marvisak:mainfrom
KainanP:perf/alias-source-unlocked
Open

KainanP wants to merge 2 commits into
Marvisak:mainfrom
KainanP:perf/alias-source-unlocked

Conversation

@KainanP

@KainanP KainanP commented Nov 26, 2026 •

Copy link
Copy Markdown

Part of #2586 (the cost of list methods on the title's threads). Local change, same behaviour.

The problem

Every texture a draw reads through a descriptor table goes through draw::readable, which calls resource::alias_source to ask whether a title texture the GPU writes shares the texture's memory. Each call took the global TEXTURES_AT mutex. Title games record lists on about five threads at once, and all of them queue on that one lock.

I profiled the list bench's draw sequence (root CBV, four root constants, index buffer, an indexed draw whose table reads six textures) on five threads, with a temporary per-call-site lock counter. The tree was main + #1978 + #2821 + #3258, the combination recommended on #2586. Ranked by how much more each lock costs on five threads than on one:

lock (per draw) takes 1 thread 5 threads found held by another thread
TEXTURES_AT (alias_source) 6 101 ns 961 ns 36% of takes
descriptor::VIEWS, shared (view_at) 6 157 ns 439 ns 0 (reader-count traffic)
Resource::host (each texture's own mutex) 6 103 ns 287 ns 2%
shadow::BUFFERS, shared (shadow::find) 4 174 ns 336 ns 0
the list's own locks (deferred, states, state, hosts) 24 408 ns 411 ns 0

(The counter adds its own cost, so use these numbers for ranking only. HostHeap::made is not taken in this sequence because its slots are already made.)

The change

Most textures share their memory with no texture the GPU writes. Each title texture now keeps that answer (no_writer_seen) together with the change count of its memory's bucket. There are 1,024 counters, hashed by page number and bumped under the map's lock whenever a title texture is made at that memory or released from it. While the count hasn't changed, alias_source returns None without taking the lock. Readers only read the counters, and only make/release writes them.

Only the "no writer" answer is kept. A memory that has a writer is looked up as before, because whether two host copies differ can change without the map changing. The fallback path is main's code, now reusing the map lookup.

Tests

New: resource::tests::a_texture_read_finds_a_writer_made_at_its_memory_later. It reads a BC1 texture alone at its memory twice (so the answer is kept), then places an R32G32_UINT unordered-access texture at the same memory (same layout, its own host texture). The BC1 read must find the writer, and must find none again once the writer is released. I checked that the test catches the bug it's meant for: with the bump on creation removed, it fails at "the writer made since".

That test passes on main too, as oni-swr found. It guards behaviour, not the change. resource::tests::a_texture_read_with_no_writer_does_not_wait_for_the_texture_map (commit 6964f11) shows the change itself. A texture found once to have no writer is read again on another thread while the test thread holds TEXTURES_AT exclusively. With this PR the read returns within 500 ms. 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, through a 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 #3275) 248 pass, 0 fail (with #3275)

The RTX failures are main's (dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views, the NVIDIA heap cap). memory_tests::heap_direct_texture_reads_mark_their_heap_index is skipped on NVIDIA because it access-violates on main too. Intel also fails a_texture_written_in_one_format_reads_in_another_through_a_placed_view on main. One main run on the RTX also failed views_bound_while_another_slot_gets_its_texture_do_not_wait (timing); it passed on the other main run and on every branch run. 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

Method: pipeline::list_bench (#[ignore]d, fork branch perf/list-call-bench, not part of this PR). Its draw sequence is recorded through the list's method table on a host device, on 1 thread and on 5 threads at once. Each thread records its own list, and all of them read the same six textures. A run takes the best of 5 × 20,000 draws per thread. VICTORY_LIST_BENCH_SCALING=1 runs only these rows. Each build's test binary was run 7 times, interleaved round-robin with the other builds, and the table gives the median (with the range). Hardware: RTX 4060 Laptop GPU, Core i7-14650HX, Windows 11, release profile. The machine was not idle: seven single-threaded CPU-bound processes I don't control ran throughout. Interleaving gives every build the same load, but absolute numbers sit a little above earlier runs (main's one-thread draw was 2.78 µs on #2586, 3.14 µs here).

ns per draw, per thread:

tree 1 thread 5 threads 5 threads / 1
main 3144 (3015–3322) 12119 (10965–14624) 3.85
main + this PR 2325 (2269–2512) 7836 (6533–8490) 3.37
main + #1978 + #2821 + #3258 1559 (1419–1893) 5100 (4188–5889) 3.27
the same + this PR 1430 (1356–1569) 2895 (2692–3066) 2.02
the same + this PR + #3275 1426 (1388–1578) 2824 (2755–2997) 1.98

A repeat on the last three trees (7 more interleaved runs) gave 4818 → 2803 → 2691 ns on five threads.

On main the one-thread draw also gets 0.8 µs faster. Main's alias_source works out the texture's layout (a mip chain) on every call before it checks for writers, and #1978 reorders that. Here the check never gets that far for a texture with no writer.

Conflicts

The text conflicts with #1978, #3245, #3213 and #1969, all edits next to alias_source or the Resource fields. With #1978: keep #1978's let mut written = … chain and start it from others? instead of map.get(&(b.data() as u64))?. That's what the measured tree uses.

What this does not show

  • No in-game numbers: I didn't run this in a game. An A/B in a title that records on several threads (frame time, or the time each render thread spends recording) would show whether the lock mattered there as much as in the bench. Textures that do have a writer at their memory (render targets read back through placed views) still take the lock.
  • The bench's five threads all read the same six textures. A title's threads read different sets, which spreads the per-texture work but not this global lock.

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; I reviewed the results.

🤖 Generated with Claude Code

…e map's lock

Every texture a draw reads through a table asks `alias_source` whether a
title texture the GPU writes shares its memory, and each ask took the
global TEXTURES_AT mutex. Title threads recording lists at once queued on
it: in the list bench (Marvisak#2586) it was the most contended lock of the draw
sequence on five threads (a third of its takes found it held).

Most textures share their memory with no such writer. That answer is now
kept on the texture with the change count of its memory's bucket (1024
counters, bumped under the map's lock whenever a title texture is made or
released there), and taken without the lock while the count is unchanged.
Only that answer is kept: a memory that has a writer is looked up as
before (whether host copies differ can change without the map changing).

Test: a texture that found no writer finds one made at its memory later,
and none once it is released.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@oni-swr

oni-swr commented Nov 26, 2026

Copy link
Copy Markdown

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

  • PR head: e718f188
  • 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 resource::tests::a_texture_read_finds_a_writer_made_at_its_memory_later: 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 cache the PR adds (a writer made later is still found, and none after it is released), not a bug on main.
  • 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 lock contention the change removes.

AI use: Claude Code (Claude Sonnet 5.5)

@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.

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

  • Caching the no_writer_seen bucket generation in Resource against the Acquire/Release bucketed counter TEXTURES_AT_CHANGES (bumped under TEXTURES_AT on insert/remove) cleanly avoids TEXTURES_AT mutex contention on hot texture reads while staying exact when writers appear or are released.
  • cargo test -p victory-gpu --lib resource:: -- --test-threads=1 (RTX 5090): 19 passed, 0 failed (including a_texture_read_finds_a_writer_made_at_its_memory_later).
  • 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).

KainanP added a commit to KainanP/AnyXboxX that referenced this pull request Nov 26, 2026
…lock

Per texture a draw reads through a view, draw::readable took up to four
mutexes that every recording thread shares:

- PLACED_TEXTURES in resource::texture_at (descriptor::host::
  placed_texture), for each view the title places in its memory without
  a resource object (device 0x230, its streamed textures);
- TARGETS_AT in resource::alias_of, for each placed texture the title
  fills;
- HOST_WRITTEN in texture::upload_from, for each texture read;
- STAGING in texture::staged_from, on a read from another first level.

Placed views: each thread keeps the placed textures it found (256
slots, by address and description as asked). Nothing takes a texture
out of PLACED_TEXTURES, so what a thread found holds.

alias_of: a texture keeps its "no target at my memory" answer with the
change count of its memory's bucket (1,024 counters by 64 KiB block,
bumped under TARGETS_AT's lock when a target enters it). While the
count is unchanged the answer stands without the lock; a target leaving
the table cannot make "none" wrong. A texture with a target is looked
up as before.

Host-written and staged level: each texture keeps whether the host
wrote it (set with the set entry) and the first level its staging
buffer holds (written under STAGING's lock wherever the table gains or
loses the entry: staging, unstage, the idle sweep; forget runs as the
texture goes). upload_from reads them without the locks.

Same answers as the locked lookups; the tables stay for every other
reader. The tests hold each table's lock on one thread and read on
another: with the locked lookups the read waits.

Source: port of the idea of Marvisak/VIctory#3274 (KainanP), first
ported as LooperSalty#444 onto the previous base.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
KainanP added a commit to KainanP/AnyXboxX that referenced this pull request Nov 26, 2026
…lock

A draw's read of a texture took up to four process-wide mutexes that
every recording thread shares:

- PLACED_TEXTURES (resource::texture_at), for every view the title
  places in its memory without a resource object (its streamed
  textures): a BTreeMap lookup keyed by address and 14 words;
- TARGETS_AT (resource::alias_of), for every placed texture the title
  fills, to find a target at its memory;
- HOST_WRITTEN (texture::upload_from), for every texture the title
  fills;
- STAGING (texture::staged_from in upload_from), for every such read
  after the list's first.

Now:

- each thread keeps the placed textures it found (256 slots by address
  and description as asked). A placed texture stays in the table for
  the run; only tests take one out, and that bumps a count that drops
  every kept answer;
- a texture keeps alias_of's "no target at its memory" answer with the
  change count of its memory's bucket (1,024 counters by 64 KiB block,
  bumped under the table's lock when a target enters it). While the
  count is unchanged the answer stands without the lock; a target
  leaving the table cannot make "none" wrong. A texture with a target
  is looked up as before;
- a texture keeps whether the host wrote it (set with the set entry)
  and the first level its staging buffer holds (written under the
  staging table's lock wherever the table gains or loses the entry),
  read without the locks.

Same answers as the locked lookups; a change racing a read is the same
race as one right after the locked lookup returned. The tables stay
for every other reader.

Tests (new texture/read_marks_tests.rs): the staged level follows the
staging buffer (made, let go), a host-written texture is marked and not
uploaded, a placed texture finds a target made at its memory after a
"none" was kept (and none once it is gone), and placed textures found
again are the table's (a new one after a test took the old one out).
Each fails with its bump or reset removed.

List bench, RTX 4060 Laptop, release, median of 7 interleaved runs,
draw sequence on 5 threads, ns a draw: alone 8357 -> 6932 (resource
views), 9538 -> 7416 (placed views); with the one-lookup change too
(and the indirect PR, which no draw goes through) 6227 -> 4450 and
7109 -> 4884.

Source: Marvisak/VIctory#3274 (the change-count idea, applied to this
tree's tables)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
KainanP added a commit to KainanP/AnyXboxX that referenced this pull request Nov 26, 2026
…lock

Per texture a draw reads through a view, draw::readable took up to four
mutexes that every recording thread shares:

- PLACED_TEXTURES in resource::texture_at (descriptor::host::
  placed_texture), for each view the title places in its memory without
  a resource object (device 0x230, its streamed textures);
- TARGETS_AT in resource::alias_of, for each placed texture the title
  fills;
- HOST_WRITTEN in texture::upload_from, for each texture read;
- STAGING in texture::staged_from, on a read from another first level.

Placed views: each thread keeps the placed textures it found (256
slots, by address and description as asked). Nothing takes a texture
out of PLACED_TEXTURES, so what a thread found holds.

alias_of: a texture keeps its "no target at my memory" answer with the
change count of its memory's bucket (1,024 counters by 64 KiB block,
bumped under TARGETS_AT's lock when a target enters it). While the
count is unchanged the answer stands without the lock; a target leaving
the table cannot make "none" wrong. A texture with a target is looked
up as before.

Host-written and staged level: each texture keeps whether the host
wrote it (set with the set entry) and the first level its staging
buffer holds (written under STAGING's lock wherever the table gains or
loses the entry: staging, unstage, the idle sweep; forget runs as the
texture goes). upload_from reads them without the locks.

Same answers as the locked lookups; the tables stay for every other
reader. The tests hold each table's lock on one thread and read on
another: with the locked lookups the read waits.

Source: port of the idea of Marvisak/VIctory#3274 (KainanP), first
ported as LooperSalty#444 onto the Neptune base;
this is its port onto the Vice-Tay base (LooperSalty#454).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… texture map

The read that found no writer at its memory is held up by another
thread holding TEXTURES_AT on main (fails there) and not with the
per-bucket change counts (passes), as oni-swr found the earlier test
passes on both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@KainanP

KainanP commented Nov 27, 2026

Copy link
Copy Markdown
Author

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

New commit 6964f11 adds resource::tests::a_texture_read_with_no_writer_does_not_wait_for_the_texture_map. A placed texture's first alias_source finds no writer. Then the test thread holds TEXTURES_AT exclusively, the way a title thread making or releasing a texture does, and another thread reads the texture again. With this PR that read returns within 500 ms. On main it blocks until the lock is dropped. Another test bumping the same memory bucket in between could make the PR path take the lock, so the test tries 3 times.

  • Fails on main (63bcca43 + only this test): the read waited for the texture map'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 (dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views), with heap_direct_texture_reads_mark_their_heap_index skipped.
  • 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).

@Desamodeth

Copy link
Copy Markdown

I checked the open question about five recording threads reading different textures. With six private SRVs per thread (30 textures in one heap), #3274 finished the same batch sooner in 12/12 paired runs.

Recording workload main 63bcca43 #3274 6964f115
1 thread, 2,000 indexed draws 4.64 ms 4.07 ms
5 threads × 2,000, same six textures 12.57 ms 10.96 ms
5 threads × 2,000, six private textures each 12.58 ms 10.47 ms

These are per-build median wall times from the first thread starting to the last finishing. Calculated within each pair, the disjoint case saved a median 2.85 ms (23.2%), range 13.1–30.6%. Close/reset and descriptor setup were outside the timer. The fixture checks the host draw count and which six of the 30 resources each list actually tracked.

Windows, Ryzen 7 9800X3D, software WARP, optimized diag build. Six A/B and six B/A pairs, rotating case order, two warm-up windows and eight measured windows per case. This covers CPU recording only. Both new alias/invalidation tests also passed. Fixture based on your list benchmark · Full test diff against main. AI-assisted with Codex Astra 6.2 High.

Fayberr pushed a commit to Fayberr/Victory-United that referenced this pull request Nov 28, 2026
…lock

Per texture a draw reads through a view, draw::readable took up to four
mutexes that every recording thread shares:

- PLACED_TEXTURES in resource::texture_at (descriptor::host::
  placed_texture), for each view the title places in its memory without
  a resource object (device 0x230, its streamed textures);
- TARGETS_AT in resource::alias_of, for each placed texture the title
  fills;
- HOST_WRITTEN in texture::upload_from, for each texture read;
- STAGING in texture::staged_from, on a read from another first level.

Placed views: each thread keeps the placed textures it found (256
slots, by address and description as asked). Nothing takes a texture
out of PLACED_TEXTURES, so what a thread found holds.

alias_of: a texture keeps its "no target at my memory" answer with the
change count of its memory's bucket (1,024 counters by 64 KiB block,
bumped under TARGETS_AT's lock when a target enters it). While the
count is unchanged the answer stands without the lock; a target leaving
the table cannot make "none" wrong. A texture with a target is looked
up as before.

Host-written and staged level: each texture keeps whether the host
wrote it (set with the set entry) and the first level its staging
buffer holds (written under STAGING's lock wherever the table gains or
loses the entry: staging, unstage, the idle sweep; forget runs as the
texture goes). upload_from reads them without the locks.

Same answers as the locked lookups; the tables stay for every other
reader. The tests hold each table's lock on one thread and read on
another: with the locked lookups the read waits.

Source: port of the idea of Marvisak#3274 (KainanP), first
ported as LooperSalty/AnyXboxX#444 onto the Neptune base;
this is its port onto the Vice-Tay base (Marvisak#454).

Picked from upstream e6e7c9e98, applied onto our tree (compile and clippy checked, not run on a GPU).
iGlideForever pushed a commit to iGlideForever/VIctory-iGL-NVIDIA that referenced this pull request Nov 28, 2026
…lock

Per texture a draw reads through a view, draw::readable took up to four
mutexes that every recording thread shares:

- PLACED_TEXTURES in resource::texture_at (descriptor::host::
  placed_texture), for each view the title places in its memory without
  a resource object (device 0x230, its streamed textures);
- TARGETS_AT in resource::alias_of, for each placed texture the title
  fills;
- HOST_WRITTEN in texture::upload_from, for each texture read;
- STAGING in texture::staged_from, on a read from another first level.

Placed views: each thread keeps the placed textures it found (256
slots, by address and description as asked). Nothing takes a texture
out of PLACED_TEXTURES, so what a thread found holds.

alias_of: a texture keeps its "no target at my memory" answer with the
change count of its memory's bucket (1,024 counters by 64 KiB block,
bumped under TARGETS_AT's lock when a target enters it). While the
count is unchanged the answer stands without the lock; a target leaving
the table cannot make "none" wrong. A texture with a target is looked
up as before.

Host-written and staged level: each texture keeps whether the host
wrote it (set with the set entry) and the first level its staging
buffer holds (written under STAGING's lock wherever the table gains or
loses the entry: staging, unstage, the idle sweep; forget runs as the
texture goes). upload_from reads them without the locks.

Same answers as the locked lookups; the tables stay for every other
reader. The tests hold each table's lock on one thread and read on
another: with the locked lookups the read waits.

Source: port of the idea of Marvisak/VIctory#3274 (KainanP), first
ported as LooperSalty/AnyXboxX#444 onto the Neptune base;
this is its port onto the Vice-Tay base (trustno1-coder#454).

Picked from upstream e6e7c9e98, applied onto our tree (compile and clippy checked, not run on a GPU).

(ported from commit 42d61974606d6e356e5dcd754d610f6ff10ae3fc of Fayberr/Victory-United integ/c25-rndne)

[iGL: ported by hand: the commit's tree is about 3,400 commits from ours, so
both files conflicted. Same changes: the three marks in Hosted (beside
slot_evicted/slot_returned), TARGET_CHANGES bumped where TARGETS_AT gains a
texture (its only insert here), the kept "no target" answer in alias_with,
the per-thread placed-texture finds around texture_at (PLACED_TEXTURES has no
removal here either), and staged_first kept with STAGING in the same four
places it changes here (the idle sweep, unstage, the insert in staging;
forget as the texture goes). read_marks_tests.rs as it was. gpu tests: 474
passed. Run 114332 (26 min, with the memory stack off): no stale texture
seen; at equal draws 5-18 % faster than run 222820 together with trustno1-coder#1179,
trustno1-coder#1095 and main 63b902f.]

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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