diff --git a/client/src/components/AtlasGlobe/GlobeRenderer.test.ts b/client/src/components/AtlasGlobe/GlobeRenderer.test.ts index b540f9f1f..8e879b134 100644 --- a/client/src/components/AtlasGlobe/GlobeRenderer.test.ts +++ b/client/src/components/AtlasGlobe/GlobeRenderer.test.ts @@ -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); }); }); diff --git a/client/src/components/AtlasGlobe/GlobeRenderer.ts b/client/src/components/AtlasGlobe/GlobeRenderer.ts index e1f06d278..1871081a1 100644 --- a/client/src/components/AtlasGlobe/GlobeRenderer.ts +++ b/client/src/components/AtlasGlobe/GlobeRenderer.ts @@ -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); } } diff --git a/client/tests/helpers/fakeWebGl2.ts b/client/tests/helpers/fakeWebGl2.ts index 1a3a9cb8f..e10db97fb 100644 --- a/client/tests/helpers/fakeWebGl2.ts +++ b/client/tests/helpers/fakeWebGl2.ts @@ -154,6 +154,7 @@ export function createFakeGl(opts: FakeGlOptions = {}) { ), getShaderInfoLog: vi.fn(() => (opts.shaderLog === undefined ? 'shader log' : opts.shaderLog)), deleteShader: vi.fn((_s: FakeShader) => undefined), + deleteProgram: vi.fn((_p: FakeProgram) => undefined), createProgram: vi.fn((): FakeProgram => { const p: FakeProgram = { kind: 'program',