Sign in to follow this  

recovering from lost d3d device

This topic is 3587 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

What is the correct way to detect and recover from loose the d3d device? I tried the method below but it seems to not restore the dvice correctly as it cause a crash at a later point after the device is lost... The crash seems to happen as soon as something trys to write to the vertex buffer after the device is lost (eg when I go to draw say a sprite).
...draw code...
	d3d_device->EndScene();
	if(D3DERR_DEVICELOST == d3d_device->Present(NULL, NULL, NULL, NULL))
	{
		d3d_device->Reset(d3d_parameters);
	}

Share this post


Link to post
Share on other sites
First off, recovering from a lost device may not succeed. You need to check return values to see if your recovery process worked. If it didn't, don't call any DirectX functions other than TestCooperativeLevel() until it does.

Secondly, when you lose the device, you should free resources. D3DX interfaces tend to have a OnLostDevice() function. You should call those if you are using any of them. You should also release any render targets, stencil surfaces, swap chains, etc.

Third, you can use TestCooperativeLevel() to check to see if the device is in a state that it can be reset. Once you've lost your device periodically call TestCooperativeLevel() until it returns D3DERR_DEVICENOTRESET. Once it does, then you can do the process of reseting.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
You should call those if you are using any of them. You should also release any render targets, stencil surfaces, swap chains, etc.

More specifically, you need to release any resource that was created in D3DPOOL_DEFAULT. After you've correctly reset the device, recreate your resources.

Share this post


Link to post
Share on other sites
Ive put a check in so that my other d3d functions won't run if a device is lost (stopping the crash) but I still can't get it to reset...

The only stuff d3d has loaded is some textures:

if (FAILED(D3DXCreateTextureFromFileEx
(d3d_device, path, 0, 0, 1, 0,
D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_DEFAULT,
colour, &scr_info, NULL, &texture)))return -1;




"The application need not release resources in the D3DPOOL_MANAGED or D3DPOOL_SYSTEMMEM memory classes." so I shouldn't need to destroy my textures...


if(D3DERR_DEVICELOST == cgm::d3d_device->Present(NULL, NULL, NULL, NULL))
{
if(!d3d_lost && error_log) log << GetTickCount() << "\tD3d device lost...resetting...\n";
if(D3D_OK == cgm::d3d_device->Reset(&cgm::d3d_parameters))
{
d3d_lost = false;//Used to stop other functions running
if(error_log) log << GetTickCount() << "\tD3d device reset.\n";
}
else d3d_lost = true;
}


Share this post


Link to post
Share on other sites
Firstly, I don't think what you're doing is going to work. You're better off using TestCooperativeLevel() at the start of each frame.

I think you can only Reset if you're getting D3DERR_DEVICENOTRESET from this, and since Present doesn't return that value, I don't think you'll ever know when it's possible to reset.

Here's my code to do it..


// check for lost device
LastError = d3d.lpD3DDevice->TestCooperativeLevel();
if(FAILED(LastError)) {

while(1) {
if(LastError == D3DERR_DEVICENOTRESET) {
// OutputDebugString(" DEVICE NOT RESET ");
if (ReleaseVolatileResources() == TRUE) {
if(FAILED(d3d.lpD3DDevice->Reset(&d3d.d3dpp))) {
// OutputDebugString("\n Couldn't reset device");
}
else {
CreateVolatileResources();
}
}
}
else if (LastError == D3DERR_DEVICELOST ) {
// OutputDebugString(" DEVICE LOST ");
}
Sleep(100);
break;
}
}



The Volatile resources functions just release and re-create my vertex buffer, and set the vertex stream source and shader/fvf. My textures are managed just as yours are. You'd probably also want to reset your render states.

Share this post


Link to post
Share on other sites
Got it to work. The problem was that I wasn't releasing and recreating the vertex buffer (I thought it was only "resources" like textures and surfaces on the video card that needed recreating)

Share this post


Link to post
Share on other sites

This topic is 3587 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this