feat(globe): instanced movers on both engines

engine.movers.createSet({ id, kind: sprite|point|model, capacity, order,
depthTest, sprite|point|model, pickable }) → a MoverSet: set(index,
fields) stores an instance (position, screen rotation, orientation, rgba,
scale, atlas cell, show, pickId) in flat arrays, commit() uploads the
changed range once and requests one render, positionsView() reads the
positions, destroy() removes it; movers.list() for QA. The options, the
store and the registry are engine-neutral (src/globe/movers.js); the fake
engine keeps them for layer tests.

- Cesium: one primitive per set in an engine-owned PrimitiveCollection
  added with the first set, in `order`: sprites a BillboardCollection
  (one image per atlas cell, NearFarScalar, disableDepthTestDistance),
  points a PointPrimitiveCollection, models a Model per instance.
- Navara: a public MeshDesc per set. Sprites and points are one
  InstancedMesh of a quad, faced to the screen and sized in CSS pixels in
  the vertex shader from uniforms (the pick pass draws the same
  footprint), atlas uv, rotation, colour and scale per instance, drawn in
  the transparent pass unlit; offsets from an origin that follows the
  camera past max(1 km, 10 % of its height). Models: the glTF once, one
  InstancedMesh per mesh, minimumPixelSize/maximumScale on the CPU per
  drawn frame, colorBlend in the shader. Pickable sets register a
  PickableInstancedMeshWrapper; pickIds.js maps batch ids back to
  instances (used by Task 4).
- The contract suite draws three 24 px sprites on both engines: centred
  within 2 px of worldToWindow, hidden by show:false, recoloured, gone on
  destroy, and no frames added once the commit has drawn.
- qa-engine-bench --scenario=movers: 10 000 synthetic aircraft
  (src/bench/syntheticFlights.js, moved from the Navara spike with its
  test), every instance moved and committed every frame. p95 Navara
  (look=minimal) / Cesium: global 5.2/7.0 ms, city 5.6/9.9, orbit 6.0/7.4
  (0.74, 0.57, 0.81; gate 1.1). Cesium is measured without its 60 fps cap.

Main chunk 2,860,302 bytes (+9,706, +0.34 %: the Cesium movers).
This commit is contained in:
German Khokhlov committed 2026-10-16 17:15:24 +04:00
1 parent 4623c8b616
commit 0109097440
24 files changed
+2735 -3

No files matched your search

