GlobalToStorage.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using System.Collections.Generic;
  3. namespace Ryujinx.Graphics.Shader.Translation.Optimizations
  4. {
  5. static class GlobalToStorage
  6. {
  7. private const int StorageDescsBaseOffset = 0x44; // In words.
  8. private const int StorageDescSize = 4; // In words.
  9. private const int StorageMaxCount = 16;
  10. private const int StorageDescsSize = StorageDescSize * StorageMaxCount;
  11. public static void RunPass(BasicBlock block, ShaderStage stage)
  12. {
  13. int sbStart = GetStorageBaseCbOffset(stage);
  14. int sbEnd = sbStart + StorageDescsSize;
  15. for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
  16. {
  17. if (!(node.Value is Operation operation))
  18. {
  19. continue;
  20. }
  21. if (operation.Inst.IsAtomic() ||
  22. operation.Inst == Instruction.LoadGlobal ||
  23. operation.Inst == Instruction.StoreGlobal)
  24. {
  25. Operand source = operation.GetSource(0);
  26. if (source.AsgOp is Operation asgOperation)
  27. {
  28. int storageIndex = SearchForStorageBase(asgOperation, sbStart, sbEnd);
  29. if (storageIndex >= 0)
  30. {
  31. node = ReplaceGlobalWithStorage(node, storageIndex);
  32. }
  33. }
  34. }
  35. }
  36. }
  37. private static LinkedListNode<INode> ReplaceGlobalWithStorage(LinkedListNode<INode> node, int storageIndex)
  38. {
  39. Operation operation = (Operation)node.Value;
  40. Operation storageOp;
  41. if (operation.Inst.IsAtomic())
  42. {
  43. Operand[] sources = new Operand[operation.SourcesCount];
  44. for (int index = 0; index < operation.SourcesCount; index++)
  45. {
  46. sources[index] = operation.GetSource(index);
  47. }
  48. Instruction inst = (operation.Inst & ~Instruction.MrMask) | Instruction.MrStorage;
  49. storageOp = new Operation(inst, storageIndex, operation.Dest, sources);
  50. }
  51. else if (operation.Inst == Instruction.LoadGlobal)
  52. {
  53. Operand source = operation.GetSource(0);
  54. storageOp = new Operation(Instruction.LoadStorage, storageIndex, operation.Dest, source);
  55. }
  56. else
  57. {
  58. Operand src1 = operation.GetSource(0);
  59. Operand src2 = operation.GetSource(1);
  60. storageOp = new Operation(Instruction.StoreStorage, storageIndex, null, src1, src2);
  61. }
  62. for (int index = 0; index < operation.SourcesCount; index++)
  63. {
  64. operation.SetSource(index, null);
  65. }
  66. LinkedListNode<INode> oldNode = node;
  67. node = node.List.AddAfter(node, storageOp);
  68. node.List.Remove(oldNode);
  69. return node;
  70. }
  71. private static int SearchForStorageBase(Operation operation, int sbStart, int sbEnd)
  72. {
  73. Queue<Operation> assignments = new Queue<Operation>();
  74. assignments.Enqueue(operation);
  75. while (assignments.TryDequeue(out operation))
  76. {
  77. for (int index = 0; index < operation.SourcesCount; index++)
  78. {
  79. Operand source = operation.GetSource(index);
  80. if (source.Type == OperandType.ConstantBuffer)
  81. {
  82. int slot = source.GetCbufSlot();
  83. int offset = source.GetCbufOffset();
  84. if (slot == 0 && offset >= sbStart && offset < sbEnd)
  85. {
  86. int storageIndex = (offset - sbStart) / StorageDescSize;
  87. return storageIndex;
  88. }
  89. }
  90. if (source.AsgOp is Operation asgOperation)
  91. {
  92. assignments.Enqueue(asgOperation);
  93. }
  94. }
  95. }
  96. return -1;
  97. }
  98. public static int GetStorageCbOffset(ShaderStage stage, int slot)
  99. {
  100. return GetStorageBaseCbOffset(stage) + slot * StorageDescSize;
  101. }
  102. private static int GetStorageBaseCbOffset(ShaderStage stage)
  103. {
  104. switch (stage)
  105. {
  106. case ShaderStage.Compute: return StorageDescsBaseOffset + 2 * StorageDescsSize;
  107. case ShaderStage.Vertex: return StorageDescsBaseOffset;
  108. case ShaderStage.TessellationControl: return StorageDescsBaseOffset + 1 * StorageDescsSize;
  109. case ShaderStage.TessellationEvaluation: return StorageDescsBaseOffset + 2 * StorageDescsSize;
  110. case ShaderStage.Geometry: return StorageDescsBaseOffset + 3 * StorageDescsSize;
  111. case ShaderStage.Fragment: return StorageDescsBaseOffset + 4 * StorageDescsSize;
  112. }
  113. return 0;
  114. }
  115. }
  116. }