How should I update per-object data (like transformations and material properties) with modern OpenGL (4.6 core)?
Let’s say I have N meshes to draw each frame. Each mesh has some unique properties, let’s say a transformation matrix and some material properties. Each mesh is drawn with the same shader program. How should one organize transform and material data?
The various methods that come to my mind are the following:
Use uniforms. foreach mesh m: updateUniforms(); draw(m);
This way, however, data must be sent to the pipeline even if both the material properties and the transformation didn’t change between subsequent frames.
Use N uniform buffers, one for each mesh (or 2N, one storing transformation and the other material data, depending on how likely they are to be updated together). This way there’s no need to send the same data to the GPU each frame if everything stays the same. However, when it comes to sending new transform data, for example, each UBO should be updated individually.
Store everything into 1 uniform buffer (or 2, for the same reasons as above). This way one could batch updates, possibly resulting in better performance. Use glBindBufferRange to select which transform and material to apply to each mesh. With glBindBufferRange, however, one must consider GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, which may require you to pad each individual transform and material data units.
Same as 3. but instead of glBindBufferRange use an offset passed as a uniform:
I’m experimenting with modern OpenGL with a hobby project, and it’s a very generic renderer with almost no assumptions about its usage (lots of objects drawn with the same shader? a few almost immutable meshes each drawn with a different program? who knows…).
This is why I was wondering what are the most common/standard approaches that could be considered almost always the best choice one could make. For example, to me approach 4. sounds better than 1. (unless really specific needs I guess).
I know UBOs and SSBOs offer some interesting usages that were not possible with uniforms, like batch updates and not having to send the exact same data over and over again to the pipeline, however, I’m not sure if and how I could use those to make the drawing routine more efficient.
I don’t know, I just wanted to know what are the most common options when one has access to modern ogl functions.
How could I batch objects to reduce the number of draw calls without knowing in advance how the application is going to use the renderer (maybe batching is the responsibility of the application and not the renderer?). Could you please point me toward resources where I could get an understanding of what you are talking about?