Pārlūkot izejas kodu

added Buffer RawFormat (default [pos,normal,uv] prefix)

Nicolas Cannasse 11 gadi atpakaļ
vecāks
revīzija
db36fe23f1

+ 2 - 2
h2d/Graphics.hx

@@ -59,10 +59,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);
+		buffer = h3d.Buffer.ofFloats(tmp, 8, [RawFormat]);
 		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.vbuf == null || b.vbuf.isDisposed() ) b.vbuf = h3d.Buffer.ofFloats(b.buf, 8, [RawFormat]);
 			if( b.ibuf == null || b.ibuf.isDisposed() ) b.ibuf = h3d.Indexes.alloc(b.idx);
 		}
 	}

+ 1 - 1
h2d/RenderContext.hx

@@ -65,7 +65,7 @@ class RenderContext {
 		if( bufPos == 0 ) return;
 		beforeDraw();
 		var nverts = Std.int(bufPos / stride);
-		var tmp = new h3d.Buffer(nverts, stride, [Quads, Dynamic]);
+		var tmp = new h3d.Buffer(nverts, stride, [Quads, Dynamic, RawFormat]);
 		tmp.uploadVector(buffer, 0, nverts);
 		engine.renderQuadBuffer(tmp);
 		tmp.dispose();

+ 1 - 1
h2d/SpriteBatch.hx

@@ -233,7 +233,7 @@ class SpriteBatch extends Drawable {
 			}
 			e = e.next;
 		}
-		var buffer = h3d.Buffer.ofFloats(tmpBuf, 8, [Dynamic, Quads]);
+		var buffer = h3d.Buffer.ofFloats(tmpBuf, 8, [Dynamic, Quads, RawFormat]);
 		ctx.beginDrawObject(this, tile.getTexture());
 		ctx.engine.renderQuadBuffer(buffer);
 		buffer.dispose();

+ 1 - 1
h2d/TileGroup.hx

