GPU: a draw's texture read takes its host copy once, and its flags without asking the driver #3258

Open
github-bot wants to merge 1 commit from github-bot/gh-3258 into main AGit
Owner

@⁠KainanP on GitHub · 2026-11-26 14:48 UTC · original
KainanP:perf/texture-read-once → main · +261 −27 · 5 files · 1 commits · open

Part of #2586 (the per-call cost of list methods on the title's threads). Local optimization, no architecture change, same behaviour.

The problem

Every texture a draw or dispatch reads through a descriptor table goes through draw::readable. For one texture it:

  • looked up the title texture's host copy three times: resource::host_resource (for the upload check), texture::upload → is_host_written → resource::host_copy, and list::ensure_state → HostList::transition → resource::host_copy. Each lookup takes the resource's host mutex and clones an Arc (host_resource also AddRefs/Releases the COM texture). Title threads drawing with the same textures all take the same per-resource mutexes.
  • asked the driver for the texture's description three times: texture::title_written (GetDesc), then host::legal_state in HostList::transition and host::legal_state again in list::ensure_state. The flags never change after the texture is made.

The per-call benchmark below has an indexed draw that reads six textures. On main, list::ensure_state with the state unchanged costs 70 ns and the whole texture path about 1.9 µs of a 2.7 µs draw.

The change

  • HostTexture keeps its resource flags (flags), read once in HostTexture::new, which already called GetDesc. HostTexture::legal(state) and HostTexture::title_written() give the same answers as host::legal_state and texture::title_written, without calling GetDesc.
  • HostList::transition_held(copy, wanted) and list::ensure_held_state(list, copy, state) move a host copy the caller already holds. transition and ensure_state look the copy up once and call them, so every other caller gets the same result.
  • texture::upload_copy(list, resource, copy) does what upload does, for a copy the caller holds. The diagnostics-only "keeps what the host copied" note moved into a shared helper, unchanged.
  • readable looks up the copy once and uses it for both the upload and the state move. A texture evicted between the old lookups could give the upload and the barrier two different host textures. With one lookup that can't happen.

Tests

Two new tests in draw::tests:

  • a_host_textures_kept_flags_give_what_its_description_does: for five kinds of host texture (plain, render target, unordered access, depth, depth + deny shader resource), the kept flags match GetDesc. legal() matches legal_state for every state bit and the common combinations, and title_written() matches texture::title_written.
  • a_texture_read_uploads_once_and_moves_as_a_read_by_resource: reading a title-written texture, moving it to copy source by resource, then reading it twice more gives exactly the barriers a read by resource gives: none on first use, then SR→copy source→SR. The list records its upload once. A texture the host wrote is not uploaded.

Runs:

RTX 4060 Laptop (driver 32.0.16.1047) Intel UHD (31.0.101.5445, through a local adapter-selection hook) WARP (--features diagnostics, VICTORY_D3D12_WARP=1, serial)
main 63bcca4 229 pass, 2 fail 228 pass, 3 fail n/a
this PR 231 pass, same 2 fail 230 pass, same 3 fail 248 pass, 0 fail

