How to debug stencil buffer?

I am messing up with stencil buffer. Is there a simple way to visualize the stencil buffer and debug? Like the image below showing GTA5’s stencil map. I saw it in this blog.

I am trying to implement my own deferred rendering pipeline. In the first pass, gBuffer pass, I use a FDepthStencil texture to store the depth and stencil. Then the following lightBuffer perform light calculation on each opaque object’s pixel. Finally, the stencil debug buffer simply output a color for the pixel passed stencil test.

It should work correctly but I just cannot make it right. :confused:

My attribute setting code on each buffer.

In gBuffer, I set every opaque object pixels’ stencil to 1, and set rest of the pixels to 0.

tmpnode.setAttrib(StencilAttrib.make(1, StencilAttrib.SCFAlways, StencilAttrib.SOZero,
            StencilAttrib.SOReplace, StencilAttrib.SOReplace, 1, 0, 1))

In lightBuffer, I let stencil value keep anyway.

tmpnode.setAttrib(ColorBlendAttrib.make(ColorBlendAttrib.MAdd, ColorBlendAttrib.OOne, ColorBlendAttrib.OOne))
tmpnode.setAttrib(StencilAttrib.make(1, StencilAttrib.SCFAlways, StencilAttrib.SOKeep,
            StencilAttrib.SOKeep, StencilAttrib.SOKeep, 1, 1, 0))

stencil debug buffer
In stencil debug buffer, I render a full screen quad and perform a simple shader outputting red color.

tmpnode.setAttrib(StencilAttrib.make(1, StencilAttrib.SCFEqual, StencilAttrib.SOKeep,
            StencilAttrib.SOKeep, StencilAttrib.SOKeep, 1, 1, 0))

I expecting red color(shader output) on opaque object, and black color(clear color) on sky(where nothing in there). However, the final image is a totally red, no difference between opaque scene and sky. I am messing up with this, which step did I wrong?

Hmm. You should be able to use the buffer viewer or an external tool like apitrace to inspect the stencil buffer.

How exactly are you binding the texture to the stencil slot?