From 9b08abc644c4afcb1b4eb59bfbe8057727ad9d70 Mon Sep 17 00:00:00 2001
From: gdkchan <gab.dark.100@gmail.com>
Date: Mon, 12 Jul 2021 16:20:33 -0300
Subject: [PATCH] Fix shader compilation on shaders that uses rectangle
 textures (#2471)

---
 Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs      |  2 +-
 .../TextureOperation.cs                         | 17 +++++++++++++++--
 Ryujinx.Graphics.Shader/Translation/Rewriter.cs | 12 ++++++++++++
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs b/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs
index e5c1fb83e..a5712a14a 100644
--- a/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs
+++ b/Ryujinx.Graphics.Gpu/Shader/ShaderCache.cs
@@ -38,7 +38,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 = 2439;
+        private const ulong ShaderCodeGenVersion = 2469;
 
         // Progress reporting helpers
         private volatile int _shaderCount;
diff --git a/Ryujinx.Graphics.Shader/IntermediateRepresentation/TextureOperation.cs b/Ryujinx.Graphics.Shader/IntermediateRepresentation/TextureOperation.cs
index b76d5dc6d..e80f9c113 100644
--- a/Ryujinx.Graphics.Shader/IntermediateRepresentation/TextureOperation.cs
+++ b/Ryujinx.Graphics.Shader/IntermediateRepresentation/TextureOperation.cs
@@ -16,18 +16,31 @@ namespace Ryujinx.Graphics.Shader.IntermediateRepresentation
             SamplerType type,
             TextureFormat format,
             TextureFlags flags,
+            int cbufSlot,
             int handle,
             int compIndex,
             Operand dest,
-            params Operand[] sources) : base(inst, compIndex, dest, sources)
+            Operand[] sources) : base(inst, compIndex, dest, sources)
         {
             Type = type;
             Format = format;
             Flags = flags;
-            CbufSlot = DefaultCbufSlot;
+            CbufSlot = cbufSlot;
             Handle = handle;
         }
 
+        public TextureOperation(
+            Instruction inst,
+            SamplerType type,
+            TextureFormat format,
+            TextureFlags flags,
+            int handle,
+            int compIndex,
+            Operand dest,
+            Operand[] sources) : this(inst, type, format, flags, DefaultCbufSlot, handle, compIndex, dest, sources)
+        {
+        }
+
         public void TurnIntoIndexed(int handle)
         {
             Type |= SamplerType.Indexed;
diff --git a/Ryujinx.Graphics.Shader/Translation/Rewriter.cs b/Ryujinx.Graphics.Shader/Translation/Rewriter.cs
index 84aa7c10a..fba586c8c 100644
--- a/Ryujinx.Graphics.Shader/Translation/Rewriter.cs
+++ b/Ryujinx.Graphics.Shader/Translation/Rewriter.cs
@@ -291,6 +291,8 @@ namespace Ryujinx.Graphics.Shader.Translation
             // We normalize by dividing the coords by the texture size.
             if (isRect && !intCoords)
             {
+                config.SetUsedFeature(FeatureFlags.IntegerSampling);
+
                 for (int index = 0; index < coordsCount; index++)
                 {
                     Operand coordSize = Local();
@@ -311,11 +313,14 @@ namespace Ryujinx.Graphics.Shader.Translation
                         texOp.Type,
                         texOp.Format,
                         texOp.Flags,
+                        texOp.CbufSlot,
                         texOp.Handle,
                         index,
                         coordSize,
                         texSizeSources));
 
+                    config.SetUsedTexture(Instruction.TextureSize, texOp.Type, texOp.Format, texOp.Flags, texOp.CbufSlot, texOp.Handle);
+
                     Operand source = sources[coordsIndex + index];
 
                     Operand coordNormalized = Local();
@@ -352,6 +357,8 @@ namespace Ryujinx.Graphics.Shader.Translation
                 }
                 else
                 {
+                    config.SetUsedFeature(FeatureFlags.IntegerSampling);
+
                     Operand lod = Local();
 
                     node.List.AddBefore(node, new TextureOperation(
@@ -359,6 +366,7 @@ namespace Ryujinx.Graphics.Shader.Translation
                         texOp.Type,
                         texOp.Format,
                         texOp.Flags,
+                        texOp.CbufSlot,
                         texOp.Handle,
                         0,
                         lod,
@@ -384,11 +392,14 @@ namespace Ryujinx.Graphics.Shader.Translation
                             texOp.Type,
                             texOp.Format,
                             texOp.Flags,
+                            texOp.CbufSlot,
                             texOp.Handle,
                             index,
                             coordSize,
                             texSizeSources));
 
+                        config.SetUsedTexture(Instruction.TextureSize, texOp.Type, texOp.Format, texOp.Flags, texOp.CbufSlot, texOp.Handle);
+
                         Operand offset = Local();
 
                         Operand intOffset = offsets[index + (hasOffsets ? texOp.Index * coordsCount : 0)];
@@ -420,6 +431,7 @@ namespace Ryujinx.Graphics.Shader.Translation
                 texOp.Type,
                 texOp.Format,
                 texOp.Flags & ~(TextureFlags.Offset | TextureFlags.Offsets),
+                texOp.CbufSlot,
                 texOp.Handle,
                 componentIndex,
                 texOp.Dest,