3D engine that AI agents drive
Real-time WebGPU tracer: output-size history option, per-part motion for moved parts (issue #624)
Nguyen Xuan An7fa19611d040
diff --git a/bench/realtime-tracer/page.js b/bench/realtime-tracer/page.jsindex f8dc2acb..dedfb29a 100644--- a/bench/realtime-tracer/page.js+++ b/bench/realtime-tracer/page.js@@ -43,13 +43,13 @@ window.measureRealtimeFrames = async function measureRealtimeFrames(source, optireturn summary;};-// A resting camera for `seconds`; returns the last frame and how many frames fitted.-window.renderRestingFrame = async function renderRestingFrame(source, options, seconds = 1) {+// A resting camera for `seconds` (or exactly `frameLimit` frames when given); returns the last frame and how many frames fitted.+window.renderRestingFrame = async function renderRestingFrame(source, options, seconds = 1, frameLimit = 0) {const { scene, tracer } = await openTracer(source, options);await tracer.frame(scene.camera, { wait: true }).done;const started = performance.now();let frames = 1;- while (performance.now() - started < seconds * 1000) {+ while (frameLimit ? frames < frameLimit : performance.now() - started < seconds * 1000) {await tracer.frame(scene.camera, { wait: true }).done;frames++;}@@ -70,3 +70,91 @@ window.renderCameraMove = async function renderCameraMove(source, options, { wartracer.dispose();return images;};++function boxPart(halfSize, color) {+ const faces = [+ { normal: [0, 0, 1], u: [1, 0, 0], v: [0, 1, 0] }, { normal: [0, 0, -1], u: [-1, 0, 0], v: [0, 1, 0] },+ { normal: [1, 0, 0], u: [0, 0, -1], v: [0, 1, 0] }, { normal: [-1, 0, 0], u: [0, 0, 1], v: [0, 1, 0] },+ { normal: [0, 1, 0], u: [1, 0, 0], v: [0, 0, -1] }, { normal: [0, -1, 0], u: [1, 0, 0], v: [0, 0, 1] },+ ];+ const positions = [];+ const normals = [];+ const uvs = [];+ const indices = [];+ faces.forEach((face, faceIndex) => {+ for (const [cornerU, cornerV] of [[-1, -1], [1, -1], [1, 1], [-1, 1]]) {+ for (let axis = 0; axis < 3; axis++) positions.push((face.normal[axis] + face.u[axis] * cornerU + face.v[axis] * cornerV) * halfSize);+ normals.push(...face.normal);+ uvs.push((cornerU + 1) / 2, (cornerV + 1) / 2);+ }+ const first = faceIndex * 4;+ indices.push(first, first + 1, first + 2, first, first + 2, first + 3);+ });+ const geometry = { positions: new Float32Array(positions), normals: new Float32Array(normals), uvs: new Float32Array(uvs), indices: new Uint32Array(indices) };+ return { name: 'moving box', geometry, material: { color } };+}++const translationMatrix = ([x, y, z]) => [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1];++function meanAbsoluteDifference(first, second, inside) {+ let sum = 0;+ let count = 0;+ for (let row = 0; row < first.height; row++) {+ for (let column = 0; column < first.width; column++) {+ if (!inside(column + 0.5, row + 0.5)) continue;+ const offset = (row * first.width + column) * 4;+ sum += (Math.abs(first.rgba[offset] - second.rgba[offset]) + Math.abs(first.rgba[offset + 1] - second.rgba[offset + 1]) + Math.abs(first.rgba[offset + 2] - second.rgba[offset + 2])) / 3;+ count++;+ }+ }+ return Number((sum / Math.max(count, 1)).toFixed(3));+}++// A box slides across the frame, one scene update per frame, then the camera rests. The frame is compared with a tracer that+// rested on the final scene for as many frames: `trailError` is the mean absolute difference (8 bit levels) where the box was at the+// start, `frameError` over the whole frame. With the motion followed the trail is as clean as the rest of the frame; with+// `followPartMotion: false` the history stays as it was and the old box shows through.+window.measureMovingPart = async function measureMovingPart(source, options, { moveFrames = 10, restFrames = 12, warmupFrames = 30 } = {}) {+ const { createRealtimeTracer } = await import('/src/path-tracer-realtime.js');+ const { createCamera } = await import('/src/path-tracer.js');+ const { projectToPixel } = await import('/src/path-tracer-reprojection.js');+ const scene = await window.loadTraceSceneFor(source);+ const { eye, target } = scene.camera;+ const toTarget = [0, 1, 2].map(axis => target[axis] - eye[axis]);+ const distance = Math.hypot(...toTarget);+ const forward = toTarget.map(value => value / distance);+ const rightRaw = [-forward[2], 0, forward[0]];+ const right = rightRaw.map(value => value / Math.hypot(...rightRaw));+ const halfSize = distance * 0.05;+ const startAt = [0, 1, 2].map(axis => target[axis] - right[axis] * distance * 0.15);+ const position = step => startAt.map((value, axis) => value + right[axis] * distance * 0.03 * step);+ const withBox = step => [...scene.parts, { ...boxPart(halfSize, [0.9, 0.1, 0.1]), matrix: translationMatrix(position(step)) }];+ const environment = scene.environment;+ const restFrame = async (tracerOptions, slide) => {+ const tracer = await createRealtimeTracer({ ...scene, parts: withBox(slide ? 0 : moveFrames), environment }, tracerOptions);+ for (let frame = 0; frame < warmupFrames; frame++) await tracer.frame(scene.camera, { wait: true }).done;+ for (let step = 1; slide && step <= moveFrames; step++) {+ await tracer.update({ parts: withBox(step), environment });+ await tracer.frame(scene.camera, { wait: true }).done;+ }+ for (let frame = 0; frame < (slide ? restFrames : moveFrames + restFrames); frame++) await tracer.frame(scene.camera, { wait: true }).done;+ const picture = await tracer.readFrame();+ tracer.dispose();+ return picture;+ };+ const reference = await restFrame(options, false);+ const view = createCamera(scene.camera, reference.width, reference.height);+ const startPixel = projectToPixel(view, position(0), reference.width, reference.height);+ const edgePixel = projectToPixel(view, position(0).map((value, axis) => value + right[axis] * halfSize), reference.width, reference.height);+ const radius = Math.max(8, Math.abs(edgePixel.x - startPixel.x) * 1.5);+ const results = { trail: { x: startPixel.x, y: startPixel.y, radius }, reference: { image: toBase64Png(reference) } };+ for (const [label, followPartMotion] of [['follow', true], ['ignore', false]]) {+ const moved = await restFrame({ ...options, followPartMotion }, true);+ results[label] = {+ trailError: meanAbsoluteDifference(moved, reference, (x, y) => Math.hypot(x - startPixel.x, y - startPixel.y) <= radius),+ frameError: meanAbsoluteDifference(moved, reference, () => true),+ image: toBase64Png(moved),+ };+ }+ return results;+};diff --git a/bench/realtime-tracer/run.mjs b/bench/realtime-tracer/run.mjsindex bd9b2b53..d440699d 100644--- a/bench/realtime-tracer/run.mjs+++ b/bench/realtime-tracer/run.mjs@@ -1,6 +1,6 @@// The real-time path tracer (issue #624): frame time with an orbiting camera, the resting frame after one second, a camera move.// [GPU_LOCK="bash <path to gpu-lock.sh>"] ${GPU_LOCK} node bench/realtime-tracer/run.mjs <output directory> <scene id or recipe.json>,...-// [--mode time|rest|move|all] [--frames 200] [--width 1920] [--height 1080] [--bounces 2] [--repetitions 3] [--backend gpu|webgpu]+// [--mode time|rest|move|ghost|all] [--output-history] [--rest-seconds 1 | --rest-frames 60] [--frames 200] [--width 1920] [--height 1080] [--bounces 2] [--repetitions 3] [--backend gpu|webgpu]// (use one output directory per backend: the picture files carry no backend name)// Files: <scene>@realtime-rest.png, <scene>@move-<n>.png and timings.json. bench/realtime-tracer/score.py scores the// resting frames against Cycles, and bench/realtime-tracer/sheet.py puts a camera move on one contact sheet.@@ -18,7 +18,9 @@ const valueAfter = (flag, fallback) => (argumentsGiven.includes(flag) ? argumentconst mode = valueAfter('--mode', 'all');const frames = Number(valueAfter('--frames', 200));const repetitions = Number(valueAfter('--repetitions', 3));-const options = { width: Number(valueAfter('--width', 1920)), height: Number(valueAfter('--height', 1080)), bounces: Number(valueAfter('--bounces', 2)), resolutionDivisor: Number(valueAfter('--divisor', 2)), backend: valueAfter('--backend', 'gpu') };+const options = { width: Number(valueAfter('--width', 1920)), height: Number(valueAfter('--height', 1080)), bounces: Number(valueAfter('--bounces', 2)), resolutionDivisor: Number(valueAfter('--divisor', 2)), backend: valueAfter('--backend', 'gpu'), accumulateAtOutputSize: argumentsGiven.includes('--output-history'), filterIterations: valueAfter('--iterations', undefined) === undefined ? undefined : Number(valueAfter('--iterations', undefined)) };+const restSeconds = Number(valueAfter('--rest-seconds', 1));+const restFrames = Number(valueAfter('--rest-frames', 0));const recipeName = id => id.replace(/^.*[\/]/, '').replace(/\.recipe\.json$|\.json$/, '');setPriority(0, constants.priority.PRIORITY_BELOW_NORMAL);@@ -50,7 +52,7 @@ try {}}if (mode === 'rest' || mode === 'all') {- const rest = await page.evaluate(([scene, tracerOptions]) => window.renderRestingFrame(scene, tracerOptions, 1), [source, options]);+ const rest = await page.evaluate(([scene, tracerOptions, seconds, frameLimit]) => window.renderRestingFrame(scene, tracerOptions, seconds, frameLimit), [source, options, restSeconds, restFrames]);await writeFile(`${outputDirectory}/${name}@realtime-rest.png`, Buffer.from(rest.image, 'base64'));timings[`${name}:rest-frame`] = { frames: rest.frames, seconds: Number(rest.elapsedSeconds.toFixed(2)) };console.log(name, 'resting frame', JSON.stringify(timings[`${name}:rest-frame`]));@@ -60,6 +62,12 @@ try {for (const [index, image] of images.entries()) await writeFile(`${outputDirectory}/${name}@move-${index + 1}.png`, Buffer.from(image, 'base64'));console.log(name, 'camera move', images.length, 'frames');}+ if (mode === 'ghost' || mode === 'all') {+ const ghost = await page.evaluate(([scene, tracerOptions]) => window.measureMovingPart(scene, tracerOptions), [source, options]);+ for (const label of ['reference', 'follow', 'ignore']) await writeFile(`${outputDirectory}/${name}@ghost-${label}.png`, Buffer.from(ghost[label].image, 'base64'));+ timings[`${name}:ghost`] = { trailPixels: ghost.trail, followed: { trailError: ghost.follow.trailError, frameError: ghost.follow.frameError }, ignored: { trailError: ghost.ignore.trailError, frameError: ghost.ignore.frameError } };+ console.log(name, 'moving part', JSON.stringify(timings[`${name}:ghost`]));+ }}} finally {await browser.close();diff --git a/changes/624-realtime-2.md b/changes/624-realtime-2.mdnew file mode 100644index 00000000..5bdaa7bd--- /dev/null+++ b/changes/624-realtime-2.md@@ -0,0 +1,2 @@+- Real-time WebGPU path tracer (#624): `accumulateAtOutputSize: true` keeps the temporal history at the output size, gathering the traced samples by their jittered positions, so a camera at rest converges to the output resolution instead of an upscaled half-size image (off by default, it costs frame time)+- Real-time WebGPU path tracer (#624): `update({ parts })` with the same part count keeps the history and the radiance cache, follows a part that only moved or turned (its matrix changed) through a per-part motion table, and rejects history on a part whose geometry changed, so a moving object leaves no trail; `followPartMotion: false` turns the following off for comparisondiff --git a/docs/path-tracer.md b/docs/path-tracer.mdindex 133cfc2c..f97e15dd 100644--- a/docs/path-tracer.md+++ b/docs/path-tracer.md@@ -165,10 +165,12 @@ The scene that shows the gain is `many-lights` (`bench/reference/scenes.js`, iss- **Radiance cache** (`src/path-tracer-radiance-cache.js`, SHaRC style). A cell is the quantized position (cells double in size each time the distance from the eye doubles past 64 cells) and the quantized normal of a surface point, hashed into 2^20 slots with no collision check (about 0.4% of 7,500 test cells collided). The last hit of a path reads its cell instead of tracing on; the hit before it writes what the rest of the path brought back, and a point pass adds the records into the cells with additive blending (WebGL2 has no atomics), as a running average of up to 128 records. The cache reads as diffuse. It needs the scene to rest: a new scene empties it.- **Temporal denoiser and upsampler** (`src/path-tracer-temporal-denoise.js`, `src/path-tracer-reprojection.js`, SVGF style). The light is divided by the albedo, reprojected history (bilinear, rejected where distance or normal differ) is blended in over up to 48 frames, the variance comes from the history of the luminance (from the 3 by 3 neighbours while the history is short), four a-trous passes filter with plane distance, normal and variance guided luminance weights, and a joint bilateral upsample to the output size multiplies the albedo history back in. A sample far above its neighbours is clamped while the history is short.-What it is not (the issue asked for more): primary hits are traced, not taken from a raster G-buffer (the tracer's first hit already writes albedo, normal and distance; sharing the rasterizer's depth pass would save a ray per pixel and is left open), and motion vectors come from the camera's move, so a moving or animated part would ghost; the scene is static. Material ids are not used for history rejection on the WebGL2 backend (the WebGPU backend uses them, below). The upsample is a joint bilinear filter over half-size data with jittered rays averaged over time, not a full-size accumulation.+What it is not (the issue asked for more): primary hits are traced, not taken from a raster G-buffer (the tracer's first hit already writes albedo, normal and distance; sharing the rasterizer's depth pass would save a ray per pixel and is left open), and the WebGL2 backend's motion vectors come from the camera's move only, so a moving part would ghost there (the WebGPU backend follows moved parts, below). Material ids are not used for history rejection on the WebGL2 backend (the WebGPU backend uses them, below). The upsample is a joint bilinear filter over half-size data with jittered rays averaged over time, not a full-size accumulation.**WebGPU backend** (`createRealtimeTracer(scene, { backend: 'webgpu' })`, `src/path-tracer-realtime-webgpu.js`, `-kernel.js` and `-passes.js`). The same frame as WGSL compute in one command buffer. The megakernel of `src/path-tracer-webgpu.js` gets the cache pieces through `buildMegakernelSource({ realtime: true })`: the last hit reads the cache table, the hit before it writes a record, the first hit also picks one light by importance, and the entry point writes radiance with distance, albedo, normal and the material of the first hit to a frame buffer. The cache update is two dispatches: records are summed per cell with atomic adds on fixed point numbers (1/1024, a record clamped at 32), then every cell folds its sum into its running average (up to 128 records) and empties the sum. The denoiser (accumulate, four a-trous steps, upsample with tone mapping) is the same maths as the WebGL2 passes, with one addition: history is rejected where the material at the first hit differs, which stops the light of one surface from bleeding into a neighbour it passes behind. The result is packed into a buffer and copied to `canvas` (the tracer's own, or `options.canvas`). Without `navigator.gpu`, an adapter or 10 storage buffers per stage, `backend: 'webgpu'` falls back to WebGL2 and the tracer reports `backend` and `fallbackReason`. Differences to the WebGL2 object: `frame(camera, { wait: true })` returns `{ frame, done }` (await `done` to time a frame), `readFrame()` and `update()` return promises, and it does not share the WebGL2 context, so it can run beside a WebGL2 render. `bench/realtime-tracer/run.mjs --backend webgpu` measures it (one output directory per backend).Measured on the RTX 3060 with `bench/realtime-tracer/run.mjs` (1920 by 1080 output, 960 by 540 traced, median of 200 frames, three runs of a fresh tracer, the card held by the job): `lit-interior` 20.0 to 20.1 ms orbiting and 20.1 to 20.5 ms resting; the demo room (`tests/fixtures/demo-room.recipe.json`) 20.2 ms orbiting 0.5 degrees per frame (it turns away from the heavy view) and 36.2 to 36.3 ms at the scene's own camera, 27.6 frames per second, 3 ms over the 33 ms target. With `bounces: 1` the demo room at its own camera took 24.5 ms (one run of 100 frames). On a shared card the same runs took up to 62 ms, so time it behind the GPU lock. FLIP of the frame after one second of rest (51 frames) against Cycles, `lit-interior`: 0.0637, against 0.0769 for the 64 sample still (the demo room has no Cycles render). `bench/realtime-tracer/score.py` scores it and `sheet.py` makes the contact sheet of a camera move: no trails behind the sphere, box or sign in either scene, but a fast move leaves visible speckle in dark parts of the demo room, where one sample per pixel and a cold cache are not enough.+WebGPU backend, second round (issue #624, `bench/realtime-tracer/run.mjs --backend webgpu`, same card, a loaded machine, so compare rows of one run only). `accumulateAtOutputSize: true` (`src/path-tracer-realtime-webgpu-fullres.js`) keeps the temporal history at the output size: each output pixel gathers the traced samples around it weighted by their jittered positions (the kernel writes the jitter next to the albedo), reprojects its own full-size history and filters at full size. It costs frame time, so it is off by default: the demo room took 21.4 ms at rest and 20.5 to 21.3 ms orbiting by default, 27.7 to 28.2 ms with it (24.3 to 25.1 ms with 3 a-trous steps instead of 4). It did not lower FLIP against Cycles on `lit-interior`: after 60 frames 0.0579 by default and 0.0597 with it (240 frames: 0.0539 by default; the run with it did not finish), so the remaining error is the lighting (cache, light sampling), not the resolution of the history. Moving parts: `update({ parts })` with the same part count keeps the history and cache, and a per-part motion table (`partMotionTable` in `src/path-tracer-reprojection.js`, indexed by the material id the kernel already writes) reprojects history on a part that moved or turned and rejects it on a part whose geometry changed. In the sliding box bench (`--mode ghost`) the trail left behind is the same with and without the table (1.69 levels in both), because the material id test already rejects the empty floor; what the table adds is history kept on the moving surface, which that bench does not measure. A scene update still rebuilds the BVH and the kernel (about 0.5 s), so this is not yet a per-frame animation path. Primary hits from a raster G-buffer are not done.+WebGPU backend, same setup (2026-11-09, `--backend webgpu`, median of 200 frames, three runs of a fresh tracer, card held through the GPU lock, awaiting `onSubmittedWorkDone` after every frame): `lit-interior` 10.6 to 11.0 ms orbiting and 12.1 to 12.2 ms resting (WebGL2 in the same session: 20.1 to 20.3 ms and 20.1 to 20.3 ms); the demo room 12.0 to 12.5 ms orbiting and 19.6 to 19.9 ms at the scene's own camera (WebGL2: 20.4 to 31.9 ms and 36.3 ms), so the 33 ms target holds with 13 ms to spare, at 1920 by 1080 output and 960 by 540 traced. The kernel compiles in 0.46 s against 10.8 s. FLIP of the frame after one second of rest against Cycles, `lit-interior`: 0.0504 (84 frames) against 0.0637 on WebGL2 (51 frames), the gain mostly from the frames that fit in the second. The contact sheet of a camera move on the demo room (8 frames, 2 degrees each) shows no trails; speckle in the dark floor is as on WebGL2. Left open: primary hits from a raster G-buffer, per-object motion vectors, and full-size accumulation instead of the joint bilinear upsample.diff --git a/src/path-tracer-realtime-options.js b/src/path-tracer-realtime-options.jsindex 6fbbc301..2b9b42c0 100644--- a/src/path-tracer-realtime-options.js+++ b/src/path-tracer-realtime-options.js@@ -5,6 +5,8 @@ import { normalizePathTracerOptions } from './path-tracer.js';export const REALTIME_BACKENDS = ['gpu', 'webgpu'];const DEFAULT_BOUNCES = 2;const DEFAULT_RESOLUTION_DIVISOR = 2;+// The WebGPU backend keeps its temporal history at the output size (path-tracer-realtime-webgpu-fullres.js) instead of the traced size.+const DEFAULT_ACCUMULATE_AT_OUTPUT_SIZE = false;const CACHE_CELLS_ACROSS_SCENE = 200;const CACHE_LEVEL_CELLS = 64;const SCENE_EXTENT_SCALE = 1e4;@@ -26,6 +28,8 @@ export function resolveRealtimeOptions(rawOptions) {return {backend, outputWidth, outputHeight, traceWidth, traceHeight, options,historyLimit: rawOptions.historyLimit, filterIterations: rawOptions.filterIterations, cacheCellSize: rawOptions.cacheCellSize,+ accumulateAtOutputSize: rawOptions.accumulateAtOutputSize ?? DEFAULT_ACCUMULATE_AT_OUTPUT_SIZE,+ followPartMotion: rawOptions.followPartMotion ?? true,};}diff --git a/src/path-tracer-realtime-webgpu-fullres.js b/src/path-tracer-realtime-webgpu-fullres.jsnew file mode 100644index 00000000..d42c6b94--- /dev/null+++ b/src/path-tracer-realtime-webgpu-fullres.js@@ -0,0 +1,341 @@+import { DEFAULT_FILTER_ITERATIONS, DEFAULT_HISTORY_LIMIT } from './path-tracer-temporal-denoise.js';+import {+ DENOISE_COMMON, DENOISE_PARAMETER_BYTES, FIREFLY_DEVIATIONS, MINIMUM_ALBEDO, SHORT_HISTORY_LENGTH, SPATIAL_VARIANCE_HISTORY,+ bind, compileModule, createPipeline, outputRowWords, partMotionSource, storage, uniform, writeDenoiseParameters,+} from './path-tracer-realtime-webgpu-passes.js';++// The temporal denoiser of the WebGPU real-time tracer with its history at the OUTPUT size (issue #624), the option+// `accumulateAtOutputSize`. The default denoiser (path-tracer-realtime-webgpu-passes.js) accumulates on the traced low+// resolution grid and upsamples the result, so a camera at rest converges to an upscaled image. Here every output pixel+// gathers the traced samples around it by their jittered positions (the kernel writes each sample's jitter next to its+// albedo), which is supersampling over frames: the history keeps sub-pixel detail, and the picture converges to the output+// resolution. The history holds four planes of the output size: light and history length, moments, variance and distance,+// albedo, and normal and material. The traced frame is read as three planes: radiance and distance, albedo and jitter,+// normal and material. The pass order is resolve (gather, reproject, blend), a-trous steps, present.++const SAMPLE_SIGMA = 0.5;+const NEIGHBOURHOOD_RADIUS = 1;+const NEIGHBOUR_COUNT = (2 * NEIGHBOURHOOD_RADIUS + 1) ** 2;+const JITTER_STEPS = 4095;+const MINIMUM_SAMPLE_WEIGHT = 1e-3;++const FULL_RESOLUTION_COMMON = DENOISE_COMMON + /* wgsl */ `+fn outputIndex(texel: vec2<i32>) -> u32 {+ return u32(texel.y * params.sizes.z + texel.x);+}++fn outputPlaneSize() -> u32 {+ return u32(params.sizes.z * params.sizes.w);+}++fn insideOutputImage(texel: vec2<i32>) -> bool {+ return all(texel >= vec2<i32>(0)) && all(texel < params.sizes.zw);+}+`;++const RESOLVE_SOURCE = FULL_RESOLUTION_COMMON + partMotionSource(5) + /* wgsl */ `+@group(0) @binding(1) var<storage, read> traced: array<vec4<f32>>;+@group(0) @binding(2) var<storage, read> historyPrevious: array<vec4<f32>>;+@group(0) @binding(3) var<storage, read_write> historyCurrent: array<vec4<f32>>;+@group(0) @binding(4) var<storage, read_write> filtered: array<vec4<f32>>;++// The jitter of a sample, packed by the kernel as two 12 bit fractions in one float.+fn jitterOf(packed: f32) -> vec2<f32> {+ let bits = u32(packed);+ return vec2<f32>(f32(bits >> 12u), f32(bits & 4095u)) / ${JITTER_STEPS}.0;+}++fn sampleIllumination(index: u32) -> vec3<f32> {+ return traced[index].rgb / max(traced[planeSize() + index].rgb, vec3<f32>(MINIMUM_ALBEDO));+}++@compute @workgroup_size(8, 8, 1)+fn resolve(@builtin(global_invocation_id) id: vec3<u32>) {+ let texel = vec2<i32>(id.xy);+ if (!insideOutputImage(texel)) { return; }+ let tracePlane = planeSize();+ let outputPlane = outputPlaneSize();+ let index = outputIndex(texel);+ let traceSize = vec2<f32>(params.sizes.xy);+ let outputSize = vec2<f32>(params.sizes.zw);+ let position = (vec2<f32>(texel) + 0.5) / outputSize * traceSize;+ let centre = vec2<i32>(floor(position));++ // First look at the traced samples around: the nearest one gives the geometry of this pixel, and the spread of their light+ // sets the firefly limit.+ var nearestIndex = pixelIndex(clamp(centre, vec2<i32>(0), params.sizes.xy - vec2<i32>(1)));+ var nearestDistanceSquared = 1e9;+ var luminanceSum = 0.0;+ var luminanceSumOfSquares = 0.0;+ var brightest = 0.0;+ var count = 0.0;+ for (var offsetY = -${NEIGHBOURHOOD_RADIUS}; offsetY <= ${NEIGHBOURHOOD_RADIUS}; offsetY++) {+ for (var offsetX = -${NEIGHBOURHOOD_RADIUS}; offsetX <= ${NEIGHBOURHOOD_RADIUS}; offsetX++) {+ let neighbour = centre + vec2<i32>(offsetX, offsetY);+ if (!insideTraceImage(neighbour)) { continue; }+ let neighbourIndex = pixelIndex(neighbour);+ let separation = vec2<f32>(neighbour) + jitterOf(traced[tracePlane + neighbourIndex].w) - position;+ let distanceSquared = dot(separation, separation);+ if (distanceSquared < nearestDistanceSquared) {+ nearestDistanceSquared = distanceSquared;+ nearestIndex = neighbourIndex;+ }+ let luminance = luminanceOf(sampleIllumination(neighbourIndex));+ luminanceSum += luminance;+ luminanceSumOfSquares += luminance * luminance;+ brightest = max(brightest, luminance);+ count += 1.0;+ }+ }+ // The limit comes from the neighbours without the brightest, so a spike does not raise its own limit.+ let others = max(count - 1.0, 1.0);+ let othersMean = (luminanceSum - brightest) / others;+ let othersDeviation = sqrt(max(0.0, (luminanceSumOfSquares - brightest * brightest) / others - othersMean * othersMean));+ let luminanceLimit = othersMean * 1.05 + ${FIREFLY_DEVIATIONS} * othersDeviation + 0.01;++ let nearestTexel = vec2<i32>(i32(nearestIndex) % params.sizes.x, i32(nearestIndex) / params.sizes.x);+ let nearestNormalAndMaterial = traced[2u * tracePlane + nearestIndex];+ let normal = nearestNormalAndMaterial.xyz;+ let nearestJitter = jitterOf(traced[tracePlane + nearestIndex].w);+ let nearestSamplePosition = vec2<f32>(nearestTexel) + nearestJitter;+ let hitDistance = distanceOf(traced[nearestIndex]);+ // The world point the nearest sample hit, and where that surface point was in the previous frame.+ let point = params.eye.xyz + pixelRayDirection(nearestSamplePosition, traceSize) * hitDistance;+ let previousPoint = previousPointOf(point, nearestNormalAndMaterial.w);++ var history = vec4<f32>(0.0);+ var historyMoments = vec4<f32>(0.0);+ var historyAlbedo = vec3<f32>(0.0);+ var totalWeight = 0.0;+ let projection = projectToPrevious(previousPoint.xyz, outputSize);+ if (params.settings.z < 0.5 && projection.w > 0.5 && previousPoint.w > 0.5) {+ let base = floor(projection.xy - 0.5);+ let fraction = projection.xy - 0.5 - base;+ for (var tap = 0; tap < 4; tap++) {+ let offset = vec2<i32>(tap & 1, tap >> 1u);+ let historyTexel = vec2<i32>(base) + offset;+ if (!insideOutputImage(historyTexel)) { continue; }+ let weight = select(1.0 - fraction.x, fraction.x, offset.x == 1) * select(1.0 - fraction.y, fraction.y, offset.y == 1);+ let historyIndex = outputIndex(historyTexel);+ let previousNormal = historyPrevious[3u * outputPlane + historyIndex];+ let previousMoments = historyPrevious[outputPlane + historyIndex];+ if (!isHistoryConsistent(projection.z, previousMoments.w, normal, previousNormal.xyz, nearestNormalAndMaterial.w, previousNormal.w)) { continue; }+ history += weight * historyPrevious[historyIndex];+ historyMoments += weight * previousMoments;+ historyAlbedo += weight * historyPrevious[2u * outputPlane + historyIndex].rgb;+ totalWeight += weight;+ }+ }+ let shortHistory = totalWeight <= 1e-3 || history.w < ${SHORT_HISTORY_LENGTH}.0;++ // The new light: the samples around, weighted by how close their jittered position is to this pixel's centre.+ var sampleWeightSum = 0.0;+ var weightedIllumination = vec3<f32>(0.0);+ var sampleAlbedo = vec3<f32>(0.0);+ for (var offsetY = -${NEIGHBOURHOOD_RADIUS}; offsetY <= ${NEIGHBOURHOOD_RADIUS}; offsetY++) {+ for (var offsetX = -${NEIGHBOURHOOD_RADIUS}; offsetX <= ${NEIGHBOURHOOD_RADIUS}; offsetX++) {+ let neighbour = centre + vec2<i32>(offsetX, offsetY);+ if (!insideTraceImage(neighbour)) { continue; }+ let neighbourIndex = pixelIndex(neighbour);+ let albedoAndJitter = traced[tracePlane + neighbourIndex];+ let separation = vec2<f32>(neighbour) + jitterOf(albedoAndJitter.w) - position;+ let weight = exp(-dot(separation, separation) / (2.0 * ${SAMPLE_SIGMA * SAMPLE_SIGMA}));+ var illumination = sampleIllumination(neighbourIndex);+ let luminance = luminanceOf(illumination);+ if (shortHistory && luminance > luminanceLimit) { illumination *= luminanceLimit / luminance; }+ sampleWeightSum += weight;+ weightedIllumination += weight * illumination;+ sampleAlbedo += weight * albedoAndJitter.rgb;+ }+ }+ // No sample close enough: the nearest one stands in, so a pixel without history is never black.+ var newWeight = min(sampleWeightSum, 1.0);+ var newIllumination = sampleIllumination(nearestIndex);+ var newAlbedo = traced[tracePlane + nearestIndex].rgb;+ if (sampleWeightSum > ${MINIMUM_SAMPLE_WEIGHT}) {+ newIllumination = weightedIllumination / sampleWeightSum;+ newAlbedo = sampleAlbedo / sampleWeightSum;+ } else if (totalWeight <= 1e-3) {+ newWeight = 1.0;+ } else {+ newWeight = 0.0;+ }+ let newLuminance = luminanceOf(newIllumination);+ let currentMoments = vec2<f32>(newLuminance, newLuminance * newLuminance);++ var color = newIllumination;+ var blendedAlbedo = newAlbedo;+ var moments = currentMoments;+ var historyLength = newWeight;+ if (totalWeight > 1e-3) {+ history /= totalWeight;+ historyMoments /= totalWeight;+ historyAlbedo /= totalWeight;+ historyLength = min(history.w + newWeight, params.settings.y);+ let blend = newWeight / max(historyLength, 1e-3);+ color = mix(history.rgb, newIllumination, blend);+ blendedAlbedo = mix(historyAlbedo, newAlbedo, blend);+ moments = mix(historyMoments.xy, currentMoments, blend);+ }+ var variance = max(0.0, moments.y - moments.x * moments.x);+ if (historyLength < ${SPATIAL_VARIANCE_HISTORY}.0) {+ let mean = luminanceSum / max(count, 1.0);+ variance = max(0.0, luminanceSumOfSquares / max(count, 1.0) - mean * mean);+ }+ historyCurrent[index] = vec4<f32>(color, historyLength);+ historyCurrent[outputPlane + index] = vec4<f32>(moments, variance, hitDistance);+ historyCurrent[2u * outputPlane + index] = vec4<f32>(blendedAlbedo, 1.0);+ historyCurrent[3u * outputPlane + index] = nearestNormalAndMaterial;+ filtered[index] = vec4<f32>(color, variance);+}+`;++const ATROUS_SOURCE = FULL_RESOLUTION_COMMON + /* wgsl */ `+@group(0) @binding(1) var<storage, read> geometry: array<vec4<f32>>;+@group(0) @binding(2) var<storage, read> filterIn: array<vec4<f32>>;+@group(0) @binding(3) var<storage, read_write> filterOut: array<vec4<f32>>;++const PLANE_SIGMA: f32 = 0.02;+const LUMINANCE_SIGMA: f32 = 4.0;+const NORMAL_POWER: f32 = 64.0;++fn kernelWeight(offset: i32) -> f32 {+ if (offset == 0) { return 0.375; }+ if (offset == 1) { return 0.25; }+ return 0.0625;+}++// The geometry planes of the history: moments, variance and distance (the distance is the fourth value), and normal and material.+fn distanceAt(index: u32) -> f32 {+ return geometry[outputPlaneSize() + index].w;+}++fn normalAt(index: u32) -> vec3<f32> {+ return geometry[3u * outputPlaneSize() + index].xyz;+}++@compute @workgroup_size(8, 8, 1)+fn atrous(@builtin(global_invocation_id) id: vec3<u32>) {+ let texel = vec2<i32>(id.xy);+ if (!insideOutputImage(texel)) { return; }+ let size = vec2<f32>(params.sizes.zw);+ let stride = i32(params.settings.w);+ let index = outputIndex(texel);+ let center = filterIn[index];+ let centerNormal = normalAt(index);+ let centerDistance = distanceAt(index);+ let centerPoint = pixelRayDirection(vec2<f32>(texel) + 0.5, size) * centerDistance;+ let centerLuminance = luminanceOf(center.rgb);+ var smoothedVariance = 0.0;+ for (var offsetY = -1; offsetY <= 1; offsetY++) {+ for (var offsetX = -1; offsetX <= 1; offsetX++) {+ let neighbour = clamp(texel + vec2<i32>(offsetX, offsetY), vec2<i32>(0), params.sizes.zw - vec2<i32>(1));+ smoothedVariance += filterIn[outputIndex(neighbour)].w * select(0.25, 0.5, offsetX == 0) * select(0.25, 0.5, offsetY == 0);+ }+ }+ let luminanceScale = LUMINANCE_SIGMA * sqrt(max(smoothedVariance, 0.0)) + 1e-4;++ var sumColor = vec3<f32>(0.0);+ var sumVariance = 0.0;+ var sumWeight = 0.0;+ for (var offsetY = -2; offsetY <= 2; offsetY++) {+ for (var offsetX = -2; offsetX <= 2; offsetX++) {+ let neighbour = texel + vec2<i32>(offsetX, offsetY) * stride;+ if (!insideOutputImage(neighbour)) { continue; }+ let neighbourIndex = outputIndex(neighbour);+ let value = filterIn[neighbourIndex];+ let neighbourPoint = pixelRayDirection(vec2<f32>(neighbour) + 0.5, size) * distanceAt(neighbourIndex);+ let planeDistance = abs(dot(centerNormal, neighbourPoint - centerPoint));+ let planeWeight = exp(-planeDistance / (PLANE_SIGMA * centerDistance));+ let normalWeight = pow(max(dot(centerNormal, normalAt(neighbourIndex)), 0.0), NORMAL_POWER);+ let luminanceWeight = exp(-abs(luminanceOf(value.rgb) - centerLuminance) / luminanceScale);+ let weight = kernelWeight(abs(offsetX)) * kernelWeight(abs(offsetY)) * planeWeight * normalWeight * luminanceWeight;+ sumColor += weight * value.rgb;+ sumVariance += weight * weight * value.w;+ sumWeight += weight;+ }+ }+ filterOut[index] = select(center, vec4<f32>(sumColor / sumWeight, sumVariance / (sumWeight * sumWeight)), sumWeight > 1e-6);+}+`;++// The filtered light times the albedo history, tone mapped (ACES, gamma 2.2) and packed as the canvas format wants it.+const PRESENT_SOURCE = FULL_RESOLUTION_COMMON + /* wgsl */ `+@group(0) @binding(1) var<storage, read> filtered: array<vec4<f32>>;+@group(0) @binding(2) var<storage, read> history: array<vec4<f32>>;+@group(0) @binding(3) var<storage, read_write> outputPixels: array<u32>;++fn toneMap(linearColor: vec3<f32>) -> vec3<f32> {+ let color = max(linearColor, vec3<f32>(0.0));+ let mapped = clamp((color * (2.51 * color + 0.03)) / (color * (2.43 * color + 0.59) + 0.14), vec3<f32>(0.0), vec3<f32>(1.0));+ return pow(mapped, vec3<f32>(1.0 / 2.2));+}++@compute @workgroup_size(8, 8, 1)+fn present(@builtin(global_invocation_id) id: vec3<u32>) {+ let texel = vec2<i32>(id.xy);+ if (!insideOutputImage(texel)) { return; }+ let index = outputIndex(texel);+ let albedo = history[2u * outputPlaneSize() + index].rgb;+ let color = toneMap(filtered[index].rgb * max(albedo, vec3<f32>(MINIMUM_ALBEDO)));+ let bytes = vec3<u32>(color * 255.0 + 0.5);+ let swapped = params.outputLayout.y == 1u;+ let packedColor = select(bytes.x, bytes.z, swapped) | (bytes.y << 8u) | (select(bytes.z, bytes.x, swapped) << 16u) | (255u << 24u);+ outputPixels[id.y * params.outputLayout.x + id.x] = packedColor;+}+`;++/**+ * The same interface as `createWebGpuDenoiser`, with the history at the output size (see the header). `tracedBuffers`+ * hold the traced frames; the frame before the one in `tracedBuffers[set]` is not read, because the history carries+ * its own geometry.+ */+export async function createWebGpuFullResolutionDenoiser(device, { traceWidth, traceHeight, outputWidth, outputHeight, tracedBuffers, motionBuffer, historyLimit = DEFAULT_HISTORY_LIMIT, filterIterations = DEFAULT_FILTER_ITERATIONS, swapped = false }) {+ const [resolveModule, atrousModule, presentModule] = await Promise.all([+ compileModule(device, 'full resolution resolve', RESOLVE_SOURCE), compileModule(device, 'full resolution a-trous', ATROUS_SOURCE), compileModule(device, 'full resolution present', PRESENT_SOURCE),+ ]);+ const [resolvePipeline, atrousPipeline, presentPipeline] = await Promise.all([+ createPipeline(device, resolveModule, 'resolve'), createPipeline(device, atrousModule, 'atrous'), createPipeline(device, presentModule, 'present'),+ ]);+ const plane = outputWidth * outputHeight * 16;+ const rowWords = outputRowWords(outputWidth);+ const sizes = [traceWidth, traceHeight, outputWidth, outputHeight];+ const histories = [0, 1].map(index => storage(device, `output history ${index}`, plane * 4));+ const filters = [0, 1].map(index => storage(device, `output filter ${index}`, plane));+ const outputBuffer = storage(device, 'output pixels', rowWords * 4 * outputHeight, GPUBufferUsage.COPY_SRC);+ const resolveParameters = uniform(device, 'resolve parameters', DENOISE_PARAMETER_BYTES);+ const atrousParameters = Array.from({ length: filterIterations }, () => uniform(device, 'a-trous parameters', DENOISE_PARAMETER_BYTES));+ const presentParameters = uniform(device, 'present parameters', DENOISE_PARAMETER_BYTES);+ const finalFilter = filters[filterIterations % 2];+ const groups = [Math.ceil(outputWidth / 8), Math.ceil(outputHeight / 8)];+ const sets = [0, 1].map(set => ({+ resolve: bind(device, resolvePipeline, [resolveParameters, tracedBuffers[set], histories[1 - set], histories[set], filters[0], motionBuffer]),+ atrous: atrousParameters.map((parameters, iteration) => bind(device, atrousPipeline, [parameters, histories[set], filters[iteration % 2], filters[1 - iteration % 2]])),+ present: bind(device, presentPipeline, [presentParameters, finalFilter, histories[set], outputBuffer]),+ }));++ return {+ outputBuffer,+ rowWords,+ encode(pass, set, inputs) {+ const settings = { ...inputs, historyLimit };+ writeDenoiseParameters(device, resolveParameters, settings, sizes, 1, rowWords, swapped);+ atrousParameters.forEach((buffer, iteration) => writeDenoiseParameters(device, buffer, settings, sizes, 1 << iteration, rowWords, swapped));+ writeDenoiseParameters(device, presentParameters, settings, sizes, 1, rowWords, swapped);+ pass.setPipeline(resolvePipeline);+ pass.setBindGroup(0, sets[set].resolve);+ pass.dispatchWorkgroups(...groups);+ pass.setPipeline(atrousPipeline);+ for (const group of sets[set].atrous) {+ pass.setBindGroup(0, group);+ pass.dispatchWorkgroups(...groups);+ }+ pass.setPipeline(presentPipeline);+ pass.setBindGroup(0, sets[set].present);+ pass.dispatchWorkgroups(...groups);+ },+ dispose() {+ for (const buffer of [...histories, ...filters, outputBuffer, resolveParameters, presentParameters, ...atrousParameters]) buffer.destroy();+ },+ };+}diff --git a/src/path-tracer-realtime-webgpu-kernel.js b/src/path-tracer-realtime-webgpu-kernel.jsindex 8f1aad15..b2d4806e 100644--- a/src/path-tracer-realtime-webgpu-kernel.js+++ b/src/path-tracer-realtime-webgpu-kernel.js@@ -5,7 +5,7 @@ import { CACHE_SLOT_COUNT, MAXIMUM_LEVEL, NORMAL_AXIS_THRESHOLD } from './path-t// - the cache table and the record buffer (bindings 9 and 10), the cell hash (the same value as `cacheSlot`) and the lookup;// - light selection that draws one emitter at every hit (the denoiser averages the draws over frames), by importance;// - the last hit of a path ending in a cache lookup, and the hit before it remembering its cell for a record;-// - an entry point that writes the first hit's radiance, albedo, normal and material to the frame buffer and the record.+// - an entry point that writes the first hit's radiance, albedo (with the sample's jitter), normal and material to the frame buffer and the record.// The cell size and the level distance travel in the tile words of the parameter block, as float bits.const REALTIME_DECLARATIONS = /* wgsl */ `@@ -149,7 +149,9 @@ fn traceRealtime(@builtin(global_invocation_id) id: vec3<u32>) {if (!(all(featureAlbedo >= vec3<f32>(0.0)) && all(featureAlbedo < vec3<f32>(1e30)))) { featureAlbedo = vec3<f32>(1.0); }let pixelCount = u32(width * height);accumulation[pixel] = vec4<f32>(radiance, featureDepth);- accumulation[pixelCount + pixel] = vec4<f32>(featureAlbedo, 0.0);+ // The jitter goes next to the albedo as two 12 bit fractions in one float, for the denoiser that accumulates at the output size.+ let packedJitter = f32(u32(jitterX * 4095.0 + 0.5) * 4096u + u32(jitterY * 4095.0 + 0.5));+ accumulation[pixelCount + pixel] = vec4<f32>(featureAlbedo, packedJitter);accumulation[2u * pixelCount + pixel] = vec4<f32>(featureNormal, firstHitMaterial);// The record: the light the rest of the path brought, per unit of the weight at that hit (the incoming radiance).let recordedTail = clamp((radiance - recordedRadianceBefore) / max(recordedThroughput, vec3<f32>(0.05)), vec3<f32>(0.0), vec3<f32>(params.cameraAndClamp.z));diff --git a/src/path-tracer-realtime-webgpu-passes.js b/src/path-tracer-realtime-webgpu-passes.jsindex d12f672d..772a6493 100644--- a/src/path-tracer-realtime-webgpu-passes.js+++ b/src/path-tracer-realtime-webgpu-passes.js@@ -1,6 +1,6 @@import { CACHE_HISTORY_LIMIT, CACHE_SLOT_COUNT } from './path-tracer-radiance-cache.js';import { DEFAULT_FILTER_ITERATIONS, DEFAULT_HISTORY_LIMIT } from './path-tracer-temporal-denoise.js';-import { DEPTH_TOLERANCE, NORMAL_AGREEMENT } from './path-tracer-reprojection.js';+import { DEPTH_TOLERANCE, NORMAL_AGREEMENT, PART_MOTION_CHANGED, PART_MOTION_FLOATS } from './path-tracer-reprojection.js';// The compute passes of the WebGPU real-time tracer (issue #624): the radiance cache update and the temporal denoiser,// the WGSL form of path-tracer-radiance-cache.js and path-tracer-temporal-denoise.js (read those for the reasoning). Two@@ -12,13 +12,13 @@ import { DEPTH_TOLERANCE, NORMAL_AGREEMENT } from './path-tracer-reprojection.jsconst FIXED_POINT_SCALE = 1024;const RECORD_LIMIT = 32;-const MINIMUM_ALBEDO = 0.02;-const SPATIAL_VARIANCE_HISTORY = 4;-const FIREFLY_DEVIATIONS = 2.5;-const SHORT_HISTORY_LENGTH = 8;+export const MINIMUM_ALBEDO = 0.02;+export const SPATIAL_VARIANCE_HISTORY = 4;+export const FIREFLY_DEVIATIONS = 2.5;+export const SHORT_HISTORY_LENGTH = 8;const CACHE_WORKGROUP_SIZE = 256;const CACHE_PARAMETER_BYTES = 16;-const DENOISE_PARAMETER_BYTES = 12 * 16;+export const DENOISE_PARAMETER_BYTES = 12 * 16;const CACHE_SOURCE = /* wgsl */ `@group(0) @binding(0) var<storage, read> records: array<vec4<f32>>;@@ -56,7 +56,7 @@ fn resolveCells(@builtin(global_invocation_id) id: vec3<u32>, @builtin(num_workg}`;-const DENOISE_COMMON = /* wgsl */ `+export const DENOISE_COMMON = /* wgsl */ `struct DenoiseParams {eye: vec4<f32>,forward: vec4<f32>,@@ -120,7 +120,27 @@ fn insideTraceImage(texel: vec2<i32>) -> bool {}`;-const ACCUMULATE_SOURCE = DENOISE_COMMON + /* wgsl */ `+/**+ * The motion table of the parts (see partMotionTable in path-tracer-reprojection.js) at binding `binding`, and the lookup+ * of where a surface point of this frame was in the previous frame. The result's w is 0 when history cannot be reused.+ */+export const partMotionSource = binding => /* wgsl */ `+@group(0) @binding(${binding}) var<storage, read> partMotion: array<vec4<f32>>;++fn previousPointOf(point: vec3<f32>, material: f32) -> vec4<f32> {+ if (material < 0.5) { return vec4<f32>(point, 1.0); }+ let base = u32(material - 1.0) * ${PART_MOTION_FLOATS / 4}u;+ let kind = partMotion[base + 3u].x;+ if (kind < 0.5) { return vec4<f32>(point, 1.0); }+ if (kind > ${PART_MOTION_CHANGED}.0 - 0.5) { return vec4<f32>(point, 0.0); }+ let rowX = partMotion[base];+ let rowY = partMotion[base + 1u];+ let rowZ = partMotion[base + 2u];+ return vec4<f32>(dot(rowX.xyz, point) + rowX.w, dot(rowY.xyz, point) + rowY.w, dot(rowZ.xyz, point) + rowZ.w, 1.0);+}+`;++const ACCUMULATE_SOURCE = DENOISE_COMMON + partMotionSource(6) + /* wgsl */ `@group(0) @binding(1) var<storage, read> traced: array<vec4<f32>>;@group(0) @binding(2) var<storage, read> tracedPrevious: array<vec4<f32>>;@group(0) @binding(3) var<storage, read> historyPrevious: array<vec4<f32>>;@@ -145,13 +165,14 @@ fn accumulate(@builtin(global_invocation_id) id: vec3<u32>) {var illumination = tracedValue.rgb / max(albedo, vec3<f32>(MINIMUM_ALBEDO));let size = vec2<f32>(params.sizes.xy);let point = params.eye.xyz + pixelRayDirection(vec2<f32>(texel) + 0.5, size) * distanceOf(tracedValue);+ let previousPoint = previousPointOf(point, normalAndMaterial.w);var history = vec4<f32>(0.0);var historyMoments = vec4<f32>(0.0);var historyAlbedo = vec3<f32>(0.0);var totalWeight = 0.0;- let projection = projectToPrevious(point, size);- if (params.settings.z < 0.5 && projection.w > 0.5) {+ let projection = projectToPrevious(previousPoint.xyz, size);+ if (params.settings.z < 0.5 && projection.w > 0.5 && previousPoint.w > 0.5) {let base = floor(projection.xy - 0.5);let fraction = projection.xy - 0.5 - base;for (var tap = 0; tap < 4; tap++) {@@ -334,11 +355,11 @@ fn present(@builtin(global_invocation_id) id: vec3<u32>) {}`;-function createPipeline(device, module, entryPoint) {+export function createPipeline(device, module, entryPoint) {return device.createComputePipelineAsync({ layout: 'auto', compute: { module, entryPoint } });}-async function compileModule(device, label, code) {+export async function compileModule(device, label, code) {const module = device.createShaderModule({ label, code });const info = await module.getCompilationInfo();const errors = info.messages.filter(message => message.type === 'error');@@ -346,9 +367,9 @@ async function compileModule(device, label, code) {return module;}-const storage = (device, label, bytes, extraUsage = 0) => device.createBuffer({ label, size: Math.max(16, bytes), usage: GPUBufferUsage.STORAGE | extraUsage });-const uniform = (device, label, bytes) => device.createBuffer({ label, size: bytes, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST });-const bind = (device, pipeline, buffers) => device.createBindGroup({ layout: pipeline.getBindGroupLayout(0), entries: buffers.map((buffer, binding) => ({ binding, resource: { buffer } })) });+export const storage = (device, label, bytes, extraUsage = 0) => device.createBuffer({ label, size: Math.max(16, bytes), usage: GPUBufferUsage.STORAGE | extraUsage });+export const uniform = (device, label, bytes) => device.createBuffer({ label, size: bytes, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST });+export const bind = (device, pipeline, buffers) => device.createBindGroup({ layout: pipeline.getBindGroupLayout(0), entries: buffers.map((buffer, binding) => ({ binding, resource: { buffer } })) });/*** The cache table (CACHE_SLOT_COUNT vec4: mean radiance and the number of records behind it) and the pass that folds the@@ -388,7 +409,7 @@ export async function createWebGpuRadianceCache(device) {/** Rows of a texture copy start at multiples of 256 bytes. */export const outputRowWords = width => Math.ceil(width * 4 / 256) * 64;-function writeDenoiseParameters(device, buffer, inputs, sizes, step, rowWords, swapped) {+export function writeDenoiseParameters(device, buffer, inputs, sizes, step, rowWords, swapped) {const words = new ArrayBuffer(DENOISE_PARAMETER_BYTES);const floats = new Float32Array(words);const integers = new Int32Array(words);@@ -408,7 +429,7 @@ function writeDenoiseParameters(device, buffer, inputs, sizes, step, rowWords, s* pass for the traced frame in `tracedBuffers[set]` (the frame before it is in the other set). The result is packed* pixels in `outputBuffer`, `outputRowWords(outputWidth)` words per row, red first, or blue first when `swapped`.*/-export async function createWebGpuDenoiser(device, { traceWidth, traceHeight, outputWidth, outputHeight, tracedBuffers, historyLimit = DEFAULT_HISTORY_LIMIT, filterIterations = DEFAULT_FILTER_ITERATIONS, swapped = false }) {+export async function createWebGpuDenoiser(device, { traceWidth, traceHeight, outputWidth, outputHeight, tracedBuffers, motionBuffer, historyLimit = DEFAULT_HISTORY_LIMIT, filterIterations = DEFAULT_FILTER_ITERATIONS, swapped = false }) {const [accumulateModule, atrousModule, presentModule] = await Promise.all([compileModule(device, 'accumulate', ACCUMULATE_SOURCE), compileModule(device, 'a-trous', ATROUS_SOURCE), compileModule(device, 'present', PRESENT_SOURCE),]);@@ -427,7 +448,7 @@ export async function createWebGpuDenoiser(device, { traceWidth, traceHeight, ouconst finalFilter = filters[filterIterations % 2];const groupsFor = (width, height) => [Math.ceil(width / 8), Math.ceil(height / 8)];const sets = [0, 1].map(set => ({- accumulate: bind(device, accumulatePipeline, [accumulateParameters, tracedBuffers[set], tracedBuffers[1 - set], histories[1 - set], histories[set], filters[0]]),+ accumulate: bind(device, accumulatePipeline, [accumulateParameters, tracedBuffers[set], tracedBuffers[1 - set], histories[1 - set], histories[set], filters[0], motionBuffer]),atrous: atrousParameters.map((parameters, iteration) => bind(device, atrousPipeline, [parameters, tracedBuffers[set], filters[iteration % 2], filters[1 - iteration % 2]])),present: bind(device, presentPipeline, [presentParameters, tracedBuffers[set], finalFilter, histories[set], outputBuffer]),}));diff --git a/src/path-tracer-realtime-webgpu.js b/src/path-tracer-realtime-webgpu.jsindex 0237e719..6f178ec5 100644--- a/src/path-tracer-realtime-webgpu.js+++ b/src/path-tracer-realtime-webgpu.js@@ -4,6 +4,9 @@ import { prepareRealtimeKernel } from './path-tracer-webgpu.js';import { REALTIME_KERNEL_PIECES } from './path-tracer-realtime-webgpu-kernel.js';import { cacheCellSizes } from './path-tracer-realtime-options.js';import { createWebGpuDenoiser, createWebGpuRadianceCache } from './path-tracer-realtime-webgpu-passes.js';+import { createWebGpuFullResolutionDenoiser } from './path-tracer-realtime-webgpu-fullres.js';+import { PART_MOTION_CHANGED, PART_MOTION_FLOATS, partMotionTable } from './path-tracer-reprojection.js';+import { MAX_TRACE_PARTS } from './path-tracer-scene.js';// The WebGPU backend of the real-time path tracer (issue #624): the same frame as path-tracer-realtime.js, one path per// pixel at half the output size, two bounces ended by the radiance cache, then the temporal denoiser, but every step is a@@ -33,7 +36,7 @@ function createOutputCanvas(width, height) {/** `resolved` is what `resolveRealtimeOptions` returns for the options of `createRealtimeTracer`; `suppliedCanvas` is optional. */export async function createWebGpuRealtimeTracer(scene, resolved, suppliedCanvas) {- const { outputWidth, outputHeight, traceWidth, traceHeight, options, historyLimit, filterIterations, cacheCellSize } = resolved;+ const { outputWidth, outputHeight, traceWidth, traceHeight, options, historyLimit, filterIterations, cacheCellSize, accumulateAtOutputSize, followPartMotion } = resolved;let traceScene = null;let kernel = null;let device = null;@@ -44,6 +47,11 @@ export async function createWebGpuRealtimeTracer(scene, resolved, suppliedCanvaslet context = null;let parameterBuffer = null;let recordBuffer = null;+ let motionBuffer = null;+ let currentParts = null;+ let currentEnvironment = null;+ let pendingMotion = null;+ let motionIsActive = false;let traceGroups = null;let cellSizes = null;const pixelCount = traceWidth * traceHeight;@@ -55,7 +63,13 @@ export async function createWebGpuRealtimeTracer(scene, resolved, suppliedCanvaslet clearCache = true;let disposed = false;- const loadScene = async (parts, environment) => {+ // `motion` is for an update that moves parts of the same scene: the denoiser's history and the cache stay, and the table+ // tells the denoiser where each part was (path-tracer-reprojection.js, partMotionTable).+ const loadScene = async (parts, environment, motion = null) => {+ const keepHistory = motion !== null;+ pendingMotion = motion;+ currentParts = parts;+ currentEnvironment = environment;traceScene = buildTraceScene(parts, environment);const next = await prepareRealtimeKernel(traceScene, options, REALTIME_KERNEL_PIECES);const previousKernel = kernel;@@ -66,8 +80,10 @@ export async function createWebGpuRealtimeTracer(scene, resolved, suppliedCanvastraced = [0, 1].map(index => device.createBuffer({ label: `traced ${index}`, size: pixelCount * 16 * 3, usage: GPUBufferUsage.STORAGE }));recordBuffer = device.createBuffer({ label: 'cache records', size: pixelCount * 16, usage: GPUBufferUsage.STORAGE });parameterBuffer = device.createBuffer({ label: 'realtime parameters', size: 512, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST });+ motionBuffer = device.createBuffer({ label: 'part motion', size: MAX_TRACE_PARTS * PART_MOTION_FLOATS * 4, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST });cache = await createWebGpuRadianceCache(device);- denoiser = await createWebGpuDenoiser(device, { traceWidth, traceHeight, outputWidth, outputHeight, tracedBuffers: traced, historyLimit, filterIterations, swapped });+ const createDenoiser = accumulateAtOutputSize ? createWebGpuFullResolutionDenoiser : createWebGpuDenoiser;+ denoiser = await createDenoiser(device, { traceWidth, traceHeight, outputWidth, outputHeight, tracedBuffers: traced, motionBuffer, historyLimit, filterIterations, swapped });canvas = suppliedCanvas ?? createOutputCanvas(outputWidth, outputHeight);context = canvas.getContext('webgpu');context.configure({ device, format: navigator.gpu.getPreferredCanvasFormat(), usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_DST, alphaMode: 'opaque' });@@ -81,9 +97,11 @@ export async function createWebGpuRealtimeTracer(scene, resolved, suppliedCanvas],}));cellSizes = cacheCellSizes(traceScene, cacheCellSize);- view = null;- resetHistory = true;- clearCache = true;+ if (!keepHistory) {+ view = null;+ resetHistory = true;+ clearCache = true;+ }};await loadScene(scene.parts, scene.environment ?? {});@@ -97,7 +115,20 @@ export async function createWebGpuRealtimeTracer(scene, resolved, suppliedCanvasdevice.queue.writeBuffer(parameterBuffer, 0, words);};+ // The motion table goes up with the first frame after an update, and a table of still parts follows it.+ const uploadPartMotion = () => {+ if (pendingMotion) {+ device.queue.writeBuffer(motionBuffer, 0, pendingMotion);+ pendingMotion = null;+ motionIsActive = true;+ } else if (motionIsActive) {+ device.queue.writeBuffer(motionBuffer, 0, partMotionTable(currentParts, currentParts));+ motionIsActive = false;+ }+ };+const encodeFrame = (nextView, previousView) => {+ uploadPartMotion();const encoder = device.createCommandEncoder();if (clearCache) {cache.clear(encoder);@@ -130,9 +161,17 @@ export async function createWebGpuRealtimeTracer(scene, resolved, suppliedCanvasget compileSeconds() { return kernel.compileSeconds; },get frameCount() { return frameIndex; },- /** A new scene: the radiance cache and the denoiser's history start over. */+ /**+ * New parts. The radiance cache and the denoiser's history start over, except when the part count and the environment are the+ * same and some part keeps its geometry: then a part that only moved (another matrix) is followed by the denoiser and a+ * part that changed in any other way loses its history, so a moving object does not leave a trail.+ */async update({ parts, environment }) {- if (parts) await loadScene(parts, environment ?? {});+ if (!parts) return;+ const sameScene = currentParts?.length === parts.length && (environment === undefined || environment === currentEnvironment);+ const motion = sameScene ? partMotionTable(currentParts, parts) : null;+ const followsSomePart = motion !== null && parts.some((part, index) => motion[index * PART_MOTION_FLOATS + 12] !== PART_MOTION_CHANGED);+ await loadScene(parts, environment ?? currentEnvironment ?? {}, followsSomePart ? (followPartMotion ? motion : partMotionTable(parts, parts)) : null);},/** Forgets the denoiser's history (the cache stays: it only depends on the scene). */@@ -182,7 +221,7 @@ export async function createWebGpuRealtimeTracer(scene, resolved, suppliedCanvasdisposed = true;cache.dispose();denoiser.dispose();- for (const buffer of [...traced, recordBuffer, parameterBuffer, ...kernel.uploaded.buffers]) buffer.destroy();+ for (const buffer of [...traced, recordBuffer, parameterBuffer, motionBuffer, ...kernel.uploaded.buffers]) buffer.destroy();context.unconfigure();},};diff --git a/src/path-tracer-reprojection.js b/src/path-tracer-reprojection.jsindex 974f5c04..4d0f5e7f 100644--- a/src/path-tracer-reprojection.js+++ b/src/path-tracer-reprojection.js@@ -60,6 +60,91 @@ export function isHistoryConsistent({ expectedDistance, historyDistance, normal,return closeInDepth && normalDot >= NORMAL_AGREEMENT;}+export const PART_MOTION_STATIC = 0;+export const PART_MOTION_RIGID = 1;+export const PART_MOTION_CHANGED = 2;+/** Floats per part in the motion table: three rows of a 3 by 4 matrix, then a row whose first value is the kind. */+export const PART_MOTION_FLOATS = 16;++const IDENTITY_MATRIX = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];++function matricesEqual(first, second) {+ return first.every((value, index) => value === second[index]);+}++/** The inverse of a column major matrix whose last row is 0 0 0 1 (rotation, scale and translation), or null when it is singular. */+export function invertAffine(matrix) {+ const [a, d, g, , b, e, h, , c, f, i, , x, y, z] = matrix;+ const cofactorA = e * i - f * h;+ const cofactorB = f * g - d * i;+ const cofactorC = d * h - e * g;+ const determinant = a * cofactorA + b * cofactorB + c * cofactorC;+ if (!(Math.abs(determinant) > 1e-12)) return null;+ const inverse3 = [+ cofactorA, c * h - b * i, b * f - c * e,+ cofactorB, a * i - c * g, c * d - a * f,+ cofactorC, b * g - a * h, a * e - b * d,+ ].map(value => value / determinant);+ const translation = [0, 1, 2].map(row => -(inverse3[row * 3] * x + inverse3[row * 3 + 1] * y + inverse3[row * 3 + 2] * z));+ return [+ inverse3[0], inverse3[3], inverse3[6], 0,+ inverse3[1], inverse3[4], inverse3[7], 0,+ inverse3[2], inverse3[5], inverse3[8], 0,+ translation[0], translation[1], translation[2], 1,+ ];+}++/** A column major 4 by 4 product `left * right`, so `right` is applied first. */+function multiplyAffine(left, right) {+ const product = new Array(16).fill(0);+ for (let column = 0; column < 4; column++) {+ for (let row = 0; row < 4; row++) {+ for (let inner = 0; inner < 4; inner++) product[column * 4 + row] += left[inner * 4 + row] * right[column * 4 + inner];+ }+ }+ return product;+}++/**+ * Per part, how its surface moved between two frames, as the table the denoiser reads (PART_MOTION_FLOATS per part).+ * A part with the same geometry and a different matrix is rigid: its rows map a point of this frame to where the same+ * surface point was in the previous frame (previous matrix times the inverse of the current one). A part whose geometry,+ * skin or matrix cannot be followed is PART_MOTION_CHANGED, and history is rejected on it.+ */+export function partMotionTable(previousParts, parts) {+ const table = new Float32Array(Math.max(1, parts.length) * PART_MOTION_FLOATS);+ parts.forEach((part, index) => {+ const base = index * PART_MOTION_FLOATS;+ const previous = previousParts?.[index];+ const matrix = part.matrix ?? IDENTITY_MATRIX;+ const previousMatrix = previous?.matrix ?? IDENTITY_MATRIX;+ const sameSurface = Boolean(previous) && previous.geometry === part.geometry && !part.skin && !previous.skin;+ let kind = PART_MOTION_CHANGED;+ let relative = IDENTITY_MATRIX;+ if (sameSurface && matricesEqual(previousMatrix, matrix)) {+ kind = PART_MOTION_STATIC;+ } else if (sameSurface) {+ const inverse = invertAffine(matrix);+ if (inverse) {+ relative = multiplyAffine(previousMatrix, inverse);+ kind = PART_MOTION_RIGID;+ }+ }+ for (let row = 0; row < 3; row++) {+ for (let column = 0; column < 4; column++) table[base + row * 4 + column] = relative[column * 4 + row];+ }+ table[base + 12] = kind;+ });+ return table;+}++/** Where the surface point `point` of this frame was in the previous frame, from the table; null when history cannot be reused there. */+export function previousPointOfPart(table, partIndex, point) {+ const base = partIndex * PART_MOTION_FLOATS;+ if (table[base + 12] === PART_MOTION_CHANGED) return null;+ return [0, 1, 2].map(row => table[base + row * 4] * point[0] + table[base + row * 4 + 1] * point[1] + table[base + row * 4 + 2] * point[2] + table[base + row * 4 + 3]);+}+export const REPROJECTION_GLSL = `uniform vec3 uEye;uniform vec3 uForward;diff --git a/tests/path-tracer-realtime-webgpu-device.test.js b/tests/path-tracer-realtime-webgpu-device.test.jsindex 638f367c..b551032a 100644--- a/tests/path-tracer-realtime-webgpu-device.test.js+++ b/tests/path-tracer-realtime-webgpu-device.test.js@@ -122,7 +122,7 @@ test('the WebGPU real-time tracer encodes a frame per call and ping-pongs its hiawait tracer.update({});assert.equal(recorded.clears, 1, 'no parts, no new scene');- await tracer.update({ parts: scene.parts });+ await tracer.update({ parts: [{ ...scene.parts[0], geometry: { ...floor } }] });tracer.frame(camera);assert.equal(recorded.clears, 2, 'a new scene empties the cache again');@@ -132,3 +132,41 @@ test('the WebGPU real-time tracer encodes a frame per call and ping-pongs its hiassert.equal(recorded.unconfigured, 1);assert.throws(() => tracer.frame(camera), /disposed/);});++test('an update that only moves a part keeps the history and sends the part motion with the next frame', async () => {+ installGpu(async () => fakeAdapter);+ const tracer = await createRealtimeTracer(scene, { backend: 'webgpu', width: 64, height: 32, canvas: fakeCanvas });+ tracer.frame(camera);+ const motionWrites = [];+ const originalWriteBuffer = fakeDevice.queue.writeBuffer;+ fakeDevice.queue.writeBuffer = (buffer, offset, data) => {+ if (buffer.label === 'part motion') motionWrites.push(Array.from(data.slice(0, 16)));+ };+ const clearsBefore = recorded.clears;+ const slideMatrix = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0.5, 0, 0, 1];+ await tracer.update({ parts: [{ ...scene.parts[0], matrix: slideMatrix }] });+ tracer.frame(camera);+ tracer.frame(camera);+ tracer.frame(camera);+ fakeDevice.queue.writeBuffer = originalWriteBuffer;++ assert.equal(recorded.clears, clearsBefore, 'the cache and the history stay');+ assert.equal(motionWrites.length, 2, 'the moved table once, then the still table once');+ const [moved, still] = motionWrites;+ assert.equal(moved[12], 1, 'the part is rigid');+ assert.equal(moved[3], -0.5, 'a point of this frame was half a unit back along x');+ assert.equal(still[12], 0, 'then the part is still');+ tracer.dispose();+});++test('the denoiser with its history at the output size runs the same frame', async () => {+ installGpu(async () => fakeAdapter);+ const tracer = await createRealtimeTracer(scene, { backend: 'webgpu', width: 64, height: 32, canvas: fakeCanvas, accumulateAtOutputSize: true });+ const dispatchesBefore = recorded.dispatches;+ tracer.frame(camera);+ tracer.frame({ ...camera, eye: [0.1, 1, 4] });+ assert.ok(recorded.dispatches - dispatchesBefore >= 12, 'trace, cache, resolve, a-trous and present passes for two frames');+ const picture = await tracer.readFrame();+ assert.equal(picture.rgba.length, 64 * 32 * 4);+ tracer.dispose();+});diff --git a/tests/path-tracer-reprojection.test.js b/tests/path-tracer-reprojection.test.jsindex cc64b1e5..433cf37a 100644--- a/tests/path-tracer-reprojection.test.js+++ b/tests/path-tracer-reprojection.test.js@@ -2,7 +2,47 @@ import assert from 'node:assert/strict';import test from 'node:test';import { createCamera } from '../src/path-tracer.js';-import { bilinearTaps, isHistoryConsistent, motionVector, pixelRayDirection, projectToPixel } from '../src/path-tracer-reprojection.js';+import {+ PART_MOTION_CHANGED, PART_MOTION_FLOATS, PART_MOTION_RIGID, PART_MOTION_STATIC, bilinearTaps, invertAffine, isHistoryConsistent, motionVector,+ partMotionTable, pixelRayDirection, previousPointOfPart, projectToPixel,+} from '../src/path-tracer-reprojection.js';++function transformPoint(matrix, point) {+ return [0, 1, 2].map(axis => matrix[axis] * point[0] + matrix[4 + axis] * point[1] + matrix[8 + axis] * point[2] + matrix[12 + axis]);+}++test('a part that slid is followed, a part that changed shape is not', () => {+ const geometry = { positions: new Float32Array(9) };+ const slid = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 2, 0, 0, 1];+ const before = [{ geometry }, { geometry }, { geometry }];+ const after = [{ geometry }, { geometry, matrix: slid }, { geometry: { positions: new Float32Array(9) } }];+ const table = partMotionTable(before, after);+ assert.equal(table.length, 3 * PART_MOTION_FLOATS);+ assert.equal(table[12], PART_MOTION_STATIC);+ assert.equal(table[PART_MOTION_FLOATS + 12], PART_MOTION_RIGID);+ assert.equal(table[2 * PART_MOTION_FLOATS + 12], PART_MOTION_CHANGED);+ assert.deepEqual(previousPointOfPart(table, 0, [1, 2, 3]), [1, 2, 3]);+ assert.deepEqual(previousPointOfPart(table, 1, [3, 0, 0]), [1, 0, 0], 'a point on the part was 2 units back');+ assert.equal(previousPointOfPart(table, 2, [0, 0, 0]), null);+});++test('a part that turned and moved maps its points back through the previous matrix', () => {+ const geometry = { positions: new Float32Array(9) };+ const quarterTurn = [0, 0, -1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1];+ const turnedFurther = [0.6, 0, -0.8, 0, 0, 1, 0, 0, 0.8, 0, 0.6, 0, 5, 0, 2, 1];+ const table = partMotionTable([{ geometry, matrix: quarterTurn }], [{ geometry, matrix: turnedFurther }]);+ const surfacePoint = [1, 2, 3];+ const found = previousPointOfPart(table, 0, transformPoint(turnedFurther, surfacePoint));+ transformPoint(quarterTurn, surfacePoint).forEach((expected, axis) => assert.ok(Math.abs(found[axis] - expected) < 1e-5, `axis ${axis}`));+});++test('invertAffine undoes a matrix and refuses a singular one', () => {+ const matrix = [2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 4, 0, 1, 2, 3, 1];+ const inverse = invertAffine(matrix);+ const point = [5, 6, 7];+ transformPoint(inverse, transformPoint(matrix, point)).forEach((value, axis) => assert.ok(Math.abs(value - point[axis]) < 1e-9));+ assert.equal(invertAffine([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]), null);+});const WIDTH = 480;const HEIGHT = 270;