ソースを参照

BigPrim: support optional allocator

trethaller 3 年 前
コミット
df88de9bc3
2 ファイル変更25 行追加8 行削除
  1. 19 6
      h3d/prim/BigPrimitive.hx
  2. 6 2
      hxd/impl/Allocator.hx

+ 19 - 6
h3d/prim/BigPrimitive.hx

@@ -21,17 +21,20 @@ class BigPrimitive extends Primitive {
 	var allocPos : hxd.impl.AllocPos;
 	#end
 
+	var allocator : hxd.impl.Allocator;
+
 	public var hasTangents = false;
 	public var isStatic = true;
 
 	static var PREV_BUFFER : hxd.FloatBuffer;
 	static var PREV_INDEX : hxd.IndexBuffer;
 
-	public function new(stride, isRaw=false ) {
+	public function new(stride, isRaw=false, ?alloc) {
 		this.isRaw = isRaw;
 		buffers = [];
 		allIndexes = [];
 		bounds = new h3d.col.Bounds();
+		this.allocator = alloc;
 		this.stride = stride;
 		if( stride < 3 ) throw "Minimum stride = 3";
 		#if track_alloc
@@ -121,10 +124,20 @@ class BigPrimitive extends Primitive {
 		if( tmpBuf != null ) {
 			if( bufPos > 0 && idxPos > 0 ) {
 				flushing = true;
-				var b = h3d.Buffer.ofSubFloats(tmpBuf, stride, Std.int(bufPos / stride));
-				if( isRaw ) b.flags.set(RawFormat);
+				var b : h3d.Buffer;
+				if(allocator != null)
+					b = allocator.ofSubFloats(tmpBuf, stride, Std.int(bufPos / stride), isRaw ? RawFormat : Dynamic);
+				else {
+					b = h3d.Buffer.ofSubFloats(tmpBuf, stride, Std.int(bufPos / stride));
+					if( isRaw ) b.flags.set(RawFormat);
+				}
+
 				buffers.push(b);
-				var idx = h3d.Indexes.alloc(tmpIdx, 0, idxPos);
+				var idx = if(allocator != null)
+					allocator.ofIndexes(tmpIdx, idxPos);
+				else
+					h3d.Indexes.alloc(tmpIdx, 0, idxPos);
+
 				allIndexes.push(idx);
 				flushing = false;
 				#if track_alloc
@@ -165,9 +178,9 @@ class BigPrimitive extends Primitive {
 
 		bounds.empty();
 		for( b in buffers )
-			b.dispose();
+			if(allocator != null) allocator.disposeBuffer(b) else b.dispose();
 		for( i in allIndexes )
-			i.dispose();
+			if(allocator != null) allocator.disposeIndexBuffer(i) else i.dispose();
 		buffers = [];
 		allIndexes = [];
 		bufPos = 0;

+ 6 - 2
hxd/impl/Allocator.hx

@@ -29,8 +29,12 @@ class Allocator {
 
 	public function ofFloats( v : hxd.FloatBuffer, stride : Int, flags : BufferFlags ) {
 		var nvert = Std.int(v.length / stride);
-		var b = allocBuffer(nvert, stride, flags);
-		b.uploadVector(v, 0, nvert);
+		return ofSubFloats(v, stride, nvert, flags);
+	}
+
+	public function ofSubFloats( v : hxd.FloatBuffer, stride : Int, vertices : Int, flags : BufferFlags ) {
+		var b = allocBuffer(vertices, stride, flags);
+		b.uploadVector(v, 0, vertices);
 		return b;
 	}