Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Build the GPU path tracer kernel faster: one emitter loop holds one shadow ray (#861)

Nguyen Xuan An3864e1fada08
diff --git a/bench/reference/cold-draw.mjs b/bench/reference/cold-draw.mjs
new file mode 100644
index 00000000..1c22e755
--- /dev/null
+++ b/bench/reference/cold-draw.mjs
@@ -0,0 +1,36 @@
+// Splits the first path traced draw of a fresh page into phases (issue #861). One fresh browser page per run, so nothing is warm.
+// node bench/reference/cold-draw.mjs <scene id or recipe.json> [--runs 1] [--width 960] [--height 540]
+// Run it behind the GPU lock: bash ~/.agents/tools/gpu-lock.sh node bench/reference/cold-draw.mjs lit-interior
+import { readFile } from 'node:fs/promises';
+import { constants, setPriority } from 'node:os';
+import { chromium } from 'playwright';
+import { createServer } from 'vite';
+
+const CHROME = process.env.CHROME_PATH ?? 'C:/Program Files/Google/Chrome/Application/chrome.exe';
+const argumentsGiven = process.argv.slice(2);
+const sceneId = argumentsGiven[0];
+const valueAfter = (flag, fallback) => (argumentsGiven.includes(flag) ? argumentsGiven[argumentsGiven.indexOf(flag) + 1] : fallback);
+const runs = Number(valueAfter('--runs', 1));
+const options = { width: Number(valueAfter('--width', 960)), height: Number(valueAfter('--height', 540)), bounces: 8, denoise: false, maxSeconds: 1800 };
+const source = sceneId.endsWith('.json') ? JSON.parse(await readFile(sceneId, 'utf8')) : sceneId;
+
+setPriority(0, constants.priority.PRIORITY_BELOW_NORMAL);
+const server = await createServer({ server: { host: '127.0.0.1', port: 0, fs: { strict: false } }, logLevel: 'silent' });
+await server.listen();
+const serverUrl = `http://127.0.0.1:${server.httpServer.address().port}`;
+const browser = await chromium.launch({ executablePath: CHROME, headless: true, args: ['--enable-webgl', '--ignore-gpu-blocklist', '--disable-renderer-backgrounding'] });
+try {
+ for (let run = 0; run < runs; run++) {
+ const page = await browser.newPage({ viewport: { width: 960, height: 540 } });
+ page.on('pageerror', error => console.log('PAGE ERROR', error.message));
+ page.setDefaultTimeout(0);
+ await page.goto(`${serverUrl}/bench/reference/harness.html`, { timeout: 180000 });
+ await page.waitForFunction(() => window.profileColdFirstDraw, null, { timeout: 180000 });
+ const result = await page.evaluate(([scene, tracerOptions]) => window.profileColdFirstDraw(scene, tracerOptions), [source, options]);
+ console.log(JSON.stringify(result));
+ await page.close();
+ }
+} finally {
+ await browser.close();
+ await server.close();
+}
diff --git a/bench/reference/harness.js b/bench/reference/harness.js
index 41ac7bb7..24d433d0 100644
--- a/bench/reference/harness.js
+++ b/bench/reference/harness.js
@@ -484,6 +484,75 @@ async function measureProgressiveFrameTime(source, options, frames = 200, warmup
window.renderProgressiveCheckpoints = renderProgressiveCheckpoints;
window.measureProgressiveFrameTime = measureProgressiveFrameTime;
+// Splits the first path traced draw of a fresh page into phases, in seconds (issue #861; bench/reference/cold-draw.mjs).
+async function profileColdFirstDraw(source, options) {
+ const phases = {};
+ let mark = performance.now();
+ const lap = (name) => {
+ const now = performance.now();
+ phases[name] = Number(((now - mark) / 1000).toFixed(3));
+ mark = now;
+ };
+ const scene = await loadTraceSceneFor(source);
+ lap('loadScene');
+ const [{ buildTraceScene }, gpuModule, shaderModule, packModule, pathTracerModule] = await Promise.all([
+ import('/src/path-tracer-scene.js'), import('/src/path-tracer-gpu.js'), import('/src/path-tracer-gpu-shader.js'), import('/src/path-tracer-gpu-pack.js'), import('/src/path-tracer.js'),
+ ]);
+ lap('importModules');
+ const traceScene = buildTraceScene(scene.parts, scene.environment ?? {});
+ lap('buildTraceScene');
+ const normalized = pathTracerModule.normalizePathTracerOptions({ ...options, backend: 'gpu', samples: 1 });
+ const { gl } = gpuModule.getContext();
+ lap('createContext');
+ const variant = { hasSubsurface: packModule.sceneUsesSubsurface(traceScene.materials), texelBake: false, blueNoise: false, lightImportance: false, radianceCache: false, lightTree: false };
+ const fragmentSource = shaderModule.buildFragmentSource(variant);
+ lap('buildSource');
+ const parallel = gl.getExtension('KHR_parallel_shader_compile');
+ const program = gl.createProgram();
+ for (const [kind, text] of [[gl.VERTEX_SHADER, shaderModule.VERTEX_SOURCE], [gl.FRAGMENT_SHADER, fragmentSource]]) {
+ const shader = gl.createShader(kind);
+ gl.shaderSource(shader, text);
+ gl.compileShader(shader);
+ gl.attachShader(program, shader);
+ }
+ lap('submitCompile');
+ gl.linkProgram(program);
+ lap('submitLink');
+ if (parallel) {
+ while (!gl.getProgramParameter(program, parallel.COMPLETION_STATUS_KHR)) await new Promise(resolve => setTimeout(resolve, 5));
+ }
+ lap('waitCompletion');
+ if (!gl.getProgramParameter(program, gl.LINK_STATUS)) throw new Error(gl.getProgramInfoLog(program));
+ lap('linkStatus');
+ gl.useProgram(program);
+ const { lightCount } = gpuModule.uploadScene(gl, program, traceScene, normalized);
+ gl.finish();
+ lap('uploadScene');
+ const camera = pathTracerModule.createCamera(scene.camera, normalized.width, normalized.height);
+ gpuModule.setSceneUniforms(gl, program, traceScene, camera, normalized, lightCount);
+ gpuModule.createTargets(gl, normalized.width, normalized.height);
+ lap('uniformsAndTargets');
+ const band = gpuModule.createBandState(normalized.height);
+ const pixel = new Float32Array(4);
+ const draw = async (name, divisor) => {
+ const drawWidth = Math.ceil(normalized.width / divisor);
+ const drawHeight = Math.ceil(normalized.height / divisor);
+ gl.uniform2i(gl.getUniformLocation(program, 'uSize'), drawWidth, drawHeight);
+ await gpuModule.drawOneSample(gl, program, drawWidth, drawHeight, 0, band, () => {});
+ gl.readBuffer(gl.COLOR_ATTACHMENT0);
+ gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.FLOAT, pixel);
+ lap(name);
+ };
+ await draw('firstDrawQuarter', 4);
+ await draw('secondDrawQuarter', 4);
+ await draw('drawHalf', 2);
+ await draw('drawFull', 1);
+ gpuModule.releaseResources(gl);
+ const coldTotal = Object.entries(phases).filter(([name]) => name !== 'secondDrawQuarter' && name !== 'drawHalf' && name !== 'drawFull').reduce((sum, [, value]) => sum + value, 0);
+ return { gpu: gpuModule.getContext().gpu, fragmentSourceKilobytes: Math.round(fragmentSource.length / 1024), phases, coldTotal: Number(coldTotal.toFixed(2)) };
+}
+window.profileColdFirstDraw = profileColdFirstDraw;
+
window.loadTraceSceneFor = loadTraceSceneFor;
// The masked leaf cards of bench/native-tracer/cutout-scene.js on the path tracer's gpu or webgpu backend, as the PNG of the
diff --git a/changes/861-cold-first-draw.md b/changes/861-cold-first-draw.md
new file mode 100644
index 00000000..9023c428
--- /dev/null
+++ b/changes/861-cold-first-draw.md
@@ -0,0 +1 @@
+- The first path traced draw of a page on the WebGL2 backend builds its kernel faster, 108 s to 27 s for `lit-interior` on an RTX 3060, because the emitter loop now holds one copy of the shadow ray, with identical pixels (#861)
diff --git a/docs/path-tracer.md b/docs/path-tracer.md
index e9232dcf..7b6d2414 100644
--- a/docs/path-tracer.md
+++ b/docs/path-tracer.md
@@ -174,3 +174,7 @@ Measured on the RTX 3060 with `bench/realtime-tracer/run.mjs` (1920 by 1080 outp
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.
+
+### Cold first draw (issue #861)
+
+The first path traced draw of a fresh page on the `gpu` backend took 108 s for one sample of `lit-interior` (960 by 540, RTX 3060, ANGLE on Direct3D 11, a profile with no shader cache, `bench/reference/cold-draw.mjs`). All of it was the driver building the kernel: scene load, BVH, packing and texture upload together took 0.15 s, linking took 73.9 s and the first dispatch another 34.1 s (the driver finishes its work on first use), while every later draw took 7 to 35 ms. The compiler inlines each call, and `addLights` called the emitter sampling and the shadow ray from four places, so the traversal was built five times. `addLights` now has one loop for "every emitter" and "one chosen emitter" and the kernel holds one shadow ray (`tests/path-tracer-gpu-kernel-size.test.js` counts the call sites of each variant and the size of the plain kernel). After the change three cold runs on the loaded machine took 27.4, 35.6 and 46.6 s (15.6 to 25.4 s linking, 11.6 to 21.1 s first dispatch), and the old and new kernels compiled side by side in one page took 36 and 7.8 s, 23 and 6.5 s. The progressive tracer at 320 by 180 first step with the random, importance and tree kernels went from 22.9, 18.5 and 38.7 s to 10.3, 9.2 and 14.1 s. The cold first draw is not yet reliably under 30 s: the two places that trace rays (the camera path and the shadow ray) are still two copies of the traversal, and merging them is the next cut. The images of `lit-interior` and `spot-area` at 1, 4 and 8 samples with the random, importance and tree kernels are byte for byte the same as before (18 PNGs, MD5). A second page of the same browser session starts in 0.14 s, since the driver program cache is shared, and the compiled kernel is kept per page by `getKernel`. The `webgpu` backend compiles its kernel in about 0.5 s.
diff --git a/src/path-tracer-gpu-shader.js b/src/path-tracer-gpu-shader.js
index 8d0847e1..2244a0e8 100644
--- a/src/path-tracer-gpu-shader.js
+++ b/src/path-tracer-gpu-shader.js
@@ -1113,36 +1113,36 @@ int chooseEmitterByImportance(vec3 normal, out float probability) {
}
${lightTree ? LIGHT_TREE_FUNCTIONS : ''}
-/** The first hit lights from every emitter; later hits draw one, and divide by the chance of drawing it. */
+/**
+ * The first hit lights from every emitter; later hits draw one, and divide by the chance of drawing it. The emitters and the
+ * single draw share one loop, so the shader holds one copy of the emitter sampling and the shadow ray (issue #861).
+ */
void addLights(int depth, vec3 normal) {
bool firstHitByTree = LIGHT_TREE && uLightCount > LIGHT_TREE_FIRST_HIT_LIGHTS;
- if ((depth == 0 && !STOCHASTIC_FIRST_HIT && !firstHitByTree) || uEmitterCount <= 1) {
+ bool everyEmitter = (depth == 0 && !STOCHASTIC_FIRST_HIT && !firstHitByTree) || uEmitterCount <= 1;
+ int turns = uEmitterCount;
+ int chosen = 0;
+ bool shareAfterwards = false;
+ if (everyEmitter) {
selectionProbability = 1.0;
- for (int emitter = 0; emitter < uEmitterCount; emitter++) {
- sampleEmitter(depth, normal, emitter);
- flushShadowRay();
- }
- return;
- }
-${lightTree ? ` if (LIGHT_TREE) {
- int chosen = chooseEmitterByTree(normal, selectionProbability);
- if (chosen >= 0) {
- sampleEmitter(depth, normal, chosen);
- flushShadowRay();
- }
+${lightTree ? ` } else if (LIGHT_TREE) {
+ chosen = chooseEmitterByTree(normal, selectionProbability);
+ turns = chosen >= 0 ? 1 : 0;
// The sky is drawn with its uniform share whatever light was chosen: that share weighs the next escape ray.
+ shareAfterwards = true;
+` : ''} } else if (IMPORTANT_LIGHTS) {
+ turns = 1;
+ chosen = chooseEmitterByImportance(normal, selectionProbability);
+ } else {
+ turns = 1;
selectionProbability = 1.0 / float(uEmitterCount);
- return;
+ chosen = int(floor(nextRandom() * float(uEmitterCount)));
}
-` : ''} if (IMPORTANT_LIGHTS) {
- int chosen = chooseEmitterByImportance(normal, selectionProbability);
- sampleEmitter(depth, normal, chosen);
+ for (int turn = 0; turn < turns; turn++) {
+ sampleEmitter(depth, normal, everyEmitter ? turn : chosen);
flushShadowRay();
- return;
}
- selectionProbability = 1.0 / float(uEmitterCount);
- sampleEmitter(depth, normal, int(floor(nextRandom() * float(uEmitterCount))));
- flushShadowRay();
+ if (shareAfterwards) selectionProbability = 1.0 / float(uEmitterCount);
}
void addEscape(int depth, float previousPdf, bool afterMirror) {
diff --git a/tests/path-tracer-gpu-kernel-size.test.js b/tests/path-tracer-gpu-kernel-size.test.js
new file mode 100644
index 00000000..4a70894c
--- /dev/null
+++ b/tests/path-tracer-gpu-kernel-size.test.js
@@ -0,0 +1,40 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+
+import { buildFragmentSource } from '../src/path-tracer-gpu-shader.js';
+
+// The shader compiler of the GPU drivers inlines every function call, so a function called from n places is built n times, and
+// the traversal is the costliest of them: before these limits the first draw of a page took 108 s on an RTX 3060 (issue #861,
+// bench/reference/cold-draw.mjs). A new call site, or a larger kernel, needs a measurement of the cold first draw first.
+
+const VARIANTS = {
+ plain: {},
+ subsurface: { hasSubsurface: true },
+ texelBake: { texelBake: true },
+ blueNoise: { blueNoise: true },
+ importance: { lightImportance: true },
+ tree: { lightTree: true },
+ radianceCache: { radianceCache: true },
+ everything: { hasSubsurface: true, blueNoise: true, lightImportance: true, lightTree: true, radianceCache: true },
+};
+
+function callSites(source, name) {
+ const calls = source.match(new RegExp(`(?<![A-Za-z0-9_])${name}\\(`, 'g')) ?? [];
+ return calls.length - 1; // the definition
+}
+
+for (const [name, variant] of Object.entries(VARIANTS)) {
+ test(`the ${name} kernel holds one shadow ray and one emitter sampler`, () => {
+ const source = buildFragmentSource(variant);
+ assert.equal(callSites(source, 'sampleEmitter'), 1, 'sampleEmitter is called from one place, in addLights');
+ assert.equal(callSites(source, 'flushShadowRay'), 1, 'flushShadowRay is called from one place, in addLights');
+ assert.equal(callSites(source, 'occluded'), 1, 'occluded is called from flushShadowRay only');
+ assert.equal(callSites(source, 'traceRay'), 2, 'traceRay is called by intersectScene and occluded only');
+ assert.equal(callSites(source, 'intersectScene'), variant.hasSubsurface ? 2 : 1, 'intersectScene is called from tracePath, and from the random walk of a subsurface scene');
+ });
+}
+
+test('the plain kernel stays under its source size', () => {
+ const kilobytes = buildFragmentSource().length / 1024;
+ assert.ok(kilobytes < 48, `the plain kernel is ${kilobytes.toFixed(1)} KB; it was 46 KB when the cold first draw measured 27 s`);
+});