From 0e9823d7e6668731598b6f04b551efa801969e7b Mon Sep 17 00:00:00 2001
From: gdkchan <gab.dark.100@gmail.com>
Date: Sat, 1 May 2021 15:46:21 -0300
Subject: [PATCH] Fix shader buffer write flag on atomic instructions (#2261)

* Fix shader buffer write flag on atomic instructions

* Shader cache version bump
---
 Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs               | 2 +-
 .../CodeGen/Glsl/Instructions/InstGen.cs                 | 4 ++--
 .../CodeGen/Glsl/Instructions/InstGenMemory.cs           | 9 +++++++--
 3 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs b/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs
index 59669cb9d2..164960d041 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 = 2217;
+        private const ulong ShaderCodeGenVersion = 2261;
 
         // Progress reporting helpers
         private volatile int _shaderCount;
diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGen.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGen.cs
index 622ac646ea..0ea7f15120 100644
--- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGen.cs
+++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGen.cs
@@ -61,8 +61,8 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions
 
                         switch (memRegion)
                         {
-                            case Instruction.MrShared:  args += LoadShared (context, operation); break;
-                            case Instruction.MrStorage: args += LoadStorage(context, operation); break;
+                            case Instruction.MrShared: args += LoadShared(context, operation); break;
+                            case Instruction.MrStorage: args += LoadStorage(context, operation, forAtomic: true); break;
 
                             default: throw new InvalidOperationException($"Invalid memory region \"{memRegion}\".");
                         }
diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs
index fc5b06b1aa..273aaef8fc 100644
--- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs
+++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGenMemory.cs
@@ -205,7 +205,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions
             return $"{arrayName}[{offsetExpr}]";
         }
 
-        public static string LoadStorage(CodeGenContext context, AstOperation operation)
+        public static string LoadStorage(CodeGenContext context, AstOperation operation, bool forAtomic = false)
         {
             IAstNode src1 = operation.GetSource(0);
             IAstNode src2 = operation.GetSource(1);
@@ -213,6 +213,11 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions
             string indexExpr  = GetSoureExpr(context, src1, GetSrcVarType(operation.Inst, 0));
             string offsetExpr = GetSoureExpr(context, src2, GetSrcVarType(operation.Inst, 1));
 
+            if (forAtomic)
+            {
+                SetStorageWriteFlag(context, src1, context.Config.Stage);
+            }
+
             return GetStorageBufferAccessor(indexExpr, offsetExpr, context.Config.Stage);
         }
 
@@ -485,7 +490,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions
                         {
                             flags |= TextureUsageFlags.ResScaleUnsupported;
                         }
-                    } 
+                    }
                     else
                     {
                         // Resolution scaling cannot be applied to this texture right now.