What to do when creation of a resource fails?

Started by
4 comments, last by 21st Century Moose 11 years, 9 months ago
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?
Advertisement
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.
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.
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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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!
<snip>good stuff</snip>


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! :)

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement