Skip to content

Commit

Permalink
Vulkan: Remove unecessary present fence (#1166)
Browse files Browse the repository at this point in the history
  • Loading branch information
goeiecool9999 committed Apr 11, 2024
1 parent 391533d commit 84cad8b
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 51 deletions.
5 changes: 0 additions & 5 deletions src/Cafe/HW/Latte/Core/LatteRenderTarget.cpp
Expand Up @@ -875,11 +875,6 @@ void LatteRenderTarget_getScreenImageArea(sint32* x, sint32* y, sint32* width, s

void LatteRenderTarget_copyToBackbuffer(LatteTextureView* textureView, bool isPadView)
{
if (g_renderer->GetType() == RendererAPI::Vulkan)
{
((VulkanRenderer*)g_renderer.get())->PreparePresentationFrame(!isPadView);
}

// make sure texture is updated to latest data in cache
LatteTexture_UpdateDataToLatest(textureView->baseTexture);
// mark source texture as still in use
Expand Down
39 changes: 8 additions & 31 deletions src/Cafe/HW/Latte/Renderer/Vulkan/SwapchainInfoVk.cpp
Expand Up @@ -146,13 +146,6 @@ void SwapchainInfoVk::Create()
UnrecoverableError("Failed to create semaphore for swapchain acquire");
}

VkFenceCreateInfo fenceInfo = {};
fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
result = vkCreateFence(m_logicalDevice, &fenceInfo, nullptr, &m_imageAvailableFence);
if (result != VK_SUCCESS)
UnrecoverableError("Failed to create fence for swapchain");

m_acquireIndex = 0;
hasDefinedSwapchainImage = false;
}
Expand Down Expand Up @@ -184,12 +177,6 @@ void SwapchainInfoVk::Cleanup()
m_swapchainFramebuffers.clear();


if (m_imageAvailableFence)
{
WaitAvailableFence();
vkDestroyFence(m_logicalDevice, m_imageAvailableFence, nullptr);
m_imageAvailableFence = nullptr;
}
if (m_swapchain)
{
vkDestroySwapchainKHR(m_logicalDevice, m_swapchain, nullptr);
Expand All @@ -202,34 +189,25 @@ bool SwapchainInfoVk::IsValid() const
return m_swapchain && !m_acquireSemaphores.empty();
}

void SwapchainInfoVk::WaitAvailableFence()
{
if(m_awaitableFence != VK_NULL_HANDLE)
vkWaitForFences(m_logicalDevice, 1, &m_awaitableFence, VK_TRUE, UINT64_MAX);
m_awaitableFence = VK_NULL_HANDLE;
}

void SwapchainInfoVk::ResetAvailableFence() const
{
vkResetFences(m_logicalDevice, 1, &m_imageAvailableFence);
}

VkSemaphore SwapchainInfoVk::ConsumeAcquireSemaphore()
{
VkSemaphore ret = m_currentSemaphore;
m_currentSemaphore = VK_NULL_HANDLE;
return ret;
}

bool SwapchainInfoVk::AcquireImage(uint64 timeout)
bool SwapchainInfoVk::AcquireImage()
{
WaitAvailableFence();
ResetAvailableFence();

VkSemaphore acquireSemaphore = m_acquireSemaphores[m_acquireIndex];
VkResult result = vkAcquireNextImageKHR(m_logicalDevice, m_swapchain, timeout, acquireSemaphore, m_imageAvailableFence, &swapchainImageIndex);
VkResult result = vkAcquireNextImageKHR(m_logicalDevice, m_swapchain, 1'000'000'000, acquireSemaphore, nullptr, &swapchainImageIndex);
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR)
m_shouldRecreate = true;
if (result == VK_TIMEOUT)
{
swapchainImageIndex = -1;
return false;
}

if (result < 0)
{
swapchainImageIndex = -1;
Expand All @@ -238,7 +216,6 @@ bool SwapchainInfoVk::AcquireImage(uint64 timeout)
return false;
}
m_currentSemaphore = acquireSemaphore;
m_awaitableFence = m_imageAvailableFence;
m_acquireIndex = (m_acquireIndex + 1) % m_swapchainImages.size();

return true;
Expand Down
7 changes: 1 addition & 6 deletions src/Cafe/HW/Latte/Renderer/Vulkan/SwapchainInfoVk.h
Expand Up @@ -26,10 +26,7 @@ struct SwapchainInfoVk

bool IsValid() const;

void WaitAvailableFence();
void ResetAvailableFence() const;

bool AcquireImage(uint64 timeout);
bool AcquireImage();
// retrieve semaphore of last acquire for submitting a wait operation
// only one wait operation must be submitted per acquire (which submits a single signal operation)
// therefore subsequent calls will return a NULL handle
Expand Down Expand Up @@ -84,9 +81,7 @@ struct SwapchainInfoVk
private:
uint32 m_acquireIndex = 0;
std::vector<VkSemaphore> m_acquireSemaphores; // indexed by m_acquireIndex
VkFence m_imageAvailableFence{};
VkSemaphore m_currentSemaphore = VK_NULL_HANDLE;
VkFence m_awaitableFence = VK_NULL_HANDLE;

std::array<uint32, 2> m_swapchainQueueFamilyIndices;
VkExtent2D m_actualExtent{};
Expand Down
9 changes: 1 addition & 8 deletions src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.cpp
Expand Up @@ -1824,11 +1824,6 @@ void VulkanRenderer::DrawEmptyFrame(bool mainWindow)
SwapBuffers(mainWindow, !mainWindow);
}

void VulkanRenderer::PreparePresentationFrame(bool mainWindow)
{
AcquireNextSwapchainImage(mainWindow);
}

void VulkanRenderer::InitFirstCommandBuffer()
{
cemu_assert_debug(m_state.currentCommandBuffer == nullptr);
Expand Down Expand Up @@ -2599,7 +2594,7 @@ bool VulkanRenderer::AcquireNextSwapchainImage(bool mainWindow)
if (!UpdateSwapchainProperties(mainWindow))
return false;

bool result = chainInfo.AcquireImage(UINT64_MAX);
bool result = chainInfo.AcquireImage();
if (!result)
return false;

Expand All @@ -2612,8 +2607,6 @@ void VulkanRenderer::RecreateSwapchain(bool mainWindow, bool skipCreate)
SubmitCommandBuffer();
WaitDeviceIdle();
auto& chainInfo = GetChainInfo(mainWindow);
// make sure fence has no signal operation submitted
chainInfo.WaitAvailableFence();

Vector2i size;
if (mainWindow)
Expand Down
1 change: 0 additions & 1 deletion src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.h
Expand Up @@ -228,7 +228,6 @@ class VulkanRenderer : public Renderer
uint64 GenUniqueId(); // return unique id (uses incrementing counter)

void DrawEmptyFrame(bool mainWindow) override;
void PreparePresentationFrame(bool mainWindow);

void InitFirstCommandBuffer();
void ProcessFinishedCommandBuffers();
Expand Down

0 comments on commit 84cad8b

Please sign in to comment.