Game dev
Saved & recent

Stencil buffer

Related terms: stencil test, mask

A per-pixel number stored next to the depth buffer. One draw writes a value into it, marking an area. Later draws are told to pass only where the stencil matches.

It works like masking tape. Portals, mirrors, outlines around selected objects and cut-out UI shapes are built with it.

Further reading
LearnOpenGL: stencil testing
0 1 stencil values stencil test portal clipped scene
Clear to 0 and write 1 over the portal opening; the scene pass accepts only samples whose stencil equals 1. Both views use the same coarse mask. Other integer values are available. Stencil testing is separate from alpha opacity and depth testing.

View in the full Game dev collection