StructuredProgram.cs 12 KB

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