1 uniform buffer - N meshes. How to reallocate?

Suppose I’m rendering simple cubes at random positions.

Having 3 of them as the starting number of cubes, the application acquires a VkBuffer handle and binds it to a VkDeviceMemory in order to store the model matrices of all cubes consecutively in it, and which is later on accessed by the shader via the descriptor set. Let’s say the VkDeviceMemory has just enough memory for those 3 cubes.

What I want to do is, every time the user presses a key, a new cube should pop up somewhere. My question is, how should I go about resizing that memory? Could you provide an overview of the steps I should go through?

I realize I could use separate VkBuffer/VkDeviceMemory for each cube but I do not want to do that. Everywhere I read it is stated that’s sort of an anti-pattern. I could also allocate more and more buffers, and bind them all sequentially when drawing - but again, as stated somewhere, the number of buffers should not grow with the number of meshes.

I also realize that allocating many small chunks is a bad idea. I’m just using small numbers for the sake of argument, what really is making me scratch my head is the actual process of reallocation, and how to do it. I’m pretty sure I need to do some synchronization while the newly allocated buffer gets swapped with the previous one but… that’s how far I reach.

Could you provide an overview on how to handle it? Besides the uniform buffer, I am guessing the descriptor set is involved as well…

Thank you!

>> how should I go about resizing that memory

you can only create new uniform buffer with new size and assign it to existing descriptor set, after that you need to update command buffer (VkCmdBeginCommandBuffer/VkCmdEndCommandBuffer)

but with new size uniform buffer you need also update shader

If you know inittially max objects that you need to draw you can use dynamic uniform buffer with size for max objects and update dynamic uniform buffer object related data

But I think the best way is to create the uniq uniform buffer for each object

Also you can in rendering/drawing loop update one uniform buffer for each object and call vkCmdDraw for each object in loop (loop(updateObjectData; vkCmdDraw ))