Skip to content

Runtime: share reserved sparse buffers across queues and select sparse-capable families - #3652

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
pmilewsskiedu-gif:fix/sparse-buffer-queue-family-20261122
Nov 23, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
pmilewsskiedu-gif:fix/sparse-buffer-queue-family-20261122

Conversation

@pmilewsskiedu-gif

Copy link
Copy Markdown
Contributor

D3D12 reserved sparse buffers are shared across DIRECT, COPY and COMPUTE queues. Fix VkBuffer sharingMode EXCLUSIVE without ownership transfers by applying existing concurrent sharing across active queue families; fall back to sparse-capable graphics family only when dedicated compute/copy lacks VK_QUEUE_SPARSE_BINDING_BIT. Synthetic regression: native WARP 3/3 legal mappings, RTX Vulkan layer 3/3 exact both old and new (no observed corrupt bytes or VUID); LunarG vkCreateBuffer API-dump old EXCLUSIVE count0 vs fixed CONCURRENT count3 indices 0,2,1 with deterministic exit1 baseline/exit0 fixed. Focused CTests 3/3 pass and queue picker 7 synthetic layouts pass. No full suite rerun; queue fallback is synthetic-only on hardware with missing sparse capability. Commit 389f949, based f847748, 6 files +363/-2. Ownership claimed first at #3574 (comment). Distinct from #2382 sparse images and #2951 wait-before-signal. AI assistance disclosure: original implementation and synthetic verification were AI-assisted; no proprietary game files, shaders, assets or executables included; no authentic game execution claimed.

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

Tested 389f949b on NVIDIA GeForce RTX 2070 SUPER (TU104, Turing), driver 595.79, Windows 11 Pro x64, Vulkan 1.4.329, MSVC 19.44 (VS 2022 Community), Ninja 1.13.

1. GPU Queue & Sparse Replay Tests (PASS)

  • vk_queue_test.exe: PASS (exit 0). Queue family picker reported 7 layouts OK; copy -> direct and direct -> compute passed with 0 bad bytes on GCT (direct 0), T (copy 1), CT (compute 2).
  • d3d12_sparse_queue_family_test.exe: PASS (exit 0):
    • case=LAYER_MAP_DIRECT_COPY_COMPUTE: 0 mismatches / 65536, 0 unmapped nonzero.
    • case=LAYER_MAP_COPY_COPY_COMPUTE: 0 mismatches / 65536, 0 unmapped nonzero.
    • case=LAYER_MAP_COMPUTE_COPY_COMPUTE: 0 mismatches / 65536, 0 unmapped nonzero.
    • LAYER_FINAL PASS.
  • CTest d3d12_sparse_queue_family: Passed (0.76s).

2. Defect in CTest wrapper check_sparse_queue_family.py

CTest test 76 (d3d12_sparse_queue_family_contract) fails with exit 1 on hosts without VK_LAYER_LUNARG_api_dump installed in the system layer registry:

  • The docstring states: "If the API dump layer is absent the result skips (exit 77, CTest SKIP_RETURN_CODE)".
  • However, check_sparse_queue_family.py sets env['VK_INSTANCE_LAYERS'] = 'VK_LAYER_LUNARG_api_dump'. When this layer is absent, vkCreateInstance returns VK_ERROR_LAYER_NOT_PRESENT and create_vulkan_device_standalone exits with code 2 (FAIL Leonida create_vulkan_device_standalone hr=0x80004005).
  • Because run.returncode == 2 (not 77), line 82 raises AssertionError(f'Leonida GPU test failed exit={run.returncode}'), turning what should be a clean skip into a hard test failure in CTest.
  • Fix suggestion: In check_sparse_queue_family.py, check if device initialization failed due to missing injected layer and return 77 so CTest properly skips when LunarG API dump is not present.

@kacper-serewis kacper-serewis 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.

At 389f949 the trace checker rejects valid EXCLUSIVE common-family buffers and accepts CONCURRENT [88,99] on a synthetic device exposing families 0–2. Derive the active set and check the actual indices. Also include residencyNonResidentStrict in the queue fallback predicate at d3d12_vulkan_device.cpp:7531 to match initialization at line 4012. Otherwise sparse-disabled devices can lose dedicated queues. Seven supplied layouts and eight extra capability masks checked on CPU. AI-assisted.

@MKmajster

Copy link
Copy Markdown

Tested 389f949 on NVIDIA GeForce RTX 3070 (Ampere), driver 617.42, Windows 11 24H2 (26100), MSVC 19.44 RelWithDebInfo + Ninja: the GPU part passes, but the new d3d12_sparse_queue_family_contract CTest fails on a machine without the Khronos validation layer instead of skipping.

  • d3d12_sparse_queue_family_test.exe on its own: LAYER_FINAL PASS, all three LAYER_MAP_{DIRECT,COPY,COMPUTE}_COPY_COMPUTE cases mapped_mismatch=0/65536 unmapped_nonzero=0/65536. Same with LEONIDA_VK_VALIDATION=1 and VK_LAYER_KHRONOS_validation 1.4.350 on the loader path: pass, no validation errors.
  • vk_queue_query_sync, d3d12_queue_wait, d3d12_queue_wait_timeout, d3d12_sparse_queue_family, queue_proxy: pass.
  • Full CTest: 142/143, the one failure is d3d12_sparse_queue_family_contract.

