A Z-buffer involves a block of memory equal to the display resolution (e.g. 640x480) multiplied by the number of bits of depth resolution desired (e.g. 16-bits). The Z-buffer algorithm stores a depth value for each pixel in the display. When rendering objects, the engine calculates a Z-value for each pixel and stores that value in the Z-buffer memory location corresponding to the X/Y address of the pixel in the display buffer. The lower the Z-value, the close the object is to the viewer. When a new pixel is rendered its Z-value is compared with the Z-value of the current pixel and only if the Z-value of the new pixel is less than the one already stored will the new pixel be written to the display buffer.
The Z-buffer takes up part of the frame buffer, and it speeds up games by telling the graphics processor not to render objects that aren't actually in view, specifically objects or parts of objects which are covered up by other geometry. Z-buffer depths range from 16bit to 32bit, and it can take up as much memory as an extra frame buffer.
So you can see from here that your average 16MB video card can take up around 7MB on the frame buffer, and up to 7MB for Z calculations. That doesn't leave
too much for texture memory!
Stencil Buffer
Stencil bits are stored in the Z-buffer. It's usually only 1bit or 8bit. The stencil allows applications to "tag" various regions of the frame buffer.
Stencil Buffer Masking
The stencil buffer can tell the video card what it's allowed to redraw. For example, let's say you have an air simulation game where the view is from the cockpit. It would be a waste to render the cockpit interior every frame, right? Well, the stencil buffer will make sure that the cockpit interior doesn't get redrawn.
When the scene is first drawn the video card sets the stencil buffer write mask to a number greater than 1, and draws the cockpit interior tagging it with the >1 stencil number. Now the rest of the scene is drawn with a stencil tag of 0. The stencil buffer tag will tell the video card to only redraw where it sees a 0 in the stencil buffer. The cockpit interior with the >1 tag will always be skipped. Programmers can just clear the stencil buffer whenever they want to redraw the pixels in the cockpit interior.