Skip to content

Commit bc10485

Browse files
Vulkan: return to more conventional swapchain sync method, encapsulate more code (#525)
1 parent 8162477 commit bc10485

File tree

4 files changed

+102
-39
lines changed

4 files changed

+102
-39
lines changed

src/Cafe/HW/Latte/Renderer/Vulkan/SwapchainInfoVk.cpp

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ void SwapchainInfoVk::Create(VkPhysicalDevice physicalDevice, VkDevice logicalDe
1313
m_surfaceFormat = ChooseSurfaceFormat(details.formats);
1414
m_actualExtent = ChooseSwapExtent(details.capabilities);
1515

16-
uint32_t image_count = details.capabilities.minImageCount;
16+
// use at least two swapchain images. fewer than that causes problems on some drivers
17+
uint32_t image_count = std::max(2u, details.capabilities.minImageCount);
18+
if(details.capabilities.maxImageCount > 0)
19+
image_count = std::min(image_count, details.capabilities.maxImageCount);
20+
if(image_count < 2)
21+
cemuLog_force("Vulkan: Swapchain image count less than 2 may cause problems");
1722

1823
VkSwapchainCreateInfoKHR create_info = CreateSwapchainCreateInfo(surface, details, m_surfaceFormat, image_count, m_actualExtent);
1924
create_info.oldSwapchain = nullptr;
@@ -103,32 +108,47 @@ void SwapchainInfoVk::Create(VkPhysicalDevice physicalDevice, VkDevice logicalDe
103108
if (result != VK_SUCCESS)
104109
UnrecoverableError("Failed to create framebuffer for swapchain");
105110
}
106-
m_swapchainPresentSemaphores.resize(m_swapchainImages.size());
107-
// create present semaphore
111+
112+
m_presentSemaphores.resize(m_swapchainImages.size());
113+
// create present semaphores
108114
VkSemaphoreCreateInfo info = {};
109115
info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
110-
for (auto& semaphore : m_swapchainPresentSemaphores){
116+
for (auto& semaphore : m_presentSemaphores){
111117
if (vkCreateSemaphore(logicalDevice, &info, nullptr, &semaphore) != VK_SUCCESS)
112118
UnrecoverableError("Failed to create semaphore for swapchain present");
113119
}
114120

121+
m_acquireSemaphores.resize(m_swapchainImages.size());
122+
// create acquire semaphores
123+
info = {};
124+
info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
125+
for (auto& semaphore : m_acquireSemaphores){
126+
if (vkCreateSemaphore(logicalDevice, &info, nullptr, &semaphore) != VK_SUCCESS)
127+
UnrecoverableError("Failed to create semaphore for swapchain acquire");
128+
}
129+
115130
VkFenceCreateInfo fenceInfo = {};
116131
fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
117132
fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
118133
result = vkCreateFence(logicalDevice, &fenceInfo, nullptr, &m_imageAvailableFence);
119134
if (result != VK_SUCCESS)
120135
UnrecoverableError("Failed to create fence for swapchain");
121136

137+
m_acquireIndex = 0;
122138
hasDefinedSwapchainImage = false;
123139
}
124140

125141
void SwapchainInfoVk::Cleanup()
126142
{
127143
m_swapchainImages.clear();
128144

129-
for (auto& sem: m_swapchainPresentSemaphores)
145+
for (auto& sem: m_acquireSemaphores)
146+
vkDestroySemaphore(m_logicalDevice, sem, nullptr);
147+
m_acquireSemaphores.clear();
148+
149+
for (auto& sem: m_presentSemaphores)
130150
vkDestroySemaphore(m_logicalDevice, sem, nullptr);
131-
m_swapchainPresentSemaphores.clear();
151+
m_presentSemaphores.clear();
132152

133153
if (m_swapchainRenderPass)
134154
{
@@ -159,12 +179,55 @@ void SwapchainInfoVk::Cleanup()
159179

160180
bool SwapchainInfoVk::IsValid() const
161181
{
162-
return swapchain && m_imageAvailableFence;
182+
return swapchain && !m_acquireSemaphores.empty();
183+
}
184+
185+
void SwapchainInfoVk::WaitAvailableFence()
186+
{
187+
if(m_awaitableFence != VK_NULL_HANDLE)
188+
vkWaitForFences(m_logicalDevice, 1, &m_awaitableFence, VK_TRUE, UINT64_MAX);
189+
m_awaitableFence = VK_NULL_HANDLE;
190+
}
191+
192+
void SwapchainInfoVk::ResetAvailableFence() const
193+
{
194+
vkResetFences(m_logicalDevice, 1, &m_imageAvailableFence);
195+
}
196+
197+
VkSemaphore SwapchainInfoVk::ConsumeAcquireSemaphore()
198+
{
199+
VkSemaphore ret = m_currentSemaphore;
200+
m_currentSemaphore = VK_NULL_HANDLE;
201+
return ret;
163202
}
164203

165-
void SwapchainInfoVk::WaitAvailableFence() const
204+
bool SwapchainInfoVk::AcquireImage(uint64 timeout)
166205
{
167-
vkWaitForFences(m_logicalDevice, 1, &m_imageAvailableFence, VK_TRUE, UINT64_MAX);
206+
WaitAvailableFence();
207+
ResetAvailableFence();
208+
209+
VkSemaphore acquireSemaphore = m_acquireSemaphores[m_acquireIndex];
210+
VkResult result = vkAcquireNextImageKHR(m_logicalDevice, swapchain, timeout, acquireSemaphore, m_imageAvailableFence, &swapchainImageIndex);
211+
if (result == VK_TIMEOUT)
212+
{
213+
return false;
214+
}
215+
else if (result != VK_SUCCESS)
216+
{
217+
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR)
218+
m_shouldRecreate = true;
219+
220+
if (result == VK_ERROR_OUT_OF_DATE_KHR)
221+
return false;
222+
223+
if (result != VK_ERROR_OUT_OF_DATE_KHR && result != VK_SUBOPTIMAL_KHR)
224+
throw std::runtime_error(fmt::format("Failed to acquire next image: {}", result));
225+
}
226+
m_currentSemaphore = acquireSemaphore;
227+
m_awaitableFence = m_imageAvailableFence;
228+
m_acquireIndex = (m_acquireIndex + 1) % m_swapchainImages.size();
229+
230+
return true;
168231
}
169232

170233
void SwapchainInfoVk::UnrecoverableError(const char* errMsg)

src/Cafe/HW/Latte/Renderer/Vulkan/SwapchainInfoVk.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@ struct SwapchainInfoVk
3434

3535
bool IsValid() const;
3636

37-
void WaitAvailableFence() const;
37+
void WaitAvailableFence();
38+
void ResetAvailableFence() const;
39+
40+
bool AcquireImage(uint64 timeout);
41+
// retrieve semaphore of last acquire for submitting a wait operation
42+
// only one wait operation must be submitted per acquire (which submits a single signal operation)
43+
// therefore subsequent calls will return a NULL handle
44+
VkSemaphore ConsumeAcquireSemaphore();
3845

3946
static void UnrecoverableError(const char* errMsg);
4047

@@ -76,19 +83,24 @@ struct SwapchainInfoVk
7683
VkSurfaceFormatKHR m_surfaceFormat{};
7784
VkSwapchainKHR swapchain{};
7885
Vector2i m_desiredExtent{};
79-
VkFence m_imageAvailableFence{};
8086
uint32 swapchainImageIndex = (uint32)-1;
8187

8288

8389
// swapchain image ringbuffer (indexed by swapchainImageIndex)
8490
std::vector<VkImage> m_swapchainImages;
8591
std::vector<VkImageView> m_swapchainImageViews;
8692
std::vector<VkFramebuffer> m_swapchainFramebuffers;
87-
std::vector<VkSemaphore> m_swapchainPresentSemaphores;
88-
std::array<uint32, 2> m_swapchainQueueFamilyIndices;
93+
std::vector<VkSemaphore> m_presentSemaphores; // indexed by swapchainImageIndex
8994

9095
VkRenderPass m_swapchainRenderPass = nullptr;
9196

9297
private:
98+
uint32 m_acquireIndex = 0;
99+
std::vector<VkSemaphore> m_acquireSemaphores; // indexed by m_acquireIndex
100+
VkFence m_imageAvailableFence{};
101+
VkSemaphore m_currentSemaphore = VK_NULL_HANDLE;
102+
VkFence m_awaitableFence = VK_NULL_HANDLE;
103+
104+
std::array<uint32, 2> m_swapchainQueueFamilyIndices;
93105
VkExtent2D m_actualExtent{};
94106
};

src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.cpp

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,7 +1665,6 @@ bool VulkanRenderer::ImguiBegin(bool mainWindow)
16651665
draw_endRenderPass();
16661666
m_state.currentPipeline = VK_NULL_HANDLE;
16671667

1668-
chainInfo.WaitAvailableFence();
16691668
ImGui_ImplVulkan_CreateFontsTexture(m_state.currentCommandBuffer);
16701669
ImGui_ImplVulkan_NewFrame(m_state.currentCommandBuffer, chainInfo.m_swapchainFramebuffers[chainInfo.swapchainImageIndex], chainInfo.getExtent());
16711670
ImGui_UpdateWindowInformation(mainWindow);
@@ -1722,7 +1721,6 @@ bool VulkanRenderer::BeginFrame(bool mainWindow)
17221721

17231722
auto& chainInfo = GetChainInfo(mainWindow);
17241723

1725-
chainInfo.WaitAvailableFence();
17261724
VkClearColorValue clearColor{ 0, 0, 0, 0 };
17271725
ClearColorImageRaw(chainInfo.m_swapchainImages[chainInfo.swapchainImageIndex], 0, 0, clearColor, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
17281726

@@ -1848,7 +1846,7 @@ void VulkanRenderer::WaitForNextFinishedCommandBuffer()
18481846
ProcessFinishedCommandBuffers();
18491847
}
18501848

1851-
void VulkanRenderer::SubmitCommandBuffer(VkSemaphore* signalSemaphore, VkSemaphore* waitSemaphore)
1849+
void VulkanRenderer::SubmitCommandBuffer(VkSemaphore signalSemaphore, VkSemaphore waitSemaphore)
18521850
{
18531851
draw_endRenderPass();
18541852

@@ -1863,11 +1861,11 @@ void VulkanRenderer::SubmitCommandBuffer(VkSemaphore* signalSemaphore, VkSemapho
18631861

18641862
// signal current command buffer semaphore
18651863
VkSemaphore signalSemArray[2];
1866-
if (signalSemaphore)
1864+
if (signalSemaphore != VK_NULL_HANDLE)
18671865
{
18681866
submitInfo.signalSemaphoreCount = 2;
18691867
signalSemArray[0] = m_commandBufferSemaphores[m_commandBufferIndex]; // signal current
1870-
signalSemArray[1] = *signalSemaphore; // signal current
1868+
signalSemArray[1] = signalSemaphore; // signal current
18711869
submitInfo.pSignalSemaphores = signalSemArray;
18721870
}
18731871
else
@@ -1883,8 +1881,8 @@ void VulkanRenderer::SubmitCommandBuffer(VkSemaphore* signalSemaphore, VkSemapho
18831881
submitInfo.waitSemaphoreCount = 0;
18841882
if (m_numSubmittedCmdBuffers > 0)
18851883
waitSemArray[submitInfo.waitSemaphoreCount++] = prevSem; // wait on semaphore from previous submit
1886-
if (waitSemaphore)
1887-
waitSemArray[submitInfo.waitSemaphoreCount++] = *waitSemaphore;
1884+
if (waitSemaphore != VK_NULL_HANDLE)
1885+
waitSemArray[submitInfo.waitSemaphoreCount++] = waitSemaphore;
18881886
submitInfo.pWaitDstStageMask = semWaitStageMask;
18891887
submitInfo.pWaitSemaphores = waitSemArray;
18901888

@@ -2546,20 +2544,11 @@ bool VulkanRenderer::AcquireNextSwapchainImage(bool mainWindow)
25462544
if (!UpdateSwapchainProperties(mainWindow))
25472545
return false;
25482546

2549-
vkResetFences(m_logicalDevice, 1, &chainInfo.m_imageAvailableFence);
2550-
VkResult result = vkAcquireNextImageKHR(m_logicalDevice, chainInfo.swapchain, std::numeric_limits<uint64_t>::max(), VK_NULL_HANDLE, chainInfo.m_imageAvailableFence, &chainInfo.swapchainImageIndex);
2551-
if (result != VK_SUCCESS)
2552-
{
2553-
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR)
2554-
chainInfo.m_shouldRecreate = true;
2555-
2556-
if (result == VK_ERROR_OUT_OF_DATE_KHR)
2557-
return false;
2558-
2559-
if (result != VK_ERROR_OUT_OF_DATE_KHR && result != VK_SUBOPTIMAL_KHR)
2560-
throw std::runtime_error(fmt::format("Failed to acquire next image: {}", result));
2561-
}
2547+
bool result = chainInfo.AcquireImage(UINT64_MAX);
2548+
if (!result)
2549+
return false;
25622550

2551+
SubmitCommandBuffer(VK_NULL_HANDLE, chainInfo.ConsumeAcquireSemaphore());
25632552
return true;
25642553
}
25652554

@@ -2568,6 +2557,8 @@ void VulkanRenderer::RecreateSwapchain(bool mainWindow, bool skipCreate)
25682557
SubmitCommandBuffer();
25692558
WaitDeviceIdle();
25702559
auto& chainInfo = GetChainInfo(mainWindow);
2560+
// make sure fence has no signal operation submitted
2561+
chainInfo.WaitAvailableFence();
25712562

25722563
Vector2i size;
25732564
if (mainWindow)
@@ -2633,14 +2624,13 @@ void VulkanRenderer::SwapBuffer(bool mainWindow)
26332624

26342625
if (!chainInfo.hasDefinedSwapchainImage)
26352626
{
2636-
chainInfo.WaitAvailableFence();
26372627
// set the swapchain image to a defined state
26382628
VkClearColorValue clearColor{ 0, 0, 0, 0 };
26392629
ClearColorImageRaw(chainInfo.m_swapchainImages[chainInfo.swapchainImageIndex], 0, 0, clearColor, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
26402630
}
26412631

2642-
VkSemaphore presentSemaphore = chainInfo.m_swapchainPresentSemaphores[chainInfo.swapchainImageIndex];
2643-
SubmitCommandBuffer(&presentSemaphore); // submit all command and signal semaphore
2632+
VkSemaphore presentSemaphore = chainInfo.m_presentSemaphores[chainInfo.swapchainImageIndex];
2633+
SubmitCommandBuffer(presentSemaphore); // submit all command and signal semaphore
26442634

26452635
cemu_assert_debug(m_numSubmittedCmdBuffers > 0);
26462636

@@ -2701,7 +2691,6 @@ void VulkanRenderer::ClearColorbuffer(bool padView)
27012691
if (chainInfo.swapchainImageIndex == -1)
27022692
return;
27032693

2704-
chainInfo.WaitAvailableFence();
27052694
VkClearColorValue clearColor{ 0, 0, 0, 0 };
27062695
ClearColorImageRaw(chainInfo.m_swapchainImages[chainInfo.swapchainImageIndex], 0, 0, clearColor, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL);
27072696
}
@@ -2792,7 +2781,6 @@ void VulkanRenderer::DrawBackbufferQuad(LatteTextureView* texView, RendererOutpu
27922781
LatteTextureViewVk* texViewVk = (LatteTextureViewVk*)texView;
27932782
draw_endRenderPass();
27942783

2795-
chainInfo.WaitAvailableFence();
27962784
if (clearBackground)
27972785
ClearColorbuffer(padView);
27982786

src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ class VulkanRenderer : public Renderer
233233
void InitFirstCommandBuffer();
234234
void ProcessFinishedCommandBuffers();
235235
void WaitForNextFinishedCommandBuffer();
236-
void SubmitCommandBuffer(VkSemaphore* signalSemaphore = nullptr, VkSemaphore* waitSemaphore = nullptr);
236+
void SubmitCommandBuffer(VkSemaphore signalSemaphore = VK_NULL_HANDLE, VkSemaphore waitSemaphore = VK_NULL_HANDLE);
237237
void RequestSubmitSoon();
238238
void RequestSubmitOnIdle();
239239

0 commit comments

Comments
 (0)