StructuredProgramContext.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 EndIndex)> _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. public StructuredProgramInfo Info { get; }
  18. public ShaderConfig Config { get; }
  19. public StructuredProgramContext(int blocksCount, ShaderConfig config)
  20. {
  21. _loopTails = new HashSet<BasicBlock>();
  22. _blockStack = new Stack<(AstBlock, int)>();
  23. _localsMap = new Dictionary<Operand, AstOperand>();
  24. _gotoTempAsgs = new Dictionary<int, AstAssignment>();
  25. _gotos = new List<GotoStatement>();
  26. _currBlock = new AstBlock(AstBlockType.Main);
  27. _currEndIndex = blocksCount;
  28. Info = new StructuredProgramInfo(_currBlock);
  29. Config = config;
  30. }
  31. public void EnterBlock(BasicBlock block)
  32. {
  33. while (_currEndIndex == block.Index)
  34. {
  35. (_currBlock, _currEndIndex) = _blockStack.Pop();
  36. }
  37. if (_gotoTempAsgs.TryGetValue(block.Index, out AstAssignment gotoTempAsg))
  38. {
  39. AddGotoTempReset(block, gotoTempAsg);
  40. }
  41. LookForDoWhileStatements(block);
  42. }
  43. public void LeaveBlock(BasicBlock block, Operation branchOp)
  44. {
  45. LookForIfStatements(block, branchOp);
  46. }
  47. private void LookForDoWhileStatements(BasicBlock block)
  48. {
  49. // Check if we have any predecessor whose index is greater than the
  50. // current block, this indicates a loop.
  51. bool done = false;
  52. foreach (BasicBlock predecessor in block.Predecessors.OrderByDescending(x => x.Index))
  53. {
  54. if (predecessor.Index < block.Index)
  55. {
  56. break;
  57. }
  58. if (predecessor.Index < _currEndIndex && !done)
  59. {
  60. Operation branchOp = (Operation)predecessor.GetLastOp();
  61. NewBlock(AstBlockType.DoWhile, branchOp, predecessor.Index + 1);
  62. _loopTails.Add(predecessor);
  63. done = true;
  64. }
  65. else
  66. {
  67. AddGotoTempReset(block, GetGotoTempAsg(block.Index));
  68. break;
  69. }
  70. }
  71. }
  72. private void LookForIfStatements(BasicBlock block, Operation branchOp)
  73. {
  74. if (block.Branch == null)
  75. {
  76. return;
  77. }
  78. bool isLoop = block.Branch.Index <= block.Index;
  79. if (block.Branch.Index <= _currEndIndex && !isLoop)
  80. {
  81. NewBlock(AstBlockType.If, branchOp, block.Branch.Index);
  82. }
  83. else if (!_loopTails.Contains(block))
  84. {
  85. AstAssignment gotoTempAsg = GetGotoTempAsg(block.Branch.Index);
  86. IAstNode cond = GetBranchCond(AstBlockType.DoWhile, branchOp);
  87. AddNode(Assign(gotoTempAsg.Destination, cond));
  88. AstOperation branch = new AstOperation(branchOp.Inst);
  89. AddNode(branch);
  90. GotoStatement gotoStmt = new GotoStatement(branch, gotoTempAsg, isLoop);
  91. _gotos.Add(gotoStmt);
  92. }
  93. }
  94. private AstAssignment GetGotoTempAsg(int index)
  95. {
  96. if (_gotoTempAsgs.TryGetValue(index, out AstAssignment gotoTempAsg))
  97. {
  98. return gotoTempAsg;
  99. }
  100. AstOperand gotoTemp = NewTemp(VariableType.Bool);
  101. gotoTempAsg = Assign(gotoTemp, Const(IrConsts.False));
  102. _gotoTempAsgs.Add(index, gotoTempAsg);
  103. return gotoTempAsg;
  104. }
  105. private void AddGotoTempReset(BasicBlock block, AstAssignment gotoTempAsg)
  106. {
  107. AddNode(gotoTempAsg);
  108. // For block 0, we don't need to add the extra "reset" at the beginning,
  109. // because it is already the first node to be executed on the shader,
  110. // so it is reset to false by the "local" assignment anyway.
  111. if (block.Index != 0)
  112. {
  113. Info.MainBlock.AddFirst(Assign(gotoTempAsg.Destination, Const(IrConsts.False)));
  114. }
  115. }
  116. private void NewBlock(AstBlockType type, Operation branchOp, int endIndex)
  117. {
  118. NewBlock(type, GetBranchCond(type, branchOp), endIndex);
  119. }
  120. private void NewBlock(AstBlockType type, IAstNode cond, int endIndex)
  121. {
  122. AstBlock childBlock = new AstBlock(type, cond);
  123. AddNode(childBlock);
  124. _blockStack.Push((_currBlock, _currEndIndex));
  125. _currBlock = childBlock;
  126. _currEndIndex = endIndex;
  127. }
  128. private IAstNode GetBranchCond(AstBlockType type, Operation branchOp)
  129. {
  130. IAstNode cond;
  131. if (branchOp.Inst == Instruction.Branch)
  132. {
  133. cond = Const(type == AstBlockType.If ? IrConsts.False : IrConsts.True);
  134. }
  135. else
  136. {
  137. cond = GetOperandUse(branchOp.GetSource(0));
  138. Instruction invInst = type == AstBlockType.If
  139. ? Instruction.BranchIfTrue
  140. : Instruction.BranchIfFalse;
  141. if (branchOp.Inst == invInst)
  142. {
  143. cond = new AstOperation(Instruction.LogicalNot, cond);
  144. }
  145. }
  146. return cond;
  147. }
  148. public void AddNode(IAstNode node)
  149. {
  150. _currBlock.Add(node);
  151. }
  152. public GotoStatement[] GetGotos()
  153. {
  154. return _gotos.ToArray();
  155. }
  156. private AstOperand NewTemp(VariableType type)
  157. {
  158. AstOperand newTemp = Local(type);
  159. Info.Locals.Add(newTemp);
  160. return newTemp;
  161. }
  162. public AstOperand GetOperandDef(Operand operand)
  163. {
  164. if (TryGetUserAttributeIndex(operand, out int attrIndex))
  165. {
  166. Info.OAttributes.Add(attrIndex);
  167. }
  168. return GetOperand(operand);
  169. }
  170. public AstOperand GetOperandUse(Operand operand)
  171. {
  172. if (TryGetUserAttributeIndex(operand, out int attrIndex))
  173. {
  174. Info.IAttributes.Add(attrIndex);
  175. Info.InterpolationQualifiers[attrIndex] = operand.Interpolation;
  176. }
  177. else if (operand.Type == OperandType.Attribute && operand.Value == AttributeConsts.InstanceId)
  178. {
  179. Info.UsesInstanceId = true;
  180. }
  181. else if (operand.Type == OperandType.ConstantBuffer)
  182. {
  183. Info.CBuffers.Add(operand.GetCbufSlot());
  184. }
  185. return GetOperand(operand);
  186. }
  187. private AstOperand GetOperand(Operand operand)
  188. {
  189. if (operand == null)
  190. {
  191. return null;
  192. }
  193. if (operand.Type != OperandType.LocalVariable)
  194. {
  195. return new AstOperand(operand);
  196. }
  197. if (!_localsMap.TryGetValue(operand, out AstOperand astOperand))
  198. {
  199. astOperand = new AstOperand(operand);
  200. _localsMap.Add(operand, astOperand);
  201. Info.Locals.Add(astOperand);
  202. }
  203. return astOperand;
  204. }
  205. private static bool TryGetUserAttributeIndex(Operand operand, out int attrIndex)
  206. {
  207. if (operand.Type == OperandType.Attribute)
  208. {
  209. if (operand.Value >= AttributeConsts.UserAttributeBase &&
  210. operand.Value < AttributeConsts.UserAttributeEnd)
  211. {
  212. attrIndex = (operand.Value - AttributeConsts.UserAttributeBase) >> 4;
  213. return true;
  214. }
  215. else if (operand.Value >= AttributeConsts.FragmentOutputColorBase &&
  216. operand.Value < AttributeConsts.FragmentOutputColorEnd)
  217. {
  218. attrIndex = (operand.Value - AttributeConsts.FragmentOutputColorBase) >> 4;
  219. return true;
  220. }
  221. }
  222. attrIndex = 0;
  223. return false;
  224. }
  225. }
  226. }