<p>The stencil buffer is an unsigned integer buffer, usually 8-bit nowadays, where you can fill per-pixel infomation as you wish based on the use of various operations (<a href="https://www.opengl.org/wiki/Stencil_Test#Stencil_operations">OpenGL Ops here for example</a>) following a stencil test. </p>

<p>The stencil test is simply a per-pixel operation in which the current pixel stencil value is tested against the content of the stencil buffer. You set the condition to which the stencil test is passed ( e.g. ALWAYS, LESS etc. ). </p>

<p>You can decide what happens to the stencil value depending on the outcome of the test (in OpenGL using the operation I linked you above) </p>

<p>The stencil buffer has quite a few uses, the first ones that come to mind being:</p>

<ul>
<li>It can be used as a mask to selectively render certain part of the framebuffer and discard the rest. This, for example, is useful for reflections, but also lots more. When dealing with multiple resolution rendering it can be used to mark up edges that need to be rendered at an higher res. There are plenty of masking usages really. </li>
<li>A very simple overdraw filter for example. You can set the stencil test to always pass and always increment the value. The value you end up with in the stencil buffer represents your overdraw. [*] </li>
</ul>

<p>In the past it has been used also for shadowing techniques such as Shadow volume.</p>

<p>The poor stencil buffer is often quite underestimated, but can be a good weapon in a graphics programmer's armory. </p>

<p>[*] Value to be read back in a post-process using the right SRV! </p>

<p>EDIT: 
Also, worth mentioning that in Shader Model 5.1 (so D3D11.3 and D3D12) you have access to the stencil reference value via <code>SV_StencilRef</code></p>

CC BY-SA 3.0
2015-08-04T19:44:03.353Z
2015-08-04T20:21:59.477Z
2015-08-04T20:21:59.477Z
cifz
<blockquote>
  <p>A stencil buffer contains per-pixel integer data which is used to add
  more control over which pixels are rendered.</p>
  
  <p>A stencil buffer operates similarly to a depth buffer. So similarly,
  that stencil data is stored in a depth buffer. While depth data
  determines which pixel is closest to the camera, stencil data can be
  used as a more general purpose per-pixel mask for saving or discarding
  pixels. To create the mask, use a stencil function to compare a
  reference stencil value -- a global value -- to the value in the
  stencil buffer each time a pixel is rendered.</p>
</blockquote>

<p>Source: <a href="https://msdn.microsoft.com/en-us/library/bb976074.aspx" rel="nofollow noreferrer">What Is a Stencil Buffer? | Microsoft Developer Network</a></p>

<p>The general idea is that you can set a value for each pixel and then set the operation (<code>GREATER</code>, <code>SMALLER</code>, <code>EQUAL</code> etc), when this evaluates to true the pixel is shaded and shown.</p>

<p>One application is realtime reflection, when rendering the reflection texture the stencil buffer is used to create a mask for where the reflection is applied. For example only apply the reflection to the glass material shaded triangles. It can also be used to create arbitrary two dimensional masks for the whole framebuffer.</p>

CC BY-SA 3.0
2015-08-04T19:18:52.277Z
2018-03-04T19:56:47.600Z
2018-03-04T19:56:47.600Z
Maurice
<p>The stencil buffer definition by Wikipedia is indeed not great, it focuses too much on the details of modern implementations (OpenGL). I find <a href="https://en.wikipedia.org/wiki/Stencil_(disambiguation)" rel="noreferrer">the disambiguated version</a> easier to understand:</p>

<blockquote>
  <p>A stencil is a template used to draw or paint identical letters, symbols, shapes, or patterns every time it is used. The design produced by such a template is also called a stencil.</p>
</blockquote>

<p>That's what stencil meant before Computer Graphics. If you type <em>stencil</em> on Google Images, this is one of the first results:</p>

<p><a href="https://i.sstatic.net/bHD7z.jpg" rel="noreferrer"><img src="https://i.sstatic.net/bHD7z.jpg" alt="Stencil mask"></a></p>

<p>As you can see, it is simply a mask or pattern that can be used to "paint" the negative of the pattern onto something.</p>

<p>The <em>stencil buffer</em> works in the exact same way. One can fill the stencil buffer with a selected pattern by doing a stencil render pass, then set the appropriate stencil function which will define how the pattern is to be interpreted on subsequent drawings, then render the final scene. Pixels that fall into the rejected areas of the stencil mask, according to the compare function, are not drawn.</p>

<hr>

