I'm currently trying to figure with what equation the non-linear depth value of the fragment's gl_FragCoord.z value is calculated. I know how to transform the fragment's depth values back to their linear equivalent, but I couldn't find any good resource on the reverse operation.

Currently my shader uses the following function to transform the gl_FragCoord's z-component to its linear variant, but what equation is used for transforming from the linear to the non-linear value? So from **view-space** z value --> **screen-space** z value.

Shader code:

float LinearizeDepth(float depth) { float near= 0.1; // camera z near float far = 100.0; // camera z far float z = depth * 2.0 - 1.0; // Back to NDC return (2.0 * near) / (far + near - z * (far - near)); } void main() { float depth = LinearizeDepth(gl_FragCoord.z); // <-- Some equation that transforms it back to the original gl_FragCoord.z value? color = vec4(vec3(depth), 1.0f); }

Any insight would be appreciated.