瀏覽代碼

Some refactoring

Panagiotis Christopoulos Charitos 7 年之前
父節點
當前提交
ff2439dd43
共有 4 個文件被更改,包括 49 次插入70 次删除
  1. 1 2
      programs/DeferredShading.ankiprog
  2. 9 23
      programs/LightShading.ankiprog
  3. 39 44
      shaders/LightFunctions.glsl
  4. 0 1
      shaders/Pack.glsl

+ 1 - 2
programs/DeferredShading.ankiprog

@@ -112,7 +112,6 @@ void main()
 	// Decode and process gbuffer
 	GbufferInfo gbuffer;
 	readGBuffer(u_msRt0, u_msRt1, u_msRt2, uvToRead, 0.0, gbuffer);
-	float a2 = pow(gbuffer.roughness, 2.0);
 
 	vec4 worldPos4 = u_invViewProjMat * vec4(UV_TO_NDC(uv), depth, 1.0);
 	vec3 worldPos = worldPos4.xyz / worldPos4.w;
@@ -123,7 +122,7 @@ void main()
 	vec3 l = normalize(frag2Light);
 	float nol = max(0.0, dot(gbuffer.normal, l));
 
-	vec3 specC = computeSpecularColorBrdf(viewDir, l, gbuffer.normal, gbuffer.specular, u_lspec, a2, nol);
+	vec3 specC = computeSpecularColorBrdf(gbuffer, viewDir, l, u_lspec);
 
 	vec3 diffC = computeDiffuseColor(gbuffer.diffuse, u_ldiff);
 

+ 9 - 23
programs/LightShading.ankiprog

@@ -68,11 +68,10 @@ const float SUBSURFACE_MIN = 0.05;
 #define LIGHTING_COMMON_BRDF() \
 	vec3 frag2Light = light.posRadius.xyz - worldPos; \
 	vec3 l = normalize(frag2Light); \
-	float nol = max(0.0, dot(normal, l)); \
-	vec3 specC = computeSpecularColorBrdf(viewDir, l, normal, specCol, light.specularColorRadius.rgb, roughness, nol); \
-	vec3 diffC = computeDiffuseColor(diffCol, light.diffuseColorShadowmapId.rgb); \
+	vec3 specC = computeSpecularColorBrdf(gbuffer, viewDir, l, light.specularColorRadius.rgb); \
+	vec3 diffC = computeDiffuseColor(gbuffer.diffuse, light.diffuseColorShadowmapId.rgb); \
 	float att = computeAttenuationFactor(light.posRadius.w, frag2Light); \
-	float lambert = nol;
+	float lambert = max(0.0, dot(gbuffer.normal, l));
 
 void main()
 {
@@ -84,23 +83,9 @@ void main()
 	vec3 worldPos = worldPos4.xyz / worldPos4.w;
 
 	// Decode GBuffer
-	vec3 normal;
-	vec3 diffCol;
-	vec3 specCol;
-	float roughness;
-	float subsurface;
-	float emission;
-	float metallic;
-
 	GbufferInfo gbuffer;
 	readGBuffer(u_msRt0, u_msRt1, u_msRt2, in_uv, 0.0, gbuffer);
-	diffCol = gbuffer.diffuse;
-	specCol = gbuffer.specular;
-	normal = gbuffer.normal;
-	roughness = gbuffer.roughness;
-	metallic = gbuffer.metallic;
-	subsurface = max(gbuffer.subsurface, SUBSURFACE_MIN);
-	emission = gbuffer.emission;
+	gbuffer.subsurface = max(gbuffer.subsurface, SUBSURFACE_MIN);
 
 	// Get first light index
 	uint idxOffset;
@@ -117,7 +102,7 @@ void main()
 	idxOffset += count;
 
 	// Ambient and emissive color
-	vec3 outC = diffCol * emission;
+	vec3 outC = gbuffer.diffuse * gbuffer.emission;
 
 	// Point lights
 	vec3 viewDir = normalize(u_cameraPos - worldPos);
@@ -138,7 +123,7 @@ void main()
 			lambert *= shadow;
 		}
 
