Browse Source

Fix driver crash accessing disposed forward's light buffer. Prevent (#966)

* Fix driver crash accessing disposed forward's light buffer. Prevent
memory leaking.

Co-authored-by: clandrin <[email protected]>
clementlandrin 4 năm trước cách đây
mục cha
commit
b51b747527

+ 3 - 0
h3d/scene/LightSystem.hx

@@ -63,4 +63,7 @@ class LightSystem {
 		return shaders;
 	}
 
+	public function dispose(){
+	}
+
 }

+ 1 - 0
h3d/scene/Renderer.hx

@@ -56,6 +56,7 @@ class Renderer extends hxd.impl.AnyProps {
 			p.dispose();
 		for( f in effects )
 			f.dispose();
+		ctx.lightSystem.dispose();
 		passObjects = new Map();
 	}
 

+ 8 - 1
h3d/scene/pbr/LightBuffer.hx

@@ -13,6 +13,7 @@ class LightBuffer {
 	final SPOT_LIGHT_INFO_SIZE = 8;
 	final DIR_LIGHT_INFO_SIZE = 5;
 
+
 	public function new() {
 		createBuffers();
 	}
@@ -76,6 +77,8 @@ class LightBuffer {
 	}
 
 	public function sync( ctx : h3d.scene.RenderContext ) {
+		if (defaultForwardShader.lightInfos.isDisposed())
+			createBuffers();
 
 		var r = @:privateAccess ctx.scene.renderer;
 		var pbrRenderer = Std.downcast(r, Renderer);
@@ -83,6 +86,7 @@ class LightBuffer {
 		var p : h3d.scene.pbr.Renderer.RenderProps = pbrRenderer.props;
 		var s = defaultForwardShader;
 
+
 		s.cameraPosition = ctx.camera.pos;
 		s.emissivePower = p.emissive * p.emissive;
 
@@ -166,7 +170,6 @@ class LightBuffer {
 
 			l = l.next;
 		}
-
 		s.lightInfos.uploadVector(lightInfos, 0, s.lightInfos.vertices, 0);
 
 		var pbrIndirect = @:privateAccess pbrRenderer.pbrIndirect;
@@ -180,4 +183,8 @@ class LightBuffer {
 			s.irrSpecularLevels = pbrIndirect.irrSpecularLevels;
 		}
 	}
+
+	public function dispose() {
+		defaultForwardShader.lightInfos.dispose();
+	}
 }

+ 6 - 1
h3d/scene/pbr/LightSystem.hx

@@ -36,7 +36,8 @@ class LightSystem extends h3d.scene.LightSystem {
 
 	override function initLights( ctx : h3d.scene.RenderContext ) @:privateAccess {
 		super.initLights(ctx);
-		lightBuffer.sync(ctx);
+		if (forwardMode)
+			lightBuffer.sync(ctx);
 	}
 
 	public function drawShadows( light : Light, passes : h3d.pass.PassList ) {
@@ -62,4 +63,8 @@ class LightSystem extends h3d.scene.LightSystem {
 			plight = plight.next;
 		}
 	}
+
+	override function dispose() {
+		lightBuffer.dispose();
+	}
 }