Skip to content

Normalize binary occlusion results in Vulkan replay - #889

Merged
ItsNotRexed merged 4 commits into
ItsNotRexed:mainfrom
gabrielsteglich:fix/binary-occlusion-resolve
Nov 22, 2026
Merged

ItsNotRexed merged 4 commits into
ItsNotRexed:mainfrom
gabrielsteglich:fix/binary-occlusion-resolve

Conversation

@gabrielsteglich

Copy link
Copy Markdown
Contributor

D3D-15 follow-up claimed in coordination issue #634.

What and why

D3D12 binary occlusion returns a 64-bit 0 or 1. Vulkan can return a sample count even without the precise flag. Replay now resolves binary results in chunks into a queue-owned scratch buffer, normalizes both words on the GPU, and copies the results into the application buffer. Ordinary occlusion still returns the sample count.

A binary resolve remains unconditional under predication. Replay suspends conditional rendering around the resolve and restores it afterward. Binary resolves require a compute-capable graphics queue.

Pipeline-statistics limitation

Replay refuses a binary resolve while a pipeline-statistics query is active. The internal dispatch would otherwise be counted in the application's CSInvocations. Preparation rejects the list before submission.

Tested

Tested cce77b6 against 4a36bb1 on Windows 11 build 26200 with an NVIDIA GeForce RTX 3090, driver 621.01, Vulkan SDK 1.4.363.0, and MSVC 19.44. The test seeds the query pool with synthetic Vulkan draws and resolves the results through the Leonida replay queue.

Case Result
Baseline negative control main returned 64 where binary occlusion requires 1
Binary replay 1,026 visible results and three repeated results returned 1. The empty query and two unused slots returned 0
Ordinary occlusion Preserved the sample count of 64
Replay controls Checked offsets, untouched sentinel words, scratch reuse across consecutive submissions, a false predicate, invalid alignment, and the active statistics rejection
  • vk_binary_occlusion passed with Vulkan and synchronization validation enabled, and again with validation disabled.
  • vk_command_replay_test and d3d12_vulkan_device passed with Vulkan validation enabled.

Checklist

  • Synthetic inputs only. No game data, keys, or decryption.

AI-assisted (Codex).

@kapitche

Copy link
Copy Markdown
Contributor

Tested cce77b6 on Windows 11, NVIDIA GeForce RTX 4080 SUPER (driver 32.0.16.1714), MSVC 14.51: pass

  • ctest: 33/33 passed, including the new vk_binary_occlusion:
    binary occlusion: 1026 visible, 3 zero, 3 repeated; ordinary result 64; offsets and sentinels exact
  • So binary queries come back as 0/1, and an ordinary query still returns the raw sample count (64).

One thing builders should know: vk_binary_occlusion_test is only built when glslangValidator is found (runtime/CMakeLists.txt ~561). My first build had none on PATH, and ctest reported 32/32 with the new test simply missing, with no skip and no message. With -DVulkan_GLSLANG_VALIDATOR_EXECUTABLE=... it built and ran. A message(STATUS ...) when it's left out, as the Vulkan-headers check does, would make that visible.

@quackottu quackottu 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.

Tested cce77b6 on NVIDIA GeForce RTX 5070, driver 621.24, Windows 11 Pro (26300), VS 2022 Build Tools, Vulkan SDK 1.4.363.0, with LEONIDA_VK_VALIDATION=1 and LEONIDA_D3D12=vulkan: pass

  • ctest -C RelWithDebInfo: 100% tests passed, 0 tests failed out of 33
  • vk_binary_occlusion_test (changed here): exit=1 validation=0 | line 262: argc == 3
  • 32 test programs in the build pass with 0 validation errors
  • Not run (they take an input file, not covered here): vk_binary_occlusion_test
  • d3d12_clear_test fails (R8G8B8A8_UNORM/SNORM 0.5 ties and a D16 clear byte differ from WARP) exactly as on main 4a36bb1 here, so not from this PR; #521 addresses it

Implicit Vulkan layers (overlays) were off through an empty VK_IMPLICIT_LAYER_PATH.

@Parakeet-Wheat

Copy link
Copy Markdown
Contributor

Tested cce77b6 on an NVIDIA GeForce RTX 3070, driver 610.88, Windows 11 (26300), MSVC 19.44 (VS generator, RelWithDebInfo), Vulkan SDK 1.4.363.0, with LEONIDA_VK_VALIDATION=1: pass

  • Build passes. The only warnings are main's C4456 at vulkan_replay.h:1378. ctest: 27/27.
  • vk_binary_occlusion_test with validation on: 1026 visible, 3 zero, 3 repeated; ordinary result 64; offsets and sentinels exact, exit 0, with 0 validation and 0 synchronization-hazard messages. That makes three NVIDIA generations (Ampere, plus the 3090 and 4080 SUPER results above).
  • d3d12_vulkan_device_test (0 of 69 copy cases differ), vk_command_replay_test A to F and d3d12_com_test: pass, with no validation messages.

