From 66d91cbc6cdd0310c567eb9f979458bd80ea0269 Mon Sep 17 00:00:00 2001
From: gdkchan <gab.dark.100@gmail.com>
Date: Mon, 9 Dec 2019 01:00:56 -0300
Subject: [PATCH] Use dispatch params shared memory size when available

---
 Ryujinx.Graphics.Gpu/Engine/Compute.cs               |  1 +
 Ryujinx.Graphics.Gpu/Engine/ComputeParams.cs         |  2 +-
 Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs           |  8 +++++---
 Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs | 11 ++++++++++-
 Ryujinx.Graphics.Shader/DefineNames.cs               |  2 ++
 5 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/Ryujinx.Graphics.Gpu/Engine/Compute.cs b/Ryujinx.Graphics.Gpu/Engine/Compute.cs
index f0daac6785..61e6b3260e 100644
--- a/Ryujinx.Graphics.Gpu/Engine/Compute.cs
+++ b/Ryujinx.Graphics.Gpu/Engine/Compute.cs
@@ -22,6 +22,7 @@ namespace Ryujinx.Graphics.Gpu.Engine
 
             ComputeShader cs = _shaderCache.GetComputeShader(
                 shaderGpuVa,
+                dispatchParams.SharedMemorySize & 0xffff,
                 dispatchParams.UnpackBlockSizeX(),
                 dispatchParams.UnpackBlockSizeY(),
                 dispatchParams.UnpackBlockSizeZ());
diff --git a/Ryujinx.Graphics.Gpu/Engine/ComputeParams.cs b/Ryujinx.Graphics.Gpu/Engine/ComputeParams.cs
index 77e60aa48c..5644ca818b 100644
--- a/Ryujinx.Graphics.Gpu/Engine/ComputeParams.cs
+++ b/Ryujinx.Graphics.Gpu/Engine/ComputeParams.cs
@@ -39,7 +39,7 @@ namespace Ryujinx.Graphics.Gpu.Engine
         public int Unknown14;
         public int Unknown15;
         public int Unknown16;
-        public int Unknown17;
+        public int SharedMemorySize;
         public int BlockSizeX;
         public int BlockSizeYZ;
         public int UniformBuffersConfig;
diff --git a/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs b/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs
index 027757980c..7b9c20eb1c 100644
--- a/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs
+++ b/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs
@@ -32,7 +32,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
             _gpPrograms = new Dictionary<ShaderAddresses, List<GraphicsShader>>();
         }
 
-        public ComputeShader GetComputeShader(ulong gpuVa, int localSizeX, int localSizeY, int localSizeZ)
+        public ComputeShader GetComputeShader(ulong gpuVa, int sharedMemorySize, int localSizeX, int localSizeY, int localSizeZ)
         {
             bool isCached = _cpPrograms.TryGetValue(gpuVa, out List<ComputeShader> list);
 
@@ -47,7 +47,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
                 }
             }
 
-            CachedShader shader = TranslateComputeShader(gpuVa, localSizeX, localSizeY, localSizeZ);
+            CachedShader shader = TranslateComputeShader(gpuVa, sharedMemorySize, localSizeX, localSizeY, localSizeZ);
 
             IShader hostShader = _context.Renderer.CompileShader(shader.Program);
 
@@ -192,7 +192,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
             return false;
         }
 
-        private CachedShader TranslateComputeShader(ulong gpuVa, int localSizeX, int localSizeY, int localSizeZ)
+        private CachedShader TranslateComputeShader(ulong gpuVa, int sharedMemorySize, int localSizeX, int localSizeY, int localSizeZ)
         {
             if (gpuVa == 0)
             {
@@ -212,6 +212,8 @@ namespace Ryujinx.Graphics.Gpu.Shader
 
             int[] codeCached = MemoryMarshal.Cast<byte, int>(code.Slice(0, program.Size)).ToArray();
 
+            program.Replace(DefineNames.SharedMemorySize, sharedMemorySize.ToString(CultureInfo.InvariantCulture));
+
             program.Replace(DefineNames.LocalSizeX, localSizeX.ToString(CultureInfo.InvariantCulture));
             program.Replace(DefineNames.LocalSizeY, localSizeY.ToString(CultureInfo.InvariantCulture));
             program.Replace(DefineNames.LocalSizeZ, localSizeZ.ToString(CultureInfo.InvariantCulture));
diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs
index 1c3cf2459b..b96aa1aebd 100644
--- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs
+++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs
@@ -75,7 +75,16 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
 
             if (context.Config.Stage == ShaderStage.Compute)
             {
-                string size = NumberFormatter.FormatInt(context.Config.Capabilities.MaximumComputeSharedMemorySize / 4);
+                string size;
+
+                if ((context.Config.Flags & TranslationFlags.Unspecialized) != 0)
+                {
+                    size = DefineNames.SharedMemorySize;
+                }
+                else
+                {
+                    size = NumberFormatter.FormatInt(context.Config.Capabilities.MaximumComputeSharedMemorySize / 4);
+                }
 
                 context.AppendLine($"shared uint {DefaultNames.SharedMemoryName}[{size}];");
                 context.AppendLine();
diff --git a/Ryujinx.Graphics.Shader/DefineNames.cs b/Ryujinx.Graphics.Shader/DefineNames.cs
index f55cbd0cce..67e8e1ee3c 100644
--- a/Ryujinx.Graphics.Shader/DefineNames.cs
+++ b/Ryujinx.Graphics.Shader/DefineNames.cs
@@ -6,6 +6,8 @@ namespace Ryujinx.Graphics.Shader
 
         public const string OutQualifierPrefixName = "S_OUT_QUALIFIER";
 
+        public const string SharedMemorySize = "S_SHARED_MEMORY_SIZE";
+
         public const string LocalSizeX = "S_LOCAL_SIZE_X";
         public const string LocalSizeY = "S_LOCAL_SIZE_Y";
         public const string LocalSizeZ = "S_LOCAL_SIZE_Z";