A GLSL calculator, and what the GPU does with bad maths
Each row is one GLSL expression run on the GPU for one pixel. The last three rows are undefined behaviour: other GPUs may print other numbers.
A shader is a tiny program the GPU runs thousands of times at once: a vertex shader places each corner of a triangle, a fragment shader colours each pixel. You write them in GLSL and try them in a Shadertoy-style sandbox.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Make the sandbox show a vertical white-to-black gradient (white at the top).
Write the GLSL expression for a vec3 whose parts are the reverse of colour.
Explain it without notes
What is the difference between a vertex shader and a fragment shader, and how many times does each run for one triangle?
Why does float x = 1; fail in GLSL?
Console
Nothing logged yet.Expected console output
swizzle .zyx reverses a vec3 3, 2, 1, 0
maths works on every component 0.5, 1, 11, 12
mod, fract, floor, abs 1.5, 0.5, -1, 3
clamp, smoothstep, mix, step 1, 0.1563, 12.5, 0
3 / 2 is 1 (integers), mod(-1, 3) 1, 1.5, 2, 0
1/0, -1/0, 0/0 Infinity, -Infinity, NaN, 0
normalize of a zero vector NaN, NaN, NaN, 0
sqrt(-1), pow(-2, 2): undefined 0, 0, 0, 0