Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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 ?

share|improve this question
    
Great question, all I know is it's used for rendering shadows... But I'll be interested to hear a full explanation of it! –  Ricket Sep 12 '10 at 21:41

3 Answers 3

up vote 29 down vote accepted

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.

share|improve this answer
5  
In other words, it basically has the same function as a real life stencil, masking out parts when applying ink. –  Kaj Sep 13 '10 at 5:34
2  
Almost all devices these days support an 8-bit stencil buffer. –  Chris Howe Sep 13 '10 at 18:01
    
Thanks, Chris! I learned something new today! –  munificent Sep 13 '10 at 22:05
    
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 ? –  jokoon Sep 29 '10 at 21:04
    
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. –  munificent Oct 1 '10 at 21:50

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.