Writing directly to the stencil buffer

Instead of the standard increment, replace etc operations on the stencil buffer I’d like to be able to write arbitrary values directly from a shader. I don’t mind of this is during typical rendering with the stencil buffer bound via a frame buffer or the image_load_store extension or anything else. I just want to assign varying values in a single pass. I’m not aware of a way to do this, does anyone here have any ideas?

Unless you have a driver supporting GL_ARB_shader_stencil_export there’s no easy way to do that. When I needed a certain pattern in the stencil buffer for stencil routing I ended up with an 8-pass implementation that wrote one stencil bit per pass using the standard stencil ops.

http://www.opengl.org/registry/specs/ARB/shader_stencil_export.txt