Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Record the SwiftShader frame split and the kept changes; move size budgets for the program variants (Part of #74)

Nguyen Xuan An543d22f4a597
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4d60c658..30072872 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
## Unreleased
+- Cut the SwiftShader frame by compiling out what a frame does not use (part of issue #74, which stays open). Measured split of `instanced-objects`: the lit shader is about 190 ms of 380, the post pass 65, the MSAA resolve 35, the environment background 25 to 30. SwiftShader runs both sides of a choice such as `environmentEnabled ? a : b` and pays for code behind a uniform that is off, so the main program now takes `ENVIRONMENT_ENABLED`, `USES_SHEEN` and `USES_CLEARCOAT` from the parts and the environment map (features `environmentMap`, `studioLighting`, `sheen`, `clearcoat`); the post program is built per set of steps in use (`createPostFragment`: transparent, occlusion, bloom, volume, explosion, finish) and a new variant is built in the background while the full program keeps drawing; the main vertex program takes `USES_SKIN`, `USES_MORPH`, `USES_WIND`, `USES_WAVES` and `USES_EXPLOSION` (`createVertexShader`); and the colour clear is skipped when the environment background covers the frame. No picture changes: all 8 benchmark scenes are pixel for pixel identical. Alternated medians of 3, before to after: `large-mesh` 1139 to 887 ms, `many-materials` 983 to 880 ms, `instanced-objects` 384 to 268 ms. See `bench/runtime/PROFILE.md`.
- Fix the automatic floor skipping a lone flat object (part of issue #53). A lone part never counts as the recipe's own floor, so a gear lying flat, which spans its whole scene and is the lowest geometry, now gets a floor and a contact shadow (before, `agentGroundPlacement` returned null for it). The surface judge on the six cold recipes goes from 0 before the floor, to 6 with the first rule, to 7 with this fix (gear 0 to 1; one call per task, so direction only). A flat object with a hub on it is still read as a plate. Core did not change.
- Try two exact-picture speed-ups for the SwiftShader frame and keep neither (part of issue #74, which stays open; no engine change). Drawing the environment background after the opaque pass on the far plane, and fetching the post pass's extra FXAA taps only on edges, both moved `instanced-objects` inside the noise (medians 379 to 377 ms and 388 to 386 ms over three alternated pairs). Current ratios against three.js on SwiftShader: `large-mesh` 6.0, `many-materials` 5.0, `instanced-objects` 7.0, so the 3 times target is not met. `bench/runtime/PROFILE.md` records why: a shader that returns the base colour still costs about 4 times three.js, so the rest needs MSAA, the HDR target or the shadow filter to change, which change the picture.
diff --git a/bench/runtime/PROFILE.md b/bench/runtime/PROFILE.md
index 973c35e9..2e1d59bb 100644
--- a/bench/runtime/PROFILE.md
+++ b/bench/runtime/PROFILE.md
@@ -132,3 +132,47 @@ Both medians are inside the noise, so neither was committed and the reference im
- 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.
- The environment BRDF lookup table is the same for every map. It could be computed once per page, or on the CPU, which removes one program and one pass.
+
+## Where the 130 ms went, and what was compiled out (2026-11-01, branch `perf/swiftshader-floor`)
+
+Method: a wrapped WebGL context in a Playwright script (not kept in the repository) rewrote shader sources and skipped draw calls, `instanced-objects` on SwiftShader, 6 frames, medians of 2 to 3 rounds, below normal priority, other sessions on the machine (the RTX 3060 was 78 to 92 percent busy before runs; CPU load not measured).
+
+**The split** (before any change, frame 380 ms; each row is the frame with that part removed or replaced):
+
+| Part | Frame (ms) | So it costs |
+| --- | --- | --- |
+| Mesh draw skipped (clear, background, resolve, post remain) | 141 | mesh 240, rest 141 |
+| Mesh drawn with `RASTERIZER_DISCARD` (vertex stage only) | 153 | vertex stage 12 |
+| Mesh with a fragment shader that writes a constant | 175 | trivial shading 35, lit shading 205 |
+| Post pass skipped | 317 | post 63 |
+| Post fragment shader replaced by one texture copy | 322 | the shader, not the pass: 60 of the 63 |
+| MSAA resolve skipped | 348 | resolve 32 |
+| Environment background skipped | 363 to 373 | 10 to 20 |
+
+So the roughly 130 ms was the fixed part around the mesh (post 63, resolve 32, background 10 to 25, rest) plus a lit shader that costs 205 ms with 4 samples against three.js's whole frame of about 50. Hypotheses: (1) no `discard` or `gl_FragDepth` executes (`USES_ALPHA_MODE` is the constant `false`); (2) the opaque pass is not sorted but `instanced-objects` is one batch, and the trivial-shader row (35 ms) shows overdraw is not the cost; (3) varyings were not changed, the vertex stage is 12 ms here; (4) blend stayed off in every opaque draw (counted) and there is one colour attachment; backbuffer `antialias:false` changed nothing (377 against 380); (5) vertex stage is small on this scene but large on the other two, below; (6) R11F_G11F_B10F, see the last paragraph; (7) nothing is re-created per frame.
+
+**What it is.** SwiftShader pays for code that a uniform switches off, and for both sides of `cond ? a : b`. Replacing the uniform with a constant, one at a time (frame 384 ms): `materialSheen` and `materialCoat` tests 337 to 345, `environmentEnabled` 340, all frame constants together 294 to 315; the point-light count, fog, fill and world mode each moved it by under 3 percent. The post pass behaves the same way: removing the volume step saves 15, FXAA 12, explosion 5, with the rest of its code unused.
+
+**Kept changes** (each pictures identical, 8 scenes, SwiftShader, compared pixel by pixel against renders of the merge base: max difference 0). Alternated A, B, three pairs, medians:
+
+| Change | Scene | Before (ms) | After (ms) |
+| --- | --- | --- | --- |
+| Environment path, sheen and clear coat compiled out of the main fragment program unless a part or the map needs them | `instanced-objects` | 384, 385, 384 | 325, 341, 335 |
+| Post program per set of steps in use (full program draws until the variant is built) | `instanced-objects` | 324, 334, 332 | 295, 301, 279 |
+| Vertex program without skin, morph, wind, waves, explosion unless a part needs them, and no colour clear under an environment background | `large-mesh` | 1,119, 1,139, 1,159 | 922, 887 (one run lost) |
+| same | `many-materials` | 983, 981, 1,007 | 892, 874, 880 |
+| same | `instanced-objects` | 290, 289, 291 | 268, 280, 267 |
+
+The vertex split on `large-mesh` (1,070 ms; the vertex stage alone is 360 ms there): skin and morph calls cost 100, box position 25, water and explosion branches 25, wind and the inverse-transpose normal matrix under 5 each, everything together 170.
+
+**Ratio now** (SwiftShader frame, 67js and three.js alternated, 3 runs; `instanced-objects` lost one run each, so 2):
+
+| Scene | 67js (ms) | three.js (ms) | Ratio (medians) | Was |
+| --- | --- | --- | --- | --- |
+| `large-mesh` | 901, 888, 909 | 225, 234, 239 | 3.85 | 6.0 |
+| `many-materials` | 1,290 (load spike), 898, 899 | 231, 243, 331 | 3.69 | 5.0 |
+| `instanced-objects` | 260, 263 | 90 (load spike), 51 | 5.1 | 7.0 |
+
+**3 times is not met.** What is left, by size, on `instanced-objects` (frame about 265): the lit shader about 160 over a constant shader (shadow filter, environment lookups, three lights, runtime-uniform fill and fog), the resolve 35, the background 25, the post pass 30. Items that need a picture or format decision by An: (a) `R11F_G11F_B10F` for the 4 sample target and the frame texture, without alpha, only for an opaque canvas. Measured: `instanced-objects` 338 to 271 (20 percent), but the pictures are not identical: mean absolute difference 0.13 to 0.59 levels per scene, maximum 6 to 53 levels, and the `transparency` scene has 32,689 pixels more than 2 levels away. It was not kept; `npm run reference` was not run on it. (b) Fewer shadow taps, or no MSAA (about 40 percent), or an LDR target (about 20 percent), as before. Cheap exact ones left: `boxPosition` only when a box-mapped material exists (about 25 ms on `large-mesh`), starting the environment background program with the others.
+
+Tests: `npm test` 1,292 tests, 1,283 pass; the 8 failures all need the optional `manifold-3d`, which this checkout does not have (mesh boolean, bracket recipe, blender-golden, other-clients, agent-asset-mesh, two suite reference tests). `npm run size` passes after `--update-baseline` and `--set-budgets`, because the program variants add about 2.4 to 2.8 KB gzip to the entries that include the renderer.
diff --git a/bench/size/baseline.json b/bench/size/baseline.json
index fc0fb879..b661358e 100644
--- a/bench/size/baseline.json
+++ b/bench/size/baseline.json
@@ -14,9 +14,9 @@
},
"bundles": {
"entry:core": {
- "min": 289090,
- "gzip": 82703,
- "brotli": 70463
+ "min": 291778,
+ "gzip": 83778,
+ "brotli": 71349
},
"entry:model": {
"min": 40093,
@@ -39,14 +39,14 @@
"brotli": 7331
},
"entry:gltf": {
- "min": 158507,
- "gzip": 48584,
- "brotli": 42047
+ "min": 170614,
+ "gzip": 52375,
+ "brotli": 45267
},
"entry:gltf+lazy": {
- "min": 169480,
- "gzip": 51789,
- "brotli": 44979
+ "min": 181587,
+ "gzip": 55582,
+ "brotli": 48213
},
"entry:modeling": {
"min": 21149,
@@ -134,9 +134,9 @@
"brotli": 23608
},
"entry:path-tracer": {
- "min": 33435,
- "gzip": 13479,
- "brotli": 11838
+ "min": 35311,
+ "gzip": 14226,
+ "brotli": 12466
},
"entry:texture-set": {
"min": 1837,
@@ -144,24 +144,24 @@
"brotli": 762
},
"entry:agent-scene": {
- "min": 214722,
- "gzip": 66960,
- "brotli": 58652
+ "min": 226825,
+ "gzip": 70681,
+ "brotli": 61823
},
"entry:agent-scene+lazy": {
- "min": 677234,
- "gzip": 233780,
- "brotli": 206588
+ "min": 689337,
+ "gzip": 237502,
+ "brotli": 209805
},
"entry:agent-asset": {
- "min": 309821,
- "gzip": 98589,
- "brotli": 86642
+ "min": 321931,
+ "gzip": 102275,
+ "brotli": 89778
},
"entry:agent-asset+lazy": {
- "min": 714100,
- "gzip": 245447,
- "brotli": 216586
+ "min": 726210,
+ "gzip": 249137,
+ "brotli": 219752
},
"entry:hdri": {
"min": 8075,
@@ -179,44 +179,44 @@
"brotli": 1425
},
"entry:material-layer-shaders": {
- "min": 76556,
- "gzip": 19687,
- "brotli": 17305
+ "min": 77526,
+ "gzip": 20073,
+ "brotli": 17610
},
"entry:light-linking": {
- "min": 67970,
- "gzip": 18132,
- "brotli": 15941
+ "min": 68941,
+ "gzip": 18518,
+ "brotli": 16273
},
"entry:cascaded-shadows": {
- "min": 95719,
- "gzip": 26679,
- "brotli": 22674
+ "min": 96700,
+ "gzip": 27048,
+ "brotli": 23073
},
"scene:lit-box": {
- "min": 218641,
- "gzip": 60179,
- "brotli": 51428
+ "min": 221329,
+ "gzip": 61256,
+ "brotli": 52261
},
"scene:gltf-box": {
- "min": 280223,
- "gzip": 80810,
- "brotli": 70295
+ "min": 282929,
+ "gzip": 81913,
+ "brotli": 71197
},
"scene:gltf-box+lazy": {
- "min": 291196,
- "gzip": 84016,
- "brotli": 73226
+ "min": 293902,
+ "gzip": 85119,
+ "brotli": 74130
},
"scene:agent-scene": {
- "min": 326356,
- "gzip": 97862,
- "brotli": 85183
+ "min": 329077,
+ "gzip": 98988,
+ "brotli": 86269
},
"scene:agent-scene+lazy": {
- "min": 788868,
- "gzip": 264689,
- "brotli": 233118
+ "min": 791589,
+ "gzip": 265817,
+ "brotli": 234213
}
}
}
diff --git a/bench/size/budgets.json b/bench/size/budgets.json
index bfc45622..23651e04 100644
--- a/bench/size/budgets.json
+++ b/bench/size/budgets.json
@@ -6,13 +6,13 @@
"agent-scene": 100000
},
"budgets": {
- "entry:core": 84400,
+ "entry:core": 85500,
"entry:model": 14400,
"entry:nature": 13200,
"entry:motion": 13600,
"entry:canvas2d": 9400,
- "entry:gltf": 49600,
- "entry:gltf+lazy": 52900,
+ "entry:gltf": 53500,
+ "entry:gltf+lazy": 56700,
"entry:modeling": 8700,
"entry:sculpt": 2500,
"entry:surface": 10100,
@@ -30,22 +30,22 @@
"entry:grass": 4400,
"entry:clutter": 5300,
"entry:light-bake": 27300,
- "entry:path-tracer": 14400,
+ "entry:path-tracer": 14600,
"entry:texture-set": 900,
- "entry:agent-scene": 68300,
- "entry:agent-scene+lazy": 238500,
- "entry:agent-asset": 100600,
- "entry:agent-asset+lazy": 250400,
+ "entry:agent-scene": 72100,
+ "entry:agent-scene+lazy": 242300,
+ "entry:agent-asset": 104400,
+ "entry:agent-asset+lazy": 254200,
"entry:hdri": 3300,
"entry:camera-effects": 8600,
"entry:render-passes": 1700,
- "entry:material-layer-shaders": 20100,
- "entry:light-linking": 18500,
- "entry:cascaded-shadows": 27300,
- "scene:lit-box": 61400,
- "scene:gltf-box": 82500,
- "scene:gltf-box+lazy": 85700,
- "scene:agent-scene": 99900,
- "scene:agent-scene+lazy": 270000
+ "entry:material-layer-shaders": 20500,
+ "entry:light-linking": 18900,
+ "entry:cascaded-shadows": 27600,
+ "scene:lit-box": 62500,
+ "scene:gltf-box": 83600,
+ "scene:gltf-box+lazy": 86900,
+ "scene:agent-scene": 101000,
+ "scene:agent-scene+lazy": 271200
}
}