Using same uniform buffer for multiple frames in flight not causing error or race condition

I have a very basic setup with a single uniform buffer per frame that contains data that changes every frame like the VP matrix etc. I also have a big SSBO per frame that contains an array of per-object data like the model matrix. My engine is running in a double-buffering configuration with 2 swapchain images and so until now I had 2 instances of these buffers with 2 descriptor sets, each bound when rendering to the appropriate swap chain image.
I was under the assumption that this was done to avoid race conditions with the CPU writing to the buffer while the Shaders are reading from it. But now when I tried with just a single instance of these things, things still work fine without any race conditions or validation errors.

So my question is why is updating the buffer while the GPU is using it for rendering a frame not causing any issues? I am updating the buffers by mapping them to CPU host memory before recording the command buffers for each frame.

Vulkan only guarantees that valid use of the API works. It doesn’t guarantee that invalid use of the API fails. You cannot play “guess and check” with Vulkan and expect to get meaningful results.

Just because your program “works” does not mean that it is valid. Maybe you got lucky. Maybe the way you modify the buffer in such a way that the GPU can’t access it. Maybe the GPU is outrunning the CPU and thus is finished before the CPU even tries to update it. Maybe you’re accidentally synchronizing things in some way, or have inserted some “WaitIdle” call that you’re not mentioning.

It’s irrelevant. Unless you synchronize access in some way, the code as you have described it is invalid.

1 Like