GlslDecl.cs 10 KB

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