From 0031edae27ba6a7bdb08db51efd84c887a6bdabd Mon Sep 17 00:00:00 2001
From: gdkchan <gab.dark.100@gmail.com>
Date: Wed, 28 Oct 2020 17:20:43 -0300
Subject: [PATCH] Avoid sampler conflicts on bindless samplers with the same
 name (#1642)

---
 .../CodeGen/Glsl/Declarations.cs              | 71 +------------------
 .../CodeGen/Glsl/OperandManager.cs            |  2 +-
 Ryujinx.Graphics.Shader/SamplerType.cs        | 68 ++++++++++++++++++
 3 files changed, 71 insertions(+), 70 deletions(-)

diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs
index 6f5e75aa76..0250d8520a 100644
--- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs
+++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/Declarations.cs
@@ -317,7 +317,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
                     continue;
                 }
 
-                string samplerTypeName = GetSamplerTypeName(texOp.Type);
+                string samplerTypeName = texOp.Type.ToGlslSamplerType();
 
                 context.AppendLine("uniform " + samplerTypeName + " " + samplerName + ";");
             }
@@ -382,7 +382,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
                     layout = "layout(" + layout + ") ";
                 }
 
-                string imageTypeName = GetImageTypeName(texOp.Type, texOp.Format.GetComponentType());
+                string imageTypeName = texOp.Type.ToGlslImageType(texOp.Format.GetComponentType());
 
                 context.AppendLine("uniform " + layout + imageTypeName + " " + imageName + ";");
             }
@@ -537,72 +537,5 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
             context.AppendLine(code.Replace("\t", CodeGenContext.Tab));
             context.AppendLine();
         }
-
-        private static string GetSamplerTypeName(SamplerType type)
-        {
-            string typeName;
-
-            switch (type & SamplerType.Mask)
-            {
-                case SamplerType.Texture1D:     typeName = "sampler1D";     break;
-                case SamplerType.TextureBuffer: typeName = "samplerBuffer"; break;
-                case SamplerType.Texture2D:     typeName = "sampler2D";     break;
-                case SamplerType.Texture3D:     typeName = "sampler3D";     break;
-                case SamplerType.TextureCube:   typeName = "samplerCube";   break;
-
-                default: throw new ArgumentException($"Invalid sampler type \"{type}\".");
-            }
-
-            if ((type & SamplerType.Multisample) != 0)
-            {
-                typeName += "MS";
-            }
-
-            if ((type & SamplerType.Array) != 0)
-            {
-                typeName += "Array";
-            }
-
-            if ((type & SamplerType.Shadow) != 0)
-            {
-                typeName += "Shadow";
-            }
-
-            return typeName;
-        }
-
-        private static string GetImageTypeName(SamplerType type, VariableType componentType)
-        {
-            string typeName;
-
-            switch (type & SamplerType.Mask)
-            {
-                case SamplerType.Texture1D:     typeName = "image1D";     break;
-                case SamplerType.TextureBuffer: typeName = "imageBuffer"; break;
-                case SamplerType.Texture2D:     typeName = "image2D";     break;
-                case SamplerType.Texture3D:     typeName = "image3D";     break;
-                case SamplerType.TextureCube:   typeName = "imageCube";   break;
-
-                default: throw new ArgumentException($"Invalid sampler type \"{type}\".");
-            }
-
-            if ((type & SamplerType.Multisample) != 0)
-            {
-                typeName += "MS";
-            }
-
-            if ((type & SamplerType.Array) != 0)
-            {
-                typeName += "Array";
-            }
-
-            switch (componentType)
-            {
-                case VariableType.U32: typeName = 'u' + typeName; break;
-                case VariableType.S32: typeName = 'i' + typeName; break;
-            }
-
-            return typeName;
-        }
     }
 }
\ No newline at end of file
diff --git a/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs b/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs
index 14ea7032b7..8e878b0d03 100644
--- a/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs
+++ b/Ryujinx.Graphics.Shader/CodeGen/Glsl/OperandManager.cs
@@ -247,7 +247,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
             {
                 AstOperand operand = texOp.GetSource(0) as AstOperand;
 
-                suffix = "_cb" + operand.CbufSlot + "_" + operand.CbufOffset;
+                suffix = $"_{texOp.Type.ToGlslSamplerType()}_cb{operand.CbufSlot}_{operand.CbufOffset}";
             }
             else
             {
diff --git a/Ryujinx.Graphics.Shader/SamplerType.cs b/Ryujinx.Graphics.Shader/SamplerType.cs
index 9546efe494..286ae9d5d6 100644
--- a/Ryujinx.Graphics.Shader/SamplerType.cs
+++ b/Ryujinx.Graphics.Shader/SamplerType.cs
@@ -1,3 +1,4 @@
+using Ryujinx.Graphics.Shader.StructuredIr;
 using System;
 
 namespace Ryujinx.Graphics.Shader
@@ -35,5 +36,72 @@ namespace Ryujinx.Graphics.Shader
 
             throw new ArgumentException($"Invalid sampler type \"{type}\".");
         }
+
+        public static string ToGlslSamplerType(this SamplerType type)
+        {
+            string typeName;
+
+            switch (type & SamplerType.Mask)
+            {
+                case SamplerType.Texture1D:     typeName = "sampler1D";     break;
+                case SamplerType.TextureBuffer: typeName = "samplerBuffer"; break;
+                case SamplerType.Texture2D:     typeName = "sampler2D";     break;
+                case SamplerType.Texture3D:     typeName = "sampler3D";     break;
+                case SamplerType.TextureCube:   typeName = "samplerCube";   break;
+
+                default: throw new ArgumentException($"Invalid sampler type \"{type}\".");
+            }
+
+            if ((type & SamplerType.Multisample) != 0)
+            {
+                typeName += "MS";
+            }
+
+            if ((type & SamplerType.Array) != 0)
+            {
+                typeName += "Array";
+            }
+
+            if ((type & SamplerType.Shadow) != 0)
+            {
+                typeName += "Shadow";
+            }
+
+            return typeName;
+        }
+
+        public static string ToGlslImageType(this SamplerType type, VariableType componentType)
+        {
+            string typeName;
+
+            switch (type & SamplerType.Mask)
+            {
+                case SamplerType.Texture1D:     typeName = "image1D";     break;
+                case SamplerType.TextureBuffer: typeName = "imageBuffer"; break;
+                case SamplerType.Texture2D:     typeName = "image2D";     break;
+                case SamplerType.Texture3D:     typeName = "image3D";     break;
+                case SamplerType.TextureCube:   typeName = "imageCube";   break;
+
+                default: throw new ArgumentException($"Invalid sampler type \"{type}\".");
+            }
+
+            if ((type & SamplerType.Multisample) != 0)
+            {
+                typeName += "MS";
+            }
+
+            if ((type & SamplerType.Array) != 0)
+            {
+                typeName += "Array";
+            }
+
+            switch (componentType)
+            {
+                case VariableType.U32: typeName = 'u' + typeName; break;
+                case VariableType.S32: typeName = 'i' + typeName; break;
+            }
+
+            return typeName;
+        }
     }
 }
\ No newline at end of file