Sign in to follow this  
dreijer

State blocks in DX10

Recommended Posts

dreijer    128
I've used state blocks (http://msdn.microsoft.com/en-us/library/windows/desktop/bb206121%28v=vs.85%29.aspx) with Direct3D 9 in the past to save off the current render state, make some state changes, render some primitives, and then reset the render state to what it was previously.

How do I do something similar in Direct3D 10/11? I basically want to take the current render state (e.g. shaders, input layout) and save them off, change the state to whatever I need in order to render something else, and then set the state back to what it was before I rendered.

Share this post


Link to post
Share on other sites
iedoc    2527
just set the state before you render your stuff, and set it again before you render things that use a different state. some states include:

blending state:
OMSetBlendState();

bind the vertex buffer to IA:
IASetVertexBuffers();

bind index buffer to IA:
IASetIndexBuffer();

set shaders (pixel and vertex, but you can set other shaders with similar methods);
VSSetShader();
PSSetShader();

set the render target:
OMSetRenderTargets()

set the rasterizer state (eg. backface culling, wireframe):
RSSetState()

set constant buffers (vertex and pixel shader)
VSSetConstantBuffers()
PSSetConstantBuffers()

set shader resources (pixel shader)(eg. textures)
PSSetShaderResources();

set sampler state (pixel shader)
PSSetSamplers()

All these are methods of the device context interface, unlike d3d9 that only has a device interface for everything (device context is used for rendering and stuff, and the regular device is used for the creation of things, if i'm not mistaking)

Share this post


Link to post
Share on other sites
dreijer    128
Well, so the problem here is that I don't actually set the initial render state myself. I'm essentially doing an overlay over the Direct3D application, which means I'm piggy backing off of the application's existing render state (i.e. whatever the application had already set) and I want to make sure that when I'm done rendering, the state is set back to what it was before I did my thing,

Share this post


Link to post
Share on other sites
iedoc    2527
to set the state back to the default, you can just pass NULL as the parameter. the application should set the state to whatever it needs before it draws it's stuff though. it's possible it set the state during initialization time, so that when you change the state, it never goes back to the state that was set in the initialization of the scene. In that case, you'll have to find where it sets the state and just set it back after you are done drawing your stuff. But, that's bad practice to assume the state does not change throughout the scene. the state should always be explicitly set before you draw things that need that specific state (at least in my opinion). Directx as you probably know is a "state machine", which means once you set a state, it will never change until you explicitly set it to something else

another possibility is that it just uses the default state, and never actually sets the state at all, so once you set your state, it doesn't actually change. So just try setting the state back to NULL to see if that helps

Share this post


Link to post
Share on other sites
dreijer    128
[quote name='iedoc' timestamp='1330106689' post='4916271']
to set the state back to the default, you can just pass NULL as the parameter. the application should set the state to whatever it needs before it draws it's stuff though. it's possible it set the state during initialization time, so that when you change the state, it never goes back to the state that was set in the initialization of the scene.[/quote]

That's pretty much the core problem. Having done this for quite a bunch of Direct3D 9 games, I know for a fact that some games only set their state initially rather than on a per-frame basis, and thus modifying the state so that I can render my stuff will mess up the game itself. One thing I've learned when interacting with games is that you can never expect game developers to Do The Right Thing (TM) in code and you always have to consider the worst case scenario.

It's therefore really important that I'm able to set back the game's render state after I'm done rendering my things. So, there's no way to do this automatically for DX10 like I can do in DX9 with state blocks?

The only solution, then, would be to hook all the state functions that I change, such that I know what value to set back once I'm done rendering. That's a bunch of work though...

Share this post


Link to post
Share on other sites
mhagain    13430
All D3D10/11 states have a "Get" accessor, so you could - for example, with rasterizer states - use RSGetState to access the current state, then GetDesc on the state object if you need it's description.

I haven't benchmarked the use of such a Get call, but I would expect that all you're doing is getting a handle to the object from the runtime (rather than reading back data from the GPU) so it should be OK.

Don't forget to Release the state object you get back when done as the Get call will AddRef it.

With D3D11 I'd create a deferred context, record my stuff in a command list, then play it back and destroy the context. FinishCommandList can be told to save and restore the previous states for you, so you won't need to worry about any of that.

Share this post


Link to post
Share on other sites
dreijer    128
[quote name='mhagain' timestamp='1330108471' post='4916280']
I haven't benchmarked the use of such a Get call, but I would expect that all you're doing is getting a handle to the object from the runtime (rather than reading back data from the GPU) so it should be OK.
[/quote]

Right, I haven't benchmarked them either so they might just be really simple wrappers and pretty cheap to call each frame. I just didn't think of going down that road since I know OpenGL's GetXX() functions are really slow and not recommended to call on a per-frame basis (although I know the DIrectX API is fundamentally different :),

[quote name='mhagain' timestamp='1330108471' post='4916280']With D3D11 I'd create a deferred context, record my stuff in a command list, then play it back and destroy the context. FinishCommandList can be told to save and restore the previous states for you, so you won't need to worry about any of that.
[/quote]

That's really cool. That's exactly what I was looking for. I need to read up on deferred contexts, but ideally I'd be able to create the context once and reuse each frame.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this