Skip to main content
ooh bubbles Optical Field Guide
Back to Bubbles

How it runs so smoothly

Ooh Bubbles simulates up to a couple of hundred soap bubbles — each with its own film thickness, wobble, colour, and pop — and draws them at your screen’s full native sharpness, aiming for 60 frames a second (or 120 Hz on supported displays) in an ordinary browser tab. When a device can’t hold pace, it sheds bubbles rather than lowering resolution.

On this page

Off-thread physics worker & native WebGPU render loop

Page thread

  • Input · Audio · UI chrome
  • Native-pixel governor
  • WebGPU / WGSL (+ WebGL2)
  • 1-pass HDR · 6 HDRI worlds

Draws every frame smoothly

CommandRing (SAB)

Page → Worker · lock-free

Binary snapshots

Worker → Page · 181 KB rows

Physics worker

  • Rust → WASM (libm only)
  • SIMD128 & Relaxed-SIMD
  • Deterministic thread pool
  • Contacts · Wobble · Smoke

Steps world at 1:1 real time

The page draws; an autonomous worker steps the physics. The page never stalls waiting for a physics substep — it presents the newest finished snapshot and advances visual film/smoke slices cleanly across each world-time span.

Physics in Rust, compiled to WebAssembly

The simulation began in TypeScript. As real physics accumulated (thin films, contact clusters, solenoidal wind, volumetric smoke, rupture events, methane combustion, and freezing), hot loops began eating the frame. Moving the core numerical kernels into pure Rust compiled to WebAssembly with SIMD delivered large, measured wins: the wind field dropped from 26–28% of a busy main thread to 6–7% at 300 bubbles; bubble wobble fell from 11–15% to 2–3.5%; and a single field-filling kernel dropped from 4.3 ms to 0.98 ms.

Keeping physics completely free of renderer imports also lets it run off the page thread on a deterministic thread pool — scheduling only changes which helper computes a chunk, never item order or floating-point arithmetic within it.

Native WebGPU, WGSL & lightweight WebGL2 fallback

Early builds rendered through Three.js and WebGL2. In PR #279 the primary renderer moved to raw WebGPU with hand-written, Naga-validated WGSL shaders on a single shared GPU device — deleting Three.js completely and cutting the median 3840×2160 Studio frame with 200 bubbles from 28.2 ms to 21.4 ms. For browsers without WebGPU, the engine ships a lightweight 22-shader WebGL2 fallback (still zero Three.js).

By default, a frame runs as a tight 1-pass HDR pipeline (drawing the studio backdrop or active 360° photographic HDRI directly inside the bubble MRT pass whenever lens effects are off, with analytical silhouette anti-aliasing instead of costly MSAA): thin-film colour lookup tables computed on the GPU (rebuilding asynchronously only on soap-recipe switch for Lorentz–Lorenz glycerol dispersion, 0.00 ms/frame steady state), 1-pass octahedral film compute with buoyant marginal-regeneration plumes and ice-rigidity locking, reflections and particle spray sharing render passes on mobile tile GPUs, lazy-loaded volumetric fireballs and GPU-deposited 3D smoke (with mobile worker budget caps) only when active, optional half-resolution depth of field and multi-level bloom, and final AgX tone mapping with dither.

Measuring the GPU honestly & testing shaders

The governor: shed bubbles, never pixels

When hardware falls behind, many web apps silently shrink the canvas and upscale it into mush. Ooh Bubbles never lowers resolution: it always draws at your screen’s full native pixel ratio. Instead, a pacing governor watches sustained medians (at least a second of frames, counting a frame late past 1.25 budgets) and sheds work in strict priority order:

  1. local soap-flow surface detail on the most prominent bubbles,
  2. post-processing effects, step by step,
  3. off-screen bubbles,
  4. visible bubbles, down to a device-class floor.

Recovery runs in reverse, slowly and gently, so a single OS hiccup never causes oscillating quality. When a frame’s CPU budget cannot afford every nominal 1/240 s physics tick, FrameStepper smoothly coalesces ticks (up to 1/60 s) so motion keeps 1:1 real-world time instead of slipping into slow motion.

Device class Starts with Probes up to Sheds down to
Phone 55 120 45
Tablet 100 180 100
Desktop 60 225 55
Strong desktop GPU 100 250 55

Ceilings tighten automatically on ultra-high-pixel displays: on a 5K desktop monitor, 225 bubbles held 87–98% of frames on time whereas 300 managed 29–88%, setting the desktop cap. Phones are identified by coarse touch pointer plus short screen side rather than GPU feature flags (because iPhone Safari exposes desktop-like features). If a phone still struggles at its floor with effects off, a one-way lite tier keeps full native sharpness while switching to a slim thin-film pipeline and dropping bloom.

