Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Add a Chrome GPU proof of shadow-only parts on the WebGL2, WebGPU and real-time tracers (#973)

Nguyen Xuan Anf8e903eb5825
diff --git a/scripts/smoke-shadow-only-web.html b/scripts/smoke-shadow-only-web.html
new file mode 100644
index 00000000..c77be47c
--- /dev/null
+++ b/scripts/smoke-shadow-only-web.html
@@ -0,0 +1,109 @@
+<!doctype html>
+<meta charset="utf-8">
+<script type="importmap">{"imports":{"gl-matrix":"/node_modules/gl-matrix/esm/index.js"}}</script>
+<script type="module">
+import { box } from '/src/geometry.js';
+import { buildTraceScene } from '/src/path-tracer-scene.js';
+import { createCamera, normalizePathTracerOptions, renderPathTracedImage } from '/src/path-tracer.js';
+import { traceOnWebGpu } from '/src/path-tracer-webgpu.js';
+import { createRealtimeTracer } from '/src/path-tracer-realtime.js';
+
+function placed(translation, scale) {
+ return new Float32Array([scale[0], 0, 0, 0, 0, scale[1], 0, 0, 0, 0, scale[2], 0, translation[0], translation[1], translation[2], 1]);
+}
+const surface = color => ({ tag: 'surface', metallic: 0, roughness: 1, emission: 0, color });
+const environment = { keyColor: [3, 3, 3], lightDirection: [0, 1, 0] };
+const camera = { eye: [0, 6, 0], target: [0, 0, 0], up: [0, 0, -1], fov: 40 };
+const floor = () => ({ name: 'floor', geometry: box(0, 1), matrix: placed([0, -0.05, 0], [20, 0.1, 20]), material: surface([0.5, 0.5, 0.5]) });
+const caster = extra => ({ name: 'caster', geometry: box(0, 1), matrix: placed([0, 1.5, 0], [2, 0.2, 2]), material: surface([0.9, 0.2, 0.1]), ...extra });
+const CASES = [['no caster', () => [floor()]], ['visible caster', () => [floor(), caster()]], ['shadowOnly caster', () => [floor(), caster({ shadowOnly: true })]]];
+
+const luminance = (r, g, b) => 0.2126 * r + 0.7152 * g + 0.0722 * b;
+
+/** Mean luminance of the centre square (under the caster) and of the outer ring (floor the shadow never reaches). */
+function regions(pixelAt, size) {
+ const quarter = Math.floor(size / 4);
+ let centreSum = 0, centreCount = 0, ringSum = 0, ringCount = 0;
+ for (let y = 0; y < size; y++) {
+ for (let x = 0; x < size; x++) {
+ const value = pixelAt(x, y);
+ if (x >= quarter && x < size - quarter && y >= quarter && y < size - quarter) { centreSum += value; centreCount++; }
+ else if (x < 3 || y < 3 || x >= size - 3 || y >= size - 3) { ringSum += value; ringCount++; }
+ }
+ }
+ return { centre: centreSum / centreCount, ring: ringSum / ringCount };
+}
+
+function draw(canvasContext, rgba, size, column) {
+ const image = new ImageData(new Uint8ClampedArray(rgba), size, size);
+ canvasContext.putImageData(image, column * size, 0);
+}
+
+function toRgba(linear, size) {
+ const bytes = new Uint8ClampedArray(size * size * 4);
+ for (let index = 0; index < size * size; index++) {
+ for (let channel = 0; channel < 3; channel++) bytes[index * 4 + channel] = 255 * Math.min(1, Math.max(0, linear[index * 3 + channel])) ** (1 / 2.2);
+ bytes[index * 4 + 3] = 255;
+ }
+ return bytes;
+}
+
+async function traceLinear(backend, parts, size) {
+ const options = { width: size, height: size, samples: 64, bounces: 2, denoise: false, threads: 1 };
+ if (backend === 'gpu') return (await renderPathTracedImage({ parts, environment, camera }, { ...options, backend: 'gpu' })).linear;
+ const kernel = backend === 'webgpu-wavefront' ? 'wavefront' : 'megakernel';
+ const resolved = normalizePathTracerOptions({ ...options, backend: 'webgpu' });
+ const traced = await traceOnWebGpu(buildTraceScene(parts, environment), createCamera(camera, size, size), { ...resolved, kernel }, () => {});
+ return traced.color;
+}
+
+async function realtimeRgba(parts, size) {
+ const canvas = document.createElement('canvas');
+ const tracer = await createRealtimeTracer({ parts, environment, camera }, { backend: 'webgpu', width: size, height: size, resolutionDivisor: 1, canvas });
+ if (tracer.backend !== 'webgpu') throw Object.assign(new Error(`real-time tracer fell back: ${tracer.fallbackReason}`), { unavailable: true });
+ for (let frame = 0; frame < 48; frame++) await tracer.frame(camera, { wait: true }).done;
+ const { rgba } = await tracer.readFrame();
+ tracer.dispose?.();
+ return rgba;
+}
+
+window.shadowOnlyProof = async (backend, size) => {
+ if (backend.startsWith('webgpu') && !navigator.gpu) return { backend, unavailable: true, error: 'navigator.gpu is missing on this page' };
+ const montage = document.createElement('canvas');
+ montage.width = size * CASES.length;
+ montage.height = size;
+ const context = montage.getContext('2d');
+ const frames = [];
+ const started = performance.now();
+ try {
+ for (const [index, [label, build]] of CASES.entries()) {
+ let rgba;
+ let pixelAt;
+ if (backend === 'webgpu-realtime') {
+ rgba = await realtimeRgba(build(), size);
+ pixelAt = (x, y) => luminance(rgba[(y * size + x) * 4], rgba[(y * size + x) * 4 + 1], rgba[(y * size + x) * 4 + 2]) / 255;
+ } else {
+ const linear = await traceLinear(backend, build(), size);
+ rgba = toRgba(linear, size);
+ pixelAt = (x, y) => luminance(linear[(y * size + x) * 3], linear[(y * size + x) * 3 + 1], linear[(y * size + x) * 3 + 2]);
+ }
+ draw(context, rgba, size, index);
+ frames.push({ label, ...regions(pixelAt, size) });
+ }
+ } catch (error) {
+ return { backend, unavailable: Boolean(error.unavailable) || /navigator\.gpu|adapter|WebGPU/i.test(error.message), error: error.message };
+ }
+ const [lit, visible, shadow] = frames;
+ // The real-time tracer returns ACES tone mapped bytes, which lift shadows, so its ratios are looser than the linear ones.
+ const toneMapped = backend === 'webgpu-realtime';
+ const darkening = toneMapped ? 0.5 : 0.25;
+ const separation = toneMapped ? 2 : 3;
+ const checks = {
+ castingShadowDarkensFloor: shadow.centre < lit.centre * darkening,
+ casterHiddenFromCamera: visible.centre > shadow.centre * separation,
+ shadowOnlyDifferentFromVisible: Math.abs(visible.centre - shadow.centre) > 0.2 * lit.centre,
+ ringUnchanged: Math.abs(shadow.ring - lit.ring) < 0.15 * lit.ring,
+ };
+ return { backend, status: Object.values(checks).every(Boolean) ? 'pass' : 'fail', frames, checks, seconds: (performance.now() - started) / 1000, montage: montage.toDataURL('image/png') };
+};
+</script>
diff --git a/scripts/smoke-shadow-only-web.mjs b/scripts/smoke-shadow-only-web.mjs
new file mode 100644
index 00000000..df27c588
--- /dev/null
+++ b/scripts/smoke-shadow-only-web.mjs
@@ -0,0 +1,96 @@
+// Proves shadow-only parts on the browser path tracers with real pixels: the WebGL2 tracer, the WebGPU megakernel and wavefront
+// kernels and the real-time WebGPU tracer each draw a floor three ways (no caster, a visible caster, a shadowOnly caster).
+// The shadow-only frame must not show the caster and the floor under it must be darker than the bare floor; every shader must
+// compile with no console error. Needs Chrome with a GPU (CHROME_PATH) and writes artifacts/973-web/ (pictures and result.json).
+// WebGPU may be missing on a runner; that backend is then reported as "unavailable" and the others still decide the exit code.
+import { createReadStream } from 'node:fs';
+import { mkdir, stat, writeFile } from 'node:fs/promises';
+import { createServer } from 'node:http';
+import { extname, join, normalize, resolve } from 'node:path';
+import { chromium } from 'playwright-core';
+
+const root = resolve(import.meta.dirname, '..');
+const outputDirectory = resolve(process.argv[2] ?? join(root, 'artifacts', '973-web'));
+const MIME = { '.js': 'application/javascript; charset=utf-8', '.html': 'text/html; charset=utf-8', '.json': 'application/json' };
+const SIZE = 48;
+
+const server = createServer(async (request, response) => {
+ const relative = normalize(decodeURIComponent(new URL(request.url, 'http://localhost').pathname));
+ const file = join(root, relative === '/' || relative === '\\' ? 'scripts/smoke-shadow-only-web.html' : relative);
+ if (!file.startsWith(root) || !(await stat(file).then(info => info.isFile(), () => false))) {
+ response.statusCode = 404;
+ response.end('not found');
+ return;
+ }
+ response.setHeader('Content-Type', MIME[extname(file)] ?? 'application/octet-stream');
+ createReadStream(file).pipe(response);
+});
+await new Promise(done => server.listen(0, '127.0.0.1', done));
+const origin = `http://localhost:${server.address().port}`;
+
+await mkdir(outputDirectory, { recursive: true });
+const GPU_FLAGS = ['--no-sandbox', '--ignore-gpu-blocklist', '--enable-unsafe-webgpu', '--enable-features=Vulkan,WebGPU,UnsafeWebGPU', '--disable-vulkan-surface', '--enable-webgl'];
+// WebGL2 is tried on each ANGLE backend in turn until the tracer's float render targets exist; the first that draws decides.
+const LAUNCHES = [
+ { label: 'angle-vulkan', args: ['--use-angle=vulkan'], backends: ['gpu', 'webgpu-megakernel', 'webgpu-wavefront', 'webgpu-realtime'] },
+ { label: 'angle-gl', args: ['--use-angle=gl'], backends: ['gpu'], onlyIfMissing: 'gpu' },
+ { label: 'angle-swiftshader', args: ['--use-angle=swiftshader', '--enable-unsafe-swiftshader'], backends: ['gpu'], onlyIfMissing: 'gpu' },
+];
+
+async function runLaunch(launch) {
+ const browser = await chromium.launch({ headless: true, executablePath: process.env.CHROME_PATH, args: [...GPU_FLAGS, ...launch.args] });
+ const page = await browser.newPage();
+ const consoleProblems = [];
+ page.on('console', message => {
+ const text = message.text();
+ // ReadPixels stall notes are driver performance hints, not shader errors.
+ if (['error', 'warning'].includes(message.type()) && !/GPU stall due to ReadPixels|CONTEXT_LOST_WEBGL: loseContext/.test(text)) consoleProblems.push(message.type() + ': ' + text);
+ });
+ page.on('pageerror', error => consoleProblems.push('pageerror: ' + error.message));
+ await page.goto(origin);
+ await page.waitForFunction(() => window.shadowOnlyProof !== undefined, null, { timeout: 15000 }).catch(error => {
+ console.error(consoleProblems.join('\n'));
+ throw error;
+ });
+ const environment = await page.evaluate(() => {
+ const gl = document.createElement('canvas').getContext('webgl2');
+ const info = gl?.getExtension('WEBGL_debug_renderer_info');
+ return { webgpu: Boolean(navigator.gpu), webglRenderer: info ? gl.getParameter(info.UNMASKED_RENDERER_WEBGL) : null, userAgent: navigator.userAgent };
+ });
+ const launchResults = [];
+ for (const backend of launch.backends) {
+ const problemsBefore = consoleProblems.length;
+ const result = await page.evaluate(([name, size]) => window.shadowOnlyProof(name, size), [backend, SIZE]).catch(error => ({ backend, error: error.message }));
+ result.launch = launch.label;
+ result.consoleProblems = consoleProblems.slice(problemsBefore);
+ launchResults.push(result);
+ if (result.montage) {
+ await writeFile(join(outputDirectory, backend + '.png'), Buffer.from(result.montage.split(',')[1], 'base64'));
+ delete result.montage;
+ }
+ }
+ await browser.close();
+ return { environment, launchResults };
+}
+
+const results = [];
+const environments = {};
+for (const launch of LAUNCHES) {
+ if (launch.onlyIfMissing && results.some(result => result.backend === launch.onlyIfMissing && result.status === 'pass')) continue;
+ const { environment, launchResults } = await runLaunch(launch);
+ environments[launch.label] = environment;
+ results.push(...launchResults);
+}
+server.close();
+
+await writeFile(join(outputDirectory, 'result.json'), JSON.stringify({ environments, results }, null, 2));
+console.log(JSON.stringify({ environments, results }, null, 2));
+// A backend that failed on one ANGLE backend but passed on a later one is reported, not counted as a failure of the proof.
+const passed = new Set(results.filter(result => result.status === 'pass').map(result => result.backend));
+const failed = results.filter(result => !passed.has(result.backend) && !result.unavailable)
+ .concat(results.filter(result => result.status === 'pass' && result.consoleProblems.length > 0));
+if (failed.length > 0 || passed.size === 0) {
+ console.error('shadow-only web proof failed: ' + (failed.map(result => result.backend).join(', ') || 'nothing ran'));
+ process.exit(1);
+}
+console.log('shadow-only web proof passed on ' + [...passed].join(', '));