[.net] Efficient DirectX Lost Device Design ?

Started by
2 comments, last by The Frugal Gourmet 19 years, 8 months ago
Beware - A beginners quistion. I've been trying for some time now to design a elegant way of handling Alt-Tab'ing and Ctrl-Alt-Delete's in both windowed and fullscreen Direct3D apps. The problem for me is to know what to do in the correct scenario for example: if a user Ctrl-Alt-Deletes in a windowed DirectX application I recive a lot of DeviceLostExceptions before a deviceNotresetException which then indicates I can Reset the device and continue. but if I'm in fullscreen and Alt-Tabs twice (out and in) I never get any DeviceNotResetException but a lot of DeviceLostExceptions but how do I know the difference And How do I know that I should respond to a DeviceLostException instead of waiting for a DeviceNotResetException. What I want is something like this -Device.Present failed (aka lost device) -Loop until either in windowed app I'm back into desktop after ctrlAltDelete in fullscreen app I've alt-tab'ed back into game. -end loop then I want to handle DirectXNoResetException DirectXLostDeviceException Is it something that I've not seen here, or some docs I've not properly studied? I've searched GameDev forums & Articles and googled for info too. but haven't been able to find a unifyed design for this problem. (btw Sorry for the bad english.)
Advertisement
When a device becomes lost, you respond by pausing your game. You then wait until it enters the "resettable" status, aka "Device not-reset" (i.e. it can be reset). Then you release all resources from the default pool, reset, and recreate resources.

How do I know its in "resettable" status? How do I know thats it gonna get in a resettable status (it don't get to resettable status if you Alt-tab out of fullscreen and in again).

should I try to recreate the device every once and awhile and then just reset it instead in case of notresetexception?
What I've typically seen done is a try {} catch {} used in the game's main drawing loop. If the app fails when presenting or ending the scene because of "DeviceLostException", it goes into inactive mode. Every time your application goes to begin drawing it checks for this inactive state. It then calls Device.TestCooperativeLevel() to see if it can begin drawing again. If it receives a "DeviceLostException" it skips the drawing phase (the device is still lost -- out of focus). If the test receives a "DeviceNotReset" exception (meaning the device is back, but not reset), it calls device.Reset() and turns drawing back on again.

Dealing with invalidating and restoring objects is wonderful in .NET and very easy. Every time you created an object that needs to be invalidated and then restored you simply add an event to Device.DeviceLost and Device.DeviceReset. It will automatically calls all your invalidate and restore events at the appropriate times. I like to map Dispose() events too, for simplicity.
Co-creator of Star Bandits -- a graphical Science Fiction multiplayer online game, in the style of "Trade Wars'.

This topic is closed to new replies.

Advertisement