Skip to content

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

Open
KainanP wants to merge 1 commit into
Marvisak:mainfrom
KainanP:perf/texture-read-once
Open

KainanP wants to merge 1 commit into
Marvisak:mainfrom
KainanP:perf/texture-read-once

Conversation

@KainanP

@KainanP KainanP commented Nov 26, 2026

Copy link
Copy Markdown

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:

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

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

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

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

vanya890 added a commit to vanya890/VIctory that referenced this pull request Nov 26, 2026
Adapt upstream Marvisak#3258 to the fork's Marvisak#3245 pinned-copy helpers. Cache immutable resource flags, use one retained HostTexture for upload/staging/state, and add flag parity and repeated-read regression coverage.
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.

2 participants