From 2bf73d5cdbe11af686b4c10c0d6a531f238b27a2 Mon Sep 17 00:00:00 2001
From: Rye Mutt <rye@alchemyviewer.org>
Date: Tue, 7 Jul 2020 23:15:08 -0400
Subject: [PATCH] Clean up repeated access of the gbuffer in shaders

---
 .../shaders/class1/deferred/deferredUtil.glsl | 26 ++++--
 .../shaders/class1/deferred/materialF.glsl    |  2 -
 .../class1/deferred/multiSpotLightF.glsl      |  6 +-
 .../shaders/class1/deferred/softenLightF.glsl |  4 +-
 .../shaders/class1/deferred/spotLightF.glsl   |  8 +-
 .../shaders/class1/deferred/waterF.glsl       |  1 -
 .../class2/deferred/multiSpotLightF.glsl      | 10 +--
 .../shaders/class2/deferred/softenLightF.glsl |  4 +-
 .../shaders/class2/deferred/spotLightF.glsl   |  8 +-
 .../shaders/class3/deferred/deferredUtil.glsl | 79 -------------------
 .../class3/deferred/multiSpotLightF.glsl      | 10 +--
 .../shaders/class3/deferred/softenLightF.glsl |  4 +-
 .../shaders/class3/deferred/spotLightF.glsl   | 10 +--
 13 files changed, 45 insertions(+), 127 deletions(-)
 delete mode 100644 indra/newview/app_settings/shaders/class3/deferred/deferredUtil.glsl

diff --git a/indra/newview/app_settings/shaders/class1/deferred/deferredUtil.glsl b/indra/newview/app_settings/shaders/class1/deferred/deferredUtil.glsl
index e27bbce0948..c5ac67c8eb4 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/deferredUtil.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/deferredUtil.glsl
@@ -39,16 +39,28 @@ vec2 getScreenCoordinate(vec2 screenpos)
     return sc - vec2(1.0, 1.0);
 }
 
+vec3 decode_normal(vec2 enc)
+{
+    vec2 fenc = enc*4-2;
+    float f = dot(fenc,fenc);
+    float g = sqrt(1-f/4);
+    vec3 n;
+    n.xy = fenc*g;
+    n.z = 1-f/2;
+    return n;
+}
+
 vec3 getNorm(vec2 screenpos)
 {
    vec2 enc = texture2DRect(normalMap, screenpos.xy).xy;
-   vec2 fenc = enc*4-2;
-   float f = dot(fenc,fenc);
-   float g = sqrt(1-f/4);
-   vec3 n;
-   n.xy = fenc*g;
-   n.z = 1-f/2;
-   return n;
+   return decode_normal(enc);
+}
+
+vec3 getNormWithEnvIntensity(vec2 screenpos, out float envIntensity)
+{
+   vec3 enc = texture2DRect(normalMap, screenpos.xy).xyz;
+   envIntensity = enc.z;
+   return decode_normal(enc.xy);
 }
 
 float getDepth(vec2 pos_screen)
diff --git a/indra/newview/app_settings/shaders/class1/deferred/materialF.glsl b/indra/newview/app_settings/shaders/class1/deferred/materialF.glsl
index f9dd0b2dd96..d29dda16232 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/materialF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/materialF.glsl
@@ -256,8 +256,6 @@ void main()
 
     norm.xyz = normalize(tnorm.xyz);
 
-    vec2 abnormal = encode_normal(norm.xyz);
-
     vec4 final_color = diffcol;
 
 #if (DIFFUSE_ALPHA_MODE != DIFFUSE_ALPHA_MODE_EMISSIVE)
diff --git a/indra/newview/app_settings/shaders/class1/deferred/multiSpotLightF.glsl b/indra/newview/app_settings/shaders/class1/deferred/multiSpotLightF.glsl
index 9bba45bc4ea..f5ec7d87e9e 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/multiSpotLightF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/multiSpotLightF.glsl
@@ -71,7 +71,7 @@ VARYING vec4 vary_fragcoord;
 uniform vec2 screen_res;
 
 uniform mat4 inv_proj;
-vec3 getNorm(vec2 pos_screen);
+vec3 getNormWithEnvIntensity(vec2 screenpos, out float envIntensity);
 vec3 srgb_to_linear(vec3 c);
 
 vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
@@ -149,8 +149,8 @@ void main()
 		discard;
 	}
 		
-	float envIntensity = texture2DRect(normalMap, frag.xy).z;
-	vec3 norm = getNorm(frag.xy);
+	float envIntensity;
+	vec3 norm = getNormWithEnvIntensity(frag.xy, envIntensity);
 
 	float l_dist = -dot(lv, proj_n);
 	
