GlslDecl.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Ryujinx.Graphics.Gal.Shader
  4. {
  5. class GlslDecl
  6. {
  7. public const int LayerAttr = 0x064;
  8. public const int PointSizeAttr = 0x06c;
  9. public const int PointCoordAttrX = 0x2e0;
  10. public const int PointCoordAttrY = 0x2e4;
  11. public const int TessCoordAttrX = 0x2f0;
  12. public const int TessCoordAttrY = 0x2f4;
  13. public const int TessCoordAttrZ = 0x2f8;
  14. public const int InstanceIdAttr = 0x2f8;
  15. public const int VertexIdAttr = 0x2fc;
  16. public const int FaceAttr = 0x3fc;
  17. public const int GlPositionVec4Index = 7;
  18. public const int PositionOutAttrLocation = 15;
  19. private const int AttrStartIndex = 8;
  20. private const int TexStartIndex = 8;
  21. public const string PositionOutAttrName = "position";
  22. private const string TextureName = "tex";
  23. private const string UniformName = "c";
  24. private const string AttrName = "attr";
  25. private const string InAttrName = "in_" + AttrName;
  26. private const string OutAttrName = "out_" + AttrName;
  27. private const string GprName = "gpr";
  28. private const string PredName = "pred";
  29. public const string FragmentOutputName = "FragColor";
  30. public const string ExtraUniformBlockName = "Extra";
  31. public const string FlipUniformName = "flip";
  32. public const string InstanceUniformName = "instance";
  33. public const string BasicBlockName = "bb";
  34. public const string BasicBlockAName = BasicBlockName + "_a";
  35. public const string BasicBlockBName = BasicBlockName + "_b";
  36. public const int SsyStackSize = 16;
  37. public const string SsyStackName = "ssy_stack";
  38. public const string SsyCursorName = "ssy_cursor";
  39. private string[] StagePrefixes = new string[] { "vp", "tcp", "tep", "gp", "fp" };
  40. private string StagePrefix;
  41. private Dictionary<ShaderIrOp, ShaderDeclInfo> m_CbTextures;
  42. private Dictionary<int, ShaderDeclInfo> m_Textures;
  43. private Dictionary<int, ShaderDeclInfo> m_Uniforms;
  44. private Dictionary<int, ShaderDeclInfo> m_Attributes;
  45. private Dictionary<int, ShaderDeclInfo> m_InAttributes;
  46. private Dictionary<int, ShaderDeclInfo> m_OutAttributes;
  47. private Dictionary<int, ShaderDeclInfo> m_Gprs;
  48. private Dictionary<int, ShaderDeclInfo> m_GprsHalf;
  49. private Dictionary<int, ShaderDeclInfo> m_Preds;
  50. public IReadOnlyDictionary<ShaderIrOp, ShaderDeclInfo> CbTextures => m_CbTextures;
  51. public IReadOnlyDictionary<int, ShaderDeclInfo> Textures => m_Textures;
  52. public IReadOnlyDictionary<int, ShaderDeclInfo> Uniforms => m_Uniforms;
  53. public IReadOnlyDictionary<int, ShaderDeclInfo> Attributes => m_Attributes;
  54. public IReadOnlyDictionary<int, ShaderDeclInfo> InAttributes => m_InAttributes;
  55. public IReadOnlyDictionary<int, ShaderDeclInfo> OutAttributes => m_OutAttributes;
  56. public IReadOnlyDictionary<int, ShaderDeclInfo> Gprs => m_Gprs;
  57. public IReadOnlyDictionary<int, ShaderDeclInfo> GprsHalf => m_GprsHalf;
  58. public IReadOnlyDictionary<int, ShaderDeclInfo> Preds => m_Preds;
  59. public GalShaderType ShaderType { get; private set; }
  60. private GlslDecl(GalShaderType ShaderType)
  61. {
  62. this.ShaderType = ShaderType;
  63. m_CbTextures = new Dictionary<ShaderIrOp, ShaderDeclInfo>();
  64. m_Textures = new Dictionary<int, ShaderDeclInfo>();
  65. m_Uniforms = new Dictionary<int, ShaderDeclInfo>();
  66. m_Attributes = new Dictionary<int, ShaderDeclInfo>();
  67. m_InAttributes = new Dictionary<int, ShaderDeclInfo>();
  68. m_OutAttributes = new Dictionary<int, ShaderDeclInfo>();
  69. m_Gprs = new Dictionary<int, ShaderDeclInfo>();
  70. m_GprsHalf = new Dictionary<int, ShaderDeclInfo>();
  71. m_Preds = new Dictionary<int, ShaderDeclInfo>();
  72. }
  73. public GlslDecl(ShaderIrBlock[] Blocks, GalShaderType ShaderType, ShaderHeader Header) : this(ShaderType)
  74. {
  75. StagePrefix = StagePrefixes[(int)ShaderType] + "_";
  76. if (ShaderType == GalShaderType.Fragment)
  77. {
  78. int Index = 0;
  79. for (int Attachment = 0; Attachment < 8; Attachment++)
  80. {
  81. for (int Component = 0; Component < 4; Component++)
  82. {
  83. if (Header.OmapTargets[Attachment].ComponentEnabled(Component))
  84. {
  85. m_Gprs.TryAdd(Index, new ShaderDeclInfo(GetGprName(Index), Index));
  86. Index++;
  87. }
  88. }
  89. }
  90. if (Header.OmapDepth)
  91. {
  92. Index = Header.DepthRegister;
  93. m_Gprs.TryAdd(Index, new ShaderDeclInfo(GetGprName(Index), Index));
  94. }
  95. }
  96. foreach (ShaderIrBlock Block in Blocks)
  97. {
  98. ShaderIrNode[] Nodes = Block.GetNodes();
  99. foreach (ShaderIrNode Node in Nodes)
  100. {
  101. Traverse(Nodes, null, Node);
  102. }
  103. }
  104. }
  105. public static GlslDecl Merge(GlslDecl VpA, GlslDecl VpB)
  106. {
  107. GlslDecl Combined = new GlslDecl(GalShaderType.Vertex);
  108. Merge(Combined.m_Textures, VpA.m_Textures, VpB.m_Textures);
  109. Merge(Combined.m_Uniforms, VpA.m_Uniforms, VpB.m_Uniforms);
  110. Merge(Combined.m_Attributes, VpA.m_Attributes, VpB.m_Attributes);
  111. Merge(Combined.m_OutAttributes, VpA.m_OutAttributes, VpB.m_OutAttributes);
  112. Merge(Combined.m_Gprs, VpA.m_Gprs, VpB.m_Gprs);
  113. Merge(Combined.m_GprsHalf, VpA.m_GprsHalf, VpB.m_GprsHalf);
  114. Merge(Combined.m_Preds, VpA.m_Preds, VpB.m_Preds);
  115. //Merge input attributes.
  116. foreach (KeyValuePair<int, ShaderDeclInfo> KV in VpA.m_InAttributes)
  117. {
  118. Combined.m_InAttributes.TryAdd(KV.Key, KV.Value);
  119. }
  120. foreach (KeyValuePair<int, ShaderDeclInfo> KV in VpB.m_InAttributes)
  121. {
  122. //If Vertex Program A already writes to this attribute,
  123. //then we don't need to add it as an input attribute since
  124. //Vertex Program A will already have written to it anyway,
  125. //and there's no guarantee that there is an input attribute
  126. //for this slot.
  127. if (!VpA.m_OutAttributes.ContainsKey(KV.Key))
  128. {
  129. Combined.m_InAttributes.TryAdd(KV.Key, KV.Value);
  130. }
  131. }
  132. return Combined;
  133. }
  134. public static string GetGprName(int Index)
  135. {
  136. return GprName + Index;
  137. }
  138. private static void Merge(
  139. Dictionary<int, ShaderDeclInfo> C,
  140. Dictionary<int, ShaderDeclInfo> A,
  141. Dictionary<int, ShaderDeclInfo> B)
  142. {
  143. foreach (KeyValuePair<int, ShaderDeclInfo> KV in A)
  144. {
  145. C.TryAdd(KV.Key, KV.Value);
  146. }
  147. foreach (KeyValuePair<int, ShaderDeclInfo> KV in B)
  148. {
  149. C.TryAdd(KV.Key, KV.Value);
  150. }
  151. }
  152. private void Traverse(ShaderIrNode[] Nodes, ShaderIrNode Parent, ShaderIrNode Node)
  153. {
  154. switch (Node)
  155. {
  156. case ShaderIrAsg Asg:
  157. {
  158. Traverse(Nodes, Asg, Asg.Dst);
  159. Traverse(Nodes, Asg, Asg.Src);
  160. break;
  161. }
  162. case ShaderIrCond Cond:
  163. {
  164. Traverse(Nodes, Cond, Cond.Pred);
  165. Traverse(Nodes, Cond, Cond.Child);
  166. break;
  167. }
  168. case ShaderIrOp Op:
  169. {
  170. Traverse(Nodes, Op, Op.OperandA);
  171. Traverse(Nodes, Op, Op.OperandB);
  172. Traverse(Nodes, Op, Op.OperandC);
  173. if (Op.Inst == ShaderIrInst.Texq ||
  174. Op.Inst == ShaderIrInst.Texs ||
  175. Op.Inst == ShaderIrInst.Txlf)
  176. {
  177. int Handle = ((ShaderIrOperImm)Op.OperandC).Value;
  178. int Index = Handle - TexStartIndex;
  179. string Name = StagePrefix + TextureName + Index;
  180. m_Textures.TryAdd(Handle, new ShaderDeclInfo(Name, Handle));
  181. }
  182. else if (Op.Inst == ShaderIrInst.Texb)
  183. {
  184. ShaderIrNode HandleSrc = null;
  185. int Index = Array.IndexOf(Nodes, Parent) - 1;
  186. for (; Index >= 0; Index--)
  187. {
  188. ShaderIrNode Curr = Nodes[Index];
  189. if (Curr is ShaderIrAsg Asg && Asg.Dst is ShaderIrOperGpr Gpr)
  190. {
  191. if (Gpr.Index == ((ShaderIrOperGpr)Op.OperandC).Index)
  192. {
  193. HandleSrc = Asg.Src;
  194. break;
  195. }
  196. }
  197. }
  198. if (HandleSrc != null && HandleSrc is ShaderIrOperCbuf Cbuf)
  199. {
  200. string Name = StagePrefix + TextureName + "_cb" + Cbuf.Index + "_" + Cbuf.Pos;
  201. m_CbTextures.Add(Op, new ShaderDeclInfo(Name, Cbuf.Pos, true, Cbuf.Index));
  202. }
  203. else
  204. {
  205. throw new NotImplementedException("Shader TEX.B instruction is not fully supported!");
  206. }
  207. }
  208. break;
  209. }
  210. case ShaderIrOperCbuf Cbuf:
  211. {
  212. if (!m_Uniforms.ContainsKey(Cbuf.Index))
  213. {
  214. string Name = StagePrefix + UniformName + Cbuf.Index;
  215. ShaderDeclInfo DeclInfo = new ShaderDeclInfo(Name, Cbuf.Pos, true, Cbuf.Index);
  216. m_Uniforms.Add(Cbuf.Index, DeclInfo);
  217. }
  218. break;
  219. }
  220. case ShaderIrOperAbuf Abuf:
  221. {
  222. //This is a built-in variable.
  223. if (Abuf.Offs == LayerAttr ||
  224. Abuf.Offs == PointSizeAttr ||
  225. Abuf.Offs == PointCoordAttrX ||
  226. Abuf.Offs == PointCoordAttrY ||
  227. Abuf.Offs == VertexIdAttr ||
  228. Abuf.Offs == InstanceIdAttr ||
  229. Abuf.Offs == FaceAttr)
  230. {
  231. break;
  232. }
  233. int Index = Abuf.Offs >> 4;
  234. int Elem = (Abuf.Offs >> 2) & 3;
  235. int GlslIndex = Index - AttrStartIndex;
  236. if (GlslIndex < 0)
  237. {
  238. return;
  239. }
  240. ShaderDeclInfo DeclInfo;
  241. if (Parent is ShaderIrAsg Asg && Asg.Dst == Node)
  242. {
  243. if (!m_OutAttributes.TryGetValue(Index, out DeclInfo))
  244. {
  245. DeclInfo = new ShaderDeclInfo(OutAttrName + GlslIndex, GlslIndex);
  246. m_OutAttributes.Add(Index, DeclInfo);
  247. }
  248. }
  249. else
  250. {
  251. if (!m_InAttributes.TryGetValue(Index, out DeclInfo))
  252. {
  253. DeclInfo = new ShaderDeclInfo(InAttrName + GlslIndex, GlslIndex);
  254. m_InAttributes.Add(Index, DeclInfo);
  255. }
  256. }
  257. DeclInfo.Enlarge(Elem + 1);
  258. if (!m_Attributes.ContainsKey(Index))
  259. {
  260. DeclInfo = new ShaderDeclInfo(AttrName + GlslIndex, GlslIndex, false, 0, 4);
  261. m_Attributes.Add(Index, DeclInfo);
  262. }
  263. Traverse(Nodes, Abuf, Abuf.Vertex);
  264. break;
  265. }
  266. case ShaderIrOperGpr Gpr:
  267. {
  268. if (!Gpr.IsConst)
  269. {
  270. string Name = GetGprName(Gpr.Index);
  271. if (Gpr.RegisterSize == ShaderRegisterSize.Single)
  272. {
  273. m_Gprs.TryAdd(Gpr.Index, new ShaderDeclInfo(Name, Gpr.Index));
  274. }
  275. else if (Gpr.RegisterSize == ShaderRegisterSize.Half)
  276. {
  277. Name += "_h" + Gpr.HalfPart;
  278. m_GprsHalf.TryAdd((Gpr.Index << 1) | Gpr.HalfPart, new ShaderDeclInfo(Name, Gpr.Index));
  279. }
  280. else /* if (Gpr.RegisterSize == ShaderRegisterSize.Double) */
  281. {
  282. throw new NotImplementedException("Double types are not supported.");
  283. }
  284. }
  285. break;
  286. }
  287. case ShaderIrOperPred Pred:
  288. {
  289. if (!Pred.IsConst && !HasName(m_Preds, Pred.Index))
  290. {
  291. string Name = PredName + Pred.Index;
  292. m_Preds.TryAdd(Pred.Index, new ShaderDeclInfo(Name, Pred.Index));
  293. }
  294. break;
  295. }
  296. }
  297. }
  298. private bool HasName(Dictionary<int, ShaderDeclInfo> Decls, int Index)
  299. {
  300. //This is used to check if the dictionary already contains
  301. //a entry for a vector at a given index position.
  302. //Used to enable turning gprs into vectors.
  303. int VecIndex = Index & ~3;
  304. if (Decls.TryGetValue(VecIndex, out ShaderDeclInfo DeclInfo))
  305. {
  306. if (DeclInfo.Size > 1 && Index < VecIndex + DeclInfo.Size)
  307. {
  308. return true;
  309. }
  310. }
  311. return Decls.ContainsKey(Index);
  312. }
  313. }
  314. }