From db966c8cb8d235c90d89645e1f6dde8667421d2a Mon Sep 17 00:00:00 2001 From: Guy Perfect Date: Thu, 26 Dec 2024 14:13:13 -0600 Subject: [PATCH] Fix memory growth handling --- web/Core.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/web/Core.js b/web/Core.js index bd1b13b..92b75b9 100644 --- a/web/Core.js +++ b/web/Core.js @@ -87,11 +87,8 @@ new class Core { this.sims.set(sim.pointer, sim); // Video - sim.pixels = this.#noalloc( - this.GetExtPixels(sim.pointer), - 384*224*4, sim, "pixels", Uint8ClampedArray - ); - sim.image = new ImageData(sim.pixels, 384, 224); + sim.pixels = this.#getPixels(sim); + sim.image = new ImageData(sim.pixels, 384, 224); // Audio sim.samples = this.#noalloc( @@ -364,8 +361,10 @@ new class Core { this.mallocs.set(buffer.pointer, buffer); this.#updateTarget(buffer); } - for (let sim of this.sims) - sim.image = new ImageData(sim.pixels, 384, 224); + for (let sim of this.sims.values()) { + sim.pixels = this.#getPixels(sim); + sim.image = new ImageData(sim.pixels, 384, 224); + } } @@ -436,6 +435,14 @@ new class Core { this.Realloc(buffer.pointer, 0); } + // Register a sim's pixel buffer + #getPixels(sim) { + return this.#noalloc( + this.GetExtPixels(sim.pointer), + 384*224*4, sim, "pixels", Uint8ClampedArray + ); + } + // Allocate memory in WebAssembly and register the buffer #malloc(count, target = null, assign = null, type = Uint8ClampedArray) { return this.#noalloc(