Code read, as the author of #258:

  • binary_occlusion_spirv.h matches its source word for word: glslangValidator -V --target-env vulkan1.0 -Os -g0 on binary_occlusion.comp gives the same 243 words.
  • Ending conditional rendering around the binary resolve and restoring it from the saved begin info is right: ResolveQueryData isn't predicated in D3D12, and the dispatch otherwise would be.
  • Worth a line in the description: emit() now adds p.binding.buffer_offset to the resolve offset for every query type, and so does the fill_buffer path for never-ended queries. On main, a resolve into a buffer bound at a non-zero offset wrote at the wrong address, so this also fixes ordinary occlusion, timestamp and statistics resolves.

Two notes for whoever rebases or builds on this:

AI-assisted (Claude Code).

@UlteriorMotive00

Copy link
Copy Markdown
Contributor

Tested cce77b6 on AMD RDNA2 iGPU and RTX 5070: pass (first AMD and Blackwell runs; the PR was tested on an RTX 3090).

Windows 11 Home 10.0.26300, MSVC 14.44.35207, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, Ninja RelWithDebInfo (scripts\build_runtime.ps1), LEONIDA_VK_VALIDATION=1, VK_LOADER_LAYERS_DISABLE=~implicit~. One PC, two GPUs, picked with VK_LOADER_DRIVERS_SELECT: AMD Radeon iGPU of a Ryzen 7 9800X3D (RDNA2, AMD 2026.04.17) and NVIDIA GeForce RTX 5070 (Blackwell, 617.42).

  • vk_command_replay_test (run directly): exit 0 on both GPUs, no VUID messages, ending with F: immediate writes in three modes, UINT and UINT64 atomic copies, buffer and texture discards.
  • ctest: RTX 5070 33/33; AMD iGPU 32/33. The AMD failure is the base's OPTIONS19.SupportedSampleCountsWithNoOutputs mismatch (since fixed on main), not this PR. The branch is 356 commits behind main.

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

@Parakeet-Wheat

Copy link
Copy Markdown
Contributor

Retested after both merges: 532f854 on an NVIDIA GeForce RTX 3070, driver 610.88, Windows 11 (26300), MSVC 19.44 (VS generator, RelWithDebInfo), Vulkan SDK 1.4.363.0, with LEONIDA_VK_VALIDATION=1: still passes

  • Build has 0 warnings. ctest (validation on): 45/45, including vk_binary_occlusion and replay_cpu_bench_selftest. I also ran 319c860 (before the second merge): 42/42, same results.
  • vk_command_replay_test A to G and d3d12_com_test: exit 0, 0 validation messages.
  • d3d12_vulkan_device_test: exit 0. Its only validation error is main's VUID-vkCreateDevice-ppEnabledExtensionNames-01387, which Runtime: the standalone Vulkan instance keeps its surface extensions next to debug utils #1027 fixes.

What changed since my run on cce77b6: the code commit is the same, so the new parts are the two merges and 319c860. I read both merges with --remerge-diff, and the resolutions look right:

  • First merge, d2b9d9d:
    • emit() gets main's &open_labels, and close_labels uses your local replay copy, which carries resolve_binary_occlusion.
    • main's conditional_rendering flag is replaced by predicate.buffer everywhere, with nothing left over. The depth-24 lambda and your binary resolve both restart the predicate from it after their dispatches.
    • The timestamp period and set_binary_occlusion_memory share one if before main's debug-utils block. CMake keeps vk_device_pick_test and the d3d12_descriptor test, and drops only the duplicate glslangValidator lookup.
  • Second merge, 532f854: your descriptor and pipeline entries follow feat(d3d12): add ExecuteIndirect CountBuffer and optional BLAS/TLAS/RayQueryKHR support #743/Replay: a buffer UAV clear writes its resource's range of a shared VkBuffer (buffer_offset) #990's indirect and acceleration-structure ones in LEONIDA_VK_DEVICE_FUNCTIONS, with no name listed twice. The binary resolve's predicate restart closes its branch before the new ExecuteIndirect and acceleration-structure-build branches.

319c860 gives the benchmark the resolve_binary_occlusion callback it now needs, since prepare refuses a binary resolve without one. It's test-only.

Still true: this and #940 conflict with each other (d3d12_vulkan.md, d3d12_vulkan_device.cpp, vulkan_backend.h, vulkan_replay.h), so whichever lands second needs one more merge.

AI-assisted (Claude Code).

@ItsNotRexed
ItsNotRexed merged commit 6d25ad0 into ItsNotRexed:main Nov 22, 2026
10 of 11 checks passed
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.

6 participants