Skip to content

dxgi: wait for vertical blank on the requested monitor - #8530

Closed
LukeOsland1 wants to merge 3 commits into
ProjectLeonida:mainfrom
LukeOsland1:codex/dxgi-output-vblank
Closed

LukeOsland1 wants to merge 3 commits into
ProjectLeonida:mainfrom
LukeOsland1:codex/dxgi-output-vblank

Conversation

@LukeOsland1

Copy link
Copy Markdown
Contributor

Depends on: #8482

Coordination claim.

A client waiting on the 120 Hz secondary monitor woke twice per refresh. The shim used a compositor-wide DwmFlush for every output. Resolve the monitor's WDDM display source and call D3DKMTWaitForVerticalBlankEvent on that source. Keep the existing fallback if the monitor or driver wait is unavailable. This follow-up shares the adapter-handle lifetime and monitor resolution introduced in #8482.

The regression compares median intervals over 17 consecutive waits on each attached output against native DXGI. Two native samples bracket the shim run to detect unstable host timings.

Output on RTX 4070 Ti SUPER Parent shim / native Fixed shim / native
240 Hz 4.17 / 4.16 ms 4.16 / 4.17 ms
120 Hz 4.18 / 8.33 ms (failed) 7.82 / 8.33 ms (passed)

Parent runtime b7e39dc and fixed runtime c7a5615, same test. The validation-enabled Windows swapchain suite passed on the RTX (driver 32.0.16.1088) and integrated AMD Radeon (32.0.21042.62) with zero validation errors. The Radeon has no attached monitor on this PC, so only the RTX exercised the physical timing comparison.

AI-assisted with Codex. Synthetic runtime test. No display settings changed.

@MartinIndra02

Copy link
Copy Markdown
Contributor

