Skip to content

Commit

Permalink
Correct the fence selection algo.
Browse files Browse the repository at this point in the history
  • Loading branch information
ravi688 committed Feb 10, 2024
1 parent 7b6797c commit 52d6793
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
8 changes: 4 additions & 4 deletions include/PlayVk/PlayVk.h
Original file line number Diff line number Diff line change
Expand Up @@ -860,15 +860,15 @@ static VkSemaphore pvkCreateSemaphore(VkDevice device)
return semaphore;
}

static VkFence pvkCreateFence(VkDevice device)
static VkFence pvkCreateFence(VkDevice device, VkFenceCreateFlags flags)
{
VkFenceCreateInfo cInfo = { .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO };
VkFenceCreateInfo cInfo = { .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .flags = flags };
VkFence fence;
PVK_CHECK(vkCreateFence(device, &cInfo, NULL, &fence));
return fence;
}

static void pvkSubmit(VkCommandBuffer commandBuffer, VkQueue queue, VkSemaphore wait, VkSemaphore signal)
static void pvkSubmit(VkCommandBuffer commandBuffer, VkQueue queue, VkSemaphore wait, VkSemaphore signal, VkFence signalFence)
{
VkPipelineStageFlags waitStage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
VkSubmitInfo info =
Expand All @@ -882,7 +882,7 @@ static void pvkSubmit(VkCommandBuffer commandBuffer, VkQueue queue, VkSemaphore
.commandBufferCount = 1,
.pCommandBuffers = &commandBuffer
};
PVK_CHECK(vkQueueSubmit(queue, 1, &info, VK_NULL_HANDLE));
PVK_CHECK(vkQueueSubmit(queue, 1, &info, signalFence));
}

static bool pvkPresent(uint32_t index, VkSwapchainKHR swapchain, VkQueue queue, VkSemaphore wait)
Expand Down
54 changes: 29 additions & 25 deletions source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,22 @@ static void recordCommandBuffers(u32 width, u32 height, VkCommandBuffer* command
}
}

static bool getAvailableFence(VkDevice device, VkFence* fences, uint32_t fenceCount, VkFence* outFence)
{
for(uint32_t i = 0; i < fenceCount; i++)
{
VkResult result = vkWaitForFences(device, 1, &fences[i], VK_TRUE, 0);
if(result == VK_SUCCESS)
{
PVK_CHECK(vkResetFences(device, 1, &fences[i]));
*outFence = fences[i];
return true;
}
else if(result != VK_TIMEOUT) PVK_CHECK(result);
}
return false;
}

int main()
{
VkInstance instance = pvkCreateVulkanInstanceWithExtensions(2, "VK_KHR_win32_surface", "VK_KHR_surface");
Expand Down Expand Up @@ -554,42 +570,27 @@ int main()
planeGeometry,
boxGeometry);

PvkSemaphoreFIFOPool* semaphorePool = pvkCreateSemaphoreFIFOPool(logicalGPU, 20);
PvkSemaphoreFIFOPool* semaphorePool = pvkCreateSemaphoreFIFOPool(logicalGPU, 6);

VkFence fences[3];
for(uint32_t i = 0; i < 3; i++)
fences[i] = pvkCreateFence(logicalGPU);
fences[i] = pvkCreateFence(logicalGPU, VK_FENCE_CREATE_SIGNALED_BIT);

float angle = 0;
uint32_t fIndex = 0;
uint32_t fUnsignaledCount = 3;
/* Rendering & Presentation */
while(!pvkWindowShouldClose(window))
{
VkSemaphore imageAvailableSemaphore = pvkSemaphoreFIFOPoolAcquire(semaphorePool);
uint32_t index = 0;
PVK_CHECK(vkAcquireNextImageKHR(logicalGPU, swapchain, UINT64_MAX, imageAvailableSemaphore, fences[fIndex++], &index));
if(fIndex >= fUnsignaledCount)
VkFence fence;
if(!getAvailableFence(logicalGPU, fences, 3, &fence))
{
PVK_CHECK(vkWaitForFences(logicalGPU, 1, &fences[0], VK_TRUE, UINT64_MAX));
PVK_CHECK(vkResetFences(logicalGPU, 1, &fences[0]));
fUnsignaledCount = 1;
VkResult result = vkWaitForFences(logicalGPU, 1, &fences[1], VK_TRUE, FENCE_WAIT_TIME);
if(result != VK_TIMEOUT)
{
PVK_CHECK(result);
PVK_CHECK(vkResetFences(logicalGPU, 1, &fences[1]));
fUnsignaledCount = 2;
result = vkWaitForFences(logicalGPU, 1, &fences[2], VK_TRUE, FENCE_WAIT_TIME);
if(result != VK_TIMEOUT)
{
PVK_CHECK(result);
PVK_CHECK(vkResetFences(logicalGPU, 1, &fences[2]));
fUnsignaledCount = 3;
}
}
fIndex = 0;
pvkWindowPollEvents(window);
continue;
}
VkSemaphore imageAvailableSemaphore = pvkSemaphoreFIFOPoolAcquire(semaphorePool);
uint32_t index;
PVK_CHECK(vkAcquireNextImageKHR(logicalGPU, swapchain, UINT64_MAX, imageAvailableSemaphore, fence, &index));

angle += 0.1f DEG;
objectData->modelMatrix = pvkMat4Transpose(pvkMat4Transform((PvkVec3) { 0, 0, 0 }, (PvkVec3) { 0, angle, 0 }));
Expand All @@ -598,7 +599,7 @@ int main()

VkSemaphore renderFinishSemaphore = pvkSemaphoreFIFOPoolAcquire(semaphorePool);
// execute commands
pvkSubmit(commandBuffers[index], graphicsQueue, imageAvailableSemaphore, renderFinishSemaphore);
pvkSubmit(commandBuffers[index], graphicsQueue, imageAvailableSemaphore, renderFinishSemaphore, VK_NULL_HANDLE);

// present the output image
if(!pvkPresent(index, swapchain, presentQueue, renderFinishSemaphore))
Expand Down Expand Up @@ -713,6 +714,9 @@ int main()

PVK_CHECK(vkDeviceWaitIdle(logicalGPU));

for(uint32_t i = 0; i < 3; i++)
vkDestroyFence(logicalGPU, fences[i], NULL);
pvkDestroySemaphoreFIFOPool(logicalGPU, semaphorePool);
delete(clearValues);
delete(objectData);
delete(camera);
Expand Down

0 comments on commit 52d6793

Please sign in to comment.