Why the contract test fails: check_sparse_queue_family.py always sets LEONIDA_VK_VALIDATION=1. Where VK_LAYER_KHRONOS_validation isn't installed, create_vulkan_device_standalone asks for it anyway, vkCreateInstance fails, the test exe exits 2, and the script reports FAIL: Leonida GPU test failed exit=2. The docstring says a missing layer should give 77.

Environment Script result
no validation layer, no api_dump FAIL (exit 1): [rt] d3d12: vkCreateInstance (Vulkan 1.3) failed, LAYER_FATAL
validation layer present, no api_dump SKIP 77 ("api_dump unavailable"), as intended
VK_INSTANCE_LAYERS=VK_LAYER_LUNARG_api_dump alone, layer absent exe still passes (the loader ignores the missing env layer)

So VK_INSTANCE_LAYERS is harmless. The forced LEONIDA_VK_VALIDATION=1 is what turns a missing layer into a failure. Possible fixes: set it only when VK_LAYER_KHRONOS_validation is enumerable (or when api_dump is, since the CONCURRENT check needs it anyway), or map the exe's LAYER_FATAL ... vkCreateInstance exit to 77. Without one of these, a full ctest fails for anyone without the Vulkan SDK's layers. That includes people who only use the pinned Vulkan-Headers (-DLEONIDA_VULKAN_HEADERS_FETCH=ON), as on this machine.

Not checked: the CONCURRENT/EXCLUSIVE api_dump contract itself, since api_dump isn't installed here.

No game files. AI-assisted (Claude).

@MatyEjsr

Copy link
Copy Markdown

Tested 389f949b on NVIDIA GeForce RTX 3090 (Ampere, driver 32.0.16.1742 / 617.42), Windows 11 (10.0.26200), VS 2022 Build Tools (MSVC 19.44.35230.0, Ninja RelWithDebInfo), Windows SDK 10.0.26100.0, Vulkan SDK 1.4.363.0: pass.

  • Clean scripts\build_runtime.ps1 build (153 actions): exit 0.
  • Targeted ctest -R ^(vk_queue_query_sync|d3d12_buffer_tiles|d3d12_sparse_queue_family|d3d12_sparse_queue_family_contract)$ (validation off): 4/4 pass:
    • d3d12_sparse_queue_family_contract confirmed GPU PASS: 3 COPY/COMPUTE sparse queue cases, mapped 65,536 bytes exact, unmapped 65,536 zeros and VK API CONTRACT PASS: 4 sparse VkBuffer creations / 8 create-info records = CONCURRENT with 2+ unique families.
  • Targeted ctest -R ^(vk_sync_validation|vk_queue_query_sync|d3d12_buffer_tiles|d3d12_sparse_queue_family|d3d12_sparse_queue_family_contract)$ with LEONIDA_VK_VALIDATION=1 and VK_LOADER_LAYERS_DISABLE=~implicit~: 5/5 pass, vk_sync_validation confirmed active, 0 validation errors, API-dump contract check passed (0.69s).
  • Full ctest (validation off, no game files): 134 passed, 8 skipped, 1 disabled (gameinput), 0 failed (116.9s).

Code read of runtime/src/queue_families.h and runtime/shims/d3d12_vulkan_device.cpp looks clean: sparse_buffer_info() reuses share(result) for CONCURRENT sharing across active queue families, and sparse_capable_queue_families() falls back to the sparse-capable graphics family only when sparse_buffers is enabled and a dedicated compute/copy family lacks VK_QUEUE_SPARSE_BINDING_BIT.

AI-assisted.

@Sc4ros

