Skip to content

Commit

Permalink
change the code to be more semantically correct
Browse files Browse the repository at this point in the history
  • Loading branch information
goeiecool9999 committed Aug 14, 2024
1 parent c795002 commit 4e6589f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/Cafe/HW/Latte/Renderer/Vulkan/SwapchainInfoVk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ void SwapchainInfoVk::Create()
m_acquireIndex = 0;
hasDefinedSwapchainImage = false;

m_numQueued = 0;
m_queueDepth = 0;
}

void SwapchainInfoVk::Cleanup()
Expand Down Expand Up @@ -348,7 +348,7 @@ VkPresentModeKHR SwapchainInfoVk::ChoosePresentMode(const std::vector<VkPresentM
return VK_PRESENT_MODE_FIFO_KHR;
}

m_maxQueued = 1;
m_maxQueued = 2;
return VK_PRESENT_MODE_FIFO_KHR;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Cafe/HW/Latte/Renderer/Vulkan/SwapchainInfoVk.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ struct SwapchainInfoVk
Vector2i m_desiredExtent{};
uint32 swapchainImageIndex = (uint32)-1;
uint64 m_presentId = 1;
uint64 m_numQueued = 0; //track consecutive present successes
uint64 m_maxQueued = 0; //track consecutive present successes
uint64 m_queueDepth = 0; //number of frames CPU is ahead of presentation
uint64 m_maxQueued = 0;


// swapchain image ringbuffer (indexed by swapchainImageIndex)
Expand Down
31 changes: 19 additions & 12 deletions src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2640,6 +2640,16 @@ bool VulkanRenderer::AcquireNextSwapchainImage(bool mainWindow)
if (!UpdateSwapchainProperties(mainWindow))
return false;

if(UsePresentWait(chainInfo))
{
if(chainInfo.m_queueDepth >= chainInfo.m_maxQueued)
{
uint64 waitFrameId = chainInfo.m_presentId - chainInfo.m_queueDepth;
vkWaitForPresentKHR(m_logicalDevice, chainInfo.m_swapchain, waitFrameId, 40'000'000);
chainInfo.m_queueDepth--;
}
}

bool result = chainInfo.AcquireImage();
if (!result)
return false;
Expand Down Expand Up @@ -2677,6 +2687,11 @@ void VulkanRenderer::RecreateSwapchain(bool mainWindow, bool skipCreate)
ImguiInit();
}

bool VulkanRenderer::UsePresentWait(const SwapchainInfoVk& chain) const
{
return m_featureControl.deviceExtensions.present_wait && chain.m_maxQueued > 0;
}

bool VulkanRenderer::UpdateSwapchainProperties(bool mainWindow)
{
auto& chainInfo = GetChainInfo(mainWindow);
Expand Down Expand Up @@ -2748,10 +2763,8 @@ void VulkanRenderer::SwapBuffer(bool mainWindow)
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = &presentSemaphore;

const bool enablePresentWait = m_featureControl.deviceExtensions.present_wait && chainInfo.m_maxQueued != 0;

// if present_wait is available, use it to limit CPU run-ahead
if (enablePresentWait)
// if present_wait is available and enabled, add frame markers to present requests
if (UsePresentWait(chainInfo))
{
presentId.sType = VK_STRUCTURE_TYPE_PRESENT_ID_KHR;
presentId.swapchainCount = 1;
Expand All @@ -2768,16 +2781,10 @@ void VulkanRenderer::SwapBuffer(bool mainWindow)
if(result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR)
chainInfo.m_shouldRecreate = true;

if(enablePresentWait && result == VK_SUCCESS)
if(result == VK_SUCCESS)
{
chainInfo.m_numQueued++;
chainInfo.m_queueDepth++;
chainInfo.m_presentId++;
if (chainInfo.m_numQueued > chainInfo.m_maxQueued)
{
uint64 waitFrameId = chainInfo.m_presentId - chainInfo.m_numQueued;
vkWaitForPresentKHR(m_logicalDevice, chainInfo.m_swapchain, waitFrameId, 40'000'000);
chainInfo.m_numQueued--;
}
}


Expand Down
1 change: 1 addition & 0 deletions src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ class VulkanRenderer : public Renderer
VkPipeline backbufferBlit_createGraphicsPipeline(VkDescriptorSetLayout descriptorLayout, bool padView, RendererOutputShader* shader);
bool AcquireNextSwapchainImage(bool mainWindow);
void RecreateSwapchain(bool mainWindow, bool skipCreate = false);
bool UsePresentWait(const SwapchainInfoVk& chain) const;

// streamout
void streamout_setupXfbBuffer(uint32 bufferIndex, sint32 ringBufferOffset, uint32 rangeAddr, uint32 rangeSize) override;
Expand Down

0 comments on commit 4e6589f

Please sign in to comment.