-		outC += (diffC + specC) * (att * max(subsurface, lambert));
+		outC += (diffC + specC) * (att * max(gbuffer.subsurface, lambert));
 	}
 
 	// Spot lights
@@ -149,7 +134,8 @@ void main()
 
 		LIGHTING_COMMON_BRDF();
 
-		float spot = computeSpotFactor(l, light.outerCosInnerCos.x, light.outerCosInnerCos.y, light.lightDir.xyz);
+		float spot = computeSpotFactor(
+			l, light.outerCosInnerCos.x, light.outerCosInnerCos.y, light.lightDir.xyz);
 
 		float shadowmapLayerIdx = light.diffuseColorShadowmapId.w;
 		if(shadowmapLayerIdx >= 0.0)
@@ -159,7 +145,7 @@ void main()
 			lambert *= shadow;
 		}
 
-		outC += (diffC + specC) * (att * spot * max(subsurface, lambert));
+		outC += (diffC + specC) * (att * spot * max(gbuffer.subsurface, lambert));
 	}
 
 	// Indirect

+ 39 - 44
shaders/LightFunctions.glsl

@@ -23,42 +23,66 @@ float computeAttenuationFactor(float lightRadius, vec3 frag2Light)
 	return att * att;
 }
 
+vec3 fresnelSchlickRoughness(vec3 F0, float roughness, vec3 normal, vec3 viewDir)
+{
+	float cosTheta = max(dot(normal, viewDir), EPSILON);
+	vec3 F = F0 + (max(vec3(1.0 - roughness), F0) - F0) * pow(1.0 + EPSILON - cosTheta, 5.0);
+	return F;
+}
+
+// Compute the factors that will be used to calculat the final specular and diffuse indirect terms
+void computeSpecAndDiffuseIndirectFactors(
+	vec3 viewDir, GbufferInfo gbuffer, sampler2D integrationLut, out vec3 specIndirectTerm, out vec3 diffIndirectTerm)
+{
+	// Reflectance
+	float roughness2 = gbuffer.roughness * gbuffer.roughness;
+	vec3 F0 = mix(gbuffer.specular, gbuffer.diffuse, gbuffer.metallic);
+	vec3 F = fresnelSchlickRoughness(F0, roughness2, gbuffer.normal, viewDir);
+
+	float ndotv = max(dot(gbuffer.normal, viewDir), EPSILON);
+	vec2 envBRDF = texture(integrationLut, vec2(gbuffer.roughness, ndotv)).xy;
+	specIndirectTerm = F * envBRDF.x + envBRDF.y;
+
+	vec3 kS = F;
+	vec3 kD = 1.0 - kS;
+	kD *= 1.0 - gbuffer.metallic;
+
+	diffIndirectTerm = kD * gbuffer.diffuse;
+}
+
 // Performs BRDF specular lighting
