Reputation: 11
I'm writing a Vulkan program. I have a problem with image being sometimes displayed in wrong order. Here is the part I suppose where things to go wrong. For information, I have 2 frames in flight (MAX_FRAMES_IN_FLIGHT), and 3 images (swapChainSupport.capabilities.minImageCount + 1). The two function below are called in my main loop:
// main loop content (simplified)
vcSwapChain->acquireNextImage(¤tImageIndex);
//... Do render stuff, set command buffer
vcSwapChain->submitCommandBuffers(&commandBuffer, ¤tImageIndex);
VkResult FwSwapChain::acquireNextImage(uint32_t* imageIndex) {
vkWaitForFences(device.getDevice(), 2, frameInFlightFences.data(), VK_TRUE, UINT64_MAX);
VkResult result = vkAcquireNextImageKHR(device.getDevice(), vkSwapChain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, imageIndex);
return result;
}
VkResult FwSwapChain::submitCommandBuffers(const VkCommandBuffer* buffers, uint32_t* imageIndex) {
VkSubmitInfo submitInfo = {};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
VkSemaphore waitSemaphores[] = { imageAvailableSemaphores[currentFrame] };
VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = waitSemaphores;
submitInfo.pWaitDstStageMask = waitStages;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = buffers;
VkSemaphore signalSemaphores[] = { renderFinishedSemaphores[currentFrame] };
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = signalSemaphores;
vkResetFences(device.getDevice(), 1, &frameInFlightFences[currentFrame]);
if (vkQueueSubmit(device.getGraphicsQueue(), 1, &submitInfo, frameInFlightFences[currentFrame]) != VK_SUCCESS) {
throw std::runtime_error("failed to submit draw command buffer!");
}
VkPresentInfoKHR presentInfo = {};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = signalSemaphores;
VkSwapchainKHR swapChains[] = { vkSwapChain };
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = swapChains;
presentInfo.pImageIndices = imageIndex;
auto result = vkQueuePresentKHR(device.getPresentQueue(), &presentInfo);
currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
return result;
}
If i set MAX_FRAMES_IN_FLIGHT to 1, then I haven't the problem.
Upvotes: 1
Views: 26