From da283ff3c37629769324926e24a7b203f9901890 Mon Sep 17 00:00:00 2001
From: riperiperi <rhy3756547@hotmail.com>
Date: Mon, 8 Mar 2021 00:12:19 +0000
Subject: [PATCH] Flip component mask if target is BGRA. (#2087)

* Flip component mask if target is BGRA.

* Make mask selection less ugly.
---
 Ryujinx.Graphics.OpenGL/Pipeline.cs | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/Ryujinx.Graphics.OpenGL/Pipeline.cs b/Ryujinx.Graphics.OpenGL/Pipeline.cs
index f42187bdf7..28478a7246 100644
--- a/Ryujinx.Graphics.OpenGL/Pipeline.cs
+++ b/Ryujinx.Graphics.OpenGL/Pipeline.cs
@@ -862,7 +862,14 @@ namespace Ryujinx.Graphics.OpenGL
 
                 _framebuffer.AttachColor(index, color);
 
-                _fpIsBgra[index] = color != null && color.Format.IsBgra8() ? 1 : 0;
+                int isBgra = color != null && color.Format.IsBgra8() ? 1 : 0;
+
+                if (_fpIsBgra[index] != isBgra)
+                {
+                    _fpIsBgra[index] = isBgra;
+
+                    RestoreComponentMask(index);
+                }
             }
 
             UpdateFpIsBgra();
@@ -1233,11 +1240,15 @@ namespace Ryujinx.Graphics.OpenGL
 
         private void RestoreComponentMask(int index)
         {
+            // If the bound render target is bgra, swap the red and blue masks.
+            uint redMask = _fpIsBgra[index] == 0 ? 1u : 4u;
+            uint blueMask = _fpIsBgra[index] == 0 ? 4u : 1u;
+
             GL.ColorMask(
                 index,
-                (_componentMasks[index] & 1u) != 0,
+                (_componentMasks[index] & redMask) != 0,
                 (_componentMasks[index] & 2u) != 0,
-                (_componentMasks[index] & 4u) != 0,
+                (_componentMasks[index] & blueMask) != 0,
                 (_componentMasks[index] & 8u) != 0);
         }