State blocks in DX10

Started by
6 comments, last by dreijer 12 years, 1 month ago
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.
Advertisement
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)
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,
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

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.


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...
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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.


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.


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 :),

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.


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.
In case others are interested, I stumbled upon an interface called ID3D10StateBlock, which presumably does exactly what I want.

This topic is closed to new replies.

Advertisement