From b54e5d50b6160580e5e55b5d2ba79f3cd40209d4 Mon Sep 17 00:00:00 2001 From: David Catuhe Date: Tue, 15 Oct 2024 11:04:33 -0700 Subject: [PATCH] Allow users to clear code cache (#15706) --- packages/dev/core/src/Materials/effect.ts | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/dev/core/src/Materials/effect.ts b/packages/dev/core/src/Materials/effect.ts index 513beea2a7c..d4a1bf6217a 100644 --- a/packages/dev/core/src/Materials/effect.ts +++ b/packages/dev/core/src/Materials/effect.ts @@ -149,6 +149,13 @@ export class Effect implements IDisposable { * Enable logging of the shader code when a compilation error occurs */ public static LogShaderCodeOnCompilationError = true; + + /** + * Use this with caution + * See ClearCodeCache function comments + */ + public static AutomaticallyClearCodeCache = false; + /** * Name of the effect. */ @@ -747,6 +754,10 @@ export class Effect implements IDisposable { if (this._fallbacks) { this._fallbacks.unBindMesh(); } + + if (Effect.AutomaticallyClearCodeCache) { + this.clearCodeCache(); + } } /** @@ -1448,6 +1459,18 @@ export class Effect implements IDisposable { return this; } + /** + * Use this wisely: It will remove the cached code from this effect + * It is probably ok to call it if you are not using ShadowDepthWrapper or if everything is already up and running + * DO NOT CALL IT if you want to have support for context lost recovery + */ + public clearCodeCache() { + this._vertexSourceCode = ""; + this._fragmentSourceCode = ""; + this._fragmentSourceCodeBeforeMigration = ""; + this._vertexSourceCodeBeforeMigration = ""; + } + /** * Release all associated resources. **/ @@ -1464,6 +1487,8 @@ export class Effect implements IDisposable { } this._engine._releaseEffect(this); + this.clearCodeCache(); + this._isDisposed = true; }