The RTX failures are descriptor::host::tests::dynamic_slots_are_held_until_released and memory::tests::lists_share_a_table_and_copies_keep_their_views (the NVIDIA heap cap, #1835). memory_tests::heap_direct_texture_reads_mark_their_heap_index was 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. cargo clippy -p victory-gpu --all-targets passes with -D warnings, with and without --all-features. rustfmt is clean on the changed lines.

Measured

Method: an #[ignore]d test, pipeline::list_bench (branch perf/list-call-bench on my fork, one commit on top of main, not part of this PR). It calls each list method through the list's method table on a host device. The title buffers are in write-watched memory, the view heap is a shader-visible title heap, and the pipelines' translations read root views, root constants and a six-texture table the way the title's do. Their host code reads nothing, so any root signature accepts it, and the CPU work per call is the same. Each row is the best of 5 × 20,000 calls. Lists are reset every 2,000 calls. Each build's test binary was run 7 times, interleaved in random order with the other builds, and the table gives the median. RTX 4060 Laptop GPU, Core i7-14650HX, Windows 11, release profile, machine otherwise idle.

ns main this PR #1978 #1978 + this PR #1978 + #2102 + #2821 + #3005 + #3061 the same + this PR
DrawIndexedInstanced, nothing changed 2713 2047 2046 1403 1774 1132
draw sequence (root CBV, 4 root constants, index buffer, draw), per draw 2780 2174 2023 1520 1888 1199
Dispatch (table: texture + UAV buffer, root CBV) 855 748 733 627 627 522
ExecuteIndirect, one indexed draw 4660 4120 3893 3294 3550 2835
draw sequence on 5 threads at once, per draw per thread 10526 9831 6664 8318 4539 4537
list::ensure_state, state unchanged 70 35 72 36 68 37

On one thread the saving adds to the other PRs' savings: about 600 ns per six-texture draw in every combination.

With five threads recording at once, the shared locks set the time:

  • this PR alone: 10526 → 9831 ns (−7%);
  • on top of #1978 alone, five threads get slower: 6664 → 8318 ns (medians; 6 of the 7 runs with both are slower than every run of #1978 alone). With less work between them, the threads reach the remaining shared locks (HostHeap::made, descriptor::VIEWS, TEXTURES_AT) more often, and those locks get more contended;
  • with #2821's lock changes in as well, it's neutral: median 4539 → 4537, fastest run 4250 → 3525.

So this PR should merge with or after #2821, not alone after #1978.

What this does not show

  • No in-game numbers: this was not run in a game. The benchmark's draw reads six textures. A title's draws read between none and dozens, so the saving per draw scales with that count (about 100 ns per texture read).
  • The 5-thread rows depend on how many textures the threads share. In the benchmark all five threads read the same six.

Request: could someone with the game run an A/B of the frame line's list-call time (or #3224's counters) on main and on this branch, in the same spot in the world?

Overlap

  • #3245 also adds a "move a host copy the caller holds" path (transition_copy / ensure_copy_state) for its use-after-free fix. Both PRs touch the same lines in HostList::transition and list::ensure_state. Whichever merges second should use the other's helper. I'll rebase onto it if #3245 lands first. This PR merges cleanly with #1978, #2102, #2821, #3061, #3215, #3238, #3254 and #3184.

AI use: Claude Code (Claude Opus 5.5) wrote the change, the tests and the benchmark, ran the measurements and drafted this description.

🤖 Generated with Claude Code

