StructuredProgramContext.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using Ryujinx.Graphics.Shader.Translation;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using static Ryujinx.Graphics.Shader.StructuredIr.AstHelper;
  6. namespace Ryujinx.Graphics.Shader.StructuredIr
  7. {
  8. class StructuredProgramContext
  9. {
  10. private HashSet<BasicBlock> _loopTails;
  11. private Stack<(AstBlock Block, int CurrEndIndex, int LoopEndIndex)> _blockStack;
  12. private Dictionary<Operand, AstOperand> _localsMap;
  13. private Dictionary<int, AstAssignment> _gotoTempAsgs;
  14. private List<GotoStatement> _gotos;
  15. private AstBlock _currBlock;
  16. private int _currEndIndex;
  17. private int _loopEndIndex;
  18. public StructuredProgramInfo Info { get; }
  19. public ShaderConfig Config { get; }
  20. public StructuredProgramContext(int blocksCount, ShaderConfig config)
  21. {
  22. _loopTails = new HashSet<BasicBlock>();
  23. _blockStack = new Stack<(AstBlock, int, int)>();
  24. _localsMap = new Dictionary<Operand, AstOperand>();
  25. _gotoTempAsgs = new Dictionary<int, AstAssignment>();
  26. _gotos = new List<GotoStatement>();
  27. _currBlock = new AstBlock(AstBlockType.Main);
  28. _currEndIndex = blocksCount;
  29. _loopEndIndex = blocksCount;
  30. Info = new StructuredProgramInfo(_currBlock);
  31. Config = config;
  32. }
  33. public void EnterBlock(BasicBlock block)
  34. {
  35. while (_currEndIndex == block.Index)
  36. {
  37. (_currBlock, _currEndIndex, _loopEndIndex) = _blockStack.Pop();
  38. }
  39. if (_gotoTempAsgs.TryGetValue(block.Index, out AstAssignment gotoTempAsg))
  40. {
  41. AddGotoTempReset(block, gotoTempAsg);
  42. }
  43. LookForDoWhileStatements(block);
  44. }
  45. public void LeaveBlock(BasicBlock block, Operation branchOp)
  46. {
  47. LookForIfStatements(block, branchOp);
  48. }
  49. private void LookForDoWhileStatements(BasicBlock block)
  50. {
  51. // Check if we have any predecessor whose index is greater than the
  52. // current block, this indicates a loop.
  53. bool done = false;
  54. foreach (BasicBlock predecessor in block.Predecessors.OrderByDescending(x => x.Index))
  55. {
  56. // If not a loop, break.
  57. if (predecessor.Index < block.Index)
  58. {
  59. break;
  60. }
  61. // Check if we can create a do-while loop here (only possible if the loop end
  62. // falls inside the current scope), if not add a goto instead.
  63. if (predecessor.Index < _currEndIndex && !done)
  64. {
  65. // Create do-while loop block. We must avoid inserting a goto at the end
  66. // of the loop later, when the tail block is processed. So we add the predecessor
  67. // to a list of loop tails to prevent it from being processed later.
  68. Operation branchOp = (Operation)predecessor.GetLastOp();
  69. NewBlock(AstBlockType.DoWhile, branchOp, predecessor.Index + 1);
  70. _loopTails.Add(predecessor);
  71. done = true;
  72. }
  73. else
  74. {
  75. // Failed to create loop. Since this block is the loop head, we reset the
  76. // goto condition variable here. The variable is always reset on the jump
  77. // target, and this block is the jump target for some loop.
  78. AddGotoTempReset(block, GetGotoTempAsg(block.Index));
  79. break;
  80. }
  81. }
  82. }
  83. private void LookForIfStatements(BasicBlock block, Operation branchOp)
  84. {
  85. if (block.Branch == null)
  86. {
  87. return;
  88. }
  89. // We can only enclose the "if" when the branch lands before
  90. // the end of the current block. If the current enclosing block
  91. // is not a loop, then we can also do so if the branch lands
  92. // right at the end of the current block. When it is a loop,
  93. // this is not valid as the loop condition would be evaluated,
  94. // and it could erroneously jump back to the start of the loop.
  95. bool inRange =
  96. block.Branch.Index < _currEndIndex ||
  97. (block.Branch.Index == _currEndIndex && block.Branch.Index < _loopEndIndex);
  98. bool isLoop = block.Branch.Index <= block.Index;
  99. if (inRange && !isLoop)
  100. {
  101. NewBlock(AstBlockType.If, branchOp, block.Branch.Index);
  102. }
  103. else if (!_loopTails.Contains(block))
  104. {
  105. AstAssignment gotoTempAsg = GetGotoTempAsg(block.Branch.Index);
  106. // We use DoWhile type here, as the condition should be true for
  107. // unconditional branches, or it should jump if the condition is true otherwise.
  108. IAstNode cond = GetBranchCond(AstBlockType.DoWhile, branchOp);
  109. AddNode(Assign(gotoTempAsg.Destination, cond));
  110. AstOperation branch = new AstOperation(branchOp.Inst);
  111. AddNode(branch);
  112. GotoStatement gotoStmt = new GotoStatement(branch, gotoTempAsg, isLoop);
  113. _gotos.Add(gotoStmt);
  114. }
  115. }
  116. private AstAssignment GetGotoTempAsg(int index)
  117. {
  118. if (_gotoTempAsgs.TryGetValue(index, out AstAssignment gotoTempAsg))
  119. {
  120. return gotoTempAsg;
  121. }
  122. AstOperand gotoTemp = NewTemp(VariableType.Bool);
  123. gotoTempAsg = Assign(gotoTemp, Const(IrConsts.False));
  124. _gotoTempAsgs.Add(index, gotoTempAsg);
  125. return gotoTempAsg;
  126. }
  127. private void AddGotoTempReset(BasicBlock block, AstAssignment gotoTempAsg)
  128. {
  129. // If it was already added, we don't need to add it again.
  130. if (gotoTempAsg.Parent != null)
  131. {
  132. return;
  133. }
  134. AddNode(gotoTempAsg);
  135. // For block 0, we don't need to add the extra "reset" at the beginning,
  136. // because it is already the first node to be executed on the shader,
  137. // so it is reset to false by the "local" assignment anyway.
  138. if (block.Index != 0)
  139. {
  140. Info.MainBlock.AddFirst(Assign(gotoTempAsg.Destination, Const(IrConsts.False)));
  141. }
  142. }
  143. private void NewBlock(AstBlockType type, Operation branchOp, int endIndex)
  144. {
  145. NewBlock(type, GetBranchCond(type, branchOp), endIndex);
  146. }
  147. private void NewBlock(AstBlockType type, IAstNode cond, int endIndex)
  148. {
  149. AstBlock childBlock = new AstBlock(type, cond);
  150. AddNode(childBlock);
  151. _blockStack.Push((_currBlock, _currEndIndex, _loopEndIndex));
  152. _currBlock = childBlock;
  153. _currEndIndex = endIndex;
  154. if (type == AstBlockType.DoWhile)
  155. {
  156. _loopEndIndex = endIndex;
  157. }
  158. }
  159. private IAstNode GetBranchCond(AstBlockType type, Operation branchOp)
  160. {
  161. IAstNode cond;
  162. if (branchOp.Inst == Instruction.Branch)
  163. {
  164. // If the branch is not conditional, the condition is a constant.
  165. // For if it's false (always jump over, if block never executed).
  166. // For loops it's always true (always loop).
  167. cond = Const(type == AstBlockType.If ? IrConsts.False : IrConsts.True);
  168. }
  169. else
  170. {
  171. cond = GetOperandUse(branchOp.GetSource(0));
  172. Instruction invInst = type == AstBlockType.If
  173. ? Instruction.BranchIfTrue
  174. : Instruction.BranchIfFalse;
  175. if (branchOp.Inst == invInst)
  176. {
  177. cond = new AstOperation(Instruction.LogicalNot, cond);
  178. }
  179. }
  180. return cond;
  181. }
  182. public void AddNode(IAstNode node)
  183. {
  184. _currBlock.Add(node);
  185. }
  186. public GotoStatement[] GetGotos()
  187. {
  188. return _gotos.ToArray();
  189. }
  190. private AstOperand NewTemp(VariableType type)
  191. {
  192. AstOperand newTemp = Local(type);
  193. Info.Locals.Add(newTemp);
  194. return newTemp;
  195. }
  196. public AstOperand GetOperandDef(Operand operand)
  197. {
  198. if (TryGetUserAttributeIndex(operand, out int attrIndex))
  199. {
  200. Info.OAttributes.Add(attrIndex);
  201. }
  202. return GetOperand(operand);
  203. }
  204. public AstOperand GetOperandUse(Operand operand)
  205. {
  206. if (TryGetUserAttributeIndex(operand, out int attrIndex))
  207. {
  208. Info.IAttributes.Add(attrIndex);
  209. Info.InterpolationQualifiers[attrIndex] = operand.Interpolation;
  210. }
  211. else if (operand.Type == OperandType.Attribute && operand.Value == AttributeConsts.InstanceId)
  212. {
  213. Info.UsesInstanceId = true;
  214. }
  215. else if (operand.Type == OperandType.ConstantBuffer)
  216. {
  217. Info.CBuffers.Add(operand.GetCbufSlot());
  218. }
  219. return GetOperand(operand);
  220. }
  221. private AstOperand GetOperand(Operand operand)
  222. {
  223. if (operand == null)
  224. {
  225. return null;
  226. }
  227. if (operand.Type != OperandType.LocalVariable)
  228. {
  229. return new AstOperand(operand);
  230. }
  231. if (!_localsMap.TryGetValue(operand, out AstOperand astOperand))
  232. {
  233. astOperand = new AstOperand(operand);
  234. _localsMap.Add(operand, astOperand);
  235. Info.Locals.Add(astOperand);
  236. }
  237. return astOperand;
  238. }
  239. private static bool TryGetUserAttributeIndex(Operand operand, out int attrIndex)
  240. {
  241. if (operand.Type == OperandType.Attribute)
  242. {
  243. if (operand.Value >= AttributeConsts.UserAttributeBase &&
  244. operand.Value < AttributeConsts.UserAttributeEnd)
  245. {
  246. attrIndex = (operand.Value - AttributeConsts.UserAttributeBase) >> 4;
  247. return true;
  248. }
  249. else if (operand.Value >= AttributeConsts.FragmentOutputColorBase &&
  250. operand.Value < AttributeConsts.FragmentOutputColorEnd)
  251. {
  252. attrIndex = (operand.Value - AttributeConsts.FragmentOutputColorBase) >> 4;
  253. return true;
  254. }
  255. }
  256. attrIndex = 0;
  257. return false;
  258. }
  259. }
  260. }