A state block is a group of device states. Device state is made up of render state, vertex state, pixel state, or all of the above. A state block contains a snapshot of a device's current state, or you can create a state block that records each state change that your application makes.
Choose the type of state that you want to capture, and create a state block like this:
IDirect3DStateBlock9* pStateBlock = NULL; pd3dDevice->CreateStateBlock( D3DSBT_ALL, &pStateBlock ); );
IDirect3DDevice9::CreateStateBlock creates a state block, and captures the device state. The device state is specified by the state block type in the first argument. This state can be one of the following:
Creating a state block automatically captures the current device state. The effect system uses a state block to save state. After ID3DXEffect::Begin is called, a state block is created and state is captured. When ID3DXEffect::End is called, the state block state is reapplied to the device.
To save a custom state sequence, wrap the state that you want to save in a IDirect3DDevice9::BeginStateBlock and IDirect3DDevice9::EndStateBlock pair. IDirect3DDevice9::BeginStateBlock tells the current device to set up a state block and add to it every state change that occurs until IDirect3DDevice9::EndStateBlock is called. Here's an example:
IDirect3DStateBlock9* pStateBlock = NULL; pd3dDevice->BeginStateBlock(); // Any number of state changes can occur here // SetRenderState, SetSamplerState, SetTextureStageState ... pd3dDevice->EndStateBlock( &pStateBlock );
This will save any number of state changes in any sequence to a custom stateblock. Later, when you want to use the stateblock to reset the device state, call IDirect3DStateBlock9::Apply. This will overwrite only the device state that has been captured in the state block. Any other device state that was not captured with the custom stateblock will not be changed. Once again, since the stateblock object is an interface, you will need to release it when you are done with it.