+110
View File
@@ -0,0 +1,110 @@
// src/bench/syntheticFlights.js
const EARTH_RADIUS_M = 6_371_000;
const RADIANS = Math.PI / 180;
/** Small deterministic PRNG so both engines draw the same fleet. */
function mulberry32(seed) {
let state = seed >>> 0;
return () => {
state = (state + 0x6d2b79f5) >>> 0;
let t = state;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const wrapLongitude = (lon) => ((((lon + 180) % 360) + 360) % 360) - 180;
function checkNear(near, count) {
if (!Number.isInteger(near.count) || near.count < 0 || near.count > count)
throw new RangeError('near.count must be an integer in [0, count]');
if (!(near.radiusM >= 0) || !Number.isFinite(near.radiusM))
throw new RangeError('near.radiusM must be a non-negative number');
if (!Number.isFinite(near.lat) || !Number.isFinite(near.lng))
throw new RangeError('near.lat and near.lng must be finite');
const bands = nearBands(near);
if (
!(bands.minAltM <= bands.maxAltM) ||
!(bands.minSpeedMps <= bands.maxSpeedMps)
)
throw new RangeError('near altitude and speed bands must be ordered');
}
const nearBands = ({
minAltM = 300,
maxAltM = 3_000,
minSpeedMps = 10,
maxSpeedMps = 40,
}) => ({ minAltM, maxAltM, minSpeedMps, maxSpeedMps });
/**
* Deterministic synthetic aircraft for engine benchmarks. Each aircraft
* flies a great circle at constant speed on a spherical Earth.
*
* With `near: { lat, lng, radiusM, count }` the first `count` aircraft start
* uniformly over the disc of `radiusM` around that point, at `minAltM` to
* `maxAltM` (default 300–3000 m) and `minSpeedMps` to `maxSpeedMps` (default
* 10–40 m/s, slow enough to stay near the point for a bench run); the rest
* spread over the globe as without `near`.
*/
export function createSyntheticFlights({ count, seed = 1, near = null }) {
if (!Number.isInteger(count) || count < 0)
throw new RangeError('count must be a non-negative integer');
if (near) checkNear(near, count);
const random = mulberry32(seed);
const lat = new Float64Array(count);
const lon = new Float64Array(count);
const alt = new Float64Array(count);
const heading = new Float64Array(count);
const speed = new Float64Array(count);
const nearCount = near ? near.count : 0;
const bands = near ? nearBands(near) : null;
for (let i = 0; i < nearCount; i++) {
// Uniform over the disc: r = R·sqrt(u), then the spherical destination.
const distance = (near.radiusM * Math.sqrt(random())) / EARTH_RADIUS_M;
const bearing = random() * 2 * Math.PI;
const phi1 = near.lat * RADIANS;
const sinPhi2 =
Math.sin(phi1) * Math.cos(distance) +
Math.cos(phi1) * Math.sin(distance) * Math.cos(bearing);
const phi2 = Math.asin(sinPhi2);
const dLambda = Math.atan2(
Math.sin(bearing) * Math.sin(distance) * Math.cos(phi1),
Math.cos(distance) - Math.sin(phi1) * sinPhi2,
);
lat[i] = phi2 / RADIANS;
lon[i] = wrapLongitude(near.lng + dLambda / RADIANS);
alt[i] = bands.minAltM + random() * (bands.maxAltM - bands.minAltM);
heading[i] = random() * 360;
speed[i] =
bands.minSpeedMps + random() * (bands.maxSpeedMps - bands.minSpeedMps);
}
for (let i = nearCount; i < count; i++) {
lat[i] = Math.asin(2 * random() - 1) / RADIANS;
lon[i] = random() * 360 - 180;
alt[i] = 1_000 + random() * 11_000;
heading[i] = random() * 360;
speed[i] = 120 + random() * 130;
}
function advance(dtSeconds) {
for (let i = 0; i < count; i++) {
const distance = (speed[i] * dtSeconds) / EARTH_RADIUS_M;
const phi1 = lat[i] * RADIANS;
const theta = heading[i] * RADIANS;
const sinPhi2 =
Math.sin(phi1) * Math.cos(distance) +
Math.cos(phi1) * Math.sin(distance) * Math.cos(theta);
const phi2 = Math.asin(sinPhi2);
const dLambda = Math.atan2(
Math.sin(theta) * Math.sin(distance) * Math.cos(phi1),
Math.cos(distance) - Math.sin(phi1) * sinPhi2,
);
lat[i] = phi2 / RADIANS;
lon[i] = wrapLongitude(lon[i] + dLambda / RADIANS);
}
}
return { count, lat, lon, alt, heading, speed, advance };
}
+198
View File
@@ -0,0 +1,198 @@
// src/bench/syntheticFlights.test.mjs
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { createSyntheticFlights } from './syntheticFlights.js';
const EARTH_RADIUS_M = 6_371_000;
const toDegrees = (radians) => (radians * 180) / Math.PI;
test('the same seed gives the same fleet, another seed a different one', () => {
const a = createSyntheticFlights({ count: 100, seed: 3 });
const b = createSyntheticFlights({ count: 100, seed: 3 });
const c = createSyntheticFlights({ count: 100, seed: 4 });
assert.deepEqual(a.lat, b.lat);
assert.deepEqual(a.lon, b.lon);
assert.notDeepEqual(a.lat, c.lat);
});
test('positions, altitudes and speeds stay in plausible ranges', () => {
const fleet = createSyntheticFlights({ count: 1000, seed: 1 });
for (let i = 0; i < fleet.count; i++) {
assert.ok(fleet.lat[i] >= -90 && fleet.lat[i] <= 90);
assert.ok(fleet.lon[i] >= -180 && fleet.lon[i] < 180);
assert.ok(fleet.alt[i] >= 1_000 && fleet.alt[i] <= 12_000);
assert.ok(fleet.speed[i] >= 120 && fleet.speed[i] <= 250);
assert.ok(fleet.heading[i] >= 0 && fleet.heading[i] < 360);
}
});
test('rejects a negative or fractional count', () => {
assert.throws(() => createSyntheticFlights({ count: -1 }), RangeError);
assert.throws(() => createSyntheticFlights({ count: 1.5 }), RangeError);
});
test('an eastbound aircraft on the equator moves east by speed × time', () => {
const fleet = createSyntheticFlights({ count: 1 });
fleet.lat[0] = 0;
fleet.lon[0] = 0;
fleet.heading[0] = 90;
fleet.speed[0] = 100;
fleet.advance(10);
assert.ok(Math.abs(fleet.lat[0]) < 1e-12);
assert.ok(Math.abs(fleet.lon[0] - toDegrees(1_000 / EARTH_RADIUS_M)) < 1e-9);
});
test('a northbound aircraft on the equator moves north', () => {
const fleet = createSyntheticFlights({ count: 1 });
fleet.lat[0] = 0;
fleet.lon[0] = 0;
fleet.heading[0] = 0;
fleet.speed[0] = 100;
fleet.advance(10);
assert.ok(Math.abs(fleet.lat[0] - toDegrees(1_000 / EARTH_RADIUS_M)) < 1e-9);
assert.ok(Math.abs(fleet.lon[0]) < 1e-12);
});
test('longitude wraps across the antimeridian', () => {
const fleet = createSyntheticFlights({ count: 1 });
fleet.lat[0] = 0;
fleet.lon[0] = 179.995;
fleet.heading[0] = 90;
fleet.speed[0] = 250;
fleet.advance(10);
assert.ok(fleet.lon[0] < -179.9 && fleet.lon[0] >= -180);
});
const NEAR = Object.freeze({
lat: 40.7069,
lng: -74.0128,
radiusM: 3_000,
count: 50,
});
/** Great-circle distance in metres on the fleet's spherical Earth. */
function haversineM(lat1, lon1, lat2, lon2) {
const rad = Math.PI / 180;
const dLat = (lat2 - lat1) * rad;
const dLon = (lon2 - lon1) * rad;
const a =
Math.sin(dLat / 2) ** 2 +
Math.cos(lat1 * rad) * Math.cos(lat2 * rad) * Math.sin(dLon / 2) ** 2;
return 2 * EARTH_RADIUS_M * Math.asin(Math.sqrt(a));
}
test('the first near.count aircraft start within radiusM of the near point', () => {
const fleet = createSyntheticFlights({ count: 1_000, seed: 7, near: NEAR });
let farthest = 0;
for (let i = 0; i < NEAR.count; i++) {
const d = haversineM(NEAR.lat, NEAR.lng, fleet.lat[i], fleet.lon[i]);
farthest = Math.max(farthest, d);
assert.ok(d <= NEAR.radiusM + 1e-6, `aircraft ${i} is ${d} m away`);
assert.ok(
fleet.alt[i] >= 300 && fleet.alt[i] <= 3_000,
`alt ${fleet.alt[i]}`,
);
assert.ok(fleet.heading[i] >= 0 && fleet.heading[i] < 360);
}
// Uniform over the disc, not bunched at the centre.
assert.ok(farthest > NEAR.radiusM * 0.8, `farthest ${farthest}`);
});
test('near aircraft are spread over the disc area', () => {
const near = { ...NEAR, count: 2_000 };
const fleet = createSyntheticFlights({ count: 2_000, seed: 2, near });
let inner = 0;
for (let i = 0; i < near.count; i++) {
const d = haversineM(near.lat, near.lng, fleet.lat[i], fleet.lon[i]);
if (d <= near.radiusM / Math.SQRT2) inner++;
}
// Half the area lies inside radius / sqrt(2).
assert.ok(
Math.abs(inner / near.count - 0.5) < 0.05,
`inner share ${inner / near.count}`,
);
});
test('the rest of a fleet with near aircraft keeps the global ranges', () => {
const fleet = createSyntheticFlights({ count: 1_000, seed: 7, near: NEAR });
let far = 0;
for (let i = NEAR.count; i < fleet.count; i++) {
assert.ok(fleet.alt[i] >= 1_000 && fleet.alt[i] <= 12_000);
assert.ok(fleet.speed[i] >= 120 && fleet.speed[i] <= 250);
if (haversineM(NEAR.lat, NEAR.lng, fleet.lat[i], fleet.lon[i]) > 100_000)
far++;
}
assert.ok(far > 900, `only ${far} aircraft spread over the globe`);
});
test('a fleet with near aircraft is deterministic for its seed', () => {
const a = createSyntheticFlights({ count: 200, seed: 5, near: NEAR });
const b = createSyntheticFlights({ count: 200, seed: 5, near: NEAR });
const c = createSyntheticFlights({ count: 200, seed: 6, near: NEAR });
assert.deepEqual(a.lat, b.lat);
assert.deepEqual(a.lon, b.lon);
assert.deepEqual(a.alt, b.alt);
assert.notDeepEqual(a.lat, c.lat);
});
test('near aircraft fly slowly enough to stay in a city view during a run', () => {
const fleet = createSyntheticFlights({ count: 100, seed: 3, near: NEAR });
for (let i = 0; i < NEAR.count; i++)
assert.ok(
fleet.speed[i] >= 10 && fleet.speed[i] <= 40,
`speed ${fleet.speed[i]}`,
);
});
test('a near block can override its altitude and speed bands', () => {
const near = {
...NEAR,
minAltM: 60,
maxAltM: 190,
minSpeedMps: 1,
maxSpeedMps: 5,
};
const fleet = createSyntheticFlights({ count: 300, seed: 9, near });
for (let i = 0; i < near.count; i++) {
assert.ok(fleet.alt[i] >= 60 && fleet.alt[i] <= 190, `alt ${fleet.alt[i]}`);
assert.ok(
fleet.speed[i] >= 1 && fleet.speed[i] <= 5,
`speed ${fleet.speed[i]}`,
);
}
assert.throws(
() =>
createSyntheticFlights({
count: 10,
near: { ...NEAR, count: 1, minAltM: 5, maxAltM: 4 },
}),
RangeError,
);
});
test('rejects a near block larger than the fleet or with a bad radius', () => {
assert.throws(
() => createSyntheticFlights({ count: 10, near: { ...NEAR, count: 11 } }),
RangeError,
);
assert.throws(
() => createSyntheticFlights({ count: 10, near: { ...NEAR, count: 1.5 } }),
RangeError,
);
assert.throws(
() =>
createSyntheticFlights({
count: 10,
near: { ...NEAR, count: 5, radiusM: -1 },
}),
RangeError,
);
});
test('a near block of zero aircraft changes nothing but the stream position', () => {
const fleet = createSyntheticFlights({
count: 0,
near: { ...NEAR, count: 0 },
});
assert.equal(fleet.count, 0);
});