GPU: a draw's texture read takes its host copy once, and its flags without asking the driver #3258
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!3258
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3258"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@KainanP on GitHub · 2026-11-26 14:48 UTC · original
KainanP:perf/texture-read-once→main· +261 −27 · 5 files · 1 commits · openPart 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:resource::host_resource(for the upload check),texture::upload→is_host_written→resource::host_copy, andlist::ensure_state→HostList::transition→resource::host_copy. Each lookup takes the resource'shostmutex and clones anArc(host_resourcealso AddRefs/Releases the COM texture). Title threads drawing with the same textures all take the same per-resource mutexes.texture::title_written(GetDesc), thenhost::legal_stateinHostList::transitionandhost::legal_stateagain inlist::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_statewith the state unchanged costs 70 ns and the whole texture path about 1.9 µs of a 2.7 µs draw.The change
HostTexturekeeps its resource flags (flags), read once inHostTexture::new, which already calledGetDesc.HostTexture::legal(state)andHostTexture::title_written()give the same answers ashost::legal_stateandtexture::title_written, without callingGetDesc.HostList::transition_held(copy, wanted)andlist::ensure_held_state(list, copy, state)move a host copy the caller already holds.transitionandensure_statelook the copy up once and call them, so every other caller gets the same result.texture::upload_copy(list, resource, copy)does whatuploaddoes, for a copy the caller holds. The diagnostics-only "keeps what the host copied" note moved into a shared helper, unchanged.readablelooks 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 matchGetDesc.legal()matcheslegal_statefor every state bit and the common combinations, andtitle_written()matchestexture::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:
--features diagnostics,VICTORY_D3D12_WARP=1, serial)63bcca4The RTX failures are
descriptor::host::tests::dynamic_slots_are_held_until_releasedandmemory::tests::lists_share_a_table_and_copies_keep_their_views(the NVIDIA heap cap, #1835).memory_tests::heap_direct_texture_reads_mark_their_heap_indexwas skipped on NVIDIA because it access-violates on main too. Intel also failsa_texture_written_in_one_format_reads_in_another_through_a_placed_viewon main.cargo clippy -p victory-gpu --all-targetspasses with-D warnings, with and without--all-features. rustfmt is clean on the changed lines.Measured
Method: an
#[ignore]d test,pipeline::list_bench(branchperf/list-call-benchon 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.DrawIndexedInstanced, nothing changedDispatch(table: texture + UAV buffer, root CBV)ExecuteIndirect, one indexed drawlist::ensure_state, state unchangedOn 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:
HostHeap::made,descriptor::VIEWS,TEXTURES_AT) more often, and those locks get more contended;So this PR should merge with or after #2821, not alone after #1978.
What this does not show
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
transition_copy/ensure_copy_state) for its use-after-free fix. Both PRs touch the same lines inHostList::transitionandlist::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
@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):
desc.Flagsonce onHostTextureat construction avoids repeatedID3D12Resource::GetDesc()calls across transitions/uploads, and passing the held&Arc<HostTexture>throughtransition_held/ensure_held_stateavoids redundanthost_copylookups 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 (includinga_host_textures_kept_flags_give_what_its_description_doesanda_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).
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.