CodeGenContext.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. AstOperand operand = texOp.GetSource(0) as AstOperand;
  65. bool bindless = (texOp.Flags & TextureFlags.Bindless) > 0;
  66. int cBufSlot = bindless ? operand.CbufSlot : 0;
  67. int cBufOffset = bindless ? operand.CbufOffset : 0;
  68. return list.FindIndex(descriptor =>
  69. descriptor.Type == texOp.Type &&
  70. descriptor.HandleIndex == texOp.Handle &&
  71. descriptor.Format == texOp.Format &&
  72. descriptor.CbufSlot == cBufSlot &&
  73. descriptor.CbufOffset == cBufOffset);
  74. }
  75. public int FindTextureDescriptorIndex(AstTextureOperation texOp)
  76. {
  77. return FindDescriptorIndex(TextureDescriptors, texOp);
  78. }
  79. public int FindImageDescriptorIndex(AstTextureOperation texOp)
  80. {
  81. return FindDescriptorIndex(ImageDescriptors, texOp);
  82. }
  83. public StructuredFunction GetFunction(int id)
  84. {
  85. return _info.Functions[id];
  86. }
  87. private void UpdateIndentation()
  88. {
  89. _indentation = GetIndentation(_level);
  90. }
  91. private static string GetIndentation(int level)
  92. {
  93. string indentation = string.Empty;
  94. for (int index = 0; index < level; index++)
  95. {
  96. indentation += Tab;
  97. }
  98. return indentation;
  99. }
  100. }
  101. }