GlobalToStorage.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using System.Collections.Generic;
  3. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  4. using static Ryujinx.Graphics.Shader.Translation.GlobalMemory;
  5. namespace Ryujinx.Graphics.Shader.Translation.Optimizations
  6. {
  7. static class GlobalToStorage
  8. {
  9. public static void RunPass(BasicBlock block, ShaderConfig config)
  10. {
  11. int sbStart = GetStorageBaseCbOffset(config.Stage);
  12. int sbEnd = sbStart + StorageDescsSize;
  13. for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
  14. {
  15. if (!(node.Value is Operation operation))
  16. {
  17. continue;
  18. }
  19. if (UsesGlobalMemory(operation.Inst))
  20. {
  21. Operand source = operation.GetSource(0);
  22. if (source.AsgOp is Operation asgOperation)
  23. {
  24. int storageIndex = SearchForStorageBase(asgOperation, sbStart, sbEnd);
  25. if (storageIndex >= 0)
  26. {
  27. node = ReplaceGlobalWithStorage(node, config, storageIndex);
  28. }
  29. }
  30. }
  31. }
  32. }
  33. private static LinkedListNode<INode> ReplaceGlobalWithStorage(LinkedListNode<INode> node, ShaderConfig config, int storageIndex)
  34. {
  35. Operation operation = (Operation)node.Value;
  36. Operation storageOp;
  37. Operand GetStorageOffset()
  38. {
  39. Operand addrLow = operation.GetSource(0);
  40. Operand baseAddrLow = Cbuf(0, GetStorageCbOffset(config.Stage, storageIndex));
  41. Operand baseAddrTrunc = Local();
  42. Operand alignMask = Const(-config.Capabilities.StorageBufferOffsetAlignment);
  43. Operation andOp = new Operation(Instruction.BitwiseAnd, baseAddrTrunc, baseAddrLow, alignMask);
  44. node.List.AddBefore(node, andOp);
  45. Operand byteOffset = Local();
  46. Operand wordOffset = Local();
  47. Operation subOp = new Operation(Instruction.Subtract, byteOffset, addrLow, baseAddrTrunc);
  48. Operation shrOp = new Operation(Instruction.ShiftRightU32, wordOffset, byteOffset, Const(2));
  49. node.List.AddBefore(node, subOp);
  50. node.List.AddBefore(node, shrOp);
  51. return wordOffset;
  52. }
  53. Operand[] sources = new Operand[operation.SourcesCount];
  54. sources[0] = Const(storageIndex);
  55. sources[1] = GetStorageOffset();
  56. for (int index = 2; index < operation.SourcesCount; index++)
  57. {
  58. sources[index] = operation.GetSource(index);
  59. }
  60. if (operation.Inst.IsAtomic())
  61. {
  62. Instruction inst = (operation.Inst & ~Instruction.MrMask) | Instruction.MrStorage;
  63. storageOp = new Operation(inst, operation.Dest, sources);
  64. }
  65. else if (operation.Inst == Instruction.LoadGlobal)
  66. {
  67. storageOp = new Operation(Instruction.LoadStorage, operation.Dest, sources);
  68. }
  69. else
  70. {
  71. storageOp = new Operation(Instruction.StoreStorage, null, sources);
  72. }
  73. for (int index = 0; index < operation.SourcesCount; index++)
  74. {
  75. operation.SetSource(index, null);
  76. }
  77. LinkedListNode<INode> oldNode = node;
  78. node = node.List.AddBefore(node, storageOp);
  79. node.List.Remove(oldNode);
  80. return node;
  81. }
  82. private static int SearchForStorageBase(Operation operation, int sbStart, int sbEnd)
  83. {
  84. Queue<Operation> assignments = new Queue<Operation>();
  85. assignments.Enqueue(operation);
  86. while (assignments.TryDequeue(out operation))
  87. {
  88. for (int index = 0; index < operation.SourcesCount; index++)
  89. {
  90. Operand source = operation.GetSource(index);
  91. if (source.Type == OperandType.ConstantBuffer)
  92. {
  93. int slot = source.GetCbufSlot();
  94. int offset = source.GetCbufOffset();
  95. if (slot == 0 && offset >= sbStart && offset < sbEnd)
  96. {
  97. int storageIndex = (offset - sbStart) / StorageDescSize;
  98. return storageIndex;
  99. }
  100. }
  101. if (source.AsgOp is Operation asgOperation)
  102. {
  103. assignments.Enqueue(asgOperation);
  104. }
  105. }
  106. }
  107. return -1;
  108. }
  109. }
  110. }