Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Make the mesh blend pass cheaper (part of #489)

Nguyen Xuan And2a37c74a6cc
diff --git a/bench/reference/mesh-blend-harness.js b/bench/reference/mesh-blend-harness.js
index 197fd58c..d694cd8d 100644
--- a/bench/reference/mesh-blend-harness.js
+++ b/bench/reference/mesh-blend-harness.js
@@ -99,6 +99,29 @@ window.measureFrames = (count) => {
return times;
};
+// GPU milliseconds per frame from EXT_disjoint_timer_query_webgl2, or null where the browser does not expose it.
+window.measureGpuFrames = async (count) => {
+ const context = renderer.context;
+ const timer = context.getExtension('EXT_disjoint_timer_query_webgl2');
+ if (!timer) return null;
+ for (let index = 0; index < 30; index++) renderer.render(frame);
+ const queries = [];
+ for (let index = 0; index < count; index++) {
+ const query = context.createQuery();
+ context.beginQuery(timer.TIME_ELAPSED_EXT, query);
+ renderer.render(frame);
+ context.endQuery(timer.TIME_ELAPSED_EXT);
+ queries.push(query);
+ }
+ const times = [];
+ for (const query of queries) {
+ while (!context.getQueryParameter(query, context.QUERY_RESULT_AVAILABLE)) await new Promise(done => setTimeout(done, 1));
+ if (!context.getParameter(timer.GPU_DISJOINT_EXT)) times.push(context.getQueryParameter(query, context.QUERY_RESULT) / 1e6);
+ context.deleteQuery(query);
+ }
+ return times;
+};
+
window.teardown = () => {
renderer.dispose();
renderer = null;
diff --git a/bench/reference/mesh-blend.mjs b/bench/reference/mesh-blend.mjs
index 7b7295b4..34f74abc 100644
--- a/bench/reference/mesh-blend.mjs
+++ b/bench/reference/mesh-blend.mjs
@@ -1,4 +1,5 @@
-// Mesh blend check for issue #478: before/after still, pixel identity against main, and the frame cost at 1080p.
+// Mesh blend check for issues #478 and #489: before/after still, pixel identity against main, and the frame cost at 1080p
+// (wall clock and, where the browser exposes EXT_disjoint_timer_query_webgl2, GPU time).
// node bench/reference/mesh-blend.mjs --main-root <dir with src/ of main, bench/reference/mesh-blend-harness.html, node_modules>
// [--out <folder>] [--rounds <n>] [--frames <n>] [--skip-cost]
// Writes the PNGs and out/478-sheet.png (before, after, and a 2x crop of a seam) to the output folder.
@@ -62,21 +63,36 @@ async function maxDifference(firstPng, secondPng) {
return { largest, differing };
}
+const middle = values => [...values].sort((first, second) => first - second)[Math.floor(values.length / 2)];
+
+// Each round measures the three modes back to back, so the cost is the per-round difference: on a shared GPU the clock and
+// the load drift between rounds, and a pooled median mixes those states.
+function reportCost(name, rows) {
+ const costs = rows.map(row => row.on - row.off).sort((first, second) => first - second);
+ const pooled = key => middle(rows.map(row => row[key]));
+ console.log(`${name}, ${rows.length} rounds x ${frames} frames: no addon ${pooled('plain').toFixed(3)} ms, off ${pooled('off').toFixed(3)} ms, on ${pooled('on').toFixed(3)} ms; cost per round median ${middle(costs).toFixed(3)} ms, range ${costs[0].toFixed(3)} to ${costs.at(-1).toFixed(3)} ms`);
+}
+
async function measureCost() {
- const times = { plain: [], off: [], on: [] };
+ const wallRows = [];
+ const gpuRows = [];
await withPage(resolve('.'), async page => {
for (let round = 0; round < rounds; round++) {
+ const wall = {};
+ const gpu = {};
for (const [label2, mode] of round % 2 ? [['plain', 'plain'], ['off', 'installed'], ['on', 'blend']] : [['on', 'blend'], ['off', 'installed'], ['plain', 'plain']]) {
await page.evaluate(options => window.setupScene(options), { scene: 'nature', mode });
- times[label2].push(...await page.evaluate(count => window.measureFrames(count), frames));
+ wall[label2] = middle(await page.evaluate(count => window.measureFrames(count), frames));
+ const gpuTimes = await page.evaluate(count => window.measureGpuFrames(count), frames);
+ if (gpuTimes?.length) gpu[label2] = middle(gpuTimes);
await page.evaluate(() => window.teardown());
}
+ wallRows.push(wall);
+ if (Object.keys(gpu).length === 3) gpuRows.push(gpu);
}
});
- const middle = values => [...values].sort((first, second) => first - second)[Math.floor(values.length / 2)];
- const off = middle(times.off);
- const on = middle(times.on);
- console.log(`1080p frame, ${rounds} rounds x ${frames} frames pooled: no addon ${middle(times.plain).toFixed(3)} ms, off ${off.toFixed(3)} ms, on ${on.toFixed(3)} ms, cost ${(on - off).toFixed(3)} ms`);
+ reportCost('1080p frame, wall clock', wallRows);
+ if (gpuRows.length) reportCost('1080p frame, GPU timer', gpuRows);
}
if (argumentsGiven.includes('--only-cost')) {
diff --git a/changes/489.md b/changes/489.md
new file mode 100644
index 00000000..09b2900f
--- /dev/null
+++ b/changes/489.md
@@ -0,0 +1 @@
+- Make the mesh blend pass cheaper (issue #489). The shader reads its 24 taps from a constant table instead of computing a square root, sine and cosine per tap, the ID attachment is cleared each frame so an ID lookup needs no depth fetch, and a batch whose parts share one radius and one level keeps its frustum culling. On the nature scene at 1080p on an RTX 3060 the pass costs 0.25 to 0.58 ms of GPU time over 12 rounds (median 0.31 ms), down from about 0.55 ms; scenes without `blend` draw the same pixels.
diff --git a/changes/489.size.md b/changes/489.size.md
new file mode 100644
index 00000000..12bc6ae4
--- /dev/null
+++ b/changes/489.size.md
@@ -0,0 +1 @@
+- entry:mesh-blend: +240 B gzip for the constant tap table, the per-frame ID clear that drops the depth fetch from every ID lookup, and the check that keeps culling on for batches whose parts share one radius (issue #489). The pass costs less GPU time on every measured round, and core and lit-box do not load this module.
diff --git a/docs/mesh-blend.md b/docs/mesh-blend.md
index a798a315..3a09f90c 100644
--- a/docs/mesh-blend.md
+++ b/docs/mesh-blend.md
@@ -19,7 +19,7 @@ For each pixel of a blend part, the pass looks at 24 taps on a spiral within `ra
- Opaque and cutout parts only. A blended part writes no ID, and translucent water or glass drawn over a blend part leaves that pixel unblended or with a stale ID (the liquid programs do not write the attachment).
- A blend part mixes toward the part on the other side of its seam; the other part changes only if it has `blend` too. Marking both gives the soft seam from both sides.
- A seam under about one pixel wide (a tiny radius far from the camera) is left alone. The path tracer does not run the pass.
-- The part ID is the draw order of opaque instances. A batch (one geometry and material) that holds a blend part is drawn without frustum culling or level selection while the pass is on, so its IDs stay in order; a large instanced forest with one blend part pays that. More than 16 million parts is not supported (the ID has 24 bits).
+- The part ID is the draw order of opaque instances. Culling packs the visible instances of a batch (one geometry and material) inside its own ID range, so a batch whose parts all share one radius and draw at one level keeps its frustum culling. A batch that mixes radii, or one with LOD levels (each level draw restarts the instance numbers), is drawn without culling or level selection while the pass is on, so its IDs stay in order; a large instanced forest of mixed radii pays that. More than 16 million parts is not supported (the ID has 24 bits).
- In a recipe, a wrong `blend` value (`radius` outside 0.005 to 2, an unknown field) is reported when the preview draws the frame, with the message of the field.
## Code
diff --git a/src/mesh-blend.js b/src/mesh-blend.js
index fb128a6a..edee6296 100644
--- a/src/mesh-blend.js
+++ b/src/mesh-blend.js
@@ -18,6 +18,20 @@ const TAP_COUNT = MESH_BLEND_TAPS.count;
const MAXIMUM_PIXEL_RADIUS = 40;
const TABLE_WIDTH = 1024;
+// The taps lie on a golden-angle spiral, denser near the pixel; a tap's weight falls off with its distance. The shader reads
+// them from a constant table, so it computes no square root, sine or cosine per tap.
+function spiralTaps() {
+ return Array.from({ length: TAP_COUNT }, (unused, index) => {
+ const fraction = Math.sqrt((index + 0.5) / TAP_COUNT);
+ const angle = index * MESH_BLEND_TAPS.angleStep;
+ return [Math.cos(angle) * fraction, Math.sin(angle) * fraction, 1 - fraction];
+ });
+}
+const glslFloat = value => (Number.isInteger(value) ? `${value}.0` : String(Math.fround(value)));
+const TAPS = spiralTaps();
+const TAP_TABLE = TAPS.map(tap => `vec3(${tap.map(glslFloat).join(', ')})`).join(', ');
+const TAP_TOTAL_WEIGHT = glslFloat(TAPS.reduce((sum, tap) => sum + tap[2], 0));
+
// gl-program.js is not imported on purpose: this module loads on demand (see line-art.js).
const fullscreenVertex = `#version 300 es
precision highp float;
@@ -56,6 +70,10 @@ export function blendRadiusTable(parts, radii = new Map()) {
// What scene_capabilities reports: one line, since every byte there is paid on each call. The limits are in the docs.
export const MESH_BLEND_CAPABILITIES = 'nodes[].blend {radius: metres 0.005-2} softens seams; needs depth; docs/mesh-blend.md';
+// tapOffsets holds each tap's offset at a radius of one pixel (x, y) and its weight (z). partAt needs no depth fetch: the ID
+// attachment is cleared to zero each frame, which fails the checksum, so the background reads as no part. The multisample
+// resolve averages the IDs of an edge pixel into a value that is no ID, so the alpha channel carries a checksum of the three
+// ID bytes; a pixel that fails it is an edge pixel and reads as no part too.
export const blendFragment = `#version 300 es
precision highp float;
uniform sampler2D sourceTexture;
@@ -67,12 +85,10 @@ uniform vec3 eye;
uniform float pixelsPerMetre;
uniform float orthographic;
out vec4 outputColor;
+const vec3 tapOffsets[${TAP_COUNT}] = vec3[${TAP_COUNT}](${TAP_TABLE});
ivec2 lastPixel;
-// The far depth marks the background. The multisample resolve averages the IDs of an edge pixel into a value that is no ID, so
-// the alpha channel carries a checksum of the three ID bytes; a pixel that fails it is an edge pixel and reads as no part.
int partAt(ivec2 pixel) {
ivec2 clamped = clamp(pixel, ivec2(0), lastPixel);
- if (texelFetch(sceneDepth, clamped, 0).r >= 1.0) return 0;
ivec4 bytes = ivec4(texelFetch(partTexture, clamped, 0) * 255.0 + 0.5);
if (bytes.w != (bytes.x * 3 + bytes.y * 5 + bytes.z * 11) % 251 + 1) return 0;
return bytes.x + bytes.y * 256 + bytes.z * 65536;
@@ -116,26 +132,27 @@ void main() {
float reach = radius * 3.0;
vec3 colorSum = vec3(0.0);
float otherWeight = 0.0;
- float totalWeight = 0.0;
ivec2 nearestTap = pixel;
+ vec3 nearestPosition = center;
bool found = false;
for (int index = 0; index < ${TAP_COUNT}; index++) {
- float fraction = sqrt((float(index) + 0.5) / ${TAP_COUNT}.0);
- float angle = float(index) * ${MESH_BLEND_TAPS.angleStep};
- ivec2 tap = pixel + ivec2(round(vec2(cos(angle), sin(angle)) * pixelRadius * fraction));
- float weight = 1.0 - fraction;
- totalWeight += weight;
+ vec3 tapOffset = tapOffsets[index];
+ ivec2 tap = pixel + ivec2(round(tapOffset.xy * pixelRadius));
int tapPart = partAt(tap);
if (tapPart == 0 || tapPart == part) continue;
- if (distance(positionAt(tap), center) > reach) continue;
- if (!found) nearestTap = tap;
+ vec3 tapPosition = positionAt(tap);
+ if (distance(tapPosition, center) > reach) continue;
+ if (!found) {
+ nearestTap = tap;
+ nearestPosition = tapPosition;
+ }
found = true;
- colorSum += weight * texelFetch(sourceTexture, clamp(tap, ivec2(0), lastPixel), 0).rgb;
- otherWeight += weight;
+ colorSum += tapOffset.z * texelFetch(sourceTexture, clamp(tap, ivec2(0), lastPixel), 0).rgb;
+ otherWeight += tapOffset.z;
}
if (!found) return;
- float creased = smoothstep(0.05, 0.4, 1.0 - dot(normalAt(pixel, center), normalAt(nearestTap, positionAt(nearestTap))));
- float amount = smoothstep(0.0, 0.5, otherWeight / totalWeight) * creased;
+ float creased = smoothstep(0.05, 0.4, 1.0 - dot(normalAt(pixel, center), normalAt(nearestTap, nearestPosition)));
+ float amount = smoothstep(0.0, 0.5, otherWeight / ${TAP_TOTAL_WEIGHT}) * creased;
outputColor.rgb = mix(source.rgb, colorSum / otherWeight, amount);
}`;
@@ -274,6 +291,16 @@ export function installMeshBlend(renderer) {
attachedFramebuffer = renderer.multisampleFramebuffer;
}
+ // The sky never writes the ID attachment and the frame's clear may leave it out, so it is cleared here: the background then
+ // reads as no part.
+ function clearParts() {
+ const { context } = renderer;
+ context.bindFramebuffer(context.FRAMEBUFFER, renderer.multisampleFramebuffer);
+ context.drawBuffers([context.NONE, context.COLOR_ATTACHMENT1]);
+ context.clearBufferfv(context.COLOR, 1, [0, 0, 0, 0]);
+ context.drawBuffers([context.COLOR_ATTACHMENT0, context.NONE]);
+ }
+
// The resolve averages the samples of an edge pixel; the checksum byte in the shader finds those pixels.
function resolveParts() {
const { context, canvas } = renderer;
@@ -333,14 +360,21 @@ export function installMeshBlend(renderer) {
state.table = null;
}
- // A batch the frustum culls draws its visible instances packed, which renumbers them; a batch with a blend part draws all
- // of its instances in order for the frame, so the part IDs the shader writes stay the table's indices.
+ // A batch the frustum culls draws its visible instances packed, which renumbers them within the batch's own ID range. When
+ // every part of the batch has the same radius and the batch draws in one call (a single level), any ID in that range still
+ // reads the right radius and the IDs stay distinct, so the batch keeps its culling. Any other batch with a blend part draws
+ // all of its instances in order for the frame, so the part IDs the shader writes stay the table's indices.
+ function needsStableIds(radii, batch) {
+ if (!radii.some(radius => radius > 0)) return false;
+ if ((batch.levelErrors?.length ?? 1) > 1) return true;
+ return radii.some(radius => radius !== radii[0]);
+ }
+
function pinBlendBatches(table) {
const pinned = [];
let firstId = 1;
for (const batch of opaqueBatches()) {
- const hasBlend = table.subarray(firstId, firstId + batch.parts.length).some(radius => radius > 0);
- if (hasBlend && batch.cullable) {
+ if (batch.cullable && needsStableIds(table.subarray(firstId, firstId + batch.parts.length), batch)) {
batch.cullable = false;
pinned.push(batch);
}
@@ -371,6 +405,7 @@ export function installMeshBlend(renderer) {
uploadTable(table);
renderer.requireFeatureNames(['meshBlend']);
attachPartBuffer();
+ clearParts();
pinned = pinBlendBatches(table);
state.active = true;
}
diff --git a/tests/mesh-blend.test.js b/tests/mesh-blend.test.js
index 1dcc0ee1..268dce28 100644
--- a/tests/mesh-blend.test.js
+++ b/tests/mesh-blend.test.js
@@ -78,7 +78,11 @@ test('pixels of one part, and the background (ID 0), never blend with themselves
test('the reference and the shader use the same tap pattern', () => {
assert.ok(blendFragment.includes(`< ${MESH_BLEND_TAPS.count}; index++`));
- assert.ok(blendFragment.includes(`float(index) * ${MESH_BLEND_TAPS.angleStep}`));
+ const lastIndex = MESH_BLEND_TAPS.count - 1;
+ const lastFraction = Math.sqrt((lastIndex + 0.5) / MESH_BLEND_TAPS.count);
+ const lastX = Math.fround(Math.cos(lastIndex * MESH_BLEND_TAPS.angleStep) * lastFraction);
+ assert.ok(blendFragment.includes(`vec3[${MESH_BLEND_TAPS.count}](vec3(${Math.fround(Math.sqrt(0.5 / MESH_BLEND_TAPS.count))}, 0.0, `), 'the first tap lies on the x axis');
+ assert.ok(blendFragment.includes(`vec3(${lastX}, `), 'the last tap follows the golden angle');
assert.ok(blendFragment.includes('smoothstep(0.05, 0.4, 1.0 - dot('));
});
@@ -166,6 +170,18 @@ test('a blend part needs no extra draw: the frame asks for the ID output and the
assert.equal(renderer.postStages[0].apply(renderer, frame, {}, {}), null, 'the stage is off again after the frame');
});
+test('a batch whose parts share one radius and one level keeps its culling, since any ID in its range reads that radius', () => {
+ const shared = fakeRenderer([{ name: 'rock-a', blend: { radius: 0.2 } }, { name: 'rock-b', blend: { radius: 0.2 } }]);
+ installMeshBlend(shared.renderer);
+ shared.renderer.render({ eye: [0, 0, 5] });
+ assert.equal(shared.drawn[0].cullableWhileDrawing, true);
+ const levelled = fakeRenderer([{ name: 'rock-a', blend: { radius: 0.2 } }, { name: 'rock-b', blend: { radius: 0.2 } }]);
+ levelled.batch.levelErrors = [0, 4];
+ installMeshBlend(levelled.renderer);
+ levelled.renderer.render({ eye: [0, 0, 5] });
+ assert.equal(levelled.drawn[0].cullableWhileDrawing, false, 'each level draw restarts the instance numbers, so IDs would collide');
+});
+
test('a radius from the radii map opts a part in like its own blend field', () => {
const { renderer, required } = fakeRenderer([{ name: 'ground' }, { name: 'rock' }]);
installMeshBlend(renderer);