GlslDecompiler.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  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 int MaxUboSize { get; }
  25. public GlslDecompiler(int MaxUboSize)
  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.Texb, GetTexbExpr },
  93. { ShaderIrInst.Texq, GetTexqExpr },
  94. { ShaderIrInst.Texs, GetTexsExpr },
  95. { ShaderIrInst.Trunc, GetTruncExpr },
  96. { ShaderIrInst.Txlf, GetTxlfExpr },
  97. { ShaderIrInst.Utof, GetUtofExpr },
  98. { ShaderIrInst.Xor, GetXorExpr }
  99. };
  100. this.MaxUboSize = MaxUboSize / 16;
  101. }
  102. public GlslProgram Decompile(
  103. IGalMemory Memory,
  104. long VpAPosition,
  105. long VpBPosition,
  106. GalShaderType ShaderType)
  107. {
  108. Header = new ShaderHeader(Memory, VpAPosition);
  109. HeaderB = new ShaderHeader(Memory, VpBPosition);
  110. Blocks = ShaderDecoder.Decode(Memory, VpAPosition);
  111. BlocksB = ShaderDecoder.Decode(Memory, VpBPosition);
  112. GlslDecl DeclVpA = new GlslDecl(Blocks, ShaderType, Header);
  113. GlslDecl DeclVpB = new GlslDecl(BlocksB, ShaderType, HeaderB);
  114. Decl = GlslDecl.Merge(DeclVpA, DeclVpB);
  115. return Decompile();
  116. }
  117. public GlslProgram Decompile(IGalMemory Memory, long Position, GalShaderType ShaderType)
  118. {
  119. Header = new ShaderHeader(Memory, Position);
  120. HeaderB = null;
  121. Blocks = ShaderDecoder.Decode(Memory, Position);
  122. BlocksB = null;
  123. Decl = new GlslDecl(Blocks, ShaderType, Header);
  124. return Decompile();
  125. }
  126. private GlslProgram Decompile()
  127. {
  128. SB = new StringBuilder();
  129. SB.AppendLine("#version 410 core");
  130. PrintDeclHeader();
  131. PrintDeclTextures();
  132. PrintDeclUniforms();
  133. PrintDeclAttributes();
  134. PrintDeclInAttributes();
  135. PrintDeclOutAttributes();
  136. PrintDeclGprs();
  137. PrintDeclPreds();
  138. PrintDeclSsy();
  139. if (BlocksB != null)
  140. {
  141. PrintBlockScope(Blocks, GlslDecl.BasicBlockAName);
  142. SB.AppendLine();
  143. PrintBlockScope(BlocksB, GlslDecl.BasicBlockBName);
  144. }
  145. else
  146. {
  147. PrintBlockScope(Blocks, GlslDecl.BasicBlockName);
  148. }
  149. SB.AppendLine();
  150. PrintMain();
  151. string GlslCode = SB.ToString();
  152. List<ShaderDeclInfo> TextureInfo = new List<ShaderDeclInfo>();
  153. TextureInfo.AddRange(Decl.Textures.Values);
  154. TextureInfo.AddRange(IterateCbTextures());
  155. return new GlslProgram(GlslCode, TextureInfo, Decl.Uniforms.Values);
  156. }
  157. private void PrintDeclHeader()
  158. {
  159. if (Decl.ShaderType == GalShaderType.Geometry)
  160. {
  161. int MaxVertices = Header.MaxOutputVertexCount;
  162. string OutputTopology;
  163. switch (Header.OutputTopology)
  164. {
  165. case ShaderHeader.PointList: OutputTopology = "points"; break;
  166. case ShaderHeader.LineStrip: OutputTopology = "line_strip"; break;
  167. case ShaderHeader.TriangleStrip: OutputTopology = "triangle_strip"; break;
  168. default: throw new InvalidOperationException();
  169. }
  170. SB.AppendLine("#extension GL_ARB_enhanced_layouts : require");
  171. SB.AppendLine();
  172. SB.AppendLine("// Stubbed. Maxwell geometry shaders don't inform input geometry type");
  173. SB.AppendLine("layout(triangles) in;" + Environment.NewLine);
  174. SB.AppendLine($"layout({OutputTopology}, max_vertices = {MaxVertices}) out;");
  175. SB.AppendLine();
  176. }
  177. }
  178. private void PrintDeclTextures()
  179. {
  180. foreach (ShaderDeclInfo DeclInfo in IterateCbTextures())
  181. {
  182. SB.AppendLine("uniform sampler2D " + DeclInfo.Name + ";");
  183. }
  184. PrintDecls(Decl.Textures, "uniform sampler2D");
  185. }
  186. private IEnumerable<ShaderDeclInfo> IterateCbTextures()
  187. {
  188. HashSet<string> Names = new HashSet<string>();
  189. foreach (ShaderDeclInfo DeclInfo in Decl.CbTextures.Values.OrderBy(DeclKeySelector))
  190. {
  191. if (Names.Add(DeclInfo.Name))
  192. {
  193. yield return DeclInfo;
  194. }
  195. }
  196. }
  197. private void PrintDeclUniforms()
  198. {
  199. if (Decl.ShaderType == GalShaderType.Vertex)
  200. {
  201. //Memory layout here is [flip_x, flip_y, instance, unused]
  202. //It's using 4 bytes, not 8
  203. SB.AppendLine("layout (std140) uniform " + GlslDecl.ExtraUniformBlockName + " {");
  204. SB.AppendLine(IdentationStr + "vec2 " + GlslDecl.FlipUniformName + ";");
  205. SB.AppendLine(IdentationStr + "int " + GlslDecl.InstanceUniformName + ";");
  206. SB.AppendLine("};");
  207. SB.AppendLine();
  208. }
  209. foreach (ShaderDeclInfo DeclInfo in Decl.Uniforms.Values.OrderBy(DeclKeySelector))
  210. {
  211. SB.AppendLine($"layout (std140) uniform {DeclInfo.Name} {{");
  212. SB.AppendLine($"{IdentationStr}vec4 {DeclInfo.Name}_data[{MaxUboSize}];");
  213. SB.AppendLine("};");
  214. }
  215. if (Decl.Uniforms.Count > 0)
  216. {
  217. SB.AppendLine();
  218. }
  219. }
  220. private void PrintDeclAttributes()
  221. {
  222. string GeometryArray = (Decl.ShaderType == GalShaderType.Geometry) ? "[" + MaxVertexInput + "]" : "";
  223. PrintDecls(Decl.Attributes, Suffix: GeometryArray);
  224. }
  225. private void PrintDeclInAttributes()
  226. {
  227. if (Decl.ShaderType == GalShaderType.Fragment)
  228. {
  229. SB.AppendLine("layout (location = " + GlslDecl.PositionOutAttrLocation + ") in vec4 " + GlslDecl.PositionOutAttrName + ";");
  230. }
  231. if (Decl.ShaderType == GalShaderType.Geometry)
  232. {
  233. if (Decl.InAttributes.Count > 0)
  234. {
  235. SB.AppendLine("in Vertex {");
  236. foreach (ShaderDeclInfo DeclInfo in Decl.InAttributes.Values.OrderBy(DeclKeySelector))
  237. {
  238. if (DeclInfo.Index >= 0)
  239. {
  240. SB.AppendLine(IdentationStr + "layout (location = " + DeclInfo.Index + ") vec4 " + DeclInfo.Name + "; ");
  241. }
  242. }
  243. SB.AppendLine("} block_in[];" + Environment.NewLine);
  244. }
  245. }
  246. else
  247. {
  248. PrintDeclAttributes(Decl.InAttributes.Values, "in");
  249. }
  250. }
  251. private void PrintDeclOutAttributes()
  252. {
  253. if (Decl.ShaderType == GalShaderType.Fragment)
  254. {
  255. int Count = 0;
  256. for (int Attachment = 0; Attachment < 8; Attachment++)
  257. {
  258. if (Header.OmapTargets[Attachment].Enabled)
  259. {
  260. SB.AppendLine("layout (location = " + Attachment + ") out vec4 " + GlslDecl.FragmentOutputName + Attachment + ";");
  261. Count++;
  262. }
  263. }
  264. if (Count > 0)
  265. {
  266. SB.AppendLine();
  267. }
  268. }
  269. else
  270. {
  271. SB.AppendLine("layout (location = " + GlslDecl.PositionOutAttrLocation + ") out vec4 " + GlslDecl.PositionOutAttrName + ";");
  272. SB.AppendLine();
  273. }
  274. PrintDeclAttributes(Decl.OutAttributes.Values, "out");
  275. }
  276. private void PrintDeclAttributes(IEnumerable<ShaderDeclInfo> Decls, string InOut)
  277. {
  278. int Count = 0;
  279. foreach (ShaderDeclInfo DeclInfo in Decls.OrderBy(DeclKeySelector))
  280. {
  281. if (DeclInfo.Index >= 0)
  282. {
  283. SB.AppendLine("layout (location = " + DeclInfo.Index + ") " + InOut + " vec4 " + DeclInfo.Name + ";");
  284. Count++;
  285. }
  286. }
  287. if (Count > 0)
  288. {
  289. SB.AppendLine();
  290. }
  291. }
  292. private void PrintDeclGprs()
  293. {
  294. PrintDecls(Decl.Gprs);
  295. PrintDecls(Decl.GprsHalf);
  296. }
  297. private void PrintDeclPreds()
  298. {
  299. PrintDecls(Decl.Preds, "bool");
  300. }
  301. private void PrintDeclSsy()
  302. {
  303. SB.AppendLine("uint " + GlslDecl.SsyCursorName + " = 0;");
  304. SB.AppendLine("uint " + GlslDecl.SsyStackName + "[" + GlslDecl.SsyStackSize + "];" + Environment.NewLine);
  305. }
  306. private void PrintDecls(IReadOnlyDictionary<int, ShaderDeclInfo> Dict, string CustomType = null, string Suffix = "")
  307. {
  308. foreach (ShaderDeclInfo DeclInfo in Dict.Values.OrderBy(DeclKeySelector))
  309. {
  310. string Name;
  311. if (CustomType != null)
  312. {
  313. Name = CustomType + " " + DeclInfo.Name + Suffix + ";";
  314. }
  315. else if (DeclInfo.Name.Contains(GlslDecl.FragmentOutputName))
  316. {
  317. Name = "layout (location = " + DeclInfo.Index / 4 + ") out vec4 " + DeclInfo.Name + Suffix + ";";
  318. }
  319. else
  320. {
  321. Name = GetDecl(DeclInfo) + Suffix + ";";
  322. }
  323. SB.AppendLine(Name);
  324. }
  325. if (Dict.Count > 0)
  326. {
  327. SB.AppendLine();
  328. }
  329. }
  330. private int DeclKeySelector(ShaderDeclInfo DeclInfo)
  331. {
  332. return DeclInfo.Cbuf << 24 | DeclInfo.Index;
  333. }
  334. private string GetDecl(ShaderDeclInfo DeclInfo)
  335. {
  336. if (DeclInfo.Size == 4)
  337. {
  338. return "vec4 " + DeclInfo.Name;
  339. }
  340. else
  341. {
  342. return "float " + DeclInfo.Name;
  343. }
  344. }
  345. private void PrintMain()
  346. {
  347. SB.AppendLine("void main() {");
  348. foreach (KeyValuePair<int, ShaderDeclInfo> KV in Decl.InAttributes)
  349. {
  350. if (!Decl.Attributes.TryGetValue(KV.Key, out ShaderDeclInfo Attr))
  351. {
  352. continue;
  353. }
  354. ShaderDeclInfo DeclInfo = KV.Value;
  355. if (Decl.ShaderType == GalShaderType.Geometry)
  356. {
  357. for (int Vertex = 0; Vertex < MaxVertexInput; Vertex++)
  358. {
  359. string Dst = Attr.Name + "[" + Vertex + "]";
  360. string Src = "block_in[" + Vertex + "]." + DeclInfo.Name;
  361. SB.AppendLine(IdentationStr + Dst + " = " + Src + ";");
  362. }
  363. }
  364. else
  365. {
  366. SB.AppendLine(IdentationStr + Attr.Name + " = " + DeclInfo.Name + ";");
  367. }
  368. }
  369. SB.AppendLine(IdentationStr + "uint pc;");
  370. if (BlocksB != null)
  371. {
  372. PrintProgram(Blocks, GlslDecl.BasicBlockAName);
  373. PrintProgram(BlocksB, GlslDecl.BasicBlockBName);
  374. }
  375. else
  376. {
  377. PrintProgram(Blocks, GlslDecl.BasicBlockName);
  378. }
  379. if (Decl.ShaderType != GalShaderType.Geometry)
  380. {
  381. PrintAttrToOutput();
  382. }
  383. if (Decl.ShaderType == GalShaderType.Fragment)
  384. {
  385. if (Header.OmapDepth)
  386. {
  387. SB.AppendLine(IdentationStr + "gl_FragDepth = " + GlslDecl.GetGprName(Header.DepthRegister) + ";");
  388. }
  389. int GprIndex = 0;
  390. for (int Attachment = 0; Attachment < 8; Attachment++)
  391. {
  392. string Output = GlslDecl.FragmentOutputName + Attachment;
  393. OmapTarget Target = Header.OmapTargets[Attachment];
  394. for (int Component = 0; Component < 4; Component++)
  395. {
  396. if (Target.ComponentEnabled(Component))
  397. {
  398. SB.AppendLine(IdentationStr + Output + "[" + Component + "] = " + GlslDecl.GetGprName(GprIndex) + ";");
  399. GprIndex++;
  400. }
  401. }
  402. }
  403. }
  404. SB.AppendLine("}");
  405. }
  406. private void PrintProgram(ShaderIrBlock[] Blocks, string Name)
  407. {
  408. const string Ident1 = IdentationStr;
  409. const string Ident2 = Ident1 + IdentationStr;
  410. const string Ident3 = Ident2 + IdentationStr;
  411. const string Ident4 = Ident3 + IdentationStr;
  412. SB.AppendLine(Ident1 + "pc = " + GetBlockPosition(Blocks[0]) + ";");
  413. SB.AppendLine(Ident1 + "do {");
  414. SB.AppendLine(Ident2 + "switch (pc) {");
  415. foreach (ShaderIrBlock Block in Blocks)
  416. {
  417. string FunctionName = Block.Position.ToString("x8");
  418. SB.AppendLine(Ident3 + "case 0x" + FunctionName + ": pc = " + Name + "_" + FunctionName + "(); break;");
  419. }
  420. SB.AppendLine(Ident3 + "default:");
  421. SB.AppendLine(Ident4 + "pc = 0;");
  422. SB.AppendLine(Ident4 + "break;");
  423. SB.AppendLine(Ident2 + "}");
  424. SB.AppendLine(Ident1 + "} while (pc != 0);");
  425. }
  426. private void PrintAttrToOutput(string Identation = IdentationStr)
  427. {
  428. foreach (KeyValuePair<int, ShaderDeclInfo> KV in Decl.OutAttributes)
  429. {
  430. if (!Decl.Attributes.TryGetValue(KV.Key, out ShaderDeclInfo Attr))
  431. {
  432. continue;
  433. }
  434. ShaderDeclInfo DeclInfo = KV.Value;
  435. string Name = Attr.Name;
  436. if (Decl.ShaderType == GalShaderType.Geometry)
  437. {
  438. Name += "[0]";
  439. }
  440. SB.AppendLine(Identation + DeclInfo.Name + " = " + Name + ";");
  441. }
  442. if (Decl.ShaderType == GalShaderType.Vertex)
  443. {
  444. SB.AppendLine(Identation + "gl_Position.xy *= " + GlslDecl.FlipUniformName + ";");
  445. }
  446. if (Decl.ShaderType != GalShaderType.Fragment)
  447. {
  448. SB.AppendLine(Identation + GlslDecl.PositionOutAttrName + " = gl_Position;");
  449. SB.AppendLine(Identation + GlslDecl.PositionOutAttrName + ".w = 1;");
  450. }
  451. }
  452. private void PrintBlockScope(ShaderIrBlock[] Blocks, string Name)
  453. {
  454. foreach (ShaderIrBlock Block in Blocks)
  455. {
  456. SB.AppendLine("uint " + Name + "_" + Block.Position.ToString("x8") + "() {");
  457. PrintNodes(Block, Block.GetNodes());
  458. SB.AppendLine("}" + Environment.NewLine);
  459. }
  460. }
  461. private void PrintNodes(ShaderIrBlock Block, ShaderIrNode[] Nodes)
  462. {
  463. foreach (ShaderIrNode Node in Nodes)
  464. {
  465. PrintNode(Block, Node, IdentationStr);
  466. }
  467. if (Nodes.Length == 0)
  468. {
  469. SB.AppendLine(IdentationStr + "return 0u;");
  470. return;
  471. }
  472. ShaderIrNode Last = Nodes[Nodes.Length - 1];
  473. bool UnconditionalFlowChange = false;
  474. if (Last is ShaderIrOp Op)
  475. {
  476. switch (Op.Inst)
  477. {
  478. case ShaderIrInst.Bra:
  479. case ShaderIrInst.Exit:
  480. case ShaderIrInst.Sync:
  481. UnconditionalFlowChange = true;
  482. break;
  483. }
  484. }
  485. if (!UnconditionalFlowChange)
  486. {
  487. if (Block.Next != null)
  488. {
  489. SB.AppendLine(IdentationStr + "return " + GetBlockPosition(Block.Next) + ";");
  490. }
  491. else
  492. {
  493. SB.AppendLine(IdentationStr + "return 0u;");
  494. }
  495. }
  496. }
  497. private void PrintNode(ShaderIrBlock Block, ShaderIrNode Node, string Identation)
  498. {
  499. if (Node is ShaderIrCond Cond)
  500. {
  501. string IfExpr = GetSrcExpr(Cond.Pred, true);
  502. if (Cond.Not)
  503. {
  504. IfExpr = "!(" + IfExpr + ")";
  505. }
  506. SB.AppendLine(Identation + "if (" + IfExpr + ") {");
  507. PrintNode(Block, Cond.Child, Identation + IdentationStr);
  508. SB.AppendLine(Identation + "}");
  509. }
  510. else if (Node is ShaderIrAsg Asg)
  511. {
  512. if (IsValidOutOper(Asg.Dst))
  513. {
  514. string Expr = GetSrcExpr(Asg.Src, true);
  515. Expr = GetExprWithCast(Asg.Dst, Asg.Src, Expr);
  516. SB.AppendLine(Identation + GetDstOperName(Asg.Dst) + " = " + Expr + ";");
  517. }
  518. }
  519. else if (Node is ShaderIrOp Op)
  520. {
  521. switch (Op.Inst)
  522. {
  523. case ShaderIrInst.Bra:
  524. {
  525. SB.AppendLine(Identation + "return " + GetBlockPosition(Block.Branch) + ";");
  526. break;
  527. }
  528. case ShaderIrInst.Emit:
  529. {
  530. PrintAttrToOutput(Identation);
  531. SB.AppendLine(Identation + "EmitVertex();");
  532. break;
  533. }
  534. case ShaderIrInst.Ssy:
  535. {
  536. string StackIndex = GlslDecl.SsyStackName + "[" + GlslDecl.SsyCursorName + "]";
  537. int TargetPosition = (Op.OperandA as ShaderIrOperImm).Value;
  538. string Target = "0x" + TargetPosition.ToString("x8") + "u";
  539. SB.AppendLine(Identation + StackIndex + " = " + Target + ";");
  540. SB.AppendLine(Identation + GlslDecl.SsyCursorName + "++;");
  541. break;
  542. }
  543. case ShaderIrInst.Sync:
  544. {
  545. SB.AppendLine(Identation + GlslDecl.SsyCursorName + "--;");
  546. string Target = GlslDecl.SsyStackName + "[" + GlslDecl.SsyCursorName + "]";
  547. SB.AppendLine(Identation + "return " + Target + ";");
  548. break;
  549. }
  550. default:
  551. SB.AppendLine(Identation + GetSrcExpr(Op, true) + ";");
  552. break;
  553. }
  554. }
  555. else if (Node is ShaderIrCmnt Cmnt)
  556. {
  557. SB.AppendLine(Identation + "// " + Cmnt.Comment);
  558. }
  559. else
  560. {
  561. throw new InvalidOperationException();
  562. }
  563. }
  564. private bool IsValidOutOper(ShaderIrNode Node)
  565. {
  566. if (Node is ShaderIrOperGpr Gpr && Gpr.IsConst)
  567. {
  568. return false;
  569. }
  570. else if (Node is ShaderIrOperPred Pred && Pred.IsConst)
  571. {
  572. return false;
  573. }
  574. return true;
  575. }
  576. private string GetDstOperName(ShaderIrNode Node)
  577. {
  578. if (Node is ShaderIrOperAbuf Abuf)
  579. {
  580. return GetOutAbufName(Abuf);
  581. }
  582. else if (Node is ShaderIrOperGpr Gpr)
  583. {
  584. return GetName(Gpr);
  585. }
  586. else if (Node is ShaderIrOperPred Pred)
  587. {
  588. return GetName(Pred);
  589. }
  590. throw new ArgumentException(nameof(Node));
  591. }
  592. private string GetSrcExpr(ShaderIrNode Node, bool Entry = false)
  593. {
  594. switch (Node)
  595. {
  596. case ShaderIrOperAbuf Abuf: return GetName (Abuf);
  597. case ShaderIrOperCbuf Cbuf: return GetName (Cbuf);
  598. case ShaderIrOperGpr Gpr: return GetName (Gpr);
  599. case ShaderIrOperImm Imm: return GetValue(Imm);
  600. case ShaderIrOperImmf Immf: return GetValue(Immf);
  601. case ShaderIrOperPred Pred: return GetName (Pred);
  602. case ShaderIrOp Op:
  603. string Expr;
  604. if (InstsExpr.TryGetValue(Op.Inst, out GetInstExpr GetExpr))
  605. {
  606. Expr = GetExpr(Op);
  607. }
  608. else
  609. {
  610. throw new NotImplementedException(Op.Inst.ToString());
  611. }
  612. if (!Entry && NeedsParentheses(Op))
  613. {
  614. Expr = "(" + Expr + ")";
  615. }
  616. return Expr;
  617. default: throw new ArgumentException(nameof(Node));
  618. }
  619. }
  620. private static bool NeedsParentheses(ShaderIrOp Op)
  621. {
  622. switch (Op.Inst)
  623. {
  624. case ShaderIrInst.Ipa:
  625. case ShaderIrInst.Texq:
  626. case ShaderIrInst.Texs:
  627. case ShaderIrInst.Txlf:
  628. return false;
  629. }
  630. return true;
  631. }
  632. private string GetName(ShaderIrOperCbuf Cbuf)
  633. {
  634. if (!Decl.Uniforms.TryGetValue(Cbuf.Index, out ShaderDeclInfo DeclInfo))
  635. {
  636. throw new InvalidOperationException();
  637. }
  638. if (Cbuf.Offs != null)
  639. {
  640. string Offset = "floatBitsToInt(" + GetSrcExpr(Cbuf.Offs) + ")";
  641. string Index = "(" + Cbuf.Pos * 4 + " + " + Offset + ")";
  642. return $"{DeclInfo.Name}_data[{Index} / 16][({Index} / 4) % 4]";
  643. }
  644. else
  645. {
  646. return $"{DeclInfo.Name}_data[{Cbuf.Pos / 4}][{Cbuf.Pos % 4}]";
  647. }
  648. }
  649. private string GetOutAbufName(ShaderIrOperAbuf Abuf)
  650. {
  651. if (Decl.ShaderType == GalShaderType.Geometry)
  652. {
  653. switch (Abuf.Offs)
  654. {
  655. case GlslDecl.LayerAttr: return "gl_Layer";
  656. }
  657. }
  658. return GetAttrTempName(Abuf);
  659. }
  660. private string GetName(ShaderIrOperAbuf Abuf)
  661. {
  662. //Handle special scalar read-only attributes here.
  663. if (Decl.ShaderType == GalShaderType.Vertex)
  664. {
  665. switch (Abuf.Offs)
  666. {
  667. case GlslDecl.VertexIdAttr: return "gl_VertexID";
  668. case GlslDecl.InstanceIdAttr: return GlslDecl.InstanceUniformName;
  669. }
  670. }
  671. else if (Decl.ShaderType == GalShaderType.TessEvaluation)
  672. {
  673. switch (Abuf.Offs)
  674. {
  675. case GlslDecl.TessCoordAttrX: return "gl_TessCoord.x";
  676. case GlslDecl.TessCoordAttrY: return "gl_TessCoord.y";
  677. case GlslDecl.TessCoordAttrZ: return "gl_TessCoord.z";
  678. }
  679. }
  680. else if (Decl.ShaderType == GalShaderType.Fragment)
  681. {
  682. switch (Abuf.Offs)
  683. {
  684. case GlslDecl.PointCoordAttrX: return "gl_PointCoord.x";
  685. case GlslDecl.PointCoordAttrY: return "gl_PointCoord.y";
  686. case GlslDecl.FaceAttr: return "(gl_FrontFacing ? -1 : 0)";
  687. }
  688. }
  689. return GetAttrTempName(Abuf);
  690. }
  691. private string GetAttrTempName(ShaderIrOperAbuf Abuf)
  692. {
  693. int Index = Abuf.Offs >> 4;
  694. int Elem = (Abuf.Offs >> 2) & 3;
  695. string Swizzle = "." + GetAttrSwizzle(Elem);
  696. if (!Decl.Attributes.TryGetValue(Index, out ShaderDeclInfo DeclInfo))
  697. {
  698. //Handle special vec4 attributes here
  699. //(for example, index 7 is always gl_Position).
  700. if (Index == GlslDecl.GlPositionVec4Index)
  701. {
  702. string Name =
  703. Decl.ShaderType != GalShaderType.Vertex &&
  704. Decl.ShaderType != GalShaderType.Geometry ? GlslDecl.PositionOutAttrName : "gl_Position";
  705. return Name + Swizzle;
  706. }
  707. else if (Abuf.Offs == GlslDecl.PointSizeAttr)
  708. {
  709. return "gl_PointSize";
  710. }
  711. }
  712. if (DeclInfo.Index >= 32)
  713. {
  714. throw new InvalidOperationException($"Shader attribute offset {Abuf.Offs} is invalid.");
  715. }
  716. if (Decl.ShaderType == GalShaderType.Geometry)
  717. {
  718. string Vertex = "floatBitsToInt(" + GetSrcExpr(Abuf.Vertex) + ")";
  719. return DeclInfo.Name + "[" + Vertex + "]" + Swizzle;
  720. }
  721. else
  722. {
  723. return DeclInfo.Name + Swizzle;
  724. }
  725. }
  726. private string GetName(ShaderIrOperGpr Gpr)
  727. {
  728. if (Gpr.IsConst)
  729. {
  730. return "0";
  731. }
  732. if (Gpr.RegisterSize == ShaderRegisterSize.Single)
  733. {
  734. return GetNameWithSwizzle(Decl.Gprs, Gpr.Index);
  735. }
  736. else if (Gpr.RegisterSize == ShaderRegisterSize.Half)
  737. {
  738. return GetNameWithSwizzle(Decl.GprsHalf, (Gpr.Index << 1) | Gpr.HalfPart);
  739. }
  740. else /* if (Gpr.RegisterSize == ShaderRegisterSize.Double) */
  741. {
  742. throw new NotImplementedException("Double types are not supported.");
  743. }
  744. }
  745. private string GetValue(ShaderIrOperImm Imm)
  746. {
  747. //Only use hex is the value is too big and would likely be hard to read as int.
  748. if (Imm.Value > 0xfff ||
  749. Imm.Value < -0xfff)
  750. {
  751. return "0x" + Imm.Value.ToString("x8", CultureInfo.InvariantCulture);
  752. }
  753. else
  754. {
  755. return GetIntConst(Imm.Value);
  756. }
  757. }
  758. private string GetValue(ShaderIrOperImmf Immf)
  759. {
  760. return GetFloatConst(Immf.Value);
  761. }
  762. private string GetName(ShaderIrOperPred Pred)
  763. {
  764. return Pred.IsConst ? "true" : GetNameWithSwizzle(Decl.Preds, Pred.Index);
  765. }
  766. private string GetNameWithSwizzle(IReadOnlyDictionary<int, ShaderDeclInfo> Dict, int Index)
  767. {
  768. int VecIndex = Index & ~3;
  769. if (Dict.TryGetValue(VecIndex, out ShaderDeclInfo DeclInfo))
  770. {
  771. if (DeclInfo.Size > 1 && Index < VecIndex + DeclInfo.Size)
  772. {
  773. return DeclInfo.Name + "." + GetAttrSwizzle(Index & 3);
  774. }
  775. }
  776. if (!Dict.TryGetValue(Index, out DeclInfo))
  777. {
  778. throw new InvalidOperationException();
  779. }
  780. return DeclInfo.Name;
  781. }
  782. private string GetAttrSwizzle(int Elem)
  783. {
  784. return "xyzw".Substring(Elem, 1);
  785. }
  786. private string GetAbsExpr(ShaderIrOp Op) => GetUnaryCall(Op, "abs");
  787. private string GetAddExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "+");
  788. private string GetAndExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "&");
  789. private string GetAsrExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">>");
  790. private string GetBandExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "&&");
  791. private string GetBnotExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "!");
  792. private string GetBorExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "||");
  793. private string GetBxorExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "^^");
  794. private string GetCeilExpr(ShaderIrOp Op) => GetUnaryCall(Op, "ceil");
  795. private string GetClampsExpr(ShaderIrOp Op)
  796. {
  797. return "clamp(" + GetOperExpr(Op, Op.OperandA) + ", " +
  798. GetOperExpr(Op, Op.OperandB) + ", " +
  799. GetOperExpr(Op, Op.OperandC) + ")";
  800. }
  801. private string GetClampuExpr(ShaderIrOp Op)
  802. {
  803. return "int(clamp(uint(" + GetOperExpr(Op, Op.OperandA) + "), " +
  804. "uint(" + GetOperExpr(Op, Op.OperandB) + "), " +
  805. "uint(" + GetOperExpr(Op, Op.OperandC) + ")))";
  806. }
  807. private string GetCeqExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "==");
  808. private string GetCequExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "==");
  809. private string GetCgeExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">=");
  810. private string GetCgeuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, ">=");
  811. private string GetCgtExpr(ShaderIrOp Op) => GetBinaryExpr(Op, ">");
  812. private string GetCgtuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, ">");
  813. private string GetCleExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "<=");
  814. private string GetCleuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "<=");
  815. private string GetCltExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "<");
  816. private string GetCltuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "<");
  817. private string GetCnanExpr(ShaderIrOp Op) => GetUnaryCall(Op, "isnan");
  818. private string GetCneExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "!=");
  819. private string GetCutExpr(ShaderIrOp Op) => "EndPrimitive()";
  820. private string GetCneuExpr(ShaderIrOp Op) => GetBinaryExprWithNaN(Op, "!=");
  821. private string GetCnumExpr(ShaderIrOp Op) => GetUnaryCall(Op, "!isnan");
  822. private string GetExitExpr(ShaderIrOp Op) => "return 0u";
  823. private string GetFcosExpr(ShaderIrOp Op) => GetUnaryCall(Op, "cos");
  824. private string GetFex2Expr(ShaderIrOp Op) => GetUnaryCall(Op, "exp2");
  825. private string GetFfmaExpr(ShaderIrOp Op) => GetTernaryExpr(Op, "*", "+");
  826. private string GetFclampExpr(ShaderIrOp Op) => GetTernaryCall(Op, "clamp");
  827. private string GetFlg2Expr(ShaderIrOp Op) => GetUnaryCall(Op, "log2");
  828. private string GetFloorExpr(ShaderIrOp Op) => GetUnaryCall(Op, "floor");
  829. private string GetFrcpExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "1 / ");
  830. private string GetFrsqExpr(ShaderIrOp Op) => GetUnaryCall(Op, "inversesqrt");
  831. private string GetFsinExpr(ShaderIrOp Op) => GetUnaryCall(Op, "sin");
  832. private string GetFsqrtExpr(ShaderIrOp Op) => GetUnaryCall(Op, "sqrt");
  833. private string GetFtosExpr(ShaderIrOp Op)
  834. {
  835. return "int(" + GetOperExpr(Op, Op.OperandA) + ")";
  836. }
  837. private string GetFtouExpr(ShaderIrOp Op)
  838. {
  839. return "int(uint(" + GetOperExpr(Op, Op.OperandA) + "))";
  840. }
  841. private string GetIpaExpr(ShaderIrOp Op)
  842. {
  843. ShaderIrMetaIpa Meta = (ShaderIrMetaIpa)Op.MetaData;
  844. ShaderIrOperAbuf Abuf = (ShaderIrOperAbuf)Op.OperandA;
  845. if (Meta.Mode == ShaderIpaMode.Pass)
  846. {
  847. int Index = Abuf.Offs >> 4;
  848. int Elem = (Abuf.Offs >> 2) & 3;
  849. if (Decl.ShaderType == GalShaderType.Fragment && Index == GlslDecl.GlPositionVec4Index)
  850. {
  851. switch (Elem)
  852. {
  853. case 0: return "gl_FragCoord.x";
  854. case 1: return "gl_FragCoord.y";
  855. case 2: return "gl_FragCoord.z";
  856. case 3: return "1";
  857. }
  858. }
  859. }
  860. return GetSrcExpr(Op.OperandA);
  861. }
  862. private string GetKilExpr(ShaderIrOp Op) => "discard";
  863. private string GetLslExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "<<");
  864. private string GetLsrExpr(ShaderIrOp Op)
  865. {
  866. return "int(uint(" + GetOperExpr(Op, Op.OperandA) + ") >> " +
  867. GetOperExpr(Op, Op.OperandB) + ")";
  868. }
  869. private string GetMaxExpr(ShaderIrOp Op) => GetBinaryCall(Op, "max");
  870. private string GetMinExpr(ShaderIrOp Op) => GetBinaryCall(Op, "min");
  871. private string GetMulExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "*");
  872. private string GetNegExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "-");
  873. private string GetNotExpr(ShaderIrOp Op) => GetUnaryExpr(Op, "~");
  874. private string GetOrExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "|");
  875. private string GetStofExpr(ShaderIrOp Op)
  876. {
  877. return "float(" + GetOperExpr(Op, Op.OperandA) + ")";
  878. }
  879. private string GetSubExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "-");
  880. private string GetTexbExpr(ShaderIrOp Op)
  881. {
  882. ShaderIrMetaTex Meta = (ShaderIrMetaTex)Op.MetaData;
  883. if (!Decl.CbTextures.TryGetValue(Op, out ShaderDeclInfo DeclInfo))
  884. {
  885. throw new InvalidOperationException();
  886. }
  887. string Coords = GetTexSamplerCoords(Op);
  888. string Ch = "rgba".Substring(Meta.Elem, 1);
  889. return "texture(" + DeclInfo.Name + ", " + Coords + ")." + Ch;
  890. }
  891. private string GetTexqExpr(ShaderIrOp Op)
  892. {
  893. ShaderIrMetaTexq Meta = (ShaderIrMetaTexq)Op.MetaData;
  894. string Ch = "xyzw".Substring(Meta.Elem, 1);
  895. if (Meta.Info == ShaderTexqInfo.Dimension)
  896. {
  897. string Sampler = GetTexSamplerName(Op);
  898. string Lod = GetOperExpr(Op, Op.OperandA); //???
  899. return "textureSize(" + Sampler + ", " + Lod + ")." + Ch;
  900. }
  901. else
  902. {
  903. throw new NotImplementedException(Meta.Info.ToString());
  904. }
  905. }
  906. private string GetTexsExpr(ShaderIrOp Op)
  907. {
  908. ShaderIrMetaTex Meta = (ShaderIrMetaTex)Op.MetaData;
  909. string Sampler = GetTexSamplerName(Op);
  910. string Coords = GetTexSamplerCoords(Op);
  911. string Ch = "rgba".Substring(Meta.Elem, 1);
  912. return "texture(" + Sampler + ", " + Coords + ")." + Ch;
  913. }
  914. private string GetTxlfExpr(ShaderIrOp Op)
  915. {
  916. ShaderIrMetaTex Meta = (ShaderIrMetaTex)Op.MetaData;
  917. string Sampler = GetTexSamplerName(Op);
  918. string Coords = GetITexSamplerCoords(Op);
  919. string Ch = "rgba".Substring(Meta.Elem, 1);
  920. return "texelFetch(" + Sampler + ", " + Coords + ", 0)." + Ch;
  921. }
  922. private string GetTruncExpr(ShaderIrOp Op) => GetUnaryCall(Op, "trunc");
  923. private string GetUtofExpr(ShaderIrOp Op)
  924. {
  925. return "float(uint(" + GetOperExpr(Op, Op.OperandA) + "))";
  926. }
  927. private string GetXorExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "^");
  928. private string GetUnaryCall(ShaderIrOp Op, string FuncName)
  929. {
  930. return FuncName + "(" + GetOperExpr(Op, Op.OperandA) + ")";
  931. }
  932. private string GetBinaryCall(ShaderIrOp Op, string FuncName)
  933. {
  934. return FuncName + "(" + GetOperExpr(Op, Op.OperandA) + ", " +
  935. GetOperExpr(Op, Op.OperandB) + ")";
  936. }
  937. private string GetTernaryCall(ShaderIrOp Op, string FuncName)
  938. {
  939. return FuncName + "(" + GetOperExpr(Op, Op.OperandA) + ", " +
  940. GetOperExpr(Op, Op.OperandB) + ", " +
  941. GetOperExpr(Op, Op.OperandC) + ")";
  942. }
  943. private string GetUnaryExpr(ShaderIrOp Op, string Opr)
  944. {
  945. return Opr + GetOperExpr(Op, Op.OperandA);
  946. }
  947. private string GetBinaryExpr(ShaderIrOp Op, string Opr)
  948. {
  949. return GetOperExpr(Op, Op.OperandA) + " " + Opr + " " +
  950. GetOperExpr(Op, Op.OperandB);
  951. }
  952. private string GetBinaryExprWithNaN(ShaderIrOp Op, string Opr)
  953. {
  954. string A = GetOperExpr(Op, Op.OperandA);
  955. string B = GetOperExpr(Op, Op.OperandB);
  956. string NaNCheck =
  957. " || isnan(" + A + ")" +
  958. " || isnan(" + B + ")";
  959. return A + " " + Opr + " " + B + NaNCheck;
  960. }
  961. private string GetTernaryExpr(ShaderIrOp Op, string Opr1, string Opr2)
  962. {
  963. return GetOperExpr(Op, Op.OperandA) + " " + Opr1 + " " +
  964. GetOperExpr(Op, Op.OperandB) + " " + Opr2 + " " +
  965. GetOperExpr(Op, Op.OperandC);
  966. }
  967. private string GetTexSamplerName(ShaderIrOp Op)
  968. {
  969. ShaderIrOperImm Node = (ShaderIrOperImm)Op.OperandC;
  970. int Handle = ((ShaderIrOperImm)Op.OperandC).Value;
  971. if (!Decl.Textures.TryGetValue(Handle, out ShaderDeclInfo DeclInfo))
  972. {
  973. throw new InvalidOperationException();
  974. }
  975. return DeclInfo.Name;
  976. }
  977. private string GetTexSamplerCoords(ShaderIrOp Op)
  978. {
  979. return "vec2(" + GetOperExpr(Op, Op.OperandA) + ", " +
  980. GetOperExpr(Op, Op.OperandB) + ")";
  981. }
  982. private string GetITexSamplerCoords(ShaderIrOp Op)
  983. {
  984. return "ivec2(" + GetOperExpr(Op, Op.OperandA) + ", " +
  985. GetOperExpr(Op, Op.OperandB) + ")";
  986. }
  987. private string GetOperExpr(ShaderIrOp Op, ShaderIrNode Oper)
  988. {
  989. return GetExprWithCast(Op, Oper, GetSrcExpr(Oper));
  990. }
  991. private static string GetExprWithCast(ShaderIrNode Dst, ShaderIrNode Src, string Expr)
  992. {
  993. //Note: The "DstType" (of the cast) is the type that the operation
  994. //uses on the source operands, while the "SrcType" is the destination
  995. //type of the operand result (if it is a operation) or just the type
  996. //of the variable for registers/uniforms/attributes.
  997. OperType DstType = GetSrcNodeType(Dst);
  998. OperType SrcType = GetDstNodeType(Src);
  999. if (DstType != SrcType)
  1000. {
  1001. //Check for invalid casts
  1002. //(like bool to int/float and others).
  1003. if (SrcType != OperType.F32 &&
  1004. SrcType != OperType.I32)
  1005. {
  1006. throw new InvalidOperationException();
  1007. }
  1008. switch (Src)
  1009. {
  1010. case ShaderIrOperGpr Gpr:
  1011. {
  1012. //When the Gpr is ZR, just return the 0 value directly,
  1013. //since the float encoding for 0 is 0.
  1014. if (Gpr.IsConst)
  1015. {
  1016. return "0";
  1017. }
  1018. break;
  1019. }
  1020. case ShaderIrOperImm Imm:
  1021. {
  1022. //For integer immediates being used as float,
  1023. //it's better (for readability) to just return the float value.
  1024. if (DstType == OperType.F32)
  1025. {
  1026. float Value = BitConverter.Int32BitsToSingle(Imm.Value);
  1027. if (!float.IsNaN(Value) && !float.IsInfinity(Value))
  1028. {
  1029. return GetFloatConst(Value);
  1030. }
  1031. }
  1032. break;
  1033. }
  1034. }
  1035. switch (DstType)
  1036. {
  1037. case OperType.F32: Expr = "intBitsToFloat(" + Expr + ")"; break;
  1038. case OperType.I32: Expr = "floatBitsToInt(" + Expr + ")"; break;
  1039. }
  1040. }
  1041. return Expr;
  1042. }
  1043. private static string GetIntConst(int Value)
  1044. {
  1045. string Expr = Value.ToString(CultureInfo.InvariantCulture);
  1046. return Value < 0 ? "(" + Expr + ")" : Expr;
  1047. }
  1048. private static string GetFloatConst(float Value)
  1049. {
  1050. string Expr = Value.ToString(CultureInfo.InvariantCulture);
  1051. return Value < 0 ? "(" + Expr + ")" : Expr;
  1052. }
  1053. private static OperType GetDstNodeType(ShaderIrNode Node)
  1054. {
  1055. //Special case instructions with the result type different
  1056. //from the input types (like integer <-> float conversion) here.
  1057. if (Node is ShaderIrOp Op)
  1058. {
  1059. switch (Op.Inst)
  1060. {
  1061. case ShaderIrInst.Stof:
  1062. case ShaderIrInst.Txlf:
  1063. case ShaderIrInst.Utof:
  1064. return OperType.F32;
  1065. case ShaderIrInst.Ftos:
  1066. case ShaderIrInst.Ftou:
  1067. return OperType.I32;
  1068. }
  1069. }
  1070. return GetSrcNodeType(Node);
  1071. }
  1072. private static OperType GetSrcNodeType(ShaderIrNode Node)
  1073. {
  1074. switch (Node)
  1075. {
  1076. case ShaderIrOperAbuf Abuf:
  1077. return Abuf.Offs == GlslDecl.LayerAttr ||
  1078. Abuf.Offs == GlslDecl.InstanceIdAttr ||
  1079. Abuf.Offs == GlslDecl.VertexIdAttr ||
  1080. Abuf.Offs == GlslDecl.FaceAttr
  1081. ? OperType.I32
  1082. : OperType.F32;
  1083. case ShaderIrOperCbuf Cbuf: return OperType.F32;
  1084. case ShaderIrOperGpr Gpr: return OperType.F32;
  1085. case ShaderIrOperImm Imm: return OperType.I32;
  1086. case ShaderIrOperImmf Immf: return OperType.F32;
  1087. case ShaderIrOperPred Pred: return OperType.Bool;
  1088. case ShaderIrOp Op:
  1089. if (Op.Inst > ShaderIrInst.B_Start &&
  1090. Op.Inst < ShaderIrInst.B_End)
  1091. {
  1092. return OperType.Bool;
  1093. }
  1094. else if (Op.Inst > ShaderIrInst.F_Start &&
  1095. Op.Inst < ShaderIrInst.F_End)
  1096. {
  1097. return OperType.F32;
  1098. }
  1099. else if (Op.Inst > ShaderIrInst.I_Start &&
  1100. Op.Inst < ShaderIrInst.I_End)
  1101. {
  1102. return OperType.I32;
  1103. }
  1104. break;
  1105. }
  1106. throw new ArgumentException(nameof(Node));
  1107. }
  1108. private static string GetBlockPosition(ShaderIrBlock Block)
  1109. {
  1110. if (Block != null)
  1111. {
  1112. return "0x" + Block.Position.ToString("x8") + "u";
  1113. }
  1114. else
  1115. {
  1116. return "0u";
  1117. }
  1118. }
  1119. }
  1120. }