Stencil buffer -> Fragment shader

Does anyone know how to pass stencil buffer into fragment shader? Maybe via a texture? I’ve been trying to do this, but have not figured out how to make it work. There are no stencil textures…

AFAIK, choosing depth+stencil attachment texture for the FBO gives you 24bits of depth plus 8 bits of stencil.
I guess you can read it in the Integer format and retrieve lower 8 bits, IIRC.

You don’t. Even a combined depth/stencil texture is still just a depth texture as far as the fragment program is concerned. It cannot access or use stencil values.

You would have to do some kind of copy to an integer texture, and then have the fragment program use that.

Yeah, as Alfonse said, you can’t access directly.

Up until just recently, textures were per pixel, stencil was per fragment (where with MSAA/SSAA, you might have multiple stencil samples per pixel). So it formerly was an apples and oranges thing. Because you can bind that to a texture unit / shader sampler.

But now with multisample textures and fragment access to those subsamples, it makes more sense to talk about accessing the stencil buffer at the stencil rate (per fragment). But yeah, you at least need to do a render-to-texture with stencil test on or something to fast-transcribe the stencil buffer into a texture of some sort.