@@ -1665,6 +1665,7 @@ bool VulkanRenderer::ImguiBegin(bool mainWindow)
1665
1665
draw_endRenderPass ();
1666
1666
m_state.currentPipeline = VK_NULL_HANDLE;
1667
1667
1668
+ chainInfo.WaitAvailableFence ();
1668
1669
ImGui_ImplVulkan_CreateFontsTexture (m_state.currentCommandBuffer );
1669
1670
ImGui_ImplVulkan_NewFrame (m_state.currentCommandBuffer , chainInfo.m_swapchainFramebuffers [chainInfo.swapchainImageIndex ], chainInfo.getExtent ());
1670
1671
ImGui_UpdateWindowInformation (mainWindow);
@@ -1721,6 +1722,7 @@ bool VulkanRenderer::BeginFrame(bool mainWindow)
1721
1722
1722
1723
auto & chainInfo = GetChainInfo (mainWindow);
1723
1724
1725
+ chainInfo.WaitAvailableFence ();
1724
1726
VkClearColorValue clearColor{ 0 , 0 , 0 , 0 };
1725
1727
ClearColorImageRaw (chainInfo.m_swapchainImages [chainInfo.swapchainImageIndex ], 0 , 0 , clearColor, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
1726
1728
@@ -2544,12 +2546,8 @@ bool VulkanRenderer::AcquireNextSwapchainImage(bool mainWindow)
2544
2546
if (chainInfo.swapchainImageIndex != -1 )
2545
2547
return true ; // image already reserved
2546
2548
2547
- vkWaitForFences (m_logicalDevice, 1 , &chainInfo.m_imageAvailableFence , VK_TRUE, std::numeric_limits<uint64_t >::max ());
2548
2549
vkResetFences (m_logicalDevice, 1 , &chainInfo.m_imageAvailableFence );
2549
-
2550
- auto & acquireSemaphore = chainInfo.m_acquireSemaphores [chainInfo.m_acquireIndex ];
2551
-
2552
- VkResult result = vkAcquireNextImageKHR (m_logicalDevice, chainInfo.swapchain , std::numeric_limits<uint64_t >::max (), acquireSemaphore, chainInfo.m_imageAvailableFence , &chainInfo.swapchainImageIndex );
2550
+ VkResult result = vkAcquireNextImageKHR (m_logicalDevice, chainInfo.swapchain , std::numeric_limits<uint64_t >::max (), VK_NULL_HANDLE, chainInfo.m_imageAvailableFence , &chainInfo.swapchainImageIndex );
2553
2551
if (result != VK_SUCCESS)
2554
2552
{
2555
2553
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR)
@@ -2562,8 +2560,6 @@ bool VulkanRenderer::AcquireNextSwapchainImage(bool mainWindow)
2562
2560
throw std::runtime_error (fmt::format (" Failed to acquire next image: {}" , result));
2563
2561
}
2564
2562
2565
- chainInfo.m_acquireIndex = (chainInfo.m_acquireIndex + 1 ) % chainInfo.m_acquireSemaphores .size ();
2566
- SubmitCommandBuffer (nullptr , &acquireSemaphore);
2567
2563
return true ;
2568
2564
}
2569
2565
@@ -2572,9 +2568,6 @@ void VulkanRenderer::RecreateSwapchain(bool mainWindow, bool skipCreate)
2572
2568
SubmitCommandBuffer ();
2573
2569
WaitDeviceIdle ();
2574
2570
auto & chainInfo = GetChainInfo (mainWindow);
2575
- vkWaitForFences (m_logicalDevice, 1 , &chainInfo.m_imageAvailableFence , VK_TRUE,
2576
- std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::milliseconds (10 )).count ()
2577
- );
2578
2571
2579
2572
Vector2i size;
2580
2573
if (mainWindow)
@@ -2640,27 +2633,17 @@ void VulkanRenderer::SwapBuffer(bool mainWindow)
2640
2633
2641
2634
if (!chainInfo.hasDefinedSwapchainImage )
2642
2635
{
2636
+ chainInfo.WaitAvailableFence ();
2643
2637
// set the swapchain image to a defined state
2644
2638
VkClearColorValue clearColor{ 0 , 0 , 0 , 0 };
2645
2639
ClearColorImageRaw (chainInfo.m_swapchainImages [chainInfo.swapchainImageIndex ], 0 , 0 , clearColor, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
2646
2640
}
2647
2641
2648
- // make sure any writes to the image have finished (is this necessary? End of command buffer implicitly flushes everything?)
2649
- VkMemoryBarrier memoryBarrier{};
2650
- memoryBarrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER;
2651
- memoryBarrier.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
2652
- memoryBarrier.dstAccessMask = 0 ;
2653
- VkPipelineStageFlags srcStage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
2654
- VkPipelineStageFlags dstStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
2655
- vkCmdPipelineBarrier (m_state.currentCommandBuffer , srcStage, dstStage, 0 , 1 , &memoryBarrier, 0 , nullptr , 0 , nullptr );
2656
-
2657
-
2658
2642
VkSemaphore presentSemaphore = chainInfo.m_swapchainPresentSemaphores [chainInfo.swapchainImageIndex ];
2659
2643
SubmitCommandBuffer (&presentSemaphore); // submit all command and signal semaphore
2660
2644
2661
2645
cemu_assert_debug (m_numSubmittedCmdBuffers > 0 );
2662
2646
2663
-
2664
2647
VkPresentInfoKHR presentInfo = {};
2665
2648
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
2666
2649
presentInfo.swapchainCount = 1 ;
@@ -2673,37 +2656,10 @@ void VulkanRenderer::SwapBuffer(bool mainWindow)
2673
2656
VkResult result = vkQueuePresentKHR (m_presentQueue, &presentInfo);
2674
2657
if (result != VK_SUCCESS)
2675
2658
{
2676
- if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) // todo: dont loop but handle error state?
2677
- {
2678
- int counter = 0 ;
2679
- while (true )
2680
- {
2681
- try
2682
- {
2683
- RecreateSwapchain (mainWindow);
2684
- return ;
2685
- }
2686
- catch (std::exception&)
2687
- {
2688
- // loop until successful
2689
- counter++;
2690
- if (counter > 25 )
2691
- {
2692
- cemuLog_log (LogType::Force, " Failed to recreate swapchain during SwapBuffer" );
2693
- cemuLog_waitForFlush ();
2694
- exit (0 );
2695
- }
2696
- }
2697
-
2698
- std::this_thread::yield ();
2699
- std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
2700
- }
2701
- }
2702
-
2703
- cemuLog_log (LogType::Force, fmt::format (" vkQueuePresentKHR failed with error {}" , result));
2704
- cemuLog_waitForFlush ();
2705
-
2706
- throw std::runtime_error (fmt::format (" Failed to present draw command buffer: {}" , result));
2659
+ if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR)
2660
+ chainInfo.m_shouldRecreate = true ;
2661
+ else
2662
+ throw std::runtime_error (fmt::format (" Failed to present image: {}" , result));
2707
2663
}
2708
2664
2709
2665
chainInfo.hasDefinedSwapchainImage = false ;
@@ -2745,6 +2701,7 @@ void VulkanRenderer::ClearColorbuffer(bool padView)
2745
2701
if (chainInfo.swapchainImageIndex == -1 )
2746
2702
return ;
2747
2703
2704
+ chainInfo.WaitAvailableFence ();
2748
2705
VkClearColorValue clearColor{ 0 , 0 , 0 , 0 };
2749
2706
ClearColorImageRaw (chainInfo.m_swapchainImages [chainInfo.swapchainImageIndex ], 0 , 0 , clearColor, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL);
2750
2707
}
@@ -2835,6 +2792,7 @@ void VulkanRenderer::DrawBackbufferQuad(LatteTextureView* texView, RendererOutpu
2835
2792
LatteTextureViewVk* texViewVk = (LatteTextureViewVk*)texView;
2836
2793
draw_endRenderPass ();
2837
2794
2795
+ chainInfo.WaitAvailableFence ();
2838
2796
if (clearBackground)
2839
2797
ClearColorbuffer (padView);
2840
2798
0 commit comments