I read the stencil word a lot, and I don't have a clue what is its real purpose in computer graphics.

Seeing the picture in black and white on wikipedia, I'm still having problem with it.

Why do we use a stencil or a stencil buffer, and what is the difference with a Z-buffer ? Why using this word which seems to have some meaning in graphic design ?

  • \$\begingroup\$ Great question, all I know is it's used for rendering shadows... But I'll be interested to hear a full explanation of it! \$\endgroup\$
    – Ricket
    Commented Sep 12, 2010 at 21:41

3 Answers 3


In its original incarnation, a stencil buffer was a one-bit-per-pixel (i.e. black or white, but no grays) framebuffer. You could render to it whatever you wanted like any other framebuffer. Then, later, you could use the contents of that buffer to "stencil" or mask out when drawing to your regular buffer.

An example: Let's say you're making a driving game. You want to have a little rear-view mirror onscreen that shows you what's behind the car. You'll need to render a view pointing behind the car, but you only want to render that within the little rounded rectangle of the rear-view mirror. The typical solution is:

  1. Render the rounded rectangle shape to the stencil buffer.
  2. Enable stencilling.
  3. Render the backwards pointing view onto the regular buffer.

The stencil will then mask it out so that you only draw into the shape of the mirror.

Now that render pipelines are much more flexible and programmable, stencil buffers are used as just a generic 1-bit framebuffer that you can do whatever you want with. Shadows are a common use case.

  • 10
    \$\begingroup\$ In other words, it basically has the same function as a real life stencil, masking out parts when applying ink. \$\endgroup\$
    – Kaj
    Commented Sep 13, 2010 at 5:34
  • 4
    \$\begingroup\$ Almost all devices these days support an 8-bit stencil buffer. \$\endgroup\$
    – Chris Howe
    Commented Sep 13, 2010 at 18:01
  • \$\begingroup\$ I don't really get your example, it's like there is a second frustrum ? does it rasterizes another part of the scene ? Can you give an example with shadows ? \$\endgroup\$
    – jokoon
    Commented Sep 29, 2010 at 21:04
  • \$\begingroup\$ It's just a second frame buffer. You could always change the frustum before you render to it (which is what I think shadow systems do), but that isn't part of the core concept. \$\endgroup\$
    – munificent
    Commented Oct 1, 2010 at 21:50

In addition to the functional usage of stencil buffers that people have already discussed, stencil buffers can also be used for optimization.

Due to the position of the stencil test in the graphics pipeline (which is before pixel shading), stencil testing can be used to kill pixels that do heavy shading work when they're not needed. Often this can be done at a coarser granularity than just a single pixel eg. a tile of pixels, so work can often be culled during the coarse rasterization stages too.


It's just a buffer that controls which pixels can be modified when polygons are rendered. For each pixel, there's a corresponding stencil value, and pixels can be conditionally modified/ignored depending on these values

At it's simplest, it can be used like a physical stencil - a mask that allows some pixels through, and stops other pixels being modified.

It is very similar in behaviour to a z-buffer - like depth testing, stencil testing is a true/false test, and there are similar settings to control the stencil test (pass if greater/less/equal). But instead of writing a depth to it, it will usually have pre-defined values written to it, or be incremented/decremented when pixels are drawn, as used in stencil shadow algorithms.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .