GlobalToStorage.cs 15 KB

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