-vec3 computeSpecularColorBrdf(vec3 v, // view dir
-	vec3 l, // light dir
-	vec3 n, // normal
-	vec3 specCol,
-	vec3 lightSpecCol,
-	float a2, // rougness^2
-	float nol) // N dot L
+vec3 computeSpecularColorBrdf(GbufferInfo gbuffer, vec3 viewDir, vec3 frag2Light, vec3 lightSpecCol)
 {
-	vec3 h = normalize(l + v);
+	float nol = max(0.0, dot(gbuffer.normal, frag2Light));
+	vec3 h = normalize(frag2Light + viewDir);
+	float a2 = gbuffer.roughness * gbuffer.roughness;
 
 	// Fresnel
-	float voh = dot(v, h);
 #if 0
 	// Schlick
-	vec3 F = specCol + (1.0 - specCol) * pow((1.0 + EPSILON - loh), 5.0);
+	vec3 F = gbuffer.specular + (1.0 - gbuffer.specular) * pow((1.0 + EPSILON - loh), 5.0);
 #else
 	// Unreal
-	vec3 F = specCol + (1.0 - specCol) * pow(2.0, (-5.55473 * voh - 6.98316) * voh);
+	float voh = dot(viewDir, h);
+	vec3 F = gbuffer.specular + (1.0 - gbuffer.specular) * pow(2.0, (-5.55473 * voh - 6.98316) * voh);
 #endif
 
 	// D(n,h) aka NDF: GGX Trowbridge-Reitz
-	float noh = dot(n, h);
+	float noh = dot(gbuffer.normal, h);
 	float D = noh * noh * (a2 - 1.0) + 1.0;
 	D = a2 / (PI * D * D);
 
-// G(l,v,h)/(4*dot(n,h)*dot(n,v)) aka Visibility term: Geometric shadowing divided by BRDF denominator
+	// G(frag2Light,viewDir,h)/(4*dot(n,h)*dot(n,viewDir)) aka Visibility term: Geometric shadowing divided by BRDF
+	// denominator
 #if 0
-	float nov = max(EPSILON, dot(n, v));
+	float nov = max(EPSILON, dot(n, viewDir));
 	float V_v = nov + sqrt((nov - nov * a2) * nov + a2);
 	float V_l = nol + sqrt((nol - nol * a2) * nol + a2);
 	float V = 1.0 / (V_l * V_v);
 #else
 	float k = (a2 + 1.0);
 	k = k * k / 8.0;
-	float nov = max(EPSILON, dot(n, v));
+	float nov = max(EPSILON, dot(gbuffer.normal, viewDir));
 	float V_v = nov * (1.0 - k) + k;
 	float V_l = nol * (1.0 - k) + k;
 	float V = 1.0 / (4.0 * V_l * V_v);
@@ -164,33 +188,4 @@ vec3 computeCubemapVecCheap(in vec3 r, in float R2, in vec3 f)
 	return r;
 }
 
-vec3 fresnelSchlickRoughness(float cosTheta, vec3 F0, float roughness)
-{
-	// Make sure that we won't end up with a zero in pow()
-	float a = 1.0 - cosTheta;
-	a = max(a, EPSILON);
-
-	return F0 + (max(vec3(1.0 - roughness), F0) - F0) * pow(a, 5.0);
-}
-
-// Compute the factors that will be used to calculat the final specular and diffuse indirect terms
-void computeSpecAndDiffuseIndirectFactors(
-	vec3 viewDir, GbufferInfo gbuffer, sampler2D integrationLut, out vec3 specIndirectTerm, out vec3 diffIndirectTerm)
-{
-	float ndotv = dot(gbuffer.normal, viewDir);
-
-	// Reflectance
-	vec3 F0 = mix(gbuffer.specular, gbuffer.diffuse, gbuffer.metallic);
-	vec3 F = fresnelSchlickRoughness(max(ndotv, 0.0), F0, gbuffer.roughness);
-
-	vec2 envBRDF = texture(integrationLut, vec2(gbuffer.roughness, ndotv)).xy;
-	specIndirectTerm = F * envBRDF.x + envBRDF.y;
-
-	vec3 kS = F;
-	vec3 kD = 1.0 - kS;
-	kD *= 1.0 - gbuffer.metallic;
-
-	diffIndirectTerm = kD * gbuffer.diffuse;
-}
-
 #endif

+ 0 - 1
shaders/Pack.glsl

@@ -169,7 +169,6 @@ void readRoughnessSpecularFromGBuffer(in sampler2D rt1, in vec2 uv, out float ro
 	// Fix roughness
 	const float MIN_ROUGHNESS = 0.5;
 	roughness = roughness * (1.0 - MIN_ROUGHNESS) + MIN_ROUGHNESS;
-	roughness *= roughness;
 }
 
 // Read from the G buffer