Skip to content

3D engine that AI agents drive

Let scene files turn on levels of detail for dense scans (Part of #1163)

Nguyen Xuan An3387721aa1c8
diff --git a/bench/glaze-passes/diff-frames.mjs b/bench/glaze-passes/diff-frames.mjs
index 4ebde803..1aa27670 100644
--- a/bench/glaze-passes/diff-frames.mjs
+++ b/bench/glaze-passes/diff-frames.mjs
@@ -11,16 +11,12 @@ if (before.length !== after.length) throw new Error(`The frames differ in size:
let maximumChannelDifference = 0;
let differingPixels = 0;
-let squaredSum = 0;
-let pixelsOverEleven = 0;
const marks = Buffer.alloc(before.length);
for (let offset = 0; offset < before.length; offset += 4) {
let pixelDifference = 0;
for (let channel = 0; channel < 4; channel++) {
pixelDifference = Math.max(pixelDifference, Math.abs(before[offset + channel] - after[offset + channel]));
}
- for (let channel = 0; channel < 3; channel++) squaredSum += (before[offset + channel] - after[offset + channel]) ** 2;
- if (pixelDifference > 11) pixelsOverEleven++;
maximumChannelDifference = Math.max(maximumChannelDifference, pixelDifference);
const differs = pixelDifference > 0;
if (differs) differingPixels++;
@@ -29,5 +25,5 @@ for (let offset = 0; offset < before.length; offset += 4) {
marks[offset + 2] = differs ? 0 : before[offset + 2] >> 2;
marks[offset + 3] = 255;
}
-console.log(JSON.stringify({ pixels: before.length / 4, differingPixels, pixelsOverEleven, rmseOf255: Number(Math.sqrt(squaredSum / (before.length / 4 * 3)).toFixed(3)), maximumChannelDifference }));
+console.log(JSON.stringify({ pixels: before.length / 4, differingPixels, maximumChannelDifference }));
if (pngFile) await writeFile(pngFile, encodePng(marks, Number(width)));
diff --git a/bench/glaze-passes/harness.js b/bench/glaze-passes/harness.js
index 87949271..c12e8116 100644
--- a/bench/glaze-passes/harness.js
+++ b/bench/glaze-passes/harness.js
@@ -90,7 +90,7 @@ function replaceColourShaders(replaceVertex) {
};
}
-window.measureScene = async function measureScene({ description, glbUrl, hdriUrl, frames = 200, capture = false, alternate = false, segmented = true, width = 1280, height = 720, rendererOverrides = {}, experiment = null, cameraDistanceScale = 1, lodRatios = [0.25, 0.0625] }) {
+window.measureScene = async function measureScene({ description, glbUrl, hdriUrl, frames = 200, capture = false, alternate = false, segmented = true, width = 1280, height = 720, rendererOverrides = {}, denseLods = false, experiment = null }) {
const canvas = document.querySelector('#viewport');
const experiments = new Set(String(experiment ?? '').split(','));
if (experiments.has('no-invariant')) {
@@ -113,15 +113,6 @@ window.measureScene = async function measureScene({ description, glbUrl, hdriUrl
return originalSource.call(this, shader, source);
};
}
- const quarter = [...experiments].find(name => name.startsWith('index-fraction-'));
- if (quarter) {
- // Experiment only: draws the first part of every index range, to price a mesh with fewer triangles (the picture is wrong).
- const fraction = Number(quarter.slice('index-fraction-'.length));
- const original = WebGL2RenderingContext.prototype.drawElementsInstanced;
- WebGL2RenderingContext.prototype.drawElementsInstanced = function drawElementsInstanced(mode, count, type, offset, instances) {
- return original.call(this, mode, Math.max(3, Math.floor(count * fraction / 3) * 3), type, offset, instances);
- };
- }
if (experiment === 'trivial-fragment') replaceColourShaders(false);
if (experiment === 'trivial-both') replaceColourShaders(true);
canvas.style.width = `${width}px`;
@@ -132,17 +123,9 @@ window.measureScene = async function measureScene({ description, glbUrl, hdriUrl
loadHdri(hdriUrl, description.hdriOptions ?? {}),
]);
const asset = await loadGltf(model, { baseUrl: new URL('.', new URL(glbUrl, location.href)).href, sharedTextures: createSharedTextures(), ...(description.gltfOptions ?? {}) });
- let lodReport = null;
- let parts = asset.parts;
- if (experiments.has('lods')) {
- // The shipped helper: levels for every dense, eligible part (canvas pixels as the renderer counts them).
- const started = performance.now();
- parts = addLodsToDenseParts(asset.parts, { pixels: width * height * (description.renderer?.pixelRatio ?? 1.5) ** 2, ratios: lodRatios });
- lodReport = { milliseconds: Math.round(performance.now() - started), partsWithLevels: parts.filter(part => part.lods).length, levelErrors: [...new Set(parts.filter(part => part.lods).map(part => part.lods))].map(levels => levels.map(level => [level.triangles, Number(level.error.toPrecision(3))])) };
- }
const rendererOptions = description.renderer ?? {};
const shadowCascades = rendererOptions.shadowCascades ? createCascadedShadows(rendererOptions.shadowCascades) : undefined;
- const renderer = new CrucibleRenderer(canvas, parts, {
+ const renderer = new CrucibleRenderer(canvas, denseLods ? addLodsToDenseParts(asset.parts, { pixels: width * height }) : asset.parts, {
ground: false, hdr: true, samples: 4, ...rendererOptions, shadowCascades,
environment: { ...(rendererOptions.environment ?? {}), map },
});
@@ -150,7 +133,7 @@ window.measureScene = async function measureScene({ description, glbUrl, hdriUrl
const timer = createSegmentTimer(context);
// The renderer's own timer would open a query inside ours, and WebGL allows one at a time.
renderer.gpuTimer = null;
- const frame = { exposure: description.exposure ?? 1, ...(description.frame ?? {}), eye: description.eye.map((value, axis) => description.target[axis] + (value - description.target[axis]) * cameraDistanceScale), target: description.target };
+ const frame = { exposure: description.exposure ?? 1, ...(description.frame ?? {}), eye: description.eye, target: description.target };
if (experiment === 'discard') {
// Vertex work only: the rasterizer throws every triangle away, so no pixel is shaded.
const originalDraw = renderer.drawAlphaBatch.bind(renderer);
@@ -243,7 +226,6 @@ window.measureScene = async function measureScene({ description, glbUrl, hdriUrl
segmentMedianMs: Object.fromEntries(Object.entries(alternatingSegments[mode]).map(([name, values]) => [name, Number(median(values).toFixed(3))])),
}])) : undefined,
pairedOnOverOffMedian: alternate ? Number(median(alternating.on.slice(0, alternating.off.length).map((value, index) => value / alternating.off[index])).toFixed(3)) : undefined,
- lodReport: lodReport && { ...lodReport, perPart: undefined, selectedTriangles: renderer.batches.map(batch => batch.selection ? batch.selection.counts.slice() : null) },
capturedFrame,
centerPixel: Array.from(ONE_PIXEL),
glError: context.getError(),
diff --git a/changes/1163.md b/changes/1163.md
new file mode 100644
index 00000000..efc8c1ae
--- /dev/null
+++ b/changes/1163.md
@@ -0,0 +1 @@
+- A scene file's `renderer.denseLods: true` gives a dense scan levels of detail (off by default, no change to existing scenes), which cut the Monster scan's GPU frame about 2.7x in paired runs (#1163).
diff --git a/docs/culling-lod.md b/docs/culling-lod.md
index e3b69b70..80ba9d1a 100644
--- a/docs/culling-lod.md
+++ b/docs/culling-lod.md
@@ -59,3 +59,5 @@ A scan with about one triangle per pixel or more is shaded once per sample at ne
`generateGeometryLods` keeps the uvs, light uvs, colours and normals of a geometry that has them: each level is the geometry with some edges collapsed onto a vertex that stays, so no attribute is blended. Vertices on a UV seam, a hard edge or an open border never move, and a level can keep more triangles than its ratio when those vertices leave no safe collapse. `error` is the square root of the largest quadric cost of any collapse so far, an estimate of how far the surface moved, so it is a distance in the geometry's units (each plane counts once, so scaling the mesh by k scales it by k) and sits on the cautious side for small meshes with many seams. A geometry may have at most `MAXIMUM_SIMPLIFY_TRIANGLES` triangles and `MAXIMUM_SIMPLIFY_VERTICES` vertices (2 million each); a bigger one throws a RangeError, so split the part or skip levels for it (`addLodsToDenseParts` leaves such parts as they are).
`addLodsToDenseParts(parts, { pixels })` from `src/dense-lods.js` gives levels to the parts of a scene that has more than one triangle per pixel (`pixels` is the canvas width times its height times the pixel ratio squared). A part gets levels when it has at least `minimumTriangles` (default 20000) triangles and can take them; parts with the same geometry share one set. It returns `parts` itself for a scene under the density. It is not applied by itself, and it takes seconds on a million triangles, so call it once while loading. On the Monster scene (1.6 million triangles, 1280 by 720, 4 samples) called with the canvas size, the frame went from 5.8 to 1.9 ms, and Lone Monk (5.6 million) from 15.4 to 9.5 ms (measured before the error metric was changed to a distance); the picture changes slightly (root mean square 2.0 of 255), because far parts draw from coarser levels (#1163).
+
+A scene file (`scene.json`, rendered by `scripts/render-media.mjs --scene file`) asks for the same levels with `"renderer": { "denseLods": true }`. The option is off by default, so a scene file without it draws exactly as before; with it, `partsForRendererOptions` in `src/dense-lods.js` calls `addLodsToDenseParts` at the canvas size and pixel ratio before the renderer is built (#1163).
diff --git a/src/dense-lods.js b/src/dense-lods.js
index 5d595b71..94ca938c 100644
--- a/src/dense-lods.js
+++ b/src/dense-lods.js
@@ -34,3 +34,16 @@ export function addLodsToDenseParts(parts, { pixels, minimumTrianglesPerPixel =
return { ...part, lods: levelsByGeometry.get(part.geometry) };
});
}
+
+/**
+ * The parts a scene file's renderer options ask for: `denseLods: true` runs addLodsToDenseParts at the canvas's size and
+ * pixel ratio (the default ratio of CrucibleRenderer is 1.5); absent or `false` returns `parts` itself, so a scene file
+ * without the option draws exactly as before.
+ */
+export function partsForRendererOptions(parts, canvas, rendererOptions = {}) {
+ const { denseLods = false, pixelRatio = 1.5 } = rendererOptions;
+ if (typeof denseLods !== 'boolean') throw new TypeError('renderer.denseLods must be true or false.');
+ if (!denseLods) return parts;
+ const pixels = Math.max(1, canvas.clientWidth) * Math.max(1, canvas.clientHeight) * pixelRatio * pixelRatio;
+ return addLodsToDenseParts(parts, { pixels });
+}
diff --git a/src/file-scene.js b/src/file-scene.js
index bb8e8517..edafb312 100644
--- a/src/file-scene.js
+++ b/src/file-scene.js
@@ -6,6 +6,7 @@ import { applyWaterReflection, attachWaterReflection } from './water-reflection.
import { createCameraSampler } from './camera-track.js';
import { inWholeWorldOrder, readPartRanks } from './showcase-download.js';
import { withinDrawnLightLimits } from './drawn-light-lists.js';
+import { partsForRendererOptions } from './dense-lods.js';
// Headless twin of the showcase page for scripts/render-media.mjs (--scene file). The export script serves the scene.json
// at /__file/config.json with every asset path already pointing at a file it can read, and an optional camera track at
@@ -57,7 +58,8 @@ async function start() {
const rendererOptions = description.renderer ?? {};
const shadowCascades = rendererOptions.shadowCascades ? createCascadedShadows(rendererOptions.shadowCascades) : undefined;
const reflection = description.waterReflection ? applyWaterReflection(asset.parts) : { parts: asset.parts, surface: null };
- const renderer = new CrucibleRenderer(document.querySelector('#file-viewport'), reflection.parts, {
+ const viewport = document.querySelector('#file-viewport');
+ const renderer = new CrucibleRenderer(viewport, partsForRendererOptions(reflection.parts, viewport, rendererOptions), {
ground: false, hdr: true, samples: 4, memoryBudgetMiB: 384,
...rendererOptions,
shadowCascades,
diff --git a/tests/dense-lods-option.test.js b/tests/dense-lods-option.test.js
new file mode 100644
index 00000000..1d7e840c
--- /dev/null
+++ b/tests/dense-lods-option.test.js
@@ -0,0 +1,36 @@
+// A scene file's renderer option `denseLods` gives a dense scan levels of detail (#1163); without it the parts are untouched.
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { sphere } from '../src/geometry.js';
+import { partsForRendererOptions } from '../src/dense-lods.js';
+
+function scanPart() {
+ return { name: 'scan', geometry: sphere(200, 100), matrix: new Float32Array(16), material: { tag: 'surface', color: [0.5, 0.5, 0.5], metallic: 0, roughness: 0.5 } };
+}
+
+const SMALL_CANVAS = { clientWidth: 20, clientHeight: 20 };
+const HUGE_CANVAS = { clientWidth: 8000, clientHeight: 8000 };
+
+test('denseLods true gives levels to a scan that has more than one triangle per pixel', () => {
+ const parts = [scanPart()];
+ const result = partsForRendererOptions(parts, SMALL_CANVAS, { denseLods: true, pixelRatio: 1 });
+ assert.notEqual(result, parts);
+ assert.ok(result[0].lods.length > 1, 'the part carries levels');
+});
+
+test('without denseLods, or with it false, the same parts come back', () => {
+ const parts = [scanPart()];
+ assert.equal(partsForRendererOptions(parts, SMALL_CANVAS, {}), parts);
+ assert.equal(partsForRendererOptions(parts, SMALL_CANVAS, { denseLods: false }), parts);
+ assert.equal(partsForRendererOptions(parts, SMALL_CANVAS), parts);
+});
+
+test('the pixel ratio counts: the same scan is sparse on a big canvas', () => {
+ const parts = [scanPart()];
+ assert.equal(partsForRendererOptions(parts, HUGE_CANVAS, { denseLods: true, pixelRatio: 1 }), parts);
+ assert.notEqual(partsForRendererOptions(parts, SMALL_CANVAS, { denseLods: true, pixelRatio: 1 }), parts);
+});
+
+test('denseLods must be a boolean, and the error says so', () => {
+ assert.throws(() => partsForRendererOptions([scanPart()], SMALL_CANVAS, { denseLods: 'yes' }), /renderer\.denseLods must be true or false/);
+});