GlobalToStorage.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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(block, 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(BasicBlock block, LinkedListNode<INode> node, ShaderConfig config, int storageIndex)
  49. {
  50. Operation operation = (Operation)node.Value;
  51. bool isAtomic = operation.Inst.IsAtomic();
  52. bool isStg16Or8 = operation.Inst == Instruction.StoreGlobal16 || operation.Inst == Instruction.StoreGlobal8;
  53. bool isWrite = isAtomic || operation.Inst == Instruction.StoreGlobal || isStg16Or8;
  54. config.SetUsedStorageBuffer(storageIndex, isWrite);
  55. Operand[] sources = new Operand[operation.SourcesCount];
  56. sources[0] = Const(storageIndex);
  57. sources[1] = GetStorageOffset(block, node, config, storageIndex, operation.GetSource(0), isStg16Or8);
  58. for (int index = 2; index < operation.SourcesCount; index++)
  59. {
  60. sources[index] = operation.GetSource(index);
  61. }
  62. Operation storageOp;
  63. if (isAtomic)
  64. {
  65. Instruction inst = (operation.Inst & ~Instruction.MrMask) | Instruction.MrStorage;
  66. storageOp = new Operation(inst, operation.Dest, sources);
  67. }
  68. else if (operation.Inst == Instruction.LoadGlobal)
  69. {
  70. storageOp = new Operation(Instruction.LoadStorage, operation.Dest, sources);
  71. }
  72. else
  73. {
  74. Instruction storeInst = operation.Inst switch
  75. {
  76. Instruction.StoreGlobal16 => Instruction.StoreStorage16,
  77. Instruction.StoreGlobal8 => Instruction.StoreStorage8,
  78. _ => Instruction.StoreStorage
  79. };
  80. storageOp = new Operation(storeInst, null, sources);
  81. }
  82. for (int index = 0; index < operation.SourcesCount; index++)
  83. {
  84. operation.SetSource(index, null);
  85. }
  86. LinkedListNode<INode> oldNode = node;
  87. node = node.List.AddBefore(node, storageOp);
  88. node.List.Remove(oldNode);
  89. return node;
  90. }
  91. private static Operand GetStorageOffset(
  92. BasicBlock block,
  93. LinkedListNode<INode> node,
  94. ShaderConfig config,
  95. int storageIndex,
  96. Operand addrLow,
  97. bool isStg16Or8)
  98. {
  99. int baseAddressCbOffset = GetStorageCbOffset(config.Stage, storageIndex);
  100. bool storageAligned = !(config.GpuAccessor.QueryHasUnalignedStorageBuffer() || config.GpuAccessor.QueryHostStorageBufferOffsetAlignment() > Constants.StorageAlignment);
  101. (Operand byteOffset, int constantOffset) = storageAligned ?
  102. GetStorageOffset(block, Utils.FindLastOperation(addrLow, block), baseAddressCbOffset) :
  103. (null, 0);
  104. if (byteOffset != null)
  105. {
  106. ReplaceAddressAlignment(node.List, addrLow, byteOffset, constantOffset);
  107. }
  108. if (byteOffset == null)
  109. {
  110. Operand baseAddrLow = Cbuf(0, baseAddressCbOffset);
  111. Operand baseAddrTrunc = Local();
  112. Operand alignMask = Const(-config.GpuAccessor.QueryHostStorageBufferOffsetAlignment());
  113. Operation andOp = new Operation(Instruction.BitwiseAnd, baseAddrTrunc, baseAddrLow, alignMask);
  114. node.List.AddBefore(node, andOp);
  115. Operand offset = Local();
  116. Operation subOp = new Operation(Instruction.Subtract, offset, addrLow, baseAddrTrunc);
  117. node.List.AddBefore(node, subOp);
  118. byteOffset = offset;
  119. }
  120. else if (constantOffset != 0)
  121. {
  122. Operand offset = Local();
  123. Operation addOp = new Operation(Instruction.Add, offset, byteOffset, Const(constantOffset));
  124. node.List.AddBefore(node, addOp);
  125. byteOffset = offset;
  126. }
  127. if (isStg16Or8)
  128. {
  129. return byteOffset;
  130. }
  131. Operand wordOffset = Local();
  132. Operation shrOp = new Operation(Instruction.ShiftRightU32, wordOffset, byteOffset, Const(2));
  133. node.List.AddBefore(node, shrOp);
  134. return wordOffset;
  135. }
  136. private static bool IsCb0Offset(Operand operand, int offset)
  137. {
  138. return operand.Type == OperandType.ConstantBuffer && operand.GetCbufSlot() == 0 && operand.GetCbufOffset() == offset;
  139. }
  140. private static void ReplaceAddressAlignment(LinkedList<INode> list, Operand address, Operand byteOffset, int constantOffset)
  141. {
  142. // When we emit 16/8-bit LDG, we add extra code to determine the address alignment.
  143. // Eliminate the storage buffer base address from this too, leaving only the byte offset.
  144. foreach (INode useNode in address.UseOps)
  145. {
  146. if (useNode is Operation op && op.Inst == Instruction.BitwiseAnd)
  147. {
  148. Operand src1 = op.GetSource(0);
  149. Operand src2 = op.GetSource(1);
  150. int addressIndex = -1;
  151. if (src1 == address && src2.Type == OperandType.Constant && src2.Value == 3)
  152. {
  153. addressIndex = 0;
  154. }
  155. else if (src2 == address && src1.Type == OperandType.Constant && src1.Value == 3)
  156. {
  157. addressIndex = 1;
  158. }
  159. if (addressIndex != -1)
  160. {
  161. LinkedListNode<INode> node = list.Find(op);
  162. // Add offset calculation before the use. Needs to be on the same block.
  163. if (node != null)
  164. {
  165. Operand offset = Local();
  166. Operation addOp = new Operation(Instruction.Add, offset, byteOffset, Const(constantOffset));
  167. list.AddBefore(node, addOp);
  168. op.SetSource(addressIndex, offset);
  169. }
  170. }
  171. }
  172. }
  173. }
  174. private static (Operand, int) GetStorageOffset(BasicBlock block, Operand address, int baseAddressCbOffset)
  175. {
  176. if (IsCb0Offset(address, baseAddressCbOffset))
  177. {
  178. // Direct offset: zero.
  179. return (Const(0), 0);
  180. }
  181. (address, int constantOffset) = GetStorageConstantOffset(block, address);
  182. address = Utils.FindLastOperation(address, block);
  183. if (IsCb0Offset(address, baseAddressCbOffset))
  184. {
  185. // Only constant offset
  186. return (Const(0), constantOffset);
  187. }
  188. if (!(address.AsgOp is Operation offsetAdd) || offsetAdd.Inst != Instruction.Add)
  189. {
  190. return (null, 0);
  191. }
  192. Operand src1 = offsetAdd.GetSource(0);
  193. Operand src2 = Utils.FindLastOperation(offsetAdd.GetSource(1), block);
  194. if (IsCb0Offset(src2, baseAddressCbOffset))
  195. {
  196. return (src1, constantOffset);
  197. }
  198. else if (IsCb0Offset(src1, baseAddressCbOffset))
  199. {
  200. return (src2, constantOffset);
  201. }
  202. return (null, 0);
  203. }
  204. private static (Operand, int) GetStorageConstantOffset(BasicBlock block, Operand address)
  205. {
  206. if (!(address.AsgOp is Operation offsetAdd) || offsetAdd.Inst != Instruction.Add)
  207. {
  208. return (address, 0);
  209. }
  210. Operand src1 = offsetAdd.GetSource(0);
  211. Operand src2 = offsetAdd.GetSource(1);
  212. if (src2.Type != OperandType.Constant)
  213. {
  214. return (address, 0);
  215. }
  216. return (src1, src2.Value);
  217. }
  218. private static LinkedListNode<INode> ReplaceLdgWithLdc(LinkedListNode<INode> node, ShaderConfig config, int storageIndex)
  219. {
  220. Operation operation = (Operation)node.Value;
  221. Operand GetCbufOffset()
  222. {
  223. Operand addrLow = operation.GetSource(0);
  224. Operand baseAddrLow = Cbuf(0, UbeBaseOffset + storageIndex * StorageDescSize);
  225. Operand baseAddrTrunc = Local();
  226. Operand alignMask = Const(-config.GpuAccessor.QueryHostStorageBufferOffsetAlignment());
  227. Operation andOp = new Operation(Instruction.BitwiseAnd, baseAddrTrunc, baseAddrLow, alignMask);
  228. node.List.AddBefore(node, andOp);
  229. Operand byteOffset = Local();
  230. Operand wordOffset = Local();
  231. Operation subOp = new Operation(Instruction.Subtract, byteOffset, addrLow, baseAddrTrunc);
  232. Operation shrOp = new Operation(Instruction.ShiftRightU32, wordOffset, byteOffset, Const(2));
  233. node.List.AddBefore(node, subOp);
  234. node.List.AddBefore(node, shrOp);
  235. return wordOffset;
  236. }
  237. Operand[] sources = new Operand[operation.SourcesCount];
  238. int cbSlot = UbeFirstCbuf + storageIndex;
  239. sources[0] = Const(cbSlot);
  240. sources[1] = GetCbufOffset();
  241. config.SetUsedConstantBuffer(cbSlot);
  242. for (int index = 2; index < operation.SourcesCount; index++)
  243. {
  244. sources[index] = operation.GetSource(index);
  245. }
  246. Operation ldcOp = new Operation(Instruction.LoadConstant, operation.Dest, sources);
  247. for (int index = 0; index < operation.SourcesCount; index++)
  248. {
  249. operation.SetSource(index, null);
  250. }
  251. LinkedListNode<INode> oldNode = node;
  252. node = node.List.AddBefore(node, ldcOp);
  253. node.List.Remove(oldNode);
  254. return node;
  255. }
  256. private static int SearchForStorageBase(BasicBlock block, Operand globalAddress, int sbStart, int sbEnd)
  257. {
  258. globalAddress = Utils.FindLastOperation(globalAddress, block);
  259. if (globalAddress.Type == OperandType.ConstantBuffer)
  260. {
  261. return GetStorageIndex(globalAddress, sbStart, sbEnd);
  262. }
  263. Operation operation = globalAddress.AsgOp as Operation;
  264. if (operation == null || operation.Inst != Instruction.Add)
  265. {
  266. return -1;
  267. }
  268. Operand src1 = operation.GetSource(0);
  269. Operand src2 = operation.GetSource(1);
  270. if ((src1.Type == OperandType.LocalVariable && src2.Type == OperandType.Constant) ||
  271. (src2.Type == OperandType.LocalVariable && src1.Type == OperandType.Constant))
  272. {
  273. if (src1.Type == OperandType.LocalVariable)
  274. {
  275. operation = Utils.FindLastOperation(src1, block).AsgOp as Operation;
  276. }
  277. else
  278. {
  279. operation = Utils.FindLastOperation(src2, block).AsgOp as Operation;
  280. }
  281. if (operation == null || operation.Inst != Instruction.Add)
  282. {
  283. return -1;
  284. }
  285. }
  286. for (int index = 0; index < operation.SourcesCount; index++)
  287. {
  288. Operand source = operation.GetSource(index);
  289. int storageIndex = GetStorageIndex(source, sbStart, sbEnd);
  290. if (storageIndex != -1)
  291. {
  292. return storageIndex;
  293. }
  294. }
  295. return -1;
  296. }
  297. private static int GetStorageIndex(Operand operand, int sbStart, int sbEnd)
  298. {
  299. if (operand.Type == OperandType.ConstantBuffer)
  300. {
  301. int slot = operand.GetCbufSlot();
  302. int offset = operand.GetCbufOffset();
  303. if (slot == 0 && offset >= sbStart && offset < sbEnd)
  304. {
  305. int storageIndex = (offset - sbStart) / StorageDescSize;
  306. return storageIndex;
  307. }
  308. }
  309. return -1;
  310. }
  311. }
  312. }