Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShadowDepthWrapper: Fix a memory leak when new effects must be created #14449

Merged
merged 10 commits into from Oct 20, 2023
19 changes: 16 additions & 3 deletions packages/dev/core/src/Materials/shadowDepthWrapper.ts
Expand Up @@ -116,18 +116,31 @@ export class ShadowDepthWrapper {
const subMesh = key.value;
if (subMesh?.getMesh() === (mesh as AbstractMesh)) {
this._subMeshToEffect.delete(subMesh);
this._subMeshToDepthWrapper.mm.delete(subMesh);
this._deleteDepthWrapperEffect(subMesh);
}
}
})
);
}

this._subMeshToEffect.set(params.subMesh, [params.effect, this._scene.getEngine().currentRenderPassId]);
this._subMeshToDepthWrapper.mm.delete(params.subMesh); // trigger a depth effect recreation
if (this._subMeshToEffect.get(params.subMesh)?.[0] !== params.effect) {
this._subMeshToEffect.set(params.subMesh, [params.effect, this._scene.getEngine().currentRenderPassId]);
this._deleteDepthWrapperEffect(params.subMesh);
}
});
}

private _deleteDepthWrapperEffect(subMesh: Nullable<SubMesh>): void {
const depthWrapperEntries = this._subMeshToDepthWrapper.mm.get(subMesh);
if (depthWrapperEntries) {
// find and release the previous depth effect
depthWrapperEntries.forEach((depthWrapper) => {
depthWrapper.mainDrawWrapper.effect?.dispose();
});
this._subMeshToDepthWrapper.mm.delete(subMesh); // trigger a depth effect recreation
}
}

/**
* Gets the effect to use to generate the depth map
* @param subMesh subMesh to get the effect for
Expand Down