GlslDecompiler.cs 25 KB

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