CU_LostDevice.zip (28.2 KiB, 2,325 hits)

When a device is in fullscreen mode, many of the resources that the application is using (textures, buffers, etc.) are stored in a certain place in video memory. When the user Alt-Tabs to go to another application, video memory may be given over to other applications and the original data may be lost. Function calls will still succeed, but nothing will be rendered. This is what is known as a lost device. Lost devices may also be caused by minimizing the application window.

When a device is lost, the IDirect3DDevice9::TestCooperativeLevel function will return a fail code. Once we know the device is lost we have to check if the device can be reset.
If this function returns D3DERR_DEVICENOTRESET, then we can reset the device and continue on with the program. Otherwise we aren’t able to reset the device just yet, so we Sleep for a bit and return from the method.

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Summary: Renders the current frame.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void CFramework::OnRenderFrame()
{
if ( !m_active || (m_pGraphics->GetDevice() == NULL) )
{
return;

}

// Check for lost device
HRESULT result = m_pGraphics->GetDevice()->TestCooperativeLevel();
if ( FAILED( result ) )
{

if ( result == D3DERR_DEVICELOST )
{
Sleep( 50 );
return;

}
else
{

OnLostDevice();
if ( m_pGraphics->Reset() == D3DERR_DEVICELOST )
{
// Device is lost still
Sleep( 50 );
return;

}
else
{

OnResetDevice();

}

}

}

if ( m_pGameApp != NULL )
{

m_pGameApp->OnRenderFrame( m_pGraphics->GetDevice() );

}

}

Every frame, we check if the device is lost or not. If the device is lost, weI check if weI can reset it by calling IDirect3DDevice9::TestCooperativeLevel.
If the returned value is D3DERR_DEVICENOTRESET, then we can reset the device and carry on with the program. The other value that can be returned is D3DERR_DEVICELOST. This error code means that the device is lost but we can’t reset it yet, which is most likely because the window still doesn’t have focus. In this case, weI just return to prevent any rendering from occuring.

And that’s all there is to it!

Laters, C.