Sign in to follow this  
Aqua Costa

What to do when creation of a resource fails?

Recommended Posts

I never had problems creating textures, buffers, render targets, etc, but I only have a simple if(FAILED(...)) to check if it was successful, if not, the game shuts down with an error message.

The thing is I'm not even sure why it would fail (apart from trying to create a resource using invalid flags in the DESC) I'm guessing it might fail if there isn't enough memory available...

How should I handle fails like this?

Share this post


Link to post
Share on other sites
For a texture, just return a pointer to a default texture instead.
For a render surface, keep trying in half resolution increments.
Only quit as a last resort.

Log the errors. Make sure to log if it was a file not found, invalid flag, or lack of memory.

Share this post


Link to post
Share on other sites
Possibly call a function that has something like this to pre-format the text for you log. Also get the error codes off of DirectX on MSDN

switch(hr)
{
...
case E_INVALIDARG:
...
case E_OUTOFMEMORY:
...
case S_OK:
...
}

Maybe use a big red X texture as default for debugging to visually see what texture is not loading.

Share this post


Link to post
Share on other sites
I'm not too sure that I agree with the idea of failing gracefully here. You should be budgeting your resource requirements and have a reasonable idea of what you'll need for minimal/typical/maximum content loads, so a failed-to-create scenario is most likely an indicator that something else has gone wrong. Typically you want to be aware of this as quickly as possible (can't beat a nice big dirty error message for doing that) so you can take appropriate remedial steps.

Halving the size of render targets until you get a success sounds like a good approach, but what if you need to create a vertex buffer sometime after? That's very likely going to fail as well and how do you gracefully recover from that? Worse yet, the failure diagnosis will be pointing to a problem with the vertex buffer creation, when it was really happening at the render target, so you're already at a disadvantage when it comes to determining the cause.

On the other hand there are cases that you can more gracefully recover from, e.g. by selecting a lower-precision format if the user's hardware doesn't support the high-precision one you tried with first (in the real world that would be something you'd check for before even trying to create; this is just an example for the purposes of discussion), so it's safe to say that there is no one-size-fits-all approach. You need to know the appropriate recovery procedure for each type of resource you create, and sometimes a big dirty error is the best thing to do.

Share this post


Link to post
Share on other sites
[quote]I'm not too sure that I agree with the idea of failing gracefully here.[/quote] 15 years ago when I was modding levels into other people's engines, I was glad that the game's texture manager would return the default white texture with a bug ugly orange D on it, instead of crashing to the desktop.

I could say to myself "oops, I forgot to pack my new curb texture", and move on with the 150 other things I needed to test out during that execution run, like triggers, item placement, etc... The alternative would be to get nothing done, and spend the afternoon digging through everything, and recompiling the level (took hours sometimes), over and over again, until I found the issue. Even if there was a log pointing it out, I'd rather not have to recompile the whole level just to fix a trivial issue. A misplaced texture was the least important thing.

If the large render target creation fails, and you create a half resolution one, you still log all the failures. A log should tell the entire story of execution from init to shutdown. When something goes wrong, it's nice to have as much info as possible!

Share this post


Link to post
Share on other sites
[quote name='Daaark' timestamp='1343235384' post='4962985']<snip>good stuff</snip>[/quote]

I deliberately didn't -1 you for your original because I knew you wouldn't say that without good reason. I guess we're talking about two different usage scenarios here, with different failure cases being more appropriate for each. More for the communal wisdom pot! :)

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