Help end child hunger

Fragment Processor

Prev: Vertex Processor Next: Setup for GLSL

The fragment processor is where the fragment shaders run. This unit is responsible for operations like:

  • Computing colors, and texture coordinates per pixel
  • Texture application
  • Fog computation
  • Computing normals if you want lighting per pixel

The inputs for this unit are the interpolated values computed in the previous stage of the pipeline such as vertex positions, colors, normals, etc…

In the vertex shader these values are computed for each vertex. Now we’re dealing with the fragments inside the primitives, hence the need for the interpolated values.

As in the vertex processor, when you write a fragment shader it replaces all the fixed functionality. Therefore it is not possible to have a fragment shader texturing the fragment and leave the fog for the fixed functionality. The programmer must code all effects that the application requires.

The fragment processor operates on single fragments, i.e. it has no clue about the neighboring fragments. The shader has access to OpenGL state, similar to the vertex shaders, and therefore it can access for instance the fog color specified in an OpenGL application.

One important point is that a fragment shader can’t change the pixel coordinate, as computed previously in the pipeline. Recall that in the vertex processor the modelview and projection matrices can be used to transform the vertex. The viewport comes into play after that but before the fragment processor. The fragment shader has access to the pixels location on screen but it can’t change it.

A fragment shader has two output options:

  • to discard the fragment, hence outputting nothing
  • to compute either gl_FragColor (the final color of the fragment), or gl_FragData when rendering to multiple targets.

Depth can also be written although it is not required since the previous stage already has computed it.

Notice that the fragment shader has no access to the frame buffer. This implies that operations such as blending occur only after the fragment shader has run.


Prev: Vertex Processor Next: Setup for GLSL

  2 Responses to “Fragment Processor”

  1. Hello dear lighthouse team 🙂

    This series of tutorials is ridiulously amazing and well written, and untill know i haven’t found
    any tutorials in this quality, so i want to thank you for that very very much!

    But i also have a question 🙂
    When you wrote: ‘The fragment shader has access to the pixels location on screen but it can’t change it.’ -> with ‘…has acces to the pixels location on screen…’ I suppose you mean the position in
    ‘3D’ space right? which are then interpreted by the frustum.
    This may be something obvious but i cannot figure what you meant there exactly 🙂

    Again, thank you very much, and keep the site up, I have to go for that donation button!

    greetings and all the best from luxembourg, Jesse

    take care

    • Hi Jesse,

      Thanks for your kind words.

      In the fragment shader there is an input variable called gl_FragCoord, which contains the window relative coordinates, namely the X and Y window coordinates.

      Best Regards,


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: