Added a batch renderer for quads under "extras".
Short version:
This batch renderer stores the quads geometry you build up while rendering your frame and then draws it with a (potentially) single draw call.
Longer version:
Every time you have do draw a quad, you send the vertex data to the batch renderer instead of issuing a drawing call.
The renderer is dimensioned at the start, when you specify how much space you want to reserve in your memory buffer for your quads. The renderer stores the vertex data, the colors (including alpha for transparency) and the OpenGL texture ID required by the quad (if the quad is textured).
Every time a new texture ID is seen by the renderer, it verifies if it has already reserved a texture unit for it.
If the answer if yes it simply puts the texture unit number in the vertex data buffer.
If the texture is a new one it reserves a new texture unit for it and decrements the number of available texture units. and then puts it in the vertex data buffer.
While you pump your data into it, the renderer limits may be exceeded.
For example you dimensioned it for 1000 quads but you are pushing 1300 quads per frame,
In that case the renderer pause at 1000, executes a flush of its data and a single drawing call to send it to the GPU. Then it resets itself and goes on with accepting new data,
This is transparent to you, and the ending result is you will have executed two drawing calls (1000 quads + 300 quads) instead of just one to draw your stuff.
The other case causing a flush it's if you run out of texture units.
Suppose your GPU supports 16 texture units for the fragment shader stage.
If you are drawing 900 quads using just 10 textures, no flush is required. But if you use more textures the renderer as in the case above has to pause, flush, free the texture units and go on from there.
Again this is transparent to you.
That's why if you have a lot of different textured quads it's a good idea to use a texture atlas.
You make a very big texture with many smaller images inside it and you draw your quads grabbing the texels from there. The very big texture will require a single texture unit instead of many,
Incidentally the RenderText modules included as extras in SGL are using a texture atlas to store the characters of a single font, exactly for this reason.
The batch renderer includes two types of DrawQuad().
One is for simple colored quads, optionally with some transparency and optionally textured with a single texture.
The other version supports the use of atlases, can do all the above but it has a further parameter specifying the normalized texture coordinate defining the area inside the big texture to sample from.
To see an example to how to use the "atlas" version look at the modified version of the RenderText module, named "RenderTextBatch_330". It's the same as "RenderText_330" but uses the batch renderer instead of locally buffering the text strings by itself.
The batch render dynamically pumping textured quads (10 unique textures used) with a semi-transparent extra quad moving around.
Note the single drawing call in the stats.
