mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 16:10:07 +00:00
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:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user