Skip to content

feat(dxgi): add IDXGIFactory5 tearing query, present-mode selection, and waitable swapchains - #1357

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
totonicpu:pr/dxgi-factory5-tearing-waitable
Nov 22, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
totonicpu:pr/dxgi-factory5-tearing-waitable

Conversation

@totonicpu

Copy link
Copy Markdown
Contributor

Summary

Adds IDXGIFactory5 tearing support, VkPresentModeKHR selection, waitable frame-latency swapchains, and D3D12-compliant initial backbuffer layout transitions in the Vulkan DXGI shim (runtime/shims/dxgi_com.{h,cpp}, runtime/shims/dxgi_swapchain.h):

  1. IDXGIFactory5::CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING) (dxgi_com.h, dxgi_com.cpp):
    • Upgrades Factory to IDXGIFactory5 (QueryInterface supports __uuidof(IDXGIFactory5)).
    • Implements CheckFeatureSupport for DXGI_FEATURE_PRESENT_ALLOW_TEARING (validating non-null pointer and sizeof(BOOL), returning E_INVALIDARG otherwise).
  2. VkPresentModeKHR selection and tearing validation (dxgi_swapchain.h, dxgi_com.cpp):
    • Loads vkGetPhysicalDeviceSurfacePresentModesKHR and selects VK_PRESENT_MODE_IMMEDIATE_KHR (falling back to MAILBOX then FIFO) when DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING is set, instead of always hardcoding FIFO.
    • Preserves swapchain Flags in GetDesc / GetDesc1 (and across ResizeBuffers), and enforces DXGI's Present(sync, DXGI_PRESENT_ALLOW_TEARING) rule (DXGI_ERROR_INVALID_CALL when sync != 0 or the swapchain was created without DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING).
  3. Waitable frame-latency swapchains (dxgi_com.cpp):
    • When DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT is set, initializes default max frame latency to 1 (matching DXGI), creates a Win32 semaphore (CreateSemaphoreW), returns a caller-owned DuplicateHandle copy from GetFrameLatencyWaitableObject(), releases on Present() and latency increases in SetMaximumFrameLatency(), and returns nullptr when the flag is not set.
  4. Initial backbuffer layout transitions (dxgi_swapchain.h, dxgi_com.cpp):
    • Transitions newly acquired native swapchain images once from VK_IMAGE_LAYOUT_UNDEFINED to VK_IMAGE_LAYOUT_PRESENT_SRC_KHR (and offscreen backbuffers to VK_IMAGE_LAYOUT_GENERAL on creation), matching D3D12's initial D3D12_RESOURCE_STATE_PRESENT state so presenting an untouched backbuffer passes Vulkan validation without VUID-VkPresentInfoKHR-pImageIndices-01430.
  5. GPU test coverage (runtime/test/dxgi_swapchain_test.cpp):
    • Covers IDXGIFactory5::CheckFeatureSupport (valid + invalid args), DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING validation, DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT (GetFrameLatencyWaitableObject, WaitForSingleObject, Present, CloseHandle), and presenting an untouched backbuffer under LEONIDA_VK_VALIDATION=1.

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

  • Full build in RelWithDebInfo and Debug: 0 errors, 0 new warnings.
  • dxgi_swapchain_test.exe (RelWithDebInfo + Debug): PASS (0 validation errors).
  • ctest -C RelWithDebInfo -E d3d12_clear_test: 67/67 passed (0 validation errors).
  • ctest -C Debug -E d3d12_clear_test: 67/67 passed (0 validation errors).

@evilclanker9000

Copy link
Copy Markdown
Contributor

On bf42427ed3d0b32eee187edcd67dbed5ba1941d4, a swapchain created without DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT still returns S_OK for SetMaximumFrameLatency(3), SetMaximumFrameLatency(1), and GetMaximumFrameLatency(&value). Native DXGI returned DXGI_ERROR_INVALID_CALL (0x887a0001) for all three calls on RTX 5080, integrated Radeon and WARP. The getter left a sentinel output untouched. The shim returned 1. This behavior predates the PR. Now that waitable chains are supported, the existing non-waitable test could check these HRESULTs alongside the null handle. Microsoft documents the setter's creation-flag requirement.

The positive controls matched on both Vulkan GPUs and all three native devices. On fresh three-buffer waitable HWND swapchains, I counted successful zero-timeout waits before the first Present. Default latency gave one, setting latency to 3 gave three, and setting it to 3 then immediately back to 1 still gave three, although the getter correctly reported 1. Native DXGI also retained those three already-issued signals. This checks observable handle behavior before presentation. It does not measure display latency.

@totonicpu

Copy link
Copy Markdown
Contributor Author

Good catch @Vonsek — fixed in 059b469.

  • SwapChain::SetMaximumFrameLatency and SwapChain::GetMaximumFrameLatency now return DXGI_ERROR_INVALID_CALL (and leave the output parameter untouched) when the swapchain was created without DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT, matching native DXGI.
  • Updated dxgi_swapchain_test.cpp to assert DXGI_ERROR_INVALID_CALL + untouched sentinel on the non-waitable swapchain and moved the latency 0/17/2 round-trip + initial semaphore count checks onto the waitable swapchain.
  • Re-tested dxgi_swapchain_test.exe in RelWithDebInfo and Debug on RTX 4070 Ti with LEONIDA_VK_VALIDATION=1 (0 validation errors).

