WebGL map: keep the 2D map where WebGL is drawn by the processor

With the perf benchmark now waiting for the surface the map was drawn on,
headless Chromium showed what WebGL costs without a graphics card: its
SwiftShader renderer spends about 8 µs per instance, so the coastlines alone
(29k segments, twice) took about 480 ms and a big map 1.4 s a frame, where the
2D canvas takes 46 ms. Players get the same with a blocked driver, a virtual
machine or Mesa's llvmpipe. glInit() now reads the renderer first and keeps
the 2D map on SwiftShader, llvmpipe, softpipe, lavapipe, WARP or any
"software" renderer; GLX.soft lets the GPU benchmark measure them anyway.
This commit is contained in:
Antoine 2026-11-04 14:45:48 +01:00
parent 1e9b1d458b
commit 39941c257e
No known key found for this signature in database
GPG key ID: 728FF1CE15732B05
4 changed files with 35 additions and 3 deletions

View file

@ -9,7 +9,7 @@ All notable changes to this project are documented here. The project follows
- The NOC charts have tooltips, on the dashboard and on the wallboard in the NOC window: hover (or touch) a chart to see every series on that day, with a total. Click an entry in the legend to hide or show that series, and the scale follows what is left. The "how it arrives" bars on the Traffic tab show the same kind of tooltip, and its legend picks which ways in (caches, PNIs, IX peering, transit) the table counts.
- Bilbao joins the map, where the transatlantic cables really come ashore: Marea Carey, Grace Hopscotch and Sol Survivor now land there instead of Madrid. It has no exchange of its own, so traffic heads inland to Madrid, and Drizzle Electric, Tangerine Telecom and the big carriers sell transit there. A link you already run on the old Madrid landings keeps working as a plain subsea link, and the Mid-Atlantic Accent scenario now goes Azores, Bilbao, Madrid, and Marea Carey now leaves the American coast at Virginia Beach instead of crossing the northeast to New Jersey. Columbus III-and-a-half now comes ashore near Cádiz, where it really landed, on its way to Madrid.
- Puzzles are easier to read, above all on phones: the three stars are now three goals in plain words, and the one you are working on says what is wrong and what to do, with a Show me button. The map marks it too: a red ring around a city that still needs traffic, and a pulsing ✂ on a link whose cut would hurt. On phones the sheet starts small so the map gets the screen; swipe it up for the rest. Cities far from the puzzle are left off the map, and stepping stones you are not using are small dots without a name until you tap one, build there or zoom in.
- The map is now drawn by the graphics card wherever the browser can, so panning, zooming and fast play stay smooth on big networks and big screens. The map looks the same; Settings › Map drawing › Always 2D keeps the old drawing, and browsers without WebGL2 keep it on their own, as does a game whose graphics card fails while you play.
- The map is now drawn by the graphics card wherever the browser can, so panning, zooming and fast play stay smooth on big networks and big screens. The map looks the same; Settings › Map drawing › Always 2D keeps the old drawing, and browsers without WebGL2 or without a usable graphics card keep it on their own, as does a game whose graphics card fails while you play.
- Cities, labels, building cones, ships, offer bubbles and money pop-ups are painted once and then reused, so the usual map also draws far fewer shapes each frame.
## [2.9.1] — 2026-11-04

View file

