GlslDecl.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 MaxFrameBufferAttachments = 8;
  18. public const int MaxUboSize = 1024;
  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 ProgramName = "program";
  35. public const string ProgramAName = ProgramName + "_a";
  36. public const string ProgramBName = ProgramName + "_b";
  37. private string[] StagePrefixes = new string[] { "vp", "tcp", "tep", "gp", "fp" };
  38. private string StagePrefix;
  39. private Dictionary<ShaderIrOp, ShaderDeclInfo> m_CbTextures;
  40. private Dictionary<int, ShaderDeclInfo> m_Textures;
  41. private Dictionary<int, ShaderDeclInfo> m_Uniforms;
  42. private Dictionary<int, ShaderDeclInfo> m_Attributes;
  43. private Dictionary<int, ShaderDeclInfo> m_InAttributes;
  44. private Dictionary<int, ShaderDeclInfo> m_OutAttributes;
  45. private Dictionary<int, ShaderDeclInfo> m_Gprs;
  46. private Dictionary<int, ShaderDeclInfo> m_Preds;
  47. public IReadOnlyDictionary<ShaderIrOp, ShaderDeclInfo> CbTextures => m_CbTextures;
  48. public IReadOnlyDictionary<int, ShaderDeclInfo> Textures => m_Textures;
  49. public IReadOnlyDictionary<int, ShaderDeclInfo> Uniforms => m_Uniforms;
  50. public IReadOnlyDictionary<int, ShaderDeclInfo> Attributes => m_Attributes;
  51. public IReadOnlyDictionary<int, ShaderDeclInfo> InAttributes => m_InAttributes;
  52. public IReadOnlyDictionary<int, ShaderDeclInfo> OutAttributes => m_OutAttributes;
  53. public IReadOnlyDictionary<int, ShaderDeclInfo> Gprs => m_Gprs;
  54. public IReadOnlyDictionary<int, ShaderDeclInfo> Preds => m_Preds;
  55. public GalShaderType ShaderType { get; private set; }
  56. private GlslDecl(GalShaderType ShaderType)
  57. {
  58. this.ShaderType = ShaderType;
  59. m_CbTextures = new Dictionary<ShaderIrOp, ShaderDeclInfo>();
  60. m_Textures = new Dictionary<int, ShaderDeclInfo>();
  61. m_Uniforms = new Dictionary<int, ShaderDeclInfo>();
  62. m_Attributes = new Dictionary<int, ShaderDeclInfo>();
  63. m_InAttributes = new Dictionary<int, ShaderDeclInfo>();
  64. m_OutAttributes = new Dictionary<int, ShaderDeclInfo>();
  65. m_Gprs = new Dictionary<int, ShaderDeclInfo>();
  66. m_Preds = new Dictionary<int, ShaderDeclInfo>();
  67. }
  68. public GlslDecl(ShaderIrBlock[] Blocks, GalShaderType ShaderType) : this(ShaderType)
  69. {
  70. StagePrefix = StagePrefixes[(int)ShaderType] + "_";
  71. if (ShaderType == GalShaderType.Fragment)
  72. {
  73. //Note: Replace 1 with MaxFrameBufferAttachments when attachments start to work
  74. for (int Index = 0; Index < 1; Index++)
  75. {
  76. m_Gprs.Add(Index * 4, new ShaderDeclInfo(FragmentOutputName + Index, Index * 4, false, 0, 4));
  77. }
  78. }
  79. foreach (ShaderIrBlock Block in Blocks)
  80. {
  81. ShaderIrNode[] Nodes = Block.GetNodes();
  82. foreach (ShaderIrNode Node in Nodes)
  83. {
  84. Traverse(Nodes, null, Node);
  85. }
  86. }
  87. }
  88. public static GlslDecl Merge(GlslDecl VpA, GlslDecl VpB)
  89. {
  90. GlslDecl Combined = new GlslDecl(GalShaderType.Vertex);
  91. Merge(Combined.m_Textures, VpA.m_Textures, VpB.m_Textures);
  92. Merge(Combined.m_Uniforms, VpA.m_Uniforms, VpB.m_Uniforms);
  93. Merge(Combined.m_Attributes, VpA.m_Attributes, VpB.m_Attributes);
  94. Merge(Combined.m_OutAttributes, VpA.m_OutAttributes, VpB.m_OutAttributes);
  95. Merge(Combined.m_Gprs, VpA.m_Gprs, VpB.m_Gprs);
  96. Merge(Combined.m_Preds, VpA.m_Preds, VpB.m_Preds);
  97. //Merge input attributes.
  98. foreach (KeyValuePair<int, ShaderDeclInfo> KV in VpA.m_InAttributes)
  99. {
  100. Combined.m_InAttributes.TryAdd(KV.Key, KV.Value);
  101. }
  102. foreach (KeyValuePair<int, ShaderDeclInfo> KV in VpB.m_InAttributes)
  103. {
  104. //If Vertex Program A already writes to this attribute,
  105. //then we don't need to add it as an input attribute since
  106. //Vertex Program A will already have written to it anyway,
  107. //and there's no guarantee that there is an input attribute
  108. //for this slot.
  109. if (!VpA.m_OutAttributes.ContainsKey(KV.Key))
  110. {
  111. Combined.m_InAttributes.TryAdd(KV.Key, KV.Value);
  112. }
  113. }
  114. return Combined;
  115. }
  116. private static void Merge(
  117. Dictionary<int, ShaderDeclInfo> C,
  118. Dictionary<int, ShaderDeclInfo> A,
  119. Dictionary<int, ShaderDeclInfo> B)
  120. {
  121. foreach (KeyValuePair<int, ShaderDeclInfo> KV in A)
  122. {
  123. C.TryAdd(KV.Key, KV.Value);
  124. }
  125. foreach (KeyValuePair<int, ShaderDeclInfo> KV in B)
  126. {
  127. C.TryAdd(KV.Key, KV.Value);
  128. }
  129. }
  130. private void Traverse(ShaderIrNode[] Nodes, ShaderIrNode Parent, ShaderIrNode Node)
  131. {
  132. switch (Node)
  133. {
  134. case ShaderIrAsg Asg:
  135. {
  136. Traverse(Nodes, Asg, Asg.Dst);
  137. Traverse(Nodes, Asg, Asg.Src);
  138. break;
  139. }
  140. case ShaderIrCond Cond:
  141. {
  142. Traverse(Nodes, Cond, Cond.Pred);
  143. Traverse(Nodes, Cond, Cond.Child);
  144. break;
  145. }
  146. case ShaderIrOp Op:
  147. {
  148. Traverse(Nodes, Op, Op.OperandA);
  149. Traverse(Nodes, Op, Op.OperandB);
  150. Traverse(Nodes, Op, Op.OperandC);
  151. if (Op.Inst == ShaderIrInst.Texq ||
  152. Op.Inst == ShaderIrInst.Texs ||
  153. Op.Inst == ShaderIrInst.Txlf)
  154. {
  155. int Handle = ((ShaderIrOperImm)Op.OperandC).Value;
  156. int Index = Handle - TexStartIndex;
  157. string Name = StagePrefix + TextureName + Index;
  158. m_Textures.TryAdd(Handle, new ShaderDeclInfo(Name, Handle));
  159. }
  160. else if (Op.Inst == ShaderIrInst.Texb)
  161. {
  162. ShaderIrNode HandleSrc = null;
  163. int Index = Array.IndexOf(Nodes, Parent) - 1;
  164. for (; Index >= 0; Index--)
  165. {
  166. ShaderIrNode Curr = Nodes[Index];
  167. if (Curr is ShaderIrAsg Asg && Asg.Dst is ShaderIrOperGpr Gpr)
  168. {
  169. if (Gpr.Index == ((ShaderIrOperGpr)Op.OperandC).Index)
  170. {
  171. HandleSrc = Asg.Src;
  172. break;
  173. }
  174. }
  175. }
  176. if (HandleSrc != null && HandleSrc is ShaderIrOperCbuf Cbuf)
  177. {
  178. string Name = StagePrefix + TextureName + "_cb" + Cbuf.Index + "_" + Cbuf.Pos;
  179. m_CbTextures.Add(Op, new ShaderDeclInfo(Name, Cbuf.Pos, true, Cbuf.Index));
  180. }
  181. else
  182. {
  183. throw new NotImplementedException("Shader TEX.B instruction is not fully supported!");
  184. }
  185. }
  186. break;
  187. }
  188. case ShaderIrOperCbuf Cbuf:
  189. {
  190. if (!m_Uniforms.ContainsKey(Cbuf.Index))
  191. {
  192. string Name = StagePrefix + UniformName + Cbuf.Index;
  193. ShaderDeclInfo DeclInfo = new ShaderDeclInfo(Name, Cbuf.Pos, true, Cbuf.Index);
  194. m_Uniforms.Add(Cbuf.Index, DeclInfo);
  195. }
  196. break;
  197. }
  198. case ShaderIrOperAbuf Abuf:
  199. {
  200. //This is a built-in variable.
  201. if (Abuf.Offs == LayerAttr ||
  202. Abuf.Offs == PointSizeAttr ||
  203. Abuf.Offs == PointCoordAttrX ||
  204. Abuf.Offs == PointCoordAttrY ||
  205. Abuf.Offs == VertexIdAttr ||
  206. Abuf.Offs == InstanceIdAttr ||
  207. Abuf.Offs == FaceAttr)
  208. {
  209. break;
  210. }
  211. int Index = Abuf.Offs >> 4;
  212. int Elem = (Abuf.Offs >> 2) & 3;
  213. int GlslIndex = Index - AttrStartIndex;
  214. if (GlslIndex < 0)
  215. {
  216. return;
  217. }
  218. ShaderDeclInfo DeclInfo;
  219. if (Parent is ShaderIrAsg Asg && Asg.Dst == Node)
  220. {
  221. if (!m_OutAttributes.TryGetValue(Index, out DeclInfo))
  222. {
  223. DeclInfo = new ShaderDeclInfo(OutAttrName + GlslIndex, GlslIndex);
  224. m_OutAttributes.Add(Index, DeclInfo);
  225. }
  226. }
  227. else
  228. {
  229. if (!m_InAttributes.TryGetValue(Index, out DeclInfo))
  230. {
  231. DeclInfo = new ShaderDeclInfo(InAttrName + GlslIndex, GlslIndex);
  232. m_InAttributes.Add(Index, DeclInfo);
  233. }
  234. }
  235. DeclInfo.Enlarge(Elem + 1);
  236. if (!m_Attributes.ContainsKey(Index))
  237. {
  238. DeclInfo = new ShaderDeclInfo(AttrName + GlslIndex, GlslIndex, false, 0, 4);
  239. m_Attributes.Add(Index, DeclInfo);
  240. }
  241. Traverse(Nodes, Abuf, Abuf.Vertex);
  242. break;
  243. }
  244. case ShaderIrOperGpr Gpr:
  245. {
  246. if (!Gpr.IsConst && !HasName(m_Gprs, Gpr.Index))
  247. {
  248. string Name = GprName + Gpr.Index;
  249. m_Gprs.TryAdd(Gpr.Index, new ShaderDeclInfo(Name, Gpr.Index));
  250. }
  251. break;
  252. }
  253. case ShaderIrOperPred Pred:
  254. {
  255. if (!Pred.IsConst && !HasName(m_Preds, Pred.Index))
  256. {
  257. string Name = PredName + Pred.Index;
  258. m_Preds.TryAdd(Pred.Index, new ShaderDeclInfo(Name, Pred.Index));
  259. }
  260. break;
  261. }
  262. }
  263. }
  264. private bool HasName(Dictionary<int, ShaderDeclInfo> Decls, int Index)
  265. {
  266. //This is used to check if the dictionary already contains
  267. //a entry for a vector at a given index position.
  268. //Used to enable turning gprs into vectors.
  269. int VecIndex = Index & ~3;
  270. if (Decls.TryGetValue(VecIndex, out ShaderDeclInfo DeclInfo))
  271. {
  272. if (DeclInfo.Size > 1 && Index < VecIndex + DeclInfo.Size)
  273. {
  274. return true;
  275. }
  276. }
  277. return Decls.ContainsKey(Index);
  278. }
  279. }
  280. }