Shaders Intro
A shader is a small program that runs on the GPU. Not once, but thousands or millions of times per frame, in parallel: once for every vertex of a mesh, once for every pixel a triangle covers. Every lit surface, every glowing sword, every ripple on a lake and every blurry background in a modern game is the output of shaders. Graphics programmers write them, technical artists build them in node graphs, and gameplay programmers tweak them more often than they expect.
This course teaches shaders by writing them, live, in the lesson. You change a number, press ▶ Run (or Ctrl+Enter), and the picture changes.
Your first shader
The example below runs on your GPU right now. It is a fragment shader: a function that receives the position of one pixel and returns its color. The GPU calls it for every pixel of the frame, about 60 times per second.
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = fragCoord / iResolution.xy; // 0..1 across the frame
vec3 color = vec3(uv.x, uv.y, 0.5 + 0.5 * sin(iTime));
fragColor = vec4(color, 1.0); // red, green, blue, alpha
}Three things to notice:
- There is no loop over pixels. You describe what one pixel does; the GPU runs that description everywhere at once.
- The function cannot ask "what color is my neighbor?". Each pixel is computed independently. That restriction is what makes GPUs fast, and it shapes every technique in this course.
iTimeis a uniform: a value that is the same for every pixel in a frame, set by the program that draws the shader. Here it makes the blue channel pulse.
Try it: change uv.x to 1.0 - uv.x, or sin(iTime) to sin(iTime * 4.0), and run again.
How the live examples work
Code in a glsl playground is a WebGL2 fragment shader written in GLSL ES 3.00, drawn on a rectangle that covers the frame. It is written in the style made popular by Shadertoy: you write mainImage, and the playground adds the rest.
| Uniform | Type | Meaning |
|---|---|---|
iResolution |
vec3 |
frame size in pixels (z is 1) |
iTime |
float |
seconds since the shader started |
iTimeDelta |
float |
seconds since the previous frame |
iFrame |
int |
frame counter |
iMouse |
vec4 |
xy = pointer position while a button is held; zw = where it was pressed (negated after release). Pixels, origin at the bottom-left |
fragCoord is the pixel's center in pixels, with (0.5, 0.5) at the bottom-left (the OpenGL convention), not the top-left as in canvas or CSS.
A playground can also hold a complete shader that starts with #version 300 es. Then you write main(), the precision line and the uniforms yourself, and you get in vec2 vUv (0..1 across the frame) from the built-in vertex shader:
#version 300 es
precision highp float;
in vec2 vUv;
uniform float iTime;
out vec4 outColor;
void main() {
float stripes = step(0.5, fract(vUv.x * 8.0 + iTime * 0.5));
outColor = vec4(vec3(stripes * 0.8 + 0.1), 1.0);
}Shadertoy style is shorter, so most lessons use it. The full form matters when you move a shader into a real program, where nobody adds the prelude for you.
When a shader does not compile
A GPU driver compiles your shader when the page runs it. A typo does not crash anything: the compiler reports an error and nothing is drawn. The playground shows the error under the frame, with line numbers that match your code. This shader forgets to declare glow:
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = fragCoord / iResolution.xy;
fragColor = vec4(glow * uv, 0.0, 1.0);
}Shader compile error:
ERROR: line 3: 'glow' : undeclared identifier
The message has three parts: the line (line 3, counted in your code), the token the compiler choked on ('glow'), and the reason (undeclared identifier). Declare float glow = 2.0; on line 2 and the shader runs. When a compiler prints several errors, read them from the top: the first one is usually the real problem, and the rest are often consequences of it.
Note: The exact wording of shader errors depends on the browser and GPU driver. The messages in this course come from Chrome, which compiles WebGL shaders with ANGLE. Firefox and Safari word some of them differently, but the line number and the identifier are always there.
What you will learn
| Section | What you build |
|---|---|
| Getting Started | the GPU pipeline, GLSL types and syntax |
| Fragment Shaders | gradients, SDF shapes, tiling patterns, noise, animation, mouse input, 2D lighting |
| Vertex Shaders | triangles in raw WebGL2, displaced meshes with Three.js ShaderMaterial, matrices |
| 3D Lighting | a raymarched 3D scene: normals, Lambert, Blinn-Phong, shadows, normal maps, PBR, IBL |
| Effects | toon shading, rim light, dissolve, water, fog, outlines |
| Post-Processing | render targets, blur, bloom, tone mapping, color grading, vignette |
| Engine Shaders | Unity URP HLSL and Shader Graph, Unreal materials, Godot shaders, one effect in all three |
| Low-Level APIs | OpenGL, Vulkan, Direct3D 12, Metal and WebGPU, with Vulkan and WGSL code |
| Debugging and Toolkit | RenderDoc, PIX, Nsight, Spector.js, glslang, DXC, Slang |
The course ends with field notes, exercises and a mini project: an animated raymarched scene built step by step.
Prerequisites
You need comfortable programming in any C-like language and the vector math from Math for Games: dot and cross products, normalizing, matrices, and lerp. GLSL looks like C with built-in vectors, and the GLSL Basics lesson covers the differences. The vertex-shader and post-processing lessons use JavaScript and a little Three.js.
Test yourself
How many times does the GPU run a fragment shader for one frame of a full-screen effect?
- Once
- Once per triangle
- About once per pixel covered
- Once per uniform
What is a uniform such as iTime?
- A value that differs for every pixel
- A value set by the host program that is the same for every invocation in a draw
- A local variable
- A texture
Where is fragCoord (0.5, 0.5)?
- Top-left pixel
- Bottom-left pixel
- Center of the frame
- Bottom-right pixel