From 7e3b7f9b1418b33b9eb93f921756d38264b3b8ff Mon Sep 17 00:00:00 2001
From: Rye Mutt <rye@alchemyviewer.org>
Date: Thu, 6 Apr 2023 01:28:12 -0400
Subject: [PATCH] More rlv sphere shader fixes

---
 .../shaders/class1/deferred/rlvF.glsl          | 18 +++++++++---------
 .../shaders/class1/deferred/rlvFLegacy.glsl    | 15 +++++++--------
 .../shaders/class1/deferred/rlvV.glsl          |  5 +----
 3 files changed, 17 insertions(+), 21 deletions(-)

diff --git a/indra/newview/app_settings/shaders/class1/deferred/rlvF.glsl b/indra/newview/app_settings/shaders/class1/deferred/rlvF.glsl
index 2aa54d74666..79b6d5c9160 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/rlvF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/rlvF.glsl
@@ -14,6 +14,8 @@
  *
  */
 
+/*[EXTRA_CODE_HERE]*/
+
 #ifdef DEFINE_GL_FRAGCOLOR
 	out vec4 frag_color;
 #else
@@ -43,6 +45,9 @@ uniform vec2 rlvEffectParam5;   // Blur direction (not used for blend)
 #define SPHERE_PARAMS		rlvEffectParam4
 #define BLUR_DIRECTION		rlvEffectParam5.xy
 
+vec4 getPositionWithDepth(vec2 pos_screen, float depth);
+float getDepth(vec2 pos_screen);
+
 vec3 blur13(sampler2D source, vec2 tc, vec2 direction)
 {
   vec4 color = vec4(0.0);
@@ -120,8 +125,6 @@ void main()
 	effectStrength = mix(effectStrength, mix(0, SPHERE_VALUEMIN, SPHERE_DISTEXTEND.x), distance < SPHERE_DISTMIN);
 	effectStrength = mix(effectStrength, mix(0, SPHERE_VALUEMAX, SPHERE_DISTEXTEND.y), distance > SPHERE_DISTMAX);
 
-
-
 	vec3 fragColor ;
 	switch (rlvEffectMode)
 	{
@@ -141,13 +144,10 @@ void main()
 			break;
 		case 4:		// Pixelate
 			{
-				if (effectStrength > 0)
-				{
-					effectStrength = sign(effectStrength);
-					vec2 pixelSize = vec2(1.0) / round(SPHERE_PARAMS.xy * vec2(effectStrength)); 
-					fragTC = (floor(fragTC / pixelSize) * pixelSize) + 0.5 / screen_res;
-				}
-
+				effectStrength = sign(effectStrength);
+				float pixelWidth = max(1, round(SPHERE_PARAMS.x * effectStrength)) / screen_res.x; 
+				float pixelHeight = max(1, round(SPHERE_PARAMS.y * effectStrength)) / screen_res.y;
+				fragTC = vec2(pixelWidth * floor(fragTC.x / pixelWidth), pixelHeight * floor(fragTC.y / pixelHeight));
 				fragColor = texture(diffuseRect, fragTC).rgb;
 			}
 			break;
diff --git a/indra/newview/app_settings/shaders/class1/deferred/rlvFLegacy.glsl b/indra/newview/app_settings/shaders/class1/deferred/rlvFLegacy.glsl
index 919fd0be499..1a055e009cd 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/rlvFLegacy.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/rlvFLegacy.glsl
@@ -45,6 +45,9 @@ uniform vec2 rlvEffectParam5;   // Blur direction (not used for blend)
 #define SPHERE_PARAMS		rlvEffectParam4
 #define BLUR_DIRECTION		rlvEffectParam5.xy
 
+vec4 getPositionWithDepth(vec2 pos_screen, float depth);
+float getDepth(vec2 pos_screen);
+
 vec3 blur13(sampler2D source, vec2 tc, vec2 direction)
 {
   vec4 color = vec4(0.0);
@@ -114,7 +117,6 @@ vec3 chromaticAberration(sampler2D source, vec2 tc, vec2 redDrift, vec2 blueDrif
 void main()
 {
 	vec2 fragTC = vary_fragcoord.xy;
-	float fragDepth = texture(depthMap, fragTC).x;
 	vec3 fragPosLocal = getPositionWithDepth(fragTC, getDepth(fragTC)).xyz;
 	float distance = length(fragPosLocal.xyz - SPHERE_ORIGIN);
 
@@ -153,13 +155,10 @@ void main()
 	}
 	else if (rlvEffectMode == 4)		// Pixelate
 	{
-		if (effectStrength > 0)
-		{
-			effectStrength = sign(effectStrength);
-			vec2 pixelSize = vec2(1.0) / round(SPHERE_PARAMS.xy * vec2(effectStrength)); 
-			fragTC = (floor(fragTC / pixelSize) * pixelSize) + 0.5 / screen_res;
-		}
-
+		effectStrength = sign(effectStrength);
+		float pixelWidth = max(1, round(SPHERE_PARAMS.x * effectStrength)) / screen_res.x; 
+		float pixelHeight = max(1, round(SPHERE_PARAMS.y * effectStrength)) / screen_res.y;
+		fragTC = vec2(pixelWidth * floor(fragTC.x / pixelWidth), pixelHeight * floor(fragTC.y / pixelHeight));
 		fragColor = texture(diffuseRect, fragTC).rgb;
 	}
 
diff --git a/indra/newview/app_settings/shaders/class1/deferred/rlvV.glsl b/indra/newview/app_settings/shaders/class1/deferred/rlvV.glsl
index 19124b61012..a1d3859387c 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/rlvV.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/rlvV.glsl
@@ -19,7 +19,6 @@ ATTRIBUTE vec3 position;
 uniform vec2 screen_res;
 
 VARYING vec2 vary_fragcoord;
-VARYING vec3 vary_position;
 
 void main()
 {
@@ -27,7 +26,5 @@ void main()
 	vec4 pos = vec4(position.xyz, 1.0);
 	gl_Position = pos; 
 
-
-	vary_fragcoord = (pos.xy*0.5+0.5)*screen_res;
-	vary_position = (vec4(1, 0, 0, 1.0)).xyz;
+	vary_fragcoord = (pos.xy*0.5+0.5);
 }
-- 
GitLab