GlslDecl.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using System.Collections.Generic;
  2. namespace Ryujinx.Graphics.Gal.Shader
  3. {
  4. class GlslDecl
  5. {
  6. public const int TessCoordAttrX = 0x2f0;
  7. public const int TessCoordAttrY = 0x2f4;
  8. public const int TessCoordAttrZ = 0x2f8;
  9. public const int InstanceIdAttr = 0x2f8;
  10. public const int VertexIdAttr = 0x2fc;
  11. public const int GlPositionWAttr = 0x7c;
  12. public const int MaxUboSize = 1024;
  13. public const int GlPositionVec4Index = 7;
  14. public const int PositionOutAttrLocation = 15;
  15. private const int AttrStartIndex = 8;
  16. private const int TexStartIndex = 8;
  17. public const string PositionOutAttrName = "position";
  18. private const string TextureName = "tex";
  19. private const string UniformName = "c";
  20. private const string AttrName = "attr";
  21. private const string InAttrName = "in_" + AttrName;
  22. private const string OutAttrName = "out_" + AttrName;
  23. private const string GprName = "gpr";
  24. private const string PredName = "pred";
  25. public const string FragmentOutputName = "FragColor";
  26. public const string FlipUniformName = "flip";
  27. public const string ProgramName = "program";
  28. public const string ProgramAName = ProgramName + "_a";
  29. public const string ProgramBName = ProgramName + "_b";
  30. private string[] StagePrefixes = new string[] { "vp", "tcp", "tep", "gp", "fp" };
  31. private string StagePrefix;
  32. private Dictionary<int, ShaderDeclInfo> m_Textures;
  33. private Dictionary<int, ShaderDeclInfo> m_Uniforms;
  34. private Dictionary<int, ShaderDeclInfo> m_Attributes;
  35. private Dictionary<int, ShaderDeclInfo> m_InAttributes;
  36. private Dictionary<int, ShaderDeclInfo> m_OutAttributes;
  37. private Dictionary<int, ShaderDeclInfo> m_Gprs;
  38. private Dictionary<int, ShaderDeclInfo> m_Preds;
  39. public IReadOnlyDictionary<int, ShaderDeclInfo> Textures => m_Textures;
  40. public IReadOnlyDictionary<int, ShaderDeclInfo> Uniforms => m_Uniforms;
  41. public IReadOnlyDictionary<int, ShaderDeclInfo> Attributes => m_Attributes;
  42. public IReadOnlyDictionary<int, ShaderDeclInfo> InAttributes => m_InAttributes;
  43. public IReadOnlyDictionary<int, ShaderDeclInfo> OutAttributes => m_OutAttributes;
  44. public IReadOnlyDictionary<int, ShaderDeclInfo> Gprs => m_Gprs;
  45. public IReadOnlyDictionary<int, ShaderDeclInfo> Preds => m_Preds;
  46. public GalShaderType ShaderType { get; private set; }
  47. private GlslDecl(GalShaderType ShaderType)
  48. {
  49. this.ShaderType = ShaderType;
  50. m_Uniforms = new Dictionary<int, ShaderDeclInfo>();
  51. m_Textures = new Dictionary<int, ShaderDeclInfo>();
  52. m_Attributes = new Dictionary<int, ShaderDeclInfo>();
  53. m_InAttributes = new Dictionary<int, ShaderDeclInfo>();
  54. m_OutAttributes = new Dictionary<int, ShaderDeclInfo>();
  55. m_Gprs = new Dictionary<int, ShaderDeclInfo>();
  56. m_Preds = new Dictionary<int, ShaderDeclInfo>();
  57. }
  58. public GlslDecl(ShaderIrBlock[] Blocks, GalShaderType ShaderType) : this(ShaderType)
  59. {
  60. StagePrefix = StagePrefixes[(int)ShaderType] + "_";
  61. if (ShaderType == GalShaderType.Fragment)
  62. {
  63. m_Gprs.Add(0, new ShaderDeclInfo(FragmentOutputName, 0, 0, 4));
  64. }
  65. foreach (ShaderIrBlock Block in Blocks)
  66. {
  67. foreach (ShaderIrNode Node in Block.GetNodes())
  68. {
  69. Traverse(null, Node);
  70. }
  71. }
  72. }
  73. public static GlslDecl Merge(GlslDecl VpA, GlslDecl VpB)
  74. {
  75. GlslDecl Combined = new GlslDecl(GalShaderType.Vertex);
  76. Merge(Combined.m_Textures, VpA.m_Textures, VpB.m_Textures);
  77. Merge(Combined.m_Uniforms, VpA.m_Uniforms, VpB.m_Uniforms);
  78. Merge(Combined.m_Attributes, VpA.m_Attributes, VpB.m_Attributes);
  79. Merge(Combined.m_OutAttributes, VpA.m_OutAttributes, VpB.m_OutAttributes);
  80. Merge(Combined.m_Gprs, VpA.m_Gprs, VpB.m_Gprs);
  81. Merge(Combined.m_Preds, VpA.m_Preds, VpB.m_Preds);
  82. //Merge input attributes.
  83. foreach (KeyValuePair<int, ShaderDeclInfo> KV in VpA.m_InAttributes)
  84. {
  85. Combined.m_InAttributes.TryAdd(KV.Key, KV.Value);
  86. }
  87. foreach (KeyValuePair<int, ShaderDeclInfo> KV in VpB.m_InAttributes)
  88. {
  89. //If Vertex Program A already writes to this attribute,
  90. //then we don't need to add it as an input attribute since
  91. //Vertex Program A will already have written to it anyway,
  92. //and there's no guarantee that there is an input attribute
  93. //for this slot.
  94. if (!VpA.m_OutAttributes.ContainsKey(KV.Key))
  95. {
  96. Combined.m_InAttributes.TryAdd(KV.Key, KV.Value);
  97. }
  98. }
  99. return Combined;
  100. }
  101. private static void Merge(
  102. Dictionary<int, ShaderDeclInfo> C,
  103. Dictionary<int, ShaderDeclInfo> A,
  104. Dictionary<int, ShaderDeclInfo> B)
  105. {
  106. foreach (KeyValuePair<int, ShaderDeclInfo> KV in A)
  107. {
  108. C.TryAdd(KV.Key, KV.Value);
  109. }
  110. foreach (KeyValuePair<int, ShaderDeclInfo> KV in B)
  111. {
  112. C.TryAdd(KV.Key, KV.Value);
  113. }
  114. }
  115. private void Traverse(ShaderIrNode Parent, ShaderIrNode Node)
  116. {
  117. switch (Node)
  118. {
  119. case ShaderIrAsg Asg:
  120. {
  121. Traverse(Asg, Asg.Dst);
  122. Traverse(Asg, Asg.Src);
  123. break;
  124. }
  125. case ShaderIrCond Cond:
  126. {
  127. Traverse(Cond, Cond.Pred);
  128. Traverse(Cond, Cond.Child);
  129. break;
  130. }
  131. case ShaderIrOp Op:
  132. {
  133. Traverse(Op, Op.OperandA);
  134. Traverse(Op, Op.OperandB);
  135. Traverse(Op, Op.OperandC);
  136. if (Op.Inst == ShaderIrInst.Texq ||
  137. Op.Inst == ShaderIrInst.Texs ||
  138. Op.Inst == ShaderIrInst.Txlf)
  139. {
  140. int Handle = ((ShaderIrOperImm)Op.OperandC).Value;
  141. int Index = Handle - TexStartIndex;
  142. string Name = StagePrefix + TextureName + Index;
  143. m_Textures.TryAdd(Handle, new ShaderDeclInfo(Name, Handle));
  144. }
  145. break;
  146. }
  147. case ShaderIrOperCbuf Cbuf:
  148. {
  149. if (!m_Uniforms.ContainsKey(Cbuf.Index))
  150. {
  151. string Name = StagePrefix + UniformName + Cbuf.Index;
  152. ShaderDeclInfo DeclInfo = new ShaderDeclInfo(Name, Cbuf.Pos, Cbuf.Index);
  153. m_Uniforms.Add(Cbuf.Index, DeclInfo);
  154. }
  155. break;
  156. }
  157. case ShaderIrOperAbuf Abuf:
  158. {
  159. //This is a built-in input variable.
  160. if (Abuf.Offs == VertexIdAttr ||
  161. Abuf.Offs == InstanceIdAttr)
  162. {
  163. break;
  164. }
  165. int Index = Abuf.Offs >> 4;
  166. int Elem = (Abuf.Offs >> 2) & 3;
  167. int GlslIndex = Index - AttrStartIndex;
  168. if (GlslIndex < 0)
  169. {
  170. return;
  171. }
  172. ShaderDeclInfo DeclInfo;
  173. if (Parent is ShaderIrAsg Asg && Asg.Dst == Node)
  174. {
  175. if (!m_OutAttributes.TryGetValue(Index, out DeclInfo))
  176. {
  177. DeclInfo = new ShaderDeclInfo(OutAttrName + GlslIndex, GlslIndex);
  178. m_OutAttributes.Add(Index, DeclInfo);
  179. }
  180. }
  181. else
  182. {
  183. if (!m_InAttributes.TryGetValue(Index, out DeclInfo))
  184. {
  185. DeclInfo = new ShaderDeclInfo(InAttrName + GlslIndex, GlslIndex);
  186. m_InAttributes.Add(Index, DeclInfo);
  187. }
  188. }
  189. DeclInfo.Enlarge(Elem + 1);
  190. if (!m_Attributes.ContainsKey(Index))
  191. {
  192. DeclInfo = new ShaderDeclInfo(AttrName + GlslIndex, GlslIndex, 0, 4);
  193. m_Attributes.Add(Index, DeclInfo);
  194. }
  195. break;
  196. }
  197. case ShaderIrOperGpr Gpr:
  198. {
  199. if (!Gpr.IsConst && !HasName(m_Gprs, Gpr.Index))
  200. {
  201. string Name = GprName + Gpr.Index;
  202. m_Gprs.TryAdd(Gpr.Index, new ShaderDeclInfo(Name, Gpr.Index));
  203. }
  204. break;
  205. }
  206. case ShaderIrOperPred Pred:
  207. {
  208. if (!Pred.IsConst && !HasName(m_Preds, Pred.Index))
  209. {
  210. string Name = PredName + Pred.Index;
  211. m_Preds.TryAdd(Pred.Index, new ShaderDeclInfo(Name, Pred.Index));
  212. }
  213. break;
  214. }
  215. }
  216. }
  217. private bool HasName(Dictionary<int, ShaderDeclInfo> Decls, int Index)
  218. {
  219. //This is used to check if the dictionary already contains
  220. //a entry for a vector at a given index position.
  221. //Used to enable turning gprs into vectors.
  222. int VecIndex = Index & ~3;
  223. if (Decls.TryGetValue(VecIndex, out ShaderDeclInfo DeclInfo))
  224. {
  225. if (DeclInfo.Size > 1 && Index < VecIndex + DeclInfo.Size)
  226. {
  227. return true;
  228. }
  229. }
  230. return Decls.ContainsKey(Index);
  231. }
  232. }
  233. }