StructuredProgram.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.Graphics.Shader.StructuredIr
  5. {
  6. static class StructuredProgram
  7. {
  8. public static StructuredProgramInfo MakeStructuredProgram(BasicBlock[] blocks, ShaderConfig config)
  9. {
  10. PhiFunctions.Remove(blocks);
  11. StructuredProgramContext context = new StructuredProgramContext(blocks.Length, config);
  12. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  13. {
  14. BasicBlock block = blocks[blkIndex];
  15. context.EnterBlock(block);
  16. foreach (INode node in block.Operations)
  17. {
  18. Operation operation = (Operation)node;
  19. if (IsBranchInst(operation.Inst))
  20. {
  21. context.LeaveBlock(block, operation);
  22. }
  23. else
  24. {
  25. AddOperation(context, operation);
  26. }
  27. }
  28. }
  29. GotoElimination.Eliminate(context.GetGotos());
  30. AstOptimizer.Optimize(context);
  31. return context.Info;
  32. }
  33. private static void AddOperation(StructuredProgramContext context, Operation operation)
  34. {
  35. Instruction inst = operation.Inst;
  36. IAstNode[] sources = new IAstNode[operation.SourcesCount];
  37. for (int index = 0; index < sources.Length; index++)
  38. {
  39. sources[index] = context.GetOperandUse(operation.GetSource(index));
  40. }
  41. if (operation.Dest != null)
  42. {
  43. AstOperand dest = context.GetOperandDef(operation.Dest);
  44. if (inst == Instruction.LoadConstant)
  45. {
  46. Operand slot = operation.GetSource(0);
  47. if (slot.Type != OperandType.Constant)
  48. {
  49. throw new InvalidOperationException("Found load with non-constant constant buffer slot.");
  50. }
  51. context.Info.CBuffers.Add(slot.Value);
  52. }
  53. else if (inst == Instruction.LoadStorage)
  54. {
  55. Operand slot = operation.GetSource(0);
  56. if (slot.Type != OperandType.Constant)
  57. {
  58. throw new InvalidOperationException("Found load or store with non-constant storage buffer slot.");
  59. }
  60. context.Info.SBuffers.Add(slot.Value);
  61. }
  62. AstAssignment assignment;
  63. // If all the sources are bool, it's better to use short-circuiting
  64. // logical operations, rather than forcing a cast to int and doing
  65. // a bitwise operation with the value, as it is likely to be used as
  66. // a bool in the end.
  67. if (IsBitwiseInst(inst) && AreAllSourceTypesEqual(sources, VariableType.Bool))
  68. {
  69. inst = GetLogicalFromBitwiseInst(inst);
  70. }
  71. bool isCondSel = inst == Instruction.ConditionalSelect;
  72. bool isCopy = inst == Instruction.Copy;
  73. if (isCondSel || isCopy)
  74. {
  75. VariableType type = GetVarTypeFromUses(operation.Dest);
  76. if (isCondSel && type == VariableType.F32)
  77. {
  78. inst |= Instruction.FP;
  79. }
  80. dest.VarType = type;
  81. }
  82. else
  83. {
  84. dest.VarType = InstructionInfo.GetDestVarType(inst);
  85. }
  86. int componentMask = 1 << operation.ComponentIndex;
  87. IAstNode source;
  88. if (operation is TextureOperation texOp)
  89. {
  90. AstTextureOperation astTexOp = new AstTextureOperation(
  91. inst,
  92. texOp.Target,
  93. texOp.Flags,
  94. texOp.Handle,
  95. componentMask,
  96. sources);
  97. context.Info.Samplers.Add(astTexOp);
  98. source = astTexOp;
  99. }
  100. else if (!isCopy)
  101. {
  102. source = new AstOperation(inst, componentMask, sources);
  103. }
  104. else
  105. {
  106. source = sources[0];
  107. }
  108. assignment = new AstAssignment(dest, source);
  109. context.AddNode(assignment);
  110. }
  111. else if (operation.Inst == Instruction.Comment)
  112. {
  113. context.AddNode(new AstComment(((CommentNode)operation).Comment));
  114. }
  115. else
  116. {
  117. if (inst == Instruction.StoreStorage)
  118. {
  119. Operand slot = operation.GetSource(0);
  120. if (slot.Type != OperandType.Constant)
  121. {
  122. throw new InvalidOperationException("Found load or store with non-constant storage buffer slot.");
  123. }
  124. context.Info.SBuffers.Add(slot.Value);
  125. }
  126. context.AddNode(new AstOperation(inst, sources));
  127. }
  128. }
  129. private static VariableType GetVarTypeFromUses(Operand dest)
  130. {
  131. HashSet<Operand> visited = new HashSet<Operand>();
  132. Queue<Operand> pending = new Queue<Operand>();
  133. bool Enqueue(Operand operand)
  134. {
  135. if (visited.Add(operand))
  136. {
  137. pending.Enqueue(operand);
  138. return true;
  139. }
  140. return false;
  141. }
  142. Enqueue(dest);
  143. while (pending.TryDequeue(out Operand operand))
  144. {
  145. foreach (INode useNode in operand.UseOps)
  146. {
  147. if (!(useNode is Operation operation))
  148. {
  149. continue;
  150. }
  151. if (operation.Inst == Instruction.Copy)
  152. {
  153. if (operation.Dest.Type == OperandType.LocalVariable)
  154. {
  155. if (Enqueue(operation.Dest))
  156. {
  157. break;
  158. }
  159. }
  160. else
  161. {
  162. return OperandInfo.GetVarType(operation.Dest.Type);
  163. }
  164. }
  165. else
  166. {
  167. for (int index = 0; index < operation.SourcesCount; index++)
  168. {
  169. if (operation.GetSource(index) == operand)
  170. {
  171. return InstructionInfo.GetSrcVarType(operation.Inst, index);
  172. }
  173. }
  174. }
  175. }
  176. }
  177. return VariableType.S32;
  178. }
  179. private static bool AreAllSourceTypesEqual(IAstNode[] sources, VariableType type)
  180. {
  181. foreach (IAstNode node in sources)
  182. {
  183. if (!(node is AstOperand operand))
  184. {
  185. return false;
  186. }
  187. if (operand.VarType != type)
  188. {
  189. return false;
  190. }
  191. }
  192. return true;
  193. }
  194. private static bool IsBranchInst(Instruction inst)
  195. {
  196. switch (inst)
  197. {
  198. case Instruction.Branch:
  199. case Instruction.BranchIfFalse:
  200. case Instruction.BranchIfTrue:
  201. return true;
  202. }
  203. return false;
  204. }
  205. private static bool IsBitwiseInst(Instruction inst)
  206. {
  207. switch (inst)
  208. {
  209. case Instruction.BitwiseAnd:
  210. case Instruction.BitwiseExclusiveOr:
  211. case Instruction.BitwiseNot:
  212. case Instruction.BitwiseOr:
  213. return true;
  214. }
  215. return false;
  216. }
  217. private static Instruction GetLogicalFromBitwiseInst(Instruction inst)
  218. {
  219. switch (inst)
  220. {
  221. case Instruction.BitwiseAnd: return Instruction.LogicalAnd;
  222. case Instruction.BitwiseExclusiveOr: return Instruction.LogicalExclusiveOr;
  223. case Instruction.BitwiseNot: return Instruction.LogicalNot;
  224. case Instruction.BitwiseOr: return Instruction.LogicalOr;
  225. }
  226. throw new ArgumentException($"Unexpected instruction \"{inst}\".");
  227. }
  228. }
  229. }