Starting fast, and beating Safari’s freezes

WebGPU pipelines must compile before drawing, and full thin-film optics shaders are substantial. To get live bubbles on screen immediately:

Deep dive: Safari compile fencing & cutting iPhone cold start from 25 s to 5.7 s

The Safari freeze: Early tests on a real iPhone ran smoothly at 60 Hz for 11–13 seconds, then collapsed to 0.2 fps with multi-second stalls. Safari blocks the GPU process on every shader compile — even async ones — taking ~4 s per full-optics variant. Every stall tricked the governor into stepping down, which triggered even more compiles. The fix is compile fencing: on serialized-compile browsers, startup compiles a single optics tier paced one compile per presented frame, lower tiers reuse it, and no surprise variants compile mid-play.

Cutting cold start: Uncached cold WebKit previously waited on 2 fast pipelines plus 9 full ones (~25 s on a real iPhone). Splitting startup timing diagnostics (#1398), booting physics before the first-light GPU fence (#1400), and pruning full-optics shaders into one shared general program (#1407) cut cold iPhone reveal from ~25 s to ~5.7 s, while later cold-Metal specialization work (#1939, #1945), per-bubble birth-shader pinning (#1988), and desktop mode-switch compile elimination (#2004) brought real-device iPhone cold reveal down toward ~3.2 s with zero mid-session compile freezes. Opening any page with ?bootReport=1 captures a real-device startup timeline.

Keeping it small: release gates & Brotli budgets

Every production build ends with scripts/check-dist.mjs, a hard release gate that blocks deployment if any cached asset in assets/ lacks a content hash, any worker lacks -coep-, WebAssembly is embedded in JavaScript, or 32 KiB+ of code is duplicated across chunks. Brotli transfer budgets are tracked as warnings so correctness and 60 fps performance always win:

Transfer budget (Brotli) Target limit
Player page initial JS (index.html) 57 KiB
Player page initial JS, CSS & fonts 210 KiB
Engine initial JS (simulation/index.html) 685 KiB
Engine initial transfer 690 KiB
Startup WebAssembly (single selected variant) 410 KiB

These Field Guide pages live completely outside the simulator’s module graph and share the player’s already-cached variable fonts, adding zero bytes to the simulator’s startup path.

How we got here

  1. 22 SepFirst commit: physically based soap bubble simulator ray-traced in WebGL.
  2. 1–2 OctWind and wobble move to Rust/WebAssembly; Cloudflare Pages builds compile Rust from source (#28).
  3. 6–7 OctBatched SIMD kernels, dedicated physics worker, and deterministic thread pool (−17.7% frame time with 4 threads).
  4. 12 OctGovernor fixed to require real late frames (5K on-time frames 51–77% → 100%, #233); GPU thin-film lookup table (−26% frame time, #237).
  5. 13 OctRaw WebGPU & WGSL replace WebGL/Three.js (#279); single startup kernel download (#333); Binaryen optimisation (#352); sustained-median governor (#368).
  6. 15–18 OctNative pixels always (#1029), wand loader & fast-start pipeline (#1121, #1128), Safari compile fencing (#1153), and dedicated phone & lite tiers (#1169, #1185).
  7. 20–22 OctSpray capture by neighbouring bubbles (#1393); iPhone cold start slashed from 25 s to 5.7 s (#1398, #1400, #1407).
  8. Nov 2026Composable Smoke, Fire & Frost physics pillars, Oval Wand Rayleigh–Plateau pinch-off, six CC0 360° photographic HDRI worlds, and lightweight 22-shader WebGL2 fallback.
  9. 30 Nov–1 DecRelaxed-SIMD FMA kernels (#1922), qualified 273–313 K PG/water aerosol chemistry (#1927), GPU-resident smoke deposit (#1897), and cold-Metal TTI / frost GPU / dense-foam breakthroughs (#1939, #1945).
  10. 2 DecFirst-principles physics wave & zero-freeze compiles: per-bubble birth-shader pinning (#1988), desktop mode-switch compile elimination (#2004), full −30 °C T/P thermodynamics, Bergeron–Findeisen hoarfrost, Zoelly–Timoshenko/Pogorelov ice buckling & flame plumes (#1997), calibrated pursed-lip Blow round jet & Weber breakup (#2009), Lorentz–Lorenz glycerol optics & buoyant marginal-regeneration plumes (#2010), direct Thermal Herder playability & contextual cues (#2011), continuous 3D internal smoke swirl & Taylor–Culick burst vortex rings (#2014), speed-of-sound acoustic shockwaves (#2012), and SIMD bubble-to-bubble aerodynamic wakes (#2017).