Explorar o código

removed Buffer Quads/Triangles, use MemoryManager.getTriIndexes/getQuadIndexes

Nicolas Cannasse %!s(int64=2) %!d(string=hai) anos
pai
achega
da0b8be861

+ 3 - 3
h2d/RenderContext.hx

@@ -325,7 +325,7 @@ class RenderContext extends h3d.impl.RenderContext {
 
 	/**
 		Retrieves the current filter scale factor.
-		
+
 		@param into The 2D Point instance into which the scale is written. Creates a new Point if null.
 		@returns The current filter resolution scale or `{ 1, 1 }` point.
 	**/
@@ -580,7 +580,7 @@ class RenderContext extends h3d.impl.RenderContext {
 		if( bufPos == 0 ) return;
 		beforeDraw();
 		var nverts = Std.int(bufPos / stride);
-		var tmp = new h3d.Buffer(nverts, stride, [Quads,Dynamic,RawFormat]);
+		var tmp = new h3d.Buffer(nverts, stride, [Dynamic,RawFormat]);
 		tmp.uploadVector(buffer, 0, nverts);
 		engine.renderQuadBuffer(tmp);
 		tmp.dispose();
@@ -750,7 +750,7 @@ class RenderContext extends h3d.impl.RenderContext {
 		baseShader.uvPos.set(tile.u, tile.v, tile.u2 - tile.u, tile.v2 - tile.v);
 		beforeDraw();
 		if( fixedBuffer == null || fixedBuffer.isDisposed() ) {
-			fixedBuffer = new h3d.Buffer(4, 8, [Quads, RawFormat]);
+			fixedBuffer = new h3d.Buffer(4, 8, [RawFormat]);
 			var k = new hxd.FloatBuffer();
 			for( v in [0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] )
 				k.push(v);

+ 1 - 1
h2d/SpriteBatch.hx

@@ -438,7 +438,7 @@ class SpriteBatch extends Drawable {
 		}
 		empty = bufferVertices == 0;
 		if( bufferVertices > 0 )
-			buffer = h3d.Buffer.ofSubFloats(tmpBuf, 8, bufferVertices, [Dynamic, Quads, RawFormat]);
+			buffer = h3d.Buffer.ofSubFloats(tmpBuf, 8, bufferVertices, [Dynamic, RawFormat]);
 	}
 
 	override function draw( ctx : RenderContext ) {

+ 2 - 2
h2d/TileGroup.hx

@@ -511,8 +511,8 @@ class TileLayerContent extends h3d.prim.Primitive {
 		if( tmp == null ) clear();
 		if( tmp.length > 0 ) {
 			buffer = tmp.length < useAllocatorLimit
-				? hxd.impl.Allocator.get().ofFloats(tmp, 8, RawQuads)
-				: h3d.Buffer.ofFloats(tmp, 8, [Quads, RawFormat]);
+				? hxd.impl.Allocator.get().ofFloats(tmp, 8, RawFormat)
+				: h3d.Buffer.ofFloats(tmp, 8, [RawFormat]);
 		}
 	}
 

+ 0 - 8
h3d/Buffer.hx

@@ -5,14 +5,6 @@ enum BufferFlag {
 		Indicate that the buffer content will be often modified.
 	**/
 	Dynamic;
-	/**
-		The buffer contains only triangles. Imply Managed. Make sure the position is aligned on 3 vertices multiples.
-	**/
-	Triangles;
-	/**
-		The buffer contains only quads. Imply Managed. Make sure the position is aligned on 4 vertices multiples.
-	**/
-	Quads;
 	/**
 		Directly map the buffer content to the shader inputs, without assuming [pos:vec3,normal:vec3,uv:vec2] default prefix.
 	**/

+ 2 - 2
h3d/Engine.hx

@@ -142,11 +142,11 @@ class Engine {
 	}
 
 	public inline function renderTriBuffer( b : Buffer, start = 0, max = -1 ) {
-		return renderBuffer(b, mem.triIndexes, 3, start, max);
+		return renderBuffer(b, mem.getTriIndexes(b.vertices), 3, start, max);
 	}
 
 	public inline function renderQuadBuffer( b : Buffer, start = 0, max = -1 ) {
-		return renderBuffer(b, mem.quadIndexes, 2, start, max);
+		return renderBuffer(b, mem.getQuadIndexes(b.vertices), 2, start, max);
 	}
 
 	// we use preallocated indexes so all the triangles are stored inside our buffers

+ 30 - 8
h3d/impl/MemoryManager.hx

@@ -14,8 +14,10 @@ class MemoryManager {
 	var textures : Array<h3d.mat.Texture>;
 	var depths : Array<h3d.mat.DepthBuffer>;
 
-	public var triIndexes(default,null) : Indexes;
-	public var quadIndexes(default,null) : Indexes;
+	var triIndexes16 : Indexes;
+	var quadIndexes16 : Indexes;
+	var triIndexes32 : Indexes;
+	var quadIndexes32 : Indexes;
 	public var usedMemory(default, null) : Float = 0;
 	public var texMemory(default, null) : Float = 0;
 
@@ -34,7 +36,7 @@ class MemoryManager {
 	function initIndexes() {
 		var indices = new hxd.IndexBuffer();
 		for( i in 0...SIZE ) indices.push(i);
-		triIndexes = h3d.Indexes.alloc(indices);
+		triIndexes16 = h3d.Indexes.alloc(indices);
 
 		var indices = new hxd.IndexBuffer();
 		var p = 0;
@@ -48,7 +50,7 @@ class MemoryManager {
 			indices.push(k + 3);
 		}
 		indices.push(SIZE);
-		quadIndexes = h3d.Indexes.alloc(indices);
+		quadIndexes16 = h3d.Indexes.alloc(indices);
 	}
 
 	/**
@@ -58,6 +60,22 @@ class MemoryManager {
 	public dynamic function garbage() {
 	}
 
+	public function getTriIndexes( vertices : Int ) {
+		if( vertices <= SIZE )
+			return triIndexes16;
+		if( triIndexes32 == null || triIndexes32.count < vertices )
+			throw "TODO";
+		return triIndexes32;
+	}
+
+	public function getQuadIndexes( vertices : Int ) {
+		if( vertices <= SIZE )
+			return quadIndexes16;
+		if( quadIndexes32 == null || quadIndexes32.count < vertices )
+			throw "TODO";
+		return quadIndexes32;
+	}
+
 	// ------------------------------------- BUFFERS ------------------------------------------
 
 	function allocBuffer( b : Buffer ) {
@@ -197,10 +215,14 @@ class MemoryManager {
 	}
 
 	public function dispose() {
-		if( triIndexes != null ) triIndexes.dispose();
-		if( quadIndexes != null ) quadIndexes.dispose();
-		triIndexes = null;
-		quadIndexes = null;
+		if( triIndexes16 != null ) triIndexes16.dispose();
+		if( quadIndexes16 != null ) quadIndexes16.dispose();
+		if( triIndexes32 != null ) triIndexes32.dispose();
+		if( quadIndexes32 != null ) quadIndexes32.dispose();
+		triIndexes16 = null;
+		quadIndexes16 = null;
+		triIndexes32 = null;
+		quadIndexes32 = null;
 		for( t in textures.copy() )
 			t.dispose();
 		for( b in depths.copy() )

+ 1 - 1
h3d/parts/Particles.hx

@@ -364,7 +364,7 @@ class Particles extends h3d.scene.Mesh {
 		}
 		var stride = 10;
 		if( hasColor ) stride += 4;
-		var buffer = h3d.Buffer.ofSubFloats(tmp, stride, Std.int(pos/stride), [Quads, Dynamic, RawFormat]);
+		var buffer = h3d.Buffer.ofSubFloats(tmp, stride, Std.int(pos/stride), [Dynamic, RawFormat]);
 		if( pshader.is3D )
 			pshader.size.set(globalSize, globalSize);
 		else

+ 1 - 1
h3d/prim/Instanced.hx

@@ -26,7 +26,7 @@ class Instanced extends MeshPrimitive {
 		buffer = m.buffer;
 		indexes = m.indexes;
 		baseBounds = m.getBounds();
-		if( indexes == null ) indexes = engine.mem.triIndexes;
+		if( indexes == null ) indexes = engine.mem.getTriIndexes(buffer.vertices);
 		for( bid in m.bufferCache.keys() ) {
 			var b = m.bufferCache.get(bid);
 			addBuffer(hxsl.Globals.getIDName(bid), b.buffer, b.offset);

+ 1 - 1
h3d/prim/Plane2D.hx

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

+ 4 - 5
h3d/prim/Polygon.hx

@@ -84,7 +84,6 @@ class Polygon extends MeshPrimitive {
 			}
 		}
 		var flags : Array<h3d.Buffer.BufferFlag> = [];
-		if( idx == null ) flags.push(Triangles);
 		if( normals == null || tangents != null ) flags.push(RawFormat);
 		buffer = h3d.Buffer.ofFloats(buf, size, flags);
 
@@ -281,10 +280,10 @@ class Polygon extends MeshPrimitive {
 		var bufs = getBuffers(engine);
 		if( indexes != null )
 			engine.renderMultiBuffers(bufs, indexes);
-		else if( buffer.flags.has(Quads) )
-			engine.renderMultiBuffers(bufs, engine.mem.quadIndexes, 0, triCount());
-		else
-			engine.renderMultiBuffers(bufs, engine.mem.triIndexes, 0, triCount());
+		else {
+			var count = triCount();
+			engine.renderMultiBuffers(bufs, engine.mem.getTriIndexes(count*3), 0, count);
+		}
 	}
 
 }

+ 3 - 6
h3d/prim/Primitive.hx

@@ -103,12 +103,9 @@ class Primitive {
 	**/
 	public function render( engine : h3d.Engine ) {
 		if( buffer == null || buffer.isDisposed() ) alloc(engine);
-		if( indexes == null ) {
-			if( buffer.flags.has(Quads) )
-				engine.renderQuadBuffer(buffer);
-			else
-				engine.renderTriBuffer(buffer);
-		} else
+		if( indexes == null )
+			engine.renderTriBuffer(buffer);
+		else
 			engine.renderIndexed(buffer,indexes);
 	}
 

+ 1 - 1
h3d/prim/Quads.hx

@@ -97,7 +97,7 @@ class Quads extends Primitive {
 		var size = 3;
 		if( normals != null ) size += 3;
 		if( uvs != null ) size += 2;
-		var flags : Array<h3d.Buffer.BufferFlag> = [Quads];
+		var flags : Array<h3d.Buffer.BufferFlag> = [];
 		if( normals == null ) flags.push(RawFormat);
 		buffer = h3d.Buffer.ofFloats(v, size, flags);
 	}

+ 2 - 3
h3d/prim/RawPrimitive.hx

@@ -5,7 +5,7 @@ class RawPrimitive extends Primitive {
 	var vcount : Int;
 	var tcount : Int;
 	var bounds : h3d.col.Bounds;
-	public var onContextLost : Void -> { vbuf : hxd.FloatBuffer, stride : Int, ?ibuf : hxd.IndexBuffer, ?quads : Bool };
+	public var onContextLost : Void -> { vbuf : hxd.FloatBuffer, stride : Int, ?ibuf : hxd.IndexBuffer };
 
 	public function new( inf : { vbuf : hxd.FloatBuffer, stride : Int, ?ibuf : hxd.IndexBuffer, ?quads : Bool, ?bounds : h3d.col.Bounds }, persist = false ) {
 		onContextLost = function() return inf;
@@ -18,11 +18,10 @@ class RawPrimitive extends Primitive {
 		if( onContextLost == null ) throw "Cannot realloc " + this;
 		var inf = onContextLost();
 		var flags : Array<h3d.Buffer.BufferFlag> = [];
-		if( inf.ibuf == null ) flags.push(inf.quads ? Quads : Triangles);
 		if( inf.stride < 8 ) flags.push(RawFormat);
 		buffer = h3d.Buffer.ofFloats(inf.vbuf, inf.stride, flags);
 		vcount = buffer.vertices;
-		tcount = inf.ibuf != null ? Std.int(inf.ibuf.length / 3) : inf.quads ? vcount >> 1 : Std.int(vcount/3);
+		tcount = inf.ibuf != null ? Std.int(inf.ibuf.length / 3) : Std.int(vcount/3);
 		if( inf.ibuf != null )
 			indexes = h3d.Indexes.alloc(inf.ibuf);
 		else if( indexes != null ) {

+ 0 - 2
hxd/impl/Allocator.hx

@@ -4,7 +4,6 @@ enum abstract BufferFlags(Int) {
 	public var Dynamic = 0;
 	public var UniformDynamic = 1;
 	public var RawFormat = 2;
-	public var RawQuads = 3;
 	public inline function toInt() : Int {
 		return this;
 	}
@@ -23,7 +22,6 @@ class Allocator {
 				case Dynamic: [Dynamic];
 				case UniformDynamic: [UniformBuffer,Dynamic];
 				case RawFormat: [RawFormat];
-				case RawQuads: [Quads, RawFormat];
 			});
 	}
 

+ 1 - 1
hxd/impl/CacheAllocator.hx

@@ -66,7 +66,7 @@ class CacheAllocator extends Allocator {
 	override function disposeBuffer(b:h3d.Buffer) {
 		if( b.isDisposed() ) return;
 		var f = b.flags;
-		var flags = f.has(RawFormat) ? (f.has(Quads) ? RawQuads : RawFormat) : (f.has(UniformBuffer) ? UniformDynamic : Dynamic);
+		var flags = f.has(RawFormat) ? RawFormat : (f.has(UniformBuffer) ? UniformDynamic : Dynamic);
 		var id = flags.toInt() | (b.stride << 3) | (b.vertices << 16);
 		var c = buffers.get(id);
 		if( c == null ) {