Write Fragdepth to FBO

What do I need to divide/multiply/shift gl_FragDepth by to write it out to an FBO?

I have never gotten it to work, and I need to use it.

For a normalised (non-floating-point) depth buffer, gl_FragDepth needs to be between 0.0 and 1.0. The value of gl_FragDepth on entry to the fragment shader is unspecified, i.e. you shouldn’t read it unless you’ve already written to it.

If the shader doesn’t include an assignment to gl_FragDepth, then gl_FragCoord.z will be used as the depth. If the shader conditionally assigns to gl_FragDepth, then any invocation which doesn’t assign to it will result in an unspecified depth value being written (i.e. the determination of whether to use gl_FragDepth or gl_FragCoord.z is made when the code is compiled).

Assigning to gl_FragDepth is incompatible with early depth tests (i.e. they won’t be silently enabled as an optimisation, and if the shader explicitly enables them then any assignment to gl_FragDepth is ignored).

What do I need to divide/multiply/shift gl_FragDepth by to write it out to an FBO?

gl_FragDepth is directly hooked into the depth attachment of a framebuffer object. There is nothing you can do to change where it gets written. The only thing you can control is the image format of the texture that you attach as the depth attachment for the FBO.