GPU: queries answer from the host GPU #2260

Open
github-bot wants to merge 3 commits from github-bot/gh-2260 into main AGit
Owner

@⁠FoxxoOwO on GitHub · 2026-11-25 09:14 UTC · original
FoxxoOwO:gpu/real-queries → main · +550 −56 · 9 files · 3 commits · open

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.

**@⁠FoxxoOwO** on GitHub · 2026-11-25 09:14 UTC · [original](https://github.com/Marvisak/VIctory/pull/2260) `FoxxoOwO:gpu/real-queries` → `main` · +550 −56 · 9 files · 3 commits · open 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.
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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:00:09 +00:00
Author
Owner

@⁠Dathinel on GitHub · 2026-11-25 09:25 UTC · original · edited 2026-11-25 09:32 UTC

Timestamp heap type mismatch at afffa8bd. 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 afffa8bd. 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.

**@⁠Dathinel** on GitHub · 2026-11-25 09:25 UTC · [original](https://github.com/Marvisak/VIctory/pull/2260#issuecomment-7158308989) · edited 2026-11-25 09:32 UTC **Timestamp heap type mismatch at afffa8bd.** `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](https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_query_heap_type) and [query types](https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_query_type). **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 afffa8bd. 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.
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 09:40 UTC · original

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

**@⁠kotekompotek** on GitHub · 2026-11-25 09:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/2260#issuecomment-7158645279) 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`).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 10:14 UTC · original · edited 2026-11-25 12:32 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-25 10:14 UTC · [original](https://github.com/Marvisak/VIctory/pull/2260#issuecomment-7159426447) · edited 2026-11-25 12:32 UTC <!-- victory-triage v1 head=afffa8bd0fee87d638c0e8925f96a3255de0c004 verdict=proposed-close at=2026-11-25T13:21:00Z --> 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](https://claude.ai/code)_
Author
Owner

@⁠FoxxoOwO on GitHub · 2026-11-25 11:29 UTC · original

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.

**@⁠FoxxoOwO** on GitHub · 2026-11-25 11:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/2260#issuecomment-7161113331) 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.
This pull request has changes conflicting with the target branch.
  • README.md
  • crates/victory-gpu/src/host.rs
  • crates/victory-gpu/src/list.rs
  • crates/victory-shader/src/emit/host.rs
  • crates/victory-shader/src/emit/lower.rs
  • crates/victory-shader/src/emit/mod.rs
  • crates/victory-shader/src/emit/shader.rs
  • crates/victory-shader/src/emit/valu_tests.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2260'
git switch 'github-bot/gh-2260'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2260'
git switch 'github-bot/gh-2260'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2260'
git switch 'main'
git merge --squash 'github-bot/gh-2260'
git switch 'main'
git merge --ff-only 'github-bot/gh-2260'
git switch 'main'
git merge 'github-bot/gh-2260'
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!2260
No description provided.