[SlimDX] Handling Lost Devices [Solved]

Started by
7 comments, last by Mesut 14 years, 8 months ago
I'm having various problems handling lost devices in SlimDX (I only started with it yesterday, moving from XNA). My problems occur when I hit Ctrl-Alt-Delete in Vista, which causes the debug runtimes to spit out the following:
Direct3D9: (ERROR) :Lost due to display uniqueness change
D3D9 Helper: IDirect3DDevice9::Present failed: D3DERR_DEVICELOST
A first chance exception of type 'SlimDX.Direct3D9.Direct3D9Exception' occurred in SlimDX.dll
An unhandled exception of type 'SlimDX.Direct3D9.Direct3D9Exception' occurred in SlimDX.dll

Additional information: D3DERR_DEVICELOST: Device lost (-2005530520)
Which breaks on Present() in my drawScene() call. Relevant code snippets below, the observant among you may notice it's DirectX 9.0c: A Shader Apporach, translated into C#.
public virtual void run() 
        {
            long prevTimeStamp = timer.ElapsedMilliseconds;

            while (window.Created)
            {
                if (!isDeviceLost())
                {
                    long currentTimeStamp = 0;
                    currentTimeStamp = timer.ElapsedMilliseconds;
                    float dt = (currentTimeStamp - prevTimeStamp) / 1000.0f;

                    updateScene(dt);
                    drawScene();

                    prevTimeStamp = currentTimeStamp;                    
                }
                Application.DoEvents();
            }
        }






public bool isDeviceLost()
        {
            Result result = d3dDevice.TestCooperativeLevel();

            if (result == ResultCode.DeviceLost)
            {
                onLostDevice();
                return true;
            }
            else if (result == ResultCode.DriverInternalError)
            {
                MessageBox.Show("Internal Driver Error... Exiting");
                Application.Exit();
                return true;
            }
            else if (result == ResultCode.DeviceNotReset)
            {
                d3dDevice.Reset(presentParameters);
                onResetDevice();
                return false;
            }
            else
            {
                return false;
            }
        }






[Edited by - adt7 on August 9, 2009 5:12:15 PM]
Advertisement
It seems like you've posted the answer to your own problem without realizing it:
Quote:Direct3D9: (ERROR) :All user created stateblocks must be freed before ResetEx can succeed. ResetEx Fails.

If you have created any stateblocks and have not freed them before calling Reset, the call will fail.
Mike Popoloski | Journal | SlimDX
Yeah, I just noticed that, and fixed it.

However, I'm still having issues when Ctrl-Alt-Del'ing, and then the code breaking on Present() due to a lost device.

I've edited the above to reflect the problems I'm having now.
Are you getting the same error? (Window doesn't have focus)

First, I'm not familiar with SlimDX in particular.

But:

How do you process WM_KILLFOCUS and WM_SETFOCUS?

Within your application itself, can you successfully switch between windowed and non-windowed mode?

When your app loses focus, you may have to switch to windowed mode and pause the process. When the app regains focus, switch back to full-screen and unpause.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Quote:Original post by Buckeye
Are you getting the same error? (Window doesn't have focus)

First, I'm not familiar with SlimDX in particular.

But:

How do you process WM_KILLFOCUS and WM_SETFOCUS?

Within your application itself, can you successfully switch between windowed and non-windowed mode?

When your app loses focus, you may have to switch to windowed mode and pause the process. When the app regains focus, switch back to full-screen and unpause.


I've fixed that one since my last post.

The problem was that Application.DoEvents(); wasn't being called when the device is lost due to it being within my if-block.

Still having problems with losing the device between checking if it's lost and present is called though.
Generally speaking, the most effective approach is to use the Configuration class to disable exceptions from device lost and not-reset entirely, and check the return value of TestCooperativeLevel (see Direct3D9.ResultCode) instead of waiting for an exception.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Quote:Original post by Promit
Generally speaking, the most effective approach is to use the Configuration class to disable exceptions from device lost and not-reset entirely, and check the return value of TestCooperativeLevel (see Direct3D9.ResultCode) instead of waiting for an exception.


... well, I wasn't waiting for the exception, I was checking the result and then the exception was being throw afterwards.

Adding the following seems to have fixed it (as per your advice), although it seems... hackish.
Configuration.AddResultWatch(ResultCode.DeviceLost, ResultWatchFlags.AlwaysIgnore);

Well, it'll cause the rest of your frame to silently fail, until you notice at the next TestCooperativeLevel call. This is generally not a problem and it's the approach most people take in C++, I think.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Additional to these you must check does device.Present() throw any error.

...device.EndScene();try{     device.Present();}catch (SlimDXException err){     devicelost = true;}...


At First in your isDeviceLost function check if devicelost is true. If it is then you must call Thread.Sleep for some time.

Hope this will help.
------------------------------Mesut

This topic is closed to new replies.

Advertisement