Lowering.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  5. using static Ryujinx.Graphics.Shader.Translation.GlobalMemory;
  6. namespace Ryujinx.Graphics.Shader.Translation
  7. {
  8. static class Lowering
  9. {
  10. public static void RunPass(BasicBlock[] blocks, ShaderConfig config)
  11. {
  12. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  13. {
  14. BasicBlock block = blocks[blkIndex];
  15. for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
  16. {
  17. if (!(node.Value is Operation operation))
  18. {
  19. continue;
  20. }
  21. if (UsesGlobalMemory(operation.Inst))
  22. {
  23. node = RewriteGlobalAccess(node, config);
  24. }
  25. if (!config.Capabilities.SupportsNonConstantTextureOffset && operation.Inst == Instruction.TextureSample)
  26. {
  27. node = RewriteTextureSample(node);
  28. }
  29. }
  30. }
  31. }
  32. private static LinkedListNode<INode> RewriteGlobalAccess(LinkedListNode<INode> node, ShaderConfig config)
  33. {
  34. Operation operation = (Operation)node.Value;
  35. Operation storageOp;
  36. Operand PrependOperation(Instruction inst, params Operand[] sources)
  37. {
  38. Operand local = Local();
  39. node.List.AddBefore(node, new Operation(inst, local, sources));
  40. return local;
  41. }
  42. Operand addrLow = operation.GetSource(0);
  43. Operand addrHigh = operation.GetSource(1);
  44. Operand sbBaseAddrLow = Const(0);
  45. Operand sbSlot = Const(0);
  46. for (int slot = 0; slot < StorageMaxCount; slot++)
  47. {
  48. int cbOffset = GetStorageCbOffset(config.Stage, slot);
  49. Operand baseAddrLow = Cbuf(0, cbOffset);
  50. Operand baseAddrHigh = Cbuf(0, cbOffset + 1);
  51. Operand size = Cbuf(0, cbOffset + 2);
  52. Operand offset = PrependOperation(Instruction.Subtract, addrLow, baseAddrLow);
  53. Operand borrow = PrependOperation(Instruction.CompareLessU32, addrLow, baseAddrLow);
  54. Operand inRangeLow = PrependOperation(Instruction.CompareLessU32, offset, size);
  55. Operand addrHighBorrowed = PrependOperation(Instruction.Add, addrHigh, borrow);
  56. Operand inRangeHigh = PrependOperation(Instruction.CompareEqual, addrHighBorrowed, baseAddrHigh);
  57. Operand inRange = PrependOperation(Instruction.BitwiseAnd, inRangeLow, inRangeHigh);
  58. sbBaseAddrLow = PrependOperation(Instruction.ConditionalSelect, inRange, baseAddrLow, sbBaseAddrLow);
  59. sbSlot = PrependOperation(Instruction.ConditionalSelect, inRange, Const(slot), sbSlot);
  60. }
  61. Operand alignMask = Const(-config.Capabilities.StorageBufferOffsetAlignment);
  62. Operand baseAddrTrunc = PrependOperation(Instruction.BitwiseAnd, sbBaseAddrLow, Const(-64));
  63. Operand byteOffset = PrependOperation(Instruction.Subtract, addrLow, baseAddrTrunc);
  64. Operand wordOffset = PrependOperation(Instruction.ShiftRightU32, byteOffset, Const(2));
  65. Operand[] sources = new Operand[operation.SourcesCount];
  66. sources[0] = sbSlot;
  67. sources[1] = wordOffset;
  68. for (int index = 2; index < operation.SourcesCount; index++)
  69. {
  70. sources[index] = operation.GetSource(index);
  71. }
  72. if (operation.Inst.IsAtomic())
  73. {
  74. Instruction inst = (operation.Inst & ~Instruction.MrMask) | Instruction.MrStorage;
  75. storageOp = new Operation(inst, operation.Dest, sources);
  76. }
  77. else if (operation.Inst == Instruction.LoadGlobal)
  78. {
  79. storageOp = new Operation(Instruction.LoadStorage, operation.Dest, sources);
  80. }
  81. else
  82. {
  83. storageOp = new Operation(Instruction.StoreStorage, null, sources);
  84. }
  85. for (int index = 0; index < operation.SourcesCount; index++)
  86. {
  87. operation.SetSource(index, null);
  88. }
  89. LinkedListNode<INode> oldNode = node;
  90. node = node.List.AddBefore(node, storageOp);
  91. node.List.Remove(oldNode);
  92. return node;
  93. }
  94. private static LinkedListNode<INode> RewriteTextureSample(LinkedListNode<INode> node)
  95. {
  96. TextureOperation texOp = (TextureOperation)node.Value;
  97. bool hasOffset = (texOp.Flags & TextureFlags.Offset) != 0;
  98. bool hasOffsets = (texOp.Flags & TextureFlags.Offsets) != 0;
  99. if (!(hasOffset || hasOffsets))
  100. {
  101. return node;
  102. }
  103. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  104. bool isGather = (texOp.Flags & TextureFlags.Gather) != 0;
  105. bool hasDerivatives = (texOp.Flags & TextureFlags.Derivatives) != 0;
  106. bool hasLodBias = (texOp.Flags & TextureFlags.LodBias) != 0;
  107. bool hasLodLevel = (texOp.Flags & TextureFlags.LodLevel) != 0;
  108. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  109. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  110. bool isMultisample = (texOp.Type & SamplerType.Multisample) != 0;
  111. bool isShadow = (texOp.Type & SamplerType.Shadow) != 0;
  112. int coordsCount = texOp.Type.GetDimensions();
  113. int offsetsCount = coordsCount * (hasOffsets ? 4 : 1);
  114. Operand[] offsets = new Operand[offsetsCount];
  115. Operand[] sources = new Operand[texOp.SourcesCount - offsetsCount];
  116. int srcIndex = 0;
  117. int dstIndex = 0;
  118. int copyCount = 0;
  119. if (isBindless || isIndexed)
  120. {
  121. copyCount++;
  122. }
  123. Operand[] lodSources = new Operand[copyCount + coordsCount];
  124. for (int index = 0; index < lodSources.Length; index++)
  125. {
  126. lodSources[index] = texOp.GetSource(index);
  127. }
  128. copyCount += coordsCount;
  129. if (isArray)
  130. {
  131. copyCount++;
  132. }
  133. if (isShadow)
  134. {
  135. copyCount++;
  136. }
  137. if (hasDerivatives)
  138. {
  139. copyCount += coordsCount * 2;
  140. }
  141. if (isMultisample)
  142. {
  143. copyCount++;
  144. }
  145. else if (hasLodLevel)
  146. {
  147. copyCount++;
  148. }
  149. for (int index = 0; index < copyCount; index++)
  150. {
  151. sources[dstIndex++] = texOp.GetSource(srcIndex++);
  152. }
  153. bool areAllOffsetsConstant = true;
  154. for (int index = 0; index < offsetsCount; index++)
  155. {
  156. Operand offset = texOp.GetSource(srcIndex++);
  157. areAllOffsetsConstant &= offset.Type == OperandType.Constant;
  158. offsets[index] = offset;
  159. }
  160. if (areAllOffsetsConstant)
  161. {
  162. return node;
  163. }
  164. if (hasLodBias)
  165. {
  166. sources[dstIndex++] = texOp.GetSource(srcIndex++);
  167. }
  168. if (isGather && !isShadow)
  169. {
  170. sources[dstIndex++] = texOp.GetSource(srcIndex++);
  171. }
  172. Operand Int(Operand value)
  173. {
  174. Operand res = Local();
  175. node.List.AddBefore(node, new Operation(Instruction.ConvertFPToS32, res, value));
  176. return res;
  177. }
  178. Operand Float(Operand value)
  179. {
  180. Operand res = Local();
  181. node.List.AddBefore(node, new Operation(Instruction.ConvertS32ToFP, res, value));
  182. return res;
  183. }
  184. Operand lod = Local();
  185. node.List.AddBefore(node, new TextureOperation(
  186. Instruction.Lod,
  187. texOp.Type,
  188. texOp.Flags,
  189. texOp.Handle,
  190. 1,
  191. lod,
  192. lodSources));
  193. int coordsIndex = isBindless || isIndexed ? 1 : 0;
  194. for (int index = 0; index < coordsCount; index++)
  195. {
  196. Operand coordSize = Local();
  197. Operand[] texSizeSources;
  198. if (isBindless || isIndexed)
  199. {
  200. texSizeSources = new Operand[] { sources[0], Int(lod) };
  201. }
  202. else
  203. {
  204. texSizeSources = new Operand[] { Int(lod) };
  205. }
  206. node.List.AddBefore(node, new TextureOperation(
  207. Instruction.TextureSize,
  208. texOp.Type,
  209. texOp.Flags,
  210. texOp.Handle,
  211. index,
  212. coordSize,
  213. texSizeSources));
  214. Operand offset = Local();
  215. Operand intOffset = offsets[index + (hasOffsets ? texOp.Index * coordsCount : 0)];
  216. node.List.AddBefore(node, new Operation(Instruction.FP | Instruction.Divide, offset, Float(intOffset), Float(coordSize)));
  217. Operand source = sources[coordsIndex + index];
  218. Operand coordPlusOffset = Local();
  219. node.List.AddBefore(node, new Operation(Instruction.FP | Instruction.Add, coordPlusOffset, source, offset));
  220. sources[coordsIndex + index] = coordPlusOffset;
  221. }
  222. int componentIndex;
  223. if (isGather && !isShadow)
  224. {
  225. Operand gatherComponent = sources[dstIndex - 1];
  226. Debug.Assert(gatherComponent.Type == OperandType.Constant);
  227. componentIndex = gatherComponent.Value;
  228. }
  229. else
  230. {
  231. componentIndex = texOp.Index;
  232. }
  233. TextureOperation newTexOp = new TextureOperation(
  234. Instruction.TextureSample,
  235. texOp.Type,
  236. texOp.Flags & ~(TextureFlags.Offset | TextureFlags.Offsets),
  237. texOp.Handle,
  238. componentIndex,
  239. texOp.Dest,
  240. sources);
  241. for (int index = 0; index < texOp.SourcesCount; index++)
  242. {
  243. texOp.SetSource(index, null);
  244. }
  245. LinkedListNode<INode> oldNode = node;
  246. node = node.List.AddBefore(node, newTexOp);
  247. node.List.Remove(oldNode);
  248. return node;
  249. }
  250. }
  251. }