Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions js/engine/stage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions js/scenes/kidney.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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];
Expand Down
79 changes: 47 additions & 32 deletions tests/e2e/smoke.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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();
Expand Down
Loading