A Fragment is a collection of values produced by the Rasterizer. Each fragment represents a sample-sized segment of a rasterized Primitive. The size covered by a fragment is related to the pixel area, but rasterization can produce multiple fragments from the same triangle per-pixel, depending on various multisampling parameters and OpenGL state. There will be at least one fragment produced for every pixel area covered by the primitive being rasterized.
Fragments are processed in a manner similar to vertices in a Vertex Shader. For them, an input vertex, built from Vertex Attributes defined during Vertex Specification, enters the Vertex Shader. After arbitrary processing, the vertex shader writes a number of values. These values represent the output vertex, and this output vertex is passed along to the next stages of the pipeline.
Fragments work the same way. An input fragment, built by the rasterizer, enters the Fragment Shader. After arbitrary processing, the fragment shader writes a number of values. These values represent the output fragment, and this output fragment is passed along to the next stage of the pipeline.
Fragment shader inputs
Initially, the data for a fragment consists of the following:
- Arbitrary values written by the last vertex processing stage, which have been interpolated across the primitive's surface according to each variable's interpolation qualifiers.
- The window-space position of the fragment, in X, Y and Z.
- A stencil value.
- Whether the fragment was produced from the front or back face of a primitive.
- Multisample coverage masks. When doing per-sample shading, this also includes the sample ID and location within the pixel.
- The primitive identifier, output by the rasterizer or the Geometry Shader.
- Any active user-defined clipping distances.
- If point rasterization is being used, then there will be a location within the point for this fragment.
- If a Geometry Shader is active, and it uses layered rendering, the primitive's layer and viewport indices are also part of the fragment.
Not all of these are accessible from the fragment shader.
Fragment shader outputs
After processing, the output fragment from a fragment shader consists of the following:
- a depth value, either written by the fragment shader or passed through from the screen-space fragment's Z value.
- a stencil value.
- An array of zero or more color values, as written by the fragment shader. Any entries in the array which the fragment shader did not write have undefined values.
The color value array elements are routed to different buffers based on the framebuffer's draw buffer state.
If no fragment shader is active, then the fragment has only stencil and depth (the screen-space fragment's Z value). It has no defined color values in its array.
No matter what, a discarded fragment will never affect any color buffers of the current Framebuffer through the fragment shader's color outputs. Writes via other means (such as Image Load Store) issued before a fragment shader's discard is executed are still properly issued.
Normally, a discarded fragment will also not affect the current depth or stencil buffers. However, there are certain cases where such updates can occur, even for a discarded fragment.
If a fragment shader specifies that depth/stencil tests should happen before the shader, then this also means that the depth/stencil buffers will be updated before the shader (in accord with the Stencil Test operation and Write Masks). So with early fragment tests turned on, if the fragment shader explicitly discards the fragment, the depth and stencil buffers will have already been updated. This also means that Occlusion Query sample counts are updated even from such discarded fragments.
The other way is specific to the stencil buffer. If Stencil Testing is active, then fragments discarded by the stencil test itself can still affect the stencil buffer. The stencil test can modify the stencil buffer, even on a stencil or depth test failure. And since the stencil test happens before the depth test, the depth test failures cannot prevent the stencil test from updating the stencil buffer.
Outside of these cases, a discarded fragment will have no effects.