Finding depth complexity, or how many fragments were generated for each pixel in a rendered scene, is important for analyzing rasterization performance. It indicates how well polygons are distributed across the framebuffer 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:
glClear(GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 0, 0); glStencilOp(GL_KEEP, GL_INCR, GL_INCR);
You can control the mapping of stencil values to colors by 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. In color index mode, you must turn on the color mapping with glPixelTransferiGL_MAP_COLOR, GL_TRUE(GL_MAP_COLOR, GL_TRUE).