diff --git a/indra/newview/app_settings/shaders/class1/deferred/softenLightF.glsl b/indra/newview/app_settings/shaders/class1/deferred/softenLightF.glsl
index a5804220bc0..607a635b673 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/softenLightF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/softenLightF.glsl
@@ -56,7 +56,7 @@ VARYING vec2 vary_fragcoord;
 uniform mat4 inv_proj;
 uniform vec2 screen_res;
 
-vec3 getNorm(vec2 pos_screen);
+vec3 decode_normal(vec2 enc);
 vec4 getPositionWithDepth(vec2 pos_screen, float depth);
 
 void calcAtmosphericVars(vec3 inPositionEye, vec3 light_dir, float ambFactor, out vec3 sunlit, out vec3 amblit, out vec3 additive, out vec3 atten, bool use_ao);
@@ -80,7 +80,7 @@ void main()
     vec4 pos = getPositionWithDepth(tc, depth);
     vec4 norm = texture2DRect(normalMap, tc);
     float envIntensity = norm.z;
-    norm.xyz = getNorm(tc);
+    norm.xyz = decode_normal(norm.xy);
     
     vec3 light_dir = (sun_up_factor == 1) ? sun_dir : moon_dir;
     float da = clamp(dot(norm.xyz, light_dir.xyz), 0.0, 1.0);
diff --git a/indra/newview/app_settings/shaders/class1/deferred/spotLightF.glsl b/indra/newview/app_settings/shaders/class1/deferred/spotLightF.glsl
index 694b19cdfb7..a8755038e2f 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/spotLightF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/spotLightF.glsl
@@ -70,7 +70,7 @@ uniform vec2 screen_res;
 
 uniform mat4 inv_proj;
 
-vec3 getNorm(vec2 pos_screen);
+vec3 getNormWithEnvIntensity(vec2 screenpos, out float envIntensity);
 vec3 srgb_to_linear(vec3 c);
 
 vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
@@ -148,10 +148,8 @@ void main()
 		discard;
 	}
 		
-	vec3 norm = texture2DRect(normalMap, frag.xy).xyz;
-	float envIntensity = norm.z;
-	norm = getNorm(frag.xy);
-	norm = normalize(norm);
+	float envIntensity;
+	vec3 norm = getNormWithEnvIntensity(frag.xy, envIntensity);
 	float l_dist = -dot(lv, proj_n);
 	
 	vec4 proj_tc = (proj_mat * vec4(pos.xyz, 1.0));
diff --git a/indra/newview/app_settings/shaders/class1/deferred/waterF.glsl b/indra/newview/app_settings/shaders/class1/deferred/waterF.glsl
index 6f0882bcf0b..37afad6100a 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/waterF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/waterF.glsl
@@ -64,7 +64,6 @@ VARYING vec4 view;
 VARYING vec4 vary_position;
 
 vec2 encode_normal(vec3 n);
-vec3 scaleSoftClip(vec3 l);
 vec3 srgb_to_linear(vec3 c);
 vec3 linear_to_srgb(vec3 c);
 
diff --git a/indra/newview/app_settings/shaders/class2/deferred/multiSpotLightF.glsl b/indra/newview/app_settings/shaders/class2/deferred/multiSpotLightF.glsl
index 5d7a28c3592..3431a589d2c 100644
--- a/indra/newview/app_settings/shaders/class2/deferred/multiSpotLightF.glsl
+++ b/indra/newview/app_settings/shaders/class2/deferred/multiSpotLightF.glsl
@@ -72,7 +72,7 @@ uniform vec2 screen_res;
 uniform mat4 inv_proj;
 
 vec3 srgb_to_linear(vec3 cs);
-vec3 getNorm(vec2 pos_screen);
+vec3 getNormWithEnvIntensity(vec2 screenpos, out float envIntensity);
 
 vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
 {
@@ -160,13 +160,9 @@ void main()
         shadow = clamp(shadow, 0.0, 1.0);        
     }
     
-    vec3 norm = texture2DRect(normalMap, frag.xy).xyz;
+    float envIntensity;
+    vec3 norm = getNormWithEnvIntensity(frag.xy, envIntensity);
     
-    float envIntensity = norm.z;
-
-    norm = getNorm(frag.xy);
-    
-    norm = normalize(norm);
     float l_dist = -dot(lv, proj_n);
     
     vec4 proj_tc = (proj_mat * vec4(pos.xyz, 1.0));
