Lowering.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. // Technically, non-constant texture offsets are not allowed (according to the spec),
  97. // however some GPUs does support that.
  98. // For GPUs where it is not supported, we can replace the instruction with the following:
  99. // For texture*Offset, we replace it by texture*, and add the offset to the P coords.
  100. // The offset can be calculated as offset / textureSize(lod), where lod = textureQueryLod(coords).
  101. // For texelFetchOffset, we replace it by texelFetch and add the offset to the P coords directly.
  102. // For textureGatherOffset, we take advantage of the fact that the operation is already broken down
  103. // to read the 4 pixels separately, and just replace it with 4 textureGather with a different offset
  104. // for each pixel.
  105. TextureOperation texOp = (TextureOperation)node.Value;
  106. bool hasOffset = (texOp.Flags & TextureFlags.Offset) != 0;
  107. bool hasOffsets = (texOp.Flags & TextureFlags.Offsets) != 0;
  108. if (!(hasOffset || hasOffsets))
  109. {
  110. return node;
  111. }
  112. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  113. bool isGather = (texOp.Flags & TextureFlags.Gather) != 0;
  114. bool hasDerivatives = (texOp.Flags & TextureFlags.Derivatives) != 0;
  115. bool intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
  116. bool hasLodBias = (texOp.Flags & TextureFlags.LodBias) != 0;
  117. bool hasLodLevel = (texOp.Flags & TextureFlags.LodLevel) != 0;
  118. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  119. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  120. bool isMultisample = (texOp.Type & SamplerType.Multisample) != 0;
  121. bool isShadow = (texOp.Type & SamplerType.Shadow) != 0;
  122. int coordsCount = texOp.Type.GetDimensions();
  123. int offsetsCount = coordsCount * (hasOffsets ? 4 : 1);
  124. Operand[] offsets = new Operand[offsetsCount];
  125. Operand[] sources = new Operand[texOp.SourcesCount - offsetsCount];
  126. int srcIndex = 0;
  127. int dstIndex = 0;
  128. int copyCount = 0;
  129. if (isBindless || isIndexed)
  130. {
  131. copyCount++;
  132. }
  133. Operand[] lodSources = new Operand[copyCount + coordsCount];
  134. for (int index = 0; index < lodSources.Length; index++)
  135. {
  136. lodSources[index] = texOp.GetSource(index);
  137. }
  138. copyCount += coordsCount;
  139. if (isArray)
  140. {
  141. copyCount++;
  142. }
  143. if (isShadow)
  144. {
  145. copyCount++;
  146. }
  147. if (hasDerivatives)
  148. {
  149. copyCount += coordsCount * 2;
  150. }
  151. if (isMultisample)
  152. {
  153. copyCount++;
  154. }
  155. else if (hasLodLevel)
  156. {
  157. copyCount++;
  158. }
  159. for (int index = 0; index < copyCount; index++)
  160. {
  161. sources[dstIndex++] = texOp.GetSource(srcIndex++);
  162. }
  163. bool areAllOffsetsConstant = true;
  164. for (int index = 0; index < offsetsCount; index++)
  165. {
  166. Operand offset = texOp.GetSource(srcIndex++);
  167. areAllOffsetsConstant &= offset.Type == OperandType.Constant;
  168. offsets[index] = offset;
  169. }
  170. if (areAllOffsetsConstant)
  171. {
  172. return node;
  173. }
  174. if (hasLodBias)
  175. {
  176. sources[dstIndex++] = texOp.GetSource(srcIndex++);
  177. }
  178. if (isGather && !isShadow)
  179. {
  180. sources[dstIndex++] = texOp.GetSource(srcIndex++);
  181. }
  182. int coordsIndex = isBindless || isIndexed ? 1 : 0;
  183. if (intCoords)
  184. {
  185. for (int index = 0; index < coordsCount; index++)
  186. {
  187. Operand source = sources[coordsIndex + index];
  188. Operand coordPlusOffset = Local();
  189. node.List.AddBefore(node, new Operation(Instruction.Add, coordPlusOffset, source, offsets[index]));
  190. sources[coordsIndex + index] = coordPlusOffset;
  191. }
  192. }
  193. else
  194. {
  195. Operand lod = Local();
  196. node.List.AddBefore(node, new TextureOperation(
  197. Instruction.Lod,
  198. texOp.Type,
  199. texOp.Flags,
  200. texOp.Handle,
  201. 1,
  202. lod,
  203. lodSources));
  204. Operand Int(Operand value)
  205. {
  206. Operand res = Local();
  207. node.List.AddBefore(node, new Operation(Instruction.ConvertFPToS32, res, value));
  208. return res;
  209. }
  210. Operand Float(Operand value)
  211. {
  212. Operand res = Local();
  213. node.List.AddBefore(node, new Operation(Instruction.ConvertS32ToFP, res, value));
  214. return res;
  215. }
  216. for (int index = 0; index < coordsCount; index++)
  217. {
  218. Operand coordSize = Local();
  219. Operand[] texSizeSources;
  220. if (isBindless || isIndexed)
  221. {
  222. texSizeSources = new Operand[] { sources[0], Int(lod) };
  223. }
  224. else
  225. {
  226. texSizeSources = new Operand[] { Int(lod) };
  227. }
  228. node.List.AddBefore(node, new TextureOperation(
  229. Instruction.TextureSize,
  230. texOp.Type,
  231. texOp.Flags,
  232. texOp.Handle,
  233. index,
  234. coordSize,
  235. texSizeSources));
  236. Operand offset = Local();
  237. Operand intOffset = offsets[index + (hasOffsets ? texOp.Index * coordsCount : 0)];
  238. node.List.AddBefore(node, new Operation(Instruction.FP | Instruction.Divide, offset, Float(intOffset), Float(coordSize)));
  239. Operand source = sources[coordsIndex + index];
  240. Operand coordPlusOffset = Local();
  241. node.List.AddBefore(node, new Operation(Instruction.FP | Instruction.Add, coordPlusOffset, source, offset));
  242. sources[coordsIndex + index] = coordPlusOffset;
  243. }
  244. }
  245. int componentIndex;
  246. if (isGather && !isShadow)
  247. {
  248. Operand gatherComponent = sources[dstIndex - 1];
  249. Debug.Assert(gatherComponent.Type == OperandType.Constant);
  250. componentIndex = gatherComponent.Value;
  251. }
  252. else
  253. {
  254. componentIndex = texOp.Index;
  255. }
  256. TextureOperation newTexOp = new TextureOperation(
  257. Instruction.TextureSample,
  258. texOp.Type,
  259. texOp.Flags & ~(TextureFlags.Offset | TextureFlags.Offsets),
  260. texOp.Handle,
  261. componentIndex,
  262. texOp.Dest,
  263. sources);
  264. for (int index = 0; index < texOp.SourcesCount; index++)
  265. {
  266. texOp.SetSource(index, null);
  267. }
  268. LinkedListNode<INode> oldNode = node;
  269. node = node.List.AddBefore(node, newTexOp);
  270. node.List.Remove(oldNode);
  271. return node;
  272. }
  273. }
  274. }