Sign in to follow this  
Dragon_Strike

getting rid of onLostDevice and onResetDevice

Recommended Posts

is there anyway to get rid of having to call onLostDevice and onResetDevice on most directx objects? would it be possible to create a wrapper for all objects and then store these in a (global?) list and execute them all at once? thus only having to write onLostDevice and onResetDevice only once in the main class/file... [Edited by - Dragon_Strike on January 2, 2008 5:12:26 PM]

Share this post


Link to post
Share on other sites
My guess is you're using the Direct3D X (D3DX) portion of the Direct3D library. To my knowledge there is no way, however you could just have them call a globally defined function which would take (as a parameter) the pointer to your object with which you must restore, and then use that as the handle. In which case you won't have to write multiple functions.

In addition, you should be able to just pass the pointer of a function to handle it all to the structure for onLostDevice or onResetDevice. However, I don't use Direct3D X much (Only for setting up the perspective matrix and applying transormations and the such for matrices).


Cheers,

-naota

Share this post


Link to post
Share on other sites
Well, what I do is I have wrappers for OnLostDevice and OnReleaseDevice for each class in my engine that needs them (texture class, map class, map entity class, gui widget class, game class and engine class).

The engine contains a list of textures and a list of maps, and a list of top-level gui widgets as well as pointer to game. Map contains a list of map entities. So I just call engine's OnLostDevice, which calls game's OnLostDevice, then map's OnLostDevice, then loops through all top-level GUI widgets and calls OnLostDevice for those. Map's OnLostDevice calls OnLostDevice for every map entity. That's it, now every object has got OnLostDevice called, and same with OnResetDevice.

Share this post


Link to post
Share on other sites
i was thinking about having some kind of global list like

using std::vector;
using boost::intrusive_ptr;
using boost::any;

vector<intrusive_ptr<any>> D3DXObjectList;

the problem becomes however how the program can decide if the object has a onLostDevice/onResetDevice function and if it has it should call it...

something like...

BOOST_FOREACH(intrusive_ptr<any> Object, D3DXObjectList)
{
/*....
if (Object has onLostDevice)
Object->OnLostDevice();
....*/

}

are all D3DX object derived from some baseclass i can use here?

[Edited by - Dragon_Strike on January 3, 2008 2:20:29 AM]

Share this post


Link to post
Share on other sites

typedef void (__stdcall *pFuncVV) (void);

class cClass
{
// blah blah blah
pFuncVV onLostDeviceF;
pFuncVV onResetDeviceF;
// constructor/destructors and other functions here
void onLostDevice(void);
void onResetDevice(void);
};

void cClass::onLostDevice(void)
{
if (onLostDeviceF != NULL)
{
onLostDeviceF();
}
}

void cClass::onResetDevice(void)
{
if (onResetDeviceF != NULL)
{
onResetDeviceF();
}
}


Generally you could do something like that, if I'm understanding you correctly.


Cheers,

-naota

Share this post


Link to post
Share on other sites
Quote:
Original post by ValMan
Well, what I do is I have wrappers for OnLostDevice and OnReleaseDevice for each class in my engine that needs them (texture class, map class, map entity class, gui widget class, game class and engine class).

The engine contains a list of textures and a list of maps, and a list of top-level gui widgets as well as pointer to game. Map contains a list of map entities. So I just call engine's OnLostDevice, which calls game's OnLostDevice, then map's OnLostDevice, then loops through all top-level GUI widgets and calls OnLostDevice for those. Map's OnLostDevice calls OnLostDevice for every map entity. That's it, now every object has got OnLostDevice called, and same with OnResetDevice.
I have something similar to this. I have a ResettableObject class, which all objects that need a OnLostDevice() or OnResetDevice() handler derive from.
The ResettableObject class has two pure virtual functions, for OnLostDevice() and OnResetDevice(), and it [un]registers itself with the graphics system in the constructor/destructor. That way there's only one type to iterate through on lost / reset device, and gives the advantage that the graphics system can be in control of resetting the device and notifying the appropriate objects.

Share this post


Link to post
Share on other sites
One solution is to move to Direct3D10. Direct3D10 has no lost devices, since the runtime automatically handles that for you (possible due to virtualisation changes in Windows Vista, from what I hear).

Share this post


Link to post
Share on other sites
device lost, my nightmare. I have struggle with it for half year, and now I don't really solve it. there are so many reasons can make a device lost, not releasing your resource is only one reason. A video driver bug can also make a device lost, and can't be restored. This happens on both Nvidia card and Ati agp card. Now it seems I met a more interesting device lost case, it like this:
1)first, the device lost because a Ctrl+alt+del
2)after the user unlock the pc, I begin to restore the device
3)before I finish restoring the device(Maybe the computer is very busy, so the restoring operation take a long time), the user lock computer again, and this make a second device lost
4)the game crash.
The above situation is just my guess from the crash log file. I never can reproduce this crash on my computer. And even the crash is just like my guess, I don't know how to solve it, if I solve the second device lost, how about the third one? I am gonna be crazy.

Share this post


Link to post
Share on other sites
Quote:
Original post by codefuns
device lost, my nightmare. I have struggle with it for half year, and now I don't really solve it. there are so many reasons can make a device lost, not releasing your resource is only one reason. A video driver bug can also make a device lost, and can't be restored. This happens on both Nvidia card and Ati agp card. Now it seems I met a more interesting device lost case, it like this:
1)first, the device lost because a Ctrl+alt+del
2)after the user unlock the pc, I begin to restore the device
3)before I finish restoring the device(Maybe the computer is very busy, so the restoring operation take a long time), the user lock computer again, and this make a second device lost
4)the game crash.
The above situation is just my guess from the crash log file. I never can reproduce this crash on my computer. And even the crash is just like my guess, I don't know how to solve it, if I solve the second device lost, how about the third one? I am gonna be crazy.
Your code should be something like this:

Assumes there's a member bool m_bDeviceLost
Render()
{
// Check if it's ok to render...
result = TestCooperativeLevel()
if result = D3DERR_DEVICELOST
if m_bDeviceLost = false
m_bDeviceLost = true
Call OnLostDevice() for any objects needed
endif
return;
elseif result = D3DERR_DEVICENOTRESET
Attempt to reset the device then call OnResetDevice() for everything
If device reset fails with D3DERR_DEVICELOST, just silently fail, don't error out

if reset succeeded
m_bDeviceLost = false;
endif
elseif result = D3DERR_DRIVERINTERNALERROR
Exit application, fatal internal error
endif

// Do any rendering here

// Present
result = Present()
if FAILED(result) && result != D3DERR_DEVICELOST
Error in present, either ignore (And catch in TestCooperativeLevel() next tick) or do error handling here
endif
}


That way if Reset() says the device is still lost, you do nothing and wait for the time to reset it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this