GLSL 初级教程 – Fragment Processor

来源:互联网 发布:java可移植性 编辑:程序博客网 时间:2024/06/06 05:44

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), orgl_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.