3D engine that AI agents drive
Close each cold draw bench browser in a finally block and reconcile the cold draw timings in the guide (#861)
Nguyen Xuan An97650b2419a3
diff --git a/bench/reference/cold-draw.mjs b/bench/reference/cold-draw.mjsindex 07da3335..d4d0fbdc 100644--- a/bench/reference/cold-draw.mjs+++ b/bench/reference/cold-draw.mjs@@ -22,19 +22,22 @@ try {for (let run = 0; run < runs; run++) {// A fresh browser per run: pages of one browser share the driver program cache, so a later page would start warm.const browser = await chromium.launch({ executablePath: CHROME, headless: true, args: ['--enable-webgl', '--ignore-gpu-blocklist', '--disable-renderer-backgrounding'] });- const page = await browser.newPage({ viewport: { width: 960, height: 540 } });- page.on('pageerror', error => console.log('PAGE ERROR', error.message));- page.setDefaultTimeout(0);- await page.goto(`${serverUrl}/bench/reference/harness.html`, { timeout: 180000 });- await page.waitForFunction(() => window.profileColdFirstDraw, null, { timeout: 180000 });- const wallStart = performance.now();- const result = await page.evaluate(([scene, tracerOptions]) => window.profileColdFirstDraw(scene, tracerOptions), [source, options]);- // The phases must add up to the wall time of the call within 5 s (issue #861); the gap is time spent outside a named phase.- result.wallSeconds = Number(((performance.now() - wallStart) / 1000).toFixed(2));- const allPhases = Object.values(result.phases).reduce((sum, seconds) => sum + seconds, 0);- result.unexplainedSeconds = Number((result.wallSeconds - allPhases).toFixed(2));- console.log(JSON.stringify(result));- await browser.close();+ try {+ const page = await browser.newPage({ viewport: { width: 960, height: 540 } });+ page.on('pageerror', error => console.log('PAGE ERROR', error.message));+ page.setDefaultTimeout(0);+ await page.goto(`${serverUrl}/bench/reference/harness.html`, { timeout: 180000 });+ await page.waitForFunction(() => window.profileColdFirstDraw, null, { timeout: 180000 });+ const wallStart = performance.now();+ const result = await page.evaluate(([scene, tracerOptions]) => window.profileColdFirstDraw(scene, tracerOptions), [source, options]);+ // The phases must add up to the wall time of the call within 5 s (issue #861); the gap is time spent outside a named phase.+ result.wallSeconds = Number(((performance.now() - wallStart) / 1000).toFixed(2));+ const allPhases = Object.values(result.phases).reduce((sum, seconds) => sum + seconds, 0);+ result.unexplainedSeconds = Number((result.wallSeconds - allPhases).toFixed(2));+ console.log(JSON.stringify(result));+ } finally {+ await browser.close();+ }}} finally {await server.close();diff --git a/docs/path-tracer.md b/docs/path-tracer.mdindex 47a1e462..fd0d6ac7 100644--- a/docs/path-tracer.md+++ b/docs/path-tracer.md@@ -204,6 +204,6 @@ WebGPU backend, same setup (2026-11-09, `--backend webgpu`, median of 200 frames### Cold first draw (issue #861)-The first path traced draw of a fresh page on the `gpu` backend took 108 s for one sample of `lit-interior` (960 by 540, RTX 3060, ANGLE on Direct3D 11, a profile with no shader cache, `bench/reference/cold-draw.mjs`). All of it was the driver building the kernel: scene load, BVH, packing and texture upload together took 0.15 s, linking took 73.9 s and the first dispatch another 34.1 s (the driver finishes its work on first use), while every later draw took 7 to 35 ms. The compiler inlines each call, and `addLights` called the emitter sampling and the shadow ray from four places, so the traversal was built five times. `addLights` now has one loop for "every emitter" and "one chosen emitter" and the kernel holds one shadow ray (`tests/path-tracer-gpu-kernel-size.test.js` counts the call sites of each variant and the size of the plain kernel). After the change three cold runs on the loaded machine took 27.4, 35.6 and 46.6 s (15.6 to 25.4 s linking, 11.6 to 21.1 s first dispatch), and the old and new kernels compiled side by side in one page took 36 and 7.8 s, 23 and 6.5 s. The progressive tracer at 320 by 180 first step with the random, importance and tree kernels went from 22.9, 18.5 and 38.7 s to 10.3, 9.2 and 14.1 s. The cold first draw is not yet reliably under 30 s: the two places that trace rays (the camera path and the shadow ray) are still two copies of the traversal, and merging them is the next cut. The images of `lit-interior` and `spot-area` at 1, 4 and 8 samples with the random, importance and tree kernels are byte for byte the same as before (18 PNGs, MD5). A second page of the same browser session starts in 0.14 s, since the driver program cache is shared, and the compiled kernel is kept per page by `getKernel`. The `webgpu` backend compiles its kernel in about 0.5 s.+The first path traced draw of a fresh page on the `gpu` backend took 108 s for one sample of `lit-interior` (960 by 540, RTX 3060, ANGLE on Direct3D 11, a profile with no shader cache, `bench/reference/cold-draw.mjs`). All of it was the driver building the kernel: scene load, BVH, packing and texture upload together took 0.15 s, linking took 73.9 s and the first dispatch another 34.1 s (the driver finishes its work on first use), while every later draw took 7 to 35 ms. The compiler inlines each call, and `addLights` called the emitter sampling and the shadow ray from four places, so the traversal was built five times. `addLights` now has one loop for "every emitter" and "one chosen emitter" and the kernel holds one shadow ray (`tests/path-tracer-gpu-kernel-size.test.js` counts the call sites of each variant and the size of the plain kernel). After the change three cold runs on the loaded machine took 27.4, 35.6 and 46.6 s (15.6 to 25.4 s linking, 11.6 to 21.1 s first dispatch), and the old and new kernels compiled side by side in one page took 36 and 7.8 s, 23 and 6.5 s. The progressive tracer at 320 by 180 first step with the random, importance and tree kernels went from 22.9, 18.5 and 38.7 s to 10.3, 9.2 and 14.1 s. That run was not yet reliably under 30 s, and merging the two copies of the traversal (the camera path and the shadow ray) was planned as the next cut; the measurement below supersedes that verdict. The images of `lit-interior` and `spot-area` at 1, 4 and 8 samples with the random, importance and tree kernels are byte for byte the same as before (18 PNGs, MD5). A second page of the same browser session starts in 0.14 s, since the driver program cache is shared, and the compiled kernel is kept per page by `getKernel`. The `webgpu` backend compiles its kernel in about 0.5 s.-Measured after the merge, three cold runs of `lit-interior` at 960 by 540, each in a fresh browser behind the GPU lock (`bench/reference/cold-draw.mjs lit-interior --runs 3`; the bench starts one browser per run, since pages of one browser share the driver program cache and a later page starts in 0.1 s): the wall time of the call was 9.56, 9.54 and 10.17 s. Link and wait for completion took 4.06 to 4.32 s, the first dispatch with its readback 5.35 to 5.76 s, and every other named phase (scene load, module import, trace scene and BVH, context, source, compile and link submit, scene and texture upload, uniforms and targets) added up to 0.04 s. The named phases cover the wall time to within 0.06 s (the bench prints `wallSeconds` and `unexplainedSeconds`). The kernel is 45 KB, one program per scene. `tests/path-tracer-gpu-kernel-size.test.js` now holds a source size limit for each of the eight kernel variants, one `main` per kernel and one place that builds a program, so a larger kernel or a second program fails the test.+Measured later with the same kernel source (the only changes since are to the bench and its test), three cold runs of `lit-interior` at 960 by 540, each in a fresh browser behind the GPU lock (`bench/reference/cold-draw.mjs lit-interior --runs 3`; the bench starts one browser per run, since pages of one browser share the driver program cache and a later page starts in 0.1 s): the wall time of the call was 9.56, 9.54 and 10.17 s. The 27 to 47 s runs above were taken on the loaded machine, the runs here behind the GPU lock on an otherwise idle card; this PR did not isolate which of load or the browser setup explains the gap, so read the 9.5 to 10.2 s range as the idle-card cold time and 27 to 47 s as the loaded one. Link and wait for completion took 4.06 to 4.32 s, the first dispatch with its readback 5.35 to 5.76 s, and every other named phase (scene load, module import, trace scene and BVH, context, source, compile and link submit, scene and texture upload, uniforms and targets) added up to 0.04 s. The named phases cover the wall time to within 0.06 s (the bench prints `wallSeconds` and `unexplainedSeconds`). The kernel is 45 KB, one program per scene. `tests/path-tracer-gpu-kernel-size.test.js` now holds a source size limit for each of the eight kernel variants, one `main` per kernel and one place that builds a program, so a larger kernel or a second program fails the test.diff --git a/tests/path-tracer-gpu-kernel-size.test.js b/tests/path-tracer-gpu-kernel-size.test.jsindex 06fc0100..fa5e93b7 100644--- a/tests/path-tracer-gpu-kernel-size.test.js+++ b/tests/path-tracer-gpu-kernel-size.test.js@@ -53,3 +53,12 @@ test('one scene links one kernel: the gpu backend builds a program from one placassert.equal(buildFragmentSource(variant).match(/void main\s*\(/g).length, 1);}});++test('the cold draw bench closes the browser of every run in a finally block, so a failed run does not hold the GPU', () => {+ const bench = readFileSync(new URL('../bench/reference/cold-draw.mjs', import.meta.url), 'utf8');+ const launchIndex = bench.indexOf('chromium.launch(');+ const closeIndex = bench.indexOf('await browser.close();');+ assert.ok(launchIndex >= 0 && closeIndex > launchIndex, 'the bench launches and closes a browser');+ assert.match(bench.slice(launchIndex, closeIndex), /try \{[\s\S]*\} finally \{\s*$/, 'the close sits in the finally of a try that wraps the run');+ assert.equal(bench.match(/await browser\.close\(\)/g).length, 1);+});