Просмотр исходного кода

MeshBatch : Avoid reallocating InstanceBuffer

TothBenoit 4 месяцев назад
Родитель
Сommit
a7946683b3
2 измененных файлов с 11 добавлено и 10 удалено
  1. 2 1
      h3d/impl/InstanceBuffer.hx
  2. 9 9
      h3d/scene/MeshBatch.hx

+ 2 - 1
h3d/impl/InstanceBuffer.hx

@@ -24,6 +24,7 @@ class InstanceBuffer {
 	var startIndex : Int;
 	public var triCount(default,null) : Int = 0;
 	public var commandCount(default, null) : Int;
+	public var maxCommandCount(default, null) : Int;
 
 	public function new() {
 	}
@@ -58,7 +59,7 @@ class InstanceBuffer {
 		dispose();
 
 		updateTriCount(commandCount, bytes);
-		this.commandCount = commandCount;
+		this.commandCount = this.maxCommandCount = commandCount;
 		this.indexCount = 0;
 		driver = h3d.Engine.getCurrent().driver;
 		driver.allocInstanceBuffer(this, bytes);

+ 9 - 9
h3d/scene/MeshBatch.hx

@@ -107,6 +107,10 @@ class MeshBatch extends MultiMaterial {
 	function hasPrimitiveOffset() return meshBatchFlags.has(HasPrimitiveOffset);
 	function cpuLodEnabled() return meshBatchFlags.has(EnableCpuLod);
 
+	inline function shouldResizeDown( currentSize : Int, minSize : Int ) : Bool {
+		return meshBatchFlags.has(EnableResizeDown) && currentSize > minSize << 1;
+	}
+
 	public function begin( emitCountTip = -1 ) : Int {
 		instanceCount = 0;
 		instanced.initBounds();
@@ -121,7 +125,7 @@ class MeshBatch extends MultiMaterial {
 		var alloc = hxd.impl.Allocator.get();
 		while( p != null ) {
 			var size = emitCountTip * p.paramsCount * 4;
-			if( p.data == null || p.data.length < size || ( meshBatchFlags.has(EnableResizeDown) && p.data.length > size << 1) ) {
+			if( p.data == null || p.data.length < size || shouldResizeDown(p.data.length, size) ) {
 				if( p.data != null ) alloc.disposeFloats(p.data);
 				p.data = alloc.allocFloats(size);
 			}
@@ -347,13 +351,9 @@ class MeshBatch extends MultiMaterial {
 					if( p.instanceBuffers == null )
 						p.instanceBuffers = [];
 					var ibuf = p.instanceBuffers[index];
-					if ( ibuf != null && ibuf.commandCount != count ) {
-						ibuf.dispose();
-						ibuf = null;
-					}
-					var ibufUpload = needUpload || ibuf == null;
 					if ( ibuf == null )
 						ibuf = new h3d.impl.InstanceBuffer();
+					var ibufUpload = needUpload || ibuf.commandCount != count;
 					if ( ibufUpload ) {
 						var psBytes = primitiveSubBytes[p.matIndex];
 						if ( start > 0 && count < instanceCount ) {
@@ -362,10 +362,10 @@ class MeshBatch extends MultiMaterial {
 								psBytes.setInt32(i*instanceSize+16, i);
 						}
 
-						if(count <= ibuf.commandCount && !meshBatchFlags.has(EnableResizeDown)){
-							ibuf.uploadBytes(count, psBytes);
-						} else {
+						if ( shouldResizeDown(ibufMaxCommandCount, count) || count > ibufMaxCommandCount) {
 							ibuf.allocFromBytes(count, psBytes);
+						} else {
+							ibuf.uploadBytes(count, psBytes);
 						}
 						p.instanceBuffers[index] = ibuf;
 					}