Skip to content

Commit

Permalink
D3D12, Vk: API to toggle vsync via the swapchain
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravbug committed Oct 16, 2023
1 parent b3508d5 commit 3316003
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
2 changes: 2 additions & 0 deletions include/RGL/Swapchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ namespace RGL{
virtual void GetNextImage(uint32_t* index) = 0;
virtual ITexture* ImageAtIndex(uint32_t index) = 0;
virtual void Present(const SwapchainPresentConfig&) = 0;

virtual void SetVsyncMode(bool mode) = 0;
};
}
4 changes: 4 additions & 0 deletions src/D3D12Swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ namespace RGL {
UINT presentFlags = (tearingSupported && !vsync) ? DXGI_PRESENT_ALLOW_TEARING : 0;
swapchain->Present(syncInterval, presentFlags);
}
void SwapchainD3D12::SetVsyncMode(bool mode)
{
vsync = mode;
}
SwapchainD3D12::~SwapchainD3D12()
{
}
Expand Down
4 changes: 3 additions & 1 deletion src/D3D12Swapchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ namespace RGL {
D3D12DynamicDescriptorHeap::index_t rtvIndices[g_NumFrames];

bool tearingSupported = false;
bool vsync = true;
bool initialized = false;

SwapchainD3D12(decltype(owningDevice), std::shared_ptr<SurfaceD3D12>, int width, int height, std::shared_ptr<CommandQueueD3D12> presentQueue);
Expand All @@ -37,6 +36,9 @@ namespace RGL {
void GetNextImage(uint32_t* index) final;
ITexture* ImageAtIndex(uint32_t index) final;
void Present(const SwapchainPresentConfig&) final;
void SetVsyncMode(bool mode) final;
virtual ~SwapchainD3D12();
private:
bool vsync = true;
};
}
28 changes: 22 additions & 6 deletions src/VkSwapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,22 @@ void RGL::SwapchainVK::Resize(uint32_t width, uint32_t height)
//otherwise hope the first one is good enough
return availableFormats[0];
};
constexpr auto chooseSwapPresentMode = [](const std::vector<VkPresentModeKHR>& availablePresentModes) -> VkPresentModeKHR {
for (const auto& availablePresentMode : availablePresentModes) {
if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
return availablePresentMode; // use Mailbox on high-perf devices
auto chooseSwapPresentMode = [this](const std::vector<VkPresentModeKHR>& availablePresentModes) -> VkPresentModeKHR {
if (vsync) {
// disabling this because apparently VK_PRESENT_MODE_MAILBOX_KHR is uncapped on some OS/driver combos
#if 0
for (const auto& availablePresentMode : availablePresentModes) {
if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
return availablePresentMode; // use Mailbox on high-perf devices
}
}
}
#endif

return VK_PRESENT_MODE_FIFO_KHR; // otherwise use FIFO when on low-power devices, like a mobile phone
return VK_PRESENT_MODE_FIFO_KHR; // otherwise use FIFO when on low-power devices, like a mobile phone
}
else {
return VK_PRESENT_MODE_IMMEDIATE_KHR;
}
};
auto chooseSwapExtent = [width,height](const VkSurfaceCapabilitiesKHR& capabilities) ->VkExtent2D {
if (capabilities.currentExtent.width != std::numeric_limits<uint32_t>::max()) {
Expand Down Expand Up @@ -181,6 +189,14 @@ void RGL::SwapchainVK::Present(const SwapchainPresentConfig& config)
}
}

void RGL::SwapchainVK::SetVsyncMode(bool mode)
{
vsync = mode;
auto size = RGLTextureResources[0].GetSize();
// re-create with the new queue present mode
Resize(size.width, size.height);
}


void RGL::SwapchainVK::DestroySwapchainIfNeeded()
{
Expand Down
3 changes: 3 additions & 0 deletions src/VkSwapchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ namespace RGL {

void Present(const SwapchainPresentConfig&) final;

void SetVsyncMode(bool mode) final;

private:
bool vsync = true;
void DestroySwapchainIfNeeded();
};
}

0 comments on commit 3316003

Please sign in to comment.