Stategies to load buffers for dynamically adding and removing meshes in a scene

I have a specific scenario where I need to be able to dynamically add and remove meshes from the scene during runtime from a list of a very large number of meshes. Only a small number of meshes or all of them may get used, it’s impossible to say which ones upfront.

One strategy would be to upload all data upfront in one big immutable buffer. But this will lead to unnecessarily long loading times on startup and large memory footprint, especially if only few meshes will be added to the scene.

Would it make sense to instead create one big mutable buffer and each time a new mesh is added reallocate the buffer and reupload all the vertex data together including the new data? Alternatively upload each mesh into a separate buffer, and each time a new mesh is created, create a immutable buffer for that mesh? Would the later be a better choice despite resulting in more buffer state changes?

Allocate a buffer that’s likely to be large enough. Append data to the buffer as meshes are loaded. If you run out of space, allocate a second buffer.

Having unused space in a buffer doesn’t matter unless its depriving you of memory you need for other data (e.g. textures).

Thank you for the reply!

How would this compare to implementing something similar to how dynamic arrays work? Allocate a buffer of certain size. Once it’s filled double the size and copy the data with GL_ARB_copy_buffer?

This approach seems much more flexible to me, but I’m not sure if it is as practical on GPU as it is on CPU.

It works, but the copy is probably unnecessary. While minimising the number of draw calls is overall good practice, you don’t need to draw the entire scene in one call. Using a single buffer is only likely to be advantageous if the meshes need to be drawn in a specific order and using multiple buffers would force you to use many more draw calls.

I would suggest you look into writing a Free List / Dynamic Array class for your Vertex & Index Buffers. You could make the class contain both the CPU and GPU data, that is what I did for my engine.