Independent build + test: PASS (single 60 Hz monitor, so the multi-monitor case isn't exercised)

AI-assisted (Claude Code). Windows 11 x64, MSVC 19.44.35220, Ninja RelWithDebInfo, NVIDIA GeForce RTX 3080 driver 621.24 / 32.0.16.2124, one 60 Hz monitor attached.

Head c7a56159f (includes #8482's b7e39dc0d), merged locally with current main 867d922f0 (clean merge).

Test Result
dxgi_swapchain_test.exe exit 0: WaitForVBlank: native 16.67 ms, shim 16.67 ms, dxgi_swapchain_test: PASS (... ResizeBuffers; 0 validation errors)
dxgi_device_test.exe exit 0, 3 backend(s) passed

With one output, the new per-source D3DKMTWaitForVerticalBlankEvent path matches native on this machine, but nothing here can show the 120 Hz secondary-monitor fix from the description.

Note: wait_for_vblank now calls D3DKMTOpenAdapterFromGdiDisplayName and D3DKMTCloseAdapter on every WaitForVBlank, i.e. once per frame for a title that paces with it. The median matches here, but caching the adapter handle and VidPn source per Output (and reopening only when the wait fails) would keep two kernel calls out of every frame. No issues found.

@lohant27-netizen

lohant27-netizen commented Nov 23, 2026 •

Copy link
Copy Markdown

Tested c7a56159f on Windows 11 (10.0.26300), NVIDIA GeForce RTX 5070 (driver 32.0.16.2101, subgroup 32), MSVC with SDK 10.0.26100, Release, merged onto main 867d922f0 together with #8542, #8529, #8517, #8513, #8512 and #8530 (no conflicts): pass.

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

The monitor selection is correct (WaitForVBlank(info_), with_display_source, shim/native outputs matched by HMONITOR), but the new cadence regression does not pin the requested monitor:

  • same_vblank_cadence overwrites the shim's stable with the host sample (runtime/test/dxgi_swapchain_test.cpp:89-90); the shim's stable is never consulted. An intermittent wrong-monitor wait (e.g. a failed per-monitor wait falling back to DwmFlush on some samples) can still pass the 30% median, and a same-rate secondary monitor is invisible.
  • When the first native sample fails/is unstable/under 1 ms, or the second is unstable or the median changes >10%, the test returns true and skips the shim comparison entirely; a busy desktop can mask a shim regression.
  • D3DKMTWaitForVerticalBlankEvent (runtime/shims/dxgi_com.cpp:942-951) has no timeout; a stopped/off/disabled source can block forever, since the DwmFlush fallback is reached only on an error return. The fallback is the compositor cadence, not the requested monitor, and hEvent is zero (:947).

Fix: check the shim's stable, add a same-rate/wrong-monitor and a stopped-source case, bound the wait with a timeout/watchdog, and test the fallback path. Synthetic/static only; no game files. AI-assisted (OpenCode).

@dragoso

dragoso commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested a4804d486 merged onto main 867d922f0 (clean merge) on Windows 11 (build 26300), MSVC 14.51.36231, Ninja RelWithDebInfo, Vulkan SDK 1.4.363.0, NVIDIA GeForce RTX 5060 Ti (driver 32.0.16.1742) + AMD Radeon iGPU: pass.

  • ctest -R "^dxgi_swapchain": 2/2 pass. dxgi_swapchain passed in 4.1 s with 0 validation errors, and dxgi_swapchain_count passed too.
  • Two monitors attached, both 1920×1080 at 60 Hz. The new cadence check ran on both outputs: WaitForVBlank: native 16.67 ms, shim 16.67 ms each time, with no SKIP cadence comparison.
  • Limit: both outputs run at the same rate here, so this doesn't reproduce the 240/120 Hz case in the description. It confirms the per-output wait doesn't change the cadence on a same-rate multi-monitor setup, and that the new head's stability check doesn't skip on it.

Code read (b7e39dc0d..a4804d486), no problems found:

  • with_display_source keeps dxgi: preserve fractional refresh rates and scaled modes on physical outputs #8482's open/close guard. physical_mode_list behaves as before: it still returns false when D3DKMTGetDisplayModeList is missing or fails.
  • wait_for_vblank(info) falls back to the existing DwmFlush path whenever the monitor, the adapter open or D3DKMTWaitForVerticalBlankEvent fails, so a virtual or disconnected output keeps the old behaviour.
  • The new head only changes the test: it logs why it skips and fails when the shim's own samples are unstable, and it adds TIMEOUT 90 to the test.

One non-blocking note: each WaitForVBlank now runs D3DKMTOpenAdapterFromGdiDisplayName + D3DKMTCloseAdapter before the wait. A title that waits every frame pays that pair every frame. Caching the adapter handle per output (closed in the output's destructor) would avoid it, if it ever shows up in a profile.

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

@vicennpacheco

Copy link
Copy Markdown
Contributor

Tested a4804d486 merged (--no-ff, clean) onto main 6312c057f on AMD Windows (Vega 8 driving the attached 60 Hz laptop display): pass (no regressions).

  • Hardware / toolchain: AMD Radeon Vega 8 iGPU (Ryzen 7 5825U, GCN 5 / gfx90c, PCI 1002:15E7, AMD Windows driver 31.0.21921.13001, Vulkan 1.3.260, attached 60 Hz internal display), Windows 11, VS 2022 Build Tools 17.14.45 (MSVC 19.44.35230.0) + Ninja + Windows SDK 10.0.26100.0, Vulkan SDK 1.4.363.0, RelWithDebInfo.
  • Physical WaitForVBlank timing on AMD Radeon attached display (dxgi_swapchain -V, LEONIDA_VK_VALIDATION=1):
    • WaitForVBlank: native 16.64 ms, shim 16.64 ms — pass, 0 validation errors.
  • Targeted suite (dxgi_swapchain, dxgi_swapchain_count, dxgi_device, d3d12_present, xbox_present run serially to avoid window-focus contention):
    • main 6312c057f: 5/5 pass (VAL=0 and VAL=1).
    • + #8530 a4804d486: 5/5 pass (VAL=0 and VAL=1, 0 validation errors).

Synthetic fixtures only, no game files. AI-assisted.

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.

7 participants