Playground

Python on the GPU · Fun & games

– fps
checking…
WGSL

    
How a program works
  • The editor holds a Python module. Every @kernel function becomes a WebGPU compute shader. Run (Ctrl/Cmd+Enter) compiles it; Auto does the same about 0.7 s after you stop typing. A failed compile keeps the last good program running and marks the line.
  • The grid is W cells across, set by Grid. H follows the shape of the canvas, at most 2048. Each kernel runs once per cell: gid % w is the column and gid // w the row.
  • Every list[float] parameter, in any kernel, is one W×H buffer shared by that name. Reset zeroes them.
  • Declare w and h as int, t as float (seconds since reset), frame as int, mx and my as float (pointer in cells, -1 outside the canvas) and mdown as int (1 while pressed or touching). The playground fills them in. Any other scalar becomes a slider.
  • # slider NAME MIN MAX DEFAULT sets a slider's range. Without it a float slider runs 0 to 1 at 0.5 and an int slider 0 to 100 at 10.
  • # steps N runs the per-frame kernels N times a frame. # display LO HI maps values linearly; # display cyclic FREQ wraps value × FREQ around the palette. The default is # display 0 1.
  • init runs once after each reset. Every other kernel runs every frame, in source order. The picture shows out if there is one, otherwise the last buffer the last kernel writes.
  • Pause stops the kernels and freezes t and frame. Sliders apply from the next frame; a value that only init reads applies on Reset.
  • Supported: arithmetic, comparisons, if, while, for … in range(), break, continue, and min max abs floor sqrt exp log pow with sin cos tan atan2. There is no **; write x*x.

Unlike the demos, nothing here compares the GPU result with CPython. The compiler is the same one, ported to JavaScript and held to the Python version by a test suite. In the editor, Tab indents, Shift+Tab dedents and Esc leaves the editor.