In this article
- 1. What is WebGPU?
- 2. WebGPU vs WebGL — a quick comparison
- 3. Understanding WGSL — the shading language
- 4. Your first shader: a gradient rectangle
- 5. Adding animation with uniforms
- 6. Where to go from here
1. What is WebGPU?
WebGPU is the next-generation graphics API for the web. It replaces WebGL with a modern, low-level interface that gives developers explicit control over the GPU. Think of it as the browser equivalent of Vulkan or Metal — designed for performance, flexibility, and access to modern GPU features.
Unlike WebGL, which is built on top of the aging OpenGL ES standard, WebGPU was designed from the ground up for today's hardware. It supports compute shaders, better memory management, and a pipeline architecture that mirrors how GPUs actually work.
As of 2025, WebGPU is supported in Chrome, Edge, and Firefox (behind a flag), with Safari support in development. It's production-ready for most use cases, and tools like the Shadex playground make it easy to experiment without any local setup.
2. WebGPU vs WebGL — A Quick Comparison
| Feature | WebGL | WebGPU |
|---|---|---|
| Shading language | GLSL | WGSL |
| Compute shaders | ✗ | ✓ |
| Pipeline model | Implicit state | Explicit pipelines |
| Error handling | Silent failures | Validation layer |
| Based on | OpenGL ES | Vulkan / Metal / D3D12 |
For a deeper comparison, check out our article on WebGPU vs WebGL: Why WebGPU Wins for Real-Time Graphics.
3. Understanding WGSL — The Shading Language
WGSL (WebGPU Shading Language) is a new shader language created specifically for WebGPU. If you've used GLSL or HLSL before, you'll find many familiar concepts — but with Rust-inspired syntax and stronger type safety.
Key features of WGSL:
- →Strong typing — every variable has an explicit type (
f32,vec2f,vec4f) - →Attribute annotations — built-in decorations like
@vertex,@fragment,@builtin - →Struct support — define custom data structures for uniforms and vertex data
- →Let and var — immutable (
let) and mutable (var) bindings
Here's a minimal WGSL fragment shader that outputs a solid color:
@fragment
fn fs(@builtin(position) pos: vec4f) -> @location(0) vec4f {
return vec4f(0.0, 0.9, 1.0, 1.0); // cyan
}4. Your First Shader: A Gradient Rectangle
Let's write a complete shader that renders a smooth gradient across the screen. This is the "Hello World" of shader programming — and you can try it right now in the Shadex playground.
Every WebGPU shader needs two stages: a vertex shader that positions geometry on screen, and a fragment shader that determines the color of each pixel.
struct Uniforms {
time: f32,
_pad: f32,
resolution: vec2f,
}
@group(0) @binding(0) var<uniform> u: Uniforms;
// Full-screen triangle via vertex index
@vertex
fn vs(@builtin(vertex_index) i: u32) -> @builtin(position) vec4f {
var pos = array<vec2f, 6>(
vec2f(-1, -1), vec2f(1, -1), vec2f(-1, 1),
vec2f(-1, 1), vec2f(1, -1), vec2f(1, 1),
);
return vec4f(pos[i], 0, 1);
}
// Color each pixel based on its UV coordinates
@fragment
fn fs(@builtin(position) pos: vec4f) -> @location(0) vec4f {
let uv = pos.xy / u.resolution;
// Blend between two colors based on position
let col1 = vec3f(0.0, 0.9, 1.0); // cyan
let col2 = vec3f(0.22, 1.0, 0.52); // green
let color = mix(col1, col2, uv.x);
return vec4f(color, 1.0);
}Let's break down what's happening:
- 1.The Uniforms struct holds data passed from JavaScript — time for animation and the canvas resolution.
- 2.The vertex shader outputs a full-screen quad from 6 hardcoded vertices (two triangles).
- 3.The fragment shader normalizes pixel coordinates to 0–1, then mixes two colors using the x-position.
💡 Tip: Paste this code into the Shadex playground to see it render instantly — no build step required.
5. Adding Animation with Uniforms
Static gradients are nice, but shaders really shine when they move. The Shadex playground automatically passes a time uniform to your shader, so adding animation is just a matter of using it:
@fragment
fn fs(@builtin(position) pos: vec4f) -> @location(0) vec4f {
let uv = pos.xy / u.resolution;
let t = u.time;
// Animated color cycling
let r = sin(uv.x * 6.28 + t) * 0.5 + 0.5;
let g = sin(uv.y * 6.28 + t * 1.3) * 0.5 + 0.5;
let b = sin((uv.x + uv.y) * 3.14 + t * 0.7) * 0.5 + 0.5;
return vec4f(r, g, b, 1.0);
}By combining sin() functions with time and UV coordinates, you get smooth, endlessly cycling color patterns. This technique is the foundation of countless shader effects — from plasma gradients to procedural landscapes.
Check out the Shadex gallery for more advanced examples of animated shaders, including particle systems, fractals, and raymarched 3D scenes.
6. Where to Go from Here
You've written your first WebGPU shader — congratulations! Here's how to keep leveling up:
- →Explore the gallery — Browse curated shader examples from fractals to particle simulations, and fork them into the playground.
- →Try advanced effects — Read our guide on 5 Mind-Blowing Shader Effects you can build in your browser.
- →Go deeper on WebGPU — Our WebGPU vs WebGL comparison explains why WebGPU is the future of browser graphics.
- →Upgrade to Pro — Get private shaders, high-resolution exports, and priority rendering with Shadex Pro.
Ready to start building?
Open the Shadex playground and paste your first shader. No account required — just code and create.