Lowering.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  6. using static Ryujinx.Graphics.Shader.Translation.GlobalMemory;
  7. namespace Ryujinx.Graphics.Shader.Translation
  8. {
  9. static class Lowering
  10. {
  11. public static void RunPass(BasicBlock[] blocks, ShaderConfig config)
  12. {
  13. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  14. {
  15. BasicBlock block = blocks[blkIndex];
  16. for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
  17. {
  18. if (!(node.Value is Operation operation))
  19. {
  20. continue;
  21. }
  22. if (UsesGlobalMemory(operation.Inst))
  23. {
  24. node = RewriteGlobalAccess(node, config);
  25. }
  26. if (operation is TextureOperation texOp)
  27. {
  28. if (texOp.Inst == Instruction.TextureSample)
  29. {
  30. node = RewriteTextureSample(node, config);
  31. }
  32. if (texOp.Type == SamplerType.TextureBuffer)
  33. {
  34. node = InsertSnormNormalization(node, config);
  35. }
  36. }
  37. }
  38. }
  39. }
  40. private static LinkedListNode<INode> RewriteGlobalAccess(LinkedListNode<INode> node, ShaderConfig config)
  41. {
  42. Operation operation = (Operation)node.Value;
  43. Operation storageOp;
  44. Operand PrependOperation(Instruction inst, params Operand[] sources)
  45. {
  46. Operand local = Local();
  47. node.List.AddBefore(node, new Operation(inst, local, sources));
  48. return local;
  49. }
  50. Operand addrLow = operation.GetSource(0);
  51. Operand addrHigh = operation.GetSource(1);
  52. Operand sbBaseAddrLow = Const(0);
  53. Operand sbSlot = Const(0);
  54. for (int slot = 0; slot < StorageMaxCount; slot++)
  55. {
  56. int cbOffset = GetStorageCbOffset(config.Stage, slot);
  57. Operand baseAddrLow = Cbuf(0, cbOffset);
  58. Operand baseAddrHigh = Cbuf(0, cbOffset + 1);
  59. Operand size = Cbuf(0, cbOffset + 2);
  60. Operand offset = PrependOperation(Instruction.Subtract, addrLow, baseAddrLow);
  61. Operand borrow = PrependOperation(Instruction.CompareLessU32, addrLow, baseAddrLow);
  62. Operand inRangeLow = PrependOperation(Instruction.CompareLessU32, offset, size);
  63. Operand addrHighBorrowed = PrependOperation(Instruction.Add, addrHigh, borrow);
  64. Operand inRangeHigh = PrependOperation(Instruction.CompareEqual, addrHighBorrowed, baseAddrHigh);
  65. Operand inRange = PrependOperation(Instruction.BitwiseAnd, inRangeLow, inRangeHigh);
  66. sbBaseAddrLow = PrependOperation(Instruction.ConditionalSelect, inRange, baseAddrLow, sbBaseAddrLow);
  67. sbSlot = PrependOperation(Instruction.ConditionalSelect, inRange, Const(slot), sbSlot);
  68. }
  69. Operand alignMask = Const(-config.GpuAccessor.QueryStorageBufferOffsetAlignment());
  70. Operand baseAddrTrunc = PrependOperation(Instruction.BitwiseAnd, sbBaseAddrLow, alignMask);
  71. Operand byteOffset = PrependOperation(Instruction.Subtract, addrLow, baseAddrTrunc);
  72. Operand wordOffset = PrependOperation(Instruction.ShiftRightU32, byteOffset, Const(2));
  73. Operand[] sources = new Operand[operation.SourcesCount];
  74. sources[0] = sbSlot;
  75. sources[1] = wordOffset;
  76. for (int index = 2; index < operation.SourcesCount; index++)
  77. {
  78. sources[index] = operation.GetSource(index);
  79. }
  80. if (operation.Inst.IsAtomic())
  81. {
  82. Instruction inst = (operation.Inst & ~Instruction.MrMask) | Instruction.MrStorage;
  83. storageOp = new Operation(inst, operation.Dest, sources);
  84. }
  85. else if (operation.Inst == Instruction.LoadGlobal)
  86. {
  87. storageOp = new Operation(Instruction.LoadStorage, operation.Dest, sources);
  88. }
  89. else
  90. {
  91. storageOp = new Operation(Instruction.StoreStorage, null, sources);
  92. }
  93. for (int index = 0; index < operation.SourcesCount; index++)
  94. {
  95. operation.SetSource(index, null);
  96. }
  97. LinkedListNode<INode> oldNode = node;
  98. node = node.List.AddBefore(node, storageOp);
  99. node.List.Remove(oldNode);
  100. return node;
  101. }
  102. private static LinkedListNode<INode> RewriteTextureSample(LinkedListNode<INode> node, ShaderConfig config)
  103. {
  104. TextureOperation texOp = (TextureOperation)node.Value;
  105. bool hasOffset = (texOp.Flags & TextureFlags.Offset) != 0;
  106. bool hasOffsets = (texOp.Flags & TextureFlags.Offsets) != 0;
  107. bool hasInvalidOffset = (hasOffset || hasOffsets) && !config.GpuAccessor.QuerySupportsNonConstantTextureOffset();
  108. bool isRect = config.GpuAccessor.QueryIsTextureRectangle(texOp.Handle);
  109. if (!(hasInvalidOffset || isRect))
  110. {
  111. return node;
  112. }
  113. bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
  114. bool isGather = (texOp.Flags & TextureFlags.Gather) != 0;
  115. bool hasDerivatives = (texOp.Flags & TextureFlags.Derivatives) != 0;
  116. bool intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
  117. bool hasLodBias = (texOp.Flags & TextureFlags.LodBias) != 0;
  118. bool hasLodLevel = (texOp.Flags & TextureFlags.LodLevel) != 0;
  119. bool isArray = (texOp.Type & SamplerType.Array) != 0;
  120. bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
  121. bool isMultisample = (texOp.Type & SamplerType.Multisample) != 0;
  122. bool isShadow = (texOp.Type & SamplerType.Shadow) != 0;
  123. int coordsCount = texOp.Type.GetDimensions();
  124. int offsetsCount;
  125. if (hasOffsets)
  126. {
  127. offsetsCount = coordsCount * 4;
  128. }
  129. else if (hasOffset)
  130. {
  131. offsetsCount = coordsCount;
  132. }
  133. else
  134. {
  135. offsetsCount = 0;
  136. }
  137. Operand[] offsets = new Operand[offsetsCount];
  138. Operand[] sources = new Operand[texOp.SourcesCount - offsetsCount];
  139. int copyCount = 0;
  140. if (isBindless || isIndexed)
  141. {
  142. copyCount++;
  143. }
  144. Operand[] lodSources = new Operand[copyCount + coordsCount];
  145. for (int index = 0; index < lodSources.Length; index++)
  146. {
  147. lodSources[index] = texOp.GetSource(index);
  148. }
  149. copyCount += coordsCount;
  150. if (isArray)
  151. {
  152. copyCount++;
  153. }
  154. if (isShadow)
  155. {
  156. copyCount++;
  157. }
  158. if (hasDerivatives)
  159. {
  160. copyCount += coordsCount * 2;
  161. }
  162. if (isMultisample)
  163. {
  164. copyCount++;
  165. }
  166. else if (hasLodLevel)
  167. {
  168. copyCount++;
  169. }
  170. int srcIndex = 0;
  171. int dstIndex = 0;
  172. for (int index = 0; index < copyCount; index++)
  173. {
  174. sources[dstIndex++] = texOp.GetSource(srcIndex++);
  175. }
  176. bool areAllOffsetsConstant = true;
  177. for (int index = 0; index < offsetsCount; index++)
  178. {
  179. Operand offset = texOp.GetSource(srcIndex++);
  180. areAllOffsetsConstant &= offset.Type == OperandType.Constant;
  181. offsets[index] = offset;
  182. }
  183. hasInvalidOffset &= !areAllOffsetsConstant;
  184. if (!(hasInvalidOffset || isRect))
  185. {
  186. return node;
  187. }
  188. if (hasLodBias)
  189. {
  190. sources[dstIndex++] = texOp.GetSource(srcIndex++);
  191. }
  192. if (isGather && !isShadow)
  193. {
  194. sources[dstIndex++] = texOp.GetSource(srcIndex++);
  195. }
  196. int coordsIndex = isBindless || isIndexed ? 1 : 0;
  197. int componentIndex = texOp.Index;
  198. Operand Int(Operand value)
  199. {
  200. Operand res = Local();
  201. node.List.AddBefore(node, new Operation(Instruction.ConvertFPToS32, res, value));
  202. return res;
  203. }
  204. Operand Float(Operand value)
  205. {
  206. Operand res = Local();
  207. node.List.AddBefore(node, new Operation(Instruction.ConvertS32ToFP, res, value));
  208. return res;
  209. }
  210. // Emulate texture rectangle by normalizing the coordinates on the shader.
  211. // When sampler*Rect is used, the coords are expected to the in the [0, W or H] range,
  212. // and otherwise, it is expected to be in the [0, 1] range.
  213. // We normalize by dividing the coords by the texture size.
  214. if (isRect && !intCoords)
  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], Const(0) };
  223. }
  224. else
  225. {
  226. texSizeSources = new Operand[] { Const(0) };
  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 source = sources[coordsIndex + index];
  237. Operand coordNormalized = Local();
  238. node.List.AddBefore(node, new Operation(Instruction.FP32 | Instruction.Divide, coordNormalized, source, Float(coordSize)));
  239. sources[coordsIndex + index] = coordNormalized;
  240. }
  241. }
  242. // Technically, non-constant texture offsets are not allowed (according to the spec),
  243. // however some GPUs does support that.
  244. // For GPUs where it is not supported, we can replace the instruction with the following:
  245. // For texture*Offset, we replace it by texture*, and add the offset to the P coords.
  246. // The offset can be calculated as offset / textureSize(lod), where lod = textureQueryLod(coords).
  247. // For texelFetchOffset, we replace it by texelFetch and add the offset to the P coords directly.
  248. // For textureGatherOffset, we take advantage of the fact that the operation is already broken down
  249. // to read the 4 pixels separately, and just replace it with 4 textureGather with a different offset
  250. // for each pixel.
  251. if (hasInvalidOffset)
  252. {
  253. if (intCoords)
  254. {
  255. for (int index = 0; index < coordsCount; index++)
  256. {
  257. Operand source = sources[coordsIndex + index];
  258. Operand coordPlusOffset = Local();
  259. node.List.AddBefore(node, new Operation(Instruction.Add, coordPlusOffset, source, offsets[index]));
  260. sources[coordsIndex + index] = coordPlusOffset;
  261. }
  262. }
  263. else
  264. {
  265. Operand lod = Local();
  266. node.List.AddBefore(node, new TextureOperation(
  267. Instruction.Lod,
  268. texOp.Type,
  269. texOp.Flags,
  270. texOp.Handle,
  271. 1,
  272. lod,
  273. lodSources));
  274. for (int index = 0; index < coordsCount; index++)
  275. {
  276. Operand coordSize = Local();
  277. Operand[] texSizeSources;
  278. if (isBindless || isIndexed)
  279. {
  280. texSizeSources = new Operand[] { sources[0], Int(lod) };
  281. }
  282. else
  283. {
  284. texSizeSources = new Operand[] { Int(lod) };
  285. }
  286. node.List.AddBefore(node, new TextureOperation(
  287. Instruction.TextureSize,
  288. texOp.Type,
  289. texOp.Flags,
  290. texOp.Handle,
  291. index,
  292. coordSize,
  293. texSizeSources));
  294. Operand offset = Local();
  295. Operand intOffset = offsets[index + (hasOffsets ? texOp.Index * coordsCount : 0)];
  296. node.List.AddBefore(node, new Operation(Instruction.FP32 | Instruction.Divide, offset, Float(intOffset), Float(coordSize)));
  297. Operand source = sources[coordsIndex + index];
  298. Operand coordPlusOffset = Local();
  299. node.List.AddBefore(node, new Operation(Instruction.FP32 | Instruction.Add, coordPlusOffset, source, offset));
  300. sources[coordsIndex + index] = coordPlusOffset;
  301. }
  302. }
  303. if (isGather && !isShadow)
  304. {
  305. Operand gatherComponent = sources[dstIndex - 1];
  306. Debug.Assert(gatherComponent.Type == OperandType.Constant);
  307. componentIndex = gatherComponent.Value;
  308. }
  309. }
  310. TextureOperation newTexOp = new TextureOperation(
  311. Instruction.TextureSample,
  312. texOp.Type,
  313. texOp.Flags & ~(TextureFlags.Offset | TextureFlags.Offsets),
  314. texOp.Handle,
  315. componentIndex,
  316. texOp.Dest,
  317. sources);
  318. for (int index = 0; index < texOp.SourcesCount; index++)
  319. {
  320. texOp.SetSource(index, null);
  321. }
  322. LinkedListNode<INode> oldNode = node;
  323. node = node.List.AddBefore(node, newTexOp);
  324. node.List.Remove(oldNode);
  325. return node;
  326. }
  327. private static LinkedListNode<INode> InsertSnormNormalization(LinkedListNode<INode> node, ShaderConfig config)
  328. {
  329. TextureOperation texOp = (TextureOperation)node.Value;
  330. TextureFormat format = config.GpuAccessor.QueryTextureFormat(texOp.Handle);
  331. int maxPositive = format switch
  332. {
  333. TextureFormat.R8Snorm => sbyte.MaxValue,
  334. TextureFormat.R8G8Snorm => sbyte.MaxValue,
  335. TextureFormat.R8G8B8A8Snorm => sbyte.MaxValue,
  336. TextureFormat.R16Snorm => short.MaxValue,
  337. TextureFormat.R16G16Snorm => short.MaxValue,
  338. TextureFormat.R16G16B16A16Snorm => short.MaxValue,
  339. _ => 0
  340. };
  341. // The value being 0 means that the format is not a SNORM format, so there's nothing to do here.
  342. if (maxPositive == 0)
  343. {
  344. return node;
  345. }
  346. // Do normalization. We assume SINT formats are being used as replacement for SNORM (that is not supported).
  347. INode[] uses = texOp.Dest.UseOps.ToArray();
  348. Operation convOp = new Operation(Instruction.ConvertS32ToFP, Local(), texOp.Dest);
  349. Operation normOp = new Operation(Instruction.FP32 | Instruction.Multiply, Local(), convOp.Dest, ConstF(1f / maxPositive));
  350. node = node.List.AddAfter(node, convOp);
  351. node = node.List.AddAfter(node, normOp);
  352. foreach (INode useOp in uses)
  353. {
  354. if (!(useOp is Operation op))
  355. {
  356. continue;
  357. }
  358. // Replace all uses of the texture pixel value with the normalized value.
  359. for (int index = 0; index < op.SourcesCount; index++)
  360. {
  361. if (op.GetSource(index) == texOp.Dest)
  362. {
  363. op.SetSource(index, normOp.Dest);
  364. }
  365. }
  366. }
  367. return node;
  368. }
  369. }
  370. }