浏览代码

changed Buffer.mem to Buffer.engine

Nicolas Cannasse 2 年之前
父节点
当前提交
49220f6ebe
共有 3 个文件被更改,包括 13 次插入18 次删除
  1. 7 9
      h3d/Buffer.hx
  2. 3 2
      h3d/impl/GlDriver.hx
  3. 3 7
      h3d/impl/MemoryManager.hx

+ 7 - 9
h3d/Buffer.hx

@@ -23,11 +23,8 @@ class Buffer {
 	var allocPos : hxd.impl.AllocPos;
 	var allocNext : Buffer;
 	#end
-	#if multidriver
-	var driver : h3d.impl.Driver;
-	#end
+	var engine : h3d.Engine;
 
-	var mem : h3d.impl.MemoryManager;
 	@:allow(h3d.impl.Driver) var vbuf : h3d.impl.Driver.GPUBuffer;
 	public var vertices(default,null) : Int;
 	public var format(default,null) : hxd.BufferFormat;
@@ -44,8 +41,9 @@ class Buffer {
 		if( flags != null )
 			for( f in flags )
 				this.flags.set(f);
+		engine = h3d.Engine.getCurrent();
 		if( !this.flags.has(NoAlloc) )
-			@:privateAccess h3d.Engine.getCurrent().mem.allocBuffer(this);
+			@:privateAccess engine.mem.allocBuffer(this);
 	}
 
 	public inline function getMemSize() {
@@ -58,7 +56,7 @@ class Buffer {
 
 	public function dispose() {
 		if( vbuf != null ) {
-			@:privateAccess mem.freeBuffer(this);
+			@:privateAccess engine.mem.freeBuffer(this);
 			vbuf = null;
 		}
 	}
@@ -70,7 +68,7 @@ class Buffer {
 			throw "Can't upload floats on low precision buffer";
 		if( vertices == 0 )
 			return;
-		mem.driver.uploadBufferData(this, startVertice, vertices, buf, bufPos);
+		engine.driver.uploadBufferData(this, startVertice, vertices, buf, bufPos);
 	}
 
 	public function uploadBytes( data : haxe.io.Bytes, dataPos : Int, vertices : Int ) {
@@ -78,13 +76,13 @@ class Buffer {
 			throw "Invalid vertices count";
 		if( vertices == 0 )
 			return;
-		mem.driver.uploadBufferBytes(this, 0, vertices, data, dataPos);
+		engine.driver.uploadBufferBytes(this, 0, vertices, data, dataPos);
 	}
 
 	public function readBytes( bytes : haxe.io.Bytes, bytesPosition : Int, vertices : Int,  startVertice : Int = 0 ) {
 		if( startVertice < 0 || vertices < 0 || startVertice + vertices > this.vertices )
 			throw "Invalid vertices count";
-		mem.driver.readBufferBytes(this, startVertice, vertices, bytes, bytesPosition);
+		engine.driver.readBufferBytes(this, startVertice, vertices, bytes, bytesPosition);
 	}
 
 	public static function ofFloats( v : hxd.FloatBuffer, format : hxd.BufferFormat, ?flags ) {

+ 3 - 2
h3d/impl/GlDriver.hx

@@ -1010,7 +1010,8 @@ class GlDriver extends Driver {
 		gl.bufferData(GL.ARRAY_BUFFER, tmp, b.flags.has(Dynamic) ? GL.DYNAMIC_DRAW : GL.STATIC_DRAW);
 		#end
 		#if multidriver
-		@:privateAccess b.driver = this;
+		@:privateAccess if( b.engine.driver != this )
+			throw "Invalid buffer context";
 		#end
 		var outOfMem = outOfMemoryCheck && gl.getError() == GL.OUT_OF_MEMORY;
 		gl.bindBuffer(GL.ARRAY_BUFFER, null);
@@ -1269,7 +1270,7 @@ class GlDriver extends Driver {
 		if( curShader == null )
 			throw "No shader selected";
 		#if multidriver
-		if( @:privateAccess b.driver != this )
+		if( @:privateAccess b.engine.driver != this )
 			throw "Invalid buffer context";
 		#end
 		gl.bindBuffer(GL.ARRAY_BUFFER, b.vbuf);

+ 3 - 7
h3d/impl/MemoryManager.hx

@@ -125,7 +125,6 @@ class MemoryManager {
 			}
 		}
 		usedMemory += mem;
-		b.mem = this;
 		buffers.push(b);
 	}
 
@@ -133,12 +132,9 @@ class MemoryManager {
 		if( b.vbuf == null ) return;
 		driver.disposeBuffer(b);
 		b.vbuf = null;
-		b.mem = null;
-		#if multidriver
-		b.driver = null;
-		#end
-		usedMemory -= b.getMemSize();
-		buffers.remove(b);
+		// in case it was allocated with a previous memory manager
+		if( buffers.remove(b) )
+			usedMemory -= b.getMemSize();
 	}
 
 	// ------------------------------------- INDEXES ------------------------------------------