Browse Source

Update PBR renderer, move code from render() to lighting()
Update Shadow, add local variables and early exit

ShiroSmith 5 years ago
parent
commit
b840600ad5
3 changed files with 82 additions and 70 deletions
  1. 58 53
      h3d/scene/pbr/Renderer.hx
  2. 2 1
      h3d/shader/DirShadow.hx
  3. 22 16
      h3d/shader/PointShadow.hx

+ 58 - 53
h3d/scene/pbr/Renderer.hx

@@ -193,6 +193,63 @@ class Renderer extends h3d.scene.Renderer {
 		renderPass(output, get("additive"));
 	}
 
+	function lighting() {
+
+		var ls = hxd.impl.Api.downcast(getLightSystem(), LightSystem);
+		var count = ctx.engine.drawCalls;
+		if( ls != null ) drawShadows(ls);
+		if( ctx.lightSystem != null ) ctx.lightSystem.drawPasses = ctx.engine.drawCalls - count;
+
+		var lpass = screenLightPass;
+		if( lpass == null ) {
+			lpass = new h3d.pass.ScreenFx(new h3d.shader.ScreenShader());
+			lpass.addShader(pbrProps);
+			lpass.addShader(pbrDirect);
+			lpass.pass.setBlendMode(Add);
+			screenLightPass = lpass;
+		}
+
+		mark("DirectLighting");
+		// Direct Lighting - FullScreen
+		pbrProps.isScreen = true;
+		if( ls != null ) {
+			var count = ctx.engine.drawCalls;
+			ls.drawScreenLights(this, lpass);
+			ctx.lightSystem.drawPasses += ctx.engine.drawCalls - count;
+		}
+		// Direct Lighting - With Primitive
+		pbrProps.isScreen = false;
+		draw(pbrLightPass.name);
+
+		if( renderMode == LightProbe ) {
+			pbrProps.isScreen = true;
+			pbrOut.render();
+			resetTarget();
+			copy(ctx.getGlobal("hdr"), null);
+			// no warnings
+			for( p in passObjects ) if( p != null ) p.rendered = true;
+			return;
+		}
+
+		// Indirect Lighting - Diffuse with volumetricLightmap
+		mark("VolumetricLightmap");
+		pbrProps.isScreen = false;
+		pbrIndirect.drawIndirectDiffuse = false;
+		pbrIndirect.drawIndirectSpecular = env != null ? true : false;
+		ctx.extraShaders = new hxsl.ShaderList(pbrProps, new hxsl.ShaderList(pbrIndirect, null));
+		draw("volumetricLightmap");
+		ctx.extraShaders = null;
+
+		// Indirect Lighting - Diffuse and Specular
+ 		if( env != null ) {
+			mark("Indirect Lighting");
+			pbrProps.isScreen = true;
+			pbrIndirect.drawIndirectDiffuse = true;
+			pbrIndirect.drawIndirectSpecular = true;
+			pbrOut.render();
+		}
+	}
+
 	function drawBeforeTonemapping() {
 		mark("BeforeTonemapping");
 		draw("beforeTonemappingDecal");
@@ -283,11 +340,6 @@ class Renderer extends h3d.scene.Renderer {
 		ctx.setGlobal("occlusionMap", { texture : pbr, channel : hxsl.Channel.B });
 		ctx.setGlobal("bloom", null);
 
-		var ls = hxd.impl.Api.downcast(getLightSystem(), LightSystem);
-		var count = ctx.engine.drawCalls;
-		if( ls != null ) drawShadows(ls);
-		if( ctx.lightSystem != null ) ctx.lightSystem.drawPasses = ctx.engine.drawCalls - count;
-
 		setTargets([albedo,normal,pbr,other]);
 		clear(0, 1, 0);
 		mainDraw();
@@ -371,54 +423,7 @@ class Renderer extends h3d.scene.Renderer {
 			pbrDirect.doDiscard = true;
 		}
 
-		var lpass = screenLightPass;
-		if( lpass == null ) {
-			lpass = new h3d.pass.ScreenFx(new h3d.shader.ScreenShader());
-			lpass.addShader(pbrProps);
-			lpass.addShader(pbrDirect);
-			lpass.pass.setBlendMode(Add);
-			screenLightPass = lpass;
-		}
-
-		mark("DirectLighting");
-		// Direct Lighting - FullScreen
-		pbrProps.isScreen = true;
-		if( ls != null ) {
-			var count = ctx.engine.drawCalls;
-			ls.drawScreenLights(this, lpass);
-			ctx.lightSystem.drawPasses += ctx.engine.drawCalls - count;
-		}
-		// Direct Lighting - With Primitive
-		pbrProps.isScreen = false;
-		draw(pbrLightPass.name);
-
-		if( renderMode == LightProbe ) {
-			pbrProps.isScreen = true;
-			pbrOut.render();
-			resetTarget();
-			copy(hdr, null);
-			// no warnings
-			for( p in passObjects ) if( p != null ) p.rendered = true;
-			return;
-		}
-
-		// Indirect Lighting - Diffuse with volumetricLightmap
-		mark("VolumetricLightmap");
-		pbrProps.isScreen = false;
-		pbrIndirect.drawIndirectDiffuse = false;
-		pbrIndirect.drawIndirectSpecular = env != null ? true : false;
-		ctx.extraShaders = new hxsl.ShaderList(pbrProps, new hxsl.ShaderList(pbrIndirect, null));
-		draw("volumetricLightmap");
-		ctx.extraShaders = null;
-
-		// Indirect Lighting - Diffuse and Specular
- 		if( env != null ) {
-			mark("Indirect Lighting");
-			pbrProps.isScreen = true;
-			pbrIndirect.drawIndirectDiffuse = true;
-			pbrIndirect.drawIndirectSpecular = true;
-			pbrOut.render();
-		}
+		lighting();
 
 		drawBeforeTonemapping();
 		apply(BeforeTonemapping);

+ 2 - 1
h3d/shader/DirShadow.hx

@@ -21,6 +21,7 @@ class DirShadow extends hxsl.Shader {
 
 		var transformedPosition : Vec3;
 		var shadow : Float;
+		var dirShadow : Float;
 
 		@param var poissonDiskLow : Array<Vec4,4>;
 		@param var poissonDiskHigh : Array<Vec4,12>;
@@ -85,8 +86,8 @@ class DirShadow extends hxsl.Shader {
 					shadow = shadowPos.z - shadowBias > depth ? 0 : 1;
 				}
 			}
+			dirShadow = shadow;
 		}
-
 	}
 
 	public function new() {

+ 22 - 16
h3d/shader/PointShadow.hx

@@ -21,6 +21,7 @@ class PointShadow extends hxsl.Shader {
 
 		var transformedPosition : Vec3;
 		var shadow : Float;
+		var pointShadow : Float;
 
 		function fragment() {
 			if( enable ) {
@@ -30,22 +31,26 @@ class PointShadow extends hxsl.Shader {
 					var dir = normalize(posToLight.xyz);
 					var zMax = length(posToLight);
 
-					var sampleCount = 0;
-					switch( pcfQuality ) {
-						case 1: sampleCount = 1;
-						case 2: sampleCount = 2;
-						case 3: sampleCount = 4;
-					};
-					var samplePerDim = sampleCount * 2 + 1;
-					var totalSample = samplePerDim * samplePerDim * samplePerDim;
-					var sampleStrength = 1.0 / totalSample;
-					for( i in -sampleCount ... sampleCount + 1 ) {
-						for( j in -sampleCount ... sampleCount + 1 ) {
-							for( k in -sampleCount ... sampleCount + 1 ) {
-								var offset = vec3(i, j, k) * pcfScale;
-								var depth = shadowMap.get(dir + offset).r * zFar;
-								if( zMax - shadowBias > depth )
-									shadow -= sampleStrength;
+					if( zFar < zMax )
+						shadow = 1.0;
+					else {
+						var sampleCount = 0;
+						switch( pcfQuality ) {
+							case 1: sampleCount = 1;
+							case 2: sampleCount = 2;
+							case 3: sampleCount = 4;
+						};
+						var samplePerDim = sampleCount * 2 + 1;
+						var totalSample = samplePerDim * samplePerDim * samplePerDim;
+						var sampleStrength = 1.0 / totalSample;
+						for( i in -sampleCount ... sampleCount + 1 ) {
+							for( j in -sampleCount ... sampleCount + 1 ) {
+								for( k in -sampleCount ... sampleCount + 1 ) {
+									var offset = vec3(i, j, k) * pcfScale;
+									var depth = shadowMap.get(dir + offset).r * zFar;
+									if( zMax - shadowBias > depth )
+										shadow -= sampleStrength;
+								}
 							}
 						}
 					}
@@ -66,6 +71,7 @@ class PointShadow extends hxsl.Shader {
 					shadow = zMax - shadowBias > depth ? 0 : 1;
 				}
 			}
+			pointShadow = shadow;
 		}
 	}