Lowering.cs 15 KB

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