GPU: occlusion queries measure on the host instead of reporting every draw visible #1731
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!1731
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1731"
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?
@fiestorra on GitHub · 2026-11-24 21:30 UTC · original
fiestorra:gpu/real-occlusion-queries→main· +482 −19 · 4 files · 4 commits · openOcclusion queries now measure instead of answering "visible"
BeginQueryandEndQuerywere wired to the same no-op function, so the two could not be told apart at all, andResolveQueryDataanswered every occlusion query with a fixed value:Serveral consequences, all silent: the title's own occlusion culling could never cull anything, and its dynamic resolution could never lower a resolution — the fixed "a million samples passed" means every occluder looks fully visible, in every world frame. This is H-8.1 of the #1317 report.
What it does now
D3D12_QUERY_HEAP_TYPE_OCCLUSIONwith the same count (that one heap type answers both the sample-count and the pass/fail query types). Heaps of the other kinds stay as they were.BeginQuery/EndQueryare two functions again, and each marks the host list when the heap measures there, so the draws recorded between them are what the query counts.ResolveQueryDataresolves those queries on the host into a read-back buffer, kept alive with the list; once the submission that filled it has finished, its values are written into the title's buffer at the offsets asked for. Results are written before the bottom-of-pipe writes, in case the title names the same memory in both.shortcut:note now says exactly which kinds those are.Verification
The new test goes through the real path — our query heap, our list, our queue, the host device — with four queries marked around no draws:
and it is discriminating: with the old fixed answer forced back on (
measured = false), the same test fails withso the change is what moves the value, not the test being tautological. It also checks that the rest of the title's buffer is left alone, and that the host really made the heap (it panics rather than passing silently if the heap creation fails on this machine).
cargo test -p victory-gpu --locked→ 51 passed, 1 ignored.cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings→ clean.rustfmt --checkon the added lines → clean (the file's pre-existing hunks are untouched, as in the rest of the crate).cargo test --workspace --lockedhas one failure,modules::display::tests::sdr_and_display_deferrals, which fails onmaintoo in this environment (a headless session: the display stack answersE_NOT_SUPPORTED); it is unrelated to this change and is fixed by #1630.What is not done, and what could not be measured
ID3D12Device::GetClockCalibrationand a real timestamp heap are a separate change, and with every submission waited for the two would be close anyway.AMD Radeon(TM) Graphics(driver32.0.21045.29003), which is DXGI's default on this machine. Worlds on NVIDIA also want this, since the value was fixed regardless of adapter.No game data is involved: query types, counts and the test's own synthetic draws.
Written with an AI assistant.
@catminou4 on GitHub · 2026-11-24 21:37 UTC · original · reviewed
Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff.
Occlusion queries measured on the host: query_begin/query_end/mark record into a host D3D12_QUERY_HEAP, resolve_queries defers resolution (defer_resolve) with a readback buffer, and occlusion_for_test exposes a test hook. Previously every draw reported 'passed' — now it measures. The deferred-resolve design (resolve when the title reads results, not at query end) matches D3D12 semantics. Windows-only; the one test (an_occlusion_query_reports_what_the_draws_passed) needs a device.
@kais7654 on GitHub · 2026-11-24 21:47 UTC · original
Reviewed this — the approach is right (host occlusion heap, Begin/End on
the host list, read-back written into title memory after the submission
waits), and I verified
cargo check, clippy andcargo test -p victory-gpu(51 passed, including the new test).
Two problems I found:
resolve_queriestakes the measured path without checking whether therange fits the heap. If
ResolveQueryDatarefuses the resolve (rangepast the heap's count, count == 0), the read-back buffer stays
untouched with undefined contents, and those garbage u64s are still
written into the title's memory — worse than the stub's plausible
values. Fix: check
first + countagainst the heap's count (andcount > 0) in the measured path's condition, so a refused range falls
back to the stubs. Ready as a patch, happy to push it.
A failing
Mapof the read-back buffer silently drops the results:worth a
tracing::warn!like the other deferred failures.Also opened a stacked PR with a patch for 1 and 2 (from my fork, branch
pr-1731-feedback).@fiestorra on GitHub · 2026-11-24 21:53 UTC · original
Rebased onto
main(70564ae): the conflict was the list's method tablemaingained three clears (ClearUnorderedAccessViewUint,…Float,DiscardResource) at slots 0x1a8/0x1b0/0x1b8, soMORE_METHODSgrew from 9 to 12 entries and both sides had edited it. The resolution keeps main's entries and only swaps the two query slots:Everything else applied as-is: the occlusion heap in
query.rs, the read-back buffer and the resolve replay inhost.rs, and the test inresource.rs.Verification after the rebase:
cargo test -p victory-gpu --locked→ 79 passed, 1 ignored (includes the occlusion test: four queries around no draws put zeros in the title's buffer, where the forced old path puts1048576in all four);cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings→ clean;rustfmt --checkonhost.rs,list.rsandquery.rs: the same hunks asmain, minus the trailing-newline one my edits had introduced (fixed).One small change since the pull request opened: the read-back pair is now a named type (
type Resolved = (ID3D12Resource, Vec<Target>)) so the field fits on one line — that was the only formatting complaint in the added lines.Still true, and unchanged: timestamps are the process clock, and there is no in-game measurement here (no presentable desktop; the agent runs in session 0).
No game data is involved.
Written with an AI assistant.
@crissuper20 on GitHub · 2026-11-24 22:43 UTC · original
In-game A/B on a 32-lane low-end card: with this PR the world runs faster and draws about half as much, which looks like the title's occlusion culling taking effect.
Setup: GTX 1650 (Turing, 4 GB, 32-lane waves, driver 32.0.16.2124), i5-4690, 16 GB, Windows 11 LTSC 24H2, diag build,
--console series-s, the same save andVICTORY_KEYSscript,VICTORY_SHADER_LOOP_LIMIT=64,VICTORY_NO_WATCHDOG=1,VICTORY_D3D12_DRED=1, 25 min per run.Base:
mainffe310e+ #439 #454 #493 #1129 #984 #609 (a stack that stays up in the world on this card), then the same + this PR at64317c5(merged cleanly; that was before your rebase onto70564ae).World phase only (5-second log samples from the first frame with more than 100 draws):
One run each, so the exact numbers are noisy, but everything moved the same way: half the draws, and shorter frames, copies and spikes. That fits occlusion results reaching the title instead of "always visible". The world image is still black on this card (as elsewhere on NVIDIA), so I can't check the culling visually.
Claude Code assisted.
@rminzao on GitHub · 2026-11-24 23:09 UTC · original
Query types and resolve ranges
A few correctness issues in the host query path:
query.rs~49 tests the heap'sdesc.kindagainst query-type constants. Heap type 1 isTIMESTAMP, so every timestamp heap also gets a host occlusion heap that is never used.query.rs~103,list.rs~825), so aBINARY_OCCLUSIONquery resolves as a sample count instead of 0/1.resolve_queries(list.rs~860) has nofirst + count <= heap countcheck and no check that each query was ended.ResolveQueryDatais then invalid, and uninitialised readback contents are written into title memory.Closeare never ended, which D3D12 doesn't allow.Static review (AI-assisted), read against
mainat70564ae; nothing was run on a GPU.@fiestorra on GitHub · 2026-11-24 23:35 UTC · original
All four taken, and each one is now pinned by the GPU test
You were right on every point, and the first is the one that would have hurt: I had the heap's kind and the query type conflated. Fixed in
150964d:D3D12_QUERY_HEAP_TYPE_TIMESTAMPis 1, and my code read a heap of that type as "binary occlusion", creating a host occlusion heap nothing ever marked. Now onlyD3D12_QUERY_HEAP_TYPE_OCCLUSION(0) gets a host heap, and the constant is namedHEAP_OCCLUSIONwith a comment saying why.query::host(this, query_kind, index)takes the type fromBeginQuery/EndQuery/ResolveQueryDataand maps 0 →D3D12_QUERY_TYPE_OCCLUSION, 1 →D3D12_QUERY_TYPE_BINARY_OCCLUSION; so a binary query resolves as the pass/fail form instead of as a sample count.Idle/Open/Ended): anEndQuerywithout aBeginQueryis refused and not recorded, andResolveQueryDatarequires every index infirst..first + countto be inside the heap and ended, so nothing uninitialised is written into title memory. The range check isfirst.checked_add(count) <= heap count.Close— covered by the same state: a query left open is notEnded, so it cannot be resolved, and the mark is refused at the point it is recorded rather than at the end.The test now asserts the three that do not need the title:
Verification:
cargo test -p victory-gpu --locked→ 79 passed, 1 ignored; the occlusion test on the device → 1 passed;clippy -p victory-gpu --all-targets --all-features -D warningsclean;rustfmtclean on the two files (the pre-existing hunks inresource.rsare still 15, same asmain).cargo test --workspacehas the one failuremainalso has in a headless session (sdr_and_display_deferrals).One thing I did not do, and would like your view: the heap keeps its own state, so a query marked on one list and resolved on another works only if both go through the same heap object (which they do, since the state lives in the heap) — but I do not check that the list that resolves has seen the mark, only that some mark ended it. D3D12 is stricter about ordering, and if you think that matters here I will add the check.
Written with an AI assistant.
@Marvisak on GitHub · 2026-11-25 03:36 UTC · original · edited 2026-11-26 01:19 UTC
Automated triage for the maintainers.
Needs work. The rebase resolved the earlier conflict, and it merges cleanly now. Measuring occlusion on a host
D3D12_QUERY_HEAP_TYPE_OCCLUSIONheap is the right direction, and two in-title runs show the title culling. But the push (4ccbfb9) does not address the points raised after the last review:list::markandresolve_queriesonly hold a clone of it for the call, so a title that releases its heap before the list executes frees a heap the GPU still uses.list.keep(&host)(or similar) is needed in both places.query::resolved(crates/victory-gpu/src/query.rs) resets indices toIdlewhile it walks them. A range that fails partway is left half-reset. It also runs beforereadback_buffer, so a failed allocation drops ended queries to the stub. A second resolve of the same ended range, which D3D12 allows, also gets the stub.query::markrefuses aBeginQueryon anEndedquery, which D3D12 allows. A query index that is reused without a resolve in between is silently never measured again.MapinHost::executedrops the results silently. Please add awarn!.nathanbzrr's branch (
805a891) covers the first three. Adopting those fixes, or equivalent ones, with a test would move this to a merge candidate.CI: passing (
check).Generated by Claude Code
@fiestorra on GitHub · 2026-11-25 04:47 UTC · original
Moved the new test up, so this and #1630 no longer collide
Nothing in this pull request changes; only where its test sits. The occlusion-query test was appended to the end of
resource.rs's test module, and #1630 (the texture description ladder) appends its tests at the same place — two changes that touch nothing of each other, and a conflict at merge time for whoever takes both.It now sits next to the placed-resource test, which leaves both files' tails free: the two merge in either order, and my simulation of merging the whole queue stops reporting this pair.
Checked after the move, on
main940d32f's tree with the branch applied:cargo test -p victory-gpu --features diagnostics --lib --locked147 passed, 0 failed, 2 ignored — same counts as before the move;cargo clippy --workspace --all-targets --locked -- -D warningsclean;rustfmt --checkonresource.rsshows the same 15 hunksmainalready has and no new one (my first attempt introduced one, from a stray blank line; that is what the extra force-push was).No game data is involved: file layout and test counts.
Written with an AI assistant.
@nathanbzrr on GitHub · 2026-11-25 06:49 UTC · original
AMD RDNA3 + D3D12 Debug Layer + WARP verification on
11691def(onmain940d32f), plus two additional bugs spotted in review and a verified fix covering all five items:Environment
10.0.26340x64,rustc 1.98.1(x86_64-pc-windows-msvc)32.0.32029.2010) + D3D12 WARPdxil.dll 1.8.2502.11victory-gputests only (no game files / title not run)Baseline on
11691defcargo test -p victory-gpu --locked an_occlusion_query_reports_what_the_draws_passed: 1 passedcargo test -p victory-gpu --locked -- --test-threads=1: 144 passed, 0 failed, 2 ignoredVICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --locked -- --test-threads=1: 144 passed, 0 failed, 2 ignored (0 D3D12 Debug Layer errors/warnings)VICTORY_D3D12_DEBUG=1 VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --locked -- --test-threads=1: 144 passed, 0 failed, 2 ignoredTwo extra findings beyond the triage list
resolve_queries(crates/victory-gpu/src/list.rs) currently callscrate::query::resolved(heap, first, count)beforeh.readback_buffer(...). Ifreadback_bufferreturnsNone, execution falls through to the stub-write loop while all queries infirst..first + counthave already had their state wiped fromEndedtoIdle.crates/victory-gpu/src/resource.rs:stamps(occlusion_for_test(1, COUNT)) is never released before returning froman_occlusion_query_reports_what_the_draws_passed.Verified minimal fix (
nathanbzrr/VIctory@805a891, branchreview/pr-1731-occlusion-fixes)ID3D12QueryHeapinDeferred: call the existinglist.keep(&host)helper in bothlist::markandlist::resolve_queries.BeginQueryonState::Ended: accept(true, State::Idle | State::Ended)inquery::mark.query::resolved+ non-destructive check beforereadback_buffer: validatefirst..endvia sharedcheck_resolvebeforefill(State::Idle), addquery::can_resolve, and callquery::resolvedinlist::resolve_queriesonly afterreadback_buffersucceeds.stampsinan_occlusion_query_reports_what_the_draws_passed, addquery::tests::mark_allows_rebegin_on_ended_and_resolved_is_atomic, and addresource::tests::an_occlusion_query_survives_early_heap_release_and_rebegin(kept abovecommitted_and_placed_resources_map_at_their_gpu_addressso it does not collide with#1630).Results on
805a891(review/pr-1731-occlusion-fixes)cargo clippy -p victory-gpu --all-targets --locked -- -D warnings(default and--all-features): 0 warningscargo test -p victory-gpu --locked -- --test-threads=1: 146 passed, 0 failed, 2 ignoredcargo test -p victory-gpu --all-features --locked -- --test-threads=1: 149 passed, 0 failed, 2 ignoredVICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --locked -- --test-threads=1: 146 passed, 0 failed, 2 ignored (0 D3D12 Debug Layer errors/warnings)VICTORY_D3D12_DEBUG=1 VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --locked -- --test-threads=1: 146 passed, 0 failed, 2 ignoredcargo xtask policy:asset policy: 182 files okAI-assisted review/testing; all builds and GPU runs executed locally on my own machine. No game files used.
@rminzao on GitHub · 2026-11-25 12:14 UTC · original
Re: checking that the resolving list has seen the mark
I'd leave that check out. D3D12 explicitly allows ending a query on one list and resolving it on another. The only rule for occlusion queries is that Begin and End sit in the same command list. A resolve is valid when the
EndQueryhas executed before it on the GPU timeline: either earlier in the same list, or in a list submitted before it (earlier in the sameExecuteCommandListscall, an earlier call on the same queue, or another queue behind a fence). Ending in a scene list and resolving in a later end-of-frame list is the normal pattern, and a per-list check would refuse it. The console works the same way, since its resolve just copies the query memory at that point in the GPU stream.The weak spot is the other direction:
query::mark/query::resolvedmove the heap's state in recording order, but validity depends on submission order. Two concrete cases:BeginQuery(q)before the main thread records frame N's resolve, which is submitted first.mark(begin)seesEnded, refuses, and records nothing for frame N+1, so that query is silently never measured. (Beginon an ended, unresolved query is legal D3D12 anyway, as the triage noted.)resolvedresets the range toIdle, so the second resolve falls back to the1 << 20stub and the two copies disagree.What I'd do instead is keep the host commands as they are and move the validity decision to submission. Each host list records which
(heap, index)it ends and which ranges it resolves. When the queue executes lists, it walks them in order, updates a per-heap "ended on the GPU timeline" state, and marks each deferred resolve as measured or fallback. The write-back into title memory already runs on the CPU after the fence, so that is where the choice applies. The hostResolveQueryDatacan then always be recorded, because the only harm in resolving an index that never ended is an undefined value, and that value is never copied out.Static review (AI-assisted), read against the PR head 11691deff58343b32589ab96ed0ead1c44406331; nothing was run on a GPU.
@desplmfao on GitHub · 2026-11-25 14:01 UTC · original
In-title result for this PR on Linux.
Setup: Grand Theft Auto VI Series X, the first mission's prison yard. RTX 5080 on Linux, Proton with vkd3d-proton 3.2.0. I compared our working branch (
mainplus our open GPU fixes) with the same branch with this PR merged at11691de, in the same scene with a warm pipeline cache. Each figure is the median of the last 10 frame lines.resource::tests::an_occlusion_query_reports_what_the_draws_passedpasses on the RTX 5080 under vkd3d-proton.One note for merging: on top of
mainplus open PRs,host.rsconflicts with work that adds its ownHost::readback_buffer(texture read-backs, a buffer plus mapping). For this test I renamed this PR's function toquery_readback_buffer. A more specific name might save a conflict later.@gmankab on GitHub · 2026-11-26 07:29 UTC · original
I tested a native Linux variant that addresses the recording-order issue raised above. The host query begins, ends and resolves within its producer list. After the fence, the runtime applies the title's query events in submission order. A resolve recorded before its producer can still see the producer's result, and repeated resolves can read the same value. Native test commit
The synthetic regression passed on an RX 6800 with Vulkan validation enabled. It covers cross-list and repeated resolves, binary results, early heap release, an abandoned begin with a mismatched end, two readback chunks, and the visible fallback around an unbound draw. The full GPU crate suite on Lavapipe passed 60 tests with 4 ignored.
In a four-minute Wine-free game run on Lavapipe, the title created four occlusion heaps and the last capture still showed the illustrated loading screen. No occlusion resolves reached the post-fence readback path during that run, so this provides synthetic correctness coverage and leaves the open-world performance impact unmeasured on this build.
Tested-on: Radeon RX 6800 and Lavapipe, Fedora Linux 45, native vkd3d-proton with Vulkan validation
Assisted-by: gpt-6.2 sol xhigh
@fiestorra on GitHub · 2026-11-26 12:23 UTC · original
The five points, and what I am doing with nathanbzrr's branch
All five are real; here is where each stands:
Endedand the partial reset inquery::resolved: these are the three nathanbzrr's805a891covers, and I would rather take their fix than write a second one. I am reading that branch now and will either cherry-pick it with attribution or say why it does not apply to this shape; adopting it with a test is what moves this to a merge candidate.BeginQueryandEndQueryon different threads, and validity depends on submission order. I do not have a measurement of the title recording queries that way; until I do, the honest answer is that this PR assumes one thread per list, and the assumption should be in the code as a comment rather than left implicit.warn!is unarguable and is the next commit.So: this does not become a merge candidate in this reply. It becomes one when the three from
805a891are in and the ordering assumption is written down or moved to submission; I will not claim otherwise.Written with an AI assistant.
@gmankab on GitHub · 2026-11-26 13:12 UTC · original · edited 2026-11-26 13:45 UTC
The Linux native title run reached overlapping occlusion queries as it entered heavier 3D submissions. I reproduced the same Vulkan warning with a 4×4 draw on Lavapipe. It matches the vkd3d-proton virtual-query issue. vkd3d-proton #2381
The Linux native port now uses one host query per interval with a fixed set of active title queries. This removed all three overlap warnings in the fixture and preserved both counted results (20 and 12) and both binary results (1 and 1). The port preserves the submission-order readback from the earlier test. Linux native patch and test
Assisted-by: gpt-6.2 sol xhigh
@fiestorra on GitHub · 2026-11-26 13:39 UTC · original
Four of the five, and the fifth written down instead of guessed
Head
5ddb8c36.Resolvednow carries theID3D12QueryHeapnext to the read-back buffer, anddefer_resolvetakes it. A title that releases its heap before the list runs no longer leavesResolveQueryDatarecorded against a destroyed object: the list keeps the reference the resolve needs until it executes.BeginQueryon anEndedindex opens it again, as D3D12 allows — a title that recycles indices without a resolve in between used to be silently never measured again. Only an end without a begin is refused now.Mapof the read-back buffer is logged (warn!with the error) instead of dropping the results with no trace.Ordering, the fifth point: I am not going to claim multi-threaded recording is handled. The state moves in recording order, which is the order one list records in; a list recorded from several threads can record marks out of submission order, and nothing in this PR orders them. What I can do without a measurement of the title doing that is say it: the module now documents that it assumes one thread per list for query marks, and the check that would catch it (moving the decision to submission) is a different change — the state would have to move with the list, not with the call.
Measured here:
cargo test -p victory-gpu --features diagnostics --lib -- query→ 4 passed;cargo clippy -p victory-gpu --all-targets -- -D warningsclean. Not measured: a title run that reads occlusion results back — this machine's session cannot present, so the in-title numbers in the thread are still the only ones.Written with an AI assistant.
@fiestorra on GitHub · 2026-11-26 14:50 UTC · original
Overlapping intervals are legal on this path; the fix you describe is for the Vulkan side
Your fixture and the vkd3d-proton issue are about a port that maps D3D12 queries onto Vulkan query objects and has to reuse one while another is open. This branch does not go through that path: the occlusion counts are resolved on a host
D3D12_QUERY_HEAP_TYPE_OCCLUSIONheap, one index per title index, and D3D12 allows intervals to overlap when they are different indices — that is what a query heap is for. So "one host query per interval with a fixed active set" is not needed here; it would be a waste of the same thing.What is refused, and stays refused: a second
BeginQueryon an index that is still open, and anEndQuerywithout a begin. Those are the title's own mistakes under D3D12's rules, and the module returns the failure the API returns rather than pretending.Two things from your run I would like to use, if you still have the fixture:
If you can run that fixture against this head (
38157e68, then9cf80c03's sibling) I will fold the numbers into the test rather than describing them.Written with an AI assistant.
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.