Forráskód Böngészése

slightly optimized memory allocs

ncannasse 10 éve
szülő
commit
95ec23de0a

+ 6 - 3
h3d/impl/Stage3dDriver.hx

@@ -85,6 +85,7 @@ class Stage3dDriver extends Driver {
 		s3d = flash.Lib.current.stage.stage3Ds[0];
 		programs = new Map();
 		curTextures = [];
+		curSamplerBits = [];
 		curMultiBuffer = [];
 	}
 
@@ -110,10 +111,12 @@ class Stage3dDriver extends Driver {
 		for( i in 0...curAttributes )
 			ctx.setVertexBufferAt(i, null);
 		curAttributes = 0;
-		for( i in 0...curTextures.length )
+		for( i in 0...curTextures.length ) {
 			ctx.setTextureAt(i, null);
-		curTextures = [];
-		curSamplerBits = [];
+			curTextures[i] = null;
+		}
+		for( i in 0...curSamplerBits.length )
+			curSamplerBits[i] = -1;
 	}
 
 	override function init( onCreate, forceSoftware = false ) {

+ 2 - 2
h3d/pass/Default.hx

@@ -101,8 +101,8 @@ class Default extends Base {
 
 	@:access(h3d.scene)
 	override function draw( passes : Object ) {
-		for( k in ctx.sharedGlobals.keys() )
-			globals.fastSet(k, ctx.sharedGlobals.get(k));
+		for( g in ctx.sharedGlobals )
+			globals.fastSet(g.gid, g.value);
 		setGlobals();
 		setupShaders(passes);
 		var p = passes;

+ 1 - 1
h3d/pass/Depth.hx

@@ -21,7 +21,7 @@ class Depth extends Default {
 		ctx.engine.setTarget(texture);
 		ctx.engine.clear(enableSky ? 0 : 0xFF0000, 1);
 		passes = super.draw(passes);
-		ctx.sharedGlobals.set(depthMapId, texture);
+		ctx.setGlobalID(depthMapId, texture);
 		return passes;
 	}
 

+ 1 - 1
h3d/pass/Normal.hx

@@ -19,7 +19,7 @@ class Normal extends Default {
 		ctx.engine.setTarget(texture);
 		ctx.engine.clear(0, 1);
 		passes = super.draw(passes);
-		ctx.sharedGlobals.set(normalMapId, texture);
+		ctx.setGlobalID(normalMapId, texture);
 		return passes;
 	}
 

+ 1 - 0
h3d/pass/Object.hx

@@ -5,6 +5,7 @@ class Object {
 	public var obj : h3d.scene.Object;
 	public var index : Int;
 	public var next : Object;
+	public var nextAlloc : Object;
 
 	// cache
 	public var shaders : hxsl.ShaderList;

+ 5 - 5
h3d/pass/ShadowMap.hx

@@ -88,11 +88,11 @@ class ShadowMap extends Default {
 		if( blur.quality > 0 && blur.passes > 0 )
 			blur.apply(texture, tcache.allocTarget("tmpBlur", ctx, size, size, false), true);
 
-		ctx.sharedGlobals.set(shadowMapId, texture);
-		ctx.sharedGlobals.set(shadowProjId, lightCamera.m);
-		ctx.sharedGlobals.set(shadowColorId, color);
-		ctx.sharedGlobals.set(shadowPowerId, power);
-		ctx.sharedGlobals.set(shadowBiasId, bias);
+		ctx.setGlobalID(shadowMapId, texture);
+		ctx.setGlobalID(shadowProjId, lightCamera.m);
+		ctx.setGlobalID(shadowColorId, color);
+		ctx.setGlobalID(shadowPowerId, power);
+		ctx.setGlobalID(shadowBiasId, bias);
 		return passes;
 	}
 

+ 38 - 14
h3d/scene/RenderContext.hx

@@ -1,17 +1,27 @@
 package h3d.scene;
 import h3d.pass.Object in ObjectPass;
 
+private class SharedGlobal {
+	public var gid : Int;
+	public var value : Dynamic;
+	public function new(gid, value) {
+		this.gid = gid;
+		this.value = value;
+	}
+}
+
 class RenderContext extends h3d.impl.RenderContext {
 
 	public var camera : h3d.Camera;
 	public var drawPass : ObjectPass;
 
-	public var sharedGlobals : Map<Int,Dynamic>;
+	var sharedGlobals : Array<SharedGlobal>;
 	public var lightSystem : h3d.pass.LightSystem;
 	public var uploadParams : Void -> Void;
 	public var extraShaders : hxsl.ShaderList;
 
 	var pool : ObjectPass;
+	var firstAlloc : ObjectPass;
 	var cachedShaderList : Array<hxsl.ShaderList>;
 	var cachedPos : Int;
 	var passes : ObjectPass;
@@ -32,7 +42,7 @@ class RenderContext extends h3d.impl.RenderContext {
 	}
 
 	public function start() {
-		sharedGlobals = new Map();
+		sharedGlobals = [];
 		lights = null;
 		drawPass = null;
 		passes = null;
@@ -49,19 +59,34 @@ class RenderContext extends h3d.impl.RenderContext {
 	}
 
 	public function getGlobal( name : String ) : Dynamic {
-		return sharedGlobals.get(hxsl.Globals.allocID(name));
+		var id = hxsl.Globals.allocID(name);
+		for( g in sharedGlobals )
+			if( g.gid == id )
+				return g.value;
+		return null;
+	}
+
+	public inline function setGlobal( name : String, value : Dynamic ) {
+		setGlobalID(hxsl.Globals.allocID(name), value);
 	}
 
-	public function setGlobal( name : String, value : Dynamic ) {
-		sharedGlobals.set(hxsl.Globals.allocID(name), value);
+	public function setGlobalID( gid : Int, value : Dynamic ) {
+		for( g in sharedGlobals )
+			if( g.gid == gid ) {
+				g.value = value;
+				return;
+			}
+		sharedGlobals.push(new SharedGlobal(gid, value));
 	}
 
 	public function emitPass( pass : h3d.mat.Pass, obj : h3d.scene.Object ) {
 		var o = pool;
-		if( o == null )
+		if( o == null ) {
 			o = new ObjectPass();
-		else
-			pool = o.next;
+			o.nextAlloc = firstAlloc;
+			firstAlloc = o;
+		} else
+			pool = o.nextAlloc;
 		o.pass = pass;
 		o.obj = obj;
 		o.next = passes;
@@ -89,20 +114,19 @@ class RenderContext extends h3d.impl.RenderContext {
 		drawPass = null;
 		uploadParams = null;
 		// move passes to pool, and erase data
-		var p = passes, prev = null;
+		var p = firstAlloc, prev = null;
 		while( p != null ) {
 			p.obj = null;
 			p.pass = null;
 			p.shader = null;
 			p.shaders = null;
+			p.next = null;
 			p.index = 0;
 			prev = p;
-			p = p.next;
-		}
-		if( prev != null ) {
-			prev.next = pool;
-			pool = passes;
+			p = p.nextAlloc;
 		}
+		pool = firstAlloc;
+
 		for( c in cachedShaderList ) {
 			c.s = null;
 			c.next = null;