CodeGenContext.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Collections.Generic;
  2. using System.Text;
  3. namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
  4. {
  5. class CodeGenContext
  6. {
  7. public const string Tab = " ";
  8. public ShaderConfig Config { get; }
  9. public List<BufferDescriptor> CBufferDescriptors { get; }
  10. public List<BufferDescriptor> SBufferDescriptors { get; }
  11. public List<TextureDescriptor> TextureDescriptors { get; }
  12. public List<TextureDescriptor> ImageDescriptors { get; }
  13. public OperandManager OperandManager { get; }
  14. private StringBuilder _sb;
  15. private int _level;
  16. private string _indentation;
  17. public CodeGenContext(ShaderConfig config)
  18. {
  19. Config = config;
  20. CBufferDescriptors = new List<BufferDescriptor>();
  21. SBufferDescriptors = new List<BufferDescriptor>();
  22. TextureDescriptors = new List<TextureDescriptor>();
  23. ImageDescriptors = new List<TextureDescriptor>();
  24. OperandManager = new OperandManager();
  25. _sb = new StringBuilder();
  26. }
  27. public void AppendLine()
  28. {
  29. _sb.AppendLine();
  30. }
  31. public void AppendLine(string str)
  32. {
  33. _sb.AppendLine(_indentation + str);
  34. }
  35. public string GetCode()
  36. {
  37. return _sb.ToString();
  38. }
  39. public void EnterScope()
  40. {
  41. AppendLine("{");
  42. _level++;
  43. UpdateIndentation();
  44. }
  45. public void LeaveScope(string suffix = "")
  46. {
  47. if (_level == 0)
  48. {
  49. return;
  50. }
  51. _level--;
  52. UpdateIndentation();
  53. AppendLine("}" + suffix);
  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. public string GetTabString()
  69. {
  70. return Tab;
  71. }
  72. }
  73. }