StructuredProgramContext.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 (predecessor.Index < block.Index)
  57. {
  58. break;
  59. }
  60. if (predecessor.Index < _currEndIndex && !done)
  61. {
  62. Operation branchOp = (Operation)predecessor.GetLastOp();
  63. NewBlock(AstBlockType.DoWhile, branchOp, predecessor.Index + 1);
  64. _loopTails.Add(predecessor);
  65. done = true;
  66. }
  67. else
  68. {
  69. AddGotoTempReset(block, GetGotoTempAsg(block.Index));
  70. break;
  71. }
  72. }
  73. }
  74. private void LookForIfStatements(BasicBlock block, Operation branchOp)
  75. {
  76. if (block.Branch == null)
  77. {
  78. return;
  79. }
  80. // We can only enclose the "if" when the branch lands before
  81. // the end of the current block. If the current enclosing block
  82. // is not a loop, then we can also do so if the branch lands
  83. // right at the end of the current block. When it is a loop,
  84. // this is not valid as the loop condition would be evaluated,
  85. // and it could erroneously jump back to the start of the loop.
  86. bool inRange =
  87. block.Branch.Index < _currEndIndex ||
  88. (block.Branch.Index == _currEndIndex && block.Branch.Index < _loopEndIndex);
  89. bool isLoop = block.Branch.Index <= block.Index;
  90. if (inRange && !isLoop)
  91. {
  92. NewBlock(AstBlockType.If, branchOp, block.Branch.Index);
  93. }
  94. else if (!_loopTails.Contains(block))
  95. {
  96. AstAssignment gotoTempAsg = GetGotoTempAsg(block.Branch.Index);
  97. IAstNode cond = GetBranchCond(AstBlockType.DoWhile, branchOp);
  98. AddNode(Assign(gotoTempAsg.Destination, cond));
  99. AstOperation branch = new AstOperation(branchOp.Inst);
  100. AddNode(branch);
  101. GotoStatement gotoStmt = new GotoStatement(branch, gotoTempAsg, isLoop);
  102. _gotos.Add(gotoStmt);
  103. }
  104. }
  105. private AstAssignment GetGotoTempAsg(int index)
  106. {
  107. if (_gotoTempAsgs.TryGetValue(index, out AstAssignment gotoTempAsg))
  108. {
  109. return gotoTempAsg;
  110. }
  111. AstOperand gotoTemp = NewTemp(VariableType.Bool);
  112. gotoTempAsg = Assign(gotoTemp, Const(IrConsts.False));
  113. _gotoTempAsgs.Add(index, gotoTempAsg);
  114. return gotoTempAsg;
  115. }
  116. private void AddGotoTempReset(BasicBlock block, AstAssignment gotoTempAsg)
  117. {
  118. AddNode(gotoTempAsg);
  119. // For block 0, we don't need to add the extra "reset" at the beginning,
  120. // because it is already the first node to be executed on the shader,
  121. // so it is reset to false by the "local" assignment anyway.
  122. if (block.Index != 0)
  123. {
  124. Info.MainBlock.AddFirst(Assign(gotoTempAsg.Destination, Const(IrConsts.False)));
  125. }
  126. }
  127. private void NewBlock(AstBlockType type, Operation branchOp, int endIndex)
  128. {
  129. NewBlock(type, GetBranchCond(type, branchOp), endIndex);
  130. }
  131. private void NewBlock(AstBlockType type, IAstNode cond, int endIndex)
  132. {
  133. AstBlock childBlock = new AstBlock(type, cond);
  134. AddNode(childBlock);
  135. _blockStack.Push((_currBlock, _currEndIndex, _loopEndIndex));
  136. _currBlock = childBlock;
  137. _currEndIndex = endIndex;
  138. if (type == AstBlockType.DoWhile)
  139. {
  140. _loopEndIndex = endIndex;
  141. }
  142. }
  143. private IAstNode GetBranchCond(AstBlockType type, Operation branchOp)
  144. {
  145. IAstNode cond;
  146. if (branchOp.Inst == Instruction.Branch)
  147. {
  148. cond = Const(type == AstBlockType.If ? IrConsts.False : IrConsts.True);
  149. }
  150. else
  151. {
  152. cond = GetOperandUse(branchOp.GetSource(0));
  153. Instruction invInst = type == AstBlockType.If
  154. ? Instruction.BranchIfTrue
  155. : Instruction.BranchIfFalse;
  156. if (branchOp.Inst == invInst)
  157. {
  158. cond = new AstOperation(Instruction.LogicalNot, cond);
  159. }
  160. }
  161. return cond;
  162. }
  163. public void AddNode(IAstNode node)
  164. {
  165. _currBlock.Add(node);
  166. }
  167. public GotoStatement[] GetGotos()
  168. {
  169. return _gotos.ToArray();
  170. }
  171. private AstOperand NewTemp(VariableType type)
  172. {
  173. AstOperand newTemp = Local(type);
  174. Info.Locals.Add(newTemp);
  175. return newTemp;
  176. }
  177. public AstOperand GetOperandDef(Operand operand)
  178. {
  179. if (TryGetUserAttributeIndex(operand, out int attrIndex))
  180. {
  181. Info.OAttributes.Add(attrIndex);
  182. }
  183. return GetOperand(operand);
  184. }
  185. public AstOperand GetOperandUse(Operand operand)
  186. {
  187. if (TryGetUserAttributeIndex(operand, out int attrIndex))
  188. {
  189. Info.IAttributes.Add(attrIndex);
  190. Info.InterpolationQualifiers[attrIndex] = operand.Interpolation;
  191. }
  192. else if (operand.Type == OperandType.Attribute && operand.Value == AttributeConsts.InstanceId)
  193. {
  194. Info.UsesInstanceId = true;
  195. }
  196. else if (operand.Type == OperandType.ConstantBuffer)
  197. {
  198. Info.CBuffers.Add(operand.GetCbufSlot());
  199. }
  200. return GetOperand(operand);
  201. }
  202. private AstOperand GetOperand(Operand operand)
  203. {
  204. if (operand == null)
  205. {
  206. return null;
  207. }
  208. if (operand.Type != OperandType.LocalVariable)
  209. {
  210. return new AstOperand(operand);
  211. }
  212. if (!_localsMap.TryGetValue(operand, out AstOperand astOperand))
  213. {
  214. astOperand = new AstOperand(operand);
  215. _localsMap.Add(operand, astOperand);
  216. Info.Locals.Add(astOperand);
  217. }
  218. return astOperand;
  219. }
  220. private static bool TryGetUserAttributeIndex(Operand operand, out int attrIndex)
  221. {
  222. if (operand.Type == OperandType.Attribute)
  223. {
  224. if (operand.Value >= AttributeConsts.UserAttributeBase &&
  225. operand.Value < AttributeConsts.UserAttributeEnd)
  226. {
  227. attrIndex = (operand.Value - AttributeConsts.UserAttributeBase) >> 4;
  228. return true;
  229. }
  230. else if (operand.Value >= AttributeConsts.FragmentOutputColorBase &&
  231. operand.Value < AttributeConsts.FragmentOutputColorEnd)
  232. {
  233. attrIndex = (operand.Value - AttributeConsts.FragmentOutputColorBase) >> 4;
  234. return true;
  235. }
  236. }
  237. attrIndex = 0;
  238. return false;
  239. }
  240. }
  241. }