Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Raise size budgets for the cheaper water mirror (measured + 20, rounded up to 10): culling, light skip, reuse and updateEvery add about 150 bytes gzip to core and 420 to the water reflection entry (closes part of #316)

Nguyen Xuan An39908b7df172
diff --git a/bench/size/budgets.json b/bench/size/budgets.json
index 9f0e6575..9ab881d6 100644
--- a/bench/size/budgets.json
+++ b/bench/size/budgets.json
@@ -6,7 +6,7 @@
"agent-scene": 100000
},
"budgets": {
- "entry:core": 88890,
+ "entry:core": 89050,
"entry:model": 14400,
"entry:nature": 13200,
"entry:motion": 15260,
@@ -40,16 +40,16 @@
"entry:agent-asset+lazy": 274720,
"entry:hdri": 3300,
"entry:camera-effects": 8600,
- "entry:water-reflection": 26580,
+ "entry:water-reflection": 27030,
"entry:render-passes": 1700,
"entry:gaussian-splats": 11023,
"entry:material-layer-shaders": 21400,
"entry:light-linking": 19720,
"entry:cascaded-shadows": 28530,
- "scene:lit-box": 65870,
- "scene:gltf-box": 90150,
- "scene:gltf-box+lazy": 93480,
- "scene:agent-scene": 109670,
- "scene:agent-scene+lazy": 292250
+ "scene:lit-box": 66030,
+ "scene:gltf-box": 90310,
+ "scene:gltf-box+lazy": 93640,
+ "scene:agent-scene": 109840,
+ "scene:agent-scene+lazy": 292420
}
}
diff --git a/changes/316.md b/changes/316.md
new file mode 100644
index 00000000..12b980be
--- /dev/null
+++ b/changes/316.md
@@ -0,0 +1 @@
+- Make the water reflection cheaper (part of #316): the mirror draw skips parts under the plane and sub-pixel parts, takes coarser levels, drops local lights and the cascade redraw, keeps its picture while the frame and parts are unchanged, and `attachWaterReflection` takes `updateEvery` for films.
diff --git a/scripts/benchmark-water-reflection.mjs b/scripts/benchmark-water-reflection.mjs
index cd7e85d3..6dc40344 100644
--- a/scripts/benchmark-water-reflection.mjs
+++ b/scripts/benchmark-water-reflection.mjs
@@ -1,7 +1,9 @@
// Times a lit frame of a large sea with dense terrain and many small parts, with the water reflection off and on, and counts
// what the mirror pass draws. GPU time comes from a native timer query, so run it through the GPU lock:
-// bash ~/.agents/tools/gpu-lock.sh node scripts/benchmark-water-reflection.mjs [--width 1920] [--height 1080] [--parts 20000]
+// bash ~/.agents/tools/gpu-lock.sh node scripts/benchmark-water-reflection.mjs [--width 1920] [--height 1080] [--parts 20000] [--stills folder]
+// --stills writes a PNG of the lit frame for the off, on and half-scale variants, for a side-by-side check.
// The draw counts (calls, instances, triangles in the mirror pass) do not need the GPU and stay valid when the lock is busy.
+import { mkdir, writeFile } from 'node:fs/promises';
import { constants, setPriority } from 'node:os';
import { chromium } from 'playwright';
import { createServer } from 'vite';
@@ -15,6 +17,7 @@ const width = argument('width', 1920);
const height = argument('height', 1080);
const smallPartCount = argument('parts', 20000);
const SAMPLES = argument('samples', 20);
+const stillsFolder = process.argv.includes('--stills') ? process.argv[process.argv.indexOf('--stills') + 1] : null;
const server = await createServer({ server: { host: '127.0.0.1', port: 0 }, logLevel: 'silent' });
await server.listen();
@@ -24,7 +27,7 @@ try {
const page = await browser.newPage({ viewport: { width, height } });
await page.route('**/water-benchmark.html', route => route.fulfill({ contentType: 'text/html', body: `<canvas style="width:${width}px;height:${height}px;display:block"></canvas>` }));
await page.goto(`${serverUrl}/water-benchmark.html`);
- const results = await page.evaluate(async ({ smallPartCount, samples }) => {
+ const results = await page.evaluate(async ({ smallPartCount, samples, wantStills }) => {
const { Renderer67 } = await import('/src/renderer.js');
const { box } = await import('/src/geometry.js');
const { createTerrain } = await import('/src/terrain.js');
@@ -73,17 +76,17 @@ try {
};
const frame = { eye: [0, 70, 380], target: [0, 0, 0], time: 1 };
- async function measure(label, reflect, options = {}) {
+ async function measure(label, reflect, options = {}, still = false) {
const detachNow = reflect ? attachWaterReflection(renderer, marked.surface, options) : () => {};
for (const tally of Object.values(phases)) Object.assign(tally, { calls: 0, instances: 0, triangles: 0 });
- for (let index = 0; index < 4; index++) renderer.render({ ...frame, time: 1 + index * 0.001 });
+ for (let index = 0; index < 4; index++) renderer.render(still ? frame : { ...frame, time: 1 + index * 0.001 });
const timings = [];
for (let index = 0; index < samples; index++) {
for (const tally of Object.values(phases)) Object.assign(tally, { calls: 0, instances: 0, triangles: 0 });
const query = context.createQuery();
context.beginQuery(timer.TIME_ELAPSED_EXT, query);
// The camera moves a little each frame, so the mirror image is redrawn, as in a film.
- renderer.render({ ...frame, eye: [index * 0.2, 70, 380], time: 1 + index * 0.01 });
+ renderer.render(still ? frame : { ...frame, eye: [index * 0.2, 70, 380], time: 1 + index * 0.01 });
context.endQuery(timer.TIME_ELAPSED_EXT);
context.flush();
const started = performance.now();
@@ -97,14 +100,30 @@ try {
}
timings.sort((first, second) => first - second);
const output = { label, medianMilliseconds: timings[Math.floor(timings.length / 2)], minimumMilliseconds: timings[0], maximumMilliseconds: timings[timings.length - 1], mirrorDraw: { ...phases.mirror }, mainDraw: { ...phases.main }, glError: context.getError() };
+ if (wantStills) {
+ renderer.render(frame);
+ output.image = canvas.toDataURL('image/png');
+ }
detachNow();
return output;
}
const report = { canvas: [canvas.width, canvas.height], terrainVertices: terrain.parts.reduce((sum, part) => sum + part.geometry.positions.length / 3, 0), smallParts: smallPartCount };
report.reflectionOff = await measure('off', false);
report.reflectionOn = await measure('on', true);
+ report.updateEvery2 = await measure('updateEvery 2', true, { updateEvery: 2 });
+ if (wantStills) report.halfScale = await measure('scale 0.5', true, { scale: 0.5 });
+ report.stillCamera = await measure('still camera', true, {}, true);
return report;
- }, { smallPartCount, samples: SAMPLES });
+ }, { smallPartCount, samples: SAMPLES, wantStills: stillsFolder !== null });
+ if (stillsFolder) {
+ await mkdir(stillsFolder, { recursive: true });
+ for (const [name, entry] of Object.entries(results)) {
+ if (entry?.image) {
+ await writeFile(`${stillsFolder}/${name}.png`, Buffer.from(entry.image.split(',')[1], 'base64'));
+ delete entry.image;
+ }
+ }
+ }
console.log(JSON.stringify(results, null, 2));
} finally {
await browser.close();