GlobalToStorage.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 isWrite = isAtomic || operation.Inst == Instruction.StoreGlobal;
  53. config.SetUsedStorageBuffer(storageIndex, isWrite);
  54. Operand GetStorageOffset()
  55. {
  56. Operand addrLow = operation.GetSource(0);
  57. Operand baseAddrLow = config.CreateCbuf(0, GetStorageCbOffset(config.Stage, storageIndex));
  58. Operand baseAddrTrunc = Local();
  59. Operand alignMask = Const(-config.GpuAccessor.QueryStorageBufferOffsetAlignment());
  60. Operation andOp = new Operation(Instruction.BitwiseAnd, baseAddrTrunc, baseAddrLow, alignMask);
  61. node.List.AddBefore(node, andOp);
  62. Operand byteOffset = Local();
  63. Operand wordOffset = Local();
  64. Operation subOp = new Operation(Instruction.Subtract, byteOffset, addrLow, baseAddrTrunc);
  65. Operation shrOp = new Operation(Instruction.ShiftRightU32, wordOffset, byteOffset, Const(2));
  66. node.List.AddBefore(node, subOp);
  67. node.List.AddBefore(node, shrOp);
  68. return wordOffset;
  69. }
  70. Operand[] sources = new Operand[operation.SourcesCount];
  71. sources[0] = Const(storageIndex);
  72. sources[1] = GetStorageOffset();
  73. for (int index = 2; index < operation.SourcesCount; index++)
  74. {
  75. sources[index] = operation.GetSource(index);
  76. }
  77. Operation storageOp;
  78. if (isAtomic)
  79. {
  80. Instruction inst = (operation.Inst & ~Instruction.MrMask) | Instruction.MrStorage;
  81. storageOp = new Operation(inst, operation.Dest, sources);
  82. }
  83. else if (operation.Inst == Instruction.LoadGlobal)
  84. {
  85. storageOp = new Operation(Instruction.LoadStorage, operation.Dest, sources);
  86. }
  87. else
  88. {
  89. storageOp = new Operation(Instruction.StoreStorage, null, sources);
  90. }
  91. for (int index = 0; index < operation.SourcesCount; index++)
  92. {
  93. operation.SetSource(index, null);
  94. }
  95. LinkedListNode<INode> oldNode = node;
  96. node = node.List.AddBefore(node, storageOp);
  97. node.List.Remove(oldNode);
  98. return node;
  99. }
  100. private static LinkedListNode<INode> ReplaceLdgWithLdc(LinkedListNode<INode> node, ShaderConfig config, int storageIndex)
  101. {
  102. Operation operation = (Operation)node.Value;
  103. Operand GetCbufOffset()
  104. {
  105. Operand addrLow = operation.GetSource(0);
  106. Operand baseAddrLow = config.CreateCbuf(0, UbeBaseOffset + storageIndex * StorageDescSize);
  107. Operand baseAddrTrunc = Local();
  108. Operand alignMask = Const(-config.GpuAccessor.QueryStorageBufferOffsetAlignment());
  109. Operation andOp = new Operation(Instruction.BitwiseAnd, baseAddrTrunc, baseAddrLow, alignMask);
  110. node.List.AddBefore(node, andOp);
  111. Operand byteOffset = Local();
  112. Operand wordOffset = Local();
  113. Operation subOp = new Operation(Instruction.Subtract, byteOffset, addrLow, baseAddrTrunc);
  114. Operation shrOp = new Operation(Instruction.ShiftRightU32, wordOffset, byteOffset, Const(2));
  115. node.List.AddBefore(node, subOp);
  116. node.List.AddBefore(node, shrOp);
  117. return wordOffset;
  118. }
  119. Operand[] sources = new Operand[operation.SourcesCount];
  120. int cbSlot = UbeFirstCbuf + storageIndex;
  121. sources[0] = Const(cbSlot);
  122. sources[1] = GetCbufOffset();
  123. config.SetUsedConstantBuffer(cbSlot);
  124. for (int index = 2; index < operation.SourcesCount; index++)
  125. {
  126. sources[index] = operation.GetSource(index);
  127. }
  128. Operation ldcOp = new Operation(Instruction.LoadConstant, operation.Dest, sources);
  129. for (int index = 0; index < operation.SourcesCount; index++)
  130. {
  131. operation.SetSource(index, null);
  132. }
  133. LinkedListNode<INode> oldNode = node;
  134. node = node.List.AddBefore(node, ldcOp);
  135. node.List.Remove(oldNode);
  136. return node;
  137. }
  138. private static int SearchForStorageBase(BasicBlock block, Operand globalAddress, int sbStart, int sbEnd)
  139. {
  140. globalAddress = Utils.FindLastOperation(globalAddress, block);
  141. if (globalAddress.Type == OperandType.ConstantBuffer)
  142. {
  143. return GetStorageIndex(globalAddress, sbStart, sbEnd);
  144. }
  145. Operation operation = globalAddress.AsgOp as Operation;
  146. if (operation == null || operation.Inst != Instruction.Add)
  147. {
  148. return -1;
  149. }
  150. Operand src1 = operation.GetSource(0);
  151. Operand src2 = operation.GetSource(1);
  152. if ((src1.Type == OperandType.LocalVariable && src2.Type == OperandType.Constant) ||
  153. (src2.Type == OperandType.LocalVariable && src1.Type == OperandType.Constant))
  154. {
  155. if (src1.Type == OperandType.LocalVariable)
  156. {
  157. operation = Utils.FindLastOperation(src1, block).AsgOp as Operation;
  158. }
  159. else
  160. {
  161. operation = Utils.FindLastOperation(src2, block).AsgOp as Operation;
  162. }
  163. if (operation == null || operation.Inst != Instruction.Add)
  164. {
  165. return -1;
  166. }
  167. }
  168. for (int index = 0; index < operation.SourcesCount; index++)
  169. {
  170. Operand source = operation.GetSource(index);
  171. int storageIndex = GetStorageIndex(source, sbStart, sbEnd);
  172. if (storageIndex != -1)
  173. {
  174. return storageIndex;
  175. }
  176. }
  177. return -1;
  178. }
  179. private static int GetStorageIndex(Operand operand, int sbStart, int sbEnd)
  180. {
  181. if (operand.Type == OperandType.ConstantBuffer)
  182. {
  183. int slot = operand.GetCbufSlot();
  184. int offset = operand.GetCbufOffset();
  185. if (slot == 0 && offset >= sbStart && offset < sbEnd)
  186. {
  187. int storageIndex = (offset - sbStart) / StorageDescSize;
  188. return storageIndex;
  189. }
  190. }
  191. return -1;
  192. }
  193. }
  194. }