StructuredProgram.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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(BasicBlock[] blocks, ShaderConfig config)
  10. {
  11. PhiFunctions.Remove(blocks);
  12. StructuredProgramContext context = new StructuredProgramContext(blocks.Length, config);
  13. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  14. {
  15. BasicBlock block = blocks[blkIndex];
  16. context.EnterBlock(block);
  17. foreach (INode node in block.Operations)
  18. {
  19. Operation operation = (Operation)node;
  20. if (IsBranchInst(operation.Inst))
  21. {
  22. context.LeaveBlock(block, operation);
  23. }
  24. else
  25. {
  26. AddOperation(context, operation);
  27. }
  28. }
  29. }
  30. GotoElimination.Eliminate(context.GetGotos());
  31. AstOptimizer.Optimize(context);
  32. return context.Info;
  33. }
  34. private static void AddOperation(StructuredProgramContext context, Operation operation)
  35. {
  36. Instruction inst = operation.Inst;
  37. IAstNode[] sources = new IAstNode[operation.SourcesCount];
  38. for (int index = 0; index < sources.Length; index++)
  39. {
  40. sources[index] = context.GetOperandUse(operation.GetSource(index));
  41. }
  42. AstTextureOperation GetAstTextureOperation(TextureOperation texOp)
  43. {
  44. return new AstTextureOperation(
  45. inst,
  46. texOp.Type,
  47. texOp.Flags,
  48. texOp.Handle,
  49. 4, // TODO: Non-hardcoded array size.
  50. texOp.Index,
  51. sources);
  52. }
  53. if (operation.Dest != null)
  54. {
  55. AstOperand dest = context.GetOperandDef(operation.Dest);
  56. if (inst == Instruction.LoadConstant)
  57. {
  58. Operand slot = operation.GetSource(0);
  59. if (slot.Type != OperandType.Constant)
  60. {
  61. throw new InvalidOperationException("Found load with non-constant constant buffer slot.");
  62. }
  63. context.Info.CBuffers.Add(slot.Value);
  64. }
  65. else if (UsesStorage(inst))
  66. {
  67. AddSBufferUse(context.Info.SBuffers, operation);
  68. }
  69. AstAssignment assignment;
  70. // If all the sources are bool, it's better to use short-circuiting
  71. // logical operations, rather than forcing a cast to int and doing
  72. // a bitwise operation with the value, as it is likely to be used as
  73. // a bool in the end.
  74. if (IsBitwiseInst(inst) && AreAllSourceTypesEqual(sources, VariableType.Bool))
  75. {
  76. inst = GetLogicalFromBitwiseInst(inst);
  77. }
  78. bool isCondSel = inst == Instruction.ConditionalSelect;
  79. bool isCopy = inst == Instruction.Copy;
  80. if (isCondSel || isCopy)
  81. {
  82. VariableType type = GetVarTypeFromUses(operation.Dest);
  83. if (isCondSel && type == VariableType.F32)
  84. {
  85. inst |= Instruction.FP32;
  86. }
  87. dest.VarType = type;
  88. }
  89. else
  90. {
  91. dest.VarType = InstructionInfo.GetDestVarType(inst);
  92. }
  93. IAstNode source;
  94. if (operation is TextureOperation texOp)
  95. {
  96. AstTextureOperation astTexOp = GetAstTextureOperation(texOp);
  97. if (texOp.Inst == Instruction.ImageLoad)
  98. {
  99. context.Info.Images.Add(astTexOp);
  100. }
  101. else
  102. {
  103. context.Info.Samplers.Add(astTexOp);
  104. }
  105. source = astTexOp;
  106. }
  107. else if (!isCopy)
  108. {
  109. source = new AstOperation(inst, operation.Index, sources);
  110. }
  111. else
  112. {
  113. source = sources[0];
  114. }
  115. assignment = new AstAssignment(dest, source);
  116. context.AddNode(assignment);
  117. }
  118. else if (operation.Inst == Instruction.Comment)
  119. {
  120. context.AddNode(new AstComment(((CommentNode)operation).Comment));
  121. }
  122. else if (operation is TextureOperation texOp)
  123. {
  124. AstTextureOperation astTexOp = GetAstTextureOperation(texOp);
  125. context.Info.Images.Add(astTexOp);
  126. context.AddNode(astTexOp);
  127. }
  128. else
  129. {
  130. if (UsesStorage(inst))
  131. {
  132. AddSBufferUse(context.Info.SBuffers, operation);
  133. }
  134. context.AddNode(new AstOperation(inst, operation.Index, sources));
  135. }
  136. // Those instructions needs to be emulated by using helper functions,
  137. // because they are NVIDIA specific. Those flags helps the backend to
  138. // decide which helper functions are needed on the final generated code.
  139. switch (operation.Inst)
  140. {
  141. case Instruction.MultiplyHighS32:
  142. context.Info.HelperFunctionsMask |= HelperFunctionsMask.MultiplyHighS32;
  143. break;
  144. case Instruction.MultiplyHighU32:
  145. context.Info.HelperFunctionsMask |= HelperFunctionsMask.MultiplyHighU32;
  146. break;
  147. case Instruction.Shuffle:
  148. context.Info.HelperFunctionsMask |= HelperFunctionsMask.Shuffle;
  149. break;
  150. case Instruction.ShuffleDown:
  151. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleDown;
  152. break;
  153. case Instruction.ShuffleUp:
  154. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleUp;
  155. break;
  156. case Instruction.ShuffleXor:
  157. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleXor;
  158. break;
  159. case Instruction.SwizzleAdd:
  160. context.Info.HelperFunctionsMask |= HelperFunctionsMask.SwizzleAdd;
  161. break;
  162. }
  163. }
  164. private static void AddSBufferUse(HashSet<int> sBuffers, Operation operation)
  165. {
  166. Operand slot = operation.GetSource(0);
  167. if (slot.Type == OperandType.Constant)
  168. {
  169. sBuffers.Add(slot.Value);
  170. }
  171. else
  172. {
  173. // If the value is not constant, then we don't know
  174. // how many storage buffers are used, so we assume
  175. // all of them are used.
  176. for (int index = 0; index < GlobalMemory.StorageMaxCount; index++)
  177. {
  178. sBuffers.Add(index);
  179. }
  180. }
  181. }
  182. private static VariableType GetVarTypeFromUses(Operand dest)
  183. {
  184. HashSet<Operand> visited = new HashSet<Operand>();
  185. Queue<Operand> pending = new Queue<Operand>();
  186. bool Enqueue(Operand operand)
  187. {
  188. if (visited.Add(operand))
  189. {
  190. pending.Enqueue(operand);
  191. return true;
  192. }
  193. return false;
  194. }
  195. Enqueue(dest);
  196. while (pending.TryDequeue(out Operand operand))
  197. {
  198. foreach (INode useNode in operand.UseOps)
  199. {
  200. if (!(useNode is Operation operation))
  201. {
  202. continue;
  203. }
  204. if (operation.Inst == Instruction.Copy)
  205. {
  206. if (operation.Dest.Type == OperandType.LocalVariable)
  207. {
  208. if (Enqueue(operation.Dest))
  209. {
  210. break;
  211. }
  212. }
  213. else
  214. {
  215. return OperandInfo.GetVarType(operation.Dest.Type);
  216. }
  217. }
  218. else
  219. {
  220. for (int index = 0; index < operation.SourcesCount; index++)
  221. {
  222. if (operation.GetSource(index) == operand)
  223. {
  224. return InstructionInfo.GetSrcVarType(operation.Inst, index);
  225. }
  226. }
  227. }
  228. }
  229. }
  230. return VariableType.S32;
  231. }
  232. private static bool AreAllSourceTypesEqual(IAstNode[] sources, VariableType type)
  233. {
  234. foreach (IAstNode node in sources)
  235. {
  236. if (!(node is AstOperand operand))
  237. {
  238. return false;
  239. }
  240. if (operand.VarType != type)
  241. {
  242. return false;
  243. }
  244. }
  245. return true;
  246. }
  247. private static bool IsBranchInst(Instruction inst)
  248. {
  249. switch (inst)
  250. {
  251. case Instruction.Branch:
  252. case Instruction.BranchIfFalse:
  253. case Instruction.BranchIfTrue:
  254. return true;
  255. }
  256. return false;
  257. }
  258. private static bool IsBitwiseInst(Instruction inst)
  259. {
  260. switch (inst)
  261. {
  262. case Instruction.BitwiseAnd:
  263. case Instruction.BitwiseExclusiveOr:
  264. case Instruction.BitwiseNot:
  265. case Instruction.BitwiseOr:
  266. return true;
  267. }
  268. return false;
  269. }
  270. private static Instruction GetLogicalFromBitwiseInst(Instruction inst)
  271. {
  272. switch (inst)
  273. {
  274. case Instruction.BitwiseAnd: return Instruction.LogicalAnd;
  275. case Instruction.BitwiseExclusiveOr: return Instruction.LogicalExclusiveOr;
  276. case Instruction.BitwiseNot: return Instruction.LogicalNot;
  277. case Instruction.BitwiseOr: return Instruction.LogicalOr;
  278. }
  279. throw new ArgumentException($"Unexpected instruction \"{inst}\".");
  280. }
  281. private static bool UsesStorage(Instruction inst)
  282. {
  283. if (inst == Instruction.LoadStorage || inst == Instruction.StoreStorage)
  284. {
  285. return true;
  286. }
  287. return inst.IsAtomic() && (inst & Instruction.MrMask) == Instruction.MrStorage;
  288. }
  289. }
  290. }