writing to the stencil buffer with a fragment program


is it possible (and if so: how?) to get a Cg fragment program to write to the stencil buffer? Ideally I’d like to be able to compute some value for a fragment and if it’s a certain value compute a colour and write that colour to the framebuffer, otherwise I want to touch the z-buffer and mark that fragment in the stencil buffer. However, I can’t see any way to bind the stencil buffer in the output. Is this possible?

thanks in advance,

You can’t access the stencil directly in a shader. Can you expand on what you do with the depth value, i.e. when you say you ‘touch’ the depth are you calculating a depth value in the fragment shader ?

One thing you could consider is using one of the RGB(A) components to hold a stencil value. You can enable blending and add or subtract 1/256 (or whatever).