Fragment Shader/Defined Outputs
Fragment Shaders have the following built-in output variables.
out float gl_FragDepth;
- This output is the fragment's depth. If the shader does not statically write this value, then it will take the value of gl_FragCoord.z.
- To "statically write" to a variable means that you write to it anywhere in the program. Even if the writing code is technically unreachable for some reason, if there is a gl_FragDepth = ... expression anywhere in the shader, then it is statically written.
GLSL 4.20 or ARB_conservative_depth allows the user to specify that modifications to gl_FragDepth (relative to the gl_FragCoord.z value it would have otherwise had) will happen in certain ways. This allows the implementation the freedom to not turn off Early Depth Tests in certain situations.
This is done by re-declaring gl_FragDepth with a special layout qualifier:
layout (depth_<condition>) out float gl_FragDepth;
The condition can be one of the following:
- The default. You may freely change the depth, but you lose the most potential performance.
- You will only make the depth larger, compared to gl_FragCoord.z.
- You will only make the depth smaller, compared to gl_FragCoord.z.
- If you write to gl_FragDepth, you will write exactly gl_FragCoord.z.
Violating the condition yields undefined behavior.
GLSL 4.00 or ARB_sample_shading brings us:
out int gl_SampleMask;
- This defines the sample mask for the fragment when performing mutlisampled rendering. If a shader does not statically write to it, then it will be filled in by gl_SampleMaskIn. The sample mask output here will be logically AND'd with the sample mask computed by the rasterizer.