Solved

How to recover from device lost?

Posted on 2008-10-07
12
2,437 Views
Last Modified: 2013-12-21
Hi,

below is a code snippet of a sample I found on how to recover from device lost. For some reasons
hr = m_device->Reset( &m_pp );
returns an error.
The error is not one of this methods errors (0x8876086c) so I have no clue what is wrong


if( m_bDeviceLost == true )
{
  // Yield some CPU time to other processes
  Sleep( 100 ); // 100 milliseconds
 
 
  // Test the cooperative level to see if it's okay to render.
  // The application can determine what to do on encountering a lost 
  // device by querying the return value of the TestCooperativeLevel 
  // method.
  //
 
  if( FAILED( hr = m_device->TestCooperativeLevel() ) )
  {
	// The device has been lost but cannot be reset at this time. 
	// Therefore, rendering is not possible and we'll have to return 
	// and try again at a later time.
	if( hr == D3DERR_DEVICELOST )
		return S_OK;
 
	// The device has been lost but it can be reset at this time. 
	if( hr == D3DERR_DEVICENOTRESET )
	{
		//
		// If the device can be restored, the application prepares the 
		// device by destroying all video-memory resources and any 
		// swap chains. 
		//
                  
                  // delete all Direct3D objects except IDirect3D9 and IDirect3DDevice9
		DeleteDirect3D( true );
 
		//
		// Then, the application calls the Reset method.
		//
		// Reset is the only method that has an effect when a device 
		// is lost, and is the only method by which an application can 
		// change the device from a lost to an operational state. 
		// Reset will fail unless the application releases all 
		// resources that are allocated in D3DPOOL_MANAGED, including 
		// those created by the IDirect3DDevice9::CreateRenderTarget 
		// and IDirect3DDevice9::CreateDepthStencilSurface methods.
		//
 
		hr = m_device->Reset( &m_pp );
 
		if( FAILED(hr ) )
			return S_OK;
 
		//
		// Finally, a lost device must re-create resources (including  
		// video memory resources) after it has been reset.
		//
 
		InitResources();
		//			Init(m_window, m_width, m_height, m_fontName, m_fontSizee);
		m_init = true;
		StringMap tmp = _T("");
		InitScene( true, true, tmp);
		m_init = false;
 
	}
 
	return S_OK;
  }	
 
  m_bDeviceLost = false;
}

Open in new window

0
Comment
Question by:UdiRaz
  • 6
  • 5
12 Comments
 
LVL 11

Expert Comment

by:jgordos
ID: 22673847
In this instance, you don't show where you create/initialize the variable m_pp


Is it null?

Did you give it enough stack space?

-john
0
 

Author Comment

by:UdiRaz
ID: 22683407
what do you mean by stack space?
0
 
LVL 11

Expert Comment

by:jgordos
ID: 22683523
Hmm.  

Well, when you declare a variable, it uses memory.

This memory is allocated from one of two places, depending on how you declare them: either the run-time stack, or the run-time heap.

You're passing the variable by reference, but you don't show where you allocate or declare this variable... so, it's very possible that you haven't given it space, or initialized it, or whatever, and that could be causing your problem.

-john
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

Author Comment

by:UdiRaz
ID: 22688901
I know what stuck is and what stuck space means, but not in Direct3D context. When I declare variables on Direct3D, I don't specify a stuck space.

Do you mean some general windows or direct3d settings where I can set a larger stack space?
or Do you mean that there is a Direct3D method that defines a stack space?

Thanks again.
Udi
0
 
LVL 11

Expert Comment

by:jgordos
ID: 22690760
no, the stack for the executable... the C++ program.

In other words, how did you declare  m_pp?
0
 

Author Comment

by:UdiRaz
ID: 22696578
m_pp is created once. I don't create it again after the device is lost. Am I supposed to?
/******************************************************************************************
// InitPresentParameters() : Initializes PresentParameters so they can be passed to CreateDevice
//******************************************************************************************
void CDisplay::InitPresentParameters()
{
 
	//Clear out our D3DPRESENT_PARAMETERS structure.  Even though we're going
	//to set virtually all of its members, it's good practice to zero it out first.
	ZeroMemory(&m_pp,sizeof(D3DPRESENT_PARAMETERS));
 
	//Whether we're full-screen or windowed these are the same.
	m_pp.BackBufferCount= 1;  //We only need a single back buffer
	m_pp.MultiSampleType=D3DMULTISAMPLE_NONE; //No multi-sampling
	m_pp.MultiSampleQuality=0; //No multi-sampling
	m_pp.SwapEffect     = D3DSWAPEFFECT_DISCARD; // Throw away previous frames, we don't need them
	m_pp.hDeviceWindow  = m_window;  //This is our main (and only) window
	m_pp.Flags=0;  //No flags to set
	m_pp.FullScreen_RefreshRateInHz=D3DPRESENT_RATE_DEFAULT; //Default Refresh Rate
	m_pp.PresentationInterval=D3DPRESENT_INTERVAL_IMMEDIATE; //Default Presentation rate
	m_pp.BackBufferFormat  = D3DFMT_UNKNOWN; //Display format
 
 
	m_pp.EnableAutoDepthStencil=FALSE;
 
	m_pp.AutoDepthStencilFormat=D3DFMT_UNKNOWN; //This is ignored if EnableAutoDepthStencil is FALSE
 
	//BackBufferWidth/Height have to be set for full-screen applications, these values are
	//used (along with BackBufferFormat) to determine the display mode.
	//They aren't needed in windowed mode since the size of the window will be used.
 
	m_pp.Windowed          = TRUE;
 
}

Open in new window

0
 
LVL 11

Expert Comment

by:jgordos
ID: 22713720
Sorry, got busy...

the error you're getting is

   D3DERR_INVALIDCALL

Essentially, there's something that's preventing the reset of your device.

I can't tell from the code, but if you've created any surfaces and triangles, fonts, etc that use the device, you'll need to release all of those.

Do you get any messages in the DirectX logs?  Turn debugging on and retry... you'll probably be given a clue about what's making that an INVALID call.

-john
0
 

Author Comment

by:UdiRaz
ID: 22729251
Thanks,

How to turn debugging on and how to view the directx log?

Udi
0
 
LVL 11

Expert Comment

by:jgordos
ID: 22735828
0
 

Author Comment

by:UdiRaz
ID: 22752339
Here is the line that makes reset fail :

I am calling this method at the beggining of my render method.

Any idea what is the problem?


	IDirect3DSurface9* backBuffer=0;	
	m_device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer); // get back buffer surface
	ASSERT(backBuffer);

Open in new window

0
 
LVL 11

Accepted Solution

by:
jgordos earned 500 total points
ID: 22767247
Did you release the backbuffer before calling reset?

IE, did you call
        m_device->Release();

The Release call will also return a count of the current number of "locks" on the device...  Make sure it goes to zero before you call Reset().

-john
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question