I don't understand exactly how batching works.
What I have understood: Batching is useful to regroup similar elements together and draw them in one draw call (performance++), currently drawing an element is 90% of the time in my application.
How I do that: I need to regroup elements with the same Material (Shader, Texture, …) and with his type (static or dynamic geometry).
Here is my rendering structure:
// Get Visibles spacials.
SpacialSet* spacials = this->scene;
SpacialSet* visibleSpacials = this->culler->execute(spacials, this->scene->camera);
// Render the scene (this method call "draw", for each spacial)
this->renderer->render(visibleSpacials, this->scene->camera);
How I draw an element:
void Renderer::draw( Spacial* spacial )
{
this->setGeometry(spacial->geometry); // Bind buffers
this->setMaterial(spacial->material); // Send data to shader/bind textures.
this->drawPrimitive( material->drawingMode, geometry->indicesCount);
}
How I think I will do it:
I should create a smart "Batch" class, his goal would be to create a new mesh (large VBO, pointer to textures, material) who will be a merge of all Spacials with the same apparence.
- Look current mesh and search a good batch or create a new if not found
- Merge it with previous elements
- Hide current mesh/ignore drawing
- Draw the batch/big mesh.
My questions:
Does my approach look good?
How do I manage positions per mesh?
Do I need to update my batch each frame?
How does it work with culling? If I have a static batch with all my sprites and 70% of sprites in this batch are out of screen, should I draw this batch ?
Do I need to do batching before or after culling?