Jump to content

  • Log In with Google      Sign In   
  • Create Account


[SlimDX] Handling Lost Devices [Solved]


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 adt7   Members   -  Reputation: 425

Like
0Likes
Like

Posted 09 August 2009 - 06:26 AM

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]

Sponsor:

#2 Mike.Popoloski   Crossbones+   -  Reputation: 2852

Like
0Likes
Like

Posted 09 August 2009 - 06:31 AM

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

#3 adt7   Members   -  Reputation: 425

Like
0Likes
Like

Posted 09 August 2009 - 06:34 AM

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.

#4 Buckeye   Crossbones+   -  Reputation: 3791

Like
0Likes
Like

Posted 09 August 2009 - 07:00 AM

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.


#5 adt7   Members   -  Reputation: 425

Like
0Likes
Like

Posted 09 August 2009 - 07:46 AM

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.

#6 Promit   Moderators   -  Reputation: 6096

Like
0Likes
Like

Posted 09 August 2009 - 08:45 AM

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.

#7 adt7   Members   -  Reputation: 425

Like
0Likes
Like

Posted 09 August 2009 - 10:01 AM

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);



#8 Promit   Moderators   -  Reputation: 6096

Like
0Likes
Like

Posted 09 August 2009 - 10:54 AM

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.

#9 Mesut   Members   -  Reputation: 148

Like
0Likes
Like

Posted 09 August 2009 - 11:00 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS