CodeGenContext.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using Ryujinx.Graphics.Shader.StructuredIr;
  2. using Ryujinx.Graphics.Shader.Translation;
  3. using System.Text;
  4. namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
  5. {
  6. class CodeGenContext
  7. {
  8. public const string Tab = " ";
  9. public StructuredFunction CurrentFunction { get; set; }
  10. public ShaderConfig Config { get; }
  11. public OperandManager OperandManager { get; }
  12. private readonly StructuredProgramInfo _info;
  13. private readonly StringBuilder _sb;
  14. private int _level;
  15. private string _indentation;
  16. public CodeGenContext(StructuredProgramInfo info, ShaderConfig config)
  17. {
  18. _info = info;
  19. Config = config;
  20. OperandManager = new OperandManager();
  21. _sb = new StringBuilder();
  22. }
  23. public void AppendLine()
  24. {
  25. _sb.AppendLine();
  26. }
  27. public void AppendLine(string str)
  28. {
  29. _sb.AppendLine(_indentation + str);
  30. }
  31. public string GetCode()
  32. {
  33. return _sb.ToString();
  34. }
  35. public void EnterScope()
  36. {
  37. AppendLine("{");
  38. _level++;
  39. UpdateIndentation();
  40. }
  41. public void LeaveScope(string suffix = "")
  42. {
  43. if (_level == 0)
  44. {
  45. return;
  46. }
  47. _level--;
  48. UpdateIndentation();
  49. AppendLine("}" + suffix);
  50. }
  51. public StructuredFunction GetFunction(int id)
  52. {
  53. return _info.Functions[id];
  54. }
  55. public TransformFeedbackOutput GetTransformFeedbackOutput(int location, int component)
  56. {
  57. int index = (AttributeConsts.UserAttributeBase / 4) + location * 4 + component;
  58. return _info.TransformFeedbackOutputs[index];
  59. }
  60. public TransformFeedbackOutput GetTransformFeedbackOutput(int location)
  61. {
  62. int index = location / 4;
  63. return _info.TransformFeedbackOutputs[index];
  64. }
  65. private void UpdateIndentation()
  66. {
  67. _indentation = GetIndentation(_level);
  68. }
  69. private static string GetIndentation(int level)
  70. {
  71. string indentation = string.Empty;
  72. for (int index = 0; index < level; index++)
  73. {
  74. indentation += Tab;
  75. }
  76. return indentation;
  77. }
  78. }
  79. }