Răsfoiți Sursa

keep track and dispose depth buffers on context lost

ncannasse 7 ani în urmă
părinte
comite
66ab80787b
2 a modificat fișierele cu 32 adăugiri și 3 ștergeri
  1. 25 0
      h3d/impl/MemoryManager.hx
  2. 7 3
      h3d/mat/DepthBuffer.hx

+ 25 - 0
h3d/impl/MemoryManager.hx

@@ -12,6 +12,7 @@ class MemoryManager {
 	var buffers : Array<ManagedBuffer>;
 	var indexes : Array<Indexes>;
 	var textures : Array<h3d.mat.Texture>;
+	var depths : Array<h3d.mat.DepthBuffer>;
 
 	public var triIndexes(default,null) : Indexes;
 	public var quadIndexes(default,null) : Indexes;
@@ -27,6 +28,7 @@ class MemoryManager {
 		indexes = new Array();
 		textures = new Array();
 		buffers = new Array();
+		depths = new Array();
 		initIndexes();
 	}
 
@@ -265,6 +267,27 @@ class MemoryManager {
 		texMemory += t.width * t.height * bpp(t);
 	}
 
+	@:allow(h3d.mat.DepthBuffer.alloc)
+	function allocDepth( b : h3d.mat.DepthBuffer ) {
+		var free = cleanTextures(false);
+		b.b = driver.allocDepthBuffer(b);
+		if( b.b == null ) {
+			if( driver.isDisposed() ) return;
+			if( !cleanTextures(true) ) throw "Maximum texture memory reached";
+			allocDepth(b);
+			return;
+		}
+		depths.push(b);
+		texMemory += b.width * b.height * 4;
+	}
+
+	@:allow(h3d.mat.DepthBuffer.dispose)
+	function deleteDepth( b : h3d.mat.DepthBuffer ) {
+		depths.remove(b);
+		driver.disposeDepthBuffer(b);
+		texMemory -= b.width * b.height * 4;
+	}
+
 	// ------------------------------------- DISPOSE ------------------------------------------
 
 	public function onContextLost() {
@@ -279,6 +302,8 @@ class MemoryManager {
 		quadIndexes = null;
 		for( t in textures.copy() )
 			t.dispose();
+		for( b in depths.copy() )
+			b.dispose();
 		for( b in buffers.copy() ) {
 			var b = b;
 			while( b != null ) {

+ 7 - 3
h3d/mat/DepthBuffer.hx

@@ -5,6 +5,7 @@ package h3d.mat;
 **/
 class DepthBuffer {
 
+	@:allow(h3d.impl.MemoryManager)
 	var b : h3d.impl.Driver.DepthBuffer;
 	public var width(default, null) : Int;
 	public var height(default, null) : Int;
@@ -15,13 +16,16 @@ class DepthBuffer {
 	public function new( width : Int, height : Int ) {
 		this.width = width;
 		this.height = height;
-		if( width >= 0 )
-			b = h3d.Engine.getCurrent().driver.allocDepthBuffer(this);
+		if( width >= 0 ) alloc();
+	}
+
+	function alloc() {
+		h3d.Engine.getCurrent().mem.allocDepth(this);
 	}
 
 	public function dispose() {
 		if( b != null ) {
-			h3d.Engine.getCurrent().driver.disposeDepthBuffer(this);
+			h3d.Engine.getCurrent().mem.deleteDepth(this);
 			b = null;
 		}
 	}