WebGL map: keep the pictures' keys, the waves and the clouds out of the garbage
A frame joined a key for every city, label, quiet dot and stamp it drew, built a closure to paint each even when the picture was already there, concatenated a prefix onto every key, made an array per city per copy of the map for its place on screen, and rebuilt the waves' and the clouds' arrays. Keys are now kept per city (packed into one number for the yes/no parts), per label text and look, per stamp and scale, and joined again only when the picture changes; pictures already painted are found before any closure is made; the card's and the 2D map's pictures are two maps, so keys need no prefix; the cities' places and the POP lights are computed in place; the waves and the clouds are written into arrays kept from frame to frame. Sampled heap profile, TTL save, RTX 4060 Ti: allocations under drawFrame 68 -> 37 MB/s at x1 and 62 -> 43 MB/s during a pan/zoom sweep; under glBegin 5.4 -> 0.5 MB/s.
This commit is contained in:
parent
3482142ae5
commit
87b153db6f
8 changed files with 153 additions and 37 deletions
89
tests/webgl-frame-garbage.test.js
Normal file
89
tests/webgl-frame-garbage.test.js
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
// A frame on the card makes little garbage (src/game/96-map/57-webgl-background.js, 59-gl-sprites.js, 80-cities.js):
|
||||
// the keys of the pictures are kept per city and per label and built again only when the picture changes, and the
|
||||
// waves and the clouds are written into arrays kept from frame to frame. What is drawn stays the same.
|
||||
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');
|
||||
|
||||
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();`);
|
||||
return h;
|
||||
}
|
||||
|
||||
const STUB = `(()=>{const calls=[];const gl=new Proxy({calls},{get(t,k){if(k in t)return t[k];if(typeof k==='string'&&/^[A-Z_0-9]+$/.test(k))return k;
|
||||
return (...a)=>{calls.push([k,...a]);return {};};}});
|
||||
const c=document.getElementById('mapgl'),f=document.getElementById('mapfx');
|
||||
const P=()=>({p:{},u:{}}),B=()=>({v:{},b:{}});
|
||||
Object.assign(GLX,{want:true,dead:false,gl,c,f,fx:f.getContext('2d'),line:P(),pk:P(),fill:P(),fan:P(),ell:P(),land:{},circ:P(),tex:{},lv:B(),hv:B(),pv:B(),cb:B(),wv:B(),cl:B(),empty:{},key:[],gpu:'stub'});
|
||||
GLS.prog=P();GLS.vao={};GLS.buf={};GLS.tex={};GLS.size=4096;SPRS.clear();GLS.shx=GLS.shy=GLS.shh=0;return gl;})()`;
|
||||
|
||||
// Counts the keys joined during one frame (Array#join with '|', as the picture keys are), but the ships' (a handful).
|
||||
const JOINS = (body) => `(()=>{let n=0;const j=Array.prototype.join;Array.prototype.join=function(s){if(s==='|'&&this[0]!=='ship')n++;return j.apply(this,arguments);};
|
||||
try{${body}}finally{Array.prototype.join=j;}return n;})()`;
|
||||
|
||||
for (const [view, z] of [['near', 1.4], ['far', 0.6]]) {
|
||||
test(`an unchanged frame on the card joins no picture key again (${view} view)`, () => {
|
||||
const { ev, errors } = open();
|
||||
ev(STUB);
|
||||
ev(`const h=CITY[S.home];cam.x=h.x;cam.y=h.y;cam.z=${z};UI.sel=null;draw(1000);draw(1016);`);
|
||||
const first = ev(JOINS(`SPRS.clear();CKEY.clear();PILLK.clear();FKEY.clear();QKEY.clear();draw(1032);`));
|
||||
const again = ev(JOINS('draw(1048);'));
|
||||
assert.ok(first > 20, `a first frame builds its keys (${first})`);
|
||||
assert.equal(again, 0, 'the next one builds none');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
}
|
||||
|
||||
test('a city whose state changes gets a new picture, and its old one back after', () => {
|
||||
const { ev, errors } = open();
|
||||
ev(STUB);
|
||||
const r = JSON.parse(ev(`(()=>{const id=S.home,C=CITY[id],p=S.pops[id];cam.x=C.x;cam.y=C.y;cam.z=1.4;UI.sel=null;
|
||||
const pic=()=>{draw(1000);return CKEY.get(id).k;};const a=pic();const was=p.down;p.down=S.day+3;const b=pic();p.down=was;const c=pic();
|
||||
UI.sel={type:'city',id};const d=pic();UI.sel=null;
|
||||
const before=CKEY.get(id).k,gen=p.gen;p.gen=!gen;const e=pic();p.gen=gen;
|
||||
return JSON.stringify({a,b,c,d,e,before});})()`));
|
||||
assert.notEqual(r.b, r.a, 'down: another picture');
|
||||
assert.equal(r.c, r.a, 'back up: the same picture');
|
||||
assert.notEqual(r.d, r.a, 'selected: another picture');
|
||||
assert.notEqual(r.e, r.before, 'a generator: another picture');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('labels with the same text but another look are other pictures', () => {
|
||||
const { ev, errors } = open();
|
||||
ev(STUB);
|
||||
const r = JSON.parse(ev(`(()=>{GLS.frame=true;GLS.n=0;ctx=GLX.fx;const f='800 12px "Baloo 2", sans-serif';
|
||||
pill('Lagos',f,50,19,9,'#FFFFFF','#1E2640','#1E2640',2,10,10,.5);pill('Lagos',f,50,19,9,'#FF4F9A','#FFFFFF','#1E2640',2,10,10,.5);pill('Lagos',f,50,19,9,'#FFFFFF','#1E2640','#1E2640',2,10,10,.5);
|
||||
const n=GLS.n,L=PILLK.get('Lagos').length,u=new Set([0,1,2].map(i=>GLS.data[i*SPR_STRIDE+4]+','+GLS.data[i*SPR_STRIDE+5])).size;GLS.frame=false;GLS.n=0;ctx=CTX0;
|
||||
return JSON.stringify({n,L,u});})()`));
|
||||
assert.equal(r.n, 3);
|
||||
assert.equal(r.L, 2, 'two looks kept for one text');
|
||||
assert.equal(r.u, 2, 'two pictures in the atlas, the third label reuses the first');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('the waves and the clouds are what they were, written into arrays kept from frame to frame', () => {
|
||||
const { ev, errors } = open();
|
||||
ev(STUB);
|
||||
const r = JSON.parse(ev(`(()=>{const ks=[-1,0,1],now=12345,gl=GLX.gl;
|
||||
glWaves(gl,now,ks);const wa=GLX.wa,ph0=now/900,want=[];let m=0,bad=0;
|
||||
for(const [x,y,ph] of WAVES){const o=Math.sin(ph0+ph*6)*3;for(const [x0,cx,cy,x1] of [[x-9+o,x-4.5+o,y-4,x+o],[x+o,x+4.5+o,y+4,x+9+o]]){let px=x0,py=y;
|
||||
for(let j=1;j<=4;j++){const t=j/4,u=1-t,qx=u*u*x0+2*u*t*cx+t*t*x1,qy=u*u*y+2*u*t*cy+t*t*y,o2=m*LINE_STRIDE;
|
||||
if(wa[o2]!==Math.fround(px)||wa[o2+1]!==Math.fround(py)||wa[o2+2]!==Math.fround(qx)||wa[o2+3]!==Math.fround(qy))bad++;m++;px=qx;py=qy;}}}
|
||||
glWaves(gl,now+16,ks);const sameW=GLX.wa===wa;
|
||||
glClouds(gl,now,ks);const ca=GLX.ca,A=[];for(const k of ks)for(const c of CLOUDS){const x=((c.x+now/1000*c.v)%(W+200))-100+k*W,y=c.y;A.push(x,y,16*c.s,0, x+18*c.s,y-8*c.s,20*c.s,0, x+40*c.s,y,15*c.s,0);}
|
||||
const sent=gl.calls.filter(c=>c[0]==='bufferData').pop()[2],cbad=A.filter((v,i)=>sent[i]!==Math.fround(v)).length;
|
||||
glClouds(gl,now+16,ks);
|
||||
return JSON.stringify({bad,m,sameW,cbad,clen:sent.length,alen:A.length,sameC:GLX.ca===ca});})()`));
|
||||
assert.equal(r.bad, 0, `every wave segment where it was (${r.m} segments)`);
|
||||
assert.equal(r.sameW, true);
|
||||
assert.equal(r.clen, r.alen, 'one record per puff');
|
||||
assert.equal(r.cbad, 0, 'every puff where it was');
|
||||
assert.equal(r.sameC, true, 'the clouds’ array is kept');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
|
@ -35,7 +35,7 @@ test('on the card, pictures are painted on one canvas and each goes into the atl
|
|||
const r = JSON.parse(ev(`(()=>{const subs=GLX.gl.calls.filter(c=>c[0]==='texSubImage2D'),es=[...SPRS.values()];
|
||||
return JSON.stringify({canvases:__canvases,subs:subs.length,sprites:es.length,mine:es.filter(e=>e.cv).length,
|
||||
allUp:es.every(e=>e.up),rect:subs.every(c=>c.length===10&&c[9]===GLS_PAD.cv&&c[5]>0&&c[6]>0),
|
||||
fit:subs.every(c=>c[5]<=GLS_PAD.cv.width&&c[6]<=GLS_PAD.cv.height),keys:es.length?[...SPRS.keys()].every(k=>k[0]==='g'):false});})()`));
|
||||
fit:subs.every(c=>c[5]<=GLS_PAD.cv.width&&c[6]<=GLS_PAD.cv.height),keys:es.length>0&&SPRC.size===0});})()`));
|
||||
assert.ok(r.sprites > 50, `a zoom paints many pictures (${r.sprites})`);
|
||||
assert.ok(r.canvases <= 1, `one canvas for all of them, not one each (${r.canvases} made)`);
|
||||
assert.equal(r.mine, 0, 'no picture keeps a canvas of its own');
|
||||
|
|
@ -53,7 +53,7 @@ test('a full atlas starts again and the picture that did not fit goes into the n
|
|||
const r = JSON.parse(ev(`(()=>{GLS.frame=true;GLS.n=0;ctx=GLX.fx;GLS.shy=GLS.size-4;GLS.shx=GLS.size-2;GLS.shh=4;
|
||||
const before=SPRS.size;let draws=0;const gd=glsDraw;glsDraw=function(){draws++;};
|
||||
const e=spriteEntry('big',40,40,20,20,g=>{g.fillRect(0,0,40,40);});blit(e,10,10,40,40);
|
||||
glsDraw=gd;const r={up:e.up,u0:e.u0,v0:e.v0,kept:SPRS.get('g'+DPR+'|big')===e,size:SPRS.size,n:GLS.n,cv:!!e.cv,shy:GLS.shy};GLS.frame=false;GLS.n=0;ctx=CTX0;return JSON.stringify(r);})()`));
|
||||
glsDraw=gd;const r={up:e.up,u0:e.u0,v0:e.v0,kept:SPRS.get('big')===e,size:SPRS.size,n:GLS.n,cv:!!e.cv,shy:GLS.shy};GLS.frame=false;GLS.n=0;ctx=CTX0;return JSON.stringify(r);})()`));
|
||||
assert.equal(r.up, true, 'the picture is in the atlas');
|
||||
assert.equal(r.u0, 0); assert.equal(r.v0, 0);
|
||||
assert.equal(r.kept, true, 'and kept under its key after the restart');
|
||||
|
|
@ -66,7 +66,7 @@ test('a full atlas starts again and the picture that did not fit goes into the n
|
|||
test('the 2D map keeps a canvas per picture, and its pictures are not the card’s', () => {
|
||||
const { ev, errors } = open();
|
||||
ev(`mapGl(false);const h=CITY[S.home];cam.x=h.x;cam.y=h.y;cam.z=1.4;draw(1000);`);
|
||||
const r = JSON.parse(ev(`(()=>{const es=[...SPRS.entries()];return JSON.stringify({n:es.length,cv:es.every(([k,e])=>k[0]==='c'&&e.cv&&e.cv.width===e.pw&&e.cv.height===e.ph),up:es.some(([,e])=>e.up)});})()`));
|
||||
const r = JSON.parse(ev(`(()=>{const es=[...SPRC.entries()];return JSON.stringify({n:es.length,cv:SPRS.size===0&&es.every(([k,e])=>e.cv&&e.cv.width===e.pw&&e.cv.height===e.ph),up:es.some(([,e])=>e.up)});})()`));
|
||||
assert.ok(r.n > 10);
|
||||
assert.equal(r.cv, true, 'each with its own canvas, at its size');
|
||||
assert.equal(r.up, false, 'nothing went to a card');
|
||||
|
|
@ -82,8 +82,8 @@ test('giving the card up drops its pictures; the 2D map paints its own', () => {
|
|||
assert.equal(ev('SPRS.size'), 0, 'no picture without a canvas is left behind');
|
||||
assert.equal(ev('GLS.shx+GLS.shy+GLS.shh'), 0, 'the atlas is forgotten');
|
||||
ev('draw(1016)');
|
||||
assert.ok(ev('SPRS.size') > 10);
|
||||
assert.equal(ev(`[...SPRS.values()].every(e=>e.cv)`), true, 'the 2D map’s pictures have their canvas');
|
||||
assert.ok(ev('SPRC.size') > 10);
|
||||
assert.equal(ev(`[...SPRC.values()].every(e=>e.cv)`), true, 'the 2D map’s pictures have their canvas');
|
||||
assert.ok(ev('ctx===CTX0'));
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -27,8 +27,8 @@ const STUB = `(()=>{const calls=[];const gl=new Proxy({calls},{get(t,k){if(k in
|
|||
// One POP's city pictures painted during frames from zoom z0 to z1 (the clock moving 16 ms a frame), by scale.
|
||||
const ZOOM = (z0, z1, n) => `(()=>{const id=S.home,C=CITY[id];cam.x=C.x;cam.y=C.y;const seen=new Set(),bad=[];
|
||||
for(let i=0;i<=${n};i++){cam.z=${z0}+(${z1}-${z0})*i/${n};__T+=16;draw(__T);
|
||||
for(const k of SPRS.keys()){const p=k.split('|');if(p[1]==='city'&&p[2]===id)seen.add(+p[4]);}
|
||||
const k=cityScale(iconScale()),kq=[...SPRS.keys()].filter(x=>x.split('|')[1]==='city'&&x.split('|')[2]===id).map(x=>+x.split('|')[4]);
|
||||
for(const k of SPRS.keys()){const p=k.split('|');if(p[0]==='city'&&p[1]===id)seen.add(+p[3]);}
|
||||
const k=cityScale(iconScale()),kq=[...SPRS.keys()].filter(x=>x.split('|')[0]==='city'&&x.split('|')[1]===id).map(x=>+x.split('|')[3]);
|
||||
if(kq.length&&!kq.some(q=>Math.max(q/k,k/q)<=1.25*(1+.5/24/0.7)))bad.push(i);}
|
||||
return JSON.stringify({steps:[...seen].sort(),bad});})()`;
|
||||
|
||||
|
|
@ -45,7 +45,7 @@ test('on the card a zoom keeps each city’s picture for a while, and paints it
|
|||
assert.equal(ev('zoomMoving()'), true);
|
||||
ev('__T+=300;draw(__T)');
|
||||
const rest = JSON.parse(ev(`(()=>{const id=S.home,want=Math.round(cityScale(iconScale())*24)/24;
|
||||
return JSON.stringify({f:ZH.f,want,has:[...SPRS.keys()].some(k=>{const p=k.split('|');return p[1]==='city'&&p[2]===id&&+p[4]===want;}),moving:zoomMoving()});})()`));
|
||||
return JSON.stringify({f:ZH.f,want,has:[...SPRS.keys()].some(k=>{const p=k.split('|');return p[0]==='city'&&p[1]===id&&+p[3]===want;}),moving:zoomMoving()});})()`));
|
||||
assert.equal(rest.moving, false);
|
||||
assert.equal(rest.f, 1, 'at rest nothing is stretched');
|
||||
assert.equal(rest.has, true, 'the city is painted again at its own step');
|
||||
|
|
@ -56,8 +56,8 @@ test('the 2D map paints cities at their own step on every frame of a zoom, as be
|
|||
const { ev, errors } = open();
|
||||
ev(`mapGl(false);cam.z=0.8;draw(__T);`);
|
||||
const r = JSON.parse(ev(`(()=>{const id=S.home,C=CITY[id];cam.x=C.x;cam.y=C.y;const bad=[];
|
||||
for(let i=0;i<=30;i++){cam.z=0.8+i/30;__T+=16;SPRS.clear();draw(__T);const want=Math.round(cityScale(iconScale())*24)/24;
|
||||
if(ZH.f!==1||![...SPRS.keys()].some(k=>{const p=k.split('|');return p[1]==='city'&&p[2]===id&&+p[4]===want;}))bad.push(i);}
|
||||
for(let i=0;i<=30;i++){cam.z=0.8+i/30;__T+=16;SPRC.clear();draw(__T);const want=Math.round(cityScale(iconScale())*24)/24;
|
||||
if(ZH.f!==1||![...SPRC.keys()].some(k=>{const p=k.split('|');return p[0]==='city'&&p[1]===id&&+p[3]===want;}))bad.push(i);}
|
||||
return JSON.stringify({bad});})()`));
|
||||
assert.deepEqual(r.bad, []);
|
||||
assert.deepEqual(errors, []);
|
||||
|
|
@ -83,7 +83,7 @@ test('pictures that keep their size whatever the zoom (far-view cities, cones, d
|
|||
ev(STUB);
|
||||
const r = JSON.parse(ev(`(()=>{cam.z=1;draw(__T);__T+=1000;cam.z=1;draw(__T);cam.z=1.4;__T+=16;draw(__T);GLS.frame=true;GLS.n=0;ctx=GLX.fx;
|
||||
const f=ZH.f;drawCone(100,100,0);drawBackhoe(200,100,0,false);const D=GLS.data,o=[D[2],D[3],D[SPR_STRIDE+2],D[SPR_STRIDE+3]];
|
||||
const keys=[...SPRS.keys()].filter(k=>['cone','hoe'].includes(k.split('|')[1]));GLS.frame=false;GLS.n=0;ctx=CTX0;return JSON.stringify({f,o,keys});})()`));
|
||||
const keys=[...SPRS.keys()].filter(k=>['cone','hoe'].includes(k.split('|')[0]));GLS.frame=false;GLS.n=0;ctx=CTX0;return JSON.stringify({f,o,keys});})()`));
|
||||
assert.notEqual(r.f, 1, 'the zoom is moving and city pictures are held');
|
||||
assert.deepEqual(r.o, [26, 28, 38, 32], 'drawn at their own size');
|
||||
assert.ok(r.keys.length >= 2 && r.keys.every(k => k.endsWith('|1')), 'painted at scale 1: ' + r.keys);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue