GlslDecompiler.cs 25 KB

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