From c0454b5da65dd2503381f1137db581be22ad83e3 Mon Sep 17 00:00:00 2001
From: Rye Mutt <rye@alchemyviewer.org>
Date: Tue, 9 Jan 2024 15:06:44 -0500
Subject: [PATCH] Temp revert of multiscattering until NaN issue resolved
 during alpha blend

---
 .../shaders/class1/deferred/deferredUtil.glsl         | 11 ++++-------
 .../shaders/class1/deferred/genbrdflutF.glsl          |  8 ++++----
 .../shaders/class1/interface/radianceGenF.glsl        |  4 ++--
 .../shaders/class3/environment/waterF.glsl            |  4 ++--
 4 files changed, 12 insertions(+), 15 deletions(-)

diff --git a/indra/newview/app_settings/shaders/class1/deferred/deferredUtil.glsl b/indra/newview/app_settings/shaders/class1/deferred/deferredUtil.glsl
index 5499ee18fcc..1d21db78000 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/deferredUtil.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/deferredUtil.glsl
@@ -381,17 +381,14 @@ vec3 pbrIbl(vec3 diffuseColor,
             float perceptualRough)
 {
     // retrieve a scale and bias to F0. See [1], Figure 3
-    vec2 brdf = BRDF(clamp(nv, 0, 1), perceptualRough);
+	vec2 brdf = BRDF(clamp(nv, 0, 1), 1.0-perceptualRough);
     vec3 diffuseLight = irradiance;
     vec3 specularLight = radiance;
     
-    vec3 energy = mix(brdf.xxx, brdf.yyy, specularColor);
-
-    vec3 diffuse = diffuseLight * diffuseColor * (1.0 - energy);
-    vec3 specular = specularLight * energy;
-
-    specular *= computeSpecularAO(nv, ao, perceptualRough * perceptualRough) * (1.0 + specularColor * (1.0 / brdf.y - 1.0));
+	vec3 diffuse = diffuseLight * diffuseColor;
+	vec3 specular = specularLight * (specularColor * brdf.x + brdf.y);
 
+    specular *= computeSpecularAO(nv, ao, perceptualRough * perceptualRough);
     return (diffuse * ao) + specular;
 }
 
diff --git a/indra/newview/app_settings/shaders/class1/deferred/genbrdflutF.glsl b/indra/newview/app_settings/shaders/class1/deferred/genbrdflutF.glsl
index 536538edd64..efc38b3020b 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/genbrdflutF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/genbrdflutF.glsl
@@ -84,7 +84,7 @@ vec2 hammersley2d(uint i, uint N)
 vec3 importanceSample_GGX(vec2 Xi, float a2, vec3 normal) 
 {
 	// Maps a 2D point to a hemisphere with spread based on roughness
-	float phi = 2.0 * PI * Xi.x;
+	float phi = 2.0 * PI * Xi.x + random(normal.xz) * 0.1;
 	float cosTheta = sqrt((1.0 - Xi.y) / (1.0 + (a2 - 1.0) * Xi.y));
 	float sinTheta = sqrt(1.0 - cosTheta * cosTheta);
 	vec3 H = vec3(sinTheta * cos(phi), sinTheta * sin(phi), cosTheta);
@@ -92,7 +92,7 @@ vec3 importanceSample_GGX(vec2 Xi, float a2, vec3 normal)
 	// Tangent space
 	vec3 up = abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
 	vec3 tangentX = normalize(cross(up, normal));
-	vec3 tangentY = cross(normal, tangentX);
+	vec3 tangentY = normalize(cross(normal, tangentX));
 
 	// Convert to world Space
 	return normalize(tangentX * H.x + tangentY * H.y + normal * H.z);
@@ -127,7 +127,7 @@ vec2 BRDF(float NoV, float roughness)
 			float G = V_SmithGGXCorrelated(NoV, dotNL, a2);
 			float G_Vis = G * dotVH / dotNH;
 			float Fc = pow(1.0 - dotVH, 5.0);
-			LUT += vec2(Fc * G_Vis, G_Vis);
+			LUT += vec2((1.0 - Fc) * G_Vis, Fc * G_Vis);
 		}
 	}
 	return LUT / float(NUM_SAMPLES);
@@ -135,5 +135,5 @@ vec2 BRDF(float NoV, float roughness)
 
 void main() 
 {
-	outColor = vec4(BRDF(vary_uv.s, vary_uv.t), 0.0, 1.0);
+	outColor = vec4(BRDF(vary_uv.s, 1.0-vary_uv.t), 0.0, 1.0);
 }
diff --git a/indra/newview/app_settings/shaders/class1/interface/radianceGenF.glsl b/indra/newview/app_settings/shaders/class1/interface/radianceGenF.glsl
index 2076945a1b2..9ecdf0bf770 100644
--- a/indra/newview/app_settings/shaders/class1/interface/radianceGenF.glsl
+++ b/indra/newview/app_settings/shaders/class1/interface/radianceGenF.glsl
@@ -98,7 +98,7 @@ vec3 importanceSample_GGX(vec2 Xi, float roughness, vec3 normal)
 {
 	// Maps a 2D point to a hemisphere with spread based on roughness
 	float alpha = roughness * roughness;
-	float phi = 2.0 * PI * Xi.x;
+	float phi = 2.0 * PI * Xi.x + random(normal.xz) * 0.1;
 	float cosTheta = sqrt((1.0 - Xi.y) / (1.0 + (alpha*alpha - 1.0) * Xi.y));
 	float sinTheta = sqrt(1.0 - cosTheta * cosTheta);
 	vec3 H = vec3(sinTheta * cos(phi), sinTheta * sin(phi), cosTheta);
@@ -106,7 +106,7 @@ vec3 importanceSample_GGX(vec2 Xi, float roughness, vec3 normal)
 	// Tangent space
 	vec3 up = abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
 	vec3 tangentX = normalize(cross(up, normal));
-	vec3 tangentY = cross(normal, tangentX);
+	vec3 tangentY = normalize(cross(normal, tangentX));
 
 	// Convert to world Space
 	return normalize(tangentX * H.x + tangentY * H.y + normal * H.z);
diff --git a/indra/newview/app_settings/shaders/class3/environment/waterF.glsl b/indra/newview/app_settings/shaders/class3/environment/waterF.glsl
index 34281281536..b364e454e8f 100644
--- a/indra/newview/app_settings/shaders/class3/environment/waterF.glsl
+++ b/indra/newview/app_settings/shaders/class3/environment/waterF.glsl
@@ -263,8 +263,8 @@ void main()
     color += ibl;
 
     float nv = clamp(abs(dot(norm.xyz, v)), 0.001, 1.0);
-    vec2 brdf = BRDF(clamp(nv, 0, 1), 0.0);
-    float f = 1.0-brdf.x; //1.0 - (brdf.x+brdf.y);
+    vec2 brdf = BRDF(clamp(nv, 0, 1), 1.0);
+    float f = 1.0-brdf.y; //1.0 - (brdf.x+brdf.y);
     f *= 0.9;
     f *= f;
 
-- 
GitLab