Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions docs/viewer-performance-baseline.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,35 @@ files in alternating order. Each trial times the default adaptive orbit after th

The Radeon trial view stayed on cell detail. On the RTX 4080, two trials used one refined frame each, and
the other two stayed on cell detail. The report's `refinedFrames` field records this choice.

## Instanced cells with a distant placement

The layout limits the radius of cell centres to 10% of the drawable model's diagonal. When a single placement
more than doubles that diagonal, the layout caps it at twice the diagonal of the remaining placements. All
placements enter the partition and the actual model bounds. One linear scan retains the two extrema on each
side, so the layout can evaluate these exclusions without sorting all placements. The partition allows up to
1,024 placements per cell and 8,192 instanced cells per scene.

On the regular Synthetic-400k model, three paired trials per GPU alternated the `790a881` and updated bundles
over identical model data with `PERF_ADAPTIVE_ORBIT_ONLY=1`.

| GPU | Baseline p95 median (range), ms | Updated p95 median (range), ms | Baseline / updated median submitted triangles |
| --- | ---: | ---: | ---: |
| Radeon | 33.6 (33.5–50.1) | 33.5 (33.4–33.6) | 12.37 M / 12.37 M |
| RTX 4080 | 16.8 (16.8–16.8) | 16.8 (16.8–16.8) | 38.40 M / 38.40 M |

For the outlier comparison, move the last Synthetic-1M instance to RTC X = 500,000 m by writing a float32
at `instances.matrix.o + (instances.count - 1) * 48 + 36` in the decoded BIN. Use the same edited payload for
both bundles. Fit the bounds of the other 999,999 placements, then set the camera at one quarter of that
fitted distance from the target. After warm-up, submit 36 evenly spaced orbit poses with full detail and
read one pixel per offscreen frame. Then run the five-second adaptive orbit from the same starting pose.
The table gives medians of three alternating pairs, with ranges for the two frame p95 measurements.

| GPU | Adaptive p95, before / after, ms | Full-detail completed p95, before / after, ms | Full-detail median CPU submission, before / after, ms |
| --- | ---: | ---: | ---: |
| Radeon | 33.6 (33.6–49.8) / 33.5 (33.5–33.5) | 212.6 (212.4–212.6) / 212.4 (212.4–212.6) | 4.7 / 7.0 |
| RTX 4080 | 16.8 (16.8–16.8) / 16.8 (16.8–16.8) | 16.6 (12.8–16.7) / 13.4 (13.3–16.3) | 3.7 / 5.6 |

The outlier case builds 1,666 cells, up from 1,104. At full detail the median draw count rises from 1,039
to 1,582 and median submitted triangles drop from 89.95 M to 89.57 M. This increases CPU submission time
in exchange for tighter spatial bounds. The outlier case tests a single divergent placement.
340 changes: 170 additions & 170 deletions src/IOViewer/Viewer/dist/viewer.js

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions src/IOViewer/Viewer/src/engine/chunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,36 @@ function spread(m, centres) {
return Math.hypot(x1 - x0, y1 - y0, z1 - z0) / 2;
}

// A single drawable box may at most double the diagonal used to size cells. Keep the runner-up at each edge
// so all six checks after the scan can exclude the same box on every axis.
export function cellLayoutDiagonal(boxes, weight) {
const first = new Float64Array(6).fill(-Infinity);
const second = new Float64Array(6).fill(-Infinity);
const owner = new Int32Array(6);
for (let i = 0; i < weight.length; i++) {
if (weight[i] === 0) continue;
for (let edge = 0; edge < 6; edge++) {
const coord = boxes[i * 6 + edge];
const outside = edge < 3 ? -coord : coord;
if (outside > first[edge]) {
second[edge] = first[edge];
first[edge] = outside;
owner[edge] = i;
} else if (outside > second[edge]) second[edge] = outside;
}
}
if (first[0] === -Infinity) return 0;
let diagonal = Math.hypot(first[0] + first[3], first[1] + first[4], first[2] + first[5]);
if (second[0] === -Infinity) return diagonal;

for (const excluded of owner) {
const edge = (k) => owner[k] === excluded ? second[k] : first[k];
const trimmed = Math.hypot(edge(0) + edge(3), edge(1) + edge(4), edge(2) + edge(5));
diagonal = Math.min(diagonal, 2 * trimmed);
}
return diagonal;
}

export const MERGED_CAP = 384 * 2 ** 20; // merged GPU bytes above which the repeated shapes are drawn instanced