diff --git a/indra/newview/app_settings/shaders/class2/deferred/softenLightF.glsl b/indra/newview/app_settings/shaders/class2/deferred/softenLightF.glsl
index b0dff0c6283..ee472e8dfaf 100644
--- a/indra/newview/app_settings/shaders/class2/deferred/softenLightF.glsl
+++ b/indra/newview/app_settings/shaders/class2/deferred/softenLightF.glsl
@@ -56,7 +56,6 @@ VARYING vec2 vary_fragcoord;
 uniform mat4 inv_proj;
 uniform vec2 screen_res;
 
-vec3 getNorm(vec2 pos_screen);
 vec4 getPositionWithDepth(vec2 pos_screen, float depth);
 
 void calcAtmosphericVars(vec3 inPositionEye, vec3 light_dir, float ambFactor, out vec3 sunlit, out vec3 amblit, out vec3 additive, out vec3 atten, bool use_ao);
@@ -66,6 +65,7 @@ vec3 scaleSoftClipFrag(vec3 l);
 vec3 fullbrightAtmosTransportFrag(vec3 light, vec3 additive, vec3 atten);
 vec3 fullbrightScaleSoftClip(vec3 light);
 
+vec3 decode_normal(vec2 enc);
 vec3 linear_to_srgb(vec3 c);
 vec3 srgb_to_linear(vec3 c);
 
@@ -80,7 +80,7 @@ void main()
     vec4 pos = getPositionWithDepth(tc, depth);
     vec4 norm = texture2DRect(normalMap, tc);
     float envIntensity = norm.z;
-    norm.xyz = getNorm(tc);
+    norm.xyz = decode_normal(norm.xy);
     
     vec3 light_dir = (sun_up_factor == 1) ? sun_dir : moon_dir;
     float da = clamp(dot(norm.xyz, light_dir.xyz), 0.0, 1.0);
diff --git a/indra/newview/app_settings/shaders/class2/deferred/spotLightF.glsl b/indra/newview/app_settings/shaders/class2/deferred/spotLightF.glsl
index 5ab0b5c5b4d..f68e0c1ab61 100644
--- a/indra/newview/app_settings/shaders/class2/deferred/spotLightF.glsl
+++ b/indra/newview/app_settings/shaders/class2/deferred/spotLightF.glsl
@@ -71,7 +71,7 @@ uniform vec2 screen_res;
 
 uniform mat4 inv_proj;
 
-vec3 getNorm(vec2 pos_screen);
+vec3 getNormWithEnvIntensity(vec2 screenpos, out float envIntensity);
 vec3 srgb_to_linear(vec3 c);
 
 vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
@@ -160,11 +160,9 @@ void main()
 		shadow = clamp(shadow, 0.0, 1.0);        
 	}
 	
-	vec3 norm = texture2DRect(normalMap, frag.xy).xyz;
-	float envIntensity = norm.z;
-	norm = getNorm(frag.xy);
+    float envIntensity;
+    vec3 norm = getNormWithEnvIntensity(frag.xy, envIntensity);
 	
-	norm = normalize(norm);
 	float l_dist = -dot(lv, proj_n);
 	
 	vec4 proj_tc = (proj_mat * vec4(pos.xyz, 1.0));
diff --git a/indra/newview/app_settings/shaders/class3/deferred/deferredUtil.glsl b/indra/newview/app_settings/shaders/class3/deferred/deferredUtil.glsl
deleted file mode 100644
index e27bbce0948..00000000000
--- a/indra/newview/app_settings/shaders/class3/deferred/deferredUtil.glsl
+++ /dev/null
@@ -1,79 +0,0 @@
-/** 
- * @file class1/deferred/deferredUtil.glsl
- *
- * $LicenseInfo:firstyear=2007&license=viewerlgpl$
- * Second Life Viewer Source Code
- * Copyright (C) 2007, Linden Research, Inc.
- * 
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation;
- * version 2.1 of the License only.
- * 
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- * 
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
- * 
- * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
- * $/LicenseInfo$
- */
-
-uniform sampler2DRect   normalMap;
-uniform sampler2DRect   depthMap;
-
-uniform mat4 inv_proj;
-uniform vec2 screen_res;
-
-vec2 getScreenCoordinate(vec2 screenpos)
-{
-    vec2 sc = screenpos.xy * 2.0;
-    if (screen_res.x > 0 && screen_res.y > 0)
-    {
-       sc /= screen_res;
-    }
-    return sc - vec2(1.0, 1.0);
-}
-
-vec3 getNorm(vec2 screenpos)
-{
-   vec2 enc = texture2DRect(normalMap, screenpos.xy).xy;
-   vec2 fenc = enc*4-2;
-   float f = dot(fenc,fenc);
-   float g = sqrt(1-f/4);
-   vec3 n;
-   n.xy = fenc*g;
-   n.z = 1-f/2;
-   return n;
-}
-
-float getDepth(vec2 pos_screen)
-{
-    float depth = texture2DRect(depthMap, pos_screen).r;
-    return depth;
-}
-
-vec4 getPosition(vec2 pos_screen)
-{
-    float depth = getDepth(pos_screen);
-    vec2 sc = getScreenCoordinate(pos_screen);
-    vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
-    vec4 pos = inv_proj * ndc;
-    pos /= pos.w;
-    pos.w = 1.0;
-    return pos;
-}
-
-vec4 getPositionWithDepth(vec2 pos_screen, float depth)
-{
-    vec2 sc = getScreenCoordinate(pos_screen);
-    vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
-    vec4 pos = inv_proj * ndc;
-    pos /= pos.w;
-    pos.w = 1.0;
-    return pos;
-}
diff --git a/indra/newview/app_settings/shaders/class3/deferred/multiSpotLightF.glsl b/indra/newview/app_settings/shaders/class3/deferred/multiSpotLightF.glsl
index 9d62b9d1804..dee97a8e515 100644
--- a/indra/newview/app_settings/shaders/class3/deferred/multiSpotLightF.glsl
+++ b/indra/newview/app_settings/shaders/class3/deferred/multiSpotLightF.glsl
@@ -71,7 +71,7 @@ uniform vec2 screen_res;
 
 uniform mat4 inv_proj;
 
