Skip to content

Runtime: replay overlapping statistics and binary occlusion queries (D3D-252) - #2100

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Koro4e:d3d-252-overlapping-queries
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Koro4e:d3d-252-overlapping-queries

Conversation

@Koro4e

@Koro4e Koro4e commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed on #313: #313 (comment)

What and why

D3D12's query rules are per heap element ("It is illegal to begin a query twice without ending it, for a given element", the Queries page), so a command list may overlap two pipeline-statistics queries, or an ordinary occlusion query and a binary one, on different slots of one heap — the recording side already accepts them. The replay refused every such list with ReplayResult::unsupported (vulkan_replay.h begin-on-active checks), so a title that overlaps queries got no results at all instead of both results.

The overlapping-ordinary-occlusion machinery already virtualizes concurrent queries as non-overlapping physical segments summed at resolve time. This extends it to the remaining cases:

  • Binary occlusion joins the ordinary kind in the same physical segment pool. Both are VK_QUERY_TYPE_OCCLUSION, so the invariant "one physical query of the type is open at a time" holds unchanged, and the shared intersection interval is counted by both logical queries. A binary resolve of a multi-segment query sums its segments and normalizes the total to 0/1: OcclusionResolve::sum_binary reuses the existing sum and normalize pipelines back to back, wired as sum_binary_occlusion and gated by QueryReplay::needs_sum_binary.
  • Overlapping pipeline-statistics queries split into the statistics pool with the same one-slot-per-event layout (plan_overlapping_statistics_segments), and every non-timestamp resolve — and every helper dispatch — while a query runs closes and reopens the open segment for every query still running, so D3D-162's helper-dispatch exclusion keeps holding for all of them (the helpers share the segment numbering with the single-query planner's). A resolve sums the query's contiguous slot range through the existing statistics_sum.comp.
  • The single-list replay() helper has no segment pool. Running such lists whole would keep two Vulkan queries of one type active, which the driver rejects, so it refuses them explicitly instead (PreparedList::statistics_overlaps), the way it already refuses overlapping occlusion resolves without a pool.

How checked

Windows 11 26200.9457, MSVC 14.44 (VS 2022 BuildTools), Vulkan headers fetched by the pinned LEONIDA_VULKAN_HEADERS_FETCH, glslang 16.6.0; GPU NVIDIA GeForce RTX 3060 (driver 617.14, Windows).

  • vk_overlapping_occlusion (GPU): new crossing statistics queries are checked word by word, all eleven counters, against three unsplit reference queries that carry the same draws; an ordinary query around a binary one is checked against its references (ordinary sum, binary 1); CS invocations stay 0. All pre-existing cases pass unchanged.
  • replay_query_predication (CPU, no GPU): new cases 9–13 pin the segment layouts — five/four-slot crossing statistics plans, the shared-segment kind overlap, the composite sum-and-normalize binary resolve, a D24 footprint conversion inside overlapping statistics queries (its dispatches run outside every open segment; the helpers share the numbering, statistics_segment_count grows by two), and the single-list helper's refusal.
  • d3d12_vulkan_device (GPU, end to end through the proxy): the old pin of the occlusion+binary refusal now expects a successful replay with resolves.
  • Full ctest on the RTX 3060 on the fully rebuilt tree (main at 4947579): all 117 tests pass, d3d12_trace and d3d12_draw included (an earlier local d3d12_draw failure came from stale test binaries and is gone after the rebuild); the seven skipped tests are the usual data-gated ones that skip on every run here.
  • vk_binary_occlusion, vk_command_replay, vk_queue_query_sync, predicated_copy, depth24_convert and the rest of the suite pass unchanged.

