CodeGenContext.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 ShaderConfig Config { get; }
  11. public OperandManager OperandManager { get; }
  12. private readonly StructuredProgramInfo _info;
  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. private static int FindDescriptorIndex(TextureDescriptor[] array, AstTextureOperation texOp)
  52. {
  53. for (int i = 0; i < array.Length; i++)
  54. {
  55. var descriptor = array[i];
  56. if (descriptor.Type == texOp.Type &&
  57. descriptor.CbufSlot == texOp.CbufSlot &&
  58. descriptor.HandleIndex == texOp.Handle &&
  59. descriptor.Format == texOp.Format)
  60. {
  61. return i;
  62. }
  63. }
  64. return -1;
  65. }
  66. public int FindTextureDescriptorIndex(AstTextureOperation texOp)
  67. {
  68. return FindDescriptorIndex(Config.GetTextureDescriptors(), texOp);
  69. }
  70. public int FindImageDescriptorIndex(AstTextureOperation texOp)
  71. {
  72. return FindDescriptorIndex(Config.GetImageDescriptors(), texOp);
  73. }
  74. public StructuredFunction GetFunction(int id)
  75. {
  76. return _info.Functions[id];
  77. }
  78. private void UpdateIndentation()
  79. {
  80. _indentation = GetIndentation(_level);
  81. }
  82. private static string GetIndentation(int level)
  83. {
  84. string indentation = string.Empty;
  85. for (int index = 0; index < level; index++)
  86. {
  87. indentation += Tab;
  88. }
  89. return indentation;
  90. }
  91. }
  92. }