From 0c69e02211bbf7a197784f0d99e9a88e4b515066 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 29 Sep 2026 13:35:35 +0000 Subject: [PATCH] Keep slow software-GL scenes within budget; isolate smoke-test scenes The browser job on main failed after the kidney scene: the harness reused one page and its page.goto('about:blank') timed out (30 s) while the renderer was still busy with software-GL frames. Reproduced by pinning the browser to two cores (kidney then also misses its 90 s 45-frame budget). Root cause is how slowly kidney renders under SwiftShader: - adaptive resolution counted frames (20 per step, -15 %, floor 0.5), so at ~2 s/frame it needed minutes to adapt. It now closes a window after 20 frames or 0.75 s, judges the median frame time (a lone shader-compile stall does not count), steps in proportion to the overrun (cost ~ pr^2) and may go down to 0.35 - with bloom off (H&E mode) the scene went straight to the multisampled canvas; a device that had to lower its resolution now renders through the composer's single-sample target instead. Fast GPUs are unchanged - kidney's ~3,700 epithelial and podocyte nuclei, inside translucent membranes, use an 80- instead of a 180-triangle sphere smoke.mjs gives every scene its own browser context, so no navigation has to wait for the previous scene to unload; it bounds page.evaluate/close, records a failing scene instead of crashing the run, and drops the pointless switch back to 3D mode at the end of each scene. Two cores, every scene and view in all three modes: 28/28 pass in 13.6 min (kidney 118 s; before: kidney failed at 229 s). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016pbMe2Yw2abpatNN7kc6j2 --- js/engine/stage.js | 39 +++++++++++++++++----- js/scenes/kidney.js | 9 +++--- tests/e2e/smoke.mjs | 79 +++++++++++++++++++++++++++------------------ 3 files changed, 83 insertions(+), 44 deletions(-) diff --git a/js/engine/stage.js b/js/engine/stage.js index 0a68ac5..0761909 100644 --- a/js/engine/stage.js +++ b/js/engine/stage.js @@ -15,6 +15,9 @@ const BACKGROUNDS = { }; const BLOOM = { physical: 0.12, confocal: 0.38, histology: 0.0 }; const BLOOM_THRESHOLD = { physical: 0.6, confocal: 0.42, histology: 1 }; +// adaptive resolution: frame-time target and the lowest pixel ratio it may use +const TARGET_FRAME = 1 / 30; +const MIN_PIXEL_RATIO = 0.35; export class Stage { constructor(canvas) { @@ -71,9 +74,10 @@ export class Stage { // adaptive resolution: keep interaction smooth on weak GPUs / software GL this.maxPixelRatio = Math.min(window.devicePixelRatio || 1, 2); this.pixelRatio = this.maxPixelRatio; + this._frameTimes = []; this._frameAcc = 0; - this._frameN = 0; this.adaptive = true; + this.lowPower = false; this._resize = () => this.resize(); window.addEventListener('resize', this._resize); @@ -167,22 +171,40 @@ export class Stage { shared.uTime.value += dt; if (this.onFrame) this.onFrame(this.running ? dt * this.speed : 0, dt); this.controls.update(); - if (this.bloom.strength > 0.001) this.composer.render(); - else this.renderer.render(this.scene, this.camera); + // Without bloom the scene goes straight to the multisampled canvas, except + // on a device that had to lower its resolution: there the composer's + // single-sample target is several times cheaper. + const bloomOn = this.bloom.strength > 0.001; + if (bloomOn || this.lowPower) { + this.bloom.enabled = bloomOn; + this.composer.render(); + } else this.renderer.render(this.scene, this.camera); this.frames++; }; tick(); } + /** + * Judge a window of frames by its median frame time, so that a single + * shader-compile stall does not lower the resolution. A window closes after + * 20 frames or 0.75 s (at least 3 frames), so software GL adapts in seconds. + * Cost scales with pixel count (pr^2), so the step is sized to the overrun. + */ adaptResolution(raw) { if (!this.adaptive) return; + const w = this._frameTimes; + w.push(raw); this._frameAcc += raw; - if (++this._frameN < 20) return; - const mean = this._frameAcc / this._frameN; - this._frameAcc = 0; this._frameN = 0; + if (w.length < 20 && !(this._frameAcc >= 0.75 && w.length >= 3)) return; + const median = w.slice().sort((a, b) => a - b)[w.length >> 1]; + w.length = 0; + this._frameAcc = 0; let pr = this.pixelRatio; - if (mean > 1 / 24 && pr > 0.5) pr = Math.max(0.5, pr * 0.85); - else if (mean < 1 / 50 && pr < this.maxPixelRatio) pr = Math.min(this.maxPixelRatio, pr * 1.15); + if (median > 1 / 24 && pr > MIN_PIXEL_RATIO) { + const step = Math.min(0.85, Math.max(0.5, Math.sqrt(TARGET_FRAME / median))); + pr = Math.max(MIN_PIXEL_RATIO, pr * step); + } else if (median < 1 / 50 && pr < this.maxPixelRatio) pr = Math.min(this.maxPixelRatio, pr * 1.15); + this.lowPower = pr < this.maxPixelRatio * 0.75; if (pr !== this.pixelRatio) { this.pixelRatio = pr; this.renderer.setPixelRatio(pr); @@ -194,6 +216,7 @@ export class Stage { /** Fixed resolution (used when recording previews). */ setFixedPixelRatio(pr) { this.adaptive = false; + this.lowPower = false; this.pixelRatio = pr; this.renderer.setPixelRatio(pr); this.composer.setPixelRatio?.(pr); diff --git a/js/scenes/kidney.js b/js/scenes/kidney.js index d61dedf..64c72a4 100644 --- a/js/scenes/kidney.js +++ b/js/scenes/kidney.js @@ -74,9 +74,10 @@ export default { lumen.renderOrder = 4; neph.add(lumen); - const sphere = unitSphere(2); - const epi = new InstancePool(sphere, cellMaterial({ role: 'membrane', opacity: 0.55, noise: 0.35 }), 4096, neph); - const epiNuc = new InstancePool(sphere, cellMaterial({ role: 'nucleus', color: 0xa9c4ff }), 4096, neph); + // ~3,700 epithelial cells: the nuclei sit inside translucent membranes, so + // a coarser sphere (80 instead of 180 triangles) costs nothing visible + const epi = new InstancePool(unitSphere(2), cellMaterial({ role: 'membrane', opacity: 0.55, noise: 0.35 }), 4096, neph); + const epiNuc = new InstancePool(unitSphere(1), cellMaterial({ role: 'nucleus', color: 0xa9c4ff }), 4096, neph); const tmp = new THREE.Color(); const up = new THREE.Vector3(); const cells = []; @@ -131,7 +132,7 @@ export default { corpuscle.add(new THREE.Mesh(new THREE.TubeGeometry(new THREE.CatmullRomCurve3(loop, true), 60, 1.6, 8, true), capMat)); } const podo = new InstancePool(unitSphere(2), cellMaterial({ role: 'membrane', color: 0x9dffcf, opacity: 0.6 }), 256, corpuscle); - const podoNuc = new InstancePool(unitSphere(2), cellMaterial({ role: 'nucleus', color: 0xa9c4ff }), 256, corpuscle); + const podoNuc = new InstancePool(unitSphere(1), cellMaterial({ role: 'nucleus', color: 0xa9c4ff }), 256, corpuscle); const feet = new InstancePool(unitSphere(1), cellMaterial({ role: 'reporter', color: 0x6fffc0 }), 2048, corpuscle); podo.begin(); podoNuc.begin(); feet.begin(); const dir = [0, 0, 0]; diff --git a/tests/e2e/smoke.mjs b/tests/e2e/smoke.mjs index cf7568e..d413990 100644 --- a/tests/e2e/smoke.mjs +++ b/tests/e2e/smoke.mjs @@ -25,34 +25,28 @@ const browser = await chromium.launch({ executablePath: process.env.CHROMIUM_PATH || (existsSync(preinstalled) ? preinstalled : undefined), args: ['--use-angle=swiftshader', '--enable-unsafe-swiftshader', '--ignore-gpu-blocklist'], }); -const page = await browser.newPage({ viewport: { width: 1400, height: 860 } }); -const errors = []; -page.on('console', (m) => { if (m.type() === 'error') errors.push(m.text()); }); -page.on('pageerror', (e) => errors.push(String(e))); +const viewport = { width: 1400, height: 860 }; -let failed = 0; -for (const spec of ids) { - // spec is a scene id, optionally with a query: "gallery?view=skin" - const id = spec.split('?')[0]; - const shot = spec.replace(/[?&=]/g, '-'); - // a fresh document per scene (a hash-only change would reuse the page) - await page.goto('about:blank'); - const before = errors.length; - const t0 = Date.now(); +// Bound a browser call that has no timeout of its own (page.evaluate, close). +function within(ms, what, promise) { + let timer; + const limit = new Promise((_, reject) => { timer = setTimeout(() => reject(new Error(`${what} timed out after ${ms / 1000}s`)), ms); }); + return Promise.race([promise, limit]).finally(() => clearTimeout(timer)); +} + +async function checkScene(page, spec, id, errors) { await page.goto(`${url}#${spec}`); - let ok = true, why = ''; - try { - await page.waitForFunction((sid) => window.__sim?.ready && window.__sim.sceneId === sid, id, { timeout: 90000 }); - const f0 = await page.evaluate(() => window.__sim.frames); - await page.waitForFunction((f) => window.__sim.frames > f + 45, f0, { timeout: 90000 }); - } catch (e) { ok = false; why = e.message.split('\n')[0]; } + await page.waitForFunction((sid) => window.__sim?.ready && window.__sim.sceneId === sid, id, { timeout: 90000 }); + const f0 = await within(30000, 'reading the frame count', page.evaluate(() => window.__sim.frames)); + await page.waitForFunction((f) => window.__sim.frames > f + 45, f0, { timeout: 90000 }); + let why = ''; for (const mode of modes) { if (mode !== 'physical') { - await page.evaluate((m) => window.__sim.setMode(m), mode); - const f1 = await page.evaluate(() => window.__sim.frames); + await within(30000, `switching to ${mode}`, page.evaluate((m) => window.__sim.setMode(m), mode)); + const f1 = await within(30000, 'reading the frame count', page.evaluate(() => window.__sim.frames)); await page.waitForFunction((f) => window.__sim.frames > f + 20, f1, { timeout: 60000 }).catch(() => {}); } - const stats = await page.evaluate(() => { + const stats = await within(60000, `measuring the ${mode} frame`, page.evaluate(() => { const c = document.getElementById('gl'); const w = 240, h = Math.round((240 * c.height) / c.width); const cv = Object.assign(document.createElement('canvas'), { width: w, height: h }); @@ -63,18 +57,39 @@ for (const spec of ids) { for (let i = 0; i < d.length; i += 4) { const v = (d[i] + d[i + 1] + d[i + 2]) / 3; s += v; s2 += v * v; n++; } const mean = s / n; return { mean, std: Math.sqrt(Math.max(s2 / n - mean * mean, 0)) }; - }); - await page.screenshot({ path: join(outDir, `${shot}${mode === 'physical' ? '' : '-' + mode}.png`) }); - if (stats.std < 2) { ok = false; why ||= `blank frame in ${mode} (std ${stats.std.toFixed(2)})`; } + })); + const shot = spec.replace(/[?&=]/g, '-'); + await page.screenshot({ path: join(outDir, `${shot}${mode === 'physical' ? '' : '-' + mode}.png`), timeout: 60000 }); + if (stats.std < 2) why ||= `blank frame in ${mode} (std ${stats.std.toFixed(2)})`; console.log(` ${mode.padEnd(9)} mean ${stats.mean.toFixed(1)} std ${stats.std.toFixed(1)}`); } - if (modes.length > 1) await page.evaluate(() => window.__sim.setMode('physical')); - const simErrors = await page.evaluate(() => window.__sim?.errors ?? []); - const newErrors = [...errors.slice(before), ...simErrors]; - if (newErrors.length) { ok = false; why ||= newErrors.join(' | ').slice(0, 400); } - console.log(`${ok ? 'PASS' : 'FAIL'} ${spec.padEnd(16)} ${((Date.now() - t0) / 1000).toFixed(1)}s ${why}`); - if (!ok) failed++; - await page.evaluate(() => { window.__sim.errors.length = 0; }); + const simErrors = await within(30000, 'reading scene errors', page.evaluate(() => window.__sim?.errors ?? [])); + const all = [...errors, ...simErrors]; + if (all.length) why ||= all.join(' | ').slice(0, 400); + return why; +} + +let failed = 0; +for (const spec of ids) { + // spec is a scene id, optionally with a query: "gallery?view=skin". + // Each scene gets its own browser context (and renderer process), so a scene + // still inside a long software-GL frame cannot hold up or leak into the next. + const id = spec.split('?')[0]; + const context = await browser.newContext({ viewport }); + const page = await context.newPage(); + const errors = []; + page.on('console', (m) => { if (m.type() === 'error') errors.push(m.text()); }); + page.on('pageerror', (e) => errors.push(String(e))); + const t0 = Date.now(); + let why; + try { + why = await checkScene(page, spec, id, errors); + } catch (e) { + why = e.message.split('\n')[0]; + } + await within(30000, 'closing the page', context.close()).catch((e) => console.error(` ${e.message}`)); + console.log(`${why ? 'FAIL' : 'PASS'} ${spec.padEnd(16)} ${((Date.now() - t0) / 1000).toFixed(1)}s ${why}`); + if (why) failed++; } await browser.close(); server.close();