From 17364d1eee985d46f3b552289164b252f17ff9af Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Sun, 8 Nov 2026 00:40:29 -0800 Subject: [PATCH] Settle unchanging solid lines into static batches in the Cesium draw port An undraped solid line (straight or geodesic, without a depth-fail colour, not revealed, at least half a pixel wide) is drawn by the set's polyline collection at once, as before. Once the set's lines have not changed for 30 drawn frames, every such line is built into static batches beside the collection: a primitive of polyline geometries for opaque colours and one for translucent ones, each line coloured and shown by its own instance attributes under the pick id it had. A batch builds hidden, off the main thread, and swaps in for the batch before it and the collection's polylines once it is ready, each line showing as its item then asks. A polyline collection works out every line's material every frame; a batch costs nothing until it changes. The data centre stems (4,362 lines) cost about 16 ms a frame in the collection and about 0.01 ms settled. A batched line changes its show, colour and meta in place. One that moves, or whose colour moves it between the opaque and translucent passes, leaves its batch, hidden there, for the collection until the set settles again; when a batch exists and fewer than 64 lines are left in the collection, they settle after 300 frames, as rebuilding the batch for a few lines costs more than drawing them. A line in the collection that moves or changes colour drops a batch still building; a show change does not. A colour-only change of such a line is now made in place in the collection too, where it rebuilt the set. A batch whose build fails is reported once and its lines stay in the collection. Glow, dash, arrow, flow, pattern, revealed and depth-fail lines draw as they did. The shared conformance suite checks solid lines left unchanged for many frames still pick, hide, recolour and move in place. --- src/render/cesium/draw.js | 362 +++++++++++++++++++++++-- src/render/cesium/draw.test.mjs | 338 ++++++++++++++++++++++- src/render/cesium/harness.js | 1 + src/render/conformance/checks/ports.js | 67 +++++ 4 files changed, 734 insertions(+), 34 deletions(-) diff --git a/src/render/cesium/draw.js b/src/render/cesium/draw.js index 2a23938f2..bc93c9692 100644 --- a/src/render/cesium/draw.js +++ b/src/render/cesium/draw.js @@ -19,6 +19,11 @@ * draped lines of a set made with `pickable: false` stay out of the pick * pass, so they never hide the ground geometry drawn under them from picks. * + * Solid lines in space start in a polyline collection, which costs every + * line every frame; once a set's lines stop changing for a few frames they + * settle into static batches that cost nothing until they change, and a + * line that moves again goes back to the collection (`settleLines`). + * * Icon sets whose items move, glide, recede, pick by proxy or switch images * by size, or that take columns, draw on the GPU instead (`gpuIcons.js`). * @@ -129,6 +134,21 @@ class UnpickableGroundPrimitive extends Cesium.GroundPrimitive { * Images an icon set's collection may hold, or twice those it draws when * that is more, before it is built anew holding only those it draws. */ +/** + * A static batch of lines (`settleLines`): a build that fails keeps its + * error and draws nothing, where a primitive would throw it every frame. + */ +class StaticLines extends Cesium.Primitive { + update(frameState) { + if (this.failed) return; + try { + super.update(frameState); + } catch (error) { + this.failed = error; + } + } +} + const MAX_SPARE_IMAGES = 64; const ANCHORS = { @@ -236,6 +256,20 @@ function flowTime(speed) { return (cells - Math.floor(cells)) / speed; } +/** + * Drawn frames a line set's lines stay unchanged before the solid ones + * among them settle into a static batch (`settleLines`). + */ +export const SETTLE_FRAMES = 30; + +/** + * Fewer lines than this left in the collection beside a static batch wait + * `LONG_SETTLE_FRAMES` to settle, as rebuilding the batch for them costs + * more than drawing them in the collection for a while. + */ +export const FEW_LOOSE_LINES = 64; +export const LONG_SETTLE_FRAMES = 300; + /** What `syncIcons` did: icons added or removed, or only changed in place. */ const ICONS_CHANGED = 'changed'; const ICONS_KEPT = 'kept'; @@ -1629,6 +1663,7 @@ export function createCesiumDraw({ record.lineParts = parts; // Lines with a depth-fail colour, beside the collection. let deep = null; + const looks = lineLooks(); for (const item of record.items.values()) { if (item.mode === 'draped') { const clampTo = item.clampTo ?? record.spec.clampTo ?? 'both'; @@ -1676,31 +1711,37 @@ export function createCesiumDraw({ parts.set(item.id, { look: lineLook(item), id, deep: primitive }); continue; } - const positions = linePositions(item); - const reveal = revealed(item) - ? revealUniform(item.revealFraction, positions) - : undefined; - const material = lineMaterial(record, item.style, reveal); - const polyline = polylines.add({ - positions, - width: item.widthPx, - material, - show: item.show !== false, - id: pickId(record, item), - }); + const id = pickId(record, item); + const { polyline, reveal } = addPolyline(record, polylines, item, id); + const settled = settles(item); parts.set(item.id, { look: lineLook(item), + // How it draws but for its colour, and where it runs, to tell a + // change it takes in place: kept for lines that can settle. + shape: settled ? looks.shape(item) : null, + positions: settled ? flatPositions(item.positions) : null, + id, polyline, - reveal: reveal === undefined ? null : material, + reveal, + // Drawn in the set's static batch instead (`settleLines`). + batched: null, }); } let own = polylines; - if (deep) { + // Lines in the collection that can settle into a static batch draw + // beside it once they do. + const settling = [...record.items.values()].some(settles); + if (deep || settling) { own = new Cesium.PrimitiveCollection(); own.add(polylines); - own.add(deep); + if (deep) own.add(deep); } record.deepLines = deep; + record.polylines = polylines; + record.lineBox = settling ? own : null; + record.batch = null; + record.batchPending = null; + record.settled = 0; if (!draped.size && !glows.size && !alone.length) { noteFlows(record); return replaceHost(record, own); @@ -1872,6 +1913,14 @@ export function createCesiumDraw({ !revealed(item) && was.shape === (looks ? looks.shape(item) : lineLook(item, true)); + /** + * Whether an undraped line that can settle (in the collection or a + * static batch) changes only its colour: it takes the new one in place. + */ + const recolourableSolid = (was, item, looks) => + Boolean((was.polyline || was.batched) && was.shape) && + was.shape === looks.shape(item); + /** * The colours the glow lines sharing a material that `list` recolours * are given together: each such material and the item whose colour it @@ -1938,7 +1987,14 @@ export function createCesiumDraw({ if (!was) return false; const look = lookOf.look(item); // Only its colour may change: its material takes it in place. - if (was.look !== look && !recolourable(was, item, lookOf)) return false; + if ( + was.look !== look && + !recolourable(was, item, lookOf) && + !recolourableSolid(was, item, lookOf) + ) + return false; + // A viewer destroyed under the set took its batch with it. + if (was.batched?.isDestroyed()) return false; looks.push(look); if (was.deep !== undefined && record.deepLines?.isDestroyed()) return false; @@ -1965,24 +2021,16 @@ export function createCesiumDraw({ const item = list[k]; const was = parts.get(item.id); const show = item.show !== false; + if (was.polyline || was.batched) { + patchUndraped(record, was, item, looks[k]); + continue; + } if (was.look !== looks[k]) { // A line drawing on a shared material took its colour above. if (!was.shared || was.solo) was.material.uniforms.color = toColor(item.style.color); was.look = looks[k]; } - if (was.polyline) { - const positions = linePositions(item); - was.polyline.positions = positions; - was.polyline.show = show; - was.polyline.id.meta = item.meta; - if (was.reveal) - was.reveal.uniforms.reveal = revealUniform( - item.revealFraction, - positions, - ); - continue; - } if (was.deep !== undefined) { was.id.meta = item.meta; // Built anew where it now runs, under the same pick id. @@ -2029,6 +2077,255 @@ export function createCesiumDraw({ return true; } + /** + * An undraped line without a depth-fail colour in the set's polyline + * collection, under pick id `id`; `reveal` is the material its reveal + * is set on, or null. + */ + function addPolyline(record, polylines, item, id) { + const positions = linePositions(item); + const reveal = revealed(item) + ? revealUniform(item.revealFraction, positions) + : undefined; + const material = lineMaterial(record, item.style, reveal); + const polyline = polylines.add({ + positions, + width: item.widthPx, + material, + show: item.show !== false, + id, + }); + return { polyline, reveal: reveal === undefined ? null : material }; + } + + /** + * Whether an undraped line can be drawn in a static batch: solid, all of + * it shown, at least half a pixel wide, and without a depth-fail colour + * (those draw alone). + */ + const settles = (item) => + item.mode !== 'draped' && + !item.depthFailColor && + item.style.kind === 'solid' && + !revealed(item) && + // The collection draws no line under half a pixel wide; a batch + // would. + item.widthPx >= 0.5; + + const finitePoint = (p) => + Number.isFinite(p.x) && Number.isFinite(p.y) && Number.isFinite(p.z); + + /** Whether a line's colour is drawn in a batch's translucent pass. */ + const translucentLine = (item) => toColor(item.style.color).alpha < 1; + + /** + * Stop waiting for a set's lines to settle: they changed. A static batch + * still building drew them as they were, so it is dropped. + */ + function unsettle(record) { + record.settled = 0; + const pending = record.batchPending; + if (!pending) return; + record.batchPending = null; + if (record.lineBox && !record.lineBox.isDestroyed()) + for (const primitive of pending.primitives) + record.lineBox.remove(primitive); + } + + /** + * Change an undraped line drawn in the collection or a static batch in + * place: its show and colour, and where it runs. A batched line that + * moves, or whose colour leaves its batch's pass, leaves the batch, + * hidden there, for the collection, until the set settles again. + */ + function patchUndraped(record, was, item, look) { + const show = item.show !== false; + // The polyline's pick id and the batch instance's are this one. + was.id.meta = item.meta; + const recolour = was.look !== look; + was.look = look; + if (was.batched) { + const stays = + flatEquals(item.positions, was.positions) && + (!recolour || translucentLine(item) === was.translucent); + const attributes = was.batched.getGeometryInstanceAttributes(was.id); + if (stays) { + // A batch still building draws the colour it was built with. + if (recolour) { + if (record.batchPending) unsettle(record); + attributes.color = Cesium.ColorGeometryInstanceAttribute.toValue( + toColor(item.style.color), + attributes.color, + ); + } + attributes.show = Cesium.ShowGeometryInstanceAttribute.toValue( + show, + attributes.show, + ); + return; + } + attributes.show = Cesium.ShowGeometryInstanceAttribute.toValue( + false, + attributes.show, + ); + was.batched = null; + was.positions = flatPositions(item.positions); + Object.assign(was, addPolyline(record, record.polylines, item, was.id)); + unsettle(record); + return; + } + // Only a change a batch would draw unsettles the set: a show is taken + // when the batch swaps in, and lines that cannot settle are not in it. + if (was.shape && (recolour || !flatEquals(item.positions, was.positions))) { + was.positions = flatPositions(item.positions); + unsettle(record); + } + const positions = linePositions(item); + was.polyline.positions = positions; + was.polyline.show = show; + if (recolour) + was.polyline.material.uniforms.color = toColor(item.style.color); + if (was.reveal) + was.reveal.uniforms.reveal = revealUniform( + item.revealFraction, + positions, + ); + } + + /** + * After a draw: a line set whose lines have not changed for + * `SETTLE_FRAMES` drawn frames starts building its static batch, of all + * its lines that `settles`, once some are still in the collection (when + * it has a batch and only a few are, after `LONG_SETTLE_FRAMES`); one + * built swaps in for the collection's lines it draws (`swapBatch`). + * Unchanged lines in a polyline collection cost a material id a line + * every frame; in a batch they cost nothing until they change. + */ + function settleLines(record) { + if (!record.lineBox || record.lineBox.isDestroyed()) return; + if (record.batchPending) { + swapBatch(record); + return; + } + if (record.settled > LONG_SETTLE_FRAMES) return; + record.settled += 1; + if ( + record.settled !== SETTLE_FRAMES && + record.settled !== LONG_SETTLE_FRAMES + ) + return; + // The lines that can settle still in the collection: a batch is + // rebuilt whole for them, so a few wait longer, as a few cost little. + let loose = 0; + for (const part of record.lineParts.values()) + if (part.polyline && part.shape) loose += 1; + if ( + record.batch && + loose < FEW_LOOSE_LINES && + record.settled < LONG_SETTLE_FRAMES + ) + return; + record.settled = LONG_SETTLE_FRAMES + 1; + if (!loose) return; + const members = []; + for (const [itemId, part] of record.lineParts) { + if (!part.shape || (!part.polyline && !part.batched)) continue; + const item = record.items.get(itemId); + if (!item || !settles(item)) continue; + const positions = linePositions(item); + // A batch that fails to build would throw in every frame after. + if (!drawable(positions) || !positions.every(finitePoint)) continue; + members.push({ part, item, positions }); + } + if (!members.some(({ part }) => part.polyline)) return; + const groups = [[], []]; + for (const member of members) { + member.translucent = translucentLine(member.item); + member.show = member.item.show !== false; + groups[member.translucent ? 1 : 0].push( + new Cesium.GeometryInstance({ + geometry: new Cesium.PolylineGeometry({ + positions: member.positions, + width: member.item.widthPx, + vertexFormat: Cesium.PolylineColorAppearance.VERTEX_FORMAT, + // Straight between the positions, as the collection's lines + // are (geodesic ones come densified). + arcType: Cesium.ArcType.NONE, + }), + attributes: { + color: Cesium.ColorGeometryInstanceAttribute.fromColor( + toColor(member.item.style.color), + ), + show: new Cesium.ShowGeometryInstanceAttribute(member.show), + }, + id: member.part.id, + }), + ); + member.flat = flatPositions(member.item.positions); + } + const primitives = []; + for (const [translucent, geometryInstances] of groups.entries()) { + if (!geometryInstances.length) continue; + const primitive = new StaticLines({ + geometryInstances, + appearance: new Cesium.PolylineColorAppearance({ + translucent: Boolean(translucent), + }), + // Hidden while it builds: the collection draws its lines. + show: false, + }); + primitive.translucentLines = Boolean(translucent); + primitives.push(primitive); + record.lineBox.add(primitive); + } + record.batchPending = { primitives, members }; + } + + /** + * A static batch built: it draws its lines in place of the collection's + * polylines and the batch before it, each line showing as its item now + * asks (shows changed while it built). + */ + function swapBatch(record) { + const { primitives, members } = record.batchPending; + const failed = primitives.find((p) => p.failed); + if (failed) { + // The collection draws on; the set settles no more. + unsettle(record); + record.lineBox = null; + throw failed.failed; + } + if (primitives.some((p) => !p.ready)) return; + record.batchPending = null; + const box = record.lineBox; + for (const primitive of record.batch ?? []) box.remove(primitive); + record.batch = primitives; + const byPass = new Map(primitives.map((p) => [p.translucentLines, p])); + let removed = 0; + for (const { part, item, translucent, show, flat } of members) { + const batch = byPass.get(translucent); + if (part.polyline) { + record.polylines.remove(part.polyline); + part.polyline = null; + part.reveal = null; + removed += 1; + } + part.batched = batch; + part.translucent = translucent; + part.positions = flat; + const now = record.items.get(item.id)?.show !== false; + if (now !== show) { + const attributes = batch.getGeometryInstanceAttributes(part.id); + attributes.show = Cesium.ShowGeometryInstanceAttribute.toValue( + now, + attributes.show, + ); + } + } + for (const primitive of primitives) primitive.show = true; + if (removed) requestRender(); + } + /** * Draw a draped line where it now runs, in this frame: on a ground * primitive of its own built at once, in place of its own one (or of its @@ -2783,6 +3080,15 @@ export function createCesiumDraw({ afterRender() { watchClamp(); swapReady(); + for (const record of sets.values()) + if (record.kind === 'lines') + try { + settleLines(record); + } catch (error) { + // Its lines stay in the collection, which draws them all. + record.lineBox = null; + report(error); + } // Rows moving on a live clock, and flowing lines, draw on in the // frames that follow. if ( diff --git a/src/render/cesium/draw.test.mjs b/src/render/cesium/draw.test.mjs index d547928e1..361e7da7e 100644 --- a/src/render/cesium/draw.test.mjs +++ b/src/render/cesium/draw.test.mjs @@ -2,7 +2,14 @@ import assert from 'node:assert/strict'; import * as Cesium from 'cesium'; import test from 'node:test'; import { bindRenderer, drawOf, findDraw } from '../rendererOf.js'; -import { createCesiumDraw, revealUniform, unboundDrawPart } from './draw.js'; +import { + createCesiumDraw, + FEW_LOOSE_LINES, + LONG_SETTLE_FRAMES, + revealUniform, + SETTLE_FRAMES, + unboundDrawPart, +} from './draw.js'; import { drawHitOf } from './drawHit.js'; import { MODEL_RETRY_MS } from './drawModels.js'; @@ -1942,10 +1949,11 @@ test('lines that change only whether they show, or where an undraped one runs, c const [, idB] = ground.geometryInstances.map((instance) => instance.id); assert.equal(idB.meta, 'new', 'picks read the new meta'); // An undraped line moves and hides in place, and keeps its new meta. - const line = host.get(0).get(0); + // Beside the collection, a static batch once the set settles. + const line = host.get(0).get(0).get(0); set.upsert([stem('s', 2500, false)]); assert.equal(scene.added[0], host); - assert.equal(host.get(0).get(0), line); + assert.equal(host.get(0).get(0).get(0), line); assert.equal(line.show, false); assert.equal(line.id.meta, 2500); assert.ok( @@ -1980,6 +1988,322 @@ test('lines that change only whether they show, or where an undraped one runs, c set.dispose(); })); +/** A solid 3D stem for the settling tests. */ +const settleStem = (id, height, extra = {}) => ({ + id, + positions: [ + Cesium.Cartesian3.fromDegrees(0, 0, 0), + Cesium.Cartesian3.fromDegrees(0, 0, height), + ], + widthPx: 2, + mode: '3d', + style: { kind: 'solid', color: [1, 0, 0, 1] }, + meta: height, + ...extra, +}); + +/** + * A line set over a fake scene with what the settling tests read: its + * host, the polyline collection, the static batches beside it, frames to + * settle, and a batch's build finished, its instance attributes kept per + * batch as a built primitive keeps them. + */ +function settlingSet(id = 'stems') { + const { scene, draw, frame } = drawOver(); + const set = draw.lines({ id }); + const attributes = new WeakMap(); + const attrs = (primitive, itemId) => attributes.get(primitive).get(itemId); + return { + scene, + set, + frame, + host: () => scene.added[0], + polylines: () => scene.added[0].get(0), + batches() { + const host = scene.added[0]; + const found = []; + for (let i = 1; i < host.length; i++) found.push(host.get(i)); + return found; + }, + settle() { + for (let i = 0; i < SETTLE_FRAMES; i++) frame(); + }, + built(primitive) { + Object.defineProperty(primitive, 'ready', { value: true }); + const own = new Map(); + attributes.set(primitive, own); + primitive.getGeometryInstanceAttributes = (pickId) => { + if (!own.has(pickId.id)) { + const instance = primitive.geometryInstances.find( + (one) => one.id === pickId, + ); + if (!instance) return undefined; + own.set(pickId.id, { + show: new Uint8Array(instance.attributes.show.value), + color: new Uint8Array(instance.attributes.color.value), + }); + } + return own.get(pickId.id); + }; + }, + attrs, + }; +} + +test('unchanged solid 3D lines settle into a static batch, changed there in place', () => + withImageClasses(() => { + const s = settlingSet(); + const { set, frame } = s; + const stem = settleStem; + const glow = { + id: 'glow', + positions: stem('g', 1).positions, + widthPx: 4, + mode: '3d', + style: { kind: 'glow', color: '#fff', power: 0.2 }, + }; + set.set([ + stem('a', 100), + stem('b', 200, { style: { kind: 'solid', color: [0, 1, 0, 0.5] } }), + stem('c', 300, { show: false }), + glow, + ]); + const host = s.host(); + const polylines = s.polylines(); + assert.ok(polylines instanceof Cesium.PolylineCollection); + assert.equal(polylines.length, 4, 'drawn by the collection at once'); + for (let i = 0; i < SETTLE_FRAMES - 1; i++) frame(); + assert.equal(host.length, 1, 'not before it settles'); + frame(); + // Opaque and translucent lines draw in passes of their own. + const [opaque, translucent] = s.batches(); + assert.ok(opaque instanceof Cesium.Primitive); + assert.equal(opaque.show, false, 'hidden while it builds'); + assert.equal(opaque.appearance.translucent, false); + assert.equal(translucent.appearance.translucent, true); + assert.deepEqual( + opaque.geometryInstances.map((one) => one.id.id), + ['a', 'c'], + 'every solid line, shown or not; the glow stays in the collection', + ); + const [a, c] = opaque.geometryInstances; + assert.ok(a.geometry instanceof Cesium.PolylineGeometry); + assert.equal(a.geometry._width, 2); + assert.deepEqual([...c.attributes.show.value], [0]); + assert.deepEqual([...a.attributes.color.value], [255, 0, 0, 255]); + // Picks name the set and item, through the same pick ids. + assert.equal(a.id, polylines.get(0).id); + assert.deepEqual(drawHitOf({ primitive: opaque, id: a.id }), { + set: 'stems', + id: 'a', + meta: 100, + surface: false, + }); + // A line in the collection that moves drops the build, which drew it + // where it was; one shown, or a glow line changed, does not. + set.upsert([stem('c', 300)]); + set.upsert([{ ...glow, widthPx: 4, show: false }]); + assert.equal(s.host(), host, 'changed in place'); + assert.equal(s.batches().length, 2, 'still building'); + set.upsert([stem('a', 150)]); + assert.equal(s.batches().length, 0, 'dropped'); + set.upsert([stem('a', 100)]); + s.settle(); + const [opaqueAgain, translucentAgain] = s.batches(); + s.built(opaqueAgain); + s.built(translucentAgain); + frame(); + assert.equal(opaqueAgain.show, true, 'swapped in once built'); + assert.equal(polylines.length, 1, 'the glow alone left in the collection'); + assert.equal(polylines.get(0).id.id, 'glow'); + // Show, colour and meta change in place on the batch. + set.upsert([ + stem('a', 100, { show: false }), + stem('c', 300, { style: { kind: 'solid', color: [0, 0, 1, 1] } }), + ]); + assert.equal(s.host(), host); + assert.deepEqual([...s.attrs(opaqueAgain, 'a').show], [0]); + assert.deepEqual([...s.attrs(opaqueAgain, 'c').color], [0, 0, 255, 255]); + assert.equal(polylines.length, 1, 'nothing left the batch'); + set.upsert([stem('a', 100, { meta: 'new' })]); + assert.deepEqual([...s.attrs(opaqueAgain, 'a').show], [1]); + assert.equal(opaqueAgain.geometryInstances[0].id.meta, 'new'); + // A line that moves leaves the batch, hidden there, for the + // collection; it settles back with the rest. + set.upsert([stem('a', 900)]); + assert.equal(s.host(), host); + assert.deepEqual([...s.attrs(opaqueAgain, 'a').show], [0]); + assert.equal(polylines.length, 2); + const moved = polylines.get(1); + assert.equal(moved.id.id, 'a'); + assert.ok( + Cesium.Cartesian3.equalsEpsilon( + moved.positions[1], + Cesium.Cartesian3.fromDegrees(0, 0, 900), + 1e-6, + ), + ); + // So does one turned translucent. + set.upsert([ + stem('c', 300, { style: { kind: 'solid', color: [0, 0, 1, 0.4] } }), + ]); + assert.equal(polylines.length, 3); + // Two lines are few beside a batch: they wait longer. + s.settle(); + assert.equal(s.batches().length, 2, 'not yet'); + for (let i = SETTLE_FRAMES; i < LONG_SETTLE_FRAMES; i++) frame(); + const next = s.batches().slice(2); + assert.equal(next.length, 2, 'built beside the batches drawing now'); + assert.deepEqual( + next.flatMap((one) => one.geometryInstances.map((i) => i.id.id)), + ['a', 'b', 'c'], + ); + // A batched line shown or hidden while the next batch builds shows + // there as its item now asks, once it is built. + set.upsert([ + stem('b', 200, { + style: { kind: 'solid', color: [0, 1, 0, 0.5] }, + show: false, + }), + ]); + assert.deepEqual([...s.attrs(translucentAgain, 'b').show], [0]); + assert.equal(s.batches().length, 4, 'the build goes on'); + next.forEach(s.built); + frame(); + assert.deepEqual(s.batches(), next, 'the old batches are gone'); + assert.equal(opaqueAgain.isDestroyed(), true); + assert.deepEqual([...s.attrs(next[1], 'b').show], [0]); + assert.equal(polylines.length, 1); + // A line that looks different is still a rebuild. + set.upsert([stem('b', 200, { widthPx: 5 })]); + assert.notEqual(s.host(), host); + set.dispose(); + })); + +test('many lines left beside a static batch settle as soon as they are unchanged', () => + withImageClasses(() => { + const s = settlingSet(); + const { set } = s; + const stems = (height) => + Array.from({ length: FEW_LOOSE_LINES + 1 }, (_, i) => + settleStem(`s${i}`, height + i), + ); + set.set(stems(100)); + s.settle(); + const [first] = s.batches(); + s.built(first); + s.frame(); + set.upsert(stems(500)); + assert.equal(s.polylines().length, FEW_LOOSE_LINES + 1, 'all moved'); + s.settle(); + assert.equal(s.batches().length, 2, 'rebuilding at once'); + set.dispose(); + })); + +test('a line under half a pixel wide, which the collection does not draw, never settles', () => + withImageClasses(() => { + const s = settlingSet(); + s.set.set([ + settleStem('thin', 100, { widthPx: 0.25 }), + settleStem('a', 200), + ]); + s.settle(); + const [batch] = s.batches(); + assert.deepEqual( + batch.geometryInstances.map((one) => one.id.id), + ['a'], + ); + s.set.dispose(); + })); + +test('a settling line set keeps what was drawn, not the item a caller changes in place', () => + withImageClasses(() => { + const s = settlingSet(); + const { set } = s; + const a = settleStem('a', 100); + set.set([a]); + const host = s.host(); + // In the collection, a colour change takes the polyline's material in + // place. + set.upsert([{ ...a, style: { kind: 'solid', color: [0, 1, 0, 1] } }]); + assert.equal(s.host(), host, 'recoloured in place'); + assert.ok( + Cesium.Color.equals( + s.polylines().get(0).material.uniforms.color, + new Cesium.Color(0, 1, 0, 1), + ), + ); + // The same object, changed and sent again, is compared with what was + // drawn: a new width rebuilds. + const same = { ...a }; + set.upsert([same]); + same.widthPx = 6; + set.upsert([same]); + assert.notEqual(s.host(), host, 'rebuilt for the new width'); + assert.equal(s.polylines().get(0).width, 6); + set.dispose(); + })); + +test('a settling line set rebuilt or gone while its batch builds, or after the viewer went, lets the batch go', () => + withImageClasses(() => { + const s = settlingSet(); + const { set } = s; + set.set([settleStem('a', 100), settleStem('b', 200)]); + s.settle(); + const [building] = s.batches(); + assert.ok(building, 'building'); + // A rebuild draws a new host; the batch goes with the old one. + const old = s.host(); + set.set([settleStem('a', 100)]); + assert.ok(!s.scene.added.includes(old), 'the old host is gone'); + assert.equal(old.get(1), building); + assert.equal(s.batches().length, 0); + // Its build finishing later changes nothing. + s.built(building); + s.frame(); + assert.equal(building.show, false); + s.settle(); + const [again] = s.batches(); + s.built(again); + s.frame(); + assert.equal(again.show, true); + // A viewer destroyed under the set takes its batch with it: an upsert + // after that rebuilds rather than write to it. + s.host().destroy(); + assert.equal(again.isDestroyed(), true); + set.upsert([settleStem('a', 100, { show: false })]); + assert.equal(s.polylines().get(0).show, false, 'drawn anew, hidden'); + set.dispose(); + })); + +test('a static batch that fails to build is reported once, and its lines stay in the collection', () => + withImageClasses(() => { + const scene = fakeScene(); + const reported = []; + const part = createCesiumDraw({ + scene, + report: (error) => reported.push(error), + }); + const set = part.port.lines({ id: 'failing' }); + set.set([settleStem('a', 100)]); + for (let i = 0; i < SETTLE_FRAMES; i++) part.afterRender(); + const host = scene.added[0]; + const batch = host.get(1); + assert.ok(batch, 'building'); + // Its build throws, as a primitive whose workers failed does: the + // batch keeps the error rather than throw it every frame. + batch.update({ passes: { render: true }, mode: Cesium.SceneMode.SCENE3D }); + assert.ok(batch.failed, 'kept'); + part.afterRender(); + assert.deepEqual(reported, [batch.failed]); + assert.equal(host.length, 1, 'the build is gone'); + assert.equal(host.get(0).length, 1, 'the collection draws the line'); + for (let i = 0; i < SETTLE_FRAMES * 2; i++) part.afterRender(); + assert.equal(host.length, 1, 'it settles no more'); + assert.equal(reported.length, 1); + set.dispose(); + })); + test('a draped line with segment colours and shows draws a ground instance a segment, changed in place', () => withImageClasses(() => { const { scene, draw, frame } = drawOver(); @@ -2987,14 +3311,16 @@ test('scene data packs draw their geometry and media marker through the Cesium d assert.equal(points.get(0).pixelSize, 12); assert.equal(points.get(0).outlineWidth, 2); assert.ok(Cesium.Color.CYAN.equals(points.get(0).color)); - assert.ok(lines instanceof Cesium.PolylineCollection); - assert.equal(lines.get(0).width, 4); + // The collection, beside where its static batch goes. + assert.ok(lines.get(0) instanceof Cesium.PolylineCollection); + assert.equal(lines.get(0).get(0).width, 4); const fill = shapes.get(0); assert.ok(fill.geometryInstances.geometry instanceof Cesium.PolygonGeometry); assert.equal(fill.appearance.translucent, true); assert.equal(fill.appearance.flat, true); // A click names the feature's set and item, which the targets hold. - const hit = drawHitOf({ primitive: lines.get(0), id: lines.get(0).id }); + const line = lines.get(0).get(0); + const hit = drawHitOf({ primitive: line, id: line.id }); assert.deepEqual(targets.get(JSON.stringify(['data', 'line'])), { set: hit.set, id: hit.id, diff --git a/src/render/cesium/harness.js b/src/render/cesium/harness.js index 6fa2639ee..3e9d304a9 100644 --- a/src/render/cesium/harness.js +++ b/src/render/cesium/harness.js @@ -42,6 +42,7 @@ export const CESIUM_CHECKS = new Set([ 'draw.icons.distance', 'draw.icons.update', 'draw.lines.depthFailDash', + 'draw.lines.settled', 'draw.lines.styles', 'draw.lines.drape', 'draw.lines.segments', diff --git a/src/render/conformance/checks/ports.js b/src/render/conformance/checks/ports.js index d79bd8ea9..d1411f6af 100644 --- a/src/render/conformance/checks/ports.js +++ b/src/render/conformance/checks/ports.js @@ -1378,6 +1378,73 @@ export function portChecks() { lines.dispose(); }, ), + check( + 'draw.lines.settled', + 'solid lines in space left unchanged for many frames still pick, hide, recolour and move in place', + async (h, r) => { + await overAustin(h, r); + const at = (dLon, dLat, height = 300) => + h.toEcef({ lon: AUSTIN.lon + dLon, lat: AUSTIN.lat + dLat, height }); + const lines = r.draw.lines({ id: 'conf:line-settled' }); + const mine = (pos, id) => { + const s = r.query.project(pos); + return Boolean( + s && + r.query + .drillPick(s.x, s.y) + .some((hit) => hit.set === 'conf:line-settled' && hit.id === id), + ); + }; + const until = async (test) => { + for (let i = 0; i < 300; i++) { + await drawn(h, r, 1); + if (test()) return true; + } + return false; + }; + const line = (id, dLat, extra = {}) => ({ + id, + positions: [at(-0.003, dLat), at(0.003, dLat)], + widthPx: 8, + mode: '3d', + style: { kind: 'solid', color: [1, 0.4, 0.1, 1] }, + ...extra, + }); + lines.set([ + line('a', 0.002), + line('b', -0.002, { + style: { kind: 'solid', color: [0.1, 0.6, 1, 0.6] }, + }), + ]); + ok(await until(() => mine(at(0, 0.002), 'a')), 'drawn'); + // Long enough for a renderer to draw unchanged lines another way. + await drawn(h, r, 120); + ok(mine(at(0, 0.002), 'a'), 'still picks once settled'); + ok(mine(at(0, -0.002), 'b'), 'a translucent one too'); + lines.upsert([line('a', 0.002, { show: false })]); + await drawn(h, r); + ok(!mine(at(0, 0.002), 'a'), 'hidden in place'); + ok(mine(at(0, -0.002), 'b'), 'the other still drawn'); + lines.upsert([ + line('a', 0.002, { + style: { kind: 'solid', color: [0.2, 1, 0.2, 1] }, + }), + ]); + ok(await until(() => mine(at(0, 0.002), 'a')), 'shown and recoloured'); + await drawn(h, r, 120); + lines.upsert([line('b', -0.004)]); + ok( + await until( + () => mine(at(0, -0.004), 'b') && !mine(at(0, -0.002), 'b'), + ), + 'moved', + ); + equal(lines.size(), 2, 'held'); + lines.dispose(); + await drawn(h, r); + ok(!mine(at(0, 0.002), 'a'), 'gone with its set'); + }, + ), check( 'draw.shapes.drapedRotation', 'a draped rectangle with an image fill draws turned by its rotation, picks there, and hides in place',