Jump to content
  • Advertisement
Sign in to follow this  
bsh_aoa

How to process "Device Lost" ?

This topic is 4528 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When I use D3D to program, I am puzzled about "Device Lost". My application is full-screen mode. To process "Device lost"(may be caused by Pressing ALT+TAB), what should I to do? I think : //in MsgProc() ... switch (msg) { case WM_ACTIVATEAPP: if (!wParam) g_bActive = false; else if (!g_bActive) { g_bActive = true; D3DPRESENT_PARAMETERS d3dpp; d3dpp.BackBufferWidth = 800; d3dpp.BackBufferHeight = 600; d3dpp.BackBufferCount = 1; d3dpp.BackBufferFormat = D3DFMT_R5G6B5; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D16; d3dpp.Flags = 0; d3dpp.hDeviceWindow = hWnd; d3dpp.Windowed = FALSE; d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; d3dpp.MultiSampleQuality = 0; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; g_pd3dDevice->Reset(&d3dpp); } return 0; //other case ..... } //---------------------------------------------------------------------------- Is it enough ? Anything else to do? thanks.

Share this post


Link to post
Share on other sites
Advertisement
I suggest that you take a good look at the documentation as it's rather straight forward on the topic.

Let me run you through it.
When you have a window that looses focus or minimized you might loose your device. What needs to be done is that you have to check whether the device has been lost. This is done at the Present method which will return a D3DERR_DEVICELOST hresult when the device is lost.

I usually set a boolean flag indicating whether the device has been lost and I don't continue with rendering if the flag is set.

You can use the IDirect3DDevice9::TestCooperativeLevel() method to start polling the device whether you are able to Reset the device and regain the device. IDirect3DDevice9::TestCooperativeLevel() returns 3 different errors which include the one we are interested with, which is D3DERR_DEVICENOTRESET. Once you encounter this error you are able to call the IDirect3DDevice9::Reset() method.

Note that the resources in the D3DPOOL_DEFAULT needs to be released.

PS: consult the documentation for more in detail information about Lost Devices.

I hope this helps.
Take care.

Share this post


Link to post
Share on other sites
My solution isn't pretty, but it works well and is easy to get running - simply release everything and reallocate it on the WM_ACTIVATEAPP events that gives back control to the app. This however does a lot of unneeded reloading of data, but looking at how many of the commercial games freeze and reloads everything after an Alt+Tab just as slowly as when they normally load a map I'd guess it's a technique that is considered acceptable to use... The other device lost handling mechanisms provided by D3D are IMO too complex so I haven't even bothered about learning them, and you really only need them if you have extreme amounts of volatile data you need to reload.

Share this post


Link to post
Share on other sites
First of all you need to know is your device lost, so few ways to test it :

1) Take return value from Present - function, if it is D3DERR_DEVICELOST then you need to restore the device. Example :


HRESULT hr;
hr = d3ddevice->Present(NULL, NULL, NULL, NULL);

if (hr == D3DERR_DEVICELOST)
{
DeviceLost = true;
}


2) Take return value from TestCooperativeLevel - function, if it is D3DERR_DEVICELOST then you need to restore the device. Example :


HRESULT hr;

hr = d3ddevice->TestCooperativeLevel();

if (hr == D3DERR_DEVICELOST)
{
DeviceLost = true;
}


Okay, now you know how to test if the device is lost, but how to restore it? Well, it works like this :

Note: If you are using D3DPOOL_MANAGED for your resources, you don't need to worry about restoring them, because DX will do it for you. But you still need to reset the device (see Second thing to do).

First, release all resources (textures, buffers, etc.) that are using D3DPOOL_DEFAULT.

Second, reset the device, example :


d3ddevice->Reset(&saved_params);


saved_params = your D3DPRESENT_PARAMETERS.

Last thing to do is to restore all resources that you released (reload textures, recreate buffers and so on..).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!