CodeGenContext.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using Ryujinx.Graphics.Shader.Translation;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
  5. {
  6. class CodeGenContext
  7. {
  8. public const string Tab = " ";
  9. public ShaderConfig Config { get; }
  10. public List<BufferDescriptor> CBufferDescriptors { get; }
  11. public List<BufferDescriptor> SBufferDescriptors { get; }
  12. public List<TextureDescriptor> TextureDescriptors { get; }
  13. public List<TextureDescriptor> ImageDescriptors { get; }
  14. public OperandManager OperandManager { get; }
  15. private StringBuilder _sb;
  16. private int _level;
  17. private string _indentation;
  18. public CodeGenContext(ShaderConfig config)
  19. {
  20. Config = config;
  21. CBufferDescriptors = new List<BufferDescriptor>();
  22. SBufferDescriptors = new List<BufferDescriptor>();
  23. TextureDescriptors = new List<TextureDescriptor>();
  24. ImageDescriptors = new List<TextureDescriptor>();
  25. OperandManager = new OperandManager();
  26. _sb = new StringBuilder();
  27. }
  28. public void AppendLine()
  29. {
  30. _sb.AppendLine();
  31. }
  32. public void AppendLine(string str)
  33. {
  34. _sb.AppendLine(_indentation + str);
  35. }
  36. public string GetCode()
  37. {
  38. return _sb.ToString();
  39. }
  40. public void EnterScope()
  41. {
  42. AppendLine("{");
  43. _level++;
  44. UpdateIndentation();
  45. }
  46. public void LeaveScope(string suffix = "")
  47. {
  48. if (_level == 0)
  49. {
  50. return;
  51. }
  52. _level--;
  53. UpdateIndentation();
  54. AppendLine("}" + suffix);
  55. }
  56. private void UpdateIndentation()
  57. {
  58. _indentation = GetIndentation(_level);
  59. }
  60. private static string GetIndentation(int level)
  61. {
  62. string indentation = string.Empty;
  63. for (int index = 0; index < level; index++)
  64. {
  65. indentation += Tab;
  66. }
  67. return indentation;
  68. }
  69. }
  70. }