@@ -139,7 +139,7 @@ private class TileLayerContent extends h3d.prim.Primitive {
 
 	override public function alloc(engine:h3d.Engine) {
 		if( tmp == null ) reset();
-		buffer = h3d.Buffer.ofFloats(tmp, 8, [Quads]);
+		buffer = h3d.Buffer.ofFloats(tmp, 8, [Quads, RawFormat]);
 	}
 
 	public function doRender(engine, min, len) {

+ 13 - 3
h3d/Buffer.hx

@@ -17,6 +17,10 @@ enum BufferFlag {
 		Will allocate the memory as part of an shared buffer pool, preventing a lot of small GPU buffers to be allocated.
 	**/
 	Managed;
+	/**
+		Directly map the buffer content to the shader inputs, without assuming [pos:vec3,normal:vec3,uv:vec2] default prefix.
+	**/
+	RawFormat;
 	/**
 		Used internaly
 	**/
@@ -26,6 +30,9 @@ enum BufferFlag {
 class Buffer {
 	public static var GUID = 0;
 	public var id : Int;
+	#if debug
+	var allocPos : h3d.impl.AllocPos;
+	#end
 
 	public var buffer(default,null) : h3d.impl.ManagedBuffer;
 	public var position(default,null) : Int;
@@ -33,10 +40,13 @@ class Buffer {
 	public var next(default,null) : Buffer;
 	public var flags(default, null) : haxe.EnumFlags<BufferFlag>;
 
-	public function new(vertices, stride, ?flags : Array<BufferFlag>) {
+	public function new(vertices, stride, ?flags : Array<BufferFlag>, ?allocPos : h3d.impl.AllocPos ) {
 		id = GUID++;
 		this.vertices = vertices;
 		this.flags = new haxe.EnumFlags();
+		#if debug
+		this.allocPos = allocPos;
+		#end
 		if( flags != null )
 			for( f in flags )
 				this.flags.set(f);
@@ -95,9 +105,9 @@ class Buffer {
 		}
 	}
 
-	public static function ofFloats( v : hxd.FloatBuffer, stride : Int, ?flags, ?vertices ) {
+	public static function ofFloats( v : hxd.FloatBuffer, stride : Int, ?flags, ?vertices, ?allocPos ) {
 		var nvert = vertices == null ? Std.int(v.length / stride) : vertices;
-		var b = new Buffer(nvert, stride, flags);
+		var b = new Buffer(nvert, stride, flags, allocPos);
 		b.uploadVector(v, 0, nvert);
 		return b;
 	}

+ 4 - 4
h3d/Engine.hx

@@ -102,10 +102,10 @@ class Engine {
 		driver.uploadShaderBuffers(buffers, which);
 	}
 
-	function selectBuffer( buf : h3d.impl.ManagedBuffer ) {
+	function selectBuffer( buf : Buffer ) {
 		if( buf.isDisposed() )
 			return false;
-		driver.selectBuffer(@:privateAccess buf.vbuf);
+		driver.selectBuffer(buf);
 		return true;
 	}
 
@@ -142,7 +142,7 @@ class Engine {
 					drawTri = 0;
 				}
 			}
-			if( ntri > 0 && selectBuffer(b.buffer) ) {
+			if( ntri > 0 && selectBuffer(b) ) {
 				// *3 because it's the position in indexes which are always by 3
 				driver.draw(indexes.ibuf, pos * 3, ntri);
 				drawTriangles += ntri;
@@ -160,7 +160,7 @@ class Engine {
 			return;
 		var maxTri = Std.int(indexes.count / 3);
 		if( drawTri < 0 ) drawTri = maxTri - startTri;
-		if( drawTri > 0 && selectBuffer(b.buffer) ) {
+		if( drawTri > 0 && selectBuffer(b) ) {
 			// *3 because it's the position in indexes which are always by 3
 			driver.draw(indexes.ibuf, startTri * 3, drawTri);
 			drawTriangles += drawTri;

+ 1 - 1
h3d/impl/Driver.hx

@@ -89,7 +89,7 @@ class Driver {
 		return null;
 	}
 
-	public function selectBuffer( buffer : VertexBuffer ) {
+	public function selectBuffer( buffer : Buffer ) {
 	}
 
 	public function selectMultiBuffers( buffers : Buffer.BufferOffset ) {

+ 1 - 1
h3d/impl/LogDriver.hx

@@ -205,7 +205,7 @@ class LogDriver extends Driver {
 		return d.getShaderInputNames();
 	}
 
-	override function selectBuffer( buffer : VertexBuffer ) {
+	override function selectBuffer( buffer : Buffer ) {
 		log('SelectBuffer');
 		d.selectBuffer(buffer);
 	}

+ 42 - 11
h3d/impl/Stage3dDriver.hx

@@ -58,7 +58,7 @@ class Stage3dDriver extends Driver {
 
 	var curMatBits : Int;
 	var curShader : CompiledShader;
-	var curBuffer : VertexBuffer;
+	var curBuffer : Buffer;
 	var curMultiBuffer : Array<Int>;
 	var curAttributes : Int;
 	var curTextures : Array<h3d.mat.Texture>;
@@ -82,6 +82,7 @@ class Stage3dDriver extends Driver {
 		programs = new Map();
 		curTextures = [];
 		curMultiBuffer = [];
+		logEnable = true;
 	}
 
 	override function logImpl( str : String ) {
@@ -487,24 +488,54 @@ class Stage3dDriver extends Driver {
 		}
 	}
 
-	override function selectBuffer( v : VertexBuffer ) {
+	@:access(h3d.impl.ManagedBuffer)
+	override function selectBuffer( v : Buffer ) {
 		if( v == curBuffer )
 			return;
+		if( curBuffer != null && v.buffer == curBuffer.buffer && v.buffer.flags.has(RawFormat) == curBuffer.flags.has(RawFormat) ) {
+			curBuffer = v;
+			return;
+		}
 		if( curShader == null )
 			throw "No shader selected";
 		curBuffer = v;
 		curMultiBuffer[0] = -1;
-		if( v.b.stride < curShader.stride )
-			throw "Buffer stride (" + v.b.stride + ") and shader stride (" + curShader.stride + ") mismatch";
-		if( !v.written )
-			v.finalize(this);
+
+		var m = v.buffer.vbuf;
+		if( m.b.stride < curShader.stride )
+			throw "Buffer stride (" + m.b.stride + ") and shader stride (" + curShader.stride + ") mismatch";
+		if( !m.written )
+			m.finalize(this);
 		var pos = 0, offset = 0;
 		var bits = curShader.bufferFormat;
-		while( offset < curShader.stride ) {
-			var size = bits & 7;
-			ctx.setVertexBufferAt(pos++, v.vbuf, offset, FORMAT[size]);
-			offset += size == 0 ? 1 : size;
-			bits >>= 3;
+		if( v.flags.has(RawFormat) ) {
+			while( offset < curShader.stride ) {
+				var size = bits & 7;
+				ctx.setVertexBufferAt(pos++, m.vbuf, offset, FORMAT[size]);
+				offset += size == 0 ? 1 : size;
+				bits >>= 3;
+			}
+		} else {
+			offset = 8; // custom data starts after [position, normal, uv]
+			for( s in curShader.inputNames ) {
+				switch( s ) {
+				case "position":
+					ctx.setVertexBufferAt(pos++, m.vbuf, 0, FORMAT[3]);
+				case "normal":
+					if( m.b.stride < 6 ) throw "Buffer is missing NORMAL data, set it to RAW format ?" #if debug + @:privateAccess v.allocPos #end;
+					ctx.setVertexBufferAt(pos++, m.vbuf, 3, FORMAT[3]);
+				case "uv":
+					if( m.b.stride < 8 ) throw "Buffer is missing UV data, set it to RAW format ?" #if debug + @:privateAccess v.allocPos #end;
+					ctx.setVertexBufferAt(pos++, m.vbuf, 6, FORMAT[2]);
+				default:
+					var size = bits & 7;
+					ctx.setVertexBufferAt(pos++, m.vbuf, offset, FORMAT[size]);
+					offset += size == 0 ? 1 : size;
+					if( offset > m.b.stride ) throw "Buffer is missing '"+s+"' data, set it to RAW format ?" #if debug + @:privateAccess v.allocPos #end;
+					bits >>= 3;
+				}
+				bits >>= 3;
+			}
 		}
 		for( i in pos...curAttributes )
 			ctx.setVertexBufferAt(i, null);

+ 1 - 1
h3d/parts/Emitter.hx

@@ -558,7 +558,7 @@ class Emitter extends h3d.scene.Object implements Randomized {
 		}
 		var stride = 10;
 		if( hasColor ) stride += 4;
-		var buffer = h3d.Buffer.ofFloats(tmp, stride, [Quads, Dynamic], Std.int(pos/stride));
+		var buffer = h3d.Buffer.ofFloats(tmp, stride, [Quads, Dynamic, RawFormat], Std.int(pos/stride));
 		var size = eval(state.globalSize);
 
 		/*

+ 3 - 1
h3d/prim/BigPrimitive.hx

@@ -51,7 +51,9 @@ class BigPrimitive extends Primitive {
 	public function flush() {
 		if( tmpBuf != null ) {
 			if( tmpBuf.length > 0 && tmpIdx.length > 0 ) {
-				buffers.push(h3d.Buffer.ofFloats(tmpBuf, stride));
+				var b = h3d.Buffer.ofFloats(tmpBuf, stride);
+				if( stride < 8 ) b.flags.set(RawFormat);
+				buffers.push(b);
 				allIndexes.push(h3d.Indexes.alloc(tmpIdx));
 			}
 			tmpBuf = null;

+ 1 - 1
h3d/prim/Plan2D.hx

@@ -27,7 +27,7 @@ class Plan2D extends Primitive {
 		v.push( 1);
 		v.push( 0);
 
-		buffer = h3d.Buffer.ofFloats(v, 4, [Quads]);
+		buffer = h3d.Buffer.ofFloats(v, 4, [Quads, RawFormat]);
 	}
 
 	override function render(engine:h3d.Engine) {

+ 0 - 84
h3d/prim/Plan3D.hx

@@ -1,84 +0,0 @@
-package h3d.prim;
-
-class Plan3D extends Primitive {
-
-	public var width(default,set) : Float;
-	public var height(default,set) : Float;
-
-	public function new(width = 1.0, height = 1.0) {
-		this.width = width;
-		this.height = height;
-	}
-
-	function set_width(w) {
-		width = w;
-		dispose();
-		return w;
-	}
-
-	function set_height(h) {
-		height = h;
-		dispose();
-		return h;
-	}
-
-	override function getBounds() {
-		var b = new h3d.col.Bounds();
-		b.xMin = 0;
-		b.xMax = width;
-		b.yMin = 0;
-		b.yMax = height;
-		b.zMin = b.zMax = 0;
-		return b;
-	}
-
-	override function alloc( engine : h3d.Engine ) {
-		var v = new hxd.FloatBuffer();
-		var hw = width * 0.5, hh = height * 0.5;
-		v.push( -hw);
-		v.push( -hh);
-		v.push( 0);
-		v.push( 0);
-		v.push( 1);
-
-		v.push( 0);
-		v.push( 0);
-		v.push( 1);
-
-		v.push( -hw);
-		v.push( hh);
-		v.push( 0);
-		v.push( 0);
-		v.push( 0);
-
-		v.push( 0);
-		v.push( 0);
-		v.push( 1);
-
-		v.push( hw);
-		v.push( -hh);
-		v.push( 1);
-		v.push( 1);
-
-		v.push( 0);
-		v.push( 0);
-		v.push( 1);
-
-		v.push( hw);
-		v.push( hh);
-		v.push( 1);
-		v.push( 0);
-
-		v.push( 0);
-		v.push( 0);
-		v.push( 1);
-
-		buffer = h3d.Buffer.ofFloats(v, 8, [Quads]);
-	}
-
-	override function render(engine:h3d.Engine) {
-		if( buffer == null ) alloc(engine);
-		engine.renderQuadBuffer(buffer);
-	}
-
-}

+ 9 - 6
h3d/prim/Polygon.hx

@@ -38,17 +38,17 @@ class Polygon extends Primitive {
 			buf.push(p.x);
 			buf.push(p.y);
 			buf.push(p.z);
-			if( uvs != null ) {
-				var t = uvs[k];
-				buf.push(t.u);
-				buf.push(t.v);
-			}
 			if( normals != null ) {
 				var n = normals[k];
 				buf.push(n.x);
 				buf.push(n.y);
 				buf.push(n.z);
 			}
+			if( uvs != null ) {
+				var t = uvs[k];
+				buf.push(t.u);
+				buf.push(t.v);
+			}
 			if( colors != null ) {
 				var c = colors[k];
 				buf.push(c.x);
@@ -56,7 +56,10 @@ class Polygon extends Primitive {
 				buf.push(c.z);
 			}
 		}
-		buffer = h3d.Buffer.ofFloats(buf, size, idx == null ? [Triangles] : null);
+		var flags = [];
+		if( idx == null ) flags.push(Triangles);
+		if( normals == null ) flags.push(RawFormat);
+		buffer = h3d.Buffer.ofFloats(buf, size, flags);
 
 		if( idx != null )
 			indexes = h3d.Indexes.alloc(idx);

+ 8 - 6
h3d/prim/Quads.hx

@@ -47,22 +47,24 @@ class Quads extends Primitive {
 			v.push(pt.x);
 			v.push(pt.y);
 			v.push(pt.z);
-			if( uvs != null ) {
-				var t = uvs[i];
-				v.push(t.u);
-				v.push(t.v);
-			}
 			if( normals != null ) {
 				var n = normals[i];
 				v.push(n.x);
 				v.push(n.y);
 				v.push(n.z);
 			}
+			if( uvs != null ) {
+				var t = uvs[i];
+				v.push(t.u);
+				v.push(t.v);
+			}
 		}
 		var size = 3;
 		if( normals != null ) size += 3;
 		if( uvs != null ) size += 2;
-		buffer = h3d.Buffer.ofFloats(v, size, [Quads]);
+		var flags = [Quads];
+		if( normals == null ) flags.push(RawFormat);
+		buffer = h3d.Buffer.ofFloats(v, size, flags);
 	}
 
 	public function getPoints() {

+ 4 - 1
h3d/prim/RawPrimitive.hx

@@ -3,7 +3,10 @@ package h3d.prim;
 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);
+		var flags = [];
+		if( ibuf == null ) flags.push(Triangles);
+		if( stride < 8 ) flags.push(RawFormat);
+		buffer = h3d.Buffer.ofFloats(vbuf, stride, flags);
 		if( ibuf != null )
 			indexes = h3d.Indexes.alloc(ibuf);
 	}