Sc4ros commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Tested 389f949 (based on current main f847748) on AMD Radeon RX 9060 XT (RDNA4, driver 32.0.32029.2010, Vulkan 1.4.359; subgroup 32..64, upload heaps in VRAM with Resizable BAR), Windows 11 build 26300, MSVC 19.44 (Visual Studio generator, Release), Vulkan SDK 1.4.363.0: the GPU part passes, the d3d12_sparse_queue_family_contract test fails 5 times out of 8 on this machine, and the cause is not this PR.

  • Build of d3d12_sparse_queue_family_test, vk_queue_test, d3d12_buffer_tiles_test, vk_queue_query_sync_test: OK.
  • ctest -R "sparse|vk_queue|buffer_tiles": vk_queue_query_sync, d3d12_buffer_tiles and d3d12_sparse_queue_family pass, with LEONIDA_VK_VALIDATION unset and set to 1. d3d12_sparse_queue_family_test.exe run on its own 8 times without the API dump layer (validation off and on): 16/16 pass.
  • d3d12_sparse_queue_family_contract fails with FAIL: Leonida GPU test failed exit=3221225477 (0xC0000005, an access violation in the test process) in 5 of 8 direct runs of check_sparse_queue_family.py, and it passed once under ctest with validation on, so it is intermittent.
  • Where the crash comes from: the same 8-run loop with VK_INSTANCE_LAYERS=VK_LAYER_LUNARG_api_dump and the script's layer settings gives 3/8 ok for the PR's test, 1/8 ok for d3d12_buffer_tiles_test and 4/8 for d3d12_queue_wait_test, two tests this PR does not touch. Without the API dump layer none of them crashed in any run (8/8). So on this AMD driver the SDK's API dump layer crashes the process often, and the contract test inherits it.
  • Suggestion: this makes the contract test unreliable on AMD, and it is a hard failure rather than a skip (the same shape as MKmajster's report, with the layer present instead of absent). Treating a crash of the child under the API dump layer as a skip, or running the byte check and the trace in separate processes so the byte check cannot be lost to the layer, would fix both.
  • Not checked: which queue families this driver reports sparse binding on, so I cannot say whether the dedicated-queue fallback path ran here.

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

@Mayzkl

Mayzkl commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested on Windows 11 (26200), NVIDIA GeForce RTX 5060 (Blackwell, 8 GB), driver 617.14, MSVC 14.44, Vulkan SDK 1.4.363.1 (with VK_LAYER_KHRONOS_validation and VK_LAYER_LUNARG_api_dump), llvm-mc 21.1.8: pass. Head 389f949 (contains main f847748).

This machine has api_dump, so it covers the part @MKmajster couldn't check: d3d12_sparse_queue_family_contract runs its API contract here instead of skipping:

GPU PASS: 3 COPY/COMPUTE sparse queue cases, mapped 65,536 bytes exact, unmapped 65,536 zeros
VK API CONTRACT PASS: 4 sparse VkBuffer creations / 8 create-info records = CONCURRENT with 2+ unique families

That is the checker as written. It doesn't address @kacper-serewis's point that the checker accepts out-of-range CONCURRENT indices and rejects valid EXCLUSIVE ones, since this device takes neither path.

  • Full runtime build: OK; no new compiler warnings (only the four that main already has).
  • Full ctest: 142/142 pass, including both new tests and the translated-shader tests (LLVM_MC set).
  • With LEONIDA_VK_VALIDATION=1: d3d12_sparse_queue_family, d3d12_sparse_queue_family_contract, d3d12_buffer_tiles, d3d12_vulkan_device, vk_queue_query_sync: 5/5, no validation errors.

AI-assisted check (Claude Code).

@crastinator

Copy link
Copy Markdown
Contributor

Tested 389f949 merged into main f847748c (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: 185/199 native, 185/199 with LEONIDA_W64_EMULATE=1 (skips: the D3D12-only fixtures and ps_w64x_readlane, as on main). ctest 41/41.

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

@MeNoSkills

MeNoSkills commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Tested 389f949b merged into current main aa2988cc (clean merge) on Windows 10 Pro 19045, MSVC 2022, Vulkan SDK 1.4.363.0 (its api_dump layer installed), two adapters selected with LEONIDA_VK_DEVICE:

ctest RTX 4070 Ti (621.24) AMD Radeon Graphics iGPU, Ryzen 7 9700X (24.30.18)
d3d12_sparse_queue_family pass pass
d3d12_buffer_tiles, queue_proxy, vk_queue_query_sync, d3d12_queue_handover pass pass
d3d12_sparse_queue_family_contract pass (4 sparse VkBuffer / 8 records = CONCURRENT, 2+ families) FAIL, exit=3221225477 (0xC0000005), failed every time I ran it (3 of 3: once under ctest, once through the script, once with the two layers set by hand)

(d3d12_vulkan_device fails on both here, but that is the Windows 10 host cases of #3557, not this PR.)

The AMD failure is not about the sparse logic. Running d3d12_sparse_queue_family_test.exe directly on the iGPU:

  • plain: exit 0; LEONIDA_VK_VALIDATION=1 only: exit 0; VK_INSTANCE_LAYERS=VK_LAYER_LUNARG_api_dump only: exit 0;
  • both together, which is what check_sparse_queue_family.py sets (lines 69-70): 0xC0000005. On the RTX 4070 Ti the same combination is fine.

With a copy of the script that does not set LEONIDA_VK_VALIDATION=1, the contract check passes on the AMD iGPU too: VK API CONTRACT PASS: 4 sparse VkBuffer creations / 8 create-info records = CONCURRENT with 2+ unique families. So api_dump alone is enough to read the vkCreateBuffer records, and the extra validation layer only adds the crash on AMD's driver. This matches what @Sc4ros saw on an RX 9060 XT (fails on AMD, 5 of 8) and @MKmajster's skip-vs-fail point. Suggestion: drop env["LEONIDA_VK_VALIDATION"] = "1" from that checker (the plain d3d12_sparse_queue_family test already runs with a Validation Error;VUID- fail pattern), or skip with 77 when the traced process crashes before the trace is written.

I did not check @kacper-serewis's two review points (the active-set check and residencyNonResidentStrict in the fallback predicate). 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.

10 participants