Read stencil buffer to texture

Thank you @serega-kkz and @rdb for your answers. However, I must admit that I’m a bit confused because @serega-kkz says it’s possible, while @rdb (and being a Panda3D developer) writes that it’s not possible.

@rdb, sending a feature request won’t benefit me - I work on macOS and the highest supported OpenGL is 4.1.

So maybe I will ask differently: what is the easiest way to find out from the fragment shader (which is a full-screen filter) which object a given pixel of the original texture belongs to? I wrote about it in this thread:

Generally speaking, I write my own volumetric lighting filter. In a scene, as you know, some objects cast light and some don’t. Moreover, I have ideas about various interactions between volumetric light and rendered objects (scattering on pixels of floating dust made of particles, or shining on the edge of the surface where a streak of volumetric light falls). This means the need to “code” at least a few categories of objects. For now, since I don’t use the alpha channel in my scene anyway, I thought of using it, which actually works for now. I just make different objects different transparency, and then read it in the shader as the .a component of vec4. It may be stupid, but it works. Of course, as long as I don’t want to switch on and use transparency.

But maybe there are some better methods to solve my problem?