GlslDecompiler.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. namespace Ryujinx.Graphics.Gal.Shader
  7. {
  8. class GlslDecompiler
  9. {
  10. private delegate string GetInstExpr(ShaderIrOp Op);
  11. private Dictionary<ShaderIrInst, GetInstExpr> InstsExpr;
  12. private enum OperType
  13. {
  14. Bool,
  15. F32,
  16. I32
  17. }
  18. private const string IdentationStr = " ";
  19. private static string[] ElemTypes = new string[] { "float", "vec2", "vec3", "vec4" };
  20. private GlslDecl Decl;
  21. private StringBuilder SB;
  22. public GlslDecompiler()
  23. {
  24. InstsExpr = new Dictionary<ShaderIrInst, GetInstExpr>()
  25. {
  26. { ShaderIrInst.And, GetAndExpr },
  27. { ShaderIrInst.Asr, GetAsrExpr },
  28. { ShaderIrInst.Band, GetBandExpr },
  29. { ShaderIrInst.Bnot, GetBnotExpr },
  30. { ShaderIrInst.Ceil, GetCeilExpr },
  31. { ShaderIrInst.Ceq, GetCeqExpr },
  32. { ShaderIrInst.Cge, GetCgeExpr },
  33. { ShaderIrInst.Cgt, GetCgtExpr },
  34. { ShaderIrInst.Clamps, GetClampsExpr },
  35. { ShaderIrInst.Clampu, GetClampuExpr },
  36. { ShaderIrInst.Cle, GetCleExpr },
  37. { ShaderIrInst.Clt, GetCltExpr },
  38. { ShaderIrInst.Cne, GetCneExpr },
  39. { ShaderIrInst.Exit, GetExitExpr },
  40. { ShaderIrInst.Fabs, GetFabsExpr },
  41. { ShaderIrInst.Fadd, GetFaddExpr },
  42. { ShaderIrInst.Fceq, GetCeqExpr },
  43. { ShaderIrInst.Fcge, GetCgeExpr },
  44. { ShaderIrInst.Fcgt, GetCgtExpr },
  45. { ShaderIrInst.Fclamp, GetFclampExpr },
  46. { ShaderIrInst.Fcle, GetCleExpr },
  47. { ShaderIrInst.Fclt, GetCltExpr },
  48. { ShaderIrInst.Fcne, GetCneExpr },
  49. { ShaderIrInst.Fcos, GetFcosExpr },
  50. { ShaderIrInst.Fex2, GetFex2Expr },
  51. { ShaderIrInst.Ffma, GetFfmaExpr },
  52. { ShaderIrInst.Flg2, GetFlg2Expr },
  53. { ShaderIrInst.Floor, GetFloorExpr },
  54. { ShaderIrInst.Fmul, GetFmulExpr },
  55. { ShaderIrInst.Fneg, GetFnegExpr },
  56. { ShaderIrInst.Frcp, GetFrcpExpr },
  57. { ShaderIrInst.Frsq, GetFrsqExpr },
  58. { ShaderIrInst.Fsin, GetFsinExpr },
  59. { ShaderIrInst.Ftos, GetFtosExpr },
  60. { ShaderIrInst.Ftou, GetFtouExpr },
  61. { ShaderIrInst.Ipa, GetIpaExpr },
  62. { ShaderIrInst.Kil, GetKilExpr },
  63. { ShaderIrInst.Lsr, GetLsrExpr },
  64. { ShaderIrInst.Not, GetNotExpr },
  65. { ShaderIrInst.Or, GetOrExpr },
  66. { ShaderIrInst.Stof, GetStofExpr },
  67. { ShaderIrInst.Texq, GetTexqExpr },
  68. { ShaderIrInst.Texs, GetTexsExpr },
  69. { ShaderIrInst.Trunc, GetTruncExpr },
  70. { ShaderIrInst.Txlf, GetTxlfExpr },
  71. { ShaderIrInst.Utof, GetUtofExpr },
  72. { ShaderIrInst.Xor, GetXorExpr }
  73. };
  74. }
  75. public GlslProgram Decompile(int[] Code, GalShaderType ShaderType)
  76. {
  77. ShaderIrBlock Block = ShaderDecoder.DecodeBasicBlock(Code, 0);
  78. ShaderIrNode[] Nodes = Block.GetNodes();
  79. Decl = new GlslDecl(Nodes, ShaderType);
  80. SB = new StringBuilder();
  81. SB.AppendLine("#version 410 core");
  82. PrintDeclTextures();
  83. PrintDeclUniforms();
  84. PrintDeclInAttributes();
  85. PrintDeclOutAttributes();
  86. PrintDeclGprs();
  87. PrintDeclPreds();
  88. PrintBlockScope("void main()", 1, Nodes);
  89. string GlslCode = SB.ToString();
  90. return new GlslProgram(
  91. GlslCode,
  92. Decl.Textures.Values,
  93. Decl.Uniforms.Values);
  94. }
  95. private void PrintDeclTextures()
  96. {
  97. PrintDecls(Decl.Textures, "uniform sampler2D");
  98. }
  99. private void PrintDeclUniforms()
  100. {
  101. if (Decl.ShaderType == GalShaderType.Vertex)
  102. {
  103. SB.AppendLine("uniform vec2 " + GalConsts.FlipUniformName + ";");
  104. }
  105. foreach (ShaderDeclInfo DeclInfo in Decl.Uniforms.Values.OrderBy(DeclKeySelector))
  106. {
  107. SB.AppendLine($"uniform {GetDecl(DeclInfo)};");
  108. }
  109. if (Decl.Uniforms.Count > 0)
  110. {
  111. SB.AppendLine();
  112. }
  113. }
  114. private void PrintDeclInAttributes()
  115. {
  116. if (Decl.ShaderType == GalShaderType.Fragment)
  117. {
  118. SB.AppendLine("in vec4 " + GlslDecl.PositionOutAttrName + ";");
  119. }
  120. PrintDeclAttributes(Decl.InAttributes.Values, "in");
  121. }
  122. private void PrintDeclOutAttributes()
  123. {
  124. if (Decl.ShaderType == GalShaderType.Vertex)
  125. {
  126. SB.AppendLine("out vec4 " + GlslDecl.PositionOutAttrName + ";");
  127. }
  128. PrintDeclAttributes(Decl.OutAttributes.Values, "out");
  129. }
  130. private void PrintDeclAttributes(IEnumerable<ShaderDeclInfo> Decls, string InOut)
  131. {
  132. int Count = 0;
  133. foreach (ShaderDeclInfo DeclInfo in Decls.OrderBy(DeclKeySelector))
  134. {
  135. if (DeclInfo.Index >= 0)
  136. {
  137. SB.AppendLine("layout (location = " + DeclInfo.Index + ") " + InOut + " " + GetDecl(DeclInfo) + ";");
  138. Count++;
  139. }
  140. }
  141. if (Count > 0)
  142. {
  143. SB.AppendLine();
  144. }
  145. }
  146. private void PrintDeclGprs()
  147. {
  148. PrintDecls(Decl.Gprs);
  149. }
  150. private void PrintDeclPreds()
  151. {
  152. PrintDecls(Decl.Preds, "bool");
  153. }
  154. private void PrintDecls(IReadOnlyDictionary<int, ShaderDeclInfo> Dict, string CustomType = null)
  155. {
  156. foreach (ShaderDeclInfo DeclInfo in Dict.Values.OrderBy(DeclKeySelector))
  157. {
  158. string Name;
  159. if (CustomType != null)
  160. {
  161. Name = CustomType + " " + DeclInfo.Name + ";";
  162. }
  163. else if (DeclInfo.Name == GlslDecl.FragmentOutputName)
  164. {
  165. Name = "layout (location = 0) out " + GetDecl(DeclInfo) + ";" + Environment.NewLine;
  166. }
  167. else
  168. {
  169. Name = GetDecl(DeclInfo) + ";";
  170. }
  171. SB.AppendLine(Name);
  172. }
  173. if (Dict.Count > 0)
  174. {
  175. SB.AppendLine();
  176. }
  177. }
  178. private int DeclKeySelector(ShaderDeclInfo DeclInfo)
  179. {
  180. return DeclInfo.Cbuf << 24 | DeclInfo.Index;
  181. }
  182. private string GetDecl(ShaderDeclInfo DeclInfo)
  183. {
  184. return ElemTypes[DeclInfo.Size - 1] + " " + DeclInfo.Name;
  185. }
  186. private void PrintBlockScope(string ScopeName, int IdentationLevel, params ShaderIrNode[] Nodes)
  187. {
  188. string Identation = string.Empty;
  189. for (int Index = 0; Index < IdentationLevel - 1; Index++)
  190. {
  191. Identation += IdentationStr;
  192. }
  193. if (ScopeName != string.Empty)
  194. {
  195. ScopeName += " ";
  196. }
  197. SB.AppendLine(Identation + ScopeName + "{");
  198. string LastLine = Identation + "}";
  199. if (IdentationLevel > 0)
  200. {
  201. Identation += IdentationStr;
  202. }
  203. for (int Index = 0; Index < Nodes.Length; Index++)
  204. {
  205. ShaderIrNode Node = Nodes[Index];
  206. if (Node is ShaderIrCond Cond)
  207. {
  208. string IfExpr = GetSrcExpr(Cond.Pred, true);
  209. if (Cond.Not)
  210. {
  211. IfExpr = "!(" + IfExpr + ")";
  212. }
  213. string SubScopeName = "if (" + IfExpr + ")";
  214. PrintBlockScope(SubScopeName, IdentationLevel + 1, Cond.Child);
  215. }
  216. else if (Node is ShaderIrAsg Asg)
  217. {
  218. if (IsValidOutOper(Asg.Dst))
  219. {
  220. string Expr = GetSrcExpr(Asg.Src, true);
  221. Expr = GetExprWithCast(Asg.Dst, Asg.Src, Expr);
  222. SB.AppendLine(Identation + GetDstOperName(Asg.Dst) + " = " + Expr + ";");
  223. }
  224. }
  225. else if (Node is ShaderIrOp Op)
  226. {
  227. if (Op.Inst == ShaderIrInst.Exit)
  228. {
  229. //Do everything that needs to be done before
  230. //the shader ends here.
  231. if (Decl.ShaderType == GalShaderType.Vertex)
  232. {
  233. SB.AppendLine(Identation + "gl_Position.xy *= flip;");
  234. SB.AppendLine(Identation + GlslDecl.PositionOutAttrName + " = gl_Position;");
  235. }
  236. }
  237. SB.AppendLine(Identation + GetSrcExpr(Op, true) + ";");
  238. }
  239. else
  240. {
  241. throw new InvalidOperationException();
  242. }
  243. }
  244. SB.AppendLine(LastLine);
  245. }
  246. private bool IsValidOutOper(ShaderIrNode Node)
  247. {
  248. if (Node is ShaderIrOperGpr Gpr && Gpr.IsConst)
  249. {
  250. return false;
  251. }
  252. else if (Node is ShaderIrOperPred Pred && Pred.IsConst)
  253. {
  254. return false;
  255. }
  256. return true;
  257. }
  258. private string GetDstOperName(ShaderIrNode Node)
  259. {
  260. if (Node is ShaderIrOperAbuf Abuf)
  261. {
  262. return GetOutAbufName(Abuf);
  263. }
  264. else if (Node is ShaderIrOperGpr Gpr)
  265. {
  266. return GetName(Gpr);
  267. }
  268. else if (Node is ShaderIrOperPred Pred)
  269. {
  270. return GetName(Pred);
  271. }
  272. throw new ArgumentException(nameof(Node));
  273. }
  274. private string GetSrcExpr(ShaderIrNode Node, bool Entry = false)
  275. {
  276. switch (Node)
  277. {
  278. case ShaderIrOperAbuf Abuf: return GetName (Abuf);
  279. case ShaderIrOperCbuf Cbuf: return GetName (Cbuf);
  280. case ShaderIrOperGpr Gpr: return GetName (Gpr);
  281. case ShaderIrOperImm Imm: return GetValue(Imm);
  282. case ShaderIrOperImmf Immf: return GetValue(Immf);
  283. case ShaderIrOperPred Pred: return GetName (Pred);
  284. case ShaderIrOp Op:
  285. string Expr;
  286. if (InstsExpr.TryGetValue(Op.Inst, out GetInstExpr GetExpr))
  287. {
  288. Expr = GetExpr(Op);
  289. }
  290. else
  291. {
  292. throw new NotImplementedException(Op.Inst.ToString());
  293. }
  294. if (!Entry && NeedsParentheses(Op))
  295. {
  296. Expr = "(" + Expr + ")";
  297. }
  298. return Expr;
  299. default: throw new ArgumentException(nameof(Node));
  300. }
  301. }
  302. private static bool NeedsParentheses(ShaderIrOp Op)
  303. {
  304. switch (Op.Inst)
  305. {
  306. case ShaderIrInst.Frcp:
  307. return true;
  308. case ShaderIrInst.Ipa:
  309. case ShaderIrInst.Texq:
  310. case ShaderIrInst.Texs:
  311. case ShaderIrInst.Txlf:
  312. return false;
  313. }
  314. return Op.OperandB != null ||
  315. Op.OperandC != null;
  316. }
  317. private string GetName(ShaderIrOperCbuf Cbuf)
  318. {
  319. if (!Decl.Uniforms.TryGetValue((Cbuf.Index, Cbuf.Offs), out ShaderDeclInfo DeclInfo))
  320. {
  321. throw new InvalidOperationException();
  322. }
  323. return DeclInfo.Name;
  324. }
  325. private string GetOutAbufName(ShaderIrOperAbuf Abuf)
  326. {
  327. return GetName(Decl.OutAttributes, Abuf);
  328. }
  329. private string GetName(ShaderIrOperAbuf Abuf)
  330. {
  331. if (Abuf.Offs == GlslDecl.VertexIdAttr)
  332. {
  333. return "gl_VertexID";
  334. }
  335. return GetName(Decl.InAttributes, Abuf);
  336. }
  337. private string GetName(IReadOnlyDictionary<int, ShaderDeclInfo> Dict, ShaderIrOperAbuf Abuf)
  338. {
  339. int Index = Abuf.Offs >> 4;
  340. int Elem = (Abuf.Offs >> 2) & 3;
  341. if (!Dict.TryGetValue(Index, out ShaderDeclInfo DeclInfo))
  342. {
  343. throw new InvalidOperationException();
  344. }
  345. return DeclInfo.Size > 1 ? DeclInfo.Name + "." + GetAttrSwizzle(Elem) : DeclInfo.Name;
  346. }
  347. private string GetName(ShaderIrOperGpr Gpr)
  348. {
  349. return Gpr.IsConst ? "0" : GetNameWithSwizzle(Decl.Gprs, Gpr.Index);
  350. }
  351. private string GetValue(ShaderIrOperImm Imm)
  352. {
  353. //Only use hex is the value is too big and would likely be hard to read as int.
  354. if (Imm.Value > 0xfff ||
  355. Imm.Value < -0xfff)
  356. {
  357. return "0x" + Imm.Value.ToString("x8", CultureInfo.InvariantCulture);
  358. }
  359. else
  360. {
  361. return Imm.Value.ToString(CultureInfo.InvariantCulture);
  362. }
  363. }
  364. private string GetValue(ShaderIrOperImmf Immf)
  365. {
  366. return Immf.Value.ToString(CultureInfo.InvariantCulture);
  367. }
  368. private string GetName(ShaderIrOperPred Pred)
  369. {
  370. return Pred.IsConst ? "true" : GetNameWithSwizzle(Decl.Preds, Pred.Index);
  371. }
  372. private string GetNameWithSwizzle(IReadOnlyDictionary<int, ShaderDeclInfo> Dict, int Index)
  373. {
  374. int VecIndex = Index >> 2;
  375. if (Dict.TryGetValue(VecIndex, out ShaderDeclInfo DeclInfo))
  376. {
  377. if (DeclInfo.Size > 1 && Index < VecIndex + DeclInfo.Size)
  378. {
  379. return DeclInfo.Name + "." + GetAttrSwizzle(Index & 3);
  380. }
  381. }
  382. if (!Dict.TryGetValue(Index, out DeclInfo))
  383. {
  384. throw new InvalidOperationException();
  385. }
  386. return DeclInfo.Name;
  387. }
  388. private string GetAttrSwizzle(int Elem)
  389. {
  390. return "xyzw".Substring(Elem, 1);
  391. }
  392. private string GetAndExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "&");
  393. private string GetAsrExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">>");
  394. private string GetBandExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "&&");
  395. private string GetBnotExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "!");
  396. private string GetCeilExpr(ShaderIrOp Op) => GetUnaryCall(Op, "ceil");
  397. private string GetClampsExpr(ShaderIrOp Op)
  398. {
  399. return "clamp(" + GetOperExpr(Op, Op.OperandA) + ", " +
  400. GetOperExpr(Op, Op.OperandB) + ", " +
  401. GetOperExpr(Op, Op.OperandC) + ")";
  402. }
  403. private string GetClampuExpr(ShaderIrOp Op)
  404. {
  405. return "int(clamp(uint(" + GetOperExpr(Op, Op.OperandA) + "), " +
  406. "uint(" + GetOperExpr(Op, Op.OperandB) + "), " +
  407. "uint(" + GetOperExpr(Op, Op.OperandC) + ")))";
  408. }
  409. private string GetCltExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "<");
  410. private string GetCeqExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "==");
  411. private string GetCleExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "<=");
  412. private string GetCgtExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">");
  413. private string GetCneExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "!=");
  414. private string GetCgeExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">=");
  415. private string GetExitExpr(ShaderIrOp Op) => "return";
  416. private string GetFabsExpr(ShaderIrOp Op) => GetUnaryCall(Op, "abs");
  417. private string GetFaddExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "+");
  418. private string GetFcosExpr(ShaderIrOp Op) => GetUnaryCall(Op, "cos");
  419. private string GetFex2Expr(ShaderIrOp Op) => GetUnaryCall(Op, "exp2");
  420. private string GetFfmaExpr(ShaderIrOp Op) => GetTernaryExpr(Op, "*", "+");
  421. private string GetFclampExpr(ShaderIrOp Op) => GetTernaryCall(Op, "clamp");
  422. private string GetFlg2Expr(ShaderIrOp Op) => GetUnaryCall(Op, "log2");
  423. private string GetFloorExpr(ShaderIrOp Op) => GetUnaryCall(Op, "floor");
  424. private string GetFmulExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "*");
  425. private string GetFnegExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "-");
  426. private string GetFrcpExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "1 / ");
  427. private string GetFrsqExpr(ShaderIrOp Op) => GetUnaryCall(Op, "inversesqrt");
  428. private string GetFsinExpr(ShaderIrOp Op) => GetUnaryCall(Op, "sin");
  429. private string GetFtosExpr(ShaderIrOp Op)
  430. {
  431. return "int(" + GetOperExpr(Op, Op.OperandA) + ")";
  432. }
  433. private string GetFtouExpr(ShaderIrOp Op)
  434. {
  435. return "int(uint(" + GetOperExpr(Op, Op.OperandA) + "))";
  436. }
  437. private string GetIpaExpr(ShaderIrOp Op) => GetSrcExpr(Op.OperandA);
  438. private string GetKilExpr(ShaderIrOp Op) => "discard";
  439. private string GetLsrExpr(ShaderIrOp Op)
  440. {
  441. return "int(uint(" + GetOperExpr(Op, Op.OperandA) + ") >> " +
  442. GetOperExpr(Op, Op.OperandB) + ")";
  443. }
  444. private string GetNotExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "~");
  445. private string GetOrExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "|");
  446. private string GetStofExpr(ShaderIrOp Op)
  447. {
  448. return "float(" + GetOperExpr(Op, Op.OperandA) + ")";
  449. }
  450. private string GetTexqExpr(ShaderIrOp Op)
  451. {
  452. ShaderIrMetaTexq Meta = (ShaderIrMetaTexq)Op.MetaData;
  453. string Ch = "xyzw".Substring(Meta.Elem, 1);
  454. if (Meta.Info == ShaderTexqInfo.Dimension)
  455. {
  456. string Sampler = GetTexSamplerName(Op);
  457. string Lod = GetOperExpr(Op, Op.OperandA); //???
  458. return "textureSize(" + Sampler + ", " + Lod + ")." + Ch;
  459. }
  460. else
  461. {
  462. throw new NotImplementedException(Meta.Info.ToString());
  463. }
  464. }
  465. private string GetTexsExpr(ShaderIrOp Op)
  466. {
  467. ShaderIrMetaTex Meta = (ShaderIrMetaTex)Op.MetaData;
  468. string Sampler = GetTexSamplerName(Op);
  469. string Coords = GetTexSamplerCoords(Op);
  470. string Ch = "rgba".Substring(Meta.Elem, 1);
  471. return "texture(" + Sampler + ", " + Coords + ")." + Ch;
  472. }
  473. private string GetTxlfExpr(ShaderIrOp Op)
  474. {
  475. ShaderIrMetaTex Meta = (ShaderIrMetaTex)Op.MetaData;
  476. string Sampler = GetTexSamplerName(Op);
  477. string Coords = GetITexSamplerCoords(Op);
  478. string Ch = "rgba".Substring(Meta.Elem, 1);
  479. return "texelFetch(" + Sampler + ", " + Coords + ", 0)." + Ch;
  480. }
  481. private string GetTruncExpr(ShaderIrOp Op) => GetUnaryCall(Op, "trunc");
  482. private string GetUtofExpr(ShaderIrOp Op)
  483. {
  484. return "float(uint(" + GetOperExpr(Op, Op.OperandA) + "))";
  485. }
  486. private string GetXorExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "^");
  487. private string GetUnaryCall(ShaderIrOp Op, string FuncName)
  488. {
  489. return FuncName + "(" + GetOperExpr(Op, Op.OperandA) + ")";
  490. }
  491. private string GetTernaryCall(ShaderIrOp Op, string FuncName)
  492. {
  493. return FuncName + "(" + GetOperExpr(Op, Op.OperandA) + ", " +
  494. GetOperExpr(Op, Op.OperandB) + ", " +
  495. GetOperExpr(Op, Op.OperandC) + ")";
  496. }
  497. private string GetUnaryExpr(ShaderIrOp Op, string Opr)
  498. {
  499. return Opr + GetOperExpr(Op, Op.OperandA);
  500. }
  501. private string GetBinaryExpr(ShaderIrOp Op, string Opr)
  502. {
  503. return GetOperExpr(Op, Op.OperandA) + " " + Opr + " " +
  504. GetOperExpr(Op, Op.OperandB);
  505. }
  506. private string GetTernaryExpr(ShaderIrOp Op, string Opr1, string Opr2)
  507. {
  508. return GetOperExpr(Op, Op.OperandA) + " " + Opr1 + " " +
  509. GetOperExpr(Op, Op.OperandB) + " " + Opr2 + " " +
  510. GetOperExpr(Op, Op.OperandC);
  511. }
  512. private string GetTexSamplerName(ShaderIrOp Op)
  513. {
  514. ShaderIrOperImm Node = (ShaderIrOperImm)Op.OperandC;
  515. int Handle = ((ShaderIrOperImm)Op.OperandC).Value;
  516. if (!Decl.Textures.TryGetValue(Handle, out ShaderDeclInfo DeclInfo))
  517. {
  518. throw new InvalidOperationException();
  519. }
  520. return DeclInfo.Name;
  521. }
  522. private string GetTexSamplerCoords(ShaderIrOp Op)
  523. {
  524. return "vec2(" + GetOperExpr(Op, Op.OperandA) + ", " +
  525. GetOperExpr(Op, Op.OperandB) + ")";
  526. }
  527. private string GetITexSamplerCoords(ShaderIrOp Op)
  528. {
  529. return "ivec2(" + GetOperExpr(Op, Op.OperandA) + ", " +
  530. GetOperExpr(Op, Op.OperandB) + ")";
  531. }
  532. private string GetOperExpr(ShaderIrOp Op, ShaderIrNode Oper)
  533. {
  534. return GetExprWithCast(Op, Oper, GetSrcExpr(Oper));
  535. }
  536. private static string GetExprWithCast(ShaderIrNode Dst, ShaderIrNode Src, string Expr)
  537. {
  538. //Note: The "DstType" (of the cast) is the type that the operation
  539. //uses on the source operands, while the "SrcType" is the destination
  540. //type of the operand result (if it is a operation) or just the type
  541. //of the variable for registers/uniforms/attributes.
  542. OperType DstType = GetSrcNodeType(Dst);
  543. OperType SrcType = GetDstNodeType(Src);
  544. if (DstType != SrcType)
  545. {
  546. //Check for invalid casts
  547. //(like bool to int/float and others).
  548. if (SrcType != OperType.F32 &&
  549. SrcType != OperType.I32)
  550. {
  551. throw new InvalidOperationException();
  552. }
  553. switch (Src)
  554. {
  555. case ShaderIrOperGpr Gpr:
  556. {
  557. //When the Gpr is ZR, just return the 0 value directly,
  558. //since the float encoding for 0 is 0.
  559. if (Gpr.IsConst)
  560. {
  561. return "0";
  562. }
  563. break;
  564. }
  565. case ShaderIrOperImm Imm:
  566. {
  567. //For integer immediates being used as float,
  568. //it's better (for readability) to just return the float value.
  569. if (DstType == OperType.F32)
  570. {
  571. float Value = BitConverter.Int32BitsToSingle(Imm.Value);
  572. return Value.ToString(CultureInfo.InvariantCulture);
  573. }
  574. break;
  575. }
  576. }
  577. switch (DstType)
  578. {
  579. case OperType.F32: Expr = "intBitsToFloat(" + Expr + ")"; break;
  580. case OperType.I32: Expr = "floatBitsToInt(" + Expr + ")"; break;
  581. }
  582. }
  583. return Expr;
  584. }
  585. private static OperType GetDstNodeType(ShaderIrNode Node)
  586. {
  587. //Special case instructions with the result type different
  588. //from the input types (like integer <-> float conversion) here.
  589. if (Node is ShaderIrOp Op)
  590. {
  591. switch (Op.Inst)
  592. {
  593. case ShaderIrInst.Stof:
  594. case ShaderIrInst.Txlf:
  595. case ShaderIrInst.Utof:
  596. return OperType.F32;
  597. case ShaderIrInst.Ftos:
  598. case ShaderIrInst.Ftou:
  599. return OperType.I32;
  600. }
  601. }
  602. return GetSrcNodeType(Node);
  603. }
  604. private static OperType GetSrcNodeType(ShaderIrNode Node)
  605. {
  606. switch (Node)
  607. {
  608. case ShaderIrOperAbuf Abuf:
  609. return Abuf.Offs == GlslDecl.VertexIdAttr
  610. ? OperType.I32
  611. : OperType.F32;
  612. case ShaderIrOperCbuf Cbuf: return OperType.F32;
  613. case ShaderIrOperGpr Gpr: return OperType.F32;
  614. case ShaderIrOperImm Imm: return OperType.I32;
  615. case ShaderIrOperImmf Immf: return OperType.F32;
  616. case ShaderIrOperPred Pred: return OperType.Bool;
  617. case ShaderIrOp Op:
  618. if (Op.Inst > ShaderIrInst.B_Start &&
  619. Op.Inst < ShaderIrInst.B_End)
  620. {
  621. return OperType.Bool;
  622. }
  623. else if (Op.Inst > ShaderIrInst.F_Start &&
  624. Op.Inst < ShaderIrInst.F_End)
  625. {
  626. return OperType.F32;
  627. }
  628. else if (Op.Inst > ShaderIrInst.I_Start &&
  629. Op.Inst < ShaderIrInst.I_End)
  630. {
  631. return OperType.I32;
  632. }
  633. break;
  634. }
  635. throw new ArgumentException(nameof(Node));
  636. }
  637. }
  638. }