EmitterContext.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. using Ryujinx.Graphics.Shader.Decoders;
  2. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Numerics;
  6. using System.Runtime.CompilerServices;
  7. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  8. namespace Ryujinx.Graphics.Shader.Translation
  9. {
  10. class EmitterContext
  11. {
  12. public DecodedProgram Program { get; }
  13. public ShaderConfig Config { get; }
  14. public bool IsNonMain { get; }
  15. public Block CurrBlock { get; set; }
  16. public InstOp CurrOp { get; set; }
  17. public int OperationsCount => _operations.Count;
  18. private readonly struct BrxTarget
  19. {
  20. public readonly Operand Selector;
  21. public readonly int ExpectedValue;
  22. public readonly ulong NextTargetAddress;
  23. public BrxTarget(Operand selector, int expectedValue, ulong nextTargetAddress)
  24. {
  25. Selector = selector;
  26. ExpectedValue = expectedValue;
  27. NextTargetAddress = nextTargetAddress;
  28. }
  29. }
  30. private class BlockLabel
  31. {
  32. public readonly Operand Label;
  33. public BrxTarget BrxTarget;
  34. public BlockLabel(Operand label)
  35. {
  36. Label = label;
  37. }
  38. }
  39. private readonly List<Operation> _operations;
  40. private readonly Dictionary<ulong, BlockLabel> _labels;
  41. public EmitterContext(DecodedProgram program, ShaderConfig config, bool isNonMain)
  42. {
  43. Program = program;
  44. Config = config;
  45. IsNonMain = isNonMain;
  46. _operations = new List<Operation>();
  47. _labels = new Dictionary<ulong, BlockLabel>();
  48. EmitStart();
  49. }
  50. private void EmitStart()
  51. {
  52. if (Config.Stage == ShaderStage.Vertex &&
  53. Config.Options.TargetApi == TargetApi.Vulkan &&
  54. (Config.Options.Flags & TranslationFlags.VertexA) == 0)
  55. {
  56. // Vulkan requires the point size to be always written on the shader if the primitive topology is points.
  57. this.Store(StorageKind.Output, IoVariable.PointSize, null, ConstF(Config.GpuAccessor.QueryPointSize()));
  58. }
  59. }
  60. public T GetOp<T>() where T : unmanaged
  61. {
  62. Debug.Assert(Unsafe.SizeOf<T>() == sizeof(ulong));
  63. ulong op = CurrOp.RawOpCode;
  64. return Unsafe.As<ulong, T>(ref op);
  65. }
  66. public Operand Add(Instruction inst, Operand dest = null, params Operand[] sources)
  67. {
  68. Operation operation = new Operation(inst, dest, sources);
  69. _operations.Add(operation);
  70. return dest;
  71. }
  72. public Operand Add(Instruction inst, StorageKind storageKind, Operand dest = null, params Operand[] sources)
  73. {
  74. Operation operation = new Operation(inst, storageKind, dest, sources);
  75. _operations.Add(operation);
  76. return dest;
  77. }
  78. public (Operand, Operand) Add(Instruction inst, (Operand, Operand) dest, params Operand[] sources)
  79. {
  80. Operand[] dests = new[] { dest.Item1, dest.Item2 };
  81. Operation operation = new Operation(inst, 0, dests, sources);
  82. Add(operation);
  83. return dest;
  84. }
  85. public void Add(Operation operation)
  86. {
  87. _operations.Add(operation);
  88. }
  89. public TextureOperation CreateTextureOperation(
  90. Instruction inst,
  91. SamplerType type,
  92. TextureFlags flags,
  93. int handle,
  94. int compIndex,
  95. Operand[] dests,
  96. params Operand[] sources)
  97. {
  98. return CreateTextureOperation(inst, type, TextureFormat.Unknown, flags, handle, compIndex, dests, sources);
  99. }
  100. public TextureOperation CreateTextureOperation(
  101. Instruction inst,
  102. SamplerType type,
  103. TextureFormat format,
  104. TextureFlags flags,
  105. int handle,
  106. int compIndex,
  107. Operand[] dests,
  108. params Operand[] sources)
  109. {
  110. if (!flags.HasFlag(TextureFlags.Bindless))
  111. {
  112. Config.SetUsedTexture(inst, type, format, flags, TextureOperation.DefaultCbufSlot, handle);
  113. }
  114. return new TextureOperation(inst, type, format, flags, handle, compIndex, dests, sources);
  115. }
  116. public void FlagAttributeRead(int attribute)
  117. {
  118. if (Config.Stage == ShaderStage.Vertex && attribute == AttributeConsts.InstanceId)
  119. {
  120. Config.SetUsedFeature(FeatureFlags.InstanceId);
  121. }
  122. else if (Config.Stage == ShaderStage.Fragment)
  123. {
  124. switch (attribute)
  125. {
  126. case AttributeConsts.PositionX:
  127. case AttributeConsts.PositionY:
  128. Config.SetUsedFeature(FeatureFlags.FragCoordXY);
  129. break;
  130. }
  131. }
  132. }
  133. public void FlagAttributeWritten(int attribute)
  134. {
  135. if (Config.Stage == ShaderStage.Vertex)
  136. {
  137. switch (attribute)
  138. {
  139. case AttributeConsts.ClipDistance0:
  140. case AttributeConsts.ClipDistance1:
  141. case AttributeConsts.ClipDistance2:
  142. case AttributeConsts.ClipDistance3:
  143. case AttributeConsts.ClipDistance4:
  144. case AttributeConsts.ClipDistance5:
  145. case AttributeConsts.ClipDistance6:
  146. case AttributeConsts.ClipDistance7:
  147. Config.SetClipDistanceWritten((attribute - AttributeConsts.ClipDistance0) / 4);
  148. break;
  149. }
  150. }
  151. if (Config.Stage != ShaderStage.Fragment && attribute == AttributeConsts.Layer)
  152. {
  153. Config.SetUsedFeature(FeatureFlags.RtLayer);
  154. }
  155. }
  156. public void MarkLabel(Operand label)
  157. {
  158. Add(Instruction.MarkLabel, label);
  159. }
  160. public Operand GetLabel(ulong address)
  161. {
  162. return EnsureBlockLabel(address).Label;
  163. }
  164. public void SetBrxTarget(ulong address, Operand selector, int targetValue, ulong nextTargetAddress)
  165. {
  166. BlockLabel blockLabel = EnsureBlockLabel(address);
  167. Debug.Assert(blockLabel.BrxTarget.Selector == null);
  168. blockLabel.BrxTarget = new BrxTarget(selector, targetValue, nextTargetAddress);
  169. }
  170. public void EnterBlock(ulong address)
  171. {
  172. BlockLabel blockLabel = EnsureBlockLabel(address);
  173. MarkLabel(blockLabel.Label);
  174. BrxTarget brxTarget = blockLabel.BrxTarget;
  175. if (brxTarget.Selector != null)
  176. {
  177. this.BranchIfFalse(GetLabel(brxTarget.NextTargetAddress), this.ICompareEqual(brxTarget.Selector, Const(brxTarget.ExpectedValue)));
  178. }
  179. }
  180. private BlockLabel EnsureBlockLabel(ulong address)
  181. {
  182. if (!_labels.TryGetValue(address, out BlockLabel blockLabel))
  183. {
  184. blockLabel = new BlockLabel(Label());
  185. _labels.Add(address, blockLabel);
  186. }
  187. return blockLabel;
  188. }
  189. public void PrepareForVertexReturn()
  190. {
  191. if (Config.GpuAccessor.QueryViewportTransformDisable())
  192. {
  193. Operand x = this.Load(StorageKind.Output, IoVariable.Position, null, Const(0));
  194. Operand y = this.Load(StorageKind.Output, IoVariable.Position, null, Const(1));
  195. Operand xScale = this.Load(StorageKind.Input, IoVariable.SupportBlockViewInverse, null, Const(0));
  196. Operand yScale = this.Load(StorageKind.Input, IoVariable.SupportBlockViewInverse, null, Const(1));
  197. Operand negativeOne = ConstF(-1.0f);
  198. this.Store(StorageKind.Output, IoVariable.Position, null, Const(0), this.FPFusedMultiplyAdd(x, xScale, negativeOne));
  199. this.Store(StorageKind.Output, IoVariable.Position, null, Const(1), this.FPFusedMultiplyAdd(y, yScale, negativeOne));
  200. }
  201. if (Config.Options.TargetApi == TargetApi.Vulkan && Config.GpuAccessor.QueryTransformDepthMinusOneToOne())
  202. {
  203. Operand z = this.Load(StorageKind.Output, IoVariable.Position, null, Const(2));
  204. Operand w = this.Load(StorageKind.Output, IoVariable.Position, null, Const(3));
  205. Operand halfW = this.FPMultiply(w, ConstF(0.5f));
  206. this.Store(StorageKind.Output, IoVariable.Position, null, Const(2), this.FPFusedMultiplyAdd(z, ConstF(0.5f), halfW));
  207. }
  208. if (Config.Stage != ShaderStage.Geometry && Config.HasLayerInputAttribute)
  209. {
  210. Config.SetUsedFeature(FeatureFlags.RtLayer);
  211. int attrVecIndex = Config.GpLayerInputAttribute >> 2;
  212. int attrComponentIndex = Config.GpLayerInputAttribute & 3;
  213. Operand layer = this.Load(StorageKind.Output, IoVariable.UserDefined, null, Const(attrVecIndex), Const(attrComponentIndex));
  214. this.Store(StorageKind.Output, IoVariable.Layer, null, layer);
  215. }
  216. }
  217. public void PrepareForVertexReturn(out Operand oldXLocal, out Operand oldYLocal, out Operand oldZLocal)
  218. {
  219. if (Config.GpuAccessor.QueryViewportTransformDisable())
  220. {
  221. oldXLocal = Local();
  222. this.Copy(oldXLocal, this.Load(StorageKind.Output, IoVariable.Position, null, Const(0)));
  223. oldYLocal = Local();
  224. this.Copy(oldYLocal, this.Load(StorageKind.Output, IoVariable.Position, null, Const(1)));
  225. }
  226. else
  227. {
  228. oldXLocal = null;
  229. oldYLocal = null;
  230. }
  231. if (Config.Options.TargetApi == TargetApi.Vulkan && Config.GpuAccessor.QueryTransformDepthMinusOneToOne())
  232. {
  233. oldZLocal = Local();
  234. this.Copy(oldZLocal, this.Load(StorageKind.Output, IoVariable.Position, null, Const(2)));
  235. }
  236. else
  237. {
  238. oldZLocal = null;
  239. }
  240. PrepareForVertexReturn();
  241. }
  242. public void PrepareForReturn()
  243. {
  244. if (IsNonMain)
  245. {
  246. return;
  247. }
  248. if (Config.LastInVertexPipeline &&
  249. (Config.Stage == ShaderStage.Vertex || Config.Stage == ShaderStage.TessellationEvaluation) &&
  250. (Config.Options.Flags & TranslationFlags.VertexA) == 0)
  251. {
  252. PrepareForVertexReturn();
  253. }
  254. else if (Config.Stage == ShaderStage.Geometry)
  255. {
  256. void WritePositionOutput(int primIndex)
  257. {
  258. Operand x = this.Load(StorageKind.Input, IoVariable.Position, Const(primIndex), Const(0));
  259. Operand y = this.Load(StorageKind.Input, IoVariable.Position, Const(primIndex), Const(1));
  260. Operand z = this.Load(StorageKind.Input, IoVariable.Position, Const(primIndex), Const(2));
  261. Operand w = this.Load(StorageKind.Input, IoVariable.Position, Const(primIndex), Const(3));
  262. this.Store(StorageKind.Output, IoVariable.Position, null, Const(0), x);
  263. this.Store(StorageKind.Output, IoVariable.Position, null, Const(1), y);
  264. this.Store(StorageKind.Output, IoVariable.Position, null, Const(2), z);
  265. this.Store(StorageKind.Output, IoVariable.Position, null, Const(3), w);
  266. }
  267. void WriteUserDefinedOutput(int index, int primIndex)
  268. {
  269. Operand x = this.Load(StorageKind.Input, IoVariable.UserDefined, Const(index), Const(primIndex), Const(0));
  270. Operand y = this.Load(StorageKind.Input, IoVariable.UserDefined, Const(index), Const(primIndex), Const(1));
  271. Operand z = this.Load(StorageKind.Input, IoVariable.UserDefined, Const(index), Const(primIndex), Const(2));
  272. Operand w = this.Load(StorageKind.Input, IoVariable.UserDefined, Const(index), Const(primIndex), Const(3));
  273. this.Store(StorageKind.Output, IoVariable.UserDefined, null, Const(index), Const(0), x);
  274. this.Store(StorageKind.Output, IoVariable.UserDefined, null, Const(index), Const(1), y);
  275. this.Store(StorageKind.Output, IoVariable.UserDefined, null, Const(index), Const(2), z);
  276. this.Store(StorageKind.Output, IoVariable.UserDefined, null, Const(index), Const(3), w);
  277. }
  278. if (Config.GpPassthrough && !Config.GpuAccessor.QueryHostSupportsGeometryShaderPassthrough())
  279. {
  280. int inputVertices = Config.GpuAccessor.QueryPrimitiveTopology().ToInputVertices();
  281. for (int primIndex = 0; primIndex < inputVertices; primIndex++)
  282. {
  283. WritePositionOutput(primIndex);
  284. int passthroughAttributes = Config.PassthroughAttributes;
  285. while (passthroughAttributes != 0)
  286. {
  287. int index = BitOperations.TrailingZeroCount(passthroughAttributes);
  288. WriteUserDefinedOutput(index, primIndex);
  289. Config.SetOutputUserAttribute(index);
  290. passthroughAttributes &= ~(1 << index);
  291. }
  292. this.EmitVertex();
  293. }
  294. this.EndPrimitive();
  295. }
  296. }
  297. else if (Config.Stage == ShaderStage.Fragment)
  298. {
  299. GenerateAlphaToCoverageDitherDiscard();
  300. bool supportsBgra = Config.GpuAccessor.QueryHostSupportsBgraFormat();
  301. if (Config.OmapDepth)
  302. {
  303. Operand src = Register(Config.GetDepthRegister(), RegisterType.Gpr);
  304. this.Store(StorageKind.Output, IoVariable.FragmentOutputDepth, null, src);
  305. }
  306. AlphaTestOp alphaTestOp = Config.GpuAccessor.QueryAlphaTestCompare();
  307. if (alphaTestOp != AlphaTestOp.Always && (Config.OmapTargets & 8) != 0)
  308. {
  309. if (alphaTestOp == AlphaTestOp.Never)
  310. {
  311. this.Discard();
  312. }
  313. else
  314. {
  315. Instruction comparator = alphaTestOp switch
  316. {
  317. AlphaTestOp.Equal => Instruction.CompareEqual,
  318. AlphaTestOp.Greater => Instruction.CompareGreater,
  319. AlphaTestOp.GreaterOrEqual => Instruction.CompareGreaterOrEqual,
  320. AlphaTestOp.Less => Instruction.CompareLess,
  321. AlphaTestOp.LessOrEqual => Instruction.CompareLessOrEqual,
  322. AlphaTestOp.NotEqual => Instruction.CompareNotEqual,
  323. _ => 0
  324. };
  325. Debug.Assert(comparator != 0, $"Invalid alpha test operation \"{alphaTestOp}\".");
  326. Operand alpha = Register(3, RegisterType.Gpr);
  327. Operand alphaRef = ConstF(Config.GpuAccessor.QueryAlphaTestReference());
  328. Operand alphaPass = Add(Instruction.FP32 | comparator, Local(), alpha, alphaRef);
  329. Operand alphaPassLabel = Label();
  330. this.BranchIfTrue(alphaPassLabel, alphaPass);
  331. this.Discard();
  332. this.MarkLabel(alphaPassLabel);
  333. }
  334. }
  335. int regIndexBase = 0;
  336. for (int rtIndex = 0; rtIndex < 8; rtIndex++)
  337. {
  338. for (int component = 0; component < 4; component++)
  339. {
  340. bool componentEnabled = (Config.OmapTargets & (1 << (rtIndex * 4 + component))) != 0;
  341. if (!componentEnabled)
  342. {
  343. continue;
  344. }
  345. Operand src = Register(regIndexBase + component, RegisterType.Gpr);
  346. // Perform B <-> R swap if needed, for BGRA formats (not supported on OpenGL).
  347. if (!supportsBgra && (component == 0 || component == 2))
  348. {
  349. Operand isBgra = this.Load(StorageKind.Input, IoVariable.FragmentOutputIsBgra, null, Const(rtIndex));
  350. Operand lblIsBgra = Label();
  351. Operand lblEnd = Label();
  352. this.BranchIfTrue(lblIsBgra, isBgra);
  353. this.Store(StorageKind.Output, IoVariable.FragmentOutputColor, null, Const(rtIndex), Const(component), src);
  354. this.Branch(lblEnd);
  355. MarkLabel(lblIsBgra);
  356. this.Store(StorageKind.Output, IoVariable.FragmentOutputColor, null, Const(rtIndex), Const(2 - component), src);
  357. MarkLabel(lblEnd);
  358. }
  359. else
  360. {
  361. this.Store(StorageKind.Output, IoVariable.FragmentOutputColor, null, Const(rtIndex), Const(component), src);
  362. }
  363. }
  364. bool targetEnabled = (Config.OmapTargets & (0xf << (rtIndex * 4))) != 0;
  365. if (targetEnabled)
  366. {
  367. Config.SetOutputUserAttribute(rtIndex);
  368. regIndexBase += 4;
  369. }
  370. }
  371. }
  372. }
  373. private void GenerateAlphaToCoverageDitherDiscard()
  374. {
  375. // If the feature is disabled, or alpha is not written, then we're done.
  376. if (!Config.GpuAccessor.QueryAlphaToCoverageDitherEnable() || (Config.OmapTargets & 8) == 0)
  377. {
  378. return;
  379. }
  380. // 11 11 11 10 10 10 10 00
  381. // 11 01 01 01 01 00 00 00
  382. Operand ditherMask = Const(unchecked((int)0xfbb99110u));
  383. Operand fragCoordX = this.Load(StorageKind.Input, IoVariable.FragmentCoord, null, Const(0));
  384. Operand fragCoordY = this.Load(StorageKind.Input, IoVariable.FragmentCoord, null, Const(1));
  385. Operand x = this.BitwiseAnd(this.FP32ConvertToU32(fragCoordX), Const(1));
  386. Operand y = this.BitwiseAnd(this.FP32ConvertToU32(fragCoordY), Const(1));
  387. Operand xy = this.BitwiseOr(x, this.ShiftLeft(y, Const(1)));
  388. Operand alpha = Register(3, RegisterType.Gpr);
  389. Operand scaledAlpha = this.FPMultiply(this.FPSaturate(alpha), ConstF(8));
  390. Operand quantizedAlpha = this.IMinimumU32(this.FP32ConvertToU32(scaledAlpha), Const(7));
  391. Operand shift = this.BitwiseOr(this.ShiftLeft(quantizedAlpha, Const(2)), xy);
  392. Operand opaque = this.BitwiseAnd(this.ShiftRightU32(ditherMask, shift), Const(1));
  393. Operand a2cDitherEndLabel = Label();
  394. this.BranchIfTrue(a2cDitherEndLabel, opaque);
  395. this.Discard();
  396. this.MarkLabel(a2cDitherEndLabel);
  397. }
  398. public Operation[] GetOperations()
  399. {
  400. return _operations.ToArray();
  401. }
  402. }
  403. }