The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.
Rust electron-alternativegame-engineglobal-illuminationgraphicsjavascript-runtimemetalpath-tracingray-tracingreal-time-renderingrenderingrustthreejstypescriptvoxelvulkanwebgpuwgpu
// Architecture, dependency-isolation, and binary-size gate for ThreeRT `--renderer webgpu`.
// node tools/check-webgpu-gate.ts [--build] [--render]
//
// Enforces docs/RENDERING-METHODS.md:
// 1. One-way JS layering: `runtime/js/` never imports from `webgpu/` or `renderer/`.
// 2. One-way Rust crate graph: `webgpu/` (`threert-webgpu` and `threert-webgpu-sample`) links
// zero path-tracer, denoiser, voxel, physics, QuickJS, or audio crates.
// 3. Release stripped binary size budget for `threert-webgpu-sample` (macOS).
// 4. Optional headless Metal smoke render (`--render`): renders the three.js r187 WebGPURenderer
// sample scene and verifies a non-blank PNG is produced.
import { spawnSync } from "node:child_process";
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
import { join } from "node:path";
import { PNG } from "pngjs";
const root = join(import.meta.dirname, "..");
// Crates that MUST NEVER appear in the `webgpu/` dependency graph.
const FORBIDDEN_CRATES = [
"threert-render",
"threert-path",
"voxel-core",
"physics-core",
"rapier3d",
"oidn",
"dlss",
"rquickjs",
"rquickjs-core",
"rquickjs-sys",
"symphonia",
"cpal",
"ureq",
"rustls",
"wesl",
"gltf",
"bevy_mikktspace",
];
// Budget for the release, fat-LTO, stripped `threert-webgpu-sample` native binary.
// - macOS uses system CoreGraphics/CoreText/ImageIO (baseline 2.98 MiB → budget 4.50 MiB).
// - Linux bundles pure-Rust tiny-skia + cosmic-text + image (budget 8.50 MiB; still zero path-tracer).
const MAX_SAMPLE_BYTES = process.platform === "darwin"
? Math.round(4.5 * 1024 * 1024)
: Math.round(8.5 * 1024 * 1024);
function walkTs(dir: string): string[] {
const out: string[] = [];
for (const ent of readdirSync(dir, { withFileTypes: true })) {
const p = join(dir, ent.name);
if (ent.isDirectory()) out.push(...walkTs(p));
else if (ent.isFile() && ent.name.endsWith(".ts")) out.push(p);
}
return out;
}
function checkJsLayering() {
const runtimeJs = join(root, "runtime", "js");
const files = walkTs(runtimeJs);
const violations: string[] = [];
const importRe = /(?:from|import)\s*["']([^"']+)["']/g;
for (const f of files) {
const src = readFileSync(f, "utf8");
for (const m of src.matchAll(importRe)) {
const spec = m[1];
if (spec.includes("/webgpu/") || spec.includes("/renderer/")) {
violations.push(`${f.slice(root.length + 1)}: forbidden import "${spec}"`);
}
}
}
if (violations.length > 0) {
throw new Error(`runtime/js layering violations:\n ${violations.join("\n ")}`);
}
console.log(`[gate] JS layering OK (${files.length} files in runtime/js; zero webgpu/renderer imports)`);
}
function checkCargoTreeIsolation() {
const res = spawnSync("cargo", ["tree", "--locked", "--workspace", "--prefix", "none", "--format", "{p}"], {
cwd: join(root, "webgpu"),
encoding: "utf8",
});
if (res.status !== 0) {
throw new Error(`cargo tree failed:\n${res.stderr || res.stdout}`);
}
const crates = new Set<string>();
for (const line of res.stdout.split(/\r?\n/)) {
const name = line.trim().split(/\s+/)[0];
if (name) crates.add(name);
}
const found = FORBIDDEN_CRATES.filter((c) => crates.has(c));
if (found.length > 0) {
throw new Error(`forbidden path-tracer/heavy crates in webgpu/ dependency graph: ${found.join(", ")}`);
}
console.log(`[gate] cargo tree isolation OK (${crates.size} unique crates; 0 forbidden path-tracer crates)`);
}
function checkBinarySize() {
const bin = join(root, "webgpu", "target", "release", "threert-webgpu-sample");
if (!existsSync(bin)) {
throw new Error(`release binary missing: ${bin} (run \`node tools/build.ts --renderer webgpu\` first)`);
}
const size = statSync(bin).size;
const mib = (size / (1024 * 1024)).toFixed(2);
const budgetMib = (MAX_SAMPLE_BYTES / (1024 * 1024)).toFixed(2);
if (size > MAX_SAMPLE_BYTES) {
throw new Error(`threert-webgpu-sample is ${mib} MiB, over budget ${budgetMib} MiB`);
}
console.log(`[gate] binary size OK: threert-webgpu-sample = ${mib} MiB (budget <= ${budgetMib} MiB)`);
}
function checkHeadlessRender() {
const bin = join(root, "webgpu", "target", "release", "threert-webgpu-sample");
const rt = join(root, "out", "webgpu", "runtime.js");
const script = join(root, "out", "webgpu", "sample.js");
const shot = join(root, "out", "webgpu", "sample.png");
const res = spawnSync(bin, ["--runtime", rt, "--script", script, "--size", "640x480@1", "--shot", shot], {
cwd: root,
encoding: "utf8",
});
if (res.status === 2 && (res.stderr || "").includes("GPU unavailable")) {
console.log(`[gate] headless render skipped (no GPU adapter on runner)`);
return;
}
if (res.status !== 0) {
throw new Error(`threert-webgpu-sample failed (exit ${res.status}):\n${res.stderr || res.stdout}`);
}
if (!existsSync(shot)) {
throw new Error(`expected screenshot not written: ${shot}`);
}
const png = PNG.sync.read(readFileSync(shot));
if (png.width !== 640 || png.height !== 480) {
throw new Error(`unexpected PNG dimensions: ${png.width}x${png.height} (expected 640x480)`);
}
// Verify the rendered frame is not blank / flat single color
let minLum = 255;
let maxLum = 0;
let nonBg = 0;
for (let i = 0; i < png.data.length; i += 4) {
const r = png.data[i], g = png.data[i + 1], b = png.data[i + 2];
const lum = (r * 77 + g * 150 + b * 29) >> 8;
if (lum < minLum) minLum = lum;
if (lum > maxLum) maxLum = lum;
if (Math.abs(r - 0x10) + Math.abs(g - 0x14) + Math.abs(b - 0x1d) > 20) nonBg++;
}
const coverage = (nonBg / (png.width * png.height)) * 100;
if (maxLum - minLum < 40 || coverage < 5) {
throw new Error(`rendered frame looks blank/flat: lum=[${minLum}, ${maxLum}], coverage=${coverage.toFixed(1)}%`);
}
console.log(
`[gate] headless Metal render OK: ${shot} (${png.width}x${png.height}, lum=[${minLum},${maxLum}], coverage=${coverage.toFixed(1)}%)`
);
}
const args = new Set(process.argv.slice(2));
if (args.has("--build")) {
const res = spawnSync("node", [join(root, "tools", "build.ts"), "--renderer", "webgpu"], { cwd: root, stdio: "inherit" });
if (res.status !== 0) process.exit(res.status ?? 1);
}
checkJsLayering();
if (process.platform === "darwin" || process.platform === "linux") {
checkCargoTreeIsolation();
checkBinarySize();
if (args.has("--render")) {
checkHeadlessRender();
}
} else {
console.log(`[gate] unsupported native platform ${process.platform}: skipping cargo tree / binary size checks`);
}