CodeGenContext.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. public ShaderConfig Config { get; }
  12. public bool CbIndexable { get; }
  13. public List<BufferDescriptor> CBufferDescriptors { get; }
  14. public List<BufferDescriptor> SBufferDescriptors { get; }
  15. public List<TextureDescriptor> TextureDescriptors { get; }
  16. public List<TextureDescriptor> ImageDescriptors { get; }
  17. public OperandManager OperandManager { get; }
  18. private StringBuilder _sb;
  19. private int _level;
  20. private string _indentation;
  21. public CodeGenContext(ShaderConfig config, bool cbIndexable)
  22. {
  23. Config = config;
  24. CbIndexable = cbIndexable;
  25. CBufferDescriptors = new List<BufferDescriptor>();
  26. SBufferDescriptors = new List<BufferDescriptor>();
  27. TextureDescriptors = new List<TextureDescriptor>();
  28. ImageDescriptors = new List<TextureDescriptor>();
  29. OperandManager = new OperandManager();
  30. _sb = new StringBuilder();
  31. }
  32. public void AppendLine()
  33. {
  34. _sb.AppendLine();
  35. }
  36. public void AppendLine(string str)
  37. {
  38. _sb.AppendLine(_indentation + str);
  39. }
  40. public string GetCode()
  41. {
  42. return _sb.ToString();
  43. }
  44. public void EnterScope()
  45. {
  46. AppendLine("{");
  47. _level++;
  48. UpdateIndentation();
  49. }
  50. public void LeaveScope(string suffix = "")
  51. {
  52. if (_level == 0)
  53. {
  54. return;
  55. }
  56. _level--;
  57. UpdateIndentation();
  58. AppendLine("}" + suffix);
  59. }
  60. public int FindTextureDescriptorIndex(AstTextureOperation texOp)
  61. {
  62. AstOperand operand = texOp.GetSource(0) as AstOperand;
  63. bool bindless = (texOp.Flags & TextureFlags.Bindless) > 0;
  64. int cBufSlot = bindless ? operand.CbufSlot : 0;
  65. int cBufOffset = bindless ? operand.CbufOffset : 0;
  66. return TextureDescriptors.FindIndex(descriptor =>
  67. descriptor.Type == texOp.Type &&
  68. descriptor.HandleIndex == texOp.Handle &&
  69. descriptor.CbufSlot == cBufSlot &&
  70. descriptor.CbufOffset == cBufOffset);
  71. }
  72. private void UpdateIndentation()
  73. {
  74. _indentation = GetIndentation(_level);
  75. }
  76. private static string GetIndentation(int level)
  77. {
  78. string indentation = string.Empty;
  79. for (int index = 0; index < level; index++)
  80. {
  81. indentation += Tab;
  82. }
  83. return indentation;
  84. }
  85. }
  86. }