VulkanTutorial icon indicating copy to clipboard operation
VulkanTutorial copied to clipboard

Order of the updateUniformBuffer and imagesInFlight fence

Open BartSiwek opened this issue 4 years ago • 2 comments

I thought I should ask before going through the trouble of making the PR. It seems that inside the drawFrame function the order of calls to updateUniformBuffer and vkWaitForFences(device, 1, &imagesInFlight[imageIndex], VK_TRUE, UINT64_MAX); is inverted.

For example in code/29_multisampling.cpp we have:

    void drawFrame() {
        vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);

        uint32_t imageIndex;
        VkResult result = vkAcquireNextImageKHR(device, 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 std::runtime_error("failed to acquire swap chain image!");
        }

        updateUniformBuffer(imageIndex);

        if (imagesInFlight[imageIndex] != VK_NULL_HANDLE) {
            vkWaitForFences(device, 1, &imagesInFlight[imageIndex], VK_TRUE, UINT64_MAX);
        }
        imagesInFlight[imageIndex] = inFlightFences[currentFrame];

        /// ...
    }

If imagesInFlight[imageIndex] is suppose to be used to make sure GPU processing is done before the uniform buffer with index imageIndex is updated then the call updateUniformBuffer should be after the wait.

BartSiwek avatar Jan 05 '21 16:01 BartSiwek

Yeah, that should be changed.

Overv avatar Jan 29 '21 21:01 Overv

Yep had some hard time debugging this after applying what I've learned from the compute shader chapter. Uniforms waited for the compute shader to finish before updating, but updating the uniforms updated the camera as well, which was read from the graphics shader (still in flight).

Light7734 avatar May 31 '23 15:05 Light7734