# what is a stencil buffer

This topic is 4884 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

im confused of what stencil buffer is and whats its used? i planned to study and learned how to use shadow mapping/volume and the article uses the stencil buffer and i dont knw what it is

##### Share on other sites
The stencil buffer is an extra buffer that holds a value for each pixel. This is commonly either 8bit (0..255), 4bit (0..15) or 1bit (0/1). When polygons are drawn, you can update the stencil buffer with operations such as simply writing a value to it, incrementing the value or decrementing the value.
You can also test against the stencil buffer, to see whether the value for a pixel is equal to another value, above or below it.

A good example of why this is useful is using a non-rectangular clipping area. Suppose you want to draw a map display or camera view in the HUD of your game, but this should be displayed in a circular area. You could simply draw a circle in the stencil buffer, writing value 1 to each pixel. Then, you can draw your geometry, setting the stencil buffer condition to ==1. This ensures that everything drawn shows up only inside that circle. Voila, perfect clipping.

With shadow volumes, you can set the stencil operation to increase the stencil value by 1 for the volume's front-facing polygons and decrease it by 1 for back-facing polygons. This results in the stencil buffer containing 0 for all pixels that are not in the shadow, 1 (or higher) for pixels that have a shadow cast on them.

##### Share on other sites
Also, the concept of a "Depth-Stencil Buffer" can be confusing. Basically, it is just a depth buffer (which stores information about the depth of a pixel) and a stencil buffer combined, to increase efficiency.

If you look at the DepthStencil buffer formats, you will see a notation like D24S8. This means 24 bits are reserved for the depth buffer, while 8 bits are reserved for the stencil. There are pure depth buffers though, like D32.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634090
• Total Posts
3015432
×