Overv / VulkanTutorial

Tutorial for the Vulkan graphics and compute API
https://vulkan-tutorial.com
Creative Commons Attribution Share Alike 4.0 International
3.15k stars 516 forks source link

Order of the updateUniformBuffer and imagesInFlight fence #221

Open BartSiwek opened 3 years ago

BartSiwek commented 3 years ago

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.

Overv commented 3 years ago

Yeah, that should be changed.

Light7734 commented 1 year ago

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).