What is a Shader?

A shader programs a programmable stage in the graphical pipeline.

The Simplified Graphical Pipeline flow chart below shows different types and stages of shading (yellow):

Shader types in the graphical pipeline

  • Vertex Shading uses shaders to position vertices and tag them with attributes to interpolate across triangles
  • Tessellation and Geometry Shading uses shaders to refine triangle geometry and values to interpolate
  • Fragment (or pixel) Shading color individual pixels based on the interpolated values, texturing, and programmed procedures

Coding Shaders – GLSL and HLSL

Shaders are written in a C-like language, known as GLSL (GLslang) for OpenGL and HLSL (High-Level Shader Language) for DirectX D3D.

Example code:

uniform vec3 BrickColor, MortarColor;

Bricks with shader

Used with permission from “OpenGL Shading Language (3rd Edition)“

uniform vec2 BrickSize;
uniform vec2 BrickPct;

varying vec2 MCposition;
varying float LightIntensity;

void main()
{ vec3 color;
vec2 position, useBrick;

position = MCposition / BrickSize;

if (fract(position.y * 0.5) > 0.5) position.x += 0.5;

position = fract(position);

useBrick = step(position, BrickPct);

color = mix(MortarColor, BrickColor, useBrick.x * useBrick.y);
color *= LightIntensity; gl_FragColor = vec4(color, 1.0);

Shader Visual Example

Shaders can be complex, performing many advanced effects for each pixel for every frame drawn.

Graphic before shader


Graphic after shader applied

GLSL fragment shader applied to the water