Skip to content

GPU: queries answer from the host GPU - #2260

Open
FoxxoOwO wants to merge 3 commits into
Marvisak:mainfrom
FoxxoOwO:gpu/real-queries
Open

FoxxoOwO wants to merge 3 commits into
Marvisak:mainfrom
FoxxoOwO:gpu/real-queries

Conversation

@FoxxoOwO

Copy link
Copy Markdown

BeginQuery, EndQuery and ResolveQueryData now run on the host GPU when a device exists. A console query heap makes a host heap of its own kind on first use (occlusion, timestamp, pipeline statistics) and the list records into it. A resolve lands in a read-back buffer of exactly the results' size; the copy into the title's buffer waits for the list where the fence wait already puts the bottom-of-pipe writes. Kinds that map to no host type, and heaps whose host heap could not be made, keep the old plausible values.

Verified on GTA VI (I:\GTA6-Xbox): occlusion queries (kind 0) resolve per frame (~30 queries per resolve) and the world reaches mesh pipelines.

Tests: cargo test -p victory-gpu passes (51), check/clippy clean.

TeresaPC added 3 commits November 24, 2026 12:12
The console carries an image atomic's width in dmask, with no _x2
opcode variants: two data dwords are one 64-bit value (with cmpswap, the
two of the comparison after it). So the texture's host view is an R32G32
integer view and the operation is DXIL's i64 atomic (SM 6.6), the value
joined from the register pair and the old value split back into both
dwords when glc asks for it.

The validator wants AtomicInt64OnTypedResource (module flag bit 27) and
AtomicInt64OnHeapResource (bit 32), which it gets: the handle always
comes from the descriptor heap, so both hold.

Tested with two shaders in emit/valu_tests.rs, validated by dxil.dll 1.6
and dispatched on the hardware adapter and WARP: an add of 0xFFFFFFFF
twice carries out of the low dword (proving the pair is one value, not
two 32-bit lanes) with the old value returned to both dwords, and a
cmpswap that matches swaps and one whose comparison fails returns the
texel unchanged.

