GlobalToStorage.cs 15 KB

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