Jump to content

  • Log In with Google      Sign In   
  • Create Account






handling lost devices

Posted by Norman Barrows, 04 September 2013 · 288 views

DX9 Lost Device Example

I decided to implement Alt-tab in my project, and went looking for info on handling
lost devices. The DirectX docs were their usual disappointment. Eventually a serach
of Gamedev and the Web found all the bits an pieces needed, but not in one place.
So I decided to post what I'd found for use by others.

First, you'll want to make sure you load all non-dynamic resources into managed memory pool,
not default. This essentially keep a backup copy of resources in regular ram that D3D can use
to automtically restore vidram as needed.



Here's the basic algo:

result=Present();
if (result == D3DERR_DEVICELOST) do_lost_device();



do_lost_device:
while ( TestCooperativeLevel() != D3DERR_DEVICENOTRESET )
{
do_win_messages();
Sleep(10);
}
release_stuff();
d3d_device_ptr->Reset(&params);
while ( TestCooperativeLevel() != D3D_OK )
{
do_win_messages();
Sleep(10);
}
reload_stuff();
reset_stuff();


release_stuff:
call OnLostDevice for all your interfaces (fonts, sprite object, etc). You don't
have to release them. Release all default memory pool resources: dynamic textures,
dynamic bitmaps, index and vertex buffers you allocate, etc. Note that
LoadMeshFromX with no flags defaults to D3DPOOL_DEFAULT, and Mesh.GetVertexBuffer
returns a copy of the mesh's vertex buffer object, not its address. Both of these
bit me when trying to find all the places I created something in D3DPOOL_DEFAULT.


reload_stuff:
Call OnResetDevice for your interfaces (fonts, sprite object, etc).
Reload / recreate your D3DPOOL_DEFAULT resources: dynamic textures, dynamic
bitmaps, index and vertex buffers you allocate, etc. This is basically a copy
and paste of the code where you do this the first time.




reset_stuff:
Pretty much all of D3D's internal states get wiped on lost device, so you have to
set everything back to the way you want it again. This will vary by title.
I reset everyting that I set between the time I create the D3D device and the
time I start the game itself:
set default material
set flexible vertex format
set projection matrix
set default texture stage states
set gourard shading
turn on mipmaps
set min mag filetrs to anisotropic
set ambient light level
turn on normalizing normals (I do scaling and lighting)
turn on specular
turn on alpha blending




I saw one posting that tested co-op level before calling present, but that would
add an unnecessary check when things are running correctly, i would think.

The basic idea is you wait for "device not reset". You have to process windows
messages while you do so. It has to do with D3D knowing when you get focus back
apparently. The call to Sleep() releases clock cycles to the OS while you wait.
When you get "Device not reset", you release stuff from vidram. Then you call
Reset().

IF YOU STILL HAVE STUFF IN VIDRAM, Reset() WILL FAIL WITH "Invalid Call"!

If Reset() succeeds, you then have to wait for DirectX to come back alive. So you
test cooperative level until you get "D3D_OK". During this time, apparenly windows
will switch the resolution to match your desired resolution, thus the need to wait
a moment. Again, windows messages must be processed while you wait, and you want
to Sleep() to give clock cycles back to the OS. Once you get "D3D_OK", you own
the video card again, and you're pretty much back where you were when you first
created your D3D device, except your managed stuff is still loaded. Call OnReset()
for your interfaces (font, sprite, etc), reload or re-create your non-managed
resources, reset D3D back to the way you want it, and your done! Posted Image

Ok, so you do all this, and it doesn't work, or it works sometimes, but not at
other times. Then what? Enter the dreaded "Purple Screen"! purplescreen(); is
a simple tesing routine that you can insert into your code to determine where
you are creating things in vidram that you're not releasing. In semi-pseudocode:

void purplescreen()
{
int a;
a=0;
do
{
beginscene
clear // clear screen to purple 100,0,50
endscene
result=present
if result == D3DERR_DEVICELOST do_lost_device()
do_win_messages();
if (keypressed(VK_ESCAPE)) a=1;
Sleep(10);
}
while (a != 1);
}


While the purple screen is displayed, you can alt-tab to and from your game, then
hit Esc to continue. Use it to "bracket" where you're using vidram you need to
relase. If you can alt-tab from the purple screen and back, your ok. If you get
"invalid call" when you Reset(), you've loaded something you haven't released.
So you put the purple screen at program start, right after you load everything.
well of course it works there, you used that code to decide what to release!
So add another purple screen later on, like right after you draw your scene and
present it. OK, doesn't work there. Now you know D3D is allocating vidram
somewhere between those two points. Move around your calls to purplescreen
until you find where. Once you find the resource, add it to release_stuff()
and reload_stuff(). Test it again. Eventually you'll find them all, and all of
a sudden it will work!

The info in this post came from about 4 different sources and took me about two
days to find and put together into working code. It took me another two days
to find, release, and reload/recreate everything in my project. In my case it was
finding index and vertex buffers used to draw procedurally height mapped ground
quads.

Hopefully this post will save someone some time in the future.




PARTNERS