I’m trying to figure out how to read the depth buffer from a render texture in a shader. I’ve tried looking around for the answer, but it’s never been clear enough, at least to me.
You can’t access the depth buffer directly. You need to render depth manually into a separate texture and read it from there.
You can use MRT (multiple render targets) to render to multiple textures simultaneously. That can save you from needing a separate render pass just for depth.
If you don’t need the alpha channel of your main render target for anything else, you could put your depth there. You would need to use a 16 or 32 bit floating point surface format though. The 8 bit of the standard color format are almost never enough precision for depth.
I’ve tried MRT, but the depth buffer wasn’t written to at all for some reason. I might’ve done something wrong though. I can try alpha channel, though I’m not 100% sure that will be compatible with how it’s set up.