Skip to content

perf(vulkan): restore parallel prepare() with lazy depth24 lock and native-D24 skip - #1230

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
totonicpu:pr/perf-parallel-prepare-depth24
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
totonicpu:pr/perf-parallel-prepare-depth24

Conversation

@totonicpu

Copy link
Copy Markdown
Contributor

Summary

Restores concurrent prepare() across VulkanQueue::execute() calls (following up on the depth24_ serialization introduced in #927) on both native-D24 and emulated-D24 GPUs:

  1. Skip enable_depth24() on native-D24 GPUs (runtime/shims/d3d12_vulkan_device.cpp): Device previously called state_.context->enable_depth24(...) unconditionally, even when state_.depth_usable(VK_FORMAT_D24_UNORM_S8_UINT) is true (where d24_as_float is never set). Now enable_depth24() is only invoked when the GPU lacks native D24_UNORM_S8_UINT.
  2. Lazy tracker_mutex_ acquisition for depth24_prepare (runtime/src/vulkan_backend.h): When context_.depth24_ is enabled (e.g., GPUs without native D24), VulkanQueue::execute() wraps replay.depth24_prepare to lazily acquire tracker_mutex_ and call collect() only if a command list actually copies a depth24_as_float texture. All other batches run prepare(), QueryReplay::plan(), OcclusionResolve::prepare(), acquire(), and vkBeginCommandBuffer() concurrently under std::shared_lock(resource_mutex_).
  3. RAII early-failure retirement & bindless ordering (runtime/src/vulkan_backend.h):
    • Depth24Guard (declared after tracker_lock) retires uncommitted depth24_ scratch/pools with ticket 0 while tracker_lock is still held if prepare() or setup fails after an earlier depth24_prepare call.
    • tracker_lock is acquired right before bindless_prepare_() (and held through submit() on both success and failure paths) so bindless fallback initialization / descriptor-table flushes cannot be submitted out of order across concurrent queues once prepare() runs in parallel.
  4. Concurrent regression test (runtime/test/depth24_convert_test.cpp): Adds step 5 exercising two concurrent VulkanQueues on a VulkanContext with enable_depth24() active — one running depth24_as_float CopyTexture upload/readback roundtrips (lazy lock + collect()/retire()), one running regular CopyBuffer batches concurrently (parallel prepare()), plus an early-failure batch verifying Depth24Guard retirement.

Validation (NVIDIA GeForce RTX 4070 Ti, LEONIDA_VK_VALIDATION=1 — Core + Synchronization Validation)

  • Full build in RelWithDebInfo and Debug: 0 errors, 0 new warnings.
  • ctest -C RelWithDebInfo -E d3d12_clear_test: 59/59 passed (0 validation errors).
  • ctest -C Debug -E d3d12_clear_test: 59/59 passed (0 validation errors).
  • depth24_convert_test.exe (RelWithDebInfo + Debug): PASS (all 5 steps, 0 validation errors).
  • d3d12_multithread_test.exe (5x RelWithDebInfo + Debug): PASS (0 validation errors).

@kapitche

Copy link
Copy Markdown
Contributor

Tested on Windows 11 with two GPUs on one machine (runtime built from this PR's head, MSVC 14.51):

  • ctest: 59/59 on the RTX 4080 SUPER. On the Intel UHD 770, the 3 failures (d3d12_vulkan_device d3d12_vulkan_device_test: compare OPTIONS1 with the same GPU's host device, not the default adapter #962, d3d12_descriptor min-LOD, d3d12_copy_xfmt) also fail on main.
  • depth24_convert_test (not in ctest) on NVIDIA: ok, including concurrent VulkanQueue::execute (lazy depth24 lock + parallel non-d24 prepare) ok.
  • On Intel it fails: to words: f=7fc00000 got 16777215 want 0 (and every other NaN pattern), line 152: wrong == 0. That's not from this PR: a build of current main fails the same check (line 149 there). The words→float direction and the round trip pass on Intel.

Cause, I think: depth24_from_float in runtime/src/depth24_convert.comp relies on if (!(f > 0.0)) return 0u; to send NaN to 0. Intel's compiler seems to treat that compare as NaN-free, so NaN falls through to f >= 1.0 / uint(...) and becomes 0xffffff. A bit test that doesn't depend on ordered-compare semantics, e.g. if ((floatBitsToUint(f) & 0x7fffffffu) > 0x7f800000u) return 0u; before the compares, would be robust. I'll note it in #634 rather than block this PR on it.

@evilclanker9000

Copy link
Copy Markdown
Contributor

Tested 0ba4345 with two concurrent VulkanQueue wrappers sharing a context and each round-tripping its own 64×16 emulated D24 image. I kept the failing pre-submit batch, changed the second stream from a buffer copy to a depth24 copy, and gave both queues different, iteration-specific depth words and a distinct readback range for each submission. The expectation was the original low 24 bits of each input word.

Both RTX 5080 (616.92) and Radeon Graphics (32.0.21045.16010) returned 16384/16384 matching depth words per queue across 16 submissions each with core and synchronization validation enabled. Negative control: feeding only queue B the previous iteration's upload yielded 16384 wrong words on B and zero wrong words on A on both GPUs. This checks content isolation across the two conversion streams. Throughput was not measured.

AI-assisted; synthetic data only.

@Urban225

Copy link
Copy Markdown
Contributor

Tested 0ba4345 on AMD Radeon RX 7900 XTX (RDNA3, subgroup 32..64), AMD proprietary 26.10.1, Windows 11, MSVC 19.44: pass.

  • Build with scripts\build_runtime.ps1: ok.
  • depth24_convert_test.exe, run directly since it is not a ctest entry: ok, including the new step:
    depth24: concurrent VulkanQueue::execute (lazy depth24 lock + parallel non-d24 prepare) ok
    depth24_convert_test: ok
    
    Same result with LEONIDA_VK_VALIDATION=1 ("validation on"), no validation error. About 5 s per run.
  • ctest --test-dir runtime\build: 59/59 pass (gameinput disabled, no SDL3).
  • python -I scripts\run_gpu_tests.py: 116/116.

For comparison, main at 426023a on the same machine: ctest 61/61, run_gpu_tests 118/118; the different totals are this branch's older base.

Not covered: I did not measure the performance gain the PR is about, only correctness.

AI-assisted (Claude wrote the test script and read the logs); the runs are from my machine.

@totonicpu
totonicpu force-pushed the pr/perf-parallel-prepare-depth24 branch from 0ba4345 to 1c5463b Compare November 22, 2026 09:35
@ItsNotRexed
ItsNotRexed merged commit c50059a into ItsNotRexed:main Nov 22, 2026
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.

5 participants