WebGL map: free what the card holds when it is given up for good
Given up while its context still lived (memory refused, a setup failing half-way), the card kept the atlas (64 MB at 4096 px), the tubes' points and the rest while the 2D map began painting its own pictures. glKill() now deletes the biggest and lets the context go (WEBGL_lose_context, where the browser has it), unless the browser already lost it; glInit() keeps the context from the start so a failed setup is let go too, and the loss event of a context let go on purpose does not give the card up a second time. Always 2D still keeps the card for later.
This commit is contained in:
parent
123b3d00ea
commit
10ef838e87
2 changed files with 50 additions and 4 deletions
|
|
@ -8,7 +8,7 @@
|
|||
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:''};
|
||||
const GLX={want:true,dead:false,gl:null,ctx: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;
|
||||
|
|
@ -19,7 +19,15 @@ function mapGl(on,keep=true){GLX.want=!!on;if(keep)try{on?localStorage.removeIte
|
|||
function glShow(on){on=!!on;if(GLX.shown===on)return;GLX.shown=on;const e=document.getElementById('mapgl'),fx=document.getElementById('mapfx');if(e)e.hidden=!on;if(fx)fx.hidden=!on;}
|
||||
// Marks an error as the card's own, so draw() falls back to 2D instead of trying WebGL again every frame.
|
||||
function glErr(e){const x=e instanceof Error?e:new Error(String(e));x.glErr=true;return x;}
|
||||
function glKill(why){console.warn('[tubes] the map stays 2D:',why&&why.message||why);GLX.why=String(why&&why.message||why);GLX.dead=true;GLX.gl=null;spritesDrop();glShow(false);}
|
||||
function glKill(why){console.warn('[tubes] the map stays 2D:',why&&why.message||why);GLX.why=String(why&&why.message||why);GLX.dead=true;
|
||||
const gl=GLX.ctx;GLX.gl=GLX.ctx=null;spritesDrop();glFree(gl);glShow(false);}
|
||||
/* The card given up for good (not Always 2D, which keeps it for later) still held what was sent to it, the atlas alone
|
||||
64 MB, while the 2D map starts painting its own pictures. So its biggest things are deleted and the context let go
|
||||
(WEBGL_lose_context, where the browser has it), unless the browser already lost it, which freed them. glInit() keeps
|
||||
the context in GLX.ctx from the start, so a setup that fails half-way is let go too. */
|
||||
function glFree(gl){try{if(gl&&gl.isContextLost()===false){for(const x of [GLS.tex,GLX.tex])if(x)gl.deleteTexture(x);if(GLS.buf)gl.deleteBuffer(GLS.buf);
|
||||
const L=gl.getExtension('WEBGL_lose_context');if(L&&typeof L.loseContext==='function')L.loseContext();}}catch(e){}
|
||||
GLS.tex=GLS.buf=GLS.vao=GLS.prog=null;GLX.tex=null;GLX.land={};GLS_PAD.cv=GLS_PAD.g=null;}
|
||||
/* A card short of memory may refuse a texture or a buffer without throwing: it only leaves OUT_OF_MEMORY for
|
||||
getError(), and the texture then draws black (every picture a black box) or the buffer draws nothing. So the big
|
||||
uploads (the atlas, the land, the tubes, the cables) read it, and give the card up like any of its errors. Never
|
||||
|
|
@ -42,13 +50,13 @@ function glVao(gl,sizes,div=1,data=null){const v=gl.createVertexArray(),b=gl.cre
|
|||
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');
|
||||
if(!gl||typeof gl.createShader!=='function')throw new Error('no WebGL2');GLX.ctx=gl;
|
||||
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;});
|
||||
c.addEventListener('webglcontextlost',e=>{e.preventDefault();if(GLX.dead)return;glKill('the graphics context was lost');UI.drawnAt=0;});
|
||||
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.
|
||||
|
|
|
|||
|
|
@ -108,3 +108,41 @@ test('a picture the card refuses without throwing gives the map to 2D in the sam
|
|||
ev('__T+=16;draw(__T);');
|
||||
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', () => {
|
||||
const { ev, errors } = open({ tubes: true });
|
||||
assert.equal(ev('glInit()'), true);
|
||||
ev('window.__atlas=GLS.tex;window.__tubes=GLX.tex;window.__buf=GLS.buf;');
|
||||
ev('UI.tickErr=false;__T+=16;draw(__T);');
|
||||
assert.equal(ev('GLX.dead'), true);
|
||||
assert.equal(ev('__DEL.includes(__atlas)&&__DEL.includes(__tubes)&&__DEL.includes(__buf)'), true, 'the atlas, the tubes’ points and the pictures’ buffer are deleted');
|
||||
assert.equal(ev('__LOST'), 1, 'the context is let go');
|
||||
assert.equal(ev('__LOSTEV'), 1, 'its loss event came once');
|
||||
assert.equal(ev('GLS.tex===null&&GLS.buf===null&&GLX.tex===null&&GLX.gl===null&&GLX.ctx===null'), true, 'nothing on the card is referenced');
|
||||
assert.equal(ev('GLX.why'), 'the graphics card is out of memory (the tubes)', 'the loss it caused does not replace the reason');
|
||||
assert.equal(ev('!!UI.tickErr'), false, 'the frame was drawn again in 2D');
|
||||
ev('__T+=16;draw(__T);');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('a setup that fails half-way lets its context go too', () => {
|
||||
const { ev, errors } = open({ atlas: [4096, 2048] });
|
||||
assert.equal(ev('glInit()'), false);
|
||||
assert.equal(ev('__LOST'), 1);
|
||||
assert.equal(ev('GLX.ctx'), null);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('a context the browser lost is not let go a second time', () => {
|
||||
const { ev, errors } = open({});
|
||||
assert.equal(ev('glInit()'), true);
|
||||
ev('__T+=16;draw(__T);');
|
||||
ev('window.__lostNow=true;document.getElementById("mapgl").dispatchEvent(new window.Event("webglcontextlost"));');
|
||||
assert.equal(ev('GLX.dead'), true);
|
||||
assert.equal(ev('GLX.why'), 'the graphics context was lost');
|
||||
assert.equal(ev('__LOST'), 0, 'nothing to let go: the browser already did');
|
||||
ev('__T+=16;draw(__T);');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue