Thread: Lost devices in Direct3D

  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    Lost devices in Direct3D

    Finally I'm getting around to responding to lost devices in Direct3D in my core engine code. It's actually not as tough or complex as I once thought.

    However it is not working correctly yet. Here is what I'm doing:

    • Testing return value of TestCooperativeLevel() at start of render
    • If return is D3D_OK, proceed to update and render
    • If return is D3DERR_DEVICELOST do nothing but return to caller
    • If return is D3DERR_DEVICENOTRESET, invalidate device objects for CD3DFont, Reset() the device, Call InitD3D() to re-initialize device with stored D3DPRESENT_PARAMETERS (class data member), and return.



    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.

    Any ideas as to why this might be?

  2. #2
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Bubba, jut one question please... how did you get so good with DirectX?
    What tutorials did you use, if you used any... Because i'm kind'a stuck on finding some good tutorials...

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    I know for a fact that he reads books, lots of them. And he codes, lots of it. Thats the only way.

    For a beginners book on DirectX have a look at Introduction to 3D Game programming with DirectX 9.0
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  4. #4
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    is this an e-book, or do i have to buy it?

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I've had similar troubles with lost devices.

    http://msdn.microsoft.com/library/de...st_devices.asp

    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: http://cboard.cprogramming.com/showthread.php?t=84238

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.

  7. #7
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    K... Thank you ever so much

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.
    Last edited by VirtualAce; 12-06-2006 at 07:40 AM.

  9. #9
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> 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

    Okay. What is InitD3D, what class, and what window? I try to cope with a lost device something like:

    Code:
        HRESULT coop = mpd3dDevice->TestCooperativeLevel();
    
        switch(coop)
        {
            case D3DERR_DEVICELOST: 
                return;
    
            case D3DERR_DEVICENOTRESET:
    
                // Release resources
    
                if( FAILED( mpd3dDevice->Reset( &md3dpp ) ) )
                {
                    OutputDebugString( TEXT( "Reset failed" ) );
                    return 0;
                }
                
                // Get 'em back
    
                break;
    
            default:;
        }
    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?

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    When you say InitD3D, it sounds like some custom routine to reset the device. True?
    Sounds like his initial program-startup D3D initialization routine.

    Class and window are indeed referring to the Win32 windowing constructs.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why use Direct3D?
    By m3rk in forum Tech Board
    Replies: 42
    Last Post: 05-22-2009, 09:08 AM
  2. Need help with getting DirectX 9.0 initilization
    By DarkMortar in forum Windows Programming
    Replies: 7
    Last Post: 05-09-2006, 08:58 PM
  3. How to Antialias Direct3D?
    By Deo in forum Game Programming
    Replies: 6
    Last Post: 06-02-2005, 12:14 PM
  4. DIRECTDRAW DIRECT3d
    By samsam1 in forum Game Programming
    Replies: 3
    Last Post: 01-14-2003, 07:28 PM
  5. Direct3D vs. OpenGL
    By PorkyChop in forum Game Programming
    Replies: 22
    Last Post: 12-08-2002, 12:41 AM