3D engine that AI agents drive
Progressive GPU path tracer, blue noise first sample and importance light choice (issue #623)
Nguyen Xuan An7d20f1b92cea
diff --git a/bench/reference/harness.js b/bench/reference/harness.jsindex 0f644720..9e716634 100644--- a/bench/reference/harness.js+++ b/bench/reference/harness.js@@ -389,8 +389,8 @@ async function renderReferencePathTracedFeatures(id, options) {window.renderReferencePathTracedFeatures = renderReferencePathTracedFeatures;-// Traces a scene recipe the way an agent render job does (parts, environment map and camera of the agent scene).-async function renderRecipePathTraced(recipe, options) {+// Builds the parts, environment and camera of a scene recipe the way an agent render job does.+async function loadRecipeTraceScene(recipe) {const { createAgentScene, loadAllAgentSceneModules, needsMeshBooleans, loadMeshBooleans, setManifoldModuleLoader } = await import('/src/agent-scene.js');const { agentEnvironmentMap } = await import('/src/agent-look.js');await loadAllAgentSceneModules();@@ -399,9 +399,14 @@ async function renderRecipePathTraced(recipe, options) {await loadMeshBooleans();}const scene = createAgentScene(recipe);- const { renderPathTracedImage } = await import('/src/path-tracer.js');const environment = scene.environment;- const result = await renderPathTracedImage({ parts: scene.parts, environment: { ...environment, map: agentEnvironmentMap(environment) }, camera: scene.camera }, options);+ return { parts: scene.parts, environment: { ...environment, map: agentEnvironmentMap(environment) }, camera: scene.camera };+}++// Traces a scene recipe the way an agent render job does (parts, environment map and camera of the agent scene).+async function renderRecipePathTraced(recipe, options) {+ const { renderPathTracedImage } = await import('/src/path-tracer.js');+ const result = await renderPathTracedImage(await loadRecipeTraceScene(recipe), options);const canvas = document.createElement('canvas');canvas.width = result.width;canvas.height = result.height;@@ -414,3 +419,65 @@ window.renderReferenceScene = renderReferenceScene;window.loadBarnLampVariant = loadBarnLampVariant;window.renderReferencePathTraced = renderReferencePathTraced;window.referenceSceneIds = Object.keys(SCENES);++async function loadTraceSceneFor(source) {+ if (typeof source === 'string') {+ const scene = await REFERENCE_SCENES[source]();+ return { parts: applyLightLinks(scene.parts, scene.environment), environment: scene.environment, camera: scene.camera };+ }+ return loadRecipeTraceScene(source);+}++function rgbaToBase64Png(rgba, width, height) {+ const canvas = document.createElement('canvas');+ canvas.width = width;+ canvas.height = height;+ canvas.getContext('2d').putImageData(new ImageData(rgba, width, height), 0, 0);+ return canvas.toDataURL('image/png').split(',')[1];+}++const median = values => [...values].sort((first, second) => first - second)[Math.floor(values.length / 2)];++// Draws a scene progressively (issue #623) and saves the image after each count in `checkpoints` (samples at full size).+// `stepSeconds` is the time of the drawing steps alone (reduced sizes included), `snapshotSeconds` one read back and filter.+async function renderProgressiveCheckpoints(source, options, checkpoints) {+ const scene = await loadTraceSceneFor(source);+ const { createProgressiveTracer } = await import('/src/path-tracer-progressive.js');+ const setupStarted = performance.now();+ const tracer = await createProgressiveTracer(scene, options);+ const setupSeconds = (performance.now() - setupStarted) / 1000;+ const results = [];+ let stepSeconds = 0;+ for (let stepIndex = 0; results.length < checkpoints.length; stepIndex++) {+ const started = performance.now();+ const drawn = await tracer.step();+ stepSeconds += (performance.now() - started) / 1000;+ const fullSamples = drawn.divisor === 1 ? drawn.samples : 0;+ if (fullSamples !== checkpoints[results.length]) continue;+ const snapshotStarted = performance.now();+ const snapshot = tracer.snapshot();+ const snapshotSeconds = (performance.now() - snapshotStarted) / 1000;+ results.push({ samples: fullSamples, stepSeconds, snapshotSeconds, image: rgbaToBase64Png(snapshot.rgba, snapshot.width, snapshot.height) });+ }+ const report = { gpu: tracer.gpu, compileSeconds: tracer.compileSeconds, setupSeconds };+ tracer.dispose();+ return { results, report };+}++// Time of one sample of every pixel at full size, `frames` times (the first `warmup` frames are not counted).+async function measureProgressiveFrameTime(source, options, frames = 200, warmup = 5) {+ const scene = await loadTraceSceneFor(source);+ const { createProgressiveTracer } = await import('/src/path-tracer-progressive.js');+ const tracer = await createProgressiveTracer(scene, options, { resolutionDivisors: [1] });+ const times = [];+ for (let frame = 0; frame < frames + warmup; frame++) {+ const started = performance.now();+ await tracer.step();+ if (frame >= warmup) times.push(performance.now() - started);+ }+ tracer.dispose();+ return { frames, medianMilliseconds: median(times), minimumMilliseconds: Math.min(...times), maximumMilliseconds: Math.max(...times) };+}++window.renderProgressiveCheckpoints = renderProgressiveCheckpoints;+window.measureProgressiveFrameTime = measureProgressiveFrameTime;diff --git a/bench/reference/progressive-compare.mjs b/bench/reference/progressive-compare.mjsnew file mode 100644index 00000000..d4e36080--- /dev/null+++ b/bench/reference/progressive-compare.mjs@@ -0,0 +1,89 @@+// Draws reference scenes progressively (issue #623) and measures the time against the 64 sample still.+// node bench/reference/progressive-compare.mjs <output directory> <scene id or recipe.json>,... [--variant random|blue|importance|both]+// [--checkpoints 1,2,4,8,16,32,64] [--width 960] [--height 540] [--still] [--frames 200]+// Files are <scene>@<variant>-<samples>.png, <scene>@still-64.png and timings.json; bench/reference/progressive-score.py scores them against Cycles.+// With --frames the script also prints the median time of one full size sample (1 sample per pixel) for each scene.+// Run it behind a GPU lock when other jobs share the GPU: it takes the whole GPU for seconds at a time.+import { mkdir, readFile, writeFile } from 'node:fs/promises';+import { constants, setPriority } from 'node:os';+import { resolve } from 'node:path';+import { chromium } from 'playwright';+import { createServer } from 'vite';++const CHROME = process.env.CHROME_PATH ?? 'C:/Program Files/Google/Chrome/Application/chrome.exe';+const argumentsGiven = process.argv.slice(2);+const outputDirectory = resolve(argumentsGiven[0]);+const sceneIds = argumentsGiven[1].split(',');+const recipeName = id => id.replace(/^.*[\/]/, '').replace(/\.recipe\.json$|\.json$/, '');++function valueAfter(flag, fallback) {+ const index = argumentsGiven.indexOf(flag);+ return index >= 0 ? argumentsGiven[index + 1] : fallback;+}++const VARIANT_OPTIONS = {+ random: {},+ blue: { samplingPattern: 'blue-noise-first' },+ importance: { lightSelection: 'importance' },+ both: { samplingPattern: 'blue-noise-first', lightSelection: 'importance' },+};+const variantNames = valueAfter('--variant', 'random').split(',');+const checkpoints = valueAfter('--checkpoints', '1,2,4,8,16,32,64').split(',').map(Number);+const frameCount = Number(valueAfter('--frames', 0));+const baseOptions = {+ width: Number(valueAfter('--width', 960)),+ height: Number(valueAfter('--height', 540)),+ bounces: Number(valueAfter('--bounces', 8)),+ denoise: !argumentsGiven.includes('--no-denoise'),+ maxSeconds: 1800,+};++setPriority(0, constants.priority.PRIORITY_BELOW_NORMAL);+await mkdir(outputDirectory, { recursive: true });+const server = await createServer({ server: { host: '127.0.0.1', port: 0, fs: { strict: false } }, logLevel: 'silent' });+await server.listen();+const serverUrl = `http://127.0.0.1:${server.httpServer.address().port}`;+const browser = await chromium.launch({ executablePath: CHROME, headless: true, args: ['--enable-webgl', '--ignore-gpu-blocklist', '--disable-renderer-backgrounding'] });+const timings = {};+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.renderProgressiveCheckpoints, null, { timeout: 180000 });+ for (const id of sceneIds) {+ const isRecipe = id.endsWith('.json');+ const source = isRecipe ? JSON.parse(await readFile(id, 'utf8')) : id;+ const name = isRecipe ? recipeName(id) : id;+ if (argumentsGiven.includes('--still')) {+ const still = await page.evaluate(([scene, options]) => (typeof scene === 'string' ? window.renderReferencePathTraced(scene, options) : window.renderRecipePathTraced(scene, options)), [source, { ...baseOptions, samples: 64, backend: 'gpu' }]);+ await writeFile(`${outputDirectory}/${name}@still-64.png`, Buffer.from(still.image, 'base64'));+ timings[`${name}:still-64`] = { seconds: Number(still.report.seconds.toFixed(2)), tracedSeconds: Number(still.report.tracedSeconds.toFixed(2)), compileSeconds: still.report.gpuCompileSeconds };+ console.log(name, 'still-64', JSON.stringify(timings[`${name}:still-64`]));+ }+ for (const variant of variantNames) {+ const options = { ...baseOptions, ...VARIANT_OPTIONS[variant] };+ const { results, report } = await page.evaluate(([scene, tracerOptions, counts]) => window.renderProgressiveCheckpoints(scene, tracerOptions, counts), [source, options, checkpoints]);+ for (const result of results) {+ await writeFile(`${outputDirectory}/${name}@${variant}-${result.samples}.png`, Buffer.from(result.image, 'base64'));+ timings[`${name}:${variant}:${result.samples}`] = { stepSeconds: Number(result.stepSeconds.toFixed(3)), snapshotSeconds: Number(result.snapshotSeconds.toFixed(3)), setupSeconds: Number(report.setupSeconds.toFixed(2)), compileSeconds: Number(report.compileSeconds.toFixed(2)) };+ }+ console.log(name, variant, results.map(result => `${result.samples}:${result.stepSeconds.toFixed(2)}s`).join(' '));+ if (frameCount > 0) {+ const frame = await page.evaluate(([scene, tracerOptions, frames]) => window.measureProgressiveFrameTime(scene, tracerOptions, frames), [source, options, frameCount]);+ timings[`${name}:${variant}:frame`] = frame;+ console.log(name, variant, 'frame', JSON.stringify(frame));+ }+ }+ }+} finally {+ await browser.close();+ await server.close();+}+let previous = {};+try {+ previous = JSON.parse(await readFile(`${outputDirectory}/timings.json`, 'utf8'));+} catch {+ // first run in this directory+}+await writeFile(`${outputDirectory}/timings.json`, JSON.stringify({ ...previous, ...timings }, null, 1));diff --git a/bench/reference/progressive-score.py b/bench/reference/progressive-score.pynew file mode 100644index 00000000..0984490e--- /dev/null+++ b/bench/reference/progressive-score.py@@ -0,0 +1,56 @@+# FLIP of path tracer images against the Cycles renders: the images of progressive-compare.mjs (<scene>@<label>.png) and of+# gpu-compare.mjs (<scene>-pathtracer-gpu.png).+# uv run --no-project --python 3.12 --with flip-evaluator --with pillow --with numpy python -I bench/reference/progressive-score.py <directory> --cycles <directory with <scene>-cycles.npy>+# Prints one line per image and writes progressive-score.json beside the images.+import json+import sys+from pathlib import Path++import flip_evaluator+import numpy as np+from PIL import Image++GAMMA = 2.2+++def load_png(path):+ return np.asarray(Image.open(path).convert("RGB"), dtype=np.float32) / 255+++def tone_map_like_67js(linear):+ color = np.maximum(linear, 0)+ mapped = np.clip((color * (2.51 * color + 0.03)) / (color * (2.43 * color + 0.59) + 0.14), 0, 1)+ return mapped ** (1 / GAMMA)+++def flip_score(reference, test):+ _, mean_error, _ = flip_evaluator.evaluate(reference.astype(np.float32), test.astype(np.float32), "LDR", inputsRGB=True)+ return round(float(mean_error), 4)+++def split_name(stem):+ if "@" in stem:+ scene_id, label = stem.split("@", 1)+ return scene_id, label+ return stem.removesuffix("-pathtracer-gpu"), "gpu"+++def main():+ directory = Path(sys.argv[1])+ cycles_directory = Path(sys.argv[sys.argv.index("--cycles") + 1])+ cycles_cache = {}+ scores = {}+ for path in sorted(directory.glob("*.png")):+ scene_id, label = split_name(path.stem)+ cycles_path = cycles_directory / f"{scene_id}-cycles.npy"+ if not cycles_path.exists():+ continue+ if scene_id not in cycles_cache:+ cycles_cache[scene_id] = tone_map_like_67js(np.load(cycles_path))+ score = flip_score(cycles_cache[scene_id], load_png(path))+ scores[f"{scene_id}:{label}"] = score+ print(f"{scene_id:<20}{label:<16}{score:.4f}")+ (directory / "progressive-score.json").write_text(json.dumps(scores, indent=1) + "\n", encoding="utf-8")+++main()diff --git a/changes/623.md b/changes/623.mdnew file mode 100644index 00000000..2a2fe06c--- /dev/null+++ b/changes/623.md@@ -0,0 +1 @@+- `67js/addons/path-tracer-progressive` draws the GPU path tracer one sample per pixel per step, starting at a quarter of the size, and `samplingPattern: "blue-noise-first"` and `lightSelection: "importance"` are new GPU options, both off by default (#623)diff --git a/changes/623.size.md b/changes/623.size.mdnew file mode 100644index 00000000..34b41b4f--- /dev/null+++ b/changes/623.size.md@@ -0,0 +1,2 @@+- entry:path-tracer+lazy: the blue noise first sample and the importance light choice are new GPU kernel paths plus the tile maker (loaded only on demand); the core bundle does not grow.+- entry:light-bake-gpu: the shared GPU kernel text carries the two new options (compiled out unless asked); 19 bytes over the allowance.diff --git a/docs/path-tracer.md b/docs/path-tracer.mdindex d641c14e..ded5acac 100644--- a/docs/path-tracer.md+++ b/docs/path-tracer.md@@ -127,3 +127,23 @@ OIDN's output is the same bytes run to run on one machine (the CPU device, two r`npm run reference -- --path-tracer [--samples 32]` draws each reference scene with the tracer and scores it against the Cycles render of the same scene with FLIP (`bench/reference/path-tracer.json`). It leaves `baseline.json` and `images/` alone, and it needs the Cycles renders of an earlier `npm run reference`.Scores and times are in the table of `bench/reference/README.md`. Cycles used 256 samples with a denoiser, and the tracer 32 samples with its own filter, so the remaining gap is partly noise and partly the differences listed above.++## Progressive preview, blue noise and light choice (issue #623)++`67js/addons/path-tracer-progressive` (`createProgressiveTracer(scene, options)`) draws the GPU tracer one sample per pixel per `step()`. The first two steps draw at a quarter and a half of the size, one sample each, then every step adds a sample at full size; `update({ camera, parts, environment })` starts the image over, and `snapshot()` reads the image back (filtered unless `denoise` is off). It shares the WebGL2 context of the GPU still, so one render at a time owns it. The core bundle does not carry it.++Two GPU options, both off by default, so the default pixels do not change (the CPU reference rejects them):++- `samplingPattern: "blue-noise-first"` uses a 64 by 64 void-and-cluster tile (`src/path-tracer-blue-noise.js`, same bytes for the same seed) for the pixel jitter and the first two random numbers of sample 0, as `SAMPLING_PATTERN_BLUE_NOISE_FIRST` does in Cycles. Later samples are random.+- `lightSelection: "importance"` replaces the uniform light draw past the first hit: the sun and the sky keep their uniform share, and the local lights split theirs by power over squared distance, surface cosine and spot cone, mixed 10 percent with a uniform part. It ignores shadows and is a flat sum over the lights, not the light tree of Conty and Kulla (Cycles `light/tree.h`) nor ReSTIR; a scene with one local light or none draws the same pixels as before.++Measured on the RTX 3060, 960 by 540, FLIP against Cycles of the denoised image, machine shared with other jobs (times are ranges of repeated runs: GPU steps only, without the first step's kernel compile and without the read back, which took 1.8 to 2.6 s with the filter):++| Scene | Still, 64 samples | Random, 64 steps | Importance, 32 steps | Importance, 64 steps |+| --- | --- | --- | --- | --- |+| `lit-interior` | 0.0769 | 0.0769 in 2.2 s | 0.0671 in 1.3 s | 0.0572 in 2.6 s |+| `spot-area` | 0.0262 | 0.0262 in 0.9 s | 0.0274 in 0.5 s | 0.0224 in 0.9 s |++The still of `spot-area` took 60.5 s including 29.9 s of kernel compile. Blue noise alone scored the same as random within 0.0003 at every count on both scenes (it helps the 1 sample image only by 0.001 to 0.002). Median frame time of one sample per pixel, 200 frames, 960 by 540: demo room 69 ms (67 to 71) random and 71 to 73 ms with both options; `lit-interior` 32 ms; `spot-area` 14 ms.++Light choice at equal samples (16 and 64) on the 16 path tracer scenes: 12 scenes have one local light or none and are identical. Better: `lit-interior` 0.0769 to 0.0572 at 64 (0.1318 to 0.0884 at 16), `spot-area` 0.0262 to 0.0224, `light-linking` 0.0291 to 0.0273 at 16. Not better: `brick-room` 0.2587 to 0.2686 at 64 and `light-linking` 0.0213 to 0.0217 at 64. In `brick-room` both choices score worse at 64 than at 16 (0.2491 and 0.2499 at 16), so the converged image is further from Cycles and the score cannot judge the noise; the criterion "no worse on every scene" is therefore not met by this measure, which is why the option stays off. `bench/reference/progressive-compare.mjs` and `progressive-score.py` made the numbers.diff --git a/package.json b/package.jsonindex eb6c8d41..c33edd98 100644--- a/package.json+++ b/package.json@@ -85,6 +85,7 @@"./addons/ground-bounce": "./build/ground-bounce.js","./addons/floor-sky-visibility": "./build/floor-sky-visibility.js","./addons/path-tracer": "./build/path-tracer.js",+ "./addons/path-tracer-progressive": "./build/path-tracer-progressive.js","./addons/texture-set": "./build/texture-set.js","./addons/agent-scene": "./build/agent-scene.js","./addons/agent-asset": "./build/agent-asset.js",diff --git a/src/path-tracer-blue-noise.js b/src/path-tracer-blue-noise.jsnew file mode 100644index 00000000..56bf7938--- /dev/null+++ b/src/path-tracer-blue-noise.js@@ -0,0 +1,120 @@+// Blue noise for the first sample of every pixel (the idea of SAMPLING_PATTERN_BLUE_NOISE_FIRST in Cycles). A tile of+// BLUE_NOISE_SIZE by BLUE_NOISE_SIZE values is made with the void and cluster method (Ulichney 1993): the values of+// neighbouring pixels are far apart, so the error of a one sample image has no low frequency blotches. Four channels+// are four independent tiles. The tile depends only on the seed, so the same seed gives the same bytes.++export const BLUE_NOISE_SIZE = 64;+export const BLUE_NOISE_CHANNELS = 4;++const TILE_PIXELS = BLUE_NOISE_SIZE * BLUE_NOISE_SIZE;+const GAUSSIAN_SIGMA = 1.5;+const KERNEL_RADIUS = 9;+const INITIAL_FRACTION = 0.1;++function createRandomStream(seed) {+ let state = (seed >>> 0) || 1;+ return () => {+ state = (Math.imul(state, 747796405) + 2891336453) >>> 0;+ let word = Math.imul(((state >>> ((state >>> 28) + 4)) ^ state) >>> 0, 277803737) >>> 0;+ word = ((word >>> 22) ^ word) >>> 0;+ return (word >>> 8) / 16777216;+ };+}++function createKernel() {+ const side = KERNEL_RADIUS * 2 + 1;+ const kernel = new Float64Array(side * side);+ for (let dy = -KERNEL_RADIUS; dy <= KERNEL_RADIUS; dy++) {+ for (let dx = -KERNEL_RADIUS; dx <= KERNEL_RADIUS; dx++) {+ kernel[(dy + KERNEL_RADIUS) * side + dx + KERNEL_RADIUS] = Math.exp(-(dx * dx + dy * dy) / (2 * GAUSSIAN_SIGMA * GAUSSIAN_SIGMA));+ }+ }+ return kernel;+}++const KERNEL = createKernel();++// Adds (sign 1) or removes (sign -1) the energy of one set pixel, wrapping around the tile edges.+function addEnergy(energy, pixel, sign) {+ const centerX = pixel % BLUE_NOISE_SIZE;+ const centerY = (pixel - centerX) / BLUE_NOISE_SIZE;+ const side = KERNEL_RADIUS * 2 + 1;+ for (let dy = -KERNEL_RADIUS; dy <= KERNEL_RADIUS; dy++) {+ const row = ((centerY + dy + BLUE_NOISE_SIZE) % BLUE_NOISE_SIZE) * BLUE_NOISE_SIZE;+ for (let dx = -KERNEL_RADIUS; dx <= KERNEL_RADIUS; dx++) {+ energy[row + ((centerX + dx + BLUE_NOISE_SIZE) % BLUE_NOISE_SIZE)] += sign * KERNEL[(dy + KERNEL_RADIUS) * side + dx + KERNEL_RADIUS];+ }+ }+}++// The pixel with the most (or least) energy among the pixels whose set state equals `wantSet`.+function extremePixel(energy, isSet, wantSet, wantMaximum) {+ let best = -1;+ let bestEnergy = wantMaximum ? -Infinity : Infinity;+ for (let pixel = 0; pixel < TILE_PIXELS; pixel++) {+ if ((isSet[pixel] === 1) !== wantSet) continue;+ const value = energy[pixel];+ if (wantMaximum ? value > bestEnergy : value < bestEnergy) {+ bestEnergy = value;+ best = pixel;+ }+ }+ return best;+}++function createSingleChannel(seed) {+ const nextRandom = createRandomStream(seed);+ const isSet = new Uint8Array(TILE_PIXELS);+ const energy = new Float64Array(TILE_PIXELS);+ const order = Array.from({ length: TILE_PIXELS }, (_, index) => index);+ for (let index = TILE_PIXELS - 1; index > 0; index--) {+ const swapWith = Math.floor(nextRandom() * (index + 1));+ [order[index], order[swapWith]] = [order[swapWith], order[index]];+ }+ const initialCount = Math.round(TILE_PIXELS * INITIAL_FRACTION);+ for (let index = 0; index < initialCount; index++) {+ isSet[order[index]] = 1;+ addEnergy(energy, order[index], 1);+ }+ // Relax the first pattern: move the tightest cluster pixel into the largest void until they are the same pixel.+ for (let step = 0; step < TILE_PIXELS; step++) {+ const tightest = extremePixel(energy, isSet, true, true);+ isSet[tightest] = 0;+ addEnergy(energy, tightest, -1);+ const largestVoid = extremePixel(energy, isSet, false, false);+ isSet[largestVoid] = 1;+ addEnergy(energy, largestVoid, 1);+ if (largestVoid === tightest) break;+ }+ const rank = new Uint16Array(TILE_PIXELS);+ const prototype = isSet.slice();+ const prototypeEnergy = energy.slice();+ // Phase one: take the pixels of the pattern away one by one, tightest cluster first; the last one removed is rank 0.+ for (let remaining = initialCount - 1; remaining >= 0; remaining--) {+ const tightest = extremePixel(energy, isSet, true, true);+ isSet[tightest] = 0;+ addEnergy(energy, tightest, -1);+ rank[tightest] = remaining;+ }+ isSet.set(prototype);+ energy.set(prototypeEnergy);+ // Phase two and three: add each next pixel into the largest void of the set ones.++ for (let placed = initialCount; placed < TILE_PIXELS; placed++) {+ const pixel = extremePixel(energy, isSet, false, false);+ isSet[pixel] = 1;+ addEnergy(energy, pixel, 1);+ rank[pixel] = placed;+ }+ return rank;+}++/** RGBA8 bytes of a BLUE_NOISE_SIZE square tile, four blue noise channels; the same seed gives the same bytes. */+export function createBlueNoiseTile(seed = 0) {+ const bytes = new Uint8Array(TILE_PIXELS * BLUE_NOISE_CHANNELS);+ for (let channel = 0; channel < BLUE_NOISE_CHANNELS; channel++) {+ const rank = createSingleChannel(Math.imul((seed >>> 0) + 1, 0x9e3779b1) + channel * 0x632be5ab);+ for (let pixel = 0; pixel < TILE_PIXELS; pixel++) bytes[pixel * BLUE_NOISE_CHANNELS + channel] = rank[pixel] >> 4;+ }+ return bytes;+}diff --git a/src/path-tracer-gpu-shader.js b/src/path-tracer-gpu-shader.jsindex 561d721a..e45611af 100644--- a/src/path-tracer-gpu-shader.js+++ b/src/path-tracer-gpu-shader.js@@ -26,6 +26,12 @@ const CAMERA_MAIN = `void main() {if (uHasSun) sunFrame = buildFrame(uSunDirection);float jitterX = hashJitter(pixel, sampleIndex, uSeed, 1u);float jitterY = hashJitter(pixel, sampleIndex, uSeed, 2u);+ if (BLUE_NOISE_FIRST && sampleIndex == 0u) {+ blueSample = blueNoiseAt(pixelCoordinate);+ jitterX = blueSample.x;+ jitterY = blueSample.y;+ blueDrawsUsed = 0;+ }float screenX = ((float(pixelCoordinate.x) + jitterX) / float(uSize.x) * 2.0 - 1.0) * uHalfSize.x;float screenY = (1.0 - (float(row) + jitterY) / float(uSize.y) * 2.0) * uHalfSize.y;vec3 rayDirection = normalize(uForward + uRight * screenX + uAbove * screenY);@@ -55,7 +61,7 @@ const TEXEL_MAIN = `void main() {if (texelIndex >= uTexelCount) rayDirection = normal;tracePath(origin, rayDirection, pixel, sampleIndex);`;-const buildSource = (hasSubsurface, texelBake) => `#version 300 es+const buildSource = (hasSubsurface, texelBake, blueNoise, lightImportance) => `#version 300 esprecision highp float;precision highp int;precision highp sampler2D;@@ -68,6 +74,8 @@ const int TRIANGLE_TEXELS = ${TRIANGLE_TEXELS};const int MATERIAL_TEXELS = ${MATERIAL_TEXELS};const int LIGHT_TEXELS = ${LIGHT_TEXELS};const int STACK_SIZE = 128;+const bool BLUE_NOISE_FIRST = ${blueNoise};+const bool IMPORTANT_LIGHTS = ${lightImportance};const int ALPHA_STEP_LIMIT = 8;const int RUSSIAN_ROULETTE_START = 3;const float MINIMUM_ALPHA = 1e-3;@@ -82,6 +90,7 @@ uniform sampler2D uAtlas;uniform sampler2D uEnvironment;uniform sampler2D uEnvironmentRows;uniform sampler2D uEnvironmentPixels;+uniform sampler2D uBlueNoise;uniform vec3 uEye;uniform vec3 uForward;@@ -136,7 +145,20 @@ void resetRandom(uint pixel, uint sampleIndex, uint seed) {rngState = mixed == 0u ? 1u : mixed;}+// Blue noise tile of the first sample of a pixel: its first two values replace the first two random draws of the path.+vec4 blueSample = vec4(0.0);+int blueDrawsUsed = 2;++vec4 blueNoiseAt(ivec2 pixelCoordinate) {+ vec4 bytes = round(texelFetch(uBlueNoise, pixelCoordinate & 63, 0) * 255.0);+ return (bytes + 0.5) / 256.0;+}+float nextRandom() {+ if (BLUE_NOISE_FIRST && blueDrawsUsed < 2) {+ blueDrawsUsed++;+ return blueDrawsUsed == 1 ? blueSample.z : blueSample.w;+ }rngState = rngState * 747796405u + 2891336453u;uint word = ((rngState >> ((rngState >> 28) + 4u)) ^ rngState) * 277803737u;word = (word >> 22) ^ word;@@ -931,6 +953,66 @@ void sampleEmitter(int depth, vec3 normal, int emitter) {addDirect(depth, normal, distance, weightMode);}+/**+ * A cheap estimate of the light one local light brings to the hit point: its power over the squared distance, the+ * cosine at the surface and, for a spot light, whether the point is inside the cone. It ignores shadows. The estimate only+ * steers the choice; the chance of the choice is divided out, so the image stays the same in expectation.+ */+float lightImportance(int index, vec3 normal) {+ int base = index * LIGHT_TEXELS;+ vec4 positionType = fetchData(uLights, base);+ vec4 colorBit = fetchData(uLights, base + 1);+ vec4 directionRadius = fetchData(uLights, base + 2);+ vec4 acrossCone = fetchData(uLights, base + 3);+ vec4 intensityRange = fetchData(uLights, base + 5);+ vec3 delta = positionType.xyz - hitPoint;+ float distanceSquared = max(dot(delta, delta), 1e-8);+ vec3 toLight = delta * inversesqrt(distanceSquared);+ float cosine = 0.1 + max(dot(normal, toLight), 0.0);+ float power = luminance(colorBit.rgb) * intensityRange.x;+ int type = int(positionType.w);+ if (type == 2) return power * max(-dot(directionRadius.xyz, toLight), 0.0) * cosine / distanceSquared;+ float coneFactor = 1.0;+ if (type == 1 && -dot(directionRadius.xyz, toLight) - acrossCone.w <= 0.0) coneFactor = 0.0;+ return power * coneFactor * cosine * pointLightFalloff(distanceSquared, intensityRange.y, directionRadius.w);+}++/**+ * Draws one emitter. The sun and the sky keep their uniform share; the local lights split their shares by importance,+ * mixed with a uniform part so no light that could contribute has no chance. With one local light, or none, this is the+ * uniform choice with the same random number.+ */+int chooseEmitterByImportance(vec3 normal, out float probability) {+ float uniformShare = 1.0 / float(uEmitterCount);+ float draw = nextRandom();+ int firstLightEmitter = uHasSun ? 1 : 0;+ float total = 0.0;+ if (uLightCount > 1) {+ for (int index = 0; index < uLightCount; index++) total += lightImportance(index, normal);+ }+ if (!(total > 0.0)) {+ probability = uniformShare;+ return min(int(floor(draw * float(uEmitterCount))), uEmitterCount - 1);+ }+ float lightsShare = float(uLightCount) * uniformShare;+ float cumulative = 0.0;+ int chosen = uEmitterCount - 1;+ probability = uniformShare;+ for (int emitter = 0; emitter < uEmitterCount; emitter++) {+ int index = emitter - firstLightEmitter;+ float share = uniformShare;+ if (index >= 0 && index < uLightCount) share = lightsShare * (0.9 * lightImportance(index, normal) / total + 0.1 / float(uLightCount));+ cumulative += share;+ if (draw < cumulative) {+ chosen = emitter;+ probability = share;+ break;+ }+ probability = share;+ }+ return chosen;+}+/** The first hit lights from every emitter; later hits draw one, and divide by the chance of drawing it. */void addLights(int depth, vec3 normal) {if (depth == 0 || uEmitterCount <= 1) {@@ -941,6 +1023,12 @@ void addLights(int depth, vec3 normal) {}return;}+ if (IMPORTANT_LIGHTS) {+ int chosen = chooseEmitterByImportance(normal, selectionProbability);+ sampleEmitter(depth, normal, chosen);+ flushShadowRay();+ return;+ }selectionProbability = 1.0 / float(uEmitterCount);sampleEmitter(depth, normal, int(floor(nextRandom() * float(uEmitterCount))));flushShadowRay();@@ -1087,8 +1175,8 @@ ${texelBake ? TEXEL_MAIN : CAMERA_MAIN}* The kernel; `hasSubsurface` compiles in the random walk, which only scenes with a subsurface part need. `texelBake`* starts every path at a lightmap texel instead of the camera (see light-bake-gpu.js).*/-export function buildFragmentSource({ hasSubsurface = false, texelBake = false } = {}) {- return buildSource(hasSubsurface, texelBake);+export function buildFragmentSource({ hasSubsurface = false, texelBake = false, blueNoise = false, lightImportance = false } = {}) {+ return buildSource(hasSubsurface, texelBake, blueNoise, lightImportance);}export const FRAGMENT_SOURCE = buildFragmentSource();diff --git a/src/path-tracer-gpu.js b/src/path-tracer-gpu.jsindex 7962d618..12234761 100644--- a/src/path-tracer-gpu.js+++ b/src/path-tracer-gpu.js@@ -61,7 +61,17 @@ function createTexture(gl, unit, internalFormat, format, type, width, height, dareturn texture;}-function uploadScene(gl, program, scene) {+/**+ * The options with the blue noise tile of the seed added when the sampling pattern needs it. The tile maker loads on+ * demand, so a render that draws random samples (and the light bake) does not carry it.+ */+export async function withBlueNoiseTile(options) {+ if (options.samplingPattern !== 'blue-noise-first') return options;+ const { BLUE_NOISE_SIZE, createBlueNoiseTile } = await import('./path-tracer-blue-noise.js');+ return { ...options, blueNoiseTile: { size: BLUE_NOISE_SIZE, bytes: createBlueNoiseTile(options.seed ?? 0) } };+}++export function uploadScene(gl, program, scene, options = {}) {const atlas = packAtlas(scene.materials);const lights = packLights(scene.lights);const environment = packEnvironment(scene.environment);@@ -86,10 +96,14 @@ function uploadScene(gl, program, scene) {createTexture(gl, unit, gl.R32F, gl.RED, gl.FLOAT, table.width, table.height, table.data);bind(name);}+ if (options.blueNoiseTile) {+ createTexture(gl, unit, gl.RGBA8, gl.RGBA, gl.UNSIGNED_BYTE, options.blueNoiseTile.size, options.blueNoiseTile.size, options.blueNoiseTile.bytes);+ bind('uBlueNoise');+ }return { lightCount: lights.count, nextUnit: unit };}-function setSceneUniforms(gl, program, scene, camera, options, lightCount) {+export function setSceneUniforms(gl, program, scene, camera, options, lightCount) {const location = name => gl.getUniformLocation(program, name);const { sun } = scene.lights;const { environment, volume } = scene;@@ -120,7 +134,7 @@ function setSceneUniforms(gl, program, scene, camera, options, lightCount) {gl.uniform3f(location('uVolumeShape'), volume?.density ?? 0, volume?.anisotropy ?? 0, volume?.maxDistance ?? 0);}-function createTargets(gl, width, height) {+export function createTargets(gl, width, height) {const framebuffer = gl.createFramebuffer();resources.push(framebuffer);gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);@@ -143,14 +157,14 @@ function createTargets(gl, width, height) {return attachments;}-function readTarget(gl, index, width, height) {+export function readTarget(gl, index, width, height) {const pixels = new Float32Array(width * height * 4);gl.readBuffer(gl.COLOR_ATTACHMENT0 + index);gl.readPixels(0, 0, width, height, gl.RGBA, gl.FLOAT, pixels);return pixels;}-function averageBuffers(gl, width, height, samples) {+export function averageBuffers(gl, width, height, samples) {const pixelCount = width * height;const radiance = readTarget(gl, 0, width, height);const albedo = readTarget(gl, 1, width, height);@@ -180,7 +194,7 @@ const nextTask = () => new Promise(resolve => setTimeout(resolve, 0));* (color, albedo, normal, depth, variance, samplesTaken) plus a `gpu` note. `noiseThreshold` is not used: every pixel* draws all its samples. `shouldStop` throws when the time limit passes.*/-function getContext() {+export function getContext() {if (session) return session;const canvas = createCanvas();const gl = canvas?.getContext('webgl2', { antialias: false, depth: false, stencil: false, alpha: false, powerPreference: 'high-performance' });@@ -192,19 +206,21 @@ function getContext() {}/** The kernel for a scene, compiled once per variant: a scene with a subsurface part gets the random walk, any other scene the plain kernel. */-function getKernel(hasSubsurface, texelBake = false) {+export function getKernel(hasSubsurface, texelBake = false, options = {}) {const { gl, programs } = getContext();- const key = `${hasSubsurface}:${texelBake}`;+ const blueNoise = options.samplingPattern === 'blue-noise-first';+ const lightImportance = options.lightSelection === 'importance';+ const key = `${hasSubsurface}:${texelBake}:${blueNoise}:${lightImportance}`;if (!programs.has(key)) {const started = performance.now();- const program = createProgram(gl, { hasSubsurface, texelBake });+ const program = createProgram(gl, { hasSubsurface, texelBake, blueNoise, lightImportance });gl.getProgramParameter(program, gl.LINK_STATUS);programs.set(key, { program, compileSeconds: (performance.now() - started) / 1000 });}return programs.get(key);}-function releaseResources(gl) {+export function releaseResources(gl) {for (const resource of resources) {if (resource instanceof WebGLFramebuffer) gl.deleteFramebuffer(resource);else gl.deleteTexture(resource);@@ -212,45 +228,51 @@ function releaseResources(gl) {resources.length = 0;}-/** Draws every sample of every pixel in bands that take about DRAW_SECONDS_TARGET each, giving the page a turn now and then. */-async function drawSamples(gl, program, width, height, samples, shouldStop) {+/** The band size and the last turn given to the page, kept from one sample to the next. */+export function createBandState(height) {+ return { bandRows: Math.min(height, 32), lastYield: performance.now() };+}++/** Draws one sample of every pixel in bands that take about DRAW_SECONDS_TARGET each, giving the page a turn now and then. */+export async function drawOneSample(gl, program, width, height, sample, band, shouldStop) {gl.viewport(0, 0, width, height);gl.enable(gl.BLEND);gl.blendFunc(gl.ONE, gl.ONE);gl.enable(gl.SCISSOR_TEST);- const sampleLocation = gl.getUniformLocation(program, 'uSample');- let bandRows = Math.min(height, 32);- let lastYield = performance.now();- for (let sample = 0; sample < samples; sample++) {- gl.uniform1i(sampleLocation, sample);- let rowStart = 0;- while (rowStart < height) {- const rows = Math.min(bandRows, height - rowStart);- const firstRow = rowStart;- rowStart += rows;- const started = performance.now();- gl.scissor(0, firstRow, width, rows);- gl.drawArrays(gl.TRIANGLES, 0, 3);- gl.finish();- const seconds = (performance.now() - started) / 1000;- const scale = Math.min(2, DRAW_SECONDS_TARGET / Math.max(seconds, 1e-4));- bandRows = Math.max(MINIMUM_BAND_ROWS, Math.min(height, Math.round(rows * scale)));- shouldStop();- if ((performance.now() - lastYield) / 1000 > YIELD_SECONDS) {- await nextTask();- lastYield = performance.now();- }+ gl.uniform1i(gl.getUniformLocation(program, 'uSample'), sample);+ let rowStart = 0;+ while (rowStart < height) {+ const rows = Math.min(band.bandRows, height - rowStart);+ const firstRow = rowStart;+ rowStart += rows;+ const started = performance.now();+ gl.scissor(0, firstRow, width, rows);+ gl.drawArrays(gl.TRIANGLES, 0, 3);+ gl.finish();+ const seconds = (performance.now() - started) / 1000;+ const scale = Math.min(2, DRAW_SECONDS_TARGET / Math.max(seconds, 1e-4));+ band.bandRows = Math.max(MINIMUM_BAND_ROWS, Math.min(height, Math.round(rows * scale)));+ shouldStop();+ if ((performance.now() - band.lastYield) / 1000 > YIELD_SECONDS) {+ await nextTask();+ band.lastYield = performance.now();}}gl.disable(gl.SCISSOR_TEST);}-export async function traceOnGpu(scene, camera, options, shouldStop) {+async function drawSamples(gl, program, width, height, samples, shouldStop) {+ const band = createBandState(height);+ for (let sample = 0; sample < samples; sample++) await drawOneSample(gl, program, width, height, sample, band, shouldStop);+}++export async function traceOnGpu(scene, camera, rawOptions, shouldStop) {+ const options = await withBlueNoiseTile(rawOptions);const { gl, gpu } = getContext();- const { program, compileSeconds } = getKernel(sceneUsesSubsurface(scene.materials));+ const { program, compileSeconds } = getKernel(sceneUsesSubsurface(scene.materials), false, options);const { width, height, samples } = options;gl.useProgram(program);- const { lightCount } = uploadScene(gl, program, scene);+ const { lightCount } = uploadScene(gl, program, scene, options);setSceneUniforms(gl, program, scene, camera, options, lightCount);createTargets(gl, width, height);await drawSamples(gl, program, width, height, samples, shouldStop);diff --git a/src/path-tracer-progressive.js b/src/path-tracer-progressive.jsnew file mode 100644index 00000000..af4d67b6--- /dev/null+++ b/src/path-tracer-progressive.js@@ -0,0 +1,132 @@+import { buildTraceScene } from './path-tracer-scene.js';+import { denoiseRadiance } from './path-tracer-denoise.js';+import { createCamera, normalizePathTracerOptions, toneMapToRgba8 } from './path-tracer.js';+import {+ averageBuffers, createBandState, createTargets, drawOneSample, getContext, getKernel, releaseResources, setSceneUniforms, uploadScene, withBlueNoiseTile,+} from './path-tracer-gpu.js';+import { sceneUsesSubsurface } from './path-tracer-gpu-pack.js';++// A progressive view of the GPU path tracer (issue #623). `step()` draws one sample for every pixel and adds it to the+// image; while the scene and camera rest the image gets cleaner with each step, and `update()` starts it over. Like the+// Cycles viewport, it starts at a fraction of the resolution (one sample each at 1/4 and 1/2) so the first picture+// appears at once, then draws at full size. It shares the WebGL2 context of `traceOnGpu`, so one render at a time owns it.++const DEFAULT_RESOLUTION_DIVISORS = Object.freeze([4, 2, 1]);++/**+ * `scene` is `{ parts, environment, camera }` as `renderPathTracedImage` takes them. `options` are the same options+ * (`width`, `height`, `bounces`, `seed`, `samplingPattern`, `lightSelection`, `denoise`, `viewTransform`); `samples` and+ * `backend` are not used. `resolutionDivisors` lists the sizes drawn first, one sample each, ending with 1.+ */+export async function createProgressiveTracer(scene, rawOptions = {}, { resolutionDivisors = DEFAULT_RESOLUTION_DIVISORS } = {}) {+ const options = await withBlueNoiseTile(normalizePathTracerOptions({ ...rawOptions, backend: 'gpu', samples: 1, maxSeconds: rawOptions.maxSeconds ?? 3600 }));+ const { gl, gpu } = getContext();+ const { width, height } = options;+ let traceScene = null;+ let program = null;+ let compileSeconds = 0;+ let lightCount = 0;+ let levelIndex = 0;+ let samples = 0;+ let levelFinished = false;+ const synchronizationPixel = new Float32Array(4);+ let band = createBandState(height);+ let disposed = false;++ const levelSize = () => {+ const divisor = resolutionDivisors[levelIndex];+ return { divisor, levelWidth: Math.max(1, Math.ceil(width / divisor)), levelHeight: Math.max(1, Math.ceil(height / divisor)) };+ };++ const loadScene = (parts, environment) => {+ releaseResources(gl);+ traceScene = buildTraceScene(parts, environment);+ const kernel = getKernel(sceneUsesSubsurface(traceScene.materials), false, options);+ program = kernel.program;+ compileSeconds = kernel.compileSeconds;+ gl.useProgram(program);+ lightCount = uploadScene(gl, program, traceScene, options).lightCount;+ createTargets(gl, width, height);+ };++ const loadCamera = camera => {+ const view = createCamera(camera, width, height);+ setSceneUniforms(gl, program, traceScene, view, options, lightCount);+ };++ const clearTargets = () => {+ for (let index = 0; index < 4; index++) gl.clearBufferfv(gl.COLOR, index, [0, 0, 0, 0]);+ };++ const setLevelUniforms = () => {+ const { levelWidth, levelHeight } = levelSize();+ gl.uniform2i(gl.getUniformLocation(program, 'uSize'), levelWidth, levelHeight);+ };++ const startOver = () => {+ levelIndex = 0;+ samples = 0;+ levelFinished = false;+ band = createBandState(height);+ setLevelUniforms();+ clearTargets();+ };++ loadScene(scene.parts, scene.environment ?? {});+ loadCamera(scene.camera);+ startOver();++ return {+ gpu,+ get compileSeconds() { return compileSeconds; },+ get samples() { return samples; },+ get level() { return { ...levelSize(), final: levelIndex === resolutionDivisors.length - 1 }; },++ /** A new camera, and with `parts` a new scene: the image starts over at the reduced size. */+ update({ camera, parts, environment }) {+ if (parts) loadScene(parts, environment ?? {});+ if (camera) loadCamera(camera);+ startOver();+ },++ /** Draws one more sample of every pixel; resolves with the size drawn and how many samples that image holds. */+ async step(shouldStop = () => {}) {+ if (disposed) throw new Error('This progressive tracer was disposed.');+ if (levelFinished && levelIndex < resolutionDivisors.length - 1) {+ levelIndex++;+ samples = 0;+ setLevelUniforms();+ clearTargets();+ }+ const { levelWidth, levelHeight, divisor } = levelSize();+ await drawOneSample(gl, program, levelWidth, levelHeight, samples, band, shouldStop);+ // gl.finish() does not wait in every browser; reading one value back does, so a step is over when this returns.+ gl.readBuffer(gl.COLOR_ATTACHMENT0);+ gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.FLOAT, synchronizationPixel);+ samples++;+ levelFinished = true;+ return { width: levelWidth, height: levelHeight, divisor, samples };+ },++ /**+ * The image as it stands after the last `step()`, at the size of that step: linear colour (filtered unless+ * `denoise` is off), tone mapped bytes and the samples each pixel holds. It reads the targets back, so it is not free.+ */+ snapshot() {+ const { levelWidth, levelHeight } = levelSize();+ const buffers = averageBuffers(gl, levelWidth, levelHeight, samples);+ for (let pixel = 0; pixel < levelWidth * levelHeight; pixel++) {+ const length = Math.hypot(buffers.normal[pixel * 3], buffers.normal[pixel * 3 + 1], buffers.normal[pixel * 3 + 2]) || 1;+ for (let channel = 0; channel < 3; channel++) buffers.normal[pixel * 3 + channel] /= length;+ }+ const { samplesTaken, ...features } = buffers;+ const linear = options.denoiser === 'wavelet' ? denoiseRadiance({ width: levelWidth, height: levelHeight, ...features }) : buffers.color;+ return { width: levelWidth, height: levelHeight, linear, rgba: toneMapToRgba8(linear, levelWidth, levelHeight, options), samples, features };+ },++ dispose() {+ disposed = true;+ releaseResources(gl);+ },+ };+}diff --git a/src/path-tracer.js b/src/path-tracer.jsindex 4b7a44f7..aff887ca 100644--- a/src/path-tracer.js+++ b/src/path-tracer.js@@ -71,6 +71,8 @@ function boundedNumber(options, name) {// Kept equal to PATH_TRACER_DENOISERS in path-tracer-capabilities.js by tests/path-tracer-denoise-oidn.test.js; the// capabilities file stays out of this bundle.export const PATH_TRACER_DENOISERS = Object.freeze(['wavelet', 'oidn', 'none']);+export const SAMPLING_PATTERNS = Object.freeze(['random', 'blue-noise-first']);+export const LIGHT_SELECTIONS = Object.freeze(['uniform', 'importance']);/*** `denoise` is true (the wavelet filter) or false (none), or a name: "wavelet", "oidn" or "none". "oidn" is the optional@@ -100,10 +102,17 @@ export function normalizePathTracerOptions(options = {}) {viewTransform: options.viewTransform ?? 'aces',look: options.look ?? null,backend: options.backend ?? 'cpu',+ samplingPattern: options.samplingPattern ?? 'random',+ lightSelection: options.lightSelection ?? 'uniform',};result.denoise = result.denoiser !== 'none';if (!VIEW_TRANSFORM_NAMES.includes(result.viewTransform)) throw new RangeError(`Path tracer viewTransform must be one of ${VIEW_TRANSFORM_NAMES.join(', ')}.`);if (result.backend !== 'cpu' && result.backend !== 'gpu') throw new TypeError('Path tracer backend must be "cpu" or "gpu".');+ if (!SAMPLING_PATTERNS.includes(result.samplingPattern)) throw new RangeError(`Path tracer samplingPattern must be one of ${SAMPLING_PATTERNS.join(', ')}.`);+ if (!LIGHT_SELECTIONS.includes(result.lightSelection)) throw new RangeError(`Path tracer lightSelection must be one of ${LIGHT_SELECTIONS.join(', ')}.`);+ if (result.backend === 'cpu' && (result.samplingPattern !== 'random' || result.lightSelection !== 'uniform')) {+ throw new RangeError('Path tracer samplingPattern "blue-noise-first" and lightSelection "importance" need backend "gpu"; the CPU reference draws random samples and picks lights uniformly.');+ }if (result.width * result.height > MAX_TRACE_PIXELS) throw new RangeError(`A path trace takes at most ${MAX_TRACE_PIXELS} pixels.`);if (result.width * result.height * result.samples * (result.bounces + 1) > MAX_TRACE_RAY_WORK) throw new RangeError('This path trace would need too much ray work: lower the size, samples or bounces.');return result;diff --git a/tests/path-tracer-progressive.test.js b/tests/path-tracer-progressive.test.jsnew file mode 100644index 00000000..2b6bec21--- /dev/null+++ b/tests/path-tracer-progressive.test.js@@ -0,0 +1,67 @@+import assert from 'node:assert/strict';+import test from 'node:test';++import { BLUE_NOISE_CHANNELS, BLUE_NOISE_SIZE, createBlueNoiseTile } from '../src/path-tracer-blue-noise.js';+import { normalizePathTracerOptions } from '../src/path-tracer.js';+import { buildFragmentSource } from '../src/path-tracer-gpu-shader.js';++function boxBlurVariance(valueAt, boxSize) {+ let sum = 0;+ let sumOfSquares = 0;+ for (let y = 0; y < BLUE_NOISE_SIZE; y++) {+ for (let x = 0; x < BLUE_NOISE_SIZE; x++) {+ let blurred = 0;+ for (let offsetY = 0; offsetY < boxSize; offsetY++) {+ for (let offsetX = 0; offsetX < boxSize; offsetX++) {+ blurred += valueAt((x + offsetX) % BLUE_NOISE_SIZE, (y + offsetY) % BLUE_NOISE_SIZE);+ }+ }+ blurred /= boxSize * boxSize;+ sum += blurred;+ sumOfSquares += blurred * blurred;+ }+ }+ const count = BLUE_NOISE_SIZE * BLUE_NOISE_SIZE;+ return sumOfSquares / count - (sum / count) ** 2;+}++test('the blue noise tile is the same bytes for the same seed and differs for another', () => {+ const first = createBlueNoiseTile(7);+ assert.deepEqual(createBlueNoiseTile(7), first);+ assert.notDeepEqual(createBlueNoiseTile(8), first);+ assert.equal(first.length, BLUE_NOISE_SIZE * BLUE_NOISE_SIZE * BLUE_NOISE_CHANNELS);+});++test('every channel of the tile holds each value equally often, and its blurred variance is far below white noise', () => {+ const tile = createBlueNoiseTile(3);+ const pixelCount = BLUE_NOISE_SIZE * BLUE_NOISE_SIZE;+ for (let channel = 0; channel < BLUE_NOISE_CHANNELS; channel++) {+ const counts = new Array(256).fill(0);+ for (let pixel = 0; pixel < pixelCount; pixel++) counts[tile[pixel * BLUE_NOISE_CHANNELS + channel]]++;+ assert.ok(counts.every(count => count === pixelCount / 256), `channel ${channel} is not uniform`);+ const blueVariance = boxBlurVariance((x, y) => tile[(y * BLUE_NOISE_SIZE + x) * BLUE_NOISE_CHANNELS + channel] / 255, 4);+ // A 4 by 4 box of white noise has a variance of 1/12/16 = 0.0052.+ assert.ok(blueVariance < 0.0026, `channel ${channel} blurred variance ${blueVariance}`);+ }+});++test('the options choose the sampling pattern and the light choice, and the CPU reference keeps its defaults', () => {+ const defaults = normalizePathTracerOptions({});+ assert.equal(defaults.samplingPattern, 'random');+ assert.equal(defaults.lightSelection, 'uniform');+ const gpu = normalizePathTracerOptions({ backend: 'gpu', samplingPattern: 'blue-noise-first', lightSelection: 'importance' });+ assert.equal(gpu.samplingPattern, 'blue-noise-first');+ assert.equal(gpu.lightSelection, 'importance');+ assert.throws(() => normalizePathTracerOptions({ samplingPattern: 'blue-noise-first' }), /need backend "gpu"/);+ assert.throws(() => normalizePathTracerOptions({ lightSelection: 'importance' }), /need backend "gpu"/);+ assert.throws(() => normalizePathTracerOptions({ backend: 'gpu', lightSelection: 'tree' }), /lightSelection must be one of/);+});++test('the kernel compiles the new paths only when asked, so the default kernel keeps its pixels', () => {+ const plain = buildFragmentSource();+ assert.ok(plain.includes('const bool BLUE_NOISE_FIRST = false;'));+ assert.ok(plain.includes('const bool IMPORTANT_LIGHTS = false;'));+ const both = buildFragmentSource({ blueNoise: true, lightImportance: true });+ assert.ok(both.includes('const bool BLUE_NOISE_FIRST = true;'));+ assert.ok(both.includes('const bool IMPORTANT_LIGHTS = true;'));+});diff --git a/vite.lib.config.js b/vite.lib.config.jsindex f27944ae..ea058746 100644--- a/vite.lib.config.js+++ b/vite.lib.config.js@@ -74,6 +74,7 @@ export default defineConfig({'ground-bounce': 'src/ground-bounce.js','floor-sky-visibility': 'src/floor-sky-visibility.js','path-tracer': 'src/path-tracer.js',+ 'path-tracer-progressive': 'src/path-tracer-progressive.js','path-tracer-worker': 'src/path-tracer-worker.js','kernel-worker': 'src/kernel-worker.js','texture-set': 'src/texture-set.js',