CodeGenContext.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.StructuredIr;
  3. using Ryujinx.Graphics.Shader.Translation;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
  7. {
  8. class CodeGenContext
  9. {
  10. public const string Tab = " ";
  11. private readonly StructuredProgramInfo _info;
  12. public StructuredFunction CurrentFunction { get; set; }
  13. public ShaderConfig Config { get; }
  14. public bool CbIndexable => _info.UsesCbIndexing;
  15. public List<BufferDescriptor> CBufferDescriptors { get; }
  16. public List<BufferDescriptor> SBufferDescriptors { get; }
  17. public List<TextureDescriptor> TextureDescriptors { get; }
  18. public List<TextureDescriptor> ImageDescriptors { get; }
  19. public OperandManager OperandManager { get; }
  20. private StringBuilder _sb;
  21. private int _level;
  22. private string _indentation;
  23. public CodeGenContext(StructuredProgramInfo info, ShaderConfig config)
  24. {
  25. _info = info;
  26. Config = config;
  27. CBufferDescriptors = new List<BufferDescriptor>();
  28. SBufferDescriptors = new List<BufferDescriptor>();
  29. TextureDescriptors = new List<TextureDescriptor>();
  30. ImageDescriptors = new List<TextureDescriptor>();
  31. OperandManager = new OperandManager();
  32. _sb = new StringBuilder();
  33. }
  34. public void AppendLine()
  35. {
  36. _sb.AppendLine();
  37. }
  38. public void AppendLine(string str)
  39. {
  40. _sb.AppendLine(_indentation + str);
  41. }
  42. public string GetCode()
  43. {
  44. return _sb.ToString();
  45. }
  46. public void EnterScope()
  47. {
  48. AppendLine("{");
  49. _level++;
  50. UpdateIndentation();
  51. }
  52. public void LeaveScope(string suffix = "")
  53. {
  54. if (_level == 0)
  55. {
  56. return;
  57. }
  58. _level--;
  59. UpdateIndentation();
  60. AppendLine("}" + suffix);
  61. }
  62. private int FindDescriptorIndex(List<TextureDescriptor> list, AstTextureOperation texOp)
  63. {
  64. return list.FindIndex(descriptor =>
  65. descriptor.Type == texOp.Type &&
  66. descriptor.CbufSlot == texOp.CbufSlot &&
  67. descriptor.HandleIndex == texOp.Handle &&
  68. descriptor.Format == texOp.Format);
  69. }
  70. public int FindTextureDescriptorIndex(AstTextureOperation texOp)
  71. {
  72. return FindDescriptorIndex(TextureDescriptors, texOp);
  73. }
  74. public int FindImageDescriptorIndex(AstTextureOperation texOp)
  75. {
  76. return FindDescriptorIndex(ImageDescriptors, texOp);
  77. }
  78. public StructuredFunction GetFunction(int id)
  79. {
  80. return _info.Functions[id];
  81. }
  82. private void UpdateIndentation()
  83. {
  84. _indentation = GetIndentation(_level);
  85. }
  86. private static string GetIndentation(int level)
  87. {
  88. string indentation = string.Empty;
  89. for (int index = 0; index < level; index++)
  90. {
  91. indentation += Tab;
  92. }
  93. return indentation;
  94. }
  95. }
  96. }