Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
physics: ${{ steps.paths.outputs.physics }}
wasm: ${{ steps.paths.outputs.wasm }}
web: ${{ steps.paths.outputs.web }}
webgpu: ${{ steps.paths.outputs.webgpu }}
steps:
# Each job's inputs, as patterns on the paths a PR changes (the old and the new path of a rename). A change to the
# workflows, a PR of 3,000 files or more (the most the API lists) and any event other than a PR run every job.
Expand Down Expand Up @@ -68,6 +69,8 @@ jobs:
# The browser module (renderer/web, on the engine's library) and tools/web-engine.ts: the module in Node with
# the physics hello world's bundle (games/, packages/, the bundler's and the build's tools, the npm manifests).
job web '^(renderer/|presets/|games/|packages/|tools/(web-engine\.ts|bundle-game\.ts|lib/)|package(-lock)?\.json$|rust-toolchain\.toml$)'
# The native WebGPU renderer (webgpu/), shared runtime (runtime/), build CLI, and size/isolation gate.
job webgpu '^(runtime/|webgpu/|tools/(build\.ts|check-webgpu-gate\.ts)|package(-lock)?\.json$|rust-toolchain\.toml$)'

ts:
name: "ts: typecheck, tests"
Expand Down Expand Up @@ -214,9 +217,56 @@ jobs:
# the game bundle the native runtime loads, the physics hello world's script and its checks (tools/web-engine.ts).
- run: node tools/web-engine.ts

webgpu:
name: "webgpu (macOS): runtime, release build, size & isolation gate"
needs: changes
if: needs.changes.outputs.webgpu == 'true'
runs-on: macos-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 24
cache: npm
- run: npm ci
- uses: ./.github/actions/cargo-cache
with:
workspaces: |
runtime
webgpu
- run: cd runtime && cargo test --release --locked
- run: cd webgpu && cargo test --release --locked
- run: node tools/build.ts --renderer webgpu
- run: node tools/check-webgpu-gate.ts --render

webgpu-linux:
name: "webgpu (Linux): runtime, release build, size & isolation gate"
needs: changes
if: needs.changes.outputs.webgpu == 'true'
runs-on: ubuntu-26.04
timeout-minutes: 30
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: 24
cache: npm
- run: sudo apt-get update && sudo apt-get install -y --no-install-recommends libjavascriptcoregtk-4.1-dev pkg-config libssl-dev
- run: npm ci
- uses: ./.github/actions/cargo-cache
with:
workspaces: |
runtime
webgpu
- run: cd runtime && cargo test --release --locked
- run: cd webgpu && cargo test --release --locked
- run: node tools/build.ts --renderer webgpu
- run: node tools/check-webgpu-gate.ts --render

# The check to read before merging (`gh pr checks <n>`: `threert`).
threert:
needs: [changes, ts, renderer, voxel, physics, wasm, web]
needs: [changes, ts, renderer, voxel, physics, wasm, web, webgpu, webgpu-linux]
if: ${{ !cancelled() }}
runs-on: ubuntu-26.04
timeout-minutes: 5
Expand Down
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

<h1 align="center">ThreeRT</h1>

<p align="center"><b>Hardware ray-traced three.js.</b></p>
<p align="center"><b>The native runtime for three.js — hardware path tracing at 60 fps, or a clean WebGPU app.</b></p>

<p align="center">
A real-time path tracer behind a three.js-style API. Write the scene you would write in three.js,<br>
and every light in it is traced on the GPU's ray-tracing hardware, at 60 fps.
One engine, two renderers: <b><code>path</code></b> traces every light on ray-tracing hardware at 60 fps,<br>
and <b><code>webgpu</code></b> (macOS &amp; Linux, <a href="docs/RENDERING-METHODS.md">architecture</a>) runs standard three.js natively with zero path-tracer code.
</p>

<p align="center">
Expand Down Expand Up @@ -45,7 +45,18 @@

- **Ray tracing.** Every light is traced: shadows, bounce light, reflections, and glowing surfaces that light the room. No shadow maps, no light probes, no lightmaps, no baking.
- **Real time.** 60 fps in production scenes with the camera moving: Amazon Lumberyard Bistro and Beeple's Zero-Day, on a laptop. Not a still that converges once you stop.
- **Runtime.** A game runtime, not only a renderer. Scene code and game logic run inside the engine, and the same file runs as plain three.js in any browser.
- **Runtime.** A native runtime for three.js, not only a renderer. Scene code and game logic run inside the engine: choose the `path` renderer for hardware ray tracing, the `webgpu` renderer (`node tools/build.ts --renderer webgpu`) for a clean native app with zero ray-tracing overhead, or run as plain three.js in any browser.

## Renderers: `path` and `webgpu`

ThreeRT stays one engine and one runtime with two renderers selected at build time (`node tools/build.ts --renderer path` or `--renderer webgpu`). They share the native runtime (`runtime/`) with one-way dependencies (`threert-path → threert-runtime ← threert-webgpu`), enforced by CI (`node tools/check-webgpu-gate.ts`) so a `webgpu` build links no ray-tracing code at all.

