GlobalToStorage.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. 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 GetStorageOffset()
  56. {
  57. Operand addrLow = operation.GetSource(0);
  58. Operand baseAddrLow = config.CreateCbuf(0, GetStorageCbOffset(config.Stage, storageIndex));
  59. Operand baseAddrTrunc = Local();
  60. Operand alignMask = Const(-config.GpuAccessor.QueryHostStorageBufferOffsetAlignment());
  61. Operation andOp = new Operation(Instruction.BitwiseAnd, baseAddrTrunc, baseAddrLow, alignMask);
  62. node.List.AddBefore(node, andOp);
  63. Operand byteOffset = Local();
  64. Operation subOp = new Operation(Instruction.Subtract, byteOffset, addrLow, baseAddrTrunc);
  65. node.List.AddBefore(node, subOp);
  66. if (isStg16Or8)
  67. {
  68. return byteOffset;
  69. }
  70. Operand wordOffset = Local();
  71. Operation shrOp = new Operation(Instruction.ShiftRightU32, wordOffset, byteOffset, Const(2));
  72. node.List.AddBefore(node, shrOp);
  73. return wordOffset;
  74. }
  75. Operand[] sources = new Operand[operation.SourcesCount];
  76. sources[0] = Const(storageIndex);
  77. sources[1] = GetStorageOffset();
  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 LinkedListNode<INode> ReplaceLdgWithLdc(LinkedListNode<INode> node, ShaderConfig config, int storageIndex)
  112. {
  113. Operation operation = (Operation)node.Value;
  114. Operand GetCbufOffset()
  115. {
  116. Operand addrLow = operation.GetSource(0);
  117. Operand baseAddrLow = config.CreateCbuf(0, UbeBaseOffset + storageIndex * StorageDescSize);
  118. Operand baseAddrTrunc = Local();
  119. Operand alignMask = Const(-config.GpuAccessor.QueryHostStorageBufferOffsetAlignment());
  120. Operation andOp = new Operation(Instruction.BitwiseAnd, baseAddrTrunc, baseAddrLow, alignMask);
  121. node.List.AddBefore(node, andOp);
  122. Operand byteOffset = Local();
  123. Operand wordOffset = Local();
  124. Operation subOp = new Operation(Instruction.Subtract, byteOffset, addrLow, baseAddrTrunc);
  125. Operation shrOp = new Operation(Instruction.ShiftRightU32, wordOffset, byteOffset, Const(2));
  126. node.List.AddBefore(node, subOp);
  127. node.List.AddBefore(node, shrOp);
  128. return wordOffset;
  129. }
  130. Operand[] sources = new Operand[operation.SourcesCount];
  131. int cbSlot = UbeFirstCbuf + storageIndex;
  132. sources[0] = Const(cbSlot);
  133. sources[1] = GetCbufOffset();
  134. config.SetUsedConstantBuffer(cbSlot);
  135. for (int index = 2; index < operation.SourcesCount; index++)
  136. {
  137. sources[index] = operation.GetSource(index);
  138. }
  139. Operation ldcOp = new Operation(Instruction.LoadConstant, operation.Dest, sources);
  140. for (int index = 0; index < operation.SourcesCount; index++)
  141. {
  142. operation.SetSource(index, null);
  143. }
  144. LinkedListNode<INode> oldNode = node;
  145. node = node.List.AddBefore(node, ldcOp);
  146. node.List.Remove(oldNode);
  147. return node;
  148. }
  149. private static int SearchForStorageBase(BasicBlock block, Operand globalAddress, int sbStart, int sbEnd)
  150. {
  151. globalAddress = Utils.FindLastOperation(globalAddress, block);
  152. if (globalAddress.Type == OperandType.ConstantBuffer)
  153. {
  154. return GetStorageIndex(globalAddress, sbStart, sbEnd);
  155. }
  156. Operation operation = globalAddress.AsgOp as Operation;
  157. if (operation == null || operation.Inst != Instruction.Add)
  158. {
  159. return -1;
  160. }
  161. Operand src1 = operation.GetSource(0);
  162. Operand src2 = operation.GetSource(1);
  163. if ((src1.Type == OperandType.LocalVariable && src2.Type == OperandType.Constant) ||
  164. (src2.Type == OperandType.LocalVariable && src1.Type == OperandType.Constant))
  165. {
  166. if (src1.Type == OperandType.LocalVariable)
  167. {
  168. operation = Utils.FindLastOperation(src1, block).AsgOp as Operation;
  169. }
  170. else
  171. {
  172. operation = Utils.FindLastOperation(src2, block).AsgOp as Operation;
  173. }
  174. if (operation == null || operation.Inst != Instruction.Add)
  175. {
  176. return -1;
  177. }
  178. }
  179. for (int index = 0; index < operation.SourcesCount; index++)
  180. {
  181. Operand source = operation.GetSource(index);
  182. int storageIndex = GetStorageIndex(source, sbStart, sbEnd);
  183. if (storageIndex != -1)
  184. {
  185. return storageIndex;
  186. }
  187. }
  188. return -1;
  189. }
  190. private static int GetStorageIndex(Operand operand, int sbStart, int sbEnd)
  191. {
  192. if (operand.Type == OperandType.ConstantBuffer)
  193. {
  194. int slot = operand.GetCbufSlot();
  195. int offset = operand.GetCbufOffset();
  196. if (slot == 0 && offset >= sbStart && offset < sbEnd)
  197. {
  198. int storageIndex = (offset - sbStart) / StorageDescSize;
  199. return storageIndex;
  200. }
  201. }
  202. return -1;
  203. }
  204. }
  205. }