/ src / Ryujinx.Graphics.Shader / CodeGen / Glsl / CodeGenContext.cs
CodeGenContext.cs
  1  using Ryujinx.Graphics.Shader.StructuredIr;
  2  using Ryujinx.Graphics.Shader.Translation;
  3  using System.Text;
  4  
  5  namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
  6  {
  7      class CodeGenContext
  8      {
  9          public const string Tab = "    ";
 10  
 11          public StructuredFunction CurrentFunction { get; set; }
 12  
 13          public StructuredProgramInfo Info { get; }
 14  
 15          public AttributeUsage AttributeUsage { get; }
 16          public ShaderDefinitions Definitions { get; }
 17          public ShaderProperties Properties { get; }
 18          public HostCapabilities HostCapabilities { get; }
 19          public ILogger Logger { get; }
 20          public TargetApi TargetApi { get; }
 21  
 22          public OperandManager OperandManager { get; }
 23  
 24          private readonly StringBuilder _sb;
 25  
 26          private int _level;
 27  
 28          private string _indentation;
 29  
 30          public CodeGenContext(StructuredProgramInfo info, CodeGenParameters parameters)
 31          {
 32              Info = info;
 33              AttributeUsage = parameters.AttributeUsage;
 34              Definitions = parameters.Definitions;
 35              Properties = parameters.Properties;
 36              HostCapabilities = parameters.HostCapabilities;
 37              Logger = parameters.Logger;
 38              TargetApi = parameters.TargetApi;
 39  
 40              OperandManager = new OperandManager();
 41  
 42              _sb = new StringBuilder();
 43          }
 44  
 45          public void AppendLine()
 46          {
 47              _sb.AppendLine();
 48          }
 49  
 50          public void AppendLine(string str)
 51          {
 52              _sb.AppendLine(_indentation + str);
 53          }
 54  
 55          public string GetCode()
 56          {
 57              return _sb.ToString();
 58          }
 59  
 60          public void EnterScope()
 61          {
 62              AppendLine("{");
 63  
 64              _level++;
 65  
 66              UpdateIndentation();
 67          }
 68  
 69          public void LeaveScope(string suffix = "")
 70          {
 71              if (_level == 0)
 72              {
 73                  return;
 74              }
 75  
 76              _level--;
 77  
 78              UpdateIndentation();
 79  
 80              AppendLine("}" + suffix);
 81          }
 82  
 83          public StructuredFunction GetFunction(int id)
 84          {
 85              return Info.Functions[id];
 86          }
 87  
 88          private void UpdateIndentation()
 89          {
 90              _indentation = GetIndentation(_level);
 91          }
 92  
 93          private static string GetIndentation(int level)
 94          {
 95              StringBuilder indentationBuilder = new();
 96  
 97              for (int index = 0; index < level; index++)
 98              {
 99                  indentationBuilder.Append(Tab);
100              }
101  
102              return indentationBuilder.ToString();
103          }
104      }
105  }