From 22f138628bd05419791d3b569ec558bbf322398d Mon Sep 17 00:00:00 2001
From: gdkchan <gab.dark.100@gmail.com>
Date: Thu, 28 Jun 2018 00:11:49 -0300
Subject: [PATCH] Somewhat better ClearBuffers implementation

---
 Ryujinx.Graphics/Gal/IGalRasterizer.cs       |  2 +-
 Ryujinx.Graphics/Gal/OpenGL/OGLRasterizer.cs | 19 +++++++++----------
 Ryujinx.HLE/Gpu/Engines/NvGpuEngine3d.cs     | 14 ++++++--------
 3 files changed, 16 insertions(+), 19 deletions(-)

diff --git a/Ryujinx.Graphics/Gal/IGalRasterizer.cs b/Ryujinx.Graphics/Gal/IGalRasterizer.cs
index 1323372a26..45f92f27f1 100644
--- a/Ryujinx.Graphics/Gal/IGalRasterizer.cs
+++ b/Ryujinx.Graphics/Gal/IGalRasterizer.cs
@@ -2,7 +2,7 @@ namespace Ryujinx.Graphics.Gal
 {
     public interface IGalRasterizer
     {
-        void ClearBuffers(int RtIndex, GalClearBufferFlags Flags);
+        void ClearBuffers(GalClearBufferFlags Flags);
 
         bool IsVboCached(long Key, long DataSize);
 
diff --git a/Ryujinx.Graphics/Gal/OpenGL/OGLRasterizer.cs b/Ryujinx.Graphics/Gal/OpenGL/OGLRasterizer.cs
index 0525069690..ebd1e8d1c5 100644
--- a/Ryujinx.Graphics/Gal/OpenGL/OGLRasterizer.cs
+++ b/Ryujinx.Graphics/Gal/OpenGL/OGLRasterizer.cs
@@ -70,18 +70,15 @@ namespace Ryujinx.Graphics.Gal.OpenGL
             IndexBuffer = new IbInfo();
         }
 
-        public void ClearBuffers(int RtIndex, GalClearBufferFlags Flags)
+        public void ClearBuffers(GalClearBufferFlags Flags)
         {
-            ClearBufferMask Mask = 0;
+            ClearBufferMask Mask = ClearBufferMask.ColorBufferBit;
 
-            //TODO: Use glColorMask to clear just the specified channels.
-            if (Flags.HasFlag(GalClearBufferFlags.ColorRed)   &&
-                Flags.HasFlag(GalClearBufferFlags.ColorGreen) &&
-                Flags.HasFlag(GalClearBufferFlags.ColorBlue)  &&
-                Flags.HasFlag(GalClearBufferFlags.ColorAlpha))
-            {
-                Mask = ClearBufferMask.ColorBufferBit;
-            }
+            GL.ColorMask(
+                Flags.HasFlag(GalClearBufferFlags.ColorRed),
+                Flags.HasFlag(GalClearBufferFlags.ColorGreen),
+                Flags.HasFlag(GalClearBufferFlags.ColorBlue),
+                Flags.HasFlag(GalClearBufferFlags.ColorAlpha));
 
             if (Flags.HasFlag(GalClearBufferFlags.Depth))
             {
@@ -94,6 +91,8 @@ namespace Ryujinx.Graphics.Gal.OpenGL
             }
 
             GL.Clear(Mask);
+
+            GL.ColorMask(true, true, true, true);
         }
 
         public bool IsVboCached(long Key, long DataSize)
diff --git a/Ryujinx.HLE/Gpu/Engines/NvGpuEngine3d.cs b/Ryujinx.HLE/Gpu/Engines/NvGpuEngine3d.cs
index 380082b316..56cb7688b1 100644
--- a/Ryujinx.HLE/Gpu/Engines/NvGpuEngine3d.cs
+++ b/Ryujinx.HLE/Gpu/Engines/NvGpuEngine3d.cs
@@ -94,30 +94,28 @@ namespace Ryujinx.HLE.Gpu.Engines
 
             int FbIndex = (Arg0 >> 6) & 0xf;
 
-            int Layer = (Arg0 >> 10) & 0x3ff;
-
             GalClearBufferFlags Flags = (GalClearBufferFlags)(Arg0 & 0x3f);
 
-            SetFrameBuffer(Vmm, 0);
+            SetFrameBuffer(Vmm, FbIndex);
 
-            Gpu.Renderer.Rasterizer.ClearBuffers(Layer, Flags);
+            Gpu.Renderer.Rasterizer.ClearBuffers(Flags);
         }
 
         private void SetFrameBuffer(NvGpuVmm Vmm, int FbIndex)
         {
             long VA = MakeInt64From2xInt32(NvGpuEngine3dReg.FrameBufferNAddress + FbIndex * 0x10);
 
-            long PA = Vmm.GetPhysicalAddress(VA);
+            long Key = Vmm.GetPhysicalAddress(VA);
 
-            FrameBuffers.Add(PA);
+            FrameBuffers.Add(Key);
 
             int Width  = ReadRegister(NvGpuEngine3dReg.FrameBufferNWidth  + FbIndex * 0x10);
             int Height = ReadRegister(NvGpuEngine3dReg.FrameBufferNHeight + FbIndex * 0x10);
 
             //Note: Using the Width/Height results seems to give incorrect results.
             //Maybe the size of all frame buffers is hardcoded to screen size? This seems unlikely.
-            Gpu.Renderer.FrameBuffer.Create(PA, 1280, 720);
-            Gpu.Renderer.FrameBuffer.Bind(PA);
+            Gpu.Renderer.FrameBuffer.Create(Key, 1280, 720);
+            Gpu.Renderer.FrameBuffer.Bind(Key);
         }
 
         private long[] UploadShaders(NvGpuVmm Vmm)