Browse Source

Fixed buffer Allocator for index buffers

TothBenoit 3 months ago
parent
commit
867d6d069d
3 changed files with 16 additions and 14 deletions
  1. 2 2
      hxd/impl/Allocator.hx
  2. 5 4
      hxd/impl/CacheAllocator.hx
  3. 9 8
      hxd/impl/FIFOBufferAllocator.hx

+ 2 - 2
hxd/impl/Allocator.hx

@@ -61,8 +61,8 @@ class Allocator {
 		b.dispose();
 	}
 
-	public function allocIndexBuffer( count : Int ) {
-		return new h3d.Indexes(count);
+	public function allocIndexBuffer( count : Int, is32 : Bool = false ) {
+		return new h3d.Indexes(count, is32);
 	}
 
 	public function ofIndexes( ib: hxd.IndexBuffer, length = -1) {

+ 5 - 4
hxd/impl/CacheAllocator.hx

@@ -82,8 +82,8 @@ class CacheAllocator extends Allocator {
 		checkGC();
 	}
 
-	override function allocIndexBuffer( count : Int ) {
-		var id = count;
+	override function allocIndexBuffer( count : Int, is32 : Bool = false ) {
+		var id = count << 1 + (is32 ? 1 : 0);
 		checkFrame();
 		var c = indexBuffers.get(id);
 		if( c != null ) {
@@ -91,12 +91,13 @@ class CacheAllocator extends Allocator {
 			if( i != null ) return i;
 		}
 		checkGC();
-		return super.allocIndexBuffer(count);
+		return super.allocIndexBuffer(count, is32);
 	}
 
 	override function disposeIndexBuffer( i : h3d.Indexes ) {
 		if( i.isDisposed() ) return;
-		var id = i.count;
+		var is32 = cast(i, h3d.Buffer).format.strideBytes == 4;
+		var id = i.count << 1 + (is32 ? 1 : 0);
 		var c = indexBuffers.get(id);
 		if( c == null ) {
 			c = new Cache(function(i:h3d.Indexes) i.dispose());

+ 9 - 8
hxd/impl/FIFOBufferAllocator.hx

@@ -97,8 +97,8 @@ class FIFOBufferAllocator extends Allocator {
 		return findCache(buffers, vertices, formatId, flags);
 	}
 
-	function findIndexCache(count : Int) {
-		return findCache(indexBuffers, count, 0, 0);
+	function findIndexCache(count : Int, formatId : Int) {
+		return findCache(indexBuffers, count, formatId, 0);
 	}
 
 	override function allocBuffer(vertices:Int, format:hxd.BufferFormat, flags:BufferFlags=Dynamic):h3d.Buffer {
@@ -127,23 +127,24 @@ class FIFOBufferAllocator extends Allocator {
 		checkGC();
 	}
 
-	override function allocIndexBuffer( count : Int ) {
-		var id = count;
+	override function allocIndexBuffer( count : Int, is32 : Bool = false )  {
 		checkFrame();
-		var c = findIndexCache(count);
+		var formatId = is32 ? hxd.BufferFormat.INDEX32.uid : hxd.BufferFormat.INDEX16.uid;
+		var c = findIndexCache(count, formatId);
 		if( c != null ) {
 			var i = c.get();
 			if( i != null ) return i;
 		}
 		checkGC();
-		return super.allocIndexBuffer(count);
+		return super.allocIndexBuffer(count, is32);
 	}
 
 	override function disposeIndexBuffer( i : h3d.Indexes ) {
 		if( i.isDisposed() ) return;
-		var c = findIndexCache(i.count);
+		var formatId = cast(i, h3d.Buffer).format.uid;
+		var c = findIndexCache(i.count, formatId);
 		if( c == null ) {
-			var config = { vertices : i.count, formatId : 0, flags : 0 };
+			var config = { vertices : i.count, formatId : formatId, flags : 0 };
 			c = new Cache(config, this, maxKeepFrame, function(i:h3d.Indexes) i.dispose());
 			indexBuffers.push(c);
 		}