GlslDecompiler.cs 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  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. public 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 const int MaxVertexInput = 3;
  20. private static string[] ElemTypes = new string[] { "float", "vec2", "vec3", "vec4" };
  21. private GlslDecl Decl;
  22. private ShaderHeader Header, HeaderB;
  23. private ShaderIrBlock[] Blocks, BlocksB;
  24. private StringBuilder SB;
  25. public GlslDecompiler()
  26. {
  27. InstsExpr = new Dictionary<ShaderIrInst, GetInstExpr>()
  28. {
  29. { ShaderIrInst.Abs, GetAbsExpr },
  30. { ShaderIrInst.Add, GetAddExpr },
  31. { ShaderIrInst.And, GetAndExpr },
  32. { ShaderIrInst.Asr, GetAsrExpr },
  33. { ShaderIrInst.Band, GetBandExpr },
  34. { ShaderIrInst.Bnot, GetBnotExpr },
  35. { ShaderIrInst.Bor, GetBorExpr },
  36. { ShaderIrInst.Bxor, GetBxorExpr },
  37. { ShaderIrInst.Ceil, GetCeilExpr },
  38. { ShaderIrInst.Ceq, GetCeqExpr },
  39. { ShaderIrInst.Cge, GetCgeExpr },
  40. { ShaderIrInst.Cgt, GetCgtExpr },
  41. { ShaderIrInst.Clamps, GetClampsExpr },
  42. { ShaderIrInst.Clampu, GetClampuExpr },
  43. { ShaderIrInst.Cle, GetCleExpr },
  44. { ShaderIrInst.Clt, GetCltExpr },
  45. { ShaderIrInst.Cne, GetCneExpr },
  46. { ShaderIrInst.Cut, GetCutExpr },
  47. { ShaderIrInst.Exit, GetExitExpr },
  48. { ShaderIrInst.Fabs, GetAbsExpr },
  49. { ShaderIrInst.Fadd, GetAddExpr },
  50. { ShaderIrInst.Fceq, GetCeqExpr },
  51. { ShaderIrInst.Fcequ, GetCequExpr },
  52. { ShaderIrInst.Fcge, GetCgeExpr },
  53. { ShaderIrInst.Fcgeu, GetCgeuExpr },
  54. { ShaderIrInst.Fcgt, GetCgtExpr },
  55. { ShaderIrInst.Fcgtu, GetCgtuExpr },
  56. { ShaderIrInst.Fclamp, GetFclampExpr },
  57. { ShaderIrInst.Fcle, GetCleExpr },
  58. { ShaderIrInst.Fcleu, GetCleuExpr },
  59. { ShaderIrInst.Fclt, GetCltExpr },
  60. { ShaderIrInst.Fcltu, GetCltuExpr },
  61. { ShaderIrInst.Fcnan, GetCnanExpr },
  62. { ShaderIrInst.Fcne, GetCneExpr },
  63. { ShaderIrInst.Fcneu, GetCneuExpr },
  64. { ShaderIrInst.Fcnum, GetCnumExpr },
  65. { ShaderIrInst.Fcos, GetFcosExpr },
  66. { ShaderIrInst.Fex2, GetFex2Expr },
  67. { ShaderIrInst.Ffma, GetFfmaExpr },
  68. { ShaderIrInst.Flg2, GetFlg2Expr },
  69. { ShaderIrInst.Floor, GetFloorExpr },
  70. { ShaderIrInst.Fmax, GetMaxExpr },
  71. { ShaderIrInst.Fmin, GetMinExpr },
  72. { ShaderIrInst.Fmul, GetMulExpr },
  73. { ShaderIrInst.Fneg, GetNegExpr },
  74. { ShaderIrInst.Frcp, GetFrcpExpr },
  75. { ShaderIrInst.Frsq, GetFrsqExpr },
  76. { ShaderIrInst.Fsin, GetFsinExpr },
  77. { ShaderIrInst.Fsqrt, GetFsqrtExpr },
  78. { ShaderIrInst.Ftos, GetFtosExpr },
  79. { ShaderIrInst.Ftou, GetFtouExpr },
  80. { ShaderIrInst.Ipa, GetIpaExpr },
  81. { ShaderIrInst.Kil, GetKilExpr },
  82. { ShaderIrInst.Lsl, GetLslExpr },
  83. { ShaderIrInst.Lsr, GetLsrExpr },
  84. { ShaderIrInst.Max, GetMaxExpr },
  85. { ShaderIrInst.Min, GetMinExpr },
  86. { ShaderIrInst.Mul, GetMulExpr },
  87. { ShaderIrInst.Neg, GetNegExpr },
  88. { ShaderIrInst.Not, GetNotExpr },
  89. { ShaderIrInst.Or, GetOrExpr },
  90. { ShaderIrInst.Stof, GetStofExpr },
  91. { ShaderIrInst.Sub, GetSubExpr },
  92. { ShaderIrInst.Texq, GetTexqExpr },
  93. { ShaderIrInst.Texs, GetTexsExpr },
  94. { ShaderIrInst.Trunc, GetTruncExpr },
  95. { ShaderIrInst.Txlf, GetTxlfExpr },
  96. { ShaderIrInst.Utof, GetUtofExpr },
  97. { ShaderIrInst.Xor, GetXorExpr }
  98. };
  99. }
  100. public GlslProgram Decompile(
  101. IGalMemory Memory,
  102. long VpAPosition,
  103. long VpBPosition,
  104. GalShaderType ShaderType)
  105. {
  106. Header = new ShaderHeader(Memory, VpAPosition);
  107. HeaderB = new ShaderHeader(Memory, VpBPosition);
  108. Blocks = ShaderDecoder.Decode(Memory, VpAPosition);
  109. BlocksB = ShaderDecoder.Decode(Memory, VpBPosition);
  110. GlslDecl DeclVpA = new GlslDecl(Blocks, ShaderType);
  111. GlslDecl DeclVpB = new GlslDecl(BlocksB, ShaderType);
  112. Decl = GlslDecl.Merge(DeclVpA, DeclVpB);
  113. return Decompile();
  114. }
  115. public GlslProgram Decompile(IGalMemory Memory, long Position, GalShaderType ShaderType)
  116. {
  117. Header = new ShaderHeader(Memory, Position);
  118. HeaderB = null;
  119. Blocks = ShaderDecoder.Decode(Memory, Position);
  120. BlocksB = null;
  121. Decl = new GlslDecl(Blocks, ShaderType);
  122. return Decompile();
  123. }
  124. private GlslProgram Decompile()
  125. {
  126. SB = new StringBuilder();
  127. SB.AppendLine("#version 410 core");
  128. PrintDeclHeader();
  129. PrintDeclTextures();
  130. PrintDeclUniforms();
  131. PrintDeclAttributes();
  132. PrintDeclInAttributes();
  133. PrintDeclOutAttributes();
  134. PrintDeclGprs();
  135. PrintDeclPreds();
  136. if (BlocksB != null)
  137. {
  138. PrintBlockScope(Blocks[0], null, null, "void " + GlslDecl.ProgramAName + "()", IdentationStr);
  139. SB.AppendLine();
  140. PrintBlockScope(BlocksB[0], null, null, "void " + GlslDecl.ProgramBName + "()", IdentationStr);
  141. }
  142. else
  143. {
  144. PrintBlockScope(Blocks[0], null, null, "void " + GlslDecl.ProgramName + "()", IdentationStr);
  145. }
  146. SB.AppendLine();
  147. PrintMain();
  148. string GlslCode = SB.ToString();
  149. return new GlslProgram(
  150. GlslCode,
  151. Decl.Textures.Values,
  152. Decl.Uniforms.Values);
  153. }
  154. private void PrintDeclHeader()
  155. {
  156. if (Decl.ShaderType == GalShaderType.Geometry)
  157. {
  158. int MaxVertices = Header.MaxOutputVertexCount;
  159. string OutputTopology;
  160. switch (Header.OutputTopology)
  161. {
  162. case ShaderHeader.PointList: OutputTopology = "points"; break;
  163. case ShaderHeader.LineStrip: OutputTopology = "line_strip"; break;
  164. case ShaderHeader.TriangleStrip: OutputTopology = "triangle_strip"; break;
  165. default: throw new InvalidOperationException();
  166. }
  167. SB.AppendLine("#extension GL_ARB_enhanced_layouts : require");
  168. SB.AppendLine();
  169. SB.AppendLine("// Stubbed. Maxwell geometry shaders don't inform input geometry type");
  170. SB.AppendLine("layout(triangles) in;" + Environment.NewLine);
  171. SB.AppendLine($"layout({OutputTopology}, max_vertices = {MaxVertices}) out;");
  172. SB.AppendLine();
  173. }
  174. }
  175. private void PrintDeclTextures()
  176. {
  177. PrintDecls(Decl.Textures, "uniform sampler2D");
  178. }
  179. private void PrintDeclUniforms()
  180. {
  181. if (Decl.ShaderType == GalShaderType.Vertex)
  182. {
  183. SB.AppendLine("uniform vec2 " + GlslDecl.FlipUniformName + ";");
  184. }
  185. SB.AppendLine();
  186. foreach (ShaderDeclInfo DeclInfo in Decl.Uniforms.Values.OrderBy(DeclKeySelector))
  187. {
  188. SB.AppendLine($"layout (std140) uniform {DeclInfo.Name} {{");
  189. SB.AppendLine($"{IdentationStr}vec4 {DeclInfo.Name}_data[{GlslDecl.MaxUboSize}];");
  190. SB.AppendLine("};");
  191. }
  192. if (Decl.Uniforms.Count > 0)
  193. {
  194. SB.AppendLine();
  195. }
  196. }
  197. private void PrintDeclAttributes()
  198. {
  199. string GeometryArray = (Decl.ShaderType == GalShaderType.Geometry) ? "[" + MaxVertexInput + "]" : "";
  200. PrintDecls(Decl.Attributes, Suffix: GeometryArray);
  201. }
  202. private void PrintDeclInAttributes()
  203. {
  204. if (Decl.ShaderType == GalShaderType.Fragment)
  205. {
  206. SB.AppendLine("layout (location = " + GlslDecl.PositionOutAttrLocation + ") in vec4 " + GlslDecl.PositionOutAttrName + ";");
  207. }
  208. if (Decl.ShaderType == GalShaderType.Geometry)
  209. {
  210. if (Decl.InAttributes.Count > 0)
  211. {
  212. SB.AppendLine("in Vertex {");
  213. foreach (ShaderDeclInfo DeclInfo in Decl.InAttributes.Values.OrderBy(DeclKeySelector))
  214. {
  215. if (DeclInfo.Index >= 0)
  216. {
  217. SB.AppendLine(IdentationStr + "layout (location = " + DeclInfo.Index + ") " + GetDecl(DeclInfo) + "; ");
  218. }
  219. }
  220. SB.AppendLine("} block_in[];" + Environment.NewLine);
  221. }
  222. }
  223. else
  224. {
  225. PrintDeclAttributes(Decl.InAttributes.Values, "in");
  226. }
  227. }
  228. private void PrintDeclOutAttributes()
  229. {
  230. if (Decl.ShaderType != GalShaderType.Fragment)
  231. {
  232. SB.AppendLine("layout (location = " + GlslDecl.PositionOutAttrLocation + ") out vec4 " + GlslDecl.PositionOutAttrName + ";");
  233. }
  234. PrintDeclAttributes(Decl.OutAttributes.Values, "out");
  235. }
  236. private void PrintDeclAttributes(IEnumerable<ShaderDeclInfo> Decls, string InOut)
  237. {
  238. int Count = 0;
  239. foreach (ShaderDeclInfo DeclInfo in Decls.OrderBy(DeclKeySelector))
  240. {
  241. if (DeclInfo.Index >= 0)
  242. {
  243. SB.AppendLine("layout (location = " + DeclInfo.Index + ") " + InOut + " " + GetDecl(DeclInfo) + ";");
  244. Count++;
  245. }
  246. }
  247. if (Count > 0)
  248. {
  249. SB.AppendLine();
  250. }
  251. }
  252. private void PrintDeclGprs()
  253. {
  254. PrintDecls(Decl.Gprs);
  255. }
  256. private void PrintDeclPreds()
  257. {
  258. PrintDecls(Decl.Preds, "bool");
  259. }
  260. private void PrintDecls(IReadOnlyDictionary<int, ShaderDeclInfo> Dict, string CustomType = null, string Suffix = "")
  261. {
  262. foreach (ShaderDeclInfo DeclInfo in Dict.Values.OrderBy(DeclKeySelector))
  263. {
  264. string Name;
  265. if (CustomType != null)
  266. {
  267. Name = CustomType + " " + DeclInfo.Name + Suffix + ";";
  268. }
  269. else if (DeclInfo.Name == GlslDecl.FragmentOutputName)
  270. {
  271. Name = "layout (location = 0) out " + GetDecl(DeclInfo) + Suffix + ";" + Environment.NewLine;
  272. }
  273. else
  274. {
  275. Name = GetDecl(DeclInfo) + Suffix + ";";
  276. }
  277. SB.AppendLine(Name);
  278. }
  279. if (Dict.Count > 0)
  280. {
  281. SB.AppendLine();
  282. }
  283. }
  284. private int DeclKeySelector(ShaderDeclInfo DeclInfo)
  285. {
  286. return DeclInfo.Cbuf << 24 | DeclInfo.Index;
  287. }
  288. private string GetDecl(ShaderDeclInfo DeclInfo)
  289. {
  290. return ElemTypes[DeclInfo.Size - 1] + " " + DeclInfo.Name;
  291. }
  292. private void PrintMain()
  293. {
  294. SB.AppendLine("void main() {");
  295. foreach (KeyValuePair<int, ShaderDeclInfo> KV in Decl.InAttributes)
  296. {
  297. if (!Decl.Attributes.TryGetValue(KV.Key, out ShaderDeclInfo Attr))
  298. {
  299. continue;
  300. }
  301. ShaderDeclInfo DeclInfo = KV.Value;
  302. string Swizzle = ".xyzw".Substring(0, DeclInfo.Size + 1);
  303. if (Decl.ShaderType == GalShaderType.Geometry)
  304. {
  305. for (int Vertex = 0; Vertex < MaxVertexInput; Vertex++)
  306. {
  307. string Dst = Attr.Name + "[" + Vertex + "]" + Swizzle;
  308. string Src = "block_in[" + Vertex + "]." + DeclInfo.Name;
  309. SB.AppendLine(IdentationStr + Dst + " = " + Src + ";");
  310. }
  311. }
  312. else
  313. {
  314. SB.AppendLine(IdentationStr + Attr.Name + Swizzle + " = " + DeclInfo.Name + ";");
  315. }
  316. }
  317. if (BlocksB != null)
  318. {
  319. SB.AppendLine(IdentationStr + GlslDecl.ProgramAName + "();");
  320. SB.AppendLine(IdentationStr + GlslDecl.ProgramBName + "();");
  321. }
  322. else
  323. {
  324. SB.AppendLine(IdentationStr + GlslDecl.ProgramName + "();");
  325. }
  326. if (Decl.ShaderType != GalShaderType.Geometry)
  327. {
  328. PrintAttrToOutput();
  329. }
  330. SB.AppendLine("}");
  331. }
  332. private void PrintAttrToOutput(string Identation = IdentationStr)
  333. {
  334. foreach (KeyValuePair<int, ShaderDeclInfo> KV in Decl.OutAttributes)
  335. {
  336. if (!Decl.Attributes.TryGetValue(KV.Key, out ShaderDeclInfo Attr))
  337. {
  338. continue;
  339. }
  340. ShaderDeclInfo DeclInfo = KV.Value;
  341. string Swizzle = ".xyzw".Substring(0, DeclInfo.Size + 1);
  342. string Name = Attr.Name;
  343. if (Decl.ShaderType == GalShaderType.Geometry)
  344. {
  345. Name += "[0]";
  346. }
  347. SB.AppendLine(Identation + DeclInfo.Name + " = " + Name + Swizzle + ";");
  348. }
  349. if (Decl.ShaderType == GalShaderType.Vertex)
  350. {
  351. SB.AppendLine(Identation + "gl_Position.xy *= " + GlslDecl.FlipUniformName + ";");
  352. }
  353. if (Decl.ShaderType != GalShaderType.Fragment)
  354. {
  355. SB.AppendLine(Identation + GlslDecl.PositionOutAttrName + " = gl_Position;");
  356. SB.AppendLine(Identation + GlslDecl.PositionOutAttrName + ".w = 1;");
  357. }
  358. }
  359. private void PrintBlockScope(
  360. ShaderIrBlock Block,
  361. ShaderIrBlock EndBlock,
  362. ShaderIrBlock LoopBlock,
  363. string ScopeName,
  364. string Identation,
  365. bool IsDoWhile = false)
  366. {
  367. string UpIdent = Identation.Substring(0, Identation.Length - IdentationStr.Length);
  368. if (IsDoWhile)
  369. {
  370. SB.AppendLine(UpIdent + "do {");
  371. }
  372. else
  373. {
  374. SB.AppendLine(UpIdent + ScopeName + " {");
  375. }
  376. while (Block != null && Block != EndBlock)
  377. {
  378. ShaderIrNode[] Nodes = Block.GetNodes();
  379. Block = PrintNodes(Block, EndBlock, LoopBlock, Identation, Nodes);
  380. }
  381. if (IsDoWhile)
  382. {
  383. SB.AppendLine(UpIdent + "} " + ScopeName + ";");
  384. }
  385. else
  386. {
  387. SB.AppendLine(UpIdent + "}");
  388. }
  389. }
  390. private ShaderIrBlock PrintNodes(
  391. ShaderIrBlock Block,
  392. ShaderIrBlock EndBlock,
  393. ShaderIrBlock LoopBlock,
  394. string Identation,
  395. params ShaderIrNode[] Nodes)
  396. {
  397. /*
  398. * Notes about control flow and if-else/loop generation:
  399. * The code assumes that the program has sane control flow,
  400. * that is, there's no jumps to a location after another jump or
  401. * jump target (except for the end of an if-else block), and backwards
  402. * jumps to a location before the last loop dominator.
  403. * Such cases needs to be transformed on a step before the GLSL code
  404. * generation to ensure that we have sane graphs to work with.
  405. * TODO: Such transformation is not yet implemented.
  406. */
  407. string NewIdent = Identation + IdentationStr;
  408. ShaderIrBlock LoopTail = GetLoopTailBlock(Block);
  409. if (LoopTail != null && LoopBlock != Block)
  410. {
  411. //Shoock! kuma shock! We have a loop here!
  412. //The entire sequence needs to be inside a do-while block.
  413. ShaderIrBlock LoopEnd = GetDownBlock(LoopTail);
  414. PrintBlockScope(Block, LoopEnd, Block, "while (false)", NewIdent, IsDoWhile: true);
  415. return LoopEnd;
  416. }
  417. foreach (ShaderIrNode Node in Nodes)
  418. {
  419. if (Node is ShaderIrCond Cond)
  420. {
  421. string IfExpr = GetSrcExpr(Cond.Pred, true);
  422. if (Cond.Not)
  423. {
  424. IfExpr = "!(" + IfExpr + ")";
  425. }
  426. if (Cond.Child is ShaderIrOp Op && Op.Inst == ShaderIrInst.Bra)
  427. {
  428. //Branch is a loop branch and would result in infinite recursion.
  429. if (Block.Branch.Position <= Block.Position)
  430. {
  431. SB.AppendLine(Identation + "if (" + IfExpr + ") {");
  432. SB.AppendLine(Identation + IdentationStr + "continue;");
  433. SB.AppendLine(Identation + "}");
  434. continue;
  435. }
  436. string SubScopeName = "if (!" + IfExpr + ")";
  437. PrintBlockScope(Block.Next, Block.Branch, LoopBlock, SubScopeName, NewIdent);
  438. ShaderIrBlock IfElseEnd = GetUpBlock(Block.Branch).Branch;
  439. if (IfElseEnd?.Position > Block.Branch.Position)
  440. {
  441. PrintBlockScope(Block.Branch, IfElseEnd, LoopBlock, "else", NewIdent);
  442. return IfElseEnd;
  443. }
  444. return Block.Branch;
  445. }
  446. else
  447. {
  448. SB.AppendLine(Identation + "if (" + IfExpr + ") {");
  449. PrintNodes(Block, EndBlock, LoopBlock, NewIdent, Cond.Child);
  450. SB.AppendLine(Identation + "}");
  451. }
  452. }
  453. else if (Node is ShaderIrAsg Asg)
  454. {
  455. if (IsValidOutOper(Asg.Dst))
  456. {
  457. string Expr = GetSrcExpr(Asg.Src, true);
  458. Expr = GetExprWithCast(Asg.Dst, Asg.Src, Expr);
  459. SB.AppendLine(Identation + GetDstOperName(Asg.Dst) + " = " + Expr + ";");
  460. }
  461. }
  462. else if (Node is ShaderIrOp Op)
  463. {
  464. if (Op.Inst == ShaderIrInst.Bra)
  465. {
  466. if (Block.Branch.Position <= Block.Position)
  467. {
  468. SB.AppendLine(Identation + "continue;");
  469. }
  470. }
  471. else if (Op.Inst == ShaderIrInst.Emit)
  472. {
  473. PrintAttrToOutput(Identation);
  474. SB.AppendLine(Identation + "EmitVertex();");
  475. }
  476. else
  477. {
  478. SB.AppendLine(Identation + GetSrcExpr(Op, true) + ";");
  479. }
  480. }
  481. else if (Node is ShaderIrCmnt Cmnt)
  482. {
  483. SB.AppendLine(Identation + "// " + Cmnt.Comment);
  484. }
  485. else
  486. {
  487. throw new InvalidOperationException();
  488. }
  489. }
  490. return Block.Next;
  491. }
  492. private ShaderIrBlock GetUpBlock(ShaderIrBlock Block)
  493. {
  494. return Blocks.FirstOrDefault(x => x.EndPosition == Block.Position);
  495. }
  496. private ShaderIrBlock GetDownBlock(ShaderIrBlock Block)
  497. {
  498. return Blocks.FirstOrDefault(x => x.Position == Block.EndPosition);
  499. }
  500. private ShaderIrBlock GetLoopTailBlock(ShaderIrBlock LoopHead)
  501. {
  502. ShaderIrBlock Tail = null;
  503. foreach (ShaderIrBlock Block in LoopHead.Sources)
  504. {
  505. if (Block.Position >= LoopHead.Position)
  506. {
  507. if (Tail == null || Tail.Position < Block.Position)
  508. {
  509. Tail = Block;
  510. }
  511. }
  512. }
  513. return Tail;
  514. }
  515. private bool IsValidOutOper(ShaderIrNode Node)
  516. {
  517. if (Node is ShaderIrOperGpr Gpr && Gpr.IsConst)
  518. {
  519. return false;
  520. }
  521. else if (Node is ShaderIrOperPred Pred && Pred.IsConst)
  522. {
  523. return false;
  524. }
  525. return true;
  526. }
  527. private string GetDstOperName(ShaderIrNode Node)
  528. {
  529. if (Node is ShaderIrOperAbuf Abuf)
  530. {
  531. return GetOutAbufName(Abuf);
  532. }
  533. else if (Node is ShaderIrOperGpr Gpr)
  534. {
  535. return GetName(Gpr);
  536. }
  537. else if (Node is ShaderIrOperPred Pred)
  538. {
  539. return GetName(Pred);
  540. }
  541. throw new ArgumentException(nameof(Node));
  542. }
  543. private string GetSrcExpr(ShaderIrNode Node, bool Entry = false)
  544. {
  545. switch (Node)
  546. {
  547. case ShaderIrOperAbuf Abuf: return GetName (Abuf);
  548. case ShaderIrOperCbuf Cbuf: return GetName (Cbuf);
  549. case ShaderIrOperGpr Gpr: return GetName (Gpr);
  550. case ShaderIrOperImm Imm: return GetValue(Imm);
  551. case ShaderIrOperImmf Immf: return GetValue(Immf);
  552. case ShaderIrOperPred Pred: return GetName (Pred);
  553. case ShaderIrOp Op:
  554. string Expr;
  555. if (InstsExpr.TryGetValue(Op.Inst, out GetInstExpr GetExpr))
  556. {
  557. Expr = GetExpr(Op);
  558. }
  559. else
  560. {
  561. throw new NotImplementedException(Op.Inst.ToString());
  562. }
  563. if (!Entry && NeedsParentheses(Op))
  564. {
  565. Expr = "(" + Expr + ")";
  566. }
  567. return Expr;
  568. default: throw new ArgumentException(nameof(Node));
  569. }
  570. }
  571. private static bool NeedsParentheses(ShaderIrOp Op)
  572. {
  573. switch (Op.Inst)
  574. {
  575. case ShaderIrInst.Ipa:
  576. case ShaderIrInst.Texq:
  577. case ShaderIrInst.Texs:
  578. case ShaderIrInst.Txlf:
  579. return false;
  580. }
  581. return true;
  582. }
  583. private string GetName(ShaderIrOperCbuf Cbuf)
  584. {
  585. if (!Decl.Uniforms.TryGetValue(Cbuf.Index, out ShaderDeclInfo DeclInfo))
  586. {
  587. throw new InvalidOperationException();
  588. }
  589. if (Cbuf.Offs != null)
  590. {
  591. string Offset = "floatBitsToInt(" + GetSrcExpr(Cbuf.Offs) + ")";
  592. string Index = "(" + Cbuf.Pos * 4 + " + " + Offset + ")";
  593. return $"{DeclInfo.Name}_data[{Index} / 16][({Index} / 4) % 4]";
  594. }
  595. else
  596. {
  597. return $"{DeclInfo.Name}_data[{Cbuf.Pos / 4}][{Cbuf.Pos % 4}]";
  598. }
  599. }
  600. private string GetOutAbufName(ShaderIrOperAbuf Abuf)
  601. {
  602. if (Decl.ShaderType == GalShaderType.Geometry)
  603. {
  604. switch (Abuf.Offs)
  605. {
  606. case GlslDecl.LayerAttr: return "gl_Layer";
  607. }
  608. }
  609. return GetAttrTempName(Abuf);
  610. }
  611. private string GetName(ShaderIrOperAbuf Abuf)
  612. {
  613. //Handle special scalar read-only attributes here.
  614. if (Decl.ShaderType == GalShaderType.Vertex)
  615. {
  616. switch (Abuf.Offs)
  617. {
  618. case GlslDecl.VertexIdAttr: return "gl_VertexID";
  619. case GlslDecl.InstanceIdAttr: return "gl_InstanceID";
  620. }
  621. }
  622. else if (Decl.ShaderType == GalShaderType.TessEvaluation)
  623. {
  624. switch (Abuf.Offs)
  625. {
  626. case GlslDecl.TessCoordAttrX: return "gl_TessCoord.x";
  627. case GlslDecl.TessCoordAttrY: return "gl_TessCoord.y";
  628. case GlslDecl.TessCoordAttrZ: return "gl_TessCoord.z";
  629. }
  630. }
  631. else if (Decl.ShaderType == GalShaderType.Fragment)
  632. {
  633. switch (Abuf.Offs)
  634. {
  635. //Note: It's a guess that Maxwell's face is 1 when gl_FrontFacing == true
  636. case GlslDecl.FaceAttr: return "(gl_FrontFacing ? 1 : 0)";
  637. }
  638. }
  639. return GetAttrTempName(Abuf);
  640. }
  641. private string GetAttrTempName(ShaderIrOperAbuf Abuf)
  642. {
  643. int Index = Abuf.Offs >> 4;
  644. int Elem = (Abuf.Offs >> 2) & 3;
  645. string Swizzle = "." + GetAttrSwizzle(Elem);
  646. if (!Decl.Attributes.TryGetValue(Index, out ShaderDeclInfo DeclInfo))
  647. {
  648. //Handle special vec4 attributes here
  649. //(for example, index 7 is aways gl_Position).
  650. if (Index == GlslDecl.GlPositionVec4Index)
  651. {
  652. string Name =
  653. Decl.ShaderType != GalShaderType.Vertex &&
  654. Decl.ShaderType != GalShaderType.Geometry ? GlslDecl.PositionOutAttrName : "gl_Position";
  655. return Name + Swizzle;
  656. }
  657. throw new InvalidOperationException();
  658. }
  659. if (Decl.ShaderType == GalShaderType.Geometry)
  660. {
  661. string Vertex = "floatBitsToInt(" + GetSrcExpr(Abuf.Vertex) + ")";
  662. return DeclInfo.Name + "[" + Vertex + "]" + Swizzle;
  663. }
  664. else
  665. {
  666. return DeclInfo.Name + Swizzle;
  667. }
  668. }
  669. private string GetName(ShaderIrOperGpr Gpr)
  670. {
  671. return Gpr.IsConst ? "0" : GetNameWithSwizzle(Decl.Gprs, Gpr.Index);
  672. }
  673. private string GetValue(ShaderIrOperImm Imm)
  674. {
  675. //Only use hex is the value is too big and would likely be hard to read as int.
  676. if (Imm.Value > 0xfff ||
  677. Imm.Value < -0xfff)
  678. {
  679. return "0x" + Imm.Value.ToString("x8", CultureInfo.InvariantCulture);
  680. }
  681. else
  682. {
  683. return GetIntConst(Imm.Value);
  684. }
  685. }
  686. private string GetValue(ShaderIrOperImmf Immf)
  687. {
  688. return GetFloatConst(Immf.Value);
  689. }
  690. private string GetName(ShaderIrOperPred Pred)
  691. {
  692. return Pred.IsConst ? "true" : GetNameWithSwizzle(Decl.Preds, Pred.Index);
  693. }
  694. private string GetNameWithSwizzle(IReadOnlyDictionary<int, ShaderDeclInfo> Dict, int Index)
  695. {
  696. int VecIndex = Index >> 2;
  697. if (Dict.TryGetValue(VecIndex, out ShaderDeclInfo DeclInfo))
  698. {
  699. if (DeclInfo.Size > 1 && Index < VecIndex + DeclInfo.Size)
  700. {
  701. return DeclInfo.Name + "." + GetAttrSwizzle(Index & 3);
  702. }
  703. }
  704. if (!Dict.TryGetValue(Index, out DeclInfo))
  705. {
  706. throw new InvalidOperationException();
  707. }
  708. return DeclInfo.Name;
  709. }
  710. private string GetAttrSwizzle(int Elem)
  711. {
  712. return "xyzw".Substring(Elem, 1);
  713. }
  714. private string GetAbsExpr(ShaderIrOp Op) => GetUnaryCall(Op, "abs");
  715. private string GetAddExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "+");
  716. private string GetAndExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "&");
  717. private string GetAsrExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">>");
  718. private string GetBandExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "&&");
  719. private string GetBnotExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "!");
  720. private string GetBorExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "||");
  721. private string GetBxorExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "^^");
  722. private string GetCeilExpr(ShaderIrOp Op) => GetUnaryCall(Op, "ceil");
  723. private string GetClampsExpr(ShaderIrOp Op)
  724. {
  725. return "clamp(" + GetOperExpr(Op, Op.OperandA) + ", " +
  726. GetOperExpr(Op, Op.OperandB) + ", " +
  727. GetOperExpr(Op, Op.OperandC) + ")";
  728. }
  729. private string GetClampuExpr(ShaderIrOp Op)
  730. {
  731. return "int(clamp(uint(" + GetOperExpr(Op, Op.OperandA) + "), " +
  732. "uint(" + GetOperExpr(Op, Op.OperandB) + "), " +
  733. "uint(" + GetOperExpr(Op, Op.OperandC) + ")))";
  734. }
  735. private string GetCeqExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "==");
  736. private string GetCequExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "==");
  737. private string GetCgeExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">=");
  738. private string GetCgeuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, ">=");
  739. private string GetCgtExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">");
  740. private string GetCgtuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, ">");
  741. private string GetCleExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "<=");
  742. private string GetCleuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "<=");
  743. private string GetCltExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "<");
  744. private string GetCltuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "<");
  745. private string GetCnanExpr(ShaderIrOp Op) => GetUnaryCall(Op, "isnan");
  746. private string GetCneExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "!=");
  747. private string GetCutExpr(ShaderIrOp Op) => "EndPrimitive()";
  748. private string GetCneuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "!=");
  749. private string GetCnumExpr(ShaderIrOp Op) => GetUnaryCall(Op, "!isnan");
  750. private string GetExitExpr(ShaderIrOp Op) => "return";
  751. private string GetFcosExpr(ShaderIrOp Op) => GetUnaryCall(Op, "cos");
  752. private string GetFex2Expr(ShaderIrOp Op) => GetUnaryCall(Op, "exp2");
  753. private string GetFfmaExpr(ShaderIrOp Op) => GetTernaryExpr(Op, "*", "+");
  754. private string GetFclampExpr(ShaderIrOp Op) => GetTernaryCall(Op, "clamp");
  755. private string GetFlg2Expr(ShaderIrOp Op) => GetUnaryCall(Op, "log2");
  756. private string GetFloorExpr(ShaderIrOp Op) => GetUnaryCall(Op, "floor");
  757. private string GetFrcpExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "1 / ");
  758. private string GetFrsqExpr(ShaderIrOp Op) => GetUnaryCall(Op, "inversesqrt");
  759. private string GetFsinExpr(ShaderIrOp Op) => GetUnaryCall(Op, "sin");
  760. private string GetFsqrtExpr(ShaderIrOp Op) => GetUnaryCall(Op, "sqrt");
  761. private string GetFtosExpr(ShaderIrOp Op)
  762. {
  763. return "int(" + GetOperExpr(Op, Op.OperandA) + ")";
  764. }
  765. private string GetFtouExpr(ShaderIrOp Op)
  766. {
  767. return "int(uint(" + GetOperExpr(Op, Op.OperandA) + "))";
  768. }
  769. private string GetIpaExpr(ShaderIrOp Op) => GetSrcExpr(Op.OperandA);
  770. private string GetKilExpr(ShaderIrOp Op) => "discard";
  771. private string GetLslExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "<<");
  772. private string GetLsrExpr(ShaderIrOp Op)
  773. {
  774. return "int(uint(" + GetOperExpr(Op, Op.OperandA) + ") >> " +
  775. GetOperExpr(Op, Op.OperandB) + ")";
  776. }
  777. private string GetMaxExpr(ShaderIrOp Op) => GetBinaryCall(Op, "max");
  778. private string GetMinExpr(ShaderIrOp Op) => GetBinaryCall(Op, "min");
  779. private string GetMulExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "*");
  780. private string GetNegExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "-");
  781. private string GetNotExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "~");
  782. private string GetOrExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "|");
  783. private string GetStofExpr(ShaderIrOp Op)
  784. {
  785. return "float(" + GetOperExpr(Op, Op.OperandA) + ")";
  786. }
  787. private string GetSubExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "-");
  788. private string GetTexqExpr(ShaderIrOp Op)
  789. {
  790. ShaderIrMetaTexq Meta = (ShaderIrMetaTexq)Op.MetaData;
  791. string Ch = "xyzw".Substring(Meta.Elem, 1);
  792. if (Meta.Info == ShaderTexqInfo.Dimension)
  793. {
  794. string Sampler = GetTexSamplerName(Op);
  795. string Lod = GetOperExpr(Op, Op.OperandA); //???
  796. return "textureSize(" + Sampler + ", " + Lod + ")." + Ch;
  797. }
  798. else
  799. {
  800. throw new NotImplementedException(Meta.Info.ToString());
  801. }
  802. }
  803. private string GetTexsExpr(ShaderIrOp Op)
  804. {
  805. ShaderIrMetaTex Meta = (ShaderIrMetaTex)Op.MetaData;
  806. string Sampler = GetTexSamplerName(Op);
  807. string Coords = GetTexSamplerCoords(Op);
  808. string Ch = "rgba".Substring(Meta.Elem, 1);
  809. return "texture(" + Sampler + ", " + Coords + ")." + Ch;
  810. }
  811. private string GetTxlfExpr(ShaderIrOp Op)
  812. {
  813. ShaderIrMetaTex Meta = (ShaderIrMetaTex)Op.MetaData;
  814. string Sampler = GetTexSamplerName(Op);
  815. string Coords = GetITexSamplerCoords(Op);
  816. string Ch = "rgba".Substring(Meta.Elem, 1);
  817. return "texelFetch(" + Sampler + ", " + Coords + ", 0)." + Ch;
  818. }
  819. private string GetTruncExpr(ShaderIrOp Op) => GetUnaryCall(Op, "trunc");
  820. private string GetUtofExpr(ShaderIrOp Op)
  821. {
  822. return "float(uint(" + GetOperExpr(Op, Op.OperandA) + "))";
  823. }
  824. private string GetXorExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "^");
  825. private string GetUnaryCall(ShaderIrOp Op, string FuncName)
  826. {
  827. return FuncName + "(" + GetOperExpr(Op, Op.OperandA) + ")";
  828. }
  829. private string GetBinaryCall(ShaderIrOp Op, string FuncName)
  830. {
  831. return FuncName + "(" + GetOperExpr(Op, Op.OperandA) + ", " +
  832. GetOperExpr(Op, Op.OperandB) + ")";
  833. }
  834. private string GetTernaryCall(ShaderIrOp Op, string FuncName)
  835. {
  836. return FuncName + "(" + GetOperExpr(Op, Op.OperandA) + ", " +
  837. GetOperExpr(Op, Op.OperandB) + ", " +
  838. GetOperExpr(Op, Op.OperandC) + ")";
  839. }
  840. private string GetUnaryExpr(ShaderIrOp Op, string Opr)
  841. {
  842. return Opr + GetOperExpr(Op, Op.OperandA);
  843. }
  844. private string GetBinaryExpr(ShaderIrOp Op, string Opr)
  845. {
  846. return GetOperExpr(Op, Op.OperandA) + " " + Opr + " " +
  847. GetOperExpr(Op, Op.OperandB);
  848. }
  849. private string GetBinaryExprWithNaN(ShaderIrOp Op, string Opr)
  850. {
  851. string A = GetOperExpr(Op, Op.OperandA);
  852. string B = GetOperExpr(Op, Op.OperandB);
  853. string NaNCheck =
  854. " || isnan(" + A + ")" +
  855. " || isnan(" + B + ")";
  856. return A + " " + Opr + " " + B + NaNCheck;
  857. }
  858. private string GetTernaryExpr(ShaderIrOp Op, string Opr1, string Opr2)
  859. {
  860. return GetOperExpr(Op, Op.OperandA) + " " + Opr1 + " " +
  861. GetOperExpr(Op, Op.OperandB) + " " + Opr2 + " " +
  862. GetOperExpr(Op, Op.OperandC);
  863. }
  864. private string GetTexSamplerName(ShaderIrOp Op)
  865. {
  866. ShaderIrOperImm Node = (ShaderIrOperImm)Op.OperandC;
  867. int Handle = ((ShaderIrOperImm)Op.OperandC).Value;
  868. if (!Decl.Textures.TryGetValue(Handle, out ShaderDeclInfo DeclInfo))
  869. {
  870. throw new InvalidOperationException();
  871. }
  872. return DeclInfo.Name;
  873. }
  874. private string GetTexSamplerCoords(ShaderIrOp Op)
  875. {
  876. return "vec2(" + GetOperExpr(Op, Op.OperandA) + ", " +
  877. GetOperExpr(Op, Op.OperandB) + ")";
  878. }
  879. private string GetITexSamplerCoords(ShaderIrOp Op)
  880. {
  881. return "ivec2(" + GetOperExpr(Op, Op.OperandA) + ", " +
  882. GetOperExpr(Op, Op.OperandB) + ")";
  883. }
  884. private string GetOperExpr(ShaderIrOp Op, ShaderIrNode Oper)
  885. {
  886. return GetExprWithCast(Op, Oper, GetSrcExpr(Oper));
  887. }
  888. private static string GetExprWithCast(ShaderIrNode Dst, ShaderIrNode Src, string Expr)
  889. {
  890. //Note: The "DstType" (of the cast) is the type that the operation
  891. //uses on the source operands, while the "SrcType" is the destination
  892. //type of the operand result (if it is a operation) or just the type
  893. //of the variable for registers/uniforms/attributes.
  894. OperType DstType = GetSrcNodeType(Dst);
  895. OperType SrcType = GetDstNodeType(Src);
  896. if (DstType != SrcType)
  897. {
  898. //Check for invalid casts
  899. //(like bool to int/float and others).
  900. if (SrcType != OperType.F32 &&
  901. SrcType != OperType.I32)
  902. {
  903. throw new InvalidOperationException();
  904. }
  905. switch (Src)
  906. {
  907. case ShaderIrOperGpr Gpr:
  908. {
  909. //When the Gpr is ZR, just return the 0 value directly,
  910. //since the float encoding for 0 is 0.
  911. if (Gpr.IsConst)
  912. {
  913. return "0";
  914. }
  915. break;
  916. }
  917. case ShaderIrOperImm Imm:
  918. {
  919. //For integer immediates being used as float,
  920. //it's better (for readability) to just return the float value.
  921. if (DstType == OperType.F32)
  922. {
  923. float Value = BitConverter.Int32BitsToSingle(Imm.Value);
  924. if (!float.IsNaN(Value) && !float.IsInfinity(Value))
  925. {
  926. return GetFloatConst(Value);
  927. }
  928. }
  929. break;
  930. }
  931. }
  932. switch (DstType)
  933. {
  934. case OperType.F32: Expr = "intBitsToFloat(" + Expr + ")"; break;
  935. case OperType.I32: Expr = "floatBitsToInt(" + Expr + ")"; break;
  936. }
  937. }
  938. return Expr;
  939. }
  940. private static string GetIntConst(int Value)
  941. {
  942. string Expr = Value.ToString(CultureInfo.InvariantCulture);
  943. return Value < 0 ? "(" + Expr + ")" : Expr;
  944. }
  945. private static string GetFloatConst(float Value)
  946. {
  947. string Expr = Value.ToString(CultureInfo.InvariantCulture);
  948. return Value < 0 ? "(" + Expr + ")" : Expr;
  949. }
  950. private static OperType GetDstNodeType(ShaderIrNode Node)
  951. {
  952. //Special case instructions with the result type different
  953. //from the input types (like integer <-> float conversion) here.
  954. if (Node is ShaderIrOp Op)
  955. {
  956. switch (Op.Inst)
  957. {
  958. case ShaderIrInst.Stof:
  959. case ShaderIrInst.Txlf:
  960. case ShaderIrInst.Utof:
  961. return OperType.F32;
  962. case ShaderIrInst.Ftos:
  963. case ShaderIrInst.Ftou:
  964. return OperType.I32;
  965. }
  966. }
  967. return GetSrcNodeType(Node);
  968. }
  969. private static OperType GetSrcNodeType(ShaderIrNode Node)
  970. {
  971. switch (Node)
  972. {
  973. case ShaderIrOperAbuf Abuf:
  974. return Abuf.Offs == GlslDecl.LayerAttr ||
  975. Abuf.Offs == GlslDecl.InstanceIdAttr ||
  976. Abuf.Offs == GlslDecl.VertexIdAttr ||
  977. Abuf.Offs == GlslDecl.FaceAttr
  978. ? OperType.I32
  979. : OperType.F32;
  980. case ShaderIrOperCbuf Cbuf: return OperType.F32;
  981. case ShaderIrOperGpr Gpr: return OperType.F32;
  982. case ShaderIrOperImm Imm: return OperType.I32;
  983. case ShaderIrOperImmf Immf: return OperType.F32;
  984. case ShaderIrOperPred Pred: return OperType.Bool;
  985. case ShaderIrOp Op:
  986. if (Op.Inst > ShaderIrInst.B_Start &&
  987. Op.Inst < ShaderIrInst.B_End)
  988. {
  989. return OperType.Bool;
  990. }
  991. else if (Op.Inst > ShaderIrInst.F_Start &&
  992. Op.Inst < ShaderIrInst.F_End)
  993. {
  994. return OperType.F32;
  995. }
  996. else if (Op.Inst > ShaderIrInst.I_Start &&
  997. Op.Inst < ShaderIrInst.I_End)
  998. {
  999. return OperType.I32;
  1000. }
  1001. break;
  1002. }
  1003. throw new ArgumentException(nameof(Node));
  1004. }
  1005. }
  1006. }