WebGL map: give the card up for 2D when it runs out of memory without saying so

A card short of memory may refuse a texture or a buffer without throwing: the call only leaves OUT_OF_MEMORY for
getError(), which nothing read. Refused, the 64 MB atlas drew every city, label and marker as a black box while the
map stayed on the card (seen in Chromium by refusing it).

The big uploads now read it (glMem()): the atlas, the land for each zoom level, the tubes once a day, the cables.
The atlas tries 2048 px (16 MB) before giving up; any other refusal is the card's error, so draw() gives WebGL up
for the session and draws the same frame again in 2D. getError() is never read on an ordinary frame: each read waits
for the card.

tests/webgl-out-of-memory.test.js: a 4096 px atlas refused gives a 2048 px one; both refused, or the land or the
tubes refused, leave the map in 2D in that frame with no glitch for the player; panning reads no error.
This commit is contained in:
Antoine 2026-11-04 19:08:42 +01:00
parent fb76a1e6fd
commit 52c8aaffd6
No known key found for this signature in database
GPG key ID: 728FF1CE15732B05
5 changed files with 99 additions and 8 deletions

View file

@ -18,10 +18,12 @@ const SPR_FS=GL_HEAD+`uniform sampler2D uTex;uniform float uDpr;in vec2 vUV,vQ;i
void main(){if(vK>.5){float a=clamp((vR-length(vQ))*uDpr+.5,0.,1.)*vC.a;if(a<=0.)discard;o=vec4(vC.rgb*a,a);}
else o=texture(uTex,vUV)*vC.a;}`;
function glsInit(gl){GLS.prog=glProg(gl,SPR_VS,SPR_FS);const {v,b}=glVao(gl,[4,4,4,1]);GLS.vao=v;GLS.buf=b;
// 4096 px square (64 MB), or what the card allows if that is less (some phones).
GLS.size=Math.min(4096,gl.getParameter(gl.MAX_TEXTURE_SIZE)||4096);
GLS.tex=gl.createTexture();gl.activeTexture(gl.TEXTURE0);gl.bindTexture(gl.TEXTURE_2D,GLS.tex);
gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA,GLS.size,GLS.size,0,gl.RGBA,gl.UNSIGNED_BYTE,null);
// 4096 px square (64 MB), or what the card allows if that is less (some phones). A card that has not the memory for
// it gets 2048 (16 MB: more resets of the atlas, the same map); without even that, the map stays 2D (glMem).
GLS.tex=gl.createTexture();gl.activeTexture(gl.TEXTURE0);gl.bindTexture(gl.TEXTURE_2D,GLS.tex);glMem(gl,'setup');
for(GLS.size=Math.min(4096,gl.getParameter(gl.MAX_TEXTURE_SIZE)||4096);;GLS.size>>=1){
gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA,GLS.size,GLS.size,0,gl.RGBA,gl.UNSIGNED_BYTE,null);
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;}