diff --git a/src/game/96-map/59-gl-sprites.js b/src/game/96-map/59-gl-sprites.js index 615c48e..ad1d91e 100644 --- a/src/game/96-map/59-gl-sprites.js +++ b/src/game/96-map/59-gl-sprites.js @@ -5,6 +5,18 @@ const SPR_MAX=1400,SPR_STRIDE=13; // The card's pictures (SPRS) and the 2D map's (SPRC), by key; both at the screen's pixel density SPR_DPR. const SPRS=new Map(),SPRC=new Map();let SPR_DPR=0; +/* The 2D map's pictures each keep a canvas, so they are also held to a size: a long zoom on a big network paints every + city again at each step, and the list (only capped at SPR_MAX) held up to 180 MB of them where a frame shows 2 to + 23 MB. At the end of a frame over SPRC_BUDGET bytes, the pictures it did not use go, those unused the longest first, + down to 3/4 of the budget (so this happens once in a while, not every frame). The frame's own are always kept: a + screen that needs more than the budget keeps what it needs. A canvas let go is emptied (0×0) at once, which frees + its pixels without waiting for the collector. The card's atlas has its own fixed size (GLS.size). + SPR_F counts frames (draw()); finding or drawing a picture marks it as used in this one (e.f). */ +const SPRC_BUDGET=48*1048576;let SPRC_B=0,SPR_F=0; +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.fa[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}; /* 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. */ @@ -28,7 +40,7 @@ function glsInit(gl){GLS.prog=glProg(gl,SPR_VS,SPR_FS);const {v,b}=glVao(gl,[4,4 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;} // Forgets every picture and starts the atlas again, after the pictures already queued are drawn (they point into it). -function spritesReset(){glsDrawNow();SPRS.clear();SPRC.clear();GLS.shx=GLS.shy=GLS.shh=0;} +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;} /* The card's pictures are painted on one canvas, reused, and each goes into the atlas as soon as it is painted: a @@ -41,20 +53,21 @@ function glsPad(pw,ph){const P=GLS_PAD;if(!P.cv){P.cv=document.createElement('ca // Where this frame's pictures are kept: the card's or the 2D map's. A new pixel density starts both again. function sprMap(){if(DPR!==SPR_DPR){SPR_DPR=DPR;spritesReset();}return GLS.frame&&!!GLX.gl&&!!GLS.prog?SPRS:SPRC;} // The picture already painted under key, if any: callers ask first, so a picture drawn every frame costs no closure. -const spriteHas=key=>sprMap().get(key); +const spriteHas=key=>{const e=sprMap().get(key);if(e)e.f=SPR_F;return e;}; // A picture of w×h CSS px with its anchor at (ax,ay): painted once by paint(g) (the 2D canvas, at the screen's pixel // density), and kept by key in SPRS (the card's) or SPRC (the 2D map's). -function spriteEntry(key,w,h,ax,ay,paint){const M=sprMap(),gpu=M===SPRS;let e=M.get(key);if(e)return e; +function spriteEntry(key,w,h,ax,ay,paint){const M=sprMap(),gpu=M===SPRS;let e=M.get(key);if(e){e.f=SPR_F;return e;} if(SPRS.size+SPRC.size>=SPR_MAX)spritesReset(); const pw=Math.ceil(w*DPR),ph=Math.ceil(h*DPR),saved=ctx; - e={cv:null,pw,ph,w,h,ax,ay,up:false,u0:0,v0:0,u1:1,v1:1}; + e={cv:null,pw,ph,w,h,ax,ay,up:false,u0:0,v0:0,u1:1,v1:1,f:SPR_F}; if(gpu&&pw<=GLS.size&&ph<=GLS.size){const P=glsPad(pw,ph),g=P.g; g.save();g.setTransform(1,0,0,1,0,0);g.clearRect(0,0,pw,ph);ctx=g; try{g.setTransform(DPR,0,0,DPR,0,0);sprPen(g);paint(g);}finally{ctx=saved;g.restore();} // A full atlas starts again (glsPut), and the picture goes into the empty one at once: the pad is reused next. try{if(!glsPut(GLX.gl,e,P.cv))glsPut(GLX.gl,e,P.cv);}catch(x){throw glErr(x);} }else{const cv=document.createElement('canvas');cv.width=pw;cv.height=ph;const g=cv.getContext('2d');ctx=g; - try{g.setTransform(DPR,0,0,DPR,0,0);sprPen(g);paint(g);}finally{ctx=saved;}e.cv=cv;} + try{g.setTransform(DPR,0,0,DPR,0,0);sprPen(g);paint(g);}finally{ctx=saved;}e.cv=cv; + if(M===SPRC)SPRC_B+=pw*ph*4;} M.set(key,e);return e;} /* A picture starts with the pen the map's own canvas has when it draws cities and markers: the tubes leave it with round line ends and corners, the labels with text centred on its middle. Drawn straight on the map, the dashed rings, diff --git a/src/game/96-map/60-draw.js b/src/game/96-map/60-draw.js index eea050b..6709e30 100644 --- a/src/game/96-map/60-draw.js +++ b/src/game/96-map/60-draw.js @@ -1,8 +1,8 @@ /* A frame. An error from the card itself (glErr()) gives WebGL up for this session and draws the same frame again in 2D, once; any other error goes on to the loop's tickErr(). Either way the 2D context and the sprite queue are reset. */ -function draw(now){for(let pass=0;;pass++){let err=null;DFC=new Map(); +function draw(now){for(let pass=0;;pass++){let err=null;DFC=new Map();SPR_F++; try{drawFrame(now);}catch(e){err=e;} - try{glsFrameEnd();}catch(e){if(!err)err=e;}finally{DFC=null;GLS.frame=false;GLS.n=0;ctx=CTX0;} + try{glsFrameEnd();}catch(e){if(!err)err=e;}finally{DFC=null;GLS.frame=false;GLS.n=0;ctx=CTX0;sprcTrim();} if(!err)return;if(!err.glErr||pass)throw err;glKill(err);}} // Rectangles on a 64 px grid: a new one is compared only with the few that share its cells (a plain list made the // label placement quadratic in the number of cities). diff --git a/tests/map-2d-sprite-budget.test.js b/tests/map-2d-sprite-budget.test.js new file mode 100644 index 0000000..217508f --- /dev/null +++ b/tests/map-2d-sprite-budget.test.js @@ -0,0 +1,68 @@ +// The 2D map keeps a canvas per picture (59-gl-sprites.js). A long zoom on a big network paints every city again at +// each step, and the list, capped only by its length, held up to 180 MB of pictures where a frame shows at most 23 MB. +// It is now held to SPRC_BUDGET bytes: past it, the pictures no frame used since the one before are let go, and those +// in use are always kept. The page's clock is set by hand, so nothing depends on how fast the test runs. +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const { load } = require('./harness'); + +const SAVE = fs.readFileSync(path.join(__dirname, 'fixtures', 'big-network.json'), 'utf8'); + +// The big network on a 1280×800 screen at 2× in 2D. Canvases are counted as they are made; their contexts take any call. +function open() { + const h = load({ seed: 777 }); + h.ev(`S=migrate(JSON.parse(${JSON.stringify(SAVE)}));UI.started=true;UI.pitched=true;UI.speed=0;closeModal();S.nextFail=S.day+999;S.nextEvent=S.day+999;compute(); + window.__T=100000;Object.defineProperty(performance,'now',{value:()=>__T,configurable:true});mapGl(false,false); + DPR=2;CW=1280;CH=800;cam.x=CITY.fra.x;cam.y=CITY.fra.y;cam.z=0.6;window.__cv=0; + const ce=document.createElement.bind(document);document.createElement=function(t){const e=ce(t);if(String(t).toLowerCase()==='canvas')__cv++;return e;}; + HTMLCanvasElement.prototype.getContext=function(){return this.__c||(this.__c=new Proxy({},{get(t,k){if(k in t)return t[k]; + if(k==='measureText')return s=>({width:String(s).length*7});if(k==='createLinearGradient')return ()=>({addColorStop(){}});return ()=>{};},set(t,k,v){t[k]=v;return true;}}));}; + window.__bytes=()=>{let b=0;for(const e of SPRC.values())b+=e.pw*e.ph*4;return b;}; + // What the last frame drew: its pictures and their bytes. + window.__used=()=>{let n=0,b=0;for(const e of SPRC.values())if(e.f===SPR_F){n++;b+=e.pw*e.ph*4;}return {n,b};};`); + return h; +} +// A pinch from 0.6 to 3.2 and back in 180 frames of 16 ms: the most bytes held, and the most a frame used. +const PINCH = `(()=>{let held=0,used=0,drift=0;for(let i=0;i<=180;i++){const f=i<=90?i/90:(180-i)/90;cam.z=0.6*Math.pow(3.2/0.6,f);__T+=16;draw(__T); + const b=__bytes();held=Math.max(held,b);used=Math.max(used,__used().b);drift=Math.max(drift,Math.abs(b-SPRC_B));}return JSON.stringify({held,used,drift});})()`; + +test('a long zoom on the 2D map holds its pictures to the budget, plus what one frame needs', () => { + const { ev, errors } = open(); + const r = JSON.parse(ev(PINCH)); + const MB = 1048576, budget = ev('SPRC_BUDGET'); + assert.equal(r.drift, 0, 'the bytes counted are the bytes held'); + assert.ok(r.used > 5 * MB, `a frame uses a real share of it (${(r.used / MB).toFixed(1)} MB)`); + // After a frame, over the budget only if that frame alone needs more. + assert.ok(r.held <= Math.max(budget, r.used), `held ${(r.held / MB).toFixed(1)} MB, budget ${budget / MB} MB`); + assert.ok(r.held < 80 * MB, `far under the 180 MB it held before (${(r.held / MB).toFixed(1)} MB)`); + assert.deepEqual(errors, []); +}); + +test('the pictures a frame uses are never let go, and a still map paints nothing new', () => { + const { ev, errors } = open(); + ev(PINCH); + // Every picture the last frame drew is still kept (it was used, so it cannot have been let go). + ev('window.__keep=[];const b0=blit;blit=function(e,...a){__keep.push(e);return b0(e,...a);};__T+=16;draw(__T);blit=b0;'); + assert.ok(ev('__keep.length') > 50); + assert.equal(ev('__keep.every(e=>[...SPRC.values()].includes(e))'), true, 'every picture of the frame is kept'); + // The same view again and again: everything comes from the kept pictures. + ev('window.__c0=__cv;for(let i=0;i<5;i++){__T+=16;draw(__T);}'); + assert.equal(ev('__cv-__c0'), 0, 'no picture painted again'); + assert.deepEqual(errors, []); +}); + +test('under the budget nothing is let go', () => { + const { ev, errors } = open(); + ev('cam.z=1.1;__T+=16;draw(__T);window.__n=SPRC.size;window.__b=SPRC_B;'); + assert.ok(ev('__b') < ev('SPRC_BUDGET')); + // A small pan: new pictures come in, none goes. + ev('for(let i=0;i<10;i++){cam.x+=4;__T+=16;draw(__T);}'); + assert.ok(ev('SPRC.size') >= ev('__n'), 'the list only grew'); + assert.equal(ev('SPRC_B'), ev('__bytes()')); + // A reset (a new pixel density) forgets the bytes with the pictures. + ev('DPR=1;__T+=16;draw(__T);'); + assert.equal(ev('SPRC_B'), ev('__bytes()')); + assert.deepEqual(errors, []); +});