@ -139,6 +139,8 @@
addEventListener('load', async () => {
document.body.appendChild(box);
while (typeof migrate !== 'function' || typeof GLX === 'undefined') await sleep(50);
// A software renderer is measured too here (the game itself keeps the 2D map on one); the table names the renderer.
GLX.soft = true;
// ?dpr=2 draws the map as a screen of that density would (a 4K screen's pixels, measured on a smaller one).
const DPRQ = +QS.get('dpr'); if (DPRQ > 0) { Object.defineProperty(window, 'devicePixelRatio', { get: () => DPRQ }); resize(); WL.key = null; }
await sleep(800);

View file

@ -5,9 +5,13 @@
Two canvases sit over #map, which stays empty: #mapgl, where the card draws the background (57), the tubes and
packets (58) and the pictures of cities, labels and markers (59); and #mapfx, a transparent 2D canvas over it for
the few things still painted shape by shape (the drag preview, the chaos monkey), cleared only after it was used.
Puzzles, and any browser without WebGL2 or whose context fails or is lost, keep the 2D map. */
Puzzles, and any browser without WebGL2 or whose context fails or is lost, keep the 2D map. So does WebGL drawn by
the processor (no usable graphics card: a blocked driver, a virtual machine): there every instance of a line costs
microseconds, and a big map took a second and a half a frame where the 2D canvas takes 50 ms. */
const GLX={want:true,dead:false,gl:null,c:null,f:null,fx:null,key:[],nSeg:0,nPk:0,nHalo:0,shown:false,gpu:''};
try{GLX.want=localStorage.getItem('sot-map')!=='2d';}catch(e){}
// Renderers that are the processor pretending to be a graphics card (GLX.soft: use them anyway, for the GPU benchmark).
const GL_SOFT=/swiftshader|llvmpipe|softpipe|lavapipe|software|basic render/i;
const glOn=()=>GLX.want&&!GLX.dead&&!!S&&!pzOn()&&glInit();
// Switches the WebGL map on or off: the setting keeps the choice in this browser (keep), the GPU benchmark does not.
function mapGl(on,keep=true){GLX.want=!!on;if(keep)try{on?localStorage.removeItem('sot-map'):localStorage.setItem('sot-map','2d');}catch(e){}
@ -33,11 +37,12 @@ function glInit(){if(GLX.gl)return true;if(GLX.dead)return false;
try{const c=document.getElementById('mapgl'),f=document.getElementById('mapfx');if(!c||!f||!c.getContext)throw new Error('no map layers');
const gl=c.getContext('webgl2',{alpha:true,premultipliedAlpha:true,antialias:false,depth:true,stencil:true,powerPreference:'high-performance'});
if(!gl||typeof gl.createShader!=='function')throw new Error('no WebGL2');
try{const x=gl.getExtension('WEBGL_debug_renderer_info');GLX.gpu=String(x?gl.getParameter(x.UNMASKED_RENDERER_WEBGL):gl.getParameter(gl.RENDERER));}catch(e){}
if(!GLX.soft&&GL_SOFT.test(GLX.gpu))throw new Error('no graphics card, only '+GLX.gpu);
const P=(vs,fs)=>glProg(gl,vs,fs),progs={line:P(GL_LINE_VS,GL_LINE_FS),pk:P(GL_PK_VS,GL_PK_FS),fill:P(FILL_VS,FILL_FS),fan:P(FAN_VS,FAN_FS),ell:P(ELL_VS,ELL_FS),circ:P(CIRC_VS,CIRC_FS)};
// The tubes' points for the packets: floats read with texelFetch, never filtered.
const tex=gl.createTexture();gl.bindTexture(gl.TEXTURE_2D,tex);for(const k of [gl.TEXTURE_MIN_FILTER,gl.TEXTURE_MAG_FILTER])gl.texParameteri(gl.TEXTURE_2D,k,gl.NEAREST);
c.addEventListener('webglcontextlost',e=>{e.preventDefault();glKill('the graphics context was lost');UI.drawnAt=0;});
try{const x=gl.getExtension('WEBGL_debug_renderer_info');GLX.gpu=String(x?gl.getParameter(x.UNMASKED_RENDERER_WEBGL):gl.getParameter(gl.RENDERER));}catch(e){}
glsInit(gl);
// The overlay is cleared only after something was painted on it: clearing a whole screen of empty canvas every
// frame makes the browser send the layer to the screen again, which on a 4K screen cost more than the map itself.

View file

@ -305,3 +305,28 @@ test('selecting a tube sends its halo to the card, not the network again', () =>
assert.equal(r.haloDraw, 0, 'and the halo is gone');
assert.deepEqual(errors, []);
});
// A WebGL2 context that compiles everything and names its renderer: enough for glInit() to run to the end.
const FAKE_GL = (name) => `(()=>{window.CanvasRenderingContext2D=window.CanvasRenderingContext2D||class{};const c=document.getElementById('mapgl');c.getContext=(t)=>t!=='webgl2'?null:new Proxy({},{get(t,k){
if(typeof k==='string'&&/^[A-Z_0-9]+$/.test(k))return k;
if(k==='getExtension')return ()=>({UNMASKED_RENDERER_WEBGL:'REN'});
if(k==='getParameter')return (p)=>p==='REN'?${JSON.stringify(name)}:p==='ACTIVE_UNIFORMS'?0:4096;
if(k==='getShaderParameter'||k==='getProgramParameter')return (x,p)=>p==='ACTIVE_UNIFORMS'?0:true;
return ()=>({});}});})()`;
test('a graphics card drawn by the processor keeps the 2D map; a real one gets the card', () => {
for (const [name, gl] of [['ANGLE (Google, Vulkan 1.3.0 (SwiftShader Device (Subzero) (0x0000C0DE)), SwiftShader driver)', false],
['llvmpipe (LLVM 17.0.6, 256 bits)', false], ['Microsoft Basic Render Driver', false],
['ANGLE (NVIDIA, NVIDIA GeForce RTX 3060 Direct3D11 vs_5_0 ps_5_0, D3D11)', true], ['Apple M2', true], ['Mali-G78', true]]) {
const { ev } = open();
ev(FAKE_GL(name));
ev('console.warn=()=>{}');
assert.equal(ev('glInit()'), gl, name);
assert.equal(ev('GLX.dead'), !gl, name);
assert.equal(ev('GLX.gpu'), name, 'the renderer is still named');
}
const { ev } = open();
ev(FAKE_GL('SwiftShader driver'));
ev('GLX.soft=true');
assert.equal(ev('glInit()'), true, 'the GPU benchmark can still measure it');
});