My renderer(based on D3D11) is currently built a lot like the one from the Hieroglyph3 engine.Its something like this:
class Renderer
{
static ImmediateContextManager Immediate; //Encapsulates an immediate context, inherits from DeferredContextManager
static vector<DeferredContextManager> Deferred; //Each one encapsulates a deferred context
//ID3D11Device functionality - encapsulating methods, like CreateBuffer, CreateTexture, etc.
};
In the ContextManager each time you set some state it checks if it's been set before and this way it prevents redundant API calls.However that's quite a lot of if checks involved for each object I render.I thought about sorting objects by material type, but that means first I need to sort by shader, then by texture, then by vertex buffer and pretty much anything that would require a state change in the pipeline, and that's a huge amount of sorting each frame and sometimes I still end up with cases where a redundant API calls i made.Basically:
-for automatic state monitoring you get tons of branching each time you render an object
-for sorting you have to perform a huge amount of sorts to make sure everything turns out ok
The performance hits of both these methods get noticed with large amounts of objects on scene.The sorting actually comes up way heavier when I do it(I use Quicksort3), than just using the state monitoring method.Maybe I should make some combination of the two?