diff --git a/frontend/src/app/render_wasm/api.cljs b/frontend/src/app/render_wasm/api.cljs index 01187c08cf..1bf43a7a43 100644 --- a/frontend/src/app/render_wasm/api.cljs +++ b/frontend/src/app/render_wasm/api.cljs @@ -1244,6 +1244,8 @@ (when-not (nil? context) (let [handle (.registerContext ^js gl context #js {"majorVersion" 2})] (.makeContextCurrent ^js gl handle) + (set! wasm/gl-context-handle handle) + (set! wasm/gl-context context) ;; Force the WEBGL_debug_renderer_info extension as emscripten does not enable it (.getExtension context "WEBGL_debug_renderer_info") @@ -1266,6 +1268,20 @@ (set! wasm/context-initialized? false) (h/call wasm/internal-module "_clean_up") + ;; Ensure the WebGL context is properly disposed so browsers do not keep + ;; accumulating active contexts between page switches. + (when-let [gl (unchecked-get wasm/internal-module "GL")] + (when-let [handle wasm/gl-context-handle] + (try + ;; Ask the browser to release resources explicitly if available. + (when-let [ctx wasm/gl-context] + (when-let [lose-ext (.getExtension ^js ctx "WEBGL_lose_context")] + (.loseContext ^js lose-ext))) + (.deleteContext ^js gl handle) + (finally + (set! wasm/gl-context-handle nil) + (set! wasm/gl-context nil))))) + ;; If this calls panics we don't want to crash. This happens sometimes ;; with hot-reload in develop (catch :default error diff --git a/frontend/src/app/render_wasm/wasm.cljs b/frontend/src/app/render_wasm/wasm.cljs index 3901dc975f..5fec8156e3 100644 --- a/frontend/src/app/render_wasm/wasm.cljs +++ b/frontend/src/app/render_wasm/wasm.cljs @@ -9,6 +9,8 @@ (defonce internal-frame-id nil) (defonce internal-module #js {}) +(defonce gl-context-handle nil) +(defonce gl-context nil) (defonce serializers #js {:blur-type shared/RawBlurType :blend-mode shared/RawBlendMode