GlslDecompiler.cs 43 KB

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