/**
Expand Down
13 changes: 7 additions & 6 deletions src/IOViewer/Viewer/src/engine/modelScene.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { ModelDataError } from '../data.js';
import { BINS, detailBin, frustumPlanes, insideFrustum, outsideFrustum, thresholdFromBins, viewMetrics } from './culling.js';
import { GRID, normalsView, octEncodeInto, positionsView } from './quantize.js';
import {
CHUNK_VERTICES, F32, MERGED_CAP, bakeChunk, bakeSource, drawSpan, indexArray, placeSlot, planShapes, prefixByRadius, sortByRadius,
splitByCount, splitCells, unitNormals,
CHUNK_VERTICES, F32, MERGED_CAP, bakeChunk, bakeSource, cellLayoutDiagonal, drawSpan, indexArray, placeSlot,
planShapes, prefixByRadius, sortByRadius, splitByCount, splitCells, unitNormals,
} from './chunks.js';
import { patchLambert } from './slotShader.js';

Expand All @@ -16,8 +16,8 @@ const ROW = 2048; // slots per slot-texture row (the shaders use slot & 2047, sl
const RANGE_LIMIT = 256; // up to this many changed slots are uploaded one by one, more as the whole texture
const MIN_REPEATS = 16; // fewer instances of a shape stay merged even above the cap
const PER_CELL = 1024; // instances per remainder cell
// A cell's centres lie within this share of the model's diagonal: a cell spread over the site is drawn whole by the
// 1-pixel pick pass and sized by its nearest point by the detail cut. Measured at 1M synthetic elements on an
// A cell's centres lie within this share of the outlier-capped model diagonal. Wide cells weaken frustum
// rejection and are sized by their nearest point by the detail cut. Measured at 1M synthetic elements on an
// integrated GPU (cells / pick / CPU per still frame): unbounded 1024 / 61 ms / 4 ms; 0.1: 4766 / 20 ms / 23 ms;
// 0.06: over 8192 cells, 9.5 ms. Each draw costs three ~5 us of CPU, so merged-chunk extents (0.035) are out of reach.
const CELL_EXTENT = 0.1;
Expand Down Expand Up @@ -712,8 +712,9 @@ export class ModelScene {
const minRepeats = options.minRepeats ?? MIN_REPEATS;
if (shapes.reduce((sum, s) => sum + s.k * s.bytes, 0) > cap) {
// The cells of every candidate, so that the plan counts the draws they really take.
const box = this.visibleBox();
const maxRadius = box.isEmpty() ? Infinity : box.min.distanceTo(box.max) * (options.cellExtent ?? CELL_EXTENT);
const maxRadius = options.cellExtent == null
? cellLayoutDiagonal(b, weight) * CELL_EXTENT
: this.visibleBox().getSize(new THREE.Vector3()).length() * options.cellExtent;
for (const shape of shapes) {
if (shape.k < minRepeats) continue;
shape.leaves = splitByCount(centres, shape.members, PER_CELL, maxRadius);
Expand Down
28 changes: 26 additions & 2 deletions src/IOViewer/Viewer/test/unit/chunks.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
F32, GENERAL, MERGED_CAP, VERBATIM, bakeChunk, bakeSource, drawSpan, indexArray, placeSlot, planShapes, prefixByRadius, select,
sortByRadius, splitByCount, splitCells,
F32, GENERAL, MERGED_CAP, VERBATIM, bakeChunk, bakeSource, cellLayoutDiagonal, drawSpan, indexArray, placeSlot,
planShapes, prefixByRadius, select, sortByRadius, splitByCount, splitCells,
} from '../../src/engine/chunks.js';
import { GRID, octDecodeInto } from '../../src/engine/quantize.js';

Expand Down Expand Up @@ -347,3 +347,27 @@ test('plan: the draws counted are the cells a shape really takes when given', ()
const shapes = [{ k: 100, bytes: 100, cells: 5 }, { k: 90, bytes: 100, cells: 2 }, { k: 80, bytes: 100, cells: 1 }];
assert.deepEqual([...planShapes(shapes, { cap: 0, maxDraws: 6 })], [1, 0, 1]);
});

test('cell sizing caps one drawable box’s influence on every axis and keeps tied extrema', () => {
const boxes = Float32Array.from([
0, 0, 0, 1, 1, 1,
0, 0, 0, 1, 1, 1,
-1000, -1000, 0, -999, -999, 1,
0, 90, 0, 1, 91, 1,
0, 10_000, 0, 1, 10_001, 1,
]);
assert.equal(cellLayoutDiagonal(boxes, [1, 1, 1, 1, 0]), 2 * Math.hypot(1, 91, 1));
assert.equal(cellLayoutDiagonal(boxes, [1, 1, 0, 1, 0]), 2 * Math.sqrt(3));
assert.equal(cellLayoutDiagonal(boxes, [1, 1, 0, 0, 0]), Math.sqrt(3));
assert.equal(cellLayoutDiagonal(boxes, [1, 0, 0, 0, 0]), Math.sqrt(3));
assert.equal(cellLayoutDiagonal(boxes, [0, 0, 0, 0, 0]), 0);
});

test('cell sizing keeps the full bounds when no placement more than doubles the diagonal', () => {
const boxes = Float32Array.from([
0, 0, 0, 1, 1, 1,
4, 0, 0, 5, 1, 1,
8, 0, 0, 9, 1, 1,
]);
assert.equal(cellLayoutDiagonal(boxes, [1, 1, 1]), Math.hypot(9, 1, 1));
});
12 changes: 12 additions & 0 deletions src/IOViewer/Viewer/test/unit/modelScene.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,18 @@ test('over the cap, cells are bounded in extent: one shape spread along 190 m ta
for (const unit of scene._units) assert.ok(unit.sphere[3] <= cap + 1e-9, `${unit.sphere[3]} > ${cap}`);
});

test('one remote placement cannot inflate the bounds of every instanced cell', () => {
const local = Array.from({ length: 20 }, (_, i) => ({ x: i * 10 }));
const scene = model([...local, { x: 500_000 }], { options: { mergedCap: 0, minRepeats: 1 } });
assert.equal(scene.memory().instancedInstances, 21);
assert.equal(new Set(Array.from({ length: 21 }, (_, i) => scene.instance(i).slot)).size, 21);
const remote = scene.instance(20).mesh;
assert.equal(remote.count, 1);
const cap = Math.hypot(191, 1, 1) * 0.2 + Math.sqrt(3) / 2;
for (const unit of scene._units) assert.ok(unit.sphere[3] <= cap + 1e-9, `${unit.sphere[3]} > ${cap}`);
assert.equal(scene.visibleBox().max.x, 500_000.5);
});

test('a unit the detail cut leaves empty is hidden for that frame, counted in view, and shown again after', () => {
// Two chunks: a 10 m cube and a 0.1 m cube beside it.
const scene = model([{ x: 0, scale: 10 }, { x: 8, scale: 0.1 }], { options: { chunkVertices: 36 } });
Expand Down
Loading