WebGL map: check the pictures sent to the card at most once a second

A frame that sent pictures read getError() to know whether the card took them. In every browser WebGL runs in another
process, and each read waits for it to finish everything sent so far, uploads included: a wait on the main thread on
exactly the frames that already cost the most (a pan or a zoom sends pictures on most frames, up to 321 in one).

The card keeps an error until it is read, so one read a second still finds a refusal: the pictures are now checked at
most every GL_MEM_EVERY (1 000) ms, and a picture refused in between shows at worst as a dark box until then, after
which the map goes to 2D as before. The first pictures sent into a new atlas are still checked at once, where a card
that allocates it lazily refuses. The cost could not be timed here (no Vulkan driver on this machine for Chromium to
reach the graphics card); the change bounds it whatever it is.

Tests: webgl-out-of-memory reads at most twice in two seconds of panning, and a refusal after the first check gives
the map to 2D within a second.
This commit is contained in:
Antoine 2026-11-05 00:09:59 +01:00
parent c652bce7d2
commit 32f330b1c3
No known key found for this signature in database
GPG key ID: 728FF1CE15732B05
2 changed files with 41 additions and 16 deletions

View file

@ -17,7 +17,7 @@ const sprFree=e=>{if(e.cv){e.cv.width=e.cv.height=0;e.cv=null;}};
function sprcTrim(){if(SPRC_B<=SPRC_BUDGET)return;const old=[];for(const [k,e] of SPRC)if(e.f<SPR_F)old.push([e.f,k,e]);
old.sort((a,b)=>a[0]-b[0]);const goal=SPRC_BUDGET*0.75;
for(const [,k,e] of old){if(SPRC_B<=goal)break;SPRC.delete(k);SPRC_B-=e.pw*e.ph*4;sprFree(e);}}
const GLS={prog:null,vao:null,buf:null,tex:null,size:4096,shx:0,shy:0,shh:0,frame:false,data:new Float32Array(SPR_STRIDE*2048),n:0,up:0};
const GLS={prog:null,vao:null,buf:null,tex:null,size:4096,shx:0,shy:0,shh:0,frame:false,data:new Float32Array(SPR_STRIDE*2048),n:0,up:0,upq:false,memT:-1e9};
/* One record is 13 floats: x,y,w,h (CSS px) | u0,v0,u1,v1 (atlas) | r,g,b,a | kind. A picture (kind 0) takes its
colours from the atlas and a from the record; a disc (kind 1) is centred on x,y with radius w, in colour r,g,b,a. */
const SPR_VS=GL_HEAD+`layout(location=0) in vec4 aP;layout(location=1) in vec4 aUV;layout(location=2) in vec4 aC;layout(location=3) in float aK;
@ -38,11 +38,11 @@ function glsInit(gl){GLS.prog=glProg(gl,SPR_VS,SPR_FS);const {v,b}=glVao(gl,[4,4
try{glMem(gl,'the pictures');break;}catch(e){if(GLS.size<=2048)throw e;}}
for(const k of [gl.TEXTURE_MIN_FILTER,gl.TEXTURE_MAG_FILTER])gl.texParameteri(gl.TEXTURE_2D,k,gl.LINEAR);
for(const k of [gl.TEXTURE_WRAP_S,gl.TEXTURE_WRAP_T])gl.texParameteri(gl.TEXTURE_2D,k,gl.CLAMP_TO_EDGE);
GLS.shx=GLS.shy=GLS.shh=0;}
GLS.shx=GLS.shy=GLS.shh=0;GLS.memT=-1e9;GLS.upq=false;}
// Forgets every picture and starts the atlas again, after the pictures already queued are drawn (they point into it).
function spritesReset(){glsDrawNow();for(const e of SPRC.values())sprFree(e);SPRS.clear();SPRC.clear();SPRC_B=0;GLS.shx=GLS.shy=GLS.shh=0;}
// The card given up: its pictures go (they have no canvas to be drawn from in 2D) and the atlas is forgotten.
function spritesDrop(){GLS.n=GLS.up=0;SPRS.clear();GLS.shx=GLS.shy=GLS.shh=0;}
function spritesDrop(){GLS.n=GLS.up=0;GLS.upq=false;SPRS.clear();GLS.shx=GLS.shy=GLS.shh=0;}
/* The card's pictures are painted on one canvas, reused, and each goes into the atlas as soon as it is painted: a
canvas kept per picture made the browser hold thousands of them while you zoomed, until it lost the graphics context
and the map fell back to 2D. The 2D map, and a picture too big for the atlas, keep their own canvas. */
@ -130,7 +130,13 @@ function glsDraw(gl,n){gl.useProgram(GLS.prog.p);gl.bindVertexArray(GLS.vao);gl.
gl.enable(gl.BLEND);gl.blendFunc(gl.ONE,gl.ONE_MINUS_SRC_ALPHA);gl.disable(gl.DEPTH_TEST);
gl.drawArraysInstanced(gl.TRIANGLE_STRIP,0,4,n);gl.bindVertexArray(null);}
/* The end of a frame on the card: the pictures go up over the tubes. The frame is over even if that draw fails.
A frame that sent new pictures first asks the card if it took them (once, however many): a card short of memory
may refuse one without throwing, and the picture would be kept as sent. It then gives the map to 2D (glMem() throws,
draw() draws the frame again there and glKill() forgets the card's pictures). A frame that only reuses the atlas asks nothing. */
function glsFrameEnd(){if(!GLS.frame)return;GLS.frame=false;const up=GLS.up;GLS.up=0;if(up&&GLX.gl)glMem(GLX.gl,'the pictures');glsDrawNow();}
Pictures sent are checked (did the card take them?): a card short of memory may refuse one without throwing, and the
picture would be kept as sent. It then gives the map to 2D (glMem() throws, draw() draws the frame again there and
glKill() forgets the card's pictures). Each read waits for the card to finish everything sent so far, so it is done
at most once every GL_MEM_EVERY ms: the card keeps an error until it is read, so a later read still finds it, and a
picture refused in between shows at worst as a dark box for that time. The first pictures sent into a new atlas are
checked at once (a card that allocates it lazily refuses there). A frame that only reuses the atlas asks nothing. */
const GL_MEM_EVERY=1000;
function glsFrameEnd(){if(!GLS.frame)return;GLS.frame=false;if(GLS.up)GLS.upq=true;GLS.up=0;
if(GLS.upq&&GLX.gl){const t=performance.now();if(t-GLS.memT>=GL_MEM_EVERY){GLS.memT=t;GLS.upq=false;glMem(GLX.gl,'the pictures');}}
glsDrawNow();}

View file

@ -1,8 +1,8 @@
// A graphics card short of memory may refuse a texture or a buffer without throwing: the call only leaves
// OUT_OF_MEMORY for getError(), and the atlas then drew every picture as a black box while the map stayed on the card.
// The big uploads now read it (glMem(), src/game/96-map/56-webgl.js): the atlas tries 2048 px before giving up, and
// any other refusal gives the card up for 2D in the same frame. getError() is never read on an ordinary frame (each
// read waits for the card).
// any other refusal gives the card up for 2D in the same frame, or for a picture within a second. getError() is never
// read on an ordinary frame, and at most once a second while pictures are sent (each read waits for the card).
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
@ -80,13 +80,17 @@ test('an ordinary frame never asks the card for its errors', () => {
ev('__T+=16;draw(__T);');
assert.equal(ev('GLX.dead'), false);
assert.ok(ev('__GE') > 0, 'the first frame checked its uploads');
// Panning brings new cities into view, whose pictures are sent: only those frames ask, once each.
const pan = JSON.parse(ev(`(()=>{let quiet=0,asked=0,sent=0,most=0;for(let i=0;i<30;i++){const s=__SUB,g=__GE;cam.x+=3;__T+=16;draw(__T);
if(__SUB===s){quiet++;if(__GE>g)asked++;}else{sent++;most=Math.max(most,__GE-g);}}return JSON.stringify({quiet,asked,sent,most});})()`));
assert.ok(pan.quiet > 0, 'some frames send no picture');
assert.equal(pan.asked, 0, 'a frame that sends nothing big reads nothing');
assert.ok(pan.most <= 4, 'a frame that sends pictures asks once (getError() read up to four flags), not once per picture');
ev('__GE=0;for(let i=0;i<10;i++){__T+=16;draw(__T);}');
// Panning brings new cities into view, whose pictures are sent: they are checked at most once a second (each read
// waits for the card), the first time a second after the first frame's check.
const pan = JSON.parse(ev(`(()=>{let sent=0,reads=0,most=0,at=[];for(let i=0;i<120;i++){const s=__SUB,g=__GE;cam.x+=3;__T+=16;draw(__T);
if(__SUB>s)sent++;if(__GE>g){reads++;at.push(i);}most=Math.max(most,__GE-g);}return JSON.stringify({sent,reads,most,at});})()`));
assert.ok(pan.sent > 5, 'frames send pictures');
assert.ok(pan.reads >= 1 && pan.reads <= 2, `about two seconds of panning read the errors once or twice, not once per frame that sends (${JSON.stringify(pan)})`);
assert.ok(pan.most <= 4, 'one check reads at most four flags');
// The pan's last pictures are still to be checked: once, on the next frame a second on. Then a still map reads nothing.
ev('__GE=0;__T+=1100;draw(__T);');
assert.ok(ev('__GE') <= 4, 'the pictures left over are checked once');
ev('__GE=0;for(let i=0;i<10;i++){__T+=1100;draw(__T);}');
assert.equal(ev('__GE'), 0, 'a still map reads nothing');
assert.deepEqual(errors, []);
});
@ -109,6 +113,21 @@ test('a picture the card refuses without throwing gives the map to 2D in the sam
assert.deepEqual(errors, []);
});
// The card keeps an error until it is read: a picture refused between two checks is found by the next one.
test('a picture refused after the first check gives the map to 2D within a second', () => {
const { ev, errors } = open({});
assert.equal(ev('glInit()'), true);
ev('UI.tickErr=false;const h=CITY[S.home];cam.x=h.x;cam.y=h.y;cam.z=1.4;__T+=16;draw(__T);');
assert.equal(ev('GLX.dead'), false);
ev('__FAIL.sprites=true;');
const n = ev('(()=>{let i=0;for(;i<120&&!GLX.dead;i++){cam.x+=5;__T+=16;draw(__T);}return i;})()');
assert.equal(ev('GLX.dead'), true, 'given up');
assert.ok(n * 16 <= 1000 + 32, `within a second (${n} frames)`);
assert.equal(ev('SPRS.size'), 0, 'no picture is kept as sent');
assert.equal(ev('!!UI.tickErr'), false, 'no glitch for the player');
assert.deepEqual(errors, []);
});
/* Given up for good while the context still lives (memory refused), the card kept the atlas (64 MB at 4096 px) and the
rest while the 2D map began painting its own pictures. glKill() now deletes the biggest and lets the context go. */
test('giving the card up for good frees what it holds and lets the context go, and the frame is drawn in 2D', () => {