| | `path` | `webgpu` ([`docs/RENDERING-METHODS.md`](docs/RENDERING-METHODS.md)) |
|---|---|---|
| **Renderer** | Rust + wgpu compute path tracer (hardware ray queries, OIDN, DLSS / MetalFX) | Standard three.js `WebGPURenderer` over native `navigator.gpu` (wgpu) |
| **Scene API** | `@threert/scene` (physical lighting units) | Plain `three.js` + TSL node materials (+ PocketJS UI) |
| **GPU** | Ray-tracing hardware (Apple M-series, RTX, RDNA2+) | Any GPU wgpu supports (Metal, Vulkan, D3D12) |
| **Footprint** | Full ThreeRT clients | Proven at **9.9 MB installed / 4.5 MB download** (vs 445 / 170 MB Electron) |

## Hello world looks right

Expand Down Expand Up @@ -194,6 +205,7 @@ npm run test:render # the r
## Documentation

- [Getting started](https://threert.dev/docs/): the client, a first scene, the dev loop, packages
- [`docs/RENDERING-METHODS.md`](docs/RENDERING-METHODS.md): one runtime, two renderers (`path` and `webgpu`), crate layout and size-gate rules
- [`docs/RUNTIME.md`](docs/RUNTIME.md): how scene code runs in the engine: setup, handlers, world handlers, frame scripts, physics
- [`docs/MATERIAL_FUNCTIONS.md`](docs/MATERIAL_FUNCTIONS.md): a WGSL body on a material, run by the path tracer at every hit
- [`docs/VOXEL.md`](docs/VOXEL.md): the voxel engine, measured on a destructible level
Expand Down
103 changes: 103 additions & 0 deletions docs/RENDERING-METHODS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Renderers: Path Tracer (`path`) and WebGPU (`webgpu`)

> Status: active (2026-11-28). Both the `path` renderer (`renderer/`) and the `webgpu` renderer
> (`runtime/` + `webgpu/`, built via `node tools/build.ts --renderer webgpu`, gated by
> `node tools/check-webgpu-gate.ts`) live in this repository on **macOS (Metal)** and **Linux
> (Vulkan + WebKitGTK JavaScriptCore + pure-Rust `tiny-skia`/`cosmic-text`/`image`)**. `webgpu`
> runs three.js r187's `WebGPURenderer` natively on PocketJS's JavaScriptCore guest
> (`pocket-nexus/pocketjs#773`, `#774`, `#775`), originally proven in
> `pocket-nexus/pocket-agents-island#28`, `#29`. Windows support remains planned.

ThreeRT is one engine and one runtime with **two renderers**, chosen per project and per build:

| | **`path`** (Path Tracer) | **`webgpu`** (WebGPU — macOS & Linux) |
|---|---|---|
| What draws | ThreeRT's wgpu compute path tracer: hardware ray queries, OIDN, DLSS / MetalFX, the physical sky and post chain | three.js r187's own `WebGPURenderer`, running natively: `navigator.gpu` implemented over wgpu inside the engine's JavaScript guest |
| Scene code | `@threert/scene` (physical units) | plain `three.js` + TSL node materials |
| GPU | ray-tracing hardware (Apple M-series, RTX, RDNA2+) | any GPU wgpu supports (Metal, Vulkan, D3D12) |
| Interface | the game HUD (`docs/RUNTIME.md` "HUD") | PocketJS (Solid JSX + Tailwind) drawn by PocketJS's wgpu renderer over the 3D frame |
| Footprint | the ThreeRT clients | 9.9 MB installed, 4.5 MB to download (Pocket Island desktop app, measured below) |
| For | production scenes lit by traced light at 60 fps | native three.js apps and games that ship small and run on any GPU, with zero ray-tracing code |

Following Godot (Forward+ / Mobile / Compatibility) and Blender (Cycles / EEVEE), ThreeRT stays a
single product with one brand (**ThreeRT**: Three + **R**ay **T**racing, **R**eal **T**ime,
**R**un**t**ime). There is no "ThreeRT Lite" or split brand — choosing `--renderer webgpu` simply
builds the clean native runtime with the WebGPU renderer and links zero path-tracer code.

## Why a `webgpu` renderer

ThreeRT's runtime already does what a small native 3D app needs besides ray tracing: a JavaScript
guest, window and frame pacing, input, audio, networking, assets and UI. Adding the `webgpu`
renderer completes the native three.js story — standard three.js scenes running natively on any GPU
at a fraction of a browser's size and power:

- **Measured on Pocket Island** (`pocket-nexus/pocket-agents-island#28`, `apps/desktop/REPORT.md`;
Apple M5, 1200 × 800 @ 2×, foreground 60 fps, Agent Host running, whole process tree):
- **CPU (one core):** 34.7 % vs Electron's 64.8 %
- **Foreground RSS / footprint:** 471 MB / 533 MB vs Electron's 1007 MB / 932 MB
- **Window closed RSS / footprint:** 81 MB / 16 MB vs Electron's 433 MB / 104 MB
- **Installed / download:** 9.9 MB / 4.5 MB vs Electron's 445 MB / 170 MB
- **Visual parity:** the native WebGPU binding renders three.js test scenes identically to Chrome's
WebGPU (mean absolute difference 0.000 / 255).
- A three.js developer can ship a native desktop build without Electron and without rewriting their
scene for `@threert/scene`, while keeping the same ThreeRT runtime and path to hardware ray tracing
when a project wants `path`.

## Code layout

```text
renderer/ threert-render (path tracer crate; unchanged API)
runtime/ runtime/crates/* threert-jsc, threert-rt, threert-canvas2d,
threert-net, threert-pocket-host
runtime/js/ web-platform globals (events, timers, window,
Canvas 2D, net; never imports webgpu/ or renderer/)
webgpu/ threert-webgpu WebGPU (`navigator.gpu`) over wgpu for JSC,
window canvas + headless Offscreen (`webgpu-shot`)
webgpu/js/ JS WebGPU binding + runtime entrypoint
webgpu/sample/ threert-webgpu-sample (minimal three.js r187 + TSL)
```

**Dependencies point one way: `threert-path → threert-runtime ← threert-webgpu`.**
- A `webgpu` build **never links path-tracer code**: no ray-query kernels, OIDN, DLSS, MetalFX, sky
baker, or voxel traversal.
- A `path` build never links the WebGPU guest binding.
- A build selects one renderer:

```bash
node tools/build.ts --renderer webgpu # clean native three.js app: runtime + webgpu (zero path-tracer code)
node tools/build.ts --renderer path # hardware ray tracing (default): runtime + path tracer
node tools/check-webgpu-gate.ts --render # cargo tree isolation, JS layering, binary size & headless Metal check
```

**Size and dependency gates hold `webgpu` clean.** CI builds the `webgpu` sample app (release, LTO,
stripped) and checks both binary size against its budget (Pocket Island's size at extraction plus an
explicit margin) and `cargo tree` to verify no `threert-path` / denoiser / upscaler crates enter the
`webgpu` dependency graph. `path` builds have no size gate.

**`path` users see no change.** `threert-render`, `@threert/scene`, `@threert/gameplay`, the clients,
the `dlss` / `oidn` / `net` / `audio` features, and the default build stay as they are.

## What moves from `pocket-agents-island`

| `apps/desktop` today | ThreeRT |
|---|---|
| `crates/webgpu`, `runtime/webgpu.ts` (`navigator.gpu` over wgpu) | `webgpu/` (`threert-webgpu`) |
| `crates/canvas2d` (Canvas 2D, images, fonts on CoreGraphics / CoreText) | `webgpu/` (textures) and `runtime/` (UI text) |
| `crates/pocket-host` (PocketJS runtime + JavaScriptCore guest + wgpu display + native text) | `runtime/` (`threert-runtime`) |
| `crates/net`, `crates/rt`, `runtime/pocket.ts` (timers, fetch, WebSocket, storage, audio) | `runtime/` (`threert-runtime`) |
| `crates/app` window, frame pacing, shell | `runtime/` (window, pacing); app-specific shell stays with the app |
| `crates/agent-host`, Pocket Island's game and UI | stay in `pocket-agents-island` |

The JavaScriptCore guest and native text path live in PocketJS (`pocket-guest-jsc`,
`pocket-wgpu-render`'s `TextPainter`); ThreeRT depends on them rather than copying them.

## Open questions

- **The guest on Windows and Linux.** three.js's `WebGPURenderer` does per-frame work in JavaScript;
QuickJS runs it several times slower than a JIT. Windows and Linux need a JIT engine (V8 or
JavaScriptCore from WebKitGTK) before `webgpu` ships there.
- **TSL coverage.** `webgpu` runs three.js `WebGPURenderer` as written; materials that rely on
`onBeforeCompile` or GLSL `ShaderMaterial` need TSL equivalents (Pocket Island's port is the worked
example).
- **One scene, both renderers.** Whether `@threert/scene` scenes should also target `webgpu` (its
three.js fallback already runs in browsers) or `webgpu` stays plain three.js.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
"packages/*"
],
"scripts": {
"typecheck": "tsc -p packages/scene && tsc -p packages/gameplay && tsc -p packages/voxel && tsc -p games && tsc -p tools && tsc -p tests && tsc -p renderer/web/www",
"typecheck": "tsc -p packages/scene && tsc -p packages/gameplay && tsc -p packages/voxel && tsc -p games && tsc -p tools && tsc -p tests && tsc -p renderer/web/www && tsc -p runtime/js && tsc -p webgpu/js && tsc -p webgpu/sample",
"test": "node --test packages/*/test/*.test.ts tools/*/test/*.test.ts site/*.test.ts",
"build": "node tools/build.ts",
"check:webgpu": "node tools/check-webgpu-gate.ts",
"scenes": "node tools/build-scenes.ts",
"test:render": "node tools/test-render.ts",
"test:layer-b": "node tools/test-layer-b.ts",
Expand Down
Loading
Loading