GlslDecl.cs 15 KB

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