DevTools Logo

WGSL Shader Playground

WGSL Shader Playground

Write WGSL and render it on a live WebGPU canvas with inline diagnostics, uniform sliders and screenshot export.

Runs in your browserWebGPU unavailable
Try an example

WGSL fragment shader

17 lines · 0 error(s)

Diagnostics — 0 error(s), 0 other(s)

  • No diagnostics. Shader looks valid locally.

WebGPU canvas

4 uniforms · 32 bytes

Uniforms

Built-ins: u_time, u_resolution, u_mouse. Uniform buffer: 32 bytes (16-byte aligned).

Details

An in-browser WGSL scratchpad backed by real WebGPU. Edit a fragment shader, compile it through a GPUDevice, and see the canvas update on the next frame. Compilation messages appear with line and column numbers; clicking a row focuses the editor. The harness provides a fullscreen-triangle vertex stage and a Uniforms struct with built-in time, resolution and mouse values, plus sliders for user f32 fields computed from WGSL alignment rules. Four starters — plasma, checker, raymarch-lite and noise fbm — get you going; pause, screenshot and copy/download actions round out the loop.

More examples

Plasma

let t = U.u_time * U.u_speed;
let v = sin(uv.x * 10.0 + t) + sin(uv.y * 8.0 - t);

A starter fragment shader driven by the built-in time uniform and the u_speed slider.

Uniform struct

struct Uniforms { u_time: f32, u_resolution: vec2f, u_mouse: vec2f, u_speed: f32, }
@group(0) @binding(0) var<uniform> U: Uniforms;

The parser computes real offsets — vec2f at byte 8 after u_time — and pads the buffer to a 16-byte multiple.

Driver diagnostics

@fragment fn fs_main() -> @location(0) vec4f { return vec4f(1.0) }

A missing semicolon produces a severity/line/column row; the last good frame stays on the canvas.

Examples

Time-driven plasma

Input
let t = U.u_time * U.u_speed;
Output
Animated colour field

u_time advances every frame and u_speed comes from the generated slider.

Aligned struct

Input
struct Uniforms { u_time: f32, u_resolution: vec2f }
Output
u_resolution at byte 8; buffer size 16

Alignment holes are computed rather than guessed, matching WGSL's uniform layout rules.

Missing entry point

Input
@fragment fn main() {}
Output
Diagnostic: missing @fragment fn fs_main

A local check produces a readable error before the driver reports the same issue.

About this tool

WGSL Shader Playground is a live WebGPU scratchpad. Edit a fragment shader in the left pane and the canvas recompiles on a real GPU device; the browser driver — not a reimplemented parser in JavaScript — decides whether your shader is valid.

The harness supplies a fullscreen-triangle vertex stage and a uniform block with u_time, u_resolution and u_mouse built in. Add f32 fields such as u_speed to the Uniforms struct and the tool computes their WGSL-aligned byte offsets, pads the buffer to 16 bytes and generates labelled sliders. Drag a slider and the next frame picks up the new value without a recompile.

Compilation messages from getCompilationInfo() are shown with severity, line and column; clicking a row moves the caret to the reported position. Local sanity checks add friendly hints for a missing fs_main entry point, unbalanced braces or leftover GLSL syntax. A successful frame stays on the canvas while you repair a broken edit.

Four starters — plasma, checker, raymarch-lite and noise fbm — demonstrate the grammar. Pause freezes time for inspection, double-clicking the canvas resets u_time, and the Screenshot button saves the current frame as PNG. If the browser has no WebGPU adapter, a support panel explains the situation while the editor and validation remain usable.

What this tool does not do

  • Live rendering requires a browser with WebGPU enabled and an available adapter; otherwise only local validation runs.
  • The harness provides a fullscreen triangle and a single uniform buffer; there are no textures, meshes or compute pipelines.
  • Only one @group(0) @binding(0) uniform struct is mapped; multiple structs or non-zero bindings are reported as layout errors.
  • Slider ranges are inferred from uniform names; rename or edit WGSL constants when you need a different range.
  • Driver validation messages vary between browsers; the driver shown by your browser is the source of truth.

