diff --git a/src/data/iconAtlas.js b/src/data/iconAtlas.js index d8e18044b..019c36c26 100644 --- a/src/data/iconAtlas.js +++ b/src/data/iconAtlas.js @@ -32,8 +32,33 @@ export function nearImageSwitchPx(pixelRatio = 1) { return NEAR_IMAGE_DEVICE_PX / ratio; } -/** Slots a renderer can address (one vec4 uniform each). */ -export const MAX_ICON_SLOTS = 64; +/** + * Slots the atlas holds. A renderer addresses each through one vec4 + * uniform, as many as its context has room for (`addressableIconSlots`): + * every WebGL 2 context has room for all of them. + */ +export const MAX_ICON_SLOTS = 176; +/** Slots the renderer addresses, however few uniforms its context has. */ +export const MIN_ICON_SLOTS = 64; +/** + * Vertex uniform vectors the icon shader keeps for everything else: its own + * and the renderer's (about 34), with room for drivers that keep constants + * in uniforms. + */ +const OTHER_VERTEX_UNIFORMS = 80; + +/** + * How many slots a context with `maxVertexUniformVectors` addresses: all of + * them on WebGL 2 (at least 256 vectors), at least `MIN_ICON_SLOTS`. + * @param {number} maxVertexUniformVectors + */ +export function addressableIconSlots(maxVertexUniformVectors) { + const room = Math.floor(maxVertexUniformVectors) - OTHER_VERTEX_UNIFORMS; + return Math.max( + MIN_ICON_SLOTS, + Math.min(MAX_ICON_SLOTS, Number.isFinite(room) ? room : 0), + ); +} const PADDING_PX = 2; const START_SIZE_PX = 1024; diff --git a/src/data/iconSetPrimitive.js b/src/data/iconSetPrimitive.js index a80eb0320..88d78d511 100644 --- a/src/data/iconSetPrimitive.js +++ b/src/data/iconSetPrimitive.js @@ -18,8 +18,10 @@ import * as Cesium from 'cesium'; import { ICON_SEGMENT_GLSL, SEGMENT_STRIDE } from './iconSegment.js'; import { ICON_CULL_HEIGHT_M, ICON_CULL_LIFT, STYLE_STRIDE } from './iconSet.js'; import { + addressableIconSlots, iconAtlas, MAX_ICON_SLOTS, + MIN_ICON_SLOTS, NEAR_IMAGE_DEVICE_PX, } from './iconAtlas.js'; import { @@ -65,7 +67,7 @@ uniform vec3 u_cameraRight; uniform vec3 u_cameraUp; uniform vec4 u_recession; // limb distance, start ratio, strength, enabled uniform vec3 u_recessionFloors; // scale, alpha, combined alpha -uniform vec4 u_slots[${MAX_ICON_SLOTS}]; +uniform vec4 u_slots[ICON_SLOTS]; // ICON_SLOTS: as many as the context addresses uniform float u_cull; // 1: hide rows past the ellipsoid's horizon uniform float u_pickPass; // 1 while drawing for a pick @@ -143,6 +145,7 @@ void main() { : glyphPx > ${NEAR_IMAGE_DEVICE_PX.toFixed(1)}; float slot = nearImage ? style1.y : style1.x; if (slot < 0.0) return; // no image set + if (slot >= float(ICON_SLOTS)) return; // past what this context addresses vec4 rect = u_slots[int(slot)]; if (rect.z <= 0.0) return; // image not loaded yet @@ -221,6 +224,18 @@ void main() { } `; +/** + * How many atlas slots the icon shader addresses on `context`: as many as + * the vertex uniform limit leaves room for (Cesium's ContextLimits, which + * the last context created sets: the app draws in one); all of them with no + * context (a test's scene). + * @param {object} [context] A Cesium Context. + */ +export function contextIconSlots(context) { + if (!context) return MAX_ICON_SLOTS; + return addressableIconSlots(Cesium.ContextLimits.maximumVertexUniformVectors); +} + const SEGMENT_BYTES = SEGMENT_STRIDE * 4; const STYLE_BYTES = STYLE_STRIDE * 4; const EARTH_BOUNDS = new Cesium.BoundingSphere( @@ -264,6 +279,11 @@ export class IconSetPrimitive { { length: MAX_ICON_SLOTS }, () => new Cesium.Cartesian4(), ); + // The slots the shader addresses: as many as its uniforms (set with its + // program). + this._slotCount = MIN_ICON_SLOTS; + this._slotUniform = this._slots.slice(0, this._slotCount); + this._warnedSlots = false; this._horizon = new Cesium.Cartesian4(); this._cameraPosition = new Cesium.Cartesian3(); this._cameraRight = new Cesium.Cartesian3(); @@ -281,7 +301,7 @@ export class IconSetPrimitive { u_cameraUp: () => primitive._cameraUp, u_recession: () => primitive._recession, u_recessionFloors: () => primitive._recessionFloors, - u_slots: () => primitive._slots, + u_slots: () => primitive._slotUniform, u_atlas: () => primitive._texture, u_cull: () => (primitive._cull() ? 1 : 0), u_pickPass: () => primitive._pickPass, @@ -314,6 +334,12 @@ export class IconSetPrimitive { this._tau = set.seconds(renderTime); this._syncCamera(frameState); if (!this._programs) this._createCommands(context); + if (iconAtlas.size > this._slotCount && !this._warnedSlots) { + this._warnedSlots = true; + console.warn( + `[iconSetPrimitive] This context addresses ${this._slotCount} icon images; the atlas holds ${iconAtlas.size}, and rows drawing the rest draw nothing`, + ); + } for (const command of this._commands) { command.instanceCount = set.count; frameState.commandList.push(command); @@ -559,10 +585,13 @@ export class IconSetPrimitive { } _createCommands(context) { + this._slotCount = contextIconSlots(context); + this._slotUniform = this._slots.slice(0, this._slotCount); const programFor = (pass) => Cesium.ShaderProgram.fromCache({ context, vertexShaderSource: new Cesium.ShaderSource({ + defines: [`ICON_SLOTS ${this._slotCount}`], sources: [VERTEX_SHADER], }), fragmentShaderSource: new Cesium.ShaderSource({ diff --git a/src/render/cesium/gpuIcons.js b/src/render/cesium/gpuIcons.js index 58dd61828..591288985 100644 --- a/src/render/cesium/gpuIcons.js +++ b/src/render/cesium/gpuIcons.js @@ -50,7 +50,10 @@ import { writeHoldSegment, writeLerpSegment, } from '../../data/iconSegment.js'; -import { IconSetPrimitive } from '../../data/iconSetPrimitive.js'; +import { + contextIconSlots, + IconSetPrimitive, +} from '../../data/iconSetPrimitive.js'; import { iconAtlas, MAX_ICON_SLOTS } from '../../data/iconAtlas.js'; import { glideOffset, @@ -244,9 +247,11 @@ export function createGpuIcons({ if (url !== null && !iconAtlas.has(url, px)) fresh.add(`${px}|${url}`); } } - if (iconAtlas.size + fresh.size > MAX_ICON_SLOTS) + // As many as this scene's context addresses (all of them on WebGL 2). + const room = Math.min(MAX_ICON_SLOTS, contextIconSlots(scene.context)); + if (iconAtlas.size + fresh.size > room) throw new Error( - `the Cesium renderer's GPU icons have no room for ${fresh.size} more images (at most ${MAX_ICON_SLOTS} in all)`, + `the Cesium renderer's GPU icons have no room for ${fresh.size} more images (at most ${room} in all)`, ); } diff --git a/src/render/cesium/gpuIcons.test.mjs b/src/render/cesium/gpuIcons.test.mjs index 166253e15..f646002ca 100644 --- a/src/render/cesium/gpuIcons.test.mjs +++ b/src/render/cesium/gpuIcons.test.mjs @@ -4,6 +4,7 @@ import test from 'node:test'; import { createCesiumDraw } from './draw.js'; import { drawHitOf } from './drawHit.js'; import { IconSetPrimitive } from '../../data/iconSetPrimitive.js'; +import { MAX_ICON_SLOTS } from '../../data/iconAtlas.js'; import { ICON_CULL_LIFT, ICON_RECEDES, @@ -502,7 +503,7 @@ test('an icon set refuses, changing nothing, images the shared atlas has no room const { part, draw } = drawOver(); const set = draw.icons({ id: 'gpu:atlas-room' }); set.set([icon('a', { motion: { kind: 'hold' } })]); - const many = Array.from({ length: 80 }, (_, i) => + const many = Array.from({ length: MAX_ICON_SLOTS + 16 }, (_, i) => icon(`m${i}`, { image: `room-${i}.png`, motion: { kind: 'hold' } }), ); assert.throws(() => set.upsert(many), /no room for/); @@ -781,3 +782,54 @@ test('an image switch in CSS pixels stands for the aircraft layers’ 76 device // pixels, the drawn width over the scene's pixel ratio. assert.equal(rowsOf.imageSwitch(rowsOf.row('a')), 38); }); + +test('the atlas holds more than 64 images, every one addressed on WebGL 2', async () => { + const { addressableIconSlots, MIN_ICON_SLOTS } = + await import('../../data/iconAtlas.js'); + assert.ok(MAX_ICON_SLOTS > 64); + assert.equal(addressableIconSlots(256), MAX_ICON_SLOTS, 'WebGL 2’s least'); + assert.equal(addressableIconSlots(4096), MAX_ICON_SLOTS); + assert.equal(addressableIconSlots(128), MIN_ICON_SLOTS, 'WebGL 1’s least'); + assert.equal(addressableIconSlots(200), 120); + const { contextIconSlots } = await import('../../data/iconSetPrimitive.js'); + const limits = Cesium.ContextLimits; + const vectors = limits._maximumVertexUniformVectors; + try { + limits._maximumVertexUniformVectors = 4095; + assert.equal(contextIconSlots({}), MAX_ICON_SLOTS); + limits._maximumVertexUniformVectors = 128; + assert.equal(contextIconSlots({}), MIN_ICON_SLOTS); + } finally { + limits._maximumVertexUniformVectors = vectors; + } + assert.equal(contextIconSlots(undefined), MAX_ICON_SLOTS, 'no context'); + // A scene whose context addresses fewer refuses past them, as before. + const small = drawOver(); + // (No context created here: ContextLimits leave the least.) + small.scene.context = {}; + const tight = small.draw.icons({ id: 'gpu:tight-context' }); + assert.throws( + () => + tight.set( + Array.from({ length: MIN_ICON_SLOTS + 1 }, (_, i) => + icon(`t${i}`, { image: `tight-${i}.png`, motion: { kind: 'hold' } }), + ), + ), + /no room for .* \(at most 64 in all\)/, + ); + assert.equal(addressableIconSlots(0), MIN_ICON_SLOTS, 'no context yet'); + assert.equal(addressableIconSlots(NaN), MIN_ICON_SLOTS); + // A set of more images than 64 is drawn, not refused. + const { part, draw } = drawOver(); + const set = draw.icons({ id: 'gpu:many-images' }); + const items = Array.from({ length: 70 }, (_, i) => + icon(`i${i}`, { image: `many-${i}.png`, motion: { kind: 'hold' } }), + ); + set.set(items); + const { set: rowsOf } = rows(part, 'gpu:many-images'); + assert.equal(rowsOf.count, 70); + const slots = new Set( + items.map((item) => rowsOf.imageFar(rowsOf.row(item.id))), + ); + assert.equal(slots.size, 70, 'each its own slot'); +});