From 5db6bd6054c6d535142102a3f54d8fa239f33b1c Mon Sep 17 00:00:00 2001
From: Tillmann Karras <tilkax@gmail.com>
Date: Tue, 11 Feb 2025 17:32:25 +0000
Subject: [PATCH] VideoCommon: simplify dither calculation

This saves three instructions on AMD GPUs. Dunno about Nvidia.
---
 Source/Core/VideoCommon/PixelShaderGen.cpp  | 4 ++--
 Source/Core/VideoCommon/UberShaderPixel.cpp | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/Source/Core/VideoCommon/PixelShaderGen.cpp b/Source/Core/VideoCommon/PixelShaderGen.cpp
index a76681634f..0b027b2496 100644
--- a/Source/Core/VideoCommon/PixelShaderGen.cpp
+++ b/Source/Core/VideoCommon/PixelShaderGen.cpp
@@ -1308,9 +1308,9 @@ ShaderCode GeneratePixelShaderCode(APIType api_type, const ShaderHostConfig& hos
   if (uid_data->dither)
   {
     // Flipper uses a standard 2x2 Bayer Matrix for 6 bit dithering
-    // Here the matrix is encoded into the two factor constants
     out.Write("\tint2 dither = int2(rawpos.xy) & 1;\n");
-    out.Write("\tprev.rgb = (prev.rgb - (prev.rgb >> 6)) + abs(dither.y * 3 - dither.x * 2);\n");
+    out.Write(
+        "\tprev.rgb = (prev.rgb - (prev.rgb >> 6)) + (dither.x ^ dither.y) * 2 + dither.y;\n");
   }
 
   WriteFog(out, uid_data);
diff --git a/Source/Core/VideoCommon/UberShaderPixel.cpp b/Source/Core/VideoCommon/UberShaderPixel.cpp
index ca8f42ec8f..f9674aa5a3 100644
--- a/Source/Core/VideoCommon/UberShaderPixel.cpp
+++ b/Source/Core/VideoCommon/UberShaderPixel.cpp
@@ -1434,8 +1434,8 @@ ShaderCode GenPixelShader(APIType api_type, const ShaderHostConfig& host_config,
             "    // Flipper uses a standard 2x2 Bayer Matrix for 6 bit dithering\n"
             "    // Here the matrix is encoded into the two factor constants\n"
             "    int2 dither = int2(rawpos.xy) & 1;\n"
-            "    TevResult.rgb = (TevResult.rgb - (TevResult.rgb >> 6)) + abs(dither.y * 3 - "
-            "dither.x * 2);\n"
+            "    TevResult.rgb = (TevResult.rgb - (TevResult.rgb >> 6)) + (dither.x ^ dither.y) * "
+            "2 + dither.y;\n"
             "  }}\n\n");
 
   // =========