How to use this tool

  1. Check the badge

    The header badge reports WebGPU availability. In a supporting browser the pipeline boots automatically; otherwise the support panel appears and local validation still works.

  2. Pick or write a shader

    Choose one of the four starters, then edit the WGSL. Define @fragment fn fs_main and return a @location(0) vec4f colour.

  3. Use the built-in uniforms

    Read U.u_time, U.u_resolution and U.u_mouse. Add f32 fields to the Uniforms struct to get sliders for your own parameters.

  4. Read diagnostics

    Errors and warnings appear with line and column. Click a row to focus that position in the editor; the last good frame is retained.

  5. Inspect and export

    Pause to freeze a frame, move the mouse over the canvas for u_mouse, double-click to reset time, then save a PNG screenshot or copy/download the WGSL source.

Use cases

Port GLSL to WGSL

Paste a GLSL-style shader, see the friendly hint, and rewrite it against a real WebGPU driver.

Iterate on procedural art

Tune u_speed or a custom amplitude slider while the canvas animates at display refresh.

Learn uniform alignment

Add a vec3f or vec2f field and watch the byte offsets and padded buffer size update.

Capture illustrations

Pause on a pleasing frame and export a PNG for a blog post or slide deck.

Uniform layout rules used by the harness

TypeAlignment / size
f32, i32, u324-byte alignment / 4-byte size
vec2f8-byte alignment / 8-byte size
vec3f16-byte alignment / 12-byte size (with padding)
vec4f16-byte alignment / 16-byte size
structTotal size rounded up to a multiple of 16 bytes

Offsets are computed per member, so a vec2f after a single f32 begins at byte 8, not byte 4.

Common mistakes

Mistake:Writing gl_FragColor or void main().

Fix:WGSL uses @fragment fn fs_main(...) -> @location(0) vec4f; the local check points this out.

Mistake:Assuming fields are packed back-to-back.

Fix:vec2f and vec3f require 8- and 16-byte alignment; the layout panel shows the real offsets.

Mistake:Renaming the uniform struct or binding without updating the declaration.

Fix:Keep struct Uniforms and @group(0) @binding(0) var<uniform> U: Uniforms; the parser reports mismatches.

Mistake:Expecting a shader typo to blank the canvas.

Fix:Compilation failures keep the last successfully rendered frame until the source is fixed.

Mistake:Using a browser without WebGPU and expecting live rendering.

Fix:The tool shows a support panel; editing and local validation still work while you switch browsers.

Frequently asked questions

Which browsers support WebGPU here?

Recent Chrome and Edge on desktop ship WebGPU, as does Safari on supported macOS/iOS versions. Firefox support varies by release and platform. The tool checks navigator.gpu and requests an adapter at startup: if either step fails, a support panel explains why, while the editor and local validation stay available.

Why doesn't my GLSL work in WGSL?

WGSL is a different language, not a GLSL dialect. There is no gl_FragColor, precision qualifier or void main(); declare @fragment fn fs_main(@builtin(position) pos: vec4f) -> @location(0) vec4f and return a colour. The local checker adds a hint when it spots GLSL constructs.

How do uniforms map to the buffer layout?

Write struct Uniforms with fields such as f32, vec2f or vec3f and bind it with @group(0) @binding(0) var<uniform> U: Uniforms. The parser computes each field's offset using WGSL alignment rules (f32 = 4, vec2f = 8, vec3f/vec4f = 16), pads the struct to a 16-byte multiple and shows the size. f32 fields other than the built-ins become sliders.

Can I write compute shaders?

Not in this playground. It builds a render pipeline with a fixed fullscreen-triangle vertex stage and a single uniform buffer, so compute shaders, storage buffers, textures and meshes are out of scope. The WebGPU specification link in About covers those features.

What happens to my last frame when compilation fails?

The canvas keeps the last successfully rendered frame. Driver diagnostics — severity, line and column — are listed below the editor, and clicking one focuses the editor at that position. Fix the source and the next successful compile replaces the frame.

Is my shader uploaded or shared?

No. Compilation and rendering happen on your own GPU through the browser's WebGPU API. Screenshots stay on your device until you save them, and no shader text or telemetry is sent anywhere.

References & standards