**@⁠KainanP** on GitHub · 2026-11-26 14:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/3258) `KainanP:perf/texture-read-once` → `main` · +261 −27 · 5 files · 1 commits · open Part of #2586 (the per-call cost of list methods on the title's threads). Local optimization, no architecture change, same behaviour. ## The problem Every texture a draw or dispatch reads through a descriptor table goes through `draw::readable`. For one texture it: - looked up the title texture's host copy **three times**: `resource::host_resource` (for the upload check), `texture::upload` → `is_host_written` → `resource::host_copy`, and `list::ensure_state` → `HostList::transition` → `resource::host_copy`. Each lookup takes the resource's `host` mutex and clones an `Arc` (`host_resource` also AddRefs/Releases the COM texture). Title threads drawing with the same textures all take the same per-resource mutexes. - asked the driver for the texture's description **three times**: `texture::title_written` (`GetDesc`), then `host::legal_state` in `HostList::transition` and `host::legal_state` again in `list::ensure_state`. The flags never change after the texture is made. The per-call benchmark below has an indexed draw that reads six textures. On main, `list::ensure_state` with the state unchanged costs 70 ns and the whole texture path about 1.9 µs of a 2.7 µs draw. ## The change - `HostTexture` keeps its resource flags (`flags`), read once in `HostTexture::new`, which already called `GetDesc`. `HostTexture::legal(state)` and `HostTexture::title_written()` give the same answers as `host::legal_state` and `texture::title_written`, without calling `GetDesc`. - `HostList::transition_held(copy, wanted)` and `list::ensure_held_state(list, copy, state)` move a host copy the caller already holds. `transition` and `ensure_state` look the copy up once and call them, so every other caller gets the same result. - `texture::upload_copy(list, resource, copy)` does what `upload` does, for a copy the caller holds. The diagnostics-only "keeps what the host copied" note moved into a shared helper, unchanged. - `readable` looks up the copy once and uses it for both the upload and the state move. A texture evicted between the old lookups could give the upload and the barrier two different host textures. With one lookup that can't happen. ## Tests Two new tests in `draw::tests`: - `a_host_textures_kept_flags_give_what_its_description_does`: for five kinds of host texture (plain, render target, unordered access, depth, depth + deny shader resource), the kept flags match `GetDesc`. `legal()` matches `legal_state` for every state bit and the common combinations, and `title_written()` matches `texture::title_written`. - `a_texture_read_uploads_once_and_moves_as_a_read_by_resource`: reading a title-written texture, moving it to copy source by resource, then reading it twice more gives exactly the barriers a read by resource gives: none on first use, then SR→copy source→SR. The list records its upload once. A texture the host wrote is not uploaded. Runs: | | RTX 4060 Laptop (driver 32.0.16.1047) | Intel UHD (31.0.101.5445, through a local adapter-selection hook) | WARP (`--features diagnostics`, `VICTORY_D3D12_WARP=1`, serial) | |---|---|---|---| | main 63bcca4 | 229 pass, 2 fail | 228 pass, 3 fail | n/a | | this PR | 231 pass, same 2 fail | 230 pass, same 3 fail | 248 pass, 0 fail | The RTX failures are `descriptor::host::tests::dynamic_slots_are_held_until_released` and `memory::tests::lists_share_a_table_and_copies_keep_their_views` (the NVIDIA heap cap, #1835). `memory_tests::heap_direct_texture_reads_mark_their_heap_index` was 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. `cargo clippy -p victory-gpu --all-targets` passes with `-D warnings`, with and without `--all-features`. rustfmt is clean on the changed lines. ## Measured Method: an `#[ignore]`d test, `pipeline::list_bench` ([branch `perf/list-call-bench` on my fork](https://github.com/KainanP/VIctory/tree/perf/list-call-bench), one commit on top of main, not part of this PR). It calls each list method through the list's method table on a host device. The title buffers are in write-watched memory, the view heap is a shader-visible title heap, and the pipelines' translations read root views, root constants and a six-texture table the way the title's do. Their host code reads nothing, so any root signature accepts it, and the CPU work per call is the same. Each row is the best of 5 × 20,000 calls. Lists are reset every 2,000 calls. Each build's test binary was run 7 times, interleaved in random order with the other builds, and the table gives the median. RTX 4060 Laptop GPU, Core i7-14650HX, Windows 11, release profile, machine otherwise idle. | ns | main | this PR | #1978 | #1978 + this PR | #1978 + #2102 + #2821 + #3005 + #3061 | the same + this PR | |---|---:|---:|---:|---:|---:|---:| | `DrawIndexedInstanced`, nothing changed | 2713 | **2047** | 2046 | **1403** | 1774 | **1132** | | draw sequence (root CBV, 4 root constants, index buffer, draw), per draw | 2780 | **2174** | 2023 | **1520** | 1888 | **1199** | | `Dispatch` (table: texture + UAV buffer, root CBV) | 855 | **748** | 733 | **627** | 627 | **522** | | `ExecuteIndirect`, one indexed draw | 4660 | **4120** | 3893 | **3294** | 3550 | **2835** | | draw sequence on 5 threads at once, per draw per thread | 10526 | **9831** | 6664 | **8318** | 4539 | **4537** | | `list::ensure_state`, state unchanged | 70 | **35** | 72 | 36 | 68 | 37 | On one thread the saving adds to the other PRs' savings: about 600 ns per six-texture draw in every combination. With five threads recording at once, the shared locks set the time: - this PR alone: 10526 → 9831 ns (−7%); - on top of #1978 alone, five threads get **slower**: 6664 → 8318 ns (medians; 6 of the 7 runs with both are slower than every run of #1978 alone). With less work between them, the threads reach the remaining shared locks (`HostHeap::made`, `descriptor::VIEWS`, `TEXTURES_AT`) more often, and those locks get more contended; - with #2821's lock changes in as well, it's neutral: median 4539 → 4537, fastest run 4250 → 3525. So this PR should merge with or after #2821, not alone after #1978. ## What this does not show - No in-game numbers: this was not run in a game. The benchmark's draw reads six textures. A title's draws read between none and dozens, so the saving per draw scales with that count (about 100 ns per texture read). - The 5-thread rows depend on how many textures the threads share. In the benchmark all five threads read the same six. **Request:** could someone with the game run an A/B of the frame line's list-call time (or #3224's counters) on main and on this branch, in the same spot in the world? ## Overlap - #3245 also adds a "move a host copy the caller holds" path (`transition_copy` / `ensure_copy_state`) for its use-after-free fix. Both PRs touch the same lines in `HostList::transition` and `list::ensure_state`. Whichever merges second should use the other's helper. I'll rebase onto it if #3245 lands first. This PR merges cleanly with #1978, #2102, #2821, #3061, #3215, #3238, #3254 and #3184. _AI use: Claude Code (Claude Opus 5.5) wrote the change, the tests and the benchmark, ran the measurements and drafted this description._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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 (#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 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:55:58 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 15:12 UTC · original · approved these changes

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

  • Caching desc.Flags once on HostTexture at construction avoids repeated ID3D12Resource::GetDesc() calls across transitions/uploads, and passing the held &Arc<HostTexture> through transition_held / ensure_held_state avoids redundant host_copy lookups while keeping the host texture pinned across the whole draw read.
  • cargo test -p victory-gpu --lib draw:: -- --test-threads=1 (RTX 5090): 22 passed, 0 failed (including a_host_textures_kept_flags_give_what_its_description_does and a_texture_read_uploads_once_and_moves_as_a_read_by_resource).
  • cargo test -p victory-gpu --lib texture:: -- --test-threads=1 (RTX 5090): 2 passed, 0 failed.
  • cargo test -p victory-gpu --lib resource:: -- --test-threads=1 (RTX 5090): 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).

