Skip to content

Commit 4a0c358

Browse files
committed
Synchronization
1 parent c75bec1 commit 4a0c358

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

src/main.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ VulkanPipeline pipeline;
1414
VkCommandPool commandPool;
1515
VkCommandBuffer commandBuffer;
1616
VkFence fence;
17+
VkSemaphore acquireSemaphore;
18+
VkSemaphore releaseSemaphore;
1719

1820
bool handleMessage() {
1921
SDL_Event event;
@@ -55,8 +57,14 @@ void initApplication(SDL_Window* window) {
5557

5658
{
5759
VkFenceCreateInfo createInfo = { VK_STRUCTURE_TYPE_FENCE_CREATE_INFO };
60+
createInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
5861
VKA(vkCreateFence(context->device, &createInfo, 0, &fence));
5962
}
63+
{
64+
VkSemaphoreCreateInfo createInfo = { VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO };
65+
VKA(vkCreateSemaphore(context->device, &createInfo, 0, &acquireSemaphore));
66+
VKA(vkCreateSemaphore(context->device, &createInfo, 0, &releaseSemaphore));
67+
}
6068

6169
{
6270
VkCommandPoolCreateInfo createInfo = { VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO };
@@ -78,8 +86,10 @@ void renderApplication() {
7886
greenChannel += 0.01f;
7987
if (greenChannel > 1.0f) greenChannel = 0.0f;
8088
uint32_t imageIndex = 0;
81-
VK(vkAcquireNextImageKHR(context->device, swapchain.swapchain, UINT64_MAX, 0, fence, &imageIndex));
89+
VK(vkAcquireNextImageKHR(context->device, swapchain.swapchain, UINT64_MAX, acquireSemaphore, 0, &imageIndex));
8290

91+
VKA(vkWaitForFences(context->device, 1, &fence, VK_TRUE, UINT64_MAX));
92+
VKA(vkResetFences(context->device, 1, &fence));
8393
VKA(vkResetCommandPool(context->device, commandPool, 0));
8494

8595
VkCommandBufferBeginInfo beginInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO };
@@ -102,20 +112,23 @@ void renderApplication() {
102112
}
103113
VKA(vkEndCommandBuffer(commandBuffer));
104114

105-
VKA(vkWaitForFences(context->device, 1, &fence, VK_TRUE, UINT64_MAX));
106-
VKA(vkResetFences(context->device, 1, &fence));
107-
108115
VkSubmitInfo submitInfo = { VK_STRUCTURE_TYPE_SUBMIT_INFO };
109116
submitInfo.commandBufferCount = 1;
110117
submitInfo.pCommandBuffers = &commandBuffer;
111-
VKA(vkQueueSubmit(context->graphicsQueue.queue, 1, &submitInfo, 0));
112-
113-
VKA(vkDeviceWaitIdle(context->device));
118+
submitInfo.waitSemaphoreCount = 1;
119+
submitInfo.pWaitSemaphores = &acquireSemaphore;
120+
VkPipelineStageFlags waitMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
121+
submitInfo.pWaitDstStageMask = &waitMask;
122+
submitInfo.signalSemaphoreCount = 1;
123+
submitInfo.pSignalSemaphores = &releaseSemaphore;
124+
VKA(vkQueueSubmit(context->graphicsQueue.queue, 1, &submitInfo, fence));
114125

115126
VkPresentInfoKHR presentInfo = { VK_STRUCTURE_TYPE_PRESENT_INFO_KHR };
116127
presentInfo.swapchainCount = 1;
117128
presentInfo.pSwapchains = &swapchain.swapchain;
118129
presentInfo.pImageIndices = &imageIndex;
130+
presentInfo.waitSemaphoreCount = 1;
131+
presentInfo.pWaitSemaphores = &releaseSemaphore;
119132
VK(vkQueuePresentKHR(context->graphicsQueue.queue, &presentInfo));
120133
}
121134

0 commit comments

Comments
 (0)