Restoring a direct3Ddevice (Solved)

Started by
2 comments, last by The C modest god 18 years, 8 months ago
If I recognize that my D3DDevice is lost I run the following code:

pD3DDevice->Restore();

class Direct3DDevice: public BaseClass {
	public:
		void BeginScene();
		void Clear (BYTE Red, BYTE Green, BYTE Blue);
		void EndScene();
		DWORD GetHeight();
		BOOL GetNonPower2();
		DWORD GetWidth();
		void Initialize (Direct3DHardware & D3DHardware, WORD Width, WORD Height, BOOL bForcePower2);
		void Present ();
		void Restore ();

		Direct3DDevice ();
		~Direct3DDevice ();

		void CreateTexture (WORD Width, WORD Height, LPDIRECT3DTEXTURE9 & pTexture);
		void CreateVertexBuffer(WORD VertexAmount, WORD VertexSize, DWORD FlexibleVertexFormat, LPDIRECT3DVERTEXBUFFER9 & VB);
		void DrawPrimitive (D3DPRIMITIVETYPE PrimitiveType, WORD StartVertex, WORD PrimitiveCount);
		void SetStreamSource (LPDIRECT3DVERTEXBUFFER9 pBuffer, WORD BytesSize, DWORD FlexibleVertexFormat);
		void SetTexture (LPDIRECT3DTEXTURE9 pTexture);
	private:
		void LocalFree();

		LPDIRECT3DDEVICE9 pD3DDevice;
		DWORD Width, Height;
		BOOL bNonPower2;

		Direct3DHardware * pD3DHardware;
		BOOL bForcePower2;
};

void
Direct3DDevice::Restore ()
{
	this->LocalFree();
	this->Initialize (*this->pD3DHardware, this->Width, this->Height, this->bForcePower2);
}

void
Direct3DDevice::LocalFree ()
{
	if (this->pD3DDevice!=NULL)
		this->pD3DDevice->Release();
	this->pD3DDevice=NULL;
}

void 
Direct3DDevice::Initialize (Direct3DHardware & D3DHardware, WORD Width, WORD Height, BOOL bForcePower2)
{
	D3DCAPS9 DCaps;

	this->LocalFree();
	D3DHardware.CreateDevice (Width, Height, this->pD3DDevice);
    GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ));
    GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetRenderState( D3DRS_LIGHTING, FALSE ));
    GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetRenderState( D3DRS_ZENABLE, FALSE ));
	GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ));
	GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ));
	GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE ));
	GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetSamplerState (0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR));
	GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetSamplerState (0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR));
	GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetSamplerState (0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP));
	GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetSamplerState (0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP));
	GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetSamplerState (0, D3DSAMP_ADDRESSW, D3DTADDRESS_CLAMP));
    GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE ));
    GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ));
    GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ));
    GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE ));
    GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ));
    GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE ));
    GlobalError::pErrH->HandleDirect3DError(this->pD3DDevice->GetDeviceCaps(&DCaps));
	this->bNonPower2 = (DCaps.TextureCaps&D3DPTEXTURECAPS_NONPOW2CONDITIONAL);
	if (bForcePower2)
		this->bNonPower2 = FALSE;
	this->Width = Width;
	this->Height = Height;
	this->bForcePower2 = bForcePower2;
	this->pD3DHardware = &D3DHardware
}

void
Direct3DHardware::CreateDevice (WORD Width, WORD Height, LPDIRECT3DDEVICE9 & pDevice)
{
		D3DPRESENT_PARAMETERS d3dpp;

		ZeroMemory( &d3dpp, sizeof(d3dpp) );
		d3dpp.BackBufferWidth = Width;
		d3dpp.BackBufferHeight = Height;
		d3dpp.BackBufferCount = 1;
	    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
		d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
		d3dpp.MultiSampleQuality = D3DMULTISAMPLE_NONE;
		d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
		d3dpp.hDeviceWindow = this->hWnd;
		d3dpp.Windowed = FALSE;
		d3dpp.EnableAutoDepthStencil = FALSE;
//		d3dpp.AutoDepthStencilFormat;
		d3dpp.Flags = 0;
		d3dpp.FullScreen_RefreshRateInHz = this->GetHighestRefreshRate (Width, Height);
		d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;

		GlobalError::pErrH->HandleDirect3DError(this->pD3D->CreateDevice( this->AdapterNumber, D3DDEVTYPE_HAL, this->hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &pDevice ));
}

However, I get the following error from the createDevice at the last line of the code: "The device has been lost but cannot be reset at this time. Therefore, rendering is not possible." How should I restore the device? [Edited by - The C modest god on July 28, 2005 10:08:14 AM]
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Advertisement
This is one of those things I never bother to memorize myself, but I know where to go find the answer [smile]

Load up the SDK samples and dig into their framework code - it's included in all but the most trivial tutorials. Granted, it's no simple piece of code - but tracing the callback declarations and looking in the DXUTMainLoop() (I think thats the right name) code will eventually point you to the right place.

The SDK framework is effectively the "text book" perfect approach to handling the lifecycle of a Direct3D application, if you do things the way it does them you should be fine [grin]

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

On second thoughts...

You might want to read the documentation on Lost Devices and, having read that, it seems from the code you posted that you haven't called IDirect3DDevice9::Reset()

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Yes,
I have added the Reset method before the Release and it worked out.
I thought Release would be enough, but the Release failed without calling reset before it.

Thanks for the help.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.

This topic is closed to new replies.

Advertisement