From 402f05b8ef013807997589ecc0a8ff50267dcd23 Mon Sep 17 00:00:00 2001
From: gdkchan <gab.dark.100@gmail.com>
Date: Sat, 20 May 2023 16:19:26 -0300
Subject: Replace constant buffer access on shader with new Load instruction
 (#4646)

---
 .../StructuredIr/StructuredProgramContext.cs       | 32 ++++++++++++++++++----
 1 file changed, 27 insertions(+), 5 deletions(-)

(limited to 'src/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgramContext.cs')

diff --git a/src/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgramContext.cs b/src/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgramContext.cs
index 68bbdeb1..c5ad3683 100644
--- a/src/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgramContext.cs
+++ b/src/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgramContext.cs
@@ -298,6 +298,33 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
             return newTemp;
         }
 
+        public IAstNode GetOperandOrCbLoad(Operand operand)
+        {
+            if (operand.Type == OperandType.ConstantBuffer)
+            {
+                int cbufSlot = operand.GetCbufSlot();
+                int cbufOffset = operand.GetCbufOffset();
+
+                int binding = Config.ResourceManager.GetConstantBufferBinding(cbufSlot);
+                int vecIndex = cbufOffset >> 2;
+                int elemIndex = cbufOffset & 3;
+
+                Config.ResourceManager.SetUsedConstantBufferBinding(binding);
+
+                IAstNode[] sources = new IAstNode[]
+                {
+                    new AstOperand(OperandType.Constant, binding),
+                    new AstOperand(OperandType.Constant, 0),
+                    new AstOperand(OperandType.Constant, vecIndex),
+                    new AstOperand(OperandType.Constant, elemIndex)
+                };
+
+                return new AstOperation(Instruction.Load, StorageKind.ConstantBuffer, sources, sources.Length);
+            }
+
+            return GetOperand(operand);
+        }
+
         public AstOperand GetOperand(Operand operand)
         {
             if (operand == null)
@@ -307,11 +334,6 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
 
             if (operand.Type != OperandType.LocalVariable)
             {
-                if (operand.Type == OperandType.ConstantBuffer)
-                {
-                    Config.SetUsedConstantBuffer(operand.GetCbufSlot());
-                }
-
                 return new AstOperand(operand);
             }
 
-- 
cgit v1.2.3-70-g09d2