Hello.
I have the following render code:
void MainWindow::drawFrame()
{
//Waiting for the fence to be ready(Waiting for the command buffer to finish executing for the frame)
vkWaitForFences(logicalDevice, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
//Acquire image from swapchain
uint32_t imageIndex;
//UINT64_MAX disables timeout, timeout is infinite
VkResult result = vkAcquireNextImageKHR(logicalDevice, swapchain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
if (result == VK_ERROR_OUT_OF_DATE_KHR) {
recreateSwapchain();
return;
}
else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
throw MakeErrorInfo("Failed to acquire swapchain image!");
}
// Check if a previous frame is using this image (i.e. there is its fence to wait on)
if (imagesInFlight[imageIndex] != VK_NULL_HANDLE) {
vkWaitForFences(logicalDevice, 1, &imagesInFlight[imageIndex], VK_TRUE, UINT64_MAX);
}
// Mark the image as now being in use by this frame
imagesInFlight[imageIndex] = inFlightFences[currentFrame];
glfwGetWindowPos(window, &windowXPos, &windowYPos);
imguiUpdateCommandBuffers(imageIndex);
updateUniformBuffer(imageIndex);
//Submitting the command buffer
//Each entry in the waitStages array corresponds to the semaphore with the same index in pWaitSemaphores.
VkSemaphore waitSemaphores[] = { imageAvailableSemaphores[currentFrame] };
std::vector<VkPipelineStageFlags> waitStages = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
//The first three parameters specify which semaphores to wait on before execution begins
//and in which stage(s) of the pipeline to wait.
//We want to wait with writing colors to the image until it's available, so we're specifying the stage of the graphics pipeline that writes to the color attachment.
submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = waitSemaphores;
submitInfo.pWaitDstStageMask = waitStages.data();
std::vector<VkCommandBuffer> currentCommandBuffers = { commandBuffers[imageIndex], imguiCommandBuffers[imageIndex] };
submitInfo.commandBufferCount = 2;
submitInfo.pCommandBuffers = currentCommandBuffers.data();
std::vector<VkSemaphore> signalSemaphores = { renderFinishedSemaphores[currentFrame], imguiRenderFinishedSemaphores[currentFrame] };
submitInfo.signalSemaphoreCount = 2;
submitInfo.pSignalSemaphores = signalSemaphores.data();
//Restore the fence to the unsignaled state
vkResetFences(logicalDevice, 1, &inFlightFences[currentFrame]);
//Start executing the command buffer
if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) {
throw MakeErrorInfo("Failed to submit draw command buffer!");
}
VkPresentInfoKHR presentInfo{};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = 2;
presentInfo.pWaitSemaphores = signalSemaphores.data();
VkSwapchainKHR swapchains[] = { swapchain };
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = swapchains;
presentInfo.pImageIndices = &imageIndex;
presentInfo.pResults = nullptr; // Optional
result = vkQueuePresentKHR(presentQueue, &presentInfo);
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebufferResized) {
framebufferResized = false;
recreateSwapchain();
if (minimizedWindowNeedClose) {
return;
}
}
else if (result != VK_SUCCESS) {
throw MakeErrorInfo("Failed to present swap chain image!");
}
currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
}
Let’s say I want to measure the time elapsed from the moment vkAcquireNextImageKHR() is called, until the command buffer execution is completed(the moment when inFlightFences[currentFrame] is signaled).
I don’t want to use vkWaitForFences() after call vkQueueSubmit() because I want to do the next frame.