From a0b4799f1978ddb2cbd8d118380c29d9d15e158b Mon Sep 17 00:00:00 2001
From: gdkchan <gab.dark.100@gmail.com>
Date: Sat, 27 Mar 2021 18:59:05 -0300
Subject: [PATCH] Fix ZN flags set for shader instructions using RZ.CC dest
 (#2147)

* Fix ZN flags set for shader instructions using RZ.CC dest

* Shader cache version bump and nits
---
 Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs    |  2 +-
 .../Instructions/InstEmitAlu.cs               | 19 +++++++------------
 .../Translation/EmitterContext.cs             |  2 +-
 3 files changed, 9 insertions(+), 14 deletions(-)

diff --git a/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs b/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs
index d4ced7c8b..293fe86bb 100644
--- a/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs
+++ b/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs
@@ -35,7 +35,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
         /// <summary>
         /// Version of the codegen (to be changed when codegen or guest format change).
         /// </summary>
-        private const ulong ShaderCodeGenVersion = 2088;
+        private const ulong ShaderCodeGenVersion = 2147;
 
         // Progress reporting helpers
         private volatile int _shaderCount;
diff --git a/Ryujinx.Graphics.Shader/Instructions/InstEmitAlu.cs b/Ryujinx.Graphics.Shader/Instructions/InstEmitAlu.cs
index 37c17ecc4..734d3aa73 100644
--- a/Ryujinx.Graphics.Shader/Instructions/InstEmitAlu.cs
+++ b/Ryujinx.Graphics.Shader/Instructions/InstEmitAlu.cs
@@ -276,12 +276,11 @@ namespace Ryujinx.Graphics.Shader.Instructions
                 : context.IMaximumU32(srcA, srcB);
 
             Operand pred = GetPredicate39(context);
+            Operand res = context.ConditionalSelect(pred, resMin, resMax);
 
-            Operand dest = GetDest(context);
+            context.Copy(GetDest(context), res);
 
-            context.Copy(dest, context.ConditionalSelect(pred, resMin, resMax));
-
-            SetZnFlags(context, dest, op.SetCondCode);
+            SetZnFlags(context, res, op.SetCondCode);
 
             // TODO: X flags.
         }
@@ -461,11 +460,9 @@ namespace Ryujinx.Graphics.Shader.Instructions
 
             EmitLopPredWrite(context, op, res, (ConditionalOperation)context.CurrOp.RawOpCode.Extract(44, 2));
 
-            Operand dest = GetDest(context);
+            context.Copy(GetDest(context), res);
 
-            context.Copy(dest, res);
-
-            SetZnFlags(context, dest, op.SetCondCode, op.Extended);
+            SetZnFlags(context, res, op.SetCondCode, op.Extended);
         }
 
         public static void Lop3(EmitterContext context)
@@ -489,11 +486,9 @@ namespace Ryujinx.Graphics.Shader.Instructions
                 EmitLopPredWrite(context, op, res, (ConditionalOperation)context.CurrOp.RawOpCode.Extract(36, 2));
             }
 
-            Operand dest = GetDest(context);
+            context.Copy(GetDest(context), res);
 
-            context.Copy(dest, res);
-
-            SetZnFlags(context, dest, op.SetCondCode, op.Extended);
+            SetZnFlags(context, res, op.SetCondCode, op.Extended);
         }
 
         public static void Popc(EmitterContext context)
diff --git a/Ryujinx.Graphics.Shader/Translation/EmitterContext.cs b/Ryujinx.Graphics.Shader/Translation/EmitterContext.cs
index df6b0839a..a4c21c1d8 100644
--- a/Ryujinx.Graphics.Shader/Translation/EmitterContext.cs
+++ b/Ryujinx.Graphics.Shader/Translation/EmitterContext.cs
@@ -91,7 +91,7 @@ namespace Ryujinx.Graphics.Shader.Translation
 
         public void PrepareForReturn()
         {
-            if (Config.Stage == ShaderStage.Fragment)
+            if (!IsNonMain && Config.Stage == ShaderStage.Fragment)
             {
                 if (Config.OmapDepth)
                 {