StructuredProgram.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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.FP;
  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.Shuffle:
  142. context.Info.HelperFunctionsMask |= HelperFunctionsMask.Shuffle;
  143. break;
  144. case Instruction.ShuffleDown:
  145. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleDown;
  146. break;
  147. case Instruction.ShuffleUp:
  148. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleUp;
  149. break;
  150. case Instruction.ShuffleXor:
  151. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleXor;
  152. break;
  153. case Instruction.SwizzleAdd:
  154. context.Info.HelperFunctionsMask |= HelperFunctionsMask.SwizzleAdd;
  155. break;
  156. }
  157. }
  158. private static void AddSBufferUse(HashSet<int> sBuffers, Operation operation)
  159. {
  160. Operand slot = operation.GetSource(0);
  161. if (slot.Type == OperandType.Constant)
  162. {
  163. sBuffers.Add(slot.Value);
  164. }
  165. else
  166. {
  167. // If the value is not constant, then we don't know
  168. // how many storage buffers are used, so we assume
  169. // all of them are used.
  170. for (int index = 0; index < GlobalMemory.StorageMaxCount; index++)
  171. {
  172. sBuffers.Add(index);
  173. }
  174. }
  175. }
  176. private static VariableType GetVarTypeFromUses(Operand dest)
  177. {
  178. HashSet<Operand> visited = new HashSet<Operand>();
  179. Queue<Operand> pending = new Queue<Operand>();
  180. bool Enqueue(Operand operand)
  181. {
  182. if (visited.Add(operand))
  183. {
  184. pending.Enqueue(operand);
  185. return true;
  186. }
  187. return false;
  188. }
  189. Enqueue(dest);
  190. while (pending.TryDequeue(out Operand operand))
  191. {
  192. foreach (INode useNode in operand.UseOps)
  193. {
  194. if (!(useNode is Operation operation))
  195. {
  196. continue;
  197. }
  198. if (operation.Inst == Instruction.Copy)
  199. {
  200. if (operation.Dest.Type == OperandType.LocalVariable)
  201. {
  202. if (Enqueue(operation.Dest))
  203. {
  204. break;
  205. }
  206. }
  207. else
  208. {
  209. return OperandInfo.GetVarType(operation.Dest.Type);
  210. }
  211. }
  212. else
  213. {
  214. for (int index = 0; index < operation.SourcesCount; index++)
  215. {
  216. if (operation.GetSource(index) == operand)
  217. {
  218. return InstructionInfo.GetSrcVarType(operation.Inst, index);
  219. }
  220. }
  221. }
  222. }
  223. }
  224. return VariableType.S32;
  225. }
  226. private static bool AreAllSourceTypesEqual(IAstNode[] sources, VariableType type)
  227. {
  228. foreach (IAstNode node in sources)
  229. {
  230. if (!(node is AstOperand operand))
  231. {
  232. return false;
  233. }
  234. if (operand.VarType != type)
  235. {
  236. return false;
  237. }
  238. }
  239. return true;
  240. }
  241. private static bool IsBranchInst(Instruction inst)
  242. {
  243. switch (inst)
  244. {
  245. case Instruction.Branch:
  246. case Instruction.BranchIfFalse:
  247. case Instruction.BranchIfTrue:
  248. return true;
  249. }
  250. return false;
  251. }
  252. private static bool IsBitwiseInst(Instruction inst)
  253. {
  254. switch (inst)
  255. {
  256. case Instruction.BitwiseAnd:
  257. case Instruction.BitwiseExclusiveOr:
  258. case Instruction.BitwiseNot:
  259. case Instruction.BitwiseOr:
  260. return true;
  261. }
  262. return false;
  263. }
  264. private static Instruction GetLogicalFromBitwiseInst(Instruction inst)
  265. {
  266. switch (inst)
  267. {
  268. case Instruction.BitwiseAnd: return Instruction.LogicalAnd;
  269. case Instruction.BitwiseExclusiveOr: return Instruction.LogicalExclusiveOr;
  270. case Instruction.BitwiseNot: return Instruction.LogicalNot;
  271. case Instruction.BitwiseOr: return Instruction.LogicalOr;
  272. }
  273. throw new ArgumentException($"Unexpected instruction \"{inst}\".");
  274. }
  275. private static bool UsesStorage(Instruction inst)
  276. {
  277. if (inst == Instruction.LoadStorage || inst == Instruction.StoreStorage)
  278. {
  279. return true;
  280. }
  281. return inst.IsAtomic() && (inst & Instruction.MrMask) == Instruction.MrStorage;
  282. }
  283. }
  284. }