瀏覽代碼

memory allocations optimizations

ncannasse 6 年之前
父節點
當前提交
d3d693c474
共有 6 個文件被更改,包括 32 次插入16 次删除
  1. 9 7
      h3d/Camera.hx
  2. 3 3
      h3d/impl/GlDriver.hx
  3. 3 1
      h3d/pass/Default.hx
  4. 4 4
      h3d/pass/PointShadowMap.hx
  5. 2 0
      h3d/scene/RenderContext.hx
  6. 11 1
      h3d/scene/Scene.hx

+ 9 - 7
h3d/Camera.hx

@@ -118,7 +118,7 @@ class Camera {
 	/**
 		Setup camera for cubemap rendering on the given face.
 	**/
-	public function setCubeMap( face : Int, position : h3d.Vector ) {
+	public function setCubeMap( face : Int, ?position : h3d.Vector ) {
 		var dx = 0, dy = 0, dz = 0;
 		switch( face ) {
 		case 0: dx = 1; up.set(0,1,0);
@@ -128,7 +128,8 @@ class Camera {
 		case 4: dz = 1; up.set(0,1,0);
 		case 5: dz = -1; up.set(0,1,0);
 		}
-		pos.set(position.x,position.y,position.z);
+		if( position != null )
+			pos.load(position);
 		target.set(pos.x + dx,pos.y + dy,pos.z + dz);
 	}
 
@@ -175,9 +176,9 @@ class Camera {
 		}
 		makeCameraMatrix(mcam);
 		makeFrustumMatrix(mproj);
-		
+
 		m.multiply(mcam, mproj);
-		
+
 		needInv = true;
 		if( miview != null ) miview._44 = 0;
 
@@ -235,10 +236,11 @@ class Camera {
 		// in leftHanded the z axis is positive else it's negative
 		// this way we make sure that our [ax,ay,-az] matrix follow the same handness as our world
 		// We build a transposed version of Matrix.lookAt
-		var az = rightHanded ? pos.sub(target) : target.sub(pos);
-		az.normalize();
+		var az = target.sub(pos);
+		if( rightHanded ) az.scale3(-1);
+		az.normalizeFast();
 		var ax = up.cross(az);
-		ax.normalize();
+		ax.normalizeFast();
 		if( ax.length() == 0 ) {
 			ax.x = az.y;
 			ax.y = az.z;

+ 3 - 3
h3d/impl/GlDriver.hx

@@ -1384,7 +1384,7 @@ class GlDriver extends Driver {
 	}
 
 	override function capturePixels(tex:h3d.mat.Texture, layer:Int, mipLevel:Int, ?region:h2d.col.IBounds) {
-		
+
 		var pixels : hxd.Pixels;
 		var x : Int, y : Int;
 		if (region != null) {
@@ -1400,7 +1400,7 @@ class GlDriver extends Driver {
 			x = 0;
 			y = 0;
 		}
-		
+
 		var old = curTarget;
 		var oldCount = numTargets;
 		var oldLayer = curTargetLayer;
@@ -1436,7 +1436,7 @@ class GlDriver extends Driver {
 		if( tex.depthBuffer != null && (tex.depthBuffer.width != tex.width || tex.depthBuffer.height != tex.height) )
 			throw "Invalid depth buffer size : does not match render target size";
 
-		if( glES == 1 && mipLevel > 0 ) throw "Cannot render to mipLevel in WebGL1, use upload() instead";
+		if( mipLevel > 0 && glES == 1 ) throw "Cannot render to mipLevel in WebGL1, use upload() instead";
 
 		if( tex.t == null )
 			tex.alloc();

+ 3 - 1
h3d/pass/Default.hx

@@ -12,6 +12,7 @@ class Default extends Base {
 	var shaderIdMap : Array<Int>;
 	var textureIdMap : Array<Int>;
 	var sortPasses = true;
+	var uploadParamsClosure : Void -> Void;
 
 	inline function get_globals() return manager.globals;
 
@@ -34,6 +35,7 @@ class Default extends Base {
 		manager = new ShaderManager(getOutputs());
 		shaderIdMap = [];
 		textureIdMap = [];
+		uploadParamsClosure = uploadParams;
 		initGlobals();
 	}
 
@@ -119,7 +121,7 @@ class Default extends Base {
 				if( d != 0 ) return d;
 				return textureIdMap[o1.texture] - textureIdMap[o2.texture];
 			});
-		ctx.uploadParams = uploadParams;
+		ctx.uploadParams = uploadParamsClosure;
 		var buf = cachedBuffer, prevShader = null;
 		for( p in passes ) {
 			globalModelView = p.obj.absPos;

+ 4 - 4
h3d/pass/PointShadowMap.hx

@@ -128,11 +128,12 @@ class PointShadowMap extends Shadows {
 
 		var pointLight = cast(light, h3d.scene.pbr.PointLight);
 		var absPos = light.getAbsPos();
+		lightCamera.pos.set(absPos.tx, absPos.ty, absPos.tz);
+		lightCamera.zFar = pointLight.range;
+		lightCamera.zNear = pointLight.zNear;
 
 		for( i in 0...6 ) {
-			lightCamera.setCubeMap(i, new h3d.Vector(absPos.tx, absPos.ty, absPos.tz));
-			lightCamera.zFar = pointLight.range;
-			lightCamera.zNear = pointLight.zNear;
+			lightCamera.setCubeMap(i);
 			lightCamera.update();
 
 			ctx.engine.pushTarget(texture, i);
@@ -142,7 +143,6 @@ class PointShadowMap extends Shadows {
 			passes.filter(function(p) return p.obj.cullingBits & bit == 0);
 			super.draw(passes);
 			passes.load(save);
-
 			ctx.engine.popTarget();
 		}
 

+ 2 - 0
h3d/scene/RenderContext.hx

@@ -26,6 +26,7 @@ class RenderContext extends h3d.impl.RenderContext {
 	var allocPool : h3d.pass.PassObject;
 	var allocFirst : h3d.pass.PassObject;
 	var cachedShaderList : Array<hxsl.ShaderList>;
+	var cachedPassObjects : Array<Renderer.PassObjects>;
 	var cachedPos : Int;
 	var passes : h3d.pass.PassObject;
 	var lights : Light;
@@ -33,6 +34,7 @@ class RenderContext extends h3d.impl.RenderContext {
 	public function new() {
 		super();
 		cachedShaderList = [];
+		cachedPassObjects = [];
 	}
 
 	@:access(h3d.mat.Pass)

+ 11 - 1
h3d/scene/Scene.hx

@@ -392,6 +392,7 @@ class Scene extends Object implements h3d.IDrawable implements hxd.SceneEvents.I
 		// group by pass implementation
 		var curPass = ctx.passes;
 		var passes = [];
+		var passIndex = -1;
 		while( curPass != null ) {
 			var passId = curPass.pass.passId;
 			var p = curPass, prev = null;
@@ -400,7 +401,11 @@ class Scene extends Object implements h3d.IDrawable implements hxd.SceneEvents.I
 				p = p.next;
 			}
 			prev.next = null;
-			var pobjs = new Renderer.PassObjects();
+			var pobjs = ctx.cachedPassObjects[++passIndex];
+			if( pobjs == null ) {
+				pobjs = new Renderer.PassObjects();
+				ctx.cachedPassObjects[passIndex] = pobjs;
+			}
 			pobjs.name = curPass.pass.name;
 			pobjs.passes.init(curPass);
 			passes.push(pobjs);
@@ -427,6 +432,11 @@ class Scene extends Object implements h3d.IDrawable implements hxd.SceneEvents.I
 		ctx.scene = null;
 		ctx.camera = null;
 		ctx.engine = null;
+		for( i in 0...passIndex ) {
+			var p = ctx.cachedPassObjects[i];
+			p.name = null;
+			p.passes.init(null);
+		}
 	}
 
 	/**