From 20f44fb522099e8e14e42ea0d5dedf76b24b9d6c Mon Sep 17 00:00:00 2001
From: Dave Parks <davep@lindenlab.com>
Date: Fri, 23 Sep 2022 18:58:04 -0500
Subject: [PATCH] SL-18190 Reduce banding - tweak noise function.

---
 .../deferred/postDeferredGammaCorrect.glsl    | 30 +++++++++++++++++--
 1 file changed, 27 insertions(+), 3 deletions(-)

diff --git a/indra/newview/app_settings/shaders/class1/deferred/postDeferredGammaCorrect.glsl b/indra/newview/app_settings/shaders/class1/deferred/postDeferredGammaCorrect.glsl
index 87931e42df7..b61f37fe47f 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/postDeferredGammaCorrect.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/postDeferredGammaCorrect.glsl
@@ -47,6 +47,7 @@ vec3 linear_to_srgb(vec3 cl);
 //	By Morgan McGuire @morgan3d, http://graphicscodex.com
 //
 float hash(float n) { return fract(sin(n) * 1e4); }
+float hash(vec2 p) { return fract(1e4 * sin(17.0 * p.x + p.y * 0.1) * (0.1 + abs(sin(p.y * 13.0 + p.x)))); }
 
 float noise(float x) {
 	float i = floor(x);
@@ -54,6 +55,28 @@ float noise(float x) {
 	float u = f * f * (3.0 - 2.0 * f);
 	return mix(hash(i), hash(i + 1.0), u);
 }
+
+float noise(vec2 x) {
+	vec2 i = floor(x);
+	vec2 f = fract(x);
+
+	// Four corners in 2D of a tile
+	float a = hash(i);
+	float b = hash(i + vec2(1.0, 0.0));
+	float c = hash(i + vec2(0.0, 1.0));
+	float d = hash(i + vec2(1.0, 1.0));
+
+	// Simple 2D lerp using smoothstep envelope between the values.
+	// return vec3(mix(mix(a, b, smoothstep(0.0, 1.0, f.x)),
+	//			mix(c, d, smoothstep(0.0, 1.0, f.x)),
+	//			smoothstep(0.0, 1.0, f.y)));
+
+	// Same code, with the clamps in smoothstep and common subexpressions
+	// optimized away.
+	vec2 u = f * f * (3.0 - 2.0 * f);
+	return mix(a, b, u.x) + (c - a) * u.y * (1.0 - u.x) + (d - b) * u.x * u.y;
+}
+
 //=============================
 
 void main() 
@@ -61,9 +84,10 @@ void main()
     //this is the one of the rare spots where diffuseRect contains linear color values (not sRGB)
     vec4 diff = texture2DRect(diffuseRect, vary_fragcoord);
     diff.rgb = linear_to_srgb(diff.rgb);
-    vec3 seed = diff.rgb*vec3(vary_fragcoord.xy, vary_fragcoord.x+vary_fragcoord.y)*2048;
-    vec3 nz = vec3(noise(seed.r), noise(seed.g), noise(seed.b));
-    diff.rgb += nz*0.005;
+    vec3 seed = (diff.rgb+vec3(1.0))*vec3(vary_fragcoord.xy, vary_fragcoord.x+vary_fragcoord.y);
+    vec3 nz = vec3(noise(seed.rg), noise(seed.gb), noise(seed.rb));
+    diff.rgb += nz*0.008;
+    //diff.rgb = nz;
     frag_color = diff;
 }
 
-- 
GitLab