From 823fcddcb508fac6d1f819633f6a91230f06b114 Mon Sep 17 00:00:00 2001
From: Cosmic Linden <cosmic@lindenlab.com>
Date: Fri, 13 Oct 2023 10:42:02 -0700
Subject: DRTVWR-592: (WIP) (has debug) Fix removing axial sign flip of normal
 texture during zero check, add another zero check

---
 .../app_settings/shaders/class1/deferred/pbrterrainUtilF.glsl | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

(limited to 'indra/newview/app_settings/shaders/class1/deferred')

diff --git a/indra/newview/app_settings/shaders/class1/deferred/pbrterrainUtilF.glsl b/indra/newview/app_settings/shaders/class1/deferred/pbrterrainUtilF.glsl
index fc04afe513..c5de74604f 100644
--- a/indra/newview/app_settings/shaders/class1/deferred/pbrterrainUtilF.glsl
+++ b/indra/newview/app_settings/shaders/class1/deferred/pbrterrainUtilF.glsl
@@ -80,7 +80,7 @@ vec4 _t_texture(sampler2D tex, vec2 uv_unflipped, float sign_or_zero)
 {
     // Handle case where sign is 0
     float sign = (2.0*sign_or_zero) + 1.0;
-    sign /= sign;
+    sign /= abs(sign);
     // If the vertex normal is negative, flip the texture back
     // right-side up.
     vec2 uv = uv_unflipped * vec2(sign, 1);
@@ -190,8 +190,8 @@ TerrainSample _t_sample(sampler2D tex, TerrainCoord terrain_coord, TerrainWeight
             ts.z = ts.x;
             break;
     }
-#else // TODO: Remove debug
-#if 0
+#else
+#if 0 // TODO: Remove debug
 // This demonstrates the case when the bug does not occur: Sampling beforehand and assigning in switch..case
 // This otherwise uses the same logic as in the case that reproduces the bug.
 #define do_sample_x() _t_texture(tex, terrain_coord[0].zw, sign(vary_vertex_normal.x))
@@ -295,10 +295,15 @@ TerrainSampleNormal _t_sample_n(sampler2D tex, TerrainCoord terrain_coord, Terra
 {
     TerrainSample ts = _t_sample(tex, terrain_coord, tw);
     TerrainSampleNormal tsn;
+    // Unpack normals
     tsn.x = ts.x.xyz*2.0-1.0;
     tsn.y = ts.y.xyz*2.0-1.0;
     tsn.z = ts.z.xyz*2.0-1.0;
+    // Get sign
     vec3 ns = sign(vary_vertex_normal);
+    // Handle case where sign is 0
+    ns = (2.0*ns) + 1.0;
+    ns /= abs(ns);
     // If the sign is negative, rotate normal by 180 degrees
     tsn.x.xy = (min(0, ns.x) * tsn.x.xy) + (min(0, -ns.x) * -tsn.x.xy);
     tsn.y.xy = (min(0, ns.y) * tsn.y.xy) + (min(0, -ns.y) * -tsn.y.xy);
-- 
cgit v1.2.3