3D engine that AI agents drive
Trace the irradiance grid on the kernel worker (issue #543)
Nguyen Xuan Ane9fb2f500645
diff --git a/bench/reference/harness.js b/bench/reference/harness.jsindex 3399dfa5..3445d8ef 100644--- a/bench/reference/harness.js+++ b/bench/reference/harness.js@@ -195,7 +195,10 @@ async function bakeScenePreset(scene, preset, irradianceGrid = false, overridesconst started = performance.now();let baked;try {- baked = bakeAgentParts(scene.parts, new Map(scene.parts.map(part => [part.name, part.matrix])), scene.environment, settings);+ // The recipe path bakes on a kernel worker (90 s); ?kernelWorkers=off keeps the page's thread (20 s) to compare.+ const { buildWithKernels } = await import('/src/kernel-offload.js');+ const available = new URLSearchParams(location.search).get('kernelWorkers') !== 'off';+ baked = await buildWithKernels(() => bakeAgentParts(scene.parts, new Map(scene.parts.map(part => [part.name, part.matrix])), scene.environment, settings), { available: available && typeof Worker === 'function' });} catch (error) {// A recipe would get this error and no bake: the scene is drawn unbaked and the row says so.return { parts: scene.parts, report: { refused: error.message, texels: 0 }, seconds: 0 };diff --git a/bench/reference/run.mjs b/bench/reference/run.mjsindex 278f1f14..7df60937 100644--- a/bench/reference/run.mjs+++ b/bench/reference/run.mjs@@ -118,7 +118,7 @@ const sceneIdsGiven = lookIds ?? (sceneFlag >= 0 ? argumentsGiven[sceneFlag + 1]// The fresh bake.json lands in the work directory and is checked against the committed one (compare-scores.mjs); --update-baseline replaces// the committed bake.json and images/<id>-baked.jpg with it.const bakeScoreRun = bakedSceneIds !== null && !sceneSet && !pathTracerFlag;-const bakeVariantFlags = ['--no-glossy', '--sky-only', '--bake-preset', '--bake-grid', '--bake-bounces', '--bake-backend', '--bake-samples', '--bake-density', '--probes', '--ambient', '--ambient-occlusion', '--cascades', '--no-object-probes', '--object-probes', '--no-water', '--ssr-water', '--no-ground-bounce', '--floor-sky', '--floor-sky-rooms', '--no-floor-sky'];+const bakeVariantFlags = ['--no-glossy', '--sky-only', '--bake-preset', '--bake-grid', '--no-bake-worker', '--bake-bounces', '--bake-backend', '--bake-samples', '--bake-density', '--probes', '--ambient', '--ambient-occlusion', '--cascades', '--no-object-probes', '--object-probes', '--no-water', '--ssr-water', '--no-ground-bounce', '--floor-sky', '--floor-sky-rooms', '--no-floor-sky'];const bakeScoreIsBaseline = bakeScoreRun && argumentsGiven.includes('--update-baseline') && !bakeVariantFlags.some(flag => argumentsGiven.includes(flag));const onlyScenes = sceneIdsGiven ?? (bakeScoreRun ? bakedSceneIds : null);const skipCpu = skipCpuFlag || bakeScoreRun || Boolean(viewTransform);@@ -132,7 +132,7 @@ async function renderBackend(backend, serverUrl, graphics) {page.on('console', message => message.text().includes('GL_') && console.log('PAGE', message.text().slice(0, 200)));// Each scene gets a fresh page, so a large scene leaves no GL state or memory behind for the next one.const openHarness = async () => {- const harnessQuery = [sceneSet ? `set=${sceneSet}` : null, argumentsGiven.includes('--no-glossy') ? 'glossy=0' : null, argumentsGiven.includes('--no-water') ? 'water=off' : argumentsGiven.includes('--ssr-water') ? 'water=ssr' : null, argumentsGiven.includes('--no-ground-bounce') ? 'groundBounce=off' : null, floorSkyStrength ? `floorSky=${floorSkyStrength}` : argumentsGiven.includes('--no-floor-sky') ? 'floorSky=0' : null, argumentsGiven.includes('--floor-sky-rooms') ? 'floorSkyRooms=1' : null, sunBounceQuery].filter(Boolean).join('&');+ const harnessQuery = [sceneSet ? `set=${sceneSet}` : null, argumentsGiven.includes('--no-glossy') ? 'glossy=0' : null, argumentsGiven.includes('--no-bake-worker') ? 'kernelWorkers=off' : null, argumentsGiven.includes('--no-water') ? 'water=off' : argumentsGiven.includes('--ssr-water') ? 'water=ssr' : null, argumentsGiven.includes('--no-ground-bounce') ? 'groundBounce=off' : null, floorSkyStrength ? `floorSky=${floorSkyStrength}` : argumentsGiven.includes('--no-floor-sky') ? 'floorSky=0' : null, argumentsGiven.includes('--floor-sky-rooms') ? 'floorSkyRooms=1' : null, sunBounceQuery].filter(Boolean).join('&');await page.goto(`${serverUrl}/bench/reference/harness.html${harnessQuery ? `?${harnessQuery}` : ''}`, { timeout: PAGE_LOAD_TIMEOUT });await page.waitForFunction(() => window.renderReferenceScene, null, { timeout: PAGE_LOAD_TIMEOUT });};diff --git a/changes/543-grid-worker.md b/changes/543-grid-worker.mdnew file mode 100644index 00000000..96af3476--- /dev/null+++ b/changes/543-grid-worker.md@@ -0,0 +1 @@+- An irradiance grid bake runs on the kernel worker with its 90 s limit instead of the page's 20 s, so 8 grid bounces fit a hall like temple-hall (#543)diff --git a/src/agent-bake.js b/src/agent-bake.jsindex 9ecc94a1..5a992d06 100644--- a/src/agent-bake.js+++ b/src/agent-bake.js@@ -2,7 +2,7 @@ import { planLightmaps, estimateBake, traceLightmaps } from './light-bake.js';import { runKernel } from './kernel-offload.js';import { normalizeProbeList, traceProbes } from './probe-trace.js';import { placeProbes } from './reflection-probes.js';-import { normalizeIrradianceGrid, placeIrradianceGrid, traceIrradianceGrid } from './irradiance-grid.js';+import { normalizeIrradianceGrid, placeIrradianceGrid, traceIrradianceGrid, withMeanRadiance } from './irradiance-grid.js';import { knownKeys, finiteNumber } from './agent-recipe-checks.js';import { BAKE_PRESETS, BAKE_RANGES, BAKE_MAXIMUM_SECONDS, BAKE_WORKER_MAXIMUM_SECONDS, BAKE_PRESET_PATH_WORK, BAKE_EXPLICIT_PATH_WORK_LIMIT } from './agent-scene-capabilities.js';@@ -245,15 +245,23 @@ export function bakeAgentParts(parts, worldMatrices, environment, settings, geneconst GRID_SAMPLES_SHARE = 4;const GRID_MINIMUM_SAMPLES = 8;+/**+ * The irradiance grid trace as a worker kernel. The time limit is read here and not passed in, so a worker traces under its+ * own 90 s and the page's 20 s never reaches it (and never changes the kernel's cache key).+ */+export function traceGridKernel(snapshots, lighting, options) {+ return traceIrradianceGrid(snapshots, lighting, { ...options, maxSeconds: bakeSecondsLimit() });+}+// bake.irradianceGrid: no lightmaps. Every part reads one baked grid of bounced light (the sun's lit patches included), and the-// probes still run. The grid traces on the page's thread, inside the same time limit as a bake.+// probes still run. Inside a worker session the trace runs on the worker (90 s); otherwise on the page's thread (20 s).function bakeIrradianceGridParts(parts, snapshots, lighting, settings) {- const grid = traceIrradianceGrid(snapshots, lighting, {+ const gridOptions = {resolution: settings.irradianceGrid,samples: Math.max(GRID_MINIMUM_SAMPLES, Math.round(settings.samples / GRID_SAMPLES_SHARE)),bounces: settings.bounces,- maxSeconds: bakeSecondsLimit(),- });+ };+ const grid = withMeanRadiance(runKernel('gridTrace', [snapshots, lighting, gridOptions], traceGridKernel));const probeTrace = !settings.probes ? null: runKernel('probeTrace', [snapshots, lighting, settings.probes, { environment: lighting, maxSeconds: bakeSecondsLimit() }], traceProbes);const gridParts = placeIrradianceGrid(parts, grid);diff --git a/src/irradiance-grid.js b/src/irradiance-grid.jsindex d0eff3d5..5a16ff4e 100644--- a/src/irradiance-grid.js+++ b/src/irradiance-grid.js@@ -293,10 +293,18 @@ export function traceIrradianceGrid(parts, lighting, { resolution = true, sample};fillInvalidNodes(data, valid, layout.size);smoothNodes(data, valid, layout.size, isConnected);- const grid = { ...layout, data, nodes: nodeCount, solidNodes, seconds: (performance.now() - started) / 1000 };- // The fog's scattering reads the grid without importing this lazy module: the mean light arriving at a point from every- // direction is the constant term of the fit. Not enumerable, so the report that copies the grid's fields stays plain data.- Object.defineProperty(grid, 'meanRadiance', { value: position => evaluateIrradianceGrid(grid, position, NO_DIRECTION) });+ return withMeanRadiance({ ...layout, data, nodes: nodeCount, solidNodes, seconds: (performance.now() - started) / 1000 });+}++/**+ * The fog's scattering reads the grid without importing this lazy module: the mean light arriving at a point from every+ * direction is the constant term of the fit. Not enumerable, so the report that copies the grid's fields stays plain data,+ * and a grid that came back from a worker (which clones plain data only) gets it here.+ */+export function withMeanRadiance(grid) {+ if (!Object.hasOwn(grid, 'meanRadiance')) {+ Object.defineProperty(grid, 'meanRadiance', { value: position => evaluateIrradianceGrid(grid, position, NO_DIRECTION) });+ }return grid;}diff --git a/src/kernel-registry.js b/src/kernel-registry.jsindex 00aa7951..98c121ff 100644--- a/src/kernel-registry.js+++ b/src/kernel-registry.js@@ -1,4 +1,4 @@-import { bakeChangedParts } from './agent-bake.js';+import { bakeChangedParts, traceGridKernel } from './agent-bake.js';import { bakeCloth } from './cloth.js';import { bakeMaterialPattern } from './material-patterns.js';import { traceProbes } from './probe-trace.js';@@ -9,6 +9,7 @@ import { unwrapUv } from './mesh-uv.js';const KERNELS = {lightBake: bakeChangedParts,probeTrace: traceProbes,+ gridTrace: traceGridKernel,cloth: bakeCloth,pattern: bakeMaterialPattern,uvUnwrap: unwrapUv,diff --git a/tests/irradiance-grid-worker.test.js b/tests/irradiance-grid-worker.test.jsnew file mode 100644index 00000000..b90a79f7--- /dev/null+++ b/tests/irradiance-grid-worker.test.js@@ -0,0 +1,83 @@+import './support/load-agent-scene-modules.mjs';+import assert from 'node:assert/strict';+import test from 'node:test';+import { box } from '../src/geometry.js';+import { bakeAgentParts, normalizeAgentBake } from '../src/agent-bake.js';+import { traceIrradianceGrid, withMeanRadiance } from '../src/irradiance-grid.js';+import { buildWithKernels, kernelKey } from '../src/kernel-offload.js';+import { answerKernelRequest } from '../src/kernel-registry.js';+import { createInProcessWorker } from './support/kernel-offload-fixture.mjs';++const cube = box(0, 1);++function placed(name, translation, scale, color) {+ const matrix = new Float32Array([scale[0], 0, 0, 0, 0, scale[1], 0, 0, 0, 0, scale[2], 0, translation[0], translation[1], translation[2], 1]);+ return { name, geometry: cube, matrix, material: { tag: 'surface', color, metallic: 0, roughness: 0.8, emission: 0 } };+}++function smallRoom() {+ const white = [0.8, 0.8, 0.8];+ return [+ placed('floor', [0, -0.05, 0], [4, 0.1, 4], [0.8, 0.4, 0.1]),+ placed('ceiling', [0, 3.05, 0], [4, 0.1, 4], white),+ placed('back', [0, 1.5, -2.05], [4.2, 3.2, 0.1], white),+ placed('left', [-2.05, 1.5, 0], [0.1, 3.2, 4], white),+ ];+}++const environment = {+ keyColor: [3, 3, 3], lightDirection: [0.2, 1, 0.1], skyTop: [0.2, 0.3, 0.6], skyBottom: [0.4, 0.4, 0.4],+ pointLights: [], spotLights: [], areaLights: [],+};+const settings = normalizeAgentBake({ preset: 'draft', irradianceGrid: 10, bounces: 2, probes: false });++function bakeRoom() {+ const parts = smallRoom();+ return bakeAgentParts(parts, new Map(parts.map(part => [part.name, part.matrix])), environment, settings);+}++function gridOf(bake) {+ return bake.parts[0].material.irradianceGrid;+}++test('the grid trace answers a worker request with the same coefficients as the inline trace', () => {+ const parts = smallRoom();+ const options = { resolution: 10, samples: 8, bounces: 2 };+ const inline = traceIrradianceGrid(parts, environment, { ...options, maxSeconds: 20 });+ const { response, transfer } = answerKernelRequest(structuredClone({ name: 'gridTrace', args: [parts, environment, options] }));+ assert.equal(response.error, undefined);+ assert.deepEqual(Array.from(response.value.data), Array.from(inline.data));+ assert.ok(transfer.includes(response.value.data.buffer), 'the coefficient buffer travels as a transferable, not a copy');+ assert.equal(Object.hasOwn(structuredClone(response.value), 'meanRadiance'), false, 'a cloned grid has no function until withMeanRadiance gives it one');+ const restored = withMeanRadiance(structuredClone(response.value));+ assert.deepEqual(restored.meanRadiance([0, 1.5, 0]), inline.meanRadiance([0, 1.5, 0]));+});++test('a bake inside a worker session gives the same grid as the page thread', async () => {+ const inline = bakeRoom();+ const requested = [];+ const onWorker = await buildWithKernels(bakeRoom, { createWorker: () => createInProcessWorker(requested), available: true });+ assert.deepEqual(requested, ['gridTrace']);+ const inlineData = gridOf(inline).data;+ const workerData = gridOf(onWorker).data;+ let largestDifference = 0;+ inlineData.forEach((value, index) => {+ largestDifference = Math.max(largestDifference, Math.abs(value - workerData[index]));+ });+ assert.equal(largestDifference, 0);+ assert.equal(typeof gridOf(onWorker).meanRadiance, 'function');+ assert.deepEqual(gridOf(onWorker).meanRadiance([0, 1.5, 0]), gridOf(inline).meanRadiance([0, 1.5, 0]));+});++test('the page time limit is not part of the request, so a worker traces under its own limit', () => {+ const keyArguments = [smallRoom(), environment, { resolution: 10, samples: 8, bounces: 2 }];+ assert.ok(kernelKey('gridTrace', keyArguments), 'the request is plain data');+ assert.equal(Object.hasOwn(keyArguments[2], 'maxSeconds'), false);+});++test('without workers the bake runs inline with the same result', async () => {+ const requested = [];+ const withoutWorkers = await buildWithKernels(bakeRoom, { createWorker: () => createInProcessWorker(requested), available: false });+ assert.deepEqual(requested, []);+ assert.deepEqual(Array.from(gridOf(withoutWorkers).data), Array.from(gridOf(bakeRoom()).data));+});