From 9906feefbd37ebfd658fecc47e960f23adc6b190 Mon Sep 17 00:00:00 2001
From: Subv <subv2112@gmail.com>
Date: Wed, 28 Jun 2017 12:43:00 -0500
Subject: [PATCH] SwRasterizer/Lighting: Move the clamp highlight calculation
 to the end of the per-light loop body.

---
 src/video_core/swrasterizer/rasterizer.cpp | 34 +++++++++++-----------
 1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/src/video_core/swrasterizer/rasterizer.cpp b/src/video_core/swrasterizer/rasterizer.cpp
index b2d2b6ef21..2c7a1a8156 100644
--- a/src/video_core/swrasterizer/rasterizer.cpp
+++ b/src/video_core/swrasterizer/rasterizer.cpp
@@ -163,14 +163,6 @@ std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors(
 
         light_vector.Normalize();
 
-        auto LV_N = Math::Dot(light_vector, normal);
-        auto dot_product = LV_N;
-
-        if (light_config.config.two_sided_diffuse)
-            dot_product = std::abs(dot_product);
-        else
-            dot_product = std::max(dot_product, 0.0f);
-
         float dist_atten = 1.0f;
         if (!lighting.IsDistAttenDisabled(num)) {
             auto distance = (-view - position).Length();
@@ -187,15 +179,6 @@ std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors(
             dist_atten = LookupLightingLut(g_state.lighting, lut, lutindex, delta);
         }
 
-        float clamp_highlights = 1.0f;
-
-        if (lighting.config0.clamp_highlights) {
-            if (LV_N <= 0.f)
-                clamp_highlights = 0.f;
-            else
-                clamp_highlights = 1.f;
-        }
-
         auto GetLutIndex = [&](unsigned num, LightingRegs::LightingLutInput input,
                                bool abs) -> std::tuple<u8, float> {
 
@@ -386,6 +369,23 @@ std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors(
             }
         }
 
+        auto dot_product = Math::Dot(light_vector, normal);
+
+        // Calculate clamp highlights before applying the two-sided diffuse configuration to the dot
+        // product.
+        float clamp_highlights = 1.0f;
+        if (lighting.config0.clamp_highlights) {
+            if (dot_product <= 0.f)
+                clamp_highlights = 0.f;
+            else
+                clamp_highlights = 1.f;
+        }
+
+        if (light_config.config.two_sided_diffuse)
+            dot_product = std::abs(dot_product);
+        else
+            dot_product = std::max(dot_product, 0.0f);
+
         auto diffuse =
             light_config.diffuse.ToVec3f() * dot_product + light_config.ambient.ToVec3f();
         diffuse_sum += Math::MakeVec(diffuse * dist_atten, 0.0f);