Not exercised: the signed and unsigned min/max i64 atomics, which take
the same path.
BeginQuery, EndQuery and ResolveQueryData used to be recorded nowhere: a
resolve wrote plausible values (a million samples, binary occlusion
visible, the submission's own length as a timestamp), so an occlusion
query could never cull anything and a timestamp measured nothing.

A console query heap now makes a host heap of its own kind on first use
(occlusion, timestamp, pipeline statistics) and the list records into it.
A resolve lands in a read-back buffer of exactly the results' size; the
copy into the title's buffer waits for the list, where the fence wait
already puts the bottom-of-pipe writes. Kinds that map to no host type,
and heaps whose host heap could not be made, keep the old plausible
values, so a resolve still fills the buffer it is given.

Not checked on hardware: nothing here proves the occlusion counts match
what the title expects to have culled, so a scene change on a real GPU
after this commit points at the queries.
@Dathinel

Dathinel commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Timestamp heap type mismatch at afffa8b. Host::query_heap receives heap.desc.kind but matches query-operation numbers. The existing query.rs fixture uses descriptor kind 1 for timestamps. An assertion immediately before native heap creation got OCCLUSION (0) for that descriptor, expected TIMESTAMP (1). The kind-0 occlusion test below therefore misses this.

In a local integration on 940d32f I changed just the heap mapping to 0 → occlusion, 1 → timestamp, 2 → pipeline statistics, leaving the command-list query type mapping alone. With four timestamp EndQuery calls, the deferred native resolve wrote 32 bytes and preserved the 32-byte sentinel tail. The four ticks were equal, so this checks readback only. With one statistics query around no work, the native resolve wrote eleven zero counters (88 bytes) and preserved the next 40 bytes. Synthetic Windows tests. D3D12 heap types and query types.

Timestamp units after the heap fix. Three separate command lists on the same queue each recorded one timestamp and resolved eight bytes. On this machine native D3D12 reported 1,000,000,000 ticks/s; the emulated queue's GetTimestampFrequency (slot 16) returned 100,000,000. Successive returned values differed by 42,782,720 and 42,169,344 ticks across roughly 42 ms gaps. A caller using the advertised frequency would compute 427.8 ms and 421.7 ms (10× the native intervals). The earlier four equal timestamps in one batch could not reveal this. This measured ratio is adapter-specific. #159 already has a clock conversion and a synthetic regression named host_timestamps_are_on_the_title_clock for keeping native and fallback results in the same clock domain.

The query changes can be reviewed separately from the shader work in #2241. This branch contains that PR's exact head (e2624b3), followed by 317573d and afffa8b. I applied just the GPU code from those latter two commits to main 940d32f. In the list method-table conflict I kept main's 12 entries, including slots 53–55, and routed slots 28/29 to begin_query/end_query. Six query-filtered tests passed on Windows.

I also adapted the empty-draw fixture from #1731 to exercise the public command-list slots. With a host occlusion heap present, I initialized a 64-byte result buffer to 0xa5, began and ended four queries with no draws, then resolved and submitted. The first 32 bytes became zero and the remaining 32 retained 0xa5. This verifies the empty-draw readback on that integration; timestamps, statistics and positive sample counts remain outside this check. #159 also has relevant fixtures for skipped draws and repeated resolves.

AI-assisted; synthetic data only.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GPU queries answered from the host GPU (query heaps: occlusion/binary/timestamp/pipeline-stats, resolved read-backs) + README status update. mergeable=False against main 940d32f — needs a rebase. Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1. Query semantics (resolve offsets, heap types per console kind) need Windows hardware validation.

@kotekompotek

Copy link
Copy Markdown

Read through the two commits past e2624b3 (this PR is stacked on #2241 — the atomics and valu_tests changes in the Files view are that base's, the actual delta here is ~200 lines in host.rs/list.rs/query.rs).

The mechanism looks right:

  • One-time host heap per console heap via OnceLock, kinds mapped 0|1→OCCLUSION / 2→TIMESTAMP / 3→PIPELINE_STATISTICS, unknown kinds and host-heap creation failures fall back to the plausible-value path rather than failing the resolve.
  • ResolveQueryData lands in a fresh read-back buffer of exactly count * 8, and the CPU copy rides the existing deferred-writes point — after the list's fence wait, so the bottom-of-pipe results are final. Bounds are checked against the title buffer before data.add(offset).

One legality question: BeginQuery is not valid for D3D12_QUERY_TYPE_TIMESTAMP on the host — timestamps may only be EndQuery'd. If a title ever calls BeginQuery(heap, 2, i), the debug layer will flag it (and the call is ignored). Harmless, but filtering kind == 2 in begin_query would keep VICTORY_D3D12_DEBUG output clean.

Smaller notes: a fresh committed read-back buffer per resolve is correct but allocation-heavy if a title resolves every frame — a pooled buffer would help later, no need to block on it. The ?desc → fielded log fix in afffa8b is right (Desc derives no Debug).

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Proposed close: superseded by #2354. As you say above, the rebased change is in #2354 ("GPU: queries run on the host GPU"), with the timestamp rescaling and the BeginQuery filter. This branch still conflicts with main (940d32f) in seven files. #2354 will be reviewed in its own right.

This PR will be closed in 48 hours unless you push a change or a maintainer removes the triage: proposed close label. Thanks for carrying the review points over.


Generated by Claude Code

@FoxxoOwO

Copy link
Copy Markdown
Author

Rebased onto current main and addressing the review:

  • Timestamp frequency: the host GPU's clock is not the title's. Results are now rescaled to the title's 100 MHz ticks (GetTimestampFrequency of the queue) before they are copied into title memory.
  • BeginQuery on timestamps: D3D12 has no BeginQuery for a timestamp query. begin_query now skips timestamps; end_query marks them.

The old branch could not be force-pushed, so the rebased change is in #2354. Happy to close this one if you prefer to review there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants