Shaders are computer programs used in computer graphics to calculate the appropriate levels of light, darkness, and color during the rendering of a 3D scene, a process known as shading. They are short programs that run on the graphics hardware and render graphics data such as meshes, textures, and lights. Shaders are coded for and run on a graphics processing unit (GPU), and shading languages are used to program the GPUs rendering pipeline.
Shaders are used to produce a range of effects in cinema post-processing, computer-generated imagery, and video games. They can alter the hue, saturation, brightness, or contrast of an image, and can also be used to apply lighting values, bump mapping, shadows, specular highlights, translucency, and other phenomena. There are two main types of shaders:
-
Pixel Shaders: Also known as fragment shaders, these compute color and other attributes of each "fragment," which is a unit of rendering work affecting at most a single output pixel. Pixel shaders can output one screen pixel as a color value or more complex shaders with multiple inputs/outputs.
-
Vertex Shaders: These operate on vertices of a mesh and are used to transform the position of each vertex in 3D space.
Shaders are executed all at once for every single pixel on the screen, which means the code you write has to behave differently depending on the position of the pixel on the screen. Shaders are fast because they take advantage of parallel processing. However, they can be challenging to code due to the abstraction required to code a general function that changes the result pixel by pixel depending on its position, and the blind and memoryless constraints.