DEPTH24_STENCIL8 texture format sampling from shaders

At the moment, shadow maps have their ambient factor added at the end of the lighting equation just before gl_FragColor. If you wanted an ambient factor in shadow volumes, you would have to play around with the alpha of the full screen quad.

Now wouldn’t it be nice if we can obtain the value of the stencil buffer from a fragment shader so we can add the ambient as we are going along. This allows the mixing of shadow maps and shadow volumes seemlessly.

Now we are partially there with FBOs and EXT_packed_depth_stencil extension. You can render to stencil, but you cant access that stencil value from fragment shaders.

I guess this request is so that we can have access to the stencil information stored in the texture.

Interesting idea. I’m just not sure if the hardware can do it, I have no idea how packed stencil is stored internally.

Any comments from the hardware guys?

Now wouldn’t it be nice if we can obtain the value of the stencil buffer from a fragment shader so we can add the ambient as we are going along. This allows the mixing of shadow maps and shadow volumes seemlessly.
You want to access it in what way?
Bind the stencil as a texture and sample it?
I guess this is not possible yet.

Yeah, I would like to bind a texture that is of DEPTH24_STENCIL8 and be able to sample the stencil bits from the texture.

At the moment, the specs clearly state that the only sampler that works with DEPTH24_STENCIL8 is sampler2DShadow, which is only useful in the cases of shadow mapping. I am interested in both and this is a very decent way of merging the two.

What would you do with stencil buffer access since glslang (until the recent nVidia extension drop) couldn’t handle integer values in any real way? I mean, it’s not like you could do bitwise operations on it.

You said it yourself, we now have integer, so now it’s useful :wink:

Even without bitwise ops, equality and greater/smaller compares could still be useful, for example when using the stencil buffer to count overlap, as in stencil shadows.

But I doubt non-integer hardware would support reading from a stencil texture anyway. I rather see this as an interesting idea that might start to make sense in the current or at least next generation of cards (provided we don’t get fully-blown blend shaders before that :stuck_out_tongue: ).

With this technique, it is theorhetically possible to do stencil shadows in 1 pass* with full ambient straight on the GPU without the CPU coming anywhere near. (Assuming you have a geometry shader enabled HW)

I think thats a pretty nice goal dont you think?

  • have to do the lighting via deferred lighting instead of forward lighting. But thats dam cool anyway…