Finding depth complexity, or how many fragments were generated for each pixel in a depth buffered scene, is important for analyzing graphics performance. It indicates how well polygons are distributed across the frame buffer and how many fragments were generated and discarded, clues for application tuning.
One way to show depth complexity is to use the color values of the pixels in the scene to indicate the number of times a pixel was written. It is relatively easy to draw an image representing depth complexity with the stencil buffer. The basic approach is simple. Increment a pixel's stencil value every time the pixel is written. When the scene is finished, read back the stencil buffer and display it in the color buffer, color coding the different stencil values.
This technique generates a count of the number of fragments generated for each pixel, whether the depth test failed or not. By changing the stencil operations, a similar technique could be used to count the number of fragments discarded after failing the depth test or to count the number of times a pixel was covered by fragments passing the depth test.
Here's the procedure in more detail:
You can control the mapping of stencil values to colors by turning on the color mapping with glPixelTransferiGL_MAP_COLOR, GL_TRUE(GL_MAP_COLOR, GL_TRUE) and setting the appropriate pixel transfer maps with glPixelMap(). You can map the stencil values to either RGBA or color index values, depending on the type of color buffer to which you're writing.