CodeGenContext.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Collections.Generic;
  2. using System.Text;
  3. namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
  4. {
  5. class CodeGenContext
  6. {
  7. private const string Tab = " ";
  8. public ShaderConfig Config { get; }
  9. public List<CBufferDescriptor> CBufferDescriptors { get; }
  10. public List<TextureDescriptor> TextureDescriptors { get; }
  11. public OperandManager OperandManager { get; }
  12. private StringBuilder _sb;
  13. private int _level;
  14. private string _indentation;
  15. public CodeGenContext(ShaderConfig config)
  16. {
  17. Config = config;
  18. CBufferDescriptors = new List<CBufferDescriptor>();
  19. TextureDescriptors = new List<TextureDescriptor>();
  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. private void UpdateIndentation()
  52. {
  53. _indentation = GetIndentation(_level);
  54. }
  55. private static string GetIndentation(int level)
  56. {
  57. string indentation = string.Empty;
  58. for (int index = 0; index < level; index++)
  59. {
  60. indentation += Tab;
  61. }
  62. return indentation;
  63. }
  64. }
  65. }