3D engine that AI agents drive
Measure what an equal feature set costs against three.js
Nguyen Xuan An9794c7288f8e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex fe612ad3..14d5291f 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -2,6 +2,7 @@## Unreleased+- Measure what an equal feature set costs against three.js (part of issue #74; no engine change, no speed-up). `bench/runtime` gets the engines `67js-ldr`, `67js-one-sample`, `67js-ldr-one-sample` and `three-one-sample` (run with `--engine`) and a `--cpu-only` flag. Measured: with no MSAA and no HDR target 67js is still 5.2 to 5.7 times three.js on SwiftShader in `large-mesh`, `many-materials` and `instanced-objects` (7.8, 5.3 and 7.8 with the defaults), so the gap is the lit shader and not those two features. On the RTX 3060 `large-mesh` costs 3.6 ms against 0.6 ms and is not explained by HDR, MSAA, the vertex shader or the vertex attributes; on ANGLE Vulkan the ratio is 2.3 instead of 5.8. Skipping zero-intensity fill lights was tried and reverted (inside the noise). The profile is in `bench/runtime/PROFILE.md`.- Make room in the npm package without adding a dependency (part of issue #36). The archive fell from 802,581 to 695,041 bytes (353,535 free under 1 MiB; the target was 300,000), and every library and MCP install stays as light as before. Two causes: (1) every one of the 80 built files began with the full `LICENSE` and `THIRD-PARTY-NOTICES.md` text, about 4 KB each; they now carry a one-line notice and the complete texts stay once at the package root, about 61,000 bytes saved; (2) the MCP SDK's Node shim pulls in Ajv (the validator for elicitation answers, about 270 KB before minification) that this server never uses, so `build/mcp/server.mjs` now bundles `scripts/mcp-sdk-shims.mjs` instead, which fails with a clear message if elicitation is ever requested: `server.mjs` fell from 677,484 to 484,111 bytes and the archive by about 47,000 bytes. `build/mcp/LICENSES.md` no longer lists the Ajv family because that code is gone, and its unused copies left `licenses/mcp/`. Measured, and not worth doing: `zod/mini` (zod's classic layer is 30 KB of the server before gzip), source maps (none are shipped), standalone core copies (none in the archive), and docs (every packaged page is linked from the README, `llms.txt`, the MCP guide or the skills). `npm run smoke:package` now loads every `exports` entry in a clean consumer. Film and site code were already outside the archive. Separate lazy-loaded packages were not built: the lazy addons together are about 157 KB gzip, so splitting them out could return at most that much, for the cost of a package, a release pipeline and a version matrix each. `archiveBytes` budget 815,000 to 705,500. Module load of the server is no slower (minimum of 25 runs, 217 and 188 ms against 231 and 209 ms).- Run the heavy scene kernels on a worker in the MCP preview (part of issue #38; edits after creation still run on the main thread). The light bake, the cloth solver, `uv_unwrap` and pattern textures are pure functions of their input, so `runKernel` in the new `src/kernel-offload.js` computes them inline as before, or, inside `buildWithKernels`, throws `KernelDeferred`, lets a module worker (`kernel-worker.js`, a package entry like the path tracer's) compute the result, caches it by a hash of the input bytes and runs the synchronous build again. The preview builds `scene_create` that way when the page has `Worker`; Node and every other caller keep the synchronous path and the same bytes (a test builds one scene cold in a separate process both ways and compares a digest of geometry, textures and the bake report). A worker error comes back with its class and message, a worker that cannot start or dies leaves the job to the inline path, and an `AbortSignal` ends the build and the worker. Measured in Node on the demo recipe: the `uv_unwrap` stack takes 3.6 s, the draft light bake 7.5 s and the standard bake passes the 20 s limit on this machine, cloth at the 10 s limit takes 3.4 s, a 1024 pattern takes 1 to 7 s, a boolean 0.12 s. Measured in headless Chrome on the GPU backend with the demo room recipe (`tests/fixtures/demo-room.recipe.json`, 9 kernels): the longest stretch the page was unresponsive during `scene_create` fell from 11.8 and 11.9 s to 1.6 s with workers (two runs each), while the build took 12.3 and 12.5 s instead of 10.9 and 11.0 s, because the kernels run one after another and the build reruns after each. `scene_inspect` now reports `kernels`: how many kernels a worker served and how many ran inline, each fallback with its reason (also a `67js kernel fallback` line on stderr when a page that has `Worker` falls back), the build time and the longest block; `npm run smoke:kernel-workers` installs the package, runs the demo room and fails unless kernels ran on the worker with no fallback. The `longtask` observer saw nothing in either mode because the browser does not count work run by a remote-debugging call, so the block is measured with a 20 ms timer. `scene_apply` edits still run on the main thread. Core did not grow (`entry:core`, `lit-box` unchanged); the lazy entries rose by about 900 bytes gzip.diff --git a/bench/runtime/PROFILE.md b/bench/runtime/PROFILE.mdindex c18d14db..0e3dd835 100644--- a/bench/runtime/PROFILE.md+++ b/bench/runtime/PROFILE.md@@ -61,6 +61,52 @@ Same scenes, `node bench/runtime/run.mjs --scene <id> --engine <name> --frames 8| `many-materials` | 3,704 to 826 | 2,465 to 1,058 | 235 | 10.5 to 4.5 || `large-mesh` | 4,384 to 975 | 2,336 to 1,189 | 231 | 10.1 to 5.1 |+## Equal feature set, and where the frame goes (2026-11-01, no engine change)++Measured on `perf/frame-cost-equal-features`, 960 by 540, Chrome headless. Four new engines in `bench/runtime` turn one feature off at a time and need `--engine <name>`: `67js-ldr` (no HDR target), `67js-one-sample` (no MSAA), `67js-ldr-one-sample` (both), `three-one-sample` (three.js without MSAA). `--cpu-only` runs only SwiftShader.++**SwiftShader, frame in ms, 6 frames each, one run per row** (`base-cpu`, `base-cpu-three-one-sample`):++| Engine | `large-mesh` | `many-materials` | `instanced-objects` |+| --- | --- | --- | --- |+| three.js (4 samples) | 215 | 231 | 48 |+| 67js (HDR, 4 samples) | 1,684 | 1,219 | 376 |+| `67js-ldr` | 1,183 | 1,082 | 292 |+| `67js-one-sample` | 556 | 522 | 227 |+| `67js-ldr-one-sample` | 533 | 490 | 194 |+| three.js (one sample) | 94 | 86 | 37 |++Fair comparison: MSAA costs three.js 2.3, 2.7 and 1.3 times and costs 67js 3.0, 2.3 and 1.7 times, so it is not what separates them. With the same feature set (no MSAA, and no HDR target for 67js) 67js is 5.7, 5.7 and 5.2 times three.js, against 7.8, 5.3 and 7.8 for the defaults. 67js still draws an environment background and a post pass that three.js does not, and those are about 45 ms each on `instanced-objects` (measured above). The equal-feature gap is therefore the cost of the lit shader itself, which agrees with the profile above.++**RTX 3060, frame in ms, 60 frames, one run per row** (`gpu-matrix`, GPU 5 to 40 percent busy):++| Engine | `large-mesh` | `many-materials` | `instanced-objects` |+| --- | --- | --- | --- |+| three.js | 0.56 | 5.67 | 7.25 |+| 67js | 3.82 | 3.48 | 2.06 |+| `67js-ldr-one-sample` | 3.24 | 3.17 | 2.08 |+| three.js (one sample) | 0.56 | 6.89 | 9.91 |++Repeated `large-mesh` 67js runs gave 3.41, 3.58, 3.63, 3.82 and 3.11 to 3.98 across sessions, and three.js 0.61 to 0.63. On the GPU 67js wins `many-materials` and `instanced-objects` in this run (three.js draws 965 and 4,154 objects one by one) and loses `large-mesh` by 6.8 times. `instanced-objects` has no `three-instanced` row here, which beat 67js in the earlier report.++**Why `large-mesh` costs 3.6 ms against 0.6 ms on the GPU.** It is one sphere with 1,046,528 triangles, and 67js draws it in 3 passes (environment background, the mesh, post), which is also true of the RTX run above. Splitting the frame by wrapping the WebGL draw calls (script not kept in the repository):++| What | Frame (ms) |+| --- | --- |+| Whole frame | 3.1 to 3.9 |+| Mesh draw replaced by one triangle (fixed cost of the passes) | 1.3 |+| Mesh drawn with a minimal vertex and fragment shader on the same vertex array and targets | 2.1 |+| Mesh draw with `RASTERIZER_DISCARD` (vertex stage only) | 2.4 to 2.5 |+| Both full-screen passes skipped | 3.2 |+| `hdr` off, `samples` 1, or both | 3.2 to 3.3 (2.7 with HDR off alone) |+| Only 5 of the 15 vertex attributes enabled, or a non-instanced `drawElements` | 3.8 to 3.9 (no change) |+| Vertex shader cut to `gl_Position` only | 2.4 (no change) |+| Same page on ANGLE Vulkan instead of D3D11 | 67js 1.20, three.js 0.52 |++So: the passes around the mesh cost 1.3 ms, three.js's whole frame is 0.6, and about 2 ms is the mesh itself, split between getting 1 million tiny triangles through the pipeline (about 0.8 ms even with trivial shaders) and shading them (about 1.5 ms). Neither HDR nor MSAA nor the vertex shader's contents nor the number of attributes moves it. On Vulkan the ratio falls from 5.8 to 2.3, so more than half of the gap is specific to ANGLE's D3D11 path, and the cause inside it was not found. Almost every one of the 1 million triangles covers less than a pixel, so the lit shader runs on a 2 by 2 quad per triangle; that fits the shading share but was not isolated.++**One change tried and reverted.** Skipping the two fill-light `illuminate` calls when `fillIntensity` is 0 (the benchmark's value). SwiftShader frame, one run each: `large-mesh` 1,684 to 1,338 (this scene swings by 20 percent between runs), `many-materials` 1,219 to 1,233, `instanced-objects` 376 to 383. Inside the noise on two of three scenes, so it was not kept.+## Left- A SwiftShader frame is still 4.5 to 7 times three.js, not 3. What remains is the per-pixel cost of the lit shader with 4 MSAA samples and an HDR target. Ideas that change the cost without changing the picture: a cheaper path for parts that are flat colour with no maps, and sharing one shadow comparison across the 3 by 3 filter. Both need the pictures checked again.diff --git a/bench/runtime/adapters.js b/bench/runtime/adapters.jsindex e1d9806b..677893be 100644--- a/bench/runtime/adapters.js+++ b/bench/runtime/adapters.js@@ -40,7 +40,7 @@ function levelsForScene(scene) {return levels;}-export async function create67Adapter(canvas, scene, characters, { culling = true, lods = true } = {}) {+export async function create67Adapter(canvas, scene, characters, { culling = true, lods = true, hdr = true, samples = 4 } = {}) {const environment = {world: true, fillIntensity: 0, ambientIntensity: 1, fogDensity: 0,lightDirection: scene.sun.direction, keyColor: scene.sun.color,@@ -67,7 +67,7 @@ export async function create67Adapter(canvas, scene, characters, { culling = tru? { eye: scene.sun.direction.map(value => value * 5), target: [0, 0, 0], up: [0, 1, 0], extent: scene.shadowExtent, near: 0.1, far: 150 }: {};const renderer = new Renderer67(canvas, parts, {- ground: false, hdr: true, depth: false, samples: 4, pixelRatio: 1, memoryBudgetMiB: 2048, world: true, environment, shadowBounds, culling,+ ground: false, hdr, depth: false, samples, pixelRatio: 1, memoryBudgetMiB: 2048, world: true, environment, shadowBounds, culling,});renderer.resize();const frame = { eye: scene.camera.eye, target: scene.camera.target, up: [0, 1, 0], fov: scene.camera.fov, near: scene.camera.near, far: scene.camera.far, environment, time: 0, exposure: 1, effects: {} };@@ -154,9 +154,9 @@ function addInstancedMesh(threeScene, scene) {threeScene.add(mesh);}-export async function createThreeAdapter(canvas, scene, characters, { instanced = false } = {}) {+export async function createThreeAdapter(canvas, scene, characters, { instanced = false, antialias = true } = {}) {installSharedToneCurve();- const renderer = new WebGLRenderer({ canvas, antialias: true });+ const renderer = new WebGLRenderer({ canvas, antialias });renderer.setPixelRatio(1);renderer.setSize(canvas.clientWidth, canvas.clientHeight, false);renderer.toneMapping = CustomToneMapping;diff --git a/bench/runtime/harness.js b/bench/runtime/harness.jsindex 4b038827..1ebf5005 100644--- a/bench/runtime/harness.js+++ b/bench/runtime/harness.js@@ -110,6 +110,10 @@ async function createAdapter(engine, canvas, scene, characters) {if (engine === '67js') return create67Adapter(canvas, scene, characters);if (engine === '67js-no-lod') return create67Adapter(canvas, scene, characters, { lods: false });if (engine === '67js-no-culling') return create67Adapter(canvas, scene, characters, { culling: false, lods: false });+ if (engine === '67js-ldr') return create67Adapter(canvas, scene, characters, { hdr: false });+ if (engine === '67js-one-sample') return create67Adapter(canvas, scene, characters, { samples: 1 });+ if (engine === '67js-ldr-one-sample') return create67Adapter(canvas, scene, characters, { hdr: false, samples: 1 });+ if (engine === 'three-one-sample') return createThreeAdapter(canvas, scene, characters, { antialias: false });if (engine === 'three-instanced') return createThreeAdapter(canvas, scene, characters, { instanced: true });return createThreeAdapter(canvas, scene, characters);}diff --git a/bench/runtime/run.mjs b/bench/runtime/run.mjsindex 979dfc9b..a2694c9d 100644--- a/bench/runtime/run.mjs+++ b/bench/runtime/run.mjs@@ -1,5 +1,5 @@// Compares 67js and three.js on matching scenes: npm run benchmark:runtime-// Use --gpu-only to skip SwiftShader. --scene <id>, --engine <name> and --frames <count> run a part, and do not rewrite+// Use --gpu-only to skip SwiftShader, or --cpu-only to run only SwiftShader. --scene <id>, --engine <name> and --frames <count> run a part, and do not rewrite// the report.import { spawnSync } from 'node:child_process';import { mkdir, readFile, writeFile } from 'node:fs/promises';@@ -16,7 +16,9 @@ const BACKENDS = {gpu: { arguments: SHARED_ARGUMENTS, frames: 120 },cpu: { arguments: [...SHARED_ARGUMENTS, '--use-angle=swiftshader', '--enable-unsafe-swiftshader'], frames: 15 },};-const ENGINES = ['67js', 'three', 'three-instanced', '67js-no-lod', '67js-no-culling'];+const ENGINES = ['67js', 'three', 'three-instanced', '67js-no-lod', '67js-no-culling', '67js-ldr', '67js-one-sample', '67js-ldr-one-sample', 'three-one-sample'];+// Ablations of the HDR target and MSAA, which cost something in every scene. They run only when named with --engine.+const FEATURE_ENGINES = new Set(['67js-ldr', '67js-one-sample', '67js-ldr-one-sample', 'three-one-sample']);const INSTANCED_SCENES = new Set(['instanced-objects']);// Ablations of this renderer's own culling and level selection, which only a scene that has them can show.const ABLATION_SCENES = new Set(['large-outdoor']);@@ -24,6 +26,7 @@ const ABLATION_ENGINES = new Set(['67js-no-lod', '67js-no-culling']);const flags = process.argv.slice(2);const gpuOnly = flags.includes('--gpu-only');+const cpuOnly = flags.includes('--cpu-only');const sceneFlag = flags.indexOf('--scene');const onlyScene = sceneFlag >= 0 ? flags[sceneFlag + 1] : null;const engineFlag = flags.indexOf('--engine');@@ -35,6 +38,7 @@ const isPartialRun = Boolean(onlyScene || onlyEngine || framesOverride);function enginesFor(sceneId) {return ENGINES.filter(engine => {if (onlyEngine) return engine === onlyEngine;+ if (FEATURE_ENGINES.has(engine)) return false;if (ABLATION_ENGINES.has(engine)) return ABLATION_SCENES.has(sceneId);return engine !== 'three-instanced' || INSTANCED_SCENES.has(sceneId);});@@ -138,7 +142,7 @@ await server.listen();const report = { resolution: [960, 540], msaaSamples: 4, graphics: {}, results: { gpu: {}, cpu: {} }, scenes: {} };try {const serverUrl = `http://127.0.0.1:${server.httpServer.address().port}`;- await runBackend('gpu', serverUrl, report);+ if (!cpuOnly) await runBackend('gpu', serverUrl, report);if (!gpuOnly) await runBackend('cpu', serverUrl, report);} finally {await server.close();