Hello,
I've stumbled upon Urho3D engine and found that it has a really nice and easy to read code structure.
I think the graphics abstraction looks really interesting and I like the idea of how it defers pipeline state changes until just before the draw call to resolve redundant state changes.
This is done by saving the state changes (blendEnabled/SRV changes/RTV changes) in member variables and just before the draw, apply the actual state changes using the graphics context.
It looks something like this (pseudo):
void PrepareDraw()
{
if(renderTargetsDirty)
{
pD3D11DeviceContext->OMSetRenderTarget(mCurrentRenderTargets);
renderTargetsDirty = false
}
if(texturesDirty)
{
pD3D11DeviceContext->PSSetShaderResourceView(..., mCurrentSRVs);
texturesDirty = false
}
....
//Some more state changes
}
This all looked like a great design at first but I've found that there is one big issue with this which I don't really understand how it is solved in their case and how I would tackle it.
I'll explain it by example, imagine I have two rendertargets: my backbuffer RT and an offscreen RT.
Say I want to render my backbuffer to the offscreen RT and then back to the backbuffer (Just for the sake of the example).
You would do something like this:
//Render to the offscreen RT
pGraphics->SetRenderTarget(pOffscreenRT->GetRTV());
pGraphics->SetTexture(diffuseSlot, pDefaultRT->GetSRV())
pGraphics->DrawQuad()
pGraphics->SetTexture(diffuseSlot, nullptr); //Remove the default RT from input
//Render to the default (screen) RT
pGraphics->SetRenderTarget(nullptr); //Default RT
pGraphics->SetTexture(diffuseSlot, pOffscreenRT->GetSRV())
pGraphics->DrawQuad();
The problem here is that the second time the application loop comes around, the offscreen rendertarget is still bound as input ShaderResourceView when it gets set as a RenderTargetView because in Urho3D, the state of the RenderTargetView will always be changed before the ShaderResourceViews (see top code snippet) even when I set the SRV to nullptr before using it as a RTV like above causing errors because a resource can't be bound to both input and rendertarget.
What is usually the solution to this?
Thanks!