Sunday, December 14, 2008

Depth/Stencil Performance Issues

I've traced the source of my recent massive FPS drop. I was blaming something from Windows Update unfairly, it turns out. This is actually a massive heads-up for anyone who ever wants to create a stencil buffer, so here goes:

It turns out that most cards will only support two formats of depth buffer: a 16-bit depth buffer or a 24-bit depth buffer. There is no point whatsoever in trying to create a 32-bit depth buffer, as you will only ever get 24 bits at best. This is as true for OpenGL as it is for Direct3D; check the results of your PixelFormat if you don't believe me.

The other 8 bits are available for use as a stencil buffer. You can of course choose not to use them for one if you wish. This gives a total of 32 bits, which is why clearing the stencil buffer is a free operation if you also have a depth buffer: either way it's a single 32-bit overwrite of everything in the combined buffer.

Where trouble starts is if you create a stencil buffer but don't clear it every frame. Now you're in a situation where you have a 32 bit buffer but you're only clearing 24 of the bits. This can be a much slower operation; I lost about 25% of my framerate on account of it. You will lose performance if you do this, even if you don't actually use the stencil buffer (say you just created it for later use).

So you have two options:

  • Don't create a stencil buffer; you have a single 32 bit operation to clear.
  • Do create a stencil buffer, but make certain that you clear it every frame, irrespective of whether you're using it; likewise a single 32 bit operation.
In case you're wondering, the 16 bit depth buffer with no stencil is the slowest of them all, so there's no way you want to create one of those unless it's all that your hardware supports.

All that aside, it's getting very near to "stick a fork in it" time... :D

0 comments: