Search code examples
openglgraphicsstencil-buffer

Why sometimes we use specific bits to do stencil test?


I am not clear why sometimes choose some specific bits of the stencil buffer to do stencil test. I cannot find examples like only test 1,3,5 bits of one stencil buffer.


Solution

  • The reason is not especially interesting. The stencil buffer usually contains 8 bits per sample, and you're free to use those 8 bits however you like in your application. So the meaning of those bits is up to you.

    Often they're used for doing volume intersection tests, such as shadow volumes for stencil shadows (a technique popular circa 2005), where you might use the stencil buffer as a counter. Another example is deferred lighting, where you use a single bit in the stencil buffer to track which pixels are affected by a particular light.

    So if you store "this pixel is affected by light #3" in bit 1, then you test bit 1 when you're rendering light #3. It's all up to the application developer.