ItsNotRexed added a commit that referenced this pull request Nov 22, 2026
@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, llvm-mc 23.1.6): pass. ctest: 67/67 on the RTX 4080 SUPER. On the Intel UHD 770 the 3 failures are known and unrelated (vk_binary_occlusion fixed by #1321, d3d12_descriptor min-LOD, d3d12_copy_xfmt).

@Dom-in

Dom-in commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Tested 059b469 merged onto main (c2967a5) on AMD Radeon RX 9070 (RDNA4), AMD 26.11.2, Windows 11 Pro 26300, MSVC 14.44: pass. This is an AMD data point next to the RTX 4070 Ti.

  • dxgi_swapchain_test: PASS. With LEONIDA_VK_VALIDATION=1 it reports device: AMD Radeon RX 9070 (validation on) and 0 validation errors.
  • Full ctest: 78/79. The one failure, d3d12_r9g9b9e5_uav, fails the same way on main on this GPU; d3d12: R9G9B9E5_SHAREDEXP never reports render-target, blend or MSAA support #1388 fixes it.
  • For the present-mode fallback, this AMD driver's surface reports IMMEDIATE, FIFO, FIFO_RELAXED and FIFO_LATEST_READY, and no MAILBOX. So the tearing path picks IMMEDIATE here, and the MAILBOX branch of the fallback is never reached on AMD Windows.

AI-assisted (Claude Code).

@ianneobot-cyber

Copy link
Copy Markdown

Tested 059b469 on Windows 11 (native, MSVC 17.14, Vulkan SDK 1.4.363), NVIDIA RTX 3070 Ti Laptop (621.14), LEONIDA_VK_VALIDATION=1: pass

One thing from the code read: Factory::CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING) (dxgi_com.cpp:776) answers TRUE whenever vkGetPhysicalDeviceSurfacePresentModesKHR resolves, which is on every Vulkan loader, not when a tearing mode is available. If the surface has no IMMEDIATE, CreateSwapChain (dxgi_swapchain.h:497) silently falls back to MAILBOX (which doesn't tear) or FIFO, so a game told tearing is supported gets vsync. Answering from the present modes of the adapter's surface (or TRUE only when IMMEDIATE exists) would match DXGI more closely; the test's unconditional !allow_tearing failure would then need to allow FALSE.

@totonicpu

Copy link
Copy Markdown
Contributor Author

Good catch @ianneobot-cyber — fixed in 1e5d0db (rebased onto current main alongside the non-waitable latency fix):

  • Factory::CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING) now probes actual VK_PRESENT_MODE_IMMEDIATE_KHR support via a temporary hidden STATIC window + VkSurfaceKHR + vkGetPhysicalDeviceSurfacePresentModesKHR (cached once via std::call_once and immediately cleaned up), returning TRUE only when IMMEDIATE is actually advertised by the driver.
  • Updated dxgi_swapchain_test.cpp so CheckFeatureSupport does not fail unconditionally on drivers/surfaces without IMMEDIATE.
  • Re-tested dxgi_swapchain_test.exe in RelWithDebInfo and Debug on RTX 4070 Ti with LEONIDA_VK_VALIDATION=1 (0 validation errors).

@totonicpu
totonicpu force-pushed the pr/dxgi-factory5-tearing-waitable branch from 059b469 to 1e5d0db Compare November 22, 2026 11:18
ItsNotRexed added a commit that referenced this pull request Nov 22, 2026
@kacper-serewis

Copy link
Copy Markdown
Contributor

Tested 059b469 merged onto current main (c2967a5) (clean merge) on RTX 5070 (Blackwell, subgroup 32..32), NVIDIA 616.56, Windows 11 Pro 26200, MSVC 19.50, Vulkan SDK 1.4.363.0: pass.

  • ctest 79/79, including dxgi_swapchain (passed)

@ianneobot-cyber

Copy link
Copy Markdown

Re-tested 1e5d0db on Windows 11 (native, MSVC 17.14, Vulkan SDK 1.4.363), NVIDIA RTX 3070 Ti Laptop (621.14), LEONIDA_VK_VALIDATION=1: pass

  • dxgi_swapchain_test: pass 3/3 runs, 0 validation errors
  • Full ctest: 79/79, 0 VUIDs (main bb2e184 on the same machine: also no failures)

Thanks for the fix: probing IMMEDIATE on a temporary surface, once, is what I had in mind, and the window and surface are cleaned up on every path. This driver advertises IMMEDIATE on its Win32 surfaces, so the answer here is TRUE.

Small optional follow-up: the test now only checks that the call succeeds. When it answers TRUE, it could also check that the ALLOW_TEARING swapchain really got VK_PRESENT_MODE_IMMEDIATE_KHR, so the query and the swapchain can't drift apart again.

@Yourrespawn

Copy link
Copy Markdown
Contributor

Tested 1e5d0dbe on NVIDIA GeForce RTX 4080 (Ada), driver 617.14, Vulkan API 1.4.351, Windows 11 Pro 10.0.26200: pass.

  • dxgi_swapchain_test with LEONIDA_VK_VALIDATION=1 (Core + Synchronization): PASS, 0 validation errors
  • ctest RelWithDebInfo: 79/79 (gameinput disabled, no SDL3)
  • run_gpu_tests.py native: 106/135 passed, 29 skipped (native wave64, expected on this 32-lane GPU)
  • run_gpu_tests.py LEONIDA_W64_EMULATE=1: 135/135 passed

The branch sits on current main (bb2e1842), so counts are higher than my #166 entry from f85582e2; no failures anywhere. AI-assisted (ZCode agent): builds and runs executed by a coding agent on this machine.

@ItsNotRexed
ItsNotRexed merged commit 48389a8 into ItsNotRexed:main Nov 22, 2026
9 of 10 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.

8 participants