Notes

  • docs/d3d12_vulkan.md 4.6 moves the case out of "Not implemented yet" and describes the design; the D3D-162 row's "non-query helpers and resolves share the segment numbering" now holds in the overlapping planner too.
  • The Intel-Arc push-constant fallback path (TEST-57's 128-byte device) is not exercised here; the compute helpers gate on can_resolve_occlusion exactly as before.
  • Depends on: none (based on main at 4947579).

AI-assisted: yes

@0xTehuti 0xTehuti 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 Verification & Code Audit: PR #2100 (D3D-252)

Tested and verified on Windows 11 with MSVC 2022, Ninja, CMake, and Vulkan SDK 1.4:

  • GPU: NVIDIA GeForce RTX 2070 SUPER (8192 MB VRAM, Turing TU104, Compute Capability 7.5)
  • Driver: NVIDIA Game Ready / Studio 595.79 (Vulkan 1.4.304 / 1.4.363)
  • Validation Layers: Khronos Validation Layer active (LEONIDA_VK_VALIDATION=1)
  • Environment: Clean-room with synthetic test harnesses (zero game assets)

Test Execution & Results

All 6 query and occlusion test targets compiled and passed 100%:

Test Target Duration Status Vulkan Validation Errors
vk_queue_query_sync 0.47s PASSED 0 errors
vk_overlapping_occlusion 0.29s PASSED 0 errors
vk_binary_occlusion 0.32s PASSED 0 errors
d3d12_vulkan_device 13.99s PASSED 0 errors
d3d12_query_rules 0.55s PASSED 0 errors
replay_query_predication 0.01s PASSED 0 errors

Total test time: 15.71s (100% pass rate).

Engineering Review Notes

  • Virtual Segment Pooling: Correctly circumvents the Vulkan limitation allowing only one active occlusion query at a time by transparently sharing and chaining physical pool segments when D3D12 begins multiple overlapping queries.
  • Normalization Compute Shaders: The composite sum-and-normalize shaders run outside conditional rendering scopes as required by D3D-148, avoiding suppression under active predicates.
  • Lifetime & Resource Management: Query pool allocations, staging buffers, and fence synchronizations in vulkan_replay.h / vulkan_backend.h show clean ownership semantics with zero leaks detected.

LGTM. Ready to merge from a Windows + NVIDIA hardware perspective.

AI-assisted verification (Orca / Antigravity). No game data used.

@mrp1no

mrp1no commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested ee379620 on Windows 11 Pro 26300, MSVC 19.52 (VS 18 Build Tools, Ninja, RelWithDebInfo), NVIDIA GeForce RTX 3060, driver 610.88, Vulkan headers/loader 1.4.357: pass, but without the main GPU check.

  • ctest --test-dir runtime/build -C RelWithDebInfo: 109/109 passed, replay_query_predication (the new CPU segment-layout cases) and d3d12_vulkan_device (the occlusion + binary list that now replays) included.
  • Not run: vk_overlapping_occlusion. This machine has no glslangValidator, so CMake doesn't build it, and that's where the crossing statistics and the ordinary-around-binary cases are checked on the GPU. Someone with glslang should run that one.

AI-assisted (Claude Code).

@ezwinxdlmao

Copy link
Copy Markdown
Contributor

Tested ee37962 on an NVIDIA GeForce RTX 3050 Laptop GPU, driver 32.0.16.2101, Windows 11, native MSVC (VS 2022 Build Tools, Ninja, RelWithDebInfo), with llvm-mc 21.1.8 for the translated-shader tests: pass.

  • ctest -R "d3d12_vulkan_device|replay_query_predication|vk_overlapping_occlusion": 3/3 pass.
  • Full ctest -j3: 115/116. The one failure is d3d12_draw (bundle blend pixel 127,0,127,255; TEST-266, fixed by Tests: wait for the bundle blend draws in d3d12_draw (fence 31, not 21) #1995), which fails identically on unmodified main 4eb6155d (this PR's base) on the same GPU. No build warnings in the files this PR touches.

AI-assisted (Claude Code).

…D3D-252)

D3D12 only forbids beginning the same query element twice while it runs, so a
command list may overlap two pipeline-statistics queries, or an ordinary
occlusion query and a binary one, on different slots of one heap. The replay
refused all of those with ReplayResult::unsupported. The overlapping-ordinary
occlusion machinery already virtualizes concurrent queries as non-overlapping
physical segments whose results are summed; this extends it to the remaining
cases:

- Binary occlusion joins the ordinary kind in the same physical pool (both are
  VK_QUERY_TYPE_OCCLUSION, so one physical query is open at a time and the
  shared intersection interval is counted by both logical queries). A binary
  resolve of a multi-segment query sums its segments and normalizes the total
  to 0/1 (OcclusionResolve::sum_binary; run flag needs_sum_binary).
- Overlapping pipeline-statistics queries split into the statistics pool with
  the same one-slot-per-event layout (plan_overlapping_statistics_segments);
  every non-timestamp resolve while a query runs closes and reopens the open
  segment for D3D-162's helper-dispatch exclusion. The single-list replay()
  helper still has no segment pool and refuses such lists outright instead of
  running two Vulkan queries of one type whole.

The device test's old pin of the occlusion+binary refusal now expects a
replay; vk_overlapping_occlusion adds crossing statistics queries checked
word-by-word against three unsplit references and an ordinary query around a
binary one; replay_query_predication pins the segment layouts, the composite
binary resolve and the helper's refusal without a GPU. docs/d3d12_vulkan.md
4.6 moves the case out of "Not implemented yet".

AI-assisted: yes
@UlteriorMotive00

Copy link
Copy Markdown
Contributor

Tested ee379620 on RTX 5070 (Blackwell), NVIDIA 617.42, Windows 11 26300: pass

AI-assisted run for D3D-252: NVIDIA GeForce RTX 5070, driver 617.42 (32.0.16.1742), Windows 11 Home 26300, MSVC 14.44 (VS 2022 Build Tools), Ninja RelWithDebInfo via scripts/build_runtime.ps1, at ee379620 (PR head, contains main 4eb6155d). LEONIDA_VK_DEVICE=5070 (the PC also has an AMD iGPU).

vk_overlapping_occlusion: pass.

  • statistics split around a sum: IA vertices 6, PS invocations 0, CS invocations 0, equal to two unsplit queries
  • crossing statistics: IA vertices 6/6, CS invocations 0, equal to the unsplit references
  • crossing 80/20; nested 84/16; 261 segments summed; alternating binary results 1/1; resolve inside an active query 4; offsets and gated overwrites exact

d3d12_vulkan_device: pass (copy comparison 0 of 69 differ; resolve comparison 60 matched, 0 mismatches; FORMAT_SUPPORT 83 formats, 0 mismatches).

replay_query_predication: pass.

Full ctest (-j 4): 116/116 (gameinput disabled: no SDL3).

One note on d3d12_draw, unrelated to this PR: it passed in that ctest run, but run on its own it fails intermittently with bundle blend pixel (0, 0): got 127,0,127,255, expected red 64. That's 5 of 5 runs on this PR's head and 4 of 5 on unmodified main 4eb6155d on the same machine, so it looks flaky rather than caused by this PR (the machine was also under heavy CPU load from another build at the time).

@AmazingMoaaz

Copy link
Copy Markdown
Contributor

Tested ee37962 on Intel UHD Graphics (Xe-LP, Tiger Lake-H iGPU, driver 101.7088) and NVIDIA RTX 3050 Laptop GPU (driver 617.42), Windows 11 Pro build 26300, scripts\build_runtime.ps1 (MSVC 19.51), Vulkan SDK 1.4.363: pass.

@Gabzinnn12

Copy link
Copy Markdown

Tested 9b17404 on AMD Radeon RX 7600 (RDNA3), AMD 26.11.2 (LLPC), Windows 11, MSVC 19.44, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8: pass.

  • replay_query_predication_test: exit 0 with and without LEONIDA_VK_VALIDATION=1 (+ VK_LAYER_VALIDATE_SYNC=1).
  • ctest: 0 failed out of 124 (118 passed, 6 skipped, gameinput disabled), including vk_overlapping_occlusion and d3d12_vulkan_device.
  • d3d12_vulkan_device, d3d12_com, d3d12_draw, d3d12_compute, d3d12_m2_textured, d3d12_m3_frame, d3d12_m4_compute, d3d12_translated_draw, d3d12_buffer_uav: exit 0 with and without validation, no Vulkan validation errors reported.

AI-assisted (Claude Code, reviewed with Codex). No game files.

@crastinator

Copy link
Copy Markdown
Contributor

Tested 9b17404 merged into main ec8ec4c9 (clean merge) on AMD Radeon RX 6700 XT (Navi 22, gfx1031, subgroup 32..64), RADV Mesa 26.3.1, CachyOS, Linux 7.3.5, GCC 16.2.1, llvm-mc 23.1.5: pass.

  • Whole run_gpu_tests.py: 175/189 native, 175/189 with LEONIDA_W64_EMULATE=1 (the skips are the D3D12-only fixtures and ps_w64x_readlane, as on main). ctest 37/37. vk_overlapping_occlusion and vk_binary_occlusion pass in ctest.

No game files. AI-assisted (Claude Code).

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.

9 participants