Lost devices in Direct3D

This is a discussion on Lost devices in Direct3D within the Game Programming forums, part of the General Programming Boards category; Finally I'm getting around to responding to lost devices in Direct3D in my core engine code. It's actually not as ...

The Reset() is not working completely. When I Alt-Tab out of the game and then back in, it works but only the font is showing. I can clear the back buffer to any color I want, but no primitives or textures are visible.

When you say the reset is not completely working, is it just not behaving right or returning an error or what? How do you allocate primitives and textures? All memory from D3DPOOL_DEFAULT must be released before calling reset, and then I presume reallocated once the device is put back into an operational state (Documentation isn't clear on that, but it's just logical). Anyways, here are my troubles with lost devies, probably won't get you much of anywhere: D3DX - ALT + TAB / Drawing stuff

Bubba, jut one question please... how did you get so good with DirectX?

As Shakti has pointed out...books and lots of em. Tutorials only go so far and most if not all of them have errors or are for older versions of DirectX. If you are going to use tutorials make sure they are recent and make sure they are from a reputable source.

Most of the tutorials I've used are from lighthouse3D which focuses on OpenGL but the concepts remain the same. The best sites I've seen for accurate DirectX/Direct3D tutorials are www.gamasutra.com (although they are a bit 'deep') and http://www.mvps.org/directx/indexes/...d_articles.htm. The last link is a link to the microsoft mvps site which is chocked full of good articles and resources.

The best authority on Microsoft DirectX is, well, Microsoft. They are not stupid and they do understand DirectX down to the smallest detail. Sometimes these details will make or break your code.

Figured out the problem with all of this. There is one sentence in the docs I failed to comprehend. I was under the impression that all managed resources were restored upon resetting the device. This is not the case. The documents state that texture surfaces are lost.

This is a simple fix. If you have a texture resource manager up and running and your objects simply use texture indexes or IDs to access their textures from the manager then you have an easy solution.

Upon a reset you will lose those texture surfaces. So when you get D3DERR_DEVICENOTRESET meaning the device can now be reset, call InitD3D() to re-init the device and don't forget you don't have to re-register the class or re-create the window. After this you need to create a function in your texture manager that will re-create all the textures for the current level/world etc. Since you were wise and stored this pertinent information during setup, you simply re-iterate the list and re-create the textures. And all of you wondered what that delay was when you ALT-TABbed out of your game and back in and now you know.

Now when the device returns D3D_OK or S_OK your objects will access the texture manager for textures....and everything should work. AFAIK you do not have to do this for vertex buffer objects and/or index buffer objects. All device states and textures are lost during a lost device. However almost all Direct3D functions are 100% guaranteed not to fail during a lost device so you can call your rendering loop (which I wouldn't) during a lost device or you can simply opt to call your update() function to continue updating the game......or you can simply pause it and release some cycles back to the CPU for whatever app it is that interrupted your all important game - that is, if you want to be nice.

EDIT:
From other docs and examples on the net it seems you may also have to re-create any custom vertex declarations for use with your shaders.

When you say InitD3D, it sounds like some custom routine to reset the device. True?
When you say the class, are you talking about the window class? Was it unregistered?
When you say the window, are you talking about like, the hwnd window window?

InitD3D() is my function that initializes Direct3D. The point is you must re-create the device after the device is lost and upon receiving D3DERR_DEVICENOTRESET from IDirect3DDevice9::TestCooperativeLevel()

Once the device has been lost it is no longer valid and any textures you have in video memory (D3DPOOL_MANAGED) will be lost as well. Some books do not show this which tends to become confusing. All I know is that once I re-init the textures for my skybox I could ALT TAB and back and it worked just fine. W/o re-init the textures all I got was the blank backbuffer with my text on it.