StructuredProgram.cs 13 KB

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