Răsfoiți Sursa

[MEMORY][TEXTURE] Dispose more textures when trying to allocate new one.

clandrin 3 ani în urmă
părinte
comite
d456f94511
2 a modificat fișierele cu 22 adăugiri și 13 ștergeri
  1. 17 13
      h3d/impl/MemoryManager.hx
  2. 5 0
      h3d/mat/Texture.hx

+ 17 - 13
h3d/impl/MemoryManager.hx

@@ -241,7 +241,7 @@ class MemoryManager {
 		textures.sort(sortByLRU);
 		for( t in textures ) {
 			if( t.realloc == null || t.isDisposed() ) continue;
-			if( force || t.lastFrame < hxd.Timer.frameCount - 3600 ) {
+			if( (force || t.lastFrame < hxd.Timer.frameCount - 3600) && t.lastFrame != h3d.mat.Texture.PREVENT_AUTO_DISPOSE ) {
 				t.dispose();
 				return true;
 			}
@@ -262,13 +262,15 @@ class MemoryManager {
 
 	@:allow(h3d.mat.Texture.alloc)
 	function allocTexture( t : h3d.mat.Texture ) {
-		var free = cleanTextures(false);
-		t.t = driver.allocTexture(t);
-		if( t.t == null ) {
+		while( true ) {
+			var free = cleanTextures(false);
+			t.t = driver.allocTexture(t);
+			if( t.t != null ) break;
+
 			if( driver.isDisposed() ) return;
-			if( !cleanTextures(true) ) throw "Maximum texture memory reached";
-			allocTexture(t);
-			return;
+			while( cleanTextures(false) ) {} // clean all old textures
+			if( !free && !cleanTextures(true) )
+				throw "Maximum texture memory reached";
 		}
 		textures.push(t);
 		texMemory += memSize(t);
@@ -276,13 +278,15 @@ class MemoryManager {
 
 	@:allow(h3d.mat.DepthBuffer.alloc)
 	function allocDepth( b : h3d.mat.DepthBuffer ) {
-		var free = cleanTextures(false);
-		b.b = driver.allocDepthBuffer(b);
-		if( b.b == null ) {
+		while( true ) {
+			var free = cleanTextures(false);
+			b.b = driver.allocDepthBuffer(b);
+			if( b.b != null ) break;
+
 			if( driver.isDisposed() ) return;
-			if( !cleanTextures(true) ) throw "Maximum texture memory reached";
-			allocDepth(b);
-			return;
+			while( cleanTextures(false) ) {} // clean all old textures
+			if( !free && !cleanTextures(true) )
+				throw "Maximum texture memory reached";
 		}
 		depths.push(b);
 		texMemory += b.width * b.height * 4;

+ 5 - 0
h3d/mat/Texture.hx

@@ -6,6 +6,7 @@ class Texture {
 
 	static var UID = 0;
 	static final PREVENT_AUTO_DISPOSE = 0x7FFFFFFF;
+	static final PREVENT_FORCED_DISPOSE = -1;
 
 	/**
 		The default texture color format
@@ -161,6 +162,10 @@ class Texture {
 		lastFrame = PREVENT_AUTO_DISPOSE;
 	}
 
+	public function preventForcedDispose() {
+		lastFrame = PREVENT_FORCED_DISPOSE;
+	}
+
 	/**
 		Some textures might take some time to load. You can check flags.has(Loading)
 		or add a waitLoad callback which will get called either immediately if the texture is already loaded