StructuredProgram.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 j = 0; j < locations.Length; j++)
  57. {
  58. byte location = locations[j];
  59. if (location < 0xc0)
  60. {
  61. context.Info.TransformFeedbackOutputs[location] = new TransformFeedbackOutput(tfbIndex, j * 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. context.Info.Inputs.Add(attrOffset);
  79. }
  80. }
  81. int sourcesCount = operation.SourcesCount;
  82. int outDestsCount = operation.DestsCount != 0 ? operation.DestsCount - 1 : 0;
  83. IAstNode[] sources = new IAstNode[sourcesCount + outDestsCount];
  84. for (int index = 0; index < operation.SourcesCount; index++)
  85. {
  86. sources[index] = context.GetOperandUse(operation.GetSource(index));
  87. }
  88. for (int index = 0; index < outDestsCount; index++)
  89. {
  90. AstOperand oper = context.GetOperandDef(operation.GetDest(1 + index));
  91. oper.VarType = InstructionInfo.GetSrcVarType(inst, sourcesCount + index);
  92. sources[sourcesCount + index] = oper;
  93. }
  94. AstTextureOperation GetAstTextureOperation(TextureOperation texOp)
  95. {
  96. return new AstTextureOperation(
  97. inst,
  98. texOp.Type,
  99. texOp.Format,
  100. texOp.Flags,
  101. texOp.CbufSlot,
  102. texOp.Handle,
  103. texOp.Index,
  104. sources);
  105. }
  106. if (operation.Dest != null)
  107. {
  108. AstOperand dest = context.GetOperandDef(operation.Dest);
  109. // If all the sources are bool, it's better to use short-circuiting
  110. // logical operations, rather than forcing a cast to int and doing
  111. // a bitwise operation with the value, as it is likely to be used as
  112. // a bool in the end.
  113. if (IsBitwiseInst(inst) && AreAllSourceTypesEqual(sources, VariableType.Bool))
  114. {
  115. inst = GetLogicalFromBitwiseInst(inst);
  116. }
  117. bool isCondSel = inst == Instruction.ConditionalSelect;
  118. bool isCopy = inst == Instruction.Copy;
  119. if (isCondSel || isCopy)
  120. {
  121. VariableType type = GetVarTypeFromUses(operation.Dest);
  122. if (isCondSel && type == VariableType.F32)
  123. {
  124. inst |= Instruction.FP32;
  125. }
  126. dest.VarType = type;
  127. }
  128. else
  129. {
  130. dest.VarType = InstructionInfo.GetDestVarType(inst);
  131. }
  132. IAstNode source;
  133. if (operation is TextureOperation texOp)
  134. {
  135. if (texOp.Inst == Instruction.ImageLoad)
  136. {
  137. dest.VarType = texOp.Format.GetComponentType();
  138. }
  139. source = GetAstTextureOperation(texOp);
  140. }
  141. else if (!isCopy)
  142. {
  143. source = new AstOperation(inst, operation.Index, sources, operation.SourcesCount);
  144. }
  145. else
  146. {
  147. source = sources[0];
  148. }
  149. context.AddNode(new AstAssignment(dest, source));
  150. }
  151. else if (operation.Inst == Instruction.Comment)
  152. {
  153. context.AddNode(new AstComment(((CommentNode)operation).Comment));
  154. }
  155. else if (operation is TextureOperation texOp)
  156. {
  157. AstTextureOperation astTexOp = GetAstTextureOperation(texOp);
  158. context.AddNode(astTexOp);
  159. }
  160. else
  161. {
  162. context.AddNode(new AstOperation(inst, operation.Index, sources, operation.SourcesCount));
  163. }
  164. // Those instructions needs to be emulated by using helper functions,
  165. // because they are NVIDIA specific. Those flags helps the backend to
  166. // decide which helper functions are needed on the final generated code.
  167. switch (operation.Inst)
  168. {
  169. case Instruction.AtomicMaxS32 | Instruction.MrShared:
  170. case Instruction.AtomicMinS32 | Instruction.MrShared:
  171. context.Info.HelperFunctionsMask |= HelperFunctionsMask.AtomicMinMaxS32Shared;
  172. break;
  173. case Instruction.AtomicMaxS32 | Instruction.MrStorage:
  174. case Instruction.AtomicMinS32 | Instruction.MrStorage:
  175. context.Info.HelperFunctionsMask |= HelperFunctionsMask.AtomicMinMaxS32Storage;
  176. break;
  177. case Instruction.MultiplyHighS32:
  178. context.Info.HelperFunctionsMask |= HelperFunctionsMask.MultiplyHighS32;
  179. break;
  180. case Instruction.MultiplyHighU32:
  181. context.Info.HelperFunctionsMask |= HelperFunctionsMask.MultiplyHighU32;
  182. break;
  183. case Instruction.Shuffle:
  184. context.Info.HelperFunctionsMask |= HelperFunctionsMask.Shuffle;
  185. break;
  186. case Instruction.ShuffleDown:
  187. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleDown;
  188. break;
  189. case Instruction.ShuffleUp:
  190. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleUp;
  191. break;
  192. case Instruction.ShuffleXor:
  193. context.Info.HelperFunctionsMask |= HelperFunctionsMask.ShuffleXor;
  194. break;
  195. case Instruction.StoreShared16:
  196. case Instruction.StoreShared8:
  197. context.Info.HelperFunctionsMask |= HelperFunctionsMask.StoreSharedSmallInt;
  198. break;
  199. case Instruction.StoreStorage16:
  200. case Instruction.StoreStorage8:
  201. context.Info.HelperFunctionsMask |= HelperFunctionsMask.StoreStorageSmallInt;
  202. break;
  203. case Instruction.SwizzleAdd:
  204. context.Info.HelperFunctionsMask |= HelperFunctionsMask.SwizzleAdd;
  205. break;
  206. case Instruction.FSIBegin:
  207. case Instruction.FSIEnd:
  208. context.Info.HelperFunctionsMask |= HelperFunctionsMask.FSI;
  209. break;
  210. }
  211. }
  212. private static VariableType GetVarTypeFromUses(Operand dest)
  213. {
  214. HashSet<Operand> visited = new HashSet<Operand>();
  215. Queue<Operand> pending = new Queue<Operand>();
  216. bool Enqueue(Operand operand)
  217. {
  218. if (visited.Add(operand))
  219. {
  220. pending.Enqueue(operand);
  221. return true;
  222. }
  223. return false;
  224. }
  225. Enqueue(dest);
  226. while (pending.TryDequeue(out Operand operand))
  227. {
  228. foreach (INode useNode in operand.UseOps)
  229. {
  230. if (useNode is not Operation operation)
  231. {
  232. continue;
  233. }
  234. if (operation.Inst == Instruction.Copy)
  235. {
  236. if (operation.Dest.Type == OperandType.LocalVariable)
  237. {
  238. if (Enqueue(operation.Dest))
  239. {
  240. break;
  241. }
  242. }
  243. else
  244. {
  245. return OperandInfo.GetVarType(operation.Dest.Type);
  246. }
  247. }
  248. else
  249. {
  250. for (int index = 0; index < operation.SourcesCount; index++)
  251. {
  252. if (operation.GetSource(index) == operand)
  253. {
  254. return InstructionInfo.GetSrcVarType(operation.Inst, index);
  255. }
  256. }
  257. }
  258. }
  259. }
  260. return VariableType.S32;
  261. }
  262. private static bool AreAllSourceTypesEqual(IAstNode[] sources, VariableType type)
  263. {
  264. foreach (IAstNode node in sources)
  265. {
  266. if (node is not AstOperand operand)
  267. {
  268. return false;
  269. }
  270. if (operand.VarType != type)
  271. {
  272. return false;
  273. }
  274. }
  275. return true;
  276. }
  277. private static bool IsBranchInst(Instruction inst)
  278. {
  279. return inst switch
  280. {
  281. Instruction.Branch or
  282. Instruction.BranchIfFalse or
  283. Instruction.BranchIfTrue => true,
  284. _ => false
  285. };
  286. }
  287. private static bool IsBitwiseInst(Instruction inst)
  288. {
  289. return inst switch
  290. {
  291. Instruction.BitwiseAnd or
  292. Instruction.BitwiseExclusiveOr or
  293. Instruction.BitwiseNot or
  294. Instruction.BitwiseOr => true,
  295. _ => false
  296. };
  297. }
  298. private static Instruction GetLogicalFromBitwiseInst(Instruction inst)
  299. {
  300. return inst switch
  301. {
  302. Instruction.BitwiseAnd => Instruction.LogicalAnd,
  303. Instruction.BitwiseExclusiveOr => Instruction.LogicalExclusiveOr,
  304. Instruction.BitwiseNot => Instruction.LogicalNot,
  305. Instruction.BitwiseOr => Instruction.LogicalOr,
  306. _ => throw new ArgumentException($"Unexpected instruction \"{inst}\".")
  307. };
  308. }
  309. }
  310. }