StructuredProgram.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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(Function[] functions, ShaderConfig config)
  10. {
  11. StructuredProgramContext context = new StructuredProgramContext(config);
  12. for (int funcIndex = 0; funcIndex < functions.Length; funcIndex++)
  13. {
  14. Function function = functions[funcIndex];
  15. BasicBlock[] blocks = function.Blocks;
  16. VariableType returnType = function.ReturnsValue ? VariableType.S32 : VariableType.None;
  17. VariableType[] inArguments = new VariableType[function.InArgumentsCount];
  18. VariableType[] outArguments = new VariableType[function.OutArgumentsCount];
  19. for (int i = 0; i < inArguments.Length; i++)
  20. {
  21. inArguments[i] = VariableType.S32;
  22. }
  23. for (int i = 0; i < outArguments.Length; i++)
  24. {
  25. outArguments[i] = VariableType.S32;
  26. }
  27. context.EnterFunction(blocks.Length, function.Name, returnType, inArguments, outArguments);
  28. PhiFunctions.Remove(blocks);
  29. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  30. {
  31. BasicBlock block = blocks[blkIndex];
  32. context.EnterBlock(block);
  33. for (LinkedListNode<INode> opNode = block.Operations.First; opNode != null; opNode = opNode.Next)
  34. {
  35. Operation operation = (Operation)opNode.Value;
  36. if (IsBranchInst(operation.Inst))
  37. {
  38. context.LeaveBlock(block, operation);
  39. }
  40. else
  41. {
  42. AddOperation(context, operation);
  43. }
  44. }
  45. }
  46. GotoElimination.Eliminate(context.GetGotos());
  47. AstOptimizer.Optimize(context);
  48. context.LeaveFunction();
  49. }
  50. if (config.TransformFeedbackEnabled && config.LastInVertexPipeline)
  51. {
  52. for (int tfbIndex = 0; tfbIndex < 4; tfbIndex++)
  53. {
  54. var locations = config.GpuAccessor.QueryTransformFeedbackVaryingLocations(tfbIndex);
  55. var stride = config.GpuAccessor.QueryTransformFeedbackStride(tfbIndex);
  56. for (int i = 0; i < locations.Length; i++)
  57. {
  58. byte location = locations[i];
  59. if (location < 0xc0)
  60. {
  61. context.Info.TransformFeedbackOutputs[location] = new TransformFeedbackOutput(tfbIndex, i * 4, stride);
  62. }
  63. }
  64. }
  65. }
  66. return context.Info;
  67. }
  68. private static void AddOperation(StructuredProgramContext context, Operation operation)
  69. {
  70. Instruction inst = operation.Inst;
  71. if (inst == Instruction.LoadAttribute)
  72. {
  73. Operand src1 = operation.GetSource(0);
  74. Operand src2 = operation.GetSource(1);
  75. if (src1.Type == OperandType.Constant && src2.Type == OperandType.Constant)
  76. {
  77. int attrOffset = (src1.Value & AttributeConsts.Mask) + (src2.Value << 2);
  78. if ((src1.Value & AttributeConsts.LoadOutputMask) != 0)
  79. {
  80. context.Info.Outputs.Add(attrOffset);
  81. }
  82. else
  83. {
  84. context.Info.Inputs.Add(attrOffset);
  85. }
  86. }
  87. }
  88. int sourcesCount = operation.SourcesCount;
  89. int outDestsCount = operation.DestsCount != 0 ? operation.DestsCount - 1 : 0;
  90. IAstNode[] sources = new IAstNode[sourcesCount + outDestsCount];
  91. for (int index = 0; index < operation.SourcesCount; index++)
  92. {
  93. sources[index] = context.GetOperandUse(operation.GetSource(index));
  94. }
  95. for (int index = 0; index < outDestsCount; index++)
  96. {
  97. AstOperand oper = context.GetOperandDef(operation.GetDest(1 + index));
  98. oper.VarType = InstructionInfo.GetSrcVarType(inst, sourcesCount + index);
  99. sources[sourcesCount + index] = oper;
  100. }
  101. AstTextureOperation GetAstTextureOperation(TextureOperation texOp)
  102. {
  103. return new AstTextureOperation(
  104. inst,
  105. texOp.Type,
  106. texOp.Format,
  107. texOp.Flags,
  108. texOp.CbufSlot,
  109. texOp.Handle,
  110. texOp.Index,
  111. sources);
  112. }
  113. if (operation.Dest != null)
  114. {
  115. AstOperand dest = context.GetOperandDef(operation.Dest);
  116. // If all the sources are bool, it's better to use short-circuiting
  117. // logical operations, rather than forcing a cast to int and doing
  118. // a bitwise operation with the value, as it is likely to be used as
  119. // a bool in the end.
  120. if (IsBitwiseInst(inst) && AreAllSourceTypesEqual(sources, VariableType.Bool))
  121. {
  122. inst = GetLogicalFromBitwiseInst(inst);
  123. }
  124. bool isCondSel = inst == Instruction.ConditionalSelect;
  125. bool isCopy = inst == Instruction.Copy;
  126. if (isCondSel || isCopy)
  127. {
  128. VariableType type = GetVarTypeFromUses(operation.Dest);
  129. if (isCondSel && type == VariableType.F32)
  130. {
  131. inst |= Instruction.FP32;
  132. }
  133. dest.VarType = type;
  134. }
  135. else
  136. {
  137. dest.VarType = InstructionInfo.GetDestVarType(inst);
  138. }
  139. IAstNode source;
  140. if (operation is TextureOperation texOp)
  141. {
  142. if (texOp.Inst == Instruction.ImageLoad)
  143. {
  144. dest.VarType = texOp.Format.GetComponentType();
  145. }
  146. source = GetAstTextureOperation(texOp);
  147. }
  148. else if (!isCopy)
  149. {
  150. source = new AstOperation(inst, operation.Index, sources, operation.SourcesCount);
  151. }
  152. else
  153. {
  154. source = sources[0];
  155. }
  156. context.AddNode(new AstAssignment(dest, source));
  157. }
  158. else if (operation.Inst == Instruction.Comment)
  159. {
  160. context.AddNode(new AstComment(((CommentNode)operation).Comment));
  161. }
  162. else if (operation is TextureOperation texOp)
  163. {
  164. AstTextureOperation astTexOp = GetAstTextureOperation(texOp);
  165. context.AddNode(astTexOp);
  166. }
  167. else
  168. {
  169. context.AddNode(new AstOperation(inst, operation.Index, sources, operation.SourcesCount));
  170. }
  171. // Those instructions needs to be emulated by using helper functions,
  172. // because they are NVIDIA specific. Those flags helps the backend to
  173. // decide which helper functions are needed on the final generated code.
  174. switch (operation.Inst)
  175. {
  176. case Instruction.AtomicMaxS32 | Instruction.MrShared:
  177. case Instruction.AtomicMinS32 | Instruction.MrShared:
  178. context.Info.HelperFunctionsMask |= HelperFunctionsMask.AtomicMinMaxS32Shared;
  179. break;
  180. case Instruction.AtomicMaxS32 | Instruction.MrStorage:
  181. case Instruction.AtomicMinS32 | Instruction.MrStorage:
  182. context.Info.HelperFunctionsMask |= HelperFunctionsMask.AtomicMinMaxS32Storage;
  183. break;
  184. case Instruction.MultiplyHighS32:
  185. context.Info.HelperFunctionsMask |= HelperFunctionsMask.MultiplyHighS32;
  186. break;
  187. case Instruction.MultiplyHighU32:
  188. context.Info.HelperFunctionsMask |= HelperFunctionsMask.MultiplyHighU32;
  189. break;
  190. case Instruction.Shuffle:
  191. context.Info.HelperFunctionsMask |= HelperFunctionsMask.Shuffle;
  192. break;
  193. case Instruction.ShuffleDown:
  194. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleDown;
  195. break;
  196. case Instruction.ShuffleUp:
  197. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleUp;
  198. break;
  199. case Instruction.ShuffleXor:
  200. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleXor;
  201. break;
  202. case Instruction.StoreShared16:
  203. case Instruction.StoreShared8:
  204. context.Info.HelperFunctionsMask |= HelperFunctionsMask.StoreSharedSmallInt;
  205. break;
  206. case Instruction.StoreStorage16:
  207. case Instruction.StoreStorage8:
  208. context.Info.HelperFunctionsMask |= HelperFunctionsMask.StoreStorageSmallInt;
  209. break;
  210. case Instruction.SwizzleAdd:
  211. context.Info.HelperFunctionsMask |= HelperFunctionsMask.SwizzleAdd;
  212. break;
  213. case Instruction.FSIBegin:
  214. case Instruction.FSIEnd:
  215. context.Info.HelperFunctionsMask |= HelperFunctionsMask.FSI;
  216. break;
  217. }
  218. }
  219. private static VariableType GetVarTypeFromUses(Operand dest)
  220. {
  221. HashSet<Operand> visited = new HashSet<Operand>();
  222. Queue<Operand> pending = new Queue<Operand>();
  223. bool Enqueue(Operand operand)
  224. {
  225. if (visited.Add(operand))
  226. {
  227. pending.Enqueue(operand);
  228. return true;
  229. }
  230. return false;
  231. }
  232. Enqueue(dest);
  233. while (pending.TryDequeue(out Operand operand))
  234. {
  235. foreach (INode useNode in operand.UseOps)
  236. {
  237. if (useNode is not Operation operation)
  238. {
  239. continue;
  240. }
  241. if (operation.Inst == Instruction.Copy)
  242. {
  243. if (operation.Dest.Type == OperandType.LocalVariable)
  244. {
  245. if (Enqueue(operation.Dest))
  246. {
  247. break;
  248. }
  249. }
  250. else
  251. {
  252. return OperandInfo.GetVarType(operation.Dest.Type);
  253. }
  254. }
  255. else
  256. {
  257. for (int index = 0; index < operation.SourcesCount; index++)
  258. {
  259. if (operation.GetSource(index) == operand)
  260. {
  261. return InstructionInfo.GetSrcVarType(operation.Inst, index);
  262. }
  263. }
  264. }
  265. }
  266. }
  267. return VariableType.S32;
  268. }
  269. private static bool AreAllSourceTypesEqual(IAstNode[] sources, VariableType type)
  270. {
  271. foreach (IAstNode node in sources)
  272. {
  273. if (node is not AstOperand operand)
  274. {
  275. return false;
  276. }
  277. if (operand.VarType != type)
  278. {
  279. return false;
  280. }
  281. }
  282. return true;
  283. }
  284. private static bool IsBranchInst(Instruction inst)
  285. {
  286. return inst switch
  287. {
  288. Instruction.Branch or
  289. Instruction.BranchIfFalse or
  290. Instruction.BranchIfTrue => true,
  291. _ => false
  292. };
  293. }
  294. private static bool IsBitwiseInst(Instruction inst)
  295. {
  296. return inst switch
  297. {
  298. Instruction.BitwiseAnd or
  299. Instruction.BitwiseExclusiveOr or
  300. Instruction.BitwiseNot or
  301. Instruction.BitwiseOr => true,
  302. _ => false
  303. };
  304. }
  305. private static Instruction GetLogicalFromBitwiseInst(Instruction inst)
  306. {
  307. return inst switch
  308. {
  309. Instruction.BitwiseAnd => Instruction.LogicalAnd,
  310. Instruction.BitwiseExclusiveOr => Instruction.LogicalExclusiveOr,
  311. Instruction.BitwiseNot => Instruction.LogicalNot,
  312. Instruction.BitwiseOr => Instruction.LogicalOr,
  313. _ => throw new ArgumentException($"Unexpected instruction \"{inst}\".")
  314. };
  315. }
  316. }
  317. }