To give you an idea of what I'm talking about, consider rendering a scene that uses multiple vertex and fragment shaders. Is it best to:
- Sort each object in the scene by the texture used first (so that those using the same texture are rendered sequentially so the texture can be reused)
- Then sort each object in the scene by the shader program (so that you don't have to call [font=courier new,courier,monospace]glUseProgram()[/font] (or the D3D equivalent) on every object in the scene)
Or is it better to do it the other way around (sort by shader program, then by texture)? Or are neither of these the best way to sort objects, and is there a better way to do it? I don't want to overcomplicate this and do too much work on the CPU, as it has other things it needs to do other than sorting objects in a scene.
When sorting these things, would the sorting be done in some [font=courier new,courier,monospace]scene[/font] object that stores references to all the renderable objects in the scene, sorts them, and draws them?
And finally, how can these ideas best be translated into a 2D game (I'm also curious about these answers for a 3D game though too)? In 2D games, the depth buffer is usually disabled, which makes the sorting/drawing order significant. There are so many transparent parts to textures and alpha blending going on that the order in which things are drawn need to be carefully controlled. Would you do the sorting above first, and then sort by layer number to ensure the correct drawing order? And if two sprites have the same layer number, you just have to make no guarantees about which will be drawn on top of the other (though you'd use a stable sorting algorithm, so that they don't alternate every frame which one is on top of the other)?
Sorry for the eleventy billion questions, but a huge thanks in advance for any responses!