浏览代码

Merge pull request #9316 from Popov72/fix-nme-pbrmetallicrough-leak

NME: Fix memory leak with Light and PBRMetallicRoughness blocks
David Catuhe 4 年之前
父节点
当前提交
ed4e4a39e3

+ 2 - 1
src/Materials/Node/Blocks/Dual/lightBlock.ts

@@ -185,7 +185,8 @@ export class LightBlock extends NodeMaterialBlock {
             if (!defines["LIGHT" + lightIndex]) {
                 break;
             }
-            MaterialHelper.PrepareUniformsAndSamplersForLight(lightIndex, state.uniforms, state.samplers, defines["PROJECTEDLIGHTTEXTURE" + lightIndex], uniformBuffers);
+            const onlyUpdateBuffersList = state.uniforms.indexOf("vLightData" + lightIndex) >= 0;
+            MaterialHelper.PrepareUniformsAndSamplersForLight(lightIndex, state.uniforms, state.samplers, defines["PROJECTEDLIGHTTEXTURE" + lightIndex], uniformBuffers, onlyUpdateBuffersList);
         }
     }
 

+ 10 - 9
src/Materials/Node/Blocks/PBR/pbrMetallicRoughnessBlock.ts

@@ -11,7 +11,7 @@ import { Light } from '../../../../Lights/light';
 import { Nullable } from '../../../../types';
 import { _TypeStore } from '../../../../Misc/typeStore';
 import { AbstractMesh } from '../../../../Meshes/abstractMesh';
-import { Effect, IEffectCreationOptions } from '../../../effect';
+import { Effect } from '../../../effect';
 import { Mesh } from '../../../../Meshes/mesh';
 import { PBRBaseMaterial } from '../../../PBR/pbrBaseMaterial';
 import { Scene } from '../../../../scene';
@@ -647,10 +647,11 @@ export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
         defines.setValue("SPECULAROVERALPHA", this.useSpecularOverAlpha, true);
         defines.setValue("SPECULARAA", this._scene.getEngine().getCaps().standardDerivatives && this.enableSpecularAntiAliasing, true);
         defines.setValue("REALTIME_FILTERING", this.realTimeFiltering, true);
-        defines.setValue("NUM_SAMPLES", "" + this.realTimeFilteringQuality, true);
 
         if (this._scene.getEngine().webGLVersion > 1) {
             defines.setValue("NUM_SAMPLES", this.realTimeFilteringQuality + "u", true);
+        } else {
+            defines.setValue("NUM_SAMPLES", "" + this.realTimeFilteringQuality, true);
         }
 
         // Advanced
@@ -700,13 +701,13 @@ export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
     }
 
     public updateUniformsAndSamples(state: NodeMaterialBuildState, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines, uniformBuffers: string[]) {
-        MaterialHelper.PrepareUniformsAndSamplersList(<IEffectCreationOptions>{
-            uniformsNames: state.uniforms,
-            uniformBuffersNames: uniformBuffers,
-            samplers: state.samplers,
-            defines: defines,
-            maxSimultaneousLights: nodeMaterial.maxSimultaneousLights
-        });
+        for (var lightIndex = 0; lightIndex < nodeMaterial.maxSimultaneousLights; lightIndex++) {
+            if (!defines["LIGHT" + lightIndex]) {
+                break;
+            }
+            const onlyUpdateBuffersList = state.uniforms.indexOf("vLightData" + lightIndex) >= 0;
+            MaterialHelper.PrepareUniformsAndSamplersForLight(lightIndex, state.uniforms, state.samplers, defines["PROJECTEDLIGHTTEXTURE" + lightIndex], uniformBuffers, onlyUpdateBuffersList);
+        }
     }
 
     public bind(effect: Effect, nodeMaterial: NodeMaterial, mesh?: Mesh) {

+ 10 - 5
src/Materials/materialHelper.ts

@@ -558,8 +558,17 @@ export class MaterialHelper {
      * @param samplersList The sampler list
      * @param projectedLightTexture defines if projected texture must be used
      * @param uniformBuffersList defines an optional list of uniform buffers
+     * @param updateOnlyBuffersList True to only update the uniformBuffersList array
      */
-    public static PrepareUniformsAndSamplersForLight(lightIndex: number, uniformsList: string[], samplersList: string[], projectedLightTexture?: any, uniformBuffersList: Nullable<string[]> = null) {
+    public static PrepareUniformsAndSamplersForLight(lightIndex: number, uniformsList: string[], samplersList: string[], projectedLightTexture?: any, uniformBuffersList: Nullable<string[]> = null, updateOnlyBuffersList = false) {
+        if (uniformBuffersList) {
+            uniformBuffersList.push("Light" + lightIndex);
+        }
+
+        if (updateOnlyBuffersList) {
+            return;
+        }
+
         uniformsList.push(
             "vLightData" + lightIndex,
             "vLightDiffuse" + lightIndex,
@@ -572,10 +581,6 @@ export class MaterialHelper {
             "depthValues" + lightIndex,
         );
 
-        if (uniformBuffersList) {
-            uniformBuffersList.push("Light" + lightIndex);
-        }
-
         samplersList.push("shadowSampler" + lightIndex);
         samplersList.push("depthSampler" + lightIndex);