VSync behaviour with DXGI Swapchain

I stumbled upon an issue with VSync on Windows with the DXGI Swapchain and VK_PRESENT_MODE_FIFO_KHR. (I suspect similar issues arise with the other present methods but i haven’t analyzed this yet.)

The present method can be set in the Nvidia control panel. (By default it’s set on “auto” which seems to use the DXGI method.)

I tested this on a 5070 TI on the latest Gameready drivers (610.47).

Basically with VK_PRESENT_MODE_FIFO_KHR the acquisition/submission of the swapchain should be synced with the Vertical blank of the screen. (So on a 60Hz display, the render thread should also cap at 60 FPS if implemented properly.)

On the “Prefer native” swapchain i can see that behaviour working as inteded.

The driver blocks/waits on

vkQueuePresentKHR

for the vertical blank, thus capping the framerate.

On the layered DXGI method however, the blocking behaviour cannot be observed, thus the FIFO present method runs effectively uncapped. In addition to that, the Vsync swap of the framebuffer seems to run independently from the vulkan queue submission (due to the layering?) causing noticeable framepacing issues/stutter.

I analyzed this with Nvidia Nsight Systems.

Prefer native:

Prefer layered on DXGI Swapchain (Auto)

You can clearly see how the frame submission wait for the Vertical sync on native whereas this isn’t the case for DXGI. (The Vsync swap happens while a frame is being rendered.)

My question is:

Is this intended behaviour? If so, how are we supposed to implement vsync on the DXGI swapchain? (Or proper framepacing for that matter.) Or should this work and it’s more of an indicator that i’m doing something fundamentally wrong on my end?

Would be great to get clarification on this.

Hi @Dev_Lewa,

That is indeed intended behavior. The Vulkan API is designed to allow for non-blocking present calls for maximum throughput. The original intent was to have the blocking mechanism as optional in vkAcquireNextImageKHR, but that turned out not to be the exact semantics people expect to provide backpressure to their render thread. In particular, there is no explicit latency control in the original VK_KHR_swapchain API.

This is not to say that Native is wrong, as the spec also allows for that behavior, but it is not guaranteed, so cannot be relied upon. The difference is unfortunate, but it reflects the capabilities of the underlying OS APIs. Native can even sometimes behave the other way under specific circumstances.

The solution to this is VK_KHR_present_wait, which allows you to do a blocking wait for previously submitted present operations. If you insert vkWaitForPresentKHR after your vkQueuePresentKHR call, you should have the blocking behavior you originally wanted.

I hope that helps,

Lionel

Hi!
Thanks for the clarification. Will try VK_KHR_present_wait out.

But i wonder now, if the blocking behaviour is optional, how exactly FIFO_KHR behaves compared to Immediate and/or mailbox mode.

Previously from my understanding was that the behaviour between the present modes was:

  • VK_PRESENT_MODE_FIFO_KHR : Submission synced with vertical sync (capped framerate)
  • VK_PRESENT_MODE_IMMEDIATE_KHR : not blocking, displays swapchain image as-is without waiting for vsync (can cause tearing)
  • VK_PRESENT_MODE_MAILBOX_KHR : not blocking (uncapped framerate), most recently submitted image will be displayed on next vsync (no tearing)

What exactly is now the difference regarding FIFO compared to the other present modes if DXGI doesn’t block? (If FIFO submits without blocking/waiting for vertical sync, doesn’t it basically replicate the behaviour of mailbox/immediate mode, depending on if tearing can occur or not?)

And if so, is it OK to always call vkWaitForPresentKHR in the case of the FIFO present mode, even if the native swapchain is used? (is it for the native swapchain a no-op or will it wait for a vertical sync a second time after the queue present thus causing unnessecary stalling?)

Or do we have the ability to query if the native or DXGI swapchain is used behind the scenes to adjust our API call accordingly?


