>Cryptic Error Messages on Stencil Buffer Clear

>

It has been a looong time since I lasted posted… Working on my game, school, and a newly acquired job have been eating away at my time. Anyway, I thought I would post something short and simple, but hopefully helpful.

The C# compiler is usually good about giving you detailed information on compiler/runtime errors, but there are those occasional times in which it gives you some really cryptic response that doesn’t help you in the least. I ran into one of those today when attempting to clear my stencil buffer. It crashed on this line:

Game.GraphicsDevice.Clear(ClearOptions.Stencil, Color.Black, 0, 0);

With the ever-so helpful error message: “An unexpected error has occurred.”

Well great, that tells me everything I need to know to fix the problem! (Extreme sarcasm intended)

After messing around for a bit I found that my problem lay in the fact that I hadn’t created my stencil buffer at startup! So if you are running into this problem, add this code to your Game1 constructor:

graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8Single;

That will make sure you have a stencil buffer ready to use, and thus the cryptic error message should go away.