Skip to main content
Glama
README.md
# webgl-lighthouse

**Give AI coding agents (Claude Code, Cursor, Antigravity) eyes into WebGL VRAM, draw calls, and 4K texture bloat.**

AI agents can write Three.js / React Three Fiber / Babylon.js / Cesium code and even inspect screenshots, yet they are completely blind to GPU state: they have no idea they just allocated 250 MB of VRAM with an uncompressed 4K PNG or broke instancing and jumped from 20 to 500 draw calls.

`webgl-lighthouse` is a zero-code-change WebGL / WebGL2 profiler (built on `greggman/webgl-memory`-style byte accounting injected via Playwright `addInitScript`) packaged as an **MCP Server**, **CLI**, and **GitHub Action**.

![WebGL Lighthouse terminal output on Three.js Littlest Tokyo](docs/demo-terminal.svg)

---

## MCP Server Setup (Cursor / Claude Code / Claude Desktop)

Add to your MCP config (`~/.cursor/mcp.json`, `claude_desktop_config.json`, or Claude Code MCP settings):

```json
{
  "mcpServers": {
    "webgl-lighthouse": {
      "command": "npx",
      "args": ["-y", "-p", "webgl-lighthouse", "webgl-lighthouse-mcp"]
    }
  }
}
```

Or run directly from GitHub before npm install:

```json
{
  "mcpServers": {
    "webgl-lighthouse": {
      "command": "npx",
      "args": ["-y", "-p", "github:damonphilipross/webgl-lighthouse", "webgl-lighthouse-mcp"]
    }
  }
}
```

### Exposed MCP Tools

- **`profile_webgl_scene(url, budget_vram_mb?, budget_draw_calls?, budget_triangles?, budget_bloated_textures?, orbit_camera?)`**
  Profiles any local dev server (`http://localhost:5173`) or live URL with zero changes to your app. Returns a Markdown summary + structured JSON with:
  - **Estimated GPU VRAM (MB)** split by textures (per-level `generateMipmap` chains + `compressedTexImage2D` KTX2/Basis byte sizes), buffers, and MSAA renderbuffers.
  - **Uncompressed Texture Bloat Detector:** Flags non-compressed 8-bit color textures `> 1024px` (ignores depth/float render targets) with estimated KTX2/Basis VRAM savings and source URLs when available.
  - **Per-frame draw metrics:** Median & peak draw calls, instanced draws (`drawArraysInstanced`, `drawElementsInstanced`, `ANGLE_instanced_arrays`), triangles, vertices, and median / 1% low FPS.
  - **Shader compile stalls:** `compileShader`, `linkProgram`, and synchronous `COMPILE_STATUS` / `LINK_STATUS` stall time in ms.
- **`compare_webgl_scenes(baseline_url, candidate_url)`**
  Profiles two URLs and returns `before -> after` VRAM, draw-call, triangle, shader-stall, and newly introduced bloated texture deltas so the agent can verify that its optimization actually worked.

---

## Real-World Output (Zero App Code Changes)

| Engine / Public Demo | Total VRAM | Draw Calls / Frame | Triangles / Frame | Notable Finding |
| --- | ---: | ---: | ---: | --- |
| Three.js Littlest Tokyo (`webgl_animation_keyframes.html`) | 117.65 MB | 79 | 142,381 | Caught 1x `4096x4096 SRGB8_ALPHA8 (+mips)` = **85.33 MB** |
| Three.js Car Configurator (`webgl_materials_car.html`) | 82.33 MB | 103 | 708,626 | 45.70 MB renderbuffers, 22.21 MB textures |
| CesiumJS Sandcastle (`sandcastle.cesium.com/standalone.html`) | 103.89 MB | 13 | 134,514 | 43 textures (74.71 MB), 39.0 ms sync shader stall |
| Babylon.js Playground (`playground.babylonjs.com/`) | 0.10 MB | 2 | 4,626 | 59.9 FPS median, 2.7 ms sync shader stall |

---

## CLI Usage

```bash
# Profile a local dev server or public URL with budgets (exits non-zero on violation)
npx webgl-lighthouse http://localhost:5173 --budget-vram 150 --budget-calls 150

# Compare a PR preview against a baseline URL
npx webgl-lighthouse https://pr-42.preview.example.com \
  --compare https://prod.example.com \
  --budget-vram 150 \
  --markdown
```

<details>
<summary>All CLI Flags</summary>

| Flag | Description |
| --- | --- |
| `--budget-vram <mb>` | Exit non-zero if total estimated GPU VRAM exceeds `<mb>` |
| `--budget-calls <n>` | Exit non-zero if median draw calls/frame exceeds `<n>` |
| `--budget-tris <n>` | Exit non-zero if median triangles/frame exceeds `<n>` |
| `--budget-shader-ms <ms>` | Exit non-zero if shader compile/link stall exceeds `<ms>` |
| `--budget-bloated <n>` | Exit non-zero if uncompressed color textures >1024px exceeds `<n>` |
| `--budget-fps <fps>` | Exit non-zero if median FPS drops below `<fps>` |
| `--compare <url>` | Profile baseline URL and output PR delta table |
| `--orbit` | Perform a synthetic mouse orbit drag on the canvas |
| `--settle <ms>` | Wait after load before sampling (default: `2500`) |
| `--sample <ms>` | Steady-state sampling window (default: `2000`) |
| `--json` / `--markdown` | Output JSON or GitHub PR Markdown |

</details>

---

## GitHub Action

Runs on standard Ubuntu runners or GitHub's native NVIDIA T4 GPU runners and posts a sticky PR comment with `before -> after` deltas:

```yaml
name: WebGL Budget
on: [pull_request]

jobs:
  webgl-lighthouse:
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
    steps:
      - uses: actions/checkout@v4
      - uses: damonphilipross/webgl-lighthouse@main
        with:
          url: ${{ env.PREVIEW_URL }}
          baseline-url: https://prod.example.com
          budget-vram: 150
          budget-calls: 150
          budget-bloated: 0
```

---

## Experimental: 2 KB Production RUM Prototype (`rum/webgl-rum.ts`)

Also includes an experimental ~2 KB browser beacon (`rum/webgl-rum.ts`) for capturing real-user `UNMASKED_RENDERER_WEBGL`, 1% low FPS, `webglcontextlost`, live texture VRAM, and suspected iOS Safari Jetsam OOM tab kills (heartbeat without clean `pagehide`).

---

## Credits & License

Byte-accounting formulas and WebGL2 prototype-wrapping approach are inspired by Gregg Tavares's [`webgl-memory`](https://github.com/greggman/webgl-memory).

MIT License.