GlslDecompiler.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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 && IsValidOutOper(Asg.Dst))
  217. {
  218. string Expr = GetSrcExpr(Asg.Src, true);
  219. Expr = GetExprWithCast(Asg.Dst, Asg.Src, Expr);
  220. SB.AppendLine(Identation + GetDstOperName(Asg.Dst) + " = " + Expr + ";");
  221. }
  222. else if (Node is ShaderIrOp Op)
  223. {
  224. if (Op.Inst == ShaderIrInst.Exit)
  225. {
  226. //Do everything that needs to be done before
  227. //the shader ends here.
  228. if (Decl.ShaderType == GalShaderType.Vertex)
  229. {
  230. SB.AppendLine(Identation + "gl_Position.xy *= flip;");
  231. SB.AppendLine(Identation + GlslDecl.PositionOutAttrName + " = gl_Position;");
  232. }
  233. }
  234. SB.AppendLine(Identation + GetSrcExpr(Op, true) + ";");
  235. }
  236. else
  237. {
  238. throw new InvalidOperationException();
  239. }
  240. }
  241. SB.AppendLine(LastLine);
  242. }
  243. private bool IsValidOutOper(ShaderIrNode Node)
  244. {
  245. if (Node is ShaderIrOperGpr Gpr && Gpr.IsConst)
  246. {
  247. return false;
  248. }
  249. else if (Node is ShaderIrOperPred Pred && Pred.IsConst)
  250. {
  251. return false;
  252. }
  253. return true;
  254. }
  255. private string GetDstOperName(ShaderIrNode Node)
  256. {
  257. if (Node is ShaderIrOperAbuf Abuf)
  258. {
  259. return GetOutAbufName(Abuf);
  260. }
  261. else if (Node is ShaderIrOperGpr Gpr)
  262. {
  263. return GetName(Gpr);
  264. }
  265. else if (Node is ShaderIrOperPred Pred)
  266. {
  267. return GetName(Pred);
  268. }
  269. throw new ArgumentException(nameof(Node));
  270. }
  271. private string GetSrcExpr(ShaderIrNode Node, bool Entry = false)
  272. {
  273. switch (Node)
  274. {
  275. case ShaderIrOperAbuf Abuf: return GetName (Abuf);
  276. case ShaderIrOperCbuf Cbuf: return GetName (Cbuf);
  277. case ShaderIrOperGpr Gpr: return GetName (Gpr);
  278. case ShaderIrOperImm Imm: return GetValue(Imm);
  279. case ShaderIrOperImmf Immf: return GetValue(Immf);
  280. case ShaderIrOperPred Pred: return GetName (Pred);
  281. case ShaderIrOp Op:
  282. string Expr;
  283. if (InstsExpr.TryGetValue(Op.Inst, out GetInstExpr GetExpr))
  284. {
  285. Expr = GetExpr(Op);
  286. }
  287. else
  288. {
  289. throw new NotImplementedException(Op.Inst.ToString());
  290. }
  291. if (!Entry && NeedsParentheses(Op))
  292. {
  293. Expr = "(" + Expr + ")";
  294. }
  295. return Expr;
  296. default: throw new ArgumentException(nameof(Node));
  297. }
  298. }
  299. private static bool NeedsParentheses(ShaderIrOp Op)
  300. {
  301. switch (Op.Inst)
  302. {
  303. case ShaderIrInst.Frcp:
  304. return true;
  305. case ShaderIrInst.Ipa:
  306. case ShaderIrInst.Texq:
  307. case ShaderIrInst.Texs:
  308. case ShaderIrInst.Txlf:
  309. return false;
  310. }
  311. return Op.OperandB != null ||
  312. Op.OperandC != null;
  313. }
  314. private string GetName(ShaderIrOperCbuf Cbuf)
  315. {
  316. if (!Decl.Uniforms.TryGetValue((Cbuf.Index, Cbuf.Offs), out ShaderDeclInfo DeclInfo))
  317. {
  318. throw new InvalidOperationException();
  319. }
  320. return DeclInfo.Name;
  321. }
  322. private string GetOutAbufName(ShaderIrOperAbuf Abuf)
  323. {
  324. return GetName(Decl.OutAttributes, Abuf);
  325. }
  326. private string GetName(ShaderIrOperAbuf Abuf)
  327. {
  328. if (Abuf.Offs == GlslDecl.VertexIdAttr)
  329. {
  330. return "gl_VertexID";
  331. }
  332. return GetName(Decl.InAttributes, Abuf);
  333. }
  334. private string GetName(IReadOnlyDictionary<int, ShaderDeclInfo> Dict, ShaderIrOperAbuf Abuf)
  335. {
  336. int Index = Abuf.Offs >> 4;
  337. int Elem = (Abuf.Offs >> 2) & 3;
  338. if (!Dict.TryGetValue(Index, out ShaderDeclInfo DeclInfo))
  339. {
  340. throw new InvalidOperationException();
  341. }
  342. return DeclInfo.Size > 1 ? DeclInfo.Name + "." + GetAttrSwizzle(Elem) : DeclInfo.Name;
  343. }
  344. private string GetName(ShaderIrOperGpr Gpr)
  345. {
  346. return Gpr.IsConst ? "0" : GetNameWithSwizzle(Decl.Gprs, Gpr.Index);
  347. }
  348. private string GetValue(ShaderIrOperImm Imm)
  349. {
  350. //Only use hex is the value is too big and would likely be hard to read as int.
  351. if (Imm.Value > 0xfff ||
  352. Imm.Value < -0xfff)
  353. {
  354. return "0x" + Imm.Value.ToString("x8", CultureInfo.InvariantCulture);
  355. }
  356. else
  357. {
  358. return Imm.Value.ToString(CultureInfo.InvariantCulture);
  359. }
  360. }
  361. private string GetValue(ShaderIrOperImmf Immf)
  362. {
  363. return Immf.Value.ToString(CultureInfo.InvariantCulture);
  364. }
  365. private string GetName(ShaderIrOperPred Pred)
  366. {
  367. return Pred.IsConst ? "true" : GetNameWithSwizzle(Decl.Preds, Pred.Index);
  368. }
  369. private string GetNameWithSwizzle(IReadOnlyDictionary<int, ShaderDeclInfo> Dict, int Index)
  370. {
  371. int VecIndex = Index >> 2;
  372. if (Dict.TryGetValue(VecIndex, out ShaderDeclInfo DeclInfo))
  373. {
  374. if (DeclInfo.Size > 1 && Index < VecIndex + DeclInfo.Size)
  375. {
  376. return DeclInfo.Name + "." + GetAttrSwizzle(Index & 3);
  377. }
  378. }
  379. if (!Dict.TryGetValue(Index, out DeclInfo))
  380. {
  381. throw new InvalidOperationException();
  382. }
  383. return DeclInfo.Name;
  384. }
  385. private string GetAttrSwizzle(int Elem)
  386. {
  387. return "xyzw".Substring(Elem, 1);
  388. }
  389. private string GetAndExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "&");
  390. private string GetAsrExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">>");
  391. private string GetBandExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "&&");
  392. private string GetBnotExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "!");
  393. private string GetCeilExpr(ShaderIrOp Op) => GetUnaryCall(Op, "ceil");
  394. private string GetClampsExpr(ShaderIrOp Op)
  395. {
  396. return "clamp(" + GetOperExpr(Op, Op.OperandA) + ", " +
  397. GetOperExpr(Op, Op.OperandB) + ", " +
  398. GetOperExpr(Op, Op.OperandC) + ")";
  399. }
  400. private string GetClampuExpr(ShaderIrOp Op)
  401. {
  402. return "int(clamp(uint(" + GetOperExpr(Op, Op.OperandA) + "), " +
  403. "uint(" + GetOperExpr(Op, Op.OperandB) + "), " +
  404. "uint(" + GetOperExpr(Op, Op.OperandC) + ")))";
  405. }
  406. private string GetCltExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "<");
  407. private string GetCeqExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "==");
  408. private string GetCleExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "<=");
  409. private string GetCgtExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">");
  410. private string GetCneExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "!=");
  411. private string GetCgeExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">=");
  412. private string GetExitExpr(ShaderIrOp Op) => "return";
  413. private string GetFabsExpr(ShaderIrOp Op) => GetUnaryCall(Op, "abs");
  414. private string GetFaddExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "+");
  415. private string GetFcosExpr(ShaderIrOp Op) => GetUnaryCall(Op, "cos");
  416. private string GetFex2Expr(ShaderIrOp Op) => GetUnaryCall(Op, "exp2");
  417. private string GetFfmaExpr(ShaderIrOp Op) => GetTernaryExpr(Op, "*", "+");
  418. private string GetFclampExpr(ShaderIrOp Op) => GetTernaryCall(Op, "clamp");
  419. private string GetFlg2Expr(ShaderIrOp Op) => GetUnaryCall(Op, "log2");
  420. private string GetFloorExpr(ShaderIrOp Op) => GetUnaryCall(Op, "floor");
  421. private string GetFmulExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "*");
  422. private string GetFnegExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "-");
  423. private string GetFrcpExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "1 / ");
  424. private string GetFrsqExpr(ShaderIrOp Op) => GetUnaryCall(Op, "inversesqrt");
  425. private string GetFsinExpr(ShaderIrOp Op) => GetUnaryCall(Op, "sin");
  426. private string GetFtosExpr(ShaderIrOp Op)
  427. {
  428. return "int(" + GetOperExpr(Op, Op.OperandA) + ")";
  429. }
  430. private string GetFtouExpr(ShaderIrOp Op)
  431. {
  432. return "int(uint(" + GetOperExpr(Op, Op.OperandA) + "))";
  433. }
  434. private string GetIpaExpr(ShaderIrOp Op) => GetSrcExpr(Op.OperandA);
  435. private string GetKilExpr(ShaderIrOp Op) => "discard";
  436. private string GetLsrExpr(ShaderIrOp Op)
  437. {
  438. return "int(uint(" + GetOperExpr(Op, Op.OperandA) + ") >> " +
  439. GetOperExpr(Op, Op.OperandB) + ")";
  440. }
  441. private string GetNotExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "~");
  442. private string GetOrExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "|");
  443. private string GetStofExpr(ShaderIrOp Op)
  444. {
  445. return "float(" + GetOperExpr(Op, Op.OperandA) + ")";
  446. }
  447. private string GetTexqExpr(ShaderIrOp Op)
  448. {
  449. ShaderIrMetaTexq Meta = (ShaderIrMetaTexq)Op.MetaData;
  450. string Ch = "xyzw".Substring(Meta.Elem, 1);
  451. if (Meta.Info == ShaderTexqInfo.Dimension)
  452. {
  453. string Sampler = GetTexSamplerName(Op);
  454. string Lod = GetOperExpr(Op, Op.OperandA); //???
  455. return "textureSize(" + Sampler + ", " + Lod + ")." + Ch;
  456. }
  457. else
  458. {
  459. throw new NotImplementedException(Meta.Info.ToString());
  460. }
  461. }
  462. private string GetTexsExpr(ShaderIrOp Op)
  463. {
  464. ShaderIrMetaTex Meta = (ShaderIrMetaTex)Op.MetaData;
  465. string Sampler = GetTexSamplerName(Op);
  466. string Coords = GetTexSamplerCoords(Op);
  467. string Ch = "rgba".Substring(Meta.Elem, 1);
  468. return "texture(" + Sampler + ", " + Coords + ")." + Ch;
  469. }
  470. private string GetTxlfExpr(ShaderIrOp Op)
  471. {
  472. ShaderIrMetaTex Meta = (ShaderIrMetaTex)Op.MetaData;
  473. string Sampler = GetTexSamplerName(Op);
  474. string Coords = GetITexSamplerCoords(Op);
  475. string Ch = "rgba".Substring(Meta.Elem, 1);
  476. return "texelFetch(" + Sampler + ", " + Coords + ", 0)." + Ch;
  477. }
  478. private string GetTruncExpr(ShaderIrOp Op) => GetUnaryCall(Op, "trunc");
  479. private string GetUtofExpr(ShaderIrOp Op)
  480. {
  481. return "float(uint(" + GetOperExpr(Op, Op.OperandA) + "))";
  482. }
  483. private string GetXorExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "^");
  484. private string GetUnaryCall(ShaderIrOp Op, string FuncName)
  485. {
  486. return FuncName + "(" + GetOperExpr(Op, Op.OperandA) + ")";
  487. }
  488. private string GetTernaryCall(ShaderIrOp Op, string FuncName)
  489. {
  490. return FuncName + "(" + GetOperExpr(Op, Op.OperandA) + ", " +
  491. GetOperExpr(Op, Op.OperandB) + ", " +
  492. GetOperExpr(Op, Op.OperandC) + ")";
  493. }
  494. private string GetUnaryExpr(ShaderIrOp Op, string Opr)
  495. {
  496. return Opr + GetOperExpr(Op, Op.OperandA);
  497. }
  498. private string GetBinaryExpr(ShaderIrOp Op, string Opr)
  499. {
  500. return GetOperExpr(Op, Op.OperandA) + " " + Opr + " " +
  501. GetOperExpr(Op, Op.OperandB);
  502. }
  503. private string GetTernaryExpr(ShaderIrOp Op, string Opr1, string Opr2)
  504. {
  505. return GetOperExpr(Op, Op.OperandA) + " " + Opr1 + " " +
  506. GetOperExpr(Op, Op.OperandB) + " " + Opr2 + " " +
  507. GetOperExpr(Op, Op.OperandC);
  508. }
  509. private string GetTexSamplerName(ShaderIrOp Op)
  510. {
  511. ShaderIrOperImm Node = (ShaderIrOperImm)Op.OperandC;
  512. int Handle = ((ShaderIrOperImm)Op.OperandC).Value;
  513. if (!Decl.Textures.TryGetValue(Handle, out ShaderDeclInfo DeclInfo))
  514. {
  515. throw new InvalidOperationException();
  516. }
  517. return DeclInfo.Name;
  518. }
  519. private string GetTexSamplerCoords(ShaderIrOp Op)
  520. {
  521. return "vec2(" + GetOperExpr(Op, Op.OperandA) + ", " +
  522. GetOperExpr(Op, Op.OperandB) + ")";
  523. }
  524. private string GetITexSamplerCoords(ShaderIrOp Op)
  525. {
  526. return "ivec2(" + GetOperExpr(Op, Op.OperandA) + ", " +
  527. GetOperExpr(Op, Op.OperandB) + ")";
  528. }
  529. private string GetOperExpr(ShaderIrOp Op, ShaderIrNode Oper)
  530. {
  531. return GetExprWithCast(Op, Oper, GetSrcExpr(Oper));
  532. }
  533. private static string GetExprWithCast(ShaderIrNode Dst, ShaderIrNode Src, string Expr)
  534. {
  535. //Note: The "DstType" (of the cast) is the type that the operation
  536. //uses on the source operands, while the "SrcType" is the destination
  537. //type of the operand result (if it is a operation) or just the type
  538. //of the variable for registers/uniforms/attributes.
  539. OperType DstType = GetSrcNodeType(Dst);
  540. OperType SrcType = GetDstNodeType(Src);
  541. if (DstType != SrcType)
  542. {
  543. //Check for invalid casts
  544. //(like bool to int/float and others).
  545. if (SrcType != OperType.F32 &&
  546. SrcType != OperType.I32)
  547. {
  548. throw new InvalidOperationException();
  549. }
  550. switch (Src)
  551. {
  552. case ShaderIrOperGpr Gpr:
  553. {
  554. //When the Gpr is ZR, just return the 0 value directly,
  555. //since the float encoding for 0 is 0.
  556. if (Gpr.IsConst)
  557. {
  558. return "0";
  559. }
  560. break;
  561. }
  562. case ShaderIrOperImm Imm:
  563. {
  564. //For integer immediates being used as float,
  565. //it's better (for readability) to just return the float value.
  566. if (DstType == OperType.F32)
  567. {
  568. float Value = BitConverter.Int32BitsToSingle(Imm.Value);
  569. return Value.ToString(CultureInfo.InvariantCulture);
  570. }
  571. break;
  572. }
  573. }
  574. switch (DstType)
  575. {
  576. case OperType.F32: Expr = "intBitsToFloat(" + Expr + ")"; break;
  577. case OperType.I32: Expr = "floatBitsToInt(" + Expr + ")"; break;
  578. }
  579. }
  580. return Expr;
  581. }
  582. private static OperType GetDstNodeType(ShaderIrNode Node)
  583. {
  584. //Special case instructions with the result type different
  585. //from the input types (like integer <-> float conversion) here.
  586. if (Node is ShaderIrOp Op)
  587. {
  588. switch (Op.Inst)
  589. {
  590. case ShaderIrInst.Stof:
  591. case ShaderIrInst.Txlf:
  592. case ShaderIrInst.Utof:
  593. return OperType.F32;
  594. case ShaderIrInst.Ftos:
  595. case ShaderIrInst.Ftou:
  596. return OperType.I32;
  597. }
  598. }
  599. return GetSrcNodeType(Node);
  600. }
  601. private static OperType GetSrcNodeType(ShaderIrNode Node)
  602. {
  603. switch (Node)
  604. {
  605. case ShaderIrOperAbuf Abuf:
  606. return Abuf.Offs == GlslDecl.VertexIdAttr
  607. ? OperType.I32
  608. : OperType.F32;
  609. case ShaderIrOperCbuf Cbuf: return OperType.F32;
  610. case ShaderIrOperGpr Gpr: return OperType.F32;
  611. case ShaderIrOperImm Imm: return OperType.I32;
  612. case ShaderIrOperImmf Immf: return OperType.F32;
  613. case ShaderIrOperPred Pred: return OperType.Bool;
  614. case ShaderIrOp Op:
  615. if (Op.Inst > ShaderIrInst.B_Start &&
  616. Op.Inst < ShaderIrInst.B_End)
  617. {
  618. return OperType.Bool;
  619. }
  620. else if (Op.Inst > ShaderIrInst.F_Start &&
  621. Op.Inst < ShaderIrInst.F_End)
  622. {
  623. return OperType.F32;
  624. }
  625. else if (Op.Inst > ShaderIrInst.I_Start &&
  626. Op.Inst < ShaderIrInst.I_End)
  627. {
  628. return OperType.I32;
  629. }
  630. break;
  631. }
  632. throw new ArgumentException(nameof(Node));
  633. }
  634. }
  635. }