**@⁠Dukestar69** on GitHub · 2026-11-26 15:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/3258#pullrequestreview-6076534190) · **approved these changes** Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090): - Caching `desc.Flags` once on `HostTexture` at construction avoids repeated `ID3D12Resource::GetDesc()` calls across transitions/uploads, and passing the held `&Arc<HostTexture>` through `transition_held` / `ensure_held_state` avoids redundant `host_copy` lookups while keeping the host texture pinned across the whole draw read. - `cargo test -p victory-gpu --lib draw:: -- --test-threads=1` (RTX 5090): 22 passed, 0 failed (including `a_host_textures_kept_flags_give_what_its_description_does` and `a_texture_read_uploads_once_and_moves_as_a_read_by_resource`). - `cargo test -p victory-gpu --lib texture:: -- --test-threads=1` (RTX 5090): 2 passed, 0 failed. - `cargo test -p victory-gpu --lib resource:: -- --test-threads=1` (RTX 5090): 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).
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3258/head:github-bot/gh-3258'
git switch 'github-bot/gh-3258'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3258'
git switch 'github-bot/gh-3258'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3258'
git switch 'github-bot/gh-3258'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3258'
git switch 'main'
git merge --squash 'github-bot/gh-3258'
git switch 'main'
git merge --ff-only 'github-bot/gh-3258'
git switch 'main'
git merge 'github-bot/gh-3258'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3258
No description provided.