GlslDecl.cs 15 KB

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