ソースを参照

bugfix if big buffer is split in previous alloc

ncannasse 11 年 前
コミット
26cad9b04f
1 ファイル変更27 行追加23 行削除
  1. 27 23
      h3d/impl/MemoryManager.hx

+ 27 - 23
h3d/impl/MemoryManager.hx

@@ -12,7 +12,7 @@ class MemoryManager {
 	var buffers : Array<ManagedBuffer>;
 	var buffers : Array<ManagedBuffer>;
 	var indexes : Array<Indexes>;
 	var indexes : Array<Indexes>;
 	var textures : Array<h3d.mat.Texture>;
 	var textures : Array<h3d.mat.Texture>;
-	
+
 	public var triIndexes(default,null) : Indexes;
 	public var triIndexes(default,null) : Indexes;
 	public var quadIndexes(default,null) : Indexes;
 	public var quadIndexes(default,null) : Indexes;
 	public var usedMemory(default, null) : Int = 0;
 	public var usedMemory(default, null) : Int = 0;
@@ -22,14 +22,14 @@ class MemoryManager {
 	public function new(driver) {
 	public function new(driver) {
 		this.driver = driver;
 		this.driver = driver;
 	}
 	}
-	
+
 	public function init() {
 	public function init() {
 		indexes = new Array();
 		indexes = new Array();
 		textures = new Array();
 		textures = new Array();
 		buffers = new Array();
 		buffers = new Array();
 		initIndexes();
 		initIndexes();
 	}
 	}
-	
+
 	function initIndexes() {
 	function initIndexes() {
 		var indices = new hxd.IndexBuffer();
 		var indices = new hxd.IndexBuffer();
 		for( i in 0...SIZE ) indices.push(i);
 		for( i in 0...SIZE ) indices.push(i);
@@ -58,7 +58,7 @@ class MemoryManager {
 	}
 	}
 
 
 	// ------------------------------------- BUFFERS ------------------------------------------
 	// ------------------------------------- BUFFERS ------------------------------------------
-	
+
 	/**
 	/**
 		Clean empty (unused) buffers
 		Clean empty (unused) buffers
 	**/
 	**/
@@ -78,7 +78,7 @@ class MemoryManager {
 			}
 			}
 		}
 		}
 	}
 	}
-	
+
 	@:allow(h3d.impl.ManagedBuffer)
 	@:allow(h3d.impl.ManagedBuffer)
 	function allocManaged( m : ManagedBuffer ) {
 	function allocManaged( m : ManagedBuffer ) {
 		if( m.vbuf != null ) return;
 		if( m.vbuf != null ) return;
@@ -106,7 +106,7 @@ class MemoryManager {
 		usedMemory -= m.size * m.stride * 4;
 		usedMemory -= m.size * m.stride * 4;
 		bufferCount--;
 		bufferCount--;
 	}
 	}
-	
+
 	@:allow(h3d.Buffer)
 	@:allow(h3d.Buffer)
 	@:access(h3d.Buffer)
 	@:access(h3d.Buffer)
 	function allocBuffer( b : Buffer, stride : Int ) {
 	function allocBuffer( b : Buffer, stride : Int ) {
@@ -119,20 +119,24 @@ class MemoryManager {
 			b.vertices = max;
 			b.vertices = max;
 			// make sure to alloc in order
 			// make sure to alloc in order
 			allocBuffer(b, stride);
 			allocBuffer(b, stride);
+
+			var n = b;
+			while( n.next != null ) n = n.next;
+
 			var flags = [];
 			var flags = [];
 			for( f in ALL_FLAGS )
 			for( f in ALL_FLAGS )
 				if( b.flags.has(f) )
 				if( b.flags.has(f) )
 					flags.push(f);
 					flags.push(f);
-			b.next = new Buffer(rem, stride, flags);
+			n.next = new Buffer(rem, stride, flags);
 			return;
 			return;
 		}
 		}
-		
+
 		if( !b.flags.has(Managed) ) {
 		if( !b.flags.has(Managed) ) {
 			var m = new ManagedBuffer(stride, b.vertices);
 			var m = new ManagedBuffer(stride, b.vertices);
-			m.allocBuffer(b);
+			if( !m.allocBuffer(b) ) throw "assert";
 			return;
 			return;
 		}
 		}
-		
+
 		// look into one of the managed buffers
 		// look into one of the managed buffers
 		var m = buffers[stride], prev = null;
 		var m = buffers[stride], prev = null;
 		while( m != null ) {
 		while( m != null ) {
@@ -166,19 +170,19 @@ class MemoryManager {
 			}
 			}
 			b.vertices = total;
 			b.vertices = total;
 		}
 		}
-		
+
 		// alloc a new managed buffer
 		// alloc a new managed buffer
 		m = new ManagedBuffer(stride, SIZE, [Managed]);
 		m = new ManagedBuffer(stride, SIZE, [Managed]);
 		if( prev == null )
 		if( prev == null )
 			buffers[stride] = m;
 			buffers[stride] = m;
 		else
 		else
 			prev.next = m;
 			prev.next = m;
-	
+
 		if( !m.allocBuffer(b) ) throw "assert";
 		if( !m.allocBuffer(b) ) throw "assert";
 	}
 	}
 
 
 	// ------------------------------------- INDEXES ------------------------------------------
 	// ------------------------------------- INDEXES ------------------------------------------
