Why is only one uniform buffer enough for multi-frame rendering?

Both in the SaschaWillems’s
[GitHub - SaschaWillems/Vulkan: Examples and demos for the new Vulkan API](Vulkan C++ examples and demos) and https://vulkan-tutorial.com/,only one uniform buffer is used.I wonder if the number of the uniform buffer in the above mentioned code repos is enough to guarantee to not have any synchronization problem?

They’re example code; they’re meant to show you how the API works. They’re not advice for how to build a real renderer. Use them to learn how the API works, not a set of best practices for API usage.

The examples that use a single uniform buffer do so because that’s the minimum needed to make the example work.

1 Like

In addition, note that the examples do use synchronization (if they update the uniform buffer after creation), for example see gears.cpp#L350 where vkWaitDeviceIdle is used to make sure (among other things) the device is done accessing the contents of the uniform buffer.
Of course, that is not the type of synchronization that you would want to use for a renderer that achieves high GPU utilization :wink:

But we have in SaschaWillems/Vulkan: Examples and demos for the new Vulkan API] two frames in-flight.If we only have one uniform buffer,then updating of which might cause some synchronization problems of another frame,right?For example,the first frame might still be reading from the uniform buffer when the uniform buffer is being updated.I’m refering to this specific example:https://github.com/SaschaWillems/Vulkan/tree/master/examples/inputattachments

I don’t see how this example has two frames in flight, perhaps I’m missing something? It has two sub-passes where the first one writes to an attachment and the second one reads from it (at the limited positions that are allowed by input attachments). The uniform buffer is not modified between the two sub-passes.
After submitting each frame it also calls vkWaitDeviceIdle, see vulkanexamplebase.cpp#L719, called from inputattachments.cpp#L573.

1 Like

The code in the master branch does not have multiple frames in flight. I do a vkQueueWaitIdle at the end of each frame, and that’s why it works with one uninform buffer. But that’s something you actually shouldn’t be doing with an api like Vulkan as it kills concurrency of CPU/GPU workkloads.

And that’s why I’m in the process of reworking synchronization in my samples, which will include multiple frames in flight. You can check out the current progress in this branch..

1 Like

Sorry,sir.I re-checked the repo and noticed the usage of vkWaitDeviceIdle function.I preconceived that it should be multi-frames in flight.