소스 검색

upload buffers in sync

Mathieu Capdegelle 9 년 전
부모
커밋
f87c162a5d
2개의 변경된 파일28개의 추가작업 그리고 5개의 파일을 삭제
  1. 10 2
      h2d/Graphics.hx
  2. 18 3
      h2d/SpriteBatch.hx

+ 10 - 2
h2d/Graphics.hx

@@ -69,12 +69,16 @@ private class GraphicsContent extends h3d.prim.Primitive {
 
 	override function render( engine : h3d.Engine ) {
 		if (index.length <= 0) return ;
-		if( buffer == null || buffer.isDisposed() ) alloc(engine);
+		flush();
 		for( b in buffers )
 			engine.renderIndexed(b.vbuf, b.ibuf);
 		super.render(engine);
 	}
 
+	public inline function flush() {
+		if( buffer == null || buffer.isDisposed() ) alloc(h3d.Engine.getCurrent());
+	}
+
 	override function dispose() {
 		for( b in buffers ) {
 			if( b.vbuf != null ) b.vbuf.dispose();
@@ -461,4 +465,8 @@ class Graphics extends Drawable {
 		content.render(ctx.engine);
 	}
 
-}
+	override function sync(ctx:RenderContext) {
+		super.sync(ctx);
+		content.flush();
+	}
+}

+ 18 - 3
h2d/SpriteBatch.hx

@@ -94,6 +94,7 @@ class SpriteBatch extends Drawable {
 	var first : BatchElement;
 	var last : BatchElement;
 	var tmpBuf : hxd.FloatBuffer;
+	var buffer : h3d.Buffer;
 
 	public function new(t,?parent) {
 		super(parent);
@@ -143,6 +144,7 @@ class SpriteBatch extends Drawable {
 				e = e.next;
 			}
 		}
+		flush();
 	}
 
 	override function getBoundsRec( relativeTo, out, forSize ) {
@@ -180,7 +182,11 @@ class SpriteBatch extends Drawable {
 		}
 	}
 
-	override function draw( ctx : RenderContext ) {
+	function flush() {
+		if( buffer != null ) {
+			buffer.dispose();
+			buffer = null;
+		}
 		if( first == null )
 			return;
 		if( tmpBuf == null ) tmpBuf = new hxd.FloatBuffer();
@@ -271,10 +277,13 @@ class SpriteBatch extends Drawable {
 			}
 			e = e.next;
 		}
-		var buffer = h3d.Buffer.ofSubFloats(tmpBuf, 8, Std.int(pos/8), [Dynamic, Quads, RawFormat]);
+		buffer = h3d.Buffer.ofSubFloats(tmpBuf, 8, Std.int(pos/8), [Dynamic, Quads, RawFormat]);
+	}
+
+	override function draw( ctx : RenderContext ) {
+		if( first == null || buffer == null || buffer.isDisposed() ) return;
 		ctx.beginDrawObject(this, tile.getTexture());
 		ctx.engine.renderQuadBuffer(buffer);
-		buffer.dispose();
 	}
 
 	public inline function isEmpty() {
@@ -285,4 +294,10 @@ class SpriteBatch extends Drawable {
 		return new ElementsIterator(first);
 	}
 
+	override function onDelete()  {
+		if( buffer != null ) {
+			buffer.dispose();
+			buffer = null;
+		}
+	}
 }