Chad Vernon
  • Home
  • Reel/Resume
  • Work
  • Resources
  • About
  • Contact
Home » Resources » DirectX 9 » Responding to a Lost Device
sideBar

Search

Categories

  • CG
  • cvxporter
  • Maya
    • API
    • Plug-ins
  • Personal

Archives

  • August 2010
  • July 2010
  • May 2010
  • April 2010
  • March 2010
  • December 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007

Rss

  • Main Entries RSS
  • Comments RSS

Responding to a Lost Device

  CU_LostDevice.zip (28.2 KiB, 1,572 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.

Home » Resources » DirectX 9 » Responding to a Lost Device

© 2010 Chad Vernon