3D engine that AI agents drive
Drop the output-size history option: 6 to 7 ms for no FLIP gain (issue #624)
Nguyen Xuan Anaa53e07bf049
diff --git a/bench/realtime-tracer/run.mjs b/bench/realtime-tracer/run.mjsindex d440699d..ae16b062 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|ghost|all] [--output-history] [--rest-seconds 1 | --rest-frames 60] [--frames 200] [--width 1920] [--height 1080] [--bounces 2] [--repetitions 3] [--backend gpu|webgpu]+// [--mode time|rest|move|ghost|all] [--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,7 @@ 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'), accumulateAtOutputSize: argumentsGiven.includes('--output-history'), filterIterations: valueAfter('--iterations', undefined) === undefined ? undefined : Number(valueAfter('--iterations', undefined)) };+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 restSeconds = Number(valueAfter('--rest-seconds', 1));const restFrames = Number(valueAfter('--rest-frames', 0));const recipeName = id => id.replace(/^.*[\/]/, '').replace(/\.recipe\.json$|\.json$/, '');diff --git a/changes/624-realtime-2.md b/changes/624-realtime-2.mdindex 5bdaa7bd..67d6d41a 100644--- a/changes/624-realtime-2.md+++ b/changes/624-realtime-2.md@@ -1,2 +1 @@-- 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 f97e15dd..f6385dbc 100644--- a/docs/path-tracer.md+++ b/docs/path-tracer.md@@ -171,6 +171,6 @@ What it is not (the issue asked for more): primary hits are traced, not taken frMeasured 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, second round (issue #624, `bench/realtime-tracer/run.mjs --backend webgpu`, same card, a loaded machine, so compare rows of one run only). Tried and not kept: a temporal history at the output size (each output pixel gathering the traced samples by their jittered positions) cost 6 to 7 ms on the demo room (21.4 to 27.7 ms at rest) and did not lower FLIP against Cycles on `lit-interior` (0.0579 against 0.0597 after 60 frames), so the remaining error is the lighting, 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 2b9b42c0..d186b78d 100644--- a/src/path-tracer-realtime-options.js+++ b/src/path-tracer-realtime-options.js@@ -5,8 +5,6 @@ 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;@@ -28,7 +26,6 @@ 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.jsdeleted file mode 100644index d42c6b94..00000000--- a/src/path-tracer-realtime-webgpu-fullres.js+++ /dev/null@@ -1,341 +0,0 @@-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 b2d4806e..8f1aad15 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 (with the sample's jitter), normal and material to the frame buffer and the record.+// - an entry point that writes the first hit's radiance, albedo, 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,9 +149,7 @@ 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);- // 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[pixelCount + pixel] = vec4<f32>(featureAlbedo, 0.0);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 772a6493..9cc519d0 100644--- a/src/path-tracer-realtime-webgpu-passes.js+++ b/src/path-tracer-realtime-webgpu-passes.js@@ -12,13 +12,13 @@ import { DEPTH_TOLERANCE, NORMAL_AGREEMENT, PART_MOTION_CHANGED, PART_MOTION_FLOconst FIXED_POINT_SCALE = 1024;const RECORD_LIMIT = 32;-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 MINIMUM_ALBEDO = 0.02;+const SPATIAL_VARIANCE_HISTORY = 4;+const FIREFLY_DEVIATIONS = 2.5;+const SHORT_HISTORY_LENGTH = 8;const CACHE_WORKGROUP_SIZE = 256;const CACHE_PARAMETER_BYTES = 16;-export const DENOISE_PARAMETER_BYTES = 12 * 16;+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}`;-export const DENOISE_COMMON = /* wgsl */ `+const DENOISE_COMMON = /* wgsl */ `struct DenoiseParams {eye: vec4<f32>,forward: vec4<f32>,@@ -124,7 +124,7 @@ fn insideTraceImage(texel: vec2<i32>) -> bool {* 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 */ `+const partMotionSource = binding => /* wgsl */ `@group(0) @binding(${binding}) var<storage, read> partMotion: array<vec4<f32>>;fn previousPointOf(point: vec3<f32>, material: f32) -> vec4<f32> {@@ -355,11 +355,11 @@ fn present(@builtin(global_invocation_id) id: vec3<u32>) {}`;-export function createPipeline(device, module, entryPoint) {+function createPipeline(device, module, entryPoint) {return device.createComputePipelineAsync({ layout: 'auto', compute: { module, entryPoint } });}-export async function compileModule(device, label, code) {+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');@@ -367,9 +367,9 @@ export async function compileModule(device, label, code) {return module;}-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 } })) });+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 } })) });/*** The cache table (CACHE_SLOT_COUNT vec4: mean radiance and the number of records behind it) and the pass that folds the@@ -409,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;-export function writeDenoiseParameters(device, buffer, inputs, sizes, step, rowWords, swapped) {+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);diff --git a/src/path-tracer-realtime-webgpu.js b/src/path-tracer-realtime-webgpu.jsindex 6f178ec5..86428153 100644--- a/src/path-tracer-realtime-webgpu.js+++ b/src/path-tracer-realtime-webgpu.js@@ -4,7 +4,6 @@ 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';@@ -36,7 +35,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, accumulateAtOutputSize, followPartMotion } = resolved;+ const { outputWidth, outputHeight, traceWidth, traceHeight, options, historyLimit, filterIterations, cacheCellSize, followPartMotion } = resolved;let traceScene = null;let kernel = null;let device = null;@@ -82,8 +81,7 @@ export async function createWebGpuRealtimeTracer(scene, resolved, suppliedCanvasparameterBuffer = 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);- const createDenoiser = accumulateAtOutputSize ? createWebGpuFullResolutionDenoiser : createWebGpuDenoiser;- denoiser = await createDenoiser(device, { traceWidth, traceHeight, outputWidth, outputHeight, tracedBuffers: traced, motionBuffer, historyLimit, filterIterations, swapped });+ denoiser = await createWebGpuDenoiser(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' });diff --git a/tests/path-tracer-realtime-webgpu-device.test.js b/tests/path-tracer-realtime-webgpu-device.test.jsindex b551032a..c0ae231e 100644--- a/tests/path-tracer-realtime-webgpu-device.test.js+++ b/tests/path-tracer-realtime-webgpu-device.test.js@@ -158,15 +158,3 @@ test('an update that only moves a part keeps the history and sends the part motiassert.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();-});