-	
+
 	@:allow(h3d.Indexes)
 	@:allow(h3d.Indexes)
 	function deleteIndexes( i : Indexes ) {
 	function deleteIndexes( i : Indexes ) {
 		indexes.remove(i);
 		indexes.remove(i);
@@ -186,7 +190,7 @@ class MemoryManager {
 		i.ibuf = null;
 		i.ibuf = null;
 		usedMemory -= i.count * 2;
 		usedMemory -= i.count * 2;
 	}
 	}
-	
+
 	@:allow(h3d.Indexes)
 	@:allow(h3d.Indexes)
 	function allocIndexes( i : Indexes ) {
 	function allocIndexes( i : Indexes ) {
 		i.ibuf = driver.allocIndexes(i.count);
 		i.ibuf = driver.allocIndexes(i.count);
@@ -194,13 +198,13 @@ class MemoryManager {
 		usedMemory += i.count * 2;
 		usedMemory += i.count * 2;
 	}
 	}
 
 
-	
+
 	// ------------------------------------- TEXTURES ------------------------------------------
 	// ------------------------------------- TEXTURES ------------------------------------------
-	
+
 	function bpp( t : h3d.mat.Texture ) {
 	function bpp( t : h3d.mat.Texture ) {
 		return 4;
 		return 4;
 	}
 	}
-	
+
 	public function cleanTextures( force = true ) {
 	public function cleanTextures( force = true ) {
 		textures.sort(sortByLRU);
 		textures.sort(sortByLRU);
 		for( t in textures ) {
 		for( t in textures ) {
@@ -212,11 +216,11 @@ class MemoryManager {
 		}
 		}
 		return false;
 		return false;
 	}
 	}
-	
+
 	function sortByLRU( t1 : h3d.mat.Texture, t2 : h3d.mat.Texture ) {
 	function sortByLRU( t1 : h3d.mat.Texture, t2 : h3d.mat.Texture ) {
 		return t1.lastFrame - t2.lastFrame;
 		return t1.lastFrame - t2.lastFrame;
 	}
 	}
-	
+
 	@:allow(h3d.mat.Texture.dispose)
 	@:allow(h3d.mat.Texture.dispose)
 	function deleteTexture( t : h3d.mat.Texture ) {
 	function deleteTexture( t : h3d.mat.Texture ) {
 		textures.remove(t);
 		textures.remove(t);
@@ -237,9 +241,9 @@ class MemoryManager {
 		textures.push(t);
 		textures.push(t);
 		texMemory += t.width * t.height * bpp(t);
 		texMemory += t.width * t.height * bpp(t);
 	}
 	}
-	
+
 	// ------------------------------------- DISPOSE ------------------------------------------
 	// ------------------------------------- DISPOSE ------------------------------------------
-	
+
 	public function onContextLost() {
 	public function onContextLost() {
 		dispose();
 		dispose();
 		initIndexes();
 		initIndexes();
@@ -268,7 +272,7 @@ class MemoryManager {
 		usedMemory = 0;
 		usedMemory = 0;
 		texMemory = 0;
 		texMemory = 0;
 	}
 	}
-	
+
 	// ------------------------------------- STATS ------------------------------------------
 	// ------------------------------------- STATS ------------------------------------------
 
 
 	function freeMemorySize() {
 	function freeMemorySize() {
@@ -286,7 +290,7 @@ class MemoryManager {
 		}
 		}
 		return size;
 		return size;
 	}
 	}
-	
+
 	public function stats() {
 	public function stats() {
 		var total = 0, free = 0, count = 0;
 		var total = 0, free = 0, count = 0;
 		for( b in buffers ) {
 		for( b in buffers ) {