StructuredProgram.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.Translation;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Numerics;
  6. namespace Ryujinx.Graphics.Shader.StructuredIr
  7. {
  8. static class StructuredProgram
  9. {
  10. public static StructuredProgramInfo MakeStructuredProgram(Function[] functions, ShaderConfig config)
  11. {
  12. StructuredProgramContext context = new StructuredProgramContext(config);
  13. for (int funcIndex = 0; funcIndex < functions.Length; funcIndex++)
  14. {
  15. Function function = functions[funcIndex];
  16. BasicBlock[] blocks = function.Blocks;
  17. AggregateType returnType = function.ReturnsValue ? AggregateType.S32 : AggregateType.Void;
  18. AggregateType[] inArguments = new AggregateType[function.InArgumentsCount];
  19. AggregateType[] outArguments = new AggregateType[function.OutArgumentsCount];
  20. for (int i = 0; i < inArguments.Length; i++)
  21. {
  22. inArguments[i] = AggregateType.S32;
  23. }
  24. for (int i = 0; i < outArguments.Length; i++)
  25. {
  26. outArguments[i] = AggregateType.S32;
  27. }
  28. context.EnterFunction(blocks.Length, function.Name, returnType, inArguments, outArguments);
  29. PhiFunctions.Remove(blocks);
  30. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  31. {
  32. BasicBlock block = blocks[blkIndex];
  33. context.EnterBlock(block);
  34. for (LinkedListNode<INode> opNode = block.Operations.First; opNode != null; opNode = opNode.Next)
  35. {
  36. Operation operation = (Operation)opNode.Value;
  37. if (IsBranchInst(operation.Inst))
  38. {
  39. context.LeaveBlock(block, operation);
  40. }
  41. else
  42. {
  43. AddOperation(context, operation);
  44. }
  45. }
  46. }
  47. GotoElimination.Eliminate(context.GetGotos());
  48. AstOptimizer.Optimize(context);
  49. context.LeaveFunction();
  50. }
  51. if (config.TransformFeedbackEnabled && config.LastInVertexPipeline)
  52. {
  53. for (int tfbIndex = 0; tfbIndex < 4; tfbIndex++)
  54. {
  55. var locations = config.GpuAccessor.QueryTransformFeedbackVaryingLocations(tfbIndex);
  56. var stride = config.GpuAccessor.QueryTransformFeedbackStride(tfbIndex);
  57. for (int i = 0; i < locations.Length; i++)
  58. {
  59. byte location = locations[i];
  60. if (location < 0xc0)
  61. {
  62. context.Info.TransformFeedbackOutputs[location] = new TransformFeedbackOutput(tfbIndex, i * 4, stride);
  63. }
  64. }
  65. }
  66. }
  67. return context.Info;
  68. }
  69. private static void AddOperation(StructuredProgramContext context, Operation operation)
  70. {
  71. Instruction inst = operation.Inst;
  72. if (inst == Instruction.LoadAttribute)
  73. {
  74. Operand src1 = operation.GetSource(0);
  75. Operand src2 = operation.GetSource(1);
  76. if (src1.Type == OperandType.Constant && src2.Type == OperandType.Constant)
  77. {
  78. int attrOffset = (src1.Value & AttributeConsts.Mask) + (src2.Value << 2);
  79. if ((src1.Value & AttributeConsts.LoadOutputMask) != 0)
  80. {
  81. context.Info.Outputs.Add(attrOffset);
  82. }
  83. else
  84. {
  85. context.Info.Inputs.Add(attrOffset);
  86. }
  87. }
  88. }
  89. bool vectorDest = IsVectorDestInst(inst);
  90. int sourcesCount = operation.SourcesCount;
  91. int outDestsCount = operation.DestsCount != 0 && !vectorDest ? operation.DestsCount - 1 : 0;
  92. IAstNode[] sources = new IAstNode[sourcesCount + outDestsCount];
  93. for (int index = 0; index < operation.SourcesCount; index++)
  94. {
  95. sources[index] = context.GetOperandUse(operation.GetSource(index));
  96. }
  97. for (int index = 0; index < outDestsCount; index++)
  98. {
  99. AstOperand oper = context.GetOperandDef(operation.GetDest(1 + index));
  100. oper.VarType = InstructionInfo.GetSrcVarType(inst, sourcesCount + index);
  101. sources[sourcesCount + index] = oper;
  102. }
  103. AstTextureOperation GetAstTextureOperation(TextureOperation texOp)
  104. {
  105. return new AstTextureOperation(
  106. inst,
  107. texOp.Type,
  108. texOp.Format,
  109. texOp.Flags,
  110. texOp.CbufSlot,
  111. texOp.Handle,
  112. texOp.Index,
  113. sources);
  114. }
  115. int componentsCount = BitOperations.PopCount((uint)operation.Index);
  116. if (vectorDest && componentsCount > 1)
  117. {
  118. AggregateType destType = InstructionInfo.GetDestVarType(inst);
  119. IAstNode source;
  120. if (operation is TextureOperation texOp)
  121. {
  122. if (texOp.Inst == Instruction.ImageLoad)
  123. {
  124. destType = texOp.Format.GetComponentType();
  125. }
  126. source = GetAstTextureOperation(texOp);
  127. }
  128. else
  129. {
  130. source = new AstOperation(inst, operation.Index, sources, operation.SourcesCount);
  131. }
  132. AggregateType destElemType = destType;
  133. switch (componentsCount)
  134. {
  135. case 2: destType |= AggregateType.Vector2; break;
  136. case 3: destType |= AggregateType.Vector3; break;
  137. case 4: destType |= AggregateType.Vector4; break;
  138. }
  139. AstOperand destVec = context.NewTemp(destType);
  140. context.AddNode(new AstAssignment(destVec, source));
  141. for (int i = 0; i < operation.DestsCount; i++)
  142. {
  143. AstOperand dest = context.GetOperandDef(operation.GetDest(i));
  144. AstOperand index = new AstOperand(OperandType.Constant, i);
  145. dest.VarType = destElemType;
  146. context.AddNode(new AstAssignment(dest, new AstOperation(Instruction.VectorExtract, new[] { destVec, index }, 2)));
  147. }
  148. }
  149. else if (operation.Dest != null)
  150. {
  151. AstOperand dest = context.GetOperandDef(operation.Dest);
  152. // If all the sources are bool, it's better to use short-circuiting
  153. // logical operations, rather than forcing a cast to int and doing
  154. // a bitwise operation with the value, as it is likely to be used as
  155. // a bool in the end.
  156. if (IsBitwiseInst(inst) && AreAllSourceTypesEqual(sources, AggregateType.Bool))
  157. {
  158. inst = GetLogicalFromBitwiseInst(inst);
  159. }
  160. bool isCondSel = inst == Instruction.ConditionalSelect;
  161. bool isCopy = inst == Instruction.Copy;
  162. if (isCondSel || isCopy)
  163. {
  164. AggregateType type = GetVarTypeFromUses(operation.Dest);
  165. if (isCondSel && type == AggregateType.FP32)
  166. {
  167. inst |= Instruction.FP32;
  168. }
  169. dest.VarType = type;
  170. }
  171. else
  172. {
  173. dest.VarType = InstructionInfo.GetDestVarType(inst);
  174. }
  175. IAstNode source;
  176. if (operation is TextureOperation texOp)
  177. {
  178. if (texOp.Inst == Instruction.ImageLoad)
  179. {
  180. dest.VarType = texOp.Format.GetComponentType();
  181. }
  182. source = GetAstTextureOperation(texOp);
  183. }
  184. else if (!isCopy)
  185. {
  186. source = new AstOperation(inst, operation.Index, sources, operation.SourcesCount);
  187. }
  188. else
  189. {
  190. source = sources[0];
  191. }
  192. context.AddNode(new AstAssignment(dest, source));
  193. }
  194. else if (operation.Inst == Instruction.Comment)
  195. {
  196. context.AddNode(new AstComment(((CommentNode)operation).Comment));
  197. }
  198. else if (operation is TextureOperation texOp)
  199. {
  200. AstTextureOperation astTexOp = GetAstTextureOperation(texOp);
  201. context.AddNode(astTexOp);
  202. }
  203. else
  204. {
  205. context.AddNode(new AstOperation(inst, operation.Index, sources, operation.SourcesCount));
  206. }
  207. // Those instructions needs to be emulated by using helper functions,
  208. // because they are NVIDIA specific. Those flags helps the backend to
  209. // decide which helper functions are needed on the final generated code.
  210. switch (operation.Inst)
  211. {
  212. case Instruction.AtomicMaxS32 | Instruction.MrShared:
  213. case Instruction.AtomicMinS32 | Instruction.MrShared:
  214. context.Info.HelperFunctionsMask |= HelperFunctionsMask.AtomicMinMaxS32Shared;
  215. break;
  216. case Instruction.AtomicMaxS32 | Instruction.MrStorage:
  217. case Instruction.AtomicMinS32 | Instruction.MrStorage:
  218. context.Info.HelperFunctionsMask |= HelperFunctionsMask.AtomicMinMaxS32Storage;
  219. break;
  220. case Instruction.MultiplyHighS32:
  221. context.Info.HelperFunctionsMask |= HelperFunctionsMask.MultiplyHighS32;
  222. break;
  223. case Instruction.MultiplyHighU32:
  224. context.Info.HelperFunctionsMask |= HelperFunctionsMask.MultiplyHighU32;
  225. break;
  226. case Instruction.Shuffle:
  227. context.Info.HelperFunctionsMask |= HelperFunctionsMask.Shuffle;
  228. break;
  229. case Instruction.ShuffleDown:
  230. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleDown;
  231. break;
  232. case Instruction.ShuffleUp:
  233. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleUp;
  234. break;
  235. case Instruction.ShuffleXor:
  236. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleXor;
  237. break;
  238. case Instruction.StoreShared16:
  239. case Instruction.StoreShared8:
  240. context.Info.HelperFunctionsMask |= HelperFunctionsMask.StoreSharedSmallInt;
  241. break;
  242. case Instruction.StoreStorage16:
  243. case Instruction.StoreStorage8:
  244. context.Info.HelperFunctionsMask |= HelperFunctionsMask.StoreStorageSmallInt;
  245. break;
  246. case Instruction.SwizzleAdd:
  247. context.Info.HelperFunctionsMask |= HelperFunctionsMask.SwizzleAdd;
  248. break;
  249. case Instruction.FSIBegin:
  250. case Instruction.FSIEnd:
  251. context.Info.HelperFunctionsMask |= HelperFunctionsMask.FSI;
  252. break;
  253. }
  254. }
  255. private static AggregateType GetVarTypeFromUses(Operand dest)
  256. {
  257. HashSet<Operand> visited = new HashSet<Operand>();
  258. Queue<Operand> pending = new Queue<Operand>();
  259. bool Enqueue(Operand operand)
  260. {
  261. if (visited.Add(operand))
  262. {
  263. pending.Enqueue(operand);
  264. return true;
  265. }
  266. return false;
  267. }
  268. Enqueue(dest);
  269. while (pending.TryDequeue(out Operand operand))
  270. {
  271. foreach (INode useNode in operand.UseOps)
  272. {
  273. if (useNode is not Operation operation)
  274. {
  275. continue;
  276. }
  277. if (operation.Inst == Instruction.Copy)
  278. {
  279. if (operation.Dest.Type == OperandType.LocalVariable)
  280. {
  281. if (Enqueue(operation.Dest))
  282. {
  283. break;
  284. }
  285. }
  286. else
  287. {
  288. return OperandInfo.GetVarType(operation.Dest.Type);
  289. }
  290. }
  291. else
  292. {
  293. for (int index = 0; index < operation.SourcesCount; index++)
  294. {
  295. if (operation.GetSource(index) == operand)
  296. {
  297. return InstructionInfo.GetSrcVarType(operation.Inst, index);
  298. }
  299. }
  300. }
  301. }
  302. }
  303. return AggregateType.S32;
  304. }
  305. private static bool AreAllSourceTypesEqual(IAstNode[] sources, AggregateType type)
  306. {
  307. foreach (IAstNode node in sources)
  308. {
  309. if (node is not AstOperand operand)
  310. {
  311. return false;
  312. }
  313. if (operand.VarType != type)
  314. {
  315. return false;
  316. }
  317. }
  318. return true;
  319. }
  320. private static bool IsVectorDestInst(Instruction inst)
  321. {
  322. return inst switch
  323. {
  324. Instruction.ImageLoad or
  325. Instruction.TextureSample => true,
  326. _ => false
  327. };
  328. }
  329. private static bool IsBranchInst(Instruction inst)
  330. {
  331. return inst switch
  332. {
  333. Instruction.Branch or
  334. Instruction.BranchIfFalse or
  335. Instruction.BranchIfTrue => true,
  336. _ => false
  337. };
  338. }
  339. private static bool IsBitwiseInst(Instruction inst)
  340. {
  341. return inst switch
  342. {
  343. Instruction.BitwiseAnd or
  344. Instruction.BitwiseExclusiveOr or
  345. Instruction.BitwiseNot or
  346. Instruction.BitwiseOr => true,
  347. _ => false
  348. };
  349. }
  350. private static Instruction GetLogicalFromBitwiseInst(Instruction inst)
  351. {
  352. return inst switch
  353. {
  354. Instruction.BitwiseAnd => Instruction.LogicalAnd,
  355. Instruction.BitwiseExclusiveOr => Instruction.LogicalExclusiveOr,
  356. Instruction.BitwiseNot => Instruction.LogicalNot,
  357. Instruction.BitwiseOr => Instruction.LogicalOr,
  358. _ => throw new ArgumentException($"Unexpected instruction \"{inst}\".")
  359. };
  360. }
  361. }
  362. }