CodeGenContext.cs 2.9 KB

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