CodeGenContext.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 StructuredProgramInfo Info { get; }
  11. public ShaderConfig Config { get; }
  12. public OperandManager OperandManager { get; }
  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. private void UpdateIndentation()
  56. {
  57. _indentation = GetIndentation(_level);
  58. }
  59. private static string GetIndentation(int level)
  60. {
  61. string indentation = string.Empty;
  62. for (int index = 0; index < level; index++)
  63. {
  64. indentation += Tab;
  65. }
  66. return indentation;
  67. }
  68. }
  69. }