From 9f12e50a546b15533778ed0d8290202af91c10a2 Mon Sep 17 00:00:00 2001
From: gdkchan <gab.dark.100@gmail.com>
Date: Tue, 25 Apr 2023 19:51:07 -0300
Subject: Refactor attribute handling on the shader generator (#4565)

* Refactor attribute handling on the shader generator

* Implement gl_ViewportMask[]

* Add back the Intel FrontFacing bug workaround

* Fix GLSL transform feedback outputs mistmatch with fragment stage

* Shader cache version bump

* Fix geometry shader recognition

* PR feedback

* Delete GetOperandDef and GetOperandUse

* Remove replacements that are no longer needed on GLSL compilation on Vulkan

* Fix incorrect load for per-patch outputs

* Fix build
---
 .../StructuredIr/StructuredProgramContext.cs       | 67 +++-------------------
 1 file changed, 8 insertions(+), 59 deletions(-)

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

diff --git a/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgramContext.cs b/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgramContext.cs
index ce57a578..68bbdeb1 100644
--- a/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgramContext.cs
+++ b/Ryujinx.Graphics.Shader/StructuredIr/StructuredProgramContext.cs
@@ -37,43 +37,26 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
 
             Config = config;
 
-            if (config.Stage == ShaderStage.TessellationControl)
-            {
-                // Required to index outputs.
-                Info.Inputs.Add(AttributeConsts.InvocationId);
-            }
-            else if (config.GpPassthrough)
+            if (config.GpPassthrough)
             {
                 int passthroughAttributes = config.PassthroughAttributes;
                 while (passthroughAttributes != 0)
                 {
                     int index = BitOperations.TrailingZeroCount(passthroughAttributes);
 
-                    int attrBase = AttributeConsts.UserAttributeBase + index * 16;
-                    Info.Inputs.Add(attrBase);
-                    Info.Inputs.Add(attrBase + 4);
-                    Info.Inputs.Add(attrBase + 8);
-                    Info.Inputs.Add(attrBase + 12);
+                    Info.IoDefinitions.Add(new IoDefinition(StorageKind.Input, IoVariable.UserDefined, index));
 
                     passthroughAttributes &= ~(1 << index);
                 }
 
-                Info.Inputs.Add(AttributeConsts.PositionX);
-                Info.Inputs.Add(AttributeConsts.PositionY);
-                Info.Inputs.Add(AttributeConsts.PositionZ);
-                Info.Inputs.Add(AttributeConsts.PositionW);
-                Info.Inputs.Add(AttributeConsts.PointSize);
-
-                for (int i = 0; i < 8; i++)
-                {
-                    Info.Inputs.Add(AttributeConsts.ClipDistance0 + i * 4);
-                }
+                Info.IoDefinitions.Add(new IoDefinition(StorageKind.Input, IoVariable.Position));
+                Info.IoDefinitions.Add(new IoDefinition(StorageKind.Input, IoVariable.PointSize));
+                Info.IoDefinitions.Add(new IoDefinition(StorageKind.Input, IoVariable.ClipDistance));
             }
             else if (config.Stage == ShaderStage.Fragment)
             {
                 // Potentially used for texture coordinate scaling.
-                Info.Inputs.Add(AttributeConsts.PositionX);
-                Info.Inputs.Add(AttributeConsts.PositionY);
+                Info.IoDefinitions.Add(new IoDefinition(StorageKind.Input, IoVariable.FragmentCoord));
             }
         }
 
@@ -281,7 +264,7 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
             }
             else
             {
-                cond = GetOperandUse(branchOp.GetSource(0));
+                cond = GetOperand(branchOp.GetSource(0));
 
                 Instruction invInst = type == AstBlockType.If
                     ? Instruction.BranchIfTrue
@@ -315,41 +298,7 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
             return newTemp;
         }
 
-        public AstOperand GetOperandDef(Operand operand)
-        {
-            if (operand.Type == OperandType.Attribute)
-            {
-                Info.Outputs.Add(operand.Value & AttributeConsts.Mask);
-            }
-            else if (operand.Type == OperandType.AttributePerPatch)
-            {
-                Info.OutputsPerPatch.Add(operand.Value & AttributeConsts.Mask);
-            }
-
-            return GetOperand(operand);
-        }
-
-        public AstOperand GetOperandUse(Operand operand)
-        {
-            // If this flag is set, we're reading from an output attribute instead.
-            if (operand.Type.IsAttribute() && (operand.Value & AttributeConsts.LoadOutputMask) != 0)
-            {
-                return GetOperandDef(operand);
-            }
-
-            if (operand.Type == OperandType.Attribute)
-            {
-                Info.Inputs.Add(operand.Value);
-            }
-            else if (operand.Type == OperandType.AttributePerPatch)
-            {
-                Info.InputsPerPatch.Add(operand.Value);
-            }
-
-            return GetOperand(operand);
-        }
-
-        private AstOperand GetOperand(Operand operand)
+        public AstOperand GetOperand(Operand operand)
         {
             if (operand == null)
             {
-- 
cgit v1.2.3-70-g09d2