<p>When it comes to implementing the stencil buffer, sometimes it is indeed coupled with the depth buffer. Most graphics hardware uses a 1 byte (8 bits) stencil buffer, which is enough for most applications. Depth buffers are usually implemented using 3 bytes (24 bits), which again is normally enough for most kinds of 3D rendering. So it is only logical to pack the 8 bits of the stencil buffer with the other 24 of the depth buffer, making it possible to store each depth + stencil pixel into a 32 bit integer. That's what Wikipedia meant by:</p>

<blockquote>
  <p>The depth buffer and stencil buffer often share the same area in the RAM of the graphics hardware.</p>
</blockquote>

<hr>

<p>One application in which the stencil buffer used to be king was for shadow rendering, in a technique called <a href="https://en.wikipedia.org/wiki/Shadow_volume" rel="noreferrer"><em>shadow volumes</em></a>, or sometimes also appropriately called <em>stencil shadows</em>. This was a very clever use of the buffer, but nowadays most of the rendering field seems to have shifted towards depth-based shadow maps.</p>

CC BY-SA 3.0
2015-08-04T19:54:51.953Z
It's one character, but `cleaver` -> `clever`. I can't edit it D: Great answer by the way, this makes the most sense so far.
Qix - MONICA WAS MISTREATED
CC BY-SA 3.0
2015-08-10T05:45:33.600Z
If you have an expensive per-pixel shader, the stencil buffer can also be used to batch similar threads, reducing divergence and increasing occupancy as in [this OIT paper, sec 2.6](http://goanna.cs.rmit.edu.au/~pknowles/rbs-preprint.pdf) (disclaimer: I'm an author).
jozxyqk
CC BY-SA 3.0
2015-08-10T19:05:39.823Z
@jozxyqk, that's some outside the box thinking right there! Very nice.
glampert
CC BY-SA 4.0
2020-11-07T14:21:25.063Z
May I ask, is there a single Stencil & Depth Buffer for the entire GPU? Or a different one is assigned per object we `Draw`?
KeyC0de
CC BY-SA 4.0
2024-04-13T01:56:36.233Z
I don't know what state of the art is but, the example above shows an image of helmet from Star Wars. That suggests using the stencil buffer for image masking but AFAICT, image masking is usually alpha based so that instead of masked/not masked you choose a range between 0% and 100% and can anti-alias along the edge of the mask. So it would be good to know when it's appropriate to use a stencil buffer and when it's not.
gman
CC BY-SA 3.0
2015-08-04T19:45:33.780Z
2015-08-09T17:31:32.827Z
2015-08-09T17:31:32.827Z
glampert
<p>Wikipedia <a href="https://en.wikipedia.org/wiki/Stencil_buffer">states</a> that a stencil buffer is some arbitrary buffer a shader can use.</p>

<p>However, it hints that it's used for clipping, or otherwise "tightly binding" the depth and pixel buffers, slightly contradicting itself.</p>

<p>What does the stencil buffer <em>really</em> do, and how is it practically used in modern applications?</p>

<stencil-buffer>
What is a stencil buffer?
Qix - MONICA WAS MISTREATED
# What is a stencil buffer?

(Asked by: username_0 on 2015-08-04)

Wikipedia [states](https://en.wikipedia.org/wiki/Stencil_buffer) that a stencil buffer is some arbitrary buffer a shader can use.

However, it hints that it's used for clipping, or otherwise "tightly binding" the depth and pixel buffers, slightly contradicting itself.

What does the stencil buffer *really* do, and how is it practically used in modern applications?

**Tags:** stencil-buffer


---

## 3 answers

### Answer by username_3 on 2015-08-04. Score: 44 | Accepted answer

The stencil buffer definition by Wikipedia is indeed not great, it focuses too much on the details of modern implementations (OpenGL). I find [the disambiguated version](https://en.wikipedia.org/wiki/Stencil_(disambiguation)) easier to understand:

> A stencil is a template used to draw or paint identical letters, symbols, shapes, or patterns every time it is used. The design produced by such a template is also called a stencil.

That's what stencil meant before Computer Graphics. If you type *stencil* on Google Images, this is one of the first results:

[![Stencil mask](https://i.sstatic.net/bHD7z.jpg)](https://i.sstatic.net/bHD7z.jpg)

As you can see, it is simply a mask or pattern that can be used to "paint" the negative of the pattern onto something.

The *stencil buffer* works in the exact same way. One can fill the stencil buffer with a selected pattern by doing a stencil render pass, then set the appropriate stencil function which will define how the pattern is to be interpreted on subsequent drawings, then render the final scene. Pixels that fall into the rejected areas of the stencil mask, according to the compare function, are not drawn.

---

When it comes to implementing the stencil buffer, sometimes it is indeed coupled with the depth buffer. Most graphics hardware uses a 1 byte (8 bits) stencil buffer, which is enough for most applications. Depth buffers are usually implemented using 3 bytes (24 bits), which again is normally enough for most kinds of 3D rendering. So it is only logical to pack the 8 bits of the stencil buffer with the other 24 of the depth buffer, making it possible to store each depth + stencil pixel into a 32 bit integer. That's what Wikipedia meant by:

> The depth buffer and stencil buffer often share the same area in the RAM of the graphics hardware.

---

One application in which the stencil buffer used to be king was for shadow rendering, in a technique called [*shadow volumes*](https://en.wikipedia.org/wiki/Shadow_volume), or sometimes also appropriately called *stencil shadows*. This was a very clever use of the buffer, but nowadays most of the rendering field seems to have shifted towards depth-based shadow maps.

> **Comments:**
> **username_0**: It's one character, but `cleaver` -> `clever`. I can't edit it D: Great answer by the way, this makes the most sense so far.  
> **username_4**: If you have an expensive per-pixel shader, the stencil buffer can also be used to batch similar threads, reducing divergence and increasing occupancy as in [this OIT paper, sec 2.6](http://goanna.cs.rmit.edu.au/~pknowles/rbs-preprint.pdf) (disclaimer: I'm an author).  
> **username_3**: @username_4, that's some outside the box thinking right there! Very nice.  
> **username_5**: May I ask, is there a single Stencil & Depth Buffer for the entire GPU? Or a different one is assigned per object we `Draw`?  
> **username_6**: I don't know what state of the art is but, the example above shows an image of helmet from Star Wars. That suggests using the stencil buffer for image masking but AFAICT, image masking is usually alpha based so that instead of masked/not masked you choose a range between 0% and 100% and can anti-alias along the edge of the mask. So it would be good to know when it's appropriate to use a stencil buffer and when it's not.  

---

### Answer by username_1 on 2015-08-04. Score: 16

The stencil buffer is an unsigned integer buffer, usually 8-bit nowadays, where you can fill per-pixel infomation as you wish based on the use of various operations ([OpenGL Ops here for example](https://www.opengl.org/wiki/Stencil_Test#Stencil_operations)) following a stencil test.

The stencil test is simply a per-pixel operation in which the current pixel stencil value is tested against the content of the stencil buffer. You set the condition to which the stencil test is passed ( e.g. ALWAYS, LESS etc. ).

You can decide what happens to the stencil value depending on the outcome of the test (in OpenGL using the operation I linked you above)

The stencil buffer has quite a few uses, the first ones that come to mind being:

* It can be used as a mask to selectively render certain part of the framebuffer and discard the rest. This, for example, is useful for reflections, but also lots more. When dealing with multiple resolution rendering it can be used to mark up edges that need to be rendered at an higher res. There are plenty of masking usages really.
* A very simple overdraw filter for example. You can set the stencil test to always pass and always increment the value. The value you end up with in the stencil buffer represents your overdraw. [\*]

In the past it has been used also for shadowing techniques such as Shadow volume.

The poor stencil buffer is often quite underestimated, but can be a good weapon in a graphics programmer's armory.

[\*] Value to be read back in a post-process using the right SRV!

EDIT:
Also, worth mentioning that in Shader Model 5.1 (so D3D11.3 and D3D12) you have access to the stencil reference value via `SV_StencilRef`


---

### Answer by username_2 on 2015-08-04. Score: 8

> A stencil buffer contains per-pixel integer data which is used to add
> more control over which pixels are rendered.
>
> A stencil buffer operates similarly to a depth buffer. So similarly,
> that stencil data is stored in a depth buffer. While depth data
> determines which pixel is closest to the camera, stencil data can be
> used as a more general purpose per-pixel mask for saving or discarding
> pixels. To create the mask, use a stencil function to compare a
> reference stencil value -- a global value -- to the value in the
> stencil buffer each time a pixel is rendered.

Source: [What Is a Stencil Buffer? | Microsoft Developer Network](https://msdn.microsoft.com/en-us/library/bb976074.aspx)

The general idea is that you can set a value for each pixel and then set the operation (`GREATER`, `SMALLER`, `EQUAL` etc), when this evaluates to true the pixel is shaded and shown.

One application is realtime reflection, when rendering the reflection texture the stencil buffer is used to create a mask for where the reflection is applied. For example only apply the reflection to the glass material shaded triangles. It can also be used to create arbitrary two dimensional masks for the whole framebuffer.


---

