Răsfoiți Sursa

refactorized index buffers

ncannasse 11 ani în urmă
părinte
comite
4616a22b97
7 a modificat fișierele cu 30 adăugiri și 22 ștergeri
  1. 2 2
      h2d/Graphics.hx
  2. 3 1
      h3d/Engine.hx
  3. 9 3
      h3d/Indexes.hx
  4. 11 11
      h3d/impl/MemoryManager.hx
  5. 3 3
      h3d/prim/FBXModel.hx
  6. 1 1
      h3d/prim/Polygon.hx
  7. 1 1
      h3d/prim/RawPrimitive.hx

+ 2 - 2
h2d/Graphics.hx

@@ -60,10 +60,10 @@ private class GraphicsContent extends h3d.prim.Primitive {
 	override function alloc( engine : h3d.Engine ) {
 		if (index.length <= 0) return ;
 		buffer = h3d.Buffer.ofFloats(tmp, 8);
-		indexes = engine.mem.allocIndex(index);
+		indexes = h3d.Indexes.alloc(index);
 		for( b in buffers ) {
 			if( b.vbuf == null || b.vbuf.isDisposed() ) b.vbuf = h3d.Buffer.ofFloats(b.buf, 8);
-			if( b.ibuf == null || b.ibuf.isDisposed() ) b.ibuf = engine.mem.allocIndex(b.idx);
+			if( b.ibuf == null || b.ibuf.isDisposed() ) b.ibuf = h3d.Indexes.alloc(b.idx);
 		}
 	}
 	

+ 3 - 1
h3d/Engine.hx

@@ -193,8 +193,10 @@ class Engine {
 		}
 		if( disposed )
 			mem.onContextLost();
-		else
+		else {
 			mem = new h3d.impl.MemoryManager(driver);
+			mem.init();
+		}
 		hardware = driver.isHardware();
 		set_debug(debug);
 		set_fullScreen(fullScreen);

+ 9 - 3
h3d/Indexes.hx

@@ -8,10 +8,10 @@ class Indexes {
 	var ibuf : h3d.impl.Driver.IndexBuffer;
 	public var count(default,null) : Int;
 	
-	function new(mem, ibuf, count) {
-		this.mem = mem;
-		this.ibuf = ibuf;
+	public function new(count) {
+		this.mem = h3d.Engine.getCurrent().mem;
 		this.count = count;
+		mem.allocIndexes(this);
 	}
 	
 	public function isDisposed() {
@@ -26,5 +26,11 @@ class Indexes {
 		if( ibuf != null )
 			mem.deleteIndexes(this);
 	}
+	
+	public static function alloc( i : hxd.IndexBuffer ) {
+		var idx = new Indexes( i.length );
+		idx.upload(i, 0, i.length);
+		return idx;
+	}
 
 }

+ 11 - 11
h3d/impl/MemoryManager.hx

@@ -21,6 +21,9 @@ class MemoryManager {
 
 	public function new(driver) {
 		this.driver = driver;
+	}
+	
+	public function init() {
 		indexes = new Array();
 		textures = new Array();
 		buffers = new Array();
@@ -30,7 +33,7 @@ class MemoryManager {
 	function initIndexes() {
 		var indices = new hxd.IndexBuffer();
 		for( i in 0...SIZE ) indices.push(i);
-		triIndexes = allocIndex(indices);
+		triIndexes = h3d.Indexes.alloc(indices);
 
 		var indices = new hxd.IndexBuffer();
 		var p = 0;
@@ -44,7 +47,7 @@ class MemoryManager {
 			indices.push(k + 3);
 		}
 		indices.push(SIZE);
-		quadIndexes = allocIndex(indices);
+		quadIndexes = h3d.Indexes.alloc(indices);
 	}
 
 	/**
@@ -176,7 +179,7 @@ class MemoryManager {
 
 	// ------------------------------------- INDEXES ------------------------------------------
 	
-	@:allow(h3d.Indexes.dispose)
+	@:allow(h3d.Indexes)
 	function deleteIndexes( i : Indexes ) {
 		indexes.remove(i);
 		driver.disposeIndexes(i.ibuf);
@@ -184,14 +187,11 @@ class MemoryManager {
 		usedMemory -= i.count * 2;
 	}
 	
-	public function allocIndex( indices : hxd.IndexBuffer, pos = 0, count = -1 ) {
-		if( count < 0 ) count = indices.length;
-		var ibuf = driver.allocIndexes(count);
-		var idx = new Indexes(this, ibuf, count);
-		idx.upload(indices, 0, count);
-		indexes.push(idx);
-		usedMemory += idx.count * 2;
-		return idx;
+	@:allow(h3d.Indexes)
+	function allocIndexes( i : Indexes ) {
+		i.ibuf = driver.allocIndexes(i.count);
+		indexes.push(i);
+		usedMemory += i.count * 2;
 	}
 
 	

+ 3 - 3
h3d/prim/FBXModel.hx

@@ -195,16 +195,16 @@ class FBXModel extends MeshPrimitive {
 		}
 		if( cbuf != null ) addBuffer("color", h3d.Buffer.ofFloats(cbuf, 3));
 		
-		indexes = engine.mem.allocIndex(idx);
+		indexes = h3d.Indexes.alloc(idx);
 		if( mats != null ) {
 			groupIndexes = [];
 			for( i in midx )
-				groupIndexes.push(i == null ? null : engine.mem.allocIndex(i));
+				groupIndexes.push(i == null ? null : h3d.Indexes.alloc(i));
 		}
 		if( sidx != null ) {
 			groupIndexes = [];
 			for( i in sidx )
-				groupIndexes.push(i == null ? null : engine.mem.allocIndex(i));
+				groupIndexes.push(i == null ? null : h3d.Indexes.alloc(i));
 		}
 	}
 	

+ 1 - 1
h3d/prim/Polygon.hx

@@ -59,7 +59,7 @@ class Polygon extends Primitive {
 		buffer = h3d.Buffer.ofFloats(buf, size, idx == null ? [Triangles] : null);
 		
 		if( idx != null )
-			indexes = engine.mem.allocIndex(idx);
+			indexes = h3d.Indexes.alloc(idx);
 	}
 
 

+ 1 - 1
h3d/prim/RawPrimitive.hx

@@ -5,7 +5,7 @@ class RawPrimitive extends Primitive {
 	public function new( engine : h3d.Engine, vbuf : hxd.FloatBuffer, stride : Int, ?ibuf : hxd.IndexBuffer ) {
 		buffer = h3d.Buffer.ofFloats(vbuf, stride, ibuf == null ? [Triangles] : null);
 		if( ibuf != null )
-			indexes = engine.mem.allocIndex(ibuf);
+			indexes = h3d.Indexes.alloc(ibuf);
 	}
 
 }