diff --git a/src/game/96-map/59-gl-sprites.js b/src/game/96-map/59-gl-sprites.js index c6f3bb8..615c48e 100644 --- a/src/game/96-map/59-gl-sprites.js +++ b/src/game/96-map/59-gl-sprites.js @@ -5,7 +5,7 @@ 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; -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}; +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. */ 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; @@ -30,7 +30,7 @@ function glsInit(gl){GLS.prog=glProg(gl,SPR_VS,SPR_FS);const {v,b}=glVao(gl,[4,4 // 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;} // 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=0;SPRS.clear();GLS.shx=GLS.shy=GLS.shh=0;} +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 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. */ @@ -70,7 +70,7 @@ function glsPut(gl,e,src){const pw=e.pw,ph=e.ph,A=GLS.size;if(pw>A||ph>A)return gl.activeTexture(gl.TEXTURE0);gl.bindTexture(gl.TEXTURE_2D,GLS.tex);gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL,true); // Only the picture's corner of the source: the pad is bigger than most pictures. gl.texSubImage2D(gl.TEXTURE_2D,0,x,y,pw,ph,gl.RGBA,gl.UNSIGNED_BYTE,src);gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL,false); - Object.assign(e,{up:true,u0:x/A,v0:y/A,u1:(x+pw)/A,v1:(y+ph)/A});return true;} + GLS.up++;Object.assign(e,{up:true,u0:x/A,v0:y/A,u1:(x+pw)/A,v1:(y+ph)/A});return true;} // The next record in GLS.data, which grows as needed: take it before reading GLS.data, which it may replace. function glsNext(){if((GLS.n+1)*SPR_STRIDE>GLS.data.length){const nd=new Float32Array(GLS.data.length*2);nd.set(GLS.data);GLS.data=nd;}return GLS.n++*SPR_STRIDE;} // Draws a picture at (x,y) of w×h CSS px, on the card for a frame drawn there, else with the 2D canvas. @@ -116,5 +116,8 @@ function glsDraw(gl,n){gl.useProgram(GLS.prog.p);gl.bindVertexArray(GLS.vao);gl. gl.activeTexture(gl.TEXTURE0);gl.bindTexture(gl.TEXTURE_2D,GLS.tex); 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. -function glsFrameEnd(){if(!GLS.frame)return;GLS.frame=false;glsDrawNow();} +/* 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();} diff --git a/tests/webgl-out-of-memory.test.js b/tests/webgl-out-of-memory.test.js index 4d49761..b43f4e0 100644 --- a/tests/webgl-out-of-memory.test.js +++ b/tests/webgl-out-of-memory.test.js @@ -13,12 +13,16 @@ const SAVE = fs.readFileSync(path.join(__dirname, 'fixtures', 'big-network.json' /* A WebGL2 context that compiles everything, as tests/webgl-map.test.js's, and that refuses memory as asked in window.__FAIL: atlas (the texture sizes refused), land (the first static buffer after setup), tubes (the tubes' - points). A refusal sets OUT_OF_MEMORY, read once by getError(); the reads are counted. */ -const GL = `(()=>{window.CanvasRenderingContext2D=window.CanvasRenderingContext2D||class{};window.__GE=0;window.__ERR=0; - const F=window.__FAIL;let setup=true;const c=document.getElementById('mapgl'); + points), sprites (every picture sent into the atlas). A refusal sets OUT_OF_MEMORY, read once by getError(); the + reads are counted. It also counts the pictures sent (__SUB), what is deleted (__DEL) and the context let go (__LOST). */ +const GL = `(()=>{window.CanvasRenderingContext2D=window.CanvasRenderingContext2D||class{};window.__GE=0;window.__ERR=0;window.__SUB=0;window.__DEL=[];window.__LOST=0;window.__LOSTEV=0; + const F=window.__FAIL;let setup=true,lost=false;const c=document.getElementById('mapgl');c.addEventListener('webglcontextlost',()=>{__LOSTEV++;}); 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==='getExtension')return (n)=>n==='WEBGL_lose_context'?{loseContext:()=>{__LOST++;lost=true;c.dispatchEvent(new window.Event('webglcontextlost'));}}:{UNMASKED_RENDERER_WEBGL:'REN'}; + if(k==='isContextLost')return ()=>lost||!!window.__lostNow; + if(k==='deleteTexture'||k==='deleteBuffer')return (x)=>{__DEL.push(x);}; + if(k==='texSubImage2D')return ()=>{__SUB++;if(F.sprites)__ERR='OUT_OF_MEMORY';}; if(k==='getParameter')return (p)=>p==='REN'?'Stub GPU':p==='ACTIVE_UNIFORMS'?0:4096; if(k==='getShaderParameter'||k==='getProgramParameter')return (x,p)=>p==='ACTIVE_UNIFORMS'?0:true; if(k==='getError')return ()=>{__GE++;const e=__ERR;__ERR=0;return e;}; @@ -76,7 +80,31 @@ 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'); - ev('__GE=0;for(let i=0;i<30;i++){cam.x+=3;__T+=16;draw(__T);}'); - assert.equal(ev('__GE'), 0, 'panning sends nothing big, so nothing is read'); + // 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);}'); + assert.equal(ev('__GE'), 0, 'a still map reads nothing'); + assert.deepEqual(errors, []); +}); + +/* A picture the card refused without throwing (texSubImage2D only leaves OUT_OF_MEMORY) was kept as sent, and drawn + from an atlas that never got it. The frame now checks its new pictures before drawing them, and gives the map to 2D + in that same frame: no picture is kept as sent. */ +test('a picture the card refuses without throwing gives the map to 2D in the same frame', () => { + const { ev, errors } = open({ sprites: true }); + 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.ok(ev('__SUB') > 0, 'pictures were sent'); + assert.equal(ev('GLX.dead'), true, 'the card is given up'); + assert.equal(ev('SPRS.size'), 0, 'no picture is kept as sent to the card'); + assert.equal(ev('glOn()'), false); + assert.equal(ev('document.getElementById("mapgl").hidden'), true); + assert.ok(ev('SPRC.size') > 0, 'the same frame was drawn again in 2D, with its own pictures'); + assert.equal(ev('!!UI.tickErr'), false, 'no glitch for the player'); + ev('__T+=16;draw(__T);'); assert.deepEqual(errors, []); });