Ver Fonte

move globals from Default to RenderContext (works with DX, bug with GL ProjFlip!)

Nicolas Cannasse há 1 ano atrás
pai
commit
c9c051e41f

+ 4 - 5
h3d/pass/CascadeShadowMap.hx

@@ -90,11 +90,6 @@ class CascadeShadowMap extends DirShadowMap {
 		lightCameras[cascade - 1].orthoBounds = lightCamera.orthoBounds.clone();
 	}
 
-	override function setGlobals() {
-		super.setGlobals();
-		cameraViewProj = getCascadeProj(currentCascadeIndex);
-	}
-
 	function getCascadeProj(i:Int) {
 		return lightCameras[i].m;
 	}
@@ -125,6 +120,10 @@ class CascadeShadowMap extends DirShadowMap {
 		cshader.pcfQuality = pcfQuality;
 	}
 
+	override function getShadowProj():Matrix {
+		return getCascadeProj(currentCascadeIndex);
+	}
+
 	override function draw( passes, ?sort ) {
 		if( !enabled )
 			return;

+ 7 - 7
h3d/pass/CubeShadowMap.hx

@@ -50,13 +50,6 @@ class CubeShadowMap extends Shadows {
 		return true;
 	}
 
-	override function setGlobals() {
-		super.setGlobals();
-		cameraViewProj = getShadowProj();
-		cameraFar = lightCamera.zFar;
-		cameraPos = lightCamera.pos;
-	}
-
 	override function saveStaticData() {
 		if( mode != Mixed && mode != Static )
 			return null;
@@ -180,6 +173,10 @@ class CubeShadowMap extends Shadows {
 		lightCamera.pos.set(absPos.tx, absPos.ty, absPos.tz);
 		updateLightCameraNearFar(light);
 
+		var prevFar = @:privateAccess ctx.cameraFar;
+		var prevPos = @:privateAccess ctx.cameraPos;
+		@:privateAccess ctx.cameraFar = lightCamera.zFar;
+		@:privateAccess ctx.cameraPos = lightCamera.pos;
 
 		for( i in 0...6 ) {
 
@@ -207,6 +204,9 @@ class CubeShadowMap extends Shadows {
 			ctx.engine.popTarget();
 		}
 
+		@:privateAccess ctx.cameraFar = prevFar;
+		@:privateAccess ctx.cameraPos = prevPos;
+
 		// Blur is applied even if there's no shadows - TO DO : remove the useless blur pass
 		if( blur.radius > 0 )
 			blur.apply(ctx, texture);

+ 3 - 25
h3d/pass/Default.hx

@@ -1,6 +1,5 @@
 package h3d.pass;
 
-@:build(hxsl.Macros.buildGlobals())
 @:access(h3d.mat.Pass)
 class Default extends Base {
 
@@ -10,29 +9,9 @@ class Default extends Base {
 
 	inline function get_globals() return ctx.globals;
 
-	@global("camera.view") var cameraView : h3d.Matrix = ctx.camera.mcam;
-	@global("camera.zNear") var cameraNear : Float = ctx.camera.zNear;
-	@global("camera.zFar") var cameraFar : Float = ctx.camera.zFar;
-	@global("camera.proj") var cameraProj : h3d.Matrix = ctx.camera.mproj;
-	@global("camera.position") var cameraPos : h3d.Vector = ctx.camera.pos;
-	@global("camera.projDiag") var cameraProjDiag : h3d.Vector4 = new h3d.Vector4(ctx.camera.mproj._11,ctx.camera.mproj._22,ctx.camera.mproj._33,ctx.camera.mproj._44);
-	@global("camera.projFlip") var cameraProjFlip : Float = ctx.engine.driver.hasFeature(BottomLeftCoords) && ctx.engine.getCurrentTarget() != null ? -1 : 1;
-	@global("camera.viewProj") var cameraViewProj : h3d.Matrix = ctx.camera.m;
-	@global("camera.inverseViewProj") var cameraInverseViewProj : h3d.Matrix = ctx.camera.getInverseViewProj();
-	@global("global.time") var globalTime : Float = ctx.time;
-	@global("global.pixelSize") var pixelSize : h3d.Vector = getCurrentPixelSize();
-	@global("global.modelView") var globalModelView : h3d.Matrix;
-	@global("global.modelViewInverse") var globalModelViewInverse : h3d.Matrix;
-
 	public function new(name) {
 		super(name);
 		manager = new ShaderManager(getOutputs());
-		initGlobals();
-	}
-
-	function getCurrentPixelSize() {
-		var t = ctx.engine.getCurrentTarget();
-		return new h3d.Vector(2 / (t == null ? ctx.engine.width : t.width), 2 / (t == null ? ctx.engine.height : t.height));
 	}
 
 	function getOutputs() : Array<hxsl.Output> {
@@ -97,7 +76,6 @@ class Default extends Base {
 		if( passes.isEmpty() )
 			return;
 		#if sceneprof h3d.impl.SceneProf.begin("draw", ctx.frame); #end
-		setGlobals();
 		setupShaders(passes);
 		if( sort == null )
 			defaultSort(passes);
@@ -106,9 +84,9 @@ class Default extends Base {
 		var buf = ctx.shaderBuffers, prevShader = null;
 		for( p in passes ) {
 			#if sceneprof h3d.impl.SceneProf.mark(p.obj); #end
-			globalModelView = p.obj.absPos;
-			if( p.shader.hasGlobal(globalModelViewInverse_id.toInt()) )
-				globalModelViewInverse = p.obj.getInvPos();
+			ctx.globalModelView = p.obj.absPos;
+			if( p.shader.hasGlobal(ctx.globalModelViewInverse_id.toInt()) )
+				ctx.globalModelViewInverse = p.obj.getInvPos();
 			if( prevShader != p.shader ) {
 				prevShader = p.shader;
 				if( onShaderError != null ) {

+ 7 - 5
h3d/pass/DirShadowMap.hx

@@ -186,11 +186,6 @@ class DirShadowMap extends Shadows {
 		bounds.scaleCenter(1.01);
 	}
 
-	override function setGlobals() {
-		super.setGlobals();
-		cameraViewProj = getShadowProj();
-	}
-
 	override function syncShader(texture) {
 		dshader.shadowMap = texture;
 		dshader.shadowMapChannel = format == h3d.mat.Texture.nativeFormat ? PackedFloat : R;
@@ -266,6 +261,10 @@ class DirShadowMap extends Shadows {
 	}
 
 	function processShadowMap( passes, tex, ?sort) {
+
+		var prevViewProj = @:privateAccess ctx.cameraViewProj;
+		@:privateAccess ctx.cameraViewProj = getShadowProj();
+
 		if ( tex.isDepth() )
 			ctx.engine.pushDepth(tex);
 		else
@@ -303,6 +302,9 @@ class DirShadowMap extends Shadows {
 				ctx.engine.popTarget();
 			}
 		}
+
+		@:privateAccess ctx.cameraViewProj = prevViewProj;
+
 		return tex;
 	}
 

+ 3 - 5
h3d/pass/SpotShadowMap.hx

@@ -49,11 +49,6 @@ class SpotShadowMap extends Shadows {
 		return sshader.shadowMap;
 	}
 
-	override function setGlobals() {
-		super.setGlobals();
-		cameraViewProj = getShadowProj();
-	}
-
 	override function syncShader(texture) {
 		sshader.shadowMap = texture;
 		sshader.shadowMapChannel = format == h3d.mat.Texture.nativeFormat ? PackedFloat : R;
@@ -148,12 +143,15 @@ class SpotShadowMap extends Shadows {
 		var validBakedTexture = (staticTexture != null && staticTexture.width == texture.width);
 		if( mode == Mixed && !ctx.computingStatic && validBakedTexture ) {
 			var merge = ctx.textures.allocTarget("mergedSpotShadowMap", size, size, false, format);
+			var prev = @:privateAccess ctx.cameraViewProj;
+			@:privateAccess ctx.cameraViewProj = getShadowProj();
 			mergePass.shader.texA = texture;
 			mergePass.shader.texB = staticTexture;
 			ctx.engine.pushTarget(merge);
 			mergePass.render();
 			ctx.engine.popTarget();
 			texture = merge;
+			@:privateAccess ctx.cameraViewProj = prev;
 		}
 
 		syncShader(texture);

+ 36 - 0
h3d/scene/RenderContext.hx

@@ -9,6 +9,7 @@ private class SharedGlobal {
 	}
 }
 
+@:build(hxsl.Macros.buildGlobals())
 class RenderContext extends h3d.impl.RenderContext {
 
 	public var camera : h3d.Camera;
@@ -25,6 +26,20 @@ class RenderContext extends h3d.impl.RenderContext {
 	public var shaderBuffers : h3d.shader.Buffers;
 	public var cullingCollider : h3d.col.Collider;
 
+	@global("camera.view") var cameraView : h3d.Matrix;
+	@global("camera.zNear") var cameraNear : Float;
+	@global("camera.zFar") var cameraFar : Float;
+	@global("camera.proj") var cameraProj : h3d.Matrix;
+	@global("camera.position") var cameraPos : h3d.Vector;
+	@global("camera.projDiag") var cameraProjDiag : h3d.Vector4;
+	@global("camera.projFlip") var cameraProjFlip : Float;
+	@global("camera.viewProj") var cameraViewProj : h3d.Matrix;
+	@global("camera.inverseViewProj") var cameraInverseViewProj : h3d.Matrix;
+	@global("global.time") var globalTime : Float;
+	@global("global.pixelSize") var pixelSize : h3d.Vector;
+	@global("global.modelView") var globalModelView : h3d.Matrix;
+	@global("global.modelViewInverse") var globalModelViewInverse : h3d.Matrix;
+
 	var allocPool : h3d.pass.PassObject;
 	var allocFirst : h3d.pass.PassObject;
 	var cachedShaderList : Array<hxsl.ShaderList>;
@@ -37,6 +52,24 @@ class RenderContext extends h3d.impl.RenderContext {
 		super();
 		cachedShaderList = [];
 		cachedPassObjects = [];
+		initGlobals();
+	}
+
+	public function setCamera( cam : h3d.Camera ) {
+		cameraView = cam.mcam;
+		cameraNear = cam.zNear;
+		cameraFar = cam.zFar;
+		cameraProj = cam.mproj;
+		cameraPos = cam.pos;
+		cameraProjDiag = new h3d.Vector4(cam.mproj._11,cam.mproj._22,cam.mproj._33,cam.mproj._44);
+		cameraProjFlip = engine.driver.hasFeature(BottomLeftCoords) && engine.getCurrentTarget() != null ? -1 : 1;
+		cameraViewProj = cam.m;
+		cameraInverseViewProj = camera.getInverseViewProj();
+	}
+
+	function getCurrentPixelSize() {
+		var t = engine.getCurrentTarget();
+		return new h3d.Vector(2 / (t == null ? engine.width : t.width), 2 / (t == null ? engine.height : t.height));
 	}
 
 	@:access(h3d.mat.Pass)
@@ -59,6 +92,9 @@ class RenderContext extends h3d.impl.RenderContext {
 		time += elapsedTime;
 		frame++;
 		setCurrent();
+		globalTime = time;
+		pixelSize = getCurrentPixelSize();
+		setCamera(camera);
 	}
 
 	public inline function nextPass() {

+ 1 - 11
hxsl/Macros.hx

@@ -447,7 +447,6 @@ class Macros {
 	public static function buildGlobals() {
 		var fields = Context.getBuildFields();
 		var globals = [];
-		var sets = [];
 		for( f in fields ) {
 			if( f.meta == null ) continue;
 			for( m in f.meta ) {
@@ -484,7 +483,7 @@ class Macros {
 						});
 						globals.push(macro $i{id} = new hxsl.Globals.GlobalSlot($v{ name }));
 						if( set != null )
-							sets.push(macro $i{f.name} = $set);
+							Context.error("Value ignored", set.pos);
 					default:
 					}
 			}
@@ -499,15 +498,6 @@ class Macros {
 			}),
 			pos : p,
 		});
-		fields.push({
-			name : "setGlobals",
-			kind : FFun({
-				ret : null,
-				expr : { expr : EBlock(sets), pos : p },
-				args : [],
-			}),
-			pos : p,
-		});
 		return fields;
 	}
 	#end