GlobalToStorage.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. int storageIndex = SearchForStorageBase(block, source, sbStart, sbEnd);
  23. if (storageIndex >= 0)
  24. {
  25. // Storage buffers are implemented using global memory access.
  26. // If we know from where the base address of the access is loaded,
  27. // we can guess which storage buffer it is accessing.
  28. // We can then replace the global memory access with a storage
  29. // buffer access.
  30. node = ReplaceGlobalWithStorage(node, config, storageIndex);
  31. }
  32. else if (config.Stage == ShaderStage.Compute && operation.Inst == Instruction.LoadGlobal)
  33. {
  34. // Here we effectively try to replace a LDG instruction with LDC.
  35. // The hardware only supports a limited amount of constant buffers
  36. // so NVN "emulates" more constant buffers using global memory access.
  37. // Here we try to replace the global access back to a constant buffer
  38. // load.
  39. storageIndex = SearchForStorageBase(block, source, UbeBaseOffset, UbeBaseOffset + UbeDescsSize);
  40. if (storageIndex >= 0)
  41. {
  42. node = ReplaceLdgWithLdc(node, config, storageIndex);
  43. }
  44. }
  45. }
  46. }
  47. }
  48. private static LinkedListNode<INode> ReplaceGlobalWithStorage(LinkedListNode<INode> node, ShaderConfig config, int storageIndex)
  49. {
  50. Operation operation = (Operation)node.Value;
  51. Operand GetStorageOffset()
  52. {
  53. Operand addrLow = operation.GetSource(0);
  54. Operand baseAddrLow = Cbuf(0, GetStorageCbOffset(config.Stage, storageIndex));
  55. Operand baseAddrTrunc = Local();
  56. Operand alignMask = Const(-config.GpuAccessor.QueryStorageBufferOffsetAlignment());
  57. Operation andOp = new Operation(Instruction.BitwiseAnd, baseAddrTrunc, baseAddrLow, alignMask);
  58. node.List.AddBefore(node, andOp);
  59. Operand byteOffset = Local();
  60. Operand wordOffset = Local();
  61. Operation subOp = new Operation(Instruction.Subtract, byteOffset, addrLow, baseAddrTrunc);
  62. Operation shrOp = new Operation(Instruction.ShiftRightU32, wordOffset, byteOffset, Const(2));
  63. node.List.AddBefore(node, subOp);
  64. node.List.AddBefore(node, shrOp);
  65. return wordOffset;
  66. }
  67. Operand[] sources = new Operand[operation.SourcesCount];
  68. sources[0] = Const(storageIndex);
  69. sources[1] = GetStorageOffset();
  70. for (int index = 2; index < operation.SourcesCount; index++)
  71. {
  72. sources[index] = operation.GetSource(index);
  73. }
  74. Operation storageOp;
  75. if (operation.Inst.IsAtomic())
  76. {
  77. Instruction inst = (operation.Inst & ~Instruction.MrMask) | Instruction.MrStorage;
  78. storageOp = new Operation(inst, operation.Dest, sources);
  79. }
  80. else if (operation.Inst == Instruction.LoadGlobal)
  81. {
  82. storageOp = new Operation(Instruction.LoadStorage, operation.Dest, sources);
  83. }
  84. else
  85. {
  86. storageOp = new Operation(Instruction.StoreStorage, null, sources);
  87. }
  88. for (int index = 0; index < operation.SourcesCount; index++)
  89. {
  90. operation.SetSource(index, null);
  91. }
  92. LinkedListNode<INode> oldNode = node;
  93. node = node.List.AddBefore(node, storageOp);
  94. node.List.Remove(oldNode);
  95. return node;
  96. }
  97. private static LinkedListNode<INode> ReplaceLdgWithLdc(LinkedListNode<INode> node, ShaderConfig config, int storageIndex)
  98. {
  99. Operation operation = (Operation)node.Value;
  100. Operand GetCbufOffset()
  101. {
  102. Operand addrLow = operation.GetSource(0);
  103. Operand baseAddrLow = Cbuf(0, UbeBaseOffset + storageIndex * StorageDescSize);
  104. Operand baseAddrTrunc = Local();
  105. Operand alignMask = Const(-config.GpuAccessor.QueryStorageBufferOffsetAlignment());
  106. Operation andOp = new Operation(Instruction.BitwiseAnd, baseAddrTrunc, baseAddrLow, alignMask);
  107. node.List.AddBefore(node, andOp);
  108. Operand byteOffset = Local();
  109. Operand wordOffset = Local();
  110. Operation subOp = new Operation(Instruction.Subtract, byteOffset, addrLow, baseAddrTrunc);
  111. Operation shrOp = new Operation(Instruction.ShiftRightU32, wordOffset, byteOffset, Const(2));
  112. node.List.AddBefore(node, subOp);
  113. node.List.AddBefore(node, shrOp);
  114. return wordOffset;
  115. }
  116. Operand[] sources = new Operand[operation.SourcesCount];
  117. sources[0] = Const(UbeFirstCbuf + storageIndex);
  118. sources[1] = GetCbufOffset();
  119. for (int index = 2; index < operation.SourcesCount; index++)
  120. {
  121. sources[index] = operation.GetSource(index);
  122. }
  123. Operation ldcOp = new Operation(Instruction.LoadConstant, operation.Dest, sources);
  124. for (int index = 0; index < operation.SourcesCount; index++)
  125. {
  126. operation.SetSource(index, null);
  127. }
  128. LinkedListNode<INode> oldNode = node;
  129. node = node.List.AddBefore(node, ldcOp);
  130. node.List.Remove(oldNode);
  131. return node;
  132. }
  133. private static int SearchForStorageBase(BasicBlock block, Operand globalAddress, int sbStart, int sbEnd)
  134. {
  135. globalAddress = Utils.FindLastOperation(globalAddress, block);
  136. if (globalAddress.Type == OperandType.ConstantBuffer)
  137. {
  138. return GetStorageIndex(globalAddress, sbStart, sbEnd);
  139. }
  140. Operation operation = globalAddress.AsgOp as Operation;
  141. if (operation == null || operation.Inst != Instruction.Add)
  142. {
  143. return -1;
  144. }
  145. Operand src1 = operation.GetSource(0);
  146. Operand src2 = operation.GetSource(1);
  147. if ((src1.Type == OperandType.LocalVariable && src2.Type == OperandType.Constant) ||
  148. (src2.Type == OperandType.LocalVariable && src1.Type == OperandType.Constant))
  149. {
  150. if (src1.Type == OperandType.LocalVariable)
  151. {
  152. operation = Utils.FindLastOperation(src1, block).AsgOp as Operation;
  153. }
  154. else
  155. {
  156. operation = Utils.FindLastOperation(src2, block).AsgOp as Operation;
  157. }
  158. if (operation == null || operation.Inst != Instruction.Add)
  159. {
  160. return -1;
  161. }
  162. }
  163. for (int index = 0; index < operation.SourcesCount; index++)
  164. {
  165. Operand source = operation.GetSource(index);
  166. int storageIndex = GetStorageIndex(source, sbStart, sbEnd);
  167. if (storageIndex != -1)
  168. {
  169. return storageIndex;
  170. }
  171. }
  172. return -1;
  173. }
  174. private static int GetStorageIndex(Operand operand, int sbStart, int sbEnd)
  175. {
  176. if (operand.Type == OperandType.ConstantBuffer)
  177. {
  178. int slot = operand.GetCbufSlot();
  179. int offset = operand.GetCbufOffset();
  180. if (slot == 0 && offset >= sbStart && offset < sbEnd)
  181. {
  182. int storageIndex = (offset - sbStart) / StorageDescSize;
  183. return storageIndex;
  184. }
  185. }
  186. return -1;
  187. }
  188. }
  189. }