recovering from lost d3d device

Started by
5 comments, last by superpig 16 years, 2 months ago
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);
	}

Advertisement
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.
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.
NextWar: The Quest for Earth available now for Windows Phone 7.
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;	}
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.
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)
Vertex and index buffers are resources too.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

This topic is closed to new replies.

Advertisement