GlobalToStorage.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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, ref int sbUseMask, ref int ubeUseMask)
  10. {
  11. int sbStart = GetStorageBaseCbOffset(config.Stage);
  12. int sbEnd = sbStart + StorageDescsSize;
  13. int ubeStart = UbeBaseOffset;
  14. int ubeEnd = UbeBaseOffset + UbeDescsSize;
  15. for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
  16. {
  17. for (int index = 0; index < node.Value.SourcesCount; index++)
  18. {
  19. Operand src = node.Value.GetSource(index);
  20. int storageIndex = GetStorageIndex(src, sbStart, sbEnd);
  21. if (storageIndex >= 0)
  22. {
  23. sbUseMask |= 1 << storageIndex;
  24. }
  25. if (config.Stage == ShaderStage.Compute)
  26. {
  27. int constantIndex = GetStorageIndex(src, ubeStart, ubeEnd);
  28. if (constantIndex >= 0)
  29. {
  30. ubeUseMask |= 1 << constantIndex;
  31. }
  32. }
  33. }
  34. if (!(node.Value is Operation operation))
  35. {
  36. continue;
  37. }
  38. if (UsesGlobalMemory(operation.Inst))
  39. {
  40. Operand source = operation.GetSource(0);
  41. int storageIndex = SearchForStorageBase(block, source, sbStart, sbEnd);
  42. if (storageIndex >= 0)
  43. {
  44. // Storage buffers are implemented using global memory access.
  45. // If we know from where the base address of the access is loaded,
  46. // we can guess which storage buffer it is accessing.
  47. // We can then replace the global memory access with a storage
  48. // buffer access.
  49. node = ReplaceGlobalWithStorage(block, node, config, storageIndex);
  50. }
  51. else if (config.Stage == ShaderStage.Compute && operation.Inst == Instruction.LoadGlobal)
  52. {
  53. // Here we effectively try to replace a LDG instruction with LDC.
  54. // The hardware only supports a limited amount of constant buffers
  55. // so NVN "emulates" more constant buffers using global memory access.
  56. // Here we try to replace the global access back to a constant buffer
  57. // load.
  58. storageIndex = SearchForStorageBase(block, source, ubeStart, ubeStart + ubeEnd);
  59. if (storageIndex >= 0)
  60. {
  61. node = ReplaceLdgWithLdc(node, config, storageIndex);
  62. }
  63. }
  64. }
  65. }
  66. config.SetAccessibleBufferMasks(sbUseMask, ubeUseMask);
  67. }
  68. private static LinkedListNode<INode> ReplaceGlobalWithStorage(BasicBlock block, LinkedListNode<INode> node, ShaderConfig config, int storageIndex)
  69. {
  70. Operation operation = (Operation)node.Value;
  71. bool isAtomic = operation.Inst.IsAtomic();
  72. bool isStg16Or8 = operation.Inst == Instruction.StoreGlobal16 || operation.Inst == Instruction.StoreGlobal8;
  73. bool isWrite = isAtomic || operation.Inst == Instruction.StoreGlobal || isStg16Or8;
  74. config.SetUsedStorageBuffer(storageIndex, isWrite);
  75. Operand[] sources = new Operand[operation.SourcesCount];
  76. sources[0] = Const(storageIndex);
  77. sources[1] = GetStorageOffset(block, node, config, storageIndex, operation.GetSource(0), isStg16Or8);
  78. for (int index = 2; index < operation.SourcesCount; index++)
  79. {
  80. sources[index] = operation.GetSource(index);
  81. }
  82. Operation storageOp;
  83. if (isAtomic)
  84. {
  85. Instruction inst = (operation.Inst & ~Instruction.MrMask) | Instruction.MrStorage;
  86. storageOp = new Operation(inst, operation.Dest, sources);
  87. }
  88. else if (operation.Inst == Instruction.LoadGlobal)
  89. {
  90. storageOp = new Operation(Instruction.LoadStorage, operation.Dest, sources);
  91. }
  92. else
  93. {
  94. Instruction storeInst = operation.Inst switch
  95. {
  96. Instruction.StoreGlobal16 => Instruction.StoreStorage16,
  97. Instruction.StoreGlobal8 => Instruction.StoreStorage8,
  98. _ => Instruction.StoreStorage
  99. };
  100. storageOp = new Operation(storeInst, null, sources);
  101. }
  102. for (int index = 0; index < operation.SourcesCount; index++)
  103. {
  104. operation.SetSource(index, null);
  105. }
  106. LinkedListNode<INode> oldNode = node;
  107. node = node.List.AddBefore(node, storageOp);
  108. node.List.Remove(oldNode);
  109. return node;
  110. }
  111. private static Operand GetStorageOffset(
  112. BasicBlock block,
  113. LinkedListNode<INode> node,
  114. ShaderConfig config,
  115. int storageIndex,
  116. Operand addrLow,
  117. bool isStg16Or8)
  118. {
  119. int baseAddressCbOffset = GetStorageCbOffset(config.Stage, storageIndex);
  120. bool storageAligned = !(config.GpuAccessor.QueryHasUnalignedStorageBuffer() || config.GpuAccessor.QueryHostStorageBufferOffsetAlignment() > Constants.StorageAlignment);
  121. (Operand byteOffset, int constantOffset) = storageAligned ?
  122. GetStorageOffset(block, Utils.FindLastOperation(addrLow, block), baseAddressCbOffset) :
  123. (null, 0);
  124. if (byteOffset != null)
  125. {
  126. ReplaceAddressAlignment(node.List, addrLow, byteOffset, constantOffset);
  127. }
  128. if (byteOffset == null)
  129. {
  130. Operand baseAddrLow = Cbuf(0, baseAddressCbOffset);
  131. Operand baseAddrTrunc = Local();
  132. Operand alignMask = Const(-config.GpuAccessor.QueryHostStorageBufferOffsetAlignment());
  133. Operation andOp = new Operation(Instruction.BitwiseAnd, baseAddrTrunc, baseAddrLow, alignMask);
  134. node.List.AddBefore(node, andOp);
  135. Operand offset = Local();
  136. Operation subOp = new Operation(Instruction.Subtract, offset, addrLow, baseAddrTrunc);
  137. node.List.AddBefore(node, subOp);
  138. byteOffset = offset;
  139. }
  140. else if (constantOffset != 0)
  141. {
  142. Operand offset = Local();
  143. Operation addOp = new Operation(Instruction.Add, offset, byteOffset, Const(constantOffset));
  144. node.List.AddBefore(node, addOp);
  145. byteOffset = offset;
  146. }
  147. if (isStg16Or8)
  148. {
  149. return byteOffset;
  150. }
  151. Operand wordOffset = Local();
  152. Operation shrOp = new Operation(Instruction.ShiftRightU32, wordOffset, byteOffset, Const(2));
  153. node.List.AddBefore(node, shrOp);
  154. return wordOffset;
  155. }
  156. private static bool IsCb0Offset(Operand operand, int offset)
  157. {
  158. return operand.Type == OperandType.ConstantBuffer && operand.GetCbufSlot() == 0 && operand.GetCbufOffset() == offset;
  159. }
  160. private static void ReplaceAddressAlignment(LinkedList<INode> list, Operand address, Operand byteOffset, int constantOffset)
  161. {
  162. // When we emit 16/8-bit LDG, we add extra code to determine the address alignment.
  163. // Eliminate the storage buffer base address from this too, leaving only the byte offset.
  164. foreach (INode useNode in address.UseOps)
  165. {
  166. if (useNode is Operation op && op.Inst == Instruction.BitwiseAnd)
  167. {
  168. Operand src1 = op.GetSource(0);
  169. Operand src2 = op.GetSource(1);
  170. int addressIndex = -1;
  171. if (src1 == address && src2.Type == OperandType.Constant && src2.Value == 3)
  172. {
  173. addressIndex = 0;
  174. }
  175. else if (src2 == address && src1.Type == OperandType.Constant && src1.Value == 3)
  176. {
  177. addressIndex = 1;
  178. }
  179. if (addressIndex != -1)
  180. {
  181. LinkedListNode<INode> node = list.Find(op);
  182. // Add offset calculation before the use. Needs to be on the same block.
  183. if (node != null)
  184. {
  185. Operand offset = Local();
  186. Operation addOp = new Operation(Instruction.Add, offset, byteOffset, Const(constantOffset));
  187. list.AddBefore(node, addOp);
  188. op.SetSource(addressIndex, offset);
  189. }
  190. }
  191. }
  192. }
  193. }
  194. private static (Operand, int) GetStorageOffset(BasicBlock block, Operand address, int baseAddressCbOffset)
  195. {
  196. if (IsCb0Offset(address, baseAddressCbOffset))
  197. {
  198. // Direct offset: zero.
  199. return (Const(0), 0);
  200. }
  201. (address, int constantOffset) = GetStorageConstantOffset(block, address);
  202. address = Utils.FindLastOperation(address, block);
  203. if (IsCb0Offset(address, baseAddressCbOffset))
  204. {
  205. // Only constant offset
  206. return (Const(0), constantOffset);
  207. }
  208. if (!(address.AsgOp is Operation offsetAdd) || offsetAdd.Inst != Instruction.Add)
  209. {
  210. return (null, 0);
  211. }
  212. Operand src1 = offsetAdd.GetSource(0);
  213. Operand src2 = Utils.FindLastOperation(offsetAdd.GetSource(1), block);
  214. if (IsCb0Offset(src2, baseAddressCbOffset))
  215. {
  216. return (src1, constantOffset);
  217. }
  218. else if (IsCb0Offset(src1, baseAddressCbOffset))
  219. {
  220. return (src2, constantOffset);
  221. }
  222. return (null, 0);
  223. }
  224. private static (Operand, int) GetStorageConstantOffset(BasicBlock block, Operand address)
  225. {
  226. if (!(address.AsgOp is Operation offsetAdd) || offsetAdd.Inst != Instruction.Add)
  227. {
  228. return (address, 0);
  229. }
  230. Operand src1 = offsetAdd.GetSource(0);
  231. Operand src2 = offsetAdd.GetSource(1);
  232. if (src2.Type != OperandType.Constant)
  233. {
  234. return (address, 0);
  235. }
  236. return (src1, src2.Value);
  237. }
  238. private static LinkedListNode<INode> ReplaceLdgWithLdc(LinkedListNode<INode> node, ShaderConfig config, int storageIndex)
  239. {
  240. Operation operation = (Operation)node.Value;
  241. Operand GetCbufOffset()
  242. {
  243. Operand addrLow = operation.GetSource(0);
  244. Operand baseAddrLow = Cbuf(0, UbeBaseOffset + storageIndex * StorageDescSize);
  245. Operand baseAddrTrunc = Local();
  246. Operand alignMask = Const(-config.GpuAccessor.QueryHostStorageBufferOffsetAlignment());
  247. Operation andOp = new Operation(Instruction.BitwiseAnd, baseAddrTrunc, baseAddrLow, alignMask);
  248. node.List.AddBefore(node, andOp);
  249. Operand byteOffset = Local();
  250. Operand wordOffset = Local();
  251. Operation subOp = new Operation(Instruction.Subtract, byteOffset, addrLow, baseAddrTrunc);
  252. Operation shrOp = new Operation(Instruction.ShiftRightU32, wordOffset, byteOffset, Const(2));
  253. node.List.AddBefore(node, subOp);
  254. node.List.AddBefore(node, shrOp);
  255. return wordOffset;
  256. }
  257. Operand[] sources = new Operand[operation.SourcesCount];
  258. int cbSlot = UbeFirstCbuf + storageIndex;
  259. sources[0] = Const(cbSlot);
  260. sources[1] = GetCbufOffset();
  261. config.SetUsedConstantBuffer(cbSlot);
  262. for (int index = 2; index < operation.SourcesCount; index++)
  263. {
  264. sources[index] = operation.GetSource(index);
  265. }
  266. Operation ldcOp = new Operation(Instruction.LoadConstant, operation.Dest, sources);
  267. for (int index = 0; index < operation.SourcesCount; index++)
  268. {
  269. operation.SetSource(index, null);
  270. }
  271. LinkedListNode<INode> oldNode = node;
  272. node = node.List.AddBefore(node, ldcOp);
  273. node.List.Remove(oldNode);
  274. return node;
  275. }
  276. private static int SearchForStorageBase(BasicBlock block, Operand globalAddress, int sbStart, int sbEnd)
  277. {
  278. globalAddress = Utils.FindLastOperation(globalAddress, block);
  279. if (globalAddress.Type == OperandType.ConstantBuffer)
  280. {
  281. return GetStorageIndex(globalAddress, sbStart, sbEnd);
  282. }
  283. Operation operation = globalAddress.AsgOp as Operation;
  284. if (operation == null || operation.Inst != Instruction.Add)
  285. {
  286. return -1;
  287. }
  288. Operand src1 = operation.GetSource(0);
  289. Operand src2 = operation.GetSource(1);
  290. if ((src1.Type == OperandType.LocalVariable && src2.Type == OperandType.Constant) ||
  291. (src2.Type == OperandType.LocalVariable && src1.Type == OperandType.Constant))
  292. {
  293. if (src1.Type == OperandType.LocalVariable)
  294. {
  295. operation = Utils.FindLastOperation(src1, block).AsgOp as Operation;
  296. }
  297. else
  298. {
  299. operation = Utils.FindLastOperation(src2, block).AsgOp as Operation;
  300. }
  301. if (operation == null || operation.Inst != Instruction.Add)
  302. {
  303. return -1;
  304. }
  305. }
  306. for (int index = 0; index < operation.SourcesCount; index++)
  307. {
  308. Operand source = operation.GetSource(index);
  309. int storageIndex = GetStorageIndex(source, sbStart, sbEnd);
  310. if (storageIndex != -1)
  311. {
  312. return storageIndex;
  313. }
  314. }
  315. return -1;
  316. }
  317. private static int GetStorageIndex(Operand operand, int sbStart, int sbEnd)
  318. {
  319. if (operand.Type == OperandType.ConstantBuffer)
  320. {
  321. int slot = operand.GetCbufSlot();
  322. int offset = operand.GetCbufOffset();
  323. if (slot == 0 && offset >= sbStart && offset < sbEnd)
  324. {
  325. int storageIndex = (offset - sbStart) / StorageDescSize;
  326. return storageIndex;
  327. }
  328. }
  329. return -1;
  330. }
  331. }
  332. }