Eliminating Redundant State Changes

In earlier versions of Microsoft® Direct3D® Retained Mode, Retained Mode could not assume render and light states would remain the same from one call to the next. This led to many redundant state changes in a typical application because Retained Mode set the state to a value equal to its current value. The IDirect3DRMDevice3 interface introduces several methods that can be used to eliminate these redundant changes for a modest performance boost: GetStateChangeOptions, LightStateChange, RenderStateChange, and SetStateChangeOptions. To use these methods, follow these steps:

  1. Turn on the redundant state change check by using the following two methods on the IDirect3DRMDevice3 interface. This indicates that you will not change any render or light state without notice.
    Dev3->SetStateChangeOptions(D3DRMSTATECHANGE_RENDER, 0,
                                D3DRMSTATECHANGE_NONVOLATILE);
    Dev3->SetStateChangeOptions(D3DRMSTATECHANGE_LIGHT, 0, D3DRMSTATECHANGE_NONVOLATILE);
    
  2. When you change a render or light state (either through an execute buffer or a draw primitive), you must inform Direct3D Retained Mode of the change so that the state cache is updated. You can do this in two ways. You can have Retained Mode change the state for you (and prevent redundant changes) or you can inform Retained Mode that you have changed the state to a new value. Both use the LightStateChange and RenderStateChange methods.

For example, the following code shows you how to change the texture handle and update the state cache.

Dev3->RenderStateChange(D3DRENDERSTATE_TEXTUREHANDLE, hTex, 0UL);

If you have changed the state and just want Retained Mode to update its state cache, use the UPDATEONLY flag, as shown in the following:

Dev3->RenderStateChange(D3DRENDERSTATE_TEXTUREHANDLE, hTex,
                        D3DRMSTATECHANGE_UPDATEONLY);

If you are going to be changing a state value many times and do not want Retained Mode to keep track of current values, you can turn off redundant state checking for the specified state with the following code. However, this is not recommended because you will lose performance benefits.

Dev3->SetStateChangeOptions(D3DRMSTATECHANGE_RENDER, D3DRENDERSTATE_TEXTUREHANDLE,
                            D3DRMSTATECHANGE_VOLATILE);

Top of Page Top of Page
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.