Jump to content

  • Log In with Google      Sign In   
  • Create Account

What to do when creation of a resource fails?


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
5 replies to this topic

#1 TiagoCosta   Crossbones+   -  Reputation: 2199

Like
1Likes
Like

Posted 24 July 2012 - 03:06 PM

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?

Sponsor:

#2 MrDaaark   Members   -  Reputation: 3555

Like
1Likes
Like

Posted 24 July 2012 - 08:54 PM

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.

#3 Neometron   Members   -  Reputation: 372

Like
0Likes
Like

Posted 24 July 2012 - 10:47 PM

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.

#4 mhagain   Crossbones+   -  Reputation: 7978

Like
2Likes
Like

Posted 25 July 2012 - 05:03 AM

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.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#5 MrDaaark   Members   -  Reputation: 3555

Like
1Likes
Like

Posted 25 July 2012 - 10:56 AM

I'm not too sure that I agree with the idea of failing gracefully here.

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!

#6 mhagain   Crossbones+   -  Reputation: 7978

Like
1Likes
Like

Posted 25 July 2012 - 01:10 PM

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

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.





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