Vulkan: Further simplify swapchain code (#502)

This commit is contained in:
goeiecool9999 2022-11-21 16:47:43 +01:00 committed by GitHub
parent 3f84c7fa24
commit 3a94a276da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 75 deletions

View file

@ -1665,6 +1665,7 @@ bool VulkanRenderer::ImguiBegin(bool mainWindow)
draw_endRenderPass();
m_state.currentPipeline = VK_NULL_HANDLE;
chainInfo.WaitAvailableFence();
ImGui_ImplVulkan_CreateFontsTexture(m_state.currentCommandBuffer);
ImGui_ImplVulkan_NewFrame(m_state.currentCommandBuffer, chainInfo.m_swapchainFramebuffers[chainInfo.swapchainImageIndex], chainInfo.getExtent());
ImGui_UpdateWindowInformation(mainWindow);
@ -1721,6 +1722,7 @@ bool VulkanRenderer::BeginFrame(bool mainWindow)
auto& chainInfo = GetChainInfo(mainWindow);
chainInfo.WaitAvailableFence();
VkClearColorValue clearColor{ 0, 0, 0, 0 };
ClearColorImageRaw(chainInfo.m_swapchainImages[chainInfo.swapchainImageIndex], 0, 0, clearColor, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
@ -2544,12 +2546,8 @@ bool VulkanRenderer::AcquireNextSwapchainImage(bool mainWindow)
if (chainInfo.swapchainImageIndex != -1)
return true; // image already reserved
vkWaitForFences(m_logicalDevice, 1, &chainInfo.m_imageAvailableFence, VK_TRUE, std::numeric_limits<uint64_t>::max());
vkResetFences(m_logicalDevice, 1, &chainInfo.m_imageAvailableFence);
auto& acquireSemaphore = chainInfo.m_acquireSemaphores[chainInfo.m_acquireIndex];
VkResult result = vkAcquireNextImageKHR(m_logicalDevice, chainInfo.swapchain, std::numeric_limits<uint64_t>::max(), acquireSemaphore, chainInfo.m_imageAvailableFence, &chainInfo.swapchainImageIndex);
VkResult result = vkAcquireNextImageKHR(m_logicalDevice, chainInfo.swapchain, std::numeric_limits<uint64_t>::max(), VK_NULL_HANDLE, chainInfo.m_imageAvailableFence, &chainInfo.swapchainImageIndex);
if (result != VK_SUCCESS)
{
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR)
@ -2562,8 +2560,6 @@ bool VulkanRenderer::AcquireNextSwapchainImage(bool mainWindow)
throw std::runtime_error(fmt::format("Failed to acquire next image: {}", result));
}
chainInfo.m_acquireIndex = (chainInfo.m_acquireIndex + 1) % chainInfo.m_acquireSemaphores.size();
SubmitCommandBuffer(nullptr, &acquireSemaphore);
return true;
}
@ -2572,9 +2568,6 @@ void VulkanRenderer::RecreateSwapchain(bool mainWindow, bool skipCreate)
SubmitCommandBuffer();
WaitDeviceIdle();
auto& chainInfo = GetChainInfo(mainWindow);
vkWaitForFences(m_logicalDevice, 1, &chainInfo.m_imageAvailableFence, VK_TRUE,
std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::milliseconds(10)).count()
);
Vector2i size;
if (mainWindow)
@ -2640,27 +2633,17 @@ void VulkanRenderer::SwapBuffer(bool mainWindow)
if (!chainInfo.hasDefinedSwapchainImage)
{
chainInfo.WaitAvailableFence();
// set the swapchain image to a defined state
VkClearColorValue clearColor{ 0, 0, 0, 0 };
ClearColorImageRaw(chainInfo.m_swapchainImages[chainInfo.swapchainImageIndex], 0, 0, clearColor, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
}
// make sure any writes to the image have finished (is this necessary? End of command buffer implicitly flushes everything?)
VkMemoryBarrier memoryBarrier{};
memoryBarrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER;
memoryBarrier.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
memoryBarrier.dstAccessMask = 0;
VkPipelineStageFlags srcStage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
VkPipelineStageFlags dstStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
vkCmdPipelineBarrier(m_state.currentCommandBuffer, srcStage, dstStage, 0, 1, &memoryBarrier, 0, nullptr, 0, nullptr);
VkSemaphore presentSemaphore = chainInfo.m_swapchainPresentSemaphores[chainInfo.swapchainImageIndex];
SubmitCommandBuffer(&presentSemaphore); // submit all command and signal semaphore
cemu_assert_debug(m_numSubmittedCmdBuffers > 0);
VkPresentInfoKHR presentInfo = {};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.swapchainCount = 1;
@ -2673,37 +2656,10 @@ void VulkanRenderer::SwapBuffer(bool mainWindow)
VkResult result = vkQueuePresentKHR(m_presentQueue, &presentInfo);
if (result != VK_SUCCESS)
{
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) // todo: dont loop but handle error state?
{
int counter = 0;
while (true)
{
try
{
RecreateSwapchain(mainWindow);
return;
}
catch (std::exception&)
{
// loop until successful
counter++;
if (counter > 25)
{
cemuLog_log(LogType::Force, "Failed to recreate swapchain during SwapBuffer");
cemuLog_waitForFlush();
exit(0);
}
}
std::this_thread::yield();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
cemuLog_log(LogType::Force, fmt::format("vkQueuePresentKHR failed with error {}", result));
cemuLog_waitForFlush();
throw std::runtime_error(fmt::format("Failed to present draw command buffer: {}", result));
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR)
chainInfo.m_shouldRecreate = true;
else
throw std::runtime_error(fmt::format("Failed to present image: {}", result));
}
chainInfo.hasDefinedSwapchainImage = false;
@ -2745,6 +2701,7 @@ void VulkanRenderer::ClearColorbuffer(bool padView)
if (chainInfo.swapchainImageIndex == -1)
return;
chainInfo.WaitAvailableFence();
VkClearColorValue clearColor{ 0, 0, 0, 0 };
ClearColorImageRaw(chainInfo.m_swapchainImages[chainInfo.swapchainImageIndex], 0, 0, clearColor, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL);
}
@ -2835,6 +2792,7 @@ void VulkanRenderer::DrawBackbufferQuad(LatteTextureView* texView, RendererOutpu
LatteTextureViewVk* texViewVk = (LatteTextureViewVk*)texView;
draw_endRenderPass();
chainInfo.WaitAvailableFence();
if (clearBackground)
ClearColorbuffer(padView);