-vec3 getNorm(vec2 pos_screen);
+vec3 getNormWithEnvIntensity(vec2 screenpos, out float envIntensity);
 
 vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
 {
@@ -151,13 +151,9 @@ void main()
         shadow = clamp(shadow, 0.0, 1.0);        
     }
     
-    vec3 norm = texture2DRect(normalMap, frag.xy).xyz;
-    
-    float envIntensity = norm.z;
+    float envIntensity;
+    vec3 norm = getNormWithEnvIntensity(frag.xy, envIntensity);
 
-    norm = getNorm(frag.xy);
-    
-    norm = normalize(norm);
     float l_dist = -dot(lv, proj_n);
     
     vec4 proj_tc = (proj_mat * vec4(pos.xyz, 1.0));
diff --git a/indra/newview/app_settings/shaders/class3/deferred/softenLightF.glsl b/indra/newview/app_settings/shaders/class3/deferred/softenLightF.glsl
index 7ed9e7b4fc6..5e9ea6ddcb5 100644
--- a/indra/newview/app_settings/shaders/class3/deferred/softenLightF.glsl
+++ b/indra/newview/app_settings/shaders/class3/deferred/softenLightF.glsl
@@ -79,6 +79,8 @@ vec4 getPositionWithDepth(vec2 pos_screen, float depth);
 vec4 getPosition(vec2 pos_screen);
 vec3 getNorm(vec2 pos_screen);
 
+vec3 decode_normal(vec2 enc);
+
 #ifdef WATER_FOG
 vec4 applyWaterFogView(vec3 pos, vec4 color);
 #endif
@@ -90,7 +92,7 @@ void main()
     vec3 pos = getPositionWithDepth(tc, depth).xyz;
     vec4 norm = texture2DRect(normalMap, tc);
     float envIntensity = norm.z;
-    norm.xyz = getNorm(tc);
+    norm.xyz = decode_normal(norm.xy);
 
     float da = max(dot(norm.xyz, sun_dir.xyz), 0.0);
 
diff --git a/indra/newview/app_settings/shaders/class3/deferred/spotLightF.glsl b/indra/newview/app_settings/shaders/class3/deferred/spotLightF.glsl
index 56b0f4e5cec..10e9fe7b6d6 100644
--- a/indra/newview/app_settings/shaders/class3/deferred/spotLightF.glsl
+++ b/indra/newview/app_settings/shaders/class3/deferred/spotLightF.glsl
@@ -71,7 +71,7 @@ uniform vec2 screen_res;
 
 uniform mat4 inv_proj;
 
-vec3 getNorm(vec2 pos_screen);
+vec3 getNormWithEnvIntensity(vec2 screenpos, out float envIntensity);
 
 vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
 {
@@ -151,11 +151,9 @@ void main()
         shadow = clamp(shadow, 0.0, 1.0);
     }
     
-    vec3 norm = texture2DRect(normalMap, frag.xy).xyz;
-    float envIntensity = norm.z;
-    norm = getNorm(frag.xy);
-    
-    norm = normalize(norm);
+    float envIntensity;
+    vec3 norm = getNormWithEnvIntensity(frag.xy, envIntensity);
+
     float l_dist = -dot(lv, proj_n);
     
     vec4 proj_tc = (proj_mat * vec4(pos.xyz, 1.0));
-- 
GitLab