fix(atlas): give back every GPU object when the globe goes

dispose() freed the world and region layers only; the ocean, arcs, outline, pick texture and framebuffer, status textures, shared buffers and programs stayed on the context, which React keeps alive across a remount, so each visit to the Atlas left them behind.
This commit is contained in:
Maurice
2026-11-29 10:42:19 +01:00
parent 9018ee8980
commit ab0e6fda8b
3 changed files with 44 additions and 4 deletions
@@ -655,7 +655,18 @@ describe('GlobeRenderer dispose', () => {
const deleted = deletedBuffers(fake);
expect(deleted).toContain(fake.bufferOf(w.fill.ll));
expect(deleted).toContain(fake.bufferOf(reg.borders.seg));
expect(fake.gl.deleteVertexArray).toHaveBeenCalledTimes(8);
// World and region: fill, pick, coast and borders each; then the quad.
expect(fake.gl.deleteVertexArray).toHaveBeenCalledTimes(9);
});
it('FE-ATLAS2-RENDER-048: gives back the programs, textures and shared buffers too, so a revisit leaks nothing', () => {
const { fake, r } = setup();
r.dispose();
expect(fake.gl.deleteProgram).toHaveBeenCalledTimes(5);
// The status and fx textures; no pick texture before the first pick.
expect(fake.gl.deleteTexture).toHaveBeenCalledTimes(2);
expect(fake.gl.deleteFramebuffer).not.toHaveBeenCalled();
expect(fake.gl.deleteBuffer).toHaveBeenCalledTimes(2);
});
});
@@ -132,6 +132,7 @@ export class GlobeRenderer {
private skyP: Program;
private rimP: Program;
private quad: WebGLVertexArrayObject;
private quadBuf: WebGLBuffer | null = null;
private cornerBuf: WebGLBuffer;
private statusTex: WebGLTexture;
private fxTex: WebGLTexture;
@@ -185,6 +186,7 @@ export class GlobeRenderer {
const vao = gl.createVertexArray() as WebGLVertexArrayObject;
gl.bindVertexArray(vao);
const b = gl.createBuffer();
this.quadBuf = b;
gl.bindBuffer(gl.ARRAY_BUFFER, b);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), gl.STATIC_DRAW);
for (const p of [this.skyP, this.rimP]) {
@@ -578,14 +580,40 @@ export class GlobeRenderer {
return id - 1;
}
/**
* Gives back everything this renderer put on the GPU. The context itself is
* left alive: React mounts twice in development and the second engine takes
* over the same canvas, so whatever stayed behind here would pile up on it
* with every visit to the Atlas.
*/
dispose(): void {
const gl = this.gl;
for (const w of this.worlds.values()) this.freeLayer(w);
this.worlds.clear();
for (const r of this.regions.values()) this.freeLayer(r);
this.regions.clear();
// The context is left alive: React mounts twice in development and the
// second engine takes over the same canvas.
void gl;
const freeLines = (l: GpuLines) => {
for (const b of l.buffers) gl.deleteBuffer(b);
gl.deleteVertexArray(l.vao);
};
if (this.ocean) {
freeLines(this.ocean);
gl.deleteVertexArray(this.ocean.pickVao);
this.ocean = null;
}
for (const a of this.arcs.values()) freeLines(a);
this.arcs.clear();
if (this.outline) freeLines(this.outline.gpu);
this.outline = null;
if (this.pickTex) gl.deleteTexture(this.pickTex);
if (this.pickFbo) gl.deleteFramebuffer(this.pickFbo);
this.pickTex = null;
this.pickFbo = null;
gl.deleteTexture(this.statusTex);
gl.deleteTexture(this.fxTex);
gl.deleteVertexArray(this.quad);
if (this.quadBuf) gl.deleteBuffer(this.quadBuf);
gl.deleteBuffer(this.cornerBuf);
for (const p of [this.fillP, this.pickP, this.lineP, this.skyP, this.rimP]) gl.deleteProgram(p.prog);
}
}