CodeGenContext.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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<BufferDescriptor> CBufferDescriptors { get; }
  10. public List<BufferDescriptor> SBufferDescriptors { get; }
  11. public List<TextureDescriptor> TextureDescriptors { get; }
  12. public OperandManager OperandManager { get; }
  13. private StringBuilder _sb;
  14. private int _level;
  15. private string _indentation;
  16. public CodeGenContext(ShaderConfig config)
  17. {
  18. Config = config;
  19. CBufferDescriptors = new List<BufferDescriptor>();
  20. SBufferDescriptors = new List<BufferDescriptor>();
  21. TextureDescriptors = new List<TextureDescriptor>();
  22. OperandManager = new OperandManager();
  23. _sb = new StringBuilder();
  24. }
  25. public void AppendLine()
  26. {
  27. _sb.AppendLine();
  28. }
  29. public void AppendLine(string str)
  30. {
  31. _sb.AppendLine(_indentation + str);
  32. }
  33. public string GetCode()
  34. {
  35. return _sb.ToString();
  36. }
  37. public void EnterScope()
  38. {
  39. AppendLine("{");
  40. _level++;
  41. UpdateIndentation();
  42. }
  43. public void LeaveScope(string suffix = "")
  44. {
  45. if (_level == 0)
  46. {
  47. return;
  48. }
  49. _level--;
  50. UpdateIndentation();
  51. AppendLine("}" + suffix);
  52. }
  53. private void UpdateIndentation()
  54. {
  55. _indentation = GetIndentation(_level);
  56. }
  57. private static string GetIndentation(int level)
  58. {
  59. string indentation = string.Empty;
  60. for (int index = 0; index < level; index++)
  61. {
  62. indentation += Tab;
  63. }
  64. return indentation;
  65. }
  66. }
  67. }