Fragment shaders are invoked for each fragment, or as helper invocations.
Most operations in the fragment shader are not performed in rasterization order, with exceptions called out in the following sections.
For fragment shaders invoked by fragments, the following rules apply:
- A fragment shader must not be executed if a fragment operation that executes before fragment shading discards the fragment.
- A fragment shader may not be executed if:
- An implementation determines that another fragment shader, invoked by a subsequent primitive in primitive order, overwrites all results computed by the shader (including writes to storage resources).
- Any other fragment operation discards the fragment, and the shader does not write to any storage resources.
- Otherwise, at least one fragment shader must be executed.
- If sample shading is enabled and multiple invocations per fragment are required, additional invocations must be executed as specified.
- Each covered sample must be included in at least one fragment shader invocation.
If no fragment shader is included in the pipeline, no fragment shader is executed, and undefined values may be written to all color attachment outputs during this fragment operation.
Multiple fragment shader invocations may be executed for the same fragment for any number of implementation-dependent reasons. When there is more than one fragment shader invocation per fragment, the association of samples to invocations is implementation-dependent. Stores and atomics performed by these additional invocations have the normal effect.
For example, if the subpass includes multiple views in its view mask, a fragment shader may be invoked separately for each view.
Reading from the SampleMask built-in in the Input storage class will return the coverage mask for the current fragment as calculated by fragment operations that executed prior to fragment shading.
If sample shading is enabled, fragment shaders will only see values of 1 for samples being shaded – other bits will be 0.
Each bit of the coverage mask is associated with a sample index as described in the rasterization chapter. If the bit in SampleMask which is associated with that same sample index is set to 0, that coverage mask bit is set to 0.
Values written to the SampleMask built-in in the Output storage class will be used by the multisample coverage operation, with the same encoding as the input built-in.
Writing to the FragDepth built-in will replace the fragment’s calculated depth values for each sample in the input SampleMask. Depth testing performed after the fragment shader for this fragment will use this new value as zf.