/Edit: Tried out the VK_KHR_present_wait2 extension and it does work to a certain extent but it doesn’t cap the framerate to perfect 60 fps. (instead i get a per-frame execution time of around 14ms) thus the renderer runs at slightly above 60FPS (i measured 70-80)

    VkPresentId2KHR presentIdInfo = {
        .sType = VK_STRUCTURE_TYPE_PRESENT_ID_2_KHR,
        .pNext = nullptr,
        .swapchainCount = 1,
        .pPresentIds = &mFrameCounter};

    VkPresentInfoKHR presentInfo = {};
    presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;

    presentInfo.waitSemaphoreCount = 1;
    presentInfo.pWaitSemaphores = waitSemaphores;

    auto imageIndex = renderDevice->getCurrentSwapchainImageIndex();
    presentInfo.pImageIndices = &imageIndex;
    presentInfo.pNext = &presentIdInfo;

    VkSwapchainKHR swapChains[] = {getVkSwapChain()};
    presentInfo.swapchainCount = 1;
    presentInfo.pSwapchains = swapChains;

    auto presentationQueue = device->getQueue(renderDevice->getDeviceQueues().get(Engine::Graphics::DeviceQueue::Present).queue)->getVkQueue();

    vkQueuePresentKHR(presentationQueue, &presentInfo);

    VkPresentWait2InfoKHR waitInfo = {};
    waitInfo.sType = VK_STRUCTURE_TYPE_PRESENT_WAIT_2_INFO_KHR;
    waitInfo.presentId = mFrameCounter;
    waitInfo.timeout = std::numeric_limits<uint64_t>::max();
    waitInfo.pNext = VK_NULL_HANDLE;

    vkWaitForPresent2KHR(device->getVkDevice(), getVkSwapChain(), &waitInfo);

    mFrameCounter++;

So, i spent quite a bit of time experimenting and debugging this but i can’t make sense of how vkWaitForPresent(). It’s either a driver bug or i’m using it wrong/not understanding the spec correctly.

Here is the NSIGHT analysis of a FIFO swapchain with vulkan native set in the nvidia driver: (Notice how vkQueueSubmit blocks until Vsync is reached.)

Now here is the same with VK_KHR_present_wait2 (vkWaitForPresent2KHR)

Notice how the VkWaitForPresent2KHR ends even though the Vsync point is much earlier in the timeline. (I’m also not sure why with the DXGI swapchain multiple Vsync timelines are displayed in Nsight. I assume one of them is from the DXGI layer, one for the internal Vulkan swapchain which leaves an unknown third one.)

Any ideas/tips/info would be greatly appreciated.

Hi @Dev_Lewa

Thank you for reporting your findings. I admit I am a bit suspicious about those 3 VSYNC lines in your second screenshot. Would you mind providing a bit more details about your system? In particular, what kind of display you are using. I also see in your screenshots that you have an integrated GPU. Is it enabled and driving your display? If available, did you enable G-Sync (or other VRR technology)?

Thank you for your patience,

Lionel

Would you mind providing a bit more details about your system?

sure!

In particular, what kind of display you are using.

Triple monitor setup. 2x 1080p 60hz and 1x 4k 160 Hz (which i reduced to 60Hz for this test)

I also see in your screenshots that you have an integrated GPU

Yes, the IGPU is enabled but unused. I don’t have anything plugged into it/the motherboard. (all displays are driven by the Nvidia Gpu)

If available, did you enable G-Sync (or other VRR technology)?

Neither G-Sync, VRR or anything like that is enabled. (It’s a standard out of the box setup)

Actually i think i might’ve found something:

The 3 vsync lines seem to be related to the multi monitor setup. I redid the test with my 3 monitors enabled (with the DXGI layering enabled) and this time only 2 vsync lines appear. Only one of them has the Vsync points marked:

Sometimes it show 2 vsync lines, sometimes 3 (as it did in the screenshot from my previous post) and always one of the vsync lines doesn’t show the vsync markings on them. (all monitors are active and running though)

Then i unplugged the two 1080p monitors from my GPU (single monitor setup where only the 4k monitor set to 60Hz is plugged in) and did the test again:

What’s weird is that with a single monitor setup, the game properly locks at 60 with vkWaitforPresent (if the DXGI layering is enabled) which isn’t the case for the multi monitor setup. (where it runs at 90ish in my case, even though i have all monitors set to 60) (Plugging in at least one monitor causes hitching/stuttering and the vkWaitForPresent call returning too early it seems?)

This issues don’t happen with the native Vulkan swapchain.

So at first glance vkWaitForPresent doesn’t seem to work properly